Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_integrity_check_links.php
1 <?php
24 error_reporting(E_ALL);
25 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
26 
27 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
28 if (empty($SYSTEM_ROOT)) {
29  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
30  exit();
31 }
32 
33 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
34  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
35  exit();
36 }
37 
38 require_once $SYSTEM_ROOT.'/core/include/init.inc';
39 
40 // login as root user to avoid problems with safe edit assets
41 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
42 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
43  echo "Failed login in as root user\n";
44  exit();
45 }
46 
47 $ROOT_ASSETID = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '1';
48 if ($ROOT_ASSETID == 1) {
49  echo "\nWARNING: You are running this integrity checker on the whole system.\nThis is fine but it may take a long time\n\nYOU HAVE 5 SECONDS TO CANCEL THIS SCRIPT... ";
50  for ($i = 1; $i <= 5; $i++) {
51  sleep(1);
52  echo $i.' ';
53  }
54  echo "\n\n";
55 }
56 
57 
58 // go trough each wysiwyg in the system and validate the links
59 $wysiwygids = $GLOBALS['SQ_SYSTEM']->am->getChildren($ROOT_ASSETID, 'content_type_wysiwyg', false);
60 foreach ($wysiwygids as $wysiwygid => $type_code_data) {
61  $type_code = $type_code_data[0]['type_code'];
62  $wysiwyg = &$GLOBALS['SQ_SYSTEM']->am->getAsset($wysiwygid, $type_code);
63  $html = $wysiwyg->attr('html');
64 
65  // extract the ./?a=xx style links
66  $e = '/\\.\\/\\?a=([0-9]+)/';
67  $matches = Array();
68  preg_match_all($e, $html, $matches);
69  $internal_assetids = $matches[1];
70 
71  foreach ($internal_assetids as $assetid) {
72  printWYSIWYGName('WYSIWYG #'.$wysiwyg->id.' - LINK #'.$assetid);
73  if(!empty($assetid)) {
74  $asset = &$GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, '', true);
75  }
76  else {
77  $asset = NULL;
78  }
79 
80  if (is_null($asset)) {
81  // the asset was invalid
82  printUpdateStatus('INVALID');
83  } else if ($GLOBALS['SQ_SYSTEM']->am->assetInTrash($assetid, true)) {
84  // the asset is in the trash
85  printUpdateStatus('TRASH');
86  } else {
87  printUpdateStatus('OK');
88  }
89  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
90  }
91 
92  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($wysiwyg);
93 
94 }//end foreach
95 
96 
107 function printWYSIWYGName($name)
108 {
109  printf ('%s%'.(35 - strlen($name)).'s', $name, '');
110 
111 }//end printWYSIWYGName()
112 
113 
122 function printUpdateStatus($status)
123 {
124  echo "[ $status ]\n";
125 
126 }//end printUpdateStatus()
127 
128 
129 ?>