source: trunk/phpgwapi/inc/class.setup.inc.php @ 1322

Revision 1322, 28.5 KB checked in by niltonneto, 15 years ago (diff)

Ticket #463 - Retirado menor octeto da verificação de dependência de versões.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare - Setup                                                       *
4  * http://www.egroupware.org                                                *
5  * --------------------------------------------                             *
6  * This file written by Joseph Engo<jengo@phpgroupware.org>                 *
7  *  and Dan Kuykendall<seek3r@phpgroupware.org>                             *
8  *  and Mark Peters<skeeter@phpgroupware.org>                               *
9  *  and Miles Lott<milosch@groupwhere.org>                                  *
10  * --------------------------------------------                             *
11  *  This program is free software; you can redistribute it and/or modify it *
12  *  under the terms of the GNU General Public License as published by the   *
13  *  Free Software Foundation; either version 2 of the License, or (at your  *
14  *  option) any later version.                                              *
15  \**************************************************************************/
16
17
18        class setup
19        {
20                var $db;
21                var $oProc;
22
23                var $detection = '';
24                var $process = '';
25                var $lang = '';
26                var $html = '';
27                var $appreg = '';
28
29                /* table name vars */
30                var $tbl_apps;
31                var $tbl_config;
32                var $tbl_hooks;
33
34                function setup($html=False, $translation=False)
35                {
36                        $this->detection = CreateObject('phpgwapi.setup_detection');
37                        $this->process   = CreateObject('phpgwapi.setup_process');
38                        $this->appreg    = CreateObject('phpgwapi.app_registry');
39
40                        /* The setup application needs these */
41                        $this->html = $html ? CreateObject('phpgwapi.setup_html') : '';
42                        $this->translation = $translation ? CreateObject('phpgwapi.setup_translation') : '';
43
44//                      $this->tbl_apps    = $this->get_apps_table_name();
45//                      $this->tbl_config  = $this->get_config_table_name();
46                        $this->tbl_hooks   = $this->get_hooks_table_name();
47                }
48
49                /*!
50                @function loaddb
51                @abstract include api db class for the ConfigDomain and connect to the db
52                */
53                function loaddb()
54                {
55                        if(!isset($this->ConfigDomain) || empty($this->ConfigDomain))
56                        {
57                                $this->ConfigDomain = get_var('ConfigDomain',array('COOKIE','POST'),$_POST['FormDomain']);
58                        }
59
60                        $GLOBALS['phpgw_info']['server']['db_type'] = $GLOBALS['phpgw_domain'][$this->ConfigDomain]['db_type'];
61
62                        if ($GLOBALS['phpgw_info']['server']['db_type'] == 'pgsql')
63                        {
64                                $GLOBALS['phpgw_info']['server']['db_persistent'] = False;
65                        }
66                        $this->db           = CreateObject('phpgwapi.db');
67                        $this->db->Host     = $GLOBALS['phpgw_domain'][$this->ConfigDomain]['db_host'];
68                        $this->db->Port     = $GLOBALS['phpgw_domain'][$this->ConfigDomain]['db_port'];
69                        $this->db->Type     = $GLOBALS['phpgw_domain'][$this->ConfigDomain]['db_type'];
70                        $this->db->Database = $GLOBALS['phpgw_domain'][$this->ConfigDomain]['db_name'];
71                        $this->db->User     = $GLOBALS['phpgw_domain'][$this->ConfigDomain]['db_user'];
72                        $this->db->Password = $GLOBALS['phpgw_domain'][$this->ConfigDomain]['db_pass'];
73                }
74
75                /**
76                * Set the domain used for cookies
77                *
78                * @return string domain
79                */
80                function set_cookiedomain()
81                {
82                        $this->cookie_domain = $_SERVER['HTTP_HOST'];
83
84                        // remove port from HTTP_HOST
85                        if (preg_match("/^(.*):(.*)$/",$this->cookie_domain,$arr))
86                        {
87                                $this->cookie_domain = $arr[1];
88                        }
89                        if (count(explode('.',$this->cookie_domain)) <= 1)
90                        {
91                                // setcookie dont likes domains without dots, leaving it empty, gets setcookie to fill the domain in
92                                $this->cookie_domain = '';
93                        }
94                }
95
96                /**
97                * Set a cookie
98                *
99                * @param string $cookiename name of cookie to be set
100                * @param string $cookievalue value to be used, if unset cookie is cleared (optional)
101                * @param int $cookietime when cookie should expire, 0 for session only (optional)
102                */
103                function set_cookie($cookiename,$cookievalue='',$cookietime=0)
104                {
105                        if(!isset($this->cookie_domain) || !$this->cookie_domain)
106                        {
107                                $this->set_cookiedomain();
108                        }
109                        setcookie($cookiename,$cookievalue,$cookietime,'/',$this->cookie_domain,null,true);
110                }
111
112                /*!
113                @function auth
114                @abstract authenticate the setup user
115                @param  $auth_type      ???
116                */
117                function auth($auth_type='Config')
118                {
119                        #phpinfo();
120                        $FormLogout = get_var('FormLogout',  array('GET','POST'));
121                        if(!$FormLogout)
122                        {
123                                $ConfigLogin  = get_var('ConfigLogin', array('POST'));
124                                $HeaderLogin  = get_var('HeaderLogin', array('POST'));
125                                $FormDomain   = get_var('FormDomain',  array('POST'));
126                                $FormUser     = get_var('FormUser',    array('POST'));
127                                $FormPW       = get_var('FormPW',      array('POST'));
128
129                                $this->ConfigDomain = get_var('ConfigDomain',array('POST','COOKIE'));
130                                $ConfigUser   = get_var('ConfigUser',  array('POST','COOKIE'));
131                                $ConfigPW     = get_var('ConfigPW',    array('POST','COOKIE'));
132                                $HeaderUser   = get_var('HeaderUser',  array('POST','COOKIE'));
133                                $HeaderPW     = get_var('HeaderPW',    array('POST','COOKIE'));
134                                $ConfigLang   = get_var('ConfigLang',  array('POST','COOKIE'));
135
136                                /* Setup defaults to aid in header upgrade to version 1.26.
137                                 * This was the first version to include the following values.
138                                 */
139                                if(!@isset($GLOBALS['phpgw_domain'][$FormDomain]['config_user']) && isset($GLOBALS['phpgw_domain'][$FormDomain]))
140                                {
141                                        @$GLOBALS['phpgw_domain'][$FormDomain]['config_user'] = 'admin';
142                                }
143                                if(!@isset($GLOBALS['phpgw_info']['server']['header_admin_user']))
144                                {
145                                        @$GLOBALS['phpgw_info']['server']['header_admin_user'] = 'admin';
146                                }
147                        }
148
149                        $remoteip   = $_SERVER['REMOTE_ADDR'];
150                        if(!empty($remoteip) && !$this->checkip($remoteip)) { return False; }
151
152                        /* If FormLogout is set, simply invalidate the cookies (LOGOUT) */
153                        switch(strtolower($FormLogout))
154                        {
155                                case 'config':
156                                        /* config logout */
157                                        $expire = time() - 86400;
158                                        $this->set_cookie('ConfigUser','',$expire,'/');
159                                        $this->set_cookie('ConfigPW','',$expire,'/');
160                                        $this->set_cookie('ConfigDomain','',$expire,'/');
161                                        $this->set_cookie('ConfigLang','',$expire,'/');
162                                        $GLOBALS['phpgw_info']['setup']['LastDomain'] = $_COOKIE['ConfigDomain'];
163                                        $GLOBALS['phpgw_info']['setup']['ConfigLoginMSG'] = lang('You have successfully logged out');
164                                        $GLOBALS['phpgw_info']['setup']['HeaderLoginMSG'] = '';
165                                        return False;
166                                case 'header':
167                                        /* header admin logout */
168                                        $expire = time() - 86400;
169                                        $this->set_cookie('HeaderUser','',$expire,'/');
170                                        $this->set_cookie('HeaderPW','',$expire,'/');
171                                        $this->set_cookie('ConfigLang','',$expire,'/');
172                                        $GLOBALS['phpgw_info']['setup']['HeaderLoginMSG'] = lang('You have successfully logged out');
173                                        $GLOBALS['phpgw_info']['setup']['ConfigLoginMSG'] = '';
174                                        return False;
175                        }
176
177                        /* We get here if FormLogout is not set (LOGIN or subsequent pages) */
178                        /* Expire login if idle for 20 minutes.  The cookies are updated on every page load. */
179                        $expire = (int)(time() + (1200*9));
180
181                        switch(strtolower($auth_type))
182                        {
183                                case 'header':
184                                        if(!empty($HeaderLogin))
185                                        {
186                                                /* header admin login */
187                                                /* New test is md5, cleartext version is for header < 1.26 */
188                                                if ($this->check_auth($FormUser,$FormPW,$GLOBALS['phpgw_info']['server']['header_admin_user'],
189                                                        $GLOBALS['phpgw_info']['server']['header_admin_password']))
190                                                {
191                                                        $this->set_cookie('HeaderUser',"$FormUser",$expire,'/');
192                                                        $this->set_cookie('HeaderPW',"$FormPW",$expire,'/');
193                                                        $this->set_cookie('ConfigLang',"$ConfigLang",$expire,'/');
194                                                        return True;
195                                                }
196                                                else
197                                                {
198                                                        $GLOBALS['phpgw_info']['setup']['HeaderLoginMSG'] = lang('Invalid password');
199                                                        $GLOBALS['phpgw_info']['setup']['ConfigLoginMSG'] = '';
200                                                        return False;
201                                                }
202                                        }
203                                        elseif(!empty($HeaderPW) && $auth_type == 'Header')
204                                        {
205                                                // Returning after login to header admin
206                                                /* New test is md5, cleartext version is for header < 1.26 */
207                                                if ($this->check_auth($HeaderUser,$HeaderPW,$GLOBALS['phpgw_info']['server']['header_admin_user'],
208                                                        $GLOBALS['phpgw_info']['server']['header_admin_password']))
209                                                {
210                                                        $this->set_cookie('HeaderUser',"$HeaderUser",$expire,'/');
211                                                        $this->set_cookie('HeaderPW',"$HeaderPW",$expire,'/');
212                                                        $this->set_cookie('ConfigLang',"$ConfigLang",$expire,'/');
213                                                        return True;
214                                                }
215                                                else
216                                                {
217                                                        $GLOBALS['phpgw_info']['setup']['HeaderLoginMSG'] = lang('Invalid password');
218                                                        $GLOBALS['phpgw_info']['setup']['ConfigLoginMSG'] = '';
219                                                        return False;
220                                                }
221                                        }
222                                        break;
223                                case 'config':
224                                        if(!empty($ConfigLogin))
225                                        {
226                                                /* config login */
227                                                /* New test is md5, cleartext version is for header < 1.26 */
228                                                if (isset($GLOBALS['phpgw_domain'][$FormDomain]) &&
229                                                        $this->check_auth($FormUser,$FormPW,@$GLOBALS['phpgw_domain'][$FormDomain]['config_user'],
230                                                        @$GLOBALS['phpgw_domain'][$FormDomain]['config_passwd']))
231                                                {
232                                                        $this->set_cookie('ConfigUser',"$FormUser",$expire,'/');
233                                                        $this->set_cookie('ConfigPW',"$FormPW",$expire,'/');
234                                                        $this->set_cookie('ConfigDomain',"$FormDomain",$expire,'/');
235                                                        /* Set this now since the cookie will not be available until the next page load */
236                                                        $this->ConfigDomain = "$FormDomain";
237                                                        $this->set_cookie('ConfigLang',"$ConfigLang",$expire,'/');
238                                                        return True;
239                                                }
240                                                else
241                                                {
242                                                        $GLOBALS['phpgw_info']['setup']['ConfigLoginMSG'] = lang('Invalid password');
243                                                        $GLOBALS['phpgw_info']['setup']['HeaderLoginMSG'] = '';
244                                                        return False;
245                                                }
246                                        }
247                                        elseif(!empty($ConfigPW))
248                                        {
249                                                // Returning after login to config
250                                                /* New test is md5, cleartext version is for header < 1.26 */
251                                                if ($this->check_auth($ConfigUser,$ConfigPW,@$GLOBALS['phpgw_domain'][$this->ConfigDomain]['config_user'],
252                                                        @$GLOBALS['phpgw_domain'][$this->ConfigDomain]['config_passwd']))
253                                                {
254                                                        $this->set_cookie('ConfigUser',"$ConfigUser",$expire,'/');
255                                                        $this->set_cookie('ConfigPW',"$ConfigPW",$expire,'/');
256                                                        $this->set_cookie('ConfigDomain',$this->ConfigDomain,$expire,'/');
257                                                        $this->set_cookie('ConfigLang',"$ConfigLang",$expire,'/');
258                                                        return True;
259                                                }
260                                                else
261                                                {
262                                                        $GLOBALS['phpgw_info']['setup']['ConfigLoginMSG'] = lang('Invalid password');
263                                                        $GLOBALS['phpgw_info']['setup']['HeaderLoginMSG'] = '';
264                                                        return False;
265                                                }
266                                        }
267                                        break;
268                        }
269
270                        return False;
271                }
272
273                // returns True if user and pw match, if conf_pw is a md5 ONLY compare with md5($pw) and NOT the plaintext !!!
274                function check_auth($user,$pw,$conf_user,$conf_pw)
275                {
276                        if ($user != $conf_user)
277                        {
278                                return False; // wrong username
279                        }
280                        if (preg_match('/^[0-9a-f]{32}$/',$conf_pw))    // $conf_pw is a md5
281                        {
282                                $pw = md5($pw);
283                        }
284                        return $pw == $conf_pw;
285                }
286
287                function checkip($remoteip='')
288                {
289                        //echo "<p>setup::checkip($remoteip) against setup_acl='".$GLOBALS['phpgw_info']['server']['setup_acl']."'</p>\n";
290                        $allowed_ips = explode(',',@$GLOBALS['phpgw_info']['server']['setup_acl']);
291                        if(empty($GLOBALS['phpgw_info']['server']['setup_acl']) || !is_array($allowed_ips))
292                        {
293                                return True;    // no test
294                        }
295                        $remotes = explode('.',$remoteip);
296                        foreach($allowed_ips as $value)
297                        {
298                                if (!preg_match('/^[0-9.]+$/',$value))
299                                {
300                                        $value = gethostbyname($was=$value);            // resolve domain-name, eg. a dyndns account
301                                        //echo "resolving '$was' to '$value'<br>\n";
302                                }
303                                $values = explode('.',$value);
304                                for($i = 0; $i < count($values); ++$i)
305                                {
306                                        if ((int) $values[$i] != (int) $remotes[$i])
307                                        {
308                                                break;
309                                        }
310                                }
311                                if ($i == count($values))
312                                {
313                                        return True;    // match
314                                }
315                        }
316                        $GLOBALS['phpgw_info']['setup']['HeaderLoginMSG'] = '';
317                        $GLOBALS['phpgw_info']['setup']['ConfigLoginMSG'] = lang('Invalid IP address');
318
319                        return False;
320                }
321
322                /*!
323                @function get_major
324                @abstract Return X.X.X major version from X.X.X.X versionstring
325                @param  $
326                */
327                function get_major($versionstring)
328                {
329                        if(!$versionstring)
330                        {
331                                return False;
332                        }
333
334                        $version = str_replace('-','',str_replace('pre','.',$versionstring));
335                        $varray  = explode('.',$version);
336                        $major   = implode('.',array($varray[0],$varray[1]));
337
338                        return $major;
339                }
340
341                /*!
342                @function clear_session_cache
343                @abstract Clear system/user level cache so as to have it rebuilt with the next access
344                @param  None
345                */
346                function clear_session_cache()
347                {
348                        $tables = Array();
349                        $tablenames = $this->db->table_names();
350                        foreach($tablenames as $key => $val)
351                        {
352                                $tables[] = $val['table_name'];
353                        }
354                        if(in_array('phpgw_app_sessions',$tables))
355                        {
356                                $this->db->lock(array('phpgw_app_sessions'));
357                                @$this->db->query("DELETE FROM phpgw_app_sessions WHERE sessionid = '0' and loginid = '0' and app = 'phpgwapi' and location = 'config'",__LINE__,__FILE__);
358                                @$this->db->query("DELETE FROM phpgw_app_sessions WHERE app = 'phpgwapi' and location = 'phpgw_info_cache'",__LINE__,__FILE__);
359                                $this->db->unlock();
360                        }
361                }
362
363                /*!
364                @function register_app
365                @abstract Add an application to the phpgw_applications table
366                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
367                @param  $enable         optional, set to True/False to override setup.inc.php setting
368                */
369                function register_app($appname,$enable=99)
370                {
371                        $setup_info = $GLOBALS['setup_info'];
372
373                        if(!$appname)
374                        {
375                                return False;
376                        }
377
378                        if($enable==99)
379                        {
380                                $enable = $setup_info[$appname]['enable'];
381                        }
382                        $enable = (int)$enable;
383
384                        /*
385                        Use old applications table if the currentver is less than 0.9.10pre8,
386                        but not if the currentver = '', which probably means new install.
387                        */
388                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
389                        {
390                                $appstbl = 'applications';
391                        }
392                        else
393                        {
394                                $appstbl = 'phpgw_applications';
395                        }
396
397                        if($GLOBALS['DEBUG'])
398                        {
399                                echo '<br>register_app(): ' . $appname . ', version: ' . $setup_info[$appname]['version'] . ', table: ' . $appstbl . '<br>';
400                                // _debug_array($setup_info[$appname]);
401                        }
402
403                        if($setup_info[$appname]['version'])
404                        {
405                                if($setup_info[$appname]['tables'])
406                                {
407                                        $tables = implode(',',$setup_info[$appname]['tables']);
408                                }
409                                if ($setup_info[$appname]['tables_use_prefix'] == True)
410                                {
411                                        echo $setup_info[$appname]['name'] . ' uses tables_use_prefix, storing '
412                                                . $setup_info[$appname]['tables_prefix']
413                                                . ' as prefix for ' . $setup_info[$appname]['name'] . " tables\n";
414
415                                        $sql = "INSERT INTO phpgw_config (config_app,config_name,config_value) "
416                                                ."VALUES ('".$setup_info[$appname]['name']."','"
417                                                .$appname."_tables_prefix','".$setup_info[$appname]['tables_prefix']."');";
418                                        $this->db->query($sql,__LINE__,__FILE__);
419                                }
420                                $this->db->query("INSERT INTO $appstbl "
421                                        . "(app_name,app_enabled,app_order,app_tables,app_version) "
422                                        . "VALUES ("
423                                        . "'" . $setup_info[$appname]['name'] . "',"
424                                        . $enable . ","
425                                        . (int)$setup_info[$appname]['app_order'] . ","
426                                        . "'" . $tables . "',"
427                                        . "'" . $setup_info[$appname]['version'] . "');"
428                                );
429                                $this->clear_session_cache();
430                        }
431                }
432
433                /*!
434                @function app_registered
435                @abstract Check if an application has info in the db
436                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
437                @param  $enabled        optional, set to False to not enable this app
438                */
439                function app_registered($appname)
440                {
441                        $setup_info = $GLOBALS['setup_info'];
442
443                        if(!$appname)
444                        {
445                                return False;
446                        }
447
448                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
449                        {
450                                $appstbl = 'applications';
451                        }
452                        else
453                        {
454                                $appstbl = 'phpgw_applications';
455                        }
456
457                        if(@$GLOBALS['DEBUG'])
458                        {
459                                echo '<br>app_registered(): checking ' . $appname . ', table: ' . $appstbl;
460                                // _debug_array($setup_info[$appname]);
461                        }
462
463                        $this->db->query("SELECT COUNT(app_name) FROM $appstbl WHERE app_name='".$appname."'");
464                        $this->db->next_record();
465                        if($this->db->f(0))
466                        {
467                                if(@$GLOBALS['DEBUG'])
468                                {
469                                        echo '... app previously registered.';
470                                }
471                                return True;
472                        }
473                        if(@$GLOBALS['DEBUG'])
474                        {
475                                echo '... app not registered';
476                        }
477                        return False;
478                }
479
480                /*!
481                @function update_app
482                @abstract Update application info in the db
483                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
484                @param  $enabled        optional, set to False to not enable this app
485                */
486                function update_app($appname)
487                {
488                        $setup_info = $GLOBALS['setup_info'];
489
490                        if(!$appname)
491                        {
492                                return False;
493                        }
494
495                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
496                        {
497                                $appstbl = 'applications';
498                        }
499                        else
500                        {
501                                $appstbl = 'phpgw_applications';
502                        }
503
504                        if($GLOBALS['DEBUG'])
505                        {
506                                echo '<br>update_app(): ' . $appname . ', version: ' . $setup_info[$appname]['currentver'] . ', table: ' . $appstbl . '<br>';
507                                // _debug_array($setup_info[$appname]);
508                        }
509
510                        $this->db->query("SELECT COUNT(app_name) FROM $appstbl WHERE app_name='".$appname."'");
511                        $this->db->next_record();
512                        if(!$this->db->f(0))
513                        {
514                                return False;
515                        }
516
517                        if($setup_info[$appname]['version'])
518                        {
519                                //echo '<br>' . $setup_info[$appname]['version'];
520                                if($setup_info[$appname]['tables'])
521                                {
522                                        $tables = implode(',',$setup_info[$appname]['tables']);
523                                }
524
525                                $sql = "UPDATE $appstbl "
526                                        . "SET app_name='" . $setup_info[$appname]['name'] . "',"
527                                        . " app_enabled=" . (int)$setup_info[$appname]['enable'] . ","
528                                        . " app_order=" . (int)$setup_info[$appname]['app_order'] . ","
529                                        . " app_tables='" . $tables . "',"
530                                        . " app_version='" . $setup_info[$appname]['version'] . "'"
531                                        . " WHERE app_name='" . $appname . "'";
532                                //echo $sql; exit;
533
534                                $this->db->query($sql);
535                        }
536                }
537
538                /*!
539                @function update_app_version
540                @abstract Update application version in applications table, post upgrade
541                @param  $setup_info             Array of application information (multiple apps or single)
542                @param  $appname                Application 'name' with a matching $setup_info[$appname] array slice
543                @param  $tableschanged  ???
544                */
545                function update_app_version($setup_info, $appname, $tableschanged = True)
546                {
547                        if(!$appname)
548                        {
549                                return False;
550                        }
551
552                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
553                        {
554                                $appstbl = 'applications';
555                        }
556                        else
557                        {
558                                $appstbl = 'phpgw_applications';
559                        }
560
561                        if($tableschanged == True)
562                        {
563                                $GLOBALS['phpgw_info']['setup']['tableschanged'] = True;
564                        }
565                        if($setup_info[$appname]['currentver'])
566                        {
567                                $this->db->query("UPDATE $appstbl SET app_version='" . $setup_info[$appname]['currentver'] . "' WHERE app_name='".$appname."'");
568                        }
569                        return $setup_info;
570                }
571
572                /*!
573                @function deregister_app
574                @abstract de-Register an application
575                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
576                */
577                function deregister_app($appname)
578                {
579                        if(!$appname)
580                        {
581                                return False;
582                        }
583                        $setup_info = $GLOBALS['setup_info'];
584
585                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
586                        {
587                                $appstbl = 'applications';
588                        }
589                        else
590                        {
591                                $appstbl = 'phpgw_applications';
592                        }
593
594                        //echo 'DELETING application: ' . $appname;
595                        $this->db->query("DELETE FROM $appstbl WHERE app_name='". $appname ."'");
596                        $this->clear_session_cache();
597                }
598
599                /*!
600                @function register_hooks
601                @abstract Register an application's hooks
602                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
603                */
604                function register_hooks($appname)
605                {
606                        $setup_info = $GLOBALS['setup_info'];
607
608                        if(!$appname)
609                        {
610                                return False;
611                        }
612
613                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.8pre5') && ($setup_info['phpgwapi']['currentver'] != ''))
614                        {
615                                /* No phpgw_hooks table yet. */
616                                return False;
617                        }
618
619                        if (!is_object($this->hooks))
620                        {
621                                $this->hooks = CreateObject('phpgwapi.hooks',$this->db);
622                        }
623                        $this->hooks->register_hooks($appname,$setup_info[$appname]['hooks']);
624                }
625
626                /*!
627                @function update_hooks
628                @abstract Update an application's hooks
629                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
630                */
631                function update_hooks($appname)
632                {
633                        $this->register_hooks($appname);
634                }
635
636                /*!
637                @function deregister_hooks
638                @abstract de-Register an application's hooks
639                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
640                */
641                function deregister_hooks($appname)
642                {
643                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.8pre5'))
644                        {
645                                /* No phpgw_hooks table yet. */
646                                return False;
647                        }
648
649                        if(!$appname)
650                        {
651                                return False;
652                        }
653                       
654                        //echo "DELETING hooks for: " . $setup_info[$appname]['name'];
655                        if (!is_object($this->hooks))
656                        {
657                                $this->hooks = CreateObject('phpgwapi.hooks',$this->db);
658                        }
659                        $this->hooks->register_hooks($appname);
660                }
661
662                /*!
663                 @function hook
664                 @abstract call the hooks for a single application
665                 @param $location hook location - required
666                 @param $appname application name - optional
667                */
668                function hook($location, $appname='')
669                {
670                        if (!is_object($this->hooks))
671                        {
672                                $this->hooks = CreateObject('phpgwapi.hooks',$this->db);
673                        }
674                        return $this->hooks->single($location,$appname,True,True);
675                }
676
677                /*
678                @function alessthanb
679                @abstract phpgw version checking, is param 1 < param 2 in phpgw versionspeak?
680                @param  $a      phpgw version number to check if less than $b
681                @param  $b      phpgw version number to check $a against
682                #return True if $a < $b
683                */
684                function alessthanb($a,$b,$DEBUG=False)
685                {
686                        $num = array('1st','2nd','3rd','4th');
687
688                        if($DEBUG)
689                        {
690                                echo'<br>Input values: '
691                                        . 'A="'.$a.'", B="'.$b.'"';
692                        }                       
693                        $newa = str_replace('-','',str_replace('pre','.',$a));
694                        $newb = str_replace('-','',str_replace('pre','.',$b));
695                        $testa = explode('.',$newa);
696                        if(@$testa[1] == '')
697                        {
698                                $testa[1] = 0;
699                        }
700
701                        $testb = explode('.',$newb);
702                        if(@$testb[1] == '')
703                        {
704                                $testb[1] = 0;
705                        }
706                        if(@$testb[3] == '')
707                        {
708                                $testb[3] = 0;
709                        }
710                        $less = 0;
711
712                        for($i=0;$i<count($testa);$i++)
713                        {
714                                if($DEBUG) { echo'<br>Checking if '. (int)$testa[$i] . ' is less than ' . (int)$testb[$i] . ' ...'; }
715                                if((int)$testa[$i] < (int)$testb[$i])
716                                {
717                                        if ($DEBUG) { echo ' yes.'; }
718                                        $less++;
719                                        if($i<3)
720                                        {
721                                                /* Ensure that this is definitely smaller */
722                                                if($DEBUG) { echo"  This is the $num[$i] octet, so A is definitely less than B."; }
723                                                $less = 5;
724                                                break;
725                                        }
726                                }
727                                elseif((int)$testa[$i] > (int)$testb[$i])
728                                {
729                                        if($DEBUG) { echo ' no.'; }
730                                        $less--;
731                                        if($i<2)
732                                        {
733                                                /* Ensure that this is definitely greater */
734                                                if($DEBUG) { echo"  This is the $num[$i] octet, so A is definitely greater than B."; }
735                                                $less = -5;
736                                                break;
737                                        }
738                                }
739                                else
740                                {
741                                        if($DEBUG) { echo ' no, they are equal or of different length.'; }
742                                        // makes sure eg. '1.0.0' is counted less the '1.0.0.xxx' !
743                                        $less = count($testa) < count($testb) ? 1 : 0;
744                                }
745                        }
746                        if($DEBUG) { echo '<br>Check value is: "'.$less.'"'; }
747                        if($less>0)
748                        {
749                                if($DEBUG) { echo '<br>A is less than B'; }
750                                return True;
751                        }
752                        elseif($less<0)
753                        {
754                                if($DEBUG) { echo '<br>A is greater than B'; }
755                                return False;
756                        }
757                        else
758                        {
759                                if($DEBUG) { echo '<br>A is equal to B'; }
760                                return False;
761                        }
762                }
763
764                /*!
765                @function amorethanb
766                @abstract phpgw version checking, is param 1 > param 2 in phpgw versionspeak?
767                @param  $a      phpgw version number to check if more than $b
768                @param  $b      phpgw version number to check $a against
769                #return True if $a < $b
770                */
771                function amorethanb($a,$b,$DEBUG=False)
772                {
773                        $num = array('1st','2nd','3rd','4th');
774
775                        if($DEBUG)
776                        {
777                                echo'<br>Input values: '
778                                        . 'A="'.$a.'", B="'.$b.'"';
779                        }
780                        $newa = str_replace('-','',str_replace('pre','.',$a));
781                        $newb = str_replace('-','',str_replace('pre','.',$b));
782                        $testa = explode('.',$newa);
783                        if($testa[3] == '')
784                        {
785                                $testa[3] = 0;
786                        }
787                        $testb = explode('.',$newb);
788                        if($testb[3] == '')
789                        {
790                                $testb[3] = 0;
791                        }
792                        $less = 0;
793
794                        for($i=0;$i<count($testa);$i++)
795                        {
796                                if($DEBUG) { echo'<br>Checking if '. (int)$testa[$i] . ' is more than ' . (int)$testb[$i] . ' ...'; }
797                                if((int)$testa[$i] > (int)$testb[$i])
798                                {
799                                        if($DEBUG) { echo ' yes.'; }
800                                        $less++;
801                                        if($i<3)
802                                        {
803                                                /* Ensure that this is definitely greater */
804                                                if($DEBUG) { echo"  This is the $num[$i] octet, so A is definitely greater than B."; }
805                                                $less = 5;
806                                                break;
807                                        }
808                                }
809                                elseif((int)$testa[$i] < (int)$testb[$i])
810                                {
811                                        if($DEBUG) { echo ' no.'; }
812                                        $less--;
813                                        if($i<2)
814                                        {
815                                                /* Ensure that this is definitely smaller */
816                                                if($DEBUG) { echo"  This is the $num[$i] octet, so A is definitely less than B."; }
817                                                $less = -5;
818                                                break;
819                                        }
820                                }
821                                else
822                                {
823                                        if($DEBUG) { echo ' no, they are equal.'; }
824                                        $less = 0;
825                                }
826                        }
827                        if($DEBUG) { echo '<br>Check value is: "'.$less.'"'; }
828                        if($less>0)
829                        {
830                                if($DEBUG) { echo '<br>A is greater than B'; }
831                                return True;
832                        }
833                        elseif($less<0)
834                        {
835                                if($DEBUG) { echo '<br>A is less than B'; }
836                                return False;
837                        }
838                        else
839                        {
840                                if($DEBUG) { echo '<br>A is equal to B'; }
841                                return False;
842                        }
843                }
844
845                function get_hooks_table_name()
846                {
847                        if(@$this->alessthanb($GLOBALS['setup_info']['phpgwapi']['currentver'],'0.9.8pre5') &&
848                           @$GLOBALS['setup_info']['phpgwapi']['currentver'] != '')
849                        {
850                                /* No phpgw_hooks table yet. */
851                                return False;
852                        }
853                        return 'phpgw_hooks';
854                }
855
856                function setup_account_object()
857                {
858                        if (!is_object($GLOBALS['phpgw']->accounts))
859                        {
860                                if (!is_object($this->db))
861                                {
862                                        $this->loaddb();
863                                }
864                                /* Load up some configured values */
865                                $this->db->query("SELECT config_name,config_value FROM phpgw_config "
866                                        . "WHERE config_name LIKE 'ldap%' OR config_name LIKE 'account_%' OR config_name LIKE '%encryption%'",__LINE__,__FILE__);
867                                while($this->db->next_record())
868                                {
869                                        $GLOBALS['phpgw_info']['server'][$this->db->f('config_name')] = $this->db->f('config_value');
870                                }
871                                if (!is_object($GLOBALS['phpgw']))
872                                {
873                                        $GLOBALS['phpgw'] = CreateObject('phpgwapi.phpgw');
874                                }
875                                copyobj($this->db,$GLOBALS['phpgw']->db);
876                                $GLOBALS['phpgw']->common      = CreateObject('phpgwapi.common');
877                                $GLOBALS['phpgw']->accounts    = CreateObject('phpgwapi.accounts');
878
879                                if(($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap') &&
880                                        !$GLOBALS['phpgw']->accounts->ds)
881                                {
882                                        printf("<b>Error: Error connecting to LDAP server %s!</b><br>",$GLOBALS['phpgw_info']['server']['ldap_host']);
883                                        exit;
884                                }
885                        }
886                }
887
888                /*!
889                @function add_account
890                @abstract add an user account or a user group
891                @param username string alphanumerical username or groupname (account_lid)
892                @param first, last string first / last name
893                @param $passwd string cleartext pw
894                @param $group string/boolean Groupname for users primary group or False for a group, default 'Default'
895                @param $changepw boolean user has right to change pw, default False
896                @returns the numerical user-id
897                @note if the $username already exists, only the id is returned, no new user / group gets created
898                */
899                function add_account($username,$first,$last,$passwd,$group='default',$changepw=False)
900                {
901                        $this->setup_account_object();
902
903                        $groupid = $group ? $GLOBALS['phpgw']->accounts->name2id($group) : False;
904
905                        if(!($accountid = $GLOBALS['phpgw']->accounts->name2id($username)))
906                        {
907                                $accountid = $accountid ? $accountid : $GLOBALS['phpgw']->accounts->create(array(
908                                        'account_type'      => $group ? 'u' : 'g',
909                                        'account_lid'       => $username,
910                                        'account_passwd'    => $passwd,
911                                        'account_firstname' => $first,
912                                        'account_lastname'  => $last,
913                                        'account_status'    => 'A',
914                                        'account_primary_group' => $groupid,
915                                        'account_expires'   => -1
916                                ));
917                        }
918                        $accountid = (int)$accountid;
919                        if($groupid)
920                        {
921                                $this->add_acl('phpgw_group',(int)$groupid,$accountid);
922                        }
923                        $this->add_acl('preferences','changepassword',$accountid,(int)$changepw);
924
925                        return $accountid;
926                }
927
928                /*!
929                @function add_acl
930                @abstract Add ACL rights
931                @param $app string/array with app-names
932                @param $locations string eg. run
933                @param $account int/string accountid or account_lid
934                @param $rights int rights to set, default 1
935                */
936                function add_acl($apps,$location,$account,$rights=1)
937                {
938                        if (!is_int($account))
939                        {
940                                $this->setup_account_object();
941                                $account = $GLOBALS['phpgw']->accounts->name2id($account);
942                        }
943                        $rights = (int)$rights;
944                        if(!is_object($this->db))
945                        {
946                                $this->loaddb();
947                        }
948
949                        if(!is_array($apps))
950                        {
951                                $apps = array($apps);
952                        }
953                        foreach($apps as $app)
954                        {
955                                $this->db->query("DELETE FROM phpgw_acl WHERE acl_appname='$app' AND acl_location='$location' AND acl_account=$account");
956                                if ($rights)
957                                {
958                                        $this->db->query("INSERT INTO phpgw_acl(acl_appname,acl_location,acl_account,acl_rights) VALUES('$app','$location',$account,$rights)");
959                                }
960                        }
961                }
962        }
963?>
Note: See TracBrowser for help on using the repository browser.