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