Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
upgrade_bodycopy_content_file.php
1 <?php
28 // Usage: php upgrade_bodycopy_content_file.php <SYSTEM_ROOT> [REPORT_ONLY]
29 // [REPORT_ONLY] = {y, n}
30 
31 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', -1);
32 error_reporting(E_ALL);
33 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
34 
35 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
36 $report_only = (isset($_SERVER['argv'][2]) && $_SERVER['argv'][2]!='y') ? FALSE : TRUE;
37 
38 if (empty($SYSTEM_ROOT)) {
39  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
40  exit();
41 }
42 
43 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
44  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
45  exit();
46 }
47 
48 require_once $SYSTEM_ROOT.'/core/include/init.inc';
49 
50 if ($report_only) {
51  echo "Following Bodycopy Div asset(s) contains unsafe keywords in the content file:\n\n";
52 } else {
53  echo "Fixing the unsafe keyword(s) in content file for following Bodycopy Div asset(s):\n\n";
54 }
55 
56 // Get all the bodycopy divs
57 $assetids = array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren(1, 'bodycopy_div'));
58 $count = 0;
59 foreach($assetids as $assetid) {
60 
61  $data_dir = $SYSTEM_ROOT.'/data/private/'.asset_data_path_suffix('bodycopy_div', $assetid).'/content_file.php';
62  if (!is_file($data_dir)) {
63  continue;
64  }
65 
66  $file_content = file_get_contents($data_dir);
67  // Extract the keyword replacements in the content file
68  preg_match_all('|echo \(isset\(\$keyword_replacements\["(.*?)\]\)\) \?|mis', $file_content, $matches);
69  if (empty($matches[1])) {
70  // No keywords in the content
71  continue;
72  }
73 
74  $keywords_str = '';
75  foreach($matches[1] as $keyword) {
76  $keywords_str .= trim($keyword, '"');
77 
78  }
79 
80  if ($keywords_str != htmlentities(html_entity_decode($keywords_str))) {
81  echo "#".$assetid."\n";
82  // Keyword in the content file contains non-safe keywords, so regenerate the content file
83  if (!$report_only) {
84  $bodycopy_div = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
85  $bodycopy_div_edit_fns = $bodycopy_div->getEditFns();
86  $bodycopy_div_edit_fns->generateContentFile($bodycopy_div);
87  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($bodycopy_div);
88  }
89  $count++;
90  }
91 
92 }//end foreach
93 
94 echo $report_only ? $count. " asset(s) requires fixing" : $count. " asset(s) were fixed";
95 echo "\n";
96 ?>