Fix for double function GET_CURR_THEME()
[mailer.git] / inc / libs / theme_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 12/03/2004 *
4  * ===============                              Last change: 12/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : theme-manager.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : Theme manager                                    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Themen-Manager                                   *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Always make sure the session management is initialized first
41 require_once(PATH."inc/session.php");
42
43 // Dirty fix for dublicate function
44 if (!function_exists('GET_CURR_THEME')) {
45 // Get current theme name
46 function GET_CURR_THEME() {
47         global $INC_POOL, $_CONFIG, $CSS, $cacheArray;
48
49         // The default theme is 'default'... ;-)
50         $ret = "default";
51
52         // Load default theme if not empty from configuration
53         if (getConfig('default_theme') != "") $ret = getConfig('default_theme');
54
55         if (!isSessionVariableSet('mxchange_theme')) {
56                 // Set default theme
57                 set_session("mxchange_theme", $ret);
58         } elseif ((isSessionVariableSet('mxchange_theme')) && (GET_EXT_VERSION("sql_patches") >= "0.1.4")) {
59                 //die("<pre>".print_r($cacheArray['themes'], true)."</pre>");
60                 // Get theme from cookie
61                 $ret = get_session('mxchange_theme');
62
63                 // Is it valid?
64                 if (THEME_GET_ID($ret) == 0) {
65                         // Fix it to default
66                         $ret = "default";
67                 } // END - if
68         } elseif ((!isBooleanConstantAndTrue('mxchange_installed')) && ((isBooleanConstantAndTrue('mxchange_installing')) || ($CSS == true)) && ((!empty($_GET['theme'])) || (!empty($_POST['theme'])))) {
69                 // Prepare FQFN for checking
70                 $theme = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($_GET['theme']));
71
72                 // Installation mode active
73                 if ((!empty($_GET['theme'])) && (FILE_READABLE($theme))) {
74                         // Set cookie from URL data
75                         set_session("mxchange_theme", SQL_ESCAPE($_GET['theme']));
76                 } elseif (FILE_READABLE(sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($_POST['theme'])))) {
77                         // Set cookie from posted data
78                         set_session("mxchange_theme", SQL_ESCAPE($_POST['theme']));
79                 }
80
81                 // Set return value
82                 $ret = get_session('mxchange_theme');
83         } else {
84                 // Invalid design, reset cookie
85                 set_session("mxchange_theme", $ret);
86         }
87
88         // Add (maybe) found theme.php file to inclusion list
89         $theme = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($ret));
90
91         // Try to load the requested include file
92         if (FILE_READABLE($theme)) $INC_POOL[] = $theme;
93
94         // Return theme value
95         return $ret;
96 }
97 } // END - if
98
99 function THEME_SELECTION_BOX($mod, $act, $wht, $result) {
100         // Construction URL
101         $FORM = URL."/modules.php?module=".$mod;
102         if (!empty($act)) $FORM .= "&amp;action=".$act;
103         if (!empty($wht))   $FORM .= "&amp;what=".$wht;
104         define('__FORM_VALUE', $FORM);
105
106         // Initialize array
107         $THEMES = array(
108                 'theme_unix'   => array(), // Unix name from filesystem
109                 'theme_name'   => array()  // Title
110         );
111
112         // Load all themes
113         while(list($theme) = SQL_FETCHROW($result)) {
114                 // Load it's theme.php file
115                 $INC = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($theme));
116                 if (FILE_READABLE($INC)) {
117                         // And save all data in array
118                         require($INC);
119                         $THEMES['theme_unix'][] = $theme;
120                         $THEMES['theme_name'][] = $THEME_NAME;
121                 } // END - if
122         } // END - while
123
124         // Sort whole array by title
125         array_pk_sort($THEMES, array("theme_name"));
126
127         // Construct selection form for the box template
128         $OUT = "";
129         foreach ($THEMES['theme_unix'] as $key => $theme) {
130                 $OUT .= "  <OPTION value=\"".$theme."\"";
131                 if ($theme == GET_CURR_THEME()) $OUT .= " selected=\"selected\"";
132                 $OUT .= ">".$THEMES['theme_name'][$key]."</OPTION>\n";
133         } // END - foreach
134
135         // Return generated selection
136         define('__THEME_SELECTION_OPTIONS', $OUT);
137         $OUT = LOAD_TEMPLATE("theme_select_form", true);
138         return $OUT;
139 }
140
141 // Get version from name
142 function THEME_GET_VERSION ($name) {
143         global $cacheArray, $_CONFIG;
144
145         // Is the extension "theme" installed?
146         if (!EXT_IS_ACTIVE("theme")) {
147                 // Then abort here
148                 return "0.0";
149         } // END - if
150
151         // Default version "number"
152         $cver = "-.-";
153
154         // Is the cache entry there?
155         if (isset($cacheArray['themes']['theme_ver'][$name])) {
156                 // Get the version from cache
157                 $cver = $cacheArray['themes']['theme_ver'][$name];
158
159                 // Count up
160                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
161         } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
162                 // Load version from database
163                 $result = SQL_QUERY_ESC("SELECT theme_ver FROM `"._MYSQL_PREFIX."_themes` WHERE theme_path='%s' LIMIT 1",
164                         array($name), __FILE__, __LINE__);
165
166                 // Entry found?
167                 if (SQL_NUMROWS($result) == 1) {
168                         // Fetch data
169                         list($cver) = SQL_FETCHROW($result);
170                 } // END - if
171
172                 // Free result
173                 SQL_FREERESULT($result);
174         }
175
176         // Return version
177         return $cver;
178 }
179
180 // Get id from theme
181 function THEME_GET_ID ($name) {
182         global $cacheArray, $_CONFIG;
183
184         // Is the extension "theme" installed?
185         if (!EXT_IS_ACTIVE("theme")) {
186                 // Then abort here
187                 return 0;
188         } // END - if
189
190         // Default id
191         $id = 0;
192
193         // Is the cache entry there?
194         if (isset($cacheArray['themes']['id'][$name])) {
195                 // Get the version from cache
196                 $id = $cacheArray['themes']['id'][$name];
197
198                 // Count up
199                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
200         } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
201                 // Check if current theme is already imported or not
202                 $result = SQL_QUERY_ESC("SELECT id FROM `"._MYSQL_PREFIX."_themes` WHERE theme_path='%s' LIMIT 1",
203                         array($name), __FILE__, __LINE__);
204
205                 // Entry found?
206                 if (SQL_NUMROWS($result) == 1) {
207                         // Fetch data
208                         list($id) = SQL_FETCHROW($result);
209                 } // END - if
210
211                 // Free result
212                 SQL_FREERESULT($result);
213         }
214
215         // Return id
216         return $id;
217 }
218
219 // Checks wether a theme is found in db
220 function THEME_CHECK_EXIST ($name) {
221         // Get theme and is it not nul?
222         return (THEME_GET_ID($name) > 0);
223 }
224
225 // Checks if a theme is active
226 function THEME_IS_ACTIVE ($name) {
227         global $cacheArray, $_CONFIG;
228
229         // Is the extension "theme" installed?
230         if (!EXT_IS_ACTIVE("theme")) {
231                 // Then abort here
232                 return false;
233         } // END - if
234
235         // Default is nothing active
236         $active = false;
237
238         // Is the cache entry there?
239         if (isset($cacheArray['themes']['theme_active'][$name])) {
240                 // Get the version from cache
241                 $active = ($cacheArray['themes']['theme_active'][$name] == "Y");
242
243                 // Count up
244                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
245         } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
246                 // Check if current theme is already imported or not
247                 $result = SQL_QUERY_ESC("SELECT theme_active FROM `"._MYSQL_PREFIX."_themes` WHERE theme_path='%s' AND theme_active='Y' LIMIT 1",
248                         array($name), __FILE__, __LINE__);
249
250                 // Is the theme active and installed?
251                 $active = (SQL_NUMROWS($result) == 1);
252
253                 // Free result
254                 SQL_FREERESULT($result);
255         }
256
257         // Return result
258         return $active;
259 }
260
261 // Gets current human-readable theme name
262 function GET_CURR_THEME_NAME () {
263         global $cacheArray, $_CONFIG;
264
265         // Is the extension "theme" installed?
266         if (!EXT_IS_ACTIVE("theme")) {
267                 // Then abort here
268                 return "default";
269         } // END - if
270
271         // Get the Uni* name
272         $name = GET_CURR_THEME();
273
274         // Is the cache entry there?
275         if (isset($cacheArray['themes']['theme_name'][$name])) {
276                 // Get the version from cache
277                 $name = $cacheArray['themes']['theme_name'][$name];
278
279                 // Count up
280                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
281         } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
282                 // Check if current theme is already imported or not
283                 $result = SQL_QUERY_ESC("SELECT theme_name FROM `"._MYSQL_PREFIX."_themes` WHERE theme_path='%s' AND theme_active='Y' LIMIT 1",
284                         array($name), __FILE__, __LINE__);
285
286                 // Load theme name
287                 list($name) = SQL_FETCHROW($result);
288
289                 // Free result
290                 SQL_FREERESULT($result);
291         }
292
293         // Return name
294         return $name;
295 }
296
297 // Initialize variables
298 $currTheme = GET_CURR_THEME();
299
300 // Check if new theme is selcted
301 if ((!empty($_POST['new_theme'])) && ($_POST['new_theme'] != $currTheme)) {
302         // Set new theme for guests
303         $newTheme = $_POST['new_theme'];
304
305         // Change to new theme
306         set_session("mxchange_theme", $newTheme);
307
308         // Remove current from array and set new
309         $theme = sprintf("%stheme/%s/theme.php", PATH, $currTheme);
310         unset($INC_POOL[array_search($theme, $INC_POOL)]);
311         $INC_POOL[] = sprintf("%stheme/%s/theme.php", PATH, $newTheme);
312 } // END - if
313
314 //
315 ?>