SimpleOpenID Class Reference

List of all members.

Public Member Functions

Public Attributes


Detailed Description

Definition at line 66 of file SimpleOpenID.php.


Member Function Documentation

SimpleOpenID::array2url ( arr  ) 

Definition at line 182 of file SimpleOpenID.php.

Referenced by CURL_Request(), and GetRedirectURL().

00182                                 { // converts associated array to URL Query String
00183                 if (!is_array($arr)){
00184                         return false;
00185                 }
00186                 $query = '';
00187                 foreach($arr as $key => $value){
00188                         $query .= $key . "=" . $value . "&";
00189                 }
00190                 return $query;
00191         }

SimpleOpenID::CURL_Request ( url,
method = "GET",
params = "" 
)

Definition at line 214 of file SimpleOpenID.php.

Referenced by GetOpenIDServer().

00214                                                                  { // Remember, SSL MUST BE SUPPORTED
00215                         if (is_array($params)) $params = $this->array2url($params);
00216 
00217                         $curl = curl_init($url . ($method == "GET" && $params != "" ? "?" . $params : ""));
00218                         @curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
00219                         curl_setopt($curl, CURLOPT_HEADER, false);
00220                         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
00221                         curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET"));
00222                         curl_setopt($curl, CURLOPT_POST, ($method == "POST"));
00223                         if ($method == "POST") curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
00224                         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
00225                         $response = curl_exec($curl);
00226 
00227                         if (curl_errno($curl) == 0){
00228                                 $response;
00229                         }else{
00230                                 $this->ErrorStore('OPENID_CURL', curl_error($curl));
00231                         }
00232                         return $response;
00233         }

SimpleOpenID::ErrorStore ( code,
desc = null 
)

Definition at line 134 of file SimpleOpenID.php.

Referenced by CURL_Request(), FSOCK_Request(), and GetOpenIDServer().

00134                                                 {
00135                 $errs['OPENID_NOSERVERSFOUND'] = 'Cannot find OpenID Server TAG on Identity page.';
00136                 if ($desc == null){
00137                         $desc = $errs[$code];
00138                 }
00139                 $this->error = array($code,$desc);
00140         }

SimpleOpenID::FSOCK_Request ( url,
method = "GET",
params = "" 
)

Definition at line 192 of file SimpleOpenID.php.

00192                                                                  {
00193                 $fp = fsockopen("ssl://www.myopenid.com", 443, $errno, $errstr, 3); // Connection timeout is 3 seconds
00194                 if (!$fp) {
00195                         $this->ErrorStore('OPENID_SOCKETERROR', $errstr);
00196                         return false;
00197                 } else {
00198                         $request = $method . " /server HTTP/1.0\r\n";
00199                         $request .= "User-Agent: Simple OpenID PHP Class (http://www.phpclasses.org/simple_openid)\r\n";
00200                         $request .= "Connection: close\r\n\r\n";
00201                         fwrite($fp, $request);
00202                         stream_set_timeout($fp, 4); // Connection response timeout is 4 seconds
00203                         $res = fread($fp, 2000);
00204                         $info = stream_get_meta_data($fp);
00205                         fclose($fp);
00206 
00207                         if ($info['timed_out']) {
00208                        $this->ErrorStore('OPENID_SOCKETTIMEOUT');
00209                         } else {
00210                         return $res;
00211                         }
00212                 }
00213         }

SimpleOpenID::GetError (  ) 

Definition at line 129 of file SimpleOpenID.php.

00129                            {
00130                 $e = $this->error;
00131                 return array('code'=>$e[0],'description'=>$e[1]);
00132         }

SimpleOpenID::GetIdentity (  ) 

Definition at line 126 of file SimpleOpenID.php.

00126                               {         // Get Identity
00127                 return $this->openid_url_identity;
00128         }

SimpleOpenID::GetOpenIDServer (  ) 

Definition at line 253 of file SimpleOpenID.php.

00253                                   {
00254                 $response = $this->CURL_Request($this->openid_url_identity);
00255                 list($servers, $delegates) = $this->HTML2OpenIDServer($response);
00256                 if (count($servers) == 0){
00257                         $this->ErrorStore('OPENID_NOSERVERSFOUND');
00258                         return false;
00259                 }
00260                 if (isset($delegates[0])
00261                   && ($delegates[0] != "")){
00262                         $this->SetIdentity($delegates[0]);
00263                 }
00264                 $this->SetOpenIDServer($servers[0]);
00265                 return $servers[0];
00266         }

SimpleOpenID::GetRedirectURL (  ) 

Definition at line 268 of file SimpleOpenID.php.

Referenced by Redirect().

00268                                  {
00269                 $params = array();
00270                 $params['openid.return_to'] = urlencode($this->URLs['approved']);
00271                 $params['openid.mode'] = 'checkid_setup';
00272                 $params['openid.identity'] = urlencode($this->openid_url_identity);
00273                 $params['openid.trust_root'] = urlencode($this->URLs['trust_root']);
00274 
00275                 if (isset($this->fields['required'])
00276                   && (count($this->fields['required']) > 0)) {
00277                         $params['openid.sreg.required'] = implode(',',$this->fields['required']);
00278                 }
00279                 if (isset($this->fields['optional'])
00280                   && (count($this->fields['optional']) > 0)) {
00281                         $params['openid.sreg.optional'] = implode(',',$this->fields['optional']);
00282                 }
00283                 return $this->URLs['openid_server'] . "?". $this->array2url($params);
00284         }

SimpleOpenID::HTML2OpenIDServer ( content  ) 

Definition at line 235 of file SimpleOpenID.php.

Referenced by GetOpenIDServer().

00235                                               {
00236                 $get = array();
00237 
00238                 // Get details of their OpenID server and (optional) delegate
00239                 preg_match_all('/<link[^>]*rel=[\'"]openid.server[\'"][^>]*href=[\'"]([^\'"]+)[\'"][^>]*\/?>/i', $content, $matches1);
00240                 preg_match_all('/<link[^>]*href=\'"([^\'"]+)[\'"][^>]*rel=[\'"]openid.server[\'"][^>]*\/?>/i', $content, $matches2);
00241                 $servers = array_merge($matches1[1], $matches2[1]);
00242 
00243                 preg_match_all('/<link[^>]*rel=[\'"]openid.delegate[\'"][^>]*href=[\'"]([^\'"]+)[\'"][^>]*\/?>/i', $content, $matches1);
00244 
00245                 preg_match_all('/<link[^>]*href=[\'"]([^\'"]+)[\'"][^>]*rel=[\'"]openid.delegate[\'"][^>]*\/?>/i', $content, $matches2);
00246 
00247                 $delegates = array_merge($matches1[1], $matches2[1]);
00248 
00249                 $ret = array($servers, $delegates);
00250                 return $ret;
00251         }

SimpleOpenID::IsError (  ) 

Definition at line 142 of file SimpleOpenID.php.

00142                           {
00143                 if (count($this->error) > 0){
00144                         return true;
00145                 }else{
00146                         return false;
00147                 }
00148         }

SimpleOpenID::OpenID_Standarize ( openid_identity = null  ) 

Definition at line 163 of file SimpleOpenID.php.

00163                                                            {
00164                 if ($openid_identity === null)
00165                         $openid_identity = $this->openid_url_identity;
00166 
00167                 $u = parse_url(strtolower(trim($openid_identity)));
00168 
00169                 if (!isset($u['path']) || ($u['path'] == '/')) {
00170                         $u['path'] = '';
00171                 }
00172                 if(substr($u['path'],-1,1) == '/'){
00173                         $u['path'] = substr($u['path'], 0, strlen($u['path'])-1);
00174                 }
00175                 if (isset($u['query'])){ // If there is a query string, then use identity as is
00176                         return $u['host'] . $u['path'] . '?' . $u['query'];
00177                 }else{
00178                         return $u['host'] . $u['path'];
00179                 }
00180         }

SimpleOpenID::Redirect (  ) 

Definition at line 286 of file SimpleOpenID.php.

00286                            {
00287                 $redirect_to = $this->GetRedirectURL();
00288                 if (headers_sent()){ // Use JavaScript to redirect if content has been previously sent (not recommended, but safe)
00289                         echo '<script language="JavaScript" type="text/javascript">window.location=\'';
00290                         echo $redirect_to;
00291                         echo '\';</script>';
00292                 }else{  // Default Header Redirect
00293                         header('Location: ' . $redirect_to);
00294                 }
00295         }

SimpleOpenID::SetApprovedURL ( a  ) 

Definition at line 89 of file SimpleOpenID.php.

00089                                    {
00090                 $this->URLs['approved'] = $a;
00091         }

SimpleOpenID::SetCancelURL ( a  ) 

Definition at line 86 of file SimpleOpenID.php.

00086                                  {
00087                 $this->URLs['cancel'] = $a;
00088         }

SimpleOpenID::SetIdentity ( a  ) 

Definition at line 106 of file SimpleOpenID.php.

Referenced by GetOpenIDServer().

00106                                 {       // Set Identity URL
00107                         if ((stripos($a, 'http://') === false)
00108                            && (stripos($a, 'https://') === false)){
00109                                 $a = 'http://'.$a;
00110                         }
00111 /*
00112                         $u = parse_url(trim($a));
00113                         if (!isset($u['path'])){
00114                                 $u['path'] = '/';
00115                         }else if(substr($u['path'],-1,1) == '/'){
00116                                 $u['path'] = substr($u['path'], 0, strlen($u['path'])-1);
00117                         }
00118                         if (isset($u['query'])){ // If there is a query string, then use identity as is
00119                                 $identity = $a;
00120                         }else{
00121                                 $identity = $u['scheme'] . '://' . $u['host'] . $u['path'];
00122                         }
00123 //*/
00124                         $this->openid_url_identity = $a;
00125         }

SimpleOpenID::SetOpenIDServer ( a  ) 

Definition at line 80 of file SimpleOpenID.php.

Referenced by GetOpenIDServer().

00080                                     {
00081                 $this->URLs['openid_server'] = $a;
00082         }

SimpleOpenID::SetOptionalFields ( a  ) 

Definition at line 99 of file SimpleOpenID.php.

00099                                       {
00100                 if (is_array($a)){
00101                         $this->fields['optional'] = $a;
00102                 }else{
00103                         $this->fields['optional'][] = $a;
00104                 }
00105         }

SimpleOpenID::SetRequiredFields ( a  ) 

Definition at line 92 of file SimpleOpenID.php.

00092                                       {
00093                 if (is_array($a)){
00094                         $this->fields['required'] = $a;
00095                 }else{
00096                         $this->fields['required'][] = $a;
00097                 }
00098         }

SimpleOpenID::SetTrustRoot ( a  ) 

Definition at line 83 of file SimpleOpenID.php.

00083                                  {
00084                 $this->URLs['trust_root'] = $a;
00085         }

SimpleOpenID::SimpleOpenID (  ) 

Definition at line 75 of file SimpleOpenID.php.

00075                                {
00076                 if (!function_exists('curl_exec')) {
00077                         die('Error: Class SimpleOpenID requires curl extension to work');
00078                 }
00079         }

SimpleOpenID::splitResponse ( response  ) 

Definition at line 150 of file SimpleOpenID.php.

00150                                           {
00151                 $r = array();
00152                 $response = explode("\n", $response);
00153                 foreach($response as $line) {
00154                         $line = trim($line);
00155                         if ($line != "") {
00156                                 list($key, $value) = explode(":", $line, 2);
00157                                 $r[trim($key)] = trim($value);
00158                         }
00159                 }
00160                 return $r;
00161         }

SimpleOpenID::ValidateWithServer (  ) 

Definition at line 297 of file SimpleOpenID.php.

00297                                      {
00298                 $params = array(
00299                         'openid.assoc_handle' => urlencode($_GET['openid_assoc_handle']),
00300                         'openid.signed' => urlencode($_GET['openid_signed']),
00301                         'openid.sig' => urlencode($_GET['openid_sig'])
00302                 );
00303                 // Send only required parameters to confirm validity
00304                 $arr_signed = explode(",",str_replace('sreg.','sreg_',$_GET['openid_signed']));
00305                 for ($i=0; $i<count($arr_signed); $i++){
00306                         $s = str_replace('sreg_','sreg.', $arr_signed[$i]);
00307                         $c = $_GET['openid_' . $arr_signed[$i]];
00308                         // if ($c != ""){
00309                                 $params['openid.' . $s] = urlencode($c);
00310                         // }
00311                 }
00312                 $params['openid.mode'] = "check_authentication";
00313 
00314                 $openid_server = $this->GetOpenIDServer();
00315                 if ($openid_server == false){
00316                         return false;
00317                 }
00318                 $response = $this->CURL_Request($openid_server,'POST',$params);
00319                 $data = $this->splitResponse($response);
00320                 if ($data['is_valid'] == "true") {
00321                         return true;
00322                 }else{
00323                         return false;
00324                 }
00325         }


Member Data Documentation

SimpleOpenID::$error = array()

Definition at line 69 of file SimpleOpenID.php.

SimpleOpenID::$fields
Initial value:
 array(
                'required'       => array(),
                'optional'       => array(),
        )

Definition at line 70 of file SimpleOpenID.php.

SimpleOpenID::$openid_url_identity

Definition at line 67 of file SimpleOpenID.php.

SimpleOpenID::$URLs = array()

Definition at line 68 of file SimpleOpenID.php.


The documentation for this class was generated from the following file: