PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Weblynx/Controllers/Base.php

https://github.com/AntoDRFC/The-Art-House
PHP | 245 lines | 129 code | 44 blank | 72 comment | 28 complexity | 67ef728cb10700bac4256a4fba8eb3cf MD5 | raw file
  1. <?php
  2. class Weblynx_Controllers_Base extends Zend_Controller_Action
  3. {
  4. /**
  5. * DB connection
  6. */
  7. protected $db;
  8. /**
  9. * DataMappers
  10. */
  11. protected $dbMapper;
  12. /**
  13. * Config Object
  14. */
  15. protected $config;
  16. /**
  17. * User object
  18. */
  19. protected $user = null;
  20. /*
  21. * Shorthand of $this->getRequest();
  22. */
  23. protected $req;
  24. /**
  25. * SESSION object
  26. */
  27. protected $session;
  28. /**
  29. * Perform generic init for controllers
  30. *
  31. * - Sets up config+view objects
  32. * - creates navigation structure
  33. *
  34. */
  35. public function init()
  36. {
  37. // set the current path
  38. $currentPath = rtrim($this->getRequest()->getPathInfo(), '/');
  39. if(!$currentPath){
  40. $currentPath = '/';
  41. }
  42. if(str_replace('/', '', $currentPath) == 'admin') {
  43. $this->_redirect('/admin/login.php');
  44. }
  45. // setup config object
  46. $this->config = Zend_Registry::get('config');
  47. // setup db
  48. $this->db = Zend_Db::factory($this->config->database->weblynx);
  49. $this->dbMapper = new Weblynx_DataMappers_General($this->db);
  50. // if were in development mode, send unrouted requests to a holding page
  51. if($this->config->development->mode == 'development') {
  52. if($currentPath == '/') {
  53. $this->_redirect('/index.html');
  54. }
  55. }
  56. // setup the session
  57. Zend_Session::start();
  58. $this->session = new Zend_Session_Namespace();
  59. // setup the view
  60. $this->view = new Zend_View();
  61. $this->view->setScriptPath($this->config->paths->base . DIRECTORY_SEPARATOR . 'templates');
  62. $this->view->currentPath = $currentPath;
  63. $this->view->headJs = array();
  64. $this->view->js = array();
  65. $this->view->css = array();
  66. // set the current controller and action into to view
  67. $this->view->controller = $this->getRequest()->getControllerName();
  68. $this->view->currentaction = $this->getRequest()->getActionName();
  69. // create a shorthand verison of the $this->getRequest();
  70. $this->req = $this->getRequest();
  71. $navType = $this->config->settings->nav;
  72. // lets get the nav built up and sent to the view if were not in the admin panel
  73. if($this->view->controller != 'pagebuilder') {
  74. $currentpage = $this->req->getParam('currentpage', 'index');
  75. $useStrpos = false;
  76. // is this page a subpage?
  77. $pageinfo = $this->dbMapper->getPageByPermalink($currentpage);
  78. if($pageinfo['parent'] != 0) {
  79. $parentInfo = $this->dbMapper->getPage($pageinfo['parent']);
  80. $currentpage = $parentInfo['permalink'];
  81. }
  82. // is this page not a CMS page but a plugin?
  83. if($this->view->controller != 'index') {
  84. $currentpage = $_SERVER['REQUEST_URI'];
  85. $useStrpos = true;
  86. }
  87. if($navType == 'dynamic') {
  88. $nav = $this->buildNavigation(false, $currentpage, $useStrpos);
  89. $this->view->nav = $nav;
  90. } else {
  91. $this->view->currentPage = $currentpage;
  92. $this->view->nav = $this->view->render('nav.phtml');
  93. }
  94. }
  95. // was a form posted on the page thats errored?
  96. if(isset($this->session->formdata)) {
  97. $formdata = $this->session->formdata;
  98. unset($this->session->formdata);
  99. $this->view->formdata = $formdata;
  100. }
  101. }
  102. /**
  103. * Build the navigation structure
  104. */
  105. public function buildNavigation($parentonly = true, $currentpage, $strpos = false) {
  106. $parent = $parentonly ? 0 : false;
  107. $published = 1;
  108. $pages = $this->dbMapper->getPages($parent, $published);
  109. $nav = '';
  110. foreach($pages as $page) {
  111. if(!$strpos) {
  112. $class = ($page['permalink'] == $currentpage) ? ' class="current"' : '';
  113. } else {
  114. $class = (strpos($currentpage, $page['permalink']) !== false) ? ' class="current"' : '';
  115. }
  116. if($page['permalink'] == 'index') {
  117. $nav .= sprintf('<li%s><a href="/">%s</a></li>', $class, $page['menu_text']);
  118. } else {
  119. $prependView = $page['type'] == 'page' ? '/view/' : '';
  120. $nav .= sprintf('<li%s><a href="%s%s">%s</a></li>', $class, $prependView, $page['permalink'], $page['menu_text']);
  121. }
  122. }
  123. return $nav;
  124. }
  125. /**
  126. * Render the $template view
  127. *
  128. */
  129. public function renderView($template = "main.phtml")
  130. {
  131. // if there were errors, then these session variables have data (send it to the view)
  132. //$this->view->formErrors = Smart::ifsetor($_SESSION['formErrors'], null);
  133. //$this->view->prevPost = Smart::ifsetor($_SESSION['prevPost'], null);
  134. // Get all our stuff into the template
  135. $this->getResponse()->appendBody($this->view->render($template));
  136. // clear the session vars after rendering
  137. unset($_SESSION['prevPost']);
  138. unset($_SESSION['formErrors']);
  139. }
  140. /**
  141. * Generate and set a CSRF token (in the session data)
  142. *
  143. * @return string The token
  144. */
  145. public function createCsrfToken()
  146. {
  147. $token = md5(uniqid(rand(), TRUE));
  148. $time = time();
  149. $_SESSION['csrf_tokens'][$token] = $time;
  150. return $token;
  151. }
  152. /**
  153. * Check a $toCheck CSRF token against the value stored in the session
  154. *
  155. * @param $toCheck
  156. * @return boolean Whether the CSRF passed or not
  157. */
  158. public function checkCsrfToken($toCheck, $maxAge = 10800)
  159. {
  160. $tokens = isset($_SESSION['csrf_tokens']) ? $_SESSION['csrf_tokens'] : array();
  161. if(isset($tokens[$toCheck])){
  162. // one-time use
  163. unset($_SESSION['csrf_tokens'][$toCheck]);
  164. $time = $tokens[$toCheck];
  165. $age = time() - $time;
  166. if(($age < $maxAge)){
  167. return true;
  168. }else{
  169. return false;
  170. }
  171. }
  172. return false;
  173. }
  174. /**
  175. * Checks if the user is logged in or not
  176. *
  177. * @return boolean
  178. */
  179. public function isLoggedIn()
  180. {
  181. // if the session variable is set, assume that user
  182. if(isset($_SESSION) && $_SESSION['user_id']){
  183. $this->user = $_SESSION;
  184. return true;
  185. }else{
  186. return false;
  187. }
  188. }
  189. /**
  190. * turns a mysql result set into a valid key=>value array set ready for makeOptions
  191. *
  192. * @return array
  193. */
  194. public function mysqlValuesToOptions($data, $key, $value) {
  195. $return = array();
  196. foreach($data as $option) {
  197. $return[$option[$key]] = $option[$value];
  198. }
  199. return $return;
  200. }
  201. }