source: branches/1.2/libs/Initialize.php @ 1213

Revision 1213, 8.1 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.2] Got Hotaru to pass the $h object to ezSQL so we can include user, page and plugin info in database error email notifications.

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) 2009, 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        // session to be used by CSRF, etc.
39        if (!isset($_SESSION['HotaruCMS'])) {
40            session_start();
41            $_SESSION['HotaruCMS'] = time();
42        }
43
44        // The order here is important!
45        $this->setDefaultTimezone();
46        $this->errorReporting();
47        $this->getFiles();
48        $this->db = $this->initDatabase();
49        $this->cage = $this->initInspektCage();
50       
51        $this->readSettings();
52        $this->setUpDatabaseCache();
53        $this->isDebug = $this->checkDebug();
54
55        $this->setUpJsConstants();
56
57        return $this;
58    }
59   
60   
61    /**
62     * Access modifier to set protected properties
63     */
64    public function __set($var, $val)
65    {
66        $this->$var = $val; 
67    }
68   
69   
70    /**
71     * Access modifier to get protected properties
72     */
73    public function __get($var)
74    {
75        return $this->$var;
76    }
77   
78   
79    /**
80     * Error reporting
81     */
82    public function errorReporting()
83    {
84        // display errors
85        ini_set('display_errors', 1); // Gets disabled later in checkDebug()
86        error_reporting(E_ALL);
87       
88        // log errors to a file - the custom error handler below wasn't catching fatal errors, so using PHP's one
89        ini_set('error_log', CACHE . 'debug_logs/error_log.txt');
90        /*
91        require_once(EXTENSIONS . 'SWCMS/swcms_error_handler.php'); // error_handler class
92        $error_handler = new swcms_error_handler(0, 0, 1, NULL, CACHE . 'debug_logs/error_log.txt');
93        set_error_handler(array($error_handler, "handler"));
94        */
95    }
96
97
98    /**
99     * Set the timezone
100     */
101    public function setDefaultTimezone()
102    {
103        // set timezone
104        $version = explode('.', phpversion());
105        if($version[0] > 4){
106            $tmz = date_default_timezone_get();
107            date_default_timezone_set($tmz);
108        }
109    }
110
111
112    /**
113     * Include necessary files
114     */
115    public function getFiles()
116    {
117        // include third party libraries
118        require_once(EXTENSIONS . 'csrf/csrf_class.php'); // protection against CSRF attacks
119        require_once(EXTENSIONS . 'Inspekt/Inspekt.php'); // sanitation
120        require_once(EXTENSIONS . 'ezSQL/ez_sql_core.php'); // database
121        require_once(EXTENSIONS . 'ezSQL/mysql/ez_sql_mysql.php'); // database
122       
123        // include libraries
124        require_once(LIBS . 'Avatar.php');          // for displaying avatars
125        require_once(LIBS . 'IncludeCssJs.php');    // for including and mergeing css and javascript
126        require_once(LIBS . 'InspektExtras.php');   // for custom Inspekt methods
127        require_once(LIBS . 'Language.php');
128        require_once(LIBS . 'PageHandling.php');    // for page handling
129        require_once(LIBS . 'Plugin.php');          // for plugin properties
130        require_once(LIBS . 'PluginFunctions.php'); // for plugin functions
131        require_once(LIBS . 'PluginSettings.php');  // for plugin settings
132        require_once(LIBS . 'Post.php');            // for posts
133        require_once(LIBS . 'UserBase.php');        // for users, settings and permissions
134        require_once(LIBS . 'UserAuth.php');        // for user authentication, login and registering
135       
136        // include functions
137        require_once(FUNCTIONS . 'funcs.strings.php');
138        require_once(FUNCTIONS . 'funcs.arrays.php');
139        require_once(FUNCTIONS . 'funcs.times.php');
140        require_once(FUNCTIONS . 'funcs.files.php');
141       
142    }
143
144   
145    /**
146     * Initialize Database
147     *
148     * @return object
149     */
150    public function initDatabase()
151    {
152        $ezSQL = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
153        $ezSQL->query("SET NAMES 'utf8'");
154       
155        return $ezSQL;
156    }
157   
158   
159    /**
160     * Initialize Inspekt
161     *
162     * @return object
163     */
164    public function initInspektCage()
165    {
166        $cage = Inspekt::makeSuperCage();
167   
168        // Add Hotaru custom methods
169        $cage->addAccessor('testAlnumLines');
170        $cage->addAccessor('testPage');
171        $cage->addAccessor('testUsername');
172        $cage->addAccessor('testPassword');
173        $cage->addAccessor('getFriendlyUrl');
174        $cage->addAccessor('sanitizeAll');
175        $cage->addAccessor('sanitizeTags');
176        $cage->addAccessor('sanitizeEnts');
177        $cage->addAccessor('getHtmLawed');
178       
179        return $cage;
180    }
181   
182   
183    /**
184     * Returns all site settings
185     *
186     * @return bool
187     */
188    public function readSettings()
189    {
190        $sql = "SELECT * FROM " . TABLE_SETTINGS;
191        $settings = $this->db->get_results($this->db->prepare($sql));
192       
193        if(!$settings) { return false; }
194       
195        // Make Hotaru settings global constants
196        foreach ($settings as $setting)
197        {
198            if (!defined($setting->settings_name)) {
199                define($setting->settings_name, $setting->settings_value);
200            }
201        }
202        return true;
203    }
204   
205
206    /**
207     * Set up database cache
208     *
209     * Note: Queries are still only cached following $this->db->cache_queries = true;
210     */
211    public function setUpDatabaseCache()
212    {
213        // Setup database cache
214        $this->db->cache_timeout = DB_CACHE_DURATION; // Note: this is hours
215        $this->db->cache_dir = CACHE . 'db_cache';
216        if (DB_CACHE_ON == "true") {
217            $this->db->use_disk_cache = true;
218            return true;
219        } else {
220            $this->db->use_disk_cache = false;
221            return false;
222        }   
223    }
224   
225   
226    /**
227     * Debug timer
228     *
229     * @ return bool
230     */
231    public function checkDebug()
232    {
233        // Start timer if debugging
234        if (DEBUG == "true") {
235            require_once(FUNCTIONS . 'funcs.times.php');
236            timer_start();
237            ini_set('display_errors', 1); // show errors
238            ini_set('error_log', CACHE . 'debug_logs/error_log.txt');
239            return true;
240        } else {
241            ini_set('display_errors', 0); // hide errors
242        }
243       
244        return false;
245    }
246
247        /**
248     * Get JQuery Globals
249     *
250     * 
251     */
252    public function setUpJsConstants()
253    {
254        // Start timer if debugging
255                $global_js_var = "jQuery('document').ready(function($) {BASEURL = '". BASEURL ."'; ADMIN_THEME = '" . ADMIN_THEME . "'; THEME = '" . THEME . "';});";   
256                $JsConstantsFile = "css_js_cache/JavascriptConstants.js";
257
258                if (!file_exists(CACHE . $JsConstantsFile)) {
259                        $JsConstantsPath = CACHE . $JsConstantsFile;
260                        $JsConstantsfh = fopen($JsConstantsPath, 'w') or die ("Can't open file");       
261                        fwrite($JsConstantsfh, $global_js_var);
262                        fclose($JsConstantsfh);         
263                }       
264        return false;
265    }
266       
267}
268?>
Note: See TracBrowser for help on using the repository browser.