/app/controllers/initialize.php

https://github.com/dmagick/search · PHP · 212 lines · 103 code · 31 blank · 78 comment · 15 complexity · e5bb806856977fdb53c211ca18928710 MD5 · raw file

  1. <?php
  2. /**
  3. * This is an abstract class so the other controllers can extend it and use
  4. * the initialize method, but also do their own thing.
  5. */
  6. abstract class initialize
  7. {
  8. /**
  9. * Keep a reference to the base directory so we know where to start
  10. * looking for things.
  11. */
  12. protected static $_basedir = NULL;
  13. /**
  14. * Keep a reference to the config so we only load it up once.
  15. * This is passed to all models - they can deal with it as they wish.
  16. */
  17. protected static $_config = array();
  18. /**
  19. * This is a static class. Calling it with 'new initisalize' should fail.
  20. */
  21. public function __construct()
  22. {
  23. return FALSE;
  24. }
  25. /**
  26. * This sets up everything for us.
  27. * Loads other systems, includes the config, gets everything ready to go.
  28. *
  29. *
  30. * @param string $basedir The base directory of the app.
  31. *
  32. * @return boolean
  33. */
  34. public static function initialize($basedir=NULL)
  35. {
  36. if ($basedir === NULL) {
  37. return FALSE;
  38. }
  39. /**
  40. * This is a bit of a silly check, but lets make sure it's a valid base.
  41. * We know where *we* should be - let's check.
  42. */
  43. if (is_file($basedir.'/app/controllers/initialize.php') === FALSE) {
  44. return FALSE;
  45. }
  46. /**
  47. * Passed that check - phew! We're good to go.
  48. */
  49. self::$_basedir = $basedir;
  50. return TRUE;
  51. }
  52. /**
  53. * All the "magic" works here.
  54. * This checks the config is set up, logs folder exists and is writable
  55. * and if neither of those are true, then displays an appropriate message.
  56. *
  57. * If it is true, it works out the url you're trying to view and passes it to the
  58. * appropriate controller to deal with.
  59. *
  60. * @return void
  61. */
  62. public static function process()
  63. {
  64. /**
  65. * We're always going to need the template controller.
  66. * Let's include it now.
  67. */
  68. require self::$_basedir.'/app/controllers/template.php';
  69. template::initialize(self::$_basedir);
  70. $configFile = self::$_basedir.'/app/config.php';
  71. if (is_file($configFile) === FALSE) {
  72. template::printTemplate(NULL, 'configuration_required', 500);
  73. exit;
  74. }
  75. $errors = array();
  76. $config = require $configFile;
  77. if (isset($config['flickrApiKey']) === FALSE || empty($config['flickrApiKey']) === TRUE) {
  78. $errors[] = array('error' => 'Please set the flickr api key in app/config.php.');
  79. }
  80. $logDir = self::$_basedir.'/app/logs';
  81. if (is_dir($logDir) === FALSE) {
  82. $errors[] = array('error' => 'Please create an app/logs directory and make sure it\'s writable by the web server.');
  83. } else {
  84. if (is_writable($logDir) === FALSE) {
  85. $errors[] = array('error' => 'The app/logs directory exists but it\'s not writable by the web server.');
  86. }
  87. }
  88. if (empty($errors) === FALSE) {
  89. template::setKeyword('configuration_required:errors:errors', $errors);
  90. template::printTemplate(NULL, 'configuration_required', 500);
  91. exit;
  92. }
  93. self::$_config = $config;
  94. $controller = 'search';
  95. $otherInfo = '';
  96. $queryInfo = $_GET;
  97. /**
  98. * If we've got a url, lets see if it's valid.
  99. * /controller/stuff
  100. * stuff is passed to the controller::process() method.
  101. */
  102. if (isset($_SERVER['PATH_INFO']) === TRUE) {
  103. $info = trim($_SERVER['PATH_INFO'], '/');
  104. $bits = explode('/', $info);
  105. $controller = array_shift($bits);
  106. $otherInfo = implode('/', $bits);
  107. }
  108. /**
  109. * Allow access to only particular controllers:
  110. * - search
  111. * mainly so people can't guess this class name and cause errors
  112. * and same for templates.
  113. */
  114. $allowedControllers = array(
  115. 'search',
  116. );
  117. $controllerFile = self::$_basedir.'/app/controllers/'.$controller.'.php';
  118. if (file_exists($controllerFile) === FALSE || in_array($controller, $allowedControllers) === FALSE) {
  119. template::printTemplate(NULL, '404', 404);
  120. exit;
  121. }
  122. require $controllerFile;
  123. $controller::process($otherInfo, $queryInfo);
  124. }
  125. /**
  126. * Get a model for a controller to work with.
  127. * If it can't be found, returns false.
  128. * Otherwise returns the model ready for use.
  129. *
  130. * @param string $modelName The model to get.
  131. *
  132. * @return mixed
  133. */
  134. protected static function getModel($modelName=NULL)
  135. {
  136. if ($modelName === NULL) {
  137. return FALSE;
  138. }
  139. $modelFile = self::$_basedir.'/app/models/'.$modelName.'.php';
  140. if (file_exists($modelFile) === FALSE) {
  141. return FALSE;
  142. }
  143. require $modelFile;
  144. $modelName = $modelName.'Model';
  145. $model = $modelName::getInstance(self::$_config);
  146. return $model;
  147. }
  148. /**
  149. * Print the generic server error page ('uhoh'), with the appropriate header.
  150. *
  151. * @return void
  152. */
  153. protected static function printServerError()
  154. {
  155. header('HTTP/1.1 500 Internal Server Error');
  156. echo template::printTemplate(NULL, 'uhoh');
  157. }
  158. /**
  159. * Error handler.
  160. * This logs the error and where it comes from to the app/logs/errors.log file.
  161. * If it's a E_USER_ERROR, it also displays the server error page to let
  162. * the user know something went really wrong.
  163. */
  164. public static function error_handler($errno, $errstr, $errfile, $errline)
  165. {
  166. $message = date('r')."\tGot error ${errstr} (${errno}) from ";
  167. $message .= "${errfile} on line ${errline}\n";
  168. error_log($message, 3, self::$_basedir.'/app/logs/errors.log');
  169. // Leave this as a switch in case we need to extend it later.
  170. switch ($errno)
  171. {
  172. case E_USER_ERROR:
  173. self::printServerError();
  174. exit;
  175. break;
  176. }
  177. return TRUE;
  178. }
  179. }
  180. set_error_handler(array('initialize', 'error_handler'));
  181. /* vim: set expandtab ts=4 sw=4: */