a1b563054a0e02599f65d9848e68bdfe9ea7aa26
[mailer.git] / inc / libs / theme_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Create a selection box with installed and activated themes or all if admin
46 function generateThemeSelectionBox () {
47         // Init variables and fill them if set
48         $what = getWhat();
49         $mod = getModule();
50
51         // Construction URL
52         $formAction = '{%url=modules.php?module='. $mod;
53         if (!empty($what)) $formAction .= '&amp;what=' . $what;
54         $formAction .= '%}';
55
56         // Initialize array
57         $themesArray = array(
58                 'theme_unix'   => array(), // Unix name from filesystem
59                 'theme_name'   => array()  // Title
60         );
61
62         // Only activated themes for the user
63         $add = " WHERE `theme_active`='Y'";
64
65         // Do we have admin?
66         if (isAdmin()) $add = '';
67
68         // Select all themes we want
69         $result = SQL_QUERY("SELECT
70         `theme_path`, `theme_name`
71 FROM
72         `{?_MYSQL_PREFIX?}_themes`
73 ".$add."
74 ORDER BY
75         `theme_name` ASC", __FILE__, __LINE__);
76
77         // Load all themes
78         while ($content = SQL_FETCHARRAY($result)) {
79                 // Construct relative include file name
80                 $inc = sprintf("theme/%s/theme.php", secureString($content['theme_path']));
81
82                 // Load it's theme.php file if found
83                 if (isIncludeReadable($inc)) {
84                         // And save all data in array
85                         loadInclude($inc);
86                         $themesArray['theme_unix'][] = $content['theme_path'];
87                         $themesArray['theme_name'][] = $GLOBALS['theme_data']['name'];
88                 } // END - if
89         } // END - while
90
91         // Free the result
92         SQL_FREERESULT($result);
93
94         // Construct selection form for the box template
95         $OUT = '';
96         foreach ($themesArray['theme_unix'] as $key => $theme) {
97                 $OUT .= "  <option value=\"".$theme."\"";
98                 if ($theme == getCurrentTheme()) $OUT .= ' selected="selected"';
99                 $OUT .= ">".$themesArray['theme_name'][$key]."</option>\n";
100         } // END - foreach
101
102         // Remember content
103         $content = array(
104                 'form_action' => $formAction,
105                 'selection'   => $OUT
106         );
107
108         // Return generated selection
109         return loadTemplate('theme_select_form', true, $content);
110 }
111
112 // Get version from name
113 function getThemeVersion ($name) {
114         // Is the extension 'theme' installed?
115         if (!isExtensionActive('theme')) {
116                 // Then abort here
117                 return '!.!';
118         } // END - if
119
120         // Default version 'number'
121         $cver = '?.?';
122
123         // Is the cache entry there?
124         if (isset($GLOBALS['cache_array']['themes']['theme_ver'][$name])) {
125                 // Get the version from cache
126                 $cver = $GLOBALS['cache_array']['themes']['theme_ver'][$name];
127
128                 // Count up
129                 incrementStatsEntry('cache_hits');
130         } elseif (getExtensionVersion('cache') != '0.1.8') {
131                 // Load version from database
132                 $result = SQL_QUERY_ESC("SELECT `theme_ver` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
133                         array($name), __FUNCTION__, __LINE__);
134
135                 // Entry found?
136                 if (SQL_NUMROWS($result) == 1) {
137                         // Fetch data
138                         list($cver) = SQL_FETCHROW($result);
139                 } // END - if
140
141                 // Free result
142                 SQL_FREERESULT($result);
143         }
144
145         // Return version
146         return $cver;
147 }
148
149 // Checks wether a theme is found in db
150 function ifThemeExists ($name) {
151         // Get theme and is it not nul?
152         return ((isExtensionActive('theme')) && (getThemeId($name) > 0));
153 }
154
155 // Checks if a theme is active
156 function isThemeActive ($name) {
157         // Is the extension 'theme' installed?
158         if (!isExtensionActive('theme')) {
159                 // Then abort here
160                 return false;
161         } // END - if
162
163         // Default is nothing active
164         $active = false;
165
166         // Is the cache entry there?
167         if (isset($GLOBALS['cache_array']['themes']['theme_active'][$name])) {
168                 // Get the version from cache
169                 $active = ($GLOBALS['cache_array']['themes']['theme_active'][$name] == 'Y');
170
171                 // Count up
172                 incrementStatsEntry('cache_hits');
173         } elseif (getExtensionVersion('cache') != '0.1.8') {
174                 // Check if current theme is already imported or not
175                 $result = SQL_QUERY_ESC("SELECT `theme_active` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
176                         array($name), __FUNCTION__, __LINE__);
177
178                 // Is the theme active and installed?
179                 $active = (SQL_NUMROWS($result) == 1);
180
181                 // Free result
182                 SQL_FREERESULT($result);
183         }
184
185         // Return result
186         return $active;
187 }
188
189 // Gets current human-readable theme name
190 function getCurrentThemeName () {
191         // Is the extension 'theme' installed?
192         if (!isExtensionActive('theme')) {
193                 // Then abort here
194                 return 'default';
195         } // END - if
196
197         // Get the Uni* name
198         $name = getCurrentTheme();
199
200         // Is the cache entry there?
201         if (isset($GLOBALS['cache_array']['themes']['theme_name'][$name])) {
202                 // Get the version from cache
203                 $name = $GLOBALS['cache_array']['themes']['theme_name'][$name];
204
205                 // Count up
206                 incrementStatsEntry('cache_hits');
207         } elseif (getExtensionVersion('cache') != '0.1.8') {
208                 // Check if current theme is already imported or not
209                 $result = SQL_QUERY_ESC("SELECT `theme_name` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
210                         array($name), __FUNCTION__, __LINE__);
211
212                 // Load theme name
213                 list($name) = SQL_FETCHROW($result);
214
215                 // Free result
216                 SQL_FREERESULT($result);
217         }
218
219         // Return name
220         return $name;
221 }
222
223 // Get current theme name
224 function getActualTheme () {
225         // The default theme is 'default'... ;-)
226         $ret = 'default';
227
228         // Load default theme if not empty from configuration
229         if ((isConfigEntrySet('default_theme')) && (getConfig('default_theme') != '')) $ret = getConfig('default_theme');
230
231         if (!isSessionVariableSet('mxchange_theme')) {
232                 // Set default theme
233                 setTheme($ret);
234         } elseif ((isSessionVariableSet('mxchange_theme')) && (isExtensionInstalledAndNewer('sql_patches', '0.1.4'))) {
235                 //die("<pre>".print_r($GLOBALS['cache_array']['themes'], true)."</pre>");
236                 // Get theme from cookie
237                 $ret = getSession('mxchange_theme');
238
239                 // Is it valid?
240                 if ((!isExtensionActive('theme')) || (getThemeId($ret) == '0')) {
241                         // Fix it to default
242                         $ret = 'default';
243                 } // END - if
244         } elseif ((!isInstalled()) && ((isInstalling()) || (getOutputMode() == true)) && ((isGetRequestParameterSet('theme')) || (isPostRequestParameterSet('theme')))) {
245                 // Prepare filename for checking
246                 $themeFile = sprintf("theme/%s/theme.php", getRequestParameter('theme'));
247
248                 // Installation mode active
249                 if ((isGetRequestParameterSet('theme')) && (isIncludeReadable($theme))) {
250                         // Set cookie from URL data
251                         setTheme(getRequestParameter('theme'));
252                 } elseif (isIncludeReadable(sprintf("theme/%s/theme.php", secureString(postRequestParameter('theme'))))) {
253                         // Set cookie from posted data
254                         setTheme(secureString(postRequestParameter('theme')));
255                 }
256
257                 // Set return value
258                 $ret = getSession('mxchange_theme');
259         } else {
260                 // Invalid design, reset cookie
261                 setTheme($ret);
262         }
263
264         // Return theme value
265         return $ret;
266 }
267
268 // Setter for theme in session
269 function setTheme ($newTheme) {
270         setSession('mxchange_theme', $newTheme);
271 }
272
273 // Get id from theme
274 function getThemeId ($name) {
275         // Default id
276         $id = '0';
277
278         // Is the cache entry there?
279         if (isset($GLOBALS['cache_array']['themes']['id'][$name])) {
280                 // Get the version from cache
281                 $id = $GLOBALS['cache_array']['themes']['id'][$name];
282
283                 // Count up
284                 incrementStatsEntry('cache_hits');
285         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
286                 // Check if current theme is already imported or not
287                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
288                         array($name), __FUNCTION__, __LINE__);
289
290                 // Entry found?
291                 if (SQL_NUMROWS($result) == 1) {
292                         // Fetch data
293                         list($id) = SQL_FETCHROW($result);
294                 } // END - if
295
296                 // Free result
297                 SQL_FREERESULT($result);
298         }
299
300         // Return id
301         return $id;
302 }
303
304 ///////////////////////////////////////////////////////////////////////////////
305 //                              Only filter functions
306 ///////////////////////////////////////////////////////////////////////////////
307
308 // Filter for generic handling of theme change
309 function FILTER_HANDLE_THEME_CHANGE () {
310         // Check if new theme is selcted
311         if ((isPostRequestParameterSet('new_theme')) && (postRequestParameter('new_theme') != getCurrentTheme())) {
312                 // Set new theme for guests
313                 $newTheme = postRequestParameter('new_theme');
314
315                 // Change to new theme
316                 setTheme($newTheme);
317
318                 // Remove current from array
319                 removeIncludeFromPool('theme', sprintf("theme/%s/theme.php", getCurrentTheme()));
320
321                 // Add new theme
322                 addIncludeToPool('theme', sprintf("theme/%s/theme.php", $newTheme));
323         } // END - if
324 }
325
326 // Filter for settings theme from user profile, must be executed only if FILTER_FETCH_USER_DATA() ran before
327 function FILTER_SET_USERS_THEME () {
328         // Is the user data valid?
329         if (!isMember()) {
330                 // Do only run for logged in members
331                 debug_report_bug('Please only run this filter for logged in users.');
332         } // END - if
333
334         // Change to new theme
335         setTheme(getUserData('curr_theme'));
336 }
337
338 // [EOF]
339 ?>