Odyssey
DirMaint.prg
1 <?php
2 
3 $sharedLibrary= dirname(__FILE__) . "/../../shared/library";
4 
5 require_once("$sharedLibrary/hcuEnv.i");
6 
7 $admVars = array();
8 $admOk = array(
9  "action" => array("filter" => FILTER_SANITIZE_STRING),
10  "ndir" => array("filter" => FILTER_SANITIZE_STRING),
11  "mdir" => array("filter" => FILTER_SANITIZE_STRING),
12  "csub" => array("filter" => FILTER_SANITIZE_STRING),
13  "newfolder" => array("filter" => FILTER_SANITIZE_STRING),
14  "delete" => array("filter" => FILTER_SANITIZE_STRING)
15 );
16 HCU_ImportVars($admVars, "DIRMAINT", $admOk);
17 
18 $dir_search = true;
19 $top_upload = false; // Allow file upload for the top directory
20 $top_dir="$home_path/public_html"; // Top Most Directory
21 
22 // Create two arrays - for files and directories
23 // these are the viewable files in the top directory
24 $allowed_dir = array();
25 $allowed_files = array();
26 
27 // Settings for Subdirectory Control
28 $allow_subdir = true; // This will control if the user may make subdirectories under the main directories (ie. board employees)
29 $max_subdir = 15; // This is the maximum number of directories allowed in any one directory
30 $allow_multi_subdir = false; // This is intended to control creation of subdirectories 2 directories and further below board/employees
31 
32 // success and error messages
33 $msg_err = "";
34 $msg_suc = "";
35 
36 // Setup the directories allowed
37 // Add any directories here to include in the html list if they exist
38 $allowed_dir[] = "employees";
39 $allowed_dir[] = "board";
40 $allowed_dir[] = "vendors"; // Add a new directory to the listing mws 6/3/2002
41 // 6/26/2002 - as requested by Jan - the new folders are: alm, supervisory, and misc
42 $allowed_dir[] = "alm";
43 $allowed_dir[] = "supervisory";
44 $allowed_dir[] = "misc";
45 $allowed_dir[] = "disaster";
46 $allowed_dir[] = "images"; // Adding images directory 6/2019
47 
48 $csubValue = HCU_array_key_value("csub", $admVars['DIRMAINT']);
49 $csub = $csubValue !== false ? $csubValue : "";
50 
51 $newfolderValue = HCU_array_key_value("newfolder", $admVars['DIRMAINT']);
52 $newfolder = $newfolderValue !== false ? $newfolderValue : "";
53 
54 $mdirValue = HCU_array_key_value("mdir", $admVars['DIRMAINT']);
55 $mdir = $mdirValue !== false ? $mdirValue : "";
56 
57 $ndirValue = HCU_array_key_value("ndir", $admVars['DIRMAINT']);
58 $ndir = $ndirValue !== false ? $ndirValue : "";
59 
60 $csubValue = HCU_array_key_value("csub", $admVars['DIRMAINT']);
61 $csub = $csubValue !== false ? $csubValue : "";
62 
63 if (strlen($ndir) > 0 && strlen($csub) >= 0 && $dir_search) {
64 
65  if ($ndir == "..") {
66  // Move up the directory -- Need to remove the last directory from the csub piece, this will also be the ndir variable
67  $tdir = $csub; // Setup a temporary variable for the full directory path
68  if (strpos(strrev($tdir), "/") !== false) {
69  $cdir = substr($tdir, 0, strlen($tdir) - strpos(strrev($tdir), "/") - 1);
70  $cpath = $top_dir .
71  ($cdir == '' ? "" : "/" . $cdir);
72  } else {
73  $cdir = '';
74  $cpath = $top_dir;
75  }
76  } else {
77  $cdir = $ndir;
78  $cpath = $top_dir .
79  ($csub == '' ? "" : "/" . $csub) .
80  ($ndir == '' ? "" : "/" . $ndir) ;
81  }
82 } elseif (isset($csub) == true) {
83  $cpath = $top_dir . ($csub == '' ? "" : "/" . $csub);
84  $cdir = substr(strrchr($csub, "/"), 1);
85 } else {
86 // Place code here so if any of these variable are NOT set, then we start over on the rotation
87 // If dir_search is false then make sure to reset all folders
88  $cdir = "";
89  $csub = "";
90  $cpath = "$home_path/public_html";
91 
92 }
93 
94 // mws 5/7/2002 - Add code here, that if we are IN the employees or board directory
95 // we must look for the password file. If NOT found the open it and save it
96 if (in_array($cdir, $allowed_dir)) {
97  // We are in ONE of these directories, create the search string for the
98  // password file
99  $pwd_file="$home_path/admin/$cdir"; // Password File - Depends on where it is called from
100  if (!file_exists($pwd_file)) {
101  // We have searched for the file, and it does NOT exist -- now we must create it
102  $fp = fopen($pwd_file, "w");
103  fclose($fp); // Now close the file
104  }
105 }
106 
107 // Create the directory name that is on top of the top_dir
108 // So if the current directory is /home/xyz/public_html/employees/sample
109 // This should return employees/sample
110 if (strpos($cpath, $top_dir) !== false) {
111  $dir_pos = strpos($cpath, $top_dir);
112  $cspath = substr($cpath, $dir_pos + 1 + strlen($top_dir)); // This should be the just the path information past the top_dir
113 } else {
114  $dir_pos = strpos($cpath, $top_dir);
115  $cspath = '';
116 }
117 
118 $action = (empty($action) ? "none" : $action);
119 $action = strtolower($action);
120 
121 switch ($action) {
122  case "delete":
123  delete_files();
124  print_status();
125  break;
126  case "view":
127  view_form();
128  break;
129  case "upload":
130  // Post all information on the form, then reload the print_status screen
131  post_form();
132  print_status();
133  break;
134  case "createsub":
135  create_subdir();
136  print_status();
137  break;
138  case "create":
139  create_dir();
140  print_status();
141  break;
142  default:
143  print_status();
144  break;
145  }
146 
147 /* ------------------------------------- */
148  function print_directory_list() {
149  global $cpath, $top_dir, $dir_search, $allowed_files, $allowed_dir, $allow_subdir, $max_subdir, $self, $cspath, $chome, $ndir;
150 
151  $dirlistReturn = array(
152  "active" => array(),
153  "activeTitle" => "Active Directories",
154  "inactive" => array(),
155  );
156 
157  $url = GetEnvSetting('TICKET_DOMAIN') ? GetEnvSetting('TICKET_DOMAIN') : 'localhost:8000';
158  $httpprotocol = GetEnvSetting('REQUIRE_ENCRYPTION') ? 'https' : 'http';
159  $finalurl = $httpprotocol . '://' . $url;
160 
161  $dirlist = array();
162  $filelist = array();
163  if ($fhandle = @opendir($cpath)) {
164  while (false !== ($file = readdir($fhandle))) {
165  if (($file == "..") && ($dir_search == true)) {
166  // This is the parent directory
167  // Make sure we will let them see this, it must not meet the top most directory
168  // Only let them go as High as top_dir
169  // Only include if the top_dir is located in the current path
170  // also do NOT show the parent if the current directory is the same as the top directory
171  if ((strpos($cpath, $top_dir) !== false) && ($cpath != $top_dir)) {
172  $dirlist[count($dirlist)] = $file;
173  // ONLY include IF the current directory and topmost path do NOT equal the current path
174  // This is an attempt to NOT allow directory viewing further up the tree then allowed
175  }
176  } elseif (($cpath == $top_dir)) {
177  // If the current path equals the top directory then ONLY show the
178  if (is_file($cpath . "/" . $file)) {
179  // Now check to see if the file is a viewable file
180  if (in_array($file, $allowed_files))
181  $filelist[count($filelist)] = $file;
182  } elseif ($dir_search == true) {
183  // Check to see if the directory is viewable
184  if (in_array($file, $allowed_dir))
185  $dirlist[count($dirlist)] = $file;
186  }
187  // Don't show any hidden files, which start with a "."
188  } elseif (($file != ".") && ($file != "..") && (substr($file, 0, 1) != ".")) {
189  // Don't inlcude the current directory information
190  // Now check to see if this is a directory or file
191  if (is_file($cpath . "/" . $file))
192  $filelist[count($filelist)] = $file;
193  elseif($dir_search == true)
194  $dirlist[count($dirlist)] = $file;
195  }
196  }
197  } else {
198  //echo "Unable to Open";
199  }
200 
201  // BUILD USER LINK
202  // password maintenance not needed for images folder
203  if (in_array($cspath, $allowed_dir) && $cspath !== 'images') {
204  $maintDirName = ucfirst($cspath) . " Password Maintenance";
205  $dirlistReturn['active'][] = array(
206  "icon" => "fa-key",
207  "name" => "$maintDirName",
208  "link" => "main.prg?ft=26&tdir=$cspath",
209  "mod" => "",
210  "delete" => false,
211  "new" => false
212  );
213  }
214 
215  // SORT and BUILD DIRECTORY LIST
216  $dirlist_count = count($dirlist);
217  if ($dirlist_count > 0) {
218  // -- Sort the directories before printing them
219  array_multisort ($dirlist, SORT_ASC, SORT_STRING);
220  reset($dirlist);
221  foreach ($dirlist as $shortname) {
222  $ftime=date("m/d/y g:i:s A",filemtime("$cpath/$shortname"));
223  if ($shortname == "..") {
224  $dirlistReturn["active"][] = array(
225  "icon" => "fa-arrow-left",
226  "name" => "Parent Folder",
227  "link" => "$self&ndir=$shortname&csub=$cspath",
228  "mod" => "$ftime",
229  "delete" => false,
230  "new" => false
231  );
232 
233  // Add first crumb out side
234  $crumbArray = explode("/", $cspath);
235  $crumbSlice = array_slice($crumbArray, 0, 1);
236  $crumbString = implode("/", $crumbSlice);
237 
238  $dirlistReturn['activeTitle'] = "<a href=\"$self&ndir=$shortname&csub=$crumbString\">Active Directories</a>";
239 
240  // Each successive breadcrumb link must include the next crumb in the exploded
241  // array. So we slice the array from index 0 to i+1 length and implode it with slashes.
242  foreach ($crumbArray as $key => $value) {
243  $crumbSlice = array_slice($crumbArray, 0, $key + 2);
244  $crumbString = implode("/", $crumbSlice);
245 
246  if ($key == count($crumbArray) - 1) {
247  $dirlistReturn['activeTitle'] .= " / {$crumbArray[$key]}";
248  } else {
249  $dirlistReturn['activeTitle'] .= " / <a href=\"$self&ndir=$shortname&csub=$crumbString\">{$crumbArray[$key]}</a>";
250  }
251  }
252 
253  // IF allowed to add new subdirectories, add New Folder
254  // row after parent. Only allowed if number of sub dirs
255  // has not met max.
256 
257  // Use <= because the parent is always in dirlist.
258  if ($allow_subdir && $dirlist_count <= $max_subdir) {
259  // only allow one level of subdirectory
260  // EX: alm/sub/no mor subs here
261  if (in_array($cspath, $allowed_dir)) {
262  $dirlistReturn["active"][] = array(
263  "icon" => "fa-folder",
264  "name" => "New Folder",
265  "link" => $cspath,
266  "mod" => "",
267  "delete" => false,
268  "new" => true
269  );
270  }
271  }
272  } else {
273  $dirDelete = ($cpath != $top_dir) && (sizeof(scandir("$cpath/$shortname")) == 2);
274  $dirlistReturn["active"][] = array(
275  "icon" => "fa-folder-open",
276  "name" => "$shortname",
277  "link" => "$self&ndir=$shortname&csub=$cspath",
278  "mod" => "$ftime",
279  "delete" => $dirDelete,
280  "new" => false
281  );
282  }
283  }
284  }
285 
286  // BUILD FILES LIST
287  if (count($filelist) > 0) {
288  natsort($filelist);
289  reset($filelist);
290  foreach ($filelist as $shortname) {
291  $ftime=date("m/d/y g:i:s A",filemtime("$cpath/$shortname"));
292  $urlencode = urlencode("$shortname");
293  $filepath = ($cspath == "" ? "$urlencode" : "$cspath/$urlencode");
294  // check if images folder or child folder in images
295  if (substr($cspath, 0, 6) === 'images') {
296  $dirlistReturn["active"][] = array(
297  "icon" => "fa-file",
298  "name" => "$shortname",
299  "link" => "/fi/$chome/$filepath",
300  "copy" => $finalurl . "/fi/$chome/$filepath",
301  "mod" => "$ftime",
302  "delete" => true,
303  "new" => false
304  );
305  } else {
306  $dirlistReturn["active"][] = array(
307  "icon" => "fa-file",
308  "name" => "$shortname",
309  "link" => "/fi/$chome/$filepath",
310  "mod" => "$ftime",
311  "delete" => true,
312  "new" => false
313  );
314  }
315  }
316  }
317 
318  // Now print any directories that have NOT been created
319  // then post to this script and allowing them to make the directories
320  // If the allowed_dir and dirlist arrays do not have the same count then list the drop-down
321  // Only allow viewing in the top directory
322  if ((count($dirlist) < count($allowed_dir)) && ($cpath == $top_dir)) {
323 
324  reset($allowed_dir);
325  foreach ($allowed_dir as $key => $value) {
326  if (!in_array($value, $dirlist)) {
327  $dirlistReturn["inactive"][] = array(
328  "icon" => "fa-folder",
329  "name" => $value,
330  "link" => $key,
331  "mod" => "",
332  "delete" => false,
333  "new" => false
334  );
335  }
336  }
337  }
338 
339  return $dirlistReturn;
340  }
341 
342 // removed the function page_header, only called cu_header mws 1/2/03
343 /* ------------------------------------- */
344  function create_dir() {
345  global $msg_err, $msg_suc, $Cn, $Cu, $mdir, $allowed_dir, $top_dir, $home_path, $chome;
346 
347  $dir_to_create = "";
348  // This will attempt to create the directory in the credit unions public_html directory
349  $mdir = intval($mdir);
350  $path_to_dir = "";
351 
352  $dir_to_create = trim($allowed_dir[$mdir]);
353  if (strlen($dir_to_create) == 0) {
354  $msg_err = "Problem creating the directory. Please select again.";
355  } else {
356  $path_to_dir = $top_dir . "/" . $dir_to_create;
357  // Now look to see if the directory is created
358  if (is_dir($path_to_dir)) {
359  $msg_err = "The directory already exists. It was not created.";
360  } else {
361  // here is where I try to create the directory
362  if (@mkdir($path_to_dir, 0755) === false) {
363  // Problem creating the directory, it failed
364  $msg_err = "Unknown error creating the directory. <br>
365  You may need to contact HomeCU to enable private directories.";
366  } else {
367  // All is okay, so here I need to create the .htaccess file in the new directory
368  $ht_file = $path_to_dir . "/.htaccess"; // path to htaccess
369  $pwd_file = "$home_path/admin/$dir_to_create";
370  if (!file_exists($ht_file)) {
371  // Create the file with the options specified
372  $hp = fopen($ht_file, "w");
373 
374  fwrite ($hp, "AuthName \"" . strtoupper($chome) . " " . ucfirst($dir_to_create) . "\"\n");
375  fwrite ($hp, "AuthType Basic\n");
376  fwrite ($hp, "require valid-user\n");
377  fwrite ($hp, "AuthUserFile $pwd_file\n");
378 
379  fclose($hp);
380  }
381  }
382  }
383  }
384  }
385 
386 /* ------------------------------------- */
387  function create_subdir() {
388  global $msg_err, $msg_suc, $Cn, $Cu, $allowed_dir, $top_dir, $home_path, $chome, $allow_subdir, $allow_multi_subdir, $max_subdir, $csub, $newfolder;
389 
390  // Values from url --
391  // csub -- Current subdirectory the user was in, before trying to create a new folder
392  // newfolder -- the name of the new folder the credit union wants to make
393 
394  // First start with the most basic of tests
395  // 1. Are we allowing creation of subdirectories
396  if ($allow_subdir == true) {
397  $dir_to_create = "";
398  // This will attempt to create the directory in the credit unions public_html directory
399  $path_to_dir = "";
400 
401  // need to make sure csub and newfolder do not contain any weird characters
402  $newfolder = preg_replace("/\W/", "", $newfolder);
403  // $csub = preg_replace("/\W/", "", $csub);
404 
405  $dir_to_create = trim($csub) . "/" . $newfolder;
406  if (strlen($dir_to_create) == 0) {
407  $msg_err = "Problem creating the directory. Please select again.";
408  } else {
409  // Okay so we are here --
410  // Now need to be sure they are not creating a multi-subdirectory
411  if ($allow_multi_subdir == true || ((array_search($csub, $allowed_dir) !== false))) {
412  // We are getting closer, but NOW we must be sure they have not
413  // exceeded the maximum number of subdirectories allowed
414 
415  $dirpath = $top_dir . "/" . $csub;
416  $dirlist = array();
417 
418  if ($fhandle = @opendir($dirpath)) {
419  while (false !== ($file = readdir($fhandle))) {
420  if (($file != ".") && ($file != "..") && (substr($file, 0, 1) != ".")) {
421  // Don't inlcude the current directory information
422  // Now check to see if this is a directory or file
423  if (is_dir($dirpath . "/" . $file)) {
424  // Only add directory names
425  $dirlist[count($dirlist)] = $file;
426  }
427  }
428  }
429  }
430 
431  $dirlist_count = count($dirlist);
432 
433  if ($dirlist_count < $max_subdir) {
434  $path_to_dir = $top_dir . "/" . $dir_to_create;
435  // Now look to see if the directory is created
436  if (is_dir($path_to_dir)) {
437  $msg_err = "The directory already exists. It can not be created.";
438  } else {
439  // here is where I try to create the directory
440  if (@mkdir($path_to_dir, 0755) === false) {
441  // Problem creating the directory, it failed
442  $msg_err = "Unknown error creating the directory. <br>
443  You may need to contact HomeCU for additional help .";
444  } else {
445  // All is okay, so here I need to create the .htaccess file in the new directory
446  // Operation a success -- tell the user
447  $msg_suc = "A new folder named '$newfolder' was created.";
448  }
449  }
450  } else {
451  // There are too many sub directories already, don't allow any more
452  $msg_err = "Maximum number of folders has been reached. You will need to remove a folder before adding additional.";
453  }
454  } else {
455  // We are NOT allowed subdirectories under subdirectories
456  $msg_err = "Unable to create the new folder. Custom folders are only allowed in the top most folder.";
457  }
458  }
459  } else {
460  // We are not allowing creation of subdirectories
461  // Possibly report that here
462  $msg_err = "Creating subdirectories is NOT allowed.";
463  }
464  }
465 
466 /* ------------------------------------- */
467  function delete_files() {
468  global $admVars, $msg_err, $msg_suc, $csub, $top_dir;
469 
470  $filesList = $admVars['DIRMAINT']['delete'];
471  $files = explode(",", $filesList);
472 
473  foreach ($files as $filename) {
474  // Be sure there are not / or \ in the file name
475  $us_file_name = str_replace("/", "", $filename);
476  $us_file_name = str_replace("\\", "", $us_file_name);
477 
478  $delete_file = $top_dir . ($csub == '' ? "" : "/$csub") . "/" . $us_file_name;
479 
480  if (file_exists($delete_file)) {
481  if (is_file($delete_file)) {
482  if (!@unlink ($delete_file)) {
483  // This will unlink the file from this directory
484  $msg_err .= "Unable to delete the file! Please try again.<br>";
485  } else {
486  $msg_suc .= "The file '$filename' was deleted successfully.<br>";
487  }
488  } elseif (is_dir($delete_file)) {
489  $directory_empty = TRUE;
490  $directory = dir($delete_file);
491  while (FALSE !== ($item = $directory->read())) {
492  // If an item is not "." or "..", dir is not empty
493  if ($item != '.' && $item != '..') {
494  $directory_empty = FALSE;
495  break;
496  }
497  }
498  $directory->close();
499  if(($directory_empty == TRUE) && ( @rmdir ($delete_file))) {
500  $msg_suc .= "The folder '$filename' was deleted successfully.<br>";
501  } else {
502  $msg_err .= "Unable to delete folder '$filename'! Please be sure the folder is empty.<br>";
503  }
504  }
505  }
506  }
507  }
508 
509 /* ------------------------------------- */
510  function print_status() {
511  global $msg_err, $msg_suc, $dir, $self, $Cn, $Cu, $cdir, $cpath, $dir_search, $top_dir, $cspath, $csub, $allowed_dir, $allowed_files, $chome, $allow_subdir, $allow_multi_subdir, $max_subdir, $top_upload, $delurl;
512 
513  $directoryList = print_directory_list();
514 
515  $url = GetEnvSetting('TICKET_DOMAIN') ? GetEnvSetting('TICKET_DOMAIN') : 'localhost:8000';
516  $httpprotocol = GetEnvSetting('REQUIRE_ENCRYPTION') ? 'https' : 'http';
517  $finalurl = $httpprotocol . '://' . $url;
518 
519  header("Expires: Sat 20 May 1995 03:32:38 GMT");
520  header("Pragma: no-cache");
521  header("Cache-Control: no-cache, must-revalidate");
522  ?>
523 
524  <!-- Clipboard.js -->
525  <script type='text/javascript' src='/admcom/static/js/clipboard.min.js'></script>
526  <!-- NEW HTML / JS -->
527  <script type="text/javascript">
528 
529  new ClipboardJS('.btn');
530 
531  var windowStack = [];
532 
533  var gridActive = null;
534  var gridInactive = null;
535  var gridData = <?php echo HCU_JsonEncode($directoryList); ?>;
536 
537  var fileUploader = null;
538  var fileNameToUpload = null;
539  var fileSizeToBeUploaded = null;
540 
541  // Messages
542  var msg_err = "<?php echo $msg_err; ?>";
543  var msg_suc = "<?php echo $msg_suc; ?>";
544 
545  // Display Errors here, since every actions required page load
546  if (msg_err.length > 0) {
547  $.homecuValidator.homecuResetMessage = true;
548  $.homecuValidator.displayMessage(msg_err, $.homecuValidator.settings.statusError);
549  } else if (msg_suc.length > 0) {
550  $.homecuValidator.homecuResetMessage = true;
551  $.homecuValidator.displayMessage(msg_suc, $.homecuValidator.settings.statusSuccess);
552  }
553 
554  $(document).ready(function(e) {
555  gridActive = $("#gridActive").kendoGrid({
556  dataSource: {
557  data: gridData.active
558  },
559  noRecords: {
560  template: "No active directories found."
561  },
562  columns: [{
563  headerTemplate: gridData.activeTitle,
564  field: "name"
565  },{
566  title: ""
567  },{
568  title: "",
569  width: 25,
570  headerTemplate: "<input type=\"checkbox\" name=\"chk_delete_all\" />"
571  }],
572  rowTemplate: kendo.template($("#rowTemplateActive").html()),
573  dataBound: function(e) {
574  // Select all option, select all checkboxes in grid
575  // for deletion.
576  $("input[name=\"chk_delete_all\"]").on("change", function(e) {
577  var checked = $(this).prop("checked");
578 
579  $("input[name=\"chk_delete\"]").prop("checked", checked);
580 
581  // Disable / Enable delete button
582  $("#btn_delete").prop("disabled", !checked);
583  });
584 
585  $("input[name=\"chk_delete\"]").on("change", function(e) {
586  var checked = $(this).prop("checked");
587 
588  // Get number of boxes and number of checked boxes.
589  var numBoxes = $("input[name=\"chk_delete\"]").length;
590  var numChecked = $("input[name=\"chk_delete\"]:checked").length;
591 
592  if (checked) {
593  // If all checkboxes are now checked, check the
594  // select all box at the top.
595  if (numBoxes == numChecked) {
596  $("input[name=\"chk_delete_all\"]").prop("checked", true);
597  }
598  } else {
599  // Unchecking any single checkbox will uncheck
600  // the select all checkbox.
601  $("input[name=\"chk_delete_all\"]").prop("checked", false);
602  }
603 
604  // Disable / Enable delete button
605  $("#btn_delete").prop("disabled", (numChecked == 0));
606  });
607  }
608  }).data("kendoGrid");
609 
610  // Only show this grid if on root directory
611  <?php if ($cpath == $top_dir) { ?>
612  gridInactive = $("#gridInactive").kendoGrid({
613  dataSource: {
614  data: gridData.inactive
615  },
616  noRecords: {
617  template: "No inactive directories found."
618  },
619  columns: [{
620  title: "Inactive Directories",
621  field: "name"
622  }],
623  rowTemplate: kendo.template($("#rowTemplateInactive").html())
624  }).data("kendoGrid");
625  <?php } ?>
626 
627  $("#btn_delete").on("click", check_delete);
628  $("#btn_upload").on("click", check_upload);
629  fileUploader = $("#upload").kendoUpload({
630  multiple: false,
631  autoUpload: false,
632  validation: {
633  maxFileSize: 40000000
634  },
635  select: function(e) {
636  fileNameToUpload = e.files[0].name;
637  fileSizeToBeUploaded = e.files[0].size;
638  console.log(fileSizeToBeUploaded);
639  }
640  }).data("kendoUpload");
641  });
642 
643  $(document).on("click", ".k-overlay", function() {
644  if (windowStack.length > 0) {
645  var openWindow = windowStack[windowStack.length - 1];
646  openWindow.close();
647  }
648  });
649  </script>
650 
651  <!-- GRID ROW TEMPLATE -->
652  <script type="text/x-kendo-tmpl" id="rowTemplateActive">
653  <tr data-uid="#: uid #">
654  <td>
655  <span class="fa #: data.icon #"></span>&emsp;
656  # if (data.new) { #
657  <a href="\\#" onclick="new_folder('#: data.link #')">#: data.name #</a>
658  # } else { #
659  # if (data.icon == "fa-file") { #
660  <a href="#: data.link #" target="_blank">#: data.name #</a>
661 
662  # if (data.copy) { #
663  <img src="/admcom/static/img/clippy.png" class="btn" data-clipboard-text="#: data.copy #" alt="Copy to clipboard">
664  # } #
665 
666  # } else { #
667  <a href="#: data.link #">#: data.name #</a>
668  # } #
669  # } #
670  </td>
671  <td>#: data.mod #</td>
672  # if (data.delete) { #
673  <td>
674  <input type="checkbox" name="chk_delete" value="#: data.name #" />
675  </td>
676  # } else { #
677  <td></td>
678  # } #
679  </tr>
680  </script>
681 
682  <!-- This grid will only display if user is on root directory -->
683  <script type="text/x-kendo-tmpl" id="rowTemplateInactive">
684  <tr data-uid="#: uid #">
685  <td>
686  <span class="fa #: data.icon #"></span>&emsp;
687  <a href="\\#" onclick="confirm_dir('#:data.name#', '#: data.link #')">#: data.name #</a>
688  </td>
689  </tr>
690  </script>
691 
692  <div class="container-fluid">
693  <div id="dirMaintPopout"></div>
694  <h2>Private Directories</h2>
695  <form id="pdForm" method="post" action="<?php echo $self ?>" enctype="multipart/form-data">
696  <input type="hidden" name="action" value="upload">
697  <input type="hidden" name="delete" value="">
698  <input type="hidden" name="csub" value="<?php echo $cspath; ?>">
699 
700  <!-- Only show upload for non root directory -->
701  <?php if ($cpath != $top_dir) { ?>
702  <p>
703  <label>Directory Access Link: </label>
704  <a href="<?php echo $finalurl . "/fi/$chome/$cspath"; ?>"><?php echo $finalurl ."/fi/$chome/$cspath"; ?></a>
705  </p>
706  <div class="well well-sm">
707  <input type="file" name="upload" id="upload">
708  <p style="padding-top: 1em; text-align: right;">
709  <button type="button" class="k-button k-primary" id="btn_upload"><span class="fa fa-upload">&ensp;</span>Upload</button>
710  </p>
711  </div>
712  <?php } ?>
713 
714  <div id="gridActive"></div>
715 
716  <?php if ($cpath != $top_dir) { ?>
717  <div class="hcu-template">
718  <div class="hcu-edit-buttons k-state-default">
719  <button type="button" id="btn_delete" class="k-button" disabled>
720  <i class="fa fa-trash fa-lg"></i>Delete
721  </button>
722  </div>
723  </div>
724  <?php } ?>
725 
726  <br>
727  <!-- Only show inactive grid if on root directory -->
728  <?php if ($cpath == $top_dir) { ?>
729  <div class="hcu-secondary">
730  <div class="vsgSecondary">
731  <span>To activate a private directory, click on the directory name.</span>
732  </div>
733  </div>
734  <div id="gridInactive"></div>
735  <?php } ?>
736  </form>
737  </div>
738 
739  <!-- END NEW HTML / JS -->
740  <script language="javascript">
741 
742  // This function will first check
743  function check_upload() {
744  // Check if upload selection already exists
745  var fileFound = false;
746  for (var i = 0; i < gridData.active.length; i++) {
747  var name = gridData.active[i].name;
748  if (name == fileNameToUpload) {
749  fileFound = true;
750  break;
751  }
752  }
753 
754  if (fileSizeToBeUploaded > 40000000) {
755  // ERROR, This is caught by the uploader validation
756  // I will leave this here in case it is needed later on.
757  return false;
758  }
759 
760  if (fileFound) {
761  var confirmOverwrite = $("<div></div>").kendoDialog({
762  title: "Upload File",
763  modal: true,
764  visible: false,
765  resizable: false,
766  minWidth: 300,
767  maxWidth: 500,
768  show: function(e) {
769  windowStack.push(this);
770  },
771  close: function(e) {
772  windowStack.pop();
773  this.destroy();
774  },
775  actions: [
776  { text: "No" },
777  {
778  text: "Yes", primary: true,
779  action: function(e) {
780  // User is OK with overwriting file
781  $("#pdForm").submit();
782  }
783  }
784  ],
785  content: "<div class=\"col-xs-12\"><p><strong>You are about to overwrite the file '" + fileNameToUpload + "'.</strong></p><p>Do you wish to continue?</p></div>"
786  }).data("kendoDialog");
787 
788  confirmOverwrite.open().center();
789  } else {
790  // File doesn't exist already, submit form
791  $("#pdForm").submit();
792  }
793  }
794 
795  function check_delete() {
796  // Check if any rows are selected for deletion
797  var numChecked = $("input[name=\"chk_delete\"]:checked").length;
798 
799  if (numChecked > 0) {
800  var dialogConfirmDelete = $("<div></div>").kendoDialog({
801  title: "Delete Files",
802  modal: true,
803  visible: false,
804  resizable: false,
805  minWidth: 300,
806  maxWidth: 500,
807  show: function(e) {
808  windowStack.push(this);
809  },
810  close: function(e) {
811  windowStack.pop();
812  this.destroy();
813  },
814  actions: [
815  { text: "No" },
816  {
817  text: "Yes", primary: true,
818  action: function(e) {
819  // Add file names to string and send to server
820  var delete_files = [];
821  $("input[name=\"chk_delete\"]:checked").each(function(e) {
822  delete_files.push($(this).val());
823  });
824 
825  // Combine into comma separated list.
826  var delete_files_string = delete_files.join(",");
827 
828  // Add data to hidden delete field and submit form
829  $("input[name=\"delete\"]").val(delete_files_string);
830  $("input[name=\"action\"]").val("delete");
831  $("#pdForm").submit();
832  }
833  }
834  ],
835  content: "<div class=\"col-xs-12\"><p><strong>You are about to delete the delected files or folders.</strong></p><p>Do you wish to continue?</p></div>"
836  }).data("kendoDialog");
837 
838  dialogConfirmDelete.open().center();
839  }
840  }
841 
842  function confirm_dir(sdir, idir) {
843  // This function will be used to confirm if they
844  // want to create the desired directory
845  var confirmActivate = $("<div></div>").kendoDialog({
846  title: "Activate Directory",
847  modal: true,
848  visible: false,
849  resizable: false,
850  minWidth: 300,
851  maxWidth: 500,
852  show: function(e) {
853  windowStack.push(this);
854  },
855  close: function(e) {
856  windowStack.pop();
857  this.destroy();
858  },
859  actions: [
860  { text: "No" },
861  {
862  text: "Yes", primary: true,
863  action: function(e) {
864  var url = "<?php echo $self; ?>&action=create&mdir=" + idir;
865  document.location = url;
866  }
867  }
868  ],
869  content: "<div class=\"col-xs-12\"><p><strong>This will create the private directory '" + sdir + "'</strong></p><p>Do you wish to continue?</p></div>"
870  }).data("kendoDialog");
871 
872  confirmActivate.open().center();
873  }
874  function new_folder(cur_sub) {
875  var confirmNewFolder = $("<div></div>").kendoDialog({
876  title: "Activate Directory",
877  modal: true,
878  visible: false,
879  resizable: false,
880  minWidth: 300,
881  maxWidth: 500,
882  show: function(e) {
883  windowStack.push(this);
884  },
885  close: function(e) {
886  windowStack.pop();
887  this.destroy();
888  },
889  actions: [
890  { text: "No" },
891  {
892  text: "Yes", primary: true,
893  action: function(e) {
894  var newFolderField = $("#newFolder");
895 
896  if (newFolderField) {
897  var folderName = newFolderField.val();
898  // Strip all non-alpha-numeric characters
899  folderName = folderName.replace(/\W/g, "");
900  var url = "<?php echo $self; ?>&action=createsub&csub=" + escape(cur_sub) + "&newfolder=" + folderName;
901  document.location = url;
902  }
903  }
904  }
905  ],
906  content: "<div class=\"col-xs-12\"><p>Please enter a name for the new directory</p><input class=\"k-textbox hcu-all-100\" type=\"text\" name=\"newFolder\" id=\"newFolder\" maxlength=\"10\"></div>"
907  }).data("kendoDialog");
908 
909  confirmNewFolder.open().center();
910  }
911  // -->
912  </script>
913 
914 <?php }
915 
916 function view_form() {
917  global $dir;
918  global $cspath;
919  global $file;
920  global $self;
921  global $top_dir;
922 
923  $file_path = "$top_dir/$cspath/$file";
924  $form = file($file_path);
925  if (is_array($form)){
926  print implode("", $form);
927  }else{
928  print "Cannot open $file_path";
929  }
930 }
931 
932 function post_form() {
933  // Here we will post all the variables from the form and execute as needed
934  global $msg_err, $msg_suc, $replace, $csub, $top_dir, $err_msg;
935 
936  // Don't allow a file size of 0 bytes
937  $up_err = "";
938  if (!is_uploaded_file($_FILES['upload']['tmp_name']) || ($_FILES['upload']['size'] < 1)) {
939  switch ($_FILES['upload']['error']) {
940  case 0:
941  $msg_suc = "0: No error, the file was uploaded successfully";
942  break;
943  case 1:
944  $msg_err = "1: The upload is larger than the amount allowable by the upload_max_filesize directive in the php.ini";
945  $msg_err = "1: The file you tried to upload is just too big. The allowable limit is 40mb.";
946  break;
947  case 2:
948  $msg_err = "2: The upload is larger than the MAX_FILE_SIZE directive that was specified via html";
949  $msg_err = "2: The file you tried to upload is just too big. The allowable limit is 40mb.";
950  break;
951  case 3:
952  $msg_err = "3: The file was only partially uploaded";
953  break;
954  case 3:
955  $msg_err = "4: no file was uploaded";
956  break;
957  default:
958  $msg_err = "NOT SET";
959  }
960  } else {
961  // The file name will always be that of the file being uploaded
962  $filedir = $top_dir . ($csub == '' ? "" : "/$csub");
963 
964  // strip these characters '/', '\', ';', and ' ' replace with an '_'.
965  $find_ary = array("\\", "/", " ", ";"); // instead of these...
966  $rpl_ary = array("_", "_", "_", "_"); // use the _ character
967  $us_file_name = str_replace($find_ary, $rpl_ary, $_FILES['upload']['name']);
968  # make sure we still have a valid file name:
969  if ("$us_file_name" == "") {
970  $up_err = "Error Reported - Invalid Upload to Private Directory";
971  }
972 
973  // Success message
974  $msg_suc = "The file '$us_file_name' was uploaded successfully.";
975  }
976  if ($msg_err != "") {
977  $msg = "
978 Upload File Error --\n
979 Client Information \n
980  Client Filename: {$_FILES['upload']['name']}\n
981  Client Filesize: {$_FILES['upload']['size']}\n
982  Client Filetype: {$_FILES['upload']['type']}\n\n
983  Client IP: {$_SERVER['REMOTE_ADDR']}\n\n
984 Web Server Information \n
985  Temp File Name - {$_FILES['upload']['tmp_name']}
986  Error Reported - $up_err\n\n
987 
988  ";
989  $notify = new ErrorMail;
990  $notify->line = __LINE__;
991  $notify->file = __FILE__;
992  $notify->callingfunction = __FUNCTION__;
993  $notify->sql = $msg;
994  $notify->cu = "$Cu";
995  $notify->mailto = "custmnt1@homecu.net";
996  $notify->subject = "Private Directories upload error";
997  $notify->SendErr();
998  // IF we got here, the file is NOT an uploaded file report an error
999 
1000  // An error may occur here if the file is too large, this can be set in the php.ini file the variable is upload_max_filesize
1001  print <<< print_html
1002  <center>
1003  <br>
1004  There was an error uploading the file <b>{$_FILES['upload']['name']}</b>.<br>
1005  The error reported was: <b>$up_err</b>.<br>
1006  Please retry uploading the file.<br>
1007  If the problem persists call HomeCU and report the problem.
1008 
1009  </center>
1010 print_html;
1011  } else {
1012  $filename = $filedir . "/" . $us_file_name;
1013  // move the file into the selected private directory
1014  if (!move_uploaded_file($_FILES['upload']['tmp_name'], $filename)) {
1015  unlink ($_FILES['upload']['tmp_name']);
1016  // Print error screen -- couldn't move file
1017  print <<< html_text
1018  <br><br>
1019  Uh-oh, couldn't post the file where it belongs. Not sure what happened. Better call HomeCU.
1020 html_text;
1021  } else {
1022  // File was moved change the mod settings
1023  $rc = @chmod($filename, 0644);
1024  }
1025 }
1026  }
1027 ?>