Marius Cramer
2014-09-26 132081c0e69f0313ccb0d23637499e940d29f470
commit | author | age
87a4a6 1 <?php
436ed8 2
87a4a6 3 /*
436ed8 4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
87a4a6 5 All rights reserved.
T 6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
8cf78b 31 //* Set timezone
T 32 if(isset($conf['timezone']) && $conf['timezone'] != '') date_default_timezone_set($conf['timezone']);
33
87a4a6 34 class app {
7fe908 35
06b7fa 36     var $loaded_modules = array();
J 37     var $loaded_plugins = array();
132081 38     var $_calling_script = '';
7fe908 39
1d8f7f 40     function __construct() {
87a4a6 41
06b7fa 42         global $conf;
J 43
7fe908 44         if($conf['start_db'] == true) {
MC 45             $this->load('db_'.$conf['db_type']);
46             $this->db = new db;
47
48             /*
49                     Initialize the connection to the master DB,
50                     if we are in a multiserver setup
51                     */
52
53             if($conf['dbmaster_host'] != '' && ($conf['dbmaster_host'] != $conf['db_host'] || ($conf['dbmaster_host'] == $conf['db_host'] && $conf['dbmaster_database'] != $conf['db_database']))) {
54                 $this->dbmaster = new db($conf['dbmaster_host'], $conf['dbmaster_user'], $conf['dbmaster_password'], $conf['dbmaster_database']);
55             } else {
56                 $this->dbmaster = $this->db;
57             }
58
59
60         }
61
62     }
63
132081 64     function setCaller($caller) {
MC 65         $this->_calling_script = $caller;
66     }
67     
68     function getCaller() {
69         return $this->_calling_script;
70     }
71     
72     function forceErrorExit($errmsg = 'undefined') {
73         global $conf;
74         
75         if($this->_calling_script == 'server') {
76             @unlink($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock');
77         }
78         die('Exiting because of error: ' . $errmsg);
79     }
80
7fe908 81     function uses($classes) {
MC 82
83         global $conf;
84
85         $cl = explode(',', $classes);
06b7fa 86         if(is_array($cl)) {
J 87             foreach($cl as $classname) {
88                 if(!@is_object($this->$classname)) {
0ea2a5 89                     if(is_file($conf['classpath'].'/'.$classname.'.inc.php') && (DEVSYSTEM ||  !is_link($conf['classpath'].'/'.$classname.'.inc.php'))) {
7fe908 90                         include_once $conf['classpath'].'/'.$classname.'.inc.php';
06b7fa 91                         $this->$classname = new $classname;
5f9759 92                     }
T 93                 }
94             }
06b7fa 95         }
7fe908 96     }
87a4a6 97
7fe908 98     function load($classes) {
87a4a6 99
06b7fa 100         global $conf;
J 101
7fe908 102         $cl = explode(',', $classes);
06b7fa 103         if(is_array($cl)) {
J 104             foreach($cl as $classname) {
0ea2a5 105                 if(is_file($conf['classpath'].'/'.$classname.'.inc.php') && (DEVSYSTEM || !is_link($conf['classpath'].'/'.$classname.'.inc.php'))) {
7fe908 106                     include_once $conf['classpath'].'/'.$classname.'.inc.php';
06b7fa 107                 } else {
J 108                     die('Unable to load: '.$conf['classpath'].'/'.$classname.'.inc.php');
32b40d 109                 }
T 110             }
06b7fa 111         }
7fe908 112     }
87a4a6 113
7fe908 114     /*
87a4a6 115          0 = DEBUG
T 116          1 = WARNING
117          2 = ERROR
118         */
119
7fe908 120     function log($msg, $priority = 0) {
MC 121
06b7fa 122         global $conf;
7fe908 123
615a0a 124         switch ($priority) {
7fe908 125         case 0:
MC 126             $priority_txt = 'DEBUG';
127             break;
128         case 1:
129             $priority_txt = 'WARNING';
130             break;
131         case 2:
132             $priority_txt = 'ERROR';
133             break;
615a0a 134         }
T 135         $log_msg = @date('d.m.Y-H:i').' - '.$priority_txt.' - '. $msg;
06b7fa 136
J 137         if($priority >= $conf['log_priority']) {
7fe908 138             //if (is_writable($conf["log_file"])) {
MC 139             if (!$fp = fopen($conf['log_file'], 'a')) {
140                 die('Unable to open logfile.');
141             }
9eff6c 142
7fe908 143             if (!fwrite($fp, $log_msg."\r\n")) {
MC 144                 die('Unable to write to logfile.');
145             }
146
147             echo $log_msg."\n";
148             fclose($fp);
149
150             // Log to database
151             if(isset($this->dbmaster)) {
152                 $server_id = $conf['server_id'];
153                 $loglevel = $priority;
154                 $tstamp = time();
155                 $message = $this->dbmaster->quote($msg);
156                 $datalog_id = (isset($this->modules->current_datalog_id) && $this->modules->current_datalog_id > 0)?$this->modules->current_datalog_id:0;
157                 if($datalog_id > 0) {
158                     $tmp_rec = $this->dbmaster->queryOneRecord("SELECT count(syslog_id) as number FROM sys_log WHERE datalog_id = $datalog_id AND loglevel = ".LOGLEVEL_ERROR);
159                     //* Do not insert duplicate errors into the web log.
160                     if($tmp_rec['number'] == 0) {
161                         $sql = "INSERT INTO sys_log (server_id,datalog_id,loglevel,tstamp,message) VALUES ('$server_id',$datalog_id,'$loglevel','$tstamp','$message')";
162                         $this->dbmaster->query($sql);
06b7fa 163                     }
7fe908 164                 } else {
MC 165                     $sql = "INSERT INTO sys_log (server_id,datalog_id,loglevel,tstamp,message) VALUES ('$server_id',0,'$loglevel','$tstamp','$message')";
166                     $this->dbmaster->query($sql);
615a0a 167                 }
7fe908 168             }
87a4a6 169
7fe908 170             //} else {
MC 171             //    die("Unable to write to logfile.");
172             //}
173
174
175         } // if
176
177         if(isset($conf['admin_notify_priority']) && $priority >= $conf['admin_notify_priority'] && $conf['admin_mail'] != '') {
178             // send notification to admin
179             $mailBody = $log_msg;
180             $mailSubject = substr($log_msg, 0, 50).'...';
181             $mailHeaders      = "MIME-Version: 1.0" . "\n";
182             $mailHeaders     .= "Content-type: text/plain; charset=utf-8" . "\n";
183             $mailHeaders     .= "Content-Transfer-Encoding: 8bit" . "\n";
184             $mailHeaders     .= "From: ". $conf['admin_mail'] . "\n";
185             $mailHeaders     .= "Reply-To: ". $conf['admin_mail'] . "\n";
186
187             mail($conf['admin_mail'], $mailSubject, $mailBody, $mailHeaders);
188         }
189     } // func
190
191
192     /*
87a4a6 193          0 = DEBUG
T 194          1 = WARNING
195          2 = ERROR
196         */
197
7fe908 198     function error($msg) {
MC 199         $this->log($msg, 3);
06b7fa 200         die($msg);
7fe908 201     }
87a4a6 202
T 203 }
204
205 /*
206  Initialize application (app) object
207 */
208
209 $app = new app;
210
5f027c 211 ?>