Example #1
2
 public static function clean($var, $charset = NULL)
 {
     if (!$charset) {
         // Use the application character set
         $charset = JsonApiApplication::$charset;
     }
     if (is_array($var) or is_object($var)) {
         foreach ($var as $key => $val) {
             // Recursion!
             $var[UTF8::clean($key)] = UTF8::clean($val);
         }
     } elseif (is_string($var) and $var !== "") {
         // Remove control characters
         $var = UTF8::strip_ascii_ctrl($var);
         if (!UTF8::is_ascii($var)) {
             // Temporarily save the mb_substitute_character() value into a variable
             $mb_substitute_character = mb_substitute_character();
             // Disable substituting illegal characters with the default '?' character
             mb_substitute_character("none");
             // convert encoding, this is expensive, used when $var is not ASCII
             $var = mb_convert_encoding($var, $charset, $charset);
             // Reset mb_substitute_character() value back to the original setting
             mb_substitute_character($mb_substitute_character);
         }
     }
     return $var;
 }
Example #2
0
 /**
  * Print the message
  */
 function text($indexName, $extra = FALSE)
 {
     include_once "MISC.php";
     include_once "../UTF8.php";
     $utf8 = new UTF8();
     $arrays = $this->loadArrays();
     $string = $arrays[$indexName];
     $string = $extra ? preg_replace("/###/", $utf8->smartUtf8_decode($extra), $string) : preg_replace("/###/", "", $string);
     return MISC::p($utf8->encodeUtf8($string), "success", "center");
 }
Example #3
0
 /**
  * Before action
  */
 public function before()
 {
     parent::before();
     // Получаем статус ошибки
     $status = (int) $this->request->action();
     // Если вызов из строки браузера (http://example.com/error/500)
     if (Request::$initial === Request::$current) {
         $status = 404;
         $this->response->status($status);
         $this->request->action($status);
     } else {
         // Если кода ошибки нет в списке обрабатываемых
         if (!in_array($status, [403, 404, 500, 503])) {
             $status = 404;
             $this->response->status($status);
             $this->request->action($status);
         } else {
             $this->response->status($status);
             $message = $this->request->param('message');
             // Если стандартное сообщение 404
             if (UTF8::strpos($message, 'Unable to find a route to match the URI') !== false) {
                 // Не будем выводить message
                 //$message = '';
             }
         }
     }
     $this->content = View::factory('errors/' . $status)->bind('message', $message);
 }
Example #4
0
/**
 * UTF8::str_ireplace
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2010 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _str_ireplace($search, $replace, $str, & $count = NULL)
{
	if (UTF8::is_ascii($search) AND UTF8::is_ascii($replace) AND UTF8::is_ascii($str))
		return str_ireplace($search, $replace, $str, $count);

	if (is_array($str))
	{
		foreach ($str as $key => $val)
		{
			$str[$key] = UTF8::str_ireplace($search, $replace, $val, $count);
		}
		return $str;
	}

	if (is_array($search))
	{
		$keys = array_keys($search);

		foreach ($keys as $k)
		{
			if (is_array($replace))
			{
				if (array_key_exists($k, $replace))
				{
					$str = UTF8::str_ireplace($search[$k], $replace[$k], $str, $count);
				}
				else
				{
					$str = UTF8::str_ireplace($search[$k], '', $str, $count);
				}
			}
			else
			{
				$str = UTF8::str_ireplace($search[$k], $replace, $str, $count);
			}
		}
		return $str;
	}

	$search = UTF8::strtolower($search);
	$str_lower = UTF8::strtolower($str);

	$total_matched_strlen = 0;
	$i = 0;

	while (preg_match('/(.*?)'.preg_quote($search, '/').'/s', $str_lower, $matches))
	{
		$matched_strlen = strlen($matches[0]);
		$str_lower = substr($str_lower, $matched_strlen);

		$offset = $total_matched_strlen + strlen($matches[1]) + ($i * (strlen($replace) - 1));
		$str = substr_replace($str, $replace, $offset, strlen($search));

		$total_matched_strlen += $matched_strlen;
		$i++;
	}

	$count += $i;
	return $str;
}
Example #5
0
 protected function _pad_word(&$word, $letter, $length = 4)
 {
     $l = UTF8::strlen($word);
     if ($l < $length) {
         $word = str_repeat($letter, $length - $l) . $word;
     }
 }
Example #6
0
 /**
  * Retrieve a JSON object containing autocomplete suggestions for existing users.
  */
 public function action_tag()
 {
     $string = $this->request->param('string', FALSE);
     $type = $this->request->param('type', 'blog');
     // The user enters a comma-separated list of tags. We only autocomplete the last tag.
     $tags_typed = Tags::explode($string);
     $tag_last = UTF8::strtolower(array_pop($tags_typed));
     $matches = array();
     if (!empty($tag_last)) {
         $query = DB::select('name')->from('tags')->where('name', 'LIKE', $tag_last . '%')->where('type', '=', $type);
         // Do not select already entered terms.
         if (!empty($tags_typed)) {
             $query->where('name', 'NOT IN', $tags_typed);
         }
         $result = $query->limit('10')->execute();
         $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';
         foreach ($result as $tag) {
             $n = $tag['name'];
             // Tag names containing commas or quotes must be wrapped in quotes.
             if (strpos($tag['name'], ',') !== FALSE or strpos($tag['name'], '"') !== FALSE) {
                 $n = '"' . str_replace('"', '""', $tag['name']) . '"';
             } else {
                 $matches[$prefix . $n] = Text::plain($tag['name']);
             }
         }
     }
     $this->response->body(JSON::encode($matches));
 }
Example #7
0
 /**
  * Overload Sprig::update() to save revision change
  * @param bump  whether to bump the version number
  */
 public function update($bump = TRUE)
 {
     Kohana::$log->add(Kohana::DEBUG, 'Executing Versioned_Sprig::update');
     $updated = FALSE;
     foreach ($this->_fields as $field => $object) {
         if ($object instanceof Sprig_Field_Tracked and $this->changed($field)) {
             $this->comment = UTF8::ucwords($object->label) . ' changed from "' . $this->_original[$field] . '" to "' . $this->_changed[$field] . '".';
         }
         if ($object instanceof Sprig_Field_Versioned and $this->changed($field) and $bump) {
             $diff = '';
             if ($this->version != 0) {
                 $diff = Versioned::diff($this->_original[$field], $this->_changed[$field]);
                 $diff = Versioned::clean_array($diff);
                 $diff = serialize($diff);
             }
             $this->version++;
             $revision = Sprig::factory($this->_model . '_revision');
             $revision->values(array('entry' => $this->id, 'version' => $this->version, 'editor' => $this->editor, 'diff' => $diff));
             $revision->comments = $this->comments;
             $revision->create();
             $updated = TRUE;
             $this->comments = array();
         }
     }
     if (!$updated and count($this->comments) > 0) {
         $revision = Sprig::factory($this->_model . '_revision');
         $revision->entry = $this->id;
         $revision->version = $this->version;
         $revision->load();
         $revision->comments = array_merge($revision->comments, $this->comments);
         $revision->update();
     }
     return parent::update();
 }
Example #8
0
 /**
  * Outputs the Captcha image.
  *
  * @param boolean $html HTML output
  * @return mixed
  */
 public function render($html = TRUE)
 {
     // Creates a black image to start from
     $this->image_create(Captcha::$config['background']);
     // Add random white/gray arcs, amount depends on complexity setting
     $count = (Captcha::$config['width'] + Captcha::$config['height']) / 2;
     $count = $count / 5 * min(10, Captcha::$config['complexity']);
     for ($i = 0; $i < $count; $i++) {
         imagesetthickness($this->image, mt_rand(1, 2));
         $color = imagecolorallocatealpha($this->image, 255, 255, 255, mt_rand(0, 120));
         imagearc($this->image, mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(0, 360), mt_rand(0, 360), $color);
     }
     // Use different fonts if available
     $font = Captcha::$config['fontpath'] . Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
     // Draw the character's white shadows
     $size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / UTF8::strlen($this->response));
     $angle = mt_rand(-15 + UTF8::strlen($this->response), 15 - UTF8::strlen($this->response));
     $x = mt_rand(1, Captcha::$config['width'] * 0.9 - $size * UTF8::strlen($this->response));
     $y = (Captcha::$config['height'] - $size) / 2 + $size;
     $color = imagecolorallocate($this->image, 255, 255, 255);
     imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response);
     // Add more shadows for lower complexities
     Captcha::$config['complexity'] < 10 and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font, $this->response);
     Captcha::$config['complexity'] < 8 and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font, $this->response);
     Captcha::$config['complexity'] < 6 and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font, $this->response);
     Captcha::$config['complexity'] < 4 and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font, $this->response);
     Captcha::$config['complexity'] < 2 and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font, $this->response);
     // Finally draw the foreground characters
     $color = imagecolorallocate($this->image, 0, 0, 0);
     imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response);
     // Output
     return $this->image_render($html);
 }
Example #9
0
/**
 * UTF8::str_pad
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2010 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
    if (UTF8::is_ascii($str) and UTF8::is_ascii($pad_str)) {
        return str_pad($str, $final_str_length, $pad_str, $pad_type);
    }
    $str_length = UTF8::strlen($str);
    if ($final_str_length <= 0 or $final_str_length <= $str_length) {
        return $str;
    }
    $pad_str_length = UTF8::strlen($pad_str);
    $pad_length = $final_str_length - $str_length;
    if ($pad_type == STR_PAD_RIGHT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return UTF8::substr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
    }
    if ($pad_type == STR_PAD_LEFT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return UTF8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
    }
    if ($pad_type == STR_PAD_BOTH) {
        $pad_length /= 2;
        $pad_length_left = floor($pad_length);
        $pad_length_right = ceil($pad_length);
        $repeat_left = ceil($pad_length_left / $pad_str_length);
        $repeat_right = ceil($pad_length_right / $pad_str_length);
        $pad_left = UTF8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
        $pad_right = UTF8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
        return $pad_left . $str . $pad_right;
    }
    trigger_error('UTF8::str_pad: Unknown padding type (' . $pad_type . ')', E_USER_ERROR);
}
Example #10
0
 public static function slug($string, $replacement = '-')
 {
     $slug = UTF8::trim(UTF8::strtolower($string));
     $map = array('/ä/' => 'ae', '/ö/' => 'oe', '/ü/' => 'ue', '/щ/' => 'sch', '/ш/' => 'sh', '/ч/' => 'ch', '/ц/' => 'c', '/ю/' => 'yu', '/я/' => 'ya', '/ж/' => 'zh', '/а/' => 'a', '/б/' => 'b', '/в/' => 'v', '/г|ґ/' => 'g', '/д/' => 'd', '/е/' => 'e', '/ё/' => 'yo', '/з/' => 'z', '/и|і/' => 'i', '/й/' => 'y', '/к/' => 'k', '/л/' => 'l', '/м/' => 'm', '/н/' => 'n', '/о/' => 'o', '/п/' => 'p', '/р/' => 'r', '/с/' => 's', '/т/' => 't', '/у/' => 'u', '/ф/' => 'f', '/х/' => 'h', '/ы/' => 'y', '/э/' => 'e', '/є/' => 'ye', '/ї/' => 'yi', '/º|°/' => 0, '/¹/' => 1, '/²/' => 2, '/³/' => 3, '/à|á|å|â|ã|ä|ą|ă|ā|ª/' => 'a', '/@/' => 'at', '/æ/' => 'ae', '/ḃ/' => 'b', '/č|ç|¢|ć|ċ|ĉ|©/' => 'c', '/ď|ḋ|đ/' => 'd', '/€/' => 'euro', '/è|é|ê|ě|ẽ|ë|ę|ē|ė|ĕ/' => 'e', '/ƒ|ḟ/' => 'f', '/ģ|ğ|ĝ|ġ/' => 'g', '/ĥ|ħ/' => 'h', '/Ì|Í|Î|Ï/' => 'I', '/ì|í|î|ï|ĩ|ī|į|ı/' => 'i', '/ĵ/' => 'j', '/ķ/' => 'k', '/ł|ľ|ĺ|ļ/' => 'l', '/Ł/' => 'L', '/ṁ/' => 'm', '/ñ|ń|ņ|ň/' => 'n', '/ò|ó|ô|ø|õ|ö|ō|ð|ơ|ő/' => 'o', '/œ/' => 'oe', '/ṗ/' => 'p', '/ř|ŗ|ŕ|®/' => 'r', '/š|ś|ṡ|ş|ș/' => 's', '/ť|ț|ŧ|ţ|ṫ/' => 't', '/þ/' => 'th', '/ß/' => 'ss', '/™/' => 'tm', '/ù|ú|ů|û|ü|ū|ũ|ű|ŭ|ư|ų|µ/' => 'u', '/ẃ|ẅ|ẁ|ŵ/' => 'w', '/×/' => 'x', '/ÿ|ý|ỳ|ŷ|¥/' => 'y', '/Ž|Ż|Ź/' => 'Z', '/ž|ż|ź/' => 'z', '/\\s+/' => $replacement);
     $slug = preg_replace(array_keys($map), array_values($map), $slug);
     return URL::title($slug, $replacement);
 }
Example #11
0
function RefererURLBeautifier_handler($target, $mother)
{
    $keyword = false;
    if (preg_match('/\\W(q|query|k|keyword|search|stext|nlia|aqa|wd)(?:=|%3D)([^&]+)/i', $mother['url'], $matches)) {
        $keyword = urldecode(rawurldecode($matches[2]));
    } else {
        if (strpos($mother['host'], 'images.google.') !== false && preg_match('/%3Fsearch%3D([^&]+)/i', $mother['url'], $matches)) {
            $keyword = urldecode(rawurldecode($matches[1]));
        } else {
            if (strpos($mother['host'], 'yahoo.') !== false && preg_match('/\\Wp=([^&]+)/i', $mother['url'], $matches)) {
                $keyword = urldecode(rawurldecode($matches[1]));
            } else {
                if (preg_match('@/search/(?:\\w+/)*([^/?]+)@i', $mother['url'], $matches)) {
                    $keyword = urldecode(rawurldecode($matches[1]));
                }
            }
        }
    }
    if (!UTF8::validate($keyword)) {
        $keyword = UTF8::correct(UTF8::bring($keyword));
    }
    $keyword = UTF16UrlDecode($keyword);
    $url = rawurldecode(substr($mother['url'], 7));
    if (!UTF8::validate($url)) {
        $url = UTF8::correct(UTF8::bring($url));
    }
    //return '<img src="http://'.$mother['host'].'/favicon.ico" width="16" height="16" alt="Favicon" onerror="this.parentNode.removeChild(this)" style="vertical-align: middle"/> ' . (($keyword) ? '<span style="font-weight: bold; color: #594">['.htmlspecialchars($keyword).']</span> ' . UTF8::lessenAsEm($url, 65 - UTF8::lengthAsEm($keyword)) : UTF8::lessenAsEm($url, 65));
    return $keyword ? '<span style="font-weight: bold; color: #594">[' . htmlspecialchars($keyword) . ']</span> ' . htmlspecialchars(UTF8::lessenAsEm($url, 70 - UTF8::lengthAsEm($keyword))) : htmlspecialchars(UTF8::lessenAsEm($url, 70));
}
Example #12
0
File: user.php Project: anqh/core
 /**
  * Magic setter
  *
  * @param  string  $key
  * @param  mixed   $value
  */
 public function __set($key, $value)
 {
     switch ($key) {
         // Date of birth
         case 'dob':
             $value = Date::format(Date::DATE_SQL, $value);
             break;
             // Always lowercase e-mail
         // Always lowercase e-mail
         case 'email':
             $value = UTF8::strtolower($value);
             break;
             // Hash password
         // Hash password
         case 'password':
             $visitor = Visitor::instance();
             $value = $visitor->hash_password($value);
             break;
             // Set cleaned username when setting username
         // Set cleaned username when setting username
         case 'username':
             $this->username_clean = Text::clean($value);
             break;
     }
     parent::__set($key, $value);
 }
Example #13
0
/**
 * UTF8::strlen
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2011 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strlen($str)
{
    if (UTF8::is_ascii($str)) {
        return strlen($str);
    }
    return strlen(utf8_decode($str));
}
Example #14
0
File: trim.php Project: azuya/Wi3
/**
 * UTF8::trim
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2010 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _trim($str, $charlist = NULL)
{
    if ($charlist === NULL) {
        return trim($str);
    }
    return UTF8::ltrim(UTF8::rtrim($str, $charlist), $charlist);
}
 public function utf8_to_ascii_test()
 {
     $this->assert_equal("Te glossa mou edosan ellenike", UTF8::transliterate_to_ascii("Τη γλώσσα μου έδωσαν ελληνική"));
     $this->assert_equal("Na bierieghu pustynnykh voln", UTF8::transliterate_to_ascii("На берегу пустынных волн"));
     $this->assert_equal("vepxis tqaosani shot`a rust`aveli", UTF8::transliterate_to_ascii("ვეპხის ტყაოსანი შოთა რუსთაველი"));
     $this->assert_equal("WoNengTunXiaBoLiErBuShangShenTi", UTF8::transliterate_to_ascii("我能吞下玻璃而不伤身体"));
 }
Example #16
0
 /**
  * Updates or Creates the record depending on loaded()
  *
  * @param   Validation  $validation  Validation object
  * @return  ORM
  *
  * @uses    User::active_user
  * @uses    ACL::check
  * @uses    Text::limit_words
  * @uses    Text::markup
  * @uses    Request::$client_ip
  */
 public function save(Validation $validation = NULL)
 {
     // Set some defaults
     $this->updated = time();
     $this->format = empty($this->format) ? Kohana::$config->load('inputfilter.default_format', 1) : $this->format;
     $this->author = empty($this->author) ? User::active_user()->id : $this->author;
     if (!$this->loaded()) {
         // New comment
         $this->created = $this->updated;
         $this->hostname = substr(Request::$client_ip, 0, 32);
         //set hostname only if its new comment.
         if (empty($this->status)) {
             $this->status = ACL::check('skip comment approval') ? 'publish' : 'draft';
         }
     }
     // Validate the comment's title. If not specified, extract from comment body.
     if (trim($this->title) == '' and !empty($this->body)) {
         // The body may be in any format, so:
         // 1) Filter it into HTML
         // 2) Strip out all HTML tags
         // 3) Convert entities back to plain-text.
         $this->title = Text::limit_words(trim(UTF8::clean(strip_tags(Text::markup($this->body, $this->format)))), 10, '');
         // Edge cases where the comment body is populated only by HTML tags will
         // require a default subject.
         if ($this->title == '') {
             $this->title = __('(No subject)');
         }
     }
     parent::save($validation);
     return $this;
 }
Example #17
0
 public function snippet($text = '')
 {
     $description = '';
     $chars = array('.', '!', '?', ':', '"');
     if (!empty($text)) {
         $text = strip_tags($text);
         $arr = explode(' ', $text);
         foreach ($arr as $k => $v) {
             if (!empty($v)) {
                 $countdescription = UTF8::strlen($description);
                 $countword = UTF8::strlen($v);
                 if ($countdescription - 1 + $countword > 140) {
                     break;
                 } else {
                     $description .= $v . ' ';
                 }
             }
         }
         $description = rtrim($description);
         if (!empty($description)) {
             $lastchar = $description[UTF8::strlen($description) - 1];
             if ($lastchar == ',') {
                 $description = UTF8::substr($description, 0, UTF8::strlen($description) - 1);
             }
             if (!in_array($lastchar, $chars)) {
                 $description .= '...';
             }
         }
     }
     $this->description = $description;
     return $this;
 }
Example #18
0
 public function action_ent()
 {
     $answers = Security::xss_clean(Arr::get($_POST, 'answers', ''));
     $answers = UTF8::substr($answers, 0, UTF8::strlen($answers) - 1);
     $list = explode(',', $answers);
     $total = 0;
     $right = 0;
     $points = array();
     foreach ($list as $item) {
         $total++;
         $e = explode('.', $item);
         $quest = ORM::factory('Ent_Quest', (int) $e[0]);
         if ($quest->loaded()) {
             $variant = ORM::factory('Quest_Variant')->where('quest_id', '=', $quest->id)->and_where('id', '=', (int) $e[1])->and_where('right', '=', '1')->find();
             if ($variant->loaded()) {
                 $right++;
                 $points[] = array('quest' => $quest->id, 'right' => 1);
             } else {
                 $points[] = array('quest' => $quest->id, 'right' => 0);
             }
         }
     }
     $data = array('total' => $total, 'right' => $right, 'points' => $points);
     $this->response->body(json_encode($data));
 }
Example #19
0
/**
 * UTF8::str_pad
 *
 * @package        Kohana
 * @author         Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
    if (UTF8::is_ascii($str) and UTF8::is_ascii($pad_str)) {
        return str_pad($str, $final_str_length, $pad_str, $pad_type);
    }
    $str_length = UTF8::strlen($str);
    if ($final_str_length <= 0 or $final_str_length <= $str_length) {
        return $str;
    }
    $pad_str_length = UTF8::strlen($pad_str);
    $pad_length = $final_str_length - $str_length;
    if ($pad_type == STR_PAD_RIGHT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return UTF8::substr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
    }
    if ($pad_type == STR_PAD_LEFT) {
        $repeat = ceil($pad_length / $pad_str_length);
        return UTF8::substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
    }
    if ($pad_type == STR_PAD_BOTH) {
        $pad_length /= 2;
        $pad_length_left = floor($pad_length);
        $pad_length_right = ceil($pad_length);
        $repeat_left = ceil($pad_length_left / $pad_str_length);
        $repeat_right = ceil($pad_length_right / $pad_str_length);
        $pad_left = UTF8::substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
        $pad_right = UTF8::substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
        return $pad_left . $str . $pad_right;
    }
    throw new UTF8_Exception("UTF8::str_pad: Unknown padding type (:pad_type)", [':pad_type' => $pad_type]);
}
Example #20
0
 /**
  * Returns a string with all spaces converted to underscores (by default), accented
  * characters converted to non-accented characters, and non word characters removed.
  *
  * @static
  *
  * @param        $string      the string you want to slug
  * @param string $replacement will replace keys in map
  * @param bool $tolower все в нижний регистр
  *
  * @return mixed
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
  */
 public static function slug($string, $replacement = '-', $tolower = false)
 {
     $string = $tolower ? UTF8::strtolower($string) : $string;
     $quoted_replacement = preg_quote($replacement, '/');
     $merge = ['/[^\\s\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nd}]/mu' => ' ', '/\\s+/' => $replacement, sprintf('/^[%s]+|[%s]+$/', $quoted_replacement, $quoted_replacement) => ''];
     $map = self::$_transliteration + $merge;
     return preg_replace(array_keys($map), array_values($map), $string);
 }
Example #21
0
/**
 * Smarty string_format modifier plugin
 *
 * Type:     modifier<br>
 * Name:     shorter<br>
 * Purpose:  shorter strings to lenght
 * @link http://smarty.php.net/manual/en/language.modifier.string.format.php
 *          string_format (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @param string
 * @param string
 * @return string
 */
function smarty_modifier_shorter($string, $lenght = 30)
{
    return UTF8::substr($string, 0, $lenght);
    if (strlen($string) > $lenght) {
        return ParseHTMLText::shortenText($string, $lenght);
    }
    return $string;
}
Example #22
0
/**
 * UTF8::ucfirst
 *
 * @package        Kohana
 * @author         Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _ucfirst($str)
{
    if (UTF8::is_ascii($str)) {
        return ucfirst($str);
    }
    preg_match('/^(.?)(.*)$/us', $str, $matches);
    return UTF8::strtoupper($matches[1]) . $matches[2];
}
Example #23
0
/**
 * UTF8::strrev
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2011 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strrev($str)
{
    if (UTF8::is_ascii($str)) {
        return strrev($str);
    }
    preg_match_all('/./us', $str, $matches);
    return implode('', array_reverse($matches[0]));
}
Example #24
0
 /**
  * Replaces special/accented UTF-8 characters by ASCII-7 'equivalents'.
  *
  * @param   string   string to transliterate
  * @param   integer  -1 lowercase only, +1 uppercase only, 0 both cases
  * @return  string
  */
 public static function transliterate_to_ascii($str, $case = 0)
 {
     static $UTF8_SPECIAL_CHARS = NULL;
     if ($UTF8_SPECIAL_CHARS === null) {
         $UTF8_SPECIAL_CHARS = array('⁰' => '0', '₀' => '0', '¹' => '1', 'ˡ' => 'l', '₁' => '1', '²' => '2', '₂' => '2', '³' => '3', '₃' => '3', '⁴' => '4', '₄' => '4', '⁵' => '5', '₅' => '5', '⁶' => '6', '₆' => '6', '⁷' => '7', '₇' => '7', '⁸' => '8', '₈' => '8', '⁹' => '9', '₉' => '9', '¼' => '1/4', '½' => '1/2', '¾' => '3/4', '⅓' => '1/3', '⅔' => '2/3', '⅕' => '1/5', '⅖' => '2/5', '⅗' => '3/5', '⅘' => '4/5', '⅙' => '1/6', '⅚' => '5/6', '⅛' => '1/8', '⅜' => '3/8', '⅝' => '5/8', '⅞' => '7/8', '⅟' => '1/', '⁺' => '+', '₊' => '+', '⁻' => '-', '₋' => '-', '⁼' => '=', '₌' => '=', '⁽' => '(', '₍' => '(', '⁾' => ')', '₎' => ')', 'ª' => 'a', '@' => 'a', '€' => 'e', 'ⁿ' => 'n', '°' => 'o', 'º' => 'o', '¤' => 'o', 'ˣ' => 'x', 'ʸ' => 'y', '$' => 'S', '©' => '(c)', '℠' => 'SM', '℡' => 'TEL', '™' => 'TM', 'ä' => 'ae', 'Ä' => 'Ae', 'ö' => 'oe', 'Ö' => 'Oe', 'ü' => 'ue', 'Ü' => 'eE', 'å' => 'aa', 'Å' => 'Aa');
     }
     $str = str_replace(array_keys($UTF8_SPECIAL_CHARS), array_values($UTF8_SPECIAL_CHARS), $str);
     return UTF8::transliterate_to_ascii($str, $case);
 }
Example #25
0
/**
 * UTF8::ucwords
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _ucwords($str)
{
    if (UTF8::is_ascii($str)) {
        return ucwords($str);
    }
    // [\x0c\x09\x0b\x0a\x0d\x20] matches form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns.
    // This corresponds to the definition of a 'word' defined at http://php.net/ucwords
    return preg_replace('/(?<=^|[\\x0c\\x09\\x0b\\x0a\\x0d\\x20])[^\\x0c\\x09\\x0b\\x0a\\x0d\\x20]/ue', 'UTF8::strtoupper(\'$0\')', $str);
}
/**
 * UTF8::wordwrap
 * Taken and adapted form Zend Framework by Ivan Tcholakov, 2015.
 *
 * @param       string          The input string.
 * @param       int             The number of characters at which the string will be wrapped.
 * @param       string          The line is broken using the optional break parameter.
 * @param       bool            If the cut is set to TRUE, the string is always wrapped at or before the specified width.
 * @return      string|false
 * @license     @license        http://framework.zend.com/license/new-bsd New BSD License
 */
function _wordwrap($string, $width = 75, $break = "\n", $cut = false)
{
    $string = @(string) $string;
    if ($string === '') {
        return '';
    }
    $break = @(string) $break;
    if ($break === '') {
        trigger_error('UTF8::wordwrap(): Break string cannot be empty.', E_USER_WARNING);
        return false;
    }
    $width = (int) $width;
    if ($width === 0 && $cut) {
        trigger_error('UTF8::wordwrap(): Cannot force cut when width is zero.', E_USER_WARNING);
        return false;
    }
    $string_length = UTF8::strlen($string);
    $break_length = UTF8::strlen($break);
    $result = '';
    $last_start = 0;
    $last_space = 0;
    for ($current = 0; $current < $string_length; $current++) {
        $char = UTF8::substr($string, $current, 1);
        $possible_break = $char;
        if ($break_length !== 1) {
            $possible_break = UTF8::substr($string, $current, $break_length);
        }
        if ($possible_break === $break) {
            $result .= UTF8::substr($string, $last_start, $current - $last_start + $break_length);
            $current += $break_length - 1;
            $last_start = $last_space = $current + 1;
            continue;
        }
        if ($char === ' ') {
            if ($current - $last_start >= $width) {
                $result .= UTF8::substr($string, $last_start, $current - $last_start) . $break;
                $last_start = $current + 1;
            }
            $last_space = $current;
            continue;
        }
        if ($current - $last_start >= $width && $cut && $last_start >= $last_space) {
            $result .= UTF8::substr($string, $last_start, $current - $last_start) . $break;
            $last_start = $last_space = $current;
            continue;
        }
        if ($current - $last_start >= $width && $last_start < $last_space) {
            $result .= UTF8::substr($string, $last_start, $last_space - $last_start) . $break;
            $last_start = $last_space = $last_space + 1;
            continue;
        }
    }
    if ($last_start !== $current) {
        $result .= UTF8::substr($string, $last_start, $current - $last_start);
    }
    return $result;
}
 public function exact_length($str, $val)
 {
     if (!is_numeric($val)) {
         return false;
     } else {
         $val = (int) $val;
     }
     return IS_UTF8_CHARSET ? UTF8::strlen($str) === $val : strlen($str) === $val;
 }
/**
 * UTF8::strcasecmp
 *
 * @package    JsonApiApplication
 * @author     JsonApiApplication Team
 * @copyright  (c) 2007-2012 JsonApiApplication Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _strcasecmp($str1, $str2)
{
    if (UTF8::is_ascii($str1) and UTF8::is_ascii($str2)) {
        return strcasecmp($str1, $str2);
    }
    $str1 = UTF8::strtolower($str1);
    $str2 = UTF8::strtolower($str2);
    return strcmp($str1, $str2);
}
Example #29
0
 public function clean($replacement = '#', $replace_partial_words = FALSE)
 {
     $regex = $this->get_pattern($replace_partial_words);
     if (UTF8::strlen($replacement) == 1) {
         $regex .= 'e';
         return preg_replace($regex, 'str_repeat($replacement, UTF8::strlen(\'$1\'))', $this->string);
     }
     return preg_replace($regex, $replacement, $this->string);
 }
Example #30
0
/**
 * UTF8::rtrim
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2012 Kohana Team
 * @copyright  (c) 2005 Harry Fuecks
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
 */
function _rtrim($str, $charlist = NULL)
{
    if ($charlist === NULL) {
        return rtrim($str);
    }
    if (UTF8::is_ascii($charlist)) {
        return rtrim($str, $charlist);
    }
    $charlist = preg_replace('#[-\\[\\]:\\\\^/]#', '\\\\$0', $charlist);
    return preg_replace('/[' . $charlist . ']++$/uD', '', $str);
}