PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/processors/cache_sync.class.processor.php

https://github.com/dreeman/modx106
PHP | 312 lines | 291 code | 12 blank | 9 comment | 15 complexity | e4467115c7fb15dc935350022fee2415 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // cache & synchronise class
  3. class synccache{
  4. var $cachePath;
  5. var $showReport;
  6. var $deletedfiles = array();
  7. var $aliases = array();
  8. var $parents = array();
  9. function setCachepath($path) {
  10. $this->cachePath = $path;
  11. }
  12. function setReport($bool) {
  13. $this->showReport = $bool;
  14. }
  15. function escapeDoubleQuotes($s) {
  16. $q1 = array("\\","\"","\r","\n","\$");
  17. $q2 = array("\\\\","\\\"","\\r","\\n","\\$");
  18. return str_replace($q1,$q2,$s);
  19. }
  20. function escapeSingleQuotes($s) {
  21. $q1 = array("\\","'");
  22. $q2 = array("\\\\","\\'");
  23. return str_replace($q1,$q2,$s);
  24. }
  25. function getParents($id, $path = '') { // modx:returns child's parent
  26. global $modx;
  27. if(empty($this->aliases)) {
  28. $sql = "SELECT id, IF(alias='', id, alias) AS alias, parent FROM ".$modx->getFullTableName('site_content');
  29. $qh = $modx->db->query($sql);
  30. if ($qh && $modx->db->getRecordCount($qh) > 0) {
  31. while ($row = $modx->db->getRow($qh)) {
  32. $this->aliases[$row['id']] = $row['alias'];
  33. $this->parents[$row['id']] = $row['parent'];
  34. }
  35. }
  36. }
  37. if (isset($this->aliases[$id])) {
  38. $path = $this->aliases[$id] . ($path != '' ? '/' : '') . $path;
  39. return $this->getParents($this->parents[$id], $path);
  40. }
  41. return $path;
  42. }
  43. function emptyCache($modx = null) {
  44. if((function_exists('is_a') && is_a($modx, 'DocumentParser') === false) || get_class($modx) !== 'DocumentParser') {
  45. $modx = $GLOBALS['modx'];
  46. }
  47. if(!isset($this->cachePath)) {
  48. echo "Cache path not set.";
  49. exit;
  50. }
  51. $filesincache = 0;
  52. $deletedfilesincache = 0;
  53. if (function_exists('glob')) {
  54. // New and improved!
  55. $files = glob(realpath($this->cachePath).'/*');
  56. $filesincache = count($files);
  57. $deletedfiles = array();
  58. while ($file = array_shift($files)) {
  59. $name = basename($file);
  60. if (preg_match('/\.pageCache/',$name) && !in_array($name, $deletedfiles)) {
  61. $deletedfilesincache++;
  62. $deletedfiles[] = $name;
  63. unlink($file);
  64. }
  65. }
  66. } else {
  67. // Old way of doing it (no glob function available)
  68. if ($handle = opendir($this->cachePath)) {
  69. // Initialize deleted per round counter
  70. $deletedThisRound = 1;
  71. while ($deletedThisRound){
  72. if(!$handle) $handle = opendir($this->cachePath);
  73. $deletedThisRound = 0;
  74. while (false !== ($file = readdir($handle))) {
  75. if ($file != "." && $file != "..") {
  76. $filesincache += 1;
  77. if ( preg_match("/\.pageCache/", $file) && (!is_array($deletedfiles) || !array_search($file,$deletedfiles)) ) {
  78. $deletedfilesincache += 1;
  79. $deletedThisRound++;
  80. $deletedfiles[] = $file;
  81. unlink($this->cachePath.$file);
  82. } // End if
  83. } // End if
  84. } // End while
  85. closedir($handle);
  86. $handle = '';
  87. } // End while ($deletedThisRound)
  88. }
  89. }
  90. $this->buildCache($modx);
  91. /****************************************************************************/
  92. /* PUBLISH TIME FILE */
  93. /****************************************************************************/
  94. // update publish time file
  95. $timesArr = array();
  96. $sql = 'SELECT MIN(pub_date) AS minpub FROM '.$modx->getFullTableName('site_content').' WHERE pub_date>'.time();
  97. if(@!$result = $modx->db->query($sql)) {
  98. echo 'Couldn\'t determine next publish event!';
  99. }
  100. $tmpRow = $modx->db->getRow($result);
  101. $minpub = $tmpRow['minpub'];
  102. if($minpub!=NULL) {
  103. $timesArr[] = $minpub;
  104. }
  105. $sql = 'SELECT MIN(unpub_date) AS minunpub FROM '.$modx->getFullTableName('site_content').' WHERE unpub_date>'.time();
  106. if(@!$result = $modx->db->query($sql)) {
  107. echo 'Couldn\'t determine next unpublish event!';
  108. }
  109. $tmpRow = $modx->db->getRow($result);
  110. $minunpub = $tmpRow['minunpub'];
  111. if($minunpub!=NULL) {
  112. $timesArr[] = $minunpub;
  113. }
  114. if(count($timesArr)>0) {
  115. $nextevent = min($timesArr);
  116. } else {
  117. $nextevent = 0;
  118. }
  119. // write the file
  120. $filename = $this->cachePath.'/sitePublishing.idx.php';
  121. $somecontent = '<?php $cacheRefreshTime='.$nextevent.'; ?>';
  122. if (!$handle = fopen($filename, 'w')) {
  123. echo 'Cannot open file ('.$filename.')';
  124. exit;
  125. }
  126. // Write $somecontent to our opened file.
  127. if (fwrite($handle, $somecontent) === FALSE) {
  128. echo 'Cannot write publishing info file! Make sure the assets/cache directory is writable!';
  129. exit;
  130. }
  131. fclose($handle);
  132. /****************************************************************************/
  133. /* END OF PUBLISH TIME FILE */
  134. /****************************************************************************/
  135. // finished cache stuff.
  136. if($this->showReport==true) {
  137. global $_lang;
  138. printf($_lang['refresh_cache'], $filesincache, $deletedfilesincache);
  139. $limit = count($deletedfiles);
  140. if($limit > 0) {
  141. echo '<p>'.$_lang['cache_files_deleted'].'</p><ul>';
  142. for($i=0;$i<$limit; $i++) {
  143. echo '<li>',$deletedfiles[$i],'</li>';
  144. }
  145. echo '</ul>';
  146. }
  147. }
  148. }
  149. /**
  150. * build siteCache file
  151. * @param DocumentParser $modx
  152. * @return boolean success
  153. */
  154. function buildCache($modx) {
  155. $tmpPHP = "<?php\n";
  156. // SETTINGS & DOCUMENT LISTINGS CACHE
  157. // get settings
  158. $sql = 'SELECT * FROM '.$modx->getFullTableName('system_settings');
  159. $rs = $modx->db->query($sql);
  160. $limit_tmp = $modx->db->getRecordCount($rs);
  161. $config = array();
  162. $tmpPHP .= '$c=&$this->config;'."\n";
  163. while(list($key,$value) = $modx->db->getRow($rs,'num')) {
  164. $tmpPHP .= '$c[\''.$key.'\']'.' = "'.$this->escapeDoubleQuotes($value)."\";\n";
  165. $config[$key] = $value;
  166. }
  167. // get aliases modx: support for alias path
  168. $tmpPath = '';
  169. $tmpPHP .= '$this->aliasListing = array();' . "\n";
  170. $tmpPHP .= '$a = &$this->aliasListing;' . "\n";
  171. $tmpPHP .= '$d = &$this->documentListing;' . "\n";
  172. $tmpPHP .= '$m = &$this->documentMap;' . "\n";
  173. $sql = 'SELECT IF(alias=\'\', id, alias) AS alias, id, contentType, parent FROM '.$modx->getFullTableName('site_content').' WHERE deleted=0 ORDER BY parent, menuindex';
  174. $rs = $modx->db->query($sql);
  175. $limit_tmp = $modx->db->getRecordCount($rs);
  176. for ($i_tmp=0; $i_tmp<$limit_tmp; $i_tmp++) {
  177. $tmp1 = $modx->db->getRow($rs);
  178. if ($config['friendly_urls'] == 1 && $config['use_alias_path'] == 1) {
  179. $tmpPath = $this->getParents($tmp1['parent']);
  180. $alias= (strlen($tmpPath) > 0 ? "$tmpPath/" : '').$tmp1['alias'];
  181. $alias= $modx->db->escape($alias);
  182. $tmpPHP .= '$d[\''.$alias.'\']'." = ".$tmp1['id'].";\n";
  183. }
  184. else {
  185. $tmpPHP .= '$d[\''.$modx->db->escape($tmp1['alias']).'\']'." = ".$tmp1['id'].";\n";
  186. }
  187. $tmpPHP .= '$a[' . $tmp1['id'] . ']'." = array('id' => ".$tmp1['id'].", 'alias' => '".$modx->db->escape($tmp1['alias'])."', 'path' => '" . $modx->db->escape($tmpPath)."', 'parent' => " . $tmp1['parent']. ");\n";
  188. $tmpPHP .= '$m[]'." = array('".$tmp1['parent']."' => '".$tmp1['id']."');\n";
  189. }
  190. // get content types
  191. $sql = 'SELECT id, contentType FROM '.$modx->getFullTableName('site_content')." WHERE contentType != 'text/html'";
  192. $rs = $modx->db->query($sql);
  193. $limit_tmp = $modx->db->getRecordCount($rs);
  194. $tmpPHP .= '$c = &$this->contentTypes;' . "\n";
  195. for ($i_tmp=0; $i_tmp<$limit_tmp; $i_tmp++) {
  196. $tmp1 = $modx->db->getRow($rs);
  197. $tmpPHP .= '$c['.$tmp1['id'].']'." = '".$tmp1['contentType']."';\n";
  198. }
  199. // WRITE Chunks to cache file
  200. $sql = 'SELECT * FROM '.$modx->getFullTableName('site_htmlsnippets');
  201. $rs = $modx->db->query($sql);
  202. $limit_tmp = $modx->db->getRecordCount($rs);
  203. $tmpPHP .= '$c = &$this->chunkCache;' . "\n";
  204. for ($i_tmp=0; $i_tmp<$limit_tmp; $i_tmp++) {
  205. $tmp1 = $modx->db->getRow($rs);
  206. $tmpPHP .= '$c[\''.$modx->db->escape($tmp1['name']).'\']'." = '".$this->escapeSingleQuotes($tmp1['snippet'])."';\n";
  207. }
  208. // WRITE snippets to cache file
  209. $sql = 'SELECT ss.*,sm.properties as `sharedproperties` '.
  210. 'FROM '.$modx->getFullTableName('site_snippets').' ss '.
  211. 'LEFT JOIN '.$modx->getFullTableName('site_modules').' sm on sm.guid=ss.moduleguid';
  212. $rs = $modx->db->query($sql);
  213. $limit_tmp = $modx->db->getRecordCount($rs);
  214. $tmpPHP .= '$s = &$this->snippetCache;' . "\n";
  215. for ($i_tmp=0; $i_tmp<$limit_tmp; $i_tmp++) {
  216. $tmp1 = $modx->db->getRow($rs);
  217. $tmpPHP .= '$s[\''.$modx->db->escape($tmp1['name']).'\']'." = '".$this->escapeSingleQuotes($tmp1['snippet'])."';\n";
  218. // Raymond: save snippet properties to cache
  219. if ($tmp1['properties']!=""||$tmp1['sharedproperties']!="") $tmpPHP .= '$s[\''.$tmp1['name'].'Props\']'." = '".$this->escapeSingleQuotes($tmp1['properties']." ".$tmp1['sharedproperties'])."';\n";
  220. // End mod
  221. }
  222. // WRITE plugins to cache file
  223. $sql = 'SELECT sp.*,sm.properties as `sharedproperties`'.
  224. 'FROM '.$modx->getFullTableName('site_plugins').' sp '.
  225. 'LEFT JOIN '.$modx->getFullTableName('site_modules').' sm on sm.guid=sp.moduleguid '.
  226. 'WHERE sp.disabled=0';
  227. $rs = $modx->db->query($sql);
  228. $limit_tmp = $modx->db->getRecordCount($rs);
  229. $tmpPHP .= '$p = &$this->pluginCache;' . "\n";
  230. for ($i_tmp=0; $i_tmp<$limit_tmp; $i_tmp++) {
  231. $tmp1 = $modx->db->getRow($rs);
  232. $tmpPHP .= '$p[\''.$modx->db->escape($tmp1['name']).'\']'." = '".$this->escapeSingleQuotes($tmp1['plugincode'])."';\n";
  233. if ($tmp1['properties']!=''||$tmp1['sharedproperties']!='') $tmpPHP .= '$p[\''.$tmp1['name'].'Props\']'." = '".$this->escapeSingleQuotes($tmp1['properties'].' '.$tmp1['sharedproperties'])."';\n";
  234. }
  235. // WRITE system event triggers
  236. $sql = 'SELECT sysevt.name as `evtname`, pe.pluginid, plugs.name
  237. FROM '.$modx->getFullTableName('system_eventnames').' sysevt
  238. INNER JOIN '.$modx->getFullTableName('site_plugin_events').' pe ON pe.evtid = sysevt.id
  239. INNER JOIN '.$modx->getFullTableName('site_plugins').' plugs ON plugs.id = pe.pluginid
  240. WHERE plugs.disabled=0
  241. ORDER BY sysevt.name,pe.priority';
  242. $events = array();
  243. $rs = $modx->db->query($sql);
  244. $limit_tmp = $modx->db->getRecordCount($rs);
  245. $tmpPHP .= '$e = &$this->pluginEvent;' . "\n";
  246. for ($i=0; $i<$limit_tmp; $i++) {
  247. $evt = $modx->db->getRow($rs);
  248. if(!$events[$evt['evtname']]) $events[$evt['evtname']] = array();
  249. $events[$evt['evtname']][] = $evt['name'];
  250. }
  251. foreach($events as $evtname => $pluginnames) {
  252. $tmpPHP .= '$e[\''.$evtname.'\'] = array(\''.implode("','",$this->escapeSingleQuotes($pluginnames))."');\n";
  253. }
  254. // close and write the file
  255. $tmpPHP .= "\n";
  256. $filename = $this->cachePath.'siteCache.idx.php';
  257. $somecontent = $tmpPHP;
  258. // invoke OnBeforeCacheUpdate event
  259. if ($modx) $modx->invokeEvent('OnBeforeCacheUpdate');
  260. if (!$handle = fopen($filename, 'w')) {
  261. echo 'Cannot open file (',$filename,')';
  262. exit;
  263. }
  264. // Write $somecontent to our opened file.
  265. if (fwrite($handle, $somecontent) === FALSE) {
  266. echo 'Cannot write main MODx cache file! Make sure the assets/cache directory is writable!';
  267. exit;
  268. }
  269. fclose($handle);
  270. // invoke OnCacheUpdate event
  271. if ($modx) $modx->invokeEvent('OnCacheUpdate');
  272. return true;
  273. }
  274. }
  275. ?>