diff options
-rw-r--r-- | it.class | 13 | ||||
-rw-r--r-- | it_dbi.class | 15 | ||||
-rw-r--r-- | it_dbi_postgres.class | 18 | ||||
-rw-r--r-- | it_html.class | 4 | ||||
-rw-r--r-- | it_url.class | 67 | ||||
-rwxr-xr-x | test/exec.t | 3 | ||||
-rwxr-xr-x | test/getopt.t | 3 | ||||
-rwxr-xr-x | test/it_html.t | 3 | ||||
-rw-r--r-- | test/it_url.testserver.php | 3 | ||||
-rw-r--r-- | test/it_url_server.php | 6 |
10 files changed, 89 insertions, 46 deletions
@@ -854,7 +854,7 @@ static function getopt($usage, $p = array()) $optdesc = trim($optdesc); if ($matches = (array)it::match('^--(\w[\w-]*)(=[A-Z])?', $optdesc)) list($longoptname, $longoptarg) = $matches; - elseif ($matches = (array)it::match('^-(\w)( [A-Z])?', $optdesc)) + else if ($matches = (array)it::match('^-(\w)( [A-Z])?', $optdesc)) list($shortoptname, $shortoptarg) = $matches; } @@ -897,7 +897,7 @@ static function getopt($usage, $p = array()) } else if ($arg == "--") $noopts = true; - elseif (!$noopts && ($matches = (array)it::match('^--(\w[\w-]*)(=.*)?', $arg))) + else if (!$noopts && ($matches = (array)it::match('^--(\w[\w-]*)(=.*)?', $arg))) { list($optname, $val) = $matches; if (!isset($witharg[$optname]) || isset($val) && !$witharg[$optname]) @@ -922,7 +922,7 @@ static function getopt($usage, $p = array()) $result[$optname] = true; } } - elseif($mandatoryargs) + else if($mandatoryargs) $result[strtolower(array_shift($mandatoryargs))] = $seenarg = $arg; else $result['args'][] = $seenarg = $arg; @@ -1274,10 +1274,13 @@ static function params2utf8() $_SERVER[$var] = it::any2utf8($_SERVER[$var]); $urlfix = function($m) { return urlencode(it::any2utf8(urldecode($m[0]))); }; - foreach (['QUERY_STRING', 'REQUEST_URI', 'HTTP_REFERER'] as $var) { + foreach (['QUERY_STRING', 'REQUEST_URI', 'HTTP_REFERER'] as $var) + { $_SERVER[$var.'_RAW'] = $_SERVER[$var]; $_SERVER[$var] = it::any2utf8($_SERVER[$var]); - if (strpos($_SERVER[$var], '%') !== false) { + + if (strpos($_SERVER[$var], '%') !== false) + { if (grapheme_strlen(urldecode($_SERVER[$var])) === null) # handle latin (double encodes correct utf8) $_SERVER[$var] = preg_replace_callback('/%[89A-F][A-Z0-9]/i', $urlfix, $_SERVER[$var]); while (preg_match('/%C3%8[23]%C2%[89ab][0-9a-f]/i', $_SERVER[$var]) && $iterations++ < 3) # handle doubly encoded utf8, UTF8SAFE diff --git a/it_dbi.class b/it_dbi.class index 71b6c95..b681102 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -96,7 +96,7 @@ function __construct($p = array(), $query = null) array_shift($args); call_user_func_array(array($this, "select"), $args); } - elseif (isset($query)) + else if (isset($query)) $this->read($query); } else @@ -248,7 +248,8 @@ function _expressions($tags, $force = false) foreach((array)$tags as $field => $value) { - if (is_int($field)) { /* no key specified; pass on unchanged */ + if (is_int($field)) # No key specified; pass on unchanged + { $result[$field] = $value; continue; } @@ -311,7 +312,8 @@ function _set($tags, $force = false) { $expressions = $this->_expressions($tags, $force); $append = []; - foreach ((array)$expressions as $k => $v) { + foreach ((array)$expressions as $k => $v) + { if (is_int($k)) /* no key specified; just append */ $append[] = $v; else @@ -329,7 +331,8 @@ function _values($tags, $force = false) { $expressions = $this->_expressions($tags, $force); $append = []; - foreach ((array)$expressions as $k => $v) { + foreach ((array)$expressions as $k => $v) + { if (is_int($k)) /* no key specified; just append */ $append[] = $v; else { @@ -337,6 +340,7 @@ function _values($tags, $force = false) $vals[] = $v; } } + return $expressions ? '(' . implode(', ', $keys) . ') VALUES (' . implode(', ', $vals) . ')' . implode(' ', $append) : ''; } @@ -931,7 +935,8 @@ function _get_field_info() if (!($this->_fields = $state['fields'][$this->_p['table']])) { debug("it_dbi(): no fields for {$dbid}.{$this->_p['table']}, calculating.", 5); - foreach ($this->_get_field_defs() as $name => $field) { + foreach ($this->_get_field_defs() as $name => $field) + { $this->_fields[$name] = $field + array('Length' => preg_match('/date|time/', $field['Type']) ? 20 : intval(it::match('\d+', $field['Type']))); if (preg_match('/^(tiny|small|medium|)int|^float|^double/', $field['Type'])) $this->_convertfunc[$name] = it::match('int', $field['Type']) ? "intval" : "floatval"; diff --git a/it_dbi_postgres.class b/it_dbi_postgres.class index 46c0393..7ea3139 100644 --- a/it_dbi_postgres.class +++ b/it_dbi_postgres.class @@ -26,19 +26,23 @@ static $_global_key = 'it_dbi_postgres'; // override base class to get our own s function _where($params) { - if ($params['LIMIT'] && ($m = it::match('^\s*(\d+)\s*,\s*(\d+)\s*$', $params['LIMIT']))) { + if ($params['LIMIT'] && ($m = it::match('^\s*(\d+)\s*,\s*(\d+)\s*$', $params['LIMIT']))) + { unset($params['LIMIT']); $params[] = " OFFSET $m[0] LIMIT $m[1]"; } + return parent::_where($params); } function replace($tags = []) { - foreach (array_keys($this->_fields) as $k) { + foreach (array_keys($this->_fields) as $k) + { $escaped = $this->escape_name($k); $strings[] = $escaped . '= EXCLUDED.' . $escaped; } + $this->insert(array_merge($tags, [' ON CONFLICT (' . $this->escape_name($this->_p['keyfield']) . ') DO UPDATE SET ' . implode(', ', $strings)])); } @@ -57,7 +61,8 @@ function _get_field_defs() // recreate Key column of mysql show columns $res = $this->query('SELECT column_name,constraint_type,ordinal_position FROM information_schema.table_constraints AS t JOIN information_schema.key_column_usage USING (constraint_name, constraint_schema, constraint_catalog) ' . $where); - while ($res && ($row = $this->_fetch_assoc($res))) { + while ($res && ($row = $this->_fetch_assoc($res))) + { if ($row['constraint_type'] == 'PRIMARY KEY') $keys[$row['column_name']] = 'PRI'; else if ($row['ordinal_position'] == 1) @@ -65,9 +70,8 @@ function _get_field_defs() } $res = $this->query('SELECT * FROM information_schema.columns AS t ' . $where); - while ($res && ($field = $this->_fetch_assoc($res))) { + while ($res && ($field = $this->_fetch_assoc($res))) $result[$field['column_name']] = ['Field' => $field['column_name'], 'Type' => $field['data_type'], 'Extra' => it::match('^nextval\(', $field['column_default']) ? 'auto_increment' : '', 'Key' => $keys[$field['column_name']]]; - } return $result; } @@ -98,10 +102,12 @@ function _connect_db($p) { function _query($query, $p) { - if ($this->_p['keyfield'] && it::match('^INSERT ', $query)) { + if ($this->_p['keyfield'] && it::match('^INSERT ', $query)) + { $isinsert = true; $query .= ' RETURNING ' . $this->_escape_name($this->_p['keyfield']); } + if (!($result = pg_query($this->_link, $query)) && $p['safety']) { /* TODO diff --git a/it_html.class b/it_html.class index 6ddbab9..b27c696 100644 --- a/it_html.class +++ b/it_html.class @@ -207,7 +207,7 @@ function head($args = array()) # Add favicon if ($p['favicon']) $header .= tag('link', array('rel' => "shortcut icon", 'href' => $p['favicon'])); - elseif ($p['show_favicon'] && @file_exists($_SERVER['DOCUMENT_ROOT'] . '/favicon.ico')) + else if ($p['show_favicon'] && @file_exists($_SERVER['DOCUMENT_ROOT'] . '/favicon.ico')) $header .= tag('link', array('rel' => "shortcut icon", 'href' => "/favicon.ico")); foreach((array)$p['stylesheets'] as $type => $url) @@ -312,7 +312,7 @@ function _tag($name, $args) # close tag according to html dialect if ($this->p['htmltype'] == 'xml') # plain xml $result .= isset($data) ? ">$data</$name>$newline" : " />$newline"; - elseif ($this->p['htmltype'][0] == 'x') # xhtml: only voidtags can be shortened + else if ($this->p['htmltype'][0] == 'x') # xhtml: only voidtags can be shortened $result .= isset($data) || !self::$voidtags[$name] ? ">$data</$name>$newline" : " />$newline"; else $result .= isset($data) || !self::$voidtags[$name] ? ">$data</$name>$newline" : ">$newline"; diff --git a/it_url.class b/it_url.class index dbd39e4..bddbdb8 100644 --- a/it_url.class +++ b/it_url.class @@ -157,11 +157,12 @@ function _get($p = [], $timeout = null) function parse_http_header($header) { - foreach (explode("\n", trim($header)) as $line) { + foreach (explode("\n", trim($header)) as $line) + { $line = trim($line); if (preg_match('#^(HTTP)\S+\s(\d+)#', $line, $parts)) # Parse result code $this->headers[$parts[1]] = $this->result = $parts[2]; - elseif (preg_match('#^([^:]+): (.*)$#', $line, $parts)) + else if (preg_match('#^([^:]+): (.*)$#', $line, $parts)) $this->headers[ucwords($parts[1], '-')] = $parts[2]; if (strtolower($parts[1]) == 'set-cookie' && preg_match('/^([^=]+)=([^;]*)/', $parts[2], $cookie)) $this->cookies[$cookie[1]] = $cookie[2]; @@ -207,7 +208,8 @@ static function curl_opts($p=array()) if ($p['pass'] || $p['user']) $add += [ CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $p['user'] . ':' . $p['pass'] ]; - if ($p['writefunction']) { + if ($p['writefunction']) + { $add += [ CURLOPT_RETURNTRANSFER => false, CURLOPT_WRITEFUNCTION => $p['writefunction'], @@ -261,13 +263,15 @@ function request($p=array()) $url->headers = array(); $p['headers'] = (array)$p['headers'] + self::_default_headers($url, $p); $opts = self::curl_opts($p + array('user' => $this->user, 'pass' => $this->pass, 'followlocation' => false)); - if ($p['verbose']) { + if ($p['verbose']) + { $stderr = it::fopen("php://memory", "r+"); $opts += [CURLOPT_STDERR => $stderr, CURLOPT_VERBOSE => 1]; } $curl = curl_init($url->url); - if ($p['maxlength'] && !$p['writefunction']) { + if ($p['maxlength'] && !$p['writefunction']) + { $content = ""; $opts[CURLOPT_WRITEFUNCTION] = function ($dummy, $data) use ($p, &$content) { static $total = 0; @@ -294,18 +298,24 @@ function request($p=array()) EDC('curlinfo', curl_getinfo($curl)); - if ($got !== false) { + if ($got !== false) + { $url->header = array_slice(explode("\r\n\r\n", trim($header)), -1)[0] . "\r\n\r\n"; $url->data = $got; $url->parse_http_header($url->header); - if ($p['filemtime'] && ($url->result == 304)) { + if ($p['filemtime'] && ($url->result == 304)) + { $result = true; # Not modified, success but no data - } else if ($url->result == 414) { + } + else if ($url->result == 414) + { it::error((array)$p['it_error'] + ['title' => "Request-URI Too Long: " . substr($url->url, 0, 100) . "...(truncated " . (strlen($url->url) - 100) . " bytes)", 'body' => curl_getinfo($curl) + ($p['verbose'] ? ['verbose' => $this->verbose] : [])]); $this->errstr = "HTTP Status " . $url->result; - } else { + } + else + { $result =& $url->data; $this->errstr = "HTTP Status " . $url->result; } @@ -315,13 +325,15 @@ function request($p=array()) $this->curlinfo = curl_getinfo($curl); } - if ($p['verbose']) { + if ($p['verbose']) + { rewind($stderr); $this->verbose = stream_get_contents($stderr); fclose($stderr); } - if ($got === false && $p['retries'] <= 0) { + if ($got === false && $p['retries'] <= 0) + { it::error((array)$p['it_error'] + ['title' => "problem " . ($p['method'] ?: "gett") . "ing $url->url with curl: (" . curl_errno($curl) . ") " . curl_error($curl), 'body' => curl_getinfo($curl) + ($p['verbose'] ? ['verbose' => $this->verbose] : [])]); } @@ -382,7 +394,8 @@ static function get_multi($p=null) $iterator = $p['urls']; $parallel = $p['parallel'] ?: PHP_INT_MAX; - while (count($handles) < $parallel && $iterator->valid()) { + while (count($handles) < $parallel && $iterator->valid()) + { $addhandle($iterator->key(), $iterator->current()); $iterator->next(); } @@ -414,28 +427,37 @@ static function get_multi($p=null) $content = $p['postprocess']($content, ['it_error' => $retries[$key] < $p['retries'] ? false : (array)$p['it_error'] + ['title' => "invalid content from " . $urls[$key]]]); EDC('reqtimings', $key, $info['result'], (gettimeofday(true) - $start) * 1000); - if ($info['result'] == CURLE_OK && $content !== null) { + if ($info['result'] == CURLE_OK && $content !== null) + { if (!$p['noresults']) $results_unordered[$key] = $content; - if (it::match(self::$retryable, curl_getinfo($handles[$key], CURLINFO_RESPONSE_CODE)) && $retries[$key]++ < $p['retries']) { + if (it::match(self::$retryable, curl_getinfo($handles[$key], CURLINFO_RESPONSE_CODE)) && $retries[$key]++ < $p['retries']) + { $sleepuntils[$key] = microtime(true) + $p['retrysleep']; - } else { + } + else + { if (is_array($urls[$key]) && ($handler = $urls[$key]['handler'])) $abort = $handler($info['handle'], $content); unset($urls[$key]); } $closehandle($key); - } else if($retries[$key]++ < $p['retries']) { + } + else if($retries[$key]++ < $p['retries']) + { $closehandle($key); # closehandle must be called before addhandle as we use the same key $sleepuntils[$key] = microtime(true) + $p['retrysleep']; - } else { + } + else + { $results_unordered[$key] = false; unset($urls[$key]); $closehandle($key); } - if (!$abort && count($handles) < $parallel && $iterator->valid()) { + if (!$abort && count($handles) < $parallel && $iterator->valid()) + { $addhandle($iterator->key(), $iterator->current()); $iterator->next(); } @@ -443,8 +465,10 @@ static function get_multi($p=null) } } while ($mrc == CURLM_CALL_MULTI_PERFORM); - foreach ((array)$sleepuntils as $key => $time) { - if (microtime(true) >= $time && count($handles) < $parallel) { + foreach ((array)$sleepuntils as $key => $time) + { + if (microtime(true) >= $time && count($handles) < $parallel) + { $addhandle($key, $urls[$key]); unset($sleepuntils[$key]); } @@ -800,7 +824,8 @@ static function redirect($url = null, $type = "temporary") $url = preg_replace("/[\r\n].*/", '', it_url::absolute($url)); # Security: cut after CR/LF - if (EDC('noredir')) { + if (EDC('noredir')) + { if (!function_exists('a')) new it_html(); echo a(array('href' => $url), Q($url)) . Q(" (HTTP/1.1 $code, $type redirect)") . br() . Q("Trace: " . it_debug::backtrace()); diff --git a/test/exec.t b/test/exec.t index 293ad61..5f78482 100755 --- a/test/exec.t +++ b/test/exec.t @@ -62,7 +62,8 @@ is( "... short option without dashes" ); -foreach (["", "C", "de_CH", "de_CH.utf8"] as $locale) { +foreach (["", "C", "de_CH", "de_CH.utf8"] as $locale) +{ setlocale(LC_ALL, $locale); $arg = "preüpost"; if (it::match('utf8', $locale)) diff --git a/test/getopt.t b/test/getopt.t index 48f7d9d..77e52ae 100755 --- a/test/getopt.t +++ b/test/getopt.t @@ -18,7 +18,8 @@ function getopt_ok($argv, $exp, $name) return is($got, $exp, $name); } -foreach (["" => "blah gnaber", " (umlaute)" => "pre üäpost"] as $variant => $testarg) { +foreach (["" => "blah gnaber", " (umlaute)" => "pre üäpost"] as $variant => $testarg) +{ $exp = ['args' => [], 'positional' => $testarg, 'argument' => $testarg, 'default' => 'defäult']; getopt_ok([$testarg, '-a', $testarg], $exp, "Short version" . $variant); getopt_ok([$testarg, '--argument', $testarg], $exp, "Long version with space" . $variant); diff --git a/test/it_html.t b/test/it_html.t index a43f674..aa734c0 100755 --- a/test/it_html.t +++ b/test/it_html.t @@ -253,9 +253,8 @@ is( 'empty tags removal' ); -foreach (json_decode(it::file_get_contents(dirname($argv[0]) . '/U_tests.json'), true) as $test) { +foreach (json_decode(it::file_get_contents(dirname($argv[0]) . '/U_tests.json'), true) as $test) is(U(...$test['args']), $test['exp'], $test['name']); -} is(it_html::entity_decode("ä"), "ä"); is(it_html::entity_decode("J"), "J"); diff --git a/test/it_url.testserver.php b/test/it_url.testserver.php index f02b316..2d1bca4 100644 --- a/test/it_url.testserver.php +++ b/test/it_url.testserver.php @@ -52,7 +52,8 @@ switch ($_SERVER['PHP_SELF']) break; case "/slow_response": - for ($i = 0; $i < 6; $i++) { + for ($i = 0; $i < 6; $i++) + { sleep(1); echo "Testserver slow output $i\n"; } diff --git a/test/it_url_server.php b/test/it_url_server.php index d8049b1..b113be1 100644 --- a/test/it_url_server.php +++ b/test/it_url_server.php @@ -14,13 +14,15 @@ register_shutdown_function( usleep(100000); stream_set_blocking($pipes[2], 0); -function server_output() { +function server_output() +{ $result = array(); while (($result[] = trim(fgets($GLOBALS['pipes'][2])))) {}; return array_filter($result); } -function handle_server($args) { +function handle_server($args) +{ $res = true; foreach ((array)$args as $arg) $res = $res && $arg; |