source: companies/serpro/phpgwapi/inc/class.sessions.inc.php @ 903

Revision 903, 43.5 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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