# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # /* Class "Twitter" developed by: Sergio C. R. aka "Omega" Contact: scr.omega@gmail.com */ class twitter { private static $ch, $ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20'; public static function login($user, $pass) { self::$ch = curl_init(); curl_setopt(self::$ch, CURLOPT_HEADER, true); curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true); curl_setopt(self::$ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt(self::$ch, CURLOPT_USERAGENT, self::$ua); curl_setopt(self::$ch, CURLOPT_URL, 'http://twitter.com/login'); $result = curl_exec(self::$ch); curl_close(self::$ch); preg_match('#Set-Cookie:(.*?);#', $result, $a); $cookie = trim($a[1]); preg_match('#name="authenticity_token" type="hidden" value="(.*?)"#', $result, $a); $token = trim($a[1]); $args = 'authenticity_token='.$token.'&session%5Busername_or_email%5D='.urlencode($user).'&session%5Bpassword%5D='.urlencode($pass).'&commit=Sign+In'; self::$ch = curl_init(); curl_setopt(self::$ch, CURLOPT_HEADER, true); curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true); curl_setopt(self::$ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt(self::$ch, CURLOPT_USERAGENT, self::$ua); curl_setopt(self::$ch, CURLOPT_COOKIE, $cookie); curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $args); curl_setopt(self::$ch, CURLOPT_POST, 1); curl_setopt(self::$ch, CURLOPT_URL, 'http://twitter.com/sessions'); $result = curl_exec(self::$ch); curl_close(self::$ch); preg_match_all('#Set-Cookie:(.*?);#',$result, $a); $cookie = trim(implode(';',$a[1])); self::$ch = curl_init(); curl_setopt(self::$ch, CURLOPT_HEADER, true); curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true); curl_setopt(self::$ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt(self::$ch, CURLOPT_USERAGENT, self::$ua); curl_setopt(self::$ch, CURLOPT_COOKIE, $cookie); curl_setopt(self::$ch, CURLOPT_URL, 'http://twitter.com/home'); $result = curl_exec(self::$ch); curl_close(self::$ch); preg_match('##',$result, $a); if($a[1] == 'y') { preg_match('#Set-Cookie: _twitter_sess=(.*?);#',$result, $a); return trim($a[1]); } } private static function parse_notes($html) { preg_match_all('#
  • #',$html, $a0); # Id preg_match_all('#(.*?)#',$html, $a1); # Username preg_match_all('#class="photo fn" height="48" src="(.*?)" width="48" />#',$html, $a2); # Avatar preg_match_all('#(.*?)#',$html, $a3); # Status preg_match_all('##',$html, $a4); # Timestamp preg_match_all('#from (.*?)#',$html, $a5); array_shift($a5[1]); # From foreach($a0[2] as $c=>$id) { $a3[1][$c] = preg_replace('/\([^"]*)<\/a>/u', '\\1', $a3[1][$c]); $statuses[$id] = array( 'username'=>trim($a1[3][$c]), 'avatar'=>trim($a2[1][$c]), 'status'=>trim(strip_tags($a3[1][$c])), 'timestamp'=>strtotime(trim($a4[1][$c])), 'from'=>trim(strip_tags($a5[1][$c])) ); } return $statuses; } public static function notes($cookie, $replies=false, $pag=1) { self::$ch = curl_init(); curl_setopt(self::$ch, CURLOPT_HEADER, false); curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true); curl_setopt(self::$ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt(self::$ch, CURLOPT_USERAGENT, self::$ua); curl_setopt(self::$ch, CURLOPT_COOKIE, '_twitter_sess='.$cookie); $url = ($replies) ? 'replies' : 'home'; curl_setopt(self::$ch, CURLOPT_URL, 'http://twitter.com/'.$url.'?page='.$pag); $result = curl_exec(self::$ch); curl_close(self::$ch); preg_match('##', $result, $a); if($a[1] == 'y') { return self::parse_notes($result); } else { return 'ko'; } } } ?>