Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_integrity_clean_cache_dir.php
1 <?php
24 error_reporting(E_ALL);
25 if ((php_sapi_name() != 'cli')) {
26  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
27 }
28 
29 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
30 if (empty($SYSTEM_ROOT)) {
31  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
32  exit();
33 }
34 
35 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
36  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
37  exit();
38 }
39 
40 require_once $SYSTEM_ROOT.'/core/include/init.inc';
41 
42 echo "\nWarning: Please make sure you have the correct permission to remove cache files.\n";
43 echo 'SQ_CACHE_PATH is \''.SQ_CACHE_PATH."'\n\n";
44 // ask for the root password for the system
45 echo 'Enter the root password for "'.SQ_CONF_SYSTEM_NAME.'": ';
46 system('stty -echo');
47 $root_password = rtrim(fgets(STDIN, 4094));
48 system('stty echo');
49 
50 // check that the correct root password was entered
51 $root_user = & $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
52 if (!$root_user->comparePassword($root_password)) {
53  echo "ERROR: The root password entered was incorrect\n";
54  exit();
55 }
56 
57 // log in as root
58 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
59  echo "ERROR: Failed login in as root user\n";
60  exit();
61 }
62 
63 $cache_path_len = strlen(SQ_CACHE_PATH) + 1;
64 
65 // get all unique file paths in the sq_cache table
66 $cache_manager =& $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
67 $valid_files = $cache_manager->getAllFilePaths('col');
68 
69 // get all the cache directory names
70 exec('find '.SQ_CACHE_PATH." -type d -name '[0-9]*'", $current_dirs);
71 $count = 0;
72 $total = 0;
73 // loop through each directory, to make it less memory intensive
74 foreach ($current_dirs as $dir) {
75 
76  $current_files = Array();
77  // remove the file if there isnt a corresponding entry in the sq_cache table
78  exec("find $dir -type f -name '[a-z0-9]*'", $current_files);
79  foreach ($current_files as $file) {
80  $file_name = substr($file, $cache_path_len);
81  if (!in_array($file_name, $valid_files)) {
82  $total++;
83  printFileName($file_name);
84  $status = @unlink(SQ_CACHE_PATH.'/'.$file_name);
85  $ok = ($status) ? 'OK' : 'FAILED';
86  printStatus($ok);
87  if ($status) $count++;
88  }
89  }
90 
91 }
92 
93 echo "\nSummary: $count/$total cache file(s) removed.\n";
94 if ($count != $total) {
95  $problematic = $total - $count;
96  trigger_error("$problematic file(s) cannot be removed, please check file permission.", E_USER_WARNING);
97 }
98 
99 
108 function printFileName($file_name)
109 {
110  $str = "\tRemoving ".$file_name;
111  printf ('%s%'.(50 - strlen($str)).'s', $str,'');
112 
113 }//end printFileName()
114 
115 
124 function printStatus($status)
125 {
126  echo "[ $status ]\n";
127 
128 }//end printStatus()
129 
130 
131 ?>