Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
upgrade_clear_squid_cache_preferences.php
1 <?php
27 error_reporting(E_ALL);
28 if ((php_sapi_name() != 'cli')) {
29  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
30 }
31 
32 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
33 if (empty($SYSTEM_ROOT)) {
34  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
35  exit();
36 }
37 
38 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
39  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
40  exit();
41 }
42 
43 require_once $SYSTEM_ROOT.'/core/include/init.inc';
44 // log in as root
45 $root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
46 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
47  echo "ERROR: Failed logging in as root user\n";
48  exit();
49 }
50 
51 // forced run level
52 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
53 
54 $am = $GLOBALS['SQ_SYSTEM']->am;
55 $count = 0;
56 
57 echo 'Moving Squid Cache preferences to External Tools Config'."\n";
58 
59 // Moving Squid Cache global preferences to External Tools Config
60 echo 'Moving global preferences... ';
61 $global_prefs_file = SQ_DATA_PATH.'/private/conf/preferences.inc';
62 if (!_movePreference($global_prefs_file)) $count++;
63 
64 
65 echo 'Updating global preferences... ';
66 if (!_upgrade($global_prefs_file)) $count++;
67 
68 // Remove the Squid Cache group preferences
69 $groupids = $am->getTypeAssetids('user_group');
70 foreach ($groupids as $groupid) {
71  $asset = $am->getAsset($groupid);
72  echo 'Updating preferences for '.$asset->name.' ('.$asset->id.')... ';
73  $current_prefs_file = $asset->data_path.'/.preferences.inc';
74  if (!_upgrade($current_prefs_file)) $count++;
75 }//end foreach
76 
77 // Warn the user if an error happened
78 if (!empty($count)) {
79  echo "There were $count errors in upgrading the Squid Cache preferences.\nThese files may need to be reviewed manually.\n";
80 }//end if
81 
82 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
83 
84 
85 
94 function _movePreference($prefs_file)
95 {
96  // Start the upgrade
97  if (!file_exists($prefs_file)) {
98  echo "Preference file not found\n";
99  return FALSE;
100  }//end if
101  include_once($prefs_file);
102 
103  if (isset($preferences['tool_clear_squid_cache'])) {
104  $vars['SQ_TOOL_SQUID_CACHE_HOSTNAMES'] = $preferences['tool_clear_squid_cache']['SQ_SQUID_CACHE_HOSTNAMES']['default'];
105  $vars['SQ_TOOL_SQUID_CACHE_PATH'] = $preferences['tool_clear_squid_cache']['SQ_SQUID_CACHE_PATH']['default'];
106  $vars['SQ_TOOL_SQUID_CACHE_PORT'] = $preferences['tool_clear_squid_cache']['SQ_SQUID_CACHE_PORT']['default'];
107  $vars['SQ_TOOL_SQUID_CACHE_OPTION'] = $preferences['tool_clear_squid_cache']['SQ_SQUID_CACHE_OPTION']['default'];
108  $vars['SQ_TOOL_SQUID_CACHE_SLASH'] = $preferences['tool_clear_squid_cache']['SQ_SQUID_CACHE_SLASH']['default'];
109  $vars['SQ_TOOL_SQUID_URL_PORT'] = $preferences['tool_clear_squid_cache']['SQ_SQUID_URL_PORT']['default'];
110 
111 
112  require_once SQ_INCLUDE_PATH.'/external_tools_config.inc';
113  $cfg = new External_Tools_Config();
114  if (!$cfg->save($vars, FALSE, TRUE)) {
115  echo "Preference tool_clear_squid_cache can not be saved\n";
116  return FALSE;
117  }
118 
119  }//end if
120  else {
121  echo "Preference not found\n";
122  return TRUE;
123  }
124 
125  // Cleanup
126  unset($preferences);
127 
128  // Finish
129  echo 'Done'."\n";
130  return TRUE;
131 
132 }//end _movePreference
133 
134 
135 
136 
146 function _upgrade($prefs_file)
147 {
148  // Start the upgrade
149  if (!file_exists($prefs_file)) {
150  echo "Ignored\n";
151  return TRUE;
152  }//end if
153  include($prefs_file);
154  if (isset($preferences['tool_clear_squid_cache'])) {
155  unset($preferences['tool_clear_squid_cache']);
156  // Save the new preferences
157  if (!_savePreferences($preferences, $prefs_file)) {
158  echo "Error saving file\n";
159  return FALSE;
160  }//end if
161 
162  }//end if
163  else {
164  echo "Ignored\n";
165  return TRUE;
166  }
167 
168  // Cleanup
169  unset($preferences);
170 
171  // Finish
172  echo "Done\n";
173  return TRUE;
174 
175 }//end _upgrade
176 
177 
187 function _savePreferences($preferences, $prefs_file)
188 {
189  if (empty($preferences)) return FALSE;
190 
191  $str = '<'.'?php $preferences = '.var_export($preferences, TRUE).'; ?'.'>';
192 
193  // Backup the existing preference file
194  if (file_exists($prefs_file)) {
195 
196  $i = 0;
197  do {
198  $i++;
199  $old_version = $prefs_file.'.'.$i;
200  } while (file_exists($old_version));
201 
202  if (!copy($prefs_file, $old_version)) {
203  return FALSE;
204  }
205 
206  }// endif
207 
208  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
209  // make sure the directory exists
210  if (!create_directory(dirname($prefs_file))) {
211  return FALSE;
212  }
213 
214  // and save the file
215  if (!string_to_file($str, $prefs_file)) {
216  return FALSE;
217  }
218 
219  // Otherwise a OK!
220  return TRUE;
221 
222 }//end _savePreferences()
223 
224 
225 ?>