00001 <?php
00002 require_once GYRO_CORE_DIR . 'lib/helpers/converters/html.converter.php';
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 class ConverterTwitter implements IConverter {
00018 const EXPAND_LINKS = 1024;
00019
00020 public function encode($value, $params = false) {
00021 $value = String::escape($this->decode($value));
00022
00023 $search = '@(\s#[\S]*)@';
00024 $replace = ' <b>$1</b>';
00025 $value = String::preg_replace($search, $replace, $value);
00026
00027
00028 $search = '|(@[\S]*)|';
00029 $replace = '<i>$1</i>';
00030 $value = String::preg_replace($search, $replace, $value);
00031
00032
00033 $search = "@(http[s]?://[\S]*)@";
00034
00035 if (Common::flag_is_set($params, self::EXPAND_LINKS)) {
00036
00037 $matches = array();
00038 String::preg_match_all($search, $value, $matches);
00039
00040 Load::components('httprequest');
00041 $err = new Status();
00042 foreach (Arr::get_item($matches, 0, array()) as $url) {
00043 $head = HttpRequest::get_head($url, $err);
00044
00045 $search_loc = '@location: ([\S]*)@i';
00046 $matches_loc = array();
00047 if (String::preg_match_all($search_loc, $head, $matches_loc)) {
00048
00049 $new_url = String::escape(array_pop($matches_loc[1]));
00050 $value = str_replace($url, $new_url, $value);
00051 }
00052 }
00053 }
00054
00055 $replace = '<a href="$1">$1</a>';
00056 $value = String::preg_replace($search, $replace, $value);
00057
00058 return $value;
00059 }
00060
00061
00062 public function decode($value, $params = false) {
00063 $value = str_replace('>', ">", $value);
00064 $value = str_replace('<', "<", $value);
00065 return trim($value);
00066 }
00067 }