| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package RSFirewall! |
| 4 |
* @copyright (c) 2018 RSJoomla! |
| 5 |
* @link https://www.rsjoomla.com |
| 6 |
* @license GNU General Public License http://www.gnu.org/licenses/gpl-3.0.en.html |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
class RSFirewall_Model_File extends RSFirewall_Model |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @return object |
| 17 |
* @throws Exception |
| 18 |
*/ |
| 19 |
public function get_contents() |
| 20 |
{ |
| 21 |
return (object) array( |
| 22 |
'file' => $this->get_local_filename(), |
| 23 |
'file_contents' => $this->get_file_contents(), |
| 24 |
'status' => $this->get_status() |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @return bool|string |
| 30 |
* @since 1.0.0 |
| 31 |
*/ |
| 32 |
public function get_filename() |
| 33 |
{ |
| 34 |
if (isset($_REQUEST['file'])) { |
| 35 |
|
| 36 |
return urldecode($_REQUEST['file']); |
| 37 |
} |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return string |
| 44 |
* @since 1.0.0 |
| 45 |
*/ |
| 46 |
protected function get_local_filename() |
| 47 |
{ |
| 48 |
$path = realpath(RSFIREWALL_SITE . '/' . $this->get_filename()); |
| 49 |
$root = realpath(RSFIREWALL_SITE); |
| 50 |
|
| 51 |
// Check if the path is valid and within the root directory |
| 52 |
if ($path === false || strpos($path, $root) !== 0) { |
| 53 |
throw new Exception(sprintf(__('Invalid file path: %s', 'rsfirewall'), $this->get_filename())); |
| 54 |
} |
| 55 |
|
| 56 |
return $path; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @since 1.0.0 |
| 61 |
* @return string |
| 62 |
* @throws Exception |
| 63 |
*/ |
| 64 |
public function get_file_contents() |
| 65 |
{ |
| 66 |
$path = $this->get_local_filename(); |
| 67 |
|
| 68 |
if (!file_exists($path)) { |
| 69 |
throw new Exception(sprintf(__('Couldn\'t find %s .', 'rsfirewall'), $path)); |
| 70 |
} |
| 71 |
|
| 72 |
if (!is_readable($path)) { |
| 73 |
throw new Exception(sprintf(__('%s is not readable.', 'rsfirewall'), $path)); |
| 74 |
} |
| 75 |
|
| 76 |
if (!is_file($path)) { |
| 77 |
throw new Exception(sprintf(__('%s is not a file.', 'rsfirewall'), $path)); |
| 78 |
} |
| 79 |
|
| 80 |
return file_get_contents($path); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @since 1.0.0 |
| 85 |
* @return array|bool |
| 86 |
* @throws Exception |
| 87 |
*/ |
| 88 |
public function get_status() |
| 89 |
{ |
| 90 |
$path = $this->get_local_filename(); |
| 91 |
|
| 92 |
if (!file_exists($path)) { |
| 93 |
throw new Exception(sprintf(__('Couldn\'t find %s .', 'rsfirewall'), $path)); |
| 94 |
} |
| 95 |
|
| 96 |
if (!is_readable($path)) { |
| 97 |
throw new Exception(sprintf(__('%s is not readable.', 'rsfirewall'), $path)); |
| 98 |
} |
| 99 |
|
| 100 |
if (!is_file($path)) { |
| 101 |
throw new Exception(sprintf(__('%s is not a file.', 'rsfirewall'), $path)); |
| 102 |
} |
| 103 |
|
| 104 |
$check_model = RSFirewall_Helper::call_user_func_pro(array('RSFirewall_Model_Check', 'get_instance')); |
| 105 |
|
| 106 |
return $check_model->signatures_check($path); |
| 107 |
} |
| 108 |
} |