Pluf Framework

Pluf Framework Git Source Tree

Root/src/Pluf/Middleware/ReadOnly.php

1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of Plume Framework, a simple PHP Application Framework.
6# Copyright (C) 2001-2009 Loic d'Anterroches and contributors.
7#
8# Plume Framework is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published by
10# the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# Plume Framework is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24/**
25 * Readonly middleware.
26 *
27 * It is intercepting all the POST requests with a message telling
28 * that the website is in read only mode.
29 *
30 * Optionally, a message at the top of the page is added to inform
31 * that the website is in read only mode.
32 *
33 * Add the middleware at the top of your middleware list and
34 * optionally add a message to be displayed in your configuration
35 * file.
36 *
37 * Example:
38 *
39 * <pre>
40 * $cfg['middleware_classes'] = array(
41 * 'Pluf_Middleware_ReadOnly',
42 * 'Pluf_Middleware_Csrf',
43 * 'Pluf_Middleware_Session',
44 * 'Pluf_Middleware_Translation',
45 * );
46 * $cfg['read_only_mode_message'] = 'The server is in read only mode the '
47 * .'time to be migrated on another host.'
48 * .'Thank you for your patience.';
49 * </pre>
50 *
51 * You can put HTML in your message.
52 *
53 */
54class Pluf_Middleware_ReadOnly
55{
56 /**
57 * Process the request.
58 *
59 * @param Pluf_HTTP_Request The request
60 * @return bool false
61 */
62 function process_request(&$request)
63 {
64 if ($request->method == 'POST') {
65 $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');
66 $res->status_code = 503;
67 return $res;
68 }
69 return false;
70 }
71
72 /**
73 * Process the response of a view.
74 *
75 * If configured, add the message to inform that the website is in
76 * read only mode.
77 *
78 * @param Pluf_HTTP_Request The request
79 * @param Pluf_HTTP_Response The response
80 * @return Pluf_HTTP_Response The response
81 */
82 function process_response($request, $response)
83 {
84 if (!Pluf::f('read_only_mode_message', false)) {
85 return $response;
86 }
87 if (!in_array($response->status_code,
88 array(200, 201, 202, 203, 204, 205, 206, 404, 501))) {
89 return $response;
90 }
91 $ok = false;
92 $cts = array('text/html', 'application/xhtml+xml');
93 foreach ($cts as $ct) {
94 if (false !== strripos($response->headers['Content-Type'], $ct)) {
95 $ok = true;
96 break;
97 }
98 }
99 if ($ok == false) {
100 return $response;
101 }
102 $message = Pluf::f('read_only_mode_message');
103 $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);
104 return $response;
105 }
106
107}
108

Archive Download this file

Branches

Tags