/[refeed]/trunk/library/RF/Utility.functions.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/library/RF/Utility.functions.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Wed Jul 5 00:27:49 2006 UTC (17 years, 11 months ago) by dpavlin
File size: 11563 byte(s)
make working copy of trunk
1 <?php
2 // vim: ts=4 foldcolumn=4 foldmethod=marker
3 /**
4 * Utility functions used from other scripts
5 *
6 * This file is part of Reblog,
7 * a derivative work of Feed On Feeds.
8 *
9 * Distributed under the Gnu Public License.
10 *
11 * @package Refeed
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @author Michal Migurski <mike@stamen.com>
14 * @author Michael Frumin <mfrumin@eyebeam.org>
15 * @copyright ©2004 Michael Frumin, Michal Migurski
16 * @link http://reblog.org Reblog
17 * @link http://feedonfeeds.com Feed On Feeds
18 * @version $Revision: 1.14 $
19 */
20
21 /*
22 function ref_can_boolean_search()
23 {
24 if(isset($GLOBALS['REF_MYSQL_SUPPORTS_BOOLEAN_SEARCH']))
25 return $GLOBALS['REF_MYSQL_SUPPORTS_BOOLEAN_SEARCH'];
26
27 $q = sprintf("SELECT *
28 FROM %s AS it
29 WHERE id = 1
30 AND MATCH (it.title, it.link, it.content, it.dccreator, it.dcsubject) AGAINST ('test' IN BOOLEAN MODE)",
31 REF_ITEM_TABLE);
32
33 $res = $GLOBALS['REF_DBH']->query($q);
34 $GLOBALS['REF_MYSQL_SUPPORTS_BOOLEAN_SEARCH'] = (DB::isError($res) ? false : true);
35
36 return $GLOBALS['REF_MYSQL_SUPPORTS_BOOLEAN_SEARCH'];
37 }
38 */
39
40 /**
41 * Interpret an array looking for object literals.
42 *
43 * if any of the elements in the parameters array look like
44 * {"jsonclass":["constructor", [param1, ], "prop1": ...} where
45 * "constructor" is a valid class name, they are automatically
46 * replaced by instances of that class per the class-hinting
47 * section of the JSON spec.
48 *
49 * Old style, soon to be deprecated:
50 *
51 * if any of the elements in the parameters array look like
52 * {type:'...', args:{...}}, they are assumed to represent objects
53 * to be instantiated according the $class_mappings list.
54 *
55 * Mapped classes all assume a single, associative array constructor argument.
56 *
57 * @param array $params Array of parameters, modified recursively in-place
58 * @param array $class_mappings Array of type:class mappings.
59 * Keys are types, values are classes.
60 *
61 * @see http://json-rpc.org/specs.xhtml
62 */
63 function class_hints(&$params, $class_mappings=array())
64 {
65 if(is_array($params))
66 foreach($params as $p => $param)
67 if(isset($param['jsonclass']) && class_exists($param['jsonclass'][0]) && is_array($param['jsonclass'][1])) {
68 // new-style JSON-RPC class hinting
69 $class = $param['jsonclass'][0];
70 $args = array();
71
72 foreach($param['jsonclass'][1] as $a => $arg)
73 $args[$a] = preg_replace('#\s+#s', ' ', var_export($arg, 1));
74
75 eval('$params[$p] = new '.$class.'('.join(', ', $args).');');
76
77 foreach($param as $name => $value)
78 if($name != 'jsonclass')
79 $params[$p]->$name = $value;
80
81 } elseif(isset($param['type']) && isset($class_mappings[$param['type']])) {
82 // old-style reblog alpha class hinting, soon to be deprecated
83 $params[$p] = new $class_mappings[$param['type']]($param['args']);
84
85 } else {
86 // otherwise, we may want to look deeper
87 class_hints($params[$p], $class_mappings);
88
89 }
90 }
91
92 /**
93 * Initialize globals to be used by Reblog
94 *
95 * @param RF_Controller $controller Assigned to $GLOBALS['REBLOG_CONTROLLER']
96 * @param RF_User $user Assigned to $GLOBALS['REBLOG_USER']
97 * @param Smarty $view Assigned to $GLOBALS['REBLOG_VIEW']
98 */
99 function initialize_globals(&$controller, &$user, &$view)
100 {
101 $GLOBALS['REBLOG_CONTROLLER'] =& $controller;
102 $GLOBALS['REBLOG_USER'] =& $user;
103 $GLOBALS['REBLOG_VIEW'] =& $view;
104 }
105
106 /**
107 * Initialize session variables.
108 *
109 * @param array $ARGS Associative array of arguments, if any - set to $_REQUEST if omitted.
110 * @uses REF_USE_KEYBOARD Assigned to _SESSION[use_kb] if use_kb is empty and this is not
111 */
112 function initialize_session_variables($ARGS=null)
113 {
114 if(!is_array($ARGS))
115 $ARGS =& $_REQUEST;
116
117 $_SESSION['feed_sort'] = isset($ARGS['feed_sort'])
118 ? $ARGS['feed_sort']
119 : $_SESSION['feed_sort'];
120
121 $_SESSION['use_kb'] = isset($_SESSION['use_kb'])
122 ? $_SESSION['use_kb']
123 : (defined('REF_USE_KEYBOARD')
124 ? REF_USE_KEYBOARD
125 : 0);
126 }
127
128 /**
129 * Initialize timezone
130 *
131 * @uses REBLOG_TIMEZONE Assigned to TZ environment variable
132 * @return boolean Return value of putenv()
133 */
134 function initialize_timezone()
135 {
136 if(defined('REBLOG_TIMEZONE'))
137 return putenv('TZ='.REBLOG_TIMEZONE);
138
139 if(defined('REF_TIMEZONE'))
140 return putenv('TZ='.REF_TIMEZONE);
141 }
142
143 /**
144 * Get a path for the session cookie
145 *
146 * Starting at the current directory, iterate up parent directories
147 * until we reach the top ("/") or find ourselves at the Refeed root
148 * directory.
149 *
150 * @param string $root_path Refeed root directory in filesystem
151 * @return string Absolute path for session cookie use
152 */
153 function get_refeed_root($root_path = '')
154 {
155 if(defined('REBLOG_REFEED_ROOT_PATH') && REBLOG_REFEED_ROOT_PATH) {
156 return REBLOG_REFEED_ROOT_PATH;
157 }
158
159 if(php_sapi_name() == 'cli') {
160 return null;
161 }
162
163 if(empty($root_path)) {
164 return "http://{$_SERVER['HTTP_HOST']}" . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
165 }
166
167 $session_path = dirname($_SERVER['SCRIPT_NAME']);
168
169 for($current_path = dirname($_SERVER['SCRIPT_FILENAME']);
170 $current_path != '/' && strtolower($current_path) != strtolower($root_path);
171 $current_path = dirname($current_path)) {
172
173 $session_path = dirname($session_path);
174 }
175
176 return $session_path;
177 }
178
179 /**
180 * Get a DSN for the database connection.
181 *
182 * @return string database connection string, like "mysql://user:pass@host/name"
183 *
184 * @uses REBLOG_DSN
185 */
186 function get_configured_dsn()
187 {
188 if(defined('REBLOG_DSN'))
189 return REBLOG_DSN;
190
191 // old-style configuration vars
192 if(defined('REF_DB_USER') && defined('REF_DB_PASS') && defined('REF_DB_HOST') && defined('REF_DB_DBNAME'))
193 return sprintf('mysql://%s:%s@%s/%s', REF_DB_USER, REF_DB_PASS, REF_DB_HOST, REF_DB_DBNAME);
194
195 return '';
196 }
197
198 /**
199 * Get a GUID prefix
200 *
201 * @return string GUID prefix
202 *
203 * @uses REBLOG_GUID_PREFIX
204 */
205 function get_configured_guid_prefix()
206 {
207 if(defined('REBLOG_GUID_PREFIX'))
208 return REBLOG_GUID_PREFIX;
209
210 // old-style configuration vars
211 if(defined('REF_GUID_TAG_PREFIX'))
212 return REF_GUID_TAG_PREFIX;
213
214 return '';
215 }
216
217 /**
218 * Get a cache directory prefix
219 *
220 * @return string absolute local path to cache directory
221 *
222 * @uses REBLOG_CACHE_DIR
223 */
224 function get_configured_cache_dir()
225 {
226 if(defined('REBLOG_CACHE_DIR'))
227 return REBLOG_CACHE_DIR;
228
229 // old-style configuration vars
230 if(defined('REF_CACHE_DIR'))
231 return REF_CACHE_DIR;
232
233 return realpath(dirname(__FILE__).'..'.DIRECTORY_SEPARATOR.'..').DIRECTORY_SEPARATOR.'cache';
234 }
235
236 /**
237 * Get a HTTP username & password
238 *
239 * @return array Two-element array with username & password strings
240 *
241 * @uses REBLOG_HTTPAUTH_NAME
242 * @uses REBLOG_HTTPAUTH_PASS
243 */
244 function get_configured_httpauth()
245 {
246 if(defined('REBLOG_HTTPAUTH_NAME') && defined('REBLOG_HTTPAUTH_PASS'))
247 return array(REBLOG_HTTPAUTH_NAME, REBLOG_HTTPAUTH_PASS);
248
249 // old-style configuration vars
250 if(defined('REF_HTTPAUTH_NAME') && defined('REF_HTTPAUTH_PASS'))
251 return array(REF_HTTPAUTH_NAME, REF_HTTPAUTH_PASS);
252
253 return array('', '');
254 }
255
256 /**
257 * Get a user agent and cache age for Magpie
258 *
259 * @return array Two-element array with user-agent & cache age values
260 *
261 * @uses REBLOG_HTTP_USER_AGENT
262 * @uses REBLOG_FEED_CACHE_AGE
263 */
264 function get_configured_magpiestuff()
265 {
266 if(defined('REBLOG_HTTP_USER_AGENT') && defined('REBLOG_FEED_CACHE_AGE'))
267 return array(REBLOG_HTTP_USER_AGENT, REBLOG_FEED_CACHE_AGE);
268
269 // old-style configuration vars
270 if(defined('REF_HTTP_USER_AGENT') && defined('REF_FEED_CACHE_AGE'))
271 return array(REF_HTTP_USER_AGENT, REF_FEED_CACHE_AGE);
272
273 return array('', 60 * 15);
274 }
275
276 /**
277 * Get a lifetime for the session cookie
278 *
279 * @return int lifetime
280 *
281 * @uses REBLOG_SESSION_LIFETIME
282 */
283 function get_configured_cookielifetime()
284 {
285 if(defined('REBLOG_SESSION_LIFETIME'))
286 return REBLOG_SESSION_LIFETIME;
287
288 // old-style configuration vars
289 if(defined('REF_SESSION_LIFETIME'))
290 return REF_SESSION_LIFETIME;
291
292 return 60 * 60 * 24 * 365 * 30;
293 }
294
295 /**
296 * Get a default version for the outgoing feed
297 *
298 * @return string Version of outgoing feed
299 *
300 * @uses REF_DEFAULT_RSS_VERSION
301 */
302 function get_configured_feedversion()
303 {
304 if(defined('REBLOG_FEED_VERSION'))
305 return REBLOG_FEED_VERSION;
306
307 // old-style configuration vars
308 if(defined('REF_DEFAULT_RSS_VERSION'))
309 switch(REF_DEFAULT_RSS_VERSION) {
310 case 1:
311 return 'rss1.0';
312 case 2:
313 return 'rss2.0';
314 }
315
316 return 'rss2.0';
317 }
318
319 /**
320 * Print a diagnostic or progress report using whatever format is
321 * appropriate to the current SAPI.
322 *
323 * @param string $trace A string to print
324 */
325 function print_trace($trace)
326 {
327 if(php_sapi_name() == 'cli') {
328 echo strip_tags($trace)."\n";
329
330 } else {
331 echo '<p>'.$trace.'</p>';
332
333 }
334 }
335
336
337
338 // CDATAFY
339 function ref_cdata_escape($s)
340 {
341 return str_replace("]]>", " ] ] >", $s);
342 }
343
344 // ARRAYIFYING
345
346
347 // HELPER
348
349 function ref_find_links($content)
350 {
351 $n = preg_match_all("/<a [^\>]*href\=[\'\"]*([^\"\'\s]+)[\'\"]*[^\>]*\>/i", $content, $matches, PREG_SET_ORDER);
352
353 $res = array();
354
355 foreach($matches as $m) {
356 $res[] = $m[1];
357 }
358
359 return $res;
360 }
361
362
363 // DATE/TIME CONVERSIONS
364 function ref_unix_timestamp2iso8601($ts)
365 {
366 return date("Y-m-d\TH:i:sO",$ts);
367 }
368
369 function ref_unix_timestamp2rfc822($ts)
370 {
371 return date("r",$ts);
372 }
373
374 function ref_rfc8222unix_timestamp($rfc822)
375 {
376 return strtotime($rfc822);
377 }
378
379 function ref_iso86012unix_timestamp($iso8601)
380 {
381 return parse_w3cdtf($iso8601);
382 }
383
384 ?>

  ViewVC Help
Powered by ViewVC 1.1.26