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

Revision 7673, 28.8 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

  • 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                $values_count = count($values);
305                                for($i = 0; $i < $values_count; ++$i)
306                                {
307                                        if ((int) $values[$i] != (int) $remotes[$i])
308                                        {
309                                                break;
310                                        }
311                                }
312                                if ($i == count($values))
313                                {
314                                        return True;    // match
315                                }
316                        }
317                        $GLOBALS['phpgw_info']['setup']['HeaderLoginMSG'] = '';
318                        $GLOBALS['phpgw_info']['setup']['ConfigLoginMSG'] = lang('Invalid IP address');
319
320                        return False;
321                }
322
323                /*!
324                @function get_major
325                @abstract Return X.X.X major version from X.X.X.X versionstring
326                @param  $
327                */
328                function get_major($versionstring)
329                {
330                        if(!$versionstring)
331                        {
332                                return False;
333                        }
334
335                        $version = str_replace('-','',str_replace('pre','.',$versionstring));
336                        $varray  = explode('.',$version);
337                        $major   = implode('.',array($varray[0],$varray[1]));
338
339                        return $major;
340                }
341
342                /*!
343                @function clear_session_cache
344                @abstract Clear system/user level cache so as to have it rebuilt with the next access
345                @param  None
346                */
347                function clear_session_cache()
348                {
349                        $tables = Array();
350                        $tablenames = $this->db->table_names();
351                        foreach($tablenames as $key => $val)
352                        {
353                                $tables[] = $val['table_name'];
354                        }
355                        if(in_array('phpgw_app_sessions',$tables))
356                        {
357                                $this->db->lock(array('phpgw_app_sessions'));
358                                @$this->db->query("DELETE FROM phpgw_app_sessions WHERE sessionid = '0' and loginid = '0' and app = 'phpgwapi' and location = 'config'",__LINE__,__FILE__);
359                                @$this->db->query("DELETE FROM phpgw_app_sessions WHERE app = 'phpgwapi' and location = 'phpgw_info_cache'",__LINE__,__FILE__);
360                                $this->db->unlock();
361                        }
362                }
363
364                /*!
365                @function register_app
366                @abstract Add an application to the phpgw_applications table
367                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
368                @param  $enable         optional, set to True/False to override setup.inc.php setting
369                */
370                function register_app($appname,$enable=99)
371                {
372                        $setup_info = $GLOBALS['setup_info'];
373
374                        if(!$appname)
375                        {
376                                return False;
377                        }
378
379                        if($enable==99)
380                        {
381                                $enable = $setup_info[$appname]['enable'];
382                        }
383                        $enable = (int)$enable;
384
385                        /*
386                        Use old applications table if the currentver is less than 0.9.10pre8,
387                        but not if the currentver = '', which probably means new install.
388                        */
389                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
390                        {
391                                $appstbl = 'applications';
392                        }
393                        else
394                        {
395                                $appstbl = 'phpgw_applications';
396                        }
397
398                        if($GLOBALS['DEBUG'])
399                        {
400                                echo '<br>register_app(): ' . $appname . ', version: ' . $setup_info[$appname]['version'] . ', table: ' . $appstbl . '<br>';
401                                // _debug_array($setup_info[$appname]);
402                        }
403
404                        if($setup_info[$appname]['version'])
405                        {
406                                if($setup_info[$appname]['tables'])
407                                {
408                                        $tables = implode(',',$setup_info[$appname]['tables']);
409                                }
410                                if ($setup_info[$appname]['tables_use_prefix'] == True)
411                                {
412                                        echo $setup_info[$appname]['name'] . ' uses tables_use_prefix, storing '
413                                                . $setup_info[$appname]['tables_prefix']
414                                                . ' as prefix for ' . $setup_info[$appname]['name'] . " tables\n";
415
416                                        $sql = "INSERT INTO phpgw_config (config_app,config_name,config_value) "
417                                                ."VALUES ('".$setup_info[$appname]['name']."','"
418                                                .$appname."_tables_prefix','".$setup_info[$appname]['tables_prefix']."');";
419                                        $this->db->query($sql,__LINE__,__FILE__);
420                                }
421                                $this->db->query("INSERT INTO $appstbl "
422                                        . "(app_name,app_enabled,app_order,app_tables,app_version) "
423                                        . "VALUES ("
424                                        . "'" . $setup_info[$appname]['name'] . "',"
425                                        . $enable . ","
426                                        . (int)$setup_info[$appname]['app_order'] . ","
427                                        . "'" . $tables . "',"
428                                        . "'" . $setup_info[$appname]['version'] . "');"
429                                );
430                                $this->clear_session_cache();
431                        }
432                }
433
434                /*!
435                @function app_registered
436                @abstract Check if an application has info in the db
437                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
438                @param  $enabled        optional, set to False to not enable this app
439                */
440                function app_registered($appname)
441                {
442                        $setup_info = $GLOBALS['setup_info'];
443
444                        if(!$appname)
445                        {
446                                return False;
447                        }
448
449                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
450                        {
451                                $appstbl = 'applications';
452                        }
453                        else
454                        {
455                                $appstbl = 'phpgw_applications';
456                        }
457
458                        if(@$GLOBALS['DEBUG'])
459                        {
460                                echo '<br>app_registered(): checking ' . $appname . ', table: ' . $appstbl;
461                                // _debug_array($setup_info[$appname]);
462                        }
463
464                        $this->db->query("SELECT COUNT(app_name) FROM $appstbl WHERE app_name='".$appname."'");
465                        $this->db->next_record();
466                        if($this->db->f(0))
467                        {
468                                if(@$GLOBALS['DEBUG'])
469                                {
470                                        echo '... app previously registered.';
471                                }
472                                return True;
473                        }
474                        if(@$GLOBALS['DEBUG'])
475                        {
476                                echo '... app not registered';
477                        }
478                        return False;
479                }
480
481                /*!
482                @function update_app
483                @abstract Update application info in the db
484                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
485                @param  $enabled        optional, set to False to not enable this app
486                */
487                function update_app($appname)
488                {
489                        $setup_info = $GLOBALS['setup_info'];
490
491                        if(!$appname)
492                        {
493                                return False;
494                        }
495
496                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
497                        {
498                                $appstbl = 'applications';
499                        }
500                        else
501                        {
502                                $appstbl = 'phpgw_applications';
503                        }
504
505                        if($GLOBALS['DEBUG'])
506                        {
507                                echo '<br>update_app(): ' . $appname . ', version: ' . $setup_info[$appname]['currentver'] . ', table: ' . $appstbl . '<br>';
508                                // _debug_array($setup_info[$appname]);
509                        }
510
511                        $this->db->query("SELECT COUNT(app_name) FROM $appstbl WHERE app_name='".$appname."'");
512                        $this->db->next_record();
513                        if(!$this->db->f(0))
514                        {
515                                return False;
516                        }
517
518                        if($setup_info[$appname]['version'])
519                        {
520                                //echo '<br>' . $setup_info[$appname]['version'];
521                                if($setup_info[$appname]['tables'])
522                                {
523                                        $tables = implode(',',$setup_info[$appname]['tables']);
524                                }
525
526                                $sql = "UPDATE $appstbl "
527                                        . "SET app_name='" . $setup_info[$appname]['name'] . "',"
528                                        . " app_enabled=" . (int)$setup_info[$appname]['enable'] . ","
529                                        . " app_order=" . (int)$setup_info[$appname]['app_order'] . ","
530                                        . " app_tables='" . $tables . "',"
531                                        . " app_version='" . $setup_info[$appname]['version'] . "'"
532                                        . " WHERE app_name='" . $appname . "'";
533                                //echo $sql; exit;
534
535                                $this->db->query($sql);
536                        }
537                }
538
539                /*!
540                @function update_app_version
541                @abstract Update application version in applications table, post upgrade
542                @param  $setup_info             Array of application information (multiple apps or single)
543                @param  $appname                Application 'name' with a matching $setup_info[$appname] array slice
544                @param  $tableschanged  ???
545                */
546                function update_app_version($setup_info, $appname, $tableschanged = True)
547                {
548                        if(!$appname)
549                        {
550                                return False;
551                        }
552
553                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
554                        {
555                                $appstbl = 'applications';
556                        }
557                        else
558                        {
559                                $appstbl = 'phpgw_applications';
560                        }
561
562                        if($tableschanged == True)
563                        {
564                                $GLOBALS['phpgw_info']['setup']['tableschanged'] = True;
565                        }
566                        if($setup_info[$appname]['currentver'])
567                        {
568                                $this->db->query("UPDATE $appstbl SET app_version='" . $setup_info[$appname]['currentver'] . "' WHERE app_name='".$appname."'");
569                        }
570                        return $setup_info;
571                }
572
573                /*!
574                @function deregister_app
575                @abstract de-Register an application
576                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
577                */
578                function deregister_app($appname)
579                {
580                        if(!$appname)
581                        {
582                                return False;
583                        }
584                        $setup_info = $GLOBALS['setup_info'];
585
586                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.10pre8') && ($setup_info['phpgwapi']['currentver'] != ''))
587                        {
588                                $appstbl = 'applications';
589                        }
590                        else
591                        {
592                                $appstbl = 'phpgw_applications';
593                        }
594
595                        //echo 'DELETING application: ' . $appname;
596                        $this->db->query("DELETE FROM $appstbl WHERE app_name='". $appname ."'");
597                        $this->clear_session_cache();
598                }
599
600                /*!
601                @function register_hooks
602                @abstract Register an application's hooks
603                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
604                */
605                function register_hooks($appname)
606                {
607                        $setup_info = $GLOBALS['setup_info'];
608
609                        if(!$appname)
610                        {
611                                return False;
612                        }
613
614                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.8pre5') && ($setup_info['phpgwapi']['currentver'] != ''))
615                        {
616                                /* No phpgw_hooks table yet. */
617                                return False;
618                        }
619
620                        if (!is_object($this->hooks))
621                        {
622                                $this->hooks = CreateObject('phpgwapi.hooks',$this->db);
623                        }
624                        $this->hooks->register_hooks($appname,$setup_info[$appname]['hooks']);
625                }
626
627                /*!
628                @function update_hooks
629                @abstract Update an application's hooks
630                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
631                */
632                function update_hooks($appname)
633                {
634                        $this->register_hooks($appname);
635                }
636
637                /*!
638                @function deregister_hooks
639                @abstract de-Register an application's hooks
640                @param  $appname        Application 'name' with a matching $setup_info[$appname] array slice
641                */
642                function deregister_hooks($appname)
643                {
644                        if($this->alessthanb($setup_info['phpgwapi']['currentver'],'0.9.8pre5'))
645                        {
646                                /* No phpgw_hooks table yet. */
647                                return False;
648                        }
649
650                        if(!$appname)
651                        {
652                                return False;
653                        }
654                       
655                        //echo "DELETING hooks for: " . $setup_info[$appname]['name'];
656                        if (!is_object($this->hooks))
657                        {
658                                $this->hooks = CreateObject('phpgwapi.hooks',$this->db);
659                        }
660                        $this->hooks->register_hooks($appname);
661                }
662
663                /*!
664                 @function hook
665                 @abstract call the hooks for a single application
666                 @param $location hook location - required
667                 @param $appname application name - optional
668                */
669                function hook($location, $appname='')
670                {
671                        if (!is_object($this->hooks))
672                        {
673                                $this->hooks = CreateObject('phpgwapi.hooks',$this->db);
674                        }
675                        return $this->hooks->single($location,$appname,True,True);
676                }
677
678                /*
679                @function alessthanb
680                @abstract phpgw version checking, is param 1 < param 2 in phpgw versionspeak?
681                @param  $a      phpgw version number to check if less than $b
682                @param  $b      phpgw version number to check $a against
683                #return True if $a < $b
684                */
685                function alessthanb($a,$b,$DEBUG=False)
686                {
687                        $num = array('1st','2nd','3rd','4th');
688
689                        if($DEBUG)
690                        {
691                                echo'<br>Input values: '
692                                        . 'A="'.$a.'", B="'.$b.'"';
693                        }                       
694                        $newa = str_replace('-','',str_replace('pre','.',$a));
695                        $newb = str_replace('-','',str_replace('pre','.',$b));
696                        $testa = explode('.',$newa);
697                        if(@$testa[1] == '')
698                        {
699                                $testa[1] = 0;
700                        }
701
702                        $testb = explode('.',$newb);
703                        if(@$testb[1] == '')
704                        {
705                                $testb[1] = 0;
706                        }
707                        if(@$testb[3] == '')
708                        {
709                                $testb[3] = 0;
710                        }
711                        $less = 0;
712
713            $testa_count = count($testa);
714                        for($i=0;$i<$testa_count;++$i)
715                        {
716                                if($DEBUG) { echo'<br>Checking if '. (int)$testa[$i] . ' is less than ' . (int)$testb[$i] . ' ...'; }
717                                if((int)$testa[$i] < (int)$testb[$i])
718                                {
719                                        if ($DEBUG) { echo ' yes.'; }
720                                        ++$less;
721                                        if($i<3)
722                                        {
723                                                /* Ensure that this is definitely smaller */
724                                                if($DEBUG) { echo"  This is the $num[$i] octet, so A is definitely less than B."; }
725                                                $less = 5;
726                                                break;
727                                        }
728                                }
729                                elseif((int)$testa[$i] > (int)$testb[$i])
730                                {
731                                        if($DEBUG) { echo ' no.'; }
732                                        $less--;
733                                        if($i<2)
734                                        {
735                                                /* Ensure that this is definitely greater */
736                                                if($DEBUG) { echo"  This is the $num[$i] octet, so A is definitely greater than B."; }
737                                                $less = -5;
738                                                break;
739                                        }
740                                }
741                                else
742                                {
743                                        if($DEBUG) { echo ' no, they are equal or of different length.'; }
744                                        // makes sure eg. '1.0.0' is counted less the '1.0.0.xxx' !
745                                        $less = count($testa) < count($testb) ? 1 : 0;
746                                }
747                        }
748                        if($DEBUG) { echo '<br>Check value is: "'.$less.'"'; }
749                        if($less>0)
750                        {
751                                if($DEBUG) { echo '<br>A is less than B'; }
752                                return True;
753                        }
754                        elseif($less<0)
755                        {
756                                if($DEBUG) { echo '<br>A is greater than B'; }
757                                return False;
758                        }
759                        else
760                        {
761                                if($DEBUG) { echo '<br>A is equal to B'; }
762                                return False;
763                        }
764                }
765
766                /*!
767                @function amorethanb
768                @abstract phpgw version checking, is param 1 > param 2 in phpgw versionspeak?
769                @param  $a      phpgw version number to check if more than $b
770                @param  $b      phpgw version number to check $a against
771                #return True if $a < $b
772                */
773                function amorethanb($a,$b,$DEBUG=False)
774                {
775                        $num = array('1st','2nd','3rd','4th');
776
777                        if($DEBUG)
778                        {
779                                echo'<br>Input values: '
780                                        . 'A="'.$a.'", B="'.$b.'"';
781                        }
782                        $newa = str_replace('-','',str_replace('pre','.',$a));
783                        $newb = str_replace('-','',str_replace('pre','.',$b));
784                        $testa = explode('.',$newa);
785                        if($testa[3] == '')
786                        {
787                                $testa[3] = 0;
788                        }
789                        $testb = explode('.',$newb);
790                        if($testb[3] == '')
791                        {
792                                $testb[3] = 0;
793                        }
794                        $less = 0;
795
796            $testa_count = count($testa);
797                        for($i=0;$i<$testa_count;++$i)
798                        {
799                                if($DEBUG) { echo'<br>Checking if '. (int)$testa[$i] . ' is more than ' . (int)$testb[$i] . ' ...'; }
800                                if((int)$testa[$i] > (int)$testb[$i])
801                                {
802                                        if($DEBUG) { echo ' yes.'; }
803                                        ++$less;
804                                        if($i<3)
805                                        {
806                                                /* Ensure that this is definitely greater */
807                                                if($DEBUG) { echo"  This is the $num[$i] octet, so A is definitely greater than B."; }
808                                                $less = 5;
809                                                break;
810                                        }
811                                }
812                                elseif((int)$testa[$i] < (int)$testb[$i])
813                                {
814                                        if($DEBUG) { echo ' no.'; }
815                                        $less--;
816                                        if($i<2)
817                                        {
818                                                /* Ensure that this is definitely smaller */
819                                                if($DEBUG) { echo"  This is the $num[$i] octet, so A is definitely less than B."; }
820                                                $less = -5;
821                                                break;
822                                        }
823                                }
824                                else
825                                {
826                                        if($DEBUG) { echo ' no, they are equal.'; }
827                                        $less = 0;
828                                }
829                        }
830                        if($DEBUG) { echo '<br>Check value is: "'.$less.'"'; }
831                        if($less>0)
832                        {
833                                if($DEBUG) { echo '<br>A is greater than B'; }
834                                return True;
835                        }
836                        elseif($less<0)
837                        {
838                                if($DEBUG) { echo '<br>A is less than B'; }
839                                return False;
840                        }
841                        else
842                        {
843                                if($DEBUG) { echo '<br>A is equal to B'; }
844                                return False;
845                        }
846                }
847
848                function get_hooks_table_name()
849                {
850                        if(@$this->alessthanb($GLOBALS['setup_info']['phpgwapi']['currentver'],'0.9.8pre5') &&
851                           @$GLOBALS['setup_info']['phpgwapi']['currentver'] != '')
852                        {
853                                /* No phpgw_hooks table yet. */
854                                return False;
855                        }
856                        return 'phpgw_hooks';
857                }
858
859                function setup_account_object()
860                {
861                        if (!is_object($GLOBALS['phpgw']->accounts))
862                        {
863                                if (!is_object($this->db))
864                                {
865                                        $this->loaddb();
866                                }
867                                /* Load up some configured values */
868                                $this->db->query("SELECT config_name,config_value,config_app FROM phpgw_config "
869                                        . "WHERE config_app = 'phpgwapi' AND (config_name LIKE 'ldap%' OR config_name LIKE 'account_%' OR config_name LIKE '%encryption%')",__LINE__,__FILE__);
870                                while($this->db->next_record())
871                                {
872                                        $GLOBALS['phpgw_info']['server'][$this->db->f('config_name')] = $this->db->f('config_value');
873                                }
874                                if (!is_object($GLOBALS['phpgw']))
875                                {
876                                        $GLOBALS['phpgw'] = CreateObject('phpgwapi.phpgw');
877                                }
878                                copyobj($this->db,$GLOBALS['phpgw']->db);
879                                $GLOBALS['phpgw']->common      = CreateObject('phpgwapi.common');
880                                $GLOBALS['phpgw']->accounts    = CreateObject('phpgwapi.accounts');
881
882                                if(($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap') &&
883                                        !$GLOBALS['phpgw']->accounts->ds)
884                                {
885                                        printf("<b>Error: Error connecting to LDAP server %s!</b><br>",$GLOBALS['phpgw_info']['server']['ldap_host']);
886                                        exit;
887                                }
888                        }
889                }
890
891                /*!
892                @function add_account
893                @abstract add an user account or a user group
894                @param username string alphanumerical username or groupname (account_lid)
895                @param first, last string first / last name
896                @param $passwd string cleartext pw
897                @param $group string/boolean Groupname for users primary group or False for a group, default 'Default'
898                @param $changepw boolean user has right to change pw, default False
899                @returns the numerical user-id
900                @note if the $username already exists, only the id is returned, no new user / group gets created
901                */
902                function add_account($username,$first,$last,$passwd,$group='default',$changepw=False)
903                {
904                        $this->setup_account_object();
905
906                        $groupid = $group ? $GLOBALS['phpgw']->accounts->name2id($group) : False;
907
908                        if(!($accountid = $GLOBALS['phpgw']->accounts->name2id($username)))
909                        {
910                                $accountid = $accountid ? $accountid : $GLOBALS['phpgw']->accounts->create(array(
911                                        'account_type'      => $group ? 'u' : 'g',
912                                        'account_lid'       => $username,
913                                        'account_passwd'    => $passwd,
914                                        'account_firstname' => $first,
915                                        'account_lastname'  => $last,
916                                        'account_status'    => 'A',
917                                        'account_primary_group' => $groupid,
918                                        'account_expires'   => -1
919                                ));
920                        }
921                        $accountid = (int)$accountid;
922                        if($groupid)
923                        {
924                                $this->add_acl('phpgw_group',(int)$groupid,$accountid);
925                        }
926                        $this->add_acl('preferences','changepassword',$accountid,(int)$changepw);
927
928                        return $accountid;
929                }
930
931                /*!
932                @function add_acl
933                @abstract Add ACL rights
934                @param $app string/array with app-names
935                @param $locations string eg. run
936                @param $account int/string accountid or account_lid
937                @param $rights int rights to set, default 1
938                */
939                function add_acl($apps,$location,$account,$rights=1)
940                {
941                        if (!is_int($account))
942                        {
943                                $this->setup_account_object();
944                                $account = $GLOBALS['phpgw']->accounts->name2id($account);
945                        }
946                        $rights = (int)$rights;
947                        if(!is_object($this->db))
948                        {
949                                $this->loaddb();
950                        }
951
952                        if(!is_array($apps))
953                        {
954                                $apps = array($apps);
955                        }
956                        foreach($apps as $app)
957                        {
958                                $this->db->query("DELETE FROM phpgw_acl WHERE acl_appname='$app' AND acl_location='$location' AND acl_account=$account");
959                                if ($rights)
960                                {
961                                        $this->db->query("INSERT INTO phpgw_acl(acl_appname,acl_location,acl_account,acl_rights) VALUES('$app','$location',$account,$rights);");
962                                }
963                        }
964                        $this->db->query("END TRANSACTION; COMMIT;");
965                        if ($this->db->Error){
966                                echo "Error in acl update";
967                                exit;
968                        }
969                }
970        }
971?>
Note: See TracBrowser for help on using the repository browser.