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

OAUTH2 pear 예제 코드

  1. <?php
  2. /*
  3.  * oops\AUTH2 test page
  4.  *
  5.  * dependency
  6.  *
  7.  * pear oops/myException
  8.  * pear oops/HTTPRelay
  9.  */
  10.  
  11.  
  12. $devel = false;
  13. if ( $devel == true ) {
  14.     $iniget = function_exists ('___ini_get') ? '___ini_get' : 'ini_get';
  15.     $iniset = function_exists ('___ini_set') ? '___ini_set' : 'ini_set';
  16.  
  17.     $cwd = getcwd ();
  18.     $ccwd = basename ($cwd);
  19.     if ( $ccwd == 'tests' ) {
  20.         $oldpath = $iniget ('include_path');
  21.         $newpath = preg_replace ("!/{$ccwd}!", '', $cwd);
  22.         $iniset ('include_path', $newpath . ':' . $oldpath);
  23.     }
  24. }
  25.  
  26. require_once 'OAUTH2.php';
  27.  
  28. set_error_handler ('myException::myErrorHandler');
  29.  
  30. $callback = sprintf (
  31.     '%s://%s%s',
  32.     $_SERVER['HTTPS'] ? 'https' : 'http',
  33.     $_SERVER['HTTP_HOST'],
  34.     $_SERVER['REQUEST_URI']
  35. );
  36.  
  37. $appId = (object) array (
  38.     'vendor'   => 'google',
  39.     'id'       => 'APPLICATION_ID',
  40.     'secret'   => 'APPLICATION_SECRET_KEY',
  41.     'callback' => $callback,
  42. );
  43.  
  44. try {
  45.     $oauth2 = new oops\OAUTH2 ($appId);
  46.  
  47.     // logout 시에는 callback url에 logout parameter를 추가하고,
  48.     // logout 후에 redirect가 필요하면 redirect parameter까지 추가한다.
  49.     if ( isset ($_GET['logout']) ) {
  50.         unset ($_SESSION['oauth2']);
  51.  
  52.         if ( $_GET['redirect'] )
  53.             Header ('Location: ' . $redirect);
  54.  
  55.         printf ('%s Complete logout', strtoupper ($appId->vendor));
  56.         exit;
  57.     }
  58.  
  59.     $user = $oauth2->Profile ();
  60.     $uid = sprintf ('%s:%s', $appId->vendor, $user->id);
  61.     $_SESSION['oauth2'] = (object) array (
  62.         'uid' => $uid,
  63.         'name' => $user->name,
  64.         'email' => $user->email,
  65.         'img' => $user->img,
  66.         'logout' => $callback . '?logout'
  67.     );
  68.  
  69.     print_r ($_SESS['oauth2']);
  70. } catch ( myException $e ) {
  71.     echo $e->Message () . "\n";
  72.     print_r ($e->TraceAsArray);
  73.     $e->finalize ();
  74. }

Documentation generated on Fri, 30 Aug 2024 06:11:05 +0900 by phpDocumentor 1.4.4