* $Id$ * * = how to use = * */ class Service_Hatena_Auth { /** * */ const HATENA_AUTH_URL = 'http://auth.hatena.ne.jp/auth'; /** * */ const HATENA_INFO_URL = 'http://auth.hatena.ne.jp/api/auth.json'; /** * */ protected $_apiKey = null; /** * */ protected $_secretKey = null; /** * */ protected $_lastResult = null; /** * */ public function __construct($secretKey, $apiKey) { $this->_secretKey = $secretKey; $this->_apiKey = $apiKey; } /** * */ public function getSignInUrl($params = array()) { $params['api_key'] = $this->_apiKey; ksort($params); $queries = array(); foreach ($params as $key => $value) { $queries[] = rawurlencode($key) . '=' . rawurlencode($value); } $queries[] = 'api_sig=' . $this->_makeSignature($params); $url = self::HATENA_AUTH_URL . '?' . implode('&', $queries); return $url; } /** * */ public function getInfoUrl($cert = null) { if (empty($cert)) {$cert = isset($_GET['cert']) ? $_GET['cert'] : null;} $params = array( 'api_key' => $this->_apiKey, 'cert' => $cert, ); $params['api_sig'] = $this->_makeSignature($params); $queries = array(); foreach ($params as $key => $value) { $queries[] = rawurlencode($key) . '=' . rawurlencode($value); } $url = self::HATENA_INFO_URL . '?' . implode('&', $queries); return $url; } /** * */ public function validate($cert = null) { $url = $this->getInfoUrl($cert); $json = file_get_contents($url); require_once 'Zend/Json.php'; $this->_lastResult = Zend_Json::decode($json); if (isset($this->_lastResult['has_error']) && ($this->_lastResult['has_error'] == false)) { return true; } return false; } /** * */ public function getName() { if (isset($this->_lastResult['user']['name'])) { return $this->_lastResult['user']['name']; } } /** * */ public function getLastResult() { return $this->_lastResult; } /** * */ protected function _makeSignature($params) { ksort($params); $message = ''; foreach ($params as $key => $value) { $message .= $key . $value; } $message = $this->_secretKey . $message; $sig = md5($message); return $sig; } }