17 error_reporting(E_ALL);
19 if ((php_sapi_name() !=
'cli')) {
20 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
23 if (count($_SERVER[
'argv']) < 4 || count($_SERVER[
'argv']) > 5) {
24 echo
"This script needs to be run in the following format:\n\n";
25 echo
"\tphp publish_static.php SYSTEM_ROOT asset_ids STORAGE_PATH [--_nocache]\n\n";
26 echo
"\tEg. php scripts/publish_static.php . 21,54,113 /home/static_content\n";
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";
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";
42 if (isset($_SERVER[
'argv'][2])) {
43 $asset_ids = explode(
',', $_SERVER[
'argv'][2]);
46 $STORAGE_PATH = (isset($_SERVER[
'argv'][3])) ? $_SERVER[
'argv'][3] :
'';
47 if (empty($STORAGE_PATH) || !is_dir($STORAGE_PATH)) {
48 echo
'ERROR: The directory you specified as the storage root does not exist, or is not a directory';
52 $_nocache = (isset($_SERVER[
'argv'][4])) ? $_SERVER[
'argv'][4] : FALSE;
53 if ($_nocache !=
'--_nocache') $_nocache = FALSE;
55 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
57 $publish_urls = Array();
58 foreach ($asset_ids as $id) {
59 $urls = $GLOBALS[
'SQ_SYSTEM']->am->getURLs($id);
60 foreach ($urls as $url) {
61 $scheme = ($url[
'https'] == 1) ?
'https' : FALSE;
62 if ($scheme != FALSE) {
63 $publish_urls[] = $scheme.
'://'.$url[
'url'];
64 if ($_nocache) $publish_urls[] = $scheme.
'://'.$url[
'url'].
'/_nocache';
66 $scheme = ($url[
'http'] == 1) ?
'http' : FALSE;
67 if ($scheme != FALSE) {
68 $publish_urls[] = $scheme.
'://'.$url[
'url'];
69 if ($_nocache) $publish_urls[] = $scheme.
'://'.$url[
'url'].
'/_nocache';
74 _disconnectFromMatrixDatabase();
76 while (!empty($publish_urls)) {
77 $publish_url = array_pop($publish_urls);
78 $pid_prepare = pcntl_fork();
80 switch ($pid_prepare) {
82 trigger_error(
'Process failed to fork while publishing static content', E_USER_ERROR);
87 _connectToMatrixDatabase();
90 $user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'public_user');
91 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($user)) {
92 trigger_error(
"Failed logging in as public user\n", E_USER_ERROR);
96 render_static_url($publish_url);
98 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();
100 _disconnectFromMatrixDatabase();
105 if (empty($publish_urls)) {
107 while ($fork_num > 0) {
109 pcntl_waitpid(-1, $status);
119 function render_static_url($url) {
121 global $STORAGE_PATH;
126 $parts = parse_url($url);
128 $protocol =
'HTTP/1.1';
129 $scheme = $parts[
'scheme'];
130 $host = $parts[
'host'];
131 $path = $parts[
'path'];
134 $_SERVER[
'SERVER_PROTOCOL'] = $protocol;
135 $_SERVER[
'REQUEST_METHOD'] = $method;
136 $_SERVER[
'QUERY_STRING'] = $query;
137 $_SERVER[
'REQUEST_URI'] = $path.(empty($query)?
'':
'?').$query;
138 $_SERVER[
'SCRIPT_NAME'] = $path;
139 $_SERVER[
'PHP_SELF'] = $path;
140 $_SERVER[
'HTTP_USER_AGENT'] =
'Static Site Generator';
141 $_SERVER[
'HTTP_HOST'] = $host;
142 $_SERVER[
'SERVER_NAME'] = $host;
146 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
147 $GLOBALS[
'SQ_SYSTEM']->start();
148 $content = ob_get_clean();
150 $storage_dir =
"$STORAGE_PATH/$scheme/$host$path/index.html";
151 if (create_directory(dirname($storage_dir))){
152 file_put_contents($storage_dir, $content);
153 if ($_nocache == FALSE && is_dir(dirname($storage_dir).
'/_nocache')) {
154 delete_directory(dirname($storage_dir).
'/_nocache');
166 function _disconnectFromMatrixDatabase()
169 if (isset($conn_id) && !empty($conn_id)) {
183 function _connectToMatrixDatabase()
185 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');