Opps, not all elements for sprintf() has been set.
[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                 // Load it's theme.php file if found
81                 if (isThemeReadable($row['theme_path'])) {
82                         // And save all data in array
83                         loadInclude(sprintf('theme/%s/theme.php', secureString($row['theme_path'])));
84                         array_push($themesArray['theme_unix'], $row['theme_path']);
85                         array_push($themesArray['theme_name'], $row['theme_name']);
86                 } // END - if
87         } // END - while
88
89         // Free the result
90         sqlFreeResult($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         // Load template
108         $out = loadTemplate('theme_select_form', TRUE, $content);
109
110         // Return generated selection
111         return $out;
112 }
113
114 // Get version from name
115 function getThemeVersion ($name) {
116         // Is the extension 'theme' installed?
117         if (!isExtensionActive('theme')) {
118                 // Then abort here
119                 return '!.!';
120         } // END - if
121
122         // Default version 'number'
123         $cver = '?.?';
124
125         // Is the cache entry there?
126         if (isset($GLOBALS['cache_array']['themes']['theme_version'][$name])) {
127                 // Get the version from cache
128                 $cver = $GLOBALS['cache_array']['themes']['theme_version'][$name];
129
130                 // Count up
131                 incrementStatsEntry('cache_hits');
132         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
133                 // Load version from database
134                 $result = sqlQueryEscaped("SELECT `theme_ver` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
135                         array($name), __FUNCTION__, __LINE__);
136
137                 // Entry found?
138                 if (sqlNumRows($result) == 1) {
139                         // Fetch data
140                         list($cver) = sqlFetchRow($result);
141                 } // END - if
142
143                 // Free result
144                 sqlFreeResult($result);
145         }
146
147         // Return version
148         return $cver;
149 }
150
151 // Checks whether a theme is found in db
152 function ifThemeExists ($name) {
153         // Get theme and is it not nul?
154         return (((isExtensionActive('theme')) || (getModule() == 'admin')) && (isValidId(getThemeId($name))));
155 }
156
157 // Checks if a theme is active
158 function isThemeActive ($name) {
159         // Is the extension 'theme' installed?
160         if (!isExtensionActive('theme')) {
161                 // Then abort here
162                 return FALSE;
163         } // END - if
164
165         // Default is nothing active
166         $active = FALSE;
167
168         // Is the cache entry there?
169         if (isset($GLOBALS['cache_array']['themes']['theme_active'][$name])) {
170                 // Get the version from cache
171                 $active = ($GLOBALS['cache_array']['themes']['theme_active'][$name] == 'Y');
172
173                 // Count up
174                 incrementStatsEntry('cache_hits');
175         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
176                 // Check if current theme is already imported or not
177                 $result = sqlQueryEscaped("SELECT `theme_active` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
178                         array($name), __FUNCTION__, __LINE__);
179
180                 // Is the theme active and installed?
181                 $active = (sqlNumRows($result) == 1);
182
183                 // Free result
184                 sqlFreeResult($result);
185         }
186
187         // Return result
188         return $active;
189 }
190
191 // Gets current human-readable theme name
192 function getCurrentThemeName () {
193         // Is the extension 'theme' installed?
194         if (!isExtensionActive('theme')) {
195                 // Then abort here
196                 return 'default';
197         } // END - if
198
199         // Get the Uni* name
200         $name = getCurrentTheme();
201
202         // Is the cache entry there?
203         if (isset($GLOBALS['cache_array']['themes']['theme_name'][$name])) {
204                 // Get the version from cache
205                 $name = $GLOBALS['cache_array']['themes']['theme_name'][$name];
206
207                 // Count up
208                 incrementStatsEntry('cache_hits');
209         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
210                 // Check if current theme is already imported or not
211                 $result = sqlQueryEscaped("SELECT `theme_name` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' AND `theme_active`='Y' LIMIT 1",
212                         array($name), __FUNCTION__, __LINE__);
213
214                 // Load theme name
215                 list($name) = sqlFetchRow($result);
216
217                 // Free result
218                 sqlFreeResult($result);
219         }
220
221         // Return name
222         return $name;
223 }
224
225 // Get current theme name
226 function getActualTheme () {
227         // The default theme is 'default'... ;-)
228         $ret = 'default';
229
230         // Load default theme if not empty from configuration
231         if ((isConfigEntrySet('default_theme')) && (getConfig('default_theme') != '')) {
232                 $ret = getConfig('default_theme');
233         } // END - if
234
235         if (!isMailerThemeSet()) {
236                 // Set default theme
237                 setMailerTheme($ret);
238         } elseif ((isMailerThemeSet()) && (isExtensionInstalledAndNewer('sql_patches', '0.1.4'))) {
239                 //die("<pre>".print_r($GLOBALS['cache_array']['themes'], TRUE)."</pre>");
240                 // Get theme from cookie
241                 $ret = getSession('mailer_theme');
242
243                 // Is it valid?
244                 if ((!isExtensionActive('theme')) || (getThemeId($ret) == '0')) {
245                         // Fix it to default
246                         $ret = 'default';
247                 } // END - if
248         } elseif ((!isInstalled()) && ((isInstalling()) || (isHtmlOutputMode())) && ((isGetRequestElementSet('theme')) || (isPostRequestElementSet('theme')))) {
249                 // Prepare filename for checking
250                 $themeFile = sprintf('theme/%s/theme.php', getRequestElement('theme'));
251
252                 // Installation mode active
253                 if ((isGetRequestElementSet('theme')) && (isThemeReadable(getRequestElement('theme')))) {
254                         // Set cookie from URL data
255                         setMailerTheme(getRequestElement('theme'));
256                 } elseif ((isPostRequestElementSet('theme')) && (isThemeReadable(postRequestElement('theme')))) {
257                         // Set cookie from posted data
258                         setMailerTheme(postRequestElement('theme'));
259                 }
260
261                 // Set return value
262                 $ret = getSession('mailer_theme');
263         } else {
264                 // Invalid design, reset cookie
265                 setMailerTheme($ret);
266         }
267
268         // Return theme value
269         return $ret;
270 }
271
272 // Get id from theme
273 function getThemeId ($name) {
274         // Default id
275         $id = '0';
276
277         // Is the cache entry there?
278         if (isset($GLOBALS['cache_array']['themes']['id'][$name])) {
279                 // Get the version from cache
280                 $id = $GLOBALS['cache_array']['themes']['id'][$name];
281
282                 // Count up
283                 incrementStatsEntry('cache_hits');
284         } elseif (isExtensionInstalledAndNewer('cache', '0.1.8')) {
285                 // Check if current theme is already imported or not
286                 $result = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_themes` WHERE `theme_path`='%s' LIMIT 1",
287                         array($name), __FUNCTION__, __LINE__);
288
289                 // Entry found?
290                 if (sqlNumRows($result) == 1) {
291                         // Fetch data
292                         list($id) = sqlFetchRow($result);
293                 } // END - if
294
295                 // Free result
296                 sqlFreeResult($result);
297         }
298
299         // Return id
300         return $id;
301 }
302
303 // [EOF]
304 ?>