| 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-2007 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 | * Tidy middleware.␊ |
| 26 | *␊ |
| 27 | * Check if a response contains HTML errors. It writes a general log␊ |
| 28 | * in the 'tmp_folder' with a view of the details of the errors in␊ |
| 29 | * separate files. It relies on the availability of tidy on the system␊ |
| 30 | * path.␊ |
| 31 | *␊ |
| 32 | * It checks only the pages with following status code: 200, 201, 202,␊ |
| 33 | * 203, 204, 205, 206, 404, 501. And the following content type:␊ |
| 34 | * text/html, text/html, application/xhtml+xml.␊ |
| 35 | *␊ |
| 36 | * @see http://tidy.sourceforge.net/␊ |
| 37 | */␊ |
| 38 | class Pluf_Middleware_Tidy␊ |
| 39 | {␊ |
| 40 | /**␊ |
| 41 | * Process the response of a view.␊ |
| 42 | *␊ |
| 43 | * If the status code and content type are allowed, perform the check.␊ |
| 44 | *␊ |
| 45 | * @param Pluf_HTTP_Request The request␊ |
| 46 | * @param Pluf_HTTP_Response The response␊ |
| 47 | * @return Pluf_HTTP_Response The response␊ |
| 48 | */␊ |
| 49 | function process_response($request, $response)␊ |
| 50 | {␊ |
| 51 | if (!in_array($response->status_code, ␊ |
| 52 | array(200, 201, 202, 203, 204, 205, 206, 404, 501))) {␊ |
| 53 | return $response;␊ |
| 54 | }␊ |
| 55 | $ok = false;␊ |
| 56 | $cts = array('text/html', 'application/xhtml+xml');␊ |
| 57 | foreach ($cts as $ct) {␊ |
| 58 | if (false !== strripos($response->headers['Content-Type'], $ct)) {␊ |
| 59 | $ok = true;␊ |
| 60 | break;␊ |
| 61 | }␊ |
| 62 | }␊ |
| 63 | if ($ok == false) {␊ |
| 64 | return $response;␊ |
| 65 | }␊ |
| 66 | $content = escapeshellarg($response->content);␊ |
| 67 | $res = array();␊ |
| 68 | $rval = 0;␊ |
| 69 | exec('(echo '.$content.'| tidy -e -utf8 -q 3>&2 2>&1 1>&3) ', $res, $rval);␊ |
| 70 | if (empty($res)) {␊ |
| 71 | return $response;␊ |
| 72 | }␊ |
| 73 | $only_char_encoding_issue = Pluf::f('tidy_skip_encoding_errors', true);␊ |
| 74 | foreach ($res as $line) {␊ |
| 75 | if (false === strpos($line, 'invalid character code')) {␊ |
| 76 | $only_char_encoding_issue = false;␊ |
| 77 | break;␊ |
| 78 | }␊ |
| 79 | }␊ |
| 80 | if ($only_char_encoding_issue == true) {␊ |
| 81 | return $response;␊ |
| 82 | }␊ |
| 83 | $response->content = str_replace('</body>', '<pre style="text-align: left;">'.htmlspecialchars(join("\n", $res)).'</pre></body>', $response->content);␊ |
| 84 | return $response;␊ |
| 85 | }␊ |
| 86 | }␊ |
| 87 | |