diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/.htaccess | 1 | ||||
-rw-r--r-- | core/event.php | 149 | ||||
-rw-r--r-- | core/helper.php | 262 | ||||
-rw-r--r-- | core/registry.php | 79 | ||||
-rw-r--r-- | core/request.php | 56 | ||||
-rw-r--r-- | core/response.php | 138 | ||||
-rw-r--r-- | core/router.php | 66 | ||||
-rw-r--r-- | core/session.php | 56 | ||||
-rw-r--r-- | core/template.php | 40 | ||||
-rw-r--r-- | core/translator.php | 130 |
10 files changed, 0 insertions, 977 deletions
diff --git a/core/.htaccess b/core/.htaccess deleted file mode 100644 index 14249c50..00000000 --- a/core/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all
\ No newline at end of file diff --git a/core/event.php b/core/event.php deleted file mode 100644 index 0941acac..00000000 --- a/core/event.php +++ /dev/null @@ -1,149 +0,0 @@ -<?php - -namespace Core; - -/** - * Event listener interface - * - * @package core - * @author Frederic Guillot - */ -interface Listener { - - /** - * @return boolean - */ - public function execute(array $data); -} - -/** - * Event dispatcher class - * - * @package core - * @author Frederic Guillot - */ -class Event -{ - /** - * Contains all listeners - * - * @access private - * @var array - */ - private $listeners = array(); - - /** - * The last listener executed - * - * @access private - * @var string - */ - private $lastListener = ''; - - /** - * The last triggered event - * - * @access private - * @var string - */ - private $lastEvent = ''; - - /** - * Triggered events list - * - * @access private - * @var array - */ - private $events = array(); - - /** - * Attach a listener object to an event - * - * @access public - * @param string $eventName Event name - * @param Listener $listener Object that implements the Listener interface - */ - public function attach($eventName, Listener $listener) - { - if (! isset($this->listeners[$eventName])) { - $this->listeners[$eventName] = array(); - } - - $this->listeners[$eventName][] = $listener; - } - - /** - * Trigger an event - * - * @access public - * @param string $eventName Event name - * @param array $data Event data - */ - public function trigger($eventName, array $data) - { - $this->lastEvent = $eventName; - $this->events[] = $eventName; - - if (isset($this->listeners[$eventName])) { - foreach ($this->listeners[$eventName] as $listener) { - if ($listener->execute($data)) { - $this->lastListener = get_class($listener); - } - } - } - } - - /** - * Get the last listener executed - * - * @access public - * @return string Event name - */ - public function getLastListenerExecuted() - { - return $this->lastListener; - } - - /** - * Get the last fired event - * - * @access public - * @return string Event name - */ - public function getLastTriggeredEvent() - { - return $this->lastEvent; - } - - /** - * Get a list of triggered events - * - * @access public - * @return array - */ - public function getTriggeredEvents() - { - return $this->events; - } - - /** - * Check if a listener bind to an event - * - * @access public - * @param string $eventName Event name - * @param mixed $instance Instance name or object itself - * @return bool Yes or no - */ - public function hasListener($eventName, $instance) - { - if (isset($this->listeners[$eventName])) { - foreach ($this->listeners[$eventName] as $listener) { - if ($listener instanceof $instance) { - return true; - } - } - } - - return false; - } -} diff --git a/core/helper.php b/core/helper.php deleted file mode 100644 index 8351328a..00000000 --- a/core/helper.php +++ /dev/null @@ -1,262 +0,0 @@ -<?php - -namespace Helper; - -function template($name, array $args = array()) -{ - $tpl = new \Core\Template; - return $tpl->load($name, $args); -} - -function is_current_user($user_id) -{ - return $_SESSION['user']['id'] == $user_id; -} - -function is_admin() -{ - return $_SESSION['user']['is_admin'] == 1; -} - -function get_username() -{ - return $_SESSION['user']['username']; -} - -function parse($text) -{ - $text = markdown($text); - $text = preg_replace('!#(\d+)!i', '<a href="?controller=task&action=show&task_id=$1">$0</a>', $text); - return $text; -} - -function markdown($text) -{ - require_once __DIR__.'/../vendor/Michelf/MarkdownExtra.inc.php'; - - $parser = new \Michelf\MarkdownExtra; - $parser->no_markup = true; - $parser->no_entities = true; - - return $parser->transform($text); -} - -function get_current_base_url() -{ - $url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; - $url .= $_SERVER['SERVER_NAME']; - $url .= $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443 ? '' : ':'.$_SERVER['SERVER_PORT']; - $url .= dirname($_SERVER['PHP_SELF']) !== '/' ? dirname($_SERVER['PHP_SELF']).'/' : '/'; - - return $url; -} - -function escape($value) -{ - return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false); -} - -function flash($html) -{ - $data = ''; - - if (isset($_SESSION['flash_message'])) { - $data = sprintf($html, escape($_SESSION['flash_message'])); - unset($_SESSION['flash_message']); - } - - return $data; -} - -function flash_error($html) -{ - $data = ''; - - if (isset($_SESSION['flash_error_message'])) { - $data = sprintf($html, escape($_SESSION['flash_error_message'])); - unset($_SESSION['flash_error_message']); - } - - return $data; -} - -function format_bytes($size, $precision = 2) -{ - $base = log($size) / log(1024); - $suffixes = array('', 'k', 'M', 'G', 'T'); - - return round(pow(1024, $base - floor($base)), $precision).$suffixes[floor($base)]; -} - -function get_host_from_url($url) -{ - return escape(parse_url($url, PHP_URL_HOST)) ?: $url; -} - -function summary($value, $min_length = 5, $max_length = 120, $end = '[...]') -{ - $length = strlen($value); - - if ($length > $max_length) { - return substr($value, 0, strpos($value, ' ', $max_length)).' '.$end; - } - else if ($length < $min_length) { - return ''; - } - - return $value; -} - -function contains($haystack, $needle) -{ - return strpos($haystack, $needle) !== false; -} - -function in_list($id, array $listing, $default_value = '?') -{ - if (isset($listing[$id])) { - return escape($listing[$id]); - } - - return $default_value; -} - -function error_class(array $errors, $name) -{ - return ! isset($errors[$name]) ? '' : ' form-error'; -} - -function error_list(array $errors, $name) -{ - $html = ''; - - if (isset($errors[$name])) { - - $html .= '<ul class="form-errors">'; - - foreach ($errors[$name] as $error) { - $html .= '<li>'.escape($error).'</li>'; - } - - $html .= '</ul>'; - } - - return $html; -} - -function form_value($values, $name) -{ - if (isset($values->$name)) { - return 'value="'.escape($values->$name).'"'; - } - - return isset($values[$name]) ? 'value="'.escape($values[$name]).'"' : ''; -} - -function form_hidden($name, $values = array()) -{ - return '<input type="hidden" name="'.$name.'" id="form-'.$name.'" '.form_value($values, $name).'/>'; -} - -function form_default_select($name, array $options, $values = array(), array $errors = array(), $class = '') -{ - $options = array('' => '?') + $options; - return form_select($name, $options, $values, $errors, $class); -} - -function form_select($name, array $options, $values = array(), array $errors = array(), $class = '') -{ - $html = '<select name="'.$name.'" id="form-'.$name.'" class="'.$class.'">'; - - foreach ($options as $id => $value) { - - $html .= '<option value="'.escape($id).'"'; - - if (isset($values->$name) && $id == $values->$name) $html .= ' selected="selected"'; - if (isset($values[$name]) && $id == $values[$name]) $html .= ' selected="selected"'; - - $html .= '>'.escape($value).'</option>'; - } - - $html .= '</select>'; - $html .= error_list($errors, $name); - - return $html; -} - -function form_radios($name, array $options, array $values = array()) -{ - $html = ''; - - foreach ($options as $value => $label) { - $html .= form_radio($name, $label, $value, isset($values[$name]) && $values[$name] == $value); - } - - return $html; -} - -function form_radio($name, $label, $value, $selected = false, $class = '') -{ - return '<label><input type="radio" name="'.$name.'" class="'.$class.'" value="'.escape($value).'" '.($selected ? 'selected="selected"' : '').'>'.escape($label).'</label>'; -} - -function form_checkbox($name, $label, $value, $checked = false, $class = '') -{ - return '<label><input type="checkbox" name="'.$name.'" class="'.$class.'" value="'.escape($value).'" '.($checked ? 'checked="checked"' : '').'> '.escape($label).'</label>'; -} - -function form_label($label, $name, array $attributes = array()) -{ - return '<label for="form-'.$name.'" '.implode(' ', $attributes).'>'.escape($label).'</label>'; -} - -function form_textarea($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - $class .= error_class($errors, $name); - - $html = '<textarea name="'.$name.'" id="form-'.$name.'" class="'.$class.'" '; - $html .= implode(' ', $attributes).'>'; - $html .= isset($values->$name) ? escape($values->$name) : isset($values[$name]) ? $values[$name] : ''; - $html .= '</textarea>'; - if (in_array('required', $attributes)) $html .= '<span class="form-required">*</span>'; - $html .= error_list($errors, $name); - - return $html; -} - -function form_input($type, $name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - $class .= error_class($errors, $name); - - $html = '<input type="'.$type.'" name="'.$name.'" id="form-'.$name.'" '.form_value($values, $name).' class="'.$class.'" '; - $html .= implode(' ', $attributes).'/>'; - if (in_array('required', $attributes)) $html .= '<span class="form-required">*</span>'; - $html .= error_list($errors, $name); - - return $html; -} - -function form_text($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('text', $name, $values, $errors, $attributes, $class); -} - -function form_password($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('password', $name, $values, $errors, $attributes, $class); -} - -function form_email($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('email', $name, $values, $errors, $attributes, $class); -} - -function form_date($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('date', $name, $values, $errors, $attributes, $class); -} - -function form_number($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('number', $name, $values, $errors, $attributes, $class); -} diff --git a/core/registry.php b/core/registry.php deleted file mode 100644 index f11d427c..00000000 --- a/core/registry.php +++ /dev/null @@ -1,79 +0,0 @@ -<?php - -namespace Core; - -/** - * The registry class is a dependency injection container - * - * @package core - * @author Frederic Guillot - */ -class Registry -{ - /** - * Contains all dependencies - * - * @access private - * @var array - */ - private $container = array(); - - /** - * Contains all instances - * - * @access private - * @var array - */ - private $instances = array(); - - /** - * Set a dependency - * - * @access public - * @param string $name Unique identifier for the service/parameter - * @param mixed $value The value of the parameter or a closure to define an object - */ - public function __set($name, $value) - { - $this->container[$name] = $value; - } - - /** - * Get a dependency - * - * @access public - * @param string $name Unique identifier for the service/parameter - * @return mixed The value of the parameter or an object - * @throws RuntimeException If the identifier is not found - */ - public function __get($name) - { - if (isset($this->container[$name])) { - - if (is_callable($this->container[$name])) { - return $this->container[$name](); - } - else { - return $this->container[$name]; - } - } - - throw new \RuntimeException('Identifier not found in the registry: '.$name); - } - - /** - * Return a shared instance of a dependency - * - * @access public - * @param string $name Unique identifier for the service/parameter - * @return mixed Same object instance of the dependency - */ - public function shared($name) - { - if (! isset($this->instances[$name])) { - $this->instances[$name] = $this->$name; - } - - return $this->instances[$name]; - } -} diff --git a/core/request.php b/core/request.php deleted file mode 100644 index df8ea41a..00000000 --- a/core/request.php +++ /dev/null @@ -1,56 +0,0 @@ -<?php - -namespace Core; - -class Request -{ - public function getStringParam($name, $default_value = '') - { - return isset($_GET[$name]) ? $_GET[$name] : $default_value; - } - - public function getIntegerParam($name, $default_value = 0) - { - return isset($_GET[$name]) && ctype_digit($_GET[$name]) ? (int) $_GET[$name] : $default_value; - } - - public function getValue($name) - { - $values = $this->getValues(); - return isset($values[$name]) ? $values[$name] : null; - } - - public function getValues() - { - if (! empty($_POST)) return $_POST; - - $result = json_decode($this->getBody(), true); - if ($result) return $result; - - return array(); - } - - public function getBody() - { - return file_get_contents('php://input'); - } - - public function getFileContent($name) - { - if (isset($_FILES[$name])) { - return file_get_contents($_FILES[$name]['tmp_name']); - } - - return ''; - } - - public function isPost() - { - return isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST'; - } - - public function isAjax() - { - return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'; - } -} diff --git a/core/response.php b/core/response.php deleted file mode 100644 index a5f0e4dc..00000000 --- a/core/response.php +++ /dev/null @@ -1,138 +0,0 @@ -<?php - -namespace Core; - -class Response -{ - public function forceDownload($filename) - { - header('Content-Disposition: attachment; filename="'.$filename.'"'); - } - - /** - * @param integer $status_code - */ - public function status($status_code) - { - header('Status: '.$status_code); - header($_SERVER['SERVER_PROTOCOL'].' '.$status_code); - } - - public function redirect($url) - { - header('Location: '.$url); - exit; - } - - public function json(array $data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: application/json'); - echo json_encode($data); - - exit; - } - - public function text($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: text/plain; charset=utf-8'); - echo $data; - - exit; - } - - public function html($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: text/html; charset=utf-8'); - echo $data; - - exit; - } - - public function xml($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: text/xml; charset=utf-8'); - echo $data; - - exit; - } - - public function js($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: text/javascript; charset=utf-8'); - echo $data; - - exit; - } - - public function binary($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Transfer-Encoding: binary'); - header('Content-Type: application/octet-stream'); - echo $data; - - exit; - } - - public function csp(array $policies = array()) - { - $policies['default-src'] = "'self'"; - $values = ''; - - foreach ($policies as $policy => $hosts) { - - if (is_array($hosts)) { - - $acl = ''; - - foreach ($hosts as &$host) { - - if ($host === '*' || $host === 'self' || strpos($host, 'http') === 0) { - $acl .= $host.' '; - } - } - } - else { - - $acl = $hosts; - } - - $values .= $policy.' '.trim($acl).'; '; - } - - header('Content-Security-Policy: '.$values); - } - - public function nosniff() - { - header('X-Content-Type-Options: nosniff'); - } - - public function xss() - { - header('X-XSS-Protection: 1; mode=block'); - } - - public function hsts() - { - if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { - header('Strict-Transport-Security: max-age=31536000'); - } - } - - public function xframe($mode = 'DENY', array $urls = array()) - { - header('X-Frame-Options: '.$mode.' '.implode(' ', $urls)); - } -} diff --git a/core/router.php b/core/router.php deleted file mode 100644 index 56a95e52..00000000 --- a/core/router.php +++ /dev/null @@ -1,66 +0,0 @@ -<?php - -namespace Core; - -require __DIR__.'/request.php'; -require __DIR__.'/response.php'; -require __DIR__.'/session.php'; -require __DIR__.'/template.php'; - -class Router -{ - private $controller = ''; - private $action = ''; - private $registry; - - public function __construct(Registry $registry, $controller = '', $action = '') - { - $this->registry = $registry; - $this->controller = empty($_GET['controller']) ? $controller : $_GET['controller']; - $this->action = empty($_GET['action']) ? $controller : $_GET['action']; - } - - /** - * @param string $default_value - */ - public function sanitize($value, $default_value) - { - return ! ctype_alpha($value) || empty($value) ? $default_value : strtolower($value); - } - - /** - * @param string $filename - * @param string $class - */ - public function load($filename, $class, $method) - { - if (file_exists($filename)) { - - require $filename; - - if (! method_exists($class, $method)) return false; - - $instance = new $class($this->registry); - $instance->request = new Request; - $instance->response = new Response; - $instance->session = new Session; - $instance->template = new Template; - $instance->beforeAction($this->controller, $this->action); - $instance->$method(); - - return true; - } - - return false; - } - - public function execute() - { - $this->controller = $this->sanitize($this->controller, 'app'); - $this->action = $this->sanitize($this->action, 'index'); - - if (! $this->load('controllers/'.$this->controller.'.php', '\Controller\\'.$this->controller, $this->action)) { - die('Page not found!'); - } - } -} diff --git a/core/session.php b/core/session.php deleted file mode 100644 index 0c3ec2d9..00000000 --- a/core/session.php +++ /dev/null @@ -1,56 +0,0 @@ -<?php - -namespace Core; - -class Session -{ - const SESSION_LIFETIME = 86400; // 1 day - - public function open($base_path = '/', $save_path = '') - { - if ($save_path !== '') session_save_path($save_path); - - // HttpOnly and secure flags for session cookie - session_set_cookie_params( - self::SESSION_LIFETIME, - $base_path ?: '/', - null, - isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on', - true - ); - - // Avoid session id in the URL - ini_set('session.use_only_cookies', '1'); - - // Ensure session ID integrity - ini_set('session.entropy_file', '/dev/urandom'); - ini_set('session.entropy_length', '32'); - ini_set('session.hash_bits_per_character', 6); - - // Custom session name - session_name('__S'); - - session_start(); - - // Regenerate the session id to avoid session fixation issue - if (empty($_SESSION['__validated'])) { - session_regenerate_id(true); - $_SESSION['__validated'] = 1; - } - } - - public function close() - { - session_destroy(); - } - - public function flash($message) - { - $_SESSION['flash_message'] = $message; - } - - public function flashError($message) - { - $_SESSION['flash_error_message'] = $message; - } -} diff --git a/core/template.php b/core/template.php deleted file mode 100644 index ad31ffb7..00000000 --- a/core/template.php +++ /dev/null @@ -1,40 +0,0 @@ -<?php - -namespace Core; - -class Template -{ - const PATH = 'templates/'; - - // Template\load('template_name', ['bla' => 'value']); - public function load() - { - if (func_num_args() < 1 || func_num_args() > 2) { - die('Invalid template arguments'); - } - - if (! file_exists(self::PATH.func_get_arg(0).'.php')) { - die('Unable to load the template: "'.func_get_arg(0).'"'); - } - - if (func_num_args() === 2) { - - if (! is_array(func_get_arg(1))) { - die('Template variables must be an array'); - } - - extract(func_get_arg(1)); - } - - ob_start(); - - include self::PATH.func_get_arg(0).'.php'; - - return ob_get_clean(); - } - - public function layout($template_name, array $template_args = array(), $layout_name = 'layout') - { - return $this->load($layout_name, $template_args + array('content_for_layout' => $this->load($template_name, $template_args))); - } -} diff --git a/core/translator.php b/core/translator.php deleted file mode 100644 index 8adb1bfd..00000000 --- a/core/translator.php +++ /dev/null @@ -1,130 +0,0 @@ -<?php - -namespace Translator { - - const PATH = 'locales/'; - - function translate($identifier) - { - $args = \func_get_args(); - - \array_shift($args); - \array_unshift($args, get($identifier, $identifier)); - - foreach ($args as &$arg) { - $arg = htmlspecialchars($arg, ENT_QUOTES, 'UTF-8', false); - } - - return \call_user_func_array( - 'sprintf', - $args - ); - } - - function number($number) - { - return number_format( - $number, - get('number.decimals', 2), - get('number.decimals_separator', '.'), - get('number.thousands_separator', ',') - ); - } - - function currency($amount) - { - $position = get('currency.position', 'before'); - $symbol = get('currency.symbol', '$'); - $str = ''; - - if ($position === 'before') { - $str .= $symbol; - } - - $str .= number($amount); - - if ($position === 'after') { - $str .= ' '.$symbol; - } - - return $str; - } - - function datetime($format, $timestamp) - { - if (! $timestamp) { - return ''; - } - - return strftime(get($format, $format), (int) $timestamp); - } - - function get($identifier, $default = '') - { - $locales = container(); - - if (isset($locales[$identifier])) { - return $locales[$identifier]; - } - else { - return $default; - } - } - - function load($language) - { - setlocale(LC_TIME, $language.'.UTF-8', $language); - - $path = PATH.$language; - $locales = array(); - - if (is_dir($path)) { - - $dir = new \DirectoryIterator($path); - - foreach ($dir as $fileinfo) { - - if (strpos($fileinfo->getFilename(), '.php') !== false) { - $locales = array_merge($locales, include $fileinfo->getPathname()); - } - } - } - - container($locales); - } - - function container($locales = null) - { - static $values = array(); - - if ($locales !== null) { - $values = $locales; - } - - return $values; - } -} - - -namespace { - - function t() { - return call_user_func_array('\Translator\translate', func_get_args()); - } - - function c() { - return call_user_func_array('\Translator\currency', func_get_args()); - } - - function n() { - return call_user_func_array('\Translator\number', func_get_args()); - } - - function dt() { - return call_user_func_array('\Translator\datetime', func_get_args()); - } - - function p($value, $t1, $t2) { - return $value > 1 ? $t2 : $t1; - } -} |