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

Revision 1356, 8.2 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.2] Auto delete error log if over 500Kb, plus Hotaru version number updates.

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        // error log filename
89        $filename = CACHE . 'debug_logs/error_log.txt';
90       
91        // delete file if over 500K
92        if (file_exists($filename) && filesize($filename) > 500000) {
93            unlink($filename);
94        }
95       
96        // point PHP to our error log
97        ini_set('error_log', $filename);
98       
99        $last_modified = filemtime($this->log[$type]);
100        $expire = (14 * 24 * 60 * 60); // 2 weeks
101        if ($last_modified < (time() - $expire)) { unlink ($this->log[$type]); }
102    }
103
104
105    /**
106     * Set the timezone
107     */
108    public function setDefaultTimezone()
109    {
110        // set timezone
111        $version = explode('.', phpversion());
112        if($version[0] > 4){
113            $tmz = date_default_timezone_get();
114            date_default_timezone_set($tmz);
115        }
116    }
117
118
119    /**
120     * Include necessary files
121     */
122    public function getFiles()
123    {
124        // include third party libraries
125        require_once(EXTENSIONS . 'csrf/csrf_class.php'); // protection against CSRF attacks
126        require_once(EXTENSIONS . 'Inspekt/Inspekt.php'); // sanitation
127        require_once(EXTENSIONS . 'ezSQL/ez_sql_core.php'); // database
128        require_once(EXTENSIONS . 'ezSQL/mysql/ez_sql_mysql.php'); // database
129       
130        // include libraries
131        require_once(LIBS . 'Avatar.php');          // for displaying avatars
132        require_once(LIBS . 'IncludeCssJs.php');    // for including and mergeing css and javascript
133        require_once(LIBS . 'InspektExtras.php');   // for custom Inspekt methods
134        require_once(LIBS . 'Language.php');
135        require_once(LIBS . 'PageHandling.php');    // for page handling
136        require_once(LIBS . 'Plugin.php');          // for plugin properties
137        require_once(LIBS . 'PluginFunctions.php'); // for plugin functions
138        require_once(LIBS . 'PluginSettings.php');  // for plugin settings
139        require_once(LIBS . 'Post.php');            // for posts
140        require_once(LIBS . 'UserBase.php');        // for users, settings and permissions
141        require_once(LIBS . 'UserAuth.php');        // for user authentication, login and registering
142       
143        // include functions
144        require_once(FUNCTIONS . 'funcs.strings.php');
145        require_once(FUNCTIONS . 'funcs.arrays.php');
146        require_once(FUNCTIONS . 'funcs.times.php');
147        require_once(FUNCTIONS . 'funcs.files.php');
148       
149    }
150
151   
152    /**
153     * Initialize Database
154     *
155     * @return object
156     */
157    public function initDatabase()
158    {
159        $ezSQL = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
160        $ezSQL->query("SET NAMES 'utf8'");
161       
162        return $ezSQL;
163    }
164   
165   
166    /**
167     * Initialize Inspekt
168     *
169     * @return object
170     */
171    public function initInspektCage()
172    {
173        $cage = Inspekt::makeSuperCage();
174   
175        // Add Hotaru custom methods
176        $cage->addAccessor('testAlnumLines');
177        $cage->addAccessor('testPage');
178        $cage->addAccessor('testUsername');
179        $cage->addAccessor('testPassword');
180        $cage->addAccessor('getFriendlyUrl');
181        $cage->addAccessor('sanitizeAll');
182        $cage->addAccessor('sanitizeTags');
183        $cage->addAccessor('sanitizeEnts');
184        $cage->addAccessor('getHtmLawed');
185       
186        return $cage;
187    }
188   
189   
190    /**
191     * Returns all site settings
192     *
193     * @return bool
194     */
195    public function readSettings()
196    {
197        $sql = "SELECT * FROM " . TABLE_SETTINGS;
198        $settings = $this->db->get_results($this->db->prepare($sql));
199       
200        if(!$settings) { return false; }
201       
202        // Make Hotaru settings global constants
203        foreach ($settings as $setting)
204        {
205            if (!defined($setting->settings_name)) {
206                define($setting->settings_name, $setting->settings_value);
207            }
208        }
209        return true;
210    }
211   
212
213    /**
214     * Set up database cache
215     *
216     * Note: Queries are still only cached following $this->db->cache_queries = true;
217     */
218    public function setUpDatabaseCache()
219    {
220        // Setup database cache
221        $this->db->cache_timeout = DB_CACHE_DURATION; // Note: this is hours
222        $this->db->cache_dir = CACHE . 'db_cache';
223        if (DB_CACHE == "true") {
224            $this->db->use_disk_cache = true;
225            return true;
226        } else {
227            $this->db->use_disk_cache = false;
228            return false;
229        }   
230    }
231   
232   
233    /**
234     * Debug timer
235     *
236     * @ return bool
237     */
238    public function checkDebug()
239    {
240        // Start timer if debugging
241        if (DEBUG == "true") {
242            require_once(FUNCTIONS . 'funcs.times.php');
243            timer_start();
244            ini_set('display_errors', 1); // show errors
245            ini_set('error_log', CACHE . 'debug_logs/error_log.txt');
246            return true;
247        } else {
248            ini_set('display_errors', 0); // hide errors
249        }
250       
251        return false;
252    }
253
254        /**
255     * Get JQuery Globals
256     *
257     * 
258     */
259    public function setUpJsConstants()
260    {
261        // Start timer if debugging
262                $global_js_var = "jQuery('document').ready(function($) {BASEURL = '". BASEURL ."'; ADMIN_THEME = '" . ADMIN_THEME . "'; THEME = '" . THEME . "';});";   
263                $JsConstantsFile = "css_js_cache/JavascriptConstants.js";
264
265                if (!file_exists(CACHE . $JsConstantsFile)) {
266                        $JsConstantsPath = CACHE . $JsConstantsFile;
267                        $JsConstantsfh = fopen($JsConstantsPath, 'w') or die ("Can't open file");       
268                        fwrite($JsConstantsfh, $global_js_var);
269                        fclose($JsConstantsfh);         
270                }       
271        return false;
272    }
273       
274}
275?>
Note: See TracBrowser for help on using the repository browser.