public function who($module, $controller = 'index', $action = 'index', $stack = array()) { if ($this->isInList($this->_whiteList, $module, $controller, $action, $stack)) { $stackPart = implode('/', $stack); if ($stackPart) { $stackPart = "/{$stackPart}"; } Saf_Debug::out("White-listed access for {$module}/{$controller}/{$action}{$stackPart}"); return self::ACL_WHO_ANYONE; } if ($this->isInList($this->_blackList, $module, $controller, $action, $stack)) { $stackPart = implode('/', $stack); if ($stackPart) { $stackPart = "/{$stackPart}"; } Saf_Debug::out("Black-listed access for {$module}/{$controller}/{$action}{$stackPart}"); return self::ACL_WHO_NOONE; } if ($this->_authenticationEnabled && 'default' == $module && ('login' == $controller || 'index' == $controller && 'login' == $action)) { return self::ACL_WHO_ANYONE; } if ($this->_authenticationEnabled && 'default' == $module && ('logout' == $controller || 'index' == $controller && 'logout' == $action)) { return self::ACL_WHO_ANYUSER; } return self::ACL_WHO_UNKNOWN; }
public function __construct($configEnvironment = NULL, $configFilePath = NULL, $autoStart = FALSE) { Saf_Kickstart::go(); try { $this->_config = Saf_Config::load(!is_null($configFilePath) ? $configFilePath : APPLICATION_CONFIG, !is_null($configEnvironment) ? $configEnvironment : APPLICATION_ENV); } catch (Saf_Config_Exception_InvalidEnv $e) { Saf_Debug::out("Requested configutation section \"{$configEnvironment}\" not found, trying default..."); $this->_config = Saf_Config::load($configFilePath, 'default'); } $this->_applyConfig(); if ('install' == APPLICATION_STATUS && !array_key_exists('install', $_REQUEST)) { //#TODO how to access install $e = new Exception('This application is Install Mode and currently unavailable.'); Saf_Status::set(Saf_Status::STATUS_503_UNAVAILABLE); Saf_Kickstart::exceptionDisplay($e); } if ('down' == APPLICATION_STATUS) { $e = new Exception('This application is in Maintenance Mode and currently unavailable.'); Saf_Status::set(Saf_Status::STATUS_503_UNAVAILABLE); Saf_Kickstart::exceptionDisplay($e); } if ('online' != APPLICATION_STATUS) { $e = new Exception('This application is an unrecognized mode: ' . APPLICATION_STATUS . ' and currently unavailable.'); Saf_Status::set(Saf_Status::STATUS_503_UNAVAILABLE); Saf_Kickstart::exceptionDisplay($e); } if ($autoStart) { $this->start(); } }
public function routeShutdown(Zend_Controller_Request_Abstract $request) { try { $this->_statusRules($request->getModuleName(), $request->getControllerName(), $request->getActionName(), $request->getParam('resourceStack')); $this->_aclRules($request->getModuleName(), $request->getControllerName(), $request->getActionName(), $request->getParam('resourceStack'), $request->getQuery()); $this->_workflowRules($request->getModuleName(), $request->getControllerName(), $request->getActionName(), $request->getParam('resourceStack')); } catch (Saf_Controller_Front_Plugin_RouteRules_Exception $e) { Saf_Debug::out('Enforcing Routing Rule: ' . $e->getMessage()); $request->setModuleName($e->getModuleName()); $request->setControllerName($e->getControllerName()); $request->setActionName($e->getActionName()); $request->setParam('resourceStack', $e->getResourceStack()); } }
public function __construct($configEnvironment = NULL, $configFilePath = NULL, $autoStart = FALSE) { Saf_Kickstart::go(); try { $this->_config = Saf_Config::load(!is_null($configFilePath) ? $configFilePath : APPLICATION_CONF, !is_null($configEnvironment) ? $configEnvironment : APPLICATION_ENV); } catch (Saf_Config_Exception_InvalidEnv $e) { Saf_Debug::out("Requested configutation section \"{$configEnvironment}\" not found, trying default..."); $this->_config = Saf_Config::load($configFilePath, 'default'); } $this->_autoLoad = $this->_config->getOptional('autoLoad', FALSE); $this->_debugMode = $this->_config->getOptional('debug:mode', Saf_Debug::DEBUG_MODE_OFF); $this->_errorMode = $this->_config->getOptional('error:mode', Saf_Debug::ERROR_MODE_INTERNAL); Saf_Debug::init($this->_debugMode, $this->_errorMode, FALSE); Saf_Kickstart::initializeAutoloader($this->_autoLoad); //#TODO #2.0.0 bootstrap config //#TODO #2.0.0 init plugins // loggingf // db // etc. if ($this->_config->has('plugins')) { foreach ($this->_config->get('plugins:+') as $pluginName => $pluginConfig) { } print_r(array('plugins', gettype($this->_config->get('plugins:+')), $this->_config->get('plugins:+'))); print_r(array('plugins2', gettype($this->_config->get('plugins2:+')), $this->_config->get('plugins2'))); } /* foreach() { } * if( 'install' == APPLICATION_STATUS && !array_key_exists('install', $_REQUEST) ) { $e = new Exception('This application is Install Mode and currently unavailable.'); Saf_Status::set(Saf_Status::STATUS_503_UNAVAILABLE); Saf_Kickstart::exceptionDisplay($e); } if('down' == APPLICATION_STATUS) { $e = new Exception('This application is in Maintenance Mode and currently unavailable.'); Saf_Status::set(Saf_Status::STATUS_503_UNAVAILABLE); Saf_Kickstart::exceptionDisplay($e); } */ if ($autoStart) { $this->start($autoStart); } }
public function parseAccept($string, $table = self::ACCEPT_FORMAT) { $default = self::$_acceptTables[$table][0]; if ('' == trim($string)) { Saf_Debug::out("No accept format specified, using default ({$default}).", 'NOTICE'); return $default; } $array = explode(',', $string); foreach ($array as $option) { $format = strpos($option, ';') !== FALSE ? substr($option, 0, strpos($option, ';')) : $option; if (array_key_exists($format, self::$_acceptTables[$table])) { $notice = "Accept format ({$format}), using " . self::$_acceptTables[$table][$format] . '.'; Saf_Debug::out($notice, 'NOTICE'); return self::$_acceptTables[$table][$format]; } } Saf_Debug::out("Unrecognized accept format ({$format}), using default ({$default}).", 'NOTICE'); return $default; }
public function addError($error) { $this->_errorMessage[] = $error; if ($this->_debugMode) { Saf_Debug::out($error); } }
/** * Searches for string $key in array $array and returns true only if * it the key exists and the value it contains is an integer, boolean, or * non-empty. Optional third parameter can allow one or more falsy values: * NULL : Saf_Array::TYPE_NULL, * empty array : Saf_Array::TYPE_ARRAY, * empty string : Saf_Array::TYPE_STRING * * $key may be an array, this method will return true if all are present. * * @param string $key string array key to search for * @param array $array to be searched * @param int $allowedBlankTypes bitwise integer of blank types that are allowed * @return bool key exists and value is not blank */ public static function keyExistsAndNotBlank($key, $array, $allowedBlankTypes = 0) { if (!is_array($array)) { Saf_Debug::out('Saf_Array::keyExistsAndNotBlank got a non-array operand.'); return false; } if (!is_array($key)) { $key = array($key); } foreach ($key as $arrayKey) { if (!array_key_exists($arrayKey, $array) || !($allowedBlankTypes & self::TYPE_NULL || !is_null($array[$arrayKey])) || !(is_object($array[$arrayKey]) || is_array($array[$arrayKey]) && ($allowedBlankTypes & self::TYPE_ARRAY || count($array[$arrayKey]) > 0) || is_string($array[$arrayKey]) && ($allowedBlankTypes & self::TYPE_STRING || '' != trim($array[$arrayKey])) || is_bool($array[$arrayKey]) || is_numeric($array[$arrayKey]) && !is_string($array[$arrayKey]) || is_resource($array[$arrayKey]))) { return false; } } return true; }
public function fail() { if (Saf_Debug::isEnabled()) { Saf_Debug::out('Authentication Declined.'); } }
public static function set($status) { switch ($status) { case 200: case '200': self::_header('200 OK'); break; case 201: case '201': self::_header('201 Created'); break; case 202: case '202': self::_header('202 Accepted'); break; case 203: case '203': self::_header('203 Non-Authoritative Information'); break; case 204: case '204': self::_header('204 No Content'); break; case 205: case '205': self::_header('205 Reset Content'); break; case 300: case '300': self::_header('300 Multiple Choices'); break; case 301: case '301': self::_header('301 Moved Permanently'); //don't keep using the request-uri break; case 302: case '302': self::_header('302 Found'); //temporary, keep using the request-uri break; case 303: case '303': self::_header('303 See Other'); //context specific, keep using the request-uri POST safe redirect option //#TODO #2.0.0 use this for redirect exception, with 302 as the non-default antique browser option break; case 304: case '304': self::_header('304 Not Modified'); break; case 307: case '307': self::_header('307 Temporary Redirect'); //temporary, keep using the request-uri stricter alternative to 302 //which may incorrectly auto-redirect break; case 400: case '400': self::_header('400 Bad Request'); break; case 401: case '401': self::_header('401 Unauthorized'); break; case 403: case '403': self::_header('403 Forbidden'); break; case 404: case '404': self::_header('404 Not Found'); break; case 405: case '405': self::_header('405 Method Not Allowed'); break; case 406: case '406': self::_header('406 Not Acceptable'); //cannot formulate a response that would conform to the client's //expectations. break; case 408: case '408': self::_header('408 Request Timeout'); break; case 409: case '409': self::_header('409 Conflict'); break; case 410: case '410': self::_header('410 Gone'); break; case 412: case '412': self::header('412 Precondition Failed'); break; case 413: case '413': self::_header('413 Request Entity Too Large'); break; case 415: case '415': self::_header('415 Unsupported Media Type'); break; case 416: case '416': self::_header('416 Expectation Failed'); break; case 500: case '500': self::_header('500 Internal Server Error'); break; case 501: case '501': self::_header('501 Not Implemented'); break; case 502: case '502': self::_header('502 Bad Gateway'); break; case 503: case '503': self::_header('503 Service Unavailable'); break; case 504: case '504': self::_header('504 Gateway Timeout'); break; default: if (class_exists('Saf_Debug')) { Saf_Debug::out('Unrecognized HTTP Status Set Request: ' . $status); } return FALSE; } return TRUE; }
public function routeShutdown(Zend_Controller_Request_Abstract $request) { $stack = explode('/', $request->getPathInfo()); $newStack = array(); $preRouter = array(); if ('' == $stack[count($stack) - 1]) { array_pop($stack); } if (count($stack) && '' == $stack[0]) { array_shift($stack); } $pathParts = explode('/', ROUTER_PATH); if ('' == $pathParts[count($pathParts) - 1]) { array_pop($pathParts); } if (count($pathParts) && '' == $pathParts[0]) { array_shift($pathParts); } if ($pathParts) { if (array_key_exists(0, $pathParts)) { $request->setModuleName($pathParts[0]); } if (array_key_exists(1, $pathParts)) { $request->setControllerName($pathParts[1]); } if (array_key_exists(2, $pathParts)) { $request->setActionName($pathParts[2]); } if (array_key_exists(3, $pathParts)) { $newStack = array_merge(array_splice($pathParts, 3), $stack); } } else { $routerFound = FALSE; $moduleFound = 'default' == $request->getModuleName(); $controllerFound = $moduleFound && 'index' == $request->getControllerName(); $actionFound = $controllerFound && 'index' == $request->getActionName(); $router = ROUTER_NAME; $controllerReflector = NULL; foreach ($stack as $part) { //Saf_Debug::outData(array($part)); $routerFound = $routerFound || TRUE; //#TODO #2.0.0 is this still needed for non Zend Routing? if (!$moduleFound && $request->getModuleName() == $part) { $moduleFound = TRUE; array_shift($stack); } else { if (!$controllerFound && $request->getControllerName() == $part) { $controllerFound = TRUE; //#TODO #9.9.9 handle bug with routing with path #¯\_(ツ)_/¯ $controllerName = ucfirst($request->getControllerName()); $front = Zend_Controller_Front::getInstance(); $paths = $front->getControllerDirectory(); $controllerClass = "{$controllerName}Controller"; foreach ($paths as $path) { if (file_exists("{$path}/{$controllerClass}.php")) { include_once "{$path}/{$controllerClass}.php"; } } $controllerReflector = new ReflectionClass($controllerClass); //#TODO #2.0.0 handle the case where class is non-existant (i.e. module/[index/index/]resourcestack) array_shift($stack); continue; } else { if (!$actionFound && $request->getActionName() == $part) { $actionFound = TRUE; $actionName = ucfirst($request->getActionName()); $controllerHasAction = $controllerReflector && $controllerReflector->hasMethod("{$actionName}Action"); if ($controllerHasAction) { array_shift($stack); } else { $request->setActionName(''); } continue; } } } if ($routerFound && $moduleFound && $controllerFound && $actionFound) { //Saf_Debug::outData(array('stacking...', $routerFound, $moduleFound, $controllerFound, $request->getActionName(), $actionFound, $part)); $newStack[] = array_shift($stack); } else { //Saf_Debug::outData(array('prerouting...', $routerFound, $moduleFound, $controllerFound, $request->getActionName(), $actionFound, $part)); $preRouter[] = array_shift($stack); } } //Saf_Debug::outData(array('preparts',$pathParts,$newStack)); if (count($stack)) { $newStack = array_merge($newStack, $stack); } //Saf_Debug::outData(array('postparts',$newStack)); if ($preRouter && !$newStack) { $newStack = $preRouter; $preRouter = array(); } } if ($preRouter) { Saf_Debug::outData(array('preRouter' => $preRouter)); } $request->setParam('resourceStack', $newStack); $stackString = implode('/', $newStack); $module = $request->getModuleName(); $controller = $request->getControllerName(); $action = $request->getActionName(); Saf_Debug::out("Resolved to path: {$module} {$controller} {$action} {$stackString}", 'NOTICE'); }
/** * steps that can't wait for a bootstrap to kick in */ protected static function _goPreBoot() { if (function_exists('libxml_use_internal_errors')) { libxml_use_internal_errors(TRUE); } else { Saf_Debug::out('Unable to connect LibXML to integrated debugging. libxml_use_internal_errors() not supported.', 'NOTICE'); } }
public static function saveHash($file, $uname, $value) { if (is_null($value)) { Saf_Debug::outData(array("saving null value to hash, {$file}:{$uname}")); } if (!array_key_exists($file, self::$_hashMemory)) { self::$_hashMemory[$file] = array(); } self::$_hashMemory[$file][$uname] = $value; if (strpos($file, 'hash/') === 0) { try { $fileUnhash = substr($file, 5); self::_initHash($fileUnhash); } catch (Exception $e) { Saf_Debug::out("unable to prepare hash for {$file} : {$uname}. " . $e->getMessage()); } } $path = self::$_path . '/' . $file; $mode = file_exists($path) ? 'r+' : 'w'; //#NOTE could use c+, but $mode is overloaded $pointer = fopen($path, $mode); $fileLock = flock($pointer, LOCK_EX); if (!$fileLock) { Saf_Debug::out("write blocking {$file}"); $fileLock = flock($pointer, LOCK_EX | LOCK_NB); } if ($fileLock) { $hashValue = 'r+' == $mode ? json_decode(fread($pointer, filesize($path)), TRUE) : array(); if (is_null($hashValue)) { Saf_Debug::out("cache invalid, resetting {$file}"); $hashValue = array(); } ftruncate($pointer, 0); rewind($pointer); $time = time(); $hashValue[$uname] = array('stamp' => $time, 'payload' => $value); fwrite($pointer, json_encode($hashValue, JSON_FORCE_OBJECT)); //Saf_Debug::out("cached {$file} : {$uname}"); } else { Saf_Debug::out("unable to save {$file} : {$uname}"); } flock($pointer, LOCK_UN); fclose($pointer); }