/** * Delete all files (only) in a directory * @param $dir * @throws CException */ public static function removeFiles($dir) { if (!is_string($dir)) { $exception = new CException("Core_Directory:removeFiles error"); $exception->set("dir", $dir); //$exception->set("service", $_REQUEST["service"]); //$exception->set("request", $_REQUEST["request"]); throw $exception; } if (is_dir($dir)) { array_map('unlink', glob("{$dir}/*")); } else { FirePHP::fbLog("warn", "Core_Directory : {$dir} not found (removeFiles)"); } }
/** * Set php ini from configured option */ public static function applyIni() { $confIni = self::get("ini_set"); if (!empty($confIni)) { if (is_array($confIni)) { foreach ($confIni as $key => $val) { if ($key == "time_limit") { set_time_limit($val); } elseif ($key == "error_reporting") { error_reporting($val); } else { ini_set($key, $val); } } } else { FirePHP::fbLog("error", "INI_SET config must be an array of ini key"); } } }
/** * Fire an event and execute all associated listeners * @param string $event Event name * @return mixed|null */ public static function fire($event) { $return = null; $aEvt = Config::get("event"); if (isset($aEvt[$event]) && is_array($aEvt[$event])) { // get Event params $params = array(); $funcArg = func_get_args(); foreach ($funcArg as &$args) { $params[] = $args; } $params = array_slice($params, 1); foreach ($aEvt[$event] as $task) { $task = preg_replace("/:{1,2}/", "/", $task); $aTask = explode("/", trim($task)); if (count($aTask) == 2) { try { $return = Task::execTask($aTask[0], $aTask[1], $params); // Pour before render, passe le retour en param successif pour être pris en compte // par tous les listeners if ($event == "Page_BeforeRender") { if (count($params) == 2 && is_string($params[1]) && !empty($return)) { $params[1] = $return; } } } catch (CException $exeption) { FirePHP::fbLog("error", "Core_Event : " . $exeption->getMessage()); } } else { FirePHP::fbLog("error", "Core_Event : Service {$task} not found"); } } } else { FirePHP::fbLog("warn", "Core_Event : Event {$event} not register in configuration"); } return $return; }
public function __construct(array $REQUEST = array()) { /** Si appel par ligne de commande fill global $REQUEST ex : php index.php Train/search gare_deb_heure=0000 gare_date=15/07/2015 php index.php import/cron TODO url param !!! */ if (count($REQUEST) == 0 && isset($_SERVER["argv"]) && is_array($_SERVER["argv"])) { chdir(dirname($_SERVER["SCRIPT_FILENAME"])); $inp = $_SERVER["argv"][1]; $inp = preg_replace("!^/!", "", $inp); $_SERVER["PATH_INFO"] = "/" . $inp; /* $aInp = explode(":", $inp, 2); if(isset($aInp[0]) && trim($aInp[0]) !== "") { // replace / or \ par _ : Core/Server => Core_Server // eg : php index.php test/test:testCmd => test_test:testCmd // php index.php Core/Server:_server => Core_Server:_server $aInp[0] = preg_replace("/[\/\\\]/", "_", $aInp[0]); $REQUEST["service"] = $aInp[0]; } if(isset($aInp[1]) && trim($aInp[1]) !== "") { $REQUEST["request"] = trim($aInp[1]); } */ // Add other params to $REQUEST for ($ar = 2; $ar < count($_SERVER["argv"]); $ar++) { $param = $_SERVER["argv"][$ar]; $aP = explode("=", $param, 2); $REQUEST[$aP[0]] = $aP[1]; } } // Apply route mask $confRoute = Config::get("route"); if (!empty($confRoute) && isset($_SERVER["PATH_INFO"])) { if (is_array($confRoute)) { foreach ($confRoute as $mask => $route) { if (preg_match("!{$mask}!i", $_SERVER["PATH_INFO"])) { $_SERVER["PATH_INFO"] = preg_replace("!{$mask}!i", $route, $_SERVER["PATH_INFO"]); FirePHP::fbLog("info", "Routing to " . $_SERVER["PATH_INFO"]); break; } } } else { FirePHP::fbLog("error", "ROUTE config must be an array of mask/route"); } } /** Custom made Redirect Url eg: index.php/Test/ => Test:main index.php/Core/server/info/ => Core_Server:info index.php/Test/demo => Test:main(demo) index.php/Test/Main/demo => Test:main(demo) index.php/Test/Main/demo/id => Test:main(demo, id) */ if (isset($_SERVER["PATH_INFO"]) && !empty($_SERVER["PATH_INFO"])) { $url = $this->Parse(); $service = $url["service"]; $request = $url["request"]; // Si url compose de Service/Request/Param1/Param2/... if ($service !== null) { $REQUEST["service"] = $service; } if ($service !== null) { $REQUEST["request"] = $request; } } //Store standard params foreach ($REQUEST as $key => $value) { $key = str_replace(" ", "_", $key); $key = CString::stripAccent($key); // Strip tags on $REQUEST if magic quote active if (get_magic_quotes_gpc()) { $value = stripslashes($value); } $this->{$key} = $value; } }
/** * Send header correponding to the code * @param $statusCode */ public static function sendHeaderStatus($statusCode) { $status_codes = array(100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-Status', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 426 => 'Upgrade Required', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 509 => 'Bandwidth Limit Exceeded', 510 => 'Not Extended'); if ($status_codes[$statusCode] !== null) { $status_string = $statusCode . ' ' . $status_codes[$statusCode]; if (isset($_SERVER['SERVER_PROTOCOL'])) { header($_SERVER['SERVER_PROTOCOL'] . ' ' . $status_string, true, $statusCode); } } else { FirePHP::fbLog("warn", "Server : Status {$statusCode} not found"); } }
/** * Execute $r method of a class or instance $Obj * @param string|object $Obj Class name or an instance of the class * @param string $r Method name * @param array $params Method parameters * @return mixed * @throws CException * @throws null */ public static function execTask($Obj, $r, array $params = array()) { // Si nom de classe renseigné if (!is_object($Obj)) { $s = $Obj; try { // static class if (self::isStatic($s, $r)) { $Obj = $s; } else { $Obj = new $s(); } } catch (CException $exception) { $exception = new CException("service {$s}:{$r} not found"); $exception->set("service", $s); $exception->set("request", $r); throw $exception; } } else { // Si objet passé en paramètres $s = get_class($Obj); } if (method_exists($s, $r) && is_callable(array($s, $r))) { // TODO à corriger if (!is_array($params)) { $params = array($params); } // Limit parameter to method definition (pour securité ?) $Met = new \ReflectionMethod($s, $r); $Param = $Met->getParameters(); if (empty($Param)) { $Param = array(); } $params = array_slice($params, 0, count($Param)); $res = call_user_func_array(array($Obj, $r), $params); // Log les appels des services // TODO disable for prod $logParams = array(); foreach ($params as $p) { if (is_scalar($p)) { $l = strip_tags($p); if (strlen($l) > 20) { $l = substr($l, 0, 20) . "..."; } $logParams[] = $l; } elseif (is_array($p)) { $logParams[] = "Array"; } elseif (is_object($p)) { $logParams[] = "Object[" . get_class($p) . "]"; } } FirePHP::fbLog("info", "{$s}/{$r}(" . implode(", ", $logParams) . ")"); return $res; } else { $exception = null; if (!method_exists($s, $r)) { $exception = new CException("service {$s}:{$r} not found"); } elseif (!is_callable(array($s, $r))) { $exception = new CException("serviceNotCallable"); } $exception->set("service", $s); $exception->set("request", $r); throw $exception; } }