33 public $openid_url_identity;
37 public $URLs = array();
41 public $error = array();
45 public $fields = array(
46 'required' => array(),
47 'optional' => array(),
55 if (!function_exists(
'curl_exec')) {
56 die(
'Error: Class SimpleOpenID requires curl extension to work');
70 $this->URLs[
'openid_server'] = $a;
83 $this->URLs[
'trust_root'] = $a;
96 $this->URLs[
'cancel'] = $a;
109 $this->URLs[
'approved'] = $a;
123 $this->fields[
'required'] = $a;
125 $this->fields[
'required'][] = $a;
140 $this->fields[
'optional'] = $a;
142 $this->fields[
'optional'][] = $a;
157 if ((stripos($a,
'http://') ===
false)
158 && (stripos($a,
'https://') ===
false)) {
174 $this->openid_url_identity = $a;
187 return $this->openid_url_identity;
200 return array(
'code' => $e[0],
'description' => $e[1]);
215 $errs[
'OPENID_NOSERVERSFOUND'] =
'Cannot find OpenID Server TAG on Identity page.';
217 $desc = $errs[$code];
219 $this->error = array($code, $desc);
231 if (count($this->error) > 0) {
247 $response = explode(
"\n", $response);
248 foreach ($response as $line) {
251 list($key, $value) = explode(
":", $line, 2);
252 $r[trim($key)] = trim($value);
268 if ($openid_identity ===
null) {
269 $openid_identity = $this->openid_url_identity;
272 $u = parse_url(strtolower(trim($openid_identity)));
274 if (!isset($u[
'path']) || ($u[
'path'] ==
'/')) {
277 if (substr($u[
'path'], -1, 1) ==
'/') {
278 $u[
'path'] = substr($u[
'path'], 0, strlen($u[
'path']) - 1);
280 if (isset($u[
'query'])) {
281 return $u[
'host'].$u[
'path'].
'?'.$u[
'query'];
283 return $u[
'host'].$u[
'path'];
296 if (!is_array($arr)) {
300 foreach ($arr as $key => $value) {
301 $query .= $key.
"=".$value.
"&";
318 $fp = fsockopen(
"ssl://www.myopenid.com", 443, $errno, $errstr, 3);
320 $this->
ErrorStore(
'OPENID_SOCKETERROR', $errstr);
323 $request = $method.
" /server HTTP/1.0\r\n";
324 $request .=
"User-Agent: Dolibarr\r\n";
325 $request .=
"Connection: close\r\n\r\n";
326 fwrite($fp, $request);
327 stream_set_timeout($fp, 4);
328 $res = fread($fp, 2000);
329 $info = stream_get_meta_data($fp);
332 if ($info[
'timed_out']) {
356 preg_match_all(
'/<link[^>]*rel=[\'"]openid.server[\'"][^>]*href=[\'"]([^\'"]+)[\'"][^>]*\/?>/i', $content, $matches1);
357 preg_match_all(
'/<link[^>]*href=\'"([^\'"]+)[\'"][^>]*rel=[\'"]openid.server[\'"][^>]*\/?>/i', $content, $matches2);
358 $servers = array_merge($matches1[1], $matches2[1]);
360 preg_match_all(
'/<link[^>]*rel=[\'"]openid.delegate[\'"][^>]*href=[\'"]([^\'"]+)[\'"][^>]*\/?>/i', $content, $matches1);
362 preg_match_all(
'/<link[^>]*href=[\'"]([^\'"]+)[\'"][^>]*rel=[\'"]openid.delegate[\'"][^>]*\/?>/i', $content, $matches2);
364 $delegates = array_merge($matches1[1], $matches2[1]);
366 $ret = array($servers, $delegates);
383 include_once DOL_DOCUMENT_ROOT.
'/core/lib/geturl.lib.php';
388 $response =
getURLContent($url,
'GET',
'', 1, array(), array(
'http',
'https'));
391 if (count($servers) == 0) {
395 if (isset($delegates[0])
396 && ($delegates[0] !=
"")) {
413 $params[
'openid.return_to'] = urlencode($this->URLs[
'approved']);
414 $params[
'openid.mode'] =
'checkid_setup';
415 $params[
'openid.identity'] = urlencode($this->openid_url_identity);
416 $params[
'openid.trust_root'] = urlencode($this->URLs[
'trust_root']);
418 if (isset($this->fields[
'required'])
419 && (count($this->fields[
'required']) > 0)) {
420 $params[
'openid.sreg.required'] = implode(
',', $this->fields[
'required']);
422 if (isset($this->fields[
'optional'])
423 && (count($this->fields[
'optional']) > 0)) {
424 $params[
'openid.sreg.optional'] = implode(
',', $this->fields[
'optional']);
426 return $this->URLs[
'openid_server'].
"?".$this->
array2url($params);
439 if (headers_sent()) {
440 echo
'<script nonce="'.getNonce().
'" type="text/javascript">window.location=\'';
443 } else { // Default Header Redirect
444 header('Location:
'.$redirect_to);
453 public function validateWithServer()
456 'openid.assoc_handle
' => urlencode(GETPOST('openid_assoc_handle
')),
457 'openid.signed
' => urlencode(GETPOST('openid_signed
')),
458 'openid.sig
' => urlencode(GETPOST('openid_sig
'))
460 // Send only required parameters to confirm validity
461 $arr_signed = explode(",", str_replace('sreg.
', 'sreg_
', GETPOST('openid_signed
')));
462 $num = count($arr_signed);
463 for ($i = 0; $i < $num; $i++) {
464 $s = str_replace('sreg_
', 'sreg.
', $arr_signed[$i]);
465 $c = GETPOST('openid_
'.$arr_signed[$i]);
467 $params['openid.
'.$s] = urlencode($c);
470 $params['openid.mode
'] = "check_authentication";
472 $openid_server = $this->GetOpenIDServer();
473 if ($openid_server == false) {
477 if (is_array($params)) {
478 $params = $this->array2url($params);
481 $result = getURLContent($openid_server, 'POST
', $params);
483 $response = $result['content
'];
485 $data = $this->splitResponse($response);
486 if ($data['is_valid
'] == "true") {
502 public function sendDiscoveryRequestToGetXRDS($url = '
')
506 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php
';
508 $url = getDolGlobalString('MAIN_AUTHENTICATION_OPENID_URL
');
513 $addheaders = array('Accept: application/xrds+xml
');
514 $response = getURLContent($url, 'GET
', '', 1, $addheaders, array('http
', 'https
'), 0);
515 /* response should like this:
516 <?xml version="1.0" encoding="UTF-8"?>
517 <xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)">
519 <Service priority="0">
520 <Type>http://specs.openid.net/auth/2.0/server</Type>
521 <Type>http://openid.net/srv/ax/1.0</Type>
523 <URI>https://www.google.com/accounts/o8/ud</URI>
528 $content = $response['content
'];
531 if (preg_match('/
'.preg_quote('<URI>
', '/
').'(.*)
'.preg_quote('</URI>
', '/
').'/is
', $content, $reg)) {
535 if (empty($server)) {
536 $this->ErrorStore('OPENID_NOSERVERSFOUND
');
540 $this->SetOpenIDServer($server);
GetRedirectURL()
GetRedirectURL.
GetOpenIDServer($url='')
Get openid server.
SetApprovedURL($a)
SetApprovedURL.
__construct()
Constructor.
OpenID_Standarize($openid_identity=null)
OpenID_Standarize.
SetOptionalFields($a)
SetOptionalFields.
HTML2OpenIDServer($content)
HTML2OpenIDServer.
splitResponse($response)
splitResponse
SetCancelURL($a)
SetOpenIDServer.
GetError()
SetOpenIDServer.
SetTrustRoot($a)
SetOpenIDServer.
SetIdentity($a)
SetIdentity.
FSOCK_Request($url, $method="GET", $params="")
FSOCK_Request.
ErrorStore($code, $desc=null)
ErrorStore.
SetRequiredFields($a)
SetRequiredFields.
SetOpenIDServer($a)
SetOpenIDServer.
GetIdentity()
GetIdentity.
sendDiscoveryRequestToGetXRDS($url='')
Get XRDS response and set possible servers.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).