root/trunk/gregarius/themes.php

Revision 1787, 9.7 kB (checked in by cfriesen, 4 months ago)

This makes my N800 a mobile

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
29 function 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
74 function 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  */
85 function 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  */
121 function 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  */
155 function 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
165             $ret  =
166                     (strpos($ua,    'SonyEricsson') !== FALSE)
167                 ||    (strpos($ua,     'Nokia') !== FALSE)
168                 ||    (strpos($ua'Tablet') !== FALSE)
169                 ||    (strpos($ua,     'MIDP') !== FALSE)
170                 ||    (strpos($ua_lwr,'mobile') !== FALSE)
171                 ||    (strpos($ua,     'Windows CE') !== FALSE)
172                 ||    (strpos($ua,     'EPOC') !== FALSE)
173                 ||    (strpos($ua,     'Opera Mini') !== FALSE)
174                 ||    (strpos($ua,     'UP.Browser') !== FALSE)
175                 ||    (strpos($ua_lwr,'j2me') !== FALSE)
176                 ||    (strpos($ua,    'SGH-') !== FALSE) // Samsung
177                 ||    (strpos($ua_lwr,'samsung') !== FALSE)
178                 ||    (strpos($ua_lwr,'netfront') !== FALSE);
179
180
181             // if none of those matched, let's have a gander at grabbing the resolution...
182             if (!$ret && eregi( "([0-9]{3})x([0-9]{3})", $ua, $matches ) ) {
183                 if ($matches[1]<600 || $matches[2]<600) {
184                     $ret = true; //one of the screen dimensions is less than 600 - we'll call it a mobile device
185                 }
186             }
187         }
188
189         return $ret;
190     }
191 }
192
193 function rss_theme_option_ref_obj_from_theme($theme=null, $media=null) {
194     if ($theme===null) {
195         list($theme,$media) = getActualTheme();
196     }
197
198     $ref_obj = "theme.$theme";
199     if( $media !== null )
200         $ref_obj .= ".$media";
201
202     return $ref_obj;
203 }
204
205 function rss_theme_get_option($option, $theme=null, $media=null) {
206     return getProperty(rss_theme_option_ref_obj_from_theme($theme,$media), $option);
207 }
208
209 function rss_theme_set_option($option, $value, $theme, $media) {
210     setProperty(rss_theme_option_ref_obj_from_theme($theme,$media), $option, 'theme', $value);
211 }
212
213 function rss_theme_delete_option($option, $theme=null, $media=null) {
214     return deleteProperty(rss_theme_option_ref_obj_from_theme($theme,$media), $option);
215 }
216
217 function rss_theme_config_override_option_name_mangle($config_key) {
218     return preg_replace( '/^rss\./', 'rss.prop.theme.', $config_key ) . '.override';
219 }
220
221 function rss_theme_config_override_option($config_key, $default, $theme=null, $media=null) {
222     $ret = getProperty(rss_theme_option_ref_obj_from_theme($theme,$media), rss_theme_config_override_option_name_mangle($config_key));
223     if( $ret === null )
224         $ret = $default;
225     rss_config_override($config_key,$ret);
226     return $ret;
227 }
228
229 function rss_theme_set_config_override_option($config_key, $value, $theme=null, $media=null) {
230     setProperty(rss_theme_option_ref_obj_from_theme($theme,$media), rss_theme_config_override_option_name_mangle($config_key), 'theme', $value);
231 }
232
233 function rss_theme_delete_config_override_option($config_key, $theme=null, $media=null) {
234     deleteProperty(rss_theme_option_ref_obj_from_theme($theme,$media), rss_theme_config_override_option_name_mangle($config_key));
235 }
236
237 function loadSchemeList($pretty, $theme=null, $media=null) {
238     if ($theme===null) {
239         list($theme,$media) = getActualTheme();
240     }
241
242     $ret = array( '(use default scheme)' );
243
244     if( file_exists( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes" ) ) {
245         if( $checkDir = opendir( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes" ) ) {
246             while($file = readdir($checkDir)) {
247                 if($file != "." && $file != ".." && $file != ".svn") {
248                     if(file_exists(GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/" . $file) && is_dir(GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/" . $file)) {
249                         if( $pretty )
250                             $theme_info = getThemeInfo( "$theme/$media/schemes/$file" );
251                         if( $pretty && isset( $theme_info['name'] ) && $theme_info['name'] !== '' )
252                             $ret[] = str_replace( ",", "_", $theme_info['name'] );
253                         else
254                             $ret[] = str_replace( ",", "_", $file );
255                     }
256                 }
257             }
258         }
259     }
260
261     return $ret;
262 }
263
264
265 function rss_scheme_stylesheets($theme=null, $media=null) {
266     if ($theme===null) {
267         list($theme,$media) = getActualTheme();
268     }
269
270     $ret = getProperty(rss_theme_option_ref_obj_from_theme($theme,$media), rss_theme_config_override_option_name_mangle('rss.output.theme.scheme'));
271     if( $ret === null )
272         return "";
273
274     $arr = explode(',',$ret);
275     $ret = "";
276     $idx = array_pop($arr);
277     foreach (loadSchemeList( false, $theme, $media) as $i => $val) {
278         if ($i == $idx) {
279             if( $i > 0 ) {
280                 if( file_exists( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/$val" ) && is_dir( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/$val" ) ) {
281                     foreach( glob( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/$val/*.css" ) as $file ) {
282                         $file = substr( $file, strlen( GREGARIUS_HOME.RSS_THEME_DIR."/$theme/$media/schemes/$val/" ) );
283                         $file = getPath().RSS_THEME_DIR."/$theme/$media/schemes/$val/$file";
284                         $ret .= "    <link rel=\"stylesheet\" type=\"text/css\" href=\"$file\" />\n";
285                     }
286                 }
287             }
288             break;
289         }
290     }
291
292     return $ret;
293 }
294
295 ?>
296
Note: See TracBrowser for help on using the browser.