eGroupWare PHP Cross Reference Groupware Applications

Source: /setup/inc/class.setup_header.inc.php - 294 lines - 9178 bytes - Summary - Text - Print

Description: Setup - Manage the eGW config file header.inc.php

   1  <?php
   2  /**
   3   * Setup - Manage the eGW config file header.inc.php
   4   *
   5   * @link http://www.egroupware.org
   6   * @package setup
   7   * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
   8   * @author Miles Lott <milos@groupwhere.org>
   9   * @author Tony Puglisi (Angles)
  10   * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
  11   * @version $Id$
  12   */
  13  
  14  /**
  15   * Functions to manage the eGW config file header.inc.php
  16   *
  17   * Used by manageheader.php and the new setup command line interface setup-cli.php
  18   *
  19   * @package setup
  20   * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
  21   * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
  22   */
  23  class setup_header
  24  {
  25      /**
  26       * @var array with php-extension / ADOdb drive names => describtiv label
  27       */
  28      var $db_fullnames = array(
  29          'mysqli' => 'MySQLi (recommended, incl. transactions)',
  30          'mysql'  => 'MySQL (deprecated)',
  31          'mysqlt' => 'MySQL (deprecated, transactions)',
  32          'pgsql'  => 'PostgreSQL',
  33          'mssql'  => 'MS SQL Server',
  34          'odbc_mssql'  => 'MS SQL Server via ODBC',
  35          'oracle' => 'Oracle',
  36          'odbc_oracle' => 'Oracle via ODBC',
  37          'sapdb'  => 'SAP/Max DB via ODBC',
  38      );
  39  
  40      /**
  41       * @var array with php-extension / ADOdb drive names => default port used by database
  42       */
  43      var $default_db_ports = array(
  44          'pgsql'  => 5432,
  45          'mysql'  => 3306,
  46          'mysqli' => 3306,
  47          'mysqlt' => 3306,
  48          'mssql'  => 1433,
  49          'odbc_mssql'  => '',
  50          'oracle' => 1521,
  51          'odbc_oracle' => '',
  52          'sapdb'  => '',
  53      );
  54  
  55      /**
  56       * Detect settings or set defaults for the header.inc.php file (used if it does not yet exist)
  57       *
  58       * Sets $GLOBALS['egw_info'], $GLOBALS['egw_domains'] and the defines EGW_SERVER_ROOT and EGW_INCLUDE_ROOT,
  59       * as if the header has been included
  60       *
  61       * @param string $domain='default' domain to set
  62       */
  63  	function defaults($domain='default')
  64      {
  65          $egw_root = realpath(dirname(__FILE__).'/../..');
  66          $GLOBALS['egw_info']['server']['server_root'] = $GLOBALS['egw_info']['server']['include_root'] = $egw_root;
  67          define('EGW_SERVER_ROOT',$egw_root);    // this is usally already defined by setup and cant be changed
  68          define('EGW_INCLUDE_ROOT',$egw_root);
  69  
  70          $GLOBALS['egw_info']['server']['header_admin_user'] = 'admin';
  71          $GLOBALS['egw_info']['server']['header_admin_password'] = '';
  72          $GLOBALS['egw_info']['server']['setup_acl'] = '';
  73  
  74          if ($domain) $GLOBALS['egw_domain'][$domain] = $this->domain_defaults();
  75  
  76          $GLOBALS['egw_info']['server']['show_domain_selectbox'] = false;
  77          $GLOBALS['egw_info']['server']['db_persistent'] = True;
  78          $GLOBALS['egw_info']['login_template_set'] = 'idots';
  79          $GLOBALS['egw_info']['server']['mcrypt_enabled'] = False;
  80          $GLOBALS['egw_info']['server']['versions']['mcrypt'] = '';
  81          $GLOBALS['egw_info']['server']['mcrypt_iv'] = $this->generate_mcyrpt_iv();
  82      }
  83  
  84  	function domain_defaults($user='admin',$passwd='',$supported_db=null)
  85      {
  86          if (is_null($supported_db)) $supported_db = $this->check_db_support($null);
  87          $default_db = count($supported_db) ? $supported_db[0] : 'mysqli';
  88  
  89          return array(
  90              'db_host' => 'localhost',
  91              'db_port' => $this->default_db_ports[$default_db],
  92              'db_name' => 'egroupware',
  93              'db_user' => 'egroupware',
  94              'db_pass' => '',
  95              'db_type' => $default_db,
  96              'config_user'   => $user,
  97              'config_passwd' => $passwd,
  98          );
  99      }
 100  
 101      /**
 102       * Checks the values of the (included) header.inc.php file
 103       *
 104       * The values are set in $GLOBALS['egw_info'], $GLOBALS['egw_domain'] and EGW_SERVER_ROOT
 105       *
 106       * @return array with errors or null if no errors
 107       */
 108  	function validation_errors($path=EGW_SERVER_ROOT)
 109      {
 110          $errors = null;
 111  
 112          if (!is_dir($path) || !is_readable($path) || !is_dir($path.'/phpgwapi'))
 113          {
 114              $errors[] = lang("%1 '%2' does NOT exist, is not readable by the webserver or contains no eGroupWare installation!",lang('Server root'),$path);
 115          }
 116          if(!$GLOBALS['egw_info']['server']['header_admin_password'])
 117          {
 118              $errors[] = lang("You didn't enter a header admin password");
 119          }
 120          if(!$GLOBALS['egw_info']['server']['header_admin_user'])
 121          {
 122              $errors[] = lang("You didn't enter a header admin username");
 123          }
 124          if (!is_array($GLOBALS['egw_domain']) || !count($GLOBALS['egw_domain']))
 125          {
 126              $errors[] = lang('You need to add at least one eGroupWare domain / database instance.');
 127          }
 128          else
 129          {
 130              foreach($GLOBALS['egw_domain'] as $domain => $data)
 131              {
 132                  if (!$data['config_passwd'])
 133                  {
 134                      $errors[] = lang("You didn't enter a config password for domain %1",$domain);
 135                  }
 136                  if(!$data['config_user'])
 137                  {
 138                      $errors[] = lang("You didn't enter a config username for domain %1",$domain);
 139                  }
 140              }
 141          }
 142          return $errors;
 143      }
 144  
 145      /**
 146       * generate header.inc.php file from given values
 147       *
 148       * setup_header::generate($GLOBALS['egw_info'],$GLOBALS['egw_domains'])
 149       * should write an identical header.inc.php as the one include
 150       *
 151       * @param array $egw_info usual content (in server key) plus keys server_root and include_root
 152       * @param array $egw_domains info about the existing eGW domains / DB instances
 153       * @return string content of header.inc.php
 154       */
 155  	function generate($egw_info,$egw_domain)
 156      {
 157          $tpl = new Template('../', 'keep');    // 'keep' to not loose '{hash}' prefix of password-hashes!
 158          $tpl->set_file(array('header' => 'header.inc.php.template'));
 159          $tpl->set_block('header','domain','domain');
 160  
 161          auth::passwdhashes($most_secure_pw_hash);
 162  
 163          foreach($egw_domain as $domain => $data)
 164          {
 165              $var = array('DB_DOMAIN' => $domain);
 166              foreach($data as $name => $value)
 167              {
 168                  if ($name == 'db_port' && !$value) $value = $this->default_db_ports[$data['db_type']];
 169                  if ($name == 'config_passwd')
 170                  {
 171                      $var['CONFIG_PASS'] = self::is_hashed($value) ? $value : auth::encrypt_sql($value, $most_secure_pw_hash);
 172                  }
 173                  else
 174                  {
 175                      $var[strtoupper($name)] = addslashes($value);
 176                  }
 177              }
 178              $tpl->set_var($var);
 179              $tpl->parse('domains','domain',True);
 180          }
 181          $tpl->set_var('domain','');
 182  
 183          $var = Array();
 184          foreach($egw_info['server'] as $name => $value)
 185          {
 186              if ($name == 'header_admin_password' && $value && !self::is_hashed($value)) $value = auth::encrypt_sql($value, $most_secure_pw_hash);
 187              if ($name == 'versions')
 188              {
 189                  $name = 'mcrypt_version';
 190                  $value = $value['mcrypt'];
 191              }
 192              static $bools = array(
 193                  'mcrypt_enabled' => 'ENABLE_MCRYPT',
 194                  'db_persistent'  => 'db_persistent',
 195                  'show_domain_selectbox' => 'DOMAIN_SELECTBOX',
 196              );
 197              if (isset($bools[$name]))
 198              {
 199                  $name = $bools[$name];
 200                  $value = $value ? 'true' : 'false';
 201              }
 202              $var[strtoupper($name)] = addslashes($value);
 203          }
 204          $tpl->set_var($var);
 205  
 206          return $tpl->parse('out','header');
 207      }
 208  
 209      /**
 210       * Generate a random mcrypt_iv vector
 211       *
 212       * @return string
 213       */
 214  	function generate_mcyrpt_iv()
 215      {
 216          /*$mcrypt = mcrypt_module_open(egw_session::MCRYPT_ALGO, '', egw_session::MCRYPT_MODE, '');
 217          $size = mcrypt_enc_get_iv_size($mcrypt);
 218          if (function_exists('mcrypt_create_iv'))    // PHP 5.3+
 219          {
 220              $iv = mcrypt_create_iv($size, MCRYPT_DEV_URANDOM);
 221              error_log(__METHOD__."() size=$size returning ".array2string($iv));
 222              return $iv;
 223          }*/
 224          $size = 30;
 225          srand((double)microtime()*1000000);
 226          $random_char = array(
 227              '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',
 228              'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
 229              'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
 230              'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
 231          );
 232  
 233          $iv = '';
 234          for($i=0; $i < $size; $i++)
 235          {
 236              $iv .= $random_char[rand(1,count($random_char))];
 237          }
 238          //error_log(__METHOD__."() size=$size returning ".array2string($iv));
 239          return $iv;
 240      }
 241  
 242  	function check_db_support(&$detected)
 243      {
 244          $supported_db = $detected = array();
 245          foreach(array(
 246              // short => array(extension,func_to_check,supported_db(s))
 247              'mysqli' => array('mysql','mysqli_connect','mysqli'),
 248              'mysql'  => array('mysql','mysql_connect','mysql'),
 249              'mysqlt' => array('mysql','mysql_connect','mysqlt'),
 250              'pgsql'  => array('pgsql','pg_connect','pgsql'),
 251              'mssql'  => array('mssql','mssql_connect','mssql'),
 252              'odbc'   => array('odbc',false,'sapdb','odbc_mssql','odbc_oracle'),
 253              'oracle' => array('oci8',false,'oracle'),
 254          ) as $db => $data)
 255          {
 256              $ext = array_shift($data);
 257              $func_to_check = array_shift($data);
 258              $name = isset($this->db_fullnames[$db]) ? $this->db_fullnames[$db] : strtoupper($db);
 259              if (check_load_extension($ext) || $func_to_check && function_exists($func_to_check))
 260              {
 261                  $detected[] = lang('You appear to have %1 support.',$name);
 262                  $supported_db = array_merge($supported_db,$data);
 263              }
 264              else
 265              {
 266                  $detected[] .= lang('No %1 support found. Disabling',$name);
 267              }
 268          }
 269          return $supported_db;
 270      }
 271  
 272      /**
 273       * Check if pw is hashed
 274       *
 275       * @param string $pw
 276       * @return boolean
 277       */
 278  	static function is_hashed($pw)
 279      {
 280          $ret = $pw[0] == '{' || preg_match('/^[0-9a-f]{32}$/', $pw);
 281          //error_log(__METHOD__."('$pw') returning ".array2string($ret));
 282          return $ret;
 283      }
 284  }
 285  
 286  // some constanst for pre php4.3
 287  if (!defined('PHP_SHLIB_SUFFIX'))
 288  {
 289      define('PHP_SHLIB_SUFFIX',strtoupper(substr(PHP_OS, 0,3)) == 'WIN' ? 'dll' : 'so');
 290  }
 291  if (!defined('PHP_SHLIB_PREFIX'))
 292  {
 293      define('PHP_SHLIB_PREFIX',PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : '');
 294  }

title

Description

title

Description

title

Description

title

title

Body