1
0
mirror of https://github.com/moparisthebest/wallabag synced 2024-11-27 03:12:21 -05:00

remove PicoFarad

I’ll implement it an other day.
This commit is contained in:
Nicolas Lœuillet 2014-07-12 16:39:31 +02:00
parent b3cda72e93
commit 26b77483ee
8 changed files with 9 additions and 553 deletions

View File

@ -1,78 +0,0 @@
<?php
namespace PicoFarad\Request;
function param($name, $default_value = null)
{
return isset($_GET[$name]) ? $_GET[$name] : $default_value;
}
function int_param($name, $default_value = 0)
{
return isset($_GET[$name]) && ctype_digit($_GET[$name]) ? (int) $_GET[$name] : $default_value;
}
function value($name)
{
$values = values();
return isset($values[$name]) ? $values[$name] : null;
}
function values()
{
if (! empty($_POST)) {
return $_POST;
}
$result = json_decode(body(), true);
if ($result) {
return $result;
}
return array();
}
function body()
{
return file_get_contents('php://input');
}
function file_content($field)
{
if (isset($_FILES[$field])) {
return file_get_contents($_FILES[$field]['tmp_name']);
}
return '';
}
function file_info($field)
{
if (isset($_FILES[$field])) {
return array(
'name' => $_FILES[$field]['name'],
'mimetype' => $_FILES[$field]['type'],
'size' => $_FILES[$field]['size'],
);
}
return false;
}
function file_move($field, $destination)
{
if (isset($_FILES[$field]) && ! file_exists($destination)) {
@mkdir(dirname($destination), 0777, true);
move_uploaded_file($_FILES[$field]['tmp_name'], $destination);
}
}

View File

@ -1,156 +0,0 @@
<?php
namespace PicoFarad\Response;
function force_download($filename)
{
header('Content-Disposition: attachment; filename="'.$filename.'"');
}
function content_type($mimetype)
{
header('Content-Type: '.$mimetype);
}
function status($status_code)
{
$sapi_name = php_sapi_name();
if (strpos($sapi_name, 'apache') !== false || $sapi_name === 'cli-server') {
header('HTTP/1.0 '.$status_code);
}
else {
header('Status: '.$status_code);
}
}
function redirect($url)
{
header('Location: '.$url);
exit;
}
function json(array $data, $status_code = 200)
{
status($status_code);
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
function text($data, $status_code = 200)
{
status($status_code);
header('Content-Type: text/plain; charset=utf-8');
echo $data;
exit;
}
function html($data, $status_code = 200)
{
status($status_code);
header('Content-Type: text/html; charset=utf-8');
echo $data;
exit;
}
function xml($data, $status_code = 200)
{
status($status_code);
header('Content-Type: text/xml; charset=utf-8');
echo $data;
exit;
}
function js($data, $status_code = 200)
{
status($status_code);
header('Content-Type: text/javascript; charset=utf-8');
echo $data;
exit;
}
function binary($data, $status_code = 200)
{
status($status_code);
header('Content-Transfer-Encoding: binary');
header('Content-Type: application/octet-stream');
echo $data;
exit;
}
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);
}
function nosniff()
{
header('X-Content-Type-Options: nosniff');
}
function xss()
{
header('X-XSS-Protection: 1; mode=block');
}
function hsts()
{
header('Strict-Transport-Security: max-age=31536000');
}
function xframe($mode = 'DENY', array $urls = array())
{
header('X-Frame-Options: '.$mode.' '.implode(' ', $urls));
}

View File

@ -1,157 +0,0 @@
<?php
namespace PicoFarad\Router;
// Load controllers: bootstrap('controllers', 'controller1', 'controller2')
function bootstrap()
{
$files = \func_get_args();
$base_path = array_shift($files);
foreach ($files as $file) {
require $base_path.'/'.$file.'.php';
}
}
// Execute a callback before each action
function before($value = null)
{
static $before_callback = null;
if (is_callable($value)) {
$before_callback = $value;
}
else if (is_callable($before_callback)) {
$before_callback($value);
}
}
// Execute a callback before a specific action
function before_action($name, $value = null)
{
static $callbacks = array();
if (is_callable($value)) {
$callbacks[$name] = $value;
}
else if (isset($callbacks[$name]) && is_callable($callbacks[$name])) {
$callbacks[$name]($value);
}
}
// Execute an action
function action($name, \Closure $callback)
{
$handler = isset($_GET['action']) ? $_GET['action'] : 'default';
if ($handler === $name) {
before($name);
before_action($name);
$callback();
}
}
// Execute an action only for POST requests
function post_action($name, \Closure $callback)
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
action($name, $callback);
}
}
// Execute an action only for GET requests
function get_action($name, \Closure $callback)
{
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
action($name, $callback);
}
}
// Run when no action have been executed before
function notfound(\Closure $callback)
{
before('notfound');
before_action('notfound');
$callback();
}
// Match a request like this one: GET /myhandler
function get($url, \Closure $callback)
{
find_route('GET', $url, $callback);
}
// Match a request like this one: POST /myhandler
function post($url, \Closure $callback)
{
find_route('POST', $url, $callback);
}
// Match a request like this one: PUT /myhandler
function put($url, \Closure $callback)
{
find_route('PUT', $url, $callback);
}
// Match a request like this one: DELETE /myhandler
function delete($url, \Closure $callback)
{
find_route('DELETE', $url, $callback);
}
// Define which callback to execute according to the URL and the HTTP verb
function find_route($method, $route, \Closure $callback)
{
if ($_SERVER['REQUEST_METHOD'] === $method) {
if (! empty($_SERVER['QUERY_STRING'])) {
$url = substr($_SERVER['REQUEST_URI'], 0, -(strlen($_SERVER['QUERY_STRING']) + 1));
}
else {
$url = $_SERVER['REQUEST_URI'];
}
$params = array();
if (url_match($route, $url, $params)) {
before($route);
\call_user_func_array($callback, $params);
exit;
}
}
}
// Parse url and find matches
function url_match($route_uri, $request_uri, array &$params)
{
if ($request_uri === $route_uri) return true;
if ($route_uri === '/' || $request_uri === '/') return false;
$route_uri = trim($route_uri, '/');
$request_uri = trim($request_uri, '/');
$route_items = explode('/', $route_uri);
$request_items = explode('/', $request_uri);
$nb_route_items = count($route_items);
if ($nb_route_items === count($request_items)) {
for ($i = 0; $i < $nb_route_items; ++$i) {
if ($route_items[$i][0] === ':') {
$params[substr($route_items[$i], 1)] = $request_items[$i];
}
else if ($route_items[$i] !== $request_items[$i]) {
$params = array();
return false;
}
}
return true;
}
return false;
}

View File

@ -1,57 +0,0 @@
<?php
namespace PicoFarad\Session;
const SESSION_LIFETIME = 2678400;
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(
SESSION_LIFETIME,
$base_path ?: '/',
null,
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on',
true
);
// Avoid session id in the URL
ini_set('session.use_only_cookies', true);
// 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('__$');
session_start();
// Regenerate the session id to avoid session fixation issue
if (empty($_SESSION['__validated'])) {
session_regenerate_id(true);
$_SESSION['__validated'] = 1;
}
}
function close()
{
session_destroy();
}
function flash($message)
{
$_SESSION['flash_message'] = $message;
}
function flash_error($message)
{
$_SESSION['flash_error_message'] = $message;
}

View File

@ -1,36 +0,0 @@
<?php
namespace PicoFarad\Template;
const PATH = 'templates/';
// Template\load('template_name', ['bla' => 'value']);
function load()
{
if (func_num_args() < 1 || func_num_args() > 2) {
die('Invalid template arguments');
}
if (! file_exists(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 PATH.func_get_arg(0).'.php';
return ob_get_clean();
}
function layout($template_name, array $template_args = array(), $layout_name = 'layout')
{
return load($layout_name, $template_args + array('content_for_layout' => load($template_name, $template_args)));
}

View File

@ -11,8 +11,8 @@
class Routing
{
protected $wallabag;
public $referer;
public $view;
protected $referer;
protected $view;
protected $action;
protected $id;
protected $url;
@ -55,7 +55,7 @@ class Routing
# because messages can be added in $poche->action(), we have to add this entry now (we can add it before)
$this->vars = array_merge($this->vars, array('messages' => $this->wallabag->messages->display('all', FALSE)));
$this->render($this->file, $this->vars);
$this->_render($this->file, $this->vars);
}
private function _defineTplInformation()
@ -142,7 +142,7 @@ class Routing
}
}
public function render($file, $vars)
public function _render($file, $vars)
{
echo $this->wallabag->tpl->render($file, $vars);
}

View File

@ -40,12 +40,6 @@ require_once INCLUDES . '/3rdparty/libraries/PHPePub/Logger.php';
require_once INCLUDES . '/3rdparty/libraries/PHPePub/EPub.php';
require_once INCLUDES . '/3rdparty/libraries/PHPePub/EPubChapterSplitter.php';
require_once INCLUDES . '/3rdparty/PicoFarad/Request.php';
require_once INCLUDES . '/3rdparty/PicoFarad/Response.php';
require_once INCLUDES . '/3rdparty/PicoFarad/Router.php';
require_once INCLUDES . '/3rdparty/PicoFarad/Session.php';
require_once INCLUDES . '/3rdparty/PicoFarad/Template.php';
# system configuration; database credentials et caetera
require_once INCLUDES . '/poche/config.inc.php';
require_once INCLUDES . '/poche/config.inc.default.php';
@ -61,10 +55,3 @@ if (!ini_get('date.timezone') || !@date_default_timezone_set(ini_get('date.timez
if (defined('ERROR_REPORTING')) {
error_reporting(ERROR_REPORTING);
}
// Start session
Session::$sessionName = 'wallabag';
Session::init();
// Let's rock !
$wallabag = new Poche();

View File

@ -12,56 +12,9 @@ define ('POCHE', '1.8.0');
require 'check_setup.php';
require_once 'inc/poche/global.inc.php';
// Start session
Session::$sessionName = 'wallabag';
Session::init();
use PicoFarad\Router;
use PicoFarad\Response;
use PicoFarad\Request;
use PicoFarad\Session;
// Called before each action
Router\before(function($action) {
// Open a session only for the specified directory
Session\open(dirname($_SERVER['PHP_SELF']));
// HTTP secure headers
Response\csp();
Response\xframe();
Response\xss();
Response\nosniff();
});
// Show help
Router\get_action('unread', function() use ($wallabag) {
$view = 'home';
$id = 0;
$tpl_vars = array(
'referer' => $wallabag->routing->referer,
'view' => $wallabag->routing->view,
'poche_url' => Tools::getPocheUrl(),
'title' => _('wallabag, a read it later open source system'),
'token' => \Session::getToken(),
'theme' => $wallabag->tpl->getTheme(),
'entries' => '',
'page_links' => '',
'nb_results' => '',
'listmode' => (isset($_COOKIE['listmode']) ? true : false),
);
$count = $wallabag->store->getEntriesByViewCount($view, $wallabag->user->getId(), $id);
if ($count > 0) {
$wallabag->pagination->set_total($count);
$page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')),
$wallabag->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . (($id)?'&id='.$id:'') . '&' ));
$tpl_vars['entries'] = $wallabag->store->getEntriesByView($view, $wallabag->user->getId(), $wallabag->pagination->get_limit(), $id);
$tpl_vars['page_links'] = $page_links;
$tpl_vars['nb_results'] = $count;
}
$wallabag->routing->render('home.twig', $tpl_vars);
Tools::logm('display ' . $view . ' view');
});
// Let's rock !
$wallabag = new Poche();