diff --git a/core/polyfills.php b/core/polyfills.php index 1dfee641..437b25ed 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -173,7 +173,6 @@ function stream_file(string $file, int $start, int $end): void { $fp = fopen($file, 'r'); try { - set_time_limit(0); fseek($fp, $start); $buffer = 1024 * 1024; while (!feof($fp) && ($p = ftell($fp)) <= $end) { diff --git a/core/timeout.php b/core/timeout.php new file mode 100644 index 00000000..ee868d9c --- /dev/null +++ b/core/timeout.php @@ -0,0 +1,32 @@ +active = true; + // declare(ticks = 1); + pcntl_signal(SIGALRM, [$this, 'handle'], true); + pcntl_alarm($seconds); + set_time_limit($seconds + 5); + } + + public function clear() + { + set_time_limit(0); + $this->active = false; + } + + public function handle($signal) + { + if ($this->active) { + throw new TimeoutException(); + } + } +} diff --git a/ext/admin/main.php b/ext/admin/main.php index b06d0f7c..95648585 100644 --- a/ext/admin/main.php +++ b/ext/admin/main.php @@ -37,7 +37,7 @@ class AdminPage extends Extension public function onPageRequest(PageRequestEvent $event) { - global $database, $page, $user; + global $database, $page, $user, $_shm_timeout; if ($event->page_matches("admin")) { if (!$user->can(Permissions::MANAGE_ADMINTOOLS)) { @@ -51,7 +51,7 @@ class AdminPage extends Extension if ($user->check_auth_token()) { log_info("admin", "Util: $action"); - set_time_limit(0); + $_shm_timeout->clear(); $database->set_timeout(null); send_event($aae); } diff --git a/ext/bulk_add/main.php b/ext/bulk_add/main.php index 9aed87dd..325cca1b 100644 --- a/ext/bulk_add/main.php +++ b/ext/bulk_add/main.php @@ -24,10 +24,10 @@ class BulkAdd extends Extension public function onPageRequest(PageRequestEvent $event) { - global $page, $user; + global $page, $user, $_shm_timeout; if ($event->page_matches("bulk_add")) { if ($user->can(Permissions::BULK_ADD) && $user->check_auth_token() && isset($_POST['dir'])) { - set_time_limit(0); + $_shm_timeout->clear(); $bae = send_event(new BulkAddEvent($_POST['dir'])); foreach ($bae->results as $result) { $this->theme->add_status("Adding files", $result); diff --git a/ext/bulk_add_csv/main.php b/ext/bulk_add_csv/main.php index 6dfcb488..4c00c251 100644 --- a/ext/bulk_add_csv/main.php +++ b/ext/bulk_add_csv/main.php @@ -11,10 +11,10 @@ class BulkAddCSV extends Extension public function onPageRequest(PageRequestEvent $event) { - global $page, $user; + global $page, $user, $_shm_timeout; if ($event->page_matches("bulk_add_csv")) { if ($user->can(Permissions::BULK_ADD) && $user->check_auth_token() && isset($_POST['csv'])) { - set_time_limit(0); + $_shm_timeout->clear(); $this->add_csv($_POST['csv']); $this->theme->display_upload_results($page); } diff --git a/ext/cron_uploader/main.php b/ext/cron_uploader/main.php index 8da3c467..286d5e9a 100644 --- a/ext/cron_uploader/main.php +++ b/ext/cron_uploader/main.php @@ -350,7 +350,7 @@ class CronUploader extends Extension self::$IMPORT_RUNNING = true; try { - //set_time_limit(0); + //$_shm_timeout->clear(); $output_subdir = date('Ymd-His', time()); $image_queue = $this->generate_image_queue(); diff --git a/ext/rule34/main.php b/ext/rule34/main.php index 22000a0a..f68f5582 100644 --- a/ext/rule34/main.php +++ b/ext/rule34/main.php @@ -114,7 +114,7 @@ class Rule34 extends Extension global $database, $page, $user; # Database might not be connected at this point... - #$database->set_timeout(DATABASE_TIMEOUT+15000); // deleting users can take a while + #$database->set_timeout(null); // deleting users can take a while if (function_exists("sd_notify_watchdog")) { \sd_notify_watchdog(); diff --git a/ext/source_history/main.php b/ext/source_history/main.php index db98272e..0048d7fd 100644 --- a/ext/source_history/main.php +++ b/ext/source_history/main.php @@ -180,6 +180,7 @@ class SourceHistory extends Extension protected function process_bulk_revert_request() { + global $_shm_timeout; if (isset($_POST['revert_name']) && !empty($_POST['revert_name'])) { $revert_name = $_POST['revert_name']; } else { @@ -209,7 +210,7 @@ class SourceHistory extends Extension $revert_date = null; } - set_time_limit(0); // reverting changes can take a long time, disable php's timelimit if possible. + $_shm_timeout->clear(); // reverting changes can take a long time, disable php's timelimit if possible. // Call the revert function. $this->process_revert_all_changes($revert_name, $revert_ip, $revert_date); diff --git a/ext/tag_history/main.php b/ext/tag_history/main.php index 8b9d2a79..07bc4bba 100644 --- a/ext/tag_history/main.php +++ b/ext/tag_history/main.php @@ -178,6 +178,8 @@ class TagHistory extends Extension protected function process_bulk_revert_request() { + global $_shm_timeout; + if (isset($_POST['revert_name']) && !empty($_POST['revert_name'])) { $revert_name = $_POST['revert_name']; } else { @@ -207,7 +209,7 @@ class TagHistory extends Extension $revert_date = null; } - set_time_limit(0); // reverting changes can take a long time, disable php's timelimit if possible. + $_shm_timeout->clear(); // reverting changes can take a long time, disable php's timelimit if possible. // Call the revert function. $this->process_revert_all_changes($revert_name, $revert_ip, $revert_date); diff --git a/index.php b/index.php index c80883b6..f0b559a3 100644 --- a/index.php +++ b/index.php @@ -46,6 +46,8 @@ _set_up_shimmie_environment(); $_tracer = new \EventTracer(); $_tracer->begin("Bootstrap"); _load_core_files(); +$_shm_timeout = new \Timeout(); +$_shm_timeout->set(((int)ini_get('max_execution_time')) || 30); $cache = loadCache(CACHE_DSN); $database = new Database(DATABASE_DSN); $config = new DatabaseConfig($database);