PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/openconstructor/lib/site/pagecompiler._wc

http://openconstructor.googlecode.com/
Unknown | 655 lines | 630 code | 25 blank | 0 comment | 0 complexity | 38c76019a0c147dbadc52e154ad45c2c MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright 2003 - 2007 eSector Solutions, LLC
  4. *
  5. * All rights reserved.
  6. *
  7. * This file is part of Open Constructor (http://www.openconstructor.org/).
  8. *
  9. * Open Constructor is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * Open Constructor is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * The GNU General Public License can be found at
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @author Sanjar Akhmedov
  22. *
  23. * $Id: pagecompiler._wc,v 1.44 2007/03/10 20:31:18 sanjar Exp $
  24. */
  25. define('PAGE_LOCATION_EMPTY', 0);
  26. define('PAGE_LOCATION_EXT', 1);
  27. define('PAGE_LOCATION_INT', 2);
  28. define('PAGE_LOCATION_ROUTE', 3);
  29. class PageCompiler {
  30. var $reader, $tree;
  31. function PageCompiler() {
  32. $this->reader = & new PageReader();
  33. }
  34. function &compile(&$page) {
  35. $result = null;
  36. $location = $this->_parsePageLocation($page);
  37. switch($location['type']) {
  38. case PAGE_LOCATION_EMPTY:
  39. if(!$page->router) {
  40. $result = &$this->compilePlainPage($page, $location['query']);
  41. $result[$page->uri.'index._wc'] = null;
  42. } else
  43. $result = &$this->compileRoutedPage($page, $page->uri, $page->name);
  44. break;
  45. case PAGE_LOCATION_EXT:
  46. $result = &$this->compileRedirectPage($page, $location['page']);
  47. $result[$page->uri.'index._wc'] = null;
  48. break;
  49. case PAGE_LOCATION_INT:
  50. $result = &$this->compileLinkedPage($page, $location['page'], $location['query']);
  51. $result[$page->uri.'index._wc'] = null;
  52. break;
  53. case PAGE_LOCATION_ROUTE:
  54. $result = &$this->compileRoutedPage($page, $location['page'], $location['name'], $location['param'], $location['query']);
  55. break;
  56. }
  57. return $result;
  58. }
  59. function generateSitemap() {
  60. require_once(LIBDIR.'/tree/export/sitemapview._wc');
  61. $tree = &$this->_getSiteTree(true);
  62. return "<?php\n".
  63. PageCompiler::_getWcSign('').
  64. "\n".
  65. $tree->export(new TreeSiteMapView()).
  66. "?>";
  67. }
  68. function &compilePlainPage(&$page, $query = '') {
  69. $result = null;
  70. $content = "<?php\n".
  71. PageCompiler::_getWcSign().
  72. "\n".
  73. PageCompiler::_getQueryDef($query).
  74. $this->_getCommonPart($page).
  75. "\n".
  76. $this->_getAuthenticatingPart($page).
  77. "\n".
  78. PageCompiler::_getPageBuildingPart($page).
  79. "\n".
  80. PageCompiler::_getOutputPart($page).
  81. "?>";
  82. $result = array($page->uri.'index.php' => $content);
  83. return $result;
  84. }
  85. function &compileRedirectPage(&$page, $href) {
  86. $result = null;
  87. $content = "<?php\n".
  88. PageCompiler::_getWcSign().
  89. "\n".
  90. sprintf("\theader('Location: %s');\n", $href{0} == '/' ? "http://'.\$_SERVER['HTTP_HOST'].'".addslashes($href) : addslashes($href)).
  91. "\tdie();\n".
  92. "?>";
  93. $result = array($page->uri.'index.php' => $content);
  94. return $result;
  95. }
  96. function &compileLinkedPage(&$page, $link, $query = '') {
  97. $result = null;
  98. $content = "<?php\n".
  99. PageCompiler::_getWcSign().
  100. "\n".
  101. PageCompiler::_getRecurseCheck($page).
  102. "\n".
  103. PageCompiler::_getQueryDef($query).
  104. "\tinclude(\$_SERVER['DOCUMENT_ROOT'].'$link');\n".
  105. "\tdie();\n".
  106. "?>";
  107. $result = array($page->uri.'index.php' => $content);
  108. return $result;
  109. }
  110. function &compileRoutedPage(&$page, $route, $name, $pathInfo = '/', $query = '') {
  111. $result = null;
  112. if($page->uri == $route || $page->router) {
  113. $result = &$this->compilePlainPage($page, $query);
  114. $result[$page->uri.'index._wc'] = $result[$page->uri.'index.php'];
  115. } else
  116. $result = array($page->uri.'index._wc' => null);
  117. $routerContent = "<?php\n".
  118. PageCompiler::_getWcSign().
  119. "\n".
  120. PageCompiler::_getRecurseCheck($page).
  121. "\n".
  122. ($page->uri != $route ? PageCompiler::_getQueryDef($query) : '').
  123. "\trequire_once(\$_SERVER['DOCUMENT_ROOT'].'".WCHOME."/lib/router._wc');\n";
  124. if($page->uri == $route)
  125. $routerContent .= sprintf(
  126. "\t\$info = isset(\$_SERVER['PATH_INFO']) ? \$_SERVER['PATH_INFO'] : '%s';\n", addslashes($pathInfo)
  127. );
  128. else
  129. $routerContent .= sprintf(
  130. "\t\$info = '%s';\n", addslashes($pathInfo)
  131. );
  132. $routerContent .=
  133. "\t\$router = & new Router('$route', '$name', \$info);\n";
  134. if($route != $page->uri) {
  135. $tree = &$this->_getSiteTree();
  136. $router = $tree->getPageByUri($route);
  137. $routerContent .= sprintf(
  138. "\t@include(\$_SERVER['DOCUMENT_ROOT'].'$route'.(\$router->target ? \$router->target.'/index.php' : 'index.%s'));\n"
  139. , $this->reader->getPageRouter($router->id) == $router->id ? '_wc' : 'php'
  140. );
  141. } else {
  142. $routerContent .=
  143. "\t@include(\$_SERVER['DOCUMENT_ROOT'].'$route'.(\$router->target ? \$router->target.'/index.php' : 'index._wc'));\n";
  144. }
  145. $routerContent .=
  146. "\tinclude(\$_SERVER['DOCUMENT_ROOT'].'".WCHOME."/404.php');\n".
  147. "\tdie();\n".
  148. "?>";
  149. $result[$page->uri.'index.php'] = $routerContent;
  150. return $result;
  151. }
  152. function _getQueryDef($queryString, $indent = "\t") {
  153. $result = '';
  154. if($queryString) {
  155. $query = array();
  156. parse_str($queryString, $query);
  157. foreach($query as $k => $v)
  158. if($k{0} == '!' && strlen($k) > 1) {
  159. $result .= sprintf(
  160. "{$indent}\$_GET['%1\$s'] = '%2\$s';\n"
  161. , addslashes(substr($k, 1)), addslashes($v)
  162. );
  163. } else {
  164. $result .= sprintf(
  165. "{$indent}if(!isset(\$_GET['%1\$s']))\n{$indent}\t\$_GET['%1\$s'] = '%2\$s';\n"
  166. , addslashes($k), addslashes($v)
  167. );
  168. }
  169. }
  170. return $result;
  171. }
  172. function _getCommonPart(&$page, $i = "\t") {
  173. static $files;
  174. $result = '';
  175. if(!is_array($files)) {
  176. $files = array(
  177. "\$_SERVER['DOCUMENT_ROOT'].'/openconstructor/lib/commons._wc'",
  178. "LIBDIR.'/context._wc'",
  179. "LIBDIR.'/security/siteauthentication._wc'",
  180. "LIBDIR.'/site/webpage._wc'",
  181. "LIBDIR.'/site/sitemap._wc'",
  182. );
  183. }
  184. $router = $this->reader->getPageRouter($page);
  185. if($router)// && $router != $page->id)
  186. $result .=
  187. $i."if(!isset(\$router)) {\n".
  188. $i." @include(\$_SERVER['DOCUMENT_ROOT'].'{$GLOBALS['_wchome']}/404.php');\n".
  189. $i." die();\n".
  190. $i."}\n\n";
  191. $result .=
  192. $i."require_once(".implode(");\n{$i}require_once(", $files).");\n".
  193. "\n".
  194. $i."\$ctx = &Context::getInstance();\n";
  195. if($router) {
  196. $result .= $i."\$ctx->router = &\$router;\n";
  197. } else {
  198. $result .=
  199. $i."if(isset(\$router))\n".
  200. $i." \$ctx->router = &\$router;\n";
  201. }
  202. $phpcallback = array();
  203. $objects = &$page->getObjects();
  204. foreach($objects as $id => $obj)
  205. if($obj['observer'] && $obj['type'] == 'phpcallback' && $obj['block'] != '')
  206. $phpcallback[] = sprintf("\$ctx->_attachEventListener('%s', %s);", $obj['block'], PageCompiler::_getCreateObserverStatement($id));
  207. if(sizeof($phpcallback))
  208. $result .=
  209. "\n".
  210. $i."require_once(LIBDIR.'/wcobject._wc');\n".
  211. $i."require_once(LIBDIR.'".PageCompiler::_getClassFile('phpcallback')."');\n".
  212. $i.implode("\n$i", $phpcallback)."\n".
  213. "\n";
  214. $result .=
  215. $i."\$ctx->_fireEvent('onInitialize');\n".
  216. $i."_register_phase('Initializing');\n".
  217. "\n".
  218. $i."require_once(ROOT.FILES.'/map._wc');\n".
  219. $i."\$ctx->map = &\$map;\n".
  220. $i.sprintf("\$page = &\$map->_newPage($page->id, '%s');\n", PageCompiler::_escapeSingleQuotes($this->_getPageTitle($page))).
  221. $i."\$ctx->page = &\$page;\n".
  222. $i."\$ctx->_fireEvent('onLoadSitemap');\n".
  223. $i."_register_phase('Loading sitemap');\n";
  224. return $result;
  225. }
  226. function _getAuthenticatingPart(&$page, $i = "\t") {
  227. $profiles = $this->_getPageProfileProps($page);
  228. $result =
  229. $i."if(\$page->isSecure()) {\n".
  230. $i." session_start();\n".
  231. $i." require_once(LIBDIR.'/security/resource._wc');\n".
  232. $i." require_once(LIBDIR.'/security/wcs._wc');\n".
  233. $i." Authentication::importFromSession();\n".
  234. $i." \$page->authenticationPage = _getAuthenticationPage();\n".
  235. $i." \$page->sRes = & new WCSResource(&\$page->uri, WCS_ROOT_ID, 0);\n";
  236. $tree = &$this->_getSiteTree();
  237. $node = &$tree->node[$page->id];
  238. $authCheck = array();
  239. while(($node = &$node->parent) != null)
  240. $authCheck[] =
  241. $i." \$page->sRes->setGroup(\$map->id[$node->id]['usr']);\t// ".$node->getFullKey('/')."\n".
  242. $i." WCS::requireAuthentication(&\$page);\n";
  243. $authCheck[] =
  244. $i." \$ctx->_fireEventArgs('onBeforeAuth', \$args = array('page' => &\$page)); unset(\$args);\n";
  245. $result .= implode('', array_reverse($authCheck)).
  246. $i." \$page->sRes->setGroup(\$map->id[$page->id]['usr']);\t// $page->uri\n".
  247. $i." WCS::requireAuthentication(&\$page);\n".
  248. $i." _register_phase('Authenticating user');\n".
  249. $i." \$auth = &SiteAuthentication::createFrom(Authentication::getInstance());\n".
  250. $i."} elseif(isset(\$_COOKIE[session_name()])) {\n".
  251. $i." session_start();\n".
  252. $i." \$auth = &SiteAuthentication::createFromSession();\n".
  253. $i."} else {\n".
  254. $i." \$auth = new SiteAuthentication();\n".
  255. $i."}\n";
  256. if($profiles['load'] > 0)
  257. $result .=
  258. $i.sprintf("\$auth->_loadProfile(%d, %s);\n", $profiles['load'], $profiles['dynamic'] ? 'true' : 'false');
  259. $result .=
  260. $i."\$ctx->_setAuthentication(\$auth); unset(\$auth);\n";
  261. return $result;
  262. }
  263. function _getPageBuildingPart(&$page, $i = "\t") {
  264. $result =
  265. $i."define('WCI_ENABLED', WCI_ALLOWED && \$ctx->auth->isWciEditor());\n".
  266. "\n".
  267. $i."require_once(LIBDIR.'/smarty/wcsmarty._wc');\n".
  268. $i."\$smarty = & new WCSmarty();\n".
  269. $i."\$smarty->_ctx = &\$ctx;\n".
  270. $i."\$ctx->_smarty = &\$smarty;\n".
  271. $i."\$ctx->_fireEvent('onCreateSmarty');\n".
  272. $i."_register_phase('Creating Smarty instance');\n".
  273. "\n".
  274. $i.sprintf("\$page->setTemplate('%s.tpl');\n", $page->tpl > 0 ? $page->tpl : "file:'.LIBDIR.'/tpl/page");
  275. if($page->caching) {
  276. $cid = PageCompiler::_compileCacheVary($page->cacheVary);
  277. $result .=
  278. $i."\$smarty->caching = !WCI_ENABLED && WC_PAGE_CACHING ? 2 : 0;\n".
  279. $i.sprintf("\$smarty->cache_lifetime = %d;\n", $page->cacheLife ? $page->cacheLife : -1).
  280. $i.sprintf("\$page->_cacheGz = \$smarty->caching && WC_PAGE_CACHE_GZ && %s && !headers_sent() && (strpos(@\$_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false);\n", $page->cacheGz ? 'true' : 'false');
  281. if($cid)
  282. $result .= $i."if(\$ctx->router) \$r = &\$ctx->router->param; else \$r = array();\n";
  283. $result .=
  284. $i.sprintf("\$page->_setUID(\"{\$page->id}%s\n", $cid ? "|\".sprintf('%x', crc32($cid)));" : '");').
  285. $i.sprintf("\$page->_cacheId = \"p\".\$page->_uid.(\$page->_cacheGz ? '%1\$sgz' : '');\n", $cid ? '-' : '|');
  286. if($cid)
  287. $result .= $i."unset(\$r);\n";
  288. $result .=
  289. $i."\$_isCached = false;\n".
  290. $i."if(!\$smarty->caching || !(\$_isCached = \$smarty->is_cached(\$page->tpl, \$page->_cacheId))) {\n".
  291. PageCompiler::_getPageBuildingCore($page, strlen($i) ? $i.$i{0} : "\t").
  292. $i."} elseif(\$_isCached) {\n".
  293. $i." \$page->_eTag = '\"'.\$page->id.sprintf('-%x-%d', crc32(\$page->_uid), \$smarty->_cache_info['timestamp']).'\"';\n".
  294. $i." if(@\$_SERVER['HTTP_IF_NONE_MATCH'] == \$page->_eTag)\n".
  295. $i." \$page->status = 304;\n".
  296. $i."}\n";
  297. } else {
  298. $result .=
  299. $i."\$smarty->caching = 0;\n".
  300. "\n".
  301. PageCompiler::_getPageBuildingCore($page, $i);
  302. }
  303. return $result;
  304. }
  305. function _getPageBuildingCore(&$page, $i = "\t") {
  306. static $robots;
  307. if(!is_array($robots))
  308. $robots = array(
  309. ROBOTS_I_F => 'INDEX,FOLLOW',
  310. ROBOTS_I_NOF => 'INDEX,NOFOLLOW',
  311. ROBOTS_NOI_F => 'NOINDEX,FOLLOW',
  312. ROBOTS_NOI_NOF => 'NOINDEX,NOFOLLOW'
  313. );
  314. $result = '';
  315. $contentType = $this->reader->getPageContentType($page);
  316. $links = $this->reader->getPageLinks($page->id);
  317. if(sizeof($links)) {
  318. $tree = &$this->_getSiteTree();
  319. foreach($links as $id)
  320. $linkUris[] = $tree->node[$id]->getFullKey('/');
  321. $result .= sprintf($i."\$page->links = array(%s); // %s\n", implode(', ', $links), implode(', ', $linkUris));
  322. unset($linkUris);
  323. }
  324. $result .=
  325. $i."require_once(LIBDIR.'/site/pagecrumbs._wc');\n".
  326. $i."\$ctx->crumbs = &Crumbs::getInstance();\n".
  327. "\n".
  328. $i."\$page->robots = '{$robots[$page->robots]}';\n".
  329. $i.sprintf("\$page->keywords = '%s';\n", @htmlspecialchars($page->meta['keywords'], ENT_QUOTES, 'UTF-8')).
  330. $i.sprintf("\$page->description = '%s';\n", @htmlspecialchars($page->meta['description'], ENT_QUOTES, 'UTF-8')).
  331. $i."\$page->generator = 'Open Constructor '.(WC_MODE_DEBUG ? WC_VERSION_FULL : WC_VERSION);\n";
  332. if(sizeof($page->css) > 1)
  333. $result .= $i.sprintf("\$page->setStyleSheet(array(FILES.'/css/%s'));\n", implode("', FILES.'/css/", $page->css));
  334. else
  335. $result .= $i.sprintf("\$page->setStyleSheet(%s);\n", sizeof($page->css) ? "FILES.'/css/{$page->css[0]}'" : "''");
  336. foreach(PageCompiler::_getFavicons() as $icon)
  337. $result .= $i.sprintf("\$page->addFavicon('%s', '%s'%s);\n", $icon[0], $icon[1], isset($icon[2]) ? ", '{$icon[2]}'" : '');
  338. $result .=
  339. $i.sprintf("\$page->setContentType('%s');\n", $contentType).
  340. $i.sprintf("\$page->_caching = \$smarty->caching && %s;\n", $page->caching ? 'true' : 'false').
  341. "\n";
  342. $includes = array();
  343. $objects = &$page->getObjects();
  344. foreach($objects as $id => $obj)
  345. if($obj['block'] != '' && ($filename = PageCompiler::_getClassFile($obj['type'])) != null)
  346. $includes[$filename] = "require_once(LIBDIR.'$filename');";
  347. if(sizeof($includes)) {
  348. $result .=
  349. $i."require_once(LIBDIR.'/wcobject._wc');\n".
  350. $i.implode("\n$i", $includes)."\n".
  351. "\n".
  352. $i."\$objects = &\$page->_getObjects();\n".
  353. $i."\$ctx->_fireEventArgs('onLoadObjects', \$args = array('objects' => &\$objects)); unset(\$args, \$objects);\n".
  354. $i."\$ctx->_registerObjects(\$page->_getObjects());\n".
  355. $i."_register_phase('Loading objects');\n".
  356. $i."\$ctx->_fireEvent('onLoadBlocks');\n".
  357. $i."_register_phase('Registering objects');\n".
  358. "\n".
  359. $i."\$ctx->_buildCrumbs();\n".
  360. $i."_register_phase('Building crumbs');\n".
  361. "\n";
  362. }
  363. $result .=
  364. $i."\$smarty->assign_by_ref('ctx', \$ctx);\n".
  365. $i."\$smarty->assign_by_ref('page', \$ctx->page);\n".
  366. $i."\$smarty->assign_by_ref('map', \$ctx->map);\n".
  367. $i."\$smarty->assign_by_ref('idMap', \$ctx->map->id);\n".
  368. $i."\$smarty->assign_by_ref('uriMap', \$ctx->map->uri);\n".
  369. $i."\$ctx->_fireEvent('onSmartyAssign');\n";
  370. if(sizeof($objects)) {
  371. $pre = array();
  372. foreach($objects as $id => $obj)
  373. if($obj['block'] == 'PRE' && ($obj['type'] == 'phpinclude' || $obj['type'] == 'phpcallback'))
  374. $pre[] = "\$ctx->objects[$id]->run();";
  375. if(sizeof($pre))
  376. $result .= $i.implode("\n$i", $pre)."\n\n";
  377. }
  378. return $result;
  379. }
  380. function _getOutputPart(&$page, $i = "\t") {
  381. $result = '';
  382. $result .=
  383. $i."if(\$page->status == 404 || \$page->status == 304) {\n".
  384. $i." \$content = null;\n".
  385. $i."} else {\n".
  386. $i." if(\$page->_caching && \$page->_cacheGz)\n".
  387. $i." \$smarty->register_outputfilter('smarty_outputfilter_gzip');\n".
  388. $i." \$content = \$smarty->fetch(\$page->tpl, \$page->_cacheId);\n".
  389. $i."}\n".
  390. $i."_register_phase('Fetching content');\n".
  391. "\n".
  392. $i."switch(\$page->status) {\n".
  393. $i." case 404:\n".
  394. $i." if(!WCI_ENABLED && WC_PAGE_CACHING && \$smarty->caching)\n".
  395. $i." \$smarty->clear_cache(null, \$page->_cacheId);\n".
  396. $i." unset(\$content, \$smarty, \$page, \$map, \$__report);\n".
  397. $i." if(defined('WC_STATUS_404')) {\n".
  398. $i." @include(\$_SERVER['DOCUMENT_ROOT'].WCHOME.'/404.php');\n".
  399. $i." } else\n".
  400. $i." header('Location: http://'.\$_SERVER['HTTP_HOST'].WCHOME.'/404.php?from='.urlencode(\$_SERVER['REQUEST_URI']));\n".
  401. $i." die();\n".
  402. $i." break;\n".
  403. $i." case 304:\n".
  404. $i." header('HTTP/1.1 304 Not Modified');\n".
  405. $i." header('Cache-control: public, must-revalidate');\n".
  406. $i." break;\n".
  407. $i." default:\n".
  408. $i." \$printReport = \$page->isCommentable() && !\$page->_cacheGz && (WC_MODE_DEBUG || \$ctx->auth->groupId == WCS_ADMINS_ID);\n".
  409. $i." \$bufferSize = sendOutput(\$content, !\$printReport);\n".
  410. $i." _register_phase('Sending output [ '.number_format(strlen(\$content)).' bytes, buffer = '.number_format(\$bufferSize).' bytes ]');\n".
  411. $i." if(\$printReport)\n".
  412. $i." print_report();\n".
  413. $i." unset(\$content, \$smarty, \$ctx, \$map, \$page, \$__report);\n".
  414. $i."}\n".
  415. $i."die();\n";
  416. return $result;
  417. }
  418. function _getCreateObserverStatement($objId) {
  419. require_once(LIBDIR.'/objmanager._wc');
  420. $result = 'null';
  421. $obj = ObjManager::load($objId);
  422. if($obj)
  423. switch($obj->obj_type) {
  424. case 'phpcallback':
  425. $srcId = file_exists(ROOT.FILES.$obj->sourcePath.intval($obj->source).'._wc') ? $obj->source : 0;
  426. $result = sprintf("PHPCallback::_newCallback(%d, '%s')", $srcId, addslashes($obj->name));
  427. break;
  428. }
  429. return $result;
  430. }
  431. function _getPageTitle(&$page) {
  432. $result = array($page->cTitle);
  433. if($page->addTitle) {
  434. $db = &WCDB::bo();
  435. $res = $db->query(
  436. "SELECT p.addtitle, p.ctitle".
  437. " FROM {$this->reader->treeTable} t1, {$this->reader->treeTable} t2, {$this->reader->pagesTable} p".
  438. " WHERE t1.id = {$page->id} AND t2.num < t1.num AND (t2.next = 0 OR t2.next > t1.num) AND t2.id = p.id".
  439. " ORDER BY t2.num DESC"
  440. );
  441. if(mysql_num_rows($res) > 0) {
  442. $at = true;
  443. while($at && ($r = mysql_fetch_assoc($res))) {
  444. $result[] = $r['ctitle'];
  445. $at = $r['addtitle'] > 0;
  446. }
  447. }
  448. mysql_free_result($res);
  449. $result = array_reverse($result);
  450. }
  451. return implode('.', $result);
  452. }
  453. function _getPageProfileProps(&$page) {
  454. $result = array('load' => $page->profilesLoad, 'dynamic' => $page->profilesDynamic);
  455. if($page->profilesInherit) {
  456. $db = &WCDB::bo();
  457. $res = $db->query(
  458. "SELECT p.profilesload, p.profilesdynamic".
  459. " FROM {$this->reader->treeTable} t1, {$this->reader->treeTable} t2, {$this->reader->pagesTable} p".
  460. " WHERE t1.id = {$page->id} AND t2.num < t1.num AND (t2.next = 0 OR t2.next > t1.num) AND t2.id = p.id AND (p.profilesinherit = 0 OR t2.num = 0)".
  461. " ORDER BY t2.num DESC".
  462. " LIMIT 1"
  463. );
  464. $r = mysql_fetch_row($res);
  465. mysql_free_result($res);
  466. $result['load'] = (int) @$r[0];
  467. $result['dynamic'] = @$r[1] > 0;
  468. }
  469. return $result;
  470. }
  471. function _parsePageLocation(&$page) {
  472. $result = array('type' => PAGE_LOCATION_EMPTY, 'query' => '');
  473. if(!empty($page->location)) {
  474. $result['type'] = PAGE_LOCATION_EXT;
  475. $result['page'] = $page->location;
  476. if($page->linkTo > 0) {
  477. $tree = &$this->_getSiteTree();
  478. $result['type'] = PAGE_LOCATION_INT;
  479. $result['page'] = $tree->node[$page->linkTo]->getFullKey('/').'index.php';
  480. } elseif($page->location{0} == '/') {
  481. $url = parse_url("http://{$_SERVER['HTTP_HOST']}{$page->location}");
  482. if(substr($url['path'], -10) == '/index.php' && empty($url['fragment'])) {
  483. $uri = substr($url['path'], 0, -9);
  484. $tree = &$this->_getSiteTree();
  485. if($tree->uriExists($uri)) {
  486. if($uri == $page->uri) {
  487. $result['type'] = PAGE_LOCATION_EMPTY;
  488. $result['page'] = '';
  489. } else {
  490. $result['type'] = PAGE_LOCATION_INT;
  491. $result['page'] = $uri.'index.php';
  492. }
  493. $result['query'] = @$url['query'];
  494. }
  495. } elseif(($p = utf8_strpos($url['path'], '/index.php/')) !== false && empty($url['fragment'])) {
  496. $uri = utf8_substr($url['path'], 0, $p + 1);
  497. $tree = &$this->_getSiteTree();
  498. if($tree->uriExists($uri)) {
  499. $result['type'] = PAGE_LOCATION_ROUTE;
  500. $result['page'] = $uri;
  501. $route = $tree->getPageByUri($uri);
  502. $result['name'] = $route->key;
  503. $result['param'] = utf8_substr($url['path'], $p + 10); // 10 = strlen('/index.php/');
  504. $result['query'] = (string) @$url['query'];
  505. unset($route);
  506. }
  507. }
  508. }
  509. }
  510. return $result;
  511. }
  512. function &_getSiteTree($reload = false) {
  513. if($reload || !is_object($this->tree))
  514. $this->tree = &$this->reader->getTree();
  515. return $this->tree;
  516. }
  517. function _compileCacheVary($cacheVary) {
  518. $result = '';
  519. $params = array();
  520. $found = preg_match_all('/\{(ctx|get|route)\.([\-\.a-z0-9_]+)\}/ui', $cacheVary, $params);
  521. if($found) {
  522. foreach($params[1] as $i => $v)
  523. switch($v) {
  524. case 'ctx': $result[] = sprintf('$ctx->getParam(\'%s\')', $params[2][$i]); break;
  525. case 'get': $result[] = sprintf('@$_GET[\'%s\']', $params[2][$i]); break;
  526. case 'route': $result[] = sprintf('@$r[\'%s\']', $params[2][$i]); break;
  527. }
  528. $result = implode('.chr(255).', $result);
  529. }
  530. return $result;
  531. }
  532. function _getWcSign($indent = "\t") {
  533. static $sign;
  534. if(!is_array($sign)) {
  535. $auth = &Authentication::getOriginal();
  536. $sign = array(
  537. "// Generated by Open Constructor ".WC_VERSION_FULL." [{$auth->userLogin}]",
  538. '// '.date('D, j M Y H:i:s'),
  539. '',
  540. '// Do not edit this file. All changes that were made manually will be overwritten.'
  541. );
  542. }
  543. return $indent.implode("\n$indent", $sign)."\n";
  544. }
  545. function _escapeSingleQuotes($str) {
  546. return strtr($str, array('\'' => '\\\'', '\\' => '\\\\'));
  547. }
  548. function _getRecurseCheck(&$page, $i = "\t") {
  549. $uid = 'WCP_'.sprintf('%X', crc32($page->id));
  550. return
  551. $i."if(defined('$uid')) {\n".
  552. $i." die(\"\\n<p>Recursive inclusion of $page->uri</p>\\n\");\n".
  553. $i."} else\n".
  554. $i." define('$uid', 1);\n";
  555. }
  556. function &_getFavicons() {
  557. static $icons;
  558. if(!is_array($icons)) {
  559. $icons = array();
  560. if(@file_exists($_SERVER['DOCUMENT_ROOT'].'/favicon.ico'))
  561. $icons[] = array('/favicon.ico', 'shortcut icon', 'image/x-icon');
  562. if(@file_exists($_SERVER['DOCUMENT_ROOT'].'/favicon.gif'))
  563. $icons[] = array('/favicon.gif', 'icon', 'image/gif');
  564. if(@file_exists($_SERVER['DOCUMENT_ROOT'].'/favicon.png'))
  565. $icons[] = array('/favicon.png', 'icon', 'image/png');
  566. }
  567. return $icons;
  568. }
  569. function _getClassFile($class) {
  570. static $classes;
  571. if(!is_array($classes)) {
  572. $classes = array(
  573. 'htmltextbody'=>'/htmltext/htmltextbody._wc',
  574. 'htmltexthl'=>'/htmltext/htmltexthl._wc',
  575. 'htmltexthlintro'=>'/htmltext/htmltexthlintro._wc',
  576. 'publicationhl'=>'/publication/publicationhl._wc',
  577. 'publicationhlintro'=>'/publication/publicationhlintro._wc',
  578. 'publicationmainintro'=>'/publication/publicationmainintro._wc',
  579. 'publicationbody'=>'/publication/publicationbody._wc',
  580. 'publicationpager'=>'/publication/publicationpager._wc',
  581. 'publicationlist'=>'/publication/publicationlist._wc',
  582. 'publicationlistintro'=>'/publication/publicationlistintro._wc',
  583. 'eventcalendar'=>'/event/eventcalendar._wc',
  584. 'eventhl'=>'/event/eventhl._wc',
  585. 'eventhlintro'=>'/event/eventhlintro._wc',
  586. 'eventpager'=>'/event/eventpager._wc',
  587. 'eventbody'=>'/event/eventbody._wc',
  588. 'galleryhl'=>'/gallery/galleryhl._wc',
  589. 'galleryimage'=>'/gallery/galleryimage._wc',
  590. 'gallerypager'=>'/gallery/gallerypager._wc',
  591. 'galleryimgpager'=>'/gallery/galleryimgpager._wc',
  592. 'textrandom'=>'/textpool/textrandom._wc',
  593. 'phpcallback'=>'/phpsource/phpcallback._wc',
  594. 'phpinclude'=>'/phpsource/phpinclude._wc',
  595. 'gballmessages'=>'/guestbook/gballmessages._wc',
  596. 'gbaddmsglogic'=>'/guestbook/gbaddmsglogic._wc',
  597. 'gblist'=>'/guestbook/gblist._wc',
  598. 'gbmsgbody'=>'/guestbook/gbmsgbody._wc',
  599. 'gbmsghl'=>'/guestbook/gbmsghl._wc',
  600. 'gbpager'=>'/guestbook/gbpager._wc',
  601. 'miscfetchtpl'=>'/miscellany/miscfetchtpl._wc',
  602. 'misccrumbs'=>'/miscellany/misccrumbs._wc',
  603. 'miscinjector'=>'/miscellany/miscinjector._wc',
  604. 'miscsendmail'=>'/miscellany/miscsendmail._wc',
  605. 'filehl'=>'/file/filehl._wc',
  606. 'filepager'=>'/file/filepager._wc',
  607. 'usersauthorize'=>'/users/usersauthorize._wc',
  608. 'userslogout'=>'/users/userslogout._wc',
  609. 'articlebody'=>'/article/articlebody._wc',
  610. 'articlebodypager'=>'/article/articlebodypager._wc',
  611. 'articlehl'=>'/article/articlehl._wc',
  612. 'articlehlintro'=>'/article/articlehlintro._wc',
  613. 'articlepager'=>'/article/articlepager._wc',
  614. 'articlerelated'=>'/article/articlerelated._wc',
  615. 'hybridtree'=>'/hybrid/view/hybridtree._wc',
  616. 'hybridhl'=>'/hybrid/view/hybridhl._wc',
  617. 'hybridbar'=>'/hybrid/view/hybridbar._wc',
  618. 'hybridpager'=>'/hybrid/view/hybridpager._wc',
  619. 'hybridbody'=>'/hybrid/view/hybridbody._wc',
  620. 'hybridbodyedit'=>'/hybrid/view/hybridbodyedit._wc',
  621. 'ratingrate'=>'/rating/ratingrate._wc',
  622. 'ratingratelogic'=>'/rating/ratingratelogic._wc',
  623. 'searchdss'=>'/search/searchdss._wc',
  624. 'searchdsspager'=>'/search/searchdsspager._wc'
  625. );
  626. }
  627. return isset($classes[$class]) ? $classes[$class] : null;
  628. }
  629. }
  630. ?>