PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/common.php

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