From 92686dd8c17c6a6d46975f6ff47e7ca615b584b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Plusquellec?= Date: Wed, 22 Mar 2017 19:16:40 +0100 Subject: [PATCH] request --- lib/ghost.php | 2 + lib/lib.php | 70 +++++++++++--- lib/nativemail.php | 2 +- lib/octaliamail.php | 4 +- lib/request.php | 223 ++++++++++++++++++++++++++++++++++++++++++++ lib/response.php | 42 +++++++++ lib/routes.php | 77 ++++++++++++++- lib/smtp.php | 1 - lib/user.php | 8 +- 9 files changed, 407 insertions(+), 22 deletions(-) create mode 100644 lib/request.php diff --git a/lib/ghost.php b/lib/ghost.php index 4d94fb3..addce91 100644 --- a/lib/ghost.php +++ b/lib/ghost.php @@ -3,6 +3,8 @@ class Ghost implements \ArrayAccess { + use Notifiable; + private $_instance; public function __construct(array $data = [], $instance = null) diff --git a/lib/lib.php b/lib/lib.php index 5b53ae6..a14a1c1 100755 --- a/lib/lib.php +++ b/lib/lib.php @@ -55,19 +55,23 @@ function view($html = null, $code = 200, $title = 'Octo') { + static $viewClass = null; + if (empty($html)) { - $class = o(); + if (is_null($viewClass)) { + $viewClass = o(); - $class->macro('assign', function ($k, $v) { - $vars = Registry::get('views.vars', []); - $vars[$k] = value($v); + $viewClass->macro('assign', function ($k, $v) { + $vars = Registry::get('views.vars', []); + $vars[$k] = value($v); - Registry::set('views.vars', $vars); + Registry::set('views.vars', $vars); - return view(); - }); + return view(); + }); + } - return $class; + return $viewClass; } $tpl = '' . $title . '
##html##
'; @@ -97,6 +101,12 @@ function render($file, $context = 'controller', $args = [], $code = 200) $file = path('app') . DS . 'views' . DS . $c . DS . $a . '.phtml'; } + if (fnmatch('*.*', $file)) { + list($c, $a) = explode('.', $file, 2); + + $file = path('app') . DS . 'views' . DS . $c . DS . $a . '.phtml'; + } + if (file_exists($file)) { $content = File::read($file); @@ -128,7 +138,7 @@ function render($file, $context = 'controller', $args = [], $code = 200) function controller() { - return Registry::get('app.controller'); + return Registry::get('app.controller', null); } function is_home() @@ -1325,7 +1335,6 @@ public function _hooks() function lib($lib, $args = []) { - $class = '\\Octo\\' . Strings::camelize($lib); if (!class_exists($class)) { @@ -1521,7 +1530,7 @@ function systemBoot($dir = null) Registry::set('octo.subdir', $subdir); if (!defined('OCTO_STANDALONE')) { - defined('WEBROOT') || define('WEBROOT', Registry::get('octo.subdir', '/')); + defined('WEBROOT') || define('WEBROOT', Registry::get('octo.subdir', '/')); date_default_timezone_set(Config::get('timezone', 'Europe/Paris')); @@ -2033,7 +2042,7 @@ function middlewares($when = 'before') foreach ($middlewares as $middlewareClass) { $middleware = app($middlewareClass); - $methods = get_class_methods($middlewareClass); + $methods = $method = lcfirst(Strings::camelize('apply_' . $when)); if (in_array($method, $methods)) { @@ -4836,3 +4845,40 @@ function stream($name, $contents = 'octodummy') return $stream; } + + function undot($collection) + { + $collection = (array) $collection; + $output = []; + + foreach ($collection as $key => $value) { + aset($output, $key, $value); + + if (is_array($value) && !strpos($key, '.')) { + $nested = undot($value); + + $output[$key] = $nested; + } + } + + return $output; + } + + function notify($model, $instance, array $args = []) + { + if ($model->exists()) { + $channels = $instance->channels(); + + foreach ($channels as $channel) { + call_user_func_array([$instance, $channel], $args); + + $dbRow = em('systemNotification')->store([ + 'model' => get_class($model), + 'model_id' => $model->id, + 'type' => get_class($instance), + 'channel' => $channel, + 'read' => false + ]); + } + } + } diff --git a/lib/nativemail.php b/lib/nativemail.php index 9979288..85bd964 100644 --- a/lib/nativemail.php +++ b/lib/nativemail.php @@ -28,7 +28,7 @@ public function send() $res = call_user_func_array('mail', $args); if ($res === false) { - throw new Exception("Unable to send email."); + exception("Mail", "Unable to send email."); } } } diff --git a/lib/octaliamail.php b/lib/octaliamail.php index f90424f..79e3b80 100644 --- a/lib/octaliamail.php +++ b/lib/octaliamail.php @@ -20,14 +20,14 @@ public function send() $fromName = str_replace(" <$from>", '', $this->message->getEncodedHeader('From')); - $mail = System::Mail()->create([ + $mail = em('systemMail')->store([ 'to' => $to, 'from' => $from, 'from_name' => $fromName, 'subject' => $subject, 'html' => $html, 'text' => $text - ])->save(); + ]); if ($mail->id > 0) { return true; diff --git a/lib/request.php b/lib/request.php new file mode 100644 index 0000000..fb97d3b --- /dev/null +++ b/lib/request.php @@ -0,0 +1,223 @@ + $value) { + if ((substr($name, 0, 5) == 'HTTP_') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) { + $headers[str_replace(array(' ', 'Http'), array('-', 'HTTP'), ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; + } + } + + return $headers; + } + + public static function method() + { + $method = $_SERVER['REQUEST_METHOD']; + + if ($_SERVER['REQUEST_METHOD'] == 'HEAD') { + ob_start(); + $method = 'GET'; + } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { + $headers = self::headers(); + + if (isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], ['PUT', 'DELETE', 'PATCH'])) { + $method = isAke($headers, 'X-HTTP-Method-Override', 'PUT'); + } + + if (isset($headers['_method']) && in_array($headers['_method'], ['PUT', 'DELETE', 'PATCH'])) { + $method = isAke($headers, '_method', 'PUT'); + } + } + + return $method; + } + + public static function url($baseRoute = '') + { + $protocol = 'http'; + + if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { + $protocol .= 's'; + } + + if ($_SERVER["SERVER_PORT"] != "80") { + return "$protocol://" . + $_SERVER["SERVER_NAME"] . + ':' . + $_SERVER["SERVER_PORT"] . + self::uri($baseRoute); + } else { + return "$protocol://" . + $_SERVER["SERVER_NAME"] . + self::uri($baseRoute); + } + } + + public static function uri($baseRoute = '') + { + $uri = substr($_SERVER['REQUEST_URI'], strlen($baseRoute)); + + if (strstr($uri, '?')) { + $parts = preg_split('/\?/', $uri, -1, PREG_SPLIT_NO_EMPTY); + $uri = array_shift($parts); + $qs = array_shift($parts); + + parse_str($qs, $output); + + foreach ($output as $k => $v) { + $_REQUEST[$k] = $v; + } + } + + $uri = trim($uri, '/'); + + return !strlen($uri) ? '/' : $uri; + } + + public static function decodedUri() + { + return rawurldecode(self::uri()); + } + + public static function ip() + { + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { + return $_SERVER['HTTP_CLIENT_IP']; + } elseif (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) { + return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP']; + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + return $_SERVER['HTTP_X_FORWARDED_FOR']; + } elseif (!empty($_SERVER['X_FORWARDED_FOR'])) { + return $_SERVER['X_FORWARDED_FOR']; + } else { + return $_SERVER['REMOTE_ADDR']; + } + } + + public static function language() + { + return \Locale::acceptFromHttp( + isAke( + $_SERVER, + "HTTP_ACCEPT_LANGUAGE", + Config::get( + 'app.language', + def( + 'app.language', + 'en' + ) + ) + ) + ); + } + } diff --git a/lib/response.php b/lib/response.php index 0575568..39e6053 100644 --- a/lib/response.php +++ b/lib/response.php @@ -206,4 +206,46 @@ public function getBody() { return $this->body; } + + public static function tpl($tpl, $args, $code = 200) + { + $file = path('app') . DS . 'views' . DS . str_replace('.', DS, Strings::lower($tpl)) . '.phtml'; + + if (File::exists($file)) { + $content = File::read($file); + + if (is_array($args)) { + extract($args); + } + + ob_start(); + + eval(' namespace Octo; ?>' . $content . 'db(); + $table = $model->table(); + + $prefix = ''; + + if ('core' != $db) { + $prefix = trim(trim($orefix, '/') . '/' . $db, '/'); + } + + $controller = empty($controller) ? $table : $controller; + + $prefix = trim(trim($orefix, '/') . '/' . $controller, '/'); + + /* + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | Create + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + self::get($prefix . '/add', $controller . '#add'); + self::post($prefix . '/store', $controller . '#store'); + + /* + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | Read + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + self::get($prefix . '/([0-9]+)', function ($id) use ($controller) { + $_REQUEST['id'] = $id; + + return [$controller, 'find']; + }); + + /* + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | Update + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + self::get($prefix . '/edit/([0-9]+)', function ($id) use ($controller) { + $_REQUEST['id'] = $id; + + return [$controller, 'update']; + }); + + self::post($prefix . '/update/([0-9]+)', function ($id) use ($controller) { + $_REQUEST['id'] = $id; + + return [$controller, 'update']; + }); + + /* + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | Delete + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + self::getPost($prefix . '/delete/([0-9]+)', function ($id) use ($controller) { + $_REQUEST['id'] = $id; + + return [$controller, 'delete']; + }); + + /* + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | List + |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + self::getPost($prefix, $controller . '#index'); + self::getPost($prefix . '/list', $controller . '#index'); + } + } + public static function __callStatic($m, $a) { $uri = array_shift($a); @@ -222,8 +295,8 @@ public static function __callStatic($m, $a) } return $route - ->setController($controller) - ->setAction($action); + ->setController($controller) + ->setAction($action); }); if (!empty($a)) { diff --git a/lib/smtp.php b/lib/smtp.php index 66b88da..ff7db03 100644 --- a/lib/smtp.php +++ b/lib/smtp.php @@ -14,7 +14,6 @@ class Smtp private $persistent; private $message; - public function __construct(Courrier $message) { $this->message = $message; diff --git a/lib/user.php b/lib/user.php index 04c9034..1380652 100644 --- a/lib/user.php +++ b/lib/user.php @@ -160,7 +160,7 @@ public function log($action, $data = []) $row[$k] = $v; } - System::Track()->create($row)->save(); + em('systemTrack')->store($row); } } @@ -284,9 +284,9 @@ public function userModel() } if ($user['accounted']) { - return System::Account()->find((int) $user['id']); + return em('systemAccount')->find((int) $user['id']); } else { - return System::Visitor()->find((int) $user['id']); + return em('systemVisitor')->find((int) $user['id']); } } @@ -307,7 +307,7 @@ public function ip() } if ($ip == '127.0.0.1') { - return ['ip' > $ip, 'language' => $lng]; + return ['ip' => $ip, 'language' => $lng]; } $url = "http://ip-api.com/json/$ip";