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

/fpb-includes/load.php

https://github.com/JiffSoft/FacePress
PHP | 262 lines | 160 code | 14 blank | 88 comment | 25 complexity | 12286517dea8a29eb31cc4ffa7187e24 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * @package FacePress
  4. * @version $Id$
  5. * @copyright (c) 2010 JiffSoft
  6. * @license http://www.gnu.org/licenses/gpl.html GNU General Public License v3
  7. */
  8. // load up the required classes
  9. require(BASEDIR.'/fpb-includes/smarty/Smarty.class.php');
  10. require(BASEDIR.'/fpb-includes/auth.php');
  11. require(BASEDIR.'/fpb-includes/database.php');
  12. require(BASEDIR.'/fpb-includes/functions.php');
  13. require(BASEDIR.'/fpb-includes/fbhelper.php');
  14. require(BASEDIR.'/fpb-includes/spyc.php');
  15. require(BASEDIR.'/fpb-includes/plugins.php');
  16. Plugins::Instance()->Load();
  17. $config = FPBDatabase::Instance()->GetConfigArray();
  18. /**
  19. * The 'core_loaded' hook is executed after we load up the classes and templates,
  20. * prior to any other code being executed
  21. * @see Hooks
  22. */
  23. Plugins::RunHook('core_loaded');
  24. $fb_helper = new FacebookHelper();
  25. FPBAuth::GetInstance()->CheckFBStatus();
  26. /**
  27. * The 'auth_completed' hook is executed after the authentication module has run
  28. * @see Hooks
  29. */
  30. Plugins::RunHook('auth_completed');
  31. $site_theme = (strlen($config["Theme"]) > 0) ? $config["Theme"] : 'coolblue';
  32. $smarty = new Smarty();
  33. $smarty->template_dir = BASEDIR.'/themes/'.$site_theme;
  34. $smarty->compile_dir = BASEDIR.'/cache/';
  35. $smarty->assign('theme_path','/themes/'.$site_theme);
  36. if (FPBAuth::GetInstance()->IsLoggedIn())
  37. $smarty->assign_by_ref('user',FPBAuth::GetInstance()->GetUser());
  38. require(BASEDIR.'/fpb-includes/toolbar.php');
  39. ob_start();
  40. /**
  41. * The 'head_load' hook is executed when we build up the content in <head>
  42. * @see Hooks
  43. */
  44. Plugins::RunHook('head_load');
  45. fpb_toolbar_head();
  46. $head_contents = ob_get_contents();
  47. ob_clean();
  48. $smarty->assign('head',$head_contents);
  49. $smarty->assign('title',$page_title);
  50. $smarty->assign('site_slogan',$config["GlobalSlogan"]);
  51. $smarty->assign('site_name',$config["GlobalName"]);
  52. $smarty->assign('fb_login_button',$fb_helper->FBLoginButton());
  53. $smarty->assign('fb_root',$fb_helper->FBInitDiv());
  54. // And now we can figure out what we're doing!
  55. /**
  56. * The 'pre_action' hook runs just before we figure out what action we will be taking
  57. * @see Hooks
  58. */
  59. Plugins::RunHook('pre_action');
  60. $full_action_requested = $_SERVER['REQUEST_URI'];
  61. ob_start();
  62. /**
  63. * The 'body_pre_action' hook runs in an output buffer before the body action is performed
  64. * @see Hooks
  65. */
  66. Plugins::RunHook('body_pre_action');
  67. if (preg_match("|/(?<year>[0-9]{4})/(?<month>[0-9]{2})/(?<day>[0-9]{2})/(?<title>[-A-Za-z0-9_]*)/?$|",$full_action_requested,$action_parts) != 0) {
  68. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  69. // post of a comment
  70. if ((!FPBAuth::GetInstance()->IsLoggedIn()) || (FPBAuth::GetInstance()->IsUserBanned())) {
  71. $action = '403';
  72. header('HTTP/1.0 403 Not authorized',true,403);
  73. $smarty->assign('page_title','Not authorized');
  74. } else {
  75. FPBDatabase::Instance()->CreateComment($_POST);
  76. echo 'make comment';
  77. }
  78. // and afterwards we go back to the post
  79. }
  80. // Find a post
  81. $action = 'post';
  82. $post = FPBDatabase::Instance()->GatherPostFromURIData($action_parts);
  83. $comments = FPBDatabase::Instance()->GetCommentsArray($post->ID);
  84. if ($post == null) {
  85. // 404!
  86. $action = '404';
  87. header('HTTP/1.0 404 Not found',true,404);
  88. $smarty->assign('page_title','Page not found');
  89. } else {
  90. /**
  91. * The 'pre_post_assign' hook runs just before assigning the post to the Smarty engine
  92. * @see Hooks
  93. */
  94. Plugins::RunHook('pre_post_assign');
  95. $smarty->assign('post',$post);
  96. $smarty->assign('comments',$comments);
  97. $smarty->assign('page_title',$post->post_title);
  98. }
  99. } elseif (preg_match("|/archives/(?<year>[0-9]{4})/(?<month>[0-9]{2})/?$|",$full_action_requested,$action_parts) != 0) {
  100. // Archives!
  101. $action = 'archives';
  102. $posts = FPBDatabase::Instance()->GatherPostsFromArchive($action_parts);
  103. /**
  104. * The 'pre_posts_archive_assign' hook runs just before assigning the posts list to the
  105. * Smarty engine when browsing archives
  106. * @see Hooks
  107. */
  108. Plugins::RunHook('pre_posts_archive_assign');
  109. $smarty->assign('posts',$posts);
  110. $smarty->assign('page_title','Archives for '.$action_parts['month'].'/'.$action_parts['year']);
  111. } elseif (preg_match("|/search|",$full_action_requested,$action_parts) != 0) {
  112. // Search!
  113. $action = 'search';
  114. $results = null;
  115. /**
  116. * The 'pre_search' hook runs before running a search, if a plugin is handling the search, populate
  117. * the $results array to override the built-in search
  118. * @see Hooks
  119. */
  120. Plugins::RunHook('pre_search');
  121. if ($results == null) {
  122. $results = FPBDatabase::Instance()->Search($_GET['q']);
  123. /**
  124. * The 'post_search' hook runs after a built-in search is completed
  125. * @see Hooks
  126. */
  127. Plugins::RunHook('post_search');
  128. }
  129. if ($results == null) {
  130. // 404!
  131. $action = '404';
  132. header('HTTP/1.0 404 Not found',true,404);
  133. $smarty->assign('page_title','Page not found');
  134. } else {
  135. $smarty->assign('results',$results);
  136. $smarty->assign('page_title','Search the site');
  137. }
  138. } elseif (preg_match("|/rss/?|",$full_action_requested,$action_parts) != 0) {
  139. // RSS FEED
  140. $posts = FPBDatabase::Instance()->GatherPosts(20);
  141. header("Content-Type: application/rss+xml; charset=ISO-8859-1");
  142. $rss_title = $config["GlobalName"];
  143. $rss_description = $config["GlobalSlogan"];
  144. $last_build = getdate();
  145. $last_pub = $posts[0]->post_date;
  146. $link = "http".($_SERVER['SERVER_PORT'] == 443 ? 's' : '').'://'.$_SERVER['SERVER_NAME']."/";
  147. echo '<?xml version="1.0" encoding="ISO-8859-1" ?>';
  148. echo <<<RSSXML
  149. <!-- generator="FacePress/0.0.1 -->
  150. <rss version="2.0">
  151. <channel>
  152. <title>$rss_title</title>
  153. <link>$link</link>
  154. <description>$rss_description</description>
  155. <language>en</language>
  156. <docs>http://www.rssboard.org/rss-specification</docs>
  157. <lastBuildDate>$last_build</lastBuildDate>
  158. <pubDate>$last_pub</pubDate>
  159. RSSXML;
  160. foreach ($posts as $post) {
  161. echo "\n";
  162. $fulllink = $link.$post->post_date['year'].'/'.$post->post_date['month'].'/'.$post->post_date['day'].'/'.$post->post_name;
  163. $excerpt = str_truncate(strip_tags($post->post_content),300);
  164. $date = date("D, d M Y H:i:s O", $post->post_date);
  165. echo <<<RSSXML
  166. <item>
  167. <title>$post->post_title</title>
  168. <link>$fulllink</link>
  169. <description><![CDATA[$excerpt]]></description>
  170. <pubDate>$date</pubDate>
  171. </item>
  172. RSSXML;
  173. }
  174. echo "\n";
  175. echo <<<RSSXML
  176. </channel>
  177. </rss>
  178. </xml>
  179. RSSXML;
  180. die;
  181. } elseif (preg_match("|/(?<page>(.+))/?$|",$full_action_requested,$action_parts) != 0) {
  182. // Display a page
  183. $action = 'page';
  184. $page = FPBDatabase::Instance()->GetPageBySlug($action_parts['page']);
  185. if ($page == null) {
  186. // 404!
  187. $action = '404';
  188. header('HTTP/1.0 404 Not found',true,404);
  189. $smarty->assign('page_title','Page not found');
  190. } else {
  191. /**
  192. * The 'pre_page_assign' hook runs just before assigning the page to the Smarty engine
  193. * @see Hooks
  194. */
  195. Plugins::RunHook('pre_page_assign');
  196. $smarty->assign('page',$page);
  197. $smarty->assign('page_title',$page->post_title);
  198. }
  199. } else {
  200. // the default action
  201. $action = 'home'; // default to home
  202. $posts = FPBDatabase::Instance()->GatherPosts(20);
  203. /**
  204. * The 'pre_posts_home_assign' hook runs just before assigning the posts for the homepage
  205. * to the Smarty engine
  206. * @see Hooks
  207. */
  208. Plugins::RunHook('pre_posts_home_assign');
  209. $smarty->assign('posts',$posts);
  210. $smarty->assign('page_title',$config["GlobalSlogan"]);
  211. }
  212. fpb_toolbar_body();
  213. /**
  214. * The 'body_post_action' hook runs in an output buffer just after the body action is performed
  215. * @see Hooks
  216. */
  217. Plugins::RunHook('body_post_action');
  218. $body_contents = ob_get_contents();
  219. ob_clean();
  220. $tpl_file = $action;
  221. /**
  222. * The 'post_action' hook runs just after we have run the requested action, just before
  223. * it is finished and then rendered
  224. * @see Hooks
  225. */
  226. Plugins::RunHook('post_action');
  227. $smarty->assign('body_contents',$body_contents);
  228. // Grab all archives for the $archives variable to be populated
  229. $archives = FPBDatabase::Instance()->GetArchiveList();
  230. $smarty->assign('archives',$archives);
  231. // Grab array of all pages
  232. $pages = FPBDatabase::Instance()->GetPageArray();
  233. $smarty->assign('pages',$pages);
  234. /**
  235. * The 'pre_render' hook runs just before we render the smarty template to the client
  236. * @see Hooks
  237. */
  238. Plugins::RunHook('pre_render');
  239. // make it happen!
  240. header('Content-type: text/html; charset=UTF-8;',true);
  241. $smarty->display($tpl_file.'.tpl');
  242. /**
  243. * The 'page_ended' hook is executed at the very end of execution, this is a good
  244. * place to put plugin cleanup
  245. * @see Hooks
  246. */
  247. Plugins::RunHook('page_ended');
  248. ?>