OSDN Git Service

05fb28795037907accb2f79c6911456f2ec6ecc3
[embrj/master.git] / lib / twitese.php
1 <?php\r
2         set_time_limit(15);\r
3         if(!isset($_SESSION)){\r
4                 session_start();\r
5         }\r
6         include_once('mobile_device_detect.php');\r
7         mobile_device_detect(true,true,true,true,true,true,'https://t.orzdream.com/',false);\r
8         include_once('config.php');\r
9         include_once('utility.php');\r
10         include_once('twitteroauth.php');\r
11         include_once('oauth_lib.php');\r
12 \r
13         function refreshProfile(){\r
14                 $t = getTwitter();\r
15                 $user = $t->veverify();\r
16                 $time = $_SERVER['REQUEST_TIME']+3600*24*365;\r
17                 setcookie('friends_count', $user->friends_count, $time, '/');\r
18                 setcookie('statuses_count', $user->statuses_count, $time, '/');\r
19                 setcookie('followers_count', $user->followers_count, $time, '/');\r
20                 setcookie('imgurl', getAvatar($user->profile_image_url), $time, '/');\r
21                 setcookie('name', $user->screen_name, $time, '/');\r
22                 setcookie('listed_count', GetListed($t), $time, '/');\r
23         }\r
24 \r
25         function getDefCookie($name, $default="") {\r
26                 if (getCookie($name)) return getCookie($name);\r
27                 else return $default;\r
28         }\r
29         function format_time($time){\r
30                 date_default_timezone_set('UTC');\r
31                 return strtotime($time);\r
32         }\r
33         function formatText($text) {\r
34                 //如果开启了魔术引号\" \' 转回来\r
35                 if (get_magic_quotes_gpc()) {\r
36                         $text = stripslashes($text);\r
37                 }\r
38 \r
39                 //添加url链接\r
40                 $urlReg = '/(((http|https|ftp):\/\/){1}([[:alnum:]\-\.])+(\.)(([[:alnum:]]){2,4})?([[:alnum:]\/+=%#&@\:\;_\.~\?\!\-\,]*))/i';\r
41                 $text = preg_replace($urlReg, '<a href="\1" target="_blank" rel="noreferrer">\1</a>', $text);\r
42 \r
43                 //添加@链接\r
44                 $atReg = '/\B@{1}(([a-zA-Z0-9\_\.\-])+)/i';\r
45                 $text = preg_replace($atReg,    '<a href="user.php?id=\1" target="_blank">\0</a>', $text);\r
46 \r
47                 //添加 list 链接\r
48                 $listReg = '/(\<a[\w+=\:\%\#\&\.~\?\"\'\/\- ]+\>@{1}([a-zA-Z0-9_\.\-]+)<\/a\>([\/a-zA-Z0-9_\.\-]+))/i';\r
49                 $text = preg_replace($listReg,  '<a href="list.php?id=\2\3" target="_blank">@\2\3</a>', $text);\r
50 \r
51                 //添加标签链接\r
52                 $tagReg = "/\B(\#{1}([\w]*[\pN\pC\pL]+[\w]*))([\s]*)/u";\r
53                 $text = preg_replace($tagReg, '<a target="_blank" href="search.php?q=%23\2">#<span class="hashtag">\2</span></a>\3', $text);\r
54 \r
55                 $text = formatTweetID($text);\r
56 \r
57                 return $text;\r
58         }\r
59 \r
60         function formatEntities(&$entities,$html){\r
61                 $user_mentions = $entities->user_mentions;\r
62                 $hashtags = $entities->hashtags;\r
63                 $urls = $entities->urls;\r
64                 if(count($user_mentions) > 0) {\r
65                         foreach($user_mentions as $user_mention) {\r
66                                 $name = $user_mention->screen_name;\r
67                                 $html = str_replace("@$name","<a href=\"user.php?id=$name\" target=\"_blank\">@$name</a>",$html);\r
68                         }\r
69                 }\r
70                 if(count($hashtags) > 0) {\r
71                         foreach($hashtags as $hashtag) {\r
72                                 $text = $hashtag->text;\r
73                                 $html = str_replace("#$text","<a target=\"_blank\" href=\"search.php?q=%23$text\">#<span class=\"hashtag\">$text</span></a>",$html);\r
74                         }       \r
75                 }\r
76                 if(count($urls) > 0) {\r
77                         $scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http' : 'https';\r
78                         foreach($urls as $url) {\r
79                                 $exp = is_null($url->expanded_url) ? $url->url : $url->expanded_url;\r
80                                 if(substr($url->url,0,4) != 'http') $url->url = 'http://'.$url->url;\r
81                                 if(isset($url->display_url)) {\r
82                                         $dis = $url->display_url;\r
83                                 } else {\r
84                                         $tmp = explode('://', $url->url);\r
85                                         $dis = $tmp[1];\r
86                                 }\r
87                                 $html = str_replace($url->url,"<a href=\"$exp\" target=\"_blank\" rel=\"noreferrer\" class=\"tweet_url\">$dis</a>",$html);\r
88                         }       \r
89                 }\r
90                 if(isset($entities->media)) {\r
91                         $medias = $entities->media;\r
92                         foreach($medias as $media) {\r
93                                 $url = $media->media_url_https;\r
94                                 if (getcookie('p_avatar') == 'true') {\r
95                                                 $url = 'img.php?imgurl='.$url;\r
96                                 }\r
97                                 $html = str_replace($media->url,"<a href=\"$url\" target=\"_blank\" rel=\"noreferrer\">$media->display_url</a>",$html);\r
98                         }\r
99                 }\r
100                 return $html;\r
101         }\r
102 \r
103         function formatTweetID($text){\r
104                 $reg = '/(\<a[\w+=@\:\%\#\&\.~\?\"\'\/\-\! ]+\>[\S]+<\/a\>)/i';\r
105                 preg_match_all($reg, $text, $tmpMatches);\r
106                 if(count($tmpMatches) > 0){\r
107                         $text = preg_replace($reg, '$_holder_$', $text);\r
108                 }\r
109                 preg_match_all('/([\d]{10,})/', $text, $matches);\r
110                 if(count($matches) > 0){\r
111                         $matches = array_unique($matches[0]);\r
112                         foreach($matches as $match){\r
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);\r
114                         }\r
115                         $tmpReg = '/\$_holder_\$/i';\r
116                         foreach($tmpMatches[0] as $match){\r
117                                 $text = preg_replace($tmpReg, $match, $text, 1);\r
118                         }\r
119                 }\r
120                 return $text;\r
121         }\r
122 \r
123         function processCurl($url,$postdata=false,$header=false)\r
124         {\r
125                 $ch = curl_init($url);\r
126 \r
127                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r
128                 curl_setopt($ch, CURLOPT_VERBOSE, 1);\r
129                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);\r
130                 curl_setopt($ch, CURLOPT_TIMEOUT,120);\r
131                 \r
132                 if($postdata !== false) {\r
133                         curl_setopt ($ch, CURLOPT_POST, true);\r
134                         curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);\r
135                 }\r
136                 \r
137                 if($header !== false) {\r
138                         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);\r
139                 }\r
140                 \r
141                 $response = curl_exec($ch);\r
142                 $responseInfo=curl_getinfo($ch);\r
143                 curl_close($ch);\r
144                 if( intval( $responseInfo['http_code'] ) == 200 )\r
145                         return $response;               \r
146                 else\r
147                         return false;\r
148         }\r
149 \r
150         function objectifyXml( $data ){\r
151 \r
152                 if( function_exists('simplexml_load_string') ) {\r
153                         $obj = simplexml_load_string( $data );\r
154                 }\r
155                 if (isset($obj->error) || !$obj) return false;\r
156                 else return $obj;\r
157 \r
158                 return false;\r
159         }\r
160 \r
161         function objectifyJson($data){\r
162                 if(function_exists("json_decode")){\r
163                         $obj = json_decode($data);\r
164                 }\r
165                 if(!isset($obj->error) || $obj){\r
166                         return $obj;\r
167                 }\r
168                 return false;\r
169         }\r
170 \r
171 \r
172         function imageUpload($image){\r
173                 $t = getTwitter();\r
174                 $signingurl = 'https://api.twitter.com/1/account/verify_credentials.json';\r
175                 $request = OAuthRequest::from_consumer_and_token($t->consumer, $t->token, 'GET', $signingurl, array());\r
176                 $request->sign_request($t->sha1_method, $t->consumer, $t->token);\r
177                 $r_header = $request->to_header("http://api.twitter.com/");\r
178                 \r
179                 $url = 'http://img.ly/api/2/upload.json';\r
180                 $postdata = array('media' => $image);           \r
181                 $ch = curl_init($url);          \r
182                 if($postdata !== false)\r
183                 {\r
184                         curl_setopt ($ch, CURLOPT_POST, true);\r
185                         curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);\r
186                 }\r
187                 curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth-Service-Provider: '.$signingurl,'X-Verify-Credentials-'.$r_header)); \r
188                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r
189                 curl_setopt($ch, CURLOPT_USERAGENT, 'embr');\r
190                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);\r
191                 curl_setopt($ch, CURLOPT_TIMEOUT,120);\r
192                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);\r
193 \r
194                 $response = curl_exec($ch);\r
195                 $response_info=curl_getinfo($ch);\r
196                 curl_close($ch);\r
197                 \r
198                 if ($response_info['http_code'] == 200) {\r
199                         return objectifyJson($response);\r
200                 } else {\r
201                         return null;\r
202                 }\r
203         }\r
204         \r
205         function getTwitter() {\r
206                 if(loginStatus()){\r
207                         $access_token = $_SESSION['access_token'] ? $_SESSION['access_token'] : null;\r
208                         $oauth_token = $access_token ? $access_token['oauth_token'] : $_COOKIE['oauth_token'];\r
209                         $oauth_token_secret = $access_token ? $access_token['oauth_token_secret'] : $_COOKIE['oauth_token_secret'];\r
210                         $oauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);\r
211                         return $oauth;\r
212                 }\r
213                 return null;\r
214         }\r
215 \r
216    function loginStatus() {\r
217            if(isset($_SESSION['login_status'])){\r
218               return $_SESSION['login_status'] == 'verified' ? true : false;\r
219            }elseif(getEncryptCookie("oauth_token") != "" && getEncryptCookie("oauth_token_secret") != "" && getEncryptCookie("user_id") != "" && getEncryptCookie("twitese_name") != ""){\r
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"));\r
221               $_SESSION['access_token'] = $access_token;\r
222               $_SESSION['login_status'] = 'verified';\r
223               refreshProfile();\r
224               return true;\r
225            }\r
226            return false;\r
227    }\r
228 \r
229         function GetListed($t, $cursor = false){\r
230                 $lists = $t->beAddedLists($t->username, $cursor);\r
231                 $listed = count($lists->lists);\r
232                 if($lists->next_cursor > 1){\r
233                         $listed += GetListed($t, $lists->next_cursor);\r
234                 }\r
235                 return  $listed;\r
236         }\r
237 \r
238         function getAvatar($profileImg){\r
239                 if (getcookie('p_avatar') == 'true') {\r
240                                 return 'img.php?imgurl='.$profileImg;\r
241                 }\r
242                 return preg_replace('/https?:\/\/\w+([0-9])\.twimg\.com/i','https://s3.amazonaws.com/twitter_production',$profileImg);\r
243         }\r
244 \r
245         // $target: can't be current user\r
246         // $source: use the current user as the source user implicitly if not specified\r
247         // 9 => no relationship\r
248         // 1 => fo each other\r
249         // 2 => $source fo $target\r
250         // 3 => $target fo $source\r
251         // 4 => $source blocking $target\r
252         function getRelationship($target, $source = false){\r
253                 $relationship = getTwitter()->relationship($target, $source)->relationship;\r
254                 $target = $relationship->target;\r
255                 $source = $relationship->source;\r
256                 if($source->blocking == 1){\r
257                         return 4;\r
258                 }\r
259                 if($source->following == 1 && $target->following == 1){\r
260                         return 1;\r
261                 }\r
262                 if($source->following == 1 && $target->following != 1){\r
263                         return 2;\r
264                 }\r
265                 if($source->following != 1 && $target->following == 1){\r
266                         return 3;\r
267                 }\r
268                 return 9;\r
269         }\r
270         \r
271         function urlshorten($url, $type='goo.gl'){\r
272                 switch($type){\r
273                         case 'goo.gl':\r
274                         $data = json_encode(array('longUrl' => $url));\r
275                         $api = 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDsX2BAo9Jc2yG3Pq1VbLQALqdrtDFvXkg';\r
276                         $header = array('Content-type: application/json');\r
277                         $result = objectifyJson(processCurl($api,$data,$header))->id;\r
278                         break;\r
279                         case 'zi.mu':\r
280                         $api = 'http://zi.mu/api.php?format=simple&action=shorturl&url=';\r
281                         $result = objectifyJson(processCurl($api.rawurlencode($url)));\r
282                         break;\r
283                         default:\r
284                         break;\r
285                 }\r
286                 return $result;\r
287         }\r
288 ?>\r