Development

/plugins/ddOpenIDPlugin/lib/sfOpenID.class.php

You must first sign up to be able to contribute.

root/plugins/ddOpenIDPlugin/lib/sfOpenID.class.php

Revision 3714, 5.0 kB (checked in by davedash, 2 years ago)

sfOpenID: Enhancements to the ValidateWithServer? function

Line 
1 <?php
2
3 class sfOpenID {
4     const NO_SERVERS_FOUND = 'No OpenID servers found.';
5
6     private $openid_url_identity;
7     private $trust_root;
8     private $approved_url;
9     private $openid_server;
10     private $error = array();
11
12
13     function KVsToArray($kvs)
14     {
15         $r = array();
16         preg_match_all('|^\s*([^:]+):([^:\n]+)[ ]*$|m', $kvs, $matches);
17         for($i = 0; $i < count($matches[0]); $i++) {
18             $r[$matches[1][$i]] = $matches[2][$i];
19         }
20         return $r;
21     }
22     
23     public function __construct(){
24         if (!function_exists('curl_exec')) {
25             die('Error: Class sfOpenID() requires curl extension to work');
26         }
27     }
28
29     public function getRedirectURL()
30     {
31         $params                          = array();
32         $params['openid.return_to']      = urlencode($this->approved_url);
33         $params['openid.mode']           = 'checkid_setup';
34         $params['openid.identity']       = urlencode($this->openid_url_identity);
35         $params['openid.trust_root']     = urlencode($this->trust_root);
36             
37         return $this->getOpenIDServer() .'?'. $this->arrayToQueryString($params);
38     }
39     
40     public function setIdentity($identity)
41     {     // Set Identity URL
42         if (strpos($identity, 'http://') === false) {
43             $identity = 'http://'.$identity;
44         }
45         // if this is a server we want a trailing slash
46         // therefore if there isn't a slash somewhere in the url after
47         // http:// add one
48         if (preg_match('|^http[s]?://[^/]+$|', $identity))
49         {
50             $identity .= '/';
51         }
52         $this->openid_url_identity = $identity;
53     }
54     
55     public function setApprovedURL($url)
56     {
57         $this->approved_url = $url;
58     }
59     
60     public function setTrustRoot($url)
61     {
62         $this->trust_root = $url;
63     }
64         
65     public function getOpenIDServer()
66     {
67         $response                  = $this->web_request($this->openid_url_identity);
68         list($servers, $delegates) = $this->extractOpenIDServerFromHTML($response);
69
70         if (!isset($servers[0]))
71         {
72             //            $this->setError(self::NO_SERVERS_FOUND);
73             throw new sfException(self::NO_SERVERS_FOUND);
74         }
75         if (!empty($delegates[0]))
76         {
77             $this->openid_url_identity = $delegates[0];
78         }
79         
80         $this->setOpenIDServer($servers[0]);
81         return $servers[0];
82     }
83     
84     public function hasError()
85     {
86         return isset($this->error[0]);
87     }
88     
89     protected function setError($code)
90     {
91         $this->error[] = $code;
92     }
93
94     function getErrors()
95     {
96         return $this->error();
97     }
98         
99     public function web_request($url, $method='GET', $params = null)
100     {
101
102         if (is_array($params)) $params = $this->arrayToQueryString($params);
103
104         if (!empty($params) && $method == 'GET') $url .= '?' . $params;
105     
106         
107         $curl = curl_init($url);
108
109         if ($method == 'POST') curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
110         
111         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
112         curl_setopt($curl, CURLOPT_HEADER, false);
113         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
114         curl_setopt($curl, CURLOPT_HTTPGET, ($method == 'GET'));
115         curl_setopt($curl, CURLOPT_POST, ($method == 'POST'));
116         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
117
118         $response = curl_exec($curl);
119         
120         return $response;
121     }
122     
123     // converts associated array to URL Query String
124     public function arrayToQueryString($arr)
125     {
126         if (!is_array($arr)) {
127             return false;
128         }
129         
130         $query = '';
131         foreach ($arr as $key => $value) {
132             $query .= $key . '=' . $value . '&';
133         }
134         
135         return $query;
136     }
137
138     private function extractOpenIDServerFromHTML($content)
139     {
140         $get = array();
141         // Get details of their OpenID server and (optional) delegate
142         preg_match_all('/<link[^>]*rel="openid.server"[^>]*href="([^"]+)"[^>]*\/?>/i', $content, $matches1);
143         preg_match_all('/<link[^>]*href="([^"]+)"[^>]*rel="openid.server"[^>]*\/?>/i', $content, $matches2);
144         $servers = array_merge($matches1[1], $matches2[1]);
145
146         preg_match_all('/<link[^>]*rel="openid.delegate"[^>]*href="([^"]+)"[^>]*\/?>/i', $content, $matches1);
147
148         preg_match_all('/<link[^>]*href="([^"]+)"[^>]*rel="openid.delegate"[^>]*\/?>/i', $content, $matches2);
149
150         $delegates = array_merge($matches1[1], $matches2[1]);
151
152         return array($servers, $delegates);
153
154     }
155
156     public function setOpenIDServer($url)
157     {
158         $this->openid_server = $url;
159     }
160     
161     public function validateWithServer()
162     {   
163     
164         /*$params array altered by EJ on 2007-04-05*/
165         
166         $params = array(
167             'openid.assoc_handle' => urlencode($_GET['openid_assoc_handle']),
168             'openid.signed'     => urlencode($_GET['openid_signed']),
169             'openid.sig'         => urlencode($_GET['openid_sig']),
170         );
171         
172         $addl_params = explode("%2C", $params['openid.signed']);
173         
174         foreach($addl_params as $_k=>$_v) {
175             $params['openid.' . $_v] = urlencode($_GET['openid_' . $_v]);
176         }
177         
178         /*End changes to params array from 2007-04-05*/
179         
180         $params['openid.mode'] = 'check_authentication';
181         
182         $openid_server = $this->getOpenIDServer();
183
184         if ($openid_server == false) {
185             return false;
186         }
187         
188         
189         $data = $this->KVstoArray($this->web_request($openid_server,'POST',$params));
190         
191         if (isset($data['is_valid']) && $data['is_valid'] == 'true') {
192             return true;
193         }
194         
195         return false;
196         
197     }
198
199     public static function simplifyURL($url)
200     {
201         $match = array();
202         preg_match('|^http[s]?://([^/]+)[/]?$|', $url, $match);
203         return isset($match[1]) ? $match[1] : null;
204     }
205 }
Note: See TracBrowser for help on using the browser.