1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-30 09:39:51 +02:00

Improve URL and email detection

This commit is contained in:
Peter Knut
2024-08-14 00:33:16 +02:00
parent 8a70474651
commit de7dd4b64f
5 changed files with 27 additions and 14 deletions

View File

@@ -293,7 +293,7 @@ class Adminer {
if (preg_match('~json~', $field["type"])) { if (preg_match('~json~', $field["type"])) {
$return = "<code class='jush-js'>$return</code>"; $return = "<code class='jush-js'>$return</code>";
} }
return ($link ? "<a href='" . h($link) . "'" . (is_url($link) ? target_blank() : "") . ">$return</a>" : $return); return ($link ? "<a href='" . h($link) . "'" . (is_web_url($link) ? target_blank() : "") . ">$return</a>" : $return);
} }
/** Value conversion used in select and edit /** Value conversion used in select and edit

View File

@@ -72,7 +72,7 @@ function select($result, $connection2 = null, $orgtables = array(), $limit = 0)
$link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]); $link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
} }
} }
} elseif (is_url($val)) { } elseif (is_web_url($val)) {
$link = $val; $link = $val;
} }
if ($val === null) { if ($val === null) {
@@ -86,7 +86,7 @@ function select($result, $connection2 = null, $orgtables = array(), $limit = 0)
} }
} }
if ($link) { if ($link) {
$val = "<a href='" . h($link) . "'" . (is_url($link) ? target_blank() : '') . ">$val</a>"; $val = "<a href='" . h($link) . "'" . (is_web_url($link) ? target_blank() : '') . ">$val</a>";
} }
echo "<td>$val"; echo "<td>$val";
} }

View File

@@ -1250,7 +1250,7 @@ function select_value($val, $link, $field, $text_length) {
if (is_mail($val)) { if (is_mail($val)) {
$link = "mailto:$val"; $link = "mailto:$val";
} }
if (is_url($val)) { if (is_web_url($val)) {
$link = $val; // IE 11 and all modern browsers hide referrer $link = $val; // IE 11 and all modern browsers hide referrer
} }
} }
@@ -1271,20 +1271,32 @@ function select_value($val, $link, $field, $text_length) {
* @param string * @param string
* @return bool * @return bool
*/ */
function is_mail($email) { function is_mail($value) {
$atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // characters of local-name return is_string($value) && filter_var($value, FILTER_VALIDATE_EMAIL);
$domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
$pattern = "$atom+(\\.$atom+)*@($domain?\\.)+$domain";
return is_string($email) && preg_match("(^$pattern(,\\s*$pattern)*\$)i", $email);
} }
/** Check whether the string is URL address /** Check whether the string is web URL address
* @param string * @param string
* @return bool * @return bool
*/ */
function is_url($string) { function is_web_url($value) {
$domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component //! IDN if (!is_string($value) || !preg_match('~^https?://~i', $value)) {
return preg_match("~^(https?)://($domain?\\.)+$domain(:\\d+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string); //! restrict path, query and fragment characters return false;
}
$components = parse_url($value);
if (!$components) {
return false;
}
// Encode URL path. If path was encoded already, it will be encoded twice, but we are OK with that.
$encodedParts = array_map('urlencode', explode('/', $components['path']));
$url = str_replace($components['path'], implode('/', $encodedParts), $value);
parse_str($components['query'], $params);
$url = str_replace($components['query'], http_build_query($params), $url);
return (bool)filter_var($url, FILTER_VALIDATE_URL);
} }
/** Check if field should be shortened /** Check if field should be shortened

View File

@@ -6,6 +6,7 @@ Adminer 4.9.0-dev:
- Elasticsearch 5: Make unusable driver usable again, move it to plugins. - Elasticsearch 5: Make unusable driver usable again, move it to plugins.
- Add new Elasticsearch 7 driver. - Add new Elasticsearch 7 driver.
- Set saving to file as a default export option. - Set saving to file as a default export option.
- Improve URL and email detection.
- Update composer.json. - Update composer.json.
Adminer 4.8.2 (released 2024-03-16): Adminer 4.8.2 (released 2024-03-16):

View File

@@ -202,7 +202,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
$return = (preg_match('~^(1|t|true|y|yes|on)$~i', $val) ? lang('yes') : lang('no')); $return = (preg_match('~^(1|t|true|y|yes|on)$~i', $val) ? lang('yes') : lang('no'));
} }
if ($link) { if ($link) {
$return = "<a href='$link'" . (is_url($link) ? target_blank() : "") . ">$return</a>"; $return = "<a href='$link'" . (is_web_url($link) ? target_blank() : "") . ">$return</a>";
} }
if (!$link && !like_bool($field) && preg_match(number_type(), $field["type"])) { if (!$link && !like_bool($field) && preg_match(number_type(), $field["type"])) {
$return = "<div class='number'>$return</div>"; // Firefox doesn't support <colgroup> $return = "<div class='number'>$return</div>"; // Firefox doesn't support <colgroup>