Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_integrity_deleted_user_perms.php
1 <?php
25 error_reporting(E_ALL);
26 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
27 
28 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
29 if (empty($SYSTEM_ROOT)) {
30  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
31  exit();
32 }
33 
34 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
35  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
36  exit();
37 }
38 
39 require_once $SYSTEM_ROOT.'/core/include/init.inc';
40 
41 $ROOT_ASSETID = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '1';
42 if ($ROOT_ASSETID == 1) {
43  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";
44 }
45 
46 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
47 
48 // log in as root
49 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
50  echo "ERROR: Failed login in as root user\n";
51  exit();
52 }
53 
54 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
55 $db =& $GLOBALS['SQ_SYSTEM']->db;
56 
57 //-- CLEANING PERMISSIONS --//
58 echo 'Cleaning up permissions...'."\n";
59 
60 $sql = 'SELECT
61  DISTINCT userid
62  FROM sq_ast_perm
63  ORDER BY userid';
64 $user_ids = MatrixDAL::executeSqlAssoc($sql, 0);
65 
66 foreach ($user_ids as $user_id) {
67 
68  $id_parts = explode(':', $user_id);
69  if (isset($id_parts[1])) {
70  $real_assetid = $id_parts[0];
71  $bridge = $GLOBALS['SQ_SYSTEM']->am->getAsset($real_assetid, '', TRUE);
72  if (is_null($bridge)) {
73  // bridge is unknown, we cannot return anything from it
74  $asset = NULL;
75  } else {
76  $asset = $bridge->getAsset($user_id, '', TRUE, TRUE);
77  }
78  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($bridge);
79 
80  } else {
81  $asset = &$GLOBALS['SQ_SYSTEM']->am->getAsset($user_id, '', TRUE);
82  }
83  if (!is_null($asset)) {
84  // print info the asset, as it exists
85  printAssetName($asset);
86  printUpdateStatus('OK');
87 
88  // conserve memory and move on to the next perm
89  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
90  continue;
91  }
92 
93  // the asset doesn't exist
94  $dummy_asset->id = $user_id;
95  $dummy_asset->name = 'Unknown Asset';
96 
97  printAssetName($dummy_asset);
98 
99  // open the transaction
100  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
101 
102  try {
103  $sql = 'DELETE FROM sq_ast_perm WHERE userid = :userid';
104  $query = MatrixDAL::preparePdoQuery($sql);
105  MatrixDAL::bindValueToPdo($query, 'userid', $user_id);
106  MatrixDAL::execPdoQuery($query);
107 
108  // all good
109  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
110  printUpdateStatus('FIXED');
111  } catch (DALException $e) {
112  // no good
113  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
114  printUpdateStatus('FAILED');
115  }
116 
117 }//end for
118 
119 //-- CLEANING ROLES --//
120 // Note that userid of 0 is legitimate as this indicates a global role
121 
122 echo 'Cleaning up roles...'."\n";
123 
124 $sql = 'SELECT
125  DISTINCT userid
126  FROM sq_ast_role
127  WHERE userid <> 0
128  ORDER BY userid';
129 $user_ids = MatrixDAL::executeSqlAssoc($sql, 0);
130 
131 foreach ($user_ids as $user_id) {
132 
133  $id_parts = explode(':', $user_id);
134  if (isset($id_parts[1])) {
135  $real_assetid = $id_parts[0];
136  $bridge = $GLOBALS['SQ_SYSTEM']->am->getAsset($real_assetid, '', TRUE);
137  if (is_null($bridge)) {
138  // bridge is unknown, we cannot return anything from it
139  $asset = NULL;
140  } else {
141  $asset = $bridge->getAsset($user_id, '', TRUE, TRUE);
142  }
143  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($bridge);
144 
145  } else {
146  $asset = &$GLOBALS['SQ_SYSTEM']->am->getAsset($user_id, '', TRUE);
147  }
148  if (!is_null($asset)) {
149  // print info the asset, as it exists
150  printAssetName($asset);
151  printUpdateStatus('OK');
152 
153  // conserve memory and move on to the next perm
154  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
155  continue;
156  }
157 
158  // the asset doesn't exist
159  $dummy_asset->id = $user_id;
160  $dummy_asset->name = 'Unknown Asset';
161 
162  printAssetName($dummy_asset);
163 
164  // open the transaction
165  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
166 
167  try {
168  $sql = 'DELETE FROM sq_ast_role WHERE userid = :userid';
169  $query = MatrixDAL::preparePdoQuery($sql);
170  MatrixDAL::bindValueToPdo($query, 'userid', $user_id);
171  MatrixDAL::execPdoQuery($query);
172 
173  // all good
174  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
175  printUpdateStatus('FIXED');
176  } catch (DALException $e) {
177  // no good
178  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
179  printUpdateStatus('FAILED');
180  }
181 
182 }//end for
183 
184 
195 function printAssetName(&$asset)
196 {
197  $str = $asset->name . ' [ # '. $asset->id. ' ]';
198  printf ('%s%'.(80 - strlen($str)).'s', $str,'');
199 
200 }//end printAssetName()
201 
202 
211 function printUpdateStatus($status)
212 {
213  echo "[ $status ]\n";
214 
215 }//end printUpdateStatus()
216 
217 
218 ?>