PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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