Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
set_files_unrestricted.php
1 <?php
43 //-- DRIVER CODE --//
44 
45 
46 error_reporting(E_ALL);
47 if ((php_sapi_name() != 'cli')) {
48  trigger_error('This script must be run from the command line', E_USER_ERROR);
49  exit(1);
50 }
51 
52 // Handle cases where the number of parameters is less than expected.
53 // (Note: the count is one more than you may think, because $ARGV[0] is the
54 // script name.)
55 $ARGV = $_SERVER['argv'];
56 switch (count($ARGV)) {
57  case 1:
58  error_line('You must specify a system root to your MySource Matrix installation');
59  short_usage();
60  exit(1);
61  break;
62 
63  case 2:
64  // This may be sorta legal if we are asking for the long usage
65  if ($ARGV[1] == '--help') {
66  long_usage();
67  exit(0);
68  } else {
69  error_line('You must provide the asset ID of the root node to search under');
70  short_usage();
71  exit(1);
72  }
73  break;
74 
75  case 3:
76  error_line('You must tell the script whether files are to be restricted or unrestricted');
77  short_usage();
78  exit(1);
79  break;
80 }
81 
82 list($SYSTEM_ROOT, $ROOT_ASSETID, $UNRESTRICT_SETTING) = array_slice($ARGV, 1, 3);
83 
84 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
85  error_line('ERROR: Path provided doesn\'t point to a Matrix installation\'s System Root. Please provide correct path and try again.');;
86  exit(1);
87 }
88 
89 require_once $SYSTEM_ROOT.'/core/include/init.inc';
90 
91 // Have we been passed a dodgy root asset ID?
92 if (!assert_valid_assetid($ROOT_ASSETID, '', TRUE, FALSE)) {
93  error_line('Root asset ID "'.$ROOT_ASSETID.'" does not appear to be valid');
94  exit(1);
95 }
96 
97 $root_assetid_details = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($ROOT_ASSETID);
98 if (empty($root_assetid_details)) {
99  error_line('Root asset ID "'.$ROOT_ASSETID.'" does not exist');
100  exit(1);
101 }
102 
103 // The "unrestricted" seting is valid or not?
104 if (($UNRESTRICT_SETTING !== '0') && ($UNRESTRICT_SETTING !== '1')) {
105  error_line('The unrestricted setting is not invalid - it must be either "1" (unrestricted) or "0" (restricted)');
106  exit(1);
107 }
108 
109 $file_assetids = $GLOBALS['SQ_SYSTEM']->am->getChildren($ROOT_ASSETID, 'file', FALSE);
110 echo 'Found '.count($file_assetids).' File asset(s) underneath asset ID #'.$ROOT_ASSETID."\n";
111 
112 // no assets to work on, just exit normally, otherwise continue
113 if (count($file_assetids) > 0) {
114  // ask for the root password for the system
115  echo 'Enter the root password for "'.SQ_CONF_SYSTEM_NAME.'": ';
116  system('stty -echo');
117  $root_password = rtrim(fgets(STDIN, 4094));
118  system('stty echo');
119 
120  // check that the correct root password was entered
121  $root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
122  if (!$root_user->comparePassword($root_password)) {
123  error_line('Root password incorrect'."\n");
124  exit(1);
125  }
126 
127  // log in as root
128  if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
129  error_line('Could not log in as root user');
130  exit(1);
131  }
132 
133  echo "\n";
134  do_set_unrestricted($ROOT_ASSETID, $UNRESTRICT_SETTING, $file_assetids);
135  echo "\n";
136 
137  // Log out the root user
138  $GLOBALS['SQ_SYSTEM']->restoreCurrentUser();
139  unset($root_user);
140 }
141 
142 exit(0);
143 
144 
145 //-- ACTION FUNCTIONS --//
146 
147 
158 function do_set_unrestricted($root_node, $setting, $file_assetids)
159 {
160  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
161  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
162  $return = Array(
163  'changed' => 0,
164  'failed' => 0,
165  );
166 
167 
168  $root_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($root_node);
169  $child_query = $GLOBALS['SQ_SYSTEM']->am->generateGetChildrenQuery($root_asset, 'file', FALSE);
170 
171  // Children query normally selects asset ID and type code. We don't want type code.
172  $child_query['sql_array']['select'] = str_replace(', a.type_code', '', $child_query['sql_array']['select']);
173  $child_query['sql_array']['union_select'] = str_replace(', null AS type_code', '', $child_query['sql_array']['union_select']);
174 
175  $sql = 'SELECT assetid FROM sq_ast_attr_val';
176  $where = ' WHERE assetid IN ('.implode(' ', $child_query['sql_array']).')
177  AND attrid IN (SELECT attrid FROM sq_ast_attr
178  WHERE type_code IN (SELECT type_code FROM sq_ast_typ_inhd
179  WHERE inhd_type_code = :inhd_type_code)
180  AND name = :attr_name)
181  AND custom_val <> :setting';
182 
183  $bind_vars = Array(
184  'inhd_type_code' => 'file',
185  'attr_name' => 'allow_unrestricted',
186  'setting' => (int)$setting,
187  );
188 
189  // Get the assets (so we can update their lookups later)\
190  try {
191  status_message_start('Finding files to change...');
192 
193  $bind_vars = array_merge($bind_vars, $child_query['bind_vars']);
194  $query = MatrixDAL::preparePdoQuery($sql.$where);
195  foreach ($bind_vars as $bind_var => $bind_value) {
196  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
197  }
198  $result = array_keys(MatrixDAL::executePdoGroupedAssoc($query));
199 
200  } catch (Exception $e) {
201  status_message_result('DB ERROR');
202  throw new Exception('Database error: '.$e->getMessage());
203  }
204 
205  // bug fix #4649 set_files_unrestricted.php doesn't change any assets
206  // since we have all the file type asset's assetid we will check and
207  // see if any of them has the attributes not set
208  $sql_query = 'SELECT assetid FROM sq_ast_attr_val l WHERE l.assetid IN (\''.implode('\', \'', array_keys($file_assetids)).'\') AND l.attrid IN (SELECT attrid FROM sq_ast_attr WHERE type_code IN (SELECT type_code FROM sq_ast_typ_inhd WHERE inhd_type_code = \'file\') AND name = \'allow_unrestricted\')';
209 
210  $good_assets = MatrixDAL::executeSqlAssoc($sql_query);
211 
212  $additional_assets = array_keys($file_assetids);
213  foreach ($good_assets as $good_asset) {
214  foreach ($additional_assets as $index => $additional_asset) {
215  if ($additional_assets[$index] == $good_asset['assetid'] ) unset($additional_assets[$index]);
216  }
217  }
218 
219  status_message_result(count($result) + count($additional_assets).' assets to update');
220 
221  // If there were any assets, update them in one hit, and then update
222  // the lookups
223  if ((count($result) + count($additional_assets))> 0) {
224  status_message_start('Updating attributes...');
225 
226  // update
227  try {
228  $update_sql = 'UPDATE sq_ast_attr_val SET custom_val = :new_setting';
229  $bind_vars['new_setting'] = (int)$setting;
230 
231  $query = MatrixDAL::preparePdoQuery($update_sql.$where);
232  foreach ($bind_vars as $bind_var => $bind_value) {
233  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
234  }
235  MatrixDAL::execPdoQuery($query);
236  status_message_result('OK');
237  } catch (Exception $e) {
238  status_message_result('DB ERROR');
239  throw new Exception('Database error: '.$e->getMessage());
240  }
241 
242  // insert
243  foreach($additional_assets as $additional_asset) {
244  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($additional_asset);
245  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
246  $asset->setAttrValue('allow_unrestricted', (int)$setting);
247  $asset->saveAttributes(TRUE);
248  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
249  }
250 
251  // Now update lookups
252  status_message_start('Updating lookups...');
253  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
254  $vars = Array(
255  'assetids' => array_merge($result, $additional_assets),
256  );
257  $errors = $hh->freestyleHipo('hipo_job_update_lookups', $vars);
258  if (empty($errors)) {
259  status_message_result('OK');
260  } else {
261  status_message_result('ERRORS');
262  bam($errors);
263  }
264  }
265 
266  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
267  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
268 
269 }//end do_set_unrestricted()
270 
271 
282 function status_message_start($name) {
283  printf('%-59s ', $name);
284  return;
285 
286 }//end status_message()
287 
288 
298 function status_message_result($status) {
299  printf('[ %-2s ]'."\n", $status);
300  return;
301 
302 }//end status_message()
303 
304 
305 //-- USAGE DISPLAY --//
306 
307 
315 function short_usage()
316 {
317  if (isset($_SERVER['_'])) {
318  $php_name = basename($_SERVER['_']);
319  } else {
320  $php_name = 'php';
321  }
322 
323  echo 'Usage: '.$php_name.' '.$_SERVER['argv'][0].' <system_root> <root_assetid> <setting>'."\n";
324  echo 'Try "'.$php_name.' '.$_SERVER['argv'][0].' --help" for more information.'."\n";
325  return;
326 
327 }//end short_usage()
328 
329 
330 function long_usage()
331 {
332  if (isset($_SERVER['_'])) {
333  $php_name = basename($_SERVER['_']);
334  } else {
335  $php_name = 'php';
336  }
337 
338  echo 'Usage: '.$php_name.' '.$_SERVER['argv'][0].' <system_root> <root_assetid> <setting>'."\n";
339  echo 'Sets all File assets underneath asset #<root_assetid> to either unrestricted or' ."\n";
340  echo 'restricted, depending on the value of <setting>.' ."\n\n";
341 
342  echo 'Assets set as unrestricted will be served directly by your web server (and not ' ."\n";
343  echo 'by MySource Matrix) if they are Live and publicly readable, improving performance.' ."\n\n";
344 
345  echo 'Arguments:' ."\n";
346  echo ' system_root The root directory of this MySource Matrix installation.' ."\n";
347  echo ' root_assetid The ID of the asset to look for File assets underneath.' ."\n";
348  echo ' (This must be provided. Using "1" selects the root folder.)' ."\n";
349  echo ' setting Determines what happens to File assets underneath the root nodes:' ."\n";
350  echo ' 1 Sets Files to "unrestricted".' ."\n";
351  echo ' 0 Sets Files to "restricted".' ."\n";
352  return;
353 
354 }//end long_usage()
355 
356 
362 function error_line($error)
363 {
364  echo $_SERVER['argv'][0].': '.$error."\n";
365  return;
366 
367 }//end error_line()
368 ?>