bootstrap.php 5.55 KB
Newer Older
alban's avatar
alban committed
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13

// *****************************************************************************
// 
// Alternc bootstrapping                  
// bureau/class/config.php file is -not- test friendly
// @todo streamline test and prod 
// 
// *****************************************************************************

// Autoloading 
// ***********
$pathList                               = array_merge( array("."),explode(PATH_SEPARATOR,get_include_path()));
alban's avatar
alban committed
14
15
16
17
18
19
set_include_path(implode(PATH_SEPARATOR, $pathList));
require_once('AutoLoader.php');
AutoLoader::registerDirectory('lib');
AutoLoader::registerDirectory('../bureau/class');
AutoLoader::registerDirectory('.');

20
21
22
23
24
25
26
27
28
define('ALTERNC_PANEL',                 "../bureau"); // Custom
require_once ALTERNC_PANEL."/class/db_mysql.php";
require_once ALTERNC_PANEL."/class/functions.php";


// General variables setup
// *********************
if(is_readable('local.sh')){
    $configFile                         = file_get_contents('local.sh', 'r');
fufroma's avatar
fufroma committed
29
30
} else if(is_readable('local.sh_generic')){
    $configFile                         = file_get_contents('local.sh_generic', 'r');
31
32
33
34
35
36
37
38
39
40
41
42
} else {
    throw new Exception("You must provide a local.sh file", 1 );
}
$configFile                             = explode("\n",$configFile);
$compat                                 = array('DEFAULT_MX'   => 'MX',
    'MYSQL_USER'   => 'MYSQL_LOGIN',
    'MYSQL_PASS'   => 'MYSQL_PWD',
    'NS1_HOSTNAME' => 'NS1',
    'NS2_HOSTNAME' => 'NS2'
);
foreach ($configFile as $line) {
    if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
fufroma's avatar
fufroma committed
43
44
        //$GLOBALS['L_'.$matches[1]]      = $matches[2];
        eval('$L_'.$matches[1].' = $matches[2];'); # Ugly, but work with phpunit...
45
46
47
48
49
50
51
52
53
54
        if (isset($compat[$matches[1]])) {
            $GLOBALS['L_'.$compat[$matches[1]]]  =      $matches[2];
        }
    }
}


// Constants and globals
// ********************

fufroma's avatar
fufroma committed
55
56
57
58
// Define constants from vars of local.sh
define('ALTERNC_MAIL',              "$L_ALTERNC_MAIL");
define('ALTERNC_HTML',              "$L_ALTERNC_HTML");
define('ALTERNC_LOGS',              "$L_ALTERNC_LOGS");
59
60
61
62
63
64
65
66
67
68
69
70
if(isset($L_ALTERNC_LOGS_ARCHIVE)){
    define('ALTERNC_LOGS_ARCHIVE',      "$L_ALTERNC_LOGS_ARCHIVE");
}
define('ALTERNC_LOCALES',               ALTERNC_PANEL."/locales");
define('ALTERNC_LOCK_JOBS',             '/var/run/alternc/jobs-lock');
define('ALTERNC_LOCK_PANEL',            '/var/lib/alternc/panel/nologin.lock');
define('ALTERNC_APACHE2_GEN_TMPL_DIR',  '/etc/alternc/templates/apache2/');
define('ALTERNC_VHOST_DIR',             "/var/lib/alternc/apache-vhost/");
define('ALTERNC_VHOST_FILE',            ALTERNC_VHOST_DIR."vhosts_all.conf");
define('ALTERNC_VHOST_MANUALCONF',      ALTERNC_VHOST_DIR."manual/");
$root                                   = ALTERNC_PANEL."/";

fufroma's avatar
fufroma committed
71
72
73
74
75
76
// Create test directory
foreach (array(ALTERNC_MAIL, ALTERNC_HTML, ALTERNC_LOGS) as $crdir ) {
  if (! is_dir($crdir)) {
    mkdir($crdir, 0777, true);
  }
}
77
78
79

// Database variables setup
// ***********************
alban's avatar
alban committed
80
81
82
83
84
// Default values
$database                               = "alternc_test";
$user                                   = "root";
$password                               = "";
// Local override
85
if ( is_readable("my.cnf") ) {
alban's avatar
alban committed
86
    $mysqlConfigFile                      = file("my.cnf");
fufroma's avatar
fufroma committed
87
88
89
} else if(is_readable('my.cnf_generic')){
    $mysqlConfigFile                      = file('my.cnf_generic');

alban's avatar
alban committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    foreach ($mysqlConfigFile as $line) {
      if (preg_match('/^([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
          switch ($matches[1]) {
          case "user":
            $user                           = $matches[2];
          break;
          case "password":
            $password                       = $matches[2];
          break;
          case "database":
            $database                       = $matches[2];
          break;
        }
      }
      if (preg_match('/^#alternc_var ([A-Za-z0-9_]*) *= *"?(.*?)"?$/', trim($line), $matches)) {
        $$matches[1]                        = $matches[2];
      }
107
    }
alban's avatar
alban committed
108
} 
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153


/**
* Class for MySQL management in the bureau 
*
* This class heriting from the db class of the phplib manages
* the connection to the MySQL database.
*/
class DB_system extends DB_Sql {
    var $Host,$Database,$User,$Password;
    /**
        * Constructor
        */
    function DB_system($user,$database,$password) {
        global $L_MYSQL_HOST,$L_MYSQL_DATABASE,$L_MYSQL_LOGIN,$L_MYSQL_PWD;
        $this->Host                     = "127.0.0.1";
        $this->Database                 = $database;
        $this->User                     = $user;
        $this->Password                 = $password;
    }
}


// Creates database from schema 
// *********************************************

$queryList = array(
    "mysql -u $user --password='$password' -e 'DROP DATABASE IF EXISTS $database '",
    "mysql -u $user --password='$password' -e 'CREATE DATABASE $database'",
    "mysql -u $user --password='$password' $database < ".__DIR__."/../install/mysql.sql"
);
foreach ($queryList as $exec_command) {
    exec($exec_command,$output,$return_var);
    if(  $return_var){
        throw new \Exception("[!] Mysql exec error : $exec_command \n Error : \n ".print_r($output,1));
    }
}
$db                                     = new \DB_system($user,$database,$password);
$cuid                                   = 0;
$variables                              = new \m_variables();
$mem                                    = new \m_mem();
$err                                    = new \m_err();
$authip                                 = new \m_authip();
$hooks                                  = new \m_hooks();