Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. Core::load('Data', 'WebKit.HTTP', 'IO.FS'); class WebKit implements Core_ModuleInterface { const MODULE = 'WebKit'; const VERSION = '1.4.0'; static public function Environment(array $values = array()) { return new WebKit_Environment($values); } static public function ServerAdapter() { return new WebKit_ServerAdapter(); } static public function Runner() { return new WebKit_Runner(); } static public function Upload($tmp_name, $original_name) { return new WebKit_Upload($tmp_name, $original_name); } } class WebKit_Exception extends Core_Exception {} class WebKit_HTTPException extends WebKit_Exception { protected $status; public function __construct($status) { $this->status = $status; parent::__construct($_SERVER['SERVER_PROTOCOL'].' '.$status); } } class WebKit_RedirectionException extends WebKit_HTTPException { protected $location; public function __construct($location, $status = WebKit_HTTP::FOUND) { $this->location = $location; parent::__construct($status); } } class WebKit_ClientErrorException extends WebKit_HTTPException { protected $uri; public function __construct($uri, $status = WebKit_HTTP::NOT_FOUND) { $this->uri = $uri; parent::__construct($status); } } class WebKit_ServerErrorException extends WebKit_HTTPException { protected $uri; public function __construct($uri, $status = WebKit_HTTP::INTERNAL_SERVER_ERROR) { $this->uri = $uri; parent::__construct($status); } } class WebKit_Environment extends Data_Tree {} class WebKit_ServerAdapter { public function current_request() { return (isset($_SERVER) && isset($_POST) && isset($_GET)) ? WebKit_HTTP::Request()-> host($_SERVER['HTTP_HOST'])-> uri($_SERVER['REQUEST_URI'])-> method($_POST['_method'] ? $_POST['_method'] : $_SERVER['REQUEST_METHOD'])-> with_parameters(Data_Hash::recursive($_GET))-> with_parameters(Data_Hash::recursive($_POST))-> with_parameters($this->current_uploads())-> with_headers($this->load_request_headers()) : null; } protected function transform_upload_data(&$result, &$source, $path = '', $field = '', $level = 1) { foreach ($source as $k => &$v) { if (!preg_match('{^(?:[a-zA-Z_][a-zA-Z0-9_]+)|([0-9]+)$}', $k)) continue; if (is_array($v)) if ($level == 2) $this->transform_upload_data($result, $v, $path, $k, $level + 1); else $this->transform_upload_data($result, $v, "{$path}[$k]", $field, $level + 1); else $result["{$path}[$k]"][$field] = $v; } } protected function load_request_headers() { return apache_request_headers(); } protected function current_uploads() { $result = array(); $this->transform_upload_data($result, $_FILES); $files = array(); foreach ($result as $k => &$v) if ($v['error'] === UPLOAD_ERR_OK) eval('$files'.$k.'= WebKit::Upload($v["tmp_name"], $v["name"]);'); return Data_Hash::recursive($files); } } class WebKit_Runner { public function run(WebKit_AbstractHandler $app) { $environment = WebKit::Environment(); $environment->request = WebKit::ServerAdapter()->current_request(); $response = WebKit_HTTP::Response(); $body = $app->process($environment, $response); $this->flush_headers($response); if (Core_Types::is_array($body) || $body instanceof Iterator || $body instanceof IteratorAggregate) foreach ($body as $line) print($line); } protected function flush_headers(WebKit_HTTP_Response $response) { header($_SERVER['SERVER_PROTOCOL'].' '.$response->status); foreach ($response as $k => $v) header("$k: $v"); } } abstract class WebKit_AbstractHandler { abstract public function process(WebKit_Environment $env, WebKit_HTTP_Response $response); protected function page_not_found($url) { throw new WebKit_ClientErrorException($url); } protected function redirect_to($location) { throw new WebKit_RedirectionException($location); } } abstract class WebKit_MiddlewareHandler extends WebKit_AbstractHandler { protected $application; public function __construct(WebKit_AbstractHandler $application) { $this->application = $application; } } class WebKit_Upload extends IO_FS_File implements Core_PropertyAccessInterface { protected $original_name; public function __construct($tmp_path, $original_name) { $this->original_name = IO_FS::Path($original_name)->basename; parent::__construct($tmp_path); } public function __get($property) { switch ($property) { case 'original_name': return $this->original_name; default: return parent::__get($property); } } public function __set($property, $value) { switch ($property) { case 'original_name': throw new Core_ReadOnlyPropertyException($property); default: return parent::__set($property, $value); } } public function __isset($property) { switch ($property) { case 'original_name': return true; default: return parent::__isset($property); } } public function __unset($property) { throw $this->__isset($property) ? new Core_UndestroyablePropertyException($property) : parent::__unset($property); } protected function get_mime_type() { return $this->mime_type ? $this->mime_type : $this->mime_type = MIME::type_for_file($this->original_name); } } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement