diff --git a/cron/status.cron.php b/cron/status.cron.php index 6b21420f..a1ee428f 100644 --- a/cron/status.cron.php +++ b/cron/status.cron.php @@ -30,11 +30,20 @@ require_once __DIR__ . '/../src/bootstrap.php'; if(!psm_is_cli()) { // check if it's an allowed host - $allow = PSM_CRON_ALLOW; - if(!in_array($_SERVER['REMOTE_ADDR'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow)) { - header('HTTP/1.0 404 Not Found'); - die('404 Not Found

Not Found

The requested URL /cron/status.cron.php was not found on this server.

'); + if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ + $_SERVER["HTTP_X_FORWARDED_FOR"] = ""; } + + // define won't accept array before php 7.0.0 + // check if data is serialized (not needed when using php 7.0.0 and higher) + $data = @unserialize(PSM_CRON_ALLOW); + $allow = $data === false ? PSM_CRON_ALLOW : $data; + + if(!in_array($_SERVER['REMOTE_ADDR'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow)) { + header('HTTP/1.0 403 Forbidden'); + die('403 Forbidden

Forbidden

IP address not allowed. See the documentation for more info.

'); + } + echo "OK"; } $cron_timeout = PSM_CRON_TIMEOUT; diff --git a/docs/install.rst b/docs/install.rst index 627704fb..04573d65 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -122,7 +122,20 @@ If you're work with cPanel you can follow these steps: - Type `php /home2//public_html/phpservermon/cron/status.cron.php` in the command field 4. Submit - + +Cronjob over web +---------------- +To allow scheduled status updates over the web, the commandline check is extended with a check on allowed IP address(es). + +In config.php add following line:: + + // PHP 7.0.0 and higher + define('PSM_CRON_ALLOW', array("xxx.xxx.xxx.xxx", "yyy.yyy.yyy.yyy", "zzz.zzz.zzz.zzz")); + // lower versions + define('PSM_CRON_ALLOW', serialize(array("xxx.xxx.xxx.xxx", "yyy.yyy.yyy.yyy", "zzz.zzz.zzz.zzz"))); + +After that, you can hit the url http(s)://"yourmonitor.com"/cron/status.cron.php over the web from your allowed IP. + Troubleshooting +++++++++++++++ @@ -130,4 +143,4 @@ Troubleshooting If you have problems setting up or accessing your monitor and do not know why, enable debug mode to turn on error reporting. To enable debug mode, add the following line to your config.php file:: - define('PSM_DEBUG', true); + define('PSM_DEBUG', true); diff --git a/src/bootstrap.php b/src/bootstrap.php index 1c8e47e0..33798305 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -58,6 +58,12 @@ if(PSM_DEBUG) { ini_set('display_errors', 0); } +// check for a cron allowed ip array +if(!defined('PSM_CRON_ALLOW')) { + //serialize for php version lower than 7.0.0 + define('PSM_CRON_ALLOW', serialize(array())); +} + $vendor_autoload = PSM_PATH_SRC . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; if(!file_exists($vendor_autoload)) { die('No dependencies found in vendor dir. Did you install the dependencies? Please run "php composer.phar install".');