first try at single sign-on
[freeside.git] / fs_selfservice / fri / includes / login.php
1 <?php
2
3 /**
4  * @file
5  * login functions
6  */
7
8 /**
9   * Class for login
10   */
11 class Login {
12
13   var $error;
14
15   /**
16     * Authenticate user and register user information into a session
17     */
18   function Auth() {
19
20     global $ARI_ADMIN_USERNAME;
21     global $ARI_ADMIN_PASSWORD;
22     global $ARI_ADMIN_EXTENSIONS;
23     global $ARI_CRYPT_PASSWORD;
24     global $ASTERISK_VOICEMAIL_CONF;
25     global $ASTERISK_VOICEMAIL_CONTEXT;
26     global $ASTERISK_VOICEMAIL_PATH;
27     global $ASTERISK_PROTOCOLS;
28     global $CALLMONITOR_ADMIN_EXTENSIONS;
29     global $ARI_NO_LOGIN;
30     global $ARI_DEFAULT_ADMIN_PAGE;
31     global $ARI_DEFAULT_USER_PAGE;
32
33     $crypt = new Crypt();
34
35     // init variables
36     $extension = '';
37     $displayname = '';
38     $vm_password = '';
39     $category = '';
40     $context = '';
41     $voicemail_enabled = '';
42     $voicemail_email_address = '';
43     $voicemail_pager_address = '';
44     $voicemail_email_enable = '';
45     $admin = '';
46     $admin_callmonitor = '';
47     $default_page = '';
48
49     $username = '';
50     $password = '';
51
52     // get the ari authentication cookie 
53     $data = '';
54     $chksum = '';
55     if (isset($_COOKIE['ari_auth'])) {
56       $buf = unserialize($_COOKIE['ari_auth']);
57       list($data,$chksum) = $buf;
58     }
59     if (md5($data) == $chksum) {
60       $data = unserialize($crypt->decrypt($data,$ARI_CRYPT_PASSWORD));
61       $username = $data['username'];
62       $password = $data['password'];
63     }
64
65     if (isset($_POST['username']) && 
66           isset($_POST['password'])) {
67       $username = $_POST['username'];
68       $password = $_POST['password'];
69     }
70
71     // init email options array
72     $voicemail_email = array();
73
74     // when login, make a new session
75     if ($username && !$ARI_NO_LOGIN) {
76
77       $auth = false;
78
79       // check admin
80       if (!$auth) {
81         if ($username==$ARI_ADMIN_USERNAME && 
82               $password==$ARI_ADMIN_PASSWORD) {
83
84           // authenticated
85           $auth = true; 
86
87           $extension = 'admin';
88           $name = 'Administrator';
89           $admin = 1;
90           $admin_callmonitor = 1;
91
92           $default_page = $ARI_DEFAULT_ADMIN_PAGE;
93         }
94       }
95
96       // check voicemail login
97       if (!$auth) {
98
99         if (is_readable($ASTERISK_VOICEMAIL_CONF)) {
100
101           $lines = file($ASTERISK_VOICEMAIL_CONF);
102
103           // look for include files and tack their lines to end of array
104           foreach ($lines as $key => $line) {
105
106             if (preg_match("/include/i",$line)) {
107
108               $include_filename = '';
109               $parts = split(' ',$line);
110               if (isset($parts[1])) {
111                 $include_filename = trim($parts[1]);
112               }
113
114               if ($include_filename) {
115                 $path_parts = pathinfo($ASTERISK_VOICEMAIL_CONF);
116                 $include_path = fixPathSlash($path_parts['dirname']) . $include_filename;
117                 foreach (glob($include_path) as $include_file) {
118                   $include_lines = file($include_file);
119                   $lines = array_merge($include_lines,$lines);
120                 }
121               }
122             }
123           }
124
125           // process
126           foreach ($lines as $key => $line) {
127
128             // check for current context and process
129             if (preg_match("/\[.*\]/i",$line)) {
130               $currentContext = trim(preg_replace('/\[|\]/', '', $line));
131             }
132             if ($ASTERISK_VOICEMAIL_CONTEXT &&
133                   $currentContext!=$ASTERISK_VOICEMAIL_CONTEXT) {
134               continue;
135             }
136
137             // check for user and process
138             unset($value);
139             $parts = split('=>',$line);
140             if (isset($parts[0])) {
141               $var = $parts[0];
142             }
143             if (isset($parts[1])) {
144               $value = $parts[1];
145             }
146             $var = trim($var);
147             if ($var==$username && $value) {
148               $buf = split(',',$value);
149               if ($buf[0]==$password) {  
150
151                 // authenticated
152                 $auth = true; 
153                 $extension = $username;
154                 $displayname = $buf[1];
155                 $vm_password = $buf[0];
156                 $default_page = $ARI_DEFAULT_USER_PAGE;
157                 $context = $currentContext;
158                 $voicemail_enabled = 1;
159                 $voicemail_email_address = $buf[2];
160                 $voicemail_pager_address = $buf[3];
161                 
162                 if ($voicemail_email_address || $voicemail_pager_address) {
163                   $voicemail_email_enable = 1;
164                 }
165
166                 $options = split('\|',$buf[4]);
167                 foreach ($options as $option) {
168                   $opt_buf = split('=',$option);
169                   $voicemail_email[$opt_buf[0]] = trim($opt_buf[1]);
170                 }
171
172                 $admin = 0;
173                 if ($ARI_ADMIN_EXTENSIONS) {
174                   $extensions = split(',',$ARI_ADMIN_EXTENSIONS);
175                   foreach ($extensions as $key => $value) {
176                     if ($extension==$value) {
177                       $admin = 1;
178                       break 2;
179                     }
180                   }
181                 }
182   
183                 $admin_callmonitor = 0;
184                 if ($CALLMONITOR_ADMIN_EXTENSIONS) {
185                   $extensions = split(',',$CALLMONITOR_ADMIN_EXTENSIONS);
186                   foreach ($extensions as $key => $value) {
187                     if ($value=='all' || $extension==$value) {
188                       $admin_callmonitor = 1;
189                       break 2;
190                     }
191                   }
192                 }
193               }
194               else {
195                 $_SESSION['ari_error'] = "Incorrect Password";
196                 return;
197               }
198             }
199           }
200         }
201         else {
202           $_SESSION['ari_error'] = "File not readable: " . $ASTERISK_VOICEMAIL_CONF;
203           return;
204         }
205       }
206
207       // check sip login
208       if (!$auth) {
209
210         foreach($ASTERISK_PROTOCOLS as $protocol => $value) {
211
212           $config_files = split(';',$value['config_files']);
213           foreach ($config_files as $config_file) {
214
215             if (is_readable($config_file)) {
216
217               $lines = file($config_file);
218               foreach ($lines as $key => $line) {
219
220                 unset($value);
221                 $parts = split('=',$line);
222                 if (isset($parts[0])) {
223                   $var = trim($parts[0]);
224                 }
225                 if (isset($parts[1])) {
226                   $value = trim($parts[1]);
227                 }
228                 if ($var=="username") {
229                   $protocol_username = $value;
230                 }
231                 if ($var=="secret") {
232
233                   $protocol_password = $value;
234                   if ($protocol_username==$username &&
235                         $protocol_password==$password) {  
236
237                     // authenticated
238                     $auth = true;  
239                     $extension = $username ;
240                     $displayname = $username;
241                     $default_page = $ARI_DEFAULT_ADMIN_PAGE;
242   
243                     $admin = 0;
244                     if ($ARI_ADMIN_EXTENSIONS) {
245                       $extensions = split(',',$ARI_ADMIN_EXTENSIONS);
246                       foreach ($extensions as $key => $value) {
247                         if ($extension==$value) {
248                           $admin = 1;
249                           break 2;
250                         }
251                       }
252                     }
253
254                     $admin_callmonitor = 0;
255                     if ($CALLMONITOR_ADMIN_EXTENSIONS) {
256                       $extensions = split(',',$CALLMONITOR_ADMIN_EXTENSIONS);
257                       foreach ($extensions as $key => $value) {
258                         if ($value=='all' || $extension==$value) {
259                           $admin_callmonitor = 1;
260                           break 2;
261                         }
262                       }
263                     }
264                   }
265                   else if ($protocol_username==$username &&
266                              $protocol_password!=$password) {
267                     $_SESSION['ari_error'] = _("Incorrect Password");
268                     return;
269                   }
270                 }
271               }
272             }
273           }
274         }
275       }
276
277       // let user know bad login
278       if (!$auth) {
279         $_SESSION['ari_error'] = _("Incorrect Username or Password");
280       }
281
282       // freeside login
283       $freeside = new FreesideSelfService()
284       $domain = 'svc_phone';
285       $response = $freeside->login( array( 
286         'username' => strtolower($_username),
287         'domain'   => $domain,
288         'password' => strtolower($password),
289       ) );
290       error_log("[login] received response from freeside: $response");
291       $error = $response['error'];
292
293       if ( ! $error ) {
294
295           // sucessful freeside login
296           error_log("[login] logged into freeside with session_id=$session_id");
297       
298           // store session id in your session store, to be used for other calls
299           //$fs_session_id = $response['session_id'];
300           $_SESSION['fs_session'] = $response['session_id'];
301       
302       } else {
303       
304           // unsucessful login
305           error_log("[login] error logging into freeside: $error");
306           $auth = false;
307
308           // display error message to user
309           $_SESSION=['ari_error'] = _("Incorrect Username or Password");
310       
311       }
312
313       // if authenticated and user wants to be remembered, set cookie 
314       $remember = '';
315       if (isset($_POST['remember'])) {
316         $remember = $_POST['remember'];
317       }
318       if ($auth && $remember) {
319
320         $data = array('username' => $username, 'password' => $password);
321         $data = $crypt->encrypt(serialize($data),$ARI_CRYPT_PASSWORD);
322
323         $chksum = md5($data);
324
325         $buf = serialize(array($data,$chksum));
326         setcookie('ari_auth',$buf,time()+365*24*60*60,'/');
327       }
328
329       // set category
330       if (!$category) {
331         $category = "general";
332       }
333    
334       // set context
335       if (!$context) {
336         $context = "default";
337       }
338
339       // no login user
340       if ($ARI_NO_LOGIN) {
341         $extension = 'admin';
342         $name = 'Administrator';
343         $admin_callmonitor = 1;
344         $default_page = $ARI_DEFAULT_ADMIN_PAGE;
345       } 
346
347       // get outboundCID if it exists
348       $outboundCID = $this->getOutboundCID($extension);
349
350       // set
351       if ($extension) {
352         $_SESSION['ari_user']['extension'] = $extension;
353         $_SESSION['ari_user']['outboundCID'] = $outboundCID;
354         $_SESSION['ari_user']['displayname'] = $displayname;
355         $_SESSION['ari_user']['voicemail_password'] = $vm_password;
356         $_SESSION['ari_user']['category'] = $category;
357         $_SESSION['ari_user']['context'] = $context;
358         $_SESSION['ari_user']['voicemail_enabled'] = $voicemail_enabled;
359         $_SESSION['ari_user']['voicemail_email_address'] = $voicemail_email_address;
360         $_SESSION['ari_user']['voicemail_pager_address'] = $voicemail_pager_address;
361         $_SESSION['ari_user']['voicemail_email_enable'] = $voicemail_email_enable;
362         foreach ($voicemail_email as $key => $value) {
363           $_SESSION['ari_user']['voicemail_email'][$key] = $value;
364         }
365         $_SESSION['ari_user']['admin'] = $admin;
366         $_SESSION['ari_user']['admin_callmonitor'] = $admin_callmonitor;
367         $_SESSION['ari_user']['default_page'] = $default_page;
368
369         // force the session data saved
370         session_write_close();
371       } 
372     }
373   } 
374
375   /*
376    * Gets user outbound caller id
377    *
378    * @param $exten
379    *   Extension to get information about
380    * @return $ret
381    *   outbound caller id 
382    */
383   function getOutboundCID($extension) {
384
385     global $asterisk_manager_interface;
386
387     $ret = '';
388     $response = $asterisk_manager_interface->Command2("Action: Command\r\nCommand: database get AMPUSER $extension/outboundcid\r\n\r\n");
389     if ($response) {
390     
391                 $posLeft  = strpos( $response, "<")+strlen("<");
392                 $posRight = strpos( $response, ">", $posLeft);
393                 $ret = substr( $response,$posLeft,$posRight-$posLeft);
394     }
395     return $ret;
396   }
397
398   /**
399     * logout
400     */
401   function Unauth() {
402     unset($_COOKIE["ari_auth"]);
403     setcookie('ari_auth',"",time(),'/');
404     unset($_SESSION['ari_user']);
405   }
406
407   /**
408    * Provide a login form for user
409    *
410    * @param $request
411    *   Variable to hold data entered into form
412    */
413   function GetForm() {
414
415     global $ARI_NO_LOGIN;
416
417     if ($ARI_NO_LOGIN) {
418       $ret = '';
419       return;
420     }
421
422     if (isset($_GET['login'])) {
423       $login = $_GET['login'];
424     }
425
426     // if user name and password were given, but there was a problem report the error
427     if ($this->error!='') {
428       $ret = $this->error;
429     }
430
431     $language = new Language();
432     $display = new Display(NULL);
433
434     // new header
435     $ret .= $display->DisplayHeaderText(_("Login"));
436     $ret .= $display->DisplayLine();
437     $ret .= checkErrorMessage();
438
439     $ret .= "
440       <table id='login'>
441         <form id='login' name='login' action=" . $_SESSION['ARI_ROOT'] . " method='POST'>
442           <tr>
443             <td class='right'>
444               <small><small>" . _("Login") . ":&nbsp;&nbsp;</small></small>
445             </td>
446             <td>
447               <input type='text' name='username' value='" . $login . "' maxlength=20 tabindex=1>
448             </td>
449           </tr>
450           <tr>
451             <td class='right'>
452               <small><small>" . _("Password") . ":&nbsp;&nbsp;</small></small>
453             </td>
454             <td colspan=1>
455               <input type='password' name='password' maxlength=20 tabindex=2>
456             </td>
457           </tr> 
458           <tr>                          
459             <td></td>   
460             <td>
461               <input type='submit' name='btnSubmit' value='" . _("Submit") . "' tabindex=3></small></small></p>
462             </td>
463           </tr>
464           <tr>
465             <td class='right'>
466               <input type='checkbox' name='remember'>
467             </td>
468             <td class='left'>
469               <p class='small'>" . _("Remember Password") . "</p>
470             </td>
471           </tr>
472         </form>
473         <tr>                            
474           <td></td>     
475           <td>
476             " . $language->getForm() . "
477           </td>
478         </tr>
479         <tr><td>&nbsp;</td></tr>
480       </table>
481       <table id='login_text'>
482         <tr>
483           <td>" .
484             _("Use your <b>Voicemail Mailbox and Password</b>") . "<br>" .
485             _("This is the same password used for the phone") . "<br>" .
486             "<br>" . 
487             _("For password maintenance or assistance, contact your Phone System Administrator.") . "<br>" . "
488           </td>
489         </tr>
490       </table>";
491
492     $ret .= "
493       <script type='text/javascript'> 
494       <!-- 
495         if (document.login) { 
496           document.login.username.focus(); 
497         } 
498       // --> 
499       </script>";
500
501     return $ret;
502   } 
503
504
505 }
506
507
508 ?>