source: branches/2.3/phpgwapi/inc/class.sessions.inc.php @ 4867

Revision 4867, 42.8 KB checked in by rafaelraymundo, 13 years ago (diff)

Ticket #2169 - Disponibilizar certificado na sessão do usuário

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