0
+<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
0
+#(c)2004-2007 by Ted Kulp (ted@cmsmadesimple.org)
0
+#This project's homepage is: http://cmsmadesimple.org
0
+#This program is free software; you can redistribute it and/or modify
0
+#it under the terms of the GNU General Public License as published by
0
+#the Free Software Foundation; either version 2 of the License, or
0
+#(at your option) any later version.
0
+#This program is distributed in the hope that it will be useful,
0
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
0
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0
+#GNU General Public License for more details.
0
+#You should have received a copy of the GNU General Public License
0
+#along with this program; if not, write to the Free Software
0
+#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0
+class CmsOpenid extends CmsObject
0
+ public $delegate = '';
0
+ public $mode = 'checkid_setup';
0
+ function __construct()
0
+ parent::__construct();
0
+ public static function is_enabled()
0
+ return ini_get('allow_url_fopen');
0
+ public static function generate_checksum()
0
+ return sha1(time() . CMS_VERSION . ROOT_DIR);
0
+ public static function cleanup_openid($url)
0
+ $u = parse_url(strtolower(trim($url)));
0
+ if (!isset($u['path']) || $u['path'] == '/')
0
+ #parse_url sometimes returns a straight domain name
0
+ #with no path or scheme as a path. That obviously should
0
+ if (!isset($u['host']) && $u['path'] != '')
0
+ $u['host'] = $u['path'];
0
+ #If the path ends with a /, remove it.
0
+ if(substr($u['path'],-1,1) == '/')
0
+ $u['path'] = substr($u['path'], 0, strlen($u['path'])-1);
0
+ #Return the straightened-out openid
0
+ if (isset($u['query']))
0
+ return $u['host'] . $u['path'] . '?' . $u['query'];
0
+ return $u['host'] . $u['path'];
0
+ public static function create_url($url)
0
+ return 'http://' . self::cleanup_openid($url);
0
+ public function find_server($url)
0
+ $file = fopen($url, 'r');
0
+ $this->delegate = $url;
0
+ $line = fgets($file, 1024);
0
+ if (preg_match("/<link rel=['\"]openid\.delegate['\"] href=['\"](.*?)['\"]/", $line, $out))
0
+ $this->delegate = $out[1];
0
+ if (preg_match("/<link rel=['\"]openid\.server['\"] href=['\"](.*?)['\"]/", $line, $out))
0
+ $this->server = $out[1];
0
+ if ($this->server != '')
0
+ public function do_authentication($return_url, $checksum = '')
0
+ if ($this->server == '' || $this->delegate == '' || $return_url == '')
0
+ $checksum = self::generate_checksum();
0
+ $return_url .= strpos('?', $return_url) !== FALSE ? '&' : '?';
0
+ $return_url .= "checksum={$checksum}";
0
+ $return_url = urlencode($return_url);
0
+ $trust_root = urlencode(CmsConfig::get('root_url'));
0
+ $cleaned_delegate = urlencode($this->delegate);
0
+ CmsResponse::redirect("{$this->server}?openid.mode={$this->mode}&openid.identity={$cleaned_delegate}&openid.return_to={$return_url}&openid.trust_root={$trust_root}");
0
+ public function check_authentication($params)
0
+ if ($params['openid_mode'] == 'id_res')
0
+ $params_we_need = array();
0
+ #Gather up all the openid* parameters to send them back
0
+ foreach ($params as $k=>$v)
0
+ if (starts_with($k, 'openid') && !ends_with($k, 'mode'))
0
+ $k = str_replace('openid_', 'openid.', $k);
0
+ $params_we_need[$k] = $v;
0
+ $params_we_need['openid.mode'] = 'check_authentication';
0
+ return self::do_post_request($params_we_need['openid.op_endpoint'], $params_we_need);
0
+ * Posts behind the scenes to another page.
0
+ * Taken from: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
0
+ * @return string Response from the posted page
0
+ * @author Wez Furlong, modified by Ted Kulp
0
+ public static function do_post_request($url, $data, $method = 'POST')
0
+ $uri = parse_url($url);
0
+ $port = isset($uri['port']) ? $uri['port'] : 80;
0
+ $host = $uri['host'] . ($port != 80 ? ':'. $port : '');
0
+ $fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15);
0
+ return 'Error connecting to the openid server.';
0
+ $data = http_build_query($data);
0
+ $headers = "Content-type: application/x-www-form-urlencoded; charset=utf-8\r\n" .
0
+ "User-Agent: CMS Made Simple (http://cmsmadesimple.org)\r\n" .
0
+ 'Content-Length: '. strlen($data);
0
+ $path = isset($uri['path']) ? $uri['path'] : '/';
0
+ if (isset($uri['query']))
0
+ $path .= '?'. $uri['query'];
0
+ $request = $method .' '. $path ." HTTP/1.0\r\n";
0
+ $request .= "\r\n\r\n";
0
+ $request .= $data ."\r\n";
0
+ fwrite($fp, $request);
0
+ while (!feof($fp) && $chunk = fread($fp, 1024))
0
+ if (starts_with($response, 'HTTP/1.1 200 OK'))
0
+ if (strpos($response, 'is_valid:true') !== FALSE)
0
\ No newline at end of file
Comments
No one has commented yet.