PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

http://kv-webme.googlecode.com/
PHP | 429 lines | 346 code | 11 blank | 72 comment | 78 complexity | 612948c66490912ef823c52fa88ccbc0 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause, BSD-2-Clause, Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * front controller for WebME files
  4. *
  5. * PHP version 5.2
  6. *
  7. * @category None
  8. * @package None
  9. * @author Kae Verens <kae@kvsites.ie>
  10. * @license GPL 2.0
  11. * @link http://kvsites.ie/
  12. */
  13. // { common variables and functions
  14. // { WW_getCSS
  15. /**
  16. * retrieve a URL linking all added CSS sheets
  17. *
  18. * @return string HTML element with generated URL
  19. */
  20. function WW_getCSS() {
  21. return '<style>@import "/css/'.join('|', $GLOBALS['css_urls']).'";</style>';
  22. }
  23. // }
  24. // { WW_getInlineScripts
  25. /**
  26. * retrieve all inline JS scripts in a HTML element
  27. *
  28. * @return string HTML <script> element with inline JS scripts
  29. */
  30. function WW_getInlineScripts() {
  31. if (count($GLOBALS['scripts_inline'])) {
  32. return '<script>'.join('', $GLOBALS['scripts_inline']).'</script>';
  33. }
  34. }
  35. // }
  36. // { WW_getScripts
  37. /**
  38. * retrieve a URL linking all added external JS scripts
  39. *
  40. * @return string generated URL
  41. */
  42. function WW_getScripts() {
  43. global $scripts;
  44. $latest=filemtime(SCRIPTBASE.'j/js.js');
  45. foreach ($scripts as $script) {
  46. if (strpos($script, '/')===0) {
  47. $t=filemtime(SCRIPTBASE.$script);
  48. }
  49. else {
  50. $t=filemtime(SCRIPTBASE.'/ww.plugins/'.$script);
  51. }
  52. if ($t>$latest) {
  53. $latest=$t;
  54. }
  55. }
  56. return '/js/'.$latest.'*'
  57. .join('*', $scripts);
  58. }
  59. // }
  60. require_once 'ww.incs/common.php';
  61. if (isset($https_required) && $https_required && !$_SERVER['HTTPS']) {
  62. redirect('https://www.'.str_replace('www.', '', $_SERVER['HTTP_HOST']).'/');
  63. }
  64. if (isset($DBVARS['canonical_name'])
  65. && $_SERVER['HTTP_HOST']!=$DBVARS['canonical_name']
  66. ) {
  67. redirect(
  68. (@$_SERVER['HTTPS']=='on'?'https':'http')
  69. .'://'.$DBVARS['canonical_name'].$_SERVER['REQUEST_URI']
  70. );
  71. }
  72. if (!isset($DBVARS['version']) || $DBVARS['version']<56) {
  73. redirect('/ww.incs/upgrade.php');
  74. }
  75. $id=(int)@$_REQUEST['pageid'];
  76. $page=preg_replace('#&.*|/$#', '', @$_REQUEST['page']);
  77. // }
  78. // { is this a search?
  79. if ($page=='' && isset($_GET['search']) || isset($_GET['s'])) {
  80. require_once 'ww.incs/search.php';
  81. $id=Search_getPage();
  82. }
  83. // }
  84. // { check for Cron events
  85. if (!isset($DBVARS['cron-next']) || $DBVARS['cron-next']<date('Y-m-d H:i:s')) {
  86. require_once dirname(__FILE__).'/ww.incs/cron.php';
  87. }
  88. // }
  89. // { is maintenance mode enabled?
  90. if (isset($DBVARS['maintenance-mode']) && $DBVARS['maintenance-mode']=='yes') {
  91. if (!Core_isAdmin()) {
  92. die($DBVARS['maintenance-mode-message']);
  93. }
  94. }
  95. // }
  96. // { get current page id
  97. if (!$id) {
  98. if ($page) { // find using the page name
  99. $r=Page::getInstanceByName($page);
  100. if ($r && isset($r->id)) {
  101. $id=$r->id;
  102. $PAGEDATA=Page::getInstance($id)->initValues();
  103. if (isset($PAGEDATA->vars['_short_url'])
  104. && $PAGEDATA->vars['_short_url']
  105. ) {
  106. $s=dbOne(
  107. 'select short_url from short_urls where page_id='.$id, 'short_url'
  108. );
  109. if ($s!=$page) {
  110. redirect('/'.$s);
  111. }
  112. }
  113. }
  114. if (!$id) {
  115. $id=(int)dbOne(
  116. 'select page_id from short_urls where short_url="'
  117. .addslashes($page).'"',
  118. 'page_id'
  119. );
  120. }
  121. }
  122. if (!$id) { // or maybe it's a "special" or the home page
  123. $special=1;
  124. if (isset($_GET['special']) && $_GET['special']) {
  125. $special=$_GET['special'];
  126. }
  127. if (!$page) {
  128. $r=Page::getInstanceBySpecial($special);
  129. if ($r && isset($r->id)) {
  130. if ($special==1) {
  131. redirect($r->getRelativeUrl());
  132. }
  133. $id=$r->id;
  134. }
  135. }
  136. }
  137. if (!$id && $page) { // ok - find the nearest existing page then
  138. $unused_uri='';
  139. while (!$id && strpos($page, '/')!==false) {
  140. $l=strrpos($page, '/');
  141. $unused_uri=substr($page, $l+1).'/'.$unused_uri;
  142. $page=substr($page, 0, $l);
  143. $r=Page::getInstanceByName($page);
  144. if ($r && isset($r->id)) {
  145. $id=$r->id;
  146. $PAGE_UNUSED_URI=substr($unused_uri, 0, strlen($unused_uri)-1);
  147. }
  148. }
  149. }
  150. }
  151. // }
  152. // { load page data
  153. if ($id) {
  154. $PAGEDATA=Page::getInstance($id)->initValues();
  155. }
  156. else {
  157. if ($page!='') {
  158. redirect('/');
  159. }
  160. Core_quit(
  161. __(
  162. 'no page loaded. If this is a new site, then please'
  163. .' <a href="/ww.admin/">log into the admin area</a> and create your'
  164. .' first page.',
  165. 'core'
  166. )
  167. );
  168. }
  169. $c=Core_trigger('page-object-loaded');
  170. // }
  171. // { if URL includes a plugin override, run that instead of displaying the page
  172. if (isset($_REQUEST['_p'])
  173. && isset($PLUGINS[$_REQUEST['_p']]['page-override'])
  174. ) {
  175. Core_quit($PLUGINS[$_REQUEST['_p']]['page-override']($PAGEDATA));
  176. }
  177. // }
  178. // { main content
  179. // { check if page is protected
  180. $access_allowed=1;
  181. foreach ($PLUGINS as $p) {
  182. if ($access_allowed && isset($p['frontend']['page_display_test'])) {
  183. $access_allowed=$p['frontend']['page_display_test']($PAGEDATA);
  184. }
  185. }
  186. // }
  187. if (!$access_allowed) {
  188. $c.='<h2>'.__('Permission Denied', 'core').'</h2>'
  189. .'<p>'.__('This is a protected document.', 'core').'</p><p>'
  190. .isset($_SESSION['userdata'])
  191. ?__(
  192. 'You are not in a user-group which has access to this page. If you'
  193. .' think you should be, please contact the site administrator.',
  194. 'core'
  195. )
  196. :'<p><strong>'.__(
  197. 'If you have a user account, please <a href="/_r?type=loginpage">'
  198. .'click here</a> to log in.',
  199. 'core'
  200. );
  201. $c.='</p><p>'
  202. .__(
  203. 'If you do not have a user account, but have been supplied with a'
  204. .' password for the page, please enter it here and submit the form:',
  205. 'core'
  206. )
  207. .'</p>'
  208. .'<form method="post"><input type="password" name="privacy_password" />'
  209. .'<input type="submit" /></form>';
  210. }
  211. elseif (@$_REQUEST['cmsspecial']=='sitemap') {
  212. require_once 'ww.incs/sitemap-funcs.php';
  213. $c.=Sitemap_get();
  214. }
  215. else {
  216. switch($PAGEDATA->type) {
  217. case '0': // { normal page
  218. $c.=$PAGEDATA->render();
  219. break;
  220. // }
  221. case '1': // { redirect
  222. if (isset($PAGEDATA->vars['redirect_to'])
  223. && $PAGEDATA->vars['redirect_to']
  224. ) {
  225. redirect($PAGEDATA->vars['redirect_to']);
  226. }
  227. break; // }
  228. case '4': // { sub-page summaries
  229. require_once 'ww.incs/page.summaries.php';
  230. $c.=PageSummaries_getHtml($PAGEDATA->id);
  231. break; // }
  232. case '5': // { search results
  233. require_once 'ww.incs/search.php';
  234. $c.=$PAGEDATA->render().Search_showResults();
  235. break; // }
  236. case '9': // { table of contents
  237. require 'ww.incs/tableofcontents.php';
  238. $c.=TableOfContents_getContent($PAGEDATA);
  239. break; // }
  240. default: // { plugins, and unknown
  241. $not_found=true;
  242. if (isset($PLUGINS[$PAGEDATA->type])) {
  243. $p=$PLUGINS[$PAGEDATA->type];
  244. if (isset($p['frontend']['page_type'])
  245. && function_exists($p['frontend']['page_type'])
  246. ) {
  247. $c.=$p['frontend']['page_type']($PAGEDATA);
  248. $not_found=false;
  249. }
  250. }
  251. else {
  252. foreach ( $PLUGINS as $p ) {
  253. if (is_array(@$p[ 'frontend' ][ 'page_type' ])) {
  254. foreach ($p[ 'frontend' ][ 'page_type' ] as $name => $function) {
  255. if ($name == $PAGEDATA->type && function_exists($function)) {
  256. $c .= $function($PAGEDATA);
  257. $not_found = false;
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. if ($not_found) {
  265. $c.='<em><span>'
  266. .__('No plugin found to handle page type:', 'core')
  267. .'</span> <strong>'.htmlspecialchars($PAGEDATA->type)
  268. .'</strong>. '
  269. .__('Is the plugin installed and enabled?', 'core')
  270. .'</em>';
  271. }
  272. // }
  273. }
  274. }
  275. $pagecontent=$c
  276. .Core_trigger('page-content-created')
  277. .'<span class="end-of-page-content"></span>';
  278. // }
  279. // { load page template
  280. if (isset($_REQUEST['__t']) && !preg_match('/[\.\/]/', $_REQUEST['__t'])) {
  281. $PAGEDATA->template=$_REQUEST['__t'];
  282. }
  283. if (file_exists(THEME_DIR.'/'.THEME.'/h/'.$PAGEDATA->template.'.html')) {
  284. $template=THEME_DIR.'/'.THEME.'/h/'.$PAGEDATA->template.'.html';
  285. }
  286. elseif (file_exists(THEME_DIR.'/'.THEME.'/h/_default.html')) {
  287. $template=THEME_DIR.'/'.THEME.'/h/_default.html';
  288. }
  289. else {
  290. require_once dirname(__FILE__).'/ww.incs/template-find.php';
  291. }
  292. // }
  293. // { set up smarty
  294. $smarty=Core_smartySetup(USERBASE.'/ww.cache/pages');
  295. $smarty->template_dir=THEME_DIR.'/'.THEME.'/h/';
  296. $smarty->assign(
  297. 'PAGECONTENT', '<div id="ww-pagecontent">'.$pagecontent.'</div>'
  298. );
  299. $smarty->assign('PAGEDATA', $PAGEDATA);
  300. $smarty->assign('THEMEDIR', '/ww.skins/'.THEME);
  301. // }
  302. // { build metadata
  303. // { page title
  304. $c='<title>'
  305. .htmlspecialchars(
  306. $PAGEDATA->title
  307. ?$PAGEDATA->title
  308. :str_replace(
  309. 'www.', '',
  310. $_SERVER['HTTP_HOST']
  311. )
  312. .' > '.__FromJson($PAGEDATA->name)
  313. )
  314. .'</title>';
  315. // }
  316. // { show stylesheet and javascript links
  317. $c.='WW_CSS_GOES_HERE'.Core_getJQueryScripts()
  318. .'<script src="WW_SCRIPTS_GO_HERE"></script>';
  319. // { generate inline javascript
  320. $tmp='var pagedata={id:'.$PAGEDATA->id
  321. .Core_trigger('displaying-pagedata')
  322. .',ptop:'.$PAGEDATA->getTopParentId()
  323. .(isset($DBVARS['cdn']) && $DBVARS['cdn']?', cdn:"'.$DBVARS['cdn'].'"':'')
  324. .',sessid:"'.session_id().'"'
  325. .',lang:"'.@$_SESSION['language'].'"'
  326. .'},'
  327. .(
  328. isset($_SESSION['userdata']['id'])
  329. ?User::getAsScript()
  330. :'userdata={isAdmin:0'.(isset($_SESSION['wasAdmin'])?',wasAdmin:1':'').'};'
  331. );
  332. array_unshift($scripts_inline, $tmp);
  333. // }
  334. if (Core_isAdmin()) {
  335. foreach ($GLOBALS['PLUGINS'] as $p) {
  336. if (isset($p['frontend']['admin-script'])) {
  337. WW_addScript($p['frontend']['admin-script']);
  338. }
  339. }
  340. }
  341. // }
  342. // { meta tags
  343. $c.='<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
  344. if ($PAGEDATA->keywords) {
  345. $c.='<meta http-equiv="keywords" content="'
  346. .htmlspecialchars($PAGEDATA->keywords).'" />';
  347. }
  348. if ($PAGEDATA->description) {
  349. $c.='<meta http-equiv="description" content="'
  350. .htmlspecialchars($PAGEDATA->description).'"/>';
  351. }
  352. if (isset($PAGEDATA->vars['google-site-verification'])) {
  353. $c.='<meta name="google-site-verification" content="'
  354. .htmlspecialchars($PAGEDATA->vars['google-site-verification']).'" />';
  355. }
  356. if (isset($PAGEDATA->vars['header_html'])) {
  357. $c.=$PAGEDATA->vars['header_html'];
  358. }
  359. $smarty->assign(
  360. 'pagename',
  361. @$PAGEDATA->alias?$PAGEDATA->alias:$PAGEDATA->name
  362. );
  363. if (isset($DBVARS['theme_variant']) && $DBVARS['theme_variant']) {
  364. if (!file_exists(THEME_DIR.'/'.THEME.'/cs/'.$DBVARS['theme_variant'].'.css')) {
  365. unset($DBVARS['theme_variant']);
  366. Core_configRewrite();
  367. }
  368. else {
  369. $c.='<link rel="stylesheet" href="/ww.skins/'.THEME.'/cs/'
  370. .$DBVARS['theme_variant'].'.css" />';
  371. }
  372. }
  373. // }
  374. // { favicon
  375. if (file_exists(USERBASE.'/f/skin_files/favicon.png')) {
  376. $c.='<link rel="shortcut icon" href="/f/skin_files/favicon.png" />';
  377. }
  378. // }
  379. $smarty->assign('METADATA', $c.Core_trigger('building-metadata'));
  380. // }
  381. // { send timing header
  382. global $starttimeCount, $starttime;
  383. header(
  384. 'X-RenderTime-'.($starttimeCount++).'-totalSetup: '.((microtime(true)-$starttime)*1000)
  385. );
  386. $starttime=microtime(true);
  387. // }
  388. // { display the document
  389. ob_start();
  390. if (strpos($template, '/')===false) {
  391. $template=THEME_DIR.'/'.THEME.'/h/'.$template.'.html';
  392. }
  393. $t=$smarty->fetch($template);
  394. $t=str_replace(
  395. array('WW_SCRIPTS_GO_HERE', 'WW_CSS_GOES_HERE', '</body>'),
  396. array(WW_getScripts(), WW_getCSS(), WW_getInlineScripts().'</body>'),
  397. $t
  398. );
  399. if (isset($DBVARS['cdn'])) {
  400. $t=str_replace(
  401. array(
  402. 'href="/f/', 'src="/f/', 'src="/js/', 'href="/ww.skins/',
  403. 'url(/f/', 'src="/a/f=getImg/'
  404. ),
  405. array(
  406. 'href="//'.$DBVARS['cdn'].'/f/',
  407. 'src="//'.$DBVARS['cdn'].'/f/',
  408. 'src="//'.$DBVARS['cdn'].'/js/',
  409. 'href="//'.$DBVARS['cdn'].'/ww.skins/',
  410. 'url(//'.$DBVARS['cdn'].'/f/',
  411. 'src="//'.$DBVARS['cdn'].'/a/f=getImg/'
  412. ),
  413. $t
  414. );
  415. }
  416. echo $t;
  417. Core_flushBuffer('page', 'Content-type: text/html; Charset=utf-8');
  418. // }