source: branches/2.2/phpgwapi/inc/class.sessions.inc.php @ 3430

Revision 3430, 42.1 KB checked in by eduardoalex, 13 years ago (diff)

Ticket #1202 - Máximo de 2 ips por registro na tabela de access log.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare API - Session management                                      *
4  * This file written by Dan Kuykendall <seek3r@phpgroupware.org>            *
5  * and Joseph Engo <jengo@phpgroupware.org>                                 *
6  * and Ralf Becker <ralfbecker@outdoor-training.de>                         *
7  * Copyright (C) 2000, 2001 Dan Kuykendall                                  *
8  * Parts Copyright (C) 2003 Free Software Foundation Inc                    *
9  * -------------------------------------------------------------------------*
10  * This library is part of the eGroupWare API                               *
11  * http://www.egroupware.org/api                                            * 
12  * ------------------------------------------------------------------------ *
13  * This library is free software; you can redistribute it and/or modify it  *
14  * under the terms of the GNU Lesser General Public License as published by *
15  * the Free Software Foundation; either version 2.1 of the License,         *
16  * or any later version.                                                    *
17  * This library is distributed in the hope that it will be useful, but      *
18  * WITHOUT ANY WARRANTY; without even the implied warranty of               *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
20  * See the GNU Lesser General Public License for more details.              *
21  * You should have received a copy of the GNU Lesser General Public License *
22  * along with this library; if not, write to the Free Software Foundation,  *
23  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
24  \**************************************************************************/
25
26
27        /* sessions_type setup moved after the class below - milosch */
28
29        /**
30        * Session Management Libabray
31        *
32        * This allows eGroupWare to use php4 or database sessions
33        *
34        * @package phpgwapi
35        * @subpackage sessions
36        * @abstract
37        * @author NetUSE AG Boris Erdmann, Kristian Koehntopp <br> hacked on by phpGW
38        * @copyright &copy; 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp <br> &copy; 2003 FreeSoftware Foundation
39        * @license LGPL
40        * @link http://www.sanisoft.com/phplib/manual/DB_sql.php
41        * @uses db
42        */
43
44        class sessions_
45        {
46                /**
47                * @var string current user login
48                */
49                var $login;
50
51                /**
52                * @var string current user password
53                */
54                var $passwd;
55
56                /**
57                * @var int current user db/ldap account id
58                */
59                var $account_id;
60
61                /**
62                * @var string current user account login id - ie user@domain
63                */
64                var $account_lid;
65
66                /**
67                * @var string previous page call id - repost prevention
68                */
69                var $history_id;
70
71                /**
72                * @var string domain for current user
73                */
74                var $account_domain;
75
76                /**
77                * @var session type flag, A - anonymous session, N - None, normal session
78                */
79                var $session_flags;
80
81                /**
82                * @var string current user session id
83                */
84                var $sessionid;
85
86                /**
87                * @var string not sure what this does, but it is important :)
88                */
89                var $kp3;
90
91                /**
92                * @var string encryption key?
93                */
94                var $key;
95
96                /**
97                * @var string iv == ivegotnoidea ;) (skwashd)
98                */
99                var $iv;
100
101                /**
102                * @var session data
103                */
104                var $data;
105       
106                /**
107                * @var object holder for the database object
108                */
109                var $db;
110       
111                /**
112                * @var array publicly available methods
113                */
114                var $public_functions = array(
115                        'list_methods' => True,
116                        'update_dla'   => True,
117                        'list'         => True,
118                        'total'        => True
119                );
120
121                /**
122                * @var string domain for cookies
123                */
124                var $cookie_domain;
125
126                /**
127                * @var name of XML-RPC/SOAP method called
128                */
129                var $xmlrpc_method_called;
130
131                /**
132                * Constructor just loads up some defaults from cookies
133                */
134                function sessions_()
135                {
136                        $this->db = $GLOBALS['phpgw']->db;
137                        $this->sessionid = get_var('sessionid',array('GET','COOKIE'));
138                        $this->kp3       = get_var('kp3',array('GET','COOKIE'));
139                        /* Create the crypto object */
140                        $GLOBALS['phpgw']->crypto = CreateObject('phpgwapi.crypto');
141                        if ($GLOBALS['phpgw_info']['server']['usecookies'])
142                        {
143                                $this->phpgw_set_cookiedomain();
144                        }
145                        // verfiy and if necessary create and save our config settings
146                        //
147                        $save_rep = False;
148                        if (!isset($GLOBALS['phpgw_info']['server']['max_access_log_age']))
149                        {
150                                $GLOBALS['phpgw_info']['server']['max_access_log_age'] = 90;    // default 90 days
151                                $save_rep = True;
152                        }
153                        if (!isset($GLOBALS['phpgw_info']['server']['block_time']))
154                        {
155                                $GLOBALS['phpgw_info']['server']['block_time'] = 30;    // default 30min
156                                $save_rep = True;
157                        }
158                        if (!isset($GLOBALS['phpgw_info']['server']['num_unsuccessful_id']))
159                        {
160                                $GLOBALS['phpgw_info']['server']['num_unsuccessful_id']  = 3;   // default 3 trys per id
161                                $save_rep = True;
162                        }
163                        if (!isset($GLOBALS['phpgw_info']['server']['num_unsuccessful_ip']))
164                        {
165                                $GLOBALS['phpgw_info']['server']['num_unsuccessful_ip']  = $GLOBALS['phpgw_info']['server']['num_unsuccessful_id'];     // default same as for id
166                                $save_rep = True;
167                        }
168                        if (!isset($GLOBALS['phpgw_info']['server']['install_id']))
169                        {
170                                $GLOBALS['phpgw_info']['server']['install_id']  = md5($GLOBALS['phpgw']->common->randomstring(15));
171                                $save_rep = True;
172                        }
173                        if (!isset($GLOBALS['phpgw_info']['server']['sessions_timeout']))
174                        {
175                                $GLOBALS['phpgw_info']['server']['sessions_timeout'] = 14400;
176                                $save_rep = True;
177                        }
178                        if (!isset($GLOBALS['phpgw_info']['server']['sessions_app_timeout']))
179                        {
180                                $GLOBALS['phpgw_info']['server']['sessions_app_timeout'] = 86400;
181                                $save_rep = True;
182                        }
183                        if (!isset($GLOBALS['phpgw_info']['server']['max_history']))
184                        {
185                                $GLOBALS['phpgw_info']['server']['max_history'] = 20;
186                                $save_rep = True;
187                        }
188                       
189                        // jakjr: ? usando o hardcode, para evitar sempre 2 chamadas ao banco.
190                        /*
191                        if ($save_rep)
192                        {
193                                $config = CreateObject('phpgwapi.config','phpgwapi');
194                                $config->read_repository();
195                                $config->value('max_access_log_age',$GLOBALS['phpgw_info']['server']['max_access_log_age']);
196                                $config->value('block_time',$GLOBALS['phpgw_info']['server']['block_time']);
197                                $config->value('num_unsuccessful_id',$GLOBALS['phpgw_info']['server']['num_unsuccessful_id']);
198                                $config->value('num_unsuccessful_ip',$GLOBALS['phpgw_info']['server']['num_unsuccessful_ip']);
199                                $config->value('install_id',$GLOBALS['phpgw_info']['server']['install_id']);
200                                $config->value('sessions_timeout',$GLOBALS['phpgw_info']['server']['sessions_timeout']);
201                                $config->value('sessions_app_timeout',$GLOBALS['phpgw_info']['server']['sessions_app_timeout']);
202                                $config->save_repository();
203                                unset($config);
204                        }*/
205                }
206
207                /**
208                * Introspection for XML-RPC/SOAP
209                * Diabled - why??
210                *
211                * @param string $_type tpye of introspection being sought
212                * @return array available methods and args
213                */
214                function DONTlist_methods($_type)
215                {
216                        if (is_array($_type))
217                        {
218                                $_type = $_type['type'];
219                        }
220
221                        switch($_type)
222                        {
223                                case 'xmlrpc':
224                                        $xml_functions = array(
225                                                'list_methods' => array(
226                                                        'function'  => 'list_methods',
227                                                        'signature' => array(array(xmlrpcStruct,xmlrpcString)),
228                                                        'docstring' => lang('Read this list of methods.')
229                                                ),
230                                                'update_dla' => array(
231                                                        'function'  => 'update_dla',
232                                                        'signature' => array(array(xmlrpcBoolean)),
233                                                        'docstring' => lang('Returns an array of todo items')
234                                                )
235                                        );
236                                        return $xml_functions;
237                                        break;
238                                case 'soap':
239                                        return $this->soap_functions;
240                                        break;
241                                default:
242                                        return array();
243                                        break;
244                        }
245                }
246
247                function split_login_domain($both,&$login,&$domain)
248                {
249                        $parts = explode('@',$both);
250                        $domain = count($parts) > 1 ? array_pop($parts) :
251                                $GLOBALS['phpgw_info']['server']['default_domain'];
252                        $login = implode('@',$parts);
253                }
254
255                /**
256                * Check to see if a session is still current and valid
257                *
258                * @param string $sessionid session id to be verfied
259                * @param string $kp3 ?? to be verified
260                * @return bool is the session valid?
261                */
262                function verify($sessionid='',$kp3='')
263                {
264                        if(empty($sessionid) || !$sessionid)
265                        {
266                                $sessionid = get_var('sessionid',array('GET','COOKIE'));
267                                $kp3       = get_var('kp3',array('GET','COOKIE'));
268                        }
269
270                        $this->sessionid = $sessionid;
271                        $this->kp3       = $kp3;
272
273                        $session = $this->read_session();
274                        //echo "<pre>session::verify(id='$sessionid'): \n".print_r($session,True)."</pre>\n";
275                        /*
276                        $fp = fopen('/tmp/session_verify','a+');
277                        fwrite($fp,"session::verify(id='$sessionid'): \n".print_r($session,True)."\n\n");
278                        fclose($fp);
279                        */
280                        if ($session['session_dla'] <= (time() - $GLOBALS['phpgw_info']['server']['sessions_timeout']))
281                        {
282                                $this->destroy($sessionid,$kp3);
283                                return False;
284                        }
285
286                        $this->session_flags = $session['session_flags'];
287
288                        sessions_::split_login_domain($session['session_lid'],$this->account_lid,$this->account_domain);
289
290                        $GLOBALS['phpgw_info']['user']['kp3'] = $this->kp3;
291
292                        $this->update_dla();
293                        if (isset($_SESSION['phpgw_session']['account_id']))
294                                $this->account_id =  $_SESSION['phpgw_session']['account_id'];
295                        else
296                                $this->account_id = $GLOBALS['phpgw']->accounts->name2id($this->account_lid);
297                        if (!$this->account_id)
298                        {
299                                return False;
300                        }
301                        $GLOBALS['phpgw_info']['user']['account_id'] = $this->account_id;
302                        $_SESSION['phpgw_session']['account_id'] = $this->account_id;
303
304                        /* init the crypto object before appsession call below */
305                        $this->key = md5($this->kp3 . $this->sessionid . @$GLOBALS['phpgw_info']['server']['encryptkey']);
306                        $this->iv  = $GLOBALS['phpgw_info']['server']['mcrypt_iv'];
307                        $GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
308
309                        $this->read_repositories(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info']);
310                        if (strlen($this->user['expires']) == 0)
311                                $this->user['expires'] = $_SESSION['phpgw_session']['expires_account'];
312                        if ($this->user['expires'] != -1 && $this->user['expires'] < time())
313                        {
314                                if(is_object($GLOBALS['phpgw']->log))
315                                {
316                                        $GLOBALS['phpgw']->log->message(array(
317                                                'text' => 'W-VerifySession, account loginid %1 is expired',
318                                                'p1'   => $this->account_lid,
319                                                'line' => __LINE__,
320                                                'file' => __FILE__
321                                        ));
322                                        $GLOBALS['phpgw']->log->commit();
323                                }
324                                return False;
325                        }
326                        $_SESSION['phpgw_session']['expires_account'] = $this->user['expires'];
327
328
329                        $GLOBALS['phpgw_info']['user']  = $this->user;
330                        $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
331
332                        $GLOBALS['phpgw_info']['user']['session_ip'] = $session['session_ip'];
333                        $GLOBALS['phpgw_info']['user']['passwd']     = base64_decode($this->appsession('password','phpgwapi'));
334
335                        if ($this->account_domain != $GLOBALS['phpgw_info']['user']['domain'])
336                        {
337                                if(is_object($GLOBALS['phpgw']->log))
338                                {
339                                        $GLOBALS['phpgw']->log->message(array(
340                                                'text' => 'W-VerifySession, the domains %1 and %2 don\'t match',
341                                                'p1'   => $userid_array[1],
342                                                'p2'   => $GLOBALS['phpgw_info']['user']['domain'],
343                                                'line' => __LINE__,
344                                                'file' => __FILE__
345                                        ));
346                                        $GLOBALS['phpgw']->log->commit();
347                                }
348                                return False;
349                        }
350
351                       
352                        $GLOBALS['phpgw']->acl->acl($this->account_id);
353                        $GLOBALS['phpgw']->accounts->accounts($this->account_id);
354                        $GLOBALS['phpgw']->preferences->preferences($this->account_id);
355                        $GLOBALS['phpgw']->applications->applications($this->account_id);
356
357                        if (! $this->account_lid)
358                        {
359                                if(is_object($GLOBALS['phpgw']->log))
360                                {
361                                        // This needs some better wording
362                                        $GLOBALS['phpgw']->log->message(array(
363                                                'text' => 'W-VerifySession, account_id is empty',
364                                                'line' => __LINE__,
365                                                'file' => __FILE__
366                                        ));
367                                        $GLOBALS['phpgw']->log->commit();
368                                }
369                                //echo 'DEBUG: Sessions: account_id is empty!<br>'."\n";
370                                return False;
371                        }
372                        return True;
373                }
374
375                /**
376                * Functions for creating and verifying the session
377                */
378       
379                /**
380                * Get the ip address of current users
381                *
382                * @return string ip address
383                */
384                function getuser_ip()
385                {
386                        $ip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR']."," : "").$_SERVER['REMOTE_ADDR'];
387                        if(strlen($ip)>30) {
388                                $ip_exploded = explode(",",$ip);
389                                $ip = "";
390                                for($i=0;$i<2;$i++)
391                                        $ip .= isset($ip_exploded[$i])?(($i==1?",":"").trim($ip_exploded[$i])):("");
392                                if(strlen($ip)>30)
393                                        $ip = $ip_exploded[0];
394                        }
395                       
396                        return $ip;
397                }
398
399                /**
400                * Set the domain used for cookies
401                *
402                * @return string domain
403                */
404                function phpgw_set_cookiedomain()
405                {
406                        // Use HTTP_X_FORWARDED_HOST if set, which is the case behind a none-transparent proxy
407                        $this->cookie_domain = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ?  $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
408
409                        // remove port from HTTP_HOST
410                        if (preg_match("/^(.*):(.*)$/",$this->cookie_domain,$arr))
411                        {
412                                $this->cookie_domain = $arr[1];
413                        }
414                        if (count(explode('.',$this->cookie_domain)) <= 1)
415                        {
416                                // setcookie dont likes domains without dots, leaving it empty, gets setcookie to fill the domain in
417                                $this->cookie_domain = '';
418                        }
419                        print_debug('COOKIE_DOMAIN',$this->cookie_domain,'api');
420
421                        $this->set_cookie_params($this->cookie_domain); // for php4 sessions necessary
422                }
423
424                /**
425                * Set a cookie
426                *
427                * @param string $cookiename name of cookie to be set
428                * @param string $cookievalue value to be used, if unset cookie is cleared (optional)
429                * @param int $cookietime when cookie should expire, 0 for session only (optional)
430                */
431                function phpgw_setcookie($cookiename,$cookievalue='',$cookietime=0)
432                {
433                        if (!$this->cookie_domain)
434                        {
435                                $this->phpgw_set_cookiedomain();
436                        }
437                        setcookie($cookiename,$cookievalue,$cookietime,'/',$this->cookie_domain,null,true);
438                }
439
440                /**
441                * Create a new session
442                *
443                * @param string $login user login
444                * @param string $passwd user password
445                * @param string $passwd_type type of password being used, ie plaintext, md5, sha1
446                * @return string session id
447                */
448                function create($login,$passwd = '',$passwd_type = '')
449                {
450                        if (is_array($login))
451                        {
452                                $this->login       = $login['login'];
453                                $this->passwd      = $login['passwd'];
454                                $this->passwd_type = $login['passwd_type'];
455                                $login             = $this->login;
456                        }
457                        else
458                        {
459                                $this->login       = $login;
460                                $this->passwd      = $passwd;
461                                $this->passwd_type = $passwd_type;
462                        }
463
464                        $this->clean_sessions();
465                        //sessions_::split_login_domain($login,$this->account_lid,$this->account_domain);
466                        // jakjr: allow uid with (@);
467                        $this->account_lid = $login;
468                        $this->account_domain = 'default';
469
470                        $now = time();
471
472                        //echo "<p>session::create(login='$login'): lid='$this->account_lid', domain='$this->account_domain'</p>\n";
473                        $user_ip = $this->getuser_ip();
474                               
475                        $this->account_id = $GLOBALS['phpgw']->accounts->name2id($this->account_lid);
476
477                        if (($blocked = $this->login_blocked($login,$user_ip)) ||       // too many unsuccessful attempts
478                                $GLOBALS['phpgw_info']['server']['global_denied_users'][$this->account_lid] ||
479                                !$GLOBALS['phpgw']->auth->authenticate($this->account_lid, $this->passwd, $this->passwd_type) ||
480                                $this->account_id && $GLOBALS['phpgw']->accounts->get_type($this->account_id) == 'g')
481                        {
482                                $this->reason = $blocked ? 'blocked, too many attempts' : 'bad login or password';
483                                $this->cd_reason = $blocked ? 99 : 5;
484
485                                $this->log_access($this->reason,$login,$user_ip,0);     // log unsuccessfull login
486                                return False;
487                        }
488                        // Só verifica tempo de inatividade do usuário, caso esteja configurado no Administrador.
489                        if(isset($GLOBALS['phpgw_info']['server']['time_to_account_expires']) &&
490                                $this->account_id !=null && $this->account_lid != "expresso-admin") {
491                                        $last_access = $this->get_last_access_on_history($this->account_id);
492                                        $this->read_repositories(False);
493                                        if ($last_access && ($last_access+($GLOBALS['phpgw_info']['server']['time_to_account_expires']*86400) < time()))
494                                        {
495                                                if(is_object($GLOBALS['phpgw']->log))
496                                                {
497                                                        $GLOBALS['phpgw']->log->message(array(
498                                                                'text' => 'W-LoginFailure, account loginid %1 is expired for innativity',
499                                                                'p1'   => $this->account_lid,
500                                                                'line' => __LINE__,
501                                                                'file' => __FILE__
502                                                        ));
503                                                        $GLOBALS['phpgw']->log->commit();
504                                                }
505                                                $this->reason = 'account is expired';
506                                                $this->cd_reason = 98;
507               
508                                                return False;
509                                        }
510                        }
511
512                        /* jakjr: Expresso does not use auto-create account.
513                        if (!$this->account_id && $GLOBALS['phpgw_info']['server']['auto_create_acct'] == True)
514                        {
515                                $this->account_id = $GLOBALS['phpgw']->accounts->auto_add($this->account_lid, $passwd);
516                        }
517                        */
518
519                        $GLOBALS['phpgw_info']['user']['account_id'] = $this->account_id;
520                        $GLOBALS['phpgw']->accounts->accounts($this->account_id);
521                        $this->sessionid = $this->new_session_id();
522                        $this->kp3       = md5($GLOBALS['phpgw']->common->randomstring(15));
523
524                        if ($GLOBALS['phpgw_info']['server']['usecookies'])
525                        {
526                                $this->phpgw_setcookie('sessionid',$this->sessionid);
527                                $this->phpgw_setcookie('kp3',$this->kp3);
528                                $this->phpgw_setcookie('domain',$this->account_domain);
529                        }
530                        if ($GLOBALS['phpgw_info']['server']['usecookies'] || isset($_COOKIE['last_loginid']))
531                        {
532                                $this->phpgw_setcookie('last_loginid', $this->account_lid ,$now+1209600); /* For 2 weeks */
533                                $this->phpgw_setcookie('last_domain',$this->account_domain,$now+1209600);
534                                $this->phpgw_setcookie('last_organization',$_POST['organization'],$now+1209600);
535                        }
536                        unset($GLOBALS['phpgw_info']['server']['default_domain']); /* we kill this for security reasons */
537
538                        /* init the crypto object */
539                        $this->key = md5($this->kp3 . $this->sessionid . $GLOBALS['phpgw_info']['server']['encryptkey']);
540                        $this->iv  = $GLOBALS['phpgw_info']['server']['mcrypt_iv'];
541                        $GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
542
543                        $this->read_repositories(False);
544                        if ($this->user['expires'] != -1 && $this->user['expires'] < time())
545                        {
546                                if(is_object($GLOBALS['phpgw']->log))
547                                {
548                                        $GLOBALS['phpgw']->log->message(array(
549                                                'text' => 'W-LoginFailure, account loginid %1 is expired',
550                                                'p1'   => $this->account_lid,
551                                                'line' => __LINE__,
552                                                'file' => __FILE__
553                                        ));
554                                        $GLOBALS['phpgw']->log->commit();
555                                }
556                                $this->reason = 'account is expired';
557                                $this->cd_reason = 98;
558
559                                return False;
560                        }
561
562                        $GLOBALS['phpgw_info']['user']  = $this->user;
563                        $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
564
565                        $this->appsession('password','phpgwapi',base64_encode($this->passwd));
566                        if ($GLOBALS['phpgw']->acl->check('anonymous',1,'phpgwapi'))
567                        {
568                                $session_flags = 'A';
569                        }
570                        else
571                        {
572                                $session_flags = 'N';
573                        }
574
575                        $GLOBALS['phpgw']->db->transaction_begin();
576                        $this->register_session($login,$user_ip,$now,$session_flags);
577                        if ($session_flags != 'A')              // dont log anonymous sessions
578                        {
579                                $this->log_access($this->sessionid,$login,$user_ip,$this->account_id);
580                        }
581                        $this->appsession('account_previous_login','phpgwapi',$GLOBALS['phpgw']->auth->previous_login);
582                        // Expresso
583                        //$GLOBALS['phpgw']->auth->update_lastlogin($this->account_id,$user_ip);
584                        $GLOBALS['phpgw']->db->transaction_commit();
585
586                        //if (!$this->sessionid) echo "<p>session::create(login='$login') = '$this->sessionid': lid='$this->account_lid', domain='$this->account_domain'</p>\n";
587
588                        return $this->sessionid;
589                }
590
591                /**
592                 * Retorna o UNIX DATE do ultimo acesso dessa conta, baseado na tabela de histórico.
593                 */
594                function get_last_access_on_history($account_id) {
595                        $GLOBALS['phpgw']->db->query("select li from phpgw_access_log where account_id='$account_id' order by li desc limit 1",__LINE__,__FILE__);
596                        if(!$GLOBALS['phpgw']->db->next_record())
597                                return false;
598                        return $GLOBALS['phpgw']->db->f('li');
599                }
600
601                /**
602        * Write or update (for logout) the access_log
603                *
604                * @param string $sessionid id of session or 0 for unsuccessful logins
605                * @param string $login account_lid (evtl. with domain) or '' for settion the logout-time
606                * @param string $user_ip ip to log
607                * @param int $account_id numerical account_id
608                */
609                function log_access($sessionid,$login='',$user_ip='',$account_id='')
610                {
611                        $now = time();
612
613                        if ($login != '')
614                        {
615                                if (strlen($login) > 30)
616                                {
617                                        $login = substr($login,0,30);
618                                }
619                                $GLOBALS['phpgw']->db->query('INSERT INTO phpgw_access_log(sessionid,loginid,ip,li,lo,account_id,browser)'
620                                        . " VALUES ('" . $sessionid . "','" . $this->db->db_addslashes($login). "','"
621                                        . $this->db->db_addslashes($user_ip) . "',$now,0," . (int)$account_id .",'".$this->db->db_addslashes(substr($_SERVER[ 'HTTP_USER_AGENT' ],0,199))."')",__LINE__,__FILE__);
622                        }
623                        else if($sessionid != 'bad login or password')
624                        {
625                                $GLOBALS['phpgw']->db->query("UPDATE phpgw_access_log SET lo=" . $now . " WHERE sessionid='"
626                                        . $sessionid . "'",__LINE__,__FILE__);
627                        }
628
629                        /* jakjr: Clean phpgw_access_log with a crontab event.
630                        if ($GLOBALS['phpgw_info']['server']['max_access_log_age'])
631                        {
632                                $max_age = $now - $GLOBALS['phpgw_info']['server']['max_access_log_age'] * 24 * 60 * 60;
633
634                                $GLOBALS['phpgw']->db->query("DELETE FROM phpgw_access_log WHERE li < $max_age");
635                        }
636                        */
637                }
638
639                /**
640                * Protect against brute force attacks, block login if too many unsuccessful login attmepts
641        *
642                * @param string $login account_lid (evtl. with domain)
643                * @param string $ip ip of the user
644                * @returns bool login blocked?
645                */
646                function login_blocked($login,$ip)
647                {
648                        /*jakjr: Disable this protection. When block an proxy server ip, all the sub-network will be blocking.*/
649                        //return false; //
650
651                        $blocked = False;
652                        $block_time = time() - $GLOBALS['phpgw_info']['server']['block_time'] * 60;
653                        /*
654                        $ip = $this->db->db_addslashes($ip);
655                        $this->db->query("SELECT count(*) FROM phpgw_access_log WHERE account_id=0 AND ip='$ip' AND li > $block_time",__LINE__,__FILE__);
656                        $this->db->next_record();
657                        if (($false_ip = $this->db->f(0)) > $GLOBALS['phpgw_info']['server']['num_unsuccessful_ip'])
658                        {
659                                //echo "<p>login_blocked: ip='$ip' ".$this->db->f(0)." trys (".$GLOBALS['phpgw_info']['server']['num_unsuccessful_ip']." max.) since ".date('Y/m/d H:i',$block_time)."</p>\n";
660                                $blocked = True;
661                        }
662                        */
663                        $login = $this->db->db_addslashes($login);
664                        $this->db->query("SELECT count(*) FROM phpgw_access_log WHERE account_id=0 AND (loginid='$login' OR loginid LIKE '$login@%') AND li > $block_time",__LINE__,__FILE__);
665                        $this->db->next_record();
666                        if (($false_id = $this->db->f(0)) > $GLOBALS['phpgw_info']['server']['num_unsuccessful_id'])
667                        {
668                                //echo "<p>login_blocked: login='$login' ".$this->db->f(0)." trys (".$GLOBALS['phpgw_info']['server']['num_unsuccessful_id']." max.) since ".date('Y/m/d H:i',$block_time)."</p>\n";
669                                $blocked = True;
670                        }
671                        if ($blocked && $GLOBALS['phpgw_info']['server']['admin_mails'] &&
672                                // max. one mail each 5mins
673                                $GLOBALS['phpgw_info']['server']['login_blocked_mail_time'] < time()-5*60)
674                        {
675                                // notify admin(s) via email
676                                $from    = 'eGroupWare@'.$GLOBALS['phpgw_info']['server']['mail_suffix'];
677                                $subject = lang("eGroupWare: login blocked for user '%1', IP %2",$login,$ip);
678                                $body    = lang("Too many unsucessful attempts to login: %1 for the user '%2', %3 for the IP %4",$false_id,$login,$false_ip,$ip);
679                               
680                                if(!is_object($GLOBALS['phpgw']->send))
681                                {
682                                        $GLOBALS['phpgw']->send = CreateObject('phpgwapi.send');
683                                }
684                                $subject = $GLOBALS['phpgw']->send->encode_subject($subject);
685                                $admin_mails = explode(',',$GLOBALS['phpgw_info']['server']['admin_mails']);
686                                foreach($admin_mails as $to)
687                                {
688                                        $GLOBALS['phpgw']->send->msg('email',$to,$subject,$body,'','','',$from,$from);
689                                }
690                                // save time of mail, to not send to many mails
691                                $config = CreateObject('phpgwapi.config','phpgwapi');
692                                $config->read_repository();
693                                $config->value('login_blocked_mail_time',time());
694                                $config->save_repository();
695                        }
696                        return $blocked;
697                }
698
699                /**
700                * Verfy a peer server access request
701                *
702                * @param string $sessionid session id to verfiy
703                * @param string $kp3 ??
704                * @return bool verfied?
705                */
706                function verify_server($sessionid, $kp3)
707                {
708                        $GLOBALS['phpgw']->interserver = CreateObject('phpgwapi.interserver');
709                        $this->sessionid = $sessionid;
710                        $this->kp3       = $kp3;
711
712                        $session = $this->read_session();
713                        $this->session_flags = $session['session_flags'];
714
715                        list($this->account_lid,$this->account_domain) = explode('@', $session['session_lid']);
716                       
717                        if ($this->account_domain == '')
718                        {
719                                $this->account_domain = $GLOBALS['phpgw_info']['server']['default_domain'];
720                        }
721
722                        $GLOBALS['phpgw_info']['user']['kp3'] = $this->kp3;
723                        $phpgw_info_flags = $GLOBALS['phpgw_info']['flags'];
724
725                        $GLOBALS['phpgw_info']['flags'] = $phpgw_info_flags;
726
727                        $this->update_dla();
728                        $this->account_id = $GLOBALS['phpgw']->interserver->name2id($this->account_lid);
729
730                        if (!$this->account_id)
731                        {
732                                return False;
733                        }
734
735                        $GLOBALS['phpgw_info']['user']['account_id'] = $this->account_id;
736
737                        $this->read_repositories(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info']);
738
739                        /* init the crypto object before appsession call below */
740                        $this->key = md5($this->kp3 . $this->sessionid . $GLOBALS['phpgw_info']['server']['encryptkey']);
741                        $this->iv  = $GLOBALS['phpgw_info']['server']['mcrypt_iv'];
742                        $GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
743
744                        $GLOBALS['phpgw_info']['user']  = $this->user;
745                        $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
746
747                        $GLOBALS['phpgw_info']['user']['session_ip'] = $session['session_ip'];
748                        $GLOBALS['phpgw_info']['user']['passwd'] = base64_decode($this->appsession('password','phpgwapi'));
749
750                        if ($userid_array[1] != $GLOBALS['phpgw_info']['user']['domain'])
751                        {
752                                if(is_object($GLOBALS['phpgw']->log))
753                                {
754                                        $GLOBALS['phpgw']->log->message(array(
755                                                'text' => 'W-VerifySession, the domains %1 and %2 don\t match',
756                                                'p1'   => $userid_array[1],
757                                                'p2'   => $GLOBALS['phpgw_info']['user']['domain'],
758                                                'line' => __LINE__,
759                                                'file' => __FILE__
760                                        ));
761                                        $GLOBALS['phpgw']->log->commit();
762                                }
763
764                                if(is_object($GLOBALS['phpgw']->crypto))
765                                {
766                                        $GLOBALS['phpgw']->crypto->cleanup();
767                                        unset($GLOBALS['phpgw']->crypto);
768                                }
769                                return False;
770                        }
771
772                        if(@$GLOBALS['phpgw_info']['server']['sessions_checkip'])
773                        {
774                                if((PHP_OS != 'Windows') && (PHP_OS != 'WINNT') &&
775                                        (!$GLOBALS['phpgw_info']['user']['session_ip'] || $GLOBALS['phpgw_info']['user']['session_ip'] != $this->getuser_ip())
776                                )
777                                {
778                                        if(is_object($GLOBALS['phpgw']->log))
779                                        {
780                                                // This needs some better wording
781                                                $GLOBALS['phpgw']->log->message(array(
782                                                        'text' => 'W-VerifySession, IP %1 doesn\'t match IP %2 in session table',
783                                                        'p1'   => $this->getuser_ip(),
784                                                        'p2'   => $GLOBALS['phpgw_info']['user']['session_ip'],
785                                                        'line' => __LINE__,
786                                                        'file' => __FILE__
787                                                ));
788                                                $GLOBALS['phpgw']->log->commit();
789                                        }
790
791                                        if(is_object($GLOBALS['phpgw']->crypto))
792                                        {
793                                                $GLOBALS['phpgw']->crypto->cleanup();
794                                                unset($GLOBALS['phpgw']->crypto);
795                                        }
796                                        return False;
797                                }
798                        }
799
800                        $GLOBALS['phpgw']->acl->acl($this->account_id);
801                        $GLOBALS['phpgw']->accounts->accounts($this->account_id);
802                        $GLOBALS['phpgw']->preferences->preferences($this->account_id);
803                        $GLOBALS['phpgw']->applications->applications($this->account_id);
804
805                        if (! $this->account_lid)
806                        {
807                                if(is_object($GLOBALS['phpgw']->log))
808                                {
809                                        // This needs some better wording
810                                        $GLOBALS['phpgw']->log->message(array(
811                                                'text' => 'W-VerifySession, account_id is empty',
812                                                'line' => __LINE__,
813                                                'file' => __FILE__
814                                        ));
815                                        $GLOBALS['phpgw']->log->commit();
816                                }
817
818                                if(is_object($GLOBALS['phpgw']->crypto))
819                                {
820                                        $GLOBALS['phpgw']->crypto->cleanup();
821                                        unset($GLOBALS['phpgw']->crypto);
822                                }
823                                return False;
824                        }
825                        else
826                        {
827                                return True;
828                        }
829                }
830
831                /**
832                * Validate a peer server login request
833                *
834                * @param string $login login name
835                * @param string $password password
836                * @return bool login ok?
837                */
838                function create_server($login,$passwd)
839                {
840                        $GLOBALS['phpgw']->interserver = CreateObject('phpgwapi.interserver');
841                        $this->login  = $login;
842                        $this->passwd = $passwd;
843                        $this->clean_sessions();
844                        $login_array = explode('@', $login);
845                        $this->account_lid = $login_array[0];
846                        $now = time();
847
848                        if ($login_array[1] != '')
849                        {
850                                $this->account_domain = $login_array[1];
851                        }
852                        else
853                        {
854                                $this->account_domain = $GLOBALS['phpgw_info']['server']['default_domain'];
855                        }
856
857                        $serverdata = array(
858                                'server_name' => $this->account_domain,
859                                'username'    => $this->account_lid,
860                                'password'    => $passwd
861                        );
862                        if (!$GLOBALS['phpgw']->interserver->auth($serverdata))
863                        {
864                                return False;
865                                exit;
866                        }
867
868                        if (!$GLOBALS['phpgw']->interserver->exists($this->account_lid))
869                        {
870                                $this->account_id = $GLOBALS['phpgw']->interserver->name2id($this->account_lid);
871                        }
872                        $GLOBALS['phpgw_info']['user']['account_id'] = $this->account_id;
873                        $GLOBALS['phpgw']->interserver->serverid = $this->account_id;
874
875                        $this->sessionid = md5($GLOBALS['phpgw']->common->randomstring(10));
876                        $this->kp3       = md5($GLOBALS['phpgw']->common->randomstring(15));
877
878                        /* re-init the crypto object */
879                        $this->key = md5($this->kp3 . $this->sessionid . $GLOBALS['phpgw_info']['server']['encryptkey']);
880                        $this->iv  = $GLOBALS['phpgw_info']['server']['mcrypt_iv'];
881                        $GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
882
883                        //$this->read_repositories(False);
884
885                        $GLOBALS['phpgw_info']['user']  = $this->user;
886                        $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
887
888                        $this->appsession('password','phpgwapi',base64_encode($this->passwd));
889                        $session_flags = 'S';
890
891                        $user_ip = $this->getuser_ip();
892
893                        $GLOBALS['phpgw']->db->transaction_begin();
894                        $this->register_session($login,$user_ip,$now,$session_flags);
895
896                        $this->log_access($this->sessionid,$login,$user_ip,$this->account_id);
897
898                        $this->appsession('account_previous_login','phpgwapi',$GLOBALS['phpgw']->auth->previous_login);
899                        $GLOBALS['phpgw']->auth->update_lastlogin($this->account_id,$user_ip);
900                        $GLOBALS['phpgw']->db->transaction_commit();
901
902                        return array($this->sessionid,$this->kp3);
903                }
904
905                /**
906                * Functions for appsession data and session cache
907                */
908
909                /**
910                * Is this also useless?? (skwashd)
911                */
912                function read_repositories($cached='',$write_cache=True)
913                {
914                        $GLOBALS['phpgw']->acl->acl($this->account_id);
915                        $GLOBALS['phpgw']->accounts->accounts($this->account_id);
916                        $GLOBALS['phpgw']->preferences->preferences($this->account_id);
917                        $GLOBALS['phpgw']->applications->applications($this->account_id);
918
919                        if(@$cached)
920                        {
921                                $this->user = $this->appsession('phpgw_info_cache','phpgwapi');
922                                if(!empty($this->user))
923                                {
924                                        $GLOBALS['phpgw']->preferences->data = $this->user['preferences'];
925                                        if (!isset($GLOBALS['phpgw_info']['apps']) || !is_array($GLOBALS['phpgw_info']['apps']))
926                                        {
927                                                $GLOBALS['phpgw']->applications->read_installed_apps();
928                                        }
929                                }
930                                else
931                                {
932                                        $this->setup_cache($write_cache);
933                                }
934                        }
935                        else
936                        {
937                                $this->setup_cache($write_cache);
938                        }
939                        $this->hooks = $GLOBALS['phpgw']->hooks->read();
940                }
941
942                /**
943                * Is this also useless?? (skwashd)
944                */
945                function setup_cache($write_cache=True)
946                {
947                        $this->user                = $GLOBALS['phpgw']->accounts->read_repository();
948                        $this->user['acl']         = $GLOBALS['phpgw']->acl->read_repository();
949                        $this->user['preferences'] = $GLOBALS['phpgw']->preferences->read_repository();
950                        $this->user['apps']        = $GLOBALS['phpgw']->applications->read_repository();
951                        //@reset($this->data['user']['apps']);
952
953                        $this->user['domain']      = $this->account_domain;
954                        $this->user['sessionid']   = $this->sessionid;
955                        $this->user['kp3']         = $this->kp3;
956                        $this->user['session_ip']  = $this->getuser_ip();
957                        $this->user['session_lid'] = $this->account_lid.'@'.$this->account_domain;
958                        $this->user['account_id']  = $this->account_id;
959                        $this->user['account_lid'] = $this->account_lid;
960                        $this->user['userid']      = $this->account_lid;
961                        $this->user['passwd']      = @$this->passwd;
962                        if(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info'] && $write_cache)
963                        {
964                                $this->delete_cache();
965                                $this->appsession('phpgw_info_cache','phpgwapi',$this->user);
966                        }
967                }
968       
969                /**
970                * This looks to be useless
971                * This will capture everything in the $GLOBALS['phpgw_info'] including server info,
972                * and store it in appsessions.  This is really incompatible with any type of restoring
973                * from appsession as the saved user info is really in ['user'] rather than the root of
974                * the structure, which is what this class likes.
975                */
976                function save_repositories()
977                {
978                        $phpgw_info_temp = $GLOBALS['phpgw_info'];
979                        $phpgw_info_temp['user']['kp3'] = '';
980                        $phpgw_info_temp['flags'] = array();
981
982                        if ($GLOBALS['phpgw_info']['server']['cache_phpgw_info'])
983                        {
984                                $this->appsession('phpgw_info_cache','phpgwapi',$phpgw_info_temp);
985                        }
986                }
987
988                function restore()
989                {
990                        $sessionData = $this->appsession('sessiondata');
991
992                        if (!empty($sessionData) && is_array($sessionData))
993                        {
994                                foreach($sessionData as $key => $value)
995                                {
996                                        global $$key;
997                                        $$key = $value;
998                                        $this->variableNames[$key] = 'registered';
999                                        // echo 'restored: '.$key.', ' . $value . '<br>';
1000                                }
1001                        }
1002                }
1003
1004                /**
1005                * Save the current values of all registered variables
1006                */
1007                function save()
1008                {
1009                        if (is_array($this->variableNames))
1010                        {
1011                                reset($this->variableNames);
1012                                while(list($key, $value) = each($this->variableNames))
1013                                {
1014                                        if ($value == 'registered')
1015                                        {
1016                                                global $$key;
1017                                                $sessionData[$key] = $$key;
1018                                        }
1019                                }
1020                                $this->appsession('sessiondata','',$sessionData);
1021                        }
1022                }
1023
1024                /**
1025                * Create a list a variable names, which data needs to be restored
1026                *
1027                * @param string $_variableName name of variable to be registered
1028                */
1029                function register($_variableName)
1030                {
1031                        $this->variableNames[$_variableName]='registered';
1032                        #print 'registered '.$_variableName.'<br>';
1033                }
1034
1035                /**
1036                * Mark variable as unregistered
1037                *
1038                * @param string $_variableName name of variable to deregister
1039                */
1040                function unregister($_variableName)
1041                {
1042                        $this->variableNames[$_variableName]='unregistered';
1043                        #print 'unregistered '.$_variableName.'<br>';
1044                }
1045
1046                /**
1047                * Check if we have a variable registred already
1048                *
1049                * @param string $_variableName name of variable to check
1050                * @return bool was the variable found?
1051                */
1052                function is_registered($_variableName)
1053                {
1054                        if ($this->variableNames[$_variableName] == 'registered')
1055                        {
1056                                return True;
1057                        }
1058                        else
1059                        {
1060                                return False;
1061                        }
1062                }
1063                /**
1064                * Additional tracking of user actions - prevents reposts/use of back button
1065                *
1066                * @author skwashd
1067                * @return string current history id
1068                */
1069                function generate_click_history()
1070                {
1071                        if(!isset($this->history_id))
1072                        {
1073                                $this->history_id = md5($this->login . time());
1074                                $history = $this->appsession($location = 'history', $appname = 'phpgwapi');
1075                               
1076                                if(count($history) >= $GLOBALS['phpgw_info']['server']['max_history'])
1077                                {
1078                                        array_shift($history);
1079                                        $this->appsession($location = 'history', $appname = 'phpgwapi', $history);
1080                                }
1081                        }
1082                        return $this->history_id;
1083                }
1084               
1085                /**
1086                * Detects if the page has already been called before - good for forms
1087                *
1088                * @author skwashd
1089                * @param bool $diplay_error when implemented will use the generic error handling code
1090                * @return True if called previously, else False - call ok
1091                */
1092                function is_repost($display_error = False)
1093                {
1094                        $history = $this->appsession($location = 'history', $appname = 'phpgwapi');
1095                        if(isset($history[$_GET['click_history']]))
1096                        {
1097                                if($display_error)
1098                                {
1099                                        $GLOBALS['phpgw']->redirect_link('/error.php', 'type=repost');//more on this later :)
1100                                }
1101                                else
1102                                {
1103                                        return True; //handled by the app
1104                                }
1105                        }
1106                        else
1107                        {
1108                                $history[$_GET['click_history']] = True;
1109                                $this->appsession($location = 'history', $appname = 'phpgwapi', $history);
1110                                return False;
1111                        }
1112                }
1113
1114                /**
1115                * Generate a url which supports url or cookies based sessions
1116                *
1117                * @param string $url a url relative to the egroupware install root
1118                * @param array $extravars query string arguements
1119                * @return string generated url
1120                */
1121                function link($url, $extravars = '')
1122                {
1123                        //echo "<p>session::link(url='".print_r($url,True)."',extravars='".print_r($extravars,True)."')";
1124                        /* first we process the $url to build the full scriptname */
1125                        $full_scriptname = True;
1126
1127                        $url_firstchar = substr($url ,0,1);
1128                        if ($url_firstchar == '/' && $GLOBALS['phpgw_info']['server']['webserver_url'] == '/')
1129                        {
1130                                $full_scriptname = False;
1131                        }
1132
1133                        if ($url_firstchar != '/')
1134                        {
1135                                $app = $GLOBALS['phpgw_info']['flags']['currentapp'];
1136                                if ($app != 'home' && $app != 'login' && $app != 'logout')
1137                                {
1138                                        $url = $app.'/'.$url;
1139                                }
1140                        }
1141
1142                        if($full_scriptname)
1143                        {
1144                                $webserver_url_count = strlen($GLOBALS['phpgw_info']['server']['webserver_url'])-1;
1145                                if(substr($GLOBALS['phpgw_info']['server']['webserver_url'] ,$webserver_url_count,1) != '/' && $url_firstchar != '/')
1146                                {
1147                                        $url = $GLOBALS['phpgw_info']['server']['webserver_url'] .'/'. $url;
1148                                }
1149                                else
1150                                {
1151                                        $url = $GLOBALS['phpgw_info']['server']['webserver_url'] . $url;
1152                                }
1153                        }
1154
1155                        if(@isset($GLOBALS['phpgw_info']['server']['enforce_ssl']) && $GLOBALS['phpgw_info']['server']['enforce_ssl']) // && !$_SERVER['HTTPS']) imho https should always be a full path - skwashd
1156                        {
1157                                if(substr($url ,0,4) != 'http')
1158                                {
1159                                        $url = 'https://'.$GLOBALS['phpgw_info']['server']['hostname'].$url;
1160                                }
1161                                else
1162                                {
1163                                        $url = str_replace ( 'http:', 'https:', $url);
1164                                }
1165                        }
1166
1167                        /* Now we process the extravars into a proper url format */
1168                        /* if its not an array, then we turn it into one */
1169                        /* We do this to help prevent any duplicates from being sent. */
1170                        if (!is_array($extravars) && $extravars != '')
1171                        {
1172                                $new_extravars = Array();
1173
1174                                $a = explode('&', $extravars);
1175                                $i = 0;
1176                                while ($i < count($a))
1177                                {
1178                                        $b = split('=', $a[$i],2);
1179                                        // Check if this value doesn't already exist in new_extravars
1180                                        if(array_key_exists($b[0], $new_extravars))
1181                                        {
1182                                                // print "Debug::Error !!! " . $b[0] . " ($i) already exists<br>";
1183                                                if( eregi("\[\]", $b[0]) )
1184                                                {
1185                                                        $b[0] = eregi_replace("\[\]", "[$i]", $b[0]);
1186                                                }
1187                                        }
1188
1189                                        $new_extravars[$b[0]] = $b[1];
1190                                        $i++;
1191                                }
1192                                $extravars = $new_extravars;
1193                                unset($new_extravars);
1194                        }
1195
1196                        /* if using frames we make sure there is a framepart */
1197                        if(@defined('PHPGW_USE_FRAMES') && PHPGW_USE_FRAMES)
1198                        {
1199                                if (!isset($extravars['framepart']))
1200                                {
1201                                        $extravars['framepart']='body';
1202                                }
1203                        }
1204
1205                        /* add session params if not using cookies */
1206                        if (@!$GLOBALS['phpgw_info']['server']['usecookies'])
1207                        {
1208                                $extravars['sessionid'] = $this->sessionid;
1209                                $extravars['kp3'] = $this->kp3;
1210                                $extravars['domain'] = $this->account_domain;
1211                        }
1212
1213                        //used for repost prevention
1214//                      $extravars['click_history'] = $this->generate_click_history();
1215
1216                        /* if we end up with any extravars then we generate the url friendly string */
1217                        if (is_array($extravars))
1218                        {
1219                                $new_extravars = '';
1220                                foreach($extravars as $key => $value)
1221                                {
1222                                        if (!empty($new_extravars))
1223                                        {
1224                                                $new_extravars .= '&';
1225                                        }
1226                                        $new_extravars .= $key.'='.urlencode($value);
1227                                }
1228                                $url .= '?' . $new_extravars;
1229                        }
1230                        //echo " = '$url'</p>\n";
1231                        return $url;
1232                }
1233
1234                /**
1235                * The remaining methods are abstract - as they are unique for each session handler
1236                */
1237
1238                /**
1239                * Load user's session information
1240                *
1241                * The sessionid of the session to read is passed in the class-var $this->sessionid
1242                *
1243                * @return mixed the session data
1244                */
1245                function read_session()
1246                {}
1247
1248                /**
1249                * Remove stale sessions out of the database
1250                */
1251                function clean_sessions()
1252                {}
1253
1254                /**
1255                * Set paramaters for cookies - only implemented in PHP4 sessions
1256                *
1257                * @param string $domain domain name to use in cookie
1258                */
1259
1260                function set_cookie_params($domain)
1261                {}
1262
1263                /**
1264                * Create a new session id
1265                *
1266                * @return string a new session id
1267                */
1268                function new_session_id()
1269                {}
1270
1271                /**
1272                * Create a new session
1273                *
1274                * @param string $login user login
1275                * @param string $user_ip users ip address
1276                * @param int $now time now as a unix timestamp
1277                * @param string $session_flags A = Anonymous, N = Normal
1278                */
1279                function register_session($login,$user_ip,$now,$session_flags)
1280                {}
1281
1282                /**
1283                * Update the date last active info for the session, so the login does not expire
1284                *
1285                * @return bool did it suceed?
1286                */
1287                function update_dla()
1288                {}
1289
1290                /**
1291                * Terminate a session
1292                *
1293                * @param string $sessionid the id of the session to be terminated
1294                * @param string $kp3 - NOT SURE
1295                * @return bool did it suceed?
1296                */
1297                function destroy($sessionid, $kp3)
1298                {}
1299
1300                /**
1301                * Functions for appsession data and session cache
1302                */
1303       
1304                /**
1305                * Delete all data from the session cache for a user
1306                *
1307                * @param int $accountid user account id, defaults to current user (optional)
1308                */
1309                function delete_cache($accountid='')
1310                {}
1311
1312                /**
1313                * Stores or retrieves information from the sessions cache
1314                *
1315                * @param string $location identifier for data
1316                * @param string $appname name of app which is responsbile for the data
1317                * @param mixed $data data to be stored, if left blank data is retreived (optional)
1318                * @return mixed data from cache, only returned if $data arg is not used
1319                */
1320                function appsession($location = 'default', $appname = '', $data = '##NOTHING##')
1321                {}
1322
1323                /**
1324                * Get list of normal / non-anonymous sessions
1325                * Note: The data from the session-files get cached in the app_session phpgwapi/php4_session_cache
1326                *
1327                * @author ralfbecker
1328                * @param int $start session to start at
1329                * @param string $order field to sort on
1330                * @param string $sort sort order
1331                * @param bool $all_no_sort list all with out sorting (optional) default False
1332                * @return array info for all current sessions
1333                */
1334                function list_sessions($start,$order,$sort,$all_no_sort = False)
1335                {}
1336               
1337                /**
1338                * Get the number of normal / non-anonymous sessions
1339                *
1340                * @author ralfbecker
1341                * @return int number of sessions
1342                */
1343                function total()
1344                {}
1345        }
1346
1347        if(empty($GLOBALS['phpgw_info']['server']['sessions_type']))
1348        {
1349                $GLOBALS['phpgw_info']['server']['sessions_type'] = 'php4';     // the more performant default
1350        }
1351        // for php4 sessions, check if the extension is loaded, try loading it and fallback to db sessions if not
1352        if ($GLOBALS['phpgw_info']['server']['sessions_type'] == 'php4' && !extension_loaded('session'))
1353        {
1354                // some constanst for pre php4.3
1355                if (!defined('PHP_SHLIB_SUFFIX'))
1356                {
1357                        define('PHP_SHLIB_SUFFIX',strtoupper(substr(PHP_OS, 0,3)) == 'WIN' ? 'dll' : 'so');
1358                }
1359                if (!defined('PHP_SHLIB_PREFIX'))
1360                {
1361                        define('PHP_SHLIB_PREFIX',PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : '');
1362                }
1363                if (!function_exists('dl') || !@dl(PHP_SHLIB_PREFIX.'session'.'.'.PHP_SHLIB_SUFFIX))
1364                {
1365                        $GLOBALS['phpgw_info']['server']['sessions_type'] = 'db';       // fallback if we have no php4 sessions support
1366                }
1367        }
1368        include_once(PHPGW_API_INC.'/class.sessions_'.$GLOBALS['phpgw_info']['server']['sessions_type'].'.inc.php');
Note: See TracBrowser for help on using the repository browser.