Skip to content

Commit

Permalink
request
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Mar 22, 2017
1 parent 07c710c commit 92686dd
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/ghost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

class Ghost implements \ArrayAccess
{
use Notifiable;

private $_instance;

public function __construct(array $data = [], $instance = null)
Expand Down
70 changes: 58 additions & 12 deletions lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>' . $title . '</title><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Oswald:100,300,400,700,900"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css"><style>body{font-family:"Oswald";}</style></head><body id="app-layout"><div class="container-fluid">##html##</div><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script></body></html>';
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1325,7 +1335,6 @@ public function _hooks()

function lib($lib, $args = [])
{

$class = '\\Octo\\' . Strings::camelize($lib);

if (!class_exists($class)) {
Expand Down Expand Up @@ -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'));

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
]);
}
}
}
2 changes: 1 addition & 1 deletion lib/nativemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}
4 changes: 2 additions & 2 deletions lib/octaliamail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
223 changes: 223 additions & 0 deletions lib/request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php
namespace Octo;

class Request
{
use Macroable;

public static function post($key = null, $default = null)
{
if (!$key) {
return classify('postRequest', $_POST);
}

return isAke($_POST, $key, $default);
}

public static function get($key = null, $default = null)
{
if (!$key) {
return classify('getRequest', $_GET);
}

return isAke($_GET, $key, $default);
}

public static function files($key = null, $default = null)
{
if (!$key) {
return classify('filesRequest', $_FILES);
}

return isAke($_FILES, $key, $default);
}

public static function cookies($key = null, $default = null)
{
if (!$key) {
return classify('cookiesRequest', $_COOKIE);
}

return isAke($_COOKIE, $key, $default);
}

public static function session($key = null, $default = null)
{
if (!$key) {
return classify('sessionRequest', $_SESSION);
}

return isAke($_SESSION, $key, $default);
}

public static function server($key = null, $default = null)
{
if (!$key) {
return classify('serverRequest', $_SERVER);
}

return isAke($_SERVER, $key, $default);
}

public static function has($key)
{
return 'octodummy' != isAke($_REQUEST, $key, 'octodummy');
}

public static function exists($key)
{
return 'octodummy' != isAke($_REQUEST, $key, 'octodummy');
}

public static function only($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();

$results = [];

foreach ($keys as $key) {
aset($results, $key, isAke($_REQUEST, $key, null));
}

return $results;
}

public static function except($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();

$results = $_REQUEST;

adel($results, $keys);

return $results;
}

public static function all()
{
return $_REQUEST + $_COOKIE;
}

public static function hasCookie($key)
{
return !is_null(static::cookies($key));
}

public static function headers()
{
if (function_exists('getallheaders')) {
return getallheaders();
}

$headers = [];

foreach ($_SERVER as $name => $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'
)
)
)
);
}
}
Loading

0 comments on commit 92686dd

Please sign in to comment.