/** * Similar function as in_array() but case-insensitive with multibyte support. * * @param string $needle Needle value * @param array $heystack Array to search in * * @return boolean True if found, False if not */ function in_array_nocase($needle, $haystack) { // use much faster method for ascii if (is_ascii($needle)) { foreach ((array) $haystack as $value) { if (strcasecmp($value, $needle) === 0) { return true; } } } else { $needle = mb_strtolower($needle); foreach ((array) $haystack as $value) { if ($needle === mb_strtolower($value)) { return true; } } } return false; }
function expression_recherche($recherche, $options) { // ne calculer qu'une seule fois l'expression par hit // (meme si utilisee dans plusieurs boucles) static $expression = array(); $key = serialize(array($recherche, $options['preg_flags'])); if (isset($expression[$key])) { return $expression[$key]; } $u = $GLOBALS['meta']['pcre_u']; if ($u and strpos($options['preg_flags'], $u) === false) { $options['preg_flags'] .= $u; } include_spip('inc/charsets'); $recherche = trim($recherche); $is_preg = false; if (substr($recherche, 0, 1) == '/' and substr($recherche, -1, 1) == '/') { // c'est une preg $recherche_trans = translitteration($recherche); $preg = $recherche_trans . $options['preg_flags']; $is_preg = true; } else { // s'il y a plusieurs mots il faut les chercher tous : oblige REGEXP, // sauf ceux de moins de 4 lettres (on supprime ainsi 'le', 'les', 'un', // 'une', 'des' ...) // attention : plusieurs mots entre guillemets sont a rechercher tels quels $recherche_trans = $recherche_mod = $recherche; // les expressions entre " " sont un mot a chercher tel quel // -> on remplace les espaces par un \x1 et on enleve les guillemets if (preg_match(',["][^"]+["],Uims', $recherche_mod, $matches)) { foreach ($matches as $match) { $word = preg_replace(",\\s+,Uims", "", $match); $word = trim($word, '"'); $recherche_mod = str_replace($match, $word, $recherche_mod); } } if (preg_match(",\\s+," . $u, $recherche_mod)) { $is_preg = true; $recherche_inter = '|'; $recherche_mots = explode(' ', $recherche_mod); $min_long = defined('_RECHERCHE_MIN_CAR') ? _RECHERCHE_MIN_CAR : 4; foreach ($recherche_mots as $mot) { if (strlen($mot) >= $min_long) { // echapper les caracteres de regexp qui sont eventuellement dans la recherche $recherche_inter .= preg_quote($mot) . ' '; } } $recherche_inter = str_replace("", '\\s', $recherche_inter); // mais on cherche quand même l'expression complète, même si elle // comporte des mots de moins de quatre lettres $recherche = rtrim(preg_quote($recherche) . preg_replace(',\\s+,' . $u, '|', $recherche_inter), '|'); $recherche_trans = translitteration($recherche); } $preg = '/' . str_replace('/', '\\/', $recherche_trans) . '/' . $options['preg_flags']; } // Si la chaine est inactive, on va utiliser LIKE pour aller plus vite // ou si l'expression reguliere est invalide if (!$is_preg or @preg_match($preg, '') === FALSE) { $methode = 'LIKE'; $u = $GLOBALS['meta']['pcre_u']; // echapper les % et _ $q = str_replace(array('%', '_'), array('\\%', '\\_'), trim($recherche)); // eviter les parentheses et autres caractères qui interferent avec pcre par la suite (dans le preg_match_all) s'il y a des reponses $recherche = preg_quote($recherche); $recherche_trans = translitteration($recherche); $recherche_mod = $recherche_trans; // les expressions entre " " sont un mot a chercher tel quel // -> on remplace les espaces par un _ et on enleve les guillemets // corriger le like dans le $q if (preg_match(',["][^"]+["],Uims', $q, $matches)) { foreach ($matches as $match) { $word = preg_replace(",\\s+,Uims", "_", $match); $word = trim($word, '"'); $q = str_replace($match, $word, $q); } } // corriger la regexp if (preg_match(',["][^"]+["],Uims', $recherche_mod, $matches)) { foreach ($matches as $match) { $word = preg_replace(",\\s+,Uims", "[\\s]", $match); $word = trim($word, '"'); $recherche_mod = str_replace($match, $word, $recherche_mod); } } $q = sql_quote("%" . preg_replace(",\\s+," . $u, "%", $q) . "%"); $preg = '/' . preg_replace(",\\s+," . $u, ".+", trim($recherche_mod)) . '/' . $options['preg_flags']; } else { $methode = 'REGEXP'; $q = sql_quote(trim($recherche, '/')); } // tous les caracteres transliterables de $q sont remplaces par un joker // permet de matcher en SQL meme si on est sensible aux accents (SQLite) $q_t = $q; for ($i = 0; $i < spip_strlen($q); $i++) { $char = spip_substr($q, $i, 1); if (!is_ascii($char) and $char_t = translitteration($char) and $char_t !== $char) { $q_t = str_replace($char, $is_preg ? "." : "_", $q_t); } } $q = $q_t; // fix : SQLite 3 est sensible aux accents, on jokerise les caracteres // les plus frequents qui peuvent etre accentues // (oui c'est tres dicustable...) if (isset($GLOBALS['connexions'][$options['serveur'] ? $options['serveur'] : 0]['type']) and strncmp($GLOBALS['connexions'][$options['serveur'] ? $options['serveur'] : 0]['type'], 'sqlite', 6) == 0) { $q_t = strtr($q, "aeuioc", $is_preg ? "......" : "______"); // si il reste au moins un char significatif... if (preg_match(",[^'%_.],", $q_t)) { $q = $q_t; } } return $expression[$key] = array($methode, $q, $preg); }
/** * protected search method * * @param string $folder Folder name * @param string $criteria Search criteria * @param string $charset Charset * @param string $sort_field Sorting field * * @return rcube_result_index|rcube_result_thread Search results (UIDs) * @see rcube_imap::search() */ protected function search_index($folder, $criteria = 'ALL', $charset = NULL, $sort_field = NULL) { if (!$this->check_connection()) { if ($this->threading) { return new rcube_result_thread(); } else { return new rcube_result_index(); } } if ($this->options['skip_deleted'] && !preg_match('/UNDELETED/', $criteria)) { $criteria = 'UNDELETED ' . $criteria; } // unset CHARSET if criteria string is ASCII, this way // SEARCH won't be re-sent after "unsupported charset" response if ($charset && $charset != 'US-ASCII' && is_ascii($criteria)) { $charset = 'US-ASCII'; } if ($this->threading) { $threads = $this->conn->thread($folder, $this->threading, $criteria, true, $charset); // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, // but I've seen that Courier doesn't support UTF-8) if ($threads->is_error() && $charset && $charset != 'US-ASCII') { $threads = $this->conn->thread($folder, $this->threading, self::convert_criteria($criteria, $charset), true, 'US-ASCII'); } return $threads; } if ($sort_field && $this->get_capability('SORT')) { $charset = $charset ? $charset : $this->default_charset; $messages = $this->conn->sort($folder, $sort_field, $criteria, true, $charset); // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, // but I've seen Courier with disabled UTF-8 support) if ($messages->is_error() && $charset && $charset != 'US-ASCII') { $messages = $this->conn->sort($folder, $sort_field, self::convert_criteria($criteria, $charset), true, 'US-ASCII'); } if (!$messages->is_error()) { $this->search_sorted = true; return $messages; } } $messages = $this->conn->search($folder, ($charset && $charset != 'US-ASCII' ? "CHARSET {$charset} " : '') . $criteria, true); // Error, try with US-ASCII (some servers may support only US-ASCII) if ($messages->is_error() && $charset && $charset != 'US-ASCII') { $messages = $this->conn->search($folder, self::convert_criteria($criteria, $charset), true); } $this->search_sorted = false; return $messages; }
function encode($string, $to = 'UTF-8', $from = 'UTF-8') { if ($to == 'UTF-8' and is_ascii($string)) { return $string; } return @iconv($from, $to . '//TRANSLIT//IGNORE', $string); }
function make_record($zone, $input) { global $defaults; $name = isset($input['name']) ? $input['name'] : ''; if ('' == $name) { $name = $zone['name']; } elseif (string_ends_with($name, '.')) { # "absolute" name, shouldn't append zone[name] - but check. $name = substr($name, 0, -1); if (!string_ends_with($name, $zone['name'])) { jtable_respond(null, 'error', "Name {$name} not in zone " . $zone['name']); } } else { if (!string_ends_with($name, $zone['name'])) { $name = $name . '.' . $zone['name']; } } $ttl = (int) (isset($input['ttl']) && $input['ttl'] ? $input['ttl'] : $defaults['ttl']); $type = isset($input['type']) ? $input['type'] : ''; $disabled = (bool) (isset($input['disabled']) && $input['disabled']); $content = isset($input['content']) ? $input['content'] : ''; if ($type === 'TXT') { # empty TXT records are ok, otherwise require surrounding quotes: "..." if (strlen($content) == 1 || substr($content, 0, 1) !== '"' || substr($content, -1) !== '"') { # fix quoting: first escape all \, then all ", then surround with quotes. $content = '"' . str_replace('"', '\\"', str_replace('\\', '\\\\', $content)) . '"'; } } if (!_valid_label($name)) { jtable_respond(null, 'error', "Please only use [a-z0-9_/.-]"); } if (!$type) { jtable_respond(null, 'error', "Require a type"); } if (!is_ascii($content)) { jtable_respond(null, 'error', "Please only use ASCII-characters in your fields"); } return array('disabled' => $disabled, 'type' => $type, 'name' => $name, 'ttl' => $ttl, 'content' => $content); }
/** * Convert a string from one encoding to another encoding * and remove invalid bytes sequences. * * @param string $string to convert * @param string $to encoding you want the string in * @param string $from encoding that string is in * @return string */ function encode($string, $to = 'UTF-8', $from = 'UTF-8') { // ASCII is already valid UTF-8 if ($to == 'UTF-8' and is_ascii($string)) { return $string; } // Convert the string return @iconv($from, $to . '//TRANSLIT//IGNORE', $string); }
require_once "../inc/user.inc"; check_get_args(array()); $auth = post_str("auth", true); $email_addr = strtolower(post_str("email_addr", true)); $old_passwd = post_str("old_passwd", true); $passwd = post_str("passwd"); $passwd2 = post_str("passwd2"); if ($passwd != $passwd2) { error_page(tra("New passwords are different")); } $config = get_config(); $min_passwd_length = parse_config($config, "<min_passwd_length>"); if (!$min_passwd_length) { $min_passwd_length = 6; } if (!is_ascii($passwd)) { error_page(tra("Passwords may only include ASCII characters.")); } if (strlen($passwd) < $min_passwd_length) { error_page(tra("New password is too short: minimum password length is %1 characters.", $min_passwd_length)); } if ($auth) { $user = lookup_user_auth($auth); if (!$user) { error_page(tra("Invalid account key")); } } else { $user = lookup_user_email_addr($email_addr); if (!$user) { error_page(tra("No account with that email address was found")); }
/** * Copy of rcube_imap::search_index() */ protected function search_index() { $criteria = $this->search; $charset = $this->charset; $imap = $this->worker->get_imap(); if (!$imap->connected()) { trigger_error("No IMAP connection for {$this->folder}", E_USER_WARNING); if ($this->threading) { return new rcube_result_thread($this->folder); } else { return new rcube_result_index($this->folder); } } if ($this->worker->options['skip_deleted'] && !preg_match('/UNDELETED/', $criteria)) { $criteria = 'UNDELETED ' . $criteria; } // unset CHARSET if criteria string is ASCII, this way // SEARCH won't be re-sent after "unsupported charset" response if ($charset && $charset != 'US-ASCII' && is_ascii($criteria)) { $charset = 'US-ASCII'; } if ($this->threading) { $threads = $imap->thread($this->folder, $this->threading, $criteria, true, $charset); // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, // but I've seen that Courier doesn't support UTF-8) if ($threads->is_error() && $charset && $charset != 'US-ASCII') { $threads = $imap->thread($this->folder, $this->threading, rcube_imap::convert_criteria($criteria, $charset), true, 'US-ASCII'); } return $threads; } if ($this->sort_field) { $messages = $imap->sort($this->folder, $this->sort_field, $criteria, true, $charset); // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, // but I've seen Courier with disabled UTF-8 support) if ($messages->is_error() && $charset && $charset != 'US-ASCII') { $messages = $imap->sort($this->folder, $this->sort_field, rcube_imap::convert_criteria($criteria, $charset), true, 'US-ASCII'); } } if (!$messages || $messages->is_error()) { $messages = $imap->search($this->folder, ($charset && $charset != 'US-ASCII' ? "CHARSET {$charset} " : '') . $criteria, true); // Error, try with US-ASCII (some servers may support only US-ASCII) if ($messages->is_error() && $charset && $charset != 'US-ASCII') { $messages = $imap->search($this->folder, rcube_imap::convert_criteria($criteria, $charset), true); } } return $messages; }
function convert_table_utf8($f, $table, $champ) { echo "<br /><b>$table</b> "; $s = spip_query("SELECT * FROM $table WHERE $champ LIKE '<CONVERT %'"); // recuperer 'id_article' (encore un truc a faire dans table_objet) preg_match(',^spip_(.*?)s?$,', $table, $r); $id_champ = 'id_'.$r[1]; if ($table == 'spip_petitions') $id_champ = 'id_article'; if ($table == 'spip_groupes_mots') $id_champ = 'id_groupe'; // lire les donnees dans un array while ($t = sql_fetch($s)) { $query = array(); $query_no_convert = ''; $query_extra = ''; $charset_source='AUTO'; foreach ($t as $c => $v) { if ($c == $champ) { preg_match(',^<CONVERT (.*?)>,', $v, $reg); $v = substr($v, strlen($reg[0])); $charset_source = $reg[1]; $query[] = "$c=" . sql_quote($v); } else { if (!is_numeric($v) AND !is_ascii($v)) { // traitement special car donnees serializees if ($c == 'extra') { $query_no_convert .= ", $c=".sql_quote($v); $query_extra = convert_extra($v, $charset_source); } else $query[] = "$c=" . sql_quote($v); } else # pour le backup $query_no_convert .= ", $c=".sql_quote($v); } } $set = join(', ', $query); $where = "$id_champ = ".$t[$id_champ]; // On l'enregistre telle quelle sur le fichier de sauvegarde if ($f) fwrite($f, "UPDATE $table SET $set$query_no_convert" ." WHERE $where;\n" ); // Mais on la transcode // en evitant une double conversion if ($charset_source != 'utf-8') { $query = "UPDATE $table SET " . unicode_to_utf_8(charset2unicode($set, $charset_source)) . $query_extra . " WHERE $where AND $champ LIKE '<CONVERT %'"; #echo $query; spip_query($query); echo '. '; flush(); } } sql_free($s); }
case "19": // TBD $scanResultsStrCopy .= ""; break; case "20": // TBD $scanResultsStrCopy .= ""; break; case "21": // File Read Results if ($dataEntry['status'] == 1 && sizeof($dataEntry['value']) > 0) { foreach ($dataEntry['value'] as $localFilePath) { if (preg_match("#\\(same file\\)#", $localFilePath)) { $localFile = explode(" ", $localFilePath)[0]; if (file_exists($localFile) && is_readable($localFile)) { if (is_ascii($localFile)) { echo "\n[*] File Content for: " . htmlentities($options_to_enable['rFile'], ENT_QUOTES, 'UTF-8') . " \n"; $scanResultsStrCopy .= "\n[*] File Content for: " . $options_to_enable['rFile'] . " \n"; $fh = fopen($localFile, "rb"); while (!feof($fh)) { $line = fgets($fh); echo htmlentities($line, ENT_QUOTES, 'UTF-8'); $scanResultsStrCopy .= $line; } fclose($fh); echo "\n"; $scanResultsStrCopy .= "\n"; } else { echo "\n[*] Non-ASCII File Content for: " . htmlentities($options_to_enable['rFile'], ENT_QUOTES, 'UTF-8') . " \n"; $scanResultsStrCopy .= "\n[*] Non-ASCII File Content for: " . $options_to_enable['rFile'] . " \n"; echo "\n [*] Unable to display as a result...\n";
function transcoder_page($texte, $headers='') { // Si tout est < 128 pas la peine d'aller plus loin if (is_ascii($texte)) { #spip_log('charset: ascii'); return $texte; } // Reconnaitre le BOM utf-8 (0xEFBBBF) if (bom_utf8($texte)) { $charset = 'utf-8'; $texte = substr($texte,3); } // charset precise par le contenu (xml) else if (preg_match( ',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs)) $charset = trim(strtolower($regs[1])); // charset precise par le contenu (html) else if (preg_match( ',<(meta|html|body)[^>]*charset[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs) # eviter #CHARSET des squelettes AND (($tmp = trim(strtolower($regs[2]))) != 'charset')) $charset = $tmp; // charset de la reponse http else if (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs)) $charset = trim(strtolower($regs[1])); else $charset = ''; // normaliser les noms du shif-jis japonais if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset)) $charset = 'shift-jis'; if ($charset) { spip_log("charset: $charset"); } else { // valeur par defaut if (is_utf8($texte)) $charset = 'utf-8'; else $charset = 'iso-8859-1'; spip_log("charset probable: $charset"); } return importer_charset($texte, $charset); }
/** * Convert a string from one encoding to another encoding * and remove invalid bytes sequences. * * @param string $string to convert * @param string $to encoding you want the string in * @param string $from encoding that string is in * @return string */ function encode($string, $to = 'UTF-8', $from = 'UTF-8') { return $to === 'UTF-8' && is_ascii($string) ? $string : @iconv($from, $to . '//TRANSLIT//IGNORE', $string); }
/** * rcube_shared.inc: is_ascii() */ function test_is_ascii() { $result = is_ascii("0123456789"); $this->assertTrue($result, "Valid ASCII (numbers)"); $result = is_ascii("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); $this->assertTrue($result, "Valid ASCII (letters)"); $result = is_ascii(" !\"#\$%&'()*+,-./:;<=>?@[\\^_`{|}~"); $this->assertTrue($result, "Valid ASCII (special characters)"); $result = is_ascii("\t\n\v\f\r" . ""); $this->assertTrue($result, "Valid ASCII (control characters)"); $result = is_ascii("\n", false); $this->assertFalse($result, "Valid ASCII (control characters)"); $result = is_ascii("ż"); $this->assertFalse($result, "Invalid ASCII (UTF-8 character)"); $result = is_ascii("ż", false); $this->assertFalse($result, "Invalid ASCII (UTF-8 character [2])"); }
/** * Convert a string from one encoding to another encoding (Defaults to UTF-8) * * @param string $string to convert * @param string $to_encoding you want the string in * @param string $from_encoding that string is in * @return string */ function encode($string, $to_encoding = 'UTF-8', $from_encoding = 'UTF-8') { // ASCII-7 is valid UTF-8 already if ($to_encoding === 'UTF-8' and is_ascii($string)) { return $string; } if (function_exists('iconv')) { // Disable notices $ER = error_reporting(~E_NOTICE); $string = iconv($from_encoding, $to_encoding . '//TRANSLIT', $string); // Turn notices back on error_reporting($ER); return $string; } else { return mb_convert_encoding($string, $to_encoding, mb_detect_encoding($string, "auto", TRUE)); } }
/** * Reverses a UTF-8 string. * @see http://php.net/strrev * * @author Harry Fuecks <*****@*****.**> * * @param string string to be reversed * @return string */ function strrev($str) { if (is_ascii($str)) { return \strrev($str); } preg_match_all('/./us', $str, $matches); return implode('', array_reverse($matches[0])); }
function draw_text($poster, $y, $font_size, $text) { $draw = new ImagickDraw(); $draw->setFont(is_ascii($text) ? '../font/bernhc.ttf' : '../font/ヒラギノ角ゴ StdN W8.otf'); $draw->setFillOpacity(0.6); $draw->setFontSize($font_size); $draw->annotation(0, $font_size, $text); $fm = $poster->queryFontMetrics($draw, $text, false); $text_im = trample_text_layer($draw, $fm['textWidth'], $fm['textHeight']); if ($text_im->getImageWidth() > 160) { $text_im->scaleImage(160, $text_im->getImageHeight()); } $poster->compositeImage($text_im, imagick::COMPOSITE_OVER, ($poster->getImageWidth() - $text_im->getImageWidth()) / 2, $y); }