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

/source/foresmo/Foresmo/App/Base.php

https://github.com/btweedy/foresmo
PHP | 384 lines | 255 code | 27 blank | 102 comment | 24 complexity | ed8cb72c1c040fea7a1a9d954f30fafa MD5 | raw file
  1. <?php
  2. /**
  3. * Foresmo_App_Base
  4. * Foresmo App Arch Class
  5. *
  6. * @category App
  7. * @package Foresmo
  8. * @author Anthony Gentile
  9. * @version 0.09
  10. * @since 0.05
  11. */
  12. class Foresmo_App_Base extends Solar_App_Base {
  13. protected $_layout_default = 'default';
  14. protected $_model;
  15. protected $_adapter;
  16. protected $_modules;
  17. protected $_cache = null;
  18. protected $_registered_hooks = array(
  19. '_preRender' => array(
  20. 'index' => array(
  21. 'main' => array(),
  22. 'tag' => array(),
  23. 'page' => array(),
  24. 'sort' => array(),
  25. ),
  26. ),
  27. '_postRender' => array(
  28. 'index' => array(
  29. 'main' => array(),
  30. 'tag' => array(),
  31. 'page' => array(),
  32. 'sort' => array(),
  33. ),
  34. ),
  35. '_preRun' => array(
  36. 'index' => array(
  37. 'main' => array(),
  38. 'tag' => array(),
  39. 'page' => array(),
  40. 'sort' => array(),
  41. ),
  42. ),
  43. '_postRun' => array(
  44. 'index' => array(
  45. 'main' => array(),
  46. 'tag' => array(),
  47. 'page' => array(),
  48. 'sort' => array(),
  49. ),
  50. ),
  51. '_postAction' => array(
  52. 'index' => array(
  53. 'main' => array(),
  54. 'tag' => array(),
  55. 'page' => array(),
  56. 'sort' => array(),
  57. ),
  58. ),
  59. );
  60. /**
  61. * _restricted_names
  62. *
  63. * Disallowed slug values for post/pages
  64. *
  65. * @var array
  66. * @access protected
  67. */
  68. protected $_restricted_names = array(
  69. 'admin',
  70. 'base',
  71. 'index',
  72. 'install',
  73. 'login',
  74. 'logout',
  75. 'module',
  76. 'page',
  77. 'tag',
  78. 'search',
  79. 'ajax',
  80. 'sort',
  81. );
  82. public $session;
  83. public $connect = true;
  84. public $installed = false;
  85. public $blog_theme = 'default';
  86. public $blog_admin_theme = 'default';
  87. public $blog_theme_options = array();
  88. public $blog_admin_theme_options = array();
  89. public $blog_title = 'Foresmo Blog';
  90. public $page_title;
  91. public $pages_count;
  92. public $web_root;
  93. public $enabled_modules_data = array();
  94. /**
  95. * _setup
  96. *
  97. * Set variables used throughout the app here.
  98. */
  99. protected function _setup()
  100. {
  101. if (Solar_Config::get('Foresmo', 'dev')) {
  102. xdebug_start_trace('/var/www/foresmo/tmp/trace');
  103. }
  104. if (!isset($this->session)) {
  105. $this->session = Solar::factory('Solar_Session', array('class' => 'Foresmo_App'));
  106. }
  107. $adapter = Solar_Config::get('Solar_Sql', 'adapter');
  108. $adapter = Solar::factory($adapter);
  109. try {
  110. $adapter->connect();
  111. } catch (Exception $e) {
  112. $this->connect = false;
  113. // should display an error page and die.
  114. }
  115. if ($this->connect) {
  116. $this->_adapter = $adapter;
  117. $this->installed = (bool) Solar_Config::get('Foresmo', 'installed');
  118. if (!$this->installed && $this->_controller != 'install') {
  119. $this->_redirect('/install');
  120. }
  121. $this->web_root = Solar::$system . '/content/';
  122. $this->_model = Solar_Registry::get('model_catalog');
  123. $cache_settings = Solar_Config::get('Foresmo', 'cache');
  124. if (isset($cache_settings['adapter'])) {
  125. $this->_model->_config['cache'] = $cache_settings;
  126. $this->_cache = Solar::factory('Solar_Cache', $cache_settings);
  127. }
  128. $results = $this->_model->options->fetchBlogOptions();
  129. foreach ($results as $result) {
  130. switch ($result['name']) {
  131. case 'blog_theme':
  132. $this->blog_theme = $result['value'];
  133. break;
  134. case 'blog_admin_theme':
  135. $this->blog_admin_theme = $result['value'];
  136. break;
  137. case 'blog_theme_options':
  138. $this->blog_theme_options = unserialize($result['value']);
  139. break;
  140. case 'blog_admin_theme_options':
  141. $this->blog_admin_theme_options = unserialize($result['value']);
  142. break;
  143. case 'blog_title':
  144. $this->blog_title = $result['value'];
  145. break;
  146. case 'blog_posts_per_page':
  147. $this->_model->posts->posts_per_page = (int) $result['value'];
  148. break;
  149. case 'blog_comment_link_limit':
  150. $this->_model->comments->link_count_limit = (int) $result['value'];
  151. break;
  152. }
  153. }
  154. $this->page_title = $this->blog_title;
  155. $time_info = Foresmo::getTimeInfo();
  156. Foresmo::$date_format = $time_info['blog_date_format'];
  157. Foresmo::$timezone = $time_info['blog_timezone'];
  158. $this->_model->posts->published_posts_count = $this->_model->posts->fetchPublishedPostsCount();
  159. $this->_setPagesCount();
  160. $this->_layout_default = $this->blog_theme;
  161. $this->_setToken();
  162. $this->_modules = Solar::factory('Foresmo_Modules', array('model' => $this->_model));
  163. $this->enabled_modules_data = $this->_modules->getEnabledModulesData();
  164. $this->_registerModuleHooks();
  165. }
  166. }
  167. /**
  168. * _setPagesCount
  169. * This function sets how many pages will be available for
  170. * the blog's posts given the amount of posts and what the
  171. * posts_per_page is set to.
  172. *
  173. * @return void
  174. */
  175. private function _setPagesCount()
  176. {
  177. $posts_per_page = $this->_model->posts->posts_per_page;
  178. $posts_count = $this->_model->posts->published_posts_count;
  179. if ($posts_count <= $posts_per_page) {
  180. $this->pages_count = 1;
  181. return;
  182. }
  183. $this->pages_count = ceil(($posts_count / $posts_per_page));
  184. }
  185. /**
  186. * allowAjaxAction
  187. * Check user permissions for an ajax action to be performed
  188. *
  189. * @param $action
  190. * @return bool
  191. */
  192. public function allowAjaxAction($action)
  193. {
  194. switch ($action) {
  195. case 'admin_post_new':
  196. return $this->isValidPermission('create_post');
  197. break;
  198. case 'admin_pages_new':
  199. return $this->isValidPermission('create_page');
  200. break;
  201. }
  202. return false;
  203. }
  204. /**
  205. * isValidPermission
  206. * Check permission against logged in user's session permission data
  207. *
  208. * @param string $permission e.g. 'create_post'
  209. * @return bool
  210. */
  211. public function isValidPermission($permission)
  212. {
  213. $user_permissions = $this->session->get('Foresmo_permissions');
  214. if (!is_array($user_permissions)) {
  215. return false;
  216. }
  217. foreach ($user_permissions as $user_permission) {
  218. if ($user_permission['name'] == $permission) {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. /**
  225. * validate
  226. * This maps to Solar validation functions, without having to use a
  227. * form
  228. *
  229. * @param $validator validate function to use e.g. validateEmail
  230. * @param $str string to validate
  231. *
  232. * @return bool
  233. */
  234. public function validate($validator, $str)
  235. {
  236. $obj = Solar::factory('Solar_Filter_' . ucfirst($validator));
  237. if (is_object($obj)) {
  238. return $obj->$validator($str);
  239. }
  240. }
  241. /**
  242. * _setToken
  243. * This will set a session token that will be used to match against
  244. * forms posted.
  245. *
  246. * @return void
  247. */
  248. protected function _setToken()
  249. {
  250. if ($this->session->get('Foresmo_token', false) === false
  251. || !$this->session->get('Foresmo_token')) {
  252. $token = md5(uniqid(mt_rand(), TRUE));
  253. $this->session->set('Foresmo_token', $token);
  254. }
  255. // else already set.
  256. }
  257. /**
  258. * _checkToken
  259. * Check a token against the one stored in the session
  260. *
  261. * @param $token
  262. * @return bool
  263. */
  264. protected function _checkToken($token)
  265. {
  266. if ($this->session->get('Foresmo_token', false) !== false
  267. && $this->session->get('Foresmo_token') === $token) {
  268. return true;
  269. }
  270. return false;
  271. }
  272. /**
  273. * _getModuleHooks
  274. * Get registered hooks from enabled modules
  275. *
  276. * @return void
  277. */
  278. protected function _registerModuleHooks()
  279. {
  280. $allowed_processes = array('_preRun', '_postRun', '_postAction', '_preRender', '_postRender');
  281. $allowed_actions = array(
  282. 'index' => array('main', 'tag', 'page', 'sort'),
  283. );
  284. $hooks = $this->_modules->getRegisteredHooks();
  285. foreach ($hooks as $module => $rh) {
  286. foreach ($rh as $process => $cont) {
  287. // allowable hook process?
  288. if (!in_array($process, $allowed_processes)) {
  289. continue;
  290. }
  291. foreach ($cont as $controller => $act) {
  292. // allowable controller?
  293. if (!in_array($controller, array_keys($allowed_actions))) {
  294. continue;
  295. }
  296. foreach ($act as $action => $module_call) {
  297. // allowable action?
  298. if (!isset($allowed_actions[$controller])
  299. || !in_array($action, array_values($allowed_actions[$controller]))) {
  300. continue;
  301. }
  302. $this->_registered_hooks[$process][$controller][$action][$module] = $module_call;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * _addViewTemplates
  310. * Override Solar_Controller_Page _addViewTemplates
  311. * to add theme view path to view stack
  312. *
  313. */
  314. protected function _addViewTemplates()
  315. {
  316. // get the parents of the current class, including self
  317. $stack = array_reverse(Solar_Class::parents($this, true));
  318. // remove Solar_Base
  319. array_pop($stack);
  320. // convert underscores to slashes, and add /View
  321. foreach ($stack as $key => $val) {
  322. $stack[$key] = str_replace('_', '/', $val) . '/View';
  323. }
  324. // add theme view path
  325. $theme_name = ($this->_controller == 'admin') ? $this->blog_admin_theme : $this->blog_theme;
  326. $theme_view_path = Solar::$system . '/themes/' . $theme_name;
  327. $theme_view_path = str_replace('Foresmo', $theme_view_path, $stack[0]);
  328. array_unshift($stack, $theme_view_path);
  329. // done, add the stack
  330. $this->_view_object->addTemplatePath($stack);
  331. }
  332. /**
  333. * _setLayoutTemplates
  334. * Override Solar_Controller_Page _setLayoutTemplates
  335. * to add theme layout path to layout stack
  336. *
  337. */
  338. protected function _setLayoutTemplates()
  339. {
  340. // get the parents of the current class, including self
  341. $stack = array_reverse(Solar_Class::parents($this, true));
  342. // remove Solar_Base
  343. array_pop($stack);
  344. // convert underscores to slashes, and add /Layout
  345. foreach ($stack as $key => $val) {
  346. $stack[$key] = str_replace('_', '/', $val) . '/Layout';
  347. }
  348. // add theme layout path
  349. $theme_name = ($this->_controller == 'admin') ? $this->blog_admin_theme : $this->blog_theme;
  350. $theme_layout_path = Solar::$system . '/themes/' . $theme_name;
  351. $theme_layout_path_base = str_replace('Foresmo', $theme_layout_path, $stack[1]);
  352. $theme_layout_path = str_replace('Foresmo', $theme_layout_path, $stack[0]);
  353. array_unshift($stack, $theme_layout_path, $theme_layout_path_base);
  354. // done, add the stack
  355. $this->_view_object->setTemplatePath($stack);
  356. }
  357. }