Alternc  latest
Alternc logiel libre pour l'hébergement
bootstrap.php
Go to the documentation of this file.
1 <?php
2 // *****************************************************************************
3 //
4 // Alternc bootstrapping
5 // bureau/class/config.php file is -not- test friendly
6 // @todo streamline test and prod
7 //
8 // *****************************************************************************
9 
10 // Autoloading
11 // ***********
12 $pathList = array_merge( array("."),explode(PATH_SEPARATOR,get_include_path()));
13 set_include_path(implode(PATH_SEPARATOR, $pathList));
14 require_once('AutoLoader.php');
16 AutoLoader::registerDirectory('../bureau/class');
18 
19 define('ALTERNC_PANEL',realpath(__DIR__."/../bureau"));; // Custom
20 define('PHPUNIT_DATASETS_PATH',realpath(__DIR__."/tests/_datasets"));
21 require_once ALTERNC_PANEL."/class/db_mysql.php";
22 require_once ALTERNC_PANEL."/class/functions.php";
23 require_once ALTERNC_PANEL."/class/variables.php";
24 
25 // General variables setup
26 // *********************
27 if(is_readable('local.sh')){
28  $configFile = file_get_contents('local.sh');
29 } else if(is_readable('local.sh_generic')){
30  $configFile = file_get_contents('local.sh_generic');
31 } else {
32  throw new Exception("You must provide a local.sh file", 1 );
33 }
34 $configFile = explode("\n",$configFile);
35 $compat = array('DEFAULT_MX' => 'MX',
36  'MYSQL_USER' => 'MYSQL_LOGIN',
37  'MYSQL_PASS' => 'MYSQL_PWD',
38  'NS1_HOSTNAME' => 'NS1',
39  'NS2_HOSTNAME' => 'NS2'
40 );
41 foreach ($configFile as $line) {
42  if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
43  //$GLOBALS['L_'.$matches[1]] = $matches[2];
44  eval('$L_'.$matches[1].' = $matches[2];'); # Ugly, but work with phpunit...
45  if (isset($compat[$matches[1]])) {
46  $GLOBALS['L_'.$compat[$matches[1]]] = $matches[2];
47  }
48  }
49 }
50 
51 
52 // Constants and globals
53 // ********************
54 
55 // Define constants from vars of local.sh
56 if( !defined("ALTERNC_MAIL") ) { define('ALTERNC_MAIL', "$L_ALTERNC_MAIL"); };
57 if( !defined("ALTERNC_HTML") ) { define('ALTERNC_HTML', "$L_ALTERNC_HTML"); };
58 if( !defined("ALTERNC_LOGS") ) { define('ALTERNC_LOGS', "$L_ALTERNC_LOGS"); };
59 if(isset($L_ALTERNC_LOGS_ARCHIVE)){
60  define('ALTERNC_LOGS_ARCHIVE', "$L_ALTERNC_LOGS_ARCHIVE");
61 }
62 if( !defined("ALTERNC_LOCALES") ) { define('ALTERNC_LOCALES', ALTERNC_PANEL."/locales"); };
63 if( !defined("ALTERNC_LOCK_JOBS") ) { define('ALTERNC_LOCK_JOBS', '/run/alternc/jobs-lock'); };
64 if( !defined("ALTERNC_LOCK_PANEL") ) { define('ALTERNC_LOCK_PANEL', '/var/lib/alternc/panel/nologin.lock'); };
65 if( !defined("ALTERNC_APACHE2_GEN_TMPL_DIR") ) { define('ALTERNC_APACHE2_GEN_TMPL_DIR', '/etc/alternc/templates/apache2/'); };
66 if( !defined("ALTERNC_VHOST_DIR") ) { define('ALTERNC_VHOST_DIR', "/var/lib/alternc/apache-vhost/"); };
67 if( !defined("ALTERNC_VHOST_FILE") ) { define('ALTERNC_VHOST_FILE', ALTERNC_VHOST_DIR."vhosts_all.conf"); };
68 if( !defined("ALTERNC_VHOST_MANUALCONF") ) { define('ALTERNC_VHOST_MANUALCONF', ALTERNC_VHOST_DIR."manual/"); };
69 define("THROW_EXCEPTIONS", TRUE);
70 
72 
73 // Create test directory
74 foreach (array(ALTERNC_MAIL, ALTERNC_HTML, ALTERNC_LOGS) as $crdir ) {
75  if (! is_dir($crdir)) {
76  mkdir($crdir, 0777, true);
77  }
78 }
79 
80 // Database variables setup
81 // ***********************
82 // Default values
83 $database = "alternc_test";
84 $user = "root";
85 $password = "";
86 // Local override
87 if ( is_readable("my.cnf") ) {
88  $mysqlConfigFile = file("my.cnf");
89 } else if(is_readable('my.cnf_generic')){
90  $mysqlConfigFile = file('my.cnf_generic');
91 } else {
92  throw new Exception("You must provide a my.cnf file", 1 );
93 }
94 
95 foreach ($mysqlConfigFile as $line) {
96  if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
97  switch ($matches[1]) {
98  case "user":
99  $user = $matches[2];
100  break;
101  case "password":
102  $password = $matches[2];
103  break;
104  case "database":
105  $database = $matches[2];
106  break;
107  }
108  }
109  if (preg_match('/^#alternc_var ([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
110  $$matches[1] = $matches[2];
111  }
112 }
113 
114 
115 /**
116 * Class for MySQL management in the bureau
117 *
118 * This class heriting from the db class of the phplib manages
119 * the connection to the MySQL database.
120 */
121 class DB_system extends DB_Sql {
123  parent::__construct($database, '127.0.0.1', $user, $password);
124  }
125 }
126 
127 // Creates database from schema
128 // *********************************************
129 
130 echo "*** In progress: importing mysql.sql\n";
131 $queryList = array(
132  "mysql -u $user --password='$password' -e 'DROP DATABASE IF EXISTS $database '",
133  "mysql -u $user --password='$password' -e 'CREATE DATABASE $database'",
134  "mysql -u $user --password='$password' $database < ".__DIR__."/../install/mysql.sql"
135 );
136 foreach ($queryList as $exec_command) {
137  exec($exec_command,$output,$return_var);
138  if( $return_var){
139  throw new \Exception("[!] Mysql exec error : $exec_command \n Error : \n ".print_r($output,true));
140  }
141 }
142 echo "*** In progress: mysql.sql imported\n";
143 
144 $db = new \DB_system($database, $user, $password);
145 $cuid = 0;
146 $msg = new \m_messages();
147 $mem = new \m_mem();
148 $err = new \m_err();
149 $authip = new \m_authip();
150 $hooks = new \m_hooks();
151 $bro = new \m_bro();
$hooks
Definition: bootstrap.php:74
const ALTERNC_PANEL
Definition: bootstrap.php:14
const ALTERNC_LOGS
Definition: bootstrap.php:13
global $db
Definition: bootstrap.php:26
const ALTERNC_MAIL
Definition: bootstrap.php:9
$mem
Definition: bootstrap.php:71
$msg
Definition: bootstrap.php:75
$authip
Definition: bootstrap.php:73
if(isset($L_ALTERNC_LOGS_ARCHIVE)) define('ALTERNC_LOGS_ARCHIVE' $L_ALTERNC_LOGS_ARCHIVE
Definition: bootstrap.php:11
$root
Definition: bootstrap.php:20
$err
Definition: bootstrap.php:72
$cuid
Definition: bootstrap.php:43
const ALTERNC_HTML
Definition: bootstrap.php:10
static registerDirectory($dirName)
Store the filename (sans extension) & full path of all ".php" files found.
Definition: AutoLoader.php:9
Mysql Database class.
Definition: db_mysql.php:27
Class for MySQL management in the panel.
Definition: bootstrap.php:28
__construct($database, $user, $password)
Definition: bootstrap.php:122
const ALTERNC_VHOST_DIR
Definition: config.php:93
$pathList
Definition: bootstrap.php:12
foreach(array(ALTERNC_MAIL, ALTERNC_HTML, ALTERNC_LOGS) as $crdir) $database
Definition: bootstrap.php:83
$configFile
Definition: bootstrap.php:34
$queryList
Definition: bootstrap.php:131
$user
Definition: bootstrap.php:84
$password
Definition: bootstrap.php:85
$bro
Definition: bootstrap.php:151
$compat
Definition: bootstrap.php:35