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');
140 if ( strpos($class,
'Slim') !== 0 ) {
143 $file = dirname(__FILE__) .
'/' . str_replace(
'_', DIRECTORY_SEPARATOR, substr($class,5)) .
'.php';
144 if ( file_exists(
$file) ) {
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']
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;
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) ) {
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'));
599 if ( !is_null($status) ) {
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);
649 public function etag( $value, $type =
'strong' ) {
652 if ( !in_array($type,
array(
'strong',
'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 ) {
897 $this->
halt($status, (
string)$url);
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]) ) {
946 if ( is_callable($callable) ) {
947 $this->hooks[
$name][(int)$priority][] = $callable;
958 if ( !isset($this->hooks[
$name]) ) {
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]) ) {
1010 foreach( $this->hooks as $key => $value ) {
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);
1131 return self::generateTemplateMarkup(
'Slim Application Error', $body);
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>');
contentType( $type)
Set the HTTP response Content-Type.
static getInstance( $name='default')
Get Slim application with name.
view( $viewClass=null)
Get and/or set the View.
flashKeep()
Keep flash messages from previous request for subsequent request.
getCookie( $name)
Get the value of a Cookie from the current HTTP Request.
lastModified( $time)
Set Last-Modified HTTP Response Header.
static generateTemplateMarkup( $title, $body)
Generate default template markup.
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
error( $argument=null)
Error Handler.
root()
Get the Slim application's absolute directory path.
map()
Add generic route without associated HTTP method.
status( $code)
Set the HTTP response status code.
config( $name, $value=null)
Configure Slim Settings.
notFound( $callable=null)
Not Found Handler.
getMode()
Get application mode.
hook( $name, $callable, $priority=10)
Assign hook.
Add rich text string
The name of the decorator.
run()
Run the Slim application.
flashNow( $key, $value)
Set flash message for current request.
getEncryptedCookie( $name)
Get the value of an encrypted Cookie from the current HTTP request.
setEncryptedCookie( $name, $value, $time=null, $path=null, $domain=null, $secure=null, $httponly=null)
Set an encrypted Cookie.
getName()
Get Slim application name.
flash( $key, $value)
Set flash message for subsequent request.
clearHooks( $name=null)
Clear hook listeners.
configureMode( $mode, $callable)
Configure Slim for a given mode.
defaultNotFound()
Default Not Found handler.
getHooks( $name=null)
Get hook listeners.
setCookie( $name, $value, $time=null, $path=null, $domain=null, $secure=null, $httponly=null)
Set a normal, unencrypted Cookie.
etag( $value, $type='strong')
Set ETag HTTP Response Header.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
redirect( $url, $status=302)
Redirect.
setName( $name)
Set Slim application name.
render( $template, $data=array(), $status=null)
Render a template.
Create styles array
The data for the language used.
static handleErrors( $errno, $errstr='', $errfile='', $errline='')
Handle errors.
router()
Get the Router object.
request()
Get the Request object.
response()
Get the Response object.
static generateErrorMarkup( $message, $file='', $line='', $trace='')
Generate markup for error message.
deleteCookie( $name, $path=null, $domain=null, $secure=null, $httponly=null)
Delete a Cookie (for both normal or encrypted Cookies)
static autoload( $class)
Slim auto-loader.
defaultError()
Default Error handler.
Slim - a micro PHP 5 framework.
getLog()
Get application Log (lazy-loaded)
applyHook( $name, $hookArg=null)
Invoke hook.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
halt( $status, $message='')
Halt.
urlFor( $name, $params=array())
Get the URL for a named Route.
__construct( $userSettings=array())
Constructor.
options()
Add OPTIONS route.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
mapRoute($args)
Add GET|POST|PUT|DELETE route.
Slim - a micro PHP 5 framework.