34 ini_set(
'session.hash_bits_per_character', 4);
35 ini_set(
'session.hash_function', 0);
40 if ( !defined(
'MCRYPT_RIJNDAEL_256') ) {
41 define(
'MCRYPT_RIJNDAEL_256', 0);
43 if ( !defined(
'MCRYPT_MODE_CBC') ) {
44 define(
'MCRYPT_MODE_CBC', 0);
56 spl_autoload_register(array(
'Slim',
'autoload'));
60 if ( @date_default_timezone_set(date_default_timezone_get()) ===
false ) {
61 date_default_timezone_set(
'UTC');
76 protected static $apps = array();
122 'slim.before' => array(array()),
123 'slim.before.router' => array(array()),
124 'slim.before.dispatch' => array(array()),
125 'slim.after.dispatch' => array(array()),
126 'slim.after.router' => array(array()),
127 'slim.after' => array(array())
140 if ( strpos($class,
'Slim') !== 0 ) {
143 $file = dirname(__FILE__) .
'/' . str_replace(
'_', DIRECTORY_SEPARATOR, substr($class,5)) .
'.php';
144 if ( file_exists(
$file) ) {
158 $this->settings = array_merge(array(
160 'mode' =>
'development',
162 'log.enable' =>
false,
163 'log.logger' => null,
164 'log.path' =>
'./logs',
169 'templates.path' =>
'./templates',
170 'view' =>
'Slim_View',
172 'cookies.lifetime' =>
'20 minutes',
173 'cookies.path' =>
'/',
174 'cookies.domain' =>
'',
175 'cookies.secure' =>
false,
176 'cookies.httponly' =>
false,
178 'cookies.secret_key' =>
'CHANGE_ME',
179 'cookies.cipher' => MCRYPT_RIJNDAEL_256,
180 'cookies.cipher_mode' => MCRYPT_MODE_CBC,
181 'cookies.encrypt' =>
true,
182 'cookies.user_id' =>
'DEFAULT',
185 'session.flash_key' =>
'flash',
187 'http.version' => null
197 'high_confidentiality' => $this->settings[
'cookies.encrypt'],
198 'mcrypt_algorithm' => $this->settings[
'cookies.cipher'],
199 'mcrypt_mode' => $this->settings[
'cookies.cipher_mode'],
200 'enable_ssl' => $this->settings[
'cookies.secure']
202 $this->
response->httpVersion($this->settings[
'http.version']);
206 if ( session_id() ===
'' ) {
207 $sessionHandler = $this->
config(
'session.handler');
209 $sessionHandler->register($this);
211 session_cache_limiter(
false);
219 if ( !isset(self::$apps[
'default']) ) {
224 set_error_handler(array(
'Slim',
'handleErrors'));
232 if ( !isset($this->mode) ) {
233 if ( isset($_ENV[
'SLIM_MODE']) ) {
234 $this->mode = (string)$_ENV[
'SLIM_MODE'];
236 $envMode = getenv(
'SLIM_MODE');
237 if ( $envMode !==
false ) {
238 $this->mode = $envMode;
240 $this->mode = (string)$this->
config(
'mode');
255 return isset(self::$apps[(
string)
$name]) ? self::$apps[(string)$name] : null;
265 self::$apps[
$name] = $this;
283 if ( !isset($this->log) ) {
285 $this->log->setEnabled($this->
config(
'log.enable'));
286 $logger = $this->
config(
'log.logger');
288 $this->log->setLogger($logger);
311 if (
$mode === $this->
getMode() && is_callable($callable) ) {
312 call_user_func($callable);
336 if ( func_num_args() === 1 ) {
337 if ( is_array(
$name) ) {
338 $this->settings = array_merge($this->settings,
$name);
340 return in_array(
$name, array_keys($this->settings)) ? $this->settings[
$name] : null;
343 $this->settings[
$name] = $value;
380 $pattern = array_shift($args);
381 $callable = array_pop($args);
382 $route = $this->
router->map($pattern, $callable);
383 if ( count($args) > 0 ) {
384 $route->setMiddleware($args);
395 $args = func_get_args();
404 public function get() {
405 $args = func_get_args();
415 $args = func_get_args();
425 $args = func_get_args();
434 public function delete() {
435 $args = func_get_args();
445 $args = func_get_args();
472 if ( !is_null($callable) ) {
473 $this->
router->notFound($callable);
476 $customNotFoundHandler = $this->
router->notFound();
477 if ( is_callable($customNotFoundHandler) ) {
478 call_user_func($customNotFoundHandler);
480 call_user_func(array($this,
'defaultNotFound'));
482 $this->
halt(404, ob_get_clean());
510 public function error( $argument = null ) {
511 if ( is_callable($argument) ) {
513 $this->
router->error($argument);
517 $customErrorHandler = $this->
router->error();
518 if ( is_callable($customErrorHandler) ) {
519 call_user_func_array($customErrorHandler, array($argument));
521 call_user_func_array(array($this,
'defaultError'), array($argument));
523 $this->
halt(500, ob_get_clean());
569 public function view( $viewClass = null ) {
570 if ( !is_null($viewClass) ) {
571 $existingData = is_null($this->
view) ? array() : $this->
view->getData();
573 $this->
view = $viewClass;
575 $this->
view =
new $viewClass();
577 $this->
view->appendData($existingData);
578 $this->
view->setTemplatesDirectory($this->
config(
'templates.path'));
598 public function render( $template, $data = array(), $status = null ) {
599 if ( !is_null($status) ) {
602 $this->
view->appendData($data);
603 $this->
view->display($template);
624 if ( is_integer($time) ) {
625 $this->
response->header(
'Last-Modified', date(DATE_RFC1123, $time));
626 if ( $time === strtotime($this->
request->headers(
'IF_MODIFIED_SINCE')) ) $this->
halt(304);
628 throw new InvalidArgumentException(
'Slim::lastModified only accepts an integer UNIX timestamp value.');
649 public function etag( $value, $type =
'strong' ) {
652 if ( !in_array($type, array(
'strong',
'weak')) ) {
653 throw new InvalidArgumentException(
'Invalid Slim::etag type. Expected "strong" or "weak".');
657 $value =
'"' . $value .
'"';
658 if ( $type ===
'weak' ) $value =
'W/'.$value;
659 $this->
response->header(
'ETag', $value);
662 if ( $etagsHeader = $this->
request->headers(
'IF_NONE_MATCH')) {
663 $etags = preg_split(
'@\s*,\s*@', $etagsHeader);
664 if ( in_array($value, $etags) || in_array(
'*', $etags) ) $this->
halt(304);
686 public function setCookie(
$name, $value, $time = null,
$path = null, $domain = null, $secure = null, $httponly = null ) {
687 $time = is_null($time) ? $this->
config(
'cookies.lifetime') : $time;
689 $domain = is_null($domain) ? $this->
config(
'cookies.domain') : $domain;
690 $secure = is_null($secure) ? $this->
config(
'cookies.secure') : $secure;
691 $httponly = is_null($httponly) ? $this->
config(
'cookies.httponly') : $httponly;
692 $this->
response->getCookieJar()->setClassicCookie(
$name, $value, $time,
$path, $domain, $secure, $httponly);
725 $time = is_null($time) ? $this->
config(
'cookies.lifetime') : $time;
727 $domain = is_null($domain) ? $this->
config(
'cookies.domain') : $domain;
728 $secure = is_null($secure) ? $this->
config(
'cookies.secure') : $secure;
729 $httponly = is_null($httponly) ? $this->
config(
'cookies.httponly') : $httponly;
730 $userId = $this->
config(
'cookies.user_id');
731 $this->
response->getCookieJar()->setCookie(
$name, $value, $userId, $time,
$path, $domain, $secure, $httponly);
745 $value = $this->
response->getCookieJar()->getCookieValue(
$name);
746 return ($value ===
false) ? null : $value;
768 $domain = is_null($domain) ? $this->
config(
'cookies.domain') : $domain;
769 $secure = is_null($secure) ? $this->
config(
'cookies.secure') : $secure;
770 $httponly = is_null($httponly) ? $this->
config(
'cookies.httponly') : $httponly;
771 $this->
response->getCookieJar()->deleteCookie(
$name,
$path, $domain, $secure, $httponly );
787 return rtrim($_SERVER[
'DOCUMENT_ROOT'],
'/') . rtrim($this->
request->getRootUri(),
'/') .
'/';
801 $flash = $this->
view->getData(
'flash');
805 session_write_close();
823 public function halt( $status, $message =
'') {
824 if ( ob_get_level() !== 0 ) {
844 if ( ob_get_level() !== 0 ) {
856 $this->
response->header(
'Content-Type', $type);
895 if ( $status >= 300 && $status <= 307 ) {
896 $this->
response->header(
'Location', (
string)$url);
897 $this->
halt($status, (
string)$url);
899 throw new InvalidArgumentException(
'Slim::redirect only accepts HTTP 300-307 status codes.');
911 public function flash( $key, $value ) {
912 $this->
view->getData(
'flash')->set($key, $value);
922 $this->
view->getData(
'flash')->now($key, $value);
930 $this->
view->getData(
'flash')->keep();
942 public function hook(
$name, $callable, $priority = 10 ) {
943 if ( !isset($this->hooks[
$name]) ) {
944 $this->hooks[
$name] = array(array());
946 if ( is_callable($callable) ) {
947 $this->hooks[
$name][(int)$priority][] = $callable;
958 if ( !isset($this->hooks[
$name]) ) {
959 $this->hooks[
$name] = array(array());
961 if( !empty($this->hooks[$name]) ) {
963 if ( count($this->hooks[$name]) > 1 ) {
964 ksort($this->hooks[$name]);
966 foreach( $this->hooks[$name] as $priority ) {
967 if( !empty($priority) ) {
968 foreach($priority as $callable) {
969 $hookArg = call_user_func($callable, $hookArg);
989 if ( !is_null(
$name) ) {
990 return isset($this->hooks[(
string)
$name]) ? $this->hooks[(string)$name] : null;
1007 if ( !is_null(
$name) && isset($this->hooks[(
string)
$name]) ) {
1008 $this->hooks[(string)$name] = array(array());
1010 foreach( $this->hooks as $key => $value ) {
1011 $this->hooks[$key] = array(array());
1040 $dispatched =
false;
1041 $httpMethod = $this->
request()->getMethod();
1042 $httpMethodsAllowed = array();
1043 foreach ( $this->
router as $route ) {
1044 if ( $route->supportsHttpMethod($httpMethod) ) {
1046 $this->
applyHook(
'slim.before.dispatch');
1047 $dispatched = $route->dispatch();
1048 $this->
applyHook(
'slim.after.dispatch');
1049 if ( $dispatched ) {
1056 $httpMethodsAllowed = array_merge($httpMethodsAllowed, $route->getHttpMethods());
1059 if ( !$dispatched ) {
1060 if ( $httpMethodsAllowed ) {
1061 $this->
response()->header(
'Allow', implode(
' ', $httpMethodsAllowed));
1067 $this->
response()->write(ob_get_clean());
1069 $this->
view->getData(
'flash')->save();
1070 session_write_close();
1077 $this->
getLog()->error($e);
1078 if ( $this->
config(
'debug') ===
true ) {
1079 $this->
halt(500, self::generateErrorMarkup($e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString()));
1105 public static function handleErrors( $errno, $errstr =
'', $errfile =
'', $errline =
'' ) {
1106 if ( error_reporting() & $errno ) {
1107 throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
1126 $body =
'<p>The application could not run because of the following error:</p>';
1127 $body .=
"<h2>Details:</h2><strong>Message:</strong> $message<br/>";
1128 if (
$file !==
'' ) $body .=
"<strong>File:</strong> $file<br/>";
1129 if ( $line !==
'' ) $body .=
"<strong>Line:</strong> $line<br/>";
1130 if ( $trace !==
'' ) $body .=
'<h2>Stack Trace:</h2>' . nl2br($trace);
1146 $html =
"<html><head><title>$title</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}</style></head><body>";
1147 $html .=
"<h1>$title</h1>";
1149 $html .=
'</body></html>';
1158 echo
self::generateTemplateMarkup(
'404 Page Not Found',
'<p>The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.</p><a href="' . $this->
request->getRootUri() .
'">Visit the Home Page</a>');
1166 echo
self::generateTemplateMarkup(
'Error',
'<p>A website error has occured. The website administrator has been notified of the issue. Sorry for the temporary inconvenience.</p>');