PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/common.php

https://github.com/chyrp/site
PHP | 311 lines | 90 code | 69 blank | 152 comment | 12 complexity | 42d9973b236612a373778de0a56ab510 MD5 | raw file
  1. <?php
  2. /**
  3. * File: Common
  4. *
  5. * Chyrp - A Lightweight Blogging Engine
  6. *
  7. * Version:
  8. * v2.1
  9. *
  10. * License:
  11. * Modified MIT (See COPYING)
  12. *
  13. * Chyrp Copyright:
  14. * Copyright (c) 2011 Chyrp Team (See AUTHORS)
  15. */
  16. # Constant: CHYRP_VERSION
  17. # Chyrp's version number.
  18. define('CHYRP_VERSION', "2.1");
  19. # Constant: DEBUG
  20. # Should Chyrp use debugging processes?
  21. define('DEBUG', true);
  22. # Constant: CHECK_UPDATES
  23. # Should Chyrp check for updates?
  24. define('CHECK_UPDATES', false);
  25. # Constant: CACHE_TWIG
  26. # If defined, this will take priority over DEBUG and toggle Twig template caching.
  27. # Do not enable this during theme development.
  28. define('CACHE_TWIG', true);
  29. # Constant: JAVASCRIPT
  30. # Is this the JavaScript file?
  31. if (!defined('JAVASCRIPT')) define('JAVASCRIPT', false);
  32. # Constant: ADMIN
  33. # Is the user in the admin area?
  34. if (!defined('ADMIN')) define('ADMIN', false);
  35. # Constant: AJAX
  36. # Is this being run from an AJAX request?
  37. if (!defined('AJAX')) define('AJAX', isset($_POST['ajax']) and $_POST['ajax'] == "true");
  38. # Constant: XML_RPC
  39. # Is this being run from XML-RPC?
  40. if (!defined('XML_RPC')) define('XML_RPC', false);
  41. # Constant: TRACKBACK
  42. # Is this being run from a trackback request?
  43. if (!defined('TRACKBACK')) define('TRACKBACK', false);
  44. # Constant: UPGRADING
  45. # Is the user running the upgrader? (false)
  46. define('UPGRADING', false);
  47. # Constant: INSTALLING
  48. # Is the user running the installer? (false)
  49. define('INSTALLING', false);
  50. # Constant: TESTER
  51. # Is the site being run by the automated tester?
  52. define('TESTER', isset($_SERVER['HTTP_USER_AGENT']) and $_SERVER['HTTP_USER_AGENT'] == "tester.rb");
  53. # Constant: INDEX
  54. # Is the requested file /index.php?
  55. define('INDEX', (pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_BASENAME) == "index.php") and !ADMIN);
  56. # Constant: MAIN_DIR
  57. # Absolute path to the Chyrp root
  58. define('MAIN_DIR', dirname(dirname(__FILE__)));
  59. # Constant: INCLUDES_DIR
  60. # Absolute path to /includes
  61. define('INCLUDES_DIR', MAIN_DIR."/includes");
  62. # Constant: MODULES_DIR
  63. # Absolute path to /modules
  64. define('MODULES_DIR', MAIN_DIR."/modules");
  65. # Constant: FEATHERS_DIR
  66. # Absolute path to /feathers
  67. define('FEATHERS_DIR', MAIN_DIR."/feathers");
  68. # Constant: THEMES_DIR
  69. # Absolute path to /themes
  70. define('THEMES_DIR', MAIN_DIR."/themes");
  71. # Constant: ADMIN_THEMES_DIR
  72. # Absolute path to /admin/themes
  73. define('ADMIN_THEMES_DIR', MAIN_DIR."/admin/themes");
  74. # Constant: USE_ZLIB
  75. # Use zlib to provide GZIP compression
  76. define('USE_ZLIB', true);
  77. # Set error reporting levels, and headers for Chyrp's JS files.
  78. if (JAVASCRIPT) {
  79. error_reporting(0);
  80. header("Content-Type: application/x-javascript");
  81. header("Cache-Control: no-cache, must-revalidate");
  82. header("Expires: Mon, 03 Jun 1991 05:30:00 GMT");
  83. } else
  84. error_reporting(E_ALL | E_STRICT); # Make sure E_STRICT is on so Chyrp remains errorless.
  85. # Use GZip compression if available.
  86. if (!AJAX and
  87. extension_loaded("zlib") and
  88. !ini_get("zlib.output_compression") and
  89. isset($_SERVER['HTTP_ACCEPT_ENCODING']) and
  90. substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], "gzip") and
  91. USE_ZLIB) {
  92. ob_start("ob_gzhandler");
  93. header("Content-Encoding: gzip");
  94. } else
  95. ob_start();
  96. # File: Helpers
  97. # Various functions used throughout Chyrp's code.
  98. require_once INCLUDES_DIR."/helpers.php";
  99. # File: Gettext
  100. # Gettext library.
  101. require_once INCLUDES_DIR."/lib/gettext/gettext.php";
  102. # File: Streams
  103. # Streams library.
  104. require_once INCLUDES_DIR."/lib/gettext/streams.php";
  105. # File: YAML
  106. # Horde YAML parsing library.
  107. require_once INCLUDES_DIR."/lib/YAML.php";
  108. # File: PasswordHash
  109. # Secure hashing of user passwords
  110. require_once INCLUDES_DIR."/lib/PasswordHash.php";
  111. # File: Config
  112. # See Also:
  113. # <Config>
  114. require_once INCLUDES_DIR."/class/Config.php";
  115. # File: SQL
  116. # See Also:
  117. # <SQL>
  118. require_once INCLUDES_DIR."/class/SQL.php";
  119. # File: Model
  120. # See Also:
  121. # <Model>
  122. require_once INCLUDES_DIR."/class/Model.php";
  123. # File: User
  124. # See Also:
  125. # <User>
  126. require_once INCLUDES_DIR."/model/User.php";
  127. # File: Visitor
  128. # See Also:
  129. # <Visitor>
  130. require_once INCLUDES_DIR."/model/Visitor.php";
  131. # File: Post
  132. # See Also:
  133. # <Post>
  134. require_once INCLUDES_DIR."/model/Post.php";
  135. # File: Page
  136. # See Also:
  137. # <Page>
  138. require_once INCLUDES_DIR."/model/Page.php";
  139. # File: Group
  140. # See Also:
  141. # <Group>
  142. require_once INCLUDES_DIR."/model/Group.php";
  143. # File: Session
  144. # See Also:
  145. # <Session>
  146. require_once INCLUDES_DIR."/class/Session.php";
  147. # File: Flash
  148. # See Also:
  149. # <Flash>
  150. require_once INCLUDES_DIR."/class/Flash.php";
  151. # File: Theme
  152. # See Also:
  153. # <Theme>
  154. require_once INCLUDES_DIR."/class/Theme.php";
  155. # File: Trigger
  156. # See Also:
  157. # <Trigger>
  158. require_once INCLUDES_DIR."/class/Trigger.php";
  159. # File: Module
  160. # See Also:
  161. # <Module>
  162. require_once INCLUDES_DIR."/class/Modules.php";
  163. # File: Feathers
  164. # See Also:
  165. # <Feathers>
  166. require_once INCLUDES_DIR."/class/Feathers.php";
  167. # File: Paginator
  168. # See Also:
  169. # <Paginator>
  170. require_once INCLUDES_DIR."/class/Paginator.php";
  171. # File: Twig
  172. # Chyrp's templating engine.
  173. require_once INCLUDES_DIR."/class/Twig.php";
  174. # File: Route
  175. # See Also:
  176. # <Route>
  177. require_once INCLUDES_DIR."/class/Route.php";
  178. # File: Main
  179. # See Also:
  180. # <Main Controller>
  181. require_once INCLUDES_DIR."/controller/Main.php";
  182. # File: Admin
  183. # See Also:
  184. # <Admin Controller>
  185. require_once INCLUDES_DIR."/controller/Admin.php";
  186. # File: Feather
  187. # See Also:
  188. # <Feather>
  189. require_once INCLUDES_DIR."/interface/Feather.php";
  190. # Set the error handler to exit on error if this is being run from the tester.
  191. if (TESTER)
  192. set_error_handler("error_panicker");
  193. # Redirect to the installer if there is no config.
  194. if (!file_exists(INCLUDES_DIR."/config.yaml.php"))
  195. redirect("install.php");
  196. # Start the timer that keeps track of Chyrp's load time.
  197. timer_start();
  198. # Load the config settings.
  199. $config = Config::current();
  200. # Prepare the SQL interface.
  201. $sql = SQL::current();
  202. # Set the timezone for date(), etc.
  203. set_timezone($config->timezone);
  204. # Initialize connection to SQL server.
  205. $sql->connect();
  206. # Sanitize all input depending on magic_quotes_gpc's enabled status.
  207. sanitize_input($_GET);
  208. sanitize_input($_POST);
  209. sanitize_input($_COOKIE);
  210. sanitize_input($_REQUEST);
  211. # Begin the session.
  212. session();
  213. # Set the locale for gettext.
  214. set_locale($config->locale);
  215. # Load the translation engine.
  216. load_translator("chyrp", INCLUDES_DIR."/locale/".$config->locale.".mo");
  217. # Constant: PREVIEWING
  218. # Is the user previewing a theme?
  219. define('PREVIEWING', !ADMIN and !empty($_SESSION['theme']));
  220. # Constant: THEME_DIR
  221. # Absolute path to /themes/(current/previewed theme)
  222. define('THEME_DIR', MAIN_DIR."/themes/".(PREVIEWING ? $_SESSION['theme'] : $config->theme));
  223. # Constant: THEME_URL
  224. # URL to /themes/(current/previewed theme)
  225. define('THEME_URL', $config->chyrp_url."/themes/".(PREVIEWING ? $_SESSION['theme'] : $config->theme));
  226. # Initialize the theme.
  227. $theme = Theme::current();
  228. # Load the Visitor.
  229. $visitor = Visitor::current();
  230. # Prepare the notifier.
  231. $flash = Flash::current();
  232. # Initiate the extensions.
  233. init_extensions();
  234. # Prepare the trigger class
  235. $trigger = Trigger::current();
  236. # Filter the visitor immediately after the Modules are initialized.
  237. # Example usage scenario: custom auth systems (e.g. OpenID)
  238. $trigger->filter($visitor, "visitor");
  239. # First general-purpose trigger. There are many cases you may want to use @route_init@ instead of this, however.
  240. $trigger->call("runtime");
  241. # Set the content-type to the theme's "type" setting, or "text/html".
  242. header("Content-type: ".(INDEX ? fallback($theme->type, "text/html") : "text/html")."; charset=UTF-8");