PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/cacheextender/orig/cache_sync.class.processor.php

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