5fff9d70642ae01cd8c18a3cb5fe9166eb120b14
[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 // Get current theme name
44 function GET_CURR_THEME() {
45         global $INC_POOL, $_CONFIG, $CSS, $cacheArray;
46
47         // The default theme is 'default'... ;-)
48         $ret = "default";
49
50         // Load default theme if not empty from configuration
51         if (getConfig('default_theme') != "") $ret = getConfig('default_theme');
52
53         if (!isSessionVariableSet('mxchange_theme')) {
54                 // Set default theme
55                 set_session("mxchange_theme", $ret);
56         } elseif ((isSessionVariableSet('mxchange_theme')) && (GET_EXT_VERSION("sql_patches") >= "0.1.4")) {
57                 //die("<pre>".print_r($cacheArray['themes'], true)."</pre>");
58                 // Get theme from cookie
59                 $ret = get_session('mxchange_theme');
60
61                 // Is it valid?
62                 if (THEME_GET_ID($ret) == 0) {
63                         // Fix it to default
64                         $ret = "default";
65                 } // END - if
66         } elseif ((!isBooleanConstantAndTrue('mxchange_installed')) && ((isBooleanConstantAndTrue('mxchange_installing')) || ($CSS == true)) && ((!empty($_GET['theme'])) || (!empty($_POST['theme'])))) {
67                 // Prepare FQFN for checking
68                 $theme = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($_GET['theme']));
69
70                 // Installation mode active
71                 if ((!empty($_GET['theme'])) && (FILE_READABLE($theme))) {
72                         // Set cookie from URL data
73                         set_session("mxchange_theme", SQL_ESCAPE($_GET['theme']));
74                 } elseif (FILE_READABLE(sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($_POST['theme'])))) {
75                         // Set cookie from posted data
76                         set_session("mxchange_theme", SQL_ESCAPE($_POST['theme']));
77                 }
78
79                 // Set return value
80                 $ret = get_session('mxchange_theme');
81         } else {
82                 // Invalid design, reset cookie
83                 set_session("mxchange_theme", $ret);
84         }
85
86         // Add (maybe) found theme.php file to inclusion list
87         $theme = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($ret));
88
89         // Try to load the requested include file
90         if (FILE_READABLE($theme)) $INC_POOL[] = $theme;
91
92         // Return theme value
93         return $ret;
94 }
95
96 function THEME_SELECTION_BOX($mod, $act, $wht, $result) {
97         // Construction URL
98         $FORM = URL."/modules.php?module=".$mod;
99         if (!empty($act)) $FORM .= "&amp;action=".$act;
100         if (!empty($wht))   $FORM .= "&amp;what=".$wht;
101         define('__FORM_VALUE', $FORM);
102
103         // Initialize array
104         $THEMES = array(
105                 'theme_unix'   => array(), // Unix name from filesystem
106                 'theme_name'   => array()  // Title
107         );
108
109         // Load all themes
110         while(list($theme) = SQL_FETCHROW($result)) {
111                 // Load it's theme.php file
112                 $INC = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($theme));
113                 if (FILE_READABLE($INC)) {
114                         // And save all data in array
115                         require($INC);
116                         $THEMES['theme_unix'][] = $theme;
117                         $THEMES['theme_name'][] = $THEME_NAME;
118                 } // END - if
119         } // END - while
120
121         // Sort whole array by title
122         array_pk_sort($THEMES, array("theme_name"));
123
124         // Construct selection form for the box template
125         $OUT = "";
126         foreach ($THEMES['theme_unix'] as $key => $theme) {
127                 $OUT .= "  <OPTION value=\"".$theme."\"";
128                 if ($theme == GET_CURR_THEME()) $OUT .= " selected=\"selected\"";
129                 $OUT .= ">".$THEMES['theme_name'][$key]."</OPTION>\n";
130         } // END - foreach
131
132         // Return generated selection
133         define('__THEME_SELECTION_OPTIONS', $OUT);
134         $OUT = LOAD_TEMPLATE("theme_select_form", true);
135         return $OUT;
136 }
137
138 // Get version from name
139 function THEME_GET_VERSION ($name) {
140         global $cacheArray, $_CONFIG;
141
142         // Is the extension "theme" installed?
143         if (!EXT_IS_ACTIVE("theme")) {
144                 // Then abort here
145                 return "0.0";
146         } // END - if
147
148         // Default version "number"
149         $cver = "-.-";
150
151         // Is the cache entry there?
152         if (isset($cacheArray['themes']['theme_ver'][$name])) {
153                 // Get the version from cache
154                 $cver = $cacheArray['themes']['theme_ver'][$name];
155
156                 // Count up
157                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
158         } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
159                 // Load version from database
160                 $result = SQL_QUERY_ESC("SELECT theme_ver FROM `"._MYSQL_PREFIX."_themes` WHERE theme_path='%s' LIMIT 1",
161                         array($name), __FILE__, __LINE__);
162
163                 // Entry found?
164                 if (SQL_NUMROWS($result) == 1) {
165                         // Fetch data
166                         list($cver) = SQL_FETCHROW($result);
167                 } // END - if
168
169                 // Free result
170                 SQL_FREERESULT($result);
171         }
172
173         // Return version
174         return $cver;
175 }
176
177 // Get id from theme
178 function THEME_GET_ID ($name) {
179         global $cacheArray, $_CONFIG;
180
181         // Is the extension "theme" installed?
182         if (!EXT_IS_ACTIVE("theme")) {
183                 // Then abort here
184                 return 0;
185         } // END - if
186
187         // Default id
188         $id = 0;
189
190         // Is the cache entry there?
191         if (isset($cacheArray['themes']['id'][$name])) {
192                 // Get the version from cache
193                 $id = $cacheArray['themes']['id'][$name];
194
195                 // Count up
196                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
197         } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
198                 // Check if current theme is already imported or not
199                 $result = SQL_QUERY_ESC("SELECT id FROM `"._MYSQL_PREFIX."_themes` WHERE theme_path='%s' LIMIT 1",
200                         array($name), __FILE__, __LINE__);
201
202                 // Entry found?
203                 if (SQL_NUMROWS($result) == 1) {
204                         // Fetch data
205                         list($id) = SQL_FETCHROW($result);
206                 } // END - if
207
208                 // Free result
209                 SQL_FREERESULT($result);
210         }
211
212         // Return id
213         return $id;
214 }
215
216 // Checks wether a theme is found in db
217 function THEME_CHECK_EXIST ($name) {
218         // Get theme and is it not nul?
219         return (THEME_GET_ID($name) > 0);
220 }
221
222 // Checks if a theme is active
223 function THEME_IS_ACTIVE ($name) {
224         global $cacheArray, $_CONFIG;
225
226         // Is the extension "theme" installed?
227         if (!EXT_IS_ACTIVE("theme")) {
228                 // Then abort here
229                 return false;
230         } // END - if
231
232         // Default is nothing active
233         $active = false;
234
235         // Is the cache entry there?
236         if (isset($cacheArray['themes']['theme_active'][$name])) {
237                 // Get the version from cache
238                 $active = ($cacheArray['themes']['theme_active'][$name] == "Y");
239
240                 // Count up
241                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
242         } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
243                 // Check if current theme is already imported or not
244                 $result = SQL_QUERY_ESC("SELECT theme_active FROM `"._MYSQL_PREFIX."_themes` WHERE theme_path='%s' AND theme_active='Y' LIMIT 1",
245                         array($name), __FILE__, __LINE__);
246
247                 // Is the theme active and installed?
248                 $active = (SQL_NUMROWS($result) == 1);
249
250                 // Free result
251                 SQL_FREERESULT($result);
252         }
253
254         // Return result
255         return $active;
256 }
257
258 // Gets current human-readable theme name
259 function GET_CURR_THEME_NAME () {
260         global $cacheArray, $_CONFIG;
261
262         // Is the extension "theme" installed?
263         if (!EXT_IS_ACTIVE("theme")) {
264                 // Then abort here
265                 return "default";
266         } // END - if
267
268         // Get the Uni* name
269         $name = GET_CURR_THEME();
270
271         // Is the cache entry there?
272         if (isset($cacheArray['themes']['theme_name'][$name])) {
273                 // Get the version from cache
274                 $name = $cacheArray['themes']['theme_name'][$name];
275
276                 // Count up
277                 if (getConfig('cache_hits') > 0) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
278         } elseif (GET_EXT_VERSION("cache") != "0.1.8") {
279                 // Check if current theme is already imported or not
280                 $result = SQL_QUERY_ESC("SELECT theme_name FROM `"._MYSQL_PREFIX."_themes` WHERE theme_path='%s' AND theme_active='Y' LIMIT 1",
281                         array($name), __FILE__, __LINE__);
282
283                 // Load theme name
284                 list($name) = SQL_FETCHROW($result);
285
286                 // Free result
287                 SQL_FREERESULT($result);
288         }
289
290         // Return name
291         return $name;
292 }
293
294 // Initialize variables
295 $currTheme = GET_CURR_THEME();
296
297 // Check if new theme is selcted
298 if ((!empty($_POST['new_theme'])) && ($_POST['new_theme'] != $currTheme)) {
299         // Set new theme for guests
300         $newTheme = $_POST['new_theme'];
301
302         // Change to new theme
303         set_session("mxchange_theme", $newTheme);
304
305         // Remove current from array and set new
306         $theme = sprintf("%stheme/%s/theme.php", PATH, $currTheme);
307         unset($INC_POOL[array_search($theme, $INC_POOL)]);
308         $INC_POOL[] = sprintf("%stheme/%s/theme.php", PATH, $newTheme);
309 } // END - if
310
311 //
312 ?>