source: trunk/login.php @ 16

Revision 16, 16.0 KB checked in by niltonneto, 17 years ago (diff)

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /**************************************************************************\
3        * eGroupWare login                                                         *
4        * http://www.egroupware.org                                                *
5        * Originaly written by Dan Kuykendall <seek3r@phpgroupware.org>            *
6        *                      Joseph Engo    <jengo@phpgroupware.org>             *
7        * --------------------------------------------                             *
8        *  This program is free software; you can redistribute it and/or modify it *
9        *  under the terms of the GNU General Public License as published by the   *
10        *  Free Software Foundation; either version 2 of the License, or (at your  *
11        *  option) any later version.                                              *
12        \**************************************************************************/
13
14        $phpgw_info = array();
15        $submit = False;                        // set to some initial value
16
17        $GLOBALS['phpgw_info']['flags'] = array(
18                'disable_Template_class' => True,
19                'login'                  => True,
20                'currentapp'             => 'login',
21                'noheader'               => True
22        );
23
24        if(file_exists('./header.inc.php'))
25        {
26                include('./header.inc.php');
27                if ($GLOBALS['phpgw_info']['server']['use_https'] > 0)
28                {
29                        if ($_SERVER['HTTPS'] != 'on')
30                        {
31                                Header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
32                                exit;
33                        }
34                }
35                       
36                if(function_exists('CreateObject'))
37                {
38                        $GLOBALS['phpgw']->session = CreateObject('phpgwapi.sessions');
39                }
40                else
41                {
42                        Header('Location: setup/index.php');
43                        exit;
44                }
45        }
46        else
47        {
48                Header('Location: setup/index.php');
49                exit;
50        }
51
52        $GLOBALS['phpgw_info']['server']['template_dir'] = PHPGW_SERVER_ROOT . '/phpgwapi/templates/' . $GLOBALS['phpgw_info']['login_template_set'];
53        $tmpl = CreateObject('phpgwapi.Template', $GLOBALS['phpgw_info']['server']['template_dir']);
54
55        // read the images from the login-template-set, not the (maybe not even set) users template-set
56        $GLOBALS['phpgw_info']['user']['preferences']['common']['template_set'] = $GLOBALS['phpgw_info']['login_template_set'];
57
58        // This is used for system downtime, to prevent new logins.
59        if($GLOBALS['phpgw_info']['server']['deny_all_logins'])
60        {
61                $deny_msg=lang('Oops! You caught us in the middle of system maintainance.<br/>
62                Please, check back with us shortly.');
63
64                $tmpl->set_file(array
65                (
66                        'login_form' => 'login_denylogin.tpl'
67                ));
68
69                $tmpl->set_var('template_set','default');
70                $tmpl->set_var('deny_msg',$deny_msg);
71                $tmpl->pfp('loginout','login_form');
72                exit;
73        }
74        $tmpl->set_file(array('login_form' => 'login.tpl'));
75
76        // !! NOTE !!
77        // Do NOT and I repeat, do NOT touch ANYTHING to do with lang in this file.
78        // If there is a problem, tell me and I will fix it. (jengo)
79
80        // whoooo scaring
81
82        if($GLOBALS['phpgw_info']['server']['usecookies'] == True)
83        {
84                $GLOBALS['phpgw']->session->phpgw_setcookie('serverID', '1024');
85                //GLOBALS['phpgw']->session->phpgw_setcookie('eGroupWareLoginTime', time());
86        }
87
88/*
89        if($_GET['cd'] != 10 && $GLOBALS['phpgw_info']['server']['usecookies'] == False)
90        {
91                $GLOBALS['phpgw']->session->setcookie('sessionid');
92                $GLOBALS['phpgw']->session->setcookie('kp3');
93                $GLOBALS['phpgw']->session->setcookie('domain');
94        }
95*/
96
97/* This is not working yet because I need to figure out a way to clear the $cd =1
98        if(isset($_SERVER['PHP_AUTH_USER']) && $_GET['cd'] == '1')
99        {
100                Header('HTTP/1.0 401 Unauthorized');
101                Header('WWW-Authenticate: Basic realm="phpGroupWare"');
102                echo 'You have to re-authentificate yourself';
103                exit;
104        }
105*/
106
107        function check_logoutcode($code)
108        {
109                switch($code)
110                {
111                        case 1:
112                                return lang('You have been successfully logged out');
113                                break;
114                        case 2:
115                                return lang('Sorry, your login has expired');
116                                break;
117                        case 4:
118                                return lang('Cookies are required to login to this site.');
119                                break;
120                        case 5:
121                                return '<font color="FF0000">' . lang('Bad login or password') . '</font>';
122                                break;
123                        case 98:
124                                return '<font color="FF0000">' . lang('Account is expired') . '</font>';
125                                break;
126                        case 99:
127                                return '<font color="FF0000">' . lang('Blocked, too many attempts') . '</font>';
128                                break;
129                        case 10:
130                                $GLOBALS['phpgw']->session->phpgw_setcookie('sessionid');
131                                $GLOBALS['phpgw']->session->phpgw_setcookie('kp3');
132                                $GLOBALS['phpgw']->session->phpgw_setcookie('domain');
133
134                                //fix for bug php4 expired sessions bug
135                                if($GLOBALS['phpgw_info']['server']['sessions_type'] == 'php4')
136                                {
137                                        $GLOBALS['phpgw']->session->phpgw_setcookie(PHPGW_PHPSESSID);
138                                }
139
140                                return '<font color="#FF0000">' . lang('Your session could not be verified.') . '</font>';
141                                break;
142                        default:
143                                return '&nbsp;';
144                }
145        }
146       
147        /* Program starts here */
148
149        if($GLOBALS['phpgw_info']['server']['auth_type'] == 'http' && isset($_SERVER['PHP_AUTH_USER']))
150        {
151                $submit = True;
152                $login  = $_SERVER['PHP_AUTH_USER'];
153                $passwd = $_SERVER['PHP_AUTH_PW'];
154                $passwd_type = 'text';
155        }
156        else
157        {
158                $passwd = $_POST['passwd'];
159                $passwd_type = $_POST['passwd_type'];
160        }
161
162        # Apache + mod_ssl style SSL certificate authentication
163        # Certificate (chain) verification occurs inside mod_ssl
164        if($GLOBALS['phpgw_info']['server']['auth_type'] == 'sqlssl' && isset($_SERVER['SSL_CLIENT_S_DN']) && !isset($_GET['cd']))
165        {
166                # an X.509 subject looks like:
167                # /CN=john.doe/OU=Department/O=Company/C=xx/Email=john@comapy.tld/L=City/
168                # the username is deliberately lowercase, to ease LDAP integration
169                $sslattribs = explode('/',$_SERVER['SSL_CLIENT_S_DN']);
170                # skip the part in front of the first '/' (nothing)
171                while($sslattrib = next($sslattribs))
172                {
173                        list($key,$val) = explode('=',$sslattrib);
174                        $sslattributes[$key] = $val;
175                }
176
177                if(isset($sslattributes['Email']))
178                {
179                        $submit = True;
180
181                        # login will be set here if the user logged out and uses a different username with
182                        # the same SSL-certificate.
183                        if(!isset($_POST['login'])&&isset($sslattributes['Email']))
184                        {
185                                $login = $sslattributes['Email'];
186                                # not checked against the database, but delivered to authentication module
187                                $passwd = $_SERVER['SSL_CLIENT_S_DN'];
188                        }
189                }
190                unset($key);
191                unset($val);
192                unset($sslattributes);
193        }
194
195        if(isset($passwd_type) || $_POST['submitit_x'] || $_POST['submitit_y'] || $submit)
196        {
197                // TEMPORÁRIO, Login com ou sem organização ////////////////////////////////////////////////////
198                /*$common = CreateObject('phpgwapi.common');
199                $ldap_conn = $common->ldapConnect();
200                $justthese = array("uid");
201                $filter="(&(phpgwAccountType=u)(uid=".$_POST['user']."))";
202                $ldap_search = ldap_search($ldap_conn, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
203                $ldap_info       = ldap_get_entries($ldap_conn, $ldap_search);
204                if ($ldap_info['count'] != 0) //Usuário SEM ORG EXISTE.
205                {
206                        $_POST['login'] = $_POST['user'];
207                }
208                ldap_close($ldap_conn);
209                */
210                // TEMPORÁRIO, Login com ou sem organização ////////////////////////////////////////////////////
211
212                if(getenv('REQUEST_METHOD') != 'POST' && $_SERVER['REQUEST_METHOD'] != 'POST' &&
213                        !isset($_SERVER['PHP_AUTH_USER']) && !isset($_SERVER['SSL_CLIENT_S_DN']))
214                {
215                        $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/login.php','cd=5'));
216                }
217                #if(!isset($_COOKIE['eGroupWareLoginTime']))
218                #{
219                #       $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/login.php','cd=4'));
220                #}
221               
222                // don't get login data again when $submit is true
223                if($submit == false)
224                {
225                        $login = $_POST['login'];
226                }
227               
228                if(strstr($login,'@') === False && isset($_POST['logindomain']))
229                {
230                        $login .= '@' . $_POST['logindomain'];
231                }
232                elseif(!isset($GLOBALS['phpgw_domain'][$GLOBALS['phpgw_info']['user']['domain']]))
233                {
234                        $login .= '@'.$GLOBALS['phpgw_info']['server']['default_domain'];
235                }
236                $GLOBALS['sessionid'] = $GLOBALS['phpgw']->session->create(strtolower($login),$passwd,$passwd_type,'u');
237
238                if(!isset($GLOBALS['sessionid']) || ! $GLOBALS['sessionid'])
239                {
240                        $GLOBALS['phpgw']->redirect($GLOBALS['phpgw_info']['server']['webserver_url'] . '/login.php?cd=' . $GLOBALS['phpgw']->session->cd_reason);
241                }
242                else
243                {
244                        if ($_POST['lang'] && preg_match('/^[a-z]{2}(-[a-z]{2}){0,1}$/',$_POST['lang']) &&
245                            $_POST['lang'] != $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'])
246                        {
247                                $GLOBALS['phpgw']->preferences->add('common','lang',$_POST['lang'],'session');
248                        }
249
250                        if(!$GLOBALS['phpgw_info']['server']['disable_autoload_langfiles'])
251                        {
252                                $GLOBALS['phpgw']->translation->autoload_changed_langfiles();
253                        }
254                        $forward = isset($_GET['phpgw_forward']) ? urldecode($_GET['phpgw_forward']) : @$_POST['phpgw_forward'];
255                        if (!$forward)
256                        {
257                                $extra_vars['cd'] = 'yes';
258                                $forward = '/home.php';
259                        }
260                        else
261                        {
262                                list($forward,$extra_vars) = explode('?',$forward,2);
263                        }
264                        if ($GLOBALS['phpgw_info']['server']['use_https'] != 2)
265                        {
266                                $forward = 'http://'.$_SERVER['HTTP_HOST'].($GLOBALS['phpgw']->link($forward.'?cd=yes'));
267                                echo "<script language='Javascript1.3'>location.href='".$forward."'</script>";
268                        }
269                        else
270                        {
271                                $GLOBALS['phpgw']->redirect_link($forward,$extra_vars);
272                        }
273                }
274        }
275        else
276        {
277                // !!! DONT CHANGE THESE LINES !!!
278                // If there is something wrong with this code TELL ME!
279                // Commenting out the code will not fix it. (jengo)
280                if(isset($_COOKIE['last_loginid']))
281                {
282                        $accounts = CreateObject('phpgwapi.accounts');
283                        $prefs = CreateObject('phpgwapi.preferences', $accounts->name2id($_COOKIE['last_loginid']));
284
285                        if($prefs->account_id)
286                        {
287                                $GLOBALS['phpgw_info']['user']['preferences'] = $prefs->read_repository();
288                        }
289                }
290                if ($_GET['lang'])
291                {
292                        $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] = $_GET['lang'];
293                }
294                elseif(!isset($_COOKIE['last_loginid']) || !$prefs->account_id)
295                {
296                        // If the lastloginid cookies isn't set, we will default to the first language,
297                        // the users browser accepts.
298                        list($lang) = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
299                        if(strlen($lang) > 2)
300                        {
301                                $lang = substr($lang,0,2);
302                        }
303                        $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] = $lang;
304                }
305                #print 'LANG:' . $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] . '<br>';
306
307                $GLOBALS['phpgw']->translation->init(); // this will set the language according to the (new) set prefs
308                $GLOBALS['phpgw']->translation->add_app('login');
309                $GLOBALS['phpgw']->translation->add_app('loginscreen');
310                if(lang('loginscreen_message') == 'loginscreen_message*')
311                {
312                        $GLOBALS['phpgw']->translation->add_app('loginscreen','en');    // trying the en one
313                }
314                if(lang('loginscreen_message') != 'loginscreen_message*')
315                {
316                        $tmpl->set_var('lang_message',stripslashes(lang('loginscreen_message')));
317                }
318        }
319
320        if($GLOBALS['phpgw_info']['server']['use_prefix_organization'])
321        {
322                $organization_select = "<tr><td width=\"66\" class=\"loginLabel\">";
323                $organization_select .= lang("organization").":</td>";
324                $organization_select .="<td width=\"135\">";                                                                                   
325                $organization_select .="<select name=\"organization\">\n";             
326               
327                $obj_organization = CreateObject('phpgwapi.sector_search_ldap');
328                $organizations = $obj_organization->organization_search($GLOBALS['phpgw_info']['server']['ldap_context']);
329               
330                for ($i=0; $i<count($organizations); $i++)
331                {
332                        $tmp_array[strtolower($organizations[$i])] = $organizations[$i];       
333                }
334               
335                $arrayOrganization = $tmp_array;               
336                ksort($arrayOrganization);
337               
338                foreach($arrayOrganization
339                         as $organization_name => $organization_vars)
340                {
341                        $organization_select .= '<option value="' . $organization_name . '"';
342
343                        if($organization_name == $_COOKIE['last_organization'])
344                        {
345                                $organization_select .= ' selected';
346                        }
347                        $organization_select .= '>' . $organization_vars . "</option>\n";
348                }
349                $organization_select .= "</select>\n";
350                $organization_select .="</td><td>&nbsp;</td></tr>";
351                $tmpl->set_var('select_organization',$organization_select);
352        }
353
354        $domain_select = '&nbsp;';
355        $last_loginid = $_COOKIE['last_loginid'];
356        if($GLOBALS['phpgw_info']['server']['show_domain_selectbox'])
357        {
358                $domain_select = "<select name=\"logindomain\">\n";
359                foreach($GLOBALS['phpgw_domain'] as $domain_name => $domain_vars)
360                {
361                        $domain_select .= '<option value="' . $domain_name . '"';
362
363                        if($domain_name == $_COOKIE['last_domain'])
364                        {
365                                $domain_select .= ' selected';
366                        }
367                        $domain_select .= '>' . $domain_name . "</option>\n";
368                }
369                $domain_select .= "</select>\n";
370        }
371        elseif($last_loginid !== '')
372        {
373                reset($GLOBALS['phpgw_domain']);
374                list($default_domain) = each($GLOBALS['phpgw_domain']);
375
376                if($_COOKIE['last_domain'] != $default_domain && !empty($_COOKIE['last_domain']))
377                {
378                        $last_loginid .= '@' . $_COOKIE['last_domain'];
379                }
380        }
381        $tmpl->set_var('select_domain',$domain_select);
382
383        foreach($_GET as $name => $value)
384        {
385                if(ereg('phpgw_',$name))
386                {
387                        $extra_vars .= '&' . $name . '=' . urlencode($value);
388                }
389        }
390
391        if($extra_vars)
392        {
393                $extra_vars = '?' . substr($extra_vars,1);
394        }
395
396        /********************************************************\
397        * Check is the registration app is installed, activated  *
398        * And if the register link must be placed                *
399        \********************************************************/
400       
401        $cnf_reg = createobject('phpgwapi.config','registration');
402        $cnf_reg->read_repository();
403        $config_reg = $cnf_reg->config_data;
404
405        if($config_reg[enable_registration]=='True' && $config_reg[register_link]=='True')
406        {
407                $reg_link='&nbsp;<a href="registration/">'.lang('Not a user yet? Register now').'</a><br/>';
408        }
409
410        $GLOBALS['phpgw_info']['server']['template_set'] = $GLOBALS['phpgw_info']['login_template_set'];
411
412        $tmpl->set_var('register_link',$reg_link);
413        $tmpl->set_var('charset',$GLOBALS['phpgw']->translation->charset());
414        $tmpl->set_var('login_url', $GLOBALS['phpgw_info']['server']['webserver_url'] . '/login.php' . $extra_vars);
415        $tmpl->set_var('registration_url',$GLOBALS['phpgw_info']['server']['webserver_url'] . '/registration/');
416        $tmpl->set_var('version',$GLOBALS['phpgw_info']['server']['versions']['phpgwapi']);
417        $tmpl->set_var('cd',check_logoutcode($_GET['cd']));
418        $tmpl->set_var('cookie',$last_loginid);
419
420        $tmpl->set_var('lang_username',lang('username'));
421        $tmpl->set_var('lang_password',lang('password'));
422        $tmpl->set_var('lang_login',lang('login'));
423
424        $tmpl->set_var('website_title', $GLOBALS['phpgw_info']['server']['site_title']);
425        $tmpl->set_var('template_set',$GLOBALS['phpgw_info']['login_template_set']);
426        $tmpl->set_var('bg_color',($GLOBALS['phpgw_info']['server']['login_bg_color']?$GLOBALS['phpgw_info']['server']['login_bg_color']:'FFFFFF'));
427        $tmpl->set_var('bg_color_title',($GLOBALS['phpgw_info']['server']['login_bg_color_title']?$GLOBALS['phpgw_info']['server']['login_bg_color_title']:'486591'));
428
429        if (substr($GLOBALS['phpgw_info']['server']['login_logo_file'],0,4) == 'http')
430        {
431                $var['logo_file'] = $GLOBALS['phpgw_info']['server']['login_logo_file'];
432        }
433        else
434        {
435                $var['logo_file'] = $GLOBALS['phpgw']->common->image('phpgwapi',$GLOBALS['phpgw_info']['server']['login_logo_file']?$GLOBALS['phpgw_info']['server']['login_logo_file']:'logo');
436        }
437        $var['logo_url'] = $GLOBALS['phpgw_info']['server']['login_logo_url']?$GLOBALS['phpgw_info']['server']['login_logo_url']:'http://www.eGroupWare.org';
438        if (substr($var['logo_url'],0,4) != 'http')
439        {
440                $var['logo_url'] = 'http://'.$var['logo_url'];
441        }
442        $var['logo_title'] = $GLOBALS['phpgw_info']['server']['login_logo_title']?$GLOBALS['phpgw_info']['server']['login_logo_title']:'www.eGroupWare.org';
443        $tmpl->set_var($var);
444
445        if (@$GLOBALS['phpgw_info']['server']['login_show_language_selection'])
446        {
447                $select_lang = '<select name="lang" onchange="'."location.href=location.href+(location.search?'&':'?')+'lang='+this.value".'">';
448                $langs = $GLOBALS['phpgw']->translation->get_installed_langs();
449                uasort($langs,'strcasecmp');
450                foreach ($langs as $key => $name)       // if we have a translation use it
451                {
452                        $select_lang .= "\n\t".'<option value="'.$key.'"'.($key == $GLOBALS['phpgw_info']['user']['preferences']['common']['lang'] ? ' selected="1"' : '').'>'.$name.'</option>';
453                }
454                $select_lang .= "\n</select>\n";
455                $tmpl->set_var(array(
456                        'lang_language' => lang('Language'),
457                        'select_language' => $select_lang,
458                ));
459        }
460        else
461        {
462                $tmpl->set_block('login_form','language_select');
463                $tmpl->set_var('language_select','');
464        }
465
466        $tmpl->set_var('autocomplete', ($GLOBALS['phpgw_info']['server']['autocomplete_login'] ? 'autocomplete="off"' : ''));
467
468        $tmpl->pfp('loginout','login_form');
469?>
Note: See TracBrowser for help on using the repository browser.