source: trunk/phpgwapi/inc/class.sessions.inc.php @ 1354

Revision 1354, 42.7 KB checked in by rafaelraymundo, 15 years ago (diff)

Ticket #618 - Reativada a rotina de bloqueio de usuarios.

  • 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                        $this->account_id = $GLOBALS['phpgw']->accounts->name2id($this->account_lid);
294                        if (!$this->account_id)
295                        {
296                                return False;
297                        }
298
299                        $GLOBALS['phpgw_info']['user']['account_id'] = $this->account_id;
300
301                        /* init the crypto object before appsession call below */
302                        $this->key = md5($this->kp3 . $this->sessionid . @$GLOBALS['phpgw_info']['server']['encryptkey']);
303                        $this->iv  = $GLOBALS['phpgw_info']['server']['mcrypt_iv'];
304                        $GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
305
306                        $this->read_repositories(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info']);
307                       
308                        if ($this->user['expires'] != -1 && $this->user['expires'] < time())
309                        {
310                                if(is_object($GLOBALS['phpgw']->log))
311                                {
312                                        $GLOBALS['phpgw']->log->message(array(
313                                                'text' => 'W-VerifySession, account loginid %1 is expired',
314                                                'p1'   => $this->account_lid,
315                                                'line' => __LINE__,
316                                                'file' => __FILE__
317                                        ));
318                                        $GLOBALS['phpgw']->log->commit();
319                                }
320                                return False;
321                        }
322
323                        $GLOBALS['phpgw_info']['user']  = $this->user;
324                        $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
325
326                        $GLOBALS['phpgw_info']['user']['session_ip'] = $session['session_ip'];
327                        $GLOBALS['phpgw_info']['user']['passwd']     = base64_decode($this->appsession('password','phpgwapi'));
328
329                        if ($this->account_domain != $GLOBALS['phpgw_info']['user']['domain'])
330                        {
331                                if(is_object($GLOBALS['phpgw']->log))
332                                {
333                                        $GLOBALS['phpgw']->log->message(array(
334                                                'text' => 'W-VerifySession, the domains %1 and %2 don\'t match',
335                                                'p1'   => $userid_array[1],
336                                                'p2'   => $GLOBALS['phpgw_info']['user']['domain'],
337                                                'line' => __LINE__,
338                                                'file' => __FILE__
339                                        ));
340                                        $GLOBALS['phpgw']->log->commit();
341                                }
342                                return False;
343                        }
344
345                        /* ExpressoLivre
346                           Necessário comentar para que o redirecionamento de uma sessão
347                           https para uma http funcionasse atrás de um proxy
348                        */
349
350                        /*
351                        if (@$GLOBALS['phpgw_info']['server']['sessions_checkip'])
352                        {
353                                if((PHP_OS != 'Windows') && (PHP_OS != 'WINNT') &&
354                                        (!$GLOBALS['phpgw_info']['user']['session_ip'] || $GLOBALS['phpgw_info']['user']['session_ip'] != $this->getuser_ip())
355                                )
356                                {
357                                        //echo "IP do cliente com https   ---->  ".$GLOBALS['phpgw_info']['user']['session_ip'];
358                                        //echo "<BR>IP do cliente com http ---->  ".$this->getuser_ip();
359                                        if(is_object($GLOBALS['phpgw']->log))
360                                        {
361                                                // This needs some better wording
362                                                $GLOBALS['phpgw']->log->message(array(
363                                                        'text' => 'W-VerifySession, IP %1 doesn\'t match IP %2 in session table',
364                                                        'p1'   => $this->getuser_ip(),
365                                                        'p2'   => $GLOBALS['phpgw_info']['user']['session_ip'],
366                                                        'line' => __LINE__,
367                                                        'file' => __FILE__
368                                                ));
369                                                $GLOBALS['phpgw']->log->commit();
370                                        }
371                                        return False;
372                                }
373                        }
374                        */
375
376                        $GLOBALS['phpgw']->acl->acl($this->account_id);
377                        $GLOBALS['phpgw']->accounts->accounts($this->account_id);
378                        $GLOBALS['phpgw']->preferences->preferences($this->account_id);
379                        $GLOBALS['phpgw']->applications->applications($this->account_id);
380
381                        if (! $this->account_lid)
382                        {
383                                if(is_object($GLOBALS['phpgw']->log))
384                                {
385                                        // This needs some better wording
386                                        $GLOBALS['phpgw']->log->message(array(
387                                                'text' => 'W-VerifySession, account_id is empty',
388                                                'line' => __LINE__,
389                                                'file' => __FILE__
390                                        ));
391                                        $GLOBALS['phpgw']->log->commit();
392                                }
393                                //echo 'DEBUG: Sessions: account_id is empty!<br>'."\n";
394                                return False;
395                        }
396                        return True;
397                }
398
399                /**
400                * Functions for creating and verifying the session
401                */
402       
403                /**
404                * Get the ip address of current users
405                *
406                * @return string ip address
407                */
408                function getuser_ip()
409                {
410                /*
411                        if (getenv(HTTP_X_FORWARDED_FOR))
412                        {
413                                if (getenv(HTTP_CLIENT_IP))
414                                {
415                                        $ip=getenv(HTTP_CLIENT_IP);
416                                }
417                                else
418                                {
419                                        $ip=getenv(HTTP_X_FORWARDED_FOR);
420                                }
421                                $ip_proxy=getenv(REMOTE_ADDR);
422                        }
423                        else
424                        {
425                                $ip=getenv(REMOTE_ADDR);
426                        }
427                        return $ip;
428                */
429                        return (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
430                }
431
432                /**
433                * Set the domain used for cookies
434                *
435                * @return string domain
436                */
437                function phpgw_set_cookiedomain()
438                {
439                        // Use HTTP_X_FORWARDED_HOST if set, which is the case behind a none-transparent proxy
440                        $this->cookie_domain = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ?  $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
441
442                        // remove port from HTTP_HOST
443                        if (preg_match("/^(.*):(.*)$/",$this->cookie_domain,$arr))
444                        {
445                                $this->cookie_domain = $arr[1];
446                        }
447                        if (count(explode('.',$this->cookie_domain)) <= 1)
448                        {
449                                // setcookie dont likes domains without dots, leaving it empty, gets setcookie to fill the domain in
450                                $this->cookie_domain = '';
451                        }
452                        print_debug('COOKIE_DOMAIN',$this->cookie_domain,'api');
453
454                        $this->set_cookie_params($this->cookie_domain); // for php4 sessions necessary
455                }
456
457                /**
458                * Set a cookie
459                *
460                * @param string $cookiename name of cookie to be set
461                * @param string $cookievalue value to be used, if unset cookie is cleared (optional)
462                * @param int $cookietime when cookie should expire, 0 for session only (optional)
463                */
464                function phpgw_setcookie($cookiename,$cookievalue='',$cookietime=0)
465                {
466                        if (!$this->cookie_domain)
467                        {
468                                $this->phpgw_set_cookiedomain();
469                        }
470                        setcookie($cookiename,$cookievalue,$cookietime,'/',$this->cookie_domain,null,true);
471                }
472
473                /**
474                * Create a new session
475                *
476                * @param string $login user login
477                * @param string $passwd user password
478                * @param string $passwd_type type of password being used, ie plaintext, md5, sha1
479                * @return string session id
480                */
481                function create($login,$passwd = '',$passwd_type = '')
482                {
483                        if (is_array($login))
484                        {
485                                $this->login       = $login['login'];
486                                $this->passwd      = $login['passwd'];
487                                $this->passwd_type = $login['passwd_type'];
488                                $login             = $this->login;
489                        }
490                        else
491                        {
492                                $this->login       = $login;
493                                $this->passwd      = $passwd;
494                                $this->passwd_type = $passwd_type;
495                        }
496
497                        $this->clean_sessions();
498                        //sessions_::split_login_domain($login,$this->account_lid,$this->account_domain);
499                        // jakjr: allow uid with (@);
500                        $this->account_lid = $login;
501                        $this->account_domain = 'default';
502
503                        $now = time();
504
505                        //echo "<p>session::create(login='$login'): lid='$this->account_lid', domain='$this->account_domain'</p>\n";
506                        $user_ip = $this->getuser_ip();
507                               
508                        $this->account_id = $GLOBALS['phpgw']->accounts->name2id($this->account_lid);
509
510                        if (($blocked = $this->login_blocked($login,$user_ip)) ||       // too many unsuccessful attempts
511                                $GLOBALS['phpgw_info']['server']['global_denied_users'][$this->account_lid] ||
512                                !$GLOBALS['phpgw']->auth->authenticate($this->account_lid, $this->passwd, $this->passwd_type) ||
513                                $this->account_id && $GLOBALS['phpgw']->accounts->get_type($this->account_id) == 'g')
514                        {
515                                $this->reason = $blocked ? 'blocked, too many attempts' : 'bad login or password';
516                                $this->cd_reason = $blocked ? 99 : 5;
517
518                                $this->log_access($this->reason,$login,$user_ip,0);     // log unsuccessfull login
519                                return False;
520                        }
521                        // Só verifica tempo de inatividade do usuário, caso esteja configurado no Administrador.
522                        if(isset($GLOBALS['phpgw_info']['server']['time_to_account_expires']) &&
523                                $this->account_id !=null && $this->account_lid != "expresso-admin") {
524                                        $last_access = $this->get_last_access_on_history($this->account_id);
525                                        $this->read_repositories(False);
526                                        if ($last_access && ($last_access+($GLOBALS['phpgw_info']['server']['time_to_account_expires']*86400) < time()))
527                                        {
528                                                if(is_object($GLOBALS['phpgw']->log))
529                                                {
530                                                        $GLOBALS['phpgw']->log->message(array(
531                                                                'text' => 'W-LoginFailure, account loginid %1 is expired for innativity',
532                                                                'p1'   => $this->account_lid,
533                                                                'line' => __LINE__,
534                                                                'file' => __FILE__
535                                                        ));
536                                                        $GLOBALS['phpgw']->log->commit();
537                                                }
538                                                $this->reason = 'account is expired';
539                                                $this->cd_reason = 98;
540               
541                                                return False;
542                                        }
543                        }
544
545                        /* jakjr: Expresso does not use auto-create account.
546                        if (!$this->account_id && $GLOBALS['phpgw_info']['server']['auto_create_acct'] == True)
547                        {
548                                $this->account_id = $GLOBALS['phpgw']->accounts->auto_add($this->account_lid, $passwd);
549                        }
550                        */
551
552                        $GLOBALS['phpgw_info']['user']['account_id'] = $this->account_id;
553                        $GLOBALS['phpgw']->accounts->accounts($this->account_id);
554                        $this->sessionid = $this->new_session_id();
555                        $this->kp3       = md5($GLOBALS['phpgw']->common->randomstring(15));
556
557                        if ($GLOBALS['phpgw_info']['server']['usecookies'])
558                        {
559                                $this->phpgw_setcookie('sessionid',$this->sessionid);
560                                $this->phpgw_setcookie('kp3',$this->kp3);
561                                $this->phpgw_setcookie('domain',$this->account_domain);
562                        }
563                        if ($GLOBALS['phpgw_info']['server']['usecookies'] || isset($_COOKIE['last_loginid']))
564                        {
565                                $this->phpgw_setcookie('last_loginid', $this->account_lid ,$now+1209600); /* For 2 weeks */
566                                $this->phpgw_setcookie('last_domain',$this->account_domain,$now+1209600);
567                                $this->phpgw_setcookie('last_organization',$_POST['organization'],$now+1209600);
568                        }
569                        unset($GLOBALS['phpgw_info']['server']['default_domain']); /* we kill this for security reasons */
570
571                        /* init the crypto object */
572                        $this->key = md5($this->kp3 . $this->sessionid . $GLOBALS['phpgw_info']['server']['encryptkey']);
573                        $this->iv  = $GLOBALS['phpgw_info']['server']['mcrypt_iv'];
574                        $GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
575
576                        $this->read_repositories(False);
577                        if ($this->user['expires'] != -1 && $this->user['expires'] < time())
578                        {
579                                if(is_object($GLOBALS['phpgw']->log))
580                                {
581                                        $GLOBALS['phpgw']->log->message(array(
582                                                'text' => 'W-LoginFailure, account loginid %1 is expired',
583                                                'p1'   => $this->account_lid,
584                                                'line' => __LINE__,
585                                                'file' => __FILE__
586                                        ));
587                                        $GLOBALS['phpgw']->log->commit();
588                                }
589                                $this->reason = 'account is expired';
590                                $this->cd_reason = 98;
591
592                                return False;
593                        }
594
595                        $GLOBALS['phpgw_info']['user']  = $this->user;
596                        $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
597
598                        $this->appsession('password','phpgwapi',base64_encode($this->passwd));
599                        if ($GLOBALS['phpgw']->acl->check('anonymous',1,'phpgwapi'))
600                        {
601                                $session_flags = 'A';
602                        }
603                        else
604                        {
605                                $session_flags = 'N';
606                        }
607
608                        $GLOBALS['phpgw']->db->transaction_begin();
609                        $this->register_session($login,$user_ip,$now,$session_flags);
610                        if ($session_flags != 'A')              // dont log anonymous sessions
611                        {
612                                $this->log_access($this->sessionid,$login,$user_ip,$this->account_id);
613                        }
614                        $this->appsession('account_previous_login','phpgwapi',$GLOBALS['phpgw']->auth->previous_login);
615                        // Expresso
616                        //$GLOBALS['phpgw']->auth->update_lastlogin($this->account_id,$user_ip);
617                        $GLOBALS['phpgw']->db->transaction_commit();
618
619                        //if (!$this->sessionid) echo "<p>session::create(login='$login') = '$this->sessionid': lid='$this->account_lid', domain='$this->account_domain'</p>\n";
620
621                        return $this->sessionid;
622                }
623
624                /**
625                 * Retorna o UNIX DATE do ultimo acesso dessa conta, baseado na tabela de histórico.
626                 */
627                function get_last_access_on_history($account_id) {
628                        $GLOBALS['phpgw']->db->query("select li from phpgw_access_log where account_id='$account_id' order by li desc limit 1",__LINE__,__FILE__);
629                        if(!$GLOBALS['phpgw']->db->next_record())
630                                return false;
631                        return $GLOBALS['phpgw']->db->f('li');
632                }
633
634                /**
635        * Write or update (for logout) the access_log
636                *
637                * @param string $sessionid id of session or 0 for unsuccessful logins
638                * @param string $login account_lid (evtl. with domain) or '' for settion the logout-time
639                * @param string $user_ip ip to log
640                * @param int $account_id numerical account_id
641                */
642                function log_access($sessionid,$login='',$user_ip='',$account_id='')
643                {
644                        $now = time();
645
646                        if ($login != '')
647                        {
648                                if (strlen($login) > 30)
649                                {
650                                        $login = substr($login,0,30);
651                                }
652                                $GLOBALS['phpgw']->db->query('INSERT INTO phpgw_access_log(sessionid,loginid,ip,li,lo,account_id,browser)'
653                                        . " VALUES ('" . $sessionid . "','" . $this->db->db_addslashes($login). "','"
654                                        . $this->db->db_addslashes($user_ip) . "',$now,0," . (int)$account_id .",'".$this->db->db_addslashes($_SERVER[ 'HTTP_USER_AGENT' ])."')",__LINE__,__FILE__);
655                        }
656                        else if($sessionid != 'bad login or password')
657                        {
658                                $GLOBALS['phpgw']->db->query("UPDATE phpgw_access_log SET lo=" . $now . " WHERE sessionid='"
659                                        . $sessionid . "'",__LINE__,__FILE__);
660                        }
661
662                        /* jakjr: Clean phpgw_access_log with a crontab event.
663                        if ($GLOBALS['phpgw_info']['server']['max_access_log_age'])
664                        {
665                                $max_age = $now - $GLOBALS['phpgw_info']['server']['max_access_log_age'] * 24 * 60 * 60;
666
667                                $GLOBALS['phpgw']->db->query("DELETE FROM phpgw_access_log WHERE li < $max_age");
668                        }
669                        */
670                }
671
672                /**
673                * Protect against brute force attacks, block login if too many unsuccessful login attmepts
674        *
675                * @param string $login account_lid (evtl. with domain)
676                * @param string $ip ip of the user
677                * @returns bool login blocked?
678                */
679                function login_blocked($login,$ip)
680                {
681                        /*jakjr: Disable this protection. When block an proxy server ip, all the sub-network will be blocking.*/
682                        //return false;
683
684                        $blocked = False;
685                        $block_time = time() - $GLOBALS['phpgw_info']['server']['block_time'] * 60;
686                        /*
687                        $ip = $this->db->db_addslashes($ip);
688                        $this->db->query("SELECT count(*) FROM phpgw_access_log WHERE account_id=0 AND ip='$ip' AND li > $block_time",__LINE__,__FILE__);
689                        $this->db->next_record();
690                        if (($false_ip = $this->db->f(0)) > $GLOBALS['phpgw_info']['server']['num_unsuccessful_ip'])
691                        {
692                                //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";
693                                $blocked = True;
694                        }
695                        */
696                        $login = $this->db->db_addslashes($login);
697                        $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__);
698                        $this->db->next_record();
699                        if (($false_id = $this->db->f(0)) > $GLOBALS['phpgw_info']['server']['num_unsuccessful_id'])
700                        {
701                                //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";
702                                $blocked = True;
703                        }
704                        if ($blocked && $GLOBALS['phpgw_info']['server']['admin_mails'] &&
705                                // max. one mail each 5mins
706                                $GLOBALS['phpgw_info']['server']['login_blocked_mail_time'] < time()-5*60)
707                        {
708                                // notify admin(s) via email
709                                $from    = 'eGroupWare@'.$GLOBALS['phpgw_info']['server']['mail_suffix'];
710                                $subject = lang("eGroupWare: login blocked for user '%1', IP %2",$login,$ip);
711                                $body    = lang("Too many unsucessful attempts to login: %1 for the user '%2', %3 for the IP %4",$false_id,$login,$false_ip,$ip);
712                               
713                                if(!is_object($GLOBALS['phpgw']->send))
714                                {
715                                        $GLOBALS['phpgw']->send = CreateObject('phpgwapi.send');
716                                }
717                                $subject = $GLOBALS['phpgw']->send->encode_subject($subject);
718                                $admin_mails = explode(',',$GLOBALS['phpgw_info']['server']['admin_mails']);
719                                foreach($admin_mails as $to)
720                                {
721                                        $GLOBALS['phpgw']->send->msg('email',$to,$subject,$body,'','','',$from,$from);
722                                }
723                                // save time of mail, to not send to many mails
724                                $config = CreateObject('phpgwapi.config','phpgwapi');
725                                $config->read_repository();
726                                $config->value('login_blocked_mail_time',time());
727                                $config->save_repository();
728                        }
729                        return $blocked;
730                }
731
732                /**
733                * Verfy a peer server access request
734                *
735                * @param string $sessionid session id to verfiy
736                * @param string $kp3 ??
737                * @return bool verfied?
738                */
739                function verify_server($sessionid, $kp3)
740                {
741                        $GLOBALS['phpgw']->interserver = CreateObject('phpgwapi.interserver');
742                        $this->sessionid = $sessionid;
743                        $this->kp3       = $kp3;
744
745                        $session = $this->read_session();
746                        $this->session_flags = $session['session_flags'];
747
748                        list($this->account_lid,$this->account_domain) = explode('@', $session['session_lid']);
749                       
750                        if ($this->account_domain == '')
751                        {
752                                $this->account_domain = $GLOBALS['phpgw_info']['server']['default_domain'];
753                        }
754
755                        $GLOBALS['phpgw_info']['user']['kp3'] = $this->kp3;
756                        $phpgw_info_flags = $GLOBALS['phpgw_info']['flags'];
757
758                        $GLOBALS['phpgw_info']['flags'] = $phpgw_info_flags;
759
760                        $this->update_dla();
761                        $this->account_id = $GLOBALS['phpgw']->interserver->name2id($this->account_lid);
762
763                        if (!$this->account_id)
764                        {
765                                return False;
766                        }
767
768                        $GLOBALS['phpgw_info']['user']['account_id'] = $this->account_id;
769
770                        $this->read_repositories(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info']);
771
772                        /* init the crypto object before appsession call below */
773                        $this->key = md5($this->kp3 . $this->sessionid . $GLOBALS['phpgw_info']['server']['encryptkey']);
774                        $this->iv  = $GLOBALS['phpgw_info']['server']['mcrypt_iv'];
775                        $GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
776
777                        $GLOBALS['phpgw_info']['user']  = $this->user;
778                        $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
779
780                        $GLOBALS['phpgw_info']['user']['session_ip'] = $session['session_ip'];
781                        $GLOBALS['phpgw_info']['user']['passwd'] = base64_decode($this->appsession('password','phpgwapi'));
782
783                        if ($userid_array[1] != $GLOBALS['phpgw_info']['user']['domain'])
784                        {
785                                if(is_object($GLOBALS['phpgw']->log))
786                                {
787                                        $GLOBALS['phpgw']->log->message(array(
788                                                'text' => 'W-VerifySession, the domains %1 and %2 don\t match',
789                                                'p1'   => $userid_array[1],
790                                                'p2'   => $GLOBALS['phpgw_info']['user']['domain'],
791                                                'line' => __LINE__,
792                                                'file' => __FILE__
793                                        ));
794                                        $GLOBALS['phpgw']->log->commit();
795                                }
796
797                                if(is_object($GLOBALS['phpgw']->crypto))
798                                {
799                                        $GLOBALS['phpgw']->crypto->cleanup();
800                                        unset($GLOBALS['phpgw']->crypto);
801                                }
802                                return False;
803                        }
804
805                        if(@$GLOBALS['phpgw_info']['server']['sessions_checkip'])
806                        {
807                                if((PHP_OS != 'Windows') && (PHP_OS != 'WINNT') &&
808                                        (!$GLOBALS['phpgw_info']['user']['session_ip'] || $GLOBALS['phpgw_info']['user']['session_ip'] != $this->getuser_ip())
809                                )
810                                {
811                                        if(is_object($GLOBALS['phpgw']->log))
812                                        {
813                                                // This needs some better wording
814                                                $GLOBALS['phpgw']->log->message(array(
815                                                        'text' => 'W-VerifySession, IP %1 doesn\'t match IP %2 in session table',
816                                                        'p1'   => $this->getuser_ip(),
817                                                        'p2'   => $GLOBALS['phpgw_info']['user']['session_ip'],
818                                                        'line' => __LINE__,
819                                                        'file' => __FILE__
820                                                ));
821                                                $GLOBALS['phpgw']->log->commit();
822                                        }
823
824                                        if(is_object($GLOBALS['phpgw']->crypto))
825                                        {
826                                                $GLOBALS['phpgw']->crypto->cleanup();
827                                                unset($GLOBALS['phpgw']->crypto);
828                                        }
829                                        return False;
830                                }
831                        }
832
833                        $GLOBALS['phpgw']->acl->acl($this->account_id);
834                        $GLOBALS['phpgw']->accounts->accounts($this->account_id);
835                        $GLOBALS['phpgw']->preferences->preferences($this->account_id);
836                        $GLOBALS['phpgw']->applications->applications($this->account_id);
837
838                        if (! $this->account_lid)
839                        {
840                                if(is_object($GLOBALS['phpgw']->log))
841                                {
842                                        // This needs some better wording
843                                        $GLOBALS['phpgw']->log->message(array(
844                                                'text' => 'W-VerifySession, account_id is empty',
845                                                'line' => __LINE__,
846                                                'file' => __FILE__
847                                        ));
848                                        $GLOBALS['phpgw']->log->commit();
849                                }
850
851                                if(is_object($GLOBALS['phpgw']->crypto))
852                                {
853                                        $GLOBALS['phpgw']->crypto->cleanup();
854                                        unset($GLOBALS['phpgw']->crypto);
855                                }
856                                return False;
857                        }
858                        else
859                        {
860                                return True;
861                        }
862                }
863
864                /**
865                * Validate a peer server login request
866                *
867                * @param string $login login name
868                * @param string $password password
869                * @return bool login ok?
870                */
871                function create_server($login,$passwd)
872                {
873                        $GLOBALS['phpgw']->interserver = CreateObject('phpgwapi.interserver');
874                        $this->login  = $login;
875                        $this->passwd = $passwd;
876                        $this->clean_sessions();
877                        $login_array = explode('@', $login);
878                        $this->account_lid = $login_array[0];
879                        $now = time();
880
881                        if ($login_array[1] != '')
882                        {
883                                $this->account_domain = $login_array[1];
884                        }
885                        else
886                        {
887                                $this->account_domain = $GLOBALS['phpgw_info']['server']['default_domain'];
888                        }
889
890                        $serverdata = array(
891                                'server_name' => $this->account_domain,
892                                'username'    => $this->account_lid,
893                                'password'    => $passwd
894                        );
895                        if (!$GLOBALS['phpgw']->interserver->auth($serverdata))
896                        {
897                                return False;
898                                exit;
899                        }
900
901                        if (!$GLOBALS['phpgw']->interserver->exists($this->account_lid))
902                        {
903                                $this->account_id = $GLOBALS['phpgw']->interserver->name2id($this->account_lid);
904                        }
905                        $GLOBALS['phpgw_info']['user']['account_id'] = $this->account_id;
906                        $GLOBALS['phpgw']->interserver->serverid = $this->account_id;
907
908                        $this->sessionid = md5($GLOBALS['phpgw']->common->randomstring(10));
909                        $this->kp3       = md5($GLOBALS['phpgw']->common->randomstring(15));
910
911                        /* re-init the crypto object */
912                        $this->key = md5($this->kp3 . $this->sessionid . $GLOBALS['phpgw_info']['server']['encryptkey']);
913                        $this->iv  = $GLOBALS['phpgw_info']['server']['mcrypt_iv'];
914                        $GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
915
916                        //$this->read_repositories(False);
917
918                        $GLOBALS['phpgw_info']['user']  = $this->user;
919                        $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
920
921                        $this->appsession('password','phpgwapi',base64_encode($this->passwd));
922                        $session_flags = 'S';
923
924                        $user_ip = $this->getuser_ip();
925
926                        $GLOBALS['phpgw']->db->transaction_begin();
927                        $this->register_session($login,$user_ip,$now,$session_flags);
928
929                        $this->log_access($this->sessionid,$login,$user_ip,$this->account_id);
930
931                        $this->appsession('account_previous_login','phpgwapi',$GLOBALS['phpgw']->auth->previous_login);
932                        $GLOBALS['phpgw']->auth->update_lastlogin($this->account_id,$user_ip);
933                        $GLOBALS['phpgw']->db->transaction_commit();
934
935                        return array($this->sessionid,$this->kp3);
936                }
937
938                /**
939                * Functions for appsession data and session cache
940                */
941
942                /**
943                * Is this also useless?? (skwashd)
944                */
945                function read_repositories($cached='',$write_cache=True)
946                {
947                        $GLOBALS['phpgw']->acl->acl($this->account_id);
948                        $GLOBALS['phpgw']->accounts->accounts($this->account_id);
949                        $GLOBALS['phpgw']->preferences->preferences($this->account_id);
950                        $GLOBALS['phpgw']->applications->applications($this->account_id);
951
952                        if(@$cached)
953                        {
954                                $this->user = $this->appsession('phpgw_info_cache','phpgwapi');
955                                if(!empty($this->user))
956                                {
957                                        $GLOBALS['phpgw']->preferences->data = $this->user['preferences'];
958                                        if (!isset($GLOBALS['phpgw_info']['apps']) || !is_array($GLOBALS['phpgw_info']['apps']))
959                                        {
960                                                $GLOBALS['phpgw']->applications->read_installed_apps();
961                                        }
962                                }
963                                else
964                                {
965                                        $this->setup_cache($write_cache);
966                                }
967                        }
968                        else
969                        {
970                                $this->setup_cache($write_cache);
971                        }
972                        $this->hooks = $GLOBALS['phpgw']->hooks->read();
973                }
974
975                /**
976                * Is this also useless?? (skwashd)
977                */
978                function setup_cache($write_cache=True)
979                {
980                        $this->user                = $GLOBALS['phpgw']->accounts->read_repository();
981                        $this->user['acl']         = $GLOBALS['phpgw']->acl->read_repository();
982                        $this->user['preferences'] = $GLOBALS['phpgw']->preferences->read_repository();
983                        $this->user['apps']        = $GLOBALS['phpgw']->applications->read_repository();
984                        //@reset($this->data['user']['apps']);
985
986                        $this->user['domain']      = $this->account_domain;
987                        $this->user['sessionid']   = $this->sessionid;
988                        $this->user['kp3']         = $this->kp3;
989                        $this->user['session_ip']  = $this->getuser_ip();
990                        $this->user['session_lid'] = $this->account_lid.'@'.$this->account_domain;
991                        $this->user['account_id']  = $this->account_id;
992                        $this->user['account_lid'] = $this->account_lid;
993                        $this->user['userid']      = $this->account_lid;
994                        $this->user['passwd']      = @$this->passwd;
995                        if(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info'] && $write_cache)
996                        {
997                                $this->delete_cache();
998                                $this->appsession('phpgw_info_cache','phpgwapi',$this->user);
999                        }
1000                }
1001       
1002                /**
1003                * This looks to be useless
1004                * This will capture everything in the $GLOBALS['phpgw_info'] including server info,
1005                * and store it in appsessions.  This is really incompatible with any type of restoring
1006                * from appsession as the saved user info is really in ['user'] rather than the root of
1007                * the structure, which is what this class likes.
1008                */
1009                function save_repositories()
1010                {
1011                        $phpgw_info_temp = $GLOBALS['phpgw_info'];
1012                        $phpgw_info_temp['user']['kp3'] = '';
1013                        $phpgw_info_temp['flags'] = array();
1014
1015                        if ($GLOBALS['phpgw_info']['server']['cache_phpgw_info'])
1016                        {
1017                                $this->appsession('phpgw_info_cache','phpgwapi',$phpgw_info_temp);
1018                        }
1019                }
1020
1021                function restore()
1022                {
1023                        $sessionData = $this->appsession('sessiondata');
1024
1025                        if (!empty($sessionData) && is_array($sessionData))
1026                        {
1027                                foreach($sessionData as $key => $value)
1028                                {
1029                                        global $$key;
1030                                        $$key = $value;
1031                                        $this->variableNames[$key] = 'registered';
1032                                        // echo 'restored: '.$key.', ' . $value . '<br>';
1033                                }
1034                        }
1035                }
1036
1037                /**
1038                * Save the current values of all registered variables
1039                */
1040                function save()
1041                {
1042                        if (is_array($this->variableNames))
1043                        {
1044                                reset($this->variableNames);
1045                                while(list($key, $value) = each($this->variableNames))
1046                                {
1047                                        if ($value == 'registered')
1048                                        {
1049                                                global $$key;
1050                                                $sessionData[$key] = $$key;
1051                                        }
1052                                }
1053                                $this->appsession('sessiondata','',$sessionData);
1054                        }
1055                }
1056
1057                /**
1058                * Create a list a variable names, which data needs to be restored
1059                *
1060                * @param string $_variableName name of variable to be registered
1061                */
1062                function register($_variableName)
1063                {
1064                        $this->variableNames[$_variableName]='registered';
1065                        #print 'registered '.$_variableName.'<br>';
1066                }
1067
1068                /**
1069                * Mark variable as unregistered
1070                *
1071                * @param string $_variableName name of variable to deregister
1072                */
1073                function unregister($_variableName)
1074                {
1075                        $this->variableNames[$_variableName]='unregistered';
1076                        #print 'unregistered '.$_variableName.'<br>';
1077                }
1078
1079                /**
1080                * Check if we have a variable registred already
1081                *
1082                * @param string $_variableName name of variable to check
1083                * @return bool was the variable found?
1084                */
1085                function is_registered($_variableName)
1086                {
1087                        if ($this->variableNames[$_variableName] == 'registered')
1088                        {
1089                                return True;
1090                        }
1091                        else
1092                        {
1093                                return False;
1094                        }
1095                }
1096                /**
1097                * Additional tracking of user actions - prevents reposts/use of back button
1098                *
1099                * @author skwashd
1100                * @return string current history id
1101                */
1102                function generate_click_history()
1103                {
1104                        if(!isset($this->history_id))
1105                        {
1106                                $this->history_id = md5($this->login . time());
1107                                $history = $this->appsession($location = 'history', $appname = 'phpgwapi');
1108                               
1109                                if(count($history) >= $GLOBALS['phpgw_info']['server']['max_history'])
1110                                {
1111                                        array_shift($history);
1112                                        $this->appsession($location = 'history', $appname = 'phpgwapi', $history);
1113                                }
1114                        }
1115                        return $this->history_id;
1116                }
1117               
1118                /**
1119                * Detects if the page has already been called before - good for forms
1120                *
1121                * @author skwashd
1122                * @param bool $diplay_error when implemented will use the generic error handling code
1123                * @return True if called previously, else False - call ok
1124                */
1125                function is_repost($display_error = False)
1126                {
1127                        $history = $this->appsession($location = 'history', $appname = 'phpgwapi');
1128                        if(isset($history[$_GET['click_history']]))
1129                        {
1130                                if($display_error)
1131                                {
1132                                        $GLOBALS['phpgw']->redirect_link('/error.php', 'type=repost');//more on this later :)
1133                                }
1134                                else
1135                                {
1136                                        return True; //handled by the app
1137                                }
1138                        }
1139                        else
1140                        {
1141                                $history[$_GET['click_history']] = True;
1142                                $this->appsession($location = 'history', $appname = 'phpgwapi', $history);
1143                                return False;
1144                        }
1145                }
1146
1147                /**
1148                * Generate a url which supports url or cookies based sessions
1149                *
1150                * @param string $url a url relative to the egroupware install root
1151                * @param array $extravars query string arguements
1152                * @return string generated url
1153                */
1154                function link($url, $extravars = '')
1155                {
1156                        //echo "<p>session::link(url='".print_r($url,True)."',extravars='".print_r($extravars,True)."')";
1157                        /* first we process the $url to build the full scriptname */
1158                        $full_scriptname = True;
1159
1160                        $url_firstchar = substr($url ,0,1);
1161                        if ($url_firstchar == '/' && $GLOBALS['phpgw_info']['server']['webserver_url'] == '/')
1162                        {
1163                                $full_scriptname = False;
1164                        }
1165
1166                        if ($url_firstchar != '/')
1167                        {
1168                                $app = $GLOBALS['phpgw_info']['flags']['currentapp'];
1169                                if ($app != 'home' && $app != 'login' && $app != 'logout')
1170                                {
1171                                        $url = $app.'/'.$url;
1172                                }
1173                        }
1174
1175                        if($full_scriptname)
1176                        {
1177                                $webserver_url_count = strlen($GLOBALS['phpgw_info']['server']['webserver_url'])-1;
1178                                if(substr($GLOBALS['phpgw_info']['server']['webserver_url'] ,$webserver_url_count,1) != '/' && $url_firstchar != '/')
1179                                {
1180                                        $url = $GLOBALS['phpgw_info']['server']['webserver_url'] .'/'. $url;
1181                                }
1182                                else
1183                                {
1184                                        $url = $GLOBALS['phpgw_info']['server']['webserver_url'] . $url;
1185                                }
1186                        }
1187
1188                        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
1189                        {
1190                                if(substr($url ,0,4) != 'http')
1191                                {
1192                                        $url = 'https://'.$GLOBALS['phpgw_info']['server']['hostname'].$url;
1193                                }
1194                                else
1195                                {
1196                                        $url = str_replace ( 'http:', 'https:', $url);
1197                                }
1198                        }
1199
1200                        /* Now we process the extravars into a proper url format */
1201                        /* if its not an array, then we turn it into one */
1202                        /* We do this to help prevent any duplicates from being sent. */
1203                        if (!is_array($extravars) && $extravars != '')
1204                        {
1205                                $new_extravars = Array();
1206
1207                                $a = explode('&', $extravars);
1208                                $i = 0;
1209                                while ($i < count($a))
1210                                {
1211                                        $b = split('=', $a[$i],2);
1212                                        // Check if this value doesn't already exist in new_extravars
1213                                        if(array_key_exists($b[0], $new_extravars))
1214                                        {
1215                                                // print "Debug::Error !!! " . $b[0] . " ($i) already exists<br>";
1216                                                if( eregi("\[\]", $b[0]) )
1217                                                {
1218                                                        $b[0] = eregi_replace("\[\]", "[$i]", $b[0]);
1219                                                }
1220                                        }
1221
1222                                        $new_extravars[$b[0]] = $b[1];
1223                                        $i++;
1224                                }
1225                                $extravars = $new_extravars;
1226                                unset($new_extravars);
1227                        }
1228
1229                        /* if using frames we make sure there is a framepart */
1230                        if(@defined('PHPGW_USE_FRAMES') && PHPGW_USE_FRAMES)
1231                        {
1232                                if (!isset($extravars['framepart']))
1233                                {
1234                                        $extravars['framepart']='body';
1235                                }
1236                        }
1237
1238                        /* add session params if not using cookies */
1239                        if (@!$GLOBALS['phpgw_info']['server']['usecookies'])
1240                        {
1241                                $extravars['sessionid'] = $this->sessionid;
1242                                $extravars['kp3'] = $this->kp3;
1243                                $extravars['domain'] = $this->account_domain;
1244                        }
1245
1246                        //used for repost prevention
1247//                      $extravars['click_history'] = $this->generate_click_history();
1248
1249                        /* if we end up with any extravars then we generate the url friendly string */
1250                        if (is_array($extravars))
1251                        {
1252                                $new_extravars = '';
1253                                foreach($extravars as $key => $value)
1254                                {
1255                                        if (!empty($new_extravars))
1256                                        {
1257                                                $new_extravars .= '&';
1258                                        }
1259                                        $new_extravars .= $key.'='.urlencode($value);
1260                                }
1261                                $url .= '?' . $new_extravars;
1262                        }
1263                        //echo " = '$url'</p>\n";
1264                        return $url;
1265                }
1266
1267                /**
1268                * The remaining methods are abstract - as they are unique for each session handler
1269                */
1270
1271                /**
1272                * Load user's session information
1273                *
1274                * The sessionid of the session to read is passed in the class-var $this->sessionid
1275                *
1276                * @return mixed the session data
1277                */
1278                function read_session()
1279                {}
1280
1281                /**
1282                * Remove stale sessions out of the database
1283                */
1284                function clean_sessions()
1285                {}
1286
1287                /**
1288                * Set paramaters for cookies - only implemented in PHP4 sessions
1289                *
1290                * @param string $domain domain name to use in cookie
1291                */
1292
1293                function set_cookie_params($domain)
1294                {}
1295
1296                /**
1297                * Create a new session id
1298                *
1299                * @return string a new session id
1300                */
1301                function new_session_id()
1302                {}
1303
1304                /**
1305                * Create a new session
1306                *
1307                * @param string $login user login
1308                * @param string $user_ip users ip address
1309                * @param int $now time now as a unix timestamp
1310                * @param string $session_flags A = Anonymous, N = Normal
1311                */
1312                function register_session($login,$user_ip,$now,$session_flags)
1313                {}
1314
1315                /**
1316                * Update the date last active info for the session, so the login does not expire
1317                *
1318                * @return bool did it suceed?
1319                */
1320                function update_dla()
1321                {}
1322
1323                /**
1324                * Terminate a session
1325                *
1326                * @param string $sessionid the id of the session to be terminated
1327                * @param string $kp3 - NOT SURE
1328                * @return bool did it suceed?
1329                */
1330                function destroy($sessionid, $kp3)
1331                {}
1332
1333                /**
1334                * Functions for appsession data and session cache
1335                */
1336       
1337                /**
1338                * Delete all data from the session cache for a user
1339                *
1340                * @param int $accountid user account id, defaults to current user (optional)
1341                */
1342                function delete_cache($accountid='')
1343                {}
1344
1345                /**
1346                * Stores or retrieves information from the sessions cache
1347                *
1348                * @param string $location identifier for data
1349                * @param string $appname name of app which is responsbile for the data
1350                * @param mixed $data data to be stored, if left blank data is retreived (optional)
1351                * @return mixed data from cache, only returned if $data arg is not used
1352                */
1353                function appsession($location = 'default', $appname = '', $data = '##NOTHING##')
1354                {}
1355
1356                /**
1357                * Get list of normal / non-anonymous sessions
1358                * Note: The data from the session-files get cached in the app_session phpgwapi/php4_session_cache
1359                *
1360                * @author ralfbecker
1361                * @param int $start session to start at
1362                * @param string $order field to sort on
1363                * @param string $sort sort order
1364                * @param bool $all_no_sort list all with out sorting (optional) default False
1365                * @return array info for all current sessions
1366                */
1367                function list_sessions($start,$order,$sort,$all_no_sort = False)
1368                {}
1369               
1370                /**
1371                * Get the number of normal / non-anonymous sessions
1372                *
1373                * @author ralfbecker
1374                * @return int number of sessions
1375                */
1376                function total()
1377                {}
1378        }
1379
1380        if(empty($GLOBALS['phpgw_info']['server']['sessions_type']))
1381        {
1382                $GLOBALS['phpgw_info']['server']['sessions_type'] = 'php4';     // the more performant default
1383        }
1384        // for php4 sessions, check if the extension is loaded, try loading it and fallback to db sessions if not
1385        if ($GLOBALS['phpgw_info']['server']['sessions_type'] == 'php4' && !extension_loaded('session'))
1386        {
1387                // some constanst for pre php4.3
1388                if (!defined('PHP_SHLIB_SUFFIX'))
1389                {
1390                        define('PHP_SHLIB_SUFFIX',strtoupper(substr(PHP_OS, 0,3)) == 'WIN' ? 'dll' : 'so');
1391                }
1392                if (!defined('PHP_SHLIB_PREFIX'))
1393                {
1394                        define('PHP_SHLIB_PREFIX',PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : '');
1395                }
1396                if (!function_exists('dl') || !@dl(PHP_SHLIB_PREFIX.'session'.'.'.PHP_SHLIB_SUFFIX))
1397                {
1398                        $GLOBALS['phpgw_info']['server']['sessions_type'] = 'db';       // fallback if we have no php4 sessions support
1399                }
1400        }
1401        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.