PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/frog/app/main.php

https://github.com/Doap/FrogCMS
PHP | 251 lines | 152 code | 54 blank | 45 comment | 34 complexity | add2583253d91ff1b14f07ed872e42a3 MD5 | raw file
  1. <?php
  2. /**
  3. * Frog CMS - Content Management Simplified. <http://www.madebyfrog.com>
  4. * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
  5. * Copyright (C) 2008 Martijn van der Kleijn <martijn.niji@gmail.com>
  6. *
  7. * This file is part of Frog CMS.
  8. *
  9. * Frog CMS is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Frog CMS is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with Frog CMS. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * Frog CMS has made an exception to the GNU General Public License for plugins.
  23. * See exception.txt for details and the full text.
  24. */
  25. require APP_PATH . '/models/Plugin.php';
  26. require APP_PATH . '/classes/Page.php';
  27. if ( ! defined('HELPER_PATH')) define('HELPER_PATH', CORE_ROOT.'/helpers');
  28. if ( ! defined('URL_SUFFIX')) define('URL_SUFFIX', '');
  29. ini_set('date.timezone', DEFAULT_TIMEZONE);
  30. if(function_exists('date_default_timezone_set'))
  31. date_default_timezone_set(DEFAULT_TIMEZONE);
  32. else
  33. putenv('TZ='.DEFAULT_TIMEZONE);
  34. // Intialize Setting and Plugin
  35. Setting::init();
  36. Plugin::init();
  37. /**
  38. * Explode an URI and make a array of params
  39. */
  40. function explode_uri($uri)
  41. {
  42. return preg_split('/\//', $uri, -1, PREG_SPLIT_NO_EMPTY);
  43. }
  44. function find_page_by_uri($uri)
  45. {
  46. global $__FROG_CONN__;
  47. $uri = trim($uri, '/');
  48. $has_behavior = false;
  49. // adding the home root
  50. $urls = array_merge(array(''), explode_uri($uri));
  51. $url = '';
  52. $page = new stdClass;
  53. $page->id = 0;
  54. $parent = false;
  55. foreach ($urls as $page_slug)
  56. {
  57. $url = ltrim($url . '/' . $page_slug, '/');
  58. if ($page = find_page_by_slug($page_slug, $parent))
  59. {
  60. // check for behavior
  61. if ($page->behavior_id != '')
  62. {
  63. // add a instance of the behavior with the name of the behavior
  64. $params = explode_uri(substr($uri, strlen($url)));
  65. $page->{$page->behavior_id} = Behavior::load($page->behavior_id, $page, $params);
  66. return $page;
  67. }
  68. }
  69. else
  70. {
  71. break;
  72. }
  73. $parent = $page;
  74. } // foreach
  75. return ( ! $page && $has_behavior) ? $parent: $page;
  76. } // find_page_by_slug
  77. function find_page_by_slug($slug, &$parent)
  78. {
  79. global $__FROG_CONN__;
  80. $page_class = 'Page';
  81. $parent_id = $parent ? $parent->id: 0;
  82. $sql = 'SELECT page.*, author.name AS author, updator.name AS updator '
  83. . 'FROM '.TABLE_PREFIX.'page AS page '
  84. . 'LEFT JOIN '.TABLE_PREFIX.'user AS author ON author.id = page.created_by_id '
  85. . 'LEFT JOIN '.TABLE_PREFIX.'user AS updator ON updator.id = page.updated_by_id '
  86. . 'WHERE slug = ? AND parent_id = ? AND (status_id='.Page::STATUS_REVIEWED.' OR status_id='.Page::STATUS_PUBLISHED.' OR status_id='.Page::STATUS_HIDDEN.')';
  87. $stmt = $__FROG_CONN__->prepare($sql);
  88. $stmt->execute(array($slug, $parent_id));
  89. if ($page = $stmt->fetchObject())
  90. {
  91. // hook to be able to redefine the page class with behavior
  92. if ( ! empty($parent->behavior_id))
  93. {
  94. // will return Page by default (if not found!)
  95. $page_class = Behavior::loadPageHack($parent->behavior_id);
  96. }
  97. // create the object page
  98. $page = new $page_class($page, $parent);
  99. // assign all is parts
  100. $page->part = get_parts($page->id);
  101. return $page;
  102. }
  103. else return false;
  104. }
  105. function get_parts($page_id)
  106. {
  107. global $__FROG_CONN__;
  108. $objPart = new stdClass;
  109. $sql = 'SELECT name, content_html FROM '.TABLE_PREFIX.'page_part WHERE page_id=?';
  110. if ($stmt = $__FROG_CONN__->prepare($sql))
  111. {
  112. $stmt->execute(array($page_id));
  113. while ($part = $stmt->fetchObject())
  114. $objPart->{$part->name} = $part;
  115. }
  116. return $objPart;
  117. }
  118. function url_match($url)
  119. {
  120. $url = trim($url, '/');
  121. if (CURRENT_URI == $url)
  122. return true;
  123. return false;
  124. }
  125. function url_start_with($url)
  126. {
  127. $url = trim($url, '/');
  128. if (CURRENT_URI == $url)
  129. return true;
  130. if (strpos(CURRENT_URI, $url) === 0)
  131. return true;
  132. return false;
  133. }
  134. function main()
  135. {
  136. // get the uri string from the query
  137. $uri = $_SERVER['QUERY_STRING'];
  138. // START processing $_GET variables
  139. // If we're NOT using mod_rewrite, we check for GET variables we need to integrate
  140. if (!USE_MOD_REWRITE && strpos($uri, '?') !== false)
  141. {
  142. $_GET = array(); // empty $_GET array since we're going to rebuild it
  143. list($uri, $get_var) = explode('?', $uri);
  144. $exploded_get = explode('&', $get_var);
  145. if (count($exploded_get))
  146. {
  147. foreach ($exploded_get as $get)
  148. {
  149. list($key, $value) = explode('=', $get);
  150. $_GET[$key] = $value;
  151. }
  152. }
  153. }
  154. // We're NOT using mod_rewrite, and there's no question mark wich points to GET variables in combination with site root.
  155. else if (!USE_MOD_REWRITE && (strpos($uri, '&') !== false || strpos($uri, '=') !== false))
  156. {
  157. $uri='';
  158. }
  159. // If we're using mod_rewrite, we should have a PAGE entry.
  160. if (USE_MOD_REWRITE && array_key_exists('PAGE', $_GET))
  161. {
  162. $uri = $_GET['PAGE'];
  163. unset($_GET['PAGE']);
  164. }
  165. else if (USE_MOD_REWRITE) // We're using mod_rewrite but don't have a PAGE entry, assume site root.
  166. $uri = '';
  167. // END processing $_GET variables
  168. // remove suffix page if founded
  169. if (URL_SUFFIX !== '' and URL_SUFFIX !== '/')
  170. $uri = preg_replace('#^(.*)('.URL_SUFFIX.')$#i', "$1", $uri);
  171. define('CURRENT_URI', trim($uri, '/'));
  172. Observer::notify('page_requested', $uri);
  173. // this is where 80% of the things is done
  174. $page = find_page_by_uri($uri);
  175. // if we fund it, display it!
  176. if (is_object($page))
  177. {
  178. // If page needs login, redirect to login
  179. if ($page->getLoginNeeded() == Page::LOGIN_REQUIRED)
  180. {
  181. AuthUser::load();
  182. if (!AuthUser::isLoggedIn()) {
  183. Flash::set('redirect', $page->url());
  184. redirect(URL_PUBLIC.ADMIN_DIR.(USE_MOD_REWRITE ? '/': '/?/').'login');
  185. }
  186. }
  187. Observer::notify('page_found', $page);
  188. $page->_executeLayout();
  189. }
  190. else {
  191. page_not_found();
  192. }
  193. } // main
  194. // ok come on! let's go! (movie: Hacker's)
  195. ob_start();
  196. main();
  197. ob_end_flush();