oops-OAUTH2
[ class tree: oops-OAUTH2 ] [ index: oops-OAUTH2 ] [ all elements ]

Source for file OAUTH2.php

Documentation is available at OAUTH2.php

  1. <?php
  2. /**
  3.  * Project: oops\OAUTH2:: OAUTH2 pear package<br>
  4.  * File:    OAUTH2.php<br>
  5.  * Dependency:
  6.  *   - {@link http://pear.oops.org/docs/li_HTTPRelay.html oops/HTTPRelay}
  7.  *   - {@link http://pear.oops.org/docs/li_myException.html oops/myException}
  8.  *   - {@link http://kr1.php.net/manual/en/book.curl.php curl extension}
  9.  *
  10.  * oops\OAUTH2 pear package는 OAUTH2 login 및 profile 정보를
  11.  * 다루기 위한 library이다.
  12.  *
  13.  * 이 package를 사용하기 위해서는 먼저 각 모듈의 벤더 페이지에서
  14.  * Application ID와 Application Secret을 발급받아야 한다. 각 모듈의
  15.  * 상단 주석을 참조하라.
  16.  *
  17.  * 현재 GOOGLE, FACEBOOK, NAVER, GITHUB, KAKAO 를 지원한다.
  18.  *
  19.  * @category  HTTP
  20.  * @package   oops\OAUTH2
  21.  * @author    JoungKyun.Kim <http://oops.org>
  22.  * @copyright (c) 2019, OOPS.org
  23.  * @license   BSD License
  24.  * @link      http://pear.oops.org/package/OAUTH2
  25.  * @example   OAUTH2/tests/test.php OAUTH2 pear package 예제 코드
  26.  * @filesource
  27.  */
  28.  
  29. /**
  30.  * Namespace oops;
  31.  */
  32. namespace oops;
  33.  
  34. /**
  35.  * import myException class
  36.  */
  37. require_once 'myException.php';
  38.  
  39. /**
  40.  * oops\OAUth2 pear package의 main class
  41.  *
  42.  * OAUTH2를 이용하여 로그인을 진행하고, 로그인된 사용자의
  43.  * 프로필 정보를 관리한다.
  44.  *
  45.  * 현재 GOOGLE, FACEBOOK, NAVER, GITHUB, KAKAO 를 지원한다.
  46.  *
  47.  * @package oops/OAUTH2
  48.  * @author JoungKyun.Kim <http://oops.org>
  49.  * @copyright (c) 2019, OOPS.org
  50.  * @license BSD License
  51.  * @example OAUTH2/tests/test.php OAUTH2 pear 예제 코드
  52.  */
  53. Class OAUth2 {
  54.     // {{{ properities
  55.     /**
  56.      * @access public
  57.      * @var stdClass 벤더 정고
  58.      */
  59.     public $vendor;
  60.     /**
  61.      * access private
  62.      * @var object 
  63.      */
  64.     private $o;
  65.     // }}}
  66.  
  67.     // {{{ +-- public (void) __construct ($app)
  68.     /**
  69.      * OAUTH2 로그인 인증 과정을 수행한다. 인증 과정 중에 에러가
  70.      * 발생하면 myException으로 에러 메시지를 보낸다.
  71.      *
  72.      * logout 시에 globale 변수 $_OAUTH2_LOGOUT_TEMPALTE_ 로 사용자 logout template
  73.      * 을 지정할 수 있다. template 파일은 pear/OAUTH2/login.template 를 참조하면 된다.
  74.      *
  75.      * @access public
  76.      * @param stdClass $app  로그인 정보
  77.      *    - vendor    OAUTH2 Service Provider (현재 google/facebook/naver/kakao 지원)
  78.      *    - id        Service Provider 에서 발급받은 client ID
  79.      *    - secret    Service Provider 에서 발급받은 client secret key
  80.      *    - callback  이 class가 호출되는 URL (또는 provider에 등록한 callback url)
  81.      *    - popup     Facebook 처럼 popup login 창을 지원하는 경우 ture 로 지정
  82.      * @return void 
  83.      */
  84.     function __construct ($app{
  85.         $this->vendor = strtoupper ($app->vendor);
  86.  
  87.         $resource sprintf ('OAUTH2/%s.php'$this->vendor);
  88.  
  89.         if $this->file_exists ($resource) )
  90.             throw new \myException (sprintf ('Unsupport vendor "%s"'$app->vendor));
  91.  
  92.         require_once $resource;
  93.  
  94.         $className sprintf ('oops\OAUTH2\%s'$this->vendor);
  95.         $this->new $className ($app);
  96.     }
  97.     // }}}
  98.  
  99.     // {{{ +-- public Profile (void)
  100.     /**
  101.      * 로그인이 성공 후에, 로그인 사용자의 Profile을 가져오기 위한
  102.      * API
  103.      *
  104.      * @access public
  105.      * @return stdClass 사용자 Profile
  106.      *    - id    사용자 UID
  107.      *    - name  사용자 Nickname
  108.      *    - email 사용자 email (Provider에 따라 없을 수도 있다.)
  109.      *    - img   사용자 profile image url
  110.      *    - r     각 provider에서 제공하는 original profile 값
  111.      */
  112.     public function Profile ({
  113.         return $this->o->Profile ();
  114.     }
  115.     // }}}
  116.  
  117.     // {{{ +-- static public (stdClass) image ($url, $noprint = false)
  118.     /**
  119.      * 외부 이미지를 읽어와서 출력한다. HTTPS protocold을 사용할
  120.      * 경우 provider에서 https image를 지원하지 않을 경우 사용.
  121.      *
  122.      * @access public
  123.      * @param string $url 원본 image URL
  124.      * @param bool $noprint (optional) true로 설정하면 출력하지
  125.      *              않고 반환한다. (default: false)
  126.      * @return stdClass 2번째 인자가 true일 경우에는 void 이다.
  127.      *    - type  gif/jpg/png 중 하나
  128.      *    - data  image raw data
  129.      */
  130.     static public function image ($url$noprint false{
  131.         if $url )
  132.             return;
  133.  
  134.         if preg_match ('!^//!'$url) ) {
  135.             if $_SERVER['HTTPS')
  136.                 $url 'https:' $url;
  137.             else
  138.                 $url 'http:' $url;
  139.         }
  140.         if ($url filter_var($urlFILTER_VALIDATE_URL)) )
  141.             return;
  142.  
  143.         $c curl_init ();
  144.         curl_setopt ($cCURLOPT_URL$url);
  145.         curl_setopt ($cCURLOPT_TIMEOUT60);
  146.         curl_setopt ($cCURLOPT_NOPROGRESS1);
  147.         curl_setopt ($cCURLOPT_RETURNTRANSFER1);
  148.         curl_setopt ($cCURLOPT_USERAGENT"OAUTH2 pear package");
  149.  
  150.         curl_setopt ($cCURLOPT_HEADER0);
  151.         curl_setopt ($cCURLOPT_NOBODY0);
  152.         curl_setopt ($cCURLOPT_FAILONERROR1);
  153.  
  154.         $data curl_exec($c);
  155.         $info = (object) curl_getinfo ($c);
  156.  
  157.         if $info->http_code == 302 )
  158.             return self::image ($info->redirect_url$noprint);
  159.  
  160.         $ctype $info->content_type;
  161.  
  162.         if $noprint {
  163.             Header ('Content-Type: ' $ctype);
  164.             echo $data;
  165.         }
  166.  
  167.         curl_close ($c);
  168.  
  169.         if $noprint {
  170.             preg_match ('!/([a-z]+)!'$ctype$matches);
  171.             if $matches[1== 'jpeg' )
  172.                 $matches[1'jpg';
  173.  
  174.             return (object) array (
  175.                 'type' => $matches[1],
  176.                 'data' => $data
  177.             );    
  178.         }
  179.     }
  180.     // }}}
  181.  
  182.     // {{{ +-- private (mixed) ini_get ($var)
  183.     /**
  184.      * Gets the value of a configuration option
  185.      *
  186.      * @access private
  187.      * @param string $var ini option 이름
  188.      * @return mixed Returns the value of the configuration option as
  189.      *          a string on success, or an empty string for null values.
  190.      *          Returns FALSE if the configuration option doesn't exist.
  191.      */
  192.     private function ini_get ($var{
  193.         $func function_exists ('___ini_get''___' '';
  194.         $func .= 'ini_get';
  195.         return $func ($var);
  196.     }
  197.     // }}}
  198.  
  199.     // {{{ +-- private (bool) file_exists ($f)
  200.     /**
  201.      * 파일이나 디렉토리가 존재하는지 여부를 판단한다. pure file_exists
  202.      * 와의 차이는 include_path를 지원한다.
  203.      *
  204.      * @access private
  205.      * @param string $f 파일 경로
  206.      * @return bool 지정한 파일이나 디렉토리가 있으면 true를 반환하고
  207.      *               없으면 false를 반환
  208.      */
  209.     private function file_exists ($f{
  210.         if @file_exists ($f) )
  211.             return true;
  212.  
  213.         $entry preg_split ('/:/'$this->ini_get ('include_path'));
  214.         foreach ($entry as $base{
  215.             if @file_exists ($base '/' $f) )
  216.                 return true;
  217.         }
  218.  
  219.         return false;
  220.     }
  221.     // }}}
  222. }
  223.  
  224. /*
  225.  * Local variables:
  226.  * tab-width: 4
  227.  * c-basic-offset: 4
  228.  * End:
  229.  * vim600: noet sw=4 ts=4 fdm=marker
  230.  * vim<600: noet sw=4 ts=4
  231.  */

Documentation generated on Tue, 14 May 2019 02:00:41 +0900 by phpDocumentor 1.4.4