/** * Parse a csv file * * @return array */ public function parseCSV() { $finder = new Finder(); $rows = array(); $convert_utf8 = function ($s) { if (!mb_check_encoding($s, 'UTF-8')) { $s = utf8_encode($s); } return $s; }; $finder->files()->in($this->path)->name($this->fileName); foreach ($finder as $file) { $csv = $file; } if (($handle = fopen($csv->getRealPath(), "r")) !== false) { $i = 0; while (($data = fgetcsv($handle, null, ";")) !== false) { $i++; if ($this->ignoreFirstLine && $i == 1) { continue; } $rows[] = array_map($convert_utf8, $data); } fclose($handle); } return $rows; }
private static function validation_default($val, $min_len = 0, $max_len = 60, $regex_opt = '') { if (!is_string($val)) { // validation_exit('Invalid type', gettype($val)); return ""; } if (strlen($val) < $min_len || strlen($val) > $max_len) { validation_exit('Too long value', strlen($val)); return ""; } // Only UTF-8 is supported. // WARNING: This code assumes UTF-8 only script. if (ini_get('default_charset') != 'UTF-8') { // validation_exit('Only UTF-8 is supported', $val); return ""; } if (!mb_check_encoding($val, 'UTF-8')) { // validation_exit('Invalid encoding', $val); return ""; } // Allow only alpha numeric and UTF-8. // UTF-8 encoding: // 0xxxxxxx // 110yyyyx + 10xxxxxx // 1110yyyy + 10yxxxxx + 10xxxxxx // 11110yyy + 10yyxxxx + 10xxxxxx + 10xxxxxx // Since validity of UTF-8 encoding is checked, simply allow \x80-\xFF. if (!mb_ereg('\\A[0-9A-Za-z\\x80-\\xFF' . $regex_opt . ']*\\z', $val)) { // validation_exit('Invalid char', $val); } return $val; }
/** * Check for invalid UTF8 encoding and invalid byte . * * @param string $string Your string. * * @return boolean */ public static function checkUtf8Encoding($string) { if (!mb_check_encoding($string, 'UTF-8') || !$string == mb_convert_encoding(mb_convert_encoding($string, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32')) { return false; } return true; }
public function load($url = null) { if (is_null($url) && is_null($this->url)) { throw new Exception('You must set url to load'); } if (empty($url)) { $url = $this->url; } curl_setopt($this->curl, CURLOPT_URL, $url); $response = curl_exec($this->curl); if (curl_errno($this->curl) !== 0) { throw new \Exception('cURL error: ' . curl_error($this->curl)); } $httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); if ($httpCode != 200) { throw new \Exception('cURL request returned HTTP code ' . $httpCode); } $headersLength = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE); $headers = substr($response, 0, $headersLength); $response = substr($response, $headersLength); $this->url = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL); $this->host = parse_url($this->url, PHP_URL_HOST); $headers = preg_split('/^\\s*$/m', trim($headers)); // get last headers, after all redirects $this->headers = end($headers); $this->html = $response; $this->html = mb_check_encoding($this->html, 'UTF-8') ? $this->html : utf8_encode($this->html); }
/** * Ensures the encoding of the passed string is set to UTF-8. * * @param string $str * @return string utf-8 string */ protected function convertEncoding($str) { if (!mb_check_encoding($str, 'UTF-8')) { return mb_convert_encoding($str, 'UTF-8', 'ISO-8859-1'); } return $str; }
/** * Perform a cURL request * @param string $url */ protected function curl($url) { if ($this->debug) { echo 'cURL request: ' . $url . "\n"; } $ch = curl_init($url); curl_setopt_array($ch, array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => $this->curlFollowLocation, CURLOPT_MAXREDIRS => $this->curlMaxRedirects, CURLOPT_TIMEOUT => $this->curlTimeout, CURLOPT_USERAGENT => $this->curlUserAgent)); $response = curl_exec($ch); if (curl_errno($ch) !== 0) { throw new WappalyzerException('cURL error: ' . curl_error($ch)); } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode != 200) { throw new WappalyzerException('cURL request returned HTTP code ' . $httpCode); } $result = new stdClass(); $result->url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); $result->host = parse_url($result->url, PHP_URL_HOST); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $result->html = substr($response, $headerSize); $result->html = mb_check_encoding($result->html, 'UTF-8') ? $result->html : utf8_encode($result->html); $headers = trim(substr($response, 0, $headerSize)); $headers = preg_split('/^\\s*$/m', $headers); $headers = end($headers); $lines = array_slice(explode("\r\n", $headers), 1); foreach ($lines as $line) { if (strpos(trim($line), ': ') !== false) { list($key, $value) = explode(': ', $line); $result->headers[strtolower($key)] = $value; } } return $result; }
/** * Prepare new text message. * * If $unicode is not provided we will try to detect the * message type. Otherwise set to TRUE if you require * unicode characters. */ public function sendText($to, $from, $message, $unicode = null) { // Making sure strings are UTF-8 encoded if (!is_numeric($from) && !mb_check_encoding($from, 'UTF-8')) { trigger_error('$from needs to be a valid UTF-8 encoded string'); return false; } if (!mb_check_encoding($message, 'UTF-8')) { trigger_error('$message needs to be a valid UTF-8 encoded string'); return false; } if ($unicode === null) { $containsUnicode = max(array_map('ord', str_split($message))) > 127; } else { $containsUnicode = (bool) $unicode; } // Make sure $from is valid $from = $this->validateOriginator($from); // URL Encode $sFrom = urlencode($from); $sMessage = urlencode($message); // Send away! $hPost = ['from' => $sFrom, 'to' => $to, 'message' => $sMessage, 'type' => $containsUnicode ? 'unicode' : 'text']; return $this->sendSmsRequest($hPost); }
protected function prepare_content($content) { $result = $content; if (empty($result)) { return ''; } $encoding = null; $xml_error = new libxml_errors_mgr(); $dom = new DOMDocument(); $dom->validateOnParse = false; $dom->strictErrorChecking = false; if ($dom->loadHTML($content)) { $encoding = $dom->xmlEncoding; } if (empty($encoding)) { $encoding = mb_detect_encoding($content, 'auto', true); } if (!empty($encoding) && !mb_check_encoding($content, 'UTF-8')) { $result = mb_convert_encoding($content, 'UTF-8', $encoding); } // See if we can strip off body tag and anything outside of it. foreach (array('body', 'html') as $tagname) { $regex = str_replace('##', $tagname, "/<##[^>]*>(.+)<\\/##>/is"); if (preg_match($regex, $result, $matches)) { $result = $matches[1]; break; } } return $result; }
/** * @param string|\Closure $input * @param string|null $filename * @param string|null $title * @param (string|string[]|float)[][][] $jsonable * @param (string|string[])[] $metadata * @param string[] $logLevels * @dataProvider dictionaryProvider */ public function testParse($input, string $filename = null, string $title = null, array $jsonable = null, array $metadata = null, array $logLevels = []) { $parser = new GenericDictionaryParser(); $parser->setLogger($this); if ($input instanceof \Closure) { $archive = $input(); $file = new \SplFileInfo($archive->filename); $archive->close(); $dictionary = $parser->parse($file, $filename, $title); } else { $dictionary = $parser->parse($this->generateTempFileObject(mb_check_encoding($input, 'UTF-8') ? $this->stripIndents($input) : $input), $filename, $title); } array_walk_recursive($jsonable, (function (&$field) { if (is_string($field)) { $field = $this->stripIndents($field); } })->bindTo($this)); $this->assertEquals($jsonable, $dictionary->getWords()); array_walk_recursive($metadata, (function (string &$field) { if (is_string($field)) { $field = $this->stripIndents($field); } })->bindTo($this)); $this->assertEquals($metadata, $dictionary->getMetadata()); $this->assertEquals($logLevels, $this->logLevels); }
/** * Class constructor. * * @param mixed $string * @param string $encoding * @throws \InvalidArgumentException */ public function __construct($string, $encoding = self::ENCODING) { if (is_null($string)) { $string = 'null'; } elseif (is_bool($string)) { $string = $string ? 'true' : 'false'; } elseif (is_int($string) || is_float($string)) { $string = (string) $string; } elseif (is_object($string)) { if (!method_exists($string, '__toString')) { throw new \InvalidArgumentException(sprintf('Object of class %s cannot be converted to String', get_class($string))); } $string = (string) $string; } elseif (!is_string($string)) { throw new \InvalidArgumentException('Cannot convert a variable of type ' . gettype($string) . ' to String'); } if (!self::checkEncoding($encoding)) { throw new \InvalidArgumentException('Unsupported encoding: ' . $encoding); } if (!mb_check_encoding($string, $encoding)) { throw new \InvalidArgumentException('String is not encoded in ' . $encoding); } if ($encoding != self::ENCODING) { $string = mb_convert_encoding($string, self::ENCODING, $encoding); } $string = \Normalizer::normalize($string); $this->string = $string; $this->length = mb_strlen($string, self::ENCODING); }
/** * Ckecks a string for valid UTF8 encoding * * @param $string * * @return bool */ public static function isValidUTF8($string) { if (function_exists("mb_check_encoding")) { return mb_check_encoding($string, "UTF-8") ? TRUE : FALSE; } return preg_match("//u", $string) ? TRUE : FALSE; }
/** * @param $identifier * * @return bool */ public static function is_utf8($identifier) { if (mb_check_encoding($identifier, 'UTF-8')) { return true; } return false; }
function json_encode_string($in_str) { if (mb_check_encoding($in_str) != "UTF-8") { $in_str = mb_convert_encoding($in_str, "UTF-8"); } return str_replace('"', '\\"', $in_str); }
function coins($row) { // fmt_info (type) $fmt = "info:ofi/fmt:kev:mtx:"; // 'dissertation' is compatible with the 1.0 spec, but not the 0.1 spec if (!empty($row['thesis'])) { $fmt .= "dissertation"; } elseif (preg_match("/Journal/", $row['type'])) { $fmt .= "journal"; } elseif (preg_match("/Patent/", $row['type'])) { $fmt .= "patent"; } elseif (preg_match("/Book/", $row['type'])) { $fmt .= "book"; } else { $fmt .= "dc"; } $co = contextObject($row); $coins = "ctx_ver=Z39.88-2004" . "&rft_val_fmt=" . urlencode($fmt); foreach ($co as $coKey => $coValue) { // 'urlencode()' differs from 'rawurlencode() (i.e., RFC1738 encoding) // in that spaces are encoded as plus (+) signs $coKey = preg_replace("/au[0-9]*/i", "au", $coKey); // While COinS does not specify encoding, most javascript tools assume that it is UTF-8 // TODO: use function 'detectCharacterEncoding()' instead of the 'mb_*()' functions? // if (($contentTypeCharset == "ISO-8859-1") AND (detectCharacterEncoding($coValue) != "UTF-8")) if (mb_detect_encoding($coValue) != "UTF-8" || !mb_check_encoding($coValue, "UTF-8")) { $coValue = utf8_encode($coValue); } $coins .= "&" . $coKey . "=" . urlencode($coValue); } $coins .= "%26ctx_enc%3Dinfo%3Aofi%2Fenc%3AUTF-8"; $coinsSpan = "<span class=\"Z3988\" title=\"" . $coins . "\"></span>"; return $coinsSpan; }
function scrapeMarketGroup($url) { global $visitedIds; $html = scraperWiki::scrape($url); $html = str_replace("\n", "", $html); preg_match_all("|<a href=\"/importing/61000746/marketgroup/(\\d+?)/\">(.+?)</a>|s", $html, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $groupId = $match[1]; $groupName = html_entity_decode($match[2]); //echo $groupName."\n"; if (!in_array($groupId, $visitedIds)) { $visitedIds[] = $groupId; scrapeMarketGroup("http://goonmetrics.com/importing/61000746/marketgroup/" . $groupId . "/"); } } preg_match_all("|<tr(.*?)>(.*?)<td(.*?)><a href=\"http://games.chruker.dk/eve_online/item.php\\?type_id=(.+?)\" target=\"_blank\">(.*?)<span class=\"dot\" onclick=\"CCPEVE.showMarketDetails\\((.*?)\\)\">(.+?)</span>(.*?)</td>(.*?)<td(.*?)>(.+?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.+?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)<td(.*?)>(.*?)</td>(.*?)</tr>|s", $html, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $item = array("itemId" => trim($match[4]), "name" => trim(mb_check_encoding($match[7], 'UTF-8') ? $match[7] : utf8_encode($match[7])), "weekVol" => trim(mb_check_encoding($match[11], 'UTF-8') ? $match[11] : utf8_encode($match[11])), "k6Stock" => trim(mb_check_encoding($match[17], 'UTF-8') ? $match[17] : utf8_encode($match[17]))); $item['weekVol'] = str_replace(",", "", $item['weekVol']); $item['k6Stock'] = str_replace(",", "", $item['k6Stock']); $saved = false; $delay = 0; while (!$saved && $delay < 600) { try { @scraperwiki::save_sqlite(array('itemId'), $item, 'eve_goonmetrics'); $saved = true; } catch (Exception $e) { sleep(10); $delay++; } } } }
public function apply($value) { if (mb_check_encoding($value, 'UTF-16') && mb_substr_count($value, "") > 0) { $value = mb_convert_encoding($value, 'UTF-8', 'UTF-16'); } return $value; }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof Length) { throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Length'); } if (null === $value || '' === $value) { return; } if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { throw new UnexpectedTypeException($value, 'string'); } $stringValue = (string) $value; $invalidCharset = false; if ('UTF8' === ($charset = strtoupper($constraint->charset))) { $charset = 'UTF-8'; } if ('UTF-8' === $charset) { if (!preg_match('//u', $stringValue)) { $invalidCharset = true; } elseif (function_exists('utf8_decode')) { $length = strlen(utf8_decode($stringValue)); } else { preg_replace('/./u', '', $stringValue, -1, $length); } } elseif (function_exists('mb_strlen')) { if (@mb_check_encoding($stringValue, $constraint->charset)) { $length = mb_strlen($stringValue, $constraint->charset); } else { $invalidCharset = true; } } elseif (function_exists('iconv_strlen')) { $length = @iconv_strlen($stringValue, $constraint->charset); $invalidCharset = false === $length; } else { $length = strlen($stringValue); } if ($invalidCharset) { if ($this->context instanceof ExecutionContextInterface) { $this->context->buildViolation($constraint->charsetMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ charset }}', $constraint->charset)->setInvalidValue($value)->addViolation(); } else { $this->buildViolation($constraint->charsetMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ charset }}', $constraint->charset)->setInvalidValue($value)->addViolation(); } return; } if (null !== $constraint->max && $length > $constraint->max) { if ($this->context instanceof ExecutionContextInterface) { $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ limit }}', $constraint->max)->setInvalidValue($value)->setPlural((int) $constraint->max)->setCode(Length::TOO_LONG_ERROR)->addViolation(); } else { $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ limit }}', $constraint->max)->setInvalidValue($value)->setPlural((int) $constraint->max)->setCode(Length::TOO_LONG_ERROR)->addViolation(); } return; } if (null !== $constraint->min && $length < $constraint->min) { if ($this->context instanceof ExecutionContextInterface) { $this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ limit }}', $constraint->min)->setInvalidValue($value)->setPlural((int) $constraint->min)->setCode(Length::TOO_SHORT_ERROR)->addViolation(); } else { $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->minMessage)->setParameter('{{ value }}', $this->formatValue($stringValue))->setParameter('{{ limit }}', $constraint->min)->setInvalidValue($value)->setPlural((int) $constraint->min)->setCode(Length::TOO_SHORT_ERROR)->addViolation(); } } }
/** * @param string $name * * @return string * * @throws RuntimeException */ function checkName($name) { if ($name == '' || !mb_check_encoding($name, 'UTF-8')) { throw new \RuntimeException('Invalid currency name: ' . $name); } return $name; }
public function correct(string $input) : string { if (!mb_check_encoding($input, 'UTF-8')) { throw new SyntaxException(_('SVGファイルの符号化方式 (文字コード) は UTF-8 でなければなりません。')); } $parser = xml_parser_create_ns(); $isValid = xml_parse_into_struct($parser, $input, $nodes); xml_parser_free($parser); if (!$isValid) { throw new SyntaxException(_('整形式になっていません。')); } $document = new \DOMDocument('1.0', 'UTF-8'); $document->loadXML($input); // ノードの削除 $root = $document->documentElement; foreach ((new \DOMXPath($document))->query(self::BLACKLIST) as $node) { if ($node === $root) { throw new SyntaxException(_('ルート要素がSVG名前空間に属していません。')); } $this->logger->error(sprintf(_('SVG中にノード %s を含めることはできません。'), $node->nodeName)); if ($node->nodeType === XML_ATTRIBUTE_NODE) { $node->ownerElement->removeAttributeNode($node); } elseif ($node->parentNode) { $node->parentNode->removeChild($node); } } return $document->saveXML(); }
/** * Test whether a string is valid UTF-8. * * The function check for invalid byte sequences, overlong encoding but * not for different normalisations. * * This relies internally on the mbstring function mb_check_encoding() * hardcoded to check against UTF-8. Whenever the function is not available * we fallback to a pure PHP implementation. Setting $disableMbstring to * true will skip the use of mb_check_encoding, this is mostly intended for * unit testing our internal implementation. * * @since 1.21 * @note In MediaWiki 1.21, this function did not provide proper UTF-8 validation. * In particular, the pure PHP code path did not in fact check for overlong forms. * Beware of this when backporting code to that version of MediaWiki. * * @param string $value String to check * @param boolean $disableMbstring Whether to use the pure PHP * implementation instead of trying mb_check_encoding. Intended for unit * testing. Default: false * * @return boolean Whether the given $value is a valid UTF-8 encoded string */ static function isUtf8( $value, $disableMbstring = false ) { $value = (string)$value; // If the mbstring extension is loaded, use it. However, before PHP 5.4, values above // U+10FFFF are incorrectly allowed, so we have to check for them separately. if ( !$disableMbstring && function_exists( 'mb_check_encoding' ) ) { static $newPHP; if ( $newPHP === null ) { $newPHP = !mb_check_encoding( "\xf4\x90\x80\x80", 'UTF-8' ); } return mb_check_encoding( $value, 'UTF-8' ) && ( $newPHP || preg_match( "/\xf4[\x90-\xbf]|[\xf5-\xff]/S", $value ) === 0 ); } if ( preg_match( "/[\x80-\xff]/S", $value ) === 0 ) { // String contains only ASCII characters, has to be valid return true; } // PCRE implements repetition using recursion; to avoid a stack overflow (and segfault) // for large input, we check for invalid sequences (<= 5 bytes) rather than valid // sequences, which can be as long as the input string is. Multiple short regexes are // used rather than a single long regex for performance. static $regexes; if ( $regexes === null ) { $cont = "[\x80-\xbf]"; $after = "(?!$cont)"; // "(?:[^\x80-\xbf]|$)" would work here $regexes = array( // Continuation byte at the start "/^$cont/", // ASCII byte followed by a continuation byte "/[\\x00-\x7f]$cont/S", // Illegal byte "/[\xc0\xc1\xf5-\xff]/S", // Invalid 2-byte sequence, or valid one then an extra continuation byte "/[\xc2-\xdf](?!$cont$after)/S", // Invalid 3-byte sequence, or valid one then an extra continuation byte "/\xe0(?![\xa0-\xbf]$cont$after)/", "/[\xe1-\xec\xee\xef](?!$cont{2}$after)/S", "/\xed(?![\x80-\x9f]$cont$after)/", // Invalid 4-byte sequence, or valid one then an extra continuation byte "/\xf0(?![\x90-\xbf]$cont{2}$after)/", "/[\xf1-\xf3](?!$cont{3}$after)/S", "/\xf4(?![\x80-\x8f]$cont{2}$after)/", ); } foreach ( $regexes as $regex ) { if ( preg_match( $regex, $value ) !== 0 ) { return false; } } return true; }
/** * Content-uri action */ public function contentUriAction() { $actual = $this->getSubDomainModel()->findActual(); if (empty($actual)) { $this->getResponse()->setStatusCode(404); return; } $uri = $this->params()->fromRoute('uri'); // prevent "Invalid Encoding Attack" if (!mb_check_encoding($uri, 'UTF-8')) { $this->getResponse()->setStatusCode(404); return; } $structure = $this->getUriModel()->findBySubdomainUri($actual->id, $uri); if (empty($structure)) { $this->paragraphLayout(); $this->getResponse()->setStatusCode(404); return; } if (!$structure->default) { $default = $this->getUriModel()->findDefaultByContentSubdomain($structure->contentId, $actual->id, $structure->locale); if ($default->uri != $structure->uri) { $response = $this->getResponse(); $response->getHeaders()->addHeaderLine('Location', '/' . $default->safeUri); // Permanent redirect: for "old" uris return $response->setStatusCode(301); } } if (!empty($structure->locale)) { $this->getServiceLocator()->get('Locale')->setCurrent($structure->locale); } return $this->forward()->dispatch('Grid\\Paragraph\\Controller\\Render', array('controller' => 'Grid\\Paragraph\\Controller\\Render', 'action' => 'paragraph', 'locale' => (string) $this->locale(), 'paragraphId' => $structure->contentId)); }
public function parse($markdown) { if (!is_string($markdown)) { throw new InvalidArgumentException('markdown', 'type_invalid'); } if (!mb_check_encoding($markdown, 'UTF-8')) { throw new InvalidArgumentException('markdown', 'encoding_invalid'); } // 目的:我们希望用户输入在 code block 以外的尖括号当成普通字符对待,而不是 HTML 标签被处理 // 替换所有尖括号(成对出现时会被当成标签而忽略) $markdown = str_replace('<', '<', $markdown); $markdown = str_replace('>', '>', $markdown); $html = Marked::render($markdown, ['gfm' => true, 'tables' => true, 'breaks' => true, 'sanitize' => false, 'langPrefix' => 'prettyprint lang-']); // 恢复 <code> 中的特殊符号(它们被 escape 了两次,第二次是 markdown 引擎) $pBegin = 0; while (false !== ($pBegin = stripos($html, '<code>', $pBegin))) { $pEnd = strpos($html, '</code>', $pBegin + 6); if ($pEnd === false) { break; } $inner = substr($html, $pBegin + 6, $pEnd - $pBegin - 6); $inner = str_replace('&lt;', '<', $inner); $inner = str_replace('&gt;', '>', $inner); $html = substr_replace($html, $inner, $pBegin + 6, $pEnd - $pBegin - 6); $pBegin += strlen($inner) + 6; } // 由 HTML Purifier 进一步过滤内容确保输入安全 $html = $this->purifier->purify($html); return $html; }
/** * Routine to check if string is valid UTF string * * @static * @param string $string * @return bool */ static function validUTF($string) { if (!mb_check_encoding($string, 'UTF-8') or !($string === mb_convert_encoding(mb_convert_encoding($string, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32'))) { return false; } return true; }
protected function _getDisplayValue($value, $html = false, $date = false) { $type = $this->_getType($value); if ($type === null) { if ($html && $this->is_html) { return ' '; } else { return null; } } elseif ($type === 'number') { return $value; } elseif ($type === 'date') { if ($date) { return date($date, strtotime($value)); } else { return $value; } } elseif ($type === 'string') { if (false === mb_check_encoding($value, 'UTF-8')) { return $value = utf8_encode($value); } else { return $value; } } }
public function uploadFileAction() { $response = array(); $file = $this->request->getFile('file'); if ($file) { $fileName = basename($file['name']); // if file size more than 700 kb if ($file['size'] > 716800) { $response['message'] = $this->trans('files larger than %Size% are not allowed to upload.', array('%Size%' => '700 KB')); } else { if (in_array($file['type'], array('image/png', 'image/jpeg', 'image/gif'))) { // if non-english characters detected if (!mb_check_encoding($fileName, "ASCII")) { $fileName = 'image_' . time() . '.' . preg_replace('/image\\//', '', $file['type']); } try { $uploadFile = $this->getUploadDir() . $this->getAvailableFileName($fileName); move_uploaded_file($file['tmp_name'], $uploadFile); $response = array('fileUrl' => BASE_URL . '/uploads/' . basename($uploadFile), 'fileName' => basename($uploadFile)); } catch (\Exception $ex) { $response['message'] = $this->trans($this->beautifyError($ex->getMessage())); } } else { $response = array('message' => $this->trans('"%file%" - Image format is not supported', array('%file%' => $fileName))); } } } else { $response['message'] = $this->trans('Internal Server Error.'); } echo json_encode($response); exit; }
/** * Force l'encodage d'une chaine en UTF-8 * * @param $string * @return string */ public static function forceUTF8($string) { if (!mb_check_encoding($string, 'UTF-8') || $string !== mb_convert_encoding(mb_convert_encoding($string, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32')) { $string = mb_convert_encoding($string, 'UTF-8'); } return $string; }
/** * @param string $url * @return array|bool */ protected function parse_utf8_url($url) { if (mb_check_encoding($url, "ASCII")) { return parse_url($url); } $regexp = '~^ ( ( (?P<scheme>\\w+):// # with scheme (e.g. http://) | // # without scheme ) (?: (?P<user>[^:]+) : (?P<pass>[^@]+) @ )? (?P<host>[^/?:#]*) (:(?P<port>\\d+))? )? (?P<path>[^?#]*) (\\?(?P<query>[^#]*))? (\\#(?P<fragment>.*))? ~ux'; if (is_string($url) && preg_match($regexp, $url, $matches)) { foreach ($matches as $key => $value) { if (!isset(self::$url_parts[$key]) || empty($value)) { unset($matches[$key]); } } return $matches; } return false; }
/** * @param string $category * @param string|null $subCategory * @throws InvalidArgumentException * @throws UserException */ public function __construct($category, $subCategory = null) { if (!is_string($category)) { throw new InvalidArgumentException('category', 'type_invalid'); } if (!mb_check_encoding($category, 'UTF-8')) { throw new InvalidArgumentException('category', 'encoding_invalid'); } if (!Validator::length(VJ::TAG_MIN, VJ::TAG_MAX)->validate($category)) { throw new UserException('Problem.Tag.invalid_length'); } $keyword = KeywordFilter::isContainGeneric($category); if ($keyword !== false) { throw new UserException('Problem.Tag.name_forbid', ['keyword' => $keyword]); } if (!is_string($subCategory)) { throw new InvalidArgumentException('subCategory', 'type_invalid'); } if (!mb_check_encoding($subCategory, 'UTF-8')) { throw new InvalidArgumentException('subCategory', 'encoding_invalid'); } if (!Validator::length(VJ::TAG_MIN, VJ::TAG_MAX)->validate($subCategory)) { throw new UserException('Problem.Tag.invalid_length'); } $keyword = KeywordFilter::isContainGeneric($subCategory); if ($keyword !== false) { throw new UserException('Problem.Tag.name_forbid', ['keyword' => $keyword]); } $this->category = $category; $this->subCategory = $subCategory; }
public function sendVerify($to, $brand, $from = null, $code_length = null, $lg = null, $disable_call = false) { $post = []; if ($from !== null && !is_numeric($from) && !mb_check_encoding($from, 'UTF-8')) { error_log('$from needs to be a valid UTF-8 encoded string'); return false; } else { $post['sender_id'] = $this->validateOriginator($from); } // Send away! $post['number'] = $to; $post['brand'] = $brand; // optional parameters if (!empty($code_length)) { $post['code_length'] = intval($code_length); } if (!empty($lg)) { $post['lg'] = $lg; } if ($disable_call) { $post['avoid_voice_call'] = true; } $response = $this->sendRequest($post); if ($response === false || empty($response) || !property_exists($response, 'status')) { error_log('[NEXMO] VERIFY Invalid response received from server'); return false; } if ($response->status != 0) { error_log('[NEXMO] VERIFY Error response received from server: ' . $response->error_text); return false; } return $response->request_id; }
/** * is * Is the $input already a valid UTF-8 string? * @access public * @static */ public static function is($input) { if (!is_string($input)) { return $input; } return self::$debug ? mb_check_encoding($input, 'UTF-8') : @mb_check_encoding($input, 'UTF-8'); }