From 9376ddee188693e983df55dc87e427938c1f52af Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Wed, 16 Sep 2015 16:12:38 +0200 Subject: support keepfailed in conjunction with failed preprocessors --- it_url.class | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/it_url.class b/it_url.class index 984f621..33bf4b1 100644 --- a/it_url.class +++ b/it_url.class @@ -702,7 +702,8 @@ function get_cache($p = array()) if (!($result = @filesize($dstpath) && @rename($dstpath, $path))) { @unlink($dstpath); - @unlink($path); + if (!$p['keepfailed']) + @unlink($path); } it_url::_unlock($path, $lock); -- cgit v1.2.3 From 0f9e9ea62699f0d82ab11eb4377eb889857ac3bc Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Wed, 16 Sep 2015 16:57:40 +0200 Subject: correct returncode in keepfailed case --- it_url.class | 1 + 1 file changed, 1 insertion(+) diff --git a/it_url.class b/it_url.class index 33bf4b1..b0320a6 100644 --- a/it_url.class +++ b/it_url.class @@ -704,6 +704,7 @@ function get_cache($p = array()) @unlink($dstpath); if (!$p['keepfailed']) @unlink($path); + $result = file_exists($path); } it_url::_unlock($path, $lock); -- cgit v1.2.3 From 5c56d816dd3eb16e8ecdabd100643a5f396fd46a Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 17 Sep 2015 14:52:47 +0200 Subject: do not suppress errors inside included files --- itjs.class | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/itjs.class b/itjs.class index 6ef1c8f..b8520f5 100644 --- a/itjs.class +++ b/itjs.class @@ -160,7 +160,8 @@ static function filecontents($filenames, $execphp = true) list($filename, $paramstr) = explode("?", $filename); if ($paramstr && $execphp) parse_str($paramstr, $_GET); - $result .= it::replace(array('^1$' => ""), $execphp ? @include($filename) : @file_get_contents($filename), array('utf8' => false)); + if (file_exists($filename)) + $result .= it::replace(array('^1$' => ""), $execphp ? include($filename) : file_get_contents($filename), array('utf8' => false)); $_GET = $origget; } $result .= ob_get_clean(); -- cgit v1.2.3 From 007ac6313b90e25faadcaf7d3952daa442f4b66b Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 17 Sep 2015 16:05:51 +0200 Subject: allow structured data in body --- it.class | 1 + 1 file changed, 1 insertion(+) diff --git a/it.class b/it.class index 05efbee..7416f81 100644 --- a/it.class +++ b/it.class @@ -204,6 +204,7 @@ static function error($p = array()) { $trace = it_debug::backtrace(array('skiplevels' => $p['backtraceskip'], 'skipfiles' => $p['skipfiles'])); # moved in here for performance in mass error case + $p['body'] = is_string($p['body']) ? $p['body'] : var_export($p['body'], true); if (strlen($p['body']) > 500000) { file_put_contents($datafn = "/tmp/alertdata/error-" . substr(md5($p['body']), 0, 2), $p['body']); -- cgit v1.2.3 From 044872a5afa7b7b2d3e758f32466bc331ac197ed Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 17 Sep 2015 17:57:36 +0200 Subject: no error body if NULL --- it.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/it.class b/it.class index 7416f81..8c16c56 100644 --- a/it.class +++ b/it.class @@ -204,7 +204,7 @@ static function error($p = array()) { $trace = it_debug::backtrace(array('skiplevels' => $p['backtraceskip'], 'skipfiles' => $p['skipfiles'])); # moved in here for performance in mass error case - $p['body'] = is_string($p['body']) ? $p['body'] : var_export($p['body'], true); + $p['body'] = is_string($p['body']) || !$p['body'] ? $p['body'] : var_export($p['body'], true); if (strlen($p['body']) > 500000) { file_put_contents($datafn = "/tmp/alertdata/error-" . substr(md5($p['body']), 0, 2), $p['body']); -- cgit v1.2.3 From 745b56e0aecaa952aff61c47824a190f452cda36 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 12 Oct 2015 19:01:57 +0200 Subject: use pngcrush by default; smaller files, 10% performance hit --- it.class | 3 +++ 1 file changed, 3 insertions(+) diff --git a/it.class b/it.class index 8c16c56..2ab3c1d 100644 --- a/it.class +++ b/it.class @@ -693,6 +693,9 @@ static function imageconvert($p) if (in_array($type, explode(',', $p['types']))) # Valid type? $cmdoutput = it::exec('( ' . $ultratimeout . 'gm convert 2>&1 {-opts} {in} {type}:{out} || echo "SHELL ERROR $?" ) | grep -v " iCCP: "', $p); + if ($p['type'] == "png") + it::exec('(pngcrush.sh 2>/dev/null {out} {out}.tmp && mv {out}.tmp {out}); rm -f {out}.tmp', $p); + return $cmdoutput === ""; } -- cgit v1.2.3 From 5fc9447678d6079107a9c6fdc588cbd1679b76e7 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 12 Oct 2015 19:03:30 +0200 Subject: no need for subshell --- it.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/it.class b/it.class index 2ab3c1d..f6a1713 100644 --- a/it.class +++ b/it.class @@ -694,7 +694,7 @@ static function imageconvert($p) $cmdoutput = it::exec('( ' . $ultratimeout . 'gm convert 2>&1 {-opts} {in} {type}:{out} || echo "SHELL ERROR $?" ) | grep -v " iCCP: "', $p); if ($p['type'] == "png") - it::exec('(pngcrush.sh 2>/dev/null {out} {out}.tmp && mv {out}.tmp {out}); rm -f {out}.tmp', $p); + it::exec('pngcrush.sh 2>/dev/null {out} {out}.tmp && mv {out}.tmp {out} || rm {out}.tmp', $p); return $cmdoutput === ""; } -- cgit v1.2.3 From 4e04d8fdf3befe81bd2a93190d5acb63059b131c Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 12 Oct 2015 19:49:09 +0200 Subject: dont pngcrush by default; runtime is prohibitive after all --- it.class | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/it.class b/it.class index f6a1713..9f9749e 100644 --- a/it.class +++ b/it.class @@ -652,6 +652,7 @@ static function cdist(/* $file1, ... */) * @param $p['types'] Comma separated list of accepted input types, default "bmp,eps,gif,jp2,jpg,png,svg,tif" * @param $p['quality'] JPEG quality (0-100), default is 75 * @param $p['keepalpha'] Don't add option --flatten to preserve alpha channel + * @param $p['pngcrush'] Use pngcrush for sm * @param $p['-opts'] Custom command line options to ImageMagick convert * @return Success of convert as true/false */ @@ -693,7 +694,7 @@ static function imageconvert($p) if (in_array($type, explode(',', $p['types']))) # Valid type? $cmdoutput = it::exec('( ' . $ultratimeout . 'gm convert 2>&1 {-opts} {in} {type}:{out} || echo "SHELL ERROR $?" ) | grep -v " iCCP: "', $p); - if ($p['type'] == "png") + if ($p['pngcrush'] && $p['type'] == "png") it::exec('pngcrush.sh 2>/dev/null {out} {out}.tmp && mv {out}.tmp {out} || rm {out}.tmp', $p); return $cmdoutput === ""; -- cgit v1.2.3 From 5efecd703cebc19ed1fb08725f243920e57271a0 Mon Sep 17 00:00:00 2001 From: Christian A. Weber Date: Tue, 13 Oct 2015 12:52:46 +0200 Subject: remove ultra-obsolete read_page(), get_description() and get_title() api (introduced for Myax Knowledge Manager) --- it_url.class | 81 +----------------------------------------------------------- 1 file changed, 1 insertion(+), 80 deletions(-) diff --git a/it_url.class b/it_url.class index b0320a6..e58ad14 100644 --- a/it_url.class +++ b/it_url.class @@ -32,13 +32,7 @@ class it_url var $rawurl; /* E.g. HTTP://falcon:joshua@www.Relog.CH.:80/default.asp */ var $user; /* E.g. falcon */ var $pass; /* E.g. joshua */ - - var $page; /* Page or empty */ - var $page_read; /* true if page read */ - var $title; /* Page title or empty */ - var $description; /* Page description or empty */ var $cookies; /* key => values of cookies from server */ - var $headers; /* Headers of page fetched by get() */ var $data; /* Data part, even if return code is not 200 */ var $result; /* Return code of get() */ @@ -48,6 +42,7 @@ class it_url /** * Constructor: canonicalize an URL * @param $url URL this object represents + * @param $options['encoding'] encoding of hostname ('utf-8', 'iso-8859-1' etc.) */ function it_url($url = null, $options = array()) { @@ -122,80 +117,6 @@ function it_url($url = null, $options = array()) } -/** - * Read the page into memory, extract title and description and - * set $this->page, $this->title and $this->description - * @param $timeout Timeout for operation, defaults to unlimited (0) - * @return True if page has been read and $this->page is set - */ -function read_page($timeout = 0) -{ - unset($this->page); - unset($this->title); - unset($this->description); - - /* - ** If the URL does not contain a dot followed by at least one character, - ** it is considered bogus. This prevents 'localhost', 'www', and numerical IP addresses. - */ - if (!preg_match('/\.[a-z]+$/i', $this->realhostname)) - return 0; - - $url = $this->rawurl; - while ($this->page == '') - { - $cmd = 'LANG=C wget 2>&1 -T ' . ((int)$timeout) . ' -q -U "Mozilla/4.0 (Compatible; Relog ITools)" -O - ' . preg_replace("/[ \t]/", '\\ ', escapeshellcmd("$url")); - $this->page = `$cmd`; - - if ($this->page == '') /* An error occurred. Find out what it was. */ - { - $cmd = 'LANG=C wget 2>&1 -T' . ((int)$timeout) . ' -v -U "Mozilla/4.0 (Compatible; Relog ITools)" -O - ' . preg_replace("/[ \t]/", '\\ ', escapeshellcmd($url)); - $error = `$cmd`; - if (preg_match('/Location: ([^ ]*)/i', $error, $regs)) /* Redirect ? */ - { - $url = $regs[1]; - if (!preg_match('/^[a-z]+:/i', $url)) /* Kludge for Miss Kournikova's admirers: grok local redirects (in violation of RFC) */ - $url = $this->rawurl.'/'.$url; - } - else - break; - } - - if (++$count > 4) /* Avoid infinite redirect loops */ - break; - } - - $this->page_read = 1; - - if (preg_match('#([^<]*)#i', $this->page, $regs)) - $this->title = it_htmlentities_decode($regs[1]); - - if (preg_match('/]+content="([^"]*)">/i', $this->page, $regs)) - $this->description = it_htmlentities_decode($regs[1]); - - return ($this->page != ''); -} - - -/* Return the description of this page */ -function get_description() -{ - if (!$this->page_read) - $this->read_page(); - - return $this->description; -} - - -/* Return the title of this page */ -function get_title() -{ - if (!$this->page_read) - $this->read_page(); - - return $this->title; -} - /** * Check if a given url (currently http:port80-only) can be fetched * Note: Redirects are treated as succesful -- cgit v1.2.3 From 63b9d95ee4bce9188db0881159d163e35e2a19a4 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Wed, 14 Oct 2015 14:35:00 +0200 Subject: do not send mails if it::error was called in a function from e.g. @foo() --- it.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/it.class b/it.class index 9f9749e..1b891f8 100644 --- a/it.class +++ b/it.class @@ -169,7 +169,7 @@ static function error($p = array()) if ($toscreen && !it::is_live()) $GLOBALS['debug_noredir'] = 1; - if (!$toscreen) # this error can only be sent by mail: find out if we want to suppress it + if (error_reporting() && !$toscreen) # not called with @ and this error can only be sent by mail: find out if we want to suppress it { if (!$p['id']) $sendmail = true; -- cgit v1.2.3 From 6083ab690e67d1b08e5d10828c666a0756969a3e Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 15 Oct 2015 14:16:01 +0200 Subject: 5 secs is slow enough --- itjs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itjs.php b/itjs.php index 332e0dc..9ef6583 100644 --- a/itjs.php +++ b/itjs.php @@ -22,7 +22,7 @@ require "itools/itools.lib"; if (EDC('slow' . it::match('\.(\w+)$', $_SERVER['PHP_SELF']))) - sleep(10); + sleep(5); $crc = $_REQUEST['c'] ?: $_REQUEST['s']; if ($crc != "-" && !$_SERVER['HTTP_CACHE_CONTROL'] && $_SERVER['HTTP_IF_NONE_MATCH'] && !it::is_devel() && !$_REQUEST['retry']) -- cgit v1.2.3 From cbe302c4d849dcc1b27a9cfecf1ce46a80572c04 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Fri, 16 Oct 2015 14:00:00 +0200 Subject: handle missing it_html --- it_html.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/it_html.class b/it_html.class index e8969fb..e9e312b 100644 --- a/it_html.class +++ b/it_html.class @@ -509,7 +509,7 @@ function Q($string) { if (preg_match('/[<>&"\x00-\x08\x0a-\x0c\x0e-\x1f\x80-\xff]/', $string)) # WARNING: copy/pasted to _tag() { - $charset = $GLOBALS['it_html']->p['charset']; + $charset = $GLOBALS['it_html']->p['charset'] ?: ini_get('default_charset'); if ($GLOBALS['debug_utf8check'] && $charset == "utf-8") $string = it::any2utf8($string, "error in Q()"); -- cgit v1.2.3 From 65cfeaef1fd38b5494238ea039655c7623d7b4cd Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Fri, 16 Oct 2015 15:46:21 +0200 Subject: more compact getcache debug output --- it_url.class | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/it_url.class b/it_url.class index e58ad14..0f44639 100644 --- a/it_url.class +++ b/it_url.class @@ -647,7 +647,7 @@ function get_cache($p = array()) exec("nohup bash -c 'cd {$p['cachedir']} && sleep 10 && find ?? -mmin +$maxagemin -print0 | xargs -0 -r rm' /dev/null 2>&1 &"); } - EDC('getcache', $result, $path); + ### EDC('getcache', $result, $path); # too verbose return $result ? ($p['returnheaders'] ? array($path, $headers) : $path) : false; } @@ -671,7 +671,7 @@ function _expired($path, $maxage) if ($result = EDC('nocache') ? false : @filemtime($path)) { if (time() - $result > $maxage) - EDC('getcache', "expired", $path); + EDC('getcache', "expired", $maxage, $path); else $result = false; } -- cgit v1.2.3 From 9460c349f967928a168cbcf004fcbb8bbf1760d8 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 19 Oct 2015 13:31:31 +0200 Subject: show top level in direct calls of it_debug::backtrace --- it_debug.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/it_debug.class b/it_debug.class index 6a4c387..89d5259 100644 --- a/it_debug.class +++ b/it_debug.class @@ -174,7 +174,7 @@ static function backtrace($p = array()) $p += array('levels' => 0, 'skiplevels'=> 0, 'skipfiles' => "###"); - foreach (array_slice(debug_backtrace(@constant('DEBUG_BACKTRACE_IGNORE_ARGS')), $p['skiplevels'] + 1) as $call) + foreach (array_slice(debug_backtrace(@constant('DEBUG_BACKTRACE_IGNORE_ARGS')), $p['skiplevels']) as $call) { if (($fn = $call['file']) && !it::match($p['skipfiles'], $call['file'])) { -- cgit v1.2.3 From 29852dbc1e993bc5115604ab21c253f7ffe6b0d3 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 19 Oct 2015 18:20:00 +0200 Subject: optional cache hit rate logging --- it_url.class | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/it_url.class b/it_url.class index 0f44639..a54d1f6 100644 --- a/it_url.class +++ b/it_url.class @@ -628,6 +628,7 @@ function get_cache($p = array()) $result = file_exists($path); } + $cachemiss = 1; it_url::_unlock($path, $lock); } else @@ -647,6 +648,9 @@ function get_cache($p = array()) exec("nohup bash -c 'cd {$p['cachedir']} && sleep 10 && find ?? -mmin +$maxagemin -print0 | xargs -0 -r rm' /dev/null 2>&1 &"); } + if (EDC('getcachelog')) + it::log('debug', 'getcachelog', "miss=" . intval($cachemiss), $p['url']); + ### EDC('getcache', $result, $path); # too verbose return $result ? ($p['returnheaders'] ? array($path, $headers) : $path) : false; } -- cgit v1.2.3 From 6314ea0e3825527f898cdc64dbc5304bafe241c0 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Tue, 27 Oct 2015 18:14:25 +0100 Subject: get file type from query params --- itjs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itjs.php b/itjs.php index 9ef6583..80ae502 100644 --- a/itjs.php +++ b/itjs.php @@ -21,7 +21,7 @@ require "itools/itools.lib"; -if (EDC('slow' . it::match('\.(\w+)$', $_SERVER['PHP_SELF']))) +if (EDC('slow' . it::match('\.(css|js)\b', U($_GET)))) sleep(5); $crc = $_REQUEST['c'] ?: $_REQUEST['s']; -- cgit v1.2.3 From d908cb0d4e147721970c7eff5bd24f112bdac4cd Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 29 Oct 2015 18:18:54 +0100 Subject: adapt backtraceskip to new it_debug::backtrace count --- it.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/it.class b/it.class index 1b891f8..073b2bf 100644 --- a/it.class +++ b/it.class @@ -144,7 +144,7 @@ static function error($p = array()) 'to' => $gods ? $gods : (get_current_user() ? get_current_user() : $_SERVER['SERVER_ADMIN']), 'graceperiod' => 60, 'timewindow' => 25*3600, - 'backtraceskip' => 0, + 'backtraceskip' => 1, 'blockmail' => 3600, 'omitdebuginfo' => false, 'failcount' => 2, -- cgit v1.2.3 From da71e326d6bbd7e531f0948cac99cf0c4d51ad6e Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Fri, 30 Oct 2015 15:37:17 +0100 Subject: minimal it::sort --- it.class | 9 ++++++++- tests/it.t | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/it.class b/it.class index 073b2bf..b097e60 100644 --- a/it.class +++ b/it.class @@ -1030,5 +1030,12 @@ static function json_decode($json) return ($data = json_decode($json)) === null && $json != 'null' ? it::error(array('title' => "invalid json", 'body' => $json)) : $data; } +static function sort($array, $mode = "") +{ + $func = it::replace(array('n' => ""), count_chars($mode, 3)) . "sort"; # count_chars sorts flags + $func($array, it::match('n', $mode) ? SORT_NUMERIC : 0); + + return $array; +} + } -?> diff --git a/tests/it.t b/tests/it.t index 10c4d0e..33a6c13 100755 --- a/tests/it.t +++ b/tests/it.t @@ -393,3 +393,7 @@ is(it::any2utf8(array(utf8_decode('Müller') => utf8_decode('Müller'))), array( foreach (array($dummy, false, true, null, 1, "a", "Ä", "/", array()) as $var) is(it::json_decode(it::json_encode($var)), $var); + +is(it::sort(array("2!","19!","1!")), array("1!", "19!", "2!")); +is(it::sort(array("2!","19!","1!"), "r"), array("2!", "19!", "1!")); +is(it::sort(array("2!","19!","1!"), "rn"), array("19!", "2!", "1!")); -- cgit v1.2.3 From bb29c3272cfedc6bb5de1a216d587deccb1c8a49 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 9 Nov 2015 14:10:05 +0100 Subject: allow delay before retry --- it_url.class | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/it_url.class b/it_url.class index a54d1f6..fb5a1ff 100644 --- a/it_url.class +++ b/it_url.class @@ -156,6 +156,7 @@ function is_reachable($timeout = 5) * @param $p['filemtime'] Add HTTP header to only fetch when newer than this, otherwise return true instead of data * @param $p['data']: POST data array with key-value pairs * @param $p['retries']: Number of retries if download fails, default 1 + * @param $p['retrysleep'] Number of seconds to wait before retry, fractions ok * @return contents of resulting page, considering redirects, excluding headers, or false on error */ function get($p=null, $timeout=5) @@ -190,7 +191,10 @@ function get($p=null, $timeout=5) } if (!$result && $p['retries'] > 0 && $url->result < 400) + { + usleep($p['retrysleep']*1000000); $result = $url->get(array('retries' => $p['retries'] - 1) + $p); + } if (($filter = EDC('res')) && strstr($p['url'], it::replace(array('1' => ":"), $filter))) ED($result); -- cgit v1.2.3 From f8e5dce632789e7882f2e8a9278795ea8d6ae5ff Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Tue, 21 Jul 2015 18:01:20 +0200 Subject: Use Memcached instead of Memcache and use it for local cache too (using key@machine to not clash between machines) --- it_cache.class | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/it_cache.class b/it_cache.class index 58277ae..30db0ea 100644 --- a/it_cache.class +++ b/it_cache.class @@ -71,7 +71,7 @@ static function put($key, $value, $p = array()) $p = it_cache::_defaults($p); if ($p['distributed'] && ($memcache = it_cache::_get_memcache($p))) - $success = @$memcache->set($key, $value, MEMCACHE_COMPRESSED, $p['ttl']); + $success = @$memcache->set($key, $value, $p['ttl']); else $success = ($func = self::$_store_func) ? $func($key, $value, $p['ttl']) : null; @@ -87,12 +87,12 @@ static function _get_memcache($p) { $memcache_id = "it_cache_memcache_" . $p['hostsfile']; - if (!isset($GLOBALS[$memcache_id]) && class_exists("Memcache", false)) + if (!isset($GLOBALS[$memcache_id]) && class_exists("Memcached", false)) { - $memcache = new Memcache; + $memcache = new Memcached; foreach (array_filter(it::replace(array('[#\s].*' => ""), file($p['hostsfile']))) as $host) - $reachable += intval(@$memcache->addServer($host)); + $reachable += intval(@$memcache->addServer($host, 11211)); $GLOBALS[$memcache_id] = $reachable ? $memcache : false; } @@ -100,7 +100,28 @@ static function _get_memcache($p) return $GLOBALS[$memcache_id]; } +static function _memcache_local_fetch($key, &$success) +{ + if ($memcache = it_cache::_get_memcache(it_cache::_defaults([]))) + $result = $success = @$memcache->get(self::_memcache_local_key($key)); + + return $result; +} + +static function _memcache_local_store($key, $value, $ttl) +{ + if ($memcache = it_cache::_get_memcache(it_cache::_defaults([]))) + $success = @$memcache->set(self::_memcache_local_key($key), $value, $ttl); + + return $success; +} + +static function _memcache_local_key($key) +{ + return "$key@" . gethostname(); +} + } -it_cache::$_fetch_func = function_exists("apcu_fetch") ? "apcu_fetch" : (function_exists("apc_fetch") ? "apc_fetch" : null); -it_cache::$_store_func = function_exists("apcu_store") ? "apcu_store" : (function_exists("apc_store") ? "apc_store" : null); +it_cache::$_fetch_func = function_exists("apcu_fetch") ? "apcu_fetch" : (function_exists("apc_fetch") ? "apc_fetch" : "it_cache::_memcache_local_fetch"); +it_cache::$_store_func = function_exists("apcu_store") ? "apcu_store" : (function_exists("apc_store") ? "apc_store" : "it_cache::_memcache_local_store"); -- cgit v1.2.3 From 3deecf612f4edb20097ff29f8ee97d37bf5b4b8d Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Tue, 21 Jul 2015 18:02:31 +0200 Subject: Use old syntax for itools --- it_cache.class | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/it_cache.class b/it_cache.class index 30db0ea..27be919 100644 --- a/it_cache.class +++ b/it_cache.class @@ -102,7 +102,7 @@ static function _get_memcache($p) static function _memcache_local_fetch($key, &$success) { - if ($memcache = it_cache::_get_memcache(it_cache::_defaults([]))) + if ($memcache = it_cache::_get_memcache(it_cache::_defaults(array()))) $result = $success = @$memcache->get(self::_memcache_local_key($key)); return $result; @@ -110,7 +110,7 @@ static function _memcache_local_fetch($key, &$success) static function _memcache_local_store($key, $value, $ttl) { - if ($memcache = it_cache::_get_memcache(it_cache::_defaults([]))) + if ($memcache = it_cache::_get_memcache(it_cache::_defaults(array()))) $success = @$memcache->set(self::_memcache_local_key($key), $value, $ttl); return $success; -- cgit v1.2.3 From 7c8d68afbf676446f6446f9bdf8bcbfafa92bdcf Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Thu, 10 Sep 2015 16:15:13 +0200 Subject: Fix it_cache to pass tests on PHP7 (different code paths because of memcached instead of apcu/memcache) --- it_cache.class | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/it_cache.class b/it_cache.class index 27be919..c5b8b2c 100644 --- a/it_cache.class +++ b/it_cache.class @@ -48,10 +48,17 @@ static function get($key, $p = array()) { $p = it_cache::_defaults($p); - if ($result = $GLOBALS['it_cache_local'][$key]) - ; # Use local copy + if (isset($GLOBALS['it_cache_local'][$key])) + { + # Use local copy + $result = $GLOBALS['it_cache_local'][$key]; + $success = true; + } else if ($p['distributed'] && ($memcache = it_cache::_get_memcache($p))) - $result = $success = @$memcache->get($key); + { + $result = @$memcache->get($key); + $success = !$memcache->getResultCode(); + } else $result = ($func = self::$_fetch_func) ? $func($key, $success) : null; @@ -103,7 +110,10 @@ static function _get_memcache($p) static function _memcache_local_fetch($key, &$success) { if ($memcache = it_cache::_get_memcache(it_cache::_defaults(array()))) - $result = $success = @$memcache->get(self::_memcache_local_key($key)); + { + $result = @$memcache->get(self::_memcache_local_key($key)); + $success = !$memcache->getResultCode(); + } return $result; } -- cgit v1.2.3 From 9aab17be924ec04b7b46a971d3c9f414f6eb9bf0 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Wed, 28 Oct 2015 18:51:39 +0100 Subject: extract far future header handling for ffe.php --- itjs.class | 20 ++++++++++++++++++++ itjs.php | 19 ++----------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/itjs.class b/itjs.class index b8520f5..f76620f 100644 --- a/itjs.class +++ b/itjs.class @@ -217,6 +217,26 @@ function crcurl($url) return it::match('#', $url) ? U(trim($url, "#")) : U($url, array('c' => self::checksum(array($fn)))); } +static function far_future_headers() +{ + $crc = $_REQUEST['c'] ?: $_REQUEST['s']; + if ($crc != "-" && !$_SERVER['HTTP_CACHE_CONTROL'] && $_SERVER['HTTP_IF_NONE_MATCH'] && !it::is_devel() && !$_REQUEST['retry']) + { + header("HTTP/1.0 304 Not Modified"); # client should always keep the component that fits the page it has + exit; + } + + if ($crc != "-") + @header("Etag: alwaysvalid"); # we have checksums in the url. client should always keep the version he downloaded along with the html + + if (it::is_live() && !$_REQUEST['retry']) + { + $keeptime = $crc == "-" ? 0 : ($crc ? 30*86400 : 900); # long expire if checksum present + header("Cache-Control: max-age=$keeptime, private"); # proxies should not cache since contents of same url can differ between browsers + header("Expires: " . gmdate("D, d M Y H:i:s", time() + $keeptime). " GMT"); + } +} + } ?> diff --git a/itjs.php b/itjs.php index 80ae502..a1e5a0f 100644 --- a/itjs.php +++ b/itjs.php @@ -24,16 +24,11 @@ require "itools/itools.lib"; if (EDC('slow' . it::match('\.(css|js)\b', U($_GET)))) sleep(5); -$crc = $_REQUEST['c'] ?: $_REQUEST['s']; -if ($crc != "-" && !$_SERVER['HTTP_CACHE_CONTROL'] && $_SERVER['HTTP_IF_NONE_MATCH'] && !it::is_devel() && !$_REQUEST['retry']) -{ - header("HTTP/1.0 304 Not Modified"); # client should always keep the component that fits the page it has - exit; -} - if (it::match('/server/', $_SERVER['DOCUMENT_ROOT'])) exit; # useless here. cannot write tempfiles +itjs::far_future_headers(); # may exit + $files = itjs::filenames($_GET['files'] ?: it::match('/itjs/([-a-z0-9_,.]*)', $_SERVER['PHP_SELF'])); $data = itjs::filecontents($files); $file = end($files); @@ -82,16 +77,6 @@ else if (!it::match('\.html$', $file)) header("Content-Type: application/x-javascript; charset=$charset"); } -if ($crc != "-") - @header("Etag: alwaysvalid"); # we have checksums in the url. client should always keep the version he downloaded along with the html - -if (it::is_live() && !$_REQUEST['retry']) -{ - $keeptime = $crc == "-" ? 0 : ($crc ? 30*86400 : 900); # long expire if checksum present - header("Cache-Control: max-age=$keeptime, private"); # proxies should not cache since contents of same url can differ between browsers - header("Expires: " . gmdate("D, d M Y H:i:s", time() + $keeptime). " GMT"); -} - $data = itjs::strip($data); if ($_REQUEST['boot']) -- cgit v1.2.3 From 61d20830c3b154e343408c3206a8d01d4756789b Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Tue, 3 Nov 2015 19:15:14 +0100 Subject: dont omit long expire if url was recently redownloaded --- itjs.class | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/itjs.class b/itjs.class index f76620f..32c504b 100644 --- a/itjs.class +++ b/itjs.class @@ -207,14 +207,14 @@ static function checksum($fnlist, $p = array()) return it_cache::get($key) ?: it_cache::put($key, substr(md5(self::filecontents($filenames, false)), 0, 10), array('ttl' => 60)); } -function crcurl($url) +function crcurl($url, $p = []) { if (it::match('^http', $url)) # remote url, must fetch to crc - $fn = it_url::get_cache(array('url' => $url, 'maxage' => 3600)); + list($fn, $short_expire) = array(it_url::get_cache(array('url' => $url, 'maxage' => 3600) + $p), false); else - $fn = ($m = it::match("^//(\w+)(/.*)", $url)) ? "/www/$m[0].search.ch" . $m[1] : $GLOBALS['ULTRAHOME'] . $url; + list($fn, $short_expire) = array(($m = it::match("^//(\w+)(/.*)", $url)) ? "/www/$m[0].search.ch" . $m[1] : $GLOBALS['ULTRAHOME'] . $url, true);; - return it::match('#', $url) ? U(trim($url, "#")) : U($url, array('c' => self::checksum(array($fn)))); + return it::match('#', $url) ? U(trim($url, "#")) : U($url, array('c' => self::checksum(array($fn), array('short_expire' => $short_expire)))); } static function far_future_headers() -- cgit v1.2.3 From 8007fdcdd2ac074b6e0fc5076e160d83dde8d6a3 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 9 Nov 2015 14:34:17 +0100 Subject: php4 --- itjs.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itjs.class b/itjs.class index 32c504b..e69696a 100644 --- a/itjs.class +++ b/itjs.class @@ -207,7 +207,7 @@ static function checksum($fnlist, $p = array()) return it_cache::get($key) ?: it_cache::put($key, substr(md5(self::filecontents($filenames, false)), 0, 10), array('ttl' => 60)); } -function crcurl($url, $p = []) +function crcurl($url, $p = array()) { if (it::match('^http', $url)) # remote url, must fetch to crc list($fn, $short_expire) = array(it_url::get_cache(array('url' => $url, 'maxage' => 3600) + $p), false); -- cgit v1.2.3 From 4179279e4bb7e654ba44924821a2fdd62e1e1846 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 16 Nov 2015 13:59:53 +0100 Subject: make c= optional for far future expire --- itjs.class | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/itjs.class b/itjs.class index e69696a..92a1c58 100644 --- a/itjs.class +++ b/itjs.class @@ -217,7 +217,8 @@ function crcurl($url, $p = array()) return it::match('#', $url) ? U(trim($url, "#")) : U($url, array('c' => self::checksum(array($fn), array('short_expire' => $short_expire)))); } -static function far_future_headers() +# $p['nocrc'] means no $_REQUEST['c'] is needed for far future expire +static function far_future_headers($p = []) { $crc = $_REQUEST['c'] ?: $_REQUEST['s']; if ($crc != "-" && !$_SERVER['HTTP_CACHE_CONTROL'] && $_SERVER['HTTP_IF_NONE_MATCH'] && !it::is_devel() && !$_REQUEST['retry']) @@ -231,7 +232,7 @@ static function far_future_headers() if (it::is_live() && !$_REQUEST['retry']) { - $keeptime = $crc == "-" ? 0 : ($crc ? 30*86400 : 900); # long expire if checksum present + $keeptime = $crc == "-" ? 0 : ($crc || $p['nocrc'] ? 30*86400 : 900); # long expire if checksum present header("Cache-Control: max-age=$keeptime, private"); # proxies should not cache since contents of same url can differ between browsers header("Expires: " . gmdate("D, d M Y H:i:s", time() + $keeptime). " GMT"); } -- cgit v1.2.3 From 3b4aeabd9186666e025d3233f06f7458356bfc37 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 16 Nov 2015 14:00:23 +0100 Subject: make c= optional for far future expire --- itjs.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itjs.class b/itjs.class index 92a1c58..c54abd3 100644 --- a/itjs.class +++ b/itjs.class @@ -218,7 +218,7 @@ function crcurl($url, $p = array()) } # $p['nocrc'] means no $_REQUEST['c'] is needed for far future expire -static function far_future_headers($p = []) +static function far_future_headers($p = array()) { $crc = $_REQUEST['c'] ?: $_REQUEST['s']; if ($crc != "-" && !$_SERVER['HTTP_CACHE_CONTROL'] && $_SERVER['HTTP_IF_NONE_MATCH'] && !it::is_devel() && !$_REQUEST['retry']) -- cgit v1.2.3