Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
locale_restore.php
1 <?php
45 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
46 error_reporting(E_ALL);
47 $SYSTEM_ROOT = '';
48 $exs = Array();
49 
50 
51 // from cmd line
52 $cli = true;
53 
54 if ((php_sapi_name() == 'cli')) {
55  if (isset($_SERVER['argv'][1])) {
56  $SYSTEM_ROOT = $_SERVER['argv'][1];
57  }
58  $err_msg = "You need to supply the path to the System Root as the first argument\n";
59 
60 } else {
61  $err_msg = '
62  <div style="background-color: red; color: white; font-weight: bold;">
63  You can only run the '.$_SERVER['argv'][0].' script from the command line
64  </div>
65  ';
66 }
67 
68 if (empty($SYSTEM_ROOT)) {
69  echo $err_msg;
70  exit();
71 }
72 
73 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
74  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
75  exit();
76 }
77 
78 
79 
80 // only use console stuff if we're running from the command line
81 if ($cli) {
82  require_once 'Console/Getopt.php';
83 
84  $shortopt = 'd:rf';
85  $longopt = Array();
86 
87  $con = new Console_Getopt;
88  $args = $con->readPHPArgv();
89  array_shift($args); // remove the system root
90  $options = $con->getopt($args, $shortopt, $longopt);
91 
92  if (is_array($options[0])) {
93  $opt_list = get_console_list($options[0]);
94  $locale_list = $opt_list['locale'];
95  }
96 
97 }
98 
99 require_once $SYSTEM_ROOT.'/core/include/init.inc';
100 
101 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
102 
103 // log in as root
104 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
105  echo "Failed login in as root user\n";
106  exit();
107 }
108 
109 // get the list of functions used during install
110 require_once $SYSTEM_ROOT.'/install/install.inc';
111 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
112 require_once 'XML/Tree.php';
113 
114 if (empty($opt_list['dir'])) {
115  $opt_list['dir'] = SQ_SYSTEM_ROOT.'/data/temp/locale_backup';
116 }
117 
118 $file_list = Array();
119 $folder_list = Array(realpath($opt_list['dir']));
120 
121 while(!empty($folder_list)) {
122  $folder = array_pop($folder_list);
123  $d = @opendir($folder);
124  bam($folder);
125 
126  if ($d) {
127  while (false !== ($entry = readdir($d))) {
128  if (($entry == '..') || ($entry == '.') || ($entry == 'CVS')) continue;
129  $file = $folder.'/'.$entry;
130 
131  // if this is a folder, add it in only if -r has been included
132  if (is_dir($file) && $opt_list['recurse']) {
133  array_push($folder_list, $file);
134  }
135 
136  // if this is one of the files that we've been expecting, then
137  // go ahead and process it
138  if (substr($entry,-4) == '.xml') {
139  process_file(realpath($SYSTEM_ROOT), $file, $opt_list['force']);
140  }
141 
142  }
143 
144  }
145 
146 }
147 
148 // End of script
149 exit(0);
150 
151 
152 
163 function get_console_list($options)
164 {
165  $list = Array(
166  'locale' => Array(),
167  'dir' => '',
168  'force' => false,
169  'recurse' => false,
170  );
171 
172  foreach ($options as $option) {
173  // if nothing set, skip this entry
174  if (!isset($option[0])) continue;
175 
176  switch($option[0]) {
177  case '--locale':
178  if (!isset($option[1])) continue;
179  // Now process the list
180  $parts = explode('-', $option[1]);
181 
182  $types = Array();
183  if ((count($parts) == 2) && strlen($parts[1])) {
184  $types = explode(',', $parts[1]);
185  } else {
186  $types = Array('all');
187  }
188 
189  $list['locale'][$parts[0]] = $types;
190  break;
191 
192  case 'd':
193  if (!isset($option[1])) continue;
194  $list['dir'] = $option[1];
195  break;
196 
197  case 'r':
198  $list['recurse'] = true;
199  break;
200 
201  case 'f':
202  $list['force'] = true;
203  break;
204  }
205 
206  }
207 
208  return $list;
209 
210 }//end get_console_list()
211 
212 
220 function process_strings_file($path, $file)
221 {
222  bam('STRINGS '.$file);
223  $xml_object = new XML_Tree($file);
224  $root = $xml_object->getTreeFromFile();
225  return true;
226 
227 }//end process_strings_file()
228 
229 
237 function process_internal_messages_file($path, $file)
238 {
239  bam('MESSAGES '.$file);
240  $xml_object = new XML_Tree($file);
241  $root = $xml_object->getTreeFromFile();
242  return true;
243 
244 }//end process_internal_messages_file()
245 
246 
254 function process_errors_file($path, $file)
255 {
256  bam('ERRORS '.$file);
257  $xml_object = new XML_Tree($file);
258  $root = $xml_object->getTreeFromFile();
259 
260  assert_equals($root->name, 'files');
261  echo $locale = $root->attributes['locale'];
262  for(reset($root->children); null !== ($file_index = key($root->children)); next($root->children)) {
263  $file_tag =& $root->children[$file_index];
264  $file_name = $path.'/'.$file_tag->attributes['source'].'/'.$locale.'/lang_errors.xml';
265 
266  // now create the file (and directory if required)
267  create_directory($path.'/'.$file_tag->attributes['source'].'/'.$locale);
268  string_to_file($file_tag->children[0]->get(), $file_name);
269  }
270 
271  return true;
272 
273 }//end process_errors_file()
274 
275 
283 function process_file($path, $file, $force)
284 {
285  $file_contents = file_get_contents($file);
286  preg_match('|<files locale="([^"]*)">.*</file>|s', $file_contents, $match);
287 
288  // get locale and replace delimiters between locale parts with slashes
289  $locale = $match[1];
290  $locale = str_replace('@', '/', $locale);
291  $locale = str_replace('_', '/', $locale);
292 
293  $old_force = $force;
294 
295  preg_match_all('|<file source="([^"]*)" name="([^"]*)">(.*)</file>|Us', $file_contents, $matches, PREG_SET_ORDER);
296  foreach($matches as $match) {
297  $file_name = $path.'/'.$match[1].'/'.$locale.'/'.$match[2];
298  $file_str = '<?xml version="1.0" ?>'."\n".$match[3];
299 
300  if (!file_exists($file_name)) {
301  $force = true;
302  } else {
303  $force = $old_force;
304  }
305 
306  if (!$force) {
307  // ask for the root password for the system
308  echo 'Overwrite file '.hide_system_root($file_name).'?';
309  $overwrite_confirm = '';
310  while((strlen($overwrite_confirm) == 0) ||
311  ((strtolower($overwrite_confirm{0}) != 'y') && strtolower($overwrite_confirm{0}) != 'n')) {
312  $overwrite_confirm = rtrim(fgets(STDIN, 4094));
313  }
314  if (strtolower($overwrite_confirm{0}) == 'y') {
315  $force = true;
316  }
317  }
318 
319  if ($force) {
320  // create directory and dump file
321  create_directory($path.'/'.$match[1].'/'.$locale);
322  string_to_file($file_str, $file_name);
323  echo 'Wrote '.hide_system_root($file_name)."\n";
324  }
325  }
326 
327  return true;
328 
329 }//end process_localised_screens_file()
330 ?>