nveid
2011-12-07 f5b0ca26b34388a108f8fe1c77f3ef1c99829bfa
commit | author | age
6fa2f1 1 <?php
T 2
3 /*
44d2a7 4 Copyright (c) 2007 - 2009, Till Brehm, projektfarm Gmbh
6fa2f1 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
31 /*
32     Application Class
33 */
34
35 ob_start('ob_gzhandler');
36
37 class app {
38
39     private $_language_inc = 0;
40     private $_wb;
41     private $_loaded_classes = array();
42     private $_conf;
43
ae3a8a 44     public function __construct() {
6fa2f1 45         global $conf;
ae3a8a 46
6fa2f1 47         if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS']) || isset($_REQUEST['s']) || isset($_REQUEST['s_old']) || isset($_REQUEST['conf'])) {
T 48             die('Internal Error: var override attempt detected');
49         }
ae3a8a 50
6fa2f1 51         $this->_conf = $conf;
T 52         if($this->_conf['start_db'] == true) {
53             $this->load('db_'.$this->_conf['db_type']);
54             $this->db = new db;
55         }
ae3a8a 56
6fa2f1 57         //* Start the session
T 58         if($this->_conf['start_session'] == true) {
ae81a2 59             
T 60             $this->uses('session');
61             session_set_save_handler(    array($this->session, 'open'),
62                                         array($this->session, 'close'),
63                                         array($this->session, 'read'),
64                                         array($this->session, 'write'),
65                                         array($this->session, 'destroy'),
66                                         array($this->session, 'gc'));
67             
6fa2f1 68             session_start();
ae3a8a 69
6fa2f1 70             //* Initialize session variables
T 71             if(!isset($_SESSION['s']['id']) ) $_SESSION['s']['id'] = session_id();
72             if(empty($_SESSION['s']['theme'])) $_SESSION['s']['theme'] = $conf['theme'];
73             if(empty($_SESSION['s']['language'])) $_SESSION['s']['language'] = $conf['language'];
74         }
ae3a8a 75
5bbfc1 76         $this->uses('auth,plugin,functions');
6fa2f1 77     }
b55e2b 78     
T 79     public function __destruct() {
80         session_write_close();
81     }
6fa2f1 82
ae3a8a 83     public function uses($classes) {
V 84         $cl = explode(',', $classes);
6fa2f1 85         if(is_array($cl)) {
ae3a8a 86             foreach($cl as $classname) {
6fa2f1 87                 $classname = trim($classname);
ae3a8a 88                 //* Class is not loaded so load it
V 89                 if(!array_key_exists($classname, $this->_loaded_classes)) {
6fa2f1 90                     include_once(ISPC_CLASS_PATH."/$classname.inc.php");
T 91                     $this->$classname = new $classname();
92                     $this->_loaded_classes[$classname] = true;
93                 }
94             }
95         }
96     }
97
ae3a8a 98     public function load($files) {
6fa2f1 99         $fl = explode(',', $files);
T 100         if(is_array($fl)) {
ae3a8a 101             foreach($fl as $file) {
6fa2f1 102                 $file = trim($file);
T 103                 include_once(ISPC_CLASS_PATH."/$file.inc.php");
104             }
105         }
106     }
107
108     /** Priority values are: 0 = DEBUG, 1 = WARNING,  2 = ERROR */
ae3a8a 109     public function log($msg, $priority = 0) {
da1da4 110         global $conf;
6fa2f1 111         if($priority >= $this->_conf['log_priority']) {
da1da4 112             // $server_id = $conf["server_id"];
T 113             $server_id = 0;
114             $priority = intval($priority);
115             $tstamp = time();
116             $msg = $this->db->quote('[INTERFACE]: '.$msg);
117             $this->db->query("INSERT INTO sys_log (server_id,datalog_id,loglevel,tstamp,message) VALUES ($server_id,0,$priority,$tstamp,'$msg')");
118             /*
6fa2f1 119             if (is_writable($this->_conf['log_file'])) {
T 120                 if (!$fp = fopen ($this->_conf['log_file'], 'a')) {
121                     $this->error('Unable to open logfile.');
122                 }
123                 if (!fwrite($fp, date('d.m.Y-H:i').' - '. $msg."\r\n")) {
124                     $this->error('Unable to write to logfile.');
125                 }
126                 fclose($fp);
127             } else {
128                 $this->error('Unable to write to logfile.');
129             }
da1da4 130             */
ae3a8a 131         }
V 132     }
6fa2f1 133
ae3a8a 134     /** Priority values are: 0 = DEBUG, 1 = WARNING,  2 = ERROR */
V 135     public function error($msg, $next_link = '', $stop = true, $priority = 1) {
6fa2f1 136         //$this->uses("error");
T 137         //$this->error->message($msg, $priority);
ae3a8a 138         if($stop == true) {
903ede 139             /*
V 140              * We always have a error. So it is better not to use any more objects like
141              * the template or so, because we don't know why the error occours (it could be, that
142              * the error occours in one of these objects..)
143              */
144             /*
145              * Use the template inside the user-template - Path. If it is not found, fallback to the
146              * default-template (the "normal" behaviour of all template - files)
147              */
148             if (file_exists(dirname(__FILE__) . '/../web/themes/' . $_SESSION['s']['theme'] . '/templates/error.tpl.htm')) {
149                 $content = file_get_contents(dirname(__FILE__) . '/../web/themes/' . $_SESSION['s']['theme'] . '/templates/error.tpl.htm');
150             } else {
151                 $content = file_get_contents(dirname(__FILE__) . '/../web/themes/default/templates/error.tpl.htm');
152             }
6fa2f1 153             if($next_link != '') $msg .= '<a href="'.$next_link.'">Next</a>';
ae3a8a 154             $content = str_replace('###ERRORMSG###', $msg, $content);
V 155             die($content);
6fa2f1 156         } else {
T 157             echo $msg;
158             if($next_link != '') echo "<a href='$next_link'>Next</a>";
159         }
160     }
161
ae3a8a 162     /** Translates strings in current language */
V 163     public function lng($text) {
c161ea 164         global $conf;
6fa2f1 165         if($this->_language_inc != 1) {
e83dd1 166             $language = (isset($_SESSION['s']['language']))?$_SESSION['s']['language']:$conf['language'];
2eff06 167             //* loading global Wordbook
e83dd1 168             $this->load_language_file('lib/lang/'.$language.'.lng');
2eff06 169             //* Load module wordbook, if it exists
e83dd1 170             if(isset($_SESSION['s']['module']['name'])) {
T 171                 $lng_file = 'web/'.$_SESSION['s']['module']['name'].'/lib/lang/'.$language.'.lng';
1ca823 172                 if(!file_exists(ISPC_ROOT_PATH.'/'.$lng_file)) $lng_file = '/web/'.$_SESSION['s']['module']['name'].'/lib/lang/en.lng';
44d2a7 173                 $this->load_language_file($lng_file);
6fa2f1 174             }
T 175             $this->_language_inc = 1;
ae3a8a 176         }
6fa2f1 177         if(!empty($this->_wb[$text])) {
T 178             $text = $this->_wb[$text];
ef3719 179         } else {
T 180             if($this->_conf['debug_language']) {
181                 $text = '#'.$text.'#';
182             }
6fa2f1 183         }
T 184         return $text;
185     }
ae3a8a 186
44d2a7 187     //** Helper function to load the language files.
T 188     public function load_language_file($filename) {
189         $filename = ISPC_ROOT_PATH.'/'.$filename;
190         if(substr($filename,-4) != '.lng') $this->error('Language file has wrong extension.');
191         if(file_exists($filename)) {
1ca823 192             @include($filename);
44d2a7 193             if(is_array($wb)) {
T 194                 if(is_array($this->_wb)) {
40dd9f 195                     $this->_wb = array_merge($this->_wb,$wb);
44d2a7 196                 } else {
T 197                     $this->_wb = $wb;
198                 }
199             }
200         }
201     }
6fa2f1 202
ae3a8a 203     public function tpl_defaults() {
6fa2f1 204         $this->tpl->setVar('app_title', $this->_conf['app_title']);
b09c9a 205         if(isset($_SESSION['s']['user'])) {
T 206             $this->tpl->setVar('app_version', $this->_conf['app_version']);
207         } else {
208             $this->tpl->setVar('app_version', '');
209         }
6fa2f1 210         $this->tpl->setVar('app_link', $this->_conf['app_link']);
ae3a8a 211         if(isset($this->_conf['app_logo']) && $this->_conf['app_logo'] != '' && @is_file($this->_conf['app_logo'])) {
6fa2f1 212             $this->tpl->setVar('app_logo', '<img src="'.$this->_conf['app_logo'].'">');
T 213         } else {
214             $this->tpl->setVar('app_logo', '&nbsp;');
215         }
216
217         $this->tpl->setVar('phpsessid', session_id());
218
219         $this->tpl->setVar('theme', $_SESSION['s']['theme']);
220         $this->tpl->setVar('html_content_encoding', $this->_conf['html_content_encoding']);
221
222         $this->tpl->setVar('delete_confirmation', $this->lng('delete_confirmation'));
ae3a8a 223         //print_r($_SESSION);
6fa2f1 224         if(isset($_SESSION['s']['module']['name'])) {
T 225             $this->tpl->setVar('app_module', $_SESSION['s']['module']['name']);
226         }
227         if(isset($_SESSION['s']['user']) && $_SESSION['s']['user']['typ'] == 'admin') {
228             $this->tpl->setVar('is_admin', 1);
229         }
230         if(isset($_SESSION['s']['user']) && $this->auth->has_clients($_SESSION['s']['user']['userid'])) {
231             $this->tpl->setVar('is_reseller', 1);
232         }
955391 233         /* Show username */
V 234         if(isset($_SESSION['s']['user'])) {
235             $this->tpl->setVar('cpuser', $_SESSION['s']['user']['username']);
236         }
ae3a8a 237     }
V 238
6fa2f1 239 } // end class
T 240
241 //** Initialize application (app) object
242 //* possible future =  new app($conf);
243 $app = new app();
244
f5b0ca 245 ?>