6 include_once('mobile_device_detect.php');
7 mobile_device_detect(true,true,true,true,true,true,'https://t.orzdream.com/',false);
8 include_once('config.php');
9 include_once('utility.php');
10 include_once('twitteroauth.php');
11 include_once('oauth_lib.php');
13 function refreshProfile(){
15 $user = $t->veverify();
16 $time = $_SERVER['REQUEST_TIME']+3600*24*365;
17 setcookie('friends_count', $user->friends_count, $time, '/');
18 setcookie('statuses_count', $user->statuses_count, $time, '/');
19 setcookie('followers_count', $user->followers_count, $time, '/');
20 setcookie('imgurl', getAvatar($user->profile_image_url), $time, '/');
21 setcookie('name', $user->screen_name, $time, '/');
22 setcookie('listed_count', $user->listed_count, $time, '/');
25 function getDefCookie($name, $default="") {
26 if (getCookie($name)) return getCookie($name);
29 function format_time($time){
30 date_default_timezone_set('UTC');
31 return strtotime($time);
33 function formatText($text) {
35 if (get_magic_quotes_gpc()) {
36 $text = stripslashes($text);
40 $urlReg = '/(((http|https|ftp):\/\/){1}([[:alnum:]\-\.])+(\.)(([[:alnum:]]){2,4})?([[:alnum:]\/+=%#&@\:\;_\.~\?\!\-\,]*))/i';
41 $text = preg_replace($urlReg, '<a href="\1" target="_blank" rel="noreferrer">\1</a>', $text);
44 $atReg = '/\B@{1}(([a-zA-Z0-9\_\.\-])+)/i';
45 $text = preg_replace($atReg, '<a href="user.php?id=\1" target="_blank">\0</a>', $text);
48 $listReg = '/(\<a[\w+=\:\%\#\&\.~\?\"\'\/\- ]+\>@{1}([a-zA-Z0-9_\.\-]+)<\/a\>([\/a-zA-Z0-9_\.\-]+))/i';
49 $text = preg_replace($listReg, '<a href="list.php?id=\2\3" target="_blank">@\2\3</a>', $text);
52 $tagReg = "/\B(\#{1}([\w]*[\pN\pC\pL]+[\w]*))([\s]*)/u";
53 $text = preg_replace($tagReg, '<a target="_blank" href="search.php?q=%23\2">#<span class="hashtag">\2</span></a>\3', $text);
55 $text = formatTweetID($text);
60 function formatEntities(&$entities,$html){
61 $user_mentions = $entities->user_mentions;
62 $hashtags = $entities->hashtags;
63 $urls = $entities->urls;
64 if(count($user_mentions) > 0) {
65 foreach($user_mentions as $user_mention) {
66 $name = $user_mention->screen_name;
67 $html = str_replace("@$name","<a href=\"user.php?id=$name\" target=\"_blank\">@$name</a>",$html);
70 if(count($hashtags) > 0) {
71 foreach($hashtags as $hashtag) {
72 $text = $hashtag->text;
73 $html = str_replace("#$text","<a target=\"_blank\" href=\"search.php?q=%23$text\">#<span class=\"hashtag\">$text</span></a>",$html);
76 if(count($urls) > 0) {
77 $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';
78 foreach($urls as $url) {
79 $exp = is_null($url->expanded_url) ? $url->url : $url->expanded_url;
80 if(substr($url->url,0,4) != 'http') $url->url = 'http://'.$url->url;
81 if(isset($url->display_url)) {
82 $dis = $url->display_url;
84 $tmp = explode('://', $url->url);
87 $html = str_replace($url->url,"<a href=\"$exp\" target=\"_blank\" rel=\"noreferrer\" class=\"tweet_url\">$dis</a>",$html);
90 if(isset($entities->media)) {
91 $medias = $entities->media;
92 foreach($medias as $media) {
93 $url = $media->media_url_https;
94 if (getcookie('p_avatar') == 'true') {
95 $url = 'img.php?imgurl='.$url;
97 $html = str_replace($media->url,"<a href=\"$url\" target=\"_blank\" rel=\"noreferrer\">$media->display_url</a>",$html);
103 function formatTweetID($text){
104 $reg = '/(\<a[\w+=@\:\%\#\&\.~\?\"\'\/\-\! ]+\>[\S]+<\/a\>)/i';
105 preg_match_all($reg, $text, $tmpMatches);
106 if(count($tmpMatches) > 0){
107 $text = preg_replace($reg, '$_holder_$', $text);
109 preg_match_all('/([\d]{10,})/', $text, $matches);
110 if(count($matches) > 0){
111 $matches = array_unique($matches[0]);
112 foreach($matches as $match){
113 $text = str_replace($match, '<a title="We think it\'s a tweet ID, isn\'t it?" href="status.php?id='.$match.'" target="_blank">'.$match.'</a>', $text);
115 $tmpReg = '/\$_holder_\$/i';
116 foreach($tmpMatches[0] as $match){
117 $text = preg_replace($tmpReg, $match, $text, 1);
123 function processCurl($url,$postdata=false,$header=false)
125 $ch = curl_init($url);
127 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
128 curl_setopt($ch, CURLOPT_VERBOSE, 1);
129 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
130 curl_setopt($ch, CURLOPT_TIMEOUT,120);
132 if($postdata !== false) {
133 curl_setopt ($ch, CURLOPT_POST, true);
134 curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
137 if($header !== false) {
138 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
141 $response = curl_exec($ch);
142 $responseInfo=curl_getinfo($ch);
144 if( intval( $responseInfo['http_code'] ) == 200 )
150 function objectifyXml( $data ){
152 if( function_exists('simplexml_load_string') ) {
153 $obj = simplexml_load_string( $data );
155 if (isset($obj->error) || !$obj) return false;
161 function objectifyJson($data){
162 if(function_exists("json_decode")){
163 $obj = json_decode($data);
165 if(!isset($obj->error) || $obj){
172 function imageUpload($image){
174 $signingurl = API_URL.'/account/verify_credentials.json';
175 $request = OAuthRequest::from_consumer_and_token($t->consumer, $t->token, 'GET', $signingurl, array());
176 $request->sign_request($t->sha1_method, $t->consumer, $t->token);
177 $r_header = $request->to_header("https://api.twitter.com/");
179 $url = 'http://img.ly/api/2/upload.json';
180 $postdata = array('media' => $image);
181 $ch = curl_init($url);
182 if($postdata !== false)
184 curl_setopt ($ch, CURLOPT_POST, true);
185 curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
187 curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth-Service-Provider: '.$signingurl,'X-Verify-Credentials-'.$r_header));
188 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
189 curl_setopt($ch, CURLOPT_USERAGENT, 'embr');
190 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
191 curl_setopt($ch, CURLOPT_TIMEOUT,120);
192 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
194 $response = curl_exec($ch);
195 $response_info=curl_getinfo($ch);
198 if ($response_info['http_code'] == 200) {
199 return objectifyJson($response);
201 return $response_info['http_code'];
205 function getTwitter() {
207 $access_token = $_SESSION['access_token'] ? $_SESSION['access_token'] : null;
208 $oauth_token = $access_token ? $access_token['oauth_token'] : $_COOKIE['oauth_token'];
209 $oauth_token_secret = $access_token ? $access_token['oauth_token_secret'] : $_COOKIE['oauth_token_secret'];
210 $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
216 function loginStatus() {
217 if(isset($_SESSION['login_status'])){
218 return $_SESSION['login_status'] == 'verified' ? true : false;
219 }elseif(getEncryptCookie("oauth_token") != "" && getEncryptCookie("oauth_token_secret") != "" && getEncryptCookie("user_id") != "" && getEncryptCookie("twitese_name") != ""){
220 $access_token = array("oauth_token" => getEncryptCookie("oauth_token"), "oauth_token_secret" => getEncryptCookie("oauth_token_secret"), "user_id" => getEncryptCookie("user_id"), "screen_name" => getEncryptCookie("twitese_name"));
221 $_SESSION['access_token'] = $access_token;
222 $_SESSION['login_status'] = 'verified';
229 function getAvatar($profileImg){
230 if (getcookie('p_avatar') == 'true') {
231 return 'img.php?imgurl='.$profileImg;
233 return preg_replace('/https?:\/\/\w+([0-9])\.twimg\.com/i','https://s3.amazonaws.com/twitter_production',$profileImg);
236 // $target: can't be current user
237 // $source: use the current user as the source user implicitly if not specified
238 // 9 => no relationship
239 // 1 => fo each other
240 // 2 => $source fo $target
241 // 3 => $target fo $source
242 // 4 => $source blocking $target
243 function getRelationship($target, $source = false){
244 $relationship = getTwitter()->relationship($target, $source)->relationship;
245 $target = $relationship->target;
246 $source = $relationship->source;
247 if($source->blocking != null){
250 if($source->following == true && $target->following == true){
253 if($source->following == true && $target->following == false){
256 if($source->following == false && $target->following == true){
262 function urlshorten($url, $type='goo.gl'){
265 $data = json_encode(array('longUrl' => $url));
266 $api = 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDsX2BAo9Jc2yG3Pq1VbLQALqdrtDFvXkg';
267 $header = array('Content-type: application/json');
268 $result = objectifyJson(processCurl($api,$data,$header))->id;
271 $api = 'http://zi.mu/api.php?format=simple&action=shorturl&url=';
272 $result = objectifyJson(processCurl($api.rawurlencode($url)));
280 function expandRedirect($shorturl, &$hops) {
281 if (count($hops) >= 10) {
284 $head = array_change_key_case(get_headers($shorturl, TRUE), CASE_LOWER);
285 if (!isset($head['location']) || empty($head['location'])) {
288 $prevhop = $shorturl;
289 foreach((array)$head['location'] as $redir) {
290 if (substr($redir, 0, 1)=='/' || preg_match('/[\.\/]'.preg_quote(parse_url($prevhop, PHP_URL_HOST)).'$/', parse_url($redir, PHP_URL_HOST))) {
296 return expandRedirect($redir, $hops);