import torrus 1.0.9
[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 && $response['session_id'] ) {
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['freeside_session_id'] = $response['session_id'];
301
302           $customer_info = $freeside->customer_info( array(
303             'session_id' => $_SESSION['freeside_session_id'] ,
304           ) );
305           //XXX error checking here too
306           $displayname = $customer_info['name'];
307       
308       } else {
309       
310           // unsucessful login
311           error_log("[login] error logging into freeside: $error");
312           $auth = false;
313           $extension = '';
314
315           // display error message to user
316           $_SESSION['ari_error'] = _("Incorrect Username or Password");
317       
318       }
319
320       // if authenticated and user wants to be remembered, set cookie 
321       $remember = '';
322       if (isset($_POST['remember'])) {
323         $remember = $_POST['remember'];
324       }
325       if ($auth && $remember) {
326
327         $data = array('username' => $username, 'password' => $password);
328         $data = $crypt->encrypt(serialize($data),$ARI_CRYPT_PASSWORD);
329
330         $chksum = md5($data);
331
332         $buf = serialize(array($data,$chksum));
333         setcookie('ari_auth',$buf,time()+365*24*60*60,'/');
334       }
335
336       // set category
337       if (!$category) {
338         $category = "general";
339       }
340    
341       // set context
342       if (!$context) {
343         $context = "default";
344       }
345
346       // no login user
347       if ($ARI_NO_LOGIN) {
348         $extension = 'admin';
349         $name = 'Administrator';
350         $admin_callmonitor = 1;
351         $default_page = $ARI_DEFAULT_ADMIN_PAGE;
352       } 
353
354       // get outboundCID if it exists
355       $outboundCID = $this->getOutboundCID($extension);
356
357       // set
358       if ($extension) {
359         $_SESSION['ari_user']['extension'] = $extension;
360         $_SESSION['ari_user']['outboundCID'] = $outboundCID;
361         $_SESSION['ari_user']['displayname'] = $displayname;
362         $_SESSION['ari_user']['voicemail_password'] = $vm_password;
363         $_SESSION['ari_user']['category'] = $category;
364         $_SESSION['ari_user']['context'] = $context;
365         $_SESSION['ari_user']['voicemail_enabled'] = $voicemail_enabled;
366         $_SESSION['ari_user']['voicemail_email_address'] = $voicemail_email_address;
367         $_SESSION['ari_user']['voicemail_pager_address'] = $voicemail_pager_address;
368         $_SESSION['ari_user']['voicemail_email_enable'] = $voicemail_email_enable;
369         foreach ($voicemail_email as $key => $value) {
370           $_SESSION['ari_user']['voicemail_email'][$key] = $value;
371         }
372         $_SESSION['ari_user']['admin'] = $admin;
373         $_SESSION['ari_user']['admin_callmonitor'] = $admin_callmonitor;
374         $_SESSION['ari_user']['default_page'] = $default_page;
375
376         // force the session data saved
377         session_write_close();
378       } 
379     }
380   } 
381
382   /*
383    * Gets user outbound caller id
384    *
385    * @param $exten
386    *   Extension to get information about
387    * @return $ret
388    *   outbound caller id 
389    */
390   function getOutboundCID($extension) {
391
392     global $asterisk_manager_interface;
393
394     $ret = '';
395     $response = $asterisk_manager_interface->Command2("Action: Command\r\nCommand: database get AMPUSER $extension/outboundcid\r\n\r\n");
396     if ($response) {
397     
398                 $posLeft  = strpos( $response, "<")+strlen("<");
399                 $posRight = strpos( $response, ">", $posLeft);
400                 $ret = substr( $response,$posLeft,$posRight-$posLeft);
401     }
402     return $ret;
403   }
404
405   /**
406     * logout
407     */
408   function Unauth() {
409     unset($_COOKIE["ari_auth"]);
410     setcookie('ari_auth',"",time(),'/');
411     unset($_SESSION['ari_user']);
412   }
413
414   /**
415    * Provide a login form for user
416    *
417    * @param $request
418    *   Variable to hold data entered into form
419    */
420   function GetForm() {
421
422     global $ARI_NO_LOGIN;
423
424     if ($ARI_NO_LOGIN) {
425       $ret = '';
426       return;
427     }
428
429     if (isset($_GET['login'])) {
430       $login = $_GET['login'];
431     }
432
433     // if user name and password were given, but there was a problem report the error
434     if ($this->error!='') {
435       $ret = $this->error;
436     }
437
438     $language = new Language();
439     $display = new Display(NULL);
440
441     // new header
442     $ret .= $display->DisplayHeaderText(_("Login"));
443     $ret .= $display->DisplayLine();
444     $ret .= checkErrorMessage();
445
446     $ret .= "
447       <table id='login'>
448         <form id='login' name='login' action=" . $_SESSION['ARI_ROOT'] . " method='POST'>
449           <tr>
450             <td class='right'>
451               <small><small>" . _("Login") . ":&nbsp;&nbsp;</small></small>
452             </td>
453             <td>
454               <input type='text' name='username' value='" . $login . "' maxlength=20 tabindex=1>
455             </td>
456           </tr>
457           <tr>
458             <td class='right'>
459               <small><small>" . _("Password") . ":&nbsp;&nbsp;</small></small>
460             </td>
461             <td colspan=1>
462               <input type='password' name='password' maxlength=20 tabindex=2>
463             </td>
464           </tr> 
465           <tr>                          
466             <td></td>   
467             <td>
468               <input type='submit' name='btnSubmit' value='" . _("Submit") . "' tabindex=3></small></small></p>
469             </td>
470           </tr>
471           <tr>
472             <td class='right'>
473               <input type='checkbox' name='remember'>
474             </td>
475             <td class='left'>
476               <p class='small'>" . _("Remember Password") . "</p>
477             </td>
478           </tr>
479         </form>
480         <tr>                            
481           <td></td>     
482           <td>
483             " . $language->getForm() . "
484           </td>
485         </tr>
486         <tr><td>&nbsp;</td></tr>
487       </table>
488       <table id='login_text'>
489         <tr>
490           <td>" .
491             _("Use your <b>Voicemail Mailbox and Password</b>") . "<br>" .
492             _("This is the same password used for the phone") . "<br>" .
493             "<br>" . 
494             _("For password maintenance or assistance, contact your Phone System Administrator.") . "<br>" . "
495           </td>
496         </tr>
497       </table>";
498
499     $ret .= "
500       <script type='text/javascript'> 
501       <!-- 
502         if (document.login) { 
503           document.login.username.focus(); 
504         } 
505       // --> 
506       </script>";
507
508     return $ret;
509   } 
510
511
512 }
513
514
515 ?>