users can add pictures to their profiles; users can like and dislike videos
[living-lab-site.git] / application / libraries / Openid.php
1 <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3 * OpenID Library
4 *
5 * @package    CodeIgniter
6 * @author     bardelot
7 * @see        http://cakebaker.42dh.com/2007/01/11/cakephp-and-openid/
8 *             & http://openidenabled.com/php-openid/
9 */
10
11 class Openid{
12
13   var $storePath = 'tmp';
14   
15   var $sreg_enable = false;
16   var $sreg_required = null;
17   var $sreg_optional = null;
18   var $sreg_policy = null;
19   
20   var $pape_enable = false;
21   var $pape_policy_uris = null;
22   
23   var $request_to;
24   var $trust_root;
25   var $ext_args;
26     
27     function Openid()
28     {        
29     $CI =& get_instance();    
30         $CI->config->load('openid');
31         $this->storePath = $CI->config->item('openid_storepath');
32             
33         session_start();    
34         $this->_doIncludes();
35             
36     log_message('debug', "OpenID Class Initialized");
37     }
38     
39     function _doIncludes()
40     {
41     set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
42
43     require_once "Auth/OpenID/Consumer.php";
44     require_once "Auth/OpenID/FileStore.php";
45     require_once "Auth/OpenID/SReg.php";
46     require_once "Auth/OpenID/PAPE.php";
47   }
48     
49     function set_sreg($enable, $required = null, $optional = null, $policy = null)
50     {
51     $this->sreg_enable = $enable;
52     $this->sreg_required = $required;
53     $this->sreg_optional = $optional;
54     $this->sreg_policy = $policy;
55     }
56     
57     function set_pape($enable, $policy_uris = null)
58     {
59     $this->pape_enable = $enable;
60     $this->pape_policy_uris = $policy_uris;
61     }
62     
63     function set_request_to($uri)
64     {
65     $this->request_to = $uri;
66     }
67     
68     function set_trust_root($trust_root)
69     {
70     $this->trust_root = $trust_root;
71     }
72     
73     function set_args($args)
74     {
75     $this->ext_args = $args;
76     }
77     
78     function _set_message($error, $msg, $val = '', $sub = '%s')
79     {
80       $CI =& get_instance();
81         $CI->lang->load('openid', 'english');
82         echo str_replace($sub, $val, $CI->lang->line($msg));
83         
84         if ($error)
85         {
86       exit;
87     }
88     }
89     
90     function authenticate($openId)
91     {
92     $consumer = $this->_getConsumer();
93         $authRequest = $consumer->begin($openId);
94             
95         // No auth request means we can't begin OpenID.
96     if (!$authRequest)
97     {
98         $this->_set_message(true,'openid_auth_error');
99     }
100       
101     if ($this->sreg_enable)
102     {
103         $sreg_request = Auth_OpenID_SRegRequest::build($this->sreg_required, $this->sreg_optional, $this->sreg_policy);
104
105         if ($sreg_request)
106         {
107             $authRequest->addExtension($sreg_request);
108         }
109         else
110         {
111             $this->_set_message(true,'openid_sreg_failed');
112         }
113     }
114       
115     if ($this->pape_enable)
116     {
117         $pape_request = new Auth_OpenID_PAPE_Request($this->pape_policy_uris);
118         
119         if ($pape_request)
120         {
121             $authRequest->addExtension($pape_request);
122         }
123         else
124         {
125             $this->_set_message(true,'openid_pape_failed');
126         }
127     }
128             
129         if ($this->ext_args != null)
130         {
131                 foreach ($this->ext_args as $extensionArgument)
132                 {
133             if (count($extensionArgument) == 3)
134             {
135                  $authRequest->addExtensionArg($extensionArgument[0], $extensionArgument[1], $extensionArgument[2]);
136             }
137                 }
138     }
139             
140         // Redirect the user to the OpenID server for authentication.
141     // Store the token for this authentication so we can verify the
142     // response.
143
144     // For OpenID 1, send a redirect.  For OpenID 2, use a Javascript
145     // form to send a POST request to the server.
146     if ($authRequest->shouldSendRedirect())
147     {
148         $redirect_url = $authRequest->redirectURL($this->trust_root, $this->request_to);
149
150         // If the redirect URL can't be built, display an error
151         // message.
152         if (Auth_OpenID::isFailure($redirect_url))
153         {
154             $this->_set_message(true,'openid_redirect_failed', $redirect_url->message);
155         }
156         else
157         {
158             // Send redirect.
159             header("Location: ".$redirect_url);
160         }
161     }
162     else
163     {
164         // Generate form markup and render it.
165         $form_id = 'openid_message';
166         $form_html = $authRequest->formMarkup($this->trust_root, $this->request_to, false, array('id' => $form_id));
167
168         // Display an error if the form markup couldn't be generated;
169         // otherwise, render the HTML.
170         if (Auth_OpenID::isFailure($form_html))
171         {
172             $this->_set_message(true,'openid_redirect_failed', $form_html->message);
173         }
174         else
175         {
176             $page_contents = array(
177                "<html><head><title>",
178                "OpenID transaction in progress",
179                "</title></head>",
180                "<body onload='document.getElementById(\"".$form_id."\").submit()'>",
181                $form_html,
182                "</body></html>");
183
184             print implode("\n", $page_contents);
185         }
186     }
187
188         }
189         
190         function getResponse()
191         {
192       $consumer = $this->_getConsumer();
193       $response = $consumer->complete($this->request_to);
194             
195       return $response;
196         }
197         
198         function _getConsumer()
199         {
200             if (!file_exists($this->storePath) && !mkdir($this->storePath))
201             {
202           $this->_set_message(true,'openid_storepath_failed', $this->storePath);
203             }
204
205             $store = new Auth_OpenID_FileStore($this->storePath);
206             $consumer = new Auth_OpenID_Consumer($store);
207             
208             return $consumer;
209         }
210 }
211 ?>