10 require_once
'IAuthService.php';
37 protected $jsArguments = array();
44 protected $attributes = array();
50 protected $authenticated =
false;
55 private $fetched =
false;
65 private $redirectUrl =
'';
70 private $cancelUrl =
'';
106 public function init($component, $options = array()) {
107 if (isset($component))
110 foreach ($options as $key => $val)
114 $server = Yii::app()->request->getHostInfo();
115 $path = Yii::app()->request->getPathInfo();
148 return $this->jsArguments;
156 $this->component = $component;
164 return $this->component;
172 $this->redirectUrl = $url;
180 return $this->redirectUrl;
188 $this->cancelUrl = $url;
196 return $this->cancelUrl;
212 return $this->authenticated;
220 $this->component->redirect(isset($url) ? $url : $this->redirectUrl,
true);
227 $this->component->redirect(isset($url) ? $url : $this->cancelUrl,
false);
237 protected function makeRequest($url, $options = array(), $parseJson =
true) {
240 if (isset($options[
'referer']))
241 curl_setopt($ch, CURLOPT_REFERER, $options[
'referer']);
243 if (isset($options[
'query'])) {
244 $url_parts = parse_url($url);
245 if (isset($url_parts[
'query'])) {
246 $old_query = http_build_query($url_parts[
'query']);
247 $url_parts[
'query'] = array_merge($url_parts[
'query'], $options[
'query']);
248 $new_query = http_build_query($url_parts[
'query']);
249 $url = str_replace($old_query, $new_query, $url);
252 $url_parts[
'query'] = $options[
'query'];
253 $new_query = http_build_query($url_parts[
'query']);
254 $url .=
'?'.$new_query;
258 if (isset($options[
'data'])) {
259 curl_setopt($ch, CURLOPT_POST, 1);
260 curl_setopt($ch, CURLOPT_POSTFIELDS, $options[
'data']);
263 curl_setopt($ch, CURLOPT_URL, $url);
265 $result = curl_exec($ch);
266 $headers = curl_getinfo($ch);
268 if (curl_errno($ch) > 0)
271 if ($headers[
'http_code'] != 200) {
273 'Invalid response http code: '.$headers[
'http_code'].
'.'.PHP_EOL.
274 'URL: '.$url.PHP_EOL.
275 'Options: '.var_export($options,
true).PHP_EOL.
277 CLogger::LEVEL_ERROR,
'application.extensions.eauth'
279 throw new EAuthException(Yii::t(
'eauth',
'Invalid response http code: {code}.', array(
'{code}' => $headers[
'http_code'])), $headers[
'http_code']);
300 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
301 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
302 curl_setopt($ch, CURLOPT_HEADER, 0);
303 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
304 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
315 $result = json_decode($response);
317 if (!isset($result)) {
318 throw new EAuthException(Yii::t(
'eauth',
'Invalid response format.', array()), 500);
320 else if (isset($error)) {
326 catch(Exception $e) {
337 if (isset($json->error)) {
340 'message' =>
'Unknown error occurred.',
351 return '__eauth_'.$this->getServiceName().
'__';
362 protected function setState($key, $value, $defaultValue = null) {
363 $session = Yii::app()->session;
365 if($value === $defaultValue)
366 unset($session[$key]);
368 $session[$key] = $value;
377 $session = Yii::app()->session;
379 return isset($session[$key]);
390 protected function getState($key, $defaultValue = null) {
391 $session = Yii::app()->session;
393 return isset($session[$key]) ? $session[$key] : $defaultValue;
409 if (!$this->fetched) {
410 $this->fetched =
true;
413 $this->fetched = $result;
423 return $this->attributes[
'id'];
432 $attributes = array();
433 foreach ($this->attributes as $key => $val) {
447 $getter =
'get'.$key;
448 if (method_exists($this, $getter))
449 return $this->$getter();
451 return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
461 return isset($this->attributes[$key]);
469 $item =
new stdClass;
471 if (empty($this->title))
472 $item->title = $this->
getId();