/** * Change level flags * * @param string $list * @param bool $state */ private function _changeLevelFlags($list, $state) { $flags = Steelcode_String_Helper::explode(',', $list); foreach ($flags as $flag) { $this->_levelFlags[trim($flag)] = $state; } }
/** * Find the extension of a file from its filename and * get it in lower case * * @param string $file : name of the file * @return string : extension of the file in lower case */ public static function fileExtension($file) { if (Steelcode_String_Helper::isNull($file)) { return null; } else { $ext = end(explode('.', $file)); return strtolower($ext); } }
/** * Class constructor * * @param string $salt */ public function __construct($salt = null) { if ($salt === null) { $salt = Steelcode_String_Helper::randomString(32); } if (32 === Steelcode_String_Helper::safeLength($salt)) { $this->_salt = $salt; } else { throw new Steelcode_Crypto_Exception('Salt should be exactly 32 characters long'); } }
/** * Creates a new model * * @return bool */ protected function _createComponent() { $this->_model = ucfirst(Steelcode_String_Helper::dashedToCamel($this->_name)); $model = "{$this->_model}.php"; $this->_path = "{$this->_location}/application/models/{$model}"; $this->_filePointer = fopen($this->_path, "w"); if ($this->_filePointer === null) { $this->_setErrorText("Error! Could not create model:\n\t- {$this->_path}"); return false; } $this->_writeContent(); $this->_setMessageText("Done! Successfully created new model\n\t- {$this->_name}"); return true; }
/** * Creates a new configuration class * * @return bool */ protected function _createComponent() { $this->_config = ucfirst(Steelcode_String_Helper::dashedToCamel($this->_name)); $config = "{$this->_config}.php"; $this->_path = "{$this->_location}/application/configs/{$config}"; $this->_filePointer = fopen($this->_path, "w"); if ($this->_filePointer === null) { $this->_setErrorText("* Error! Could not create configuration:\n\t- {$this->_path}"); return false; } fwrite($this->_filePointer, "<?php\n"); fwrite($this->_filePointer, "/**\n * Class Config_{$this->_config}\n */\n"); $text = "class Config_{$this->_config} " . '{' . "\n\n}\n"; fwrite($this->_filePointer, $text); $this->_setMessageText("Done! Successfully created new configuration class:\n\t- Config_{$this->_config}"); return true; }
/** * Prepare column names for query * * @param array $columns * @return string */ public static function columns(array $columns) { $columnValue = ""; foreach ($columns as $key => $value) { if (is_numeric($key)) { $columnValue = ""; } else { $columnValue = "{$key}."; } if (Steelcode_String_Helper::hasSubSting(' ', $value)) { $columnValue .= $value; } else { $columnValue = "`{$columnValue}{$value}`"; } $columns[$key] = $columnValue; } return Steelcode_Array_Helper::implode(', ', $columns); }
/** * Get the current script url without the script name * * @return string */ public static function url() { $hostUrl = self::host(); if (!empty($scriptName = Steelcode_Server_Vars::getVar('SCRIPT_NAME'))) { $arr = explode('/', $scriptName); $arrLen = count($arr); if ($arrLen <= 2) { return $hostUrl; } else { if (Steelcode_String_Helper::isNull($arr[0])) { unset($arr[0]); } unset($arr[$arrLen - 1]); $newUrl = implode('/', $arr); return "{$hostUrl}/{$newUrl}"; } } return $hostUrl; }
/** * Hash a given password using BlowFish algorithm * * @param string $password * @return string */ public function hash($password) { if ($this->_salt === null) { $randomString = Steelcode_String_Helper::randomString(10); $randomSalt = substr(sha1($randomString), 0, 22); } else { $randomSalt = $this->_salt; } $this->_salt = null; $hashCount = $this->_loopCount; $hashPassword = "******"; while ($hashCount >= 0) { $hashPassword = sha1(sprintf("%s%s%d", $hashPassword, $randomSalt, $hashCount)); $hashCount--; } $hashPassword = crypt($hashPassword, '$2y$12$' . $randomSalt . '$'); echo $randomString . "<br>"; $hashLength = strlen($hashPassword); exit(sprintf("%s%s", $randomSalt, substr($hashPassword, 29, $hashLength))); }
/** * Set the domain * * @param string $domain */ public function setDomain($domain) { $this->_domain = Steelcode_String_Helper::camelToDashed($domain); }
/** * Create project components according to configuration * * @throws Steelcode_Project_Exception */ public function execute() { if (!isset($this->_config['component'])) { throw new Steelcode_Project_Exception('Component name not found'); } try { switch ($this->_config['component']) { case 'application': $this->_objComponent = new Steelcode_Project_Application(); break; case 'domain': $this->_objComponent = new Steelcode_Project_Domain(); break; case 'controller': if (!isset($this->_config['parent']) || Steelcode_String_Helper::isNull($this->_config['parent'])) { throw new Steelcode_Project_Exception('* Error! Domain is not specified for creating controller'); } $this->_objComponent = new Steelcode_Project_Controller(); $this->_objComponent->setDomain($this->_config['parent']); break; case 'model': $this->_objComponent = new Steelcode_Project_Model(); break; case 'layout': $this->_config['name'] = "layout-{$this->_config['name']}"; $this->_objComponent = new Steelcode_Project_Layout(); break; case 'config': $this->_objComponent = new Steelcode_Project_Config(); break; case 'bootstrap': $this->_objComponent = new Steelcode_Project_Bootstrap(); break; default: throw new Steelcode_Project_Exception("* Error! Invalid component name '{$this->_config['component']}' found"); break; } $this->_objComponent->setLocation($this->_location); $this->_objComponent->create($this->_config['name']); $this->_messageText = $this->_objComponent->getMessageText(); } catch (Steelcode_Project_Exception $ex) { $this->_errorText = $ex->getMessage(); throw $ex; } }
/** * Get file contents from $_FILE server variable and move * the uploaded file to a location specified in $location * * @param string $location (optional) : upload media directory * @throws Steelcode_Media_Exception */ public function uploadFile($location = null) { if (!isset($_FILES) || !is_array($_FILES)) { $this->_lastFileError = 'No files are selected to upload'; throw new Steelcode_Media_Exception($this->_lastFileError); } if (Steelcode_String_Helper::isNull($location)) { $mediaDir = $this->_mediaLocation; } else { $mediaDir = $location; } if (Steelcode_String_Helper::isNull($mediaDir)) { $this->_lastFileError = 'No destination is set to move uploaded files'; throw new Steelcode_Media_Exception($this->_lastFileError); } $fileName = $_FILES[$this->_inputIndex]['name']; $fileSize = $_FILES[$this->_inputIndex]['size']; $fileType = $_FILES[$this->_inputIndex]['type']; $fileExt = Steelcode_Media_Helper::fileExtension($fileName); if ($fileSize > $this->_fileMaxSize) { $this->_lastFileError = 'File size is larger than the limit'; throw new Steelcode_Media_Exception($this->_lastFileError); } if (false === in_array($fileExt, $this->_extensionList)) { $this->_lastFileError = 'You can not upload this kind of file'; throw new Steelcode_Media_Exception($this->_lastFileError); } do { $shaName = sha1($fileName); $shaName = str_shuffle($shaName) . ".{$fileExt}"; $newFilePath = "{$mediaDir}{$shaName}"; } while (file_exists($newFilePath)); if ($_FILES[$this->_inputIndex]['error'] > 0) { $this->_lastFileError = 'Server set an error code (' . $_FILES[$this->_inputIndex]['error'] . ')'; $this->_lastFileErrorCode = $_FILES[$this->_inputIndex]['error']; throw new Steelcode_Media_Exception($this->_lastFileError, $this->_lastFileErrorCode); } if (move_uploaded_file($_FILES[$this->_inputIndex]['tmp_name'], $newFilePath)) { $this->_lastFileName = $fileName; $this->_lastFileType = $fileType; $this->_lastFileExt = $fileExt; $this->_lastFileSize = $fileSize; $this->_lastFileHash = $shaName; } else { $this->_lastFileError = 'There was an error while uploading the file'; throw new Steelcode_Media_Exception($this->_lastFileError); } }
/** * Decode the token * * @param string $token * @return true */ protected function _decode($token) { $segments64 = Steelcode_String_Helper::explode('.', $token); if (count($segments64) !== 3) { $this->_setMessage('Wrong number of segments. Token is not a valid JSON web token'); return false; } $segments = $segments64; foreach ($segments as $index => $value) { $segments[$index] = $this->urlSafeB64Decode($value); if ($index < 2) { $segments[$index] = Steelcode_Json_Helper::decode($segments[$index]); } } if (empty($segments[0])) { $this->_setMessage('Invalid header encoding'); return false; } if (empty($segments[1])) { $this->_setMessage('Invalid claims encoding'); return false; } if (empty($segments[0]->alg) || !isset($this->_methods[$segments[0]->alg])) { $this->_setMessage('Invalid or unsupported algorithm'); return false; } $this->_algorithm = $segments[0]->alg; if (!$this->_verify("{$segments64[0]}.{$segments64[1]}", $segments[2], $this->_key, $this->_algorithm)) { $this->_setMessage('Signature verification failed'); return false; } if (isset($segments[1]->nbf) && $segments[1]->nbf > Steelcode_Date_Helper::time() + $this->_leeway) { $this->_setMessage('Cannot handle this token before ' . Steelcode_Date_Helper::date(DateTime::ISO8601, $segments[1]->nbf)); return false; } if (isset($segments[1]->iat) && $segments[1]->iat > Steelcode_Date_Helper::time() + $this->_leeway) { $this->_setMessage('Cannot handle token prior to ' . Steelcode_Date_Helper::date(DateTime::ISO8601, $segments[1]->iat)); return false; } if (isset($segments[1]->exp) && Steelcode_Date_Helper::time() - $this->_leeway >= $segments[1]->exp) { $this->_setMessage('Token expired'); return false; } $this->_header = (array) $segments[0]; $this->_payload = (array) $segments[1]; $this->_signature = $segments[2]; return true; }
define('INCLUDE_PATH', 'Steelcode/'); require_once INCLUDE_PATH . 'steelcode.php'; $opt = new Steelcode_Console_Getopt(); /* Array of available short options */ $shortOpts = array('l' => $opt::REQUIRED, 'a' => $opt::REQUIRED, 'c' => $opt::REQUIRED, 'n' => $opt::REQUIRED, 'p' => $opt::OPTIONAL); /* Array of available long options */ $longOptions = array('location' => $opt::REQUIRED, 'action' => $opt::REQUIRED, 'component' => $opt::REQUIRED, 'name' => $opt::REQUIRED, 'parent' => $opt::OPTIONAL); $opt->setShortOption($shortOpts); $opt->setLongOption($longOptions); $options = $opt->getOptions(); try { if (isset($options['l']) || isset($options['location'])) { $location = isset($options['l']) ? $options['l'] : $options['location']; } else { exit("No target location specified!\n"); } $config = array('action' => isset($options['a']) ? $options['a'] : $options['action'], 'component' => isset($options['c']) ? $options['c'] : $options['component'], 'name' => isset($options['n']) ? $options['n'] : $options['name']); if (isset($options['p']) && $options['p'] != false) { $config['parent'] = $options['p']; } elseif (isset($options['parent']) && $options['parent'] != false) { $config['parent'] = $options['parent']; } if (isset($config['parent']) && Steelcode_String_Helper::isNull($config['parent'])) { unset($config['parent']); } $project = new Steelcode_Project_Handler($location, $config); $project->execute(); echo "{$project->getMessageText()}\n"; } catch (Steelcode_Project_Exception $ex) { echo "{$ex->getMessage()}\n"; }
/** * Check date of format yyyy-mm-dd is valid * * @param string $date * @return bool */ public static function isValidDate($date) { if (empty($date)) { return false; } $segments = Steelcode_String_Helper::explode('-', $date); if (count($segments) != 3) { return false; } return checkdate($segments[1], $segments[2], $segments[0]); }
/** * Sets the controller name * Finds the controller class name and file name * * @param string $controller */ public function setController($controller) { $class = ucfirst(Steelcode_String_Helper::dashedToCamel($controller)); $this->_controller['name'] = $controller; $this->_controller['class'] = "Controller_{$class}"; $this->_controller['file'] = "{$class}Controller.php"; }
/** * Extract controller and domain paths from request * * @param string $requestPath */ private function _extractPath($requestPath) { $lastChar = substr($requestPath, -1); if ($lastChar == '/') { $this->_config->setDomain(trim($requestPath, '/')); $this->_config->setController('index'); return; } $arrayReq = Steelcode_String_Helper::explode('/', $requestPath); if ($arrayReq[0] == '') { unset($arrayReq[0]); } $arrayLen = count($arrayReq); if ($arrayLen === 1) { $this->_config->setDomain('index'); $this->_config->setController($arrayReq[1]); } elseif ($arrayLen > 1) { $this->_config->setDomain($arrayReq[1]); $this->_config->setController($arrayReq[2]); if ($arrayLen > 2) { unset($arrayReq[1], $arrayReq[2]); $this->_setUriAttributes($arrayReq); } } else { $this->_config->defaults(); } }
/** * Flush the headers */ public function flushHeaders() { foreach ($this->_header as $key => $value) { if ($key === 'fields') { foreach ($this->_header['fields'] as $field => $value) { header(Steelcode_String_Helper::replace("_", "-", $field) . ": " . $value); } continue; } header($value); } }