diff options
| -rw-r--r-- | auto_prepend.php | 8 | ||||
| -rw-r--r-- | it.class | 11 | ||||
| -rw-r--r-- | it_url.class | 16 | ||||
| -rwxr-xr-x | test/it.t | 4 | ||||
| -rwxr-xr-x | test/it_url.t | 59 |
5 files changed, 28 insertions, 70 deletions
diff --git a/auto_prepend.php b/auto_prepend.php index 485b0c5..da91045 100644 --- a/auto_prepend.php +++ b/auto_prepend.php @@ -158,14 +158,6 @@ function U(...$args) /************************** other functions ***************************/ /** - * Print an error message and end page - */ -function fail($text) -{ - it::fatal($text); -} - -/** * Global shortcut for $it_debug::debug() * @see it_debug::debug() */ @@ -25,7 +25,6 @@ static $error_context; # Callback, can provide $p params like 'head' and 'toscre /** * Create config class with static members initialized (e.g. $home). - * NOTE: PHP5 ONLY * @param $p Static members to be generated in newly created class * service: Class name of created class (default: from caller path) * home: Home directory of application (default: from caller path) @@ -44,6 +43,10 @@ static function createconfig($p = array()) 'service' => $parts[2], ); + # Add credentials from service's "var/credentials" as static members so they don't need to be in git + foreach (it::match('(\w+)\s*=\s*["\']?(.*?)["\']?$', @it::file_get_contents($GLOBALS['ULTRAHOME'] . "/var/credentials"), ['multiline' => true, 'all' => true]) as $env) + $p += [$env[0] => $env[1]]; + if (class_exists($p['service'] . "_tools")) $extends = "extends {$p['service']}_tools "; @@ -623,7 +626,7 @@ static function any2utf8($value, $errprefix = "") */ static function ucfirst($string) { - return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1); + return mb_ucfirst($string); } /** @@ -631,7 +634,7 @@ static function ucfirst($string) */ static function lcfirst($string) { - return mb_strtolower(mb_substr($string, 0, 1)) . mb_substr($string, 1); + return mb_lcfirst($string); } /** @@ -639,7 +642,7 @@ static function lcfirst($string) */ static function ucwords($string) { - return preg_replace_callback('/\b\w/u', function($m) { return mb_strtoupper($m[0]); }, mb_strtolower($string)); + return mb_convert_case($string, MB_CASE_TITLE, "UTF-8"); } static function substr_replace($string, $replacement, $start, $length) diff --git a/it_url.class b/it_url.class index 8ebe1e5..6916661 100644 --- a/it_url.class +++ b/it_url.class @@ -201,8 +201,8 @@ static function _default_headers($url, $p) 'X-Ultra-Https' => $_SERVER['HTTPS'], ]); - if (is_int($p['filemtime'])) - $headers['If-Modified-Since'] = date("r", $p['filemtime']); + if (!$p['unconditional'] && is_int($p['filemtime'])) + $headers['If-Modified-Since'] = gmdate("D, d M Y H:i:s T", $p['filemtime']); # Use GMT as per https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/If-Modified-Since return $headers; } @@ -602,6 +602,7 @@ static function get_cache_filename($p) * @param $p['safety'] DEPRECATED. see $p['it_error'] * @param $p['it_error'] parameters for it::error(), false means ignore errors, anything else gets passed to it::error() if errors occur * @param $p['keepfailed'] keep old versions of files if download fails + * @param $p['unconditional'] Make request unconditional, i.e. do not send If-Modified-Since header when refetching (Work-around for Windy webcam caching problem) * @param $p['returnheaders'] Return array($path, $headers) instead of simply $path * @param $p['postprocess'] UNSUPPORTED, use ::get_cache_contents * @param $p['lock'] prevent multiple requests to same url from different processes [true] @@ -727,9 +728,8 @@ static function get_cache($p = array()) if (EDC('getcachelog')) it::log('debug', 'getcachelog', $p['id'], $p['url'], !$isnewfile ? "" : "fetched=" . mb_substr(is_string($data) ? $data : "(assoc)", 0, 400)); - ### EDC('getcache', $success, $path); # too verbose return $p['returnheaders'] || $p['returncachemiss'] - ? ($success ? [$path, $headers, (bool)$isnewfile] : null) + ? [$success ? $path : false, $headers, (bool)$isnewfile] : ($success ? $path : false); } @@ -741,7 +741,7 @@ static function get_cache($p = array()) */ static function get_cache_contents($p) { - [$fn, $dummy, $cachemiss] = self::get_cache($p + ['returncachemiss' => true]); + [$fn, $headers, $cachemiss] = self::get_cache($p + ['returncachemiss' => true]); if ($fn) { $result = it::file_get_contents($fn); @@ -755,7 +755,7 @@ static function get_cache_contents($p) $result = self::_postprocess($result, $p); } else - $result = it::error((array)$p['it_error'] + ['title' => $p['safety'] === 0 ? false : "failed getting " . static::absolute($p['url']), 'body' => $p + ['fn' => $fn, 'dummy' => $dummy, 'cachemiss' => $cachemiss]]); + $result = it::error((array)$p['it_error'] + ['title' => "failed getting " . static::absolute($p['url']), 'body' => $p + ['fn' => $fn, 'headers' => $headers, 'cachemiss' => $cachemiss]]); return $result; } @@ -860,9 +860,9 @@ static function _atomicwrite($path, $data) } /** - * Make an URL absolute by using host and protocol from current Apache request (but not port number) + * Make an URL absolute by using host and protocol (but not port) from current Apache request; http: in script context * @param $url Optional URL ( foo.html, /foo.html, //host/bar.html, https://host/bar.html ), default self - * @param $proto_force Optional protocol to enforce, default protocol of current request or http if in script context + * @param $proto_force Optional protocol to enforce, even if a protocol is given in $url * @return absolute version of URL ( http[s]://host/bar.html ) */ static function absolute($url = null, $proto_force = null, $prefix = '') @@ -468,7 +468,6 @@ is(it::any2utf8([1, true, false, null]), [1, true, false, null], "any2utf8 shoul is(it::any2utf8([it::utf8_decode('Müller') => it::utf8_decode('Müller')]), ['Müller' => 'Müller'], "it::any2utf8 latin1 keys"); is(it::any2utf8("\xc2\xad"), "", "it::any2utf8 remove soft hyphens"); -is(it::any2utf8("\u{0338}\u{0338}a\u{0338}b"), "a\u{0338}b", "it::any2utf8 remove combining characters at beginning"); foreach ([ 'a' => 'ä', 'e' => 'ë', 'i' => 'ï', 'o' => 'ö', 'u' => 'ü' ] as $src => $dst) { @@ -479,8 +478,7 @@ foreach ([ 'a' => 'ä', 'e' => 'ë', 'i' => 'ï', 'o' => 'ö', 'u' => 'ü' ] as } is(it::any2utf8("\x65\xcc\x81"), "é", "it::any2utf8 convert to normal form C"); -is(it::any2utf8("\xcc\xb8x"), "x", "bad (un-nomalizable) combining char"); -is(it::any2utf8(" \xcc\xb8x"), " x", "bad (un-normalizable) combining char"); +is(it::any2utf8("\u{0338}\u{0338}a\u{0338}b \u{0338}\u{0338}c"), "a\u{0338}b c", "it::any2utf8 remove combining chars at start and after blank"); foreach ([$dummy, false, true, null, 1, "a", "Ä", "/", []] as $var) is(it::json_decode(it::json_encode($var)), $var); diff --git a/test/it_url.t b/test/it_url.t index b86f711..215cb63 100755 --- a/test/it_url.t +++ b/test/it_url.t @@ -77,60 +77,25 @@ is( $php_self = $_SERVER['PHP_SELF']; $_SERVER['PHP_SELF'] = '/foo/bar/self.php'; list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null]; -is( - it_url::absolute("/"), - 'http://gna.ch/', - 'it_url::absolute basic' -); - -is( - it_url::absolute("qux"), - 'http://gna.ch/foo/bar/qux', - 'it_url::absolute relative path' -); - -is( - it_url::absolute("qux", 'https', '/prefix'), - 'https://gna.ch/prefix/foo/bar/qux', - 'it_url::absolute relative path' -); +is(it_url::absolute("/"), 'http://gna.ch/', 'it_url::absolute basic'); +is(it_url::absolute("qux"), 'http://gna.ch/foo/bar/qux', 'it_url::absolute relative path'); +is(it_url::absolute("qux", 'https', '/prefix'), 'https://gna.ch/prefix/foo/bar/qux', 'it_url::absolute relative path'); +is(it_url::absolute("/https", "https"), 'https://gna.ch/https', 'it_url::absolute force https in http context'); +is(it_url::absolute("http://gna.ch/foo", "https"),'https://gna.ch/foo', 'it_url::absolute force https overwriting existing url'); list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch:42", 42, null]; -is( - it_url::absolute("/port"), - 'http://gna.ch:42/port', - 'it_url::absolute with non-standard port' -); - -list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", 443, true]; -is( - it_url::absolute("/https"), - 'https://gna.ch/https', - 'it_url::absolute for https' -); - -list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null]; -is( - it_url::absolute("/https", "https"), - 'https://gna.ch/https', - 'it_url::absolute force https in http context' -); +is(it_url::absolute("/port"), 'http://gna.ch:42/port', 'it_url::absolute with non-standard port'); list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", 443, true]; -is( - it_url::absolute("/foo", "http"), - 'http://gna.ch/foo', - 'it_url::absolute force http in https context' -); +is(it_url::absolute("/https"), 'https://gna.ch/https', 'it_url::absolute for https'); +is(it_url::absolute("http://gna.ch/"), 'http://gna.ch/', 'it_url::absolute leaves http: in url alone'); +is(it_url::absolute("/foo", "http"), 'http://gna.ch/foo', 'it_url::absolute force http in https context'); +is(it_url::absolute("http://gna.ch/", 'https'), 'https://gna.ch/', 'it_url::absolute overrides http: in url'); -list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = ["gna.ch", null, null]; -is( - it_url::absolute("http://gna.ch/foo", "https"), - 'https://gna.ch/foo', - 'it_url::absolute force https overwriting existing url' -); +list ($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['HTTPS']) = [null, null, null]; $_SERVER['PHP_SELF'] = $php_self; + is(it_url::is_reachable('http://search.ch/'), true, "is_reachable('http://www.search.ch/')"); is(it_url::is_reachable('http://search.ch/not_found'), false, "is_reachable('http://www.search.ch/not_found')"); is(it_url::is_reachable('http://bogus.url'), false, "is_reachable('http://bogus.url')"); |