root/branches/multiuser/themes.php

Revision 1465, 9.2 kB (checked in by mbonetti, 4 years ago)

added MIDP to mobile device detection, whatever that is

Line 
1<?php
2###############################################################################
3# Gregarius - A PHP based RSS aggregator.
4# Copyright (C) 2003 - 2006 Marco Bonetti
5#
6###############################################################################
7# This program is free software and open source software; you can redistribute
8# it and/or modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of the License,
10# or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15# more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
20# http://www.gnu.org/licenses/gpl.html
21#
22###############################################################################
23# E-mail:      mbonetti at gmail dot com
24# Web page:    http://gregarius.net/
25#
26###############################################################################
27
28
29function getThemeInfo($theme) {
30
31    $path = "../".RSS_THEME_DIR."/$theme/.themeinfo";
32    $ret = array(
33               'name' => '',
34               'url' => '',
35               'official' => false,
36               'fsname' => $theme,
37               'description' => '',
38               'htmltheme' => true,
39               'version' => "1.0",
40               'author' => '',
41               'screenshot' => ''
42           );
43    if (file_exists($path)) {
44        $f = @fopen($path,'r');
45        $contents = "";
46        if ($f) {
47            $contents .= fread($f, filesize($path));
48            @fclose($f);
49        } else {
50            $contents = "";
51        }
52
53        if ($contents && preg_match_all("/^\s?([^:]+):(.*)$/m",$contents,$matches,PREG_SET_ORDER)) {
54            foreach($matches as $match) {
55                $key = trim(strtolower($match[1]));
56                $val = trim($match[2]);
57                if (array_key_exists($key,$ret)) {
58                    if ($val == 'true') {
59                        $ret[$key] = true;
60                    }
61                    elseif ($val == 'false') {
62                        $ret[$key] = false;
63                    }
64                    else {
65                        $ret[$key] = $val;
66                    }
67                }
68            }
69        }
70    }
71    return $ret;
72}
73
74function getThemePath($path=null) {
75    list($theme,$media) = getActualTheme();
76    if (null === $path)
77        $path = getPath();
78    return $path.RSS_THEME_DIR."/$theme/$media/";
79}
80
81/**
82 * Returns an array holding the "main" theme to use,
83 * as well as the detected "media" (@see getThemeMedia)
84 */
85function getActualTheme() {
86    static $ret;
87
88    if ($ret) {
89        return $ret;
90    }
91
92
93    // Theme
94    $theme = getConfig('rss.output.theme');
95    if (null === $theme)
96        $theme = 'default';
97    if (defined('THEME_OVERRIDE')) {
98        $theme = THEME_OVERRIDE;
99    }
100    elseif (isset($_GET['theme'])) {
101                $theme = sanitize($_GET['theme'],RSS_SANITIZER_WORDS);   
102    }
103
104   
105   
106    // Media
107    $media = getThemeMedia();
108   
109    if( !file_exists(GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/") )
110        $theme = 'default';
111
112        $ret = array($theme,$media);
113    return $ret;
114}
115
116
117/**
118 * Returns the theme's "media" component, e.g. 'web',
119 * 'rss' or 'mobile'.
120 */
121function getThemeMedia() {
122    static $media;
123
124    if ($media) {
125        return $media;
126    }
127
128    // Default to "web".
129    $media = 'web';
130
131    // Has the user specified anything?
132    if (isset($_GET['rss'])) {
133        $media = 'rss';
134    }
135    elseif (isset($_SESSION['mobile']) || isset($_REQUEST['mobile']) || isMobileDevice()) {
136        $media = 'mobile';
137    }
138
139    // This is here so that auto-detected (e.g. mobile) medias
140    // can be overridden.
141    if (isset($_REQUEST['media'])) {
142        $media = sanitize($_REQUEST['media'], RSS_SANITIZER_WORDS);
143    }
144
145    // Finally: let plugins voice their opinion
146    $media = rss_plugin_hook('rss.plugins.thememedia',$media);
147
148    return $media;
149}
150
151/**
152 * Dumb dunciton to detect mobile devices based on the passed user-agent.
153 * This definitely needs some heavy tweaking.
154 */
155function isMobileDevice() {
156    static $ret;
157    if ($ret !== NULL) {
158        return $ret;
159    } else {
160        $ret = false;
161        if (isset($_SERVER['HTTP_USER_AGENT'])) {
162            $ua = $_SERVER['HTTP_USER_AGENT'];
163            $ua_lwr = strtolower( $ua );
164            $ret = strpos($ua, 'SonyEricsson') !== FALSE
165                        || strpos($ua, 'Symbian') !== FALSE
166                || strpos($ua, 'Nokia') !== FALSE
167                || strpos($ua, 'Mobile') !== FALSE
168                || strpos($ua, 'Windows CE') !== FALSE
169                || strpos($ua, 'EPOC') !== FALSE
170                || strpos($ua, 'Opera Mini') !== FALSE
171                || strpos($ua_lwr, 'j2me') !== FALSE
172                || strpos($ua,'MIDP-') !== FALSE
173                || strpos($ua, 'Netfront') !== FALSE;
174            // if none of those matched, let's have a gander at grabbing the resolution...
175            if (!$ret && eregi( "([0-9]{3})x([0-9]{3})", $ua, $matches ) ) {
176                if ($matches[1]<600 || $matches[2]<600) {
177                    $ret = true; //one of the screen dimensions is less than 600 - we'll call it a mobile device
178                }
179            }
180        }
181
182        return $ret;
183    }
184}
185
186function rss_theme_option_ref_obj_from_theme($theme=null, $media=null) {
187    if ($theme===null) {
188        list($theme,$media) = getActualTheme();
189    }
190   
191    $ref_obj = "theme.$theme";
192    if( $media !== null )
193        $ref_obj .= ".$media";
194       
195    return $ref_obj;
196}
197
198function rss_theme_get_option($option, $theme=null, $media=null) {
199    return getProperty(rss_theme_option_ref_obj_from_theme($theme,$media), $option);
200}
201
202function rss_theme_set_option($option, $value, $theme, $media) {
203    setProperty(rss_theme_option_ref_obj_from_theme($theme,$media), $option, 'theme', $value);
204}
205
206function rss_theme_delete_option($option, $theme=null, $media=null) {
207    return deleteProperty(rss_theme_option_ref_obj_from_theme($theme,$media), $option);
208}
209
210function rss_theme_config_override_option_name_mangle($config_key) {
211    return preg_replace( '/^rss\./', 'rss.prop.theme.', $config_key ) . '.override';
212}
213
214function rss_theme_config_override_option($config_key, $default, $theme=null, $media=null) {
215    $ret = getProperty(rss_theme_option_ref_obj_from_theme($theme,$media), rss_theme_config_override_option_name_mangle($config_key));
216    if( $ret === null )
217        $ret = $default;
218    rss_config_override($config_key,$ret);
219    return $ret;
220}
221
222function rss_theme_set_config_override_option($config_key, $value, $theme=null, $media=null) {
223    setProperty(rss_theme_option_ref_obj_from_theme($theme,$media), rss_theme_config_override_option_name_mangle($config_key), 'theme', $value);
224}
225
226function rss_theme_delete_config_override_option($config_key, $theme=null, $media=null) {
227    deleteProperty(rss_theme_option_ref_obj_from_theme($theme,$media), rss_theme_config_override_option_name_mangle($config_key));
228}
229
230function loadSchemeList($pretty, $theme=null, $media=null) {
231    if ($theme===null) {
232        list($theme,$media) = getActualTheme();
233    }
234
235    $ret = array( '(use default scheme)' );
236   
237        if( file_exists( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes" ) )
238        {
239                if( $checkDir = opendir( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes" ) ) {
240                        while($file = readdir($checkDir)){
241                                if($file != "." && $file != ".." && $file != ".svn"){
242                                   if(file_exists(GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/" . $file) && is_dir(GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/" . $file)){
243                                           if( $pretty )
244                                                        $theme_info = getThemeInfo( "$theme/$media/schemes/$file" );
245                                           if( $pretty && isset( $theme_info['name'] ) && $theme_info['name'] !== '' )
246                                                   $ret[] = str_replace( ",", "_", $theme_info['name'] );
247                                           else
248                                                   $ret[] = str_replace( ",", "_", $file );
249                                   }
250                                }
251                        }
252                }
253        }
254   
255    return $ret;
256}
257
258
259function rss_scheme_stylesheets($theme=null, $media=null) {
260    if ($theme===null) {
261        list($theme,$media) = getActualTheme();
262    }
263
264    $ret = getProperty(rss_theme_option_ref_obj_from_theme($theme,$media), rss_theme_config_override_option_name_mangle('rss.output.theme.scheme'));
265    if( $ret === null )
266                return "";
267
268        $arr = explode(',',$ret);
269        $ret = "";
270        $idx = array_pop($arr);
271        foreach (loadSchemeList( false, $theme, $media) as $i => $val) {
272                if ($i == $idx) {
273                        if( $i > 0 ) {
274                                if( file_exists( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/$val" ) && is_dir( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/$val" ) ) {
275                    foreach( glob( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/$val/*.css" ) as $file ) {
276                        $file = substr( $file, strlen( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/$val/" ) );
277                        $file = getPath().RSS_THEME_DIR."/$theme/$media/schemes/$val/$file";
278                        $ret .= "       <link rel=\"stylesheet\" type=\"text/css\" href=\"$file\" />\n";
279                    }
280                                }
281                        }
282                        break;
283                }
284        }
285       
286        return $ret;
287}
288
289?>
Note: See TracBrowser for help on using the browser.