First commit
This commit is contained in:
1
lib/.htaccess
Normal file
1
lib/.htaccess
Normal file
@@ -0,0 +1 @@
|
||||
Deny from all
|
||||
217
lib/helper.php
Normal file
217
lib/helper.php
Normal file
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
namespace Helper;
|
||||
|
||||
function markdown($text)
|
||||
{
|
||||
require_once __DIR__.'/../vendor/Parsedown/Parsedown.php';
|
||||
return \Parsedown::instance()->parse($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 in_list($id, array $listing)
|
||||
{
|
||||
if (isset($listing[$id])) {
|
||||
return escape($listing[$id]);
|
||||
}
|
||||
|
||||
return '?';
|
||||
}
|
||||
|
||||
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, $class = '')
|
||||
{
|
||||
return '<label for="form-'.$name.'" class="'.$class.'">'.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>';
|
||||
$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).'/>';
|
||||
$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);
|
||||
}
|
||||
44
lib/request.php
Normal file
44
lib/request.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
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 '';
|
||||
}
|
||||
}
|
||||
135
lib/response.php
Normal file
135
lib/response.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
class Response
|
||||
{
|
||||
public function forceDownload($filename)
|
||||
{
|
||||
header('Content-Disposition: attachment; filename="'.$filename.'"');
|
||||
}
|
||||
|
||||
public function status($status_code)
|
||||
{
|
||||
if (strpos(php_sapi_name(), 'apache') !== false) {
|
||||
header('HTTP/1.0 '.$status_code);
|
||||
}
|
||||
else {
|
||||
header('Status: '.$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()
|
||||
{
|
||||
header('Strict-Transport-Security: max-age=31536000');
|
||||
}
|
||||
|
||||
public function xframe($mode = 'DENY', array $urls = array())
|
||||
{
|
||||
header('X-Frame-Options: '.$mode.' '.implode(' ', $urls));
|
||||
}
|
||||
}
|
||||
46
lib/router.php
Normal file
46
lib/router.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
class Router
|
||||
{
|
||||
private $controller = '';
|
||||
private $action = '';
|
||||
|
||||
public function __construct($controller = '', $action = '')
|
||||
{
|
||||
$this->controller = empty($_GET['controller']) ? $controller : $_GET['controller'];
|
||||
$this->action = empty($_GET['action']) ? $controller : $_GET['action'];
|
||||
}
|
||||
|
||||
public function sanitize($value, $default_value)
|
||||
{
|
||||
return ! ctype_alpha($value) || empty($value) ? $default_value : strtolower($value);
|
||||
}
|
||||
|
||||
public function loadController($filename, $class, $method)
|
||||
{
|
||||
if (file_exists($filename)) {
|
||||
|
||||
require $filename;
|
||||
|
||||
if (! method_exists($class, $method)) return false;
|
||||
|
||||
$instance = new $class;
|
||||
$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->loadController('controllers/'.$this->controller.'.php', '\Controller\\'.$this->controller, $this->action)) {
|
||||
die('Page not found!');
|
||||
}
|
||||
}
|
||||
}
|
||||
34
lib/session.php
Normal file
34
lib/session.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
class Session
|
||||
{
|
||||
const SESSION_LIFETIME = 2678400;
|
||||
|
||||
public function open($base_path = '/')
|
||||
{
|
||||
session_set_cookie_params(
|
||||
self::SESSION_LIFETIME,
|
||||
$base_path,
|
||||
null,
|
||||
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on',
|
||||
true
|
||||
);
|
||||
|
||||
session_start();
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
session_destroy();
|
||||
}
|
||||
|
||||
public function flash($message)
|
||||
{
|
||||
$_SESSION['flash_message'] = $message;
|
||||
}
|
||||
|
||||
public function flashError($message)
|
||||
{
|
||||
$_SESSION['flash_error_message'] = $message;
|
||||
}
|
||||
}
|
||||
38
lib/template.php
Normal file
38
lib/template.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
124
lib/translator.php
Normal file
124
lib/translator.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Translator {
|
||||
|
||||
const PATH = 'locales/';
|
||||
|
||||
function translate($identifier)
|
||||
{
|
||||
$args = \func_get_args();
|
||||
|
||||
\array_shift($args);
|
||||
\array_unshift($args, get($identifier, $identifier));
|
||||
|
||||
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)
|
||||
{
|
||||
return strftime(get($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');
|
||||
|
||||
$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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user