Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
upgrade_session_expiry_preferences.php
1 <?php
23 error_reporting(E_ALL);
24 if ((php_sapi_name() != 'cli')) {
25  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
26 }
27 
28 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
29 
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 $root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
43 
44 // log in as root
45 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
46  echo "ERROR: Failed logging in as root user\n";
47  exit();
48 }
49 
50 $am = $GLOBALS['SQ_SYSTEM']->am;
51 $count = 0;
52 
53 // Upgrade the global preferences
54 echo 'Updating global preferences ... ';
55 $global_prefs_file = SQ_DATA_PATH.'/private/conf/preferences.inc';
56 if (!_upgrade($global_prefs_file)) $count++;
57 
58 // Upgrade the group preferences
59 $groupids = $am->getTypeAssetids('user_group');
60 foreach ($groupids as $groupid) {
61  $asset = $am->getAsset($groupid);
62  echo 'Updating preferences for '.$asset->name.' ('.$asset->id.')... ';
63  $current_prefs_file = $asset->data_path.'/.preferences.inc';
64  if (!_upgrade($current_prefs_file)) $count++;
65 }//end foreach
66 
67 // Warn the user if an error happened
68 if (!empty($count)) {
69  echo "There were $count errors in upgrading the preferences.\nThese files may need to be reviewed manually.\n";
70 }//end if
71 
72 
81 function _upgrade($prefs_file)
82 {
83  // Start the upgrade
84  if (!file_exists($prefs_file)) {
85  echo "No Preferences found\n";
86  return FALSE;
87  }//end if
88  include_once($prefs_file);
89 
90  if (isset($preferences['user']['SQ_USER_SESSION_PREFS']['default'])) {
91  $current = $preferences['user']['SQ_USER_SESSION_PREFS']['default'];
92  $test = array_pop($preferences['user']['SQ_USER_SESSION_PREFS']['default']);
93  if (!is_array($test)) {
94  unset($preferences['user']['SQ_USER_SESSION_PREFS']['default']);
95  $preferences['user']['SQ_USER_SESSION_PREFS']['default'] = Array($current);
96  // Save the new preferences
97  if (!_savePreferences($preferences, $prefs_file)) {
98  echo "Error saving file\n";
99  return FALSE;
100  }//end if
101  }//end if
102  }//end if
103 
104  // Cleanup
105  unset($preferences);
106 
107  // Finish
108  echo "Done\n";
109  return TRUE;
110 
111 }//end _upgrade
112 
113 
123 function _savePreferences($preferences, $prefs_file)
124 {
125  if (empty($preferences)) return FALSE;
126 
127  $str = '<'.'?php $preferences = '.var_export($preferences, TRUE).'; ?'.'>';
128 
129  // Backup the existing preference file
130  if (file_exists($prefs_file)) {
131 
132  $i = 0;
133  do {
134  $i++;
135  $old_version = $prefs_file.'.'.$i;
136  } while (file_exists($old_version));
137 
138  if (!copy($prefs_file, $old_version)) {
139  return FALSE;
140  }
141 
142  }// endif
143 
144  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
145  // make sure the directory exists
146  if (!create_directory(dirname($prefs_file))) {
147  return FALSE;
148  }
149 
150  // and save the file
151  if (!string_to_file($str, $prefs_file)) {
152  return FALSE;
153  }
154 
155  // Otherwise a OK!
156  return TRUE;
157 
158 }//end _savePreferences()
159 
160 
161 ?>