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