/** * Create a normalized tree of UploadedFile instances from the Environment. * * @param Environment $env The environment * * @return array|null A normalized tree of UploadedFile instances or null if none are provided. */ public static function createFromEnvironment(Environment $env) { if (is_array($env['slim.files']) && $env->has('slim.files')) { return $env['slim.files']; } elseif (isset($_FILES)) { return static::parseUploadedFiles($_FILES); } return []; }
/** * Setup the logger instance using the proper * adapter for the given connector OR the default * one if 'default' is given. * * Giving a connector named 'null' will result in * a null logger: a logger that doesn't log! * * @param string $connector */ public function __construct($connector = 'default') { $this->connection_string = $connector; //create te logger from the correct adapter if ($connector === null || strlen($connector) == 0 || strtolower($connector) == 'null' || strtolower($connector) == 'void') { $this->adapter = new \Psr\Log\NullLogger(); } elseif (strpos($connector, '://') !== false) { if ($connector == 'default') { $connector = Environment::GetCurrentEnvironment()->GetConfigurationProperty('LOG_CONNECTION_STRING'); } //separe adapter name from connection info $conection_exploded = explode('://', $connector, 2); //open a log if it is really possible: if (count($conection_exploded) == 2) { $adapter = $conection_exploded[0]; $query = $conection_exploded[1]; //get the classname from the adapter name $adapter_class = 'Gishiki\\Logging\\Adapter\\' . ucwords(strtolower($adapter)) . 'Adapter'; if (class_exists($adapter_class)) { $reflected_logger = new \ReflectionClass($adapter_class); $this->adapter = $reflected_logger->newInstance($query); } } } }
/** * Setup a logger that works on files. * * Default is error.log on the application root * * @param string $file_path the path of the file */ public function __construct($file_path = '') { //get the file path $this->path = $file_path != '' ? $file_path : Environment::GetCurrentEnvironment()->GetConfigurationProperty('APPLICATION_DIR') . 'error.log'; //open the file $this->handler = fopen($file_path, 'a'); }
/** * Create a base exception and save the log of what's happening. * * @param string $message the error message * @param int $errorCode the error code */ public function __construct($message, $errorCode) { //perform a basic Exception constructor call parent::__construct($message, $errorCode, null); //setup an empty logger $this->logger = null; $logger = !is_null(Environment::GetCurrentEnvironment()) ? new Logger() : new Logger('null'); //build the new log entry $this->setLogger($logger); //and use it to transmit the log entry $this->writeLog(); }
/** * Execute the requested operation. */ public static function Run() { //initialize the framework self::Initialize(); //each Gishiki instance is binded with a newly created Environment if (!is_object(self::$executionEnvironment)) { self::$executionEnvironment = new Environment(filter_input_array(INPUT_SERVER), true, true); } //if the framework needs to be installed..... if (Environment::applicationExists()) { //fulfill the client request Environment::GetCurrentEnvironment()->FulfillRequest(); } else { //show the no application page! echo file_get_contents(__DIR__ . DS . 'no_application.html'); } }
/** * Execute the given controller. * * @param string $action the name of the controller and action to be used * @param Request $request the request to serve * @param Response $response the response to the given request * @param GenericCollection $arguments the list of passed arguments * * @throws \InvalidArgumentException the given action identifier does not identify a valid action */ public static function Execute($action, Request &$request, Response &$response, GenericCollection &$arguments) { //check for bad action if (!is_string($action) || strpos($action, '@') === false && strpos($action, '->') === false) { throw new \InvalidArgumentException("The name of the controller to be executed must be expressed as: 'action@controller' or 'controller->action'"); } //get the name of the action and the controller to be executed $controller = strpos($action, '@') !== false ? explode('@', $action) : explode('->', $action); //check for bad names if (strlen($controller[0]) <= 0 || strlen($controller[1]) <= 0) { throw new \InvalidArgumentException('The name of the action to be taken and controller to be selectad cannot be empty names'); } if (strpos($action, '->') !== false) { $temp = $controller[1]; $controller[1] = $controller[0]; $controller[0] = $temp; } $controllerName = $controller[1]; $controllerAction = $controller[0]; //and check for a class with the given name if (!class_exists($controllerName) && !is_null(Environment::GetCurrentEnvironment())) { //get the name of the controller file $controllerFilepath = Environment::GetCurrentEnvironment()->GetConfigurationProperty('CONTROLLER_DIR') . $controllerName . '.php'; if (!file_exists($controllerFilepath)) { throw new \InvalidArgumentException('The given controller cannot be found on your application directory'); } //include the controller file include $controllerFilepath; } //and re-check for the given controller name if (!class_exists($controllerName)) { throw new \InvalidArgumentException('The given controller (' . $controllerName . ') does NOT identify a valid controller'); } //reflect the given controller class $reflectedController = new \ReflectionClass($controllerName); //and create a new instance of it $controllerMethod = $reflectedController->newInstanceArgs([&$request, &$response, &$arguments]); //reflect the requested action $reflected_action = new \ReflectionMethod($controllerName, $controllerAction); $reflected_action->setAccessible(true); //can invoke private methods :) //and execute it $reflected_action->invoke($controllerMethod); }
/** * Create new Uri from environment. * * @param Environment $env * * @return self */ public static function createFromEnvironment(Environment $env) { // Scheme $isSecure = $env->get('HTTPS'); $scheme = empty($isSecure) || $isSecure === 'off' ? 'http' : 'https'; // Authority: Username and password $username = $env->get('PHP_AUTH_USER', ''); $password = $env->get('PHP_AUTH_PW', ''); // Authority: Host if ($env->has('HTTP_HOST')) { $host = $env->get('HTTP_HOST'); } else { $host = $env->get('SERVER_NAME'); } // Authority: Port $port = (int) $env->get('SERVER_PORT', 80); if (preg_match('/^(\\[[a-fA-F0-9:.]+\\])(:\\d+)?\\z/', $host, $matches)) { $host = $matches[1]; if ($matches[2]) { $port = (int) substr($matches[2], 1); } } else { $pos = strpos($host, ':'); if ($pos !== false) { $port = (int) substr($host, $pos + 1); $host = strstr($host, ':', true); } } // Path $requestScriptName = parse_url($env->get('SCRIPT_NAME'), PHP_URL_PATH); $requestScriptDir = dirname($requestScriptName); // parse_url() requires a full URL. As we don't extract the domain name or scheme, // we use a stand-in. $requestUri = parse_url('http://example.com' . $env->get('REQUEST_URI'), PHP_URL_PATH); $basePath = ''; $virtualPath = $requestUri; if (stripos($requestUri, $requestScriptName) === 0) { $basePath = $requestScriptName; } elseif ($requestScriptDir !== '/' && stripos($requestUri, $requestScriptDir) === 0) { $basePath = $requestScriptDir; } if ($basePath) { $virtualPath = ltrim(substr($requestUri, strlen($basePath)), '/'); } // Query string $queryString = $env->get('QUERY_STRING', ''); // Fragment $fragment = ''; // Build Uri $uri = new static($scheme, $host, $port, $virtualPath, $queryString, $fragment, $username, $password); if ($basePath) { $uri = $uri->withBasePath($basePath); } return $uri; }
/** * Create an encryption key using the given serialized key. * * A serialized key is the hexadecimal representation of key. * * You can use the generate() function to retrive a really * secure key from the password (the same key derivation * algorithm that openssl internally uses). * * Usage example: * * <code> * //generate a secure pbkdf2-derived key and use it as the encryption key * $my_key = new SecretKey(SecretKey::generate("mypassword")); * * //you MUST save the generated key, because it won't be possible to * //generate the same key once again (even using the same password)! * $precious_key = (string) $my_key; * </code> * * @param string $key the password to be used */ public function __construct($key = null) { //check for the input if ((!is_string($key) || strlen($key) <= 2) && !is_null($key)) { throw new \InvalidArgumentException('The secure key must be given as a non-empty string that is the hex representation of the real key'); } //get the symmetric key to be used $key = !is_null($key) ? $key : Environment::GetCurrentEnvironment()->GetConfigurationProperty('MASTER_SYMMETRIC_KEY'); //get the real encryption key $this->keyLength = strlen($key) / 2; $this->key = hex2bin($key); }
/** * Export this private key in a string format. * * The resulting string can be used to construct another PrivateKey instance: * * <code> * use Gishiki\Security\Encryption\Asymmetric\PrivateKey; * * //this is the exported private key * $exported_key = "..."; * * //rebuild the private key * $privateKey = new PrivateKey($exported_key); * </code> * * @param string $keyPassword the private key password * * @return string the serialized private key * * @throws \InvalidArgumentException the given password is not a string * @throws AsymmetricException the given key is invalid */ public function export($keyPassword = '') { if (!is_string($keyPassword)) { throw new \InvalidArgumentException('The private key password cannot be something else than a string'); } elseif (!$this->isLoaded()) { throw new AsymmetricException('It is impossible to serialize an unloaded private key: ' . openssl_error_string(), 1); } $serialized_key = ''; //build the configuration array $config = ['digest_alg' => 'sha512', 'private_key_type' => OPENSSL_KEYTYPE_RSA]; //use the application openssl configuration if (!is_null(Environment::GetCurrentEnvironment())) { $config = array_merge($config, ['config' => file_exists(Environment::GetCurrentEnvironment()->GetConfigurationProperty('APPLICATION_DIR') . 'openssl.cnf') ? Environment::GetCurrentEnvironment()->GetConfigurationProperty('APPLICATION_DIR') . 'openssl.cnf' : null]); } //serialize the key and encrypt it if requested if (strlen($keyPassword) > 0) { openssl_pkey_export($this->key, $serialized_key, $keyPassword, $config); } else { openssl_pkey_export($this->key, $serialized_key, null, $config); } //return the serialized key return $serialized_key; }