Alternc  3.2
Alternc logiel libre pour l'h├ębergement
 All Data Structures Namespaces Files Functions Variables Pages
bootstrap.php
Go to the documentation of this file.
1 <?php
2 
3 // *****************************************************************************
4 //
5 // Alternc bootstrapping
6 // bureau/class/config.php file is -not- test friendly
7 // @todo streamline test and prod
8 //
9 // *****************************************************************************
10 
11 // Autoloading
12 // ***********
13 $pathList = array_merge( array("."),explode(PATH_SEPARATOR,get_include_path()));
14 set_include_path(implode(PATH_SEPARATOR, $pathList));
15 require_once('AutoLoader.php');
17 AutoLoader::registerDirectory('../bureau/class');
19 
20 define('ALTERNC_PANEL',realpath(__DIR__."/../bureau"));; // Custom
21 define('PHPUNIT_DATASETS_PATH',realpath(__DIR__."/tests/_datasets"));
22 require_once ALTERNC_PANEL."/class/db_mysql.php";
23 require_once ALTERNC_PANEL."/class/functions.php";
24 
25 
26 // General variables setup
27 // *********************
28 if(is_readable('local.sh')){
29  $configFile = file_get_contents('local.sh', 'r');
30 } else if(is_readable('local.sh_generic')){
31  $configFile = file_get_contents('local.sh_generic', 'r');
32 } else {
33  throw new Exception("You must provide a local.sh file", 1 );
34 }
35 $configFile = explode("\n",$configFile);
36 $compat = array('DEFAULT_MX' => 'MX',
37  'MYSQL_USER' => 'MYSQL_LOGIN',
38  'MYSQL_PASS' => 'MYSQL_PWD',
39  'NS1_HOSTNAME' => 'NS1',
40  'NS2_HOSTNAME' => 'NS2'
41 );
42 foreach ($configFile as $line) {
43  if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
44  //$GLOBALS['L_'.$matches[1]] = $matches[2];
45  eval('$L_'.$matches[1].' = $matches[2];'); # Ugly, but work with phpunit...
46  if (isset($compat[$matches[1]])) {
47  $GLOBALS['L_'.$compat[$matches[1]]] = $matches[2];
48  }
49  }
50 }
51 
52 
53 // Constants and globals
54 // ********************
55 
56 // Define constants from vars of local.sh
57 define('ALTERNC_MAIL', "$L_ALTERNC_MAIL");
58 define('ALTERNC_HTML', "$L_ALTERNC_HTML");
59 define('ALTERNC_LOGS', "$L_ALTERNC_LOGS");
61  define('ALTERNC_LOGS_ARCHIVE', "$L_ALTERNC_LOGS_ARCHIVE");
62 }
63 define('ALTERNC_LOCALES', ALTERNC_PANEL."/locales");
64 define('ALTERNC_LOCK_JOBS', '/var/run/alternc/jobs-lock');
65 define('ALTERNC_LOCK_PANEL', '/var/lib/alternc/panel/nologin.lock');
66 define('ALTERNC_APACHE2_GEN_TMPL_DIR', '/etc/alternc/templates/apache2/');
67 define('ALTERNC_VHOST_DIR', "/var/lib/alternc/apache-vhost/");
68 define('ALTERNC_VHOST_FILE', ALTERNC_VHOST_DIR."vhosts_all.conf");
69 define('ALTERNC_VHOST_MANUALCONF', ALTERNC_VHOST_DIR."manual/");
70 define("THROW_EXCEPTIONS", TRUE);
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 }
92 
93 foreach ($mysqlConfigFile as $line) {
94  if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
95  switch ($matches[1]) {
96  case "user":
97  $user = $matches[2];
98  break;
99  case "password":
100  $password = $matches[2];
101  break;
102  case "database":
103  $database = $matches[2];
104  break;
105  }
106  }
107  if (preg_match('/^#alternc_var ([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
108  $$matches[1] = $matches[2];
109  }
110 }
111 
112 
113 /**
114 * Class for MySQL management in the bureau
115 *
116 * This class heriting from the db class of the phplib manages
117 * the connection to the MySQL database.
118 */
119 class DB_system extends DB_Sql {
121  /**
122  * Constructor
123  */
125  global $L_MYSQL_HOST,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN,$L_MYSQL_PWD;
126  $this->Host = "127.0.0.1";
127  $this->Database = $database;
128  $this->User = $user;
129  $this->Password = $password;
130  }
131 }
132 
133 
134 // Creates database from schema
135 // *********************************************
136 
137 $queryList = array(
138  "mysql -u $user --password='$password' -e 'DROP DATABASE IF EXISTS $database '",
139  "mysql -u $user --password='$password' -e 'CREATE DATABASE $database'",
140  "mysql -u $user --password='$password' $database < ".__DIR__."/../install/mysql.sql"
141 );
142 foreach ($queryList as $exec_command) {
143  exec($exec_command,$output,$return_var);
144  if( $return_var){
145  throw new \Exception("[!] Mysql exec error : $exec_command \n Error : \n ".print_r($output,1));
146  }
147 }
148 $db = new \DB_system($user,$database,$password);
149 $db->connect();
150 $cuid = 0;
151 $variables = new \m_variables();
152 $mem = new \m_mem();
153 $err = new \m_err();
154 $authip = new \m_authip();
155 $hooks = new \m_hooks();
156