Initial commit
This commit is contained in:
124
pluf/src/Pluf/Middleware/Csrf.php
Normal file
124
pluf/src/Pluf/Middleware/Csrf.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Cross Site Request Forgery Middleware.
|
||||
*
|
||||
* This class provides a middleware that implements protection against
|
||||
* request forgeries from other sites. This middleware must be before
|
||||
* the Pluf_Middleware_Session middleware.
|
||||
*
|
||||
* Based on concepts from the Django CSRF middleware.
|
||||
*/
|
||||
class Pluf_Middleware_Csrf
|
||||
{
|
||||
public static function makeToken($session_key)
|
||||
{
|
||||
return md5(Pluf::f('secret_key').$session_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the request.
|
||||
*
|
||||
* When processing the request, if a POST request with a session,
|
||||
* we will check that the token is available and valid.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @return bool false
|
||||
*/
|
||||
function process_request(&$request)
|
||||
{
|
||||
if ($request->method != 'POST') {
|
||||
return false;
|
||||
}
|
||||
$cookie_name = Pluf::f('session_cookie_id', 'sessionid');
|
||||
if (!isset($request->COOKIE[$cookie_name])) {
|
||||
// no session, nothing to do
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$data = Pluf_Middleware_Session::_decodeData($request->COOKIE[$cookie_name]);
|
||||
} catch (Exception $e) {
|
||||
// no valid session
|
||||
return false;
|
||||
}
|
||||
if (!isset($data['Pluf_Session_key'])) {
|
||||
// no session key
|
||||
return false;
|
||||
}
|
||||
$token = self::makeToken($data['Pluf_Session_key']);
|
||||
if (!isset($request->POST['csrfmiddlewaretoken'])) {
|
||||
return new Pluf_HTTP_Response_Forbidden($request);
|
||||
}
|
||||
if ($request->POST['csrfmiddlewaretoken'] != $token) {
|
||||
return new Pluf_HTTP_Response_Forbidden($request);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the response of a view.
|
||||
*
|
||||
* If we find a POST form, add the token to it.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @param Pluf_HTTP_Response The response
|
||||
* @return Pluf_HTTP_Response The response
|
||||
*/
|
||||
function process_response($request, $response)
|
||||
{
|
||||
$cookie_name = Pluf::f('session_cookie_id', 'sessionid');
|
||||
if (!isset($request->COOKIE[$cookie_name])) {
|
||||
// no session, nothing to do
|
||||
return $response;
|
||||
}
|
||||
if (!isset($response->headers['Content-Type'])) {
|
||||
return $response;
|
||||
}
|
||||
try {
|
||||
$data = Pluf_Middleware_Session::_decodeData($request->COOKIE[$cookie_name]);
|
||||
} catch (Exception $e) {
|
||||
// no valid session
|
||||
return $response;
|
||||
}
|
||||
if (!isset($data['Pluf_Session_key'])) {
|
||||
// no session key
|
||||
return $response;
|
||||
}
|
||||
$ok = false;
|
||||
$cts = array('text/html', 'application/xhtml+xml');
|
||||
foreach ($cts as $ct) {
|
||||
if (false !== strripos($response->headers['Content-Type'], $ct)) {
|
||||
$ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$ok) {
|
||||
return $response;
|
||||
}
|
||||
$token = self::makeToken($data['Pluf_Session_key']);
|
||||
$extra = '<div style="display:none;"><input type="hidden" name="csrfmiddlewaretoken" value="'.$token.'" /></div>';
|
||||
$response->content = preg_replace('/(<form\W[^>]*\bmethod=(\'|"|)POST(\'|"|)\b[^>]*>)/i', '$1'.$extra, $response->content);
|
||||
return $response;
|
||||
}
|
||||
}
|
132
pluf/src/Pluf/Middleware/Debug.php
Normal file
132
pluf/src/Pluf/Middleware/Debug.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Debug middleware.
|
||||
*
|
||||
* Simply display small debug information at the end of the page. It
|
||||
* requires the xdebug extension.
|
||||
*/
|
||||
class Pluf_Middleware_Debug
|
||||
{
|
||||
/**
|
||||
* Process the response of a view.
|
||||
*
|
||||
* If the status code and content type are allowed, add the debug
|
||||
* information. Debug must be set to true in the config file to
|
||||
* active it.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @param Pluf_HTTP_Response The response
|
||||
* @return Pluf_HTTP_Response The response
|
||||
*/
|
||||
function process_response($request, $response)
|
||||
{
|
||||
if (!Pluf::f('debug', false)) {
|
||||
return $response;
|
||||
}
|
||||
if (!in_array($response->status_code,
|
||||
array(200, 201, 202, 203, 204, 205, 206, 404, 501))) {
|
||||
return $response;
|
||||
}
|
||||
$ok = false;
|
||||
$cts = array('text/html', 'text/html', 'application/xhtml+xml');
|
||||
foreach ($cts as $ct) {
|
||||
if (false !== strripos($response->headers['Content-Type'], $ct)) {
|
||||
$ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($ok == false) {
|
||||
return $response;
|
||||
}
|
||||
$js = '<script type="text/javascript">
|
||||
// <!--
|
||||
function pxDebugGetElementsByClassName(oElm, strTagName, strClassName){
|
||||
// Written by Jonathan Snook, http://www.snook.ca/jon;
|
||||
// Add-ons by Robert Nyman, http://www.robertnyman.com
|
||||
var arrElements = (strTagName == "*" && document.all)? document.all :
|
||||
oElm.getElementsByTagName(strTagName);
|
||||
var arrReturnElements = new Array();
|
||||
strClassName = strClassName.replace(/\-/g, "\\-");
|
||||
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
|
||||
var oElement;
|
||||
for(var i=0; i<arrElements.length; i++){
|
||||
oElement = arrElements[i];
|
||||
if(oRegExp.test(oElement.className)){
|
||||
arrReturnElements.push(oElement);
|
||||
}
|
||||
}
|
||||
return (arrReturnElements)
|
||||
}
|
||||
function pxDebugHideAll(elems) {
|
||||
for (var e = 0; e < elems.length; e++) {
|
||||
elems[e].style.display = \'none\';
|
||||
}
|
||||
}
|
||||
function pxDebugToggle() {
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
var e = document.getElementById(arguments[i]);
|
||||
if (e) {
|
||||
e.style.display = e.style.display == \'none\' ? \'block\' : \'none\';
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// -->
|
||||
</script>';
|
||||
$text = '<pre style="text-align: left;">';
|
||||
$text .= 'Peak mem: '.(int)(memory_get_peak_usage()/1024).'kB'."\n";
|
||||
$text .= 'Exec time: '.sprintf('%.5f', (microtime(true) - $GLOBALS['_PX_starttime'])).'s'."\n";
|
||||
$included_files = get_included_files();
|
||||
sort($included_files);
|
||||
$text .= '<a href=\'#\' onclick="return pxDebugToggle(\'debug-included-files\')">';
|
||||
$text .= 'Included files #: '.count($included_files);
|
||||
$text .= '</a></pre>'."\n";
|
||||
$text .= $js.'<div id="debug-included-files" class="debug-queries"><pre style="text-align: left;">';
|
||||
foreach ($included_files as $filename) {
|
||||
$text .= htmlspecialchars($filename)."\n";
|
||||
}
|
||||
$text .= '</pre></div>';
|
||||
if (isset($GLOBALS['_PX_debug_data']['sql_queries'])) {
|
||||
$text .= '<pre style="text-align: left;">';
|
||||
$text .= '<a href=\'#\' onclick="return pxDebugToggle(\'debug-queries\')">';
|
||||
$text .= 'DB query #: '.count($GLOBALS['_PX_debug_data']['sql_queries']);
|
||||
$text .= '</a>'."\n\n";
|
||||
$text .= '</pre>';
|
||||
$text .= '<div id="debug-queries" class="debug-queries"><pre style="text-align: left;">';
|
||||
foreach ($GLOBALS['_PX_debug_data']['sql_queries'] as $q) {
|
||||
$text .= htmlspecialchars($q)."\n";
|
||||
}
|
||||
$text .= '</pre></div>';
|
||||
} else {
|
||||
$text .= '</pre>';
|
||||
}
|
||||
$text .= '<script type="text/javascript">
|
||||
pxDebugHideAll(pxDebugGetElementsByClassName(document, \'div\', \'debug-queries\'));
|
||||
</script>';
|
||||
|
||||
$response->content = str_replace('</body>', $text.'</body>', $response->content);
|
||||
return $response;
|
||||
}
|
||||
}
|
72
pluf/src/Pluf/Middleware/GoogleAnalytics.php
Normal file
72
pluf/src/Pluf/Middleware/GoogleAnalytics.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* GoogleAnalytics middleware.
|
||||
*
|
||||
* Add the googleanalytics tracking code to the pages.
|
||||
*/
|
||||
class Pluf_Middleware_GoogleAnalytics
|
||||
{
|
||||
/**
|
||||
* Process the response of a view.
|
||||
*
|
||||
* If the status code and content type are allowed, add the
|
||||
* tracking code.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @param Pluf_HTTP_Response The response
|
||||
* @return Pluf_HTTP_Response The response
|
||||
*/
|
||||
function process_response($request, $response)
|
||||
{
|
||||
if (!Pluf::f('google_analytics_id', false)) {
|
||||
return $response;
|
||||
}
|
||||
if (!in_array($response->status_code,
|
||||
array(200, 201, 202, 203, 204, 205, 206, 404, 501))) {
|
||||
return $response;
|
||||
}
|
||||
$ok = false;
|
||||
$cts = array('text/html', 'text/html', 'application/xhtml+xml');
|
||||
foreach ($cts as $ct) {
|
||||
if (false !== strripos($response->headers['Content-Type'], $ct)) {
|
||||
$ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($ok == false) {
|
||||
return $response;
|
||||
}
|
||||
$js = '<script type="text/javascript">
|
||||
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
|
||||
document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
|
||||
</script>
|
||||
<script type="text/javascript"> try {
|
||||
var pageTracker = _gat._getTracker("'.Pluf::f('google_analytics_id').'");
|
||||
pageTracker._trackPageview(); } catch(err) {}
|
||||
</script>';
|
||||
$response->content = str_replace('</body>', $js.'</body>', $response->content);
|
||||
return $response;
|
||||
}
|
||||
}
|
57
pluf/src/Pluf/Middleware/Maintenance.php
Normal file
57
pluf/src/Pluf/Middleware/Maintenance.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Maintenance middleware.
|
||||
*
|
||||
* If a file MAINTENANCE exists in the temp folder then a maintenance
|
||||
* page is shown. If available, a template maintenance.html is used,
|
||||
* else a simple plain text 'Server in maintenance, please retry
|
||||
* later...' is shown.
|
||||
*
|
||||
* This middleware should be
|
||||
*
|
||||
* Only the actions starting with Pluf::f('maintenance_root') are not
|
||||
* interrupted, that way you can access a special url to perform
|
||||
* upgrade.
|
||||
*
|
||||
*/
|
||||
class Pluf_Middleware_Maintenance
|
||||
{
|
||||
|
||||
/**
|
||||
* Process the request.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @return bool false
|
||||
*/
|
||||
function process_request(&$request)
|
||||
{
|
||||
if (0 !== strpos($request->query, Pluf::f('maintenance_root')) && file_exists(Pluf::f('tmp_folder').'/MAINTENANCE')) {
|
||||
$res = new Pluf_HTTP_Response('Server in maintenance'."\n\n".'We are upgrading the system to make it better for you, please try again later...', 'text/plain');
|
||||
$res->status_code = 503;
|
||||
return $res;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
107
pluf/src/Pluf/Middleware/ReadOnly.php
Normal file
107
pluf/src/Pluf/Middleware/ReadOnly.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2009 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Readonly middleware.
|
||||
*
|
||||
* It is intercepting all the POST requests with a message telling
|
||||
* that the website is in read only mode.
|
||||
*
|
||||
* Optionally, a message at the top of the page is added to inform
|
||||
* that the website is in read only mode.
|
||||
*
|
||||
* Add the middleware at the top of your middleware list and
|
||||
* optionally add a message to be displayed in your configuration
|
||||
* file.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* <pre>
|
||||
* $cfg['middleware_classes'] = array(
|
||||
* 'Pluf_Middleware_ReadOnly',
|
||||
* 'Pluf_Middleware_Csrf',
|
||||
* 'Pluf_Middleware_Session',
|
||||
* 'Pluf_Middleware_Translation',
|
||||
* );
|
||||
* $cfg['read_only_mode_message'] = 'The server is in read only mode the '
|
||||
* .'time to be migrated on another host.'
|
||||
* .'Thank you for your patience.';
|
||||
* </pre>
|
||||
*
|
||||
* You can put HTML in your message.
|
||||
*
|
||||
*/
|
||||
class Pluf_Middleware_ReadOnly
|
||||
{
|
||||
/**
|
||||
* Process the request.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @return bool false
|
||||
*/
|
||||
function process_request(&$request)
|
||||
{
|
||||
if ($request->method == 'POST') {
|
||||
$res = new Pluf_HTTP_Response('Server in read only mode'."\n\n".'We are upgrading the system to make it better for you, please try again later...', 'text/plain');
|
||||
$res->status_code = 503;
|
||||
return $res;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the response of a view.
|
||||
*
|
||||
* If configured, add the message to inform that the website is in
|
||||
* read only mode.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @param Pluf_HTTP_Response The response
|
||||
* @return Pluf_HTTP_Response The response
|
||||
*/
|
||||
function process_response($request, $response)
|
||||
{
|
||||
if (!Pluf::f('read_only_mode_message', false)) {
|
||||
return $response;
|
||||
}
|
||||
if (!in_array($response->status_code,
|
||||
array(200, 201, 202, 203, 204, 205, 206, 404, 501))) {
|
||||
return $response;
|
||||
}
|
||||
$ok = false;
|
||||
$cts = array('text/html', 'application/xhtml+xml');
|
||||
foreach ($cts as $ct) {
|
||||
if (false !== strripos($response->headers['Content-Type'], $ct)) {
|
||||
$ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($ok == false) {
|
||||
return $response;
|
||||
}
|
||||
$message = Pluf::f('read_only_mode_message');
|
||||
$response->content = str_replace('<body>', '<body><div style="width: 50%; color: #c00; border: 2px solid #c00; padding: 5px; margin: 1em auto 2em; background-color: #fffde3">'.$message.'</div>', $response->content);
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
197
pluf/src/Pluf/Middleware/Session.php
Normal file
197
pluf/src/Pluf/Middleware/Session.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Session middleware.
|
||||
*
|
||||
* Allow a session object in the request and the automatic
|
||||
* login/logout of a user if a standard authentication against the
|
||||
* Pluf_User model is performed.
|
||||
*/
|
||||
class Pluf_Middleware_Session
|
||||
{
|
||||
|
||||
/**
|
||||
* Process the request.
|
||||
*
|
||||
* When processing the request, if a session is found with
|
||||
* Pluf_User creditentials the corresponding user is loaded into
|
||||
* $request->user.
|
||||
*
|
||||
* FIXME: We should logout everybody when the session table is emptied.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @return bool false
|
||||
*/
|
||||
function process_request(&$request)
|
||||
{
|
||||
$session = new Pluf_Session();
|
||||
$user_model = Pluf::f('pluf_custom_user','Pluf_User');
|
||||
$user = new $user_model();
|
||||
if (!isset($request->COOKIE[$session->cookie_name])) {
|
||||
// No session is defined. We set an empty user and empty
|
||||
// session.
|
||||
$request->user = $user;
|
||||
$request->session = $session;
|
||||
if (isset($request->COOKIE[$request->session->test_cookie_name])) {
|
||||
$request->session->test_cookie = $request->COOKIE[$request->session->test_cookie_name];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$data = self::_decodeData($request->COOKIE[$session->cookie_name]);
|
||||
} catch (Exception $e) {
|
||||
$request->user = $user;
|
||||
$request->session = $session;
|
||||
if (isset($request->COOKIE[$request->session->test_cookie_name])) {
|
||||
$request->session->test_cookie = $request->COOKIE[$request->session->test_cookie_name];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
$set_lang = false;
|
||||
if (isset($data[$user->session_key])) {
|
||||
// We can get the corresponding user
|
||||
$found_user = new $user_model($data[$user->session_key]);
|
||||
if ($found_user->id == $data[$user->session_key]) {
|
||||
// User found!
|
||||
$request->user = $found_user;
|
||||
// If the last login is from 12h or more, set it to
|
||||
// now.
|
||||
Pluf::loadFunction('Pluf_Date_Compare');
|
||||
if (43200 < Pluf_Date_Compare($request->user->last_login)) {
|
||||
$request->user->last_login = gmdate('Y-m-d H:i:s');
|
||||
$request->user->update();
|
||||
}
|
||||
$set_lang = $found_user->language;
|
||||
} else {
|
||||
$request->user = $user;
|
||||
}
|
||||
} else {
|
||||
$request->user = $user;
|
||||
}
|
||||
if (isset($data['Pluf_Session_key'])) {
|
||||
$sql = new Pluf_SQL('session_key=%s' ,$data['Pluf_Session_key']);
|
||||
$found_session = Pluf::factory('Pluf_Session')->getList(array('filter' => $sql->gen()));
|
||||
if (isset($found_session[0])) {
|
||||
$request->session = $found_session[0];
|
||||
} else {
|
||||
$request->session = $session;
|
||||
}
|
||||
} else {
|
||||
$request->session = $session;
|
||||
}
|
||||
if ($set_lang and false == $request->session->getData('pluf_language', false)) {
|
||||
$request->session->setData('pluf_language', $set_lang);
|
||||
}
|
||||
if (isset($request->COOKIE[$request->session->test_cookie_name])) {
|
||||
$request->session->test_cookie = $request->COOKIE[$request->session->test_cookie_name];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the response of a view.
|
||||
*
|
||||
* If the session has been modified save it into the database.
|
||||
* Add the session cookie to the response.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @param Pluf_HTTP_Response The response
|
||||
* @return Pluf_HTTP_Response The response
|
||||
*/
|
||||
function process_response($request, $response)
|
||||
{
|
||||
if ($request->session->touched) {
|
||||
if ($request->session->id > 0) {
|
||||
$request->session->update();
|
||||
} else {
|
||||
$request->session->create();
|
||||
}
|
||||
$data = array();
|
||||
if ($request->user->id > 0) {
|
||||
$data[$request->user->session_key] = $request->user->id;
|
||||
}
|
||||
$data['Pluf_Session_key'] = $request->session->session_key;
|
||||
$response->cookies[$request->session->cookie_name] = self::_encodeData($data);
|
||||
}
|
||||
if ($request->session->set_test_cookie != false) {
|
||||
$response->cookies[$request->session->test_cookie_name] = $request->session->test_cookie_value;
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the cookie data and create a check with the secret key.
|
||||
*
|
||||
* @param mixed Data to encode
|
||||
* @return string Encoded data ready for the cookie
|
||||
*/
|
||||
public static function _encodeData($data)
|
||||
{
|
||||
if ('' == ($key = Pluf::f('secret_key'))) {
|
||||
throw new Exception('Security error: "secret_key" is not set in the configuration file.');
|
||||
}
|
||||
$data = serialize($data);
|
||||
return base64_encode($data).md5(base64_encode($data).$key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the data and check that the data have not been tampered.
|
||||
*
|
||||
* If the data have been tampered an exception is raised.
|
||||
*
|
||||
* @param string Encoded data
|
||||
* @return mixed Decoded data
|
||||
*/
|
||||
public static function _decodeData($encoded_data)
|
||||
{
|
||||
$check = substr($encoded_data, -32);
|
||||
$base64_data = substr($encoded_data, 0, strlen($encoded_data)-32);
|
||||
if (md5($base64_data.Pluf::f('secret_key')) != $check) {
|
||||
throw new Exception('The session data may have been tampered.');
|
||||
}
|
||||
return unserialize(base64_decode($base64_data));
|
||||
}
|
||||
|
||||
public static function processContext($signal, &$params)
|
||||
{
|
||||
$params['context'] = array_merge($params['context'],
|
||||
Pluf_Middleware_Session_ContextPreProcessor($params['request']));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Context preprocessor.
|
||||
*
|
||||
* Set the $user key.
|
||||
*
|
||||
* @param Pluf_HTTP_Request Request object
|
||||
* @return array Array to merge with the context
|
||||
*/
|
||||
function Pluf_Middleware_Session_ContextPreProcessor($request)
|
||||
{
|
||||
return array('user' => $request->user);
|
||||
}
|
||||
|
||||
Pluf_Signal::connect('Pluf_Template_Context_Request::construct',
|
||||
array('Pluf_Middleware_Session', 'processContext'));
|
67
pluf/src/Pluf/Middleware/Stats.php
Normal file
67
pluf/src/Pluf/Middleware/Stats.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Debug middleware.
|
||||
*
|
||||
* Simply display small debug information at the end of the page. It
|
||||
* requires the xdebug extension.
|
||||
*/
|
||||
class Pluf_Middleware_Stats
|
||||
{
|
||||
/**
|
||||
* Process the response of a view.
|
||||
*
|
||||
* If the status code and content type are allowed, add the debug
|
||||
* information. Debug must be set to true in the config file to
|
||||
* active it.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @param Pluf_HTTP_Response The response
|
||||
* @return Pluf_HTTP_Response The response
|
||||
*/
|
||||
function process_response($request, $response)
|
||||
{
|
||||
if (!in_array($response->status_code,
|
||||
array(200, 201, 202, 203, 204, 205, 206, 404, 501))) {
|
||||
return $response;
|
||||
}
|
||||
$ok = false;
|
||||
$cts = array('text/html', 'text/html', 'application/xhtml+xml');
|
||||
foreach ($cts as $ct) {
|
||||
if (false !== strripos($response->headers['Content-Type'], $ct)) {
|
||||
$ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($ok == false) {
|
||||
return $response;
|
||||
}
|
||||
if (Pluf::f('db_debug'))
|
||||
$text = "Page rendered in " . sprintf('%.5f', (microtime(true) - $GLOBALS['_PX_starttime'])) . "s using " . count($GLOBALS['_PX_debug_data']['sql_queries']) . " queries.";
|
||||
else
|
||||
$text = "Page rendered in " . sprintf('%.5f', (microtime(true) - $GLOBALS['_PX_starttime'])) . "s.";
|
||||
$response->content = str_replace('</body>', $text.'</body>', $response->content);
|
||||
return $response;
|
||||
}
|
||||
}
|
86
pluf/src/Pluf/Middleware/Tidy.php
Normal file
86
pluf/src/Pluf/Middleware/Tidy.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Tidy middleware.
|
||||
*
|
||||
* Check if a response contains HTML errors. It writes a general log
|
||||
* in the 'tmp_folder' with a view of the details of the errors in
|
||||
* separate files. It relies on the availability of tidy on the system
|
||||
* path.
|
||||
*
|
||||
* It checks only the pages with following status code: 200, 201, 202,
|
||||
* 203, 204, 205, 206, 404, 501. And the following content type:
|
||||
* text/html, text/html, application/xhtml+xml.
|
||||
*
|
||||
* @see http://tidy.sourceforge.net/
|
||||
*/
|
||||
class Pluf_Middleware_Tidy
|
||||
{
|
||||
/**
|
||||
* Process the response of a view.
|
||||
*
|
||||
* If the status code and content type are allowed, perform the check.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @param Pluf_HTTP_Response The response
|
||||
* @return Pluf_HTTP_Response The response
|
||||
*/
|
||||
function process_response($request, $response)
|
||||
{
|
||||
if (!in_array($response->status_code,
|
||||
array(200, 201, 202, 203, 204, 205, 206, 404, 501))) {
|
||||
return $response;
|
||||
}
|
||||
$ok = false;
|
||||
$cts = array('text/html', 'application/xhtml+xml');
|
||||
foreach ($cts as $ct) {
|
||||
if (false !== strripos($response->headers['Content-Type'], $ct)) {
|
||||
$ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($ok == false) {
|
||||
return $response;
|
||||
}
|
||||
$content = escapeshellarg($response->content);
|
||||
$res = array();
|
||||
$rval = 0;
|
||||
exec('(echo '.$content.'| tidy -e -utf8 -q 3>&2 2>&1 1>&3) ', $res, $rval);
|
||||
if (empty($res)) {
|
||||
return $response;
|
||||
}
|
||||
$only_char_encoding_issue = Pluf::f('tidy_skip_encoding_errors', true);
|
||||
foreach ($res as $line) {
|
||||
if (false === strpos($line, 'invalid character code')) {
|
||||
$only_char_encoding_issue = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($only_char_encoding_issue == true) {
|
||||
return $response;
|
||||
}
|
||||
$response->content = str_replace('</body>', '<pre style="text-align: left;">'.htmlspecialchars(join("\n", $res)).'</pre></body>', $response->content);
|
||||
return $response;
|
||||
}
|
||||
}
|
87
pluf/src/Pluf/Middleware/Translation.php
Normal file
87
pluf/src/Pluf/Middleware/Translation.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Translation middleware.
|
||||
*
|
||||
* Load the translation of the website based on the useragent.
|
||||
*/
|
||||
class Pluf_Middleware_Translation
|
||||
{
|
||||
|
||||
/**
|
||||
* Process the request.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @return bool false
|
||||
*/
|
||||
function process_request(&$request)
|
||||
{
|
||||
// Find which language to use. By priority:
|
||||
// A session value with 'pluf_language'
|
||||
// A cookie with 'pluf_language'
|
||||
// The browser information.
|
||||
$lang = false;
|
||||
if (!empty($request->session)) {
|
||||
$lang = $request->session->getData('pluf_language', false);
|
||||
if ($lang and !in_array($lang, Pluf::f('languages', array('en')))) {
|
||||
$lang = false;
|
||||
}
|
||||
}
|
||||
if ($lang === false and !empty($request->COOKIE[Pluf::f('lang_cookie', 'pluf_language')])) {
|
||||
$lang = $request->COOKIE[Pluf::f('lang_cookie', 'pluf_language')];
|
||||
if ($lang and !in_array($lang, Pluf::f('languages', array('en')))) {
|
||||
$lang = false;
|
||||
}
|
||||
}
|
||||
if ($lang === false) {
|
||||
// will default to 'en'
|
||||
$lang = Pluf_Translation::getAcceptedLanguage(Pluf::f('languages', array('en')));
|
||||
}
|
||||
Pluf_Translation::loadSetLocale($lang);
|
||||
$request->language_code = $lang;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the response of a view.
|
||||
*
|
||||
* @param Pluf_HTTP_Request The request
|
||||
* @param Pluf_HTTP_Response The response
|
||||
* @return Pluf_HTTP_Response The response
|
||||
*/
|
||||
function process_response($request, $response)
|
||||
{
|
||||
$vary_h = array();
|
||||
if (!empty($response->headers['Vary'])) {
|
||||
$vary_h = preg_split('/\s*,\s*/', $response->headers['Vary'],
|
||||
-1, PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
if (!in_array('accept-language', $vary_h)) {
|
||||
$vary_h[] = 'accept-language';
|
||||
}
|
||||
$response->headers['Vary'] = implode(', ', $vary_h);
|
||||
$response->headers['Content-Language'] = $request->language_code;
|
||||
return $response;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user