source: branches/1.4/libs/Initialize.php @ 1908

Revision 1908, 8.9 KB checked in by shibuya246, 3 years ago (diff)

[Branch 1.4] multi-site check on initialize. checks siteid=1's settings table for presence of MULTI_SITE constant. If not found do nothing. if false do nothing. If true, check which site to show.

Line 
1<?php
2/**
3 * Initialize Hotaru
4 *
5 * PHP version 5
6 *
7 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
18 *
19 * @category  Content Management System
20 * @package   HotaruCMS
21 * @author    Nick Ramsay <admin@hotarucms.org>
22 * @copyright Copyright (c) 2010, Hotaru CMS
23 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
24 * @link      http://www.hotarucms.org/
25 */
26class Initialize
27{
28        protected $db;                          // database object
29        protected $cage;                        // Inspekt object
30        protected $isDebug          = false;    // show db queries and page loading time
31       
32       
33        /**
34         * Initialize Hotaru with the essentials
35         */
36        public function __construct($h)
37        {
38           
39                // session to be used by CSRF, etc.
40                if (!isset($_SESSION['HotaruCMS'])) {
41                        session_start();
42                        $_SESSION['HotaruCMS'] = time();
43                }
44               
45                // The order here is important!
46                $this->setDefaultTimezone();
47                $this->setTableConstants();                             
48               
49                $this->getFiles();
50                $this->cage = $this->initInspektCage();
51                $this->db = $this->initDatabase();
52
53                $this->getCurrentSiteID();
54                $this->errorReporting();
55
56                $this->readSettings();
57                $this->setUpDatabaseCache();
58                $this->isDebug = $this->checkDebug();
59               
60                $this->setUpJsConstants();
61               
62                return $this;
63        }
64       
65       
66        /**
67         * Access modifier to set protected properties
68         */
69        public function __set($var, $val)
70        {
71                $this->$var = $val; 
72        }
73       
74       
75        /**
76         * Access modifier to get protected properties
77         */
78        public function __get($var)
79        {
80                return $this->$var;
81        }
82       
83       
84        /**
85         * Error reporting
86         */
87        public function errorReporting()
88        {
89                // display errors
90                ini_set('display_errors', 1); // Gets disabled later in checkDebug()
91                error_reporting(E_ALL);
92               
93                // error log filename
94                $filename = CACHE . 'debug_logs/error_log.php';
95               
96                // delete file if over 500KB
97                if (file_exists($filename) && (filesize($filename) > 500000)) {
98                        unlink($filename);
99                }
100               
101                // If doesn't exist, create a new file with die() at the top
102                if (!file_exists($filename)) {
103                        $fh = fopen($filename, 'w') or die("Sorry, I can't open cache/debug_logs/error_log.php");
104                        fwrite($fh, "<?php die(); ?>\r\n");
105                        fclose($fh);
106                }
107               
108                // point PHP to our error log
109                ini_set('error_log', $filename);
110        }
111
112
113        /**
114         * Table Constants
115         */
116        public function setTableConstants()
117        {
118                // define database tables
119                $tableConstants = array(
120                        "TABLE_BLOCKED" => "blocked",
121                        "TABLE_CATEGORIES"=>"categories",
122                        "TABLE_COMMENTS"=>"comments",
123                        "TABLE_COMMENTVOTES"=>"commentvotes",
124                        "TABLE_FRIENDS"=>"friends",
125                        "TABLE_MESSAGING"=>"messaging",
126                        "TABLE_MISCDATA"=>"miscdata",
127                        "TABLE_PLUGINS"=>"plugins",
128                        "TABLE_PLUGINHOOKS"=>"pluginhooks",
129                        "TABLE_PLUGINSETTINGS"=>"pluginsettings",
130                        "TABLE_POSTS"=>"posts",
131                        "TABLE_POSTMETA"=>"postmeta",
132                        "TABLE_POSTVOTES"=>"postvotes",
133                        "TABLE_SETTINGS"=>"settings",
134                        "TABLE_SITE"=>"site",
135                        "TABLE_TAGS"=>"tags",
136                        "TABLE_TEMPDATA"=>"tempdata",
137                        "TABLE_USERS"=>"users",
138                        "TABLE_USERMETA"=>"usermeta",
139                        "TABLE_USERACTIVITY"=>"useractivity",
140                        "TABLE_WIDGETS"=>"widgets"
141                );
142
143                foreach ( $tableConstants as $key => $value ) {
144                        if (!defined($key)) {
145                                define($key, DB_PREFIX . $value);
146                        }
147                }
148        }
149       
150        /**
151         * Sets the current SiteID if multiple sites.
152         */
153        public function getCurrentSiteID()
154        {
155                // read settings for default siteid=1 first to check whether MULTISITE is TRUE
156                $sql = "SELECT settings_value FROM " . TABLE_SETTINGS . " WHERE settings_name = %s AND settings_siteid = %d";
157                $multi_site = $this->db->get_var($this->db->prepare($sql, 'MULTI_SITE', 1));
158
159                if ($multi_site == 'true') {           
160                    $url =  $this->cage->server->getRaw('HTTP_HOST');   // wanted to use sanitizeTags
161                    $sql = "SELECT site_id, site_adminuser_id FROM " . TABLE_SITE . " WHERE site_url = %s";
162                    $settings = $this->db->get_row($this->db->prepare($sql, $url));             
163
164                    if ($settings) {
165                        $siteid = $settings->site_id;
166                        $siteurl = "http://" . $url;
167                    } else {
168                        $siteid = 1;
169                        $siteurl = BASEURL;
170                    }
171
172                } else {
173                    $siteid = 1;
174                    $siteurl = BASEURL;
175                }
176
177                if (!defined('SITEID')) { define('SITEID', $siteid); }
178                if (!defined('SITEURL')) { define("SITEURL", $siteurl . "/"); }
179               
180                if (!defined('CACHE')) {
181
182                    define("CACHE", BASE . "cache/" . $siteid . "/");
183
184                    $dirs = array('', 'debug_logs/' , 'db_cache/', 'css_js_cache/', 'html_cache/', 'rss_cache/', 'lang_cache/');  // first array item is needed to create the SITEID base folder
185
186                    foreach ($dirs as $dir) {
187                        if (!is_dir(CACHE . $dir)) {
188                            mkdir(CACHE . $dir);
189                        }
190                    }
191                   
192                }
193
194                return false;
195        }
196
197        /**
198         * Set the timezone
199         */
200        public function setDefaultTimezone()
201        {
202                // set timezone
203                $version = explode('.', phpversion());
204                if($version[0] > 4){
205                        $tmz = date_default_timezone_get();
206                        date_default_timezone_set($tmz);
207                }
208        }
209       
210       
211        /**
212         * Include necessary files
213         */
214        public function getFiles()
215        {
216                // include third party libraries
217                require_once(EXTENSIONS . 'csrf/csrf_class.php'); // protection against CSRF attacks
218                require_once(EXTENSIONS . 'Inspekt/Inspekt.php'); // sanitation
219                require_once(EXTENSIONS . 'ezSQL/ez_sql_core.php'); // database
220                require_once(EXTENSIONS . 'ezSQL/mysql/ez_sql_mysql.php'); // database
221               
222                // include functions
223                require_once(FUNCTIONS . 'funcs.strings.php');
224                require_once(FUNCTIONS . 'funcs.arrays.php');
225                require_once(FUNCTIONS . 'funcs.times.php');
226                require_once(FUNCTIONS . 'funcs.files.php');
227        }
228       
229       
230        /**
231         * Initialize Database
232         *
233         * @return object
234         */
235        public function initDatabase()
236        {
237                $ezSQL = new Database(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
238                $ezSQL->query("SET NAMES 'utf8'");
239               
240                return $ezSQL;
241        }
242       
243       
244        /**
245         * Initialize Inspekt
246         *
247         * @return object
248         */
249        public function initInspektCage()
250        {
251                $cage = Inspekt::makeSuperCage();
252               
253                // Add Hotaru custom methods
254                $cage->addAccessor('testAlnumLines');
255                $cage->addAccessor('testPage');
256                $cage->addAccessor('testUsername');
257                $cage->addAccessor('testPassword');
258                $cage->addAccessor('getFriendlyUrl');
259                $cage->addAccessor('sanitizeAll');
260                $cage->addAccessor('sanitizeTags');
261                $cage->addAccessor('sanitizeEnts');
262                $cage->addAccessor('getHtmLawed');
263               
264                return $cage;
265        }
266       
267       
268        /**
269         * Returns all site settings
270         * @param <int> $siteid
271         *
272         * @return <bool>
273         */
274        public function readSettings() {
275            $sql = "SELECT settings_name, settings_value FROM " . TABLE_SETTINGS . " WHERE settings_siteid = %d";
276            $settings = $this->db->get_results($this->db->prepare($sql, SITEID));
277
278            if(!$settings) { return false; }
279
280                // Make Hotaru settings global constants
281                foreach ($settings as $setting)
282                {
283                        if (!defined($setting->settings_name)) {
284                                define($setting->settings_name, $setting->settings_value);
285                        }
286                }
287
288                return true;
289        }
290       
291       
292        /**
293         * Set up database cache
294         *
295         * Note: Queries are still only cached following $this->db->cache_queries = true;
296         */
297        public function setUpDatabaseCache()
298        {
299                // Setup database cache
300                $this->db->cache_timeout = DB_CACHE_DURATION; // Note: this is hours
301                $this->db->cache_dir = CACHE . 'db_cache';
302                if (DB_CACHE == "true") {
303                        $this->db->use_disk_cache = true;
304                        return true;
305                } else {
306                        $this->db->use_disk_cache = false;
307                        return false;
308                }   
309        }
310       
311       
312        /**
313         * Debug timer
314         *
315         * @ return bool
316         */
317        public function checkDebug()
318        {
319                // Start timer if debugging
320                if (DEBUG == "true") {
321                        require_once(FUNCTIONS . 'funcs.times.php');
322                        timer_start();
323                        ini_set('display_errors', 1); // show errors
324                        return true;
325                } else {
326                        ini_set('display_errors', 0); // hide errors
327                }
328               
329                return false;
330        }
331       
332        /**
333         * Get JQuery Globals
334         *
335         * 
336         */
337        public function setUpJsConstants()
338        {
339                // Start timer if debugging
340                $global_js_var = "jQuery('document').ready(function($) {BASEURL = '". BASEURL ."'; ADMIN_THEME = '" . ADMIN_THEME . "'; THEME = '" . THEME . "';});";   
341                $JsConstantsFile = "css_js_cache/JavascriptConstants.js";
342       
343                if (!file_exists(CACHE . $JsConstantsFile)) {
344                        $JsConstantsPath = CACHE . $JsConstantsFile;
345                        $JsConstantsfh = fopen($JsConstantsPath, 'w') or die ("Can't open file");       
346                        fwrite($JsConstantsfh, $global_js_var);
347                        fclose($JsConstantsfh);         
348                }
349                return false;
350        }
351}
352?>
Note: See TracBrowser for help on using the repository browser.