Created
April 28, 2009 01:58
-
-
Save ericsk/102883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FlickrController extends Zend_Controller_Action { | |
const PUBLIC_KEY = '9c4d2130b524e3ae884a55826d570503'; | |
const PRIVATE_KEY = 'b296b8bf8ee7887d'; | |
public function init() { | |
$this->systemp = '/home/www/sys_tmp'; | |
$this->_helper->layout()->disableLayout(); | |
# 檢查登入 | |
if (isset($_SESSION['signin_url']) || !isset($_SESSION['user_sn'])) { | |
$this->_redirect('/@login?furl=' . urlencode($_SERVER['REQUEST_URI'])); | |
return; | |
} | |
$this->db = Zend_Db::factory('Oracle', array( | |
'host' => 'localhost', | |
'username' => 'aluba', | |
'password' => 'alubackli', | |
'dbname' => 'album' | |
)); | |
$this->flickr = new Zend_XmlRpc_Client('http://api.flickr.com/services/xmlrpc/'); | |
} | |
public function indexAction() { | |
$api_sig = hash("md5", FlickrController::PRIVATE_KEY."api_key".FlickrController::PUBLIC_KEY."permsread"); | |
$this->view->flickrAuthUrl = "http://flickr.com/services/auth/?api_key=".FlickrController::PUBLIC_KEY."&perms=read&api_sig=$api_sig"; | |
$this->_helper->layout()->setLayout('my'); | |
$this->view->auth_page = true; | |
} | |
public function authAction() { | |
$this->_helper->viewRenderer->setNoRender(); | |
$frob = $this->_getParam('frob'); | |
# read token | |
$resp = $this->_doFlickrCall('flickr.auth.getToken', array('frob' => $frob)); | |
$dom = new DOMDocument(); | |
$dom->loadXML($resp); | |
$token = $dom->getElementsByTagName('token')->item(0)->nodeValue; | |
$uid = $dom->getElementsByTagName('user')->item(0)->getAttribute('nsid'); | |
# store the uid and token info into database | |
$data = $this->db->fetchRow("select * from FLICKR where USER_SN = '".$_SESSION['user_sn']."'"); | |
if ($data == null) { | |
$data = array( | |
'USER_SN' => $_SESSION['user_sn'], | |
'FLICKR_UID' => $uid, | |
'AUTH_TOKEN' => $token | |
); | |
$this->db->insert('FLICKR', $data); | |
} else { | |
$data = array('AUTH_TOKEN' => $token, 'FLICKR_UID' => $uid); | |
$this->db->update('FLICKR', $data, "USER_SN = '".$_SESSION['user_sn']."'"); | |
} | |
# redirect to start moving | |
$this->_redirect('/_flickr/move'); | |
} | |
public function moveAction() { | |
$row = $this->db->fetchRow("select * from FLICKR where USER_SN = '".$_SESSION['user_sn']."'"); | |
$resp = $this->_doFlickrCall('flickr.auth.checkToken', array( | |
'auth_token' => $row['AUTH_TOKEN'] | |
)); | |
$dom = new DOMDocument(); | |
$dom->loadXML($resp); | |
$perms = $dom->getElementsByTagName('perms')->item(0)->nodeValue; | |
if ($perms == 'none') { | |
$this->_redirect('/_flickr/index'); | |
return; | |
} | |
$resp = $this->_doFlickrCall('flickr.people.getPublicPhotos', array( | |
'user_id' => $row['FLICKR_UID'], | |
'per_page' => '5' | |
)); | |
$dom = new DOMDocument(); | |
$dom->loadXML($resp); | |
$p = $dom->getElementsByTagName('photos')->item(0); | |
$this->view->photosData = array( | |
'page' => $p->getAttribute('page'), | |
'pages' => $p->getAttribute('pages'), | |
'perpage' => $p->getAttribute('perpage'), | |
'total' => $p->getAttribute('total') | |
); | |
$photoElems = $dom->getElementsByTagName('photo'); | |
# check pro | |
$resp = $this->_doFlickrCall('flickr.people.getInfo', array( | |
'user_id' => $row['FLICKR_UID'] | |
)); | |
$dom = new DOMDocument(); | |
$dom->loadXML($resp); | |
$is_pro = $dom->getElementsByTagName('person')->item(0)->getAttribute('ispro'); | |
$photos = array(); | |
foreach ($photoElems as $pe) { | |
$photo_id = $pe->getAttribute('id'); | |
if ($is_pro == '1') { | |
$resp = $this->_doFlickrCall('flickr.photos.getInfo', array( | |
'photo_id' => $photo_id | |
)); | |
$dom = new DOMDocument(); | |
$dom->loadXML($resp); | |
$p = $dom->getElementsByTagName('photo')->item(0); | |
$server = $p->getAttribute('server'); | |
$pid = $p->getAttribute('id'); | |
$secret = $p->getAttribute('originalsecret'); | |
$otype = $p->getAttribute('originalformat'); | |
$scale = 'o'; | |
} else { | |
$server = $pe->getAttribute('server'); | |
$secret = $pe->getAttribute('secret'); | |
$scale = 'b'; | |
$otype = 'jpg'; | |
} | |
$photos[] = array( | |
'url' => "http://farm1.static.flickr.com/$server/$photo_id"."_$secret"."_$scale.$otype" | |
); | |
} | |
$this->view->photos = $photos; | |
} | |
private function _doFlickrCall($method, $params) { | |
if (!isset($params['api_key'])) { | |
$params['api_key'] = FlickrController::PUBLIC_KEY; | |
} | |
if (!isset($params['method'])) { | |
$params['method'] = $method; | |
} | |
ksort($params); | |
$raw = FlickrController::PRIVATE_KEY; | |
foreach($params as $key => $val) { | |
$raw .= "$key$val"; | |
} | |
$params['api_sig'] = hash('md5', $raw); | |
return $this->flickr->call($method, array($params)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment