|
- <?php
- /*
- * Common functions and definitions
- * Copyright (C) 2020 Polyna <https://wandystan.eu/B196>
- *
- * This file is part of LDMW statistical scripts.
- *
- * LDMW statistical scripts are free software: you can redistribute them
- * and/or modify them under the terms of the GNU Affero General Public
- * License as published by the Free Software Foundation, either version 3
- * of the License, or (at your option) any later version.
- *
- * LDMW statistical scripts are distributed in the hope that they will be
- * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with LDMW statistical scripts. If not, see
- * <https://www.gnu.org/licenses/>.
- */
- define('REGEXP_ISO8601_DATE', '\d{4}(-\d{2}|-?(\d{2}-?\d{2}|W\d{2}(-\d)?|\d{3}))');
- define('REGEXP_ISO8601_INTERVAL', 'P(\d+Y(\d+M)?(\d+D)?|\d+M(\d+D)?|\d+[DW])');
- define('REGEXP_ISO8601_DATE_OR_INTERVAL', '(' . REGEXP_ISO8601_DATE . '|' . REGEXP_ISO8601_INTERVAL . ')');
-
- function exit_error($message) {
- http_response_code(500);
- header('Content-type: text/plain; charset=utf-8');
- print 'ERROR: ' . $message . PHP_EOL;
- exit;
- }
-
- function exit_error_handler($exception) {
- exit_error($exception->getMessage());
- }
- set_exception_handler('exit_error_handler');
-
- function load_remote_dom_document($uri) {
- $content = file_get_contents($uri);
- $document = new DOMDocument;
-
- if ($content === false)
- throw new Exception("Cannot load remote URI: $uri");
-
- if (! $document->loadHTML($content, LIBXML_NOWARNING | LIBXML_NOERROR))
- throw new Exception("Cannot parse remote URI as HTML: $uri");
-
- return $document;
- }
-
- function starts_with($haystack, $needle) {
- return substr_compare($haystack, $needle, 0, strlen($needle)) === 0;
- }
|