Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
add_user_own_access.php
1 <?php
27 error_reporting(E_ALL);
28 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
29 
30 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
31 if (empty($SYSTEM_ROOT)) {
32  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
33  exit();
34 }
35 
36 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
37  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
38  exit();
39 }
40 
41 require_once $SYSTEM_ROOT.'/core/include/init.inc';
42 
43 $ROOT_ASSETID = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '1';
44 if ($ROOT_ASSETID == 1) {
45  echo "\nWARNING: You are running this integrity checker on the whole system.\nThis is fine but:\n\tit may take a long time; and\n\tit will acquire locks on many of your assets (meaning you wont be able to edit content for a while)\n\n";
46 }
47 
48 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
49 
50 // log in as root
51 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
52  echo "ERROR: Failed login in as root user\n";
53  exit();
54 }
55 
56 // go through each user in the system, lock it, set permissions, unlock it
57 $assets = $GLOBALS['SQ_SYSTEM']->am->getChildren($ROOT_ASSETID, 'user', false);
58 foreach ($assets as $assetid => $type_code_data) {
59  $type_code = $type_code_data[0]['type_code'];
60  $asset = &$GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $type_code);
61  printAssetName($asset);
62 
63  // try to lock the asset
64  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, 'permissions')) {
65  printUpdateStatus('LOCK');
66  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
67  continue;
68  }
69 
70  // give the user read access to him/herself
71  if (!$GLOBALS['SQ_SYSTEM']->am->setPermission($asset->id, $asset->id, SQ_PERMISSION_READ, 1)) {
72  printUpdateStatus('FAILED');
73  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
74  continue;
75  }
76 
77  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'asset', false, 'major', '', 1);
78  foreach ($links as $link) {
79  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($link['minorid'], 'permissions')) {
80  printUpdateStatus('SUB LOCK');
81  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
82  continue;
83  }
84 
85  if (!$GLOBALS['SQ_SYSTEM']->am->setPermission($link['minorid'], $asset->id, SQ_PERMISSION_READ, 1)) {
86  printUpdateStatus('SUB FAILED');
87  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
88  continue;
89  }
90 
91  // try to unlock the asset
92  if (!$GLOBALS['SQ_SYSTEM']->am->releaseLock($link['minorid'], 'permissions')) {
93  printUpdateStatus('SUB !!');
94  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
95  continue;
96  }
97  }
98 
99  // try to unlock the asset
100  if (!$GLOBALS['SQ_SYSTEM']->am->releaseLock($asset->id, 'permissions')) {
101  printUpdateStatus('!!');
102  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
103  continue;
104  }
105 
106  printUpdateStatus('OK');
107  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
108 
109 }//end foreach
110 
111 
122 function printAssetName(&$asset)
123 {
124  $str = $asset->name . ' [ # '. $asset->id. ' ]';
125  printf ('%s%'.(40 - strlen($str)).'s', $str,'');
126 
127 }//end printAssetName()
128 
129 
138 function printUpdateStatus($status)
139 {
140  echo "[ $status ]\n";
141 
142 }//end printUpdateStatus()
143 
144 
145 ?>