4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2009 The Nucleus Group
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * (see nucleus/documentation/index.html#license for more info)
14 * @license http://nucleuscms.org/license.txt GNU General Public License
15 * @copyright Copyright (C) 2002-2009 The Nucleus Group
16 * @version $Id: globalfunctions.php 1624 2012-01-09 11:36:20Z sakamocchi $
19 /* needed if we include globalfunctions from install.php */
20 global $nucleus, $CONF, $DIR_LIBS, $DIR_LANG, $manager, $member;
22 $nucleus['version'] = 'v4.00 SVN';
23 $nucleus['codename'] = '';
25 /* check and die if someone is trying to override internal globals (when register_globals turn on) */
26 checkVars(array('nucleus', 'CONF', 'DIR_LIBS', 'MYSQL_HOST', 'MYSQL_USER', 'MYSQL_PASSWORD', 'MYSQL_DATABASE', 'DIR_LANG', 'DIR_PLUGINS', 'HTTP_GET_VARS', 'HTTP_POST_VARS', 'HTTP_COOKIE_VARS', 'HTTP_ENV_VARS', 'HTTP_SESSION_VARS', 'HTTP_POST_FILES', 'HTTP_SERVER_VARS', 'GLOBALS', 'argv', 'argc', '_GET', '_POST', '_COOKIE', '_ENV', '_SESSION', '_SERVER', '_FILES'));
31 /* report all errors! */
32 error_reporting(E_ALL);
36 ini_set('display_errors','0');
37 error_reporting(E_ERROR | E_WARNING | E_PARSE);
41 * load and initialize i18n class
43 if (!class_exists('i18n', FALSE))
45 include($DIR_LIBS . 'i18n.php');
47 if ( !i18n::init('UTF-8', $DIR_LANG) )
49 exit('Fail to initialize i18n class.');
52 * FIXME: This is for compatibility, should be obsoleted near future.
54 define('_CHARSET', i18n::get_current_charset());
57 Indicates when Nucleus should display startup errors. Set to 1 if you want
58 the error enabled (default), false otherwise
61 Displays an error when visiting a public Nucleus page and headers have
62 been sent out to early. This usually indicates an error in either a
63 configuration file or a language file, and could cause Nucleus to
66 Displays an error only when visiting the admin area, and when one or
67 more of the installation files (install.php, install.sql, upgrades/
68 directory) are still on the server.
71 if ( !isset($CONF['alertOnHeadersSent'])
72 || (isset($CONF['alertOnHeadersSent'])
73 && $CONF['alertOnHeadersSent'] !== 0) )
75 $CONF['alertOnHeadersSent'] = 1;
77 $CONF['alertOnSecurityRisk'] = 1;
79 * NOTE: this should be removed when releasing 4.0
80 $CONF['ItemURL'] = $CONF['Self'];
81 $CONF['ArchiveURL'] = $CONF['Self'];
82 $CONF['ArchiveListURL'] = $CONF['Self'];
83 $CONF['MemberURL'] = $CONF['Self'];
84 $CONF['SearchURL'] = $CONF['Self'];
85 $CONF['BlogURL'] = $CONF['Self'];
86 $CONF['CategoryURL'] = $CONF['Self'];
90 * NOTE: this should be removed when releasing 4.0
91 * switch URLMode back to normal when $CONF['Self'] ends in .php
92 * this avoids urls like index.php/item/13/index.php/item/15
93 if (!isset($CONF['URLMode']) || (($CONF['URLMode'] == 'pathinfo') && (substr($CONF['Self'], strlen($CONF['Self']) - 4) == '.php'))) {
94 $CONF['URLMode'] = 'normal';
98 * Set these to 1 to allow viewing of future items or draft items
99 * Should really never do this, but can be useful for some plugins that might need to
100 * Could cause some other issues if you use future posts otr drafts
103 $CONF['allowDrafts'] = 0;
104 $CONF['allowFuture'] = 0;
106 if ( getNucleusPatchLevel() > 0 )
108 $nucleus['version'] .= '/' . getNucleusPatchLevel();
112 if ( !isset($CONF['installscript']) )
114 $CONF['installscript'] = 0;
117 /* we will use postVar, getVar, ... methods instead of $_GET, $_POST ...*/
118 if ($CONF['installscript'] != 1)
120 /* vars were already included in install.php */
121 include_once($DIR_LIBS . 'vars4.1.0.php');
124 /* sanitize option */
125 $bLoggingSanitizedResult=0;
126 $bSanitizeAndContinue=0;
128 $orgRequestURI = serverVar('REQUEST_URI');
131 /* get all variables that can come from the request and put them in the global scope */
132 $blogid = requestVar('blogid');
133 $itemid = intRequestVar('itemid');
134 $catid = intRequestVar('catid');
135 $skinid = requestVar('skinid');
136 $memberid = requestVar('memberid');
137 $archivelist = requestVar('archivelist');
138 $imagepopup = requestVar('imagepopup');
139 $archive = requestVar('archive');
140 $query = requestVar('query');
141 $highlight = requestVar('highlight');
142 $amount = requestVar('amount');
143 $action = requestVar('action');
144 $nextaction = requestVar('nextaction');
145 $maxresults = requestVar('maxresults');
146 $startpos = intRequestVar('startpos');
149 $special = requestVar('special');
150 $virtualpath = ((getVar('virtualpath') != null) ? getVar('virtualpath') : serverVar('PATH_INFO'));
152 if ( !headers_sent() )
154 header('Generator: Nucleus CMS ' . $nucleus['version']);
157 /* include core classes that are needed for login & plugin handling */
158 include_once($DIR_LIBS . 'mysql.php');
159 /* added for 3.5 sql_* wrapper */
160 global $MYSQL_HANDLER;
161 if ( !isset($MYSQL_HANDLER) )
163 $MYSQL_HANDLER = array('mysql','');
165 if ( $MYSQL_HANDLER[0] == '' )
167 $MYSQL_HANDLER[0] = 'mysql';
169 include_once($DIR_LIBS . 'sql/'.$MYSQL_HANDLER[0].'.php');
170 /* end new for 3.5 sql_* wrapper */
171 include($DIR_LIBS . 'MEMBER.php');
172 include($DIR_LIBS . 'ACTIONLOG.php');
173 include($DIR_LIBS . 'MANAGER.php');
174 include($DIR_LIBS . 'PLUGIN.php');
176 $manager =& MANAGER::instance();
179 * make sure there's no unnecessary escaping:
180 * set_magic_quotes_runtime(0);
182 if ( version_compare(PHP_VERSION, '5.3.0', '<') )
184 ini_set('magic_quotes_runtime', '0');
188 if ( !isset($CONF['UsingAdminArea']) )
190 $CONF['UsingAdminArea'] = 0;
193 /* only needed when updating logs */
194 if ( $CONF['UsingAdminArea'] )
196 /* XML-RPC client classes */
197 include($DIR_LIBS . 'xmlrpc.inc.php');
198 include_once($DIR_LIBS . 'ADMIN.php');
201 /* connect to database */
205 /* logs sanitized result if need */
206 if ( $orgRequestURI!==serverVar('REQUEST_URI') )
208 $msg = "Sanitized [" . serverVar('REMOTE_ADDR') . "] ";
209 $msg .= $orgRequestURI . " -> " . serverVar('REQUEST_URI');
210 if ( $bLoggingSanitizedResult )
212 addToLog(WARNING, $msg);
214 if ( !$bSanitizeAndContinue )
220 /* makes sure database connection gets closed on script termination */
221 register_shutdown_function('sql_disconnect');
227 * Nucleus CMS 4.0 uses 'locale' instead of 'language' to decide which translation file used
228 * Here simply convert old name to new name without checking translation file existance
229 * FIXME: This is for compatibility, should be obsoleted near future.
231 if ( !preg_match('#^(.+)_(.+)_(.+)$#', $CONF['Language'])
232 && ($CONF['Language'] = i18n::convert_old_language_file_name_to_locale($CONF['Language'])) === FALSE )
234 $CONF['Language'] = '';
236 $locale = $CONF['Language'];
238 /* Properly set $CONF['Self'] and others if it's not set...
239 * usually when we are access from admin menu
241 if ( !isset($CONF['Self']) )
243 $CONF['Self'] = $CONF['IndexURL'];
245 if ( $CONF['Self'][i18n::strlen($CONF['Self']) -1] == "/" )
247 $CONF['Self'] = i18n::substr($CONF['Self'], 0, i18n::strlen($CONF['Self']) -1);
251 $CONF['ItemURL'] = $CONF['Self'];
252 $CONF['ArchiveURL'] = $CONF['Self'];
253 $CONF['ArchiveListURL'] = $CONF['Self'];
254 $CONF['MemberURL'] = $CONF['Self'];
255 $CONF['SearchURL'] = $CONF['Self'];
256 $CONF['BlogURL'] = $CONF['Self'];
257 $CONF['CategoryURL'] = $CONF['Self'];
260 *switch URLMode back to normal when $CONF['Self'] ends in .php
261 * this avoids urls like index.php/item/13/index.php/item/15
263 if ( !isset($CONF['URLMode'])
264 || (($CONF['URLMode'] == 'pathinfo')
265 && (i18n::substr($CONF['Self'], i18n::strlen($CONF['Self']) - 4) == '.php')) )
267 $CONF['URLMode'] = 'normal';
270 /* automatically use simpler toolbar for mozilla */
271 if ( ($CONF['DisableJsTools'] == 0)
272 && strstr(serverVar('HTTP_USER_AGENT'), 'Mozilla/5.0')
273 && strstr(serverVar('HTTP_USER_AGENT'), 'Gecko') )
275 $CONF['DisableJsTools'] = 2;
278 /* login if cookies set*/
279 $member = new MEMBER();
281 /* secure cookie key settings (either 'none', 0, 8, 16, 24, or 32) */
282 if ( !isset($CONF['secureCookieKey']) )
284 $CONF['secureCookieKey'] = 24;
286 switch( $CONF['secureCookieKey'] )
289 $CONF['secureCookieKeyIP'] = preg_replace('/\.[0-9]+\.[0-9]+\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
292 $CONF['secureCookieKeyIP'] = preg_replace('/\.[0-9]+\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
295 $CONF['secureCookieKeyIP'] = preg_replace('/\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
298 $CONF['secureCookieKeyIP'] = serverVar('REMOTE_ADDR');
301 $CONF['secureCookieKeyIP'] = '';
305 * login/logout when required or renew cookies
306 * and decide locale on this session before plugin event generates
308 if ( $action == 'login' )
310 /* Form Authentication */
311 $login = postVar('login');
312 $pw = postVar('password');
313 /* shared computer or not */
314 $shared = intPostVar('shared');
315 /* avoid md5 collision by using a long key */
316 $pw=i18n::substr($pw,0,40);
318 if ( $member->login($login, $pw) )
320 $member->newCookieKey();
321 $member->setCookies($shared);
323 if ( $CONF['secureCookieKey']!=='none' )
325 /* secure cookie key */
326 $member->setCookieKey(md5($member->getCookieKey().$CONF['secureCookieKeyIP']));
330 /* allows direct access to parts of the admin area after logging in */
333 $action = $nextaction;
336 /* NOTE: include translation file and set locale */
337 $locale = include_translation($locale, $member);
338 i18n::set_current_locale($locale);
340 $manager->notify('LoginSuccess', array('member' => &$member, 'username' => $login) );
342 ACTIONLOG::add(INFO, "Login successful for $login (sharedpc=$shared)");
346 /* errormessage for [%errordiv%] */
347 $trimlogin = trim($login);
348 if ( empty($trimlogin) )
350 $errormessage = "Please enter a username.";
354 $errormessage = 'Login failed for ' . $login;
357 /* NOTE: include translation file and set locale */
358 $locale = include_translation($locale);
359 i18n::set_current_locale($locale);
361 $manager->notify('LoginFailed', array('username' => $login) );
362 ACTIONLOG::add(INFO, $errormessage);
367 * TODO: this should be removed when releasing 4.0
368 Backed out for now: See http://forum.nucleuscms.org/viewtopic.php?t=3684 for details
369 elseif ( serverVar('PHP_AUTH_USER') && serverVar('PHP_AUTH_PW') )
371 // HTTP Authentication
372 $login = serverVar('PHP_AUTH_USER');
373 $pw = serverVar('PHP_AUTH_PW');
375 if ( $member->login($login, $pw) )
377 $manager->notify('LoginSuccess',array('member' => &$member));
378 ACTIONLOG::add(INFO, "HTTP authentication successful for $login");
382 $manager->notify('LoginFailed',array('username' => $login));
383 ACTIONLOG::add(INFO, 'HTTP authentication failed for ' . $login);
385 //Since bad credentials, generate an apropriate error page
386 header("WWW-Authenticate: Basic realm=\"Nucleus CMS {$nucleus['version']}\"");
387 header('HTTP/1.0 401 Unauthorized');
388 echo 'Invalid username or password';
394 elseif ( ($action == 'logout')
396 && cookieVar($CONF['CookiePrefix'] . 'user') )
398 /* remove cookies on logout */
399 setcookie($CONF['CookiePrefix'] . 'user', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
400 setcookie($CONF['CookiePrefix'] . 'loginkey', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
402 /* NOTE: include translation file and set locale */
403 $locale = include_translation($locale);
404 i18n::set_current_locale($locale);
406 $manager->notify('Logout', array('username' => cookieVar($CONF['CookiePrefix'] . 'user') ) );
408 elseif ( cookieVar($CONF['CookiePrefix'] . 'user') )
410 /* Cookie Authentication */
411 $ck=cookieVar($CONF['CookiePrefix'] . 'loginkey');
414 * avoid md5 collision by using a long key
416 $ck = i18n::substr($ck,0,32);
417 if ( $CONF['secureCookieKey']!=='none' )
419 $ck = md5($ck.$CONF['secureCookieKeyIP']);
421 $res = $member->cookielogin(cookieVar($CONF['CookiePrefix'] . 'user'), $ck );
424 /* renew cookies when not on a shared computer */
426 && (cookieVar($CONF['CookiePrefix'] . 'sharedpc') != 1)
427 && (!headers_sent() ) )
429 $member->setCookieKey(cookieVar($CONF['CookiePrefix'] . 'loginkey'));
430 $member->setCookies();
433 /* NOTE: include translation file and set locale */
434 $locale = include_translation($locale, $member);
435 i18n::set_current_locale($locale);
439 /* NOTE: include translation file and set locale */
440 $locale = include_translation($locale);
441 i18n::set_current_locale($locale);
444 /* login completed */
445 $manager->notify('PostAuthentication', array('loggedIn' => $member->isLoggedIn() ) );
448 * Release ticket for plugin
452 /* first, let's see if the site is disabled or not. always allow admin area access. */
453 if ( $CONF['DisableSite'] && !$member->isAdmin() && !$CONF['UsingAdminArea'] )
455 redirect($CONF['DisableSiteURL']);
459 /* load other classes */
460 include($DIR_LIBS . 'PARSER.php');
461 include($DIR_LIBS . 'SKIN.php');
462 include($DIR_LIBS . 'TEMPLATE.php');
463 include($DIR_LIBS . 'BLOG.php');
464 include($DIR_LIBS . 'BODYACTIONS.php');
465 include($DIR_LIBS . 'COMMENTS.php');
466 include($DIR_LIBS . 'COMMENT.php');
467 /* include($DIR_LIBS . 'ITEM.php'); */
468 include($DIR_LIBS . 'NOTIFICATION.php');
469 include($DIR_LIBS . 'BAN.php');
470 include($DIR_LIBS . 'PAGEFACTORY.php');
471 include($DIR_LIBS . 'SEARCH.php');
472 include($DIR_LIBS . 'entity.php');
473 include($DIR_LIBS . 'Link.php');
475 /* set lastVisit cookie (if allowed) */
476 if ( !headers_sent() )
478 if ( $CONF['LastVisit'] )
480 setcookie($CONF['CookiePrefix'] . 'lastVisit', time(), time() + 2592000, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
484 setcookie($CONF['CookiePrefix'] . 'lastVisit', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
489 Backed out for now: See http://forum.nucleuscms.org/viewtopic.php?t=3684 for details
491 // To remove after v2.5 is released and language files have been updated.
492 // Including this makes sure that language files for v2.5beta can still be used for v2.5final
493 // without having weird _SETTINGS_EXTAUTH string showing up in the admin area.
494 if (!defined('_MEMBERS_BYPASS'))
496 define('_SETTINGS_EXTAUTH', 'Enable External Authentication');
497 define('_WARNING_EXTAUTH', 'Warning: Enable only if needed.');
498 define('_MEMBERS_BYPASS', 'Use External Authentication');
502 /* make sure the archivetype skinvar keeps working when _ARCHIVETYPE_XXX not defined */
503 if ( !defined('_ARCHIVETYPE_MONTH') )
505 define('_ARCHIVETYPE_DAY', 'day');
506 define('_ARCHIVETYPE_MONTH', 'month');
507 define('_ARCHIVETYPE_YEAR', 'year');
510 /* decode path_info */
511 if ( $CONF['URLMode'] == 'pathinfo' )
513 /* initialize keywords if this hasn't been done before */
514 if ( !isset($CONF['ItemKey']) || $CONF['ItemKey'] == '' )
516 $CONF['ItemKey'] = 'item';
519 if ( !isset($CONF['ArchiveKey']) || $CONF['ArchiveKey'] == '' )
521 $CONF['ArchiveKey'] = 'archive';
524 if ( !isset($CONF['ArchivesKey']) || $CONF['ArchivesKey'] == '' )
526 $CONF['ArchivesKey'] = 'archives';
529 if ( !isset($CONF['MemberKey']) || $CONF['MemberKey'] == '' )
531 $CONF['MemberKey'] = 'member';
534 if ( !isset($CONF['BlogKey']) || $CONF['BlogKey'] == '' )
536 $CONF['BlogKey'] = 'blog';
539 if ( !isset($CONF['CategoryKey']) || $CONF['CategoryKey'] == '' )
541 $CONF['CategoryKey'] = 'category';
544 if ( !isset($CONF['SpecialskinKey']) || $CONF['SpecialskinKey'] == '' )
546 $CONF['SpecialskinKey'] = 'special';
553 /* e.g. item, blog, ... */
554 'type' => basename(serverVar('SCRIPT_NAME') ),
555 'info' => $virtualpath,
556 'complete' => &$parsed
562 /* default implementation */
563 $data = i18n::explode("/", $virtualpath );
564 for ( $i = 0; $i < sizeof($data); $i++ )
568 /* item/1 (blogid) */
569 case $CONF['ItemKey']:
572 if ( $i < sizeof($data) )
574 $itemid = intval($data[$i]);
578 /* archives/1 (blogid) */
579 case $CONF['ArchivesKey']:
581 if ( $i < sizeof($data) )
583 $archivelist = intval($data[$i]);
587 /* two possibilities: archive/yyyy-mm or archive/1/yyyy-mm (with blogid) */
588 case $CONF['ArchiveKey']:
589 if ( (($i + 1) < sizeof($data) ) && (!strstr($data[$i + 1], '-') ) )
591 $blogid = intval($data[++$i]);
594 if ( $i < sizeof($data) )
596 $archive = $data[$i];
603 case $CONF['BlogKey']:
605 if ( $i < sizeof($data) )
607 $blogid = intval($data[$i]);
611 /* category/1 (catid) */
612 case $CONF['CategoryKey']:
615 if ( $i < sizeof($data) )
617 $catid = intval($data[$i]);
621 case $CONF['MemberKey']:
623 if ( $i < sizeof($data) )
625 $memberid = intval($data[$i]);
629 case $CONF['SpecialskinKey']:
631 if ( $i < sizeof($data) )
633 $special = $data[$i];
634 $_REQUEST['special'] = $special;
645 * PostParseURL is a place to cleanup any of the path-related global variables before the selector function is run.
646 * It has 2 values in the data in case the original virtualpath is needed, but most the use will be in tweaking
647 * global variables to clean up (scrub out catid or add catid) or to set someother global variable based on
648 * the values of something like catid or itemid
654 /* e.g. item, blog, ... */
655 'type' => basename(serverVar('SCRIPT_NAME') ),
656 'info' => $virtualpath
661 * NOTE: Here is the end of initialization
665 * This function includes or requires the specified library file
666 * @param string $file
667 * @param bool $once use the _once() version
668 * @param bool $require use require() instead of include()
670 function include_libs($file, $once = TRUE, $require = TRUE)
674 // begin if: $DIR_LIBS isn't a directory
675 if ( !is_dir($DIR_LIBS) )
680 $lib_path = $DIR_LIBS . $file;
683 if ( $once && $require )
685 require_once($lib_path);
687 else if ( $once && !$require )
689 include_once($lib_path);
704 * This function includes or requires the specified plugin file
705 * @param string $file
706 * @param bool $once use the _once() version
707 * @param bool $require use require() instead of include()
709 function include_plugins($file, $once = TRUE, $require = TRUE)
713 // begin if: $DIR_LIBS isn't a directory
714 if ( !is_dir($DIR_PLUGINS) )
719 $plugin_path = $DIR_PLUGINS . $file;
722 if ( $once && $require )
724 require_once($plugin_path);
726 else if ( $once && !$require )
728 include_once($plugin_path);
732 require($plugin_path);
736 include($plugin_path);
741 * This function decide which locale is used and include translation
742 * @param string $locale locale name referring to 'language tags' defined in RFC 5646
743 * @param mixed $member member object
745 function include_translation($locale, $member = FALSE)
750 * 1. user's locale is used if set
751 * 2. system default is used if it is not empty
752 * 3. else 'en_Latn_US.ISO-8859-1.php' is included
753 * because this translation file is expected to include only 7bit ASCII characters
754 * which common in whole character coding scheme
756 if ( $member && $member->getLocale() )
758 $locale = $member->getLocale();
760 $translation_file = $DIR_LANG . $locale . '.' . i18n::get_current_charset() . '.php';
761 if ( !file_exists($translation_file) )
763 $locale = 'en_Latn_US';
764 $translation_file = $DIR_LANG . 'en_Latn_US.ISO-8859-1.php';
766 include($translation_file);
771 * This function returns the integer value of $_POST for the variable $name
772 * @param string $name field to get the integer value of
775 function intPostVar($name)
777 return intval(postVar($name));
782 * This function returns the integer value of $_GET for the variable $name
783 * @param string $name field to get the integer value of
786 function intGetVar($name)
788 return intval(getVar($name));
793 * This function returns the integer value of $_REQUEST for the variable $name. Also checks $_GET and $_POST if not found in $_REQUEST
794 * @param string $name field to get the integer value of
797 function intRequestVar($name)
799 return intval(requestVar($name));
804 * This function returns the integer value of $_COOKIE for the variable $name
805 * @param string $name field to get the integer value of
808 function intCookieVar($name)
810 return intval(cookieVar($name));
815 * This function returns the current Nucleus version (100 = 1.00, 101 = 1.01, etc...)
818 function getNucleusVersion()
825 * TODO: Better description of this function.
827 * Power users can install patches in between nucleus releases. These patches
828 * usually add new functionality in the plugin API and allow those to
829 * be tested without having to install CVS.
833 function getNucleusPatchLevel()
840 * This function returns the latest Nucleus version available for download from nucleuscms.org or FALSE if unable to attain data
841 * Format will be major.minor/patachlevel e.g. 3.41 or 3.41/02
842 * @return string|bool
844 function getLatestVersion()
846 // begin if: cURL is not available in this PHP installation
847 if ( !function_exists('curl_init') )
854 curl_setopt ($curl, CURLOPT_URL, 'http://nucleuscms.org/version_check.php');
855 curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
856 curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
857 $return = curl_exec($curl);
864 * This function returns a Nucleus table name with the appropriate prefix
865 * @param string $name
868 function sql_table($name)
870 global $MYSQL_PREFIX;
872 // begin if: no MySQL prefix
873 if ( empty($MYSQL_PREFIX) )
875 return 'nucleus_' . $name;
877 // else: use MySQL prefix
880 return $MYSQL_PREFIX . 'nucleus_' . $name;
887 * TODO: This function should be changed to send_content_type() per the Coding Guidelines. Ensure this change is compatible with rest of core.
889 * This function sends the Content-Type header if headers have not already been sent
890 * It also determines if the browser can accept application/xhtml+xml and sends it only to those that can.
891 * @param string $content_type
892 * @param string $page_type
893 * @param string $charset Deprecated. This has no meaning.
895 function sendContentType($content_type, $page_type = '', $charset = _CHARSET)
897 global $manager, $CONF;
899 if ( !headers_sent() )
901 // if content type is application/xhtml+xml, only send it to browsers
902 // that can handle it (IE6 cannot). Otherwise, send text/html
904 // v2.5: For admin area pages, keep sending text/html (unless it's a debug version)
905 // application/xhtml+xml still causes too much problems with the javascript implementations
907 // v3.3: ($CONF['UsingAdminArea'] && !$CONF['debug']) gets removed,
908 // application/xhtml+xml seems to be working, so we're going to use it if we can.
910 if ( ($content_type == 'application/xhtml+xml')
911 && (!stristr(serverVar('HTTP_ACCEPT'), 'application/xhtml+xml') ) )
913 $content_type = 'text/html';
917 'PreSendContentType',
919 'contentType' => &$content_type,
920 'charset' => i18n::get_current_charset(),
921 'pageType' => $page_type
925 // strip strange characters
926 $content_type = preg_replace('|[^a-z0-9-+./]|i', '', $content_type);
927 header('Content-Type: ' . $content_type . '; charset=' . i18n::get_current_charset());
934 * This function highlights a specific query in a given HTML text (not within HTML tags) and returns it
935 * @param string $text text to be highlighted
936 * @param string $expression regular expression to be matched (can be an array of expressions as well)
937 * @param string $highlight highlight to be used (use \\0 to indicate the matched expression)
940 function highlight($text, $expression, $highlight)
943 if ( !$highlight || !$expression )
948 if ( is_array($expression) && (count($expression) == 0) )
953 // add a tag in front (is needed for preg_match_all to work correctly)
954 $text = '<!--h-->' . $text;
956 // split the HTML up so we have HTML tags
957 // $matches[0][i] = HTML + text
958 // $matches[1][i] = HTML
959 // $matches[2][i] = text
960 preg_match_all('/(<[^>]+>)([^<>]*)/', $text, $matches);
962 // throw it all together again while applying the highlight to the text pieces
965 $count_matches = count($matches[2]);
967 for ( $i = 0; $i < $count_matches; $i++ )
972 $result .= $matches[1][$i];
975 if ( is_array($expression) )
978 foreach ( $expression as $regex )
983 //$matches[2][$i] = @eregi_replace($regex, $highlight, $matches[2][$i]);
984 $matches[2][$i] = @preg_replace('#' . $regex . '#i', $highlight, $matches[2][$i]);
989 $result .= $matches[2][$i];
994 //$result .= @eregi_replace($expression, $highlight, $matches[2][$i]);
995 $result .= @preg_replace('#' . $expression . '#i', $highlight, $matches[2][$i]);
1006 * This function parses a query into an array of expressions that can be passed on to the highlight method
1007 * @param string $query
1009 function parseHighlight($query)
1011 // TODO: add more intelligent splitting logic
1013 // get rid of quotes
1014 $query = preg_replace('/\'|"/', '', $query);
1021 $aHighlight = i18n::explode(' ', $query);
1023 for ( $i = 0; $i < count($aHighlight); $i++ )
1025 $aHighlight[$i] = trim($aHighlight[$i]);
1027 if ( i18n::strlen($aHighlight[$i]) < 3 )
1029 unset($aHighlight[$i]);
1033 if ( count($aHighlight) == 1 )
1035 return $aHighlight[0];
1045 * This function checks if an email address is valid
1046 * @param string $address
1049 function isValidMailAddress($address)
1051 // enhancement made in 3.6x based on code by Quandary.
1052 if ( preg_match('/^(?!\\.)(?:\\.?[-a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~]+)+@(?!\\.)(?:\\.?(?!-)[-a-zA-Z0-9]+(?<!-)){2,}$/', $address) )
1065 * This function gets the blog ID from the blog name
1066 * @param string $name
1069 function getBlogIDFromName($name)
1071 return quickQuery('SELECT `bnumber` AS `result` FROM `' . sql_table('blog') . '` WHERE `bshortname` = "' . sql_real_escape_string($name) . '"');
1076 * This function gets the blog name from the blog ID
1080 function getBlogNameFromID($id)
1082 return quickQuery('SELECT `bname` AS `result` FROM `' . sql_table('blog') . '` WHERE `bnumber` = ' . intval($id));
1087 * This function gets the blog ID from the item ID
1088 * @param int $item_id
1091 function getBlogIDFromItemID($item_id)
1093 return quickQuery('SELECT `iblog` AS `result` FROM `' . sql_table('item') . '` WHERE `inumber` = ' . intval($item_id));
1098 * This function gets the blog ID from the comment ID
1099 * @param int $comment_id
1102 function getBlogIDFromCommentID($comment_id)
1104 return quickQuery('SELECT `cblog` AS `result` FROM `' . sql_table('comment') . '` WHERE `cnumber` = ' . intval($comment_id));
1109 * This function gets the blog ID from the category ID
1110 * @param int $category_id
1113 function getBlogIDFromCatID($category_id)
1115 return quickQuery('SELECT `cblog` AS `result` FROM `' . sql_table('category') . '` WHERE `catid` = ' . intval($category_id));
1120 * This function gets the category ID from the category name
1124 function getCatIDFromName($name)
1126 return quickQuery('SELECT `catid` AS `result` FROM `' . sql_table('category') . '` WHERE `cname` = "' . sql_real_escape_string($name) . '"');
1131 * This function performs a quick SQL query
1132 * @param string $query
1135 function quickQuery($query)
1137 $res = sql_query($query);
1138 $obj = sql_fetch_object($res);
1139 return $obj->result;
1142 function getPluginNameFromPid($pid) {
1143 $res = sql_query('SELECT pfile FROM ' . sql_table('plugin') . ' WHERE pid=' . intval($pid) );
1144 $obj = sql_fetch_object($res);
1146 // return isset($obj->pfile) ? $obj->pfile : false;
1149 function selector() {
1150 global $itemid, $blogid, $memberid, $query, $amount, $archivelist, $maxresults;
1151 global $archive, $skinid, $blog, $memberinfo, $CONF, $member;
1152 global $imagepopup, $catid, $special;
1155 $actionNames = array('addcomment', 'sendmessage', 'createaccount', 'forgotpassword', 'votepositive', 'votenegative', 'plugin');
1156 $action = requestVar('action');
1158 if (in_array($action, $actionNames) ) {
1159 global $DIR_LIBS, $errormessage;
1160 include_once($DIR_LIBS . 'ACTION.php');
1162 $errorInfo = $a->doAction($action);
1165 $errormessage = $errorInfo['message'];
1169 // show error when headers already sent out
1170 if (headers_sent() && $CONF['alertOnHeadersSent']) {
1172 // try to get line number/filename (extra headers_sent params only exists in PHP 4.3+)
1173 if (function_exists('version_compare') && version_compare('4.3.0', phpversion(), '<=') ) {
1174 headers_sent($hsFile, $hsLine);
1175 $extraInfo = ' in <code>' . $hsFile . '</code> line <code>' . $hsLine . '</code>';
1181 '<p>The page headers have already been sent out' . $extraInfo . '. This could cause Nucleus not to work in the expected way.</p><p>Usually, this is caused by spaces or newlines at the end of the <code>config.php</code> file, at the end of the language file or at the end of a plugin file. Please check this and try again.</p><p>If you don\'t want to see this error message again, without solving the problem, set <code>$CONF[\'alertOnHeadersSent\']</code> in <code>globalfunctions.php</code> to <code>0</code></p>',
1182 'Page headers already sent'
1187 // make is so ?archivelist without blogname or blogid shows the archivelist
1188 // for the default weblog
1189 if (serverVar('QUERY_STRING') == 'archivelist') {
1190 $archivelist = $CONF['DefaultBlog'];
1193 // now decide which type of skin we need
1195 // itemid given -> only show that item
1198 if (!$manager->existsItem($itemid,intval($CONF['allowFuture']),intval($CONF['allowDrafts'])) ) {
1199 doError(_ERROR_NOSUCHITEM);
1202 global $itemidprev, $itemidnext, $catid, $itemtitlenext, $itemtitleprev;
1204 // 1. get timestamp, blogid and catid for item
1205 $query = 'SELECT itime, iblog, icat FROM ' . sql_table('item') . ' WHERE inumber=' . intval($itemid);
1206 $res = sql_query($query);
1207 $obj = sql_fetch_object($res);
1209 // if a different blog id has been set through the request or selectBlog(),
1214 if ($blogid && (intval($blogid) != $obj->iblog) ) {
1215 doError(_ERROR_NOSUCHITEM);
1218 // if a category has been selected which doesn't match the item, ignore the
1220 if (($catid != 0) && ($catid != $obj->icat) ) {
1224 $blogid = $obj->iblog;
1225 $timestamp = strtotime($obj->itime);
1227 $b =& $manager->getBlog($blogid);
1229 if ($b->isValidCategory($catid) ) {
1230 $catextra = ' and icat=' . $catid;
1235 // get previous itemid and title
1236 $query = 'SELECT inumber, ititle FROM ' . sql_table('item') . ' WHERE itime<' . mysqldate($timestamp) . ' and idraft=0 and iblog=' . $blogid . $catextra . ' ORDER BY itime DESC LIMIT 1';
1237 $res = sql_query($query);
1239 $obj = sql_fetch_object($res);
1242 $itemidprev = $obj->inumber;
1243 $itemtitleprev = $obj->ititle;
1246 // get next itemid and title
1247 $query = 'SELECT inumber, ititle FROM ' . sql_table('item') . ' WHERE itime>' . mysqldate($timestamp) . ' and itime <= ' . mysqldate($b->getCorrectTime()) . ' and idraft=0 and iblog=' . $blogid . $catextra . ' ORDER BY itime ASC LIMIT 1';
1248 $res = sql_query($query);
1250 $obj = sql_fetch_object($res);
1253 $itemidnext = $obj->inumber;
1254 $itemtitlenext = $obj->ititle;
1257 } elseif ($archive) {
1261 // get next and prev month links ...
1262 global $archivenext, $archiveprev, $archivetype, $archivenextexists, $archiveprevexists;
1264 // sql queries for the timestamp of the first and the last published item
1265 $query = "SELECT UNIX_TIMESTAMP(itime) as result FROM ".sql_table('item')." WHERE idraft=0 ORDER BY itime ASC";
1266 $first_timestamp=quickQuery ($query);
1267 $query = "SELECT UNIX_TIMESTAMP(itime) as result FROM ".sql_table('item')." WHERE idraft=0 ORDER BY itime DESC";
1268 $last_timestamp=quickQuery ($query);
1270 sscanf($archive, '%d-%d-%d', $y, $m, $d);
1273 $archivetype = _ARCHIVETYPE_DAY;
1274 $t = mktime(0, 0, 0, $m, $d, $y);
1275 // one day has 24 * 60 * 60 = 86400 seconds
1276 $archiveprev = i18n::strftime('%Y-%m-%d', $t - 86400 );
1277 // check for published items
1278 if ($t > $first_timestamp) {
1279 $archiveprevexists = true;
1282 $archiveprevexists = false;
1287 $archivenext = i18n::strftime('%Y-%m-%d', $t);
1288 if ($t < $last_timestamp) {
1289 $archivenextexists = true;
1292 $archivenextexists = false;
1295 } elseif ($m == 0) {
1296 $archivetype = _ARCHIVETYPE_YEAR;
1297 $t = mktime(0, 0, 0, 12, 31, $y - 1);
1298 // one day before is in the previous year
1299 $archiveprev = i18n::strftime('%Y', $t);
1300 if ($t > $first_timestamp) {
1301 $archiveprevexists = true;
1304 $archiveprevexists = false;
1307 // timestamp for the next year
1308 $t = mktime(0, 0, 0, 1, 1, $y + 1);
1309 $archivenext = i18n::strftime('%Y', $t);
1310 if ($t < $last_timestamp) {
1311 $archivenextexists = true;
1314 $archivenextexists = false;
1317 $archivetype = _ARCHIVETYPE_MONTH;
1318 $t = mktime(0, 0, 0, $m, 1, $y);
1319 // one day before is in the previous month
1320 $archiveprev = i18n::strftime('%Y-%m', $t - 86400);
1321 if ($t > $first_timestamp) {
1322 $archiveprevexists = true;
1325 $archiveprevexists = false;
1328 // timestamp for the next month
1329 $t = mktime(0, 0, 0, $m+1, 1, $y);
1330 $archivenext = i18n::strftime('%Y-%m', $t);
1331 if ($t < $last_timestamp) {
1332 $archivenextexists = true;
1335 $archivenextexists = false;
1339 } elseif ($archivelist) {
1340 $type = 'archivelist';
1342 if (is_numeric($archivelist)) {
1343 $blogid = intVal($archivelist);
1345 $blogid = getBlogIDFromName($archivelist);
1349 doError(_ERROR_NOSUCHBLOG);
1355 $query = stripslashes($query);
1357 if (is_numeric($blogid)) {
1358 $blogid = intVal($blogid);
1360 $blogid = getBlogIDFromName($blogid);
1364 doError(_ERROR_NOSUCHBLOG);
1367 } elseif ($memberid) {
1370 if (!MEMBER::existsID($memberid) ) {
1371 doError(_ERROR_NOSUCHMEMBER);
1374 $memberinfo = $manager->getMember($memberid);
1376 } elseif ($imagepopup) {
1377 // media object (images etc.)
1378 $type = 'imagepopup';
1380 // TODO: check if media-object exists
1381 // TODO: set some vars?
1383 // show regular index page
1388 // any type of skin with catid
1389 if ($catid && !$blogid) {
1390 $blogid = getBlogIDFromCatID($catid);
1393 // decide which blog should be displayed
1395 $blogid = $CONF['DefaultBlog'];
1398 $b =& $manager->getBlog($blogid);
1399 $blog = $b; // references can't be placed in global variables?
1401 if (!$blog->isValid) {
1402 doError(_ERROR_NOSUCHBLOG);
1405 // set catid if necessary
1407 // check if the category is valid
1408 if (!$blog->isValidCategory($catid)) {
1409 doError(_ERROR_NOSUCHCATEGORY);
1411 $blog->setSelectedCategory($catid);
1415 // decide which skin should be used
1416 if ($skinid != '' && ($skinid == 0) ) {
1417 selectSkin($skinid);
1421 $skinid = $blog->getDefaultSkin();
1424 //$special = requestVar('special'); //get at top of file as global
1425 if (!empty($special) && isValidShortName($special)) {
1426 $type = strtolower($special);
1429 $skin = new SKIN($skinid);
1431 if (!$skin->isValid) {
1432 doError(_ERROR_NOSUCHSKIN);
1435 // set global skinpart variable so can determine quickly what is being parsed from any plugin or phpinclude
1440 $skin->parse($type);
1442 // check to see we should throw JustPosted event
1443 $blog->checkJustPosted();
1447 * Show error skin with given message. An optional skin-object to use can be given
1449 function doError($msg, $skin = '') {
1450 global $errormessage, $CONF, $skinid, $blogid, $manager;
1454 if (SKIN::existsID($skinid) ) {
1455 $skin = new SKIN($skinid);
1456 } elseif ($manager->existsBlogID($blogid) ) {
1457 $blog =& $manager->getBlog($blogid);
1458 $skin = new SKIN($blog->getDefaultSkin() );
1459 } elseif ($CONF['DefaultBlog']) {
1460 $blog =& $manager->getBlog($CONF['DefaultBlog']);
1461 $skin = new SKIN($blog->getDefaultSkin() );
1463 // this statement should actually never be executed
1464 $skin = new SKIN($CONF['BaseSkin']);
1469 $skinid = $skin->id;
1470 $errormessage = $msg;
1471 $skin->parse('error');
1475 function getConfig() {
1478 $query = 'SELECT * FROM ' . sql_table('config');
1479 $res = sql_query($query);
1481 while ($obj = sql_fetch_object($res) ) {
1482 $CONF[$obj->name] = $obj->value;
1486 // some checks for names of blogs, categories, templates, members, ...
1487 function isValidShortName($name) {
1489 # replaced eregi() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0
1490 # original eregi: eregi('^[a-z0-9]+$', $name)
1492 return preg_match('#^[a-z0-9]+$#i', $name);
1496 function isValidDisplayName($name) {
1498 # replaced eregi() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0
1499 # original eregi: eregi('^[a-z0-9]+[a-z0-9 ]*[a-z0-9]+$', $name)
1501 return preg_match('#^[a-z0-9]+[a-z0-9 ]*[a-z0-9]+$#i', $name);
1505 function isValidCategoryName($name) {
1509 function isValidTemplateName($name) {
1511 # replaced eregi() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0
1512 # original eregi: eregi('^[a-z0-9/]+$', $name)
1513 // added - and _ to valid characters as of 4.00
1515 return preg_match('#^[a-z0-9/_\-]+$#i', $name);
1519 function isValidSkinName($name) {
1521 # replaced eregi() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0
1522 # original eregi: eregi('^[a-z0-9/]+$', $name);
1523 // added - and _ to valid characters as of 4.00
1525 return preg_match('#^[a-z0-9/_\-]+$#i', $name);
1529 // add and remove linebreaks
1530 function addBreaks($var) {
1534 function removeBreaks($var) {
1535 return preg_replace("/<br \/>([\r\n])/", "$1", $var);
1539 * shortens a text string to maxlength
1540 * $suffix is what needs to be added at the end (end length is <= $maxlength)
1542 * The purpose is to limit the width of string for rendered screen in web browser.
1543 * So it depends on style sheet, browser's rendering scheme, client's system font.
1545 * NOTE: In general, non-Latin font such as Japanese, Chinese, Cyrillic have two times as long as Latin fonts,
1546 * but this is not always correct, for example, rendered by proportional font.
1548 * @param string $escaped_string target string
1549 * @param integer $maxlength maximum length of return string which includes suffix
1550 * @param string $suffix added in the end of shortened-string
1553 function shorten($string, $maxlength, $suffix)
1557 $decoded_entities_pcre = array();
1558 $encoded_entities = array();
1560 /* 1. store html entities */
1561 preg_match('#&[^&]+?;#', $string, $encoded_entities);
1562 if ( !$encoded_entities )
1572 foreach ( $encoded_entities as $encoded_entity )
1574 $decoded_entities_pcre[] = '#' . html_entity_decode($encoded_entity, ENT_QUOTES, i18n::get_current_charset()) . '#';
1578 /* 2. decode string */
1579 $string = html_entity_decode($string, ENT_QUOTES, i18n::get_current_charset());
1581 /* 3. shorten string and add suffix if string length is longer */
1582 if ( i18n::strlen($string) > $maxlength - i18n::strlen($suffix) )
1584 $string = i18n::substr($string, 0, $maxlength - i18n::strlen($suffix) );
1588 /* 4. recover entities */
1591 $string = preg_replace($decoded_entities_pcre, $encoded_entities, $string);
1598 * Converts a unix timestamp to a mysql DATETIME format, and places
1601 function mysqldate($timestamp) {
1602 return '"' . date('Y-m-d H:i:s', $timestamp) . '"';
1606 * functions for use in index.php
1608 function selectBlog($shortname) {
1609 global $blogid, $archivelist;
1610 $blogid = getBlogIDFromName($shortname);
1612 // also force archivelist variable, if it is set
1614 $archivelist = $blogid;
1618 function selectSkin($skinname) {
1620 $skinid = SKIN::getIdFromName($skinname);
1624 * Can take either a category ID or a category name (be aware that
1625 * multiple categories can have the same name)
1627 function selectCategory($cat) {
1629 if (is_numeric($cat) ) {
1630 $catid = intval($cat);
1632 $catid = getCatIDFromName($cat);
1636 function selectItem($id) {
1638 $itemid = intval($id);
1641 // force the use of a language file (warning: can cause warnings)
1642 function selectLanguage($language) {
1646 # replaced ereg_replace() below with preg_replace(). ereg* functions are deprecated in PHP 5.3.0
1647 # original ereg_replace: preg_replace( '@\\|/@', '', $language) . '.php')
1648 # important note that '\' must be matched with '\\\\' in preg* expressions
1650 include($DIR_LANG . preg_replace('#[\\\\|/]#', '', $language) . '.php');
1654 function parseFile($filename, $includeMode = 'normal', $includePrefix = '') {
1655 $handler = new ACTIONS('fileparser');
1656 $parser = new PARSER(SKIN::getAllowedActionsForType('fileparser'), $handler);
1657 $handler->parser =& $parser;
1659 // set IncludeMode properties of parser
1660 PARSER::setProperty('IncludeMode', $includeMode);
1661 PARSER::setProperty('IncludePrefix', $includePrefix);
1663 if (!file_exists($filename) ) {
1664 doError('A file is missing');
1667 $fsize = filesize($filename);
1674 $fd = fopen ($filename, 'r');
1675 $contents = fread ($fd, $fsize);
1678 // parse file contents
1679 $parser->parse($contents);
1683 * Outputs a debug message
1685 function debug($msg) {
1686 echo '<p><b>' . $msg . "</b></p>\n";
1690 function addToLog($level, $msg) {
1691 ACTIONLOG::add($level, $msg);
1694 // shows a link to help file
1695 function help($id) {
1699 function helpHtml($id) {
1701 return helplink($id) . '<img src="' . $CONF['AdminURL'] . 'documentation/icon-help.gif" width="15" height="15" alt="' . _HELP_TT . '" title="' . _HELP_TT . '" /></a>';
1704 function helplink($id) {
1706 return '<a href="' . $CONF['AdminURL'] . 'documentation/help.html#'. $id . '" onclick="if (event && event.preventDefault) event.preventDefault(); return help(this.href);">';
1709 function getMailFooter() {
1710 $message = "\n\n-----------------------------";
1711 $message .= "\n Powered by Nucleus CMS";
1712 $message .= "\n(http://www.nucleuscms.org/)";
1717 * Returns the name of the language to use
1718 * preference priority: member - site
1719 * defaults to english-utf8 when no good language found
1721 * NOTE: Deprecated, plugins to use this function should be re-worked as soon as possible!
1722 * TODO: this will be obsoleted soon.
1724 function getLanguageName()
1726 if( ($language = i18n::convert_locale_to_old_language_file_name(i18n::get_current_locale())) === FALSE )
1728 $language ='english';
1734 * Includes a PHP file. This method can be called while parsing templates and skins
1736 function includephp($filename) {
1737 // make predefined variables global, so most simple scripts can be used here
1739 // apache (names taken from PHP doc)
1740 global $GATEWAY_INTERFACE, $SERVER_NAME, $SERVER_SOFTWARE, $SERVER_PROTOCOL;
1741 global $REQUEST_METHOD, $QUERY_STRING, $DOCUMENT_ROOT, $HTTP_ACCEPT;
1742 global $HTTP_ACCEPT_CHARSET, $HTTP_ACCEPT_ENCODING, $HTTP_ACCEPT_LANGUAGE;
1743 global $HTTP_CONNECTION, $HTTP_HOST, $HTTP_REFERER, $HTTP_USER_AGENT;
1744 global $REMOTE_ADDR, $REMOTE_PORT, $SCRIPT_FILENAME, $SERVER_ADMIN;
1745 global $SERVER_PORT, $SERVER_SIGNATURE, $PATH_TRANSLATED, $SCRIPT_NAME;
1746 global $REQUEST_URI;
1748 // php (taken from PHP doc)
1749 global $argv, $argc, $PHP_SELF, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS;
1750 global $HTTP_POST_FILES, $HTTP_ENV_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
1753 global $PATH_INFO, $HTTPS, $HTTP_RAW_POST_DATA, $HTTP_X_FORWARDED_FOR;
1755 if (@file_exists($filename) ) {
1761 * Checks if a certain language exists
1762 * @param string $lang
1765 * NOTE: Deprecated, plugins to use this function should be re-worked as soon as possible!
1766 * TODO: this will be obsoleted soon.
1768 function checkLanguage($lang)
1770 return ( preg_match('#^(.+)_(.+)_(.+)$#', $lang)
1771 || i18n::convert_old_language_file_name_to_locale($lang) );
1775 * Checks if a certain plugin exists
1776 * @param string $plug
1779 function checkPlugin($plug) {
1781 global $DIR_PLUGINS;
1783 # replaced ereg_replace() below with preg_replace(). ereg* functions are deprecated in PHP 5.3.0
1784 # original ereg_replace: ereg_replace( '[\\|/]', '', $plug) . '.php')
1785 # important note that '\' must be matched with '\\\\' in preg* expressions
1787 return file_exists($DIR_PLUGINS . preg_replace('#[\\\\|/]#', '', $plug) . '.php');
1792 * Centralisation of the functions that generate links
1793 * Deprecated since 3.70:
1794 * Please use Link::FunctionName(...) instead
1796 function createItemLink($itemid, $extra = '') {
1797 return Link::createItemLink($itemid, $extra);
1800 function createMemberLink($memberid, $extra = '') {
1801 return Link::createMemberLink($memberid, $extra);
1804 function createCategoryLink($catid, $extra = '') {
1805 return Link::createCategoryLink($catid, $extra);
1808 function createArchiveListLink($blogid = '', $extra = '') {
1809 return Link::createArchiveListLink($blogid, $extra);
1812 function createArchiveLink($blogid, $archive, $extra = '') {
1813 return Link::createArchiveLink($blogid, $archive, $extra);
1816 function createBlogidLink($blogid, $params = '') {
1817 return Link::createBlogidLink($blogid, $params = '');
1820 function createLink($type, $params) {
1821 return Link::createLink($type, $params);
1824 function createBlogLink($url, $params) {
1825 return Link::createBlogLink($url, $params);
1831 * querystring to alter (e.g. foo=1&bar=2&x=y)
1833 * name of parameter to change (e.g. 'foo')
1835 * New value for that parameter (e.g. 3)
1837 * altered query string (for the examples above: foo=3&bar=2&x=y)
1839 function alterQueryStr($querystr, $param, $value) {
1840 $vars = i18n::explode('&', $querystr);
1843 for ($i = 0; $i < count($vars); $i++) {
1844 $v = i18n::explode('=', $vars[$i]);
1846 if ($v[0] == $param) {
1848 $vars[$i] = implode('=', $v);
1855 $vars[] = $param . '=' . $value;
1858 return ltrim(implode('&', $vars), '&');
1861 // passes one variable as hidden input field (multiple fields for arrays)
1862 // @see passRequestVars in varsx.x.x.php
1863 function passVar($key, $value) {
1865 if (is_array($value) ) {
1866 for ($i = 0; $i < sizeof($value); $i++) {
1867 passVar($key . '[' . $i . ']', $value[$i]);
1873 // other values: do stripslashes if needed
1874 ?><input type="hidden" name="<?php echo i18n::hsc($key)?>" value="<?php echo i18n::hsc(undoMagic($value) )?>" /><?php
1878 Date format functions (to be used from [%date(..)%] skinvars
1880 function formatDate($format, $timestamp, $defaultFormat, &$blog) {
1881 // apply blog offset (#42)
1882 $boffset = $blog ? $blog->getTimeOffset() * 3600 : 0;
1883 $offset = date('Z', $timestamp) + $boffset;
1894 $tz .= sprintf("%02d%02d", floor($offset / 3600), round(($offset % 3600) / 60) );
1895 return date('D, j M Y H:i:s ', $timestamp) . $tz;
1898 $timestamp -= $offset;
1899 return date('D, j M Y H:i:s ', $timestamp) . 'GMT';
1902 $timestamp -= $offset;
1903 return date('Y-m-d\TH:i:s\Z', $timestamp);
1912 $tz .= sprintf("%02d:%02d", floor($offset / 3600), round(($offset % 3600) / 60) );
1913 return date('Y-m-d\TH:i:s', $timestamp) . $tz;
1916 return i18n::strftime($format ? $format : $defaultFormat, $timestamp);
1920 function checkVars($aVars) {
1921 global $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_SESSION_VARS;
1923 foreach ($aVars as $varName) {
1925 if (phpversion() >= '4.1.0') {
1927 if ( isset($_GET[$varName])
1928 || isset($_POST[$varName])
1929 || isset($_COOKIE[$varName])
1930 || isset($_ENV[$varName])
1931 || isset($_SESSION[$varName])
1932 || isset($_FILES[$varName])
1934 die('Sorry. An error occurred.');
1939 if ( isset($HTTP_GET_VARS[$varName])
1940 || isset($HTTP_POST_VARS[$varName])
1941 || isset($HTTP_COOKIE_VARS[$varName])
1942 || isset($HTTP_ENV_VARS[$varName])
1943 || isset($HTTP_SESSION_VARS[$varName])
1944 || isset($HTTP_POST_FILES[$varName])
1946 die('Sorry. An error occurred.');
1955 * Sanitize parameters such as $_GET and $_SERVER['REQUEST_URI'] etc.
1958 function sanitizeParams()
1960 global $HTTP_SERVER_VARS;
1966 // REQUEST_URI of $HTTP_SERVER_VARS
1967 $str =& $HTTP_SERVER_VARS["REQUEST_URI"];
1968 serverStringToArray($str, $array, $frontParam);
1969 sanitizeArray($array);
1970 arrayToServerString($array, $frontParam, $str);
1972 // QUERY_STRING of $HTTP_SERVER_VARS
1973 $str =& $HTTP_SERVER_VARS["QUERY_STRING"];
1974 serverStringToArray($str, $array, $frontParam);
1975 sanitizeArray($array);
1976 arrayToServerString($array, $frontParam, $str);
1978 if (phpversion() >= '4.1.0') {
1979 // REQUEST_URI of $_SERVER
1980 $str =& $_SERVER["REQUEST_URI"];
1981 serverStringToArray($str, $array, $frontParam);
1982 sanitizeArray($array);
1983 arrayToServerString($array, $frontParam, $str);
1985 // QUERY_STRING of $_SERVER
1986 $str =& $_SERVER["QUERY_STRING"];
1987 serverStringToArray($str, $array, $frontParam);
1988 sanitizeArray($array);
1989 arrayToServerString($array, $frontParam, $str);
1993 convArrayForSanitizing($_GET, $array);
1994 sanitizeArray($array);
1995 revertArrayForSanitizing($array, $_GET);
1997 // $_REQUEST (only GET param)
1998 convArrayForSanitizing($_REQUEST, $array);
1999 sanitizeArray($array);
2000 revertArrayForSanitizing($array, $_REQUEST);
2004 * Check ticket when not checked in plugin's admin page
2006 * Also avoid the access to plugin/index.php by guest user.
2008 function ticketForPlugin()
2010 global $CONF, $DIR_PLUGINS, $member, $ticketforplugin;
2013 $ticketforplugin = array();
2014 $ticketforplugin['ticket'] = FALSE;
2016 /* $_SERVER['PATH_TRANSLATED']
2017 * http://www.php.net/manual/en/reserved.variables.server.php
2018 * Note: As of PHP 4.3.2, PATH_TRANSLATED is no longer set implicitly
2019 * under the Apache 2 SAPI in contrast to the situation in Apache 1,
2020 * where it's set to the same value as the SCRIPT_FILENAME server variable
2021 * when it's not populated by Apache.
2022 * This change was made to comply with the CGI specification
2023 * that PATH_TRANSLATED should only exist if PATH_INFO is defined.
2024 * Apache 2 users may use AcceptPathInfo = On inside httpd.conf to define PATH_INFO.
2027 /* Check if using plugin's php file. */
2028 $p_translated = serverVar('SCRIPT_FILENAME');
2030 if (!file_exists($p_translated) )
2032 header("HTTP/1.0 404 Not Found");
2036 $p_translated = str_replace('\\', '/', $p_translated);
2037 $d_plugins = str_replace('\\', '/', $DIR_PLUGINS);
2039 // This isn't plugin php file.
2040 if ( i18n::strpos($p_translated, $d_plugins) !== 0 )
2045 // Solve the plugin php file or admin directory
2046 $phppath = i18n::substr($p_translated, i18n::strlen($d_plugins) );
2047 // Remove the first "/" if exists.
2048 $phppath = preg_replace('#^/#', '', $phppath);
2049 // Remove the first "NP_" and the last ".php" if exists.
2050 $path = preg_replace('#^NP_(.*)\.php$#', '$1', $phppath);
2051 // Remove the "/" and beyond.
2052 $path = preg_replace('#^([^/]*)/(.*)$#', '$1', $path);
2054 // Solve the plugin name.
2056 $query = 'SELECT `pfile` FROM '.sql_table('plugin');
2057 $res = sql_query($query);
2059 while($row = sql_fetch_row($res) )
2061 $name = i18n::substr($row[0], 3);
2062 $plugins[strtolower($name)] = $name;
2065 sql_free_result($res);
2067 if (array_key_exists($path, $plugins))
2069 $plugin_name = $plugins[$path];
2071 else if (in_array($path, $plugins))
2073 $plugin_name = $path;
2077 header("HTTP/1.0 404 Not Found");
2081 /* Return if not index.php */
2082 if ( ($phppath != strtolower($plugin_name) . '/') && ($phppath != strtolower($plugin_name) . '/index.php') )
2087 /* Exit if not logged in. */
2088 if ( !$member->isLoggedIn() )
2090 exit('You aren\'t logged in.');
2093 global $manager, $DIR_LIBS, $DIR_LANG, $HTTP_GET_VARS, $HTTP_POST_VARS;
2095 /* Check if this feature is needed (ie, if "$manager->checkTicket()" is not included in the script). */
2096 if (!($p_translated = serverVar('PATH_TRANSLATED') ) )
2098 $p_translated = serverVar('SCRIPT_FILENAME');
2101 if ($file = @file($p_translated) )
2105 foreach($file as $line)
2107 if (preg_match('/[\$]manager([\s]*)[\-]>([\s]*)checkTicket([\s]*)[\(]/i', $prevline . $line) )
2116 /* Show a form if not valid ticket */
2117 if ( ( strstr(serverVar('REQUEST_URI'), '?') || serverVar('QUERY_STRING')
2118 || strtoupper(serverVar('REQUEST_METHOD') ) == 'POST')
2119 && (!$manager->checkTicket() ) )
2121 $oPluginAdmin = new PluginAdmin($plugin_name);
2122 $oPluginAdmin->start();
2123 echo '<p>' . _ERROR_BADTICKET . "</p>\n";
2125 /* Show the form to confirm action */
2126 // PHP 4.0.x support
2127 $get = (isset($_GET) ) ? $_GET : $HTTP_GET_VARS;
2128 $post = (isset($_POST) ) ? $_POST : $HTTP_POST_VARS;
2130 // Resolve URI and QUERY_STRING
2131 if ($uri = serverVar('REQUEST_URI') )
2133 list($uri, $qstring) = i18n::explode('?', $uri);
2137 if ( !($uri = serverVar('PHP_SELF') ) )
2139 $uri = serverVar('SCRIPT_NAME');
2141 $qstring = serverVar('QUERY_STRING');
2145 $qstring = '?' . $qstring;
2148 echo '<p>' . _SETTINGS_UPDATE . ' : ' . _QMENU_PLUGINS . ' <span style="color:red;">' . i18n::hsc($plugin_name) . "</span> ?</p>\n";
2150 switch(strtoupper(serverVar('REQUEST_METHOD') ) )
2153 echo '<form method="POST" action="'.i18n::hsc($uri.$qstring).'">';
2154 $manager->addTicketHidden();
2155 _addInputTags($post);
2159 echo '<form method="GET" action="'.i18n::hsc($uri).'">';
2160 $manager->addTicketHidden();
2161 _addInputTags($get);
2167 echo '<input type="submit" value="' . _YES . '" /> ';
2168 echo '<input type="button" value="' . _NO . '" onclick="history.back(); return false;" />';
2171 $oPluginAdmin->end();
2175 /* Create new ticket */
2176 $ticket=$manager->addTicketToUrl('');
2177 $ticketforplugin['ticket']=i18n::substr($ticket,i18n::strpos($ticket,'ticket=')+7);
2180 function _addInputTags(&$keys,$prefix=''){
2181 foreach($keys as $key=>$value){
2182 if ($prefix) $key=$prefix.'['.$key.']';
2183 if (is_array($value)) _addInputTags($value,$key);
2185 if (get_magic_quotes_gpc()) $value=stripslashes($value);
2186 if ($key=='ticket') continue;
2187 echo '<input type="hidden" name="'.i18n::hsc($key).
2188 '" value="'.i18n::hsc($value).'" />'."\n";
2194 * Convert the server string such as $_SERVER['REQUEST_URI']
2195 * to arry like arry['blogid']=1 and array['page']=2 etc.
2197 function serverStringToArray($str, &$array, &$frontParam)
2203 // split front param, e.g. /index.php, and others, e.g. blogid=1&page=2
2204 if (strstr($str, "?")){
2205 list($frontParam, $args) = preg_split("/\?/", $str, 2);
2212 // If there is no args like blogid=1&page=2, return
2213 if (!strstr($str, "=") && !i18n::strlen($frontParam)) {
2218 $array = i18n::explode("&", $args);
2222 * Convert array like array['blogid'] to server string
2223 * such as $_SERVER['REQUEST_URI']
2225 function arrayToServerString($array, $frontParam, &$str)
2227 if (strstr($str, "?")) {
2228 $str = $frontParam . "?";
2232 if (count($array)) {
2233 $str .= implode("&", $array);
2238 * Sanitize array parameters.
2239 * This function checks both key and value.
2240 * - check key if it inclues " (double quote), remove from array
2241 * - check value if it includes \ (escape sequece), remove remaining string
2243 function sanitizeArray(&$array)
2245 $excludeListForSanitization = array('query');
2246 // $excludeListForSanitization = array();
2248 foreach ($array as $k => $v) {
2250 // split to key and value
2251 list($key, $val) = preg_split("/=/", $v, 2);
2256 // when magic quotes is on, need to use stripslashes,
2257 // and then addslashes
2258 if (get_magic_quotes_gpc()) {
2259 $val = stripslashes($val);
2261 // note that we must use addslashes here because this function is called before the db connection is made
2262 // and sql_real_escape_string needs a db connection
2263 $val = addslashes($val);
2265 // if $key is included in exclude list, skip this param
2266 if (!in_array($key, $excludeListForSanitization)) {
2269 if (i18n::strpos($val, '\\')) {
2270 list($val, $tmp) = i18n::explode('\\', $val);
2273 // remove control code etc.
2274 $val = strtr($val, "\0\r\n<>'\"", " ");
2277 if (preg_match('/\"/i', $key)) {
2282 // set sanitized info
2283 $array[$k] = sprintf("%s=%s", $key, $val);
2289 * Convert array for sanitizeArray function
2291 function convArrayForSanitizing($src, &$array)
2294 foreach ($src as $key => $val) {
2295 if (key_exists($key, $_GET)) {
2296 array_push($array, sprintf("%s=%s", $key, $val));
2302 * Revert array after sanitizeArray function
2304 function revertArrayForSanitizing($array, &$dst)
2306 foreach ($array as $v) {
2307 list($key, $val) = preg_split("/=/", $v, 2);
2313 * Stops processing the request and redirects to the given URL.
2314 * - no actual contents should have been sent to the output yet
2315 * - the URL will be stripped of illegal or dangerous characters
2317 function redirect($url) {
2318 $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:@%*]|i', '', $url);
2319 header('Location: ' . $url);
2324 * Strip HTML tags from a string
2325 * This function is a bit more intelligent than a regular call to strip_tags(),
2326 * because it also deletes the contents of certain tags and cleans up any
2327 * unneeded whitespace.
2329 function stringStripTags ($string) {
2330 $string = preg_replace("/<del[^>]*>.+<\/del[^>]*>/isU", '', $string);
2331 $string = preg_replace("/<script[^>]*>.+<\/script[^>]*>/isU", '', $string);
2332 $string = preg_replace("/<style[^>]*>.+<\/style[^>]*>/isU", '', $string);
2333 $string = str_replace('>', '> ', $string);
2334 $string = str_replace('<', ' <', $string);
2335 $string = strip_tags($string);
2336 $string = preg_replace("/\s+/", " ", $string);
2337 $string = trim($string);
2342 * Make a string containing HTML safe for use in a HTML attribute
2343 * Tags are stripped and entities are normalized
2345 function stringToAttribute ($string)
2347 $string = stringStripTags($string);
2348 $string = entity::named_to_numeric($string);
2349 $string = entity::normalize_numeric($string);
2351 if ( i18n::get_current_charset() == 'UTF-8' )
2353 $string = entity::numeric_to_utf8($string);
2356 $string = entity::specialchars($string, 'html');
2357 $string = entity::numeric_to_named($string);
2362 * Make a string containing HTML safe for use in a XML document
2363 * Tags are stripped, entities are normalized and named entities are
2364 * converted to numeric entities.
2366 function stringToXML ($string)
2368 $string = stringStripTags($string);
2369 $string = entity::named_to_numeric($string);
2370 $string = entity::normalize_numeric($string);
2372 if ( i18n::get_current_charset() == 'UTF-8' )
2374 $string = entity::numeric_to_utf8($string);
2377 $string = entity::specialchars($string, 'xml');
2381 // START: functions from the end of file BLOG.php
2382 // used for mail notification (html -> text)
2383 function toAscii($html) {
2384 // strip off most tags
2385 $html = strip_tags($html,'<a>');
2386 $to_replace = "/<a[^>]*href=[\"\']([^\"^']*)[\"\'][^>]*>([^<]*)<\/a>/i";
2388 $ascii = preg_replace_callback ($to_replace, '_links_add', $html);
2389 $ascii .= "\n\n" . _links_list();
2390 return strip_tags($ascii);
2393 function _links_init() {
2395 $tmp_links = array();
2398 function _links_add($match) {
2400 array_push($tmp_links, $match[1]);
2401 return $match[2] . ' [' . sizeof($tmp_links) .']';
2404 function _links_list() {
2408 foreach ($tmp_links as $current) {
2409 $output .= "[$i] $current\n";
2414 // END: functions from the end of file BLOG.php
2416 // START: functions from the end of file ADMIN.php
2419 * replace html entities for plugin description, but available for the other strings
2420 * NOTE: we can use i18n::hen() or i18n::hsc() alternatively and this is deprecated.
2421 * @param string $data target string
2424 function encode_desc($data)
2426 return i18n::hen($data);
2430 * Returns the Javascript code for a bookmarklet that works on most modern browsers
2433 function getBookmarklet($blogid) {
2437 $document = 'document';
2438 $bookmarkletline = "javascript:Q='';x=".$document.";y=window;if(x.selection){Q=x.selection.createRange().text;}else if(y.getSelection){Q=y.getSelection();}else if(x.getSelection){Q=x.getSelection();}wingm=window.open('";
2439 $bookmarkletline .= $CONF['AdminURL'] . "bookmarklet.php?blogid=$blogid";
2440 $bookmarkletline .="&logtext='+escape(Q)+'&loglink='+escape(x.location.href)+'&loglinktitle='+escape(x.title),'nucleusbm','scrollbars=yes,width=600,height=550,left=10,top=10,status=yes,resizable=yes');wingm.focus();";
2442 return $bookmarkletline;
2444 // END: functions from the end of file ADMIN.php
2447 * Returns a variable or null if not set
2449 * @param mixed Variable
2450 * @return mixed Variable
2452 function ifset(&$var) {
2461 * Returns number of subscriber to an event
2464 * @return number of subscriber(s)
2466 function numberOfEventSubscriber($event) {
2467 $query = 'SELECT COUNT(*) as count FROM ' . sql_table('plugin_event') . ' WHERE event=\'' . $event . '\'';
2468 $res = sql_query($query);
2469 $obj = sql_fetch_object($res);
2474 * sets $special global variable for use in index.php before selector()
2479 function selectSpecialSkinType($id) {
2481 $special = strtolower($id);
2485 * cleans filename of uploaded file for writing to file system
2488 * @return String cleaned filename ready for use
2490 function cleanFileName($str) {
2491 $str = strtolower($str);
2492 $ext_point = i18n::strrpos($str,".");
2493 if ($ext_point===false) return false;
2494 $ext = i18n::substr($str,$ext_point,i18n::strlen($str));
2495 $str = i18n::substr($str,0,$ext_point);
2497 return preg_replace("/[^a-z0-9-]/","_",$str).$ext;