4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2009 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
14 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
15 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
16 * @version $Id: globalfunctions.php 1856 2012-05-18 09:30:16Z sakamocchi $
19 /* needed if we include globalfunctions from install.php */
\r
20 global $nucleus, $CONF, $DIR_LIBS, $DIR_LOCALES, $manager, $member, $MYSQL_HANDLER, $StartTime;
22 /* just for benchmark tag */
23 $StartTime = microtime(TRUE);
25 $nucleus['version'] = 'v4.00 SVN';
\r
26 $nucleus['codename'] = '';
\r
29 * make sure there's no unnecessary escaping:
\r
30 * set_magic_quotes_runtime(0);
\r
32 if ( version_compare(PHP_VERSION, '5.3.0', '<') )
\r
34 ini_set('magic_quotes_runtime', '0');
\r
37 /* check and die if someone is trying to override internal globals (when register_globals turn on) */
\r
38 checkVars(array('nucleus', 'CONF', 'DIR_LIBS',
39 'MYSQL_HOST', 'MYSQL_USER', 'MYSQL_PASSWORD', 'MYSQL_DATABASE',
40 'DIR_LOCALES', 'DIR_PLUGINS',
41 'HTTP_GET_VARS', 'HTTP_POST_VARS', 'HTTP_COOKIE_VARS', 'HTTP_ENV_VARS',
42 'HTTP_SESSION_VARS', 'HTTP_POST_FILES', 'HTTP_SERVER_VARS',
43 'GLOBALS', 'argv', 'argc', '_GET', '_POST', '_COOKIE', '_ENV', '_SESSION', '_SERVER', '_FILES'));
45 if ( !isset($CONF) )
\r
51 if ( array_key_exists('debug', $CONF) && $CONF['debug'] )
\r
53 /* report all errors! */
\r
54 error_reporting(E_ALL);
\r
58 ini_set('display_errors','0');
\r
59 error_reporting(E_ERROR | E_WARNING | E_PARSE);
\r
63 * alertOnHeadersSent
\r
64 * Displays an error when visiting a public Nucleus page and headers have
\r
65 * been sent out to early. This usually indicates an error in either a
\r
66 * configuration file or a translation file, and could cause Nucleus to
\r
69 if ( !array_key_exists('alertOnHeadersSent', $CONF) )
\r
71 $CONF['alertOnHeadersSent'] = 1;
\r
75 * alertOnSecurityRisk
\r
76 * Displays an error only when visiting the admin area, and when one or
\r
77 * more of the installation files (install.php, install.sql, upgrades/
\r
78 * directory) are still on the server.
\r
80 if ( !array_key_exists('alertOnSecurityRisk', $CONF) )
\r
82 $CONF['alertOnSecurityRisk'] = 1;
\r
86 * Set these to 1 to allow viewing of future items or draft items
\r
87 * Should really never do this, but can be useful for some plugins that might need to
\r
88 * Could cause some other issues if you use future posts otr drafts
\r
91 $CONF['allowDrafts'] = 0;
\r
92 $CONF['allowFuture'] = 0;
\r
94 if ( getNucleusPatchLevel() > 0 )
\r
96 $nucleus['version'] .= '/' . getNucleusPatchLevel();
\r
100 if ( !array_key_exists('installscript', $CONF) || empty($CONF['installscript']) )
\r
102 $CONF['installscript'] = 0;
\r
104 if ( !array_key_exists('UsingAdminArea', $CONF) )
\r
106 $CONF['UsingAdminArea'] = 0;
\r
109 if ( !headers_sent() )
\r
111 header('Generator: Nucleus CMS ' . $nucleus['version']);
\r
115 /* TODO: This is for compatibility since 4.0, should be obsoleted at future release. */
\r
116 if ( !isset($DIR_LOCALES) )
\r
118 $DIR_LOCALES = $DIR_NUCLEUS . 'locales/';
\r
121 if ( !isset($DIR_LANG) )
\r
123 $DIR_LANG = $DIR_LOCALES;
\r
126 /* load and initialize i18n class */
\r
127 if (!class_exists('i18n', FALSE))
\r
129 include($DIR_LIBS . 'i18n.php');
\r
131 if ( !i18n::init('UTF-8', $DIR_LOCALES) )
\r
133 exit('Fail to initialize i18n class.');
\r
136 /* TODO: This is just for compatibility since 4.0, should be obsoleted at future release. */
\r
137 define('_CHARSET', i18n::get_current_charset());
\r
141 * NOTE: Since 4.0 release, Entity class becomes to be important class
\r
142 * with some wrapper functions for htmlspechalchars/htmlentity PHP's built-in function
\r
144 include($DIR_LIBS . 'ENTITY.php');
\r
146 /* we will use postVar, getVar, ... methods instead of $_GET, $_POST ... */
\r
147 if ( $CONF['installscript'] != 1 )
\r
149 /* vars were already included in install.php */
\r
150 include_once($DIR_LIBS . 'vars4.1.0.php');
\r
152 /* added for 4.0 DB::* wrapper and compatibility sql_* */
\r
153 include_once($DIR_LIBS . 'sql/sql.php');
\r
156 /* include core classes that are needed for login & plugin handling */
\r
157 include($DIR_LIBS . 'MEMBER.php');
\r
158 include($DIR_LIBS . 'ACTIONLOG.php');
\r
159 include($DIR_LIBS . 'MANAGER.php');
\r
160 include($DIR_LIBS . 'PLUGIN.php');
\r
162 $manager =& MANAGER::instance();
\r
164 /* only needed when updating logs */
\r
165 if ( $CONF['UsingAdminArea'] )
\r
167 /* XML-RPC client classes */
\r
168 include($DIR_LIBS . 'xmlrpc.inc.php');
\r
169 include($DIR_LIBS . 'ADMIN.php');
\r
173 /* connect to database */
\r
174 if ( !isset($MYSQL_HANDLER) )
\r
176 $MYSQL_HANDLER = array('mysql','');
\r
178 if ( $MYSQL_HANDLER[0] == '' )
\r
180 $MYSQL_HANDLER[0] = 'mysql';
\r
182 DB::setConnectionInfo($MYSQL_HANDLER[1], $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE);
\r
185 /* force locale or charset */
\r
187 $charset = i18n::get_current_charset();
\r
190 'locale' => &$locale,
\r
191 'charset' => &$charset
\r
193 $manager->notify('ForceLocale', $data);
\r
195 if ( $data['locale'] !== '' )
\r
197 i18n::set_forced_locale($data['locale']);
\r
199 if ( $data['charset'] !== '' )
\r
201 i18n::set_forced_charset($data['charset']);
\r
207 /* convert forced charset to current charset */
\r
208 if ( i18n::get_forced_charset() != i18n::get_current_charset() )
\r
210 $_POST = i18n::convert_array($_POST, i18n::get_forced_charset());
\r
211 $_GET = i18n::convert_array($_GET, i18n::get_forced_charset());
\r
212 $_REQUEST = i18n::convert_array($_REQUEST, i18n::get_forced_charset());
\r
213 $_COOKIE = i18n::convert_array($_COOKIE, i18n::get_forced_charset());
\r
214 $_FILES = i18n::convert_array($_FILES, i18n::get_forced_charset());
\r
216 if ( session_id() !== '' )
\r
218 $_SESSION = i18n::convert_array($_SESSION, i18n::get_forced_charset());
\r
223 /* sanitize option */
\r
224 $bLoggingSanitizedResult = 0;
\r
225 $bSanitizeAndContinue = 0;
\r
226 $orgRequestURI = serverVar('REQUEST_URI');
\r
229 /* logs sanitized result if need */
\r
230 if ( $orgRequestURI !== serverVar('REQUEST_URI') )
\r
232 $msg = "Sanitized [" . serverVar('REMOTE_ADDR') . "] ";
\r
233 $msg .= $orgRequestURI . " -> " . serverVar('REQUEST_URI');
\r
234 if ( $bLoggingSanitizedResult )
\r
236 addToLog(WARNING, $msg);
\r
238 if ( !$bSanitizeAndContinue )
\r
244 /* get all variables that can come from the request and put them in the global scope */
\r
245 $blogid = requestVar('blogid');
\r
246 $itemid = intRequestVar('itemid');
\r
247 $catid = intRequestVar('catid');
\r
248 $skinid = requestVar('skinid');
\r
249 $memberid = requestVar('memberid');
\r
250 $archivelist = requestVar('archivelist');
\r
251 $imagepopup = requestVar('imagepopup');
\r
252 $archive = requestVar('archive');
\r
253 $query = requestVar('query');
\r
254 $highlight = requestVar('highlight');
\r
255 $amount = requestVar('amount');
\r
256 $action = requestVar('action');
\r
257 $nextaction = requestVar('nextaction');
\r
258 $maxresults = requestVar('maxresults');
\r
259 $startpos = intRequestVar('startpos');
\r
260 $errormessage = '';
\r
262 $special = requestVar('special');
\r
269 /* Properly set $CONF['Self'] and others if it's not set...
\r
270 * usually when we are access from admin menu
\r
272 if ( !array_key_exists('Self', $CONF) )
\r
274 $CONF['Self'] = $CONF['IndexURL'];
\r
275 /* strip trailing */
\r
276 if ( $CONF['Self'][i18n::strlen($CONF['Self']) -1] == "/" )
\r
278 $CONF['Self'] = i18n::substr($CONF['Self'], 0, i18n::strlen($CONF['Self']) -1);
\r
282 $CONF['ItemURL'] = $CONF['Self'];
\r
283 $CONF['ArchiveURL'] = $CONF['Self'];
\r
284 $CONF['ArchiveListURL'] = $CONF['Self'];
\r
285 $CONF['MemberURL'] = $CONF['Self'];
\r
286 $CONF['SearchURL'] = $CONF['Self'];
\r
287 $CONF['BlogURL'] = $CONF['Self'];
\r
288 $CONF['CategoryURL'] = $CONF['Self'];
\r
290 /* automatically use simpler toolbar for mozilla */
\r
291 if ( ($CONF['DisableJsTools'] == 0)
\r
292 && i18n::strpos(serverVar('HTTP_USER_AGENT'), 'Mozilla/5.0') !== FALSE
\r
293 && i18n::strpos(serverVar('HTTP_USER_AGENT'), 'Gecko') !== FALSE )
\r
295 $CONF['DisableJsTools'] = 2;
\r
298 /* login processing */
299 $member = new Member();
\r
301 if ( $action == 'login' )
\r
303 $login = postVar('login');
\r
304 $password = postVar('password');
\r
305 $shared = intPostVar('shared');
\r
306 $member->login($login, $password, $shared);
\r
308 elseif ( ($action == 'logout') )
\r
314 $member->cookielogin();
\r
318 /* TODO: This is for backward compatibility, should be obsoleted near future. */
\r
319 if ( !preg_match('#^(.+)_(.+)_(.+)$#', $CONF['Locale'])
\r
320 && ($CONF['Locale'] = i18n::convert_old_language_file_name_to_locale($CONF['Locale'])) === FALSE )
\r
322 $CONF['Locale'] = 'en_Latn_US';
\r
324 if ( !array_key_exists('Language', $CONF) )
\r
326 $CONF['Language'] = i18n::convert_locale_to_old_language_file_name($CONF['Locale']);
\r
328 $locale = $CONF['Locale'];
\r
331 /* NOTE: include translation file and set locale */
\r
332 if ( $member->isLoggedIn() )
\r
334 if ( $member->getLocale() )
\r
336 $locale = $member->getLocale();
\r
341 if ( i18n::get_forced_locale() !== '' )
\r
343 $locale = i18n::get_forced_locale();
\r
346 include_translation($locale);
\r
347 i18n::set_current_locale($locale);
\r
350 /* login completed */
\r
351 $manager->notify('PostAuthentication', array('loggedIn' => $member->isLoggedIn() ) );
\r
354 if ( $member->isLoggedIn() && $nextaction )
\r
356 $action = $nextaction;
\r
359 /* first, let's see if the site is disabled or not. always allow admin area access. */
\r
360 if ( $CONF['DisableSite'] && !$member->isAdmin() && !$CONF['UsingAdminArea'] )
\r
362 redirect($CONF['DisableSiteURL']);
\r
366 /* load other classes */
\r
367 include($DIR_LIBS . 'PARSER.php');
\r
368 include($DIR_LIBS . 'SKIN.php');
\r
369 include($DIR_LIBS . 'TEMPLATE.php');
\r
370 include($DIR_LIBS . 'BLOG.php');
\r
371 include($DIR_LIBS . 'BODYACTIONS.php');
\r
372 include($DIR_LIBS . 'COMMENTS.php');
\r
373 include($DIR_LIBS . 'COMMENT.php');
\r
374 include($DIR_LIBS . 'NOTIFICATION.php');
\r
375 include($DIR_LIBS . 'BAN.php');
\r
376 include($DIR_LIBS . 'SEARCH.php');
\r
377 include($DIR_LIBS . 'LINK.php');
\r
379 /* set lastVisit cookie (if allowed) */
\r
380 if ( !headers_sent() )
\r
382 if ( $CONF['LastVisit'] )
\r
384 setcookie($CONF['CookiePrefix'] . 'lastVisit', time(), time() + 2592000, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
\r
388 setcookie($CONF['CookiePrefix'] . 'lastVisit', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
\r
392 if ( !defined('_ARCHIVETYPE_MONTH') )
\r
394 define('_ARCHIVETYPE_DAY', 'day');
\r
395 define('_ARCHIVETYPE_MONTH', 'month');
\r
396 define('_ARCHIVETYPE_YEAR', 'year');
\r
399 /* for path resolving */
400 $virtualpath = getVar('virtualpath');
401 if ( getVar('virtualpath') == '' )
403 $virtualpath = serverVar('PATH_INFO');
407 * switch URLMode back to normal when $CONF['Self'] ends in .php
408 * this avoids urls like index.php/item/13/index.php/item/15
410 if ( !array_key_exists('URLMode', $CONF) || ($CONF['URLMode'] != 'pathinfo') )
412 $CONF['URLMode'] = 'normal';
416 if ( i18n::substr($CONF['Self'], i18n::strlen($CONF['Self']) - 4) != '.php' )
418 decodePathInfo($virtualpath);
423 * PostParseURL is a place to cleanup any of the path-related global variables before the selector function is run.
\r
424 * It has 2 values in the data in case the original virtualpath is needed, but most the use will be in tweaking
\r
425 * global variables to clean up (scrub out catid or add catid) or to set someother global variable based on
\r
426 * the values of something like catid or itemid
\r
430 'type' => basename(serverVar('SCRIPT_NAME')),
\r
431 'info' => $virtualpath
\r
433 $manager->notify('PostParseURL', $data);
\r
436 * NOTE: Here is the end of initialization
\r
441 * This function includes or requires the specified library file
\r
443 * @param string $file
\r
444 * @param boolean $once use the _once() version
\r
445 * @param boolean $require use require() instead of include()
\r
448 function include_libs($file, $once = TRUE, $require = TRUE)
\r
452 // $DIR_LIBS isn't a directory
\r
453 if ( !is_dir($DIR_LIBS) )
\r
458 $lib_path = $DIR_LIBS . $file;
\r
460 if ( $once && $require )
\r
462 require_once($lib_path);
\r
464 else if ( $once && !$require )
\r
466 include_once($lib_path);
\r
468 else if ( $require )
\r
470 require($lib_path);
\r
474 include($lib_path);
\r
480 * include_plugins()
\r
481 * This function includes or requires the specified plugin file
\r
483 * @param string $file
\r
484 * @param boolean $once use the _once() version
\r
485 * @param boolean $require use require() instead of include()
\r
488 function include_plugins($file, $once = TRUE, $require = TRUE)
\r
490 global $DIR_PLUGINS;
\r
492 // begin if: $DIR_LIBS isn't a directory
\r
493 if ( !is_dir($DIR_PLUGINS) )
\r
498 $plugin_path = $DIR_PLUGINS . $file;
\r
501 if ( $once && $require )
\r
503 require_once($plugin_path);
\r
505 else if ( $once && !$require )
\r
507 include_once($plugin_path);
\r
509 elseif ( $require )
\r
511 require($plugin_path);
\r
515 include($plugin_path);
\r
521 * include_translation()
\r
522 * This function decide which locale is used and include translation
\r
524 * @param string &$locale locale name referring to 'language tags' defined in RFC 5646
\r
527 function include_translation(&$locale)
\r
529 global $DIR_LOCALES;
\r
531 $translation_file = $DIR_LOCALES . $locale . '.' . i18n::get_current_charset() . '.php';
\r
532 if ( !file_exists($translation_file) )
\r
534 $locale = 'en_Latn_US';
\r
535 $translation_file = $DIR_LOCALES . 'en_Latn_US.ISO-8859-1.php';
\r
537 include($translation_file);
\r
543 * This function returns the integer value of $_POST for the variable $name
\r
545 * @param string $name field to get the integer value of
\r
548 function intPostVar($name)
\r
550 return (integer) postVar($name);
\r
556 * This function returns the integer value of $_GET for the variable $name
\r
558 * @param string $name field to get the integer value of
\r
561 function intGetVar($name)
\r
563 return (integer) getVar($name);
\r
569 * This function returns the integer value of $_REQUEST for the variable $name. Also checks $_GET and $_POST if not found in $_REQUEST
\r
571 * @param string $name field to get the integer value of
\r
574 function intRequestVar($name)
\r
576 return (integer) requestVar($name);
\r
582 * This function returns the integer value of $_COOKIE for the variable $name
\r
584 * @param string $name field to get the integer value of
\r
587 function intCookieVar($name)
\r
589 return (integer) cookieVar($name);
\r
593 * getNucleusVersion()
\r
594 * This function returns the current Nucleus version (100 = 1.00, 101 = 1.01, etc...)
\r
599 function getNucleusVersion()
\r
605 * getNucleusPatchLevel()
\r
606 * TODO: Better description of this function.
\r
608 * Power users can install patches in between nucleus releases. These patches
\r
609 * usually add new functionality in the plugin API and allow those to
\r
610 * be tested without having to install CVS.
\r
615 function getNucleusPatchLevel()
\r
621 * getLatestVersion()
\r
622 * This function returns the latest Nucleus version available for download from nucleuscms.org or FALSE if unable to attain data
\r
623 * Format will be major.minor/patachlevel e.g. 3.41 or 3.41/02
\r
628 function getLatestVersion()
\r
630 // begin if: cURL is not available in this PHP installation
\r
631 if ( !function_exists('curl_init') )
\r
636 $curl = curl_init();
\r
639 curl_setopt ($curl, CURLOPT_URL, 'http://nucleuscms.org/version_check.php');
\r
640 curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
\r
641 curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
\r
643 $return = curl_exec($curl);
\r
652 * This function returns a Nucleus table name with the appropriate prefix
\r
653 * @param string $name
\r
656 function sql_table($name)
\r
658 global $MYSQL_PREFIX;
\r
660 // begin if: no MySQL prefix
\r
661 if ( empty($MYSQL_PREFIX) )
\r
663 return 'nucleus_' . $name;
\r
665 // else: use MySQL prefix
\r
668 return $MYSQL_PREFIX . 'nucleus_' . $name;
\r
674 * sendContentType()
\r
675 * This function sends the Content-Type header if headers have not already been sent
\r
676 * It also determines if the browser can accept application/xhtml+xml and sends it only to those that can.
\r
678 * if content type is application/xhtml+xml, only send it to browsers
\r
679 * that can handle it (IE6 cannot). Otherwise, send text/html
\r
682 * For admin area pages, keep sending text/html (unless it's a debug version)
\r
683 * application/xhtml+xml still causes too much problems with the javascript implementations
\r
686 * ($CONF['UsingAdminArea'] && !$CONF['debug']) gets removed,
\r
687 * application/xhtml+xml seems to be working, so we're going to use it if we can.
\r
689 * @param string $content_type MIME media type registered to IANA, http://www.iana.org/assignments/media-types/index.html
\r
690 * @param string $page_type
\r
691 * @param string $charset Deprecated. This has no meaning.
\r
695 function sendContentType($content_type, $page_type = '', $charset = '')
\r
697 global $manager, $CONF;
\r
699 if ( headers_sent() )
\r
704 /* NOTE: MIME Media Type */
\r
705 if ( ($content_type == 'application/xhtml+xml')
\r
706 && (!stristr(serverVar('HTTP_ACCEPT'), 'application/xhtml+xml') ) )
\r
708 $content_type = 'text/html';
\r
711 /* NOTE: generate event */
\r
713 'pageType' => $page_type,
\r
714 'contentType' => &$content_type
\r
716 $manager->notify('PreSendContentType', $data);
\r
718 /* NOTE: confirm MIME Media Type */
\r
719 $content_type = preg_replace('#[^a-zA-Z0-9-+./]#', '', $content_type);
\r
721 /* NOTE: confirm character set */
\r
722 $charset = i18n::get_current_charset();
\r
723 if ( i18n::get_forced_charset() !== '' )
\r
725 $charset = i18n::get_forced_charset();
\r
728 /* NOTE: send HTTP 1.1 header */
\r
729 header("Content-Type: {$content_type}; charset={$charset}");
\r
731 /* NOTE: set handler for translating character set */
\r
732 if ( $charset != i18n::get_current_charset() )
\r
734 ob_start(array('i18n', 'convert_handler'));
\r
742 * This function parses a query into an array of expressions that can be passed on to the highlight method
\r
743 * @param string $query
\r
746 function parseHighlight($query)
\r
748 // TODO: add more intelligent splitting logic
\r
750 // get rid of quotes
\r
751 $query = preg_replace('/\'|"/', '', $query);
\r
758 $aHighlight = preg_split('# #', $query);
\r
760 for ( $i = 0; $i < count($aHighlight); $i++ )
\r
762 $aHighlight[$i] = trim($aHighlight[$i]);
\r
764 if ( i18n::strlen($aHighlight[$i]) < 3 )
\r
766 unset($aHighlight[$i]);
\r
770 if ( count($aHighlight) == 1 )
\r
772 return $aHighlight[0];
\r
776 return $aHighlight;
\r
787 function getConfig()
\r
791 $query = sprintf('SELECT * FROM %s', sql_table('config'));
\r
792 $res = DB::getResult($query);
\r
794 foreach ( $res as $row )
\r
796 $CONF[$row['name']] = $row['value'];
\r
802 * This function gets the blog ID from the blog name
\r
803 * @param string $name
\r
806 function getBlogIDFromName($name)
\r
808 $query = sprintf('SELECT bnumber AS result FROM %s WHERE bshortname=%s', sql_table('blog'), DB::quoteValue($name));
\r
809 return DB::getValue($query);
\r
813 * This function gets the blog name from the blog ID
\r
817 function getBlogNameFromID($id)
\r
819 $query = sprintf('SELECT bname AS result FROM %s WHERE bnumber=%d', sql_table('blog'), intval($id));
\r
820 return DB::getValue($query);
\r
824 * This function gets the blog ID from the item ID
\r
825 * @param int $item_id
\r
828 function getBlogIDFromItemID($item_id)
\r
830 $query = sprintf('SELECT iblog AS result FROM %s WHERE inumber=%d', sql_table('item'), intval($item_id));
\r
831 return DB::getValue($query);
\r
835 * This function gets the blog ID from the comment ID
\r
836 * @param int $comment_id
\r
839 function getBlogIDFromCommentID($comment_id)
\r
841 $query = sprintf('SELECT cblog AS result FROM %s WHERE cnumber=%d', sql_table('comment'), intval($comment_id));
\r
842 return DB::getValue($query);
\r
846 * This function gets the blog ID from the category ID
\r
847 * @param int $category_id
\r
850 function getBlogIDFromCatID($category_id)
\r
852 $query = sprintf('SELECT cblog AS result FROM %s WHERE catid=%d', sql_table('category'), intval($category_id));
\r
853 return DB::getValue($query);
\r
857 * This function gets the category ID from the category name
\r
861 function getCatIDFromName($name)
\r
863 $query = sprintf('SELECT catid AS result FROM %s WHERE cname=%s', sql_table('category'), DB::quoteValue($name));
\r
864 return DB::getValue($query);
\r
869 * functions to be used in index.php to select something
\r
871 function selectBlog($shortname)
\r
873 global $blogid, $archivelist;
\r
874 $blogid = getBlogIDFromName($shortname);
\r
876 // also force archivelist variable, if it is set
\r
877 if ( $archivelist )
\r
879 $archivelist = $blogid;
\r
883 function selectSkin($skinname)
\r
886 $skinid = SKIN::getIdFromName($skinname);
\r
889 function selectCategory($cat)
\r
892 if ( is_numeric($cat) )
\r
894 $catid = (integer) $cat;
\r
898 $catid = getCatIDFromName($cat);
\r
902 function selectItem($id)
\r
905 $itemid = (integer) $id;
\r
908 function selectSpecialSkinType($id)
\r
911 $special = strtolower($id);
\r
914 function selector()
\r
916 global $archive, $archivelist, $archivenext, $archivenextexists, $archiveprev, $archiveprevexists, $archivetype;
\r
917 global $blog, $blogid;
\r
919 global $itemid, $itemidnext, $itemidprev, $itemtitlenext, $itemtitleprev;
\r
920 global $CONF, $DIR_LIBS, $amount, $errormessage, $imagepopup;
\r
921 global $manager, $maxresults, $query;
\r
922 global $member, $memberid, $memberinfo;
\r
923 global $skinid, $skinpart, $special;
\r
925 $actionNames = array('addcomment', 'sendmessage', 'createaccount', 'forgotpassword', 'votepositive', 'votenegative', 'plugin');
\r
926 $action = requestVar('action');
\r
928 if ( in_array($action, $actionNames) )
\r
930 include_once($DIR_LIBS . 'ACTION.php');
\r
932 $errorInfo = $a->doAction($action);
\r
936 $errormessage = $errorInfo['message'];
\r
940 // show error when headers already sent out
\r
941 if ( headers_sent() && $CONF['alertOnHeadersSent'] )
\r
943 // try to get line number/filename (extra headers_sent params only exists in PHP 4.3+)
\r
944 if ( function_exists('version_compare') && version_compare('4.3.0', phpversion(), '<=') )
\r
946 headers_sent($hsFile, $hsLine);
\r
947 $extraInfo = ' in <code>' . $hsFile . '</code> line <code>' . $hsLine . '</code>';
\r
955 "<p>The page headers have already been sent out{$extraInfo}. This could cause Nucleus not to work in the expected way.</p>"
\r
956 . "<p>Usually, this is caused by spaces or newlines at the end of the <code>config.php</code> file, "
\r
957 . "at the end of the translation file or at the end of a plugin file.</p>"
\r
958 . "<p>Please check this and try again.</p>"
\r
959 . "<p>If you don't want to see this error message again, without solving the problem, "
\r
960 . "set <code>{$CONF['alertOnHeadersSent']}</code> in <code>globalfunctions.php</code> to <code>0</code></p>"
\r
961 . "Page headers already sent"
\r
966 // make is so ?archivelist without blogname or blogid shows the archivelist
\r
967 // for the default weblog
\r
968 if ( serverVar('QUERY_STRING') == 'archivelist' )
\r
970 $archivelist = $CONF['DefaultBlog'];
\r
973 // now decide which type of skin we need
\r
976 // itemid given -> only show that item
\r
979 if ( !$manager->existsItem($itemid,intval($CONF['allowFuture']),intval($CONF['allowDrafts'])) )
\r
981 doError(_ERROR_NOSUCHITEM);
\r
984 // 1. get timestamp, blogid and catid for item
\r
985 $query = 'SELECT itime, iblog, icat FROM %s WHERE inumber=%d';
\r
986 $query = sprintf($query, sql_table('item'), intval($itemid));
\r
987 $row = DB::getRow($query);
\r
989 // if a different blog id has been set through the request or selectBlog(),
\r
992 if ( $blogid && (intval($blogid) != $row['iblog']) )
\r
994 doError(_ERROR_NOSUCHITEM);
\r
997 // if a category has been selected which doesn't match the item, ignore the
\r
999 if ( ($catid != 0) && ($catid != $row['icat']) )
\r
1004 $blogid = $row['iblog'];
\r
1005 $timestamp = strtotime($row['itime']);
\r
1007 $b =& $manager->getBlog($blogid);
\r
1009 if ( !$b->isValidCategory($catid) )
\r
1011 $query = "SELECT inumber, ititle FROM %s WHERE itime<%s AND idraft=0 AND iblog=%d ORDER BY itime DESC LIMIT 1";
\r
1012 $query = sprintf($query, sql_table('item'), DB::formatDateTime($timestamp), intval($blogid));
\r
1016 $query = "SELECT inumber, ititle FROM %s WHERE itime<%s AND idraft=0 AND iblog=%d AND icat=%d ORDER BY itime DESC LIMIT 1";
\r
1017 $query = sprintf($query, sql_table('item'), DB::formatDateTime($timestamp), intval($blogid), intval($catid));
\r
1019 $row = DB::getRow($query);
\r
1023 $itemidprev = $row['inumber'];
\r
1024 $itemtitleprev = $row['ititle'];
\r
1027 // get next itemid and title
\r
1028 if ( !$b->isValidCategory($catid) )
\r
1030 $query = "SELECT inumber, ititle FROM %s WHERE itime>%s AND itime<=%s AND idraft=0 AND iblog=%d ORDER BY itime ASC LIMIT 1";
\r
1031 $query = sprintf($query, sql_table('item'), DB::formatDateTime($timestamp), DB::formatDateTime($b->getCorrectTime()), intval($blogid));
\r
1035 $query = "SELECT inumber, ititle FROM %s WHERE itime>%s AND itime<=%s AND idraft=0 AND iblog=%d AND icat=%d ORDER BY itime ASC LIMIT 1";
\r
1036 $query = sprintf($query, sql_table('item'), DB::formatDateTime($timestamp), DB::formatDateTime($b->getCorrectTime()), intval($blogid), intval($catid));
\r
1038 $row = DB::getRow($query);
\r
1042 $itemidnext = $row['inumber'];
\r
1043 $itemtitlenext = $row['ititle'];
\r
1046 elseif ( $archive )
\r
1049 $type = 'archive';
\r
1051 // sql queries for the timestamp of the first and the last published item
\r
1052 $query = sprintf('SELECT UNIX_TIMESTAMP(itime) as result FROM %s WHERE idraft=0 ORDER BY itime ASC', sql_table('item'));
\r
1053 $first_timestamp = DB::getValue($query);
\r
1054 $query = sprintf('SELECT UNIX_TIMESTAMP(itime) as result FROM %s WHERE idraft=0 ORDER BY itime DESC', sql_table('item'));
\r
1055 $last_timestamp = DB::getValue($query);
\r
1057 sscanf($archive, '%d-%d-%d', $y, $m, $d);
\r
1061 $archivetype = _ARCHIVETYPE_DAY;
\r
1062 $t = mktime(0, 0, 0, $m, $d, $y);
\r
1063 // one day has 24 * 60 * 60 = 86400 seconds
\r
1064 $archiveprev = i18n::formatted_datetime('%Y-%m-%d', $t - 86400 );
\r
1065 // check for published items
\r
1066 if ( $t > $first_timestamp )
\r
1068 $archiveprevexists = true;
\r
1072 $archiveprevexists = false;
\r
1077 $archivenext = i18n::formatted_datetime('%Y-%m-%d', $t);
\r
1078 if ( $t < $last_timestamp )
\r
1080 $archivenextexists = true;
\r
1084 $archivenextexists = false;
\r
1087 elseif ( $m == 0 )
\r
1089 $archivetype = _ARCHIVETYPE_YEAR;
\r
1090 $t = mktime(0, 0, 0, 12, 31, $y - 1);
\r
1091 // one day before is in the previous year
\r
1092 $archiveprev = i18n::formatted_datetime('%Y', $t);
\r
1093 if ( $t > $first_timestamp )
\r
1095 $archiveprevexists = true;
\r
1099 $archiveprevexists = false;
\r
1102 // timestamp for the next year
\r
1103 $t = mktime(0, 0, 0, 1, 1, $y + 1);
\r
1104 $archivenext = i18n::formatted_datetime('%Y', $t);
\r
1105 if ( $t < $last_timestamp )
\r
1107 $archivenextexists = true;
\r
1111 $archivenextexists = false;
\r
1116 $archivetype = _ARCHIVETYPE_MONTH;
\r
1117 $t = mktime(0, 0, 0, $m, 1, $y);
\r
1118 // one day before is in the previous month
\r
1119 $archiveprev = i18n::formatted_datetime('%Y-%m', $t - 86400);
\r
1120 if ( $t > $first_timestamp )
\r
1122 $archiveprevexists = true;
\r
1126 $archiveprevexists = false;
\r
1129 // timestamp for the next month
\r
1130 $t = mktime(0, 0, 0, $m+1, 1, $y);
\r
1131 $archivenext = i18n::formatted_datetime('%Y-%m', $t);
\r
1132 if ( $t < $last_timestamp )
\r
1134 $archivenextexists = true;
\r
1138 $archivenextexists = false;
\r
1142 elseif ( $archivelist )
\r
1144 $type = 'archivelist';
\r
1146 if ( is_numeric($archivelist) )
\r
1148 $blogid = intVal($archivelist);
\r
1152 $blogid = getBlogIDFromName($archivelist);
\r
1157 doError(_ERROR_NOSUCHBLOG);
\r
1164 $query = stripslashes($query);
\r
1166 if ( is_numeric($blogid) )
\r
1168 $blogid = intVal($blogid);
\r
1172 $blogid = getBlogIDFromName($blogid);
\r
1177 doError(_ERROR_NOSUCHBLOG);
\r
1180 elseif ( $memberid )
\r
1184 if ( !Member::existsID($memberid) )
\r
1186 doError(_ERROR_NOSUCHMEMBER);
\r
1188 $memberinfo = $manager->getMember($memberid);
\r
1190 elseif ( $imagepopup )
\r
1192 // media object (images etc.)
\r
1193 $type = 'imagepopup';
\r
1195 // TODO: check if media-object exists
\r
1196 // TODO: set some vars?
\r
1200 // show regular index page
\r
1205 // any type of skin with catid
\r
1206 if ( $catid && !$blogid )
\r
1208 $blogid = getBlogIDFromCatID($catid);
\r
1211 // decide which blog should be displayed
\r
1214 $blogid = $CONF['DefaultBlog'];
\r
1217 $b =& $manager->getBlog($blogid);
\r
1218 $blog = $b; // references can't be placed in global variables?
\r
1220 if ( !$blog->isValid )
\r
1222 doError(_ERROR_NOSUCHBLOG);
\r
1225 // set catid if necessary
\r
1228 // check if the category is valid
\r
1229 if ( !$blog->isValidCategory($catid) )
\r
1231 doError(_ERROR_NOSUCHCATEGORY);
\r
1235 $blog->setSelectedCategory($catid);
\r
1241 $skinid = $blog->getDefaultSkin();
\r
1244 if ( !empty($special) && isValidShortName($special) )
\r
1246 $type = strtolower($special);
\r
1249 $skin =& $manager->get($skinid);
\r
1251 if ( !$skin->isValid() )
\r
1253 doError(_ERROR_NOSUCHSKIN);
\r
1256 // set global skinpart variable so can determine quickly what is being parsed from any plugin or phpinclude
\r
1257 $skinpart = $type;
\r
1260 $skin->parse($type);
\r
1262 // check to see we should throw JustPosted event
\r
1263 $blog->checkJustPosted();
\r
1269 * Show error skin with given message. An optional skin-object to use can be given
\r
1271 * @param string $msg
\r
1272 * @param string $skin
\r
1275 function doError($msg, $skin = '')
\r
1277 global $errormessage, $CONF, $skinid, $blogid, $manager;
\r
1279 if ( $skin == '' )
\r
1281 if ( Skin::existsID($skinid) )
\r
1285 elseif ( $manager->existsBlogID($blogid) )
\r
1287 $blog =& $manager->getBlog($blogid);
\r
1288 $id = $blog->getDefaultSkin();
\r
1290 elseif ($CONF['DefaultBlog'] )
\r
1292 $blog =& $manager->getBlog($CONF['DefaultBlog']);
\r
1293 $id = $blog->getDefaultSkin();
\r
1297 // this statement should actually never be executed
\r
1298 $id = $CONF['BaseSkin'];
\r
1300 $skin =& $manager->getSkin($id);
\r
1303 $errormessage = $msg;
\r
1304 $skin->parse('error');
\r
1309 * Errors before the database connection has been made
\r
1311 * @param string $msg message to notify
\r
1312 * @param string $title page title
\r
1315 function startUpError($msg, $title)
\r
1317 header('Content-Type: text/xml; charset=' . i18n::get_current_charset());
\r
1318 echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
\r
1320 echo "<title>{$title}</title></head>\n";
\r
1322 echo "<h1>{$title}</h1>\n";
\r
1329 function isValidShortName($name)
\r
1331 return preg_match('#^[a-z0-9]+$#i', $name);
\r
1333 function isValidDisplayName($name)
\r
1335 return preg_match('#^[a-z0-9]+[a-z0-9 ]*[a-z0-9]+$#i', $name);
\r
1337 function isValidCategoryName($name)
\r
1341 function isValidTemplateName($name)
\r
1343 return preg_match('#^[a-z0-9/_\-]+$#i', $name);
\r
1345 function isValidSkinName($name)
\r
1347 return preg_match('#^[a-z0-9/_\-]+$#i', $name);
\r
1350 // add and remove linebreaks
\r
1351 function addBreaks($var)
\r
1353 return nl2br($var);
\r
1355 function removeBreaks($var)
\r
1357 return preg_replace("/<br \/>([\r\n])/", "$1", $var);
\r
1363 * @param string $filename
\r
1364 * @param string $includeMode
\r
1365 * @param string $includePrefix
\r
1368 function parseFile($filename, $includeMode = 'normal', $includePrefix = '')
\r
1370 global $manager, $skinid;
\r
1372 if ( !$skinid || !existsID($skinid) )
\r
1374 $skin =& $manager->getSkin($CONF['BaseSkin']);
\r
1378 $skin =& $manager->getSkin($skinid);
\r
1381 $oldIncludeMode = Parser::getProperty('IncludeMode');
\r
1382 $oldIncludePrefix = Parser::getProperty('IncludePrefix');
\r
1384 $skin->parse('fileparse', $filename);
\r
1386 Parser::setProperty('IncludeMode', $oldIncludeMode);
\r
1387 Parser::setProperty('IncludePrefix', $oldIncludePrefix);
\r
1394 * Outputs a debug message
\r
1396 * @param string $msg
\r
1399 function debug($msg)
\r
1401 echo '<p><b>' . $msg . "</b></p>\n";
\r
1405 // shows a link to help file
\r
1406 function help($id)
\r
1408 echo helpHtml($id);
\r
1411 function helpHtml($id)
\r
1414 return helplink($id) . '<img src="' . $CONF['AdminURL'] . 'documentation/icon-help.gif" width="15" height="15" alt="' . _HELP_TT . '" title="' . _HELP_TT . '" /></a>';
\r
1416 function helplink($id)
\r
1419 return '<a href="' . $CONF['AdminURL'] . 'documentation/help.html#'. $id . '" onclick="if (event && event.preventDefault) event.preventDefault(); return help(this.href);">';
\r
1424 * Includes a PHP file. This method can be called while parsing templates and skins
\r
1426 * @param string $filename name of file to parse
\r
1429 function includephp($filename)
\r
1431 // make predefined variables global, so most simple scripts can be used here
\r
1433 // apache (names taken from PHP doc)
\r
1434 global $GATEWAY_INTERFACE, $SERVER_NAME, $SERVER_SOFTWARE, $SERVER_PROTOCOL;
\r
1435 global $REQUEST_METHOD, $QUERY_STRING, $DOCUMENT_ROOT, $HTTP_ACCEPT;
\r
1436 global $HTTP_ACCEPT_CHARSET, $HTTP_ACCEPT_ENCODING, $HTTP_ACCEPT_LANGUAGE;
\r
1437 global $HTTP_CONNECTION, $HTTP_HOST, $HTTP_REFERER, $HTTP_USER_AGENT;
\r
1438 global $REMOTE_ADDR, $REMOTE_PORT, $SCRIPT_FILENAME, $SERVER_ADMIN;
\r
1439 global $SERVER_PORT, $SERVER_SIGNATURE, $PATH_TRANSLATED, $SCRIPT_NAME;
\r
1440 global $REQUEST_URI;
\r
1442 // php (taken from PHP doc)
\r
1443 global $argv, $argc, $PHP_SELF, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS;
\r
1444 global $HTTP_POST_FILES, $HTTP_ENV_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
\r
1447 global $PATH_INFO, $HTTPS, $HTTP_RAW_POST_DATA, $HTTP_X_FORWARDED_FOR;
\r
1449 if ( @file_exists($filename) )
\r
1451 include($filename);
\r
1457 * Checks if a certain plugin exists
\r
1458 * @param string $plug name of plugin
\r
1459 * @return boolean exists or not
\r
1461 function checkPlugin($name)
\r
1463 global $DIR_PLUGINS;
\r
1464 return file_exists($DIR_PLUGINS . preg_replace('#[\\\\|/]#', '', $name) . '.php');
\r
1470 * @param string $querystr querystring to alter (e.g. foo=1&bar=2&x=y)
\r
1471 * @param string $param name of parameter to change (e.g. 'foo')
\r
1472 * @param string $value New value for that parameter (e.g. 3)
\r
1473 * @return string altered query string (for the examples above: foo=3&bar=2&x=y)
\r
1475 function alterQueryStr($querystr, $param, $value)
\r
1477 $vars = preg_split('#&#', $querystr);
\r
1480 for ( $i = 0; $i < count($vars); $i++ )
\r
1482 $v = preg_split('#=#', $vars[$i]);
\r
1484 if ( $v[0] == $param )
\r
1487 $vars[$i] = implode('=', $v);
\r
1494 $vars[] = "{$param}={$value}";
\r
1496 return ltrim(implode('&', $vars), '&');
\r
1501 * passes one variable as hidden input field (multiple fields for arrays)
\r
1502 * @see passRequestVars in varsx.x.x.php
\r
1504 * @param string $key
\r
1505 * @param string $value
\r
1508 function passVar($key, $value)
\r
1511 if ( is_array($value) )
\r
1513 for ( $i = 0; $i < sizeof($value); $i++ )
\r
1515 passVar($key . '[' . $i . ']', $value[$i]);
\r
1520 // other values: do stripslashes if needed
\r
1521 echo '<input type="hidden" name="' . Entity::hsc($key) . '" value="' . Entity::hsc(undoMagic($value)) . '" />' . "\n";
\r
1528 * @param string $variables
\r
1531 function checkVars($variables)
\r
1533 foreach ( $variables as $variable )
\r
1535 if ( array_key_exists($variable, $_GET)
\r
1536 || array_key_exists($variable, $_POST)
\r
1537 || array_key_exists($variable, $_COOKIE)
\r
1538 || array_key_exists($variable, $_ENV)
\r
1539 || (session_id() !== '' && array_key_exists($variable, $_SESSION))
\r
1540 || array_key_exists($variable, $_FILES) )
\r
1542 die('Sorry. An error occurred.');
\r
1549 * sanitizeParams()
\r
1550 * Sanitize parameters such as $_GET and $_SERVER['REQUEST_URI'] etc.
\r
1556 function sanitizeParams()
\r
1562 // REQUEST_URI of $_SERVER
\r
1563 $str =& $_SERVER["REQUEST_URI"];
\r
1564 serverStringToArray($str, $array, $frontParam);
\r
1565 sanitizeArray($array);
\r
1566 arrayToServerString($array, $frontParam, $str);
\r
1568 // QUERY_STRING of $_SERVER
\r
1569 $str =& $_SERVER["QUERY_STRING"];
\r
1570 serverStringToArray($str, $array, $frontParam);
\r
1571 sanitizeArray($array);
\r
1572 arrayToServerString($array, $frontParam, $str);
\r
1575 convArrayForSanitizing($_GET, $array);
\r
1576 sanitizeArray($array);
\r
1577 revertArrayForSanitizing($array, $_GET);
\r
1579 // $_REQUEST (only GET param)
\r
1580 convArrayForSanitizing($_REQUEST, $array);
\r
1581 sanitizeArray($array);
\r
1582 revertArrayForSanitizing($array, $_REQUEST);
\r
1587 function _addInputTags(&$keys,$prefix='')
\r
1589 foreach ( $keys as $key=>$value )
\r
1593 $key=$prefix.'['.$key.']';
\r
1595 if ( is_array($value) )
\r
1597 _addInputTags($value,$key);
\r
1601 if ( get_magic_quotes_gpc() )
\r
1602 {$value=stripslashes($value);
\r
1604 if ( $key == 'ticket' )
\r
1608 echo '<input type="hidden" name="'.Entity::hsc($key).
\r
1609 '" value="'.Entity::hsc($value).'" />'."\n";
\r
1616 * serverStringToArray()
\r
1617 * Convert the server string such as $_SERVER['REQUEST_URI']
\r
1618 * to arry like arry['blogid']=1 and array['page']=2 etc.
\r
1620 * @param string $uri string
\r
1621 * @param string &$query_elements elements of query according to application/x-www-form-urlencoded
\r
1622 * @param string &$hier_part hierarchical part includes path
\r
1625 * RFC 3986: Uniform Resource Identifiers (URI): Generic Syntax
\r
1626 * 3. Syntax Components
\r
1627 * http://www.ietf.org/rfc/rfc3986.txt
\r
1629 * Hypertext Markup Language - 2.0
\r
1630 * 8.2.1. The form-urlencoded Media Type
\r
1631 * http://tools.ietf.org/html/rfc1866#section-8.2.1
\r
1633 * $_SERVER > Language Reference > Predefined Variables > PHP Manual
\r
1634 * http://www.php.net/manual/en/reserved.variables.server.php
\r
1636 function serverStringToArray($uri, &$query_elements, &$hier_part)
\r
1639 $query_elements = array();
\r
1642 // split hierarchical part, e.g. /index.php, query and fragment, e.g. blogid=1&page=2#section1
\r
1643 if ( i18n::strpos($uri, "?") > 0 )
\r
1645 list($hier_part, $query_and_fragment) = preg_split("#\?#", $uri, 2);
\r
1649 $query_and_fragment = $uri;
\r
1653 // If there is no query like blogid=1&page=2, return
\r
1654 if ( i18n::strpos($uri, "=") == FALSE && !i18n::strlen($hier_part) )
\r
1656 $hier_part = $uri;
\r
1660 $query_elements = preg_split("#&#", $query_and_fragment);
\r
1665 * arrayToServerString()
\r
1666 * Convert array like array['blogid'] to server string
\r
1667 * such as $_SERVER['REQUEST_URI']
\r
1669 * @param array $query_elements elements of query according to application/x-www-form-urlencoded
\r
1670 * @param string $hier_part hier-part defined in RFC3986
\r
1671 * @param string &$uri return value
\r
1675 * RFC 3986: Uniform Resource Identifiers (URI): Generic Syntax
\r
1676 * 3. Syntax Components
\r
1677 * http://www.ietf.org/rfc/rfc3986.txt
\r
1679 * Hypertext Markup Language - 2.0
\r
1680 * 8.2.1. The form-urlencoded Media Type
\r
1681 * http://tools.ietf.org/html/rfc1866#section-8.2.1
\r
1683 * $_SERVER > Language Reference > Predefined Variables > PHP Manual
\r
1684 * http://www.php.net/manual/en/reserved.variables.server.php
\r
1686 function arrayToServerString($query_elements, $hier_part, &$uri)
\r
1688 if ( i18n::strpos($uri, "?") !== FALSE )
\r
1690 $uri = $hier_part . "?";
\r
1694 $uri = $hier_part;
\r
1696 if ( count($query_elements) > 0 )
\r
1698 $uri .= implode("&", $query_elements);
\r
1705 * Sanitize array parameters.
\r
1706 * This function checks both key and value.
\r
1707 * - check key if it inclues " (double quote), remove from array
\r
1708 * - check value if it includes \ (escape sequece), remove remaining string
\r
1710 * @param array &$array elements of query according to application/x-www-form-urlencoded
\r
1713 function sanitizeArray(&$array)
\r
1715 $excludeListForSanitization = array('query');
\r
1717 foreach ( $array as $k => $v )
\r
1719 // split to key and value
\r
1720 list($key, $val) = preg_split("#=#", $v, 2);
\r
1721 if ( !isset($val) )
\r
1726 // when magic quotes is on, need to use stripslashes,
\r
1727 // and then addslashes
\r
1728 if ( get_magic_quotes_gpc() )
\r
1730 $val = stripslashes($val);
\r
1733 // note that we must use addslashes here because this function is called before the db connection is made
\r
1734 // and sql_real_escape_string needs a db connection
\r
1735 $val = addslashes($val);
\r
1737 // if $key is included in exclude list, skip this param
\r
1738 if ( !in_array($key, $excludeListForSanitization) )
\r
1741 if ( i18n::strpos($val, '\\') > 0 )
\r
1743 list($val, $tmp) = preg_split('#\\\\#', $val);
\r
1746 // remove control code etc.
\r
1747 $val = strtr($val, "\0\r\n<>'\"", " ");
\r
1750 if ( preg_match('#\"#', $key) > 0 )
\r
1752 unset($array[$k]);
\r
1756 // set sanitized info
\r
1757 $array[$k] = sprintf("%s=%s", $key, $val);
\r
1764 * convArrayForSanitizing()
\r
1765 * Convert array for sanitizeArray function
\r
1767 * @param string $src array to be sanitized
\r
1768 * @param array &$array array to be temporarily stored
\r
1771 function convArrayForSanitizing($src, &$array)
\r
1774 foreach ( $src as $key => $val )
\r
1776 if ( !key_exists($key, $_GET) )
\r
1780 $array[] = sprintf("%s=%s", $key, $val);
\r
1787 * revertArrayForSanitizing()
\r
1788 * Revert array after sanitizeArray function
\r
1790 * @param array $array element of query according to application/x-www-form-urlencoded
\r
1791 * @param array &$dst combination of key and value
\r
1794 function revertArrayForSanitizing($array, &$dst)
\r
1796 foreach ( $array as $v )
\r
1798 list($key, $val) = preg_split("#=#", $v, 2);
\r
1799 $dst[$key] = $val;
\r
1808 * @param string $virtualpath
1811 function decodePathInfo($virtualpath)
1813 global $CONF, $manager, $archive, $blog, $catid, $memberid, $special;
1815 /* initialize keywords if this hasn't been done before */
1816 if ( !isset($CONF['ItemKey']) || empty($CONF['ItemKey']) )
1818 $CONF['ItemKey'] = 'item';
1821 if ( !isset($CONF['ArchiveKey']) || empty($CONF['ArchiveKey']) )
1823 $CONF['ArchiveKey'] = 'archive';
1826 if ( !isset($CONF['ArchivesKey']) || empty($CONF['ArchivesKey']) )
1828 $CONF['ArchivesKey'] = 'archives';
1831 if ( !isset($CONF['MemberKey']) || empty($CONF['MemberKey']) )
1833 $CONF['MemberKey'] = 'member';
1836 if ( !isset($CONF['BlogKey']) || empty($CONF['BlogKey']) )
1838 $CONF['BlogKey'] = 'blog';
1841 if ( !isset($CONF['CategoryKey']) || empty($CONF['CategoryKey']) )
1843 $CONF['CategoryKey'] = 'category';
1846 if ( !isset($CONF['SpecialskinKey']) || empty($CONF['SpecialskinKey']) )
1848 $CONF['SpecialskinKey'] = 'special';
1853 'type' => basename(serverVar('SCRIPT_NAME') ),
1854 'info' => $virtualpath,
1855 'complete' => &$parsed
1857 $manager->notify('ParseURL', $data);
1859 /* already parsed by the other subsystem */
1864 /* default implementation */
1865 $data = preg_split("#/#", $virtualpath);
1866 for ( $i = 0; $i < sizeof($data); $i++ )
1868 switch ( $data[$i] )
1870 /* item/1 (blogid) */
1871 case $CONF['ItemKey']:
1874 if ( $i < sizeof($data) )
1876 $itemid = (integer) $data[$i];
1880 /* archives/1 (blogid) */
1881 case $CONF['ArchivesKey']:
1883 if ( $i < sizeof($data) )
1885 $archivelist = (integer) $data[$i];
1889 /* two possibilities: archive/yyyy-mm or archive/1/yyyy-mm (with blogid) */
1890 case $CONF['ArchiveKey']:
1891 if ( (($i + 1) < sizeof($data) ) && (i18n::strpos($data[$i + 1], '-') === FALSE ) )
1893 $blogid = (integer) $data[++$i];
1896 if ( $i < sizeof($data) )
1898 $archive = $data[$i];
1905 case $CONF['BlogKey']:
1907 if ( $i < sizeof($data) )
1909 $blogid = intval($data[$i]);
1913 /* category/1 (catid) */
1914 case $CONF['CategoryKey']:
1917 if ( $i < sizeof($data) )
1919 $catid = intval($data[$i]);
1923 case $CONF['MemberKey']:
1925 if ( $i < sizeof($data) )
1927 $memberid = intval($data[$i]);
1931 case $CONF['SpecialskinKey']:
1933 if ( $i < sizeof($data) )
1935 $special = $data[$i];
1951 * Stops processing the request and redirects to the given URL.
\r
1952 * - no actual contents should have been sent to the output yet
\r
1953 * - the URL will be stripped of illegal or dangerous characters
\r
1955 * @param string $uri
\r
1958 function redirect($url)
\r
1960 $url = preg_replace('#[^a-z0-9-~+_.?\#=&;,/:@%*]#i', '', $url);
\r
1961 header('Location: ' . $url);
\r
1966 * getBookmarklet()
\r
1967 * Returns the Javascript code for a bookmarklet that works on most modern browsers
\r
1969 * @param integer $blogid ID for weblog
\r
1970 * @return script to call Bookmarklet
\r
1972 function getBookmarklet($blogid, $width=600, $height=500)
\r
1979 . "if ( x.selection )"
\r
1981 . " Q=x.selection.createRange().text;"
\r
1983 . "else if ( y.getSelection )"
\r
1985 . " Q=y.getSelection();"
\r
1987 . "else if ( x.getSelection )"
\r
1989 . " Q=x.getSelection();"
\r
1991 . "wingm = window.open('{$CONF['AdminURL']}bookmarklet.php?blogid={$blogid}"
\r
1992 . " &logtext=' + encodeURIComponent(Q) +"
\r
1993 . " '&loglink=' + encodeURIComponent(x.location.href) +"
\r
1994 . " '&loglinktitle=' + encodeURIComponent(x.title),"
\r
1996 . " 'scrollbars=yes,width={$width},height={$height},left=10,top=10,status=yes,resizable=yes');"
\r
1997 . "wingm.focus();";
\r
2004 * cleans filename of uploaded file for writing to file system
\r
2006 * @param string $str
\r
2007 * @return string $cleaned filename ready for use
\r
2009 function cleanFileName($str)
\r
2011 $str = strtolower($str);
\r
2012 $ext_point = i18n::strrpos($str,".");
\r
2013 if ( $ext_point === FALSE )
\r
2017 $ext = i18n::substr($str,$ext_point,i18n::strlen($str));
\r
2018 $str = i18n::substr($str,0,$ext_point);
\r
2020 return preg_replace("#[^a-z0-9-]#", "_", $str) . $ext;
\r
2024 * use Notification class instead of this
\r
2025 * Deprecated since 4.0:
\r
2027 function getMailFooter()
\r
2029 NOTIFICATION::get_mail_footer();
\r
2031 function isValidMailAddress($address)
\r
2033 return NOTIFICATION::address_validation($address);
\r
2036 * use Entity class instead of this
\r
2037 * Deprecated since 4.0:
\r
2039 function highlight($text, $expression, $highlight)
\r
2041 return Entity::highlight($text, $expression, $highlight);
\r
2043 function shorten($string, $maxlength, $suffix)
\r
2045 return Entity::shorten($string, $maxlength, $suffix);
\r
2047 function stringStripTags ($string)
\r
2049 return Entity::strip_tags($string);
\r
2051 function toAscii($string)
\r
2053 return Entity::anchor_footnoting($string);
\r
2055 function stringToAttribute ($string)
\r
2057 return Entity::hsc($string);
\r
2059 function stringToXML ($string)
\r
2061 return Entity::hen($string);
\r
2063 function encode_desc($data)
\r
2065 return Entity::hen($data);
\r
2068 * Centralisation of the functions that deals with locales
\r
2069 * This functions is based on the old way to deal with languages
\r
2070 * Deprecated since 4.0:
\r
2072 function getLanguageName()
\r
2074 if( ($language = i18n::convert_locale_to_old_language_file_name(i18n::get_current_locale())) === FALSE )
\r
2076 $language ='english';
\r
2080 function selectLanguage($language)
\r
2083 include($DIR_LANG . preg_replace('#[\\\\|/]#', '', $language) . '.php');
\r
2087 * use i18n class instead of these
\r
2088 * Deprecated since 4.0
\r
2090 function checkLanguage($lang)
\r
2092 return ( preg_match('#^(.+)_(.+)_(.+)$#', $lang)
\r
2093 || i18n::convert_old_language_file_name_to_locale($lang) );
\r
2095 function formatDate($format, $timestamp, $default_format, &$blog)
\r
2097 $offset = date('Z', $timestamp);
\r
2100 $offset += $blog->getTimeOffset() * 3600;
\r
2102 return i18n::formatted_datetime($format, $timestamp, $offset, $default_format);
\r
2106 * use DB class instead of these
\r
2107 * Deprecated since 4.0
\r
2109 function quickQuery($query)
\r
2111 $row = DB::getRow($query);
\r
2112 return $row['result'];
\r
2114 function mysqldate($timestamp)
\r
2116 return DB::formatDateTime($timestamp);
\r
2119 * Centralisation of the functions that generate links
\r
2120 * Deprecated since 4.0:
\r
2121 * Please use Link::FunctionName(...) instead
\r
2123 function createItemLink($itemid, $extra = '')
\r
2125 return Link::create_item_link($itemid, $extra);
\r
2127 function createMemberLink($memberid, $extra = '')
\r
2129 return Link::create_member_link($memberid, $extra);
\r
2131 function createCategoryLink($catid, $extra = '')
\r
2133 return Link::create_category_link($catid, $extra);
\r
2135 function createArchiveListLink($blogid = '', $extra = '')
\r
2137 return Link::create_archivelist_link($blogid, $extra);
\r
2139 function createArchiveLink($blogid, $archive, $extra = '')
\r
2141 return Link::create_archive_link($blogid, $archive, $extra);
\r
2143 function createBlogidLink($blogid, $params = '')
\r
2145 return Link::create_blogid_link($blogid, $params = '');
\r
2147 function createLink($type, $params)
\r
2149 return Link::create_link($type, $params);
\r
2151 function createBlogLink($url, $params)
\r
2153 return Link::create_blog_link($url, $params);
\r
2156 * use ActionLog class instead of this
\r
2157 * Deprecated since 4.0
\r
2159 function addToLog($level, $msg)
\r
2161 ActionLog::add($level, $msg);
\r
2164 * use PHP's implement
\r
2165 * Deprecated since 4.0
\r
2167 function ifset(&$var)
\r
2169 if ( isset($var) )
\r
2177 * use Manager::getPluginNameFromPid() instead of this
\r
2178 * Deprecated since 4.0
\r
2180 function getPluginNameFromPid($pid)
\r
2183 return $manager->getPluginNameFromPid($pid);
\r
2186 * use Manager::numberOfEventSubscribers() instead of this
\r
2187 * Deprecated since 4.0
\r
2189 function numberOfEventSubscribers($event)
\r
2192 return $manager->getNumberOfSubscribers($event);
\r
2196 * PluginAdmin has already the alternative implement
\r
2197 * Deprecated since 4.0
\r
2199 function ticketForPlugin()
\r
2201 global $CONF, $DIR_LIBS, $DIR_LOCALES, $DIR_PLUGINS, $manager, $member, $ticketforplugin;
\r
2204 $ticketforplugin = array();
\r
2205 $ticketforplugin['ticket'] = FALSE;
\r
2207 /* Check if using plugin's php file. */
\r
2208 $p_translated = serverVar('SCRIPT_FILENAME');
\r
2210 if (!file_exists($p_translated) )
\r
2212 header("HTTP/1.0 404 Not Found");
\r
2216 // check whether this is plugin or not
\r
2217 $p_translated = str_replace('\\', '/', $p_translated);
\r
2218 $d_plugins = str_replace('\\', '/', $DIR_PLUGINS);
\r
2219 if ( i18n::strpos($p_translated, $d_plugins) !== 0 )
\r
2224 // Solve the plugin php file or admin directory
\r
2225 $phppath = i18n::substr($p_translated, i18n::strlen($d_plugins) );
\r
2226 // Remove the first "/" if exists.
\r
2227 $phppath = preg_replace('#^/#', '', $phppath);
\r
2228 // Remove the first "NP_" and the last ".php" if exists.
\r
2229 $path = preg_replace('#^NP_(.*)\.php$#', '$1', $phppath);
\r
2230 // Remove the "/" and beyond.
\r
2231 $path = preg_replace('#^([^/]*)/(.*)$#', '$1', $path);
\r
2233 // Solve the plugin name.
\r
2234 $plugins = array();
\r
2235 $query = sprintf('SELECT pfile FROM %s;', sql_table('plugin'));
\r
2236 $res = DB::getResult($query);
\r
2238 foreach ( $res as $row )
\r
2240 $name = i18n::substr($row['pfile'], 3);
\r
2241 $plugins[strtolower($name)] = $name;
\r
2244 $res->closeCursor();
\r
2246 if ( !array_key_exists($path, $plugins) )
\r
2248 header("HTTP/1.0 404 Not Found");
\r
2253 $plugin_name = $plugins[$path];
\r
2256 /* Return if not index.php */
\r
2257 if ( ($phppath != strtolower($plugin_name) . '/')
\r
2258 && ($phppath != strtolower($plugin_name) . '/index.php') )
\r
2263 /* Exit if not logged in. */
\r
2264 if ( !$member->isLoggedIn() )
\r
2266 exit('You aren\'t logged in.');
\r
2269 /* Check if this feature is needed (ie, if "$manager->checkTicket()" is not included in the script). */
\r
2270 if ( $file = @file($p_translated) )
\r
2274 foreach($file as $line)
\r
2276 if (preg_match('#[\$]manager([\s]*)[\-]>([\s]*)checkTicket([\s]*)[\(]#i', $prevline . $line) )
\r
2281 $prevline = $line;
\r
2285 /* Show a form if not valid ticket */
\r
2286 if ( (i18n::strpos(serverVar('REQUEST_URI'), '?') !== FALSE
\r
2287 || serverVar('QUERY_STRING')
\r
2288 || strtoupper(serverVar('REQUEST_METHOD') ) == 'POST')
\r
2289 && !$manager->checkTicket() )
\r
2291 $oPluginAdmin = new PluginAdmin($plugin_name);
\r
2292 $oPluginAdmin->start();
\r
2294 echo '<p>' . _ERROR_BADTICKET . "</p>\n";
\r
2296 // Resolve URI and QUERY_STRING
\r
2297 if ($uri = serverVar('REQUEST_URI') )
\r
2299 list($uri, $qstring) = preg_split('#\?#', $uri);
\r
2303 if ( !($uri = serverVar('PHP_SELF') ) )
\r
2305 $uri = serverVar('SCRIPT_NAME');
\r
2307 $qstring = serverVar('QUERY_STRING');
\r
2311 $qstring = '?' . $qstring;
\r
2314 echo '<p>' . _SETTINGS_UPDATE . ' : ' . _QMENU_PLUGINS . ' <span style="color:red;">' . Entity::hsc($plugin_name) . "</span> ?</p>\n";
\r
2316 switch(strtoupper(serverVar('REQUEST_METHOD') ) )
\r
2319 echo '<form method="POST" action="'.Entity::hsc($uri.$qstring).'">';
\r
2320 $manager->addTicketHidden();
\r
2321 _addInputTags($_POST);
\r
2325 echo '<form method="GET" action="'.Entity::hsc($uri).'">';
\r
2326 $manager->addTicketHidden();
\r
2327 _addInputTags($_GET);
\r
2333 echo '<input type="submit" value="' . _YES . '" /> ';
\r
2334 echo '<input type="button" value="' . _NO . '" onclick="history.back(); return false;" />';
\r
2337 $oPluginAdmin->end();
\r
2341 /* Create new ticket */
\r
2342 $ticket=$manager->addTicketToUrl('');
\r
2343 $ticketforplugin['ticket'] = preg_split($ticket, i18n::strpos($ticket, 'ticket=') + 7);
\r