Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| tao_install_utils_Shield | |
0.00% |
0 / 59 |
|
0.00% |
0 / 4 |
870 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| disableRewritePattern | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
90 | |||
| protectInstall | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
56 | |||
| denyAccessTo | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * install Shield to block future installation |
| 29 | * |
| 30 | * @package tao |
| 31 | |
| 32 | */ |
| 33 | class tao_install_utils_Shield |
| 34 | { |
| 35 | protected $extensions = []; |
| 36 | protected $accessFiles = []; |
| 37 | |
| 38 | public function __construct(array $extensions) |
| 39 | { |
| 40 | $this->extensions = $extensions; |
| 41 | foreach ($this->extensions as $extension) { |
| 42 | $file = ROOT_PATH . $extension . '/.htaccess'; |
| 43 | if (file_exists($file)) { |
| 44 | if (!is_readable($file)) { |
| 45 | throw new tao_install_utils_Exception( |
| 46 | "Unable to read .htaccess file of extension '" . $extension |
| 47 | . " while Production Mode activation'." |
| 48 | ); |
| 49 | } |
| 50 | $this->accessFiles[] = $file; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | public function disableRewritePattern(array $patterns) |
| 56 | { |
| 57 | |
| 58 | $globalPattern = ''; |
| 59 | $size = count($patterns) - 1; |
| 60 | foreach ($patterns as $i => $pattern) { |
| 61 | $globalPattern .= preg_quote($pattern, '/'); |
| 62 | if ($i < $size) { |
| 63 | $globalPattern .= '|'; |
| 64 | } |
| 65 | } |
| 66 | if (!empty($globalPattern)) { |
| 67 | foreach ($this->accessFiles as $file) { |
| 68 | $lines = explode("\n", file_get_contents($file)); |
| 69 | $updated = 0; |
| 70 | foreach ($lines as $i => $line) { |
| 71 | if (preg_match("/" . $globalPattern . "/", $line)) { |
| 72 | $lines[$i] = '#' . $line; |
| 73 | $updated++; |
| 74 | } |
| 75 | } |
| 76 | if ($updated > 0) { |
| 77 | if (!is_writable($file)) { |
| 78 | throw new tao_install_utils_Exception("Unable to write .htaccess file : ${file}."); |
| 79 | } |
| 80 | file_put_contents($file, implode("\n", $lines)); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public function protectInstall() |
| 87 | { |
| 88 | foreach ($this->extensions as $extension) { |
| 89 | $installDir = ROOT_PATH . $extension . '/install/'; |
| 90 | if (file_exists($installDir) && is_dir($installDir)) { |
| 91 | if ( |
| 92 | !is_writable($installDir) |
| 93 | || (file_exists($installDir . '.htaccess' && !is_writable($installDir . '.htaccess'))) |
| 94 | ) { |
| 95 | throw new tao_install_utils_Exception("Unable to write .htaccess file into : ${installDir}."); |
| 96 | } |
| 97 | file_put_contents( |
| 98 | $installDir . '.htaccess', |
| 99 | "Options +FollowSymLinks\n" |
| 100 | . "<IfModule mod_rewrite.c>\n" |
| 101 | . "RewriteEngine On\n" |
| 102 | . "RewriteCond %{REQUEST_URI} !/css/ [NC]\n" |
| 103 | . "RewriteCond %{REQUEST_URI} !/js/ [NC]\n" |
| 104 | . "RewriteCond %{REQUEST_URI} !/images/ [NC]\n" |
| 105 | . "RewriteCond %{REQUEST_URI} !/production.html [NC]\n" |
| 106 | . "RewriteRule ^.*$ " . ROOT_URL . "tao/install/production.html\n" |
| 107 | . "</IfModule>" |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public function denyAccessTo($paths) |
| 114 | { |
| 115 | |
| 116 | |
| 117 | foreach ($this->extensions as $extension) { |
| 118 | foreach ($paths as $path) { |
| 119 | if (!preg_match("/^\\" . DIRECTORY_SEPARATOR . "/", $path)) { |
| 120 | $path = DIRECTORY_SEPARATOR . $path; |
| 121 | } |
| 122 | $denied = ROOT_PATH . $extension . $path; |
| 123 | if (file_exists($denied) && is_dir($denied)) { |
| 124 | $accessFile = $denied . DIRECTORY_SEPARATOR . '.htaccess'; |
| 125 | if (!is_writable($denied) || (file_exists($accessFile && !is_writable($accessFile)))) { |
| 126 | throw new tao_install_utils_Exception("Unable to write .htaccess file into : ${denied}."); |
| 127 | } |
| 128 | file_put_contents($accessFile, "<IfModule mod_rewrite.c>\n" |
| 129 | . "RewriteEngine On\n" |
| 130 | . "RewriteRule ^.*$ - [F]\n" |
| 131 | . "</IfModule>"); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |