1a8e05bba28b6aa0656cfaa46313db2c1d1c577d
[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 - 2012 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         // Is there admin?
64         if (isAdmin()) $add = '';
65
66         // Select all themes we want
67         $result = SQL_QUERY('SELECT
68         `theme_path`,
69         `theme_name`
70 FROM
71         `{?_MYSQL_PREFIX?}_themes`
72 ' . $add . '
73 ORDER BY
74         `theme_name` ASC', __FUNCTION__, __LINE__);
75
76         // Load all themes
77         while ($content = SQL_FETCHARRAY($result)) {
78                 // Construct relative include file name
79                 $inc = sprintf("theme/%s/theme.php", secureString($content['theme_path']));
80
81                 // Load it's theme.php file if found
82                 if (isIncludeReadable($inc)) {
83                         // And save all data in array
84                         loadInclude($inc);
85                         array_push($themesArray['theme_unix'], $content['theme_path']);
86                         array_push($themesArray['theme_name'], $GLOBALS['theme_data']['name']);
87                 } // END - if
88         } // END - while
89
90         // Free the result
91         SQL_FREERESULT($result);
92
93         // Construct selection form for the box template
94         // @TODO Can't this be rewritten to an API function?
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>';
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_version'][$name])) {
125                 // Get the version from cache
126                 $cver = $GLOBALS['cache_array']['themes']['theme_version'][$name];
127
128                 // Count up
129                 incrementStatsEntry('cache_hits');
130         } elseif (isExtensionInstalledAndNewer('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 whether 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 (isExtensionInstalledAndNewer('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 (isExtensionInstalledAndNewer('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') != '')) {
230                 $ret = getConfig('default_theme');
231         } // END - if
232
233         if (!isMailerThemeSet()) {
234                 // Set default theme
235                 setMailerTheme($ret);
236         } elseif ((isMailerThemeSet()) && (isExtensionInstalledAndNewer('sql_patches', '0.1.4'))) {
237                 //die("<pre>".print_r($GLOBALS['cache_array']['themes'], TRUE)."</pre>");
238                 // Get theme from cookie
239                 $ret = getSession('mailer_theme');
240
241                 // Is it valid?
242                 if ((!isExtensionActive('theme')) || (getThemeId($ret) == '0')) {
243                         // Fix it to default
244                         $ret = 'default';
245                 } // END - if
246         } elseif ((!isInstalled()) && ((isInstalling()) || (isHtmlOutputMode())) && ((isGetRequestElementSet('theme')) || (isPostRequestElementSet('theme')))) {
247                 // Prepare filename for checking
248                 $themeFile = sprintf("theme/%s/theme.php", getRequestElement('theme'));
249
250                 // Installation mode active
251                 if ((isGetRequestElementSet('theme')) && (isIncludeReadable($theme))) {
252                         // Set cookie from URL data
253                         setMailerTheme(getRequestElement('theme'));
254                 } elseif (isIncludeReadable(sprintf("theme/%s/theme.php", postRequestElement('theme')))) {
255                         // Set cookie from posted data
256                         setMailerTheme(postRequestElement('theme'));
257                 }
258
259                 // Set return value
260                 $ret = getSession('mailer_theme');
261         } else {
262                 // Invalid design, reset cookie
263                 setMailerTheme($ret);
264         }
265
266         // Return theme value
267         return $ret;
268 }
269
270 // Get id from theme
271 function getThemeId ($name) {
272         // Default id
273         $id = '0';
274
275         // Is the cache entry there?
276         if (isset($GLOBALS['cache_array']['themes']['id'][$name])) {
277                 // Get the version from cache
278                 $id = $GLOBALS['cache_array']['themes']['id'][$name];
279
280                 // Count up
281                 incrementStatsEntry('cache_hits');
282         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
283                 // Check if current theme is already imported or not
284                 $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
285                         array($name), __FUNCTION__, __LINE__);
286
287                 // Entry found?
288                 if (SQL_NUMROWS($result) == 1) {
289                         // Fetch data
290                         list($id) = SQL_FETCHROW($result);
291                 } // END - if
292
293                 // Free result
294                 SQL_FREERESULT($result);
295         }
296
297         // Return id
298         return $id;
299 }
300
301 // [EOF]
302 ?>