PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/common.php

http://kwestion.googlecode.com/
PHP | 446 lines | 375 code | 62 blank | 9 comment | 59 complexity | a362ad7b84bf4e2b4408724bff5d9cf2 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once 'config.php';
  3. function user_oauth() {
  4. require_once 'OAuth.php';
  5. session_start();
  6. setCookie('Oauth', 'oauth', time()+3600*24*1000);
  7. if ($oauth_token = $_GET['oauth_token']) {
  8. $params = array('oauth_verifier' => $_GET['oauth_verifier']);
  9. $response = oauth_process('https://api.twitter.com/oauth/access_token', $params);
  10. parse_str($response, $token);
  11. setCookie('up', $token['oauth_token'].'|'.$token['oauth_token_secret'],time()+3600*24*1000);
  12. unset($_SESSION['oauth_request_token_secret']);
  13. setCookie('l', 'OK', time()+3600*24*1000);
  14. header('Location: '. BASE_URL);
  15. exit();
  16. } else {
  17. $params = array('oauth_callback' => BASE_URL);
  18. $response = oauth_process('https://api.twitter.com/oauth/request_token', $params);
  19. parse_str($response, $token);
  20. $_SESSION['oauth_request_token_secret'] = $token['oauth_token_secret'];
  21. $authorise_url = 'https://api.twitter.com/oauth/authorize?oauth_token='.$token['oauth_token'];
  22. header("Location: $authorise_url");
  23. }
  24. }
  25. function user_oauth_sign(&$url, &$args = false) {
  26. require_once 'OAuth.php';
  27. $method = $args !== false ? 'POST' : 'GET';
  28. if (preg_match_all('#[?&]([^=]+)=([^&]+)#', $url, $matches, PREG_SET_ORDER)) {
  29. foreach ($matches as $match){$args[$match[1]] = $match[2];}
  30. $url = substr($url, 0, strpos($url, '?'));
  31. }
  32. $sig_method = new OAuthSignatureMethod_HMAC_SHA1();
  33. $consumer = new OAuthConsumer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
  34. $token = NULL;
  35. if ((isset($_GET['oauth_token']) && ($oauth_token = $_GET['oauth_token'])) && $_SESSION['oauth_request_token_secret']) {
  36. $oauth_token_secret = $_SESSION['oauth_request_token_secret'];
  37. } else {
  38. list($oauth_token, $oauth_token_secret) = explode('|', $_COOKIE['up']);
  39. }
  40. if ($oauth_token && $oauth_token_secret) {
  41. $token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  42. }
  43. $request = OAuthRequest::from_consumer_and_token($consumer, $token, $method, $url, $args);
  44. $request->sign_request($sig_method, $consumer, $token);
  45. switch ($method) {
  46. case 'GET':
  47. $url = $request->to_url();
  48. $args = false;
  49. return;
  50. case 'POST':
  51. $url = $request->get_normalized_http_url();
  52. $args = $request->to_postdata();
  53. return;
  54. }
  55. }
  56. function oauth_process($url, $post_data = false) {
  57. if ($post_data === true) $post_data = array();
  58. user_oauth_sign($url, $post_data);
  59. $ch = curl_init($url);
  60. if($post_data !== false) {
  61. curl_setopt ($ch, CURLOPT_POST, true);
  62. curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_data);
  63. }
  64. curl_setopt($ch, CURLOPT_VERBOSE, 0);
  65. curl_setopt($ch, CURLOPT_HEADER, 0);
  66. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  67. curl_setopt($ch, CURLOPT_USERAGENT, 'kwestion');
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  69. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  70. $response = curl_exec($ch);
  71. $response_info=curl_getinfo($ch);
  72. curl_close($ch);
  73. if(intval($response_info['http_code']) == 200) {
  74. $json = json_decode($response);
  75. if ($json) return $json;
  76. return $response;
  77. }else{
  78. setCookie('Oauth', 'oauth', time()-3600*24*1000);
  79. header('Location: '. BASE_URL);
  80. }
  81. }
  82. function user_type(){
  83. return isset($_COOKIE["Oauth"]) && $_COOKIE['Oauth'] == 'oauth';
  84. }
  85. function twitter_process($url, $post_data = false) {
  86. return raw_twitter_process(getU(),getP(),$url,$post_data);
  87. }
  88. function raw_twitter_process($u,$p,$url,$post_data = false){
  89. if($post_data === true) $post_data = array();
  90. $file = false;
  91. if(isset($post_data['file'])){
  92. $file = $post_data['file'];
  93. //echo 'here';
  94. unset($post_data['file']);
  95. }
  96. if (user_type() && ( strpos($url, '/twitter.com') !== false || strpos($url, 'api.twitter.com') !== false) ) {
  97. user_oauth_sign($url, $post_data);
  98. }
  99. $ch = curl_init($url);
  100. if($post_data !== false) {
  101. curl_setopt ($ch, CURLOPT_POST, 1);
  102. curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_data);
  103. if($file!==false){
  104. curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
  105. }
  106. }
  107. if (!user_type())
  108. curl_setopt($ch, CURLOPT_USERPWD, $u.':'.$p);
  109. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  110. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  111. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  112. curl_setopt($ch, CURLOPT_VERBOSE, 0);
  113. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  114. curl_setopt($ch, CURLOPT_USERAGENT, 'kwestion');
  115. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  116. $response = curl_exec($ch);
  117. $response_info=curl_getinfo($ch);
  118. curl_close($ch);
  119. //echo $response;
  120. if(intval( $response_info['http_code'])==200){
  121. $json = json_decode($response);
  122. if(!($json===false)){
  123. return $json;
  124. }
  125. else{
  126. return false;
  127. }
  128. }
  129. else{
  130. return false;
  131. }
  132. }
  133. function oAuthRequest($url, $method, $parameters , $multi = false) {
  134. require_once 'OAuth.php';
  135. if (preg_match_all('#[?&]([^=]+)=([^&]+)#', $url, $matches, PREG_SET_ORDER)) {
  136. foreach ($matches as $match){$args[$match[1]] = $match[2];}
  137. $url = substr($url, 0, strpos($url, '?'));
  138. }
  139. $sig_method = new OAuthSignatureMethod_HMAC_SHA1();
  140. $consumer = new OAuthConsumer(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
  141. $token = NULL;
  142. if (($oauth_token = $_GET['oauth_token']) && $_SESSION['oauth_request_token_secret']) {
  143. $oauth_token_secret = $_SESSION['oauth_request_token_secret'];
  144. } else {
  145. list($oauth_token, $oauth_token_secret) = explode('|', $_COOKIE['up']);
  146. }
  147. if ($oauth_token && $oauth_token_secret) {
  148. $token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  149. }
  150. $request = OAuthRequest::from_consumer_and_token($consumer, $token, $method, $url, $parameters);
  151. $request->sign_request($sig_method, $consumer, $token);
  152. $extheader = null;
  153. if($multi){
  154. $extheader = $request->to_header();
  155. }
  156. switch ($method) {
  157. case 'GET':
  158. return http($request->to_url(), 'GET');
  159. default:
  160. return http($request->get_normalized_http_url(), $method, $request->to_postdata($multi) , $multi, $extheader );
  161. }
  162. }
  163. function http($url, $method, $postfields = NULL , $multi = false, $extheader = false) {
  164. $http_info = array();
  165. $ci = curl_init();
  166. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  167. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, 0);
  168. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  169. curl_setopt($ci, CURLOPT_VERBOSE, 0);
  170. curl_setopt($ci, CURLOPT_TIMEOUT, 10);
  171. curl_setopt($ci, CURLOPT_USERAGENT, 'kwestion');
  172. switch ($method) {
  173. case 'POST':
  174. curl_setopt($ci, CURLOPT_POST, TRUE);
  175. if (!empty($postfields)) {
  176. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  177. }
  178. break;
  179. case 'DELETE':
  180. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  181. if (!empty($postfields)) {
  182. $url = "{$url}?{$postfields}";
  183. }
  184. }
  185. $header_array2=array('Expect: ');
  186. if( $multi )
  187. $header_array2 = array($extheader, "Content-Type: multipart/form-data; boundary=" . OAuthUtil::$boundary, "Expect: ");
  188. curl_setopt($ci, CURLOPT_URL, $url );
  189. curl_setopt($ci, CURLOPT_HTTPHEADER, $header_array2 );
  190. $response = curl_exec($ci);
  191. $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  192. curl_close ($ci);
  193. return $response;
  194. }
  195. function preg_match_one($pattern, $subject, $flags = NULL) {
  196. preg_match($pattern, $subject, $matches, $flags);
  197. return trim($matches[1]);
  198. }
  199. function parse_thread($html){
  200. $output = array();
  201. $html_tweets = explode('</li>', $html);
  202. foreach ($html_tweets as $tweet) {
  203. $id = preg_match_one('#msgtxt(\d*)#', $tweet);
  204. if (!$id) continue;
  205. $output[] = (object) array(
  206. 'id' => $id,
  207. 'text' => strip_tags(preg_match_one('#</a>: (.*)</span>#', $tweet)),
  208. 'source' => preg_match_one('#>from (.*)</span>#', $tweet),
  209. 'from' => (object) array(
  210. 'id' => preg_match_one('#profile_images/(\d*)#', $tweet),
  211. 'screen_name' => preg_match_one('#twitter.com/([^"]+)#', $tweet),
  212. 'profile_image_url' => preg_match_one('#src="([^"]*)"#' , $tweet),
  213. ),
  214. 'to' => (object) array(
  215. 'screen_name' => preg_match_one('#@([^<]+)#', $tweet),
  216. ),
  217. 'created_at' => str_replace('about', '', preg_match_one('#info">\s(.*)#', $tweet)),
  218. );
  219. }
  220. return $output;
  221. }
  222. function processstatuses($statuses){
  223. $newstatuses = array();
  224. //var_dump($statuses);
  225. if(!is_array($statuses))
  226. return array();
  227. foreach($statuses as $t){
  228. $newstatuses[] = processstatus($t);
  229. }
  230. return $newstatuses;
  231. }
  232. function processstatus($status){
  233. $text = $status->text;
  234. if(UNSHORTEN_URL){
  235. $text = unshorturl($text);
  236. }
  237. $text = twitter_parse_tags($text);
  238. $status->processedtext = $text;
  239. return $status;
  240. }
  241. function twitter_parse_tags($input){
  242. $out = preg_replace('#(\w+?://[\w\#$%&~/.\-;:=,?\[\]+]*)(?<![.,])#', '<a href="$1" target="_blank">$1</a>', $input);
  243. $out = twitter_photo_replace($out);
  244. return $out;
  245. }
  246. function twitter_photo_replace($text) {
  247. $images = array();
  248. $tmp = strip_tags($text);
  249. $services = array(
  250. '#youtube\.com\/watch\?v=([_-\d\w]+)#i' => 'http://i.ytimg.com/vi/%s/1.jpg',
  251. '#youtu\.be\/([_-\d\w]+)#i' => 'http://i.ytimg.com/vi/%s/1.jpg',
  252. '#qik\.ly\/([_-\d\w]+)#i' => 'http://qik.ly/%s.jpg',
  253. '#twitpic\.com\/([\d\w]+)#i' => 'http://twitpic.com/show/thumb/%s',
  254. '#twitgoo\.com\/([\d\w]+)#i' => 'http://twitgoo.com/show/thumb/%s',
  255. '#hellotxt\.com\/i\/([\d\w]+)#i' => 'http://hellotxt.com/image/%s.s.jpg',
  256. '#ts1\.in\/(\d+)#i' => 'http://ts1.in/mini/%s',
  257. '#moby\.to\/\?([\w\d]+)#i' => 'http://moby.to/%s:square',
  258. '#mobypicture\.com\/\?([\w\d]+)#i' => 'http://mobypicture.com/?%s:square',
  259. '#twic\.li\/([\w\d]{2,7})#' => 'http://twic.li/api/photo.jpg?id=%s&size=small',
  260. '#tweetphoto\.com\/(\d+)#' => 'http://api.plixi.com/api/tpapi.svc/imagefromurl?url=http://tweetphoto.com/%s',
  261. '#plixi\.com\/p\/(\d+)#' => 'http://api.plixi.com/api/tpapi.svc/imagefromurl?url=http://plixi.com/p/%s&size=small',
  262. '#phz\.in\/([\d\w]+)#' => 'http://api.phreadz.com/thumb/%s?t=code',
  263. '#imgur\.com\/([\w]{5})[\s\.ls][\.\w]*#i' => 'http://imgur.com/%ss.png',
  264. '#imgur\.com\/gallery\/([\w]+)#i' => 'http://imgur.com/%ss.png',
  265. '#brizzly\.com\/pic\/([\w]+)#i' => 'http://pics.brizzly.com/thumb_sm_%s.jpg',
  266. '#img\.ly\/([\w\d]+)#i' => 'http://img.ly/show/thumb/%s',
  267. '#picplz\.com\/.*?\/pic\/([\d\w\.]+)#' => 'http://picplz.com/%s/thumb',
  268. '#picplz\.com\/([\d\w\.]+)#' => 'http://picplz.com/%s/thumb',
  269. '#pk\.gd\/([\d\w]+)#i' => 'http://img.pikchur.com/pic_%s_s.jpg',
  270. '#pikchur\.com\/([\d\w]+)#i' => 'http://img.pikchur.com/pic_%s_s.jpg',
  271. '#znl\.me\/([\d\w]+)#' => 'http://www.zannel.com/webservices/content/%s/Image-164x123-JPG.jpg',
  272. '#yfrog\.com\/([\d\w]+)#' => 'http://yfrog.com/%s:small'
  273. );
  274. // Loop through each service and show images for matching URLs
  275. foreach ($services as $pattern => $thumbnail_url) {
  276. if (preg_match_all($pattern, $tmp, $matches, PREG_PATTERN_ORDER) > 0) {
  277. foreach ($matches[1] as $key => $match) {
  278. $images[] = '<a href="http://'.$matches[0][$key].'" target="_blank"><img src="'.sprintf($thumbnail_url, $match).'" /></a>';
  279. }
  280. break;
  281. }
  282. }
  283. // Twitxr is handled differently because of their folder structure
  284. if (preg_match_all('#twitxr.com/[^ ]+/updates/([\d]+)#', $tmp, $matches, PREG_PATTERN_ORDER) > 0) {
  285. foreach ($matches[1] as $key => $match) {
  286. $thumb = 'http://twitxr.com/thumbnails/'.substr($match, -2).'/'.$match.'_th.jpg';
  287. $images[] = '<a href="http://'.$matches[0][$key].'" target="_blank">"<img src="'.$thumb.'" /></a>';
  288. }
  289. }
  290. // AudioBoo is handled differently because we link directly to an MP3, not an image
  291. if (preg_match_all('#boo.fm/b([\d]+)#', $tmp, $matches, PREG_PATTERN_ORDER) > 0) {
  292. foreach ($matches[1] as $key => $match) {
  293. $images[] = "<a href='http://{$matches[0][$key]}.mp3' target='_blank'>[???????]</a>";
  294. }
  295. }
  296. // match phz.in (short phreadz) urls
  297. if (preg_match_all('#phz.in/([\d\w]+)#', $tmp, $matches, PREG_PATTERN_ORDER) > 0){
  298. foreach ($matches[1] as $match){
  299. //Images from Phreadz aren't always suitable for mobile, so we use http://tinysrc.mobi/ to convert them
  300. $thumb = "http://i.tinysrc.mobi/x50/http://api.phreadz.com/thumb/" . $match . "?t=code";
  301. $images[] = '<a href="http://phz.in/'. $match.'" target="_blank"><img src="'.$thumb.'" /></a>';
  302. }
  303. }
  304. if (empty($images)) return $text;
  305. return implode('<br />', $images).'<br />'.$text;
  306. }
  307. function unshorturl($text){
  308. $urlregs = array();
  309. $urlregs[] ='/t\.co\/([a-z0-9]*)/i';
  310. $urlregs[] ='/tinyurl\.com\/([a-z0-9]{5}[a-z0-9]*)/i';
  311. $urlregs[] ='/bit\.ly\/([a-z0-9]{5}[a-z0-9]*)/i';
  312. $urlregs[] ='/twurl\.nl\/([a-z0-9]{5}[a-z0-9]*)/i';
  313. $urlregs[] ='/j\.mp\/([a-z0-9]{5}[a-z0-9]*)/i';
  314. $urlregs[] ='/retwt\.me\/([a-z0-9]{5}[a-z0-9]*)/i';
  315. $urlregs[] ='/ff\.im\/-([a-z0-9]{5}[a-z0-9]*)/i';
  316. $urlregs[] ='/tr\.im\/([a-z0-9]{5}[a-z0-9]*)/i';
  317. $urlregs[] ='/htxt\.it\/([a-z0-9]{4}[a-z0-9]*)/i';
  318. $urlregs[] ='/aa\.cx\/([a-z0-9]*)/i';
  319. $urlregs[] ='/2\.ly\/([a-z0-9]*)/i';
  320. //$text .='!!!';
  321. foreach($urlregs as $urlreg) {
  322. if(preg_match($urlreg,$text,$match)){
  323. $request = 'http://api.unshort.me/?r='.urlencode('http://' . $match[0]);
  324. $tmpstr = processcurl($request);
  325. $url = substr($tmpstr,13+strpos($tmpstr,"<resolvedURL>"),strpos($tmpstr,"</resolvedURL>") - 13 - strpos($tmpstr,"<resolvedURL>"));
  326. $text = str_replace('http://'.$match[0], $url, $text);
  327. }
  328. }
  329. return $text;
  330. }
  331. function utf8_substr($str,$start){
  332. preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/", $str, $ar);
  333. var_dump($ar);
  334. if(func_num_args() >= 3) {
  335. $end = func_get_arg(2);
  336. return join("",array_slice($ar[0],$start,$end));
  337. } else {
  338. return join("",array_slice($ar[0],$start));
  339. }
  340. }
  341. function utf8_strlen($str){
  342. preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/", $str, $ar);
  343. var_dump($ar);
  344. return count($ar[0]);
  345. }
  346. function processcurl($url,$postargs=false){
  347. $ch = curl_init($url);
  348. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  349. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  350. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  351. $response = curl_exec($ch);
  352. $responseInfo=curl_getinfo($ch);
  353. curl_close($ch);
  354. if( intval( $responseInfo['http_code'] ) == 200 )
  355. return $response;
  356. else
  357. return false;
  358. }
  359. function getU(){
  360. if(isset($_COOKIE['un']))
  361. return trim($_COOKIE['un']);
  362. return '';
  363. }
  364. function getP(){
  365. if(isset($_COOKIE['up']))
  366. return trim($_COOKIE['up']);
  367. return '';
  368. }
  369. function isLogin(){
  370. if(isset($_COOKIE['l'])){
  371. return $_COOKIE['l'] == 'OK';
  372. }
  373. return false;
  374. }
  375. function compress($content){
  376. if( !headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")){
  377. $content = gzencode($content." \n",1);
  378. header("Content-Encoding: gzip");
  379. header("Vary: Accept-Encoding");
  380. header("Content-Length: ".strlen($content));
  381. }
  382. return $content;
  383. }
  384. ?>