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);
55 $ARGV = $_SERVER[
'argv'];
56 switch (count($ARGV)) {
58 error_line(
'You must specify a system root to your MySource Matrix installation');
65 if ($ARGV[1] ==
'--help') {
69 error_line(
'You must provide the asset ID of the root node to search under');
76 error_line(
'You must tell the script whether files are to be restricted or unrestricted');
82 list($SYSTEM_ROOT, $ROOT_ASSETID, $UNRESTRICT_SETTING) = array_slice($ARGV, 1, 3);
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.');;
89 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
92 if (!assert_valid_assetid($ROOT_ASSETID,
'', TRUE, FALSE)) {
93 error_line(
'Root asset ID "'.$ROOT_ASSETID.
'" does not appear to be valid');
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');
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)');
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";
113 if (count($file_assetids) > 0) {
115 echo
'Enter the root password for "'.SQ_CONF_SYSTEM_NAME.
'": ';
116 system(
'stty -echo');
117 $root_password = rtrim(fgets(STDIN, 4094));
121 $root_user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
122 if (!$root_user->comparePassword($root_password)) {
123 error_line(
'Root password incorrect'.
"\n");
128 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
129 error_line(
'Could not log in as root user');
134 do_set_unrestricted($ROOT_ASSETID, $UNRESTRICT_SETTING, $file_assetids);
138 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();
158 function do_set_unrestricted($root_node, $setting, $file_assetids)
160 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
161 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
168 $root_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($root_node);
169 $child_query = $GLOBALS[
'SQ_SYSTEM']->am->generateGetChildrenQuery($root_asset,
'file', FALSE);
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']);
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';
184 'inhd_type_code' =>
'file',
185 'attr_name' =>
'allow_unrestricted',
186 'setting' => (
int)$setting,
191 status_message_start(
'Finding files to change...');
193 $bind_vars = array_merge($bind_vars, $child_query[
'bind_vars']);
195 foreach ($bind_vars as $bind_var => $bind_value) {
200 }
catch (Exception $e) {
201 status_message_result(
'DB ERROR');
202 throw new Exception(
'Database error: '.$e->getMessage());
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\')';
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]);
219 status_message_result(count($result) + count($additional_assets).
' assets to update');
223 if ((count($result) + count($additional_assets))> 0) {
224 status_message_start(
'Updating attributes...');
228 $update_sql =
'UPDATE sq_ast_attr_val SET custom_val = :new_setting';
229 $bind_vars[
'new_setting'] = (int)$setting;
232 foreach ($bind_vars as $bind_var => $bind_value) {
236 status_message_result(
'OK');
237 }
catch (Exception $e) {
238 status_message_result(
'DB ERROR');
239 throw new Exception(
'Database error: '.$e->getMessage());
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();
252 status_message_start(
'Updating lookups...');
253 $hh = $GLOBALS[
'SQ_SYSTEM']->getHipoHerder();
255 'assetids' => array_merge($result, $additional_assets),
257 $errors = $hh->freestyleHipo(
'hipo_job_update_lookups', $vars);
258 if (empty($errors)) {
259 status_message_result(
'OK');
261 status_message_result(
'ERRORS');
266 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
267 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
282 function status_message_start($name) {
283 printf(
'%-59s ', $name);
298 function status_message_result($status) {
299 printf(
'[ %-2s ]'.
"\n", $status);
315 function short_usage()
317 if (isset($_SERVER[
'_'])) {
318 $php_name = basename($_SERVER[
'_']);
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";
330 function long_usage()
332 if (isset($_SERVER[
'_'])) {
333 $php_name = basename($_SERVER[
'_']);
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";
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";
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";
362 function error_line($error)
364 echo $_SERVER[
'argv'][0].
': '.$error.
"\n";