[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/setup/inc/ -> class.setup.inc.php (source)

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


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7