source: branches/1.3/libs/Maintenance.php @ 1871

Revision 1871, 9.6 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.3] Fix for error/success message when clearing language cache.

Line 
1<?php
2/**
3 * Functions for maintaining the health of Hotaru CMS
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 Maintenance
27{
28        /**
29         * System Report is in libs/Debug.php
30         */
31         
32       
33        /**
34         * Calls the delete_files function, then displays a message.
35         *
36         * @param string $folder - path to the cache folder
37         * @param string $msg - show "cleared" message or not
38         * @return bool $success
39         */
40        public function clearCache($h, $folder, $msg = true)
41        {
42                $success = $this->deleteFiles(CACHE . $folder);
43                if (!$msg) { return $success; }
44                if ($success) {
45                        $h->message = $h->lang['admin_maintenance_clear_cache_success'];
46                        $h->messageType = 'green';
47                } else {
48                        $h->message = $h->lang['admin_maintenance_clear_cache_failure'];
49                        $h->messageType = 'red';   
50                }
51               
52                return $success;
53        }
54       
55       
56        /**
57         * Clear Language Cache
58         *
59         * wipe the lang array and reinclude the main and admin lang so
60         * as not to break the Maintenance page
61         */
62        public function clearLanguageCache($h, $msg = false)
63        {
64                $h->lang = array();
65                $success = $h->clearCache('lang_cache', false); // false to prevent showing message with language we don't have anymore
66               
67                $langObj = new Language();
68                $h->lang = $langObj->includeLanguagePack($h->lang, 'main');
69                $h->lang = $langObj->includeLanguagePack($h->lang, 'admin');
70               
71                if ($success && $msg) {
72                        $h->message = $h->lang['admin_maintenance_clear_cache_success'];
73                        $h->messageType = 'green';
74                } else {
75                        $h->message = $h->lang['admin_maintenance_clear_cache_failure'];
76                        $h->messageType = 'red';
77                }
78        }
79       
80       
81        /**
82         * Remove plugin settings
83         *
84         * @param string $folder - plugin folder name
85         * @param bool $msg - show "Removed" message or not
86         */
87        public function removeSettings($h, $folder, $msg = true)
88        {
89                if (!$folder) { $folder = $h->plugin->folder; }
90                $sql = "DELETE FROM " . DB_PREFIX . "pluginsettings WHERE plugin_folder = %s";
91                $h->db->get_results($h->db->prepare($sql, $folder));
92               
93                if ($msg) {
94                        $h->message = $h->lang['admin_maintenance_settings_removed'];
95                        $h->messageType = 'green';
96                }
97        }
98       
99       
100        /**
101         * Deletes rows from pluginsettings that match a given setting or plugin
102         *
103         * @param string $setting name of the setting to remove
104         * @param string $folder name of plugin folder
105         */
106        public function deleteSettings($h, $setting = '', $folder = '')
107        {
108                if ($setting) {
109                        $sql = "DELETE FROM " . TABLE_PLUGINSETTINGS . " WHERE plugin_setting = %s";
110                        $h->db->query($h->db->prepare($sql, $setting));
111                }
112                elseif ($folder)
113                {
114                        $sql = "DELETE FROM " . TABLE_PLUGINSETTINGS . " WHERE plugin_folder = %s";
115                        $h->db->query($h->db->prepare($sql, $folder));
116                }
117               
118                // optimize the table
119                $h->db->query("OPTIMIZE TABLE " . TABLE_PLUGINSETTINGS);
120        }
121       
122       
123        /**
124         * Delete all files in the specified directory except placeholder.txt
125         *
126         * @param string $dir - path to the cache folder
127         * @return bool
128         */   
129        public function deleteFiles($dir)
130        {
131                $handle=opendir($dir);
132               
133                $success = false;
134                while (($file = readdir($handle))!==false) {
135                        if ($file != 'placeholder.txt') {
136                                if (@unlink($dir.'/'.$file)) {
137                                        // ignore setting $success for the JavascriptConstants file which is ALWAYS present (even gets regenerated after deletion)
138                                        if ($file != 'JavascriptConstants.js') { $success = true; }
139                                } else {
140                                        $success = false;
141                                }
142                        }
143                }
144                closedir($handle);
145
146                return $success;
147        }
148       
149       
150        /**
151         * Optimize all database tables
152         */
153        public function optimizeTables($h)
154        {
155                $h->db->selectDB(DB_NAME);
156               
157                foreach ( $h->db->get_col("SHOW TABLES",0) as $table_name )
158                {
159                        $h->db->query("OPTIMIZE TABLE " . $table_name);
160                }
161               
162                $h->message = $h->lang['admin_maintenance_optimize_success'];
163                $h->messageType = 'green';
164        }
165       
166       
167        /**
168         * Empty plugin database table
169         *
170         * @param string $table_name - table to empty
171         * @param string $msg - show "emptied" message or not
172         */
173        public function emptyTable($h, $table_name, $msg = true)
174        {
175                $h->db->query("TRUNCATE TABLE " . $table_name);
176               
177                if ($msg) {
178                        $h->message = $h->lang['admin_maintenance_table_emptied'];
179                        $h->messageType = 'green';
180                }
181        }
182       
183       
184        /**
185         * Delete plugin database table
186         *
187         * @param string $table_name - table to drop
188         */
189        public function dropTable($h, $table_name, $msg = true)
190        {
191                $h->db->query("DROP TABLE " . $table_name);
192               
193                if ($msg) {
194                        $h->message = $h->lang['admin_maintenance_table_deleted'];
195                        $h->messageType = 'green';
196                }
197        }
198       
199       
200        /**
201         * Open or close the site for maintenance
202         *
203         * @param object $h
204         * @param string $switch - 'open' or 'close'
205         */
206        public function openCloseSite($h, $switch = 'open')
207        {
208                if ($switch == 'open') {
209                        // open
210                        $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_value = %s WHERE settings_name = %s";
211                        $h->db->query($h->db->prepare($sql, 'true', 'SITE_OPEN'));
212                        $h->message = $h->lang['admin_maintenance_site_opened'];
213                        $h->messageType = 'green';
214                } else {
215                        //close
216                        $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_value = %s WHERE settings_name = %s";
217                        $h->db->query($h->db->prepare($sql, 'false', 'SITE_OPEN'));
218                        $h->message = $h->lang['admin_maintenance_site_closed'];
219                        $h->messageType = 'green';
220                }
221        }
222       
223       
224        /**
225         * Site closed: Exit
226         *
227         * @param object $h
228         */
229        public function siteClosed($h, $lang)
230        {
231                // site closed and access not granted
232                echo "<HTML>\n<HEAD>\n";
233                echo "<link rel='stylesheet' href='" . BASEURL . "content/themes/" . THEME . "css/style.css' type='text/css'>\n";
234                echo "</HEAD>\n<BODY>\n";
235                echo "<div id='site_closed'>\n";
236               
237                // show custom maintenance page if one exists:
238                if (file_exists(THEMES . THEME . 'closed.php'))
239                {
240                        $h->displayTemplate('closed');
241                }
242                else
243                {
244                        // show default maintenance page:
245                        echo $lang['main_hotaru_site_closed'];
246                        echo "<br /><span id='site_closed_admin_link'>[<a href='" . BASEURL . "admin_index.php?page=admin_login'>Admin Login</a>]</span>";
247                }
248               
249                echo "\n</div>\n</BODY>\n</HTML>\n";
250               
251                die(); exit;
252        }
253       
254       
255        /**
256         * Get Site Annoucement for Maintenance Page (AdminPages.php)
257         */
258        public function getSiteAnnouncement($h)
259        {
260                // get announcement from database
261                $sql = "SELECT miscdata_value FROM " . TABLE_MISCDATA ." WHERE miscdata_key = %s";
262                $query = $h->db->prepare($sql, 'site_announcement');
263               
264                if ($h->pageName != 'maintenance') {
265                        $h->smartCache('on', 'miscdata', 60, $query); // start using cache
266                }
267               
268                $result = $h->db->get_var($query);
269               
270                if ($h->pageName != 'maintenance') {
271                        $h->smartCache('off'); // stop using cache
272                }
273               
274                // assign results to $h
275                if ($result) {
276                        $result = unserialize($result);
277                        $h->vars['admin_announcement'] = urldecode($result['announcement']);
278                        $h->vars['admin_announcement_enabled'] = $result['enabled'];
279                } else {
280                        $h->vars['admin_announcement'] = "";
281                        $h->vars['admin_announcement_enabled'] = "";
282                }
283               
284        }
285       
286       
287        /**
288         * Add Site Annoucement from Maintenance Page (AdminPages.php)
289         *
290         * @param object $announcement_exists - result from getSiteAnnouncement()
291         */
292        public function addSiteAnnouncement($h)
293        {
294                $allowable_tags = "<div><p><span><b><i><u><a><img><blockquote><del><br>";
295                $h->vars['admin_announcement'] = sanitize($h->cage->get->getHtmLawed('announcement_text'), 'tags', $allowable_tags);
296                if ($h->cage->get->keyExists('announcement_enabled')) {
297                        $h->vars['admin_announcement_enabled'] = "checked";
298                } else {
299                        $h->vars['admin_announcement_enabled'] = "";
300                }
301               
302                // prepare annoucment for database entry:
303                $value = array('announcement'=>urlencode($h->vars['admin_announcement']), 'enabled'=>$h->vars['admin_announcement_enabled']);
304                $value = serialize($value);
305               
306                // update existing db record
307                $sql = "UPDATE " . TABLE_MISCDATA . " SET miscdata_value = %s, miscdata_updateby = %d WHERE miscdata_key = %s";
308                $h->db->query($h->db->prepare($sql, $value, $h->currentUser->id, 'site_announcement'));
309               
310                // clear the database cache:
311                $h->clearCache('db_cache', false);
312               
313                $h->message = $h->lang['admin_maintenance_announcement_updated'];
314                $h->messageType = 'green';
315        }
316       
317       
318        /**
319         * Get all files in the specified directory except placeholder.txt
320         *
321         * @param string $dir - path to the folder
322         * @param array $exclude - array of file/folder names to exclude
323         * @return array
324         */   
325        public function getFiles($dir, $exclude = array())
326        {
327                $files = array();
328                $exceptions = array('.svn', '.', '..', 'placeholder.txt');
329                $exceptions = array_merge($exceptions, $exclude);
330               
331                $handle=opendir($dir);
332               
333                while (($file = readdir($handle))!==false) {
334                        if (!in_array($file, $exceptions)) {
335                                array_push($files, $file);
336                        }
337                }
338                closedir($handle);
339               
340                if ($files) { return $files; } else { return false; }
341        }
342}
343?>
Note: See TracBrowser for help on using the repository browser.