8539bff033401c4c406958c283de9c0e894b3600
[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 - 2013 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 = 'modules.php?module='. $mod;
51         if (!empty($what)) $formAction .= '&amp;what=' . $what;
52
53         // Initialize array
54         $themesArray = array(
55                 'theme_unix'   => array(), // Unix name from filesystem
56                 'theme_name'   => array()  // Title
57         );
58
59         // Only activated themes for the user
60         $add = " WHERE `theme_active`='Y'";
61
62         // Is there admin?
63         if (isAdmin()) {
64                 // Then display all themes
65                 $add = '';
66         } // END - if
67
68         // Select all themes we want
69         $result = sqlQuery('SELECT
70         `theme_path`,
71         `theme_name`
72 FROM
73         `{?_MYSQL_PREFIX?}_themes`
74 ' . $add . '
75 ORDER BY
76         `theme_name` ASC', __FUNCTION__, __LINE__);
77
78         // Load all themes
79         while ($row = sqlFetchArray($result)) {
80                 // Construct relative include file name
81                 $inc = sprintf("theme/%s/theme.php", secureString($row['theme_path']));
82
83                 // Load it's theme.php file if found
84                 if (isIncludeReadable($inc)) {
85                         // And save all data in array
86                         loadInclude($inc);
87                         array_push($themesArray['theme_unix'], $row['theme_path']);
88                         array_push($themesArray['theme_name'], $row['theme_name']);
89                 } // END - if
90         } // END - while
91
92         // Free the result
93         sqlFreeResult($result);
94
95         // Construct selection form for the box template
96         // @TODO Can't this be rewritten to an API function?
97         $OUT = '';
98         foreach ($themesArray['theme_unix'] as $key => $theme) {
99                 $OUT .= '  <option value="' . $theme . '"';
100                 if ($theme == getCurrentTheme()) $OUT .= ' selected="selected"';
101                 $OUT .= '>' . $themesArray['theme_name'][$key] . '</option>';
102         } // END - foreach
103
104         // Remember content
105         $content = array(
106                 'form_action' => $formAction,
107                 'selection'   => $OUT
108         );
109
110         // Load template
111         $out = loadTemplate('theme_select_form', TRUE, $content);
112
113         // Return generated selection
114         return $out;
115 }
116
117 // Get version from name
118 function getThemeVersion ($name) {
119         // Is the extension 'theme' installed?
120         if (!isExtensionActive('theme')) {
121                 // Then abort here
122                 return '!.!';
123         } // END - if
124
125         // Default version 'number'
126         $cver = '?.?';
127
128         // Is the cache entry there?
129         if (isset($GLOBALS['cache_array']['themes']['theme_version'][$name])) {
130                 // Get the version from cache
131                 $cver = $GLOBALS['cache_array']['themes']['theme_version'][$name];
132
133                 // Count up
134                 incrementStatsEntry('cache_hits');
135         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
136                 // Load version from database
137                 $result = sqlQueryEscaped("SELECT `theme_ver` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
138                         array($name), __FUNCTION__, __LINE__);
139
140                 // Entry found?
141                 if (sqlNumRows($result) == 1) {
142                         // Fetch data
143                         list($cver) = sqlFetchRow($result);
144                 } // END - if
145
146                 // Free result
147                 sqlFreeResult($result);
148         }
149
150         // Return version
151         return $cver;
152 }
153
154 // Checks whether a theme is found in db
155 function ifThemeExists ($name) {
156         // Get theme and is it not nul?
157         return (((isExtensionActive('theme')) || (getModule() == 'admin')) && (isValidId(getThemeId($name))));
158 }
159
160 // Checks if a theme is active
161 function isThemeActive ($name) {
162         // Is the extension 'theme' installed?
163         if (!isExtensionActive('theme')) {
164                 // Then abort here
165                 return FALSE;
166         } // END - if
167
168         // Default is nothing active
169         $active = FALSE;
170
171         // Is the cache entry there?
172         if (isset($GLOBALS['cache_array']['themes']['theme_active'][$name])) {
173                 // Get the version from cache
174                 $active = ($GLOBALS['cache_array']['themes']['theme_active'][$name] == 'Y');
175
176                 // Count up
177                 incrementStatsEntry('cache_hits');
178         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
179                 // Check if current theme is already imported or not
180                 $result = sqlQueryEscaped("SELECT `theme_active` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
181                         array($name), __FUNCTION__, __LINE__);
182
183                 // Is the theme active and installed?
184                 $active = (sqlNumRows($result) == 1);
185
186                 // Free result
187                 sqlFreeResult($result);
188         }
189
190         // Return result
191         return $active;
192 }
193
194 // Gets current human-readable theme name
195 function getCurrentThemeName () {
196         // Is the extension 'theme' installed?
197         if (!isExtensionActive('theme')) {
198                 // Then abort here
199                 return 'default';
200         } // END - if
201
202         // Get the Uni* name
203         $name = getCurrentTheme();
204
205         // Is the cache entry there?
206         if (isset($GLOBALS['cache_array']['themes']['theme_name'][$name])) {
207                 // Get the version from cache
208                 $name = $GLOBALS['cache_array']['themes']['theme_name'][$name];
209
210                 // Count up
211                 incrementStatsEntry('cache_hits');
212         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
213                 // Check if current theme is already imported or not
214                 $result = sqlQueryEscaped("SELECT `theme_name` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
215                         array($name), __FUNCTION__, __LINE__);
216
217                 // Load theme name
218                 list($name) = sqlFetchRow($result);
219
220                 // Free result
221                 sqlFreeResult($result);
222         }
223
224         // Return name
225         return $name;
226 }
227
228 // Get current theme name
229 function getActualTheme () {
230         // The default theme is 'default'... ;-)
231         $ret = 'default';
232
233         // Load default theme if not empty from configuration
234         if ((isConfigEntrySet('default_theme')) && (getConfig('default_theme') != '')) {
235                 $ret = getConfig('default_theme');
236         } // END - if
237
238         if (!isMailerThemeSet()) {
239                 // Set default theme
240                 setMailerTheme($ret);
241         } elseif ((isMailerThemeSet()) && (isExtensionInstalledAndNewer('sql_patches', '0.1.4'))) {
242                 //die("<pre>".print_r($GLOBALS['cache_array']['themes'], TRUE)."</pre>");
243                 // Get theme from cookie
244                 $ret = getSession('mailer_theme');
245
246                 // Is it valid?
247                 if ((!isExtensionActive('theme')) || (getThemeId($ret) == '0')) {
248                         // Fix it to default
249                         $ret = 'default';
250                 } // END - if
251         } elseif ((!isInstalled()) && ((isInstalling()) || (isHtmlOutputMode())) && ((isGetRequestElementSet('theme')) || (isPostRequestElementSet('theme')))) {
252                 // Prepare filename for checking
253                 $themeFile = sprintf("theme/%s/theme.php", getRequestElement('theme'));
254
255                 // Installation mode active
256                 if ((isGetRequestElementSet('theme')) && (isIncludeReadable($theme))) {
257                         // Set cookie from URL data
258                         setMailerTheme(getRequestElement('theme'));
259                 } elseif (isIncludeReadable(sprintf("theme/%s/theme.php", postRequestElement('theme')))) {
260                         // Set cookie from posted data
261                         setMailerTheme(postRequestElement('theme'));
262                 }
263
264                 // Set return value
265                 $ret = getSession('mailer_theme');
266         } else {
267                 // Invalid design, reset cookie
268                 setMailerTheme($ret);
269         }
270
271         // Return theme value
272         return $ret;
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 = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
290                         array($name), __FUNCTION__, __LINE__);
291
292                 // Entry found?
293                 if (sqlNumRows($result) == 1) {
294                         // Fetch data
295                         list($id) = sqlFetchRow($result);
296                 } // END - if
297
298                 // Free result
299                 sqlFreeResult($result);
300         }
301
302         // Return id
303         return $id;
304 }
305
306 // [EOF]
307 ?>