4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2007 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-2007 The Nucleus Group
16 * @version $Id: globalfunctions.php,v 1.24 2008-02-08 09:31:22 kimitake Exp $
17 * $NucleusJP: globalfunctions.php,v 1.23.2.7 2008/02/05 08:30:08 kimitake Exp $
20 // needed if we include globalfunctions from install.php
21 global $nucleus, $CONF, $DIR_LIBS, $DIR_LANG, $manager, $member;
23 $nucleus['version'] = 'v3.4';
24 $nucleus['codename'] = '';
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'));
30 error_reporting(E_ALL); // report all errors!
32 error_reporting(E_ERROR | E_WARNING | E_PARSE);
36 if (!isset($CONF['Self'])) {
37 $CONF['Self'] = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES);
41 Indicates when Nucleus should display startup errors. Set to 1 if you want
42 the error enabled (default), false otherwise
45 Displays an error when visiting a public Nucleus page and headers have
46 been sent out to early. This usually indicates an error in either a
47 configuration file or a language file, and could cause Nucleus to
50 Displays an error only when visiting the admin area, and when one or
51 more of the installation files (install.php, install.sql, upgrades/
52 directory) are still on the server.
54 $CONF['alertOnHeadersSent'] = 1;
55 $CONF['alertOnSecurityRisk'] = 1;
56 $CONF['ItemURL'] = $CONF['Self'];
57 $CONF['ArchiveURL'] = $CONF['Self'];
58 $CONF['ArchiveListURL'] = $CONF['Self'];
59 $CONF['MemberURL'] = $CONF['Self'];
60 $CONF['SearchURL'] = $CONF['Self'];
61 $CONF['BlogURL'] = $CONF['Self'];
62 $CONF['CategoryURL'] = $CONF['Self'];
64 // switch URLMode back to normal when $CONF['Self'] ends in .php
65 // this avoids urls like index.php/item/13/index.php/item/15
66 if (!isset($CONF['URLMode']) || (($CONF['URLMode'] == 'pathinfo') && (substr($CONF['Self'], strlen($CONF['Self']) - 4) == '.php'))) {
67 $CONF['URLMode'] = 'normal';
70 if (getNucleusPatchLevel() > 0) {
71 $nucleus['version'] .= '/' . getNucleusPatchLevel();
75 if (!isset($CONF['installscript'])) {
76 $CONF['installscript'] = 0;
79 // we will use postVar, getVar, ... methods instead of HTTP_GET_VARS or _GET
80 if ($CONF['installscript'] != 1) { // vars were already included in install.php
81 if (phpversion() >= '4.1.0') {
82 include_once($DIR_LIBS . 'vars4.1.0.php');
84 include_once($DIR_LIBS . 'vars4.0.6.php');
89 $bLoggingSanitizedResult=0;
90 $bSanitizeAndContinue=0;
92 $orgRequestURI = serverVar('REQUEST_URI');
95 // get all variables that can come from the request and put them in the global scope
96 $blogid = requestVar('blogid');
97 $itemid = intRequestVar('itemid');
98 $catid = intRequestVar('catid');
99 $skinid = requestVar('skinid');
100 $memberid = requestVar('memberid');
101 $archivelist = requestVar('archivelist');
102 $imagepopup = requestVar('imagepopup');
103 $archive = requestVar('archive');
104 $query = requestVar('query');
105 $highlight = requestVar('highlight');
106 $amount = requestVar('amount');
107 $action = requestVar('action');
108 $nextaction = requestVar('nextaction');
109 $maxresults = requestVar('maxresults');
110 $startpos = intRequestVar('startpos');
113 $virtualpath = ((getVar('virtualpath') != null) ? getVar('virtualpath') : serverVar('PATH_INFO'));
115 if (!headers_sent() ) {
116 header('Generator: Nucleus CMS ' . $nucleus['version']);
119 // include core classes that are needed for login & plugin handling
120 include($DIR_LIBS . 'mysql.php');
121 include($DIR_LIBS . 'MEMBER.php');
122 include($DIR_LIBS . 'ACTIONLOG.php');
123 include($DIR_LIBS . 'MANAGER.php');
124 include($DIR_LIBS . 'PLUGIN.php');
126 $manager =& MANAGER::instance();
128 // make sure there's no unnecessary escaping:
129 set_magic_quotes_runtime(0);
132 if (!isset($CONF['UsingAdminArea'])) {
133 $CONF['UsingAdminArea'] = 0;
136 // only needed when updating logs
137 if ($CONF['UsingAdminArea']) {
138 include($DIR_LIBS . 'xmlrpc.inc.php'); // XML-RPC client classes
139 include_once($DIR_LIBS . 'ADMIN.php');
142 // connect to database
146 // logs sanitized result if need
147 if ($orgRequestURI!==serverVar('REQUEST_URI')) {
148 $msg = "Sanitized [" . serverVar('REMOTE_ADDR') . "] ";
149 $msg .= $orgRequestURI . " -> " . serverVar('REQUEST_URI');
150 if ($bLoggingSanitizedResult) {
151 addToLog(WARNING, $msg);
153 if (!$bSanitizeAndContinue) {
158 // makes sure database connection gets closed on script termination
159 register_shutdown_function('sql_disconnect');
164 // automatically use simpler toolbar for mozilla
165 if (($CONF['DisableJsTools'] == 0) && strstr(serverVar('HTTP_USER_AGENT'), 'Mozilla/5.0') && strstr(serverVar('HTTP_USER_AGENT'), 'Gecko') ) {
166 $CONF['DisableJsTools'] = 2;
169 // login if cookies set
170 $member = new MEMBER();
172 // secure cookie key settings (either 'none', 0, 8, 16, 24, or 32)
173 if (!isset($CONF['secureCookieKey'])) $CONF['secureCookieKey']=24;
174 switch($CONF['secureCookieKey']){
176 $CONF['secureCookieKeyIP']=preg_replace('/\.[0-9]+\.[0-9]+\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
179 $CONF['secureCookieKeyIP']=preg_replace('/\.[0-9]+\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
182 $CONF['secureCookieKeyIP']=preg_replace('/\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
185 $CONF['secureCookieKeyIP']=serverVar('REMOTE_ADDR');
188 $CONF['secureCookieKeyIP']='';
191 // login/logout when required or renew cookies
192 if ($action == 'login') {
193 // Form Authentication
194 $login = postVar('login');
195 $pw = postVar('password');
196 $shared = intPostVar('shared'); // shared computer or not
198 $pw=substr($pw,0,40); // avoid md5 collision by using a long key
200 if ($member->login($login, $pw) ) {
202 $member->newCookieKey();
203 $member->setCookies($shared);
205 if ($CONF['secureCookieKey']!=='none') {
207 $member->setCookieKey(md5($member->getCookieKey().$CONF['secureCookieKeyIP']));
211 // allows direct access to parts of the admin area after logging in
213 $action = $nextaction;
216 $manager->notify('LoginSuccess', array('member' => &$member) );
218 ACTIONLOG::add(INFO, "Login successful for $login (sharedpc=$shared)");
220 // errormessage for [%errordiv%]
221 $errormessage = 'Login failed for ' . $login;
223 $manager->notify('LoginFailed', array('username' => $login) );
224 ACTIONLOG::add(INFO, $errormessage);
228 Backed out for now: See http://forum.nucleuscms.org/viewtopic.php?t=3684 for details
230 } elseif (serverVar('PHP_AUTH_USER') && serverVar('PHP_AUTH_PW')) {
231 // HTTP Authentication
232 $login = serverVar('PHP_AUTH_USER');
233 $pw = serverVar('PHP_AUTH_PW');
235 if ($member->login($login, $pw) ) {
236 $manager->notify('LoginSuccess',array('member' => &$member));
237 ACTIONLOG::add(INFO, "HTTP authentication successful for $login");
239 $manager->notify('LoginFailed',array('username' => $login));
240 ACTIONLOG::add(INFO, 'HTTP authentication failed for ' . $login);
242 //Since bad credentials, generate an apropriate error page
243 header("WWW-Authenticate: Basic realm=\"Nucleus CMS {$nucleus['version']}\"");
244 header('HTTP/1.0 401 Unauthorized');
245 echo 'Invalid username or password';
250 } elseif (($action == 'logout') && (!headers_sent() ) && cookieVar($CONF['CookiePrefix'] . 'user') ) {
251 // remove cookies on logout
252 setcookie($CONF['CookiePrefix'] . 'user', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
253 setcookie($CONF['CookiePrefix'] . 'loginkey', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
254 $manager->notify('Logout', array('username' => cookieVar($CONF['CookiePrefix'] . 'user') ) );
255 } elseif (cookieVar($CONF['CookiePrefix'] . 'user') ) {
256 // Cookie Authentication
257 $ck=cookieVar($CONF['CookiePrefix'] . 'loginkey');
259 $ck=substr($ck,0,32); // avoid md5 collision by using a long key
260 if ($CONF['secureCookieKey']!=='none') $ck=md5($ck.$CONF['secureCookieKeyIP']);
261 $res = $member->cookielogin(cookieVar($CONF['CookiePrefix'] . 'user'), $ck );
264 // renew cookies when not on a shared computer
265 if ($res && (cookieVar($CONF['CookiePrefix'] . 'sharedpc') != 1) && (!headers_sent() ) ) {
266 $member->setCookieKey(cookieVar($CONF['CookiePrefix'] . 'loginkey'));
267 $member->setCookies();
272 $manager->notify('PostAuthentication', array('loggedIn' => $member->isLoggedIn() ) );
275 // first, let's see if the site is disabled or not. always allow admin area access.
276 if ($CONF['DisableSite'] && !$member->isAdmin() && !$CONF['UsingAdminArea']) {
277 redirect($CONF['DisableSiteURL']);
281 // load other classes
282 include($DIR_LIBS . 'PARSER.php');
283 include($DIR_LIBS . 'SKIN.php');
284 include($DIR_LIBS . 'TEMPLATE.php');
285 include($DIR_LIBS . 'BLOG.php');
286 include($DIR_LIBS . 'BODYACTIONS.php');
287 include($DIR_LIBS . 'COMMENTS.php');
288 include($DIR_LIBS . 'COMMENT.php');
289 //include($DIR_LIBS . 'ITEM.php');
290 include($DIR_LIBS . 'NOTIFICATION.php');
291 include($DIR_LIBS . 'BAN.php');
292 include($DIR_LIBS . 'PAGEFACTORY.php');
293 include($DIR_LIBS . 'SEARCH.php');
294 include($DIR_LIBS . 'entity.php');
297 // set lastVisit cookie (if allowed)
298 if (!headers_sent() ) {
299 if ($CONF['LastVisit']) {
300 setcookie($CONF['CookiePrefix'] . 'lastVisit', time(), time() + 2592000, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
302 setcookie($CONF['CookiePrefix'] . 'lastVisit', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
306 // read language file, only after user has been initialized
307 $language = getLanguageName();
308 include($DIR_LANG . ereg_replace( '[\\|/]', '', $language) . '.php');
311 Backed out for now: See http://forum.nucleuscms.org/viewtopic.php?t=3684 for details
313 // To remove after v2.5 is released and language files have been updated.
314 // Including this makes sure that language files for v2.5beta can still be used for v2.5final
315 // without having weird _SETTINGS_EXTAUTH string showing up in the admin area.
316 if (!defined('_MEMBERS_BYPASS'))
318 define('_SETTINGS_EXTAUTH', 'Enable External Authentication');
319 define('_WARNING_EXTAUTH', 'Warning: Enable only if needed.');
320 define('_MEMBERS_BYPASS', 'Use External Authentication');
325 // make sure the archivetype skinvar keeps working when _ARCHIVETYPE_XXX not defined
326 if (!defined('_ARCHIVETYPE_MONTH') ) {
327 define('_ARCHIVETYPE_DAY', 'day');
328 define('_ARCHIVETYPE_MONTH', 'month');
332 if ($CONF['URLMode'] == 'pathinfo') {
333 // initialize keywords if this hasn't been done before
334 if ($CONF['ItemKey'] == '') {
335 $CONF['ItemKey'] = 'item';
338 if ($CONF['ArchiveKey'] == '') {
339 $CONF['ArchiveKey'] = 'archive';
342 if ($CONF['ArchivesKey'] == '') {
343 $CONF['ArchivesKey'] = 'archives';
346 if ($CONF['MemberKey'] == '') {
347 $CONF['MemberKey'] = 'member';
350 if ($CONF['BlogKey'] == '') {
351 $CONF['BlogKey'] = 'blog';
354 if ($CONF['CategoryKey'] == '') {
355 $CONF['CategoryKey'] = 'category';
358 if ($CONF['SpecialskinKey'] == '') {
359 $CONF['SpecialskinKey'] = 'special';
366 'type' => basename(serverVar('SCRIPT_NAME') ), // e.g. item, blog, ...
367 'info' => $virtualpath,
368 'complete' => &$parsed
373 // default implementation
374 $data = explode("/", $virtualpath );
375 for ($i = 0; $i < sizeof($data); $i++) {
377 case $CONF['ItemKey']: // item/1 (blogid)
380 if ($i < sizeof($data) ) {
381 $itemid = intval($data[$i]);
385 case $CONF['ArchivesKey']: // archives/1 (blogid)
388 if ($i < sizeof($data) ) {
389 $archivelist = intval($data[$i]);
393 case $CONF['ArchiveKey']: // two possibilities: archive/yyyy-mm or archive/1/yyyy-mm (with blogid)
394 if ((($i + 1) < sizeof($data) ) && (!strstr($data[$i + 1], '-') ) ) {
395 $blogid = intval($data[++$i]);
400 if ($i < sizeof($data) ) {
401 $archive = $data[$i];
405 case 'blogid': // blogid/1
406 case $CONF['BlogKey']: // blog/1
409 if ($i < sizeof($data) ) {
410 $blogid = intval($data[$i]);
414 case $CONF['CategoryKey']: // category/1 (catid)
418 if ($i < sizeof($data) ) {
419 $catid = intval($data[$i]);
423 case $CONF['MemberKey']:
426 if ($i < sizeof($data) ) {
427 $memberid = intval($data[$i]);
431 case $CONF['SpecialskinKey']:
434 if ($i < sizeof($data) ) {
435 $_REQUEST['special'] = $data[$i];
446 function intPostVar($name) {
447 return intval(postVar($name) );
450 function intGetVar($name) {
451 return intval(getVar($name) );
454 function intRequestVar($name) {
455 return intval(requestVar($name) );
458 function intCookieVar($name) {
459 return intval(cookieVar($name) );
463 * returns the currently used version (100 = 1.00, 101 = 1.01, etc...)
465 function getNucleusVersion() {
470 * power users can install patches in between nucleus releases. These patches
471 * usually add new functionality in the plugin API and allow those to
472 * be tested without having to install CVS.
474 function getNucleusPatchLevel() {
479 * Connects to mysql server
481 function sql_connect() {
482 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
484 $MYSQL_CONN = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('<p>Could not connect to MySQL database.</p>', 'Connect Error');
485 mysql_select_db($MYSQL_DATABASE) or startUpError('<p>Could not select database: ' . mysql_error() . '</p>', 'Connect Error');
491 * returns a prefixed nucleus table name
493 function sql_table($name) {
494 global $MYSQL_PREFIX;
497 return $MYSQL_PREFIX . 'nucleus_' . $name;
499 return 'nucleus_' . $name;
503 function sendContentType($contenttype, $pagetype = '', $charset = _CHARSET) {
504 global $manager, $CONF;
506 if (!headers_sent() ) {
507 // if content type is application/xhtml+xml, only send it to browsers
508 // that can handle it (IE6 cannot). Otherwise, send text/html
510 // v2.5: For admin area pages, keep sending text/html (unless it's a debug version)
511 // application/xhtml+xml still causes too much problems with the javascript implementations
513 // v3.3: ($CONF['UsingAdminArea'] && !$CONF['debug']) gets removed,
514 // application/xhtml+xml seems to be working, so we're going to use it if we can.
516 // Note: reverted the following function in JP version
521 ($contenttype == 'application/xhtml+xml')
522 && (!stristr(serverVar('HTTP_ACCEPT'), 'application/xhtml+xml') )
524 $contenttype = 'text/html';
529 ($contenttype == 'application/xhtml+xml')
530 && (($CONF['UsingAdminArea'] && !$CONF['debug']) || !stristr(serverVar('HTTP_ACCEPT'),'application/xhtml+xml'))
533 $contenttype = 'text/html';
537 'PreSendContentType',
539 'contentType' => &$contenttype,
540 'charset' => &$charset,
541 'pageType' => $pagetype
545 // strip strange characters
546 $contenttype = preg_replace('|[^a-z0-9-+./]|i', '', $contenttype);
547 $charset = preg_replace('|[^a-z0-9-_]|i', '', $charset);
549 if ($charset != '') {
550 header('Content-Type: ' . $contenttype . '; charset=' . $charset);
552 header('Content-Type: ' . $contenttype);
558 * Errors before the database connection has been made
560 function startUpError($msg, $title) {
562 <html xmlns="http://www.w3.org/1999/xhtml">
563 <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
564 <title><?php echo htmlspecialchars($title)?></title></head>
566 <h1><?php echo htmlspecialchars($title)?></h1>
574 * disconnects from SQL server
576 function sql_disconnect() {
581 * executes an SQL query
583 function sql_query($query) {
586 $res = mysql_query($query) or print("mySQL error with query $query: " . mysql_error() . '<p />');
592 * Highlights a specific query in a given HTML text (not within HTML tags) and returns it
595 * text to be highlighted
597 * regular expression to be matched (can be an array of expressions as well)
599 * highlight to be used (use \\0 to indicate the matched expression)
602 function highlight($text, $expression, $highlight) {
603 if (!$highlight || !$expression) {
607 if (is_array($expression) && (count($expression) == 0) ) {
611 // add a tag in front (is needed for preg_match_all to work correct)
612 $text = '<!--h-->' . $text;
614 // split the HTML up so we have HTML tags
615 // $matches[0][i] = HTML + text
616 // $matches[1][i] = HTML
617 // $matches[2][i] = text
618 preg_match_all('/(<[^>]+>)([^<>]*)/', $text, $matches);
620 // throw it all together again while applying the highlight to the text pieces
622 for ($i = 0; $i < sizeof($matches[2]); $i++) {
624 $result .= $matches[1][$i];
627 if (is_array($expression) ) {
628 foreach ($expression as $regex) {
630 $matches[2][$i] = @eregi_replace($regex, $highlight, $matches[2][$i]);
634 $result .= $matches[2][$i];
636 $result .= @eregi_replace($expression, $highlight, $matches[2][$i]);
644 * Parses a query into an array of expressions that can be passed on to the highlight method
646 function parseHighlight($query) {
647 // TODO: add more intelligent splitting logic
650 $query = preg_replace('/\'|"/', '', $query);
656 $aHighlight = explode(' ', $query);
658 for ($i = 0; $i < count($aHighlight); $i++) {
659 $aHighlight[$i] = trim($aHighlight[$i]);
661 if (strlen($aHighlight[$i]) < 3) {
662 unset($aHighlight[$i]);
666 if (count($aHighlight) == 1) {
667 return $aHighlight[0];
674 * Checks if email address is valid
676 function isValidMailAddress($address) {
677 if (preg_match('/^[a-zA-Z+0-9\._-]+@[a-zA-Z0-9\._-]+\.[A-Za-z]{2,5}$/', $address)) {
685 // some helper functions
686 function getBlogIDFromName($name) {
687 return quickQuery('SELECT bnumber as result FROM ' . sql_table('blog') . ' WHERE bshortname="' . addslashes($name) . '"');
690 function getBlogNameFromID($id) {
691 return quickQuery('SELECT bname as result FROM ' . sql_table('blog') . ' WHERE bnumber=' . intval($id) );
694 function getBlogIDFromItemID($itemid) {
695 return quickQuery('SELECT iblog as result FROM ' . sql_table('item') . ' WHERE inumber=' . intval($itemid) );
698 function getBlogIDFromCommentID($commentid) {
699 return quickQuery('SELECT cblog as result FROM ' . sql_table('comment') . ' WHERE cnumber=' . intval($commentid) );
702 function getBlogIDFromCatID($catid) {
703 return quickQuery('SELECT cblog as result FROM ' . sql_table('category') . ' WHERE catid=' . intval($catid) );
706 function getCatIDFromName($name) {
707 return quickQuery('SELECT catid as result FROM ' . sql_table('category') . ' WHERE cname="' . addslashes($name) . '"');
710 function quickQuery($q) {
711 $res = sql_query($q);
712 $obj = mysql_fetch_object($res);
716 function getPluginNameFromPid($pid) {
717 $res = sql_query('SELECT pfile FROM ' . sql_table('plugin') . ' WHERE pid=' . intval($pid) );
718 $obj = mysql_fetch_object($res);
722 function selector() {
723 global $itemid, $blogid, $memberid, $query, $amount, $archivelist, $maxresults;
724 global $archive, $skinid, $blog, $memberinfo, $CONF, $member;
725 global $imagepopup, $catid;
728 $actionNames = array('addcomment', 'sendmessage', 'createaccount', 'forgotpassword', 'votepositive', 'votenegative', 'plugin');
729 $action = requestVar('action');
731 if (in_array($action, $actionNames) ) {
732 global $DIR_LIBS, $errormessage;
733 include_once($DIR_LIBS . 'ACTION.php');
735 $errorInfo = $a->doAction($action);
738 $errormessage = $errorInfo['message'];
742 // show error when headers already sent out
743 if (headers_sent() && $CONF['alertOnHeadersSent']) {
745 // try to get line number/filename (extra headers_sent params only exists in PHP 4.3+)
746 if (function_exists('version_compare') && version_compare('4.3.0', phpversion(), '<=') ) {
747 headers_sent($hsFile, $hsLine);
748 $extraInfo = ' in <code>' . $hsFile . '</code> line <code>' . $hsLine . '</code>';
754 '<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>',
755 'Page headers already sent'
760 // make is so ?archivelist without blogname or blogid shows the archivelist
761 // for the default weblog
762 if (serverVar('QUERY_STRING') == 'archivelist') {
763 $archivelist = $CONF['DefaultBlog'];
766 // now decide which type of skin we need
768 // itemid given -> only show that item
771 if (!$manager->existsItem($itemid,0,0) ) {
772 doError(_ERROR_NOSUCHITEM);
775 global $itemidprev, $itemidnext, $catid, $itemtitlenext, $itemtitleprev;
777 // 1. get timestamp, blogid and catid for item
778 $query = 'SELECT itime, iblog, icat FROM ' . sql_table('item') . ' WHERE inumber=' . intval($itemid);
779 $res = sql_query($query);
780 $obj = mysql_fetch_object($res);
782 // if a different blog id has been set through the request or selectBlog(),
784 // if ($blogid && (intval($blogid) != $obj->iblog) ) {
785 // doError(_ERROR_NOSUCHITEM);
787 if ($blogid && (intval($blogid) != $obj->iblog) ) {
788 if (!headers_sent()) {
789 $b =& $manager->getBlog($obj->iblog);
790 $CONF['ItemURL'] = $b->getURL();
791 if ($CONF['URLMode'] == 'pathinfo' and substr($CONF['ItemURL'],-1) == '/')
792 $CONF['ItemURL'] = substr($CONF['ItemURL'], 0, -1);
793 $correctURL = createItemLink($itemid, '');
794 redirect($correctURL);
797 doError(_ERROR_NOSUCHITEM);
801 // if a category has been selected which doesn't match the item, ignore the
803 if (($catid != 0) && ($catid != $obj->icat) ) {
807 $blogid = $obj->iblog;
808 $timestamp = strtotime($obj->itime);
810 $b =& $manager->getBlog($blogid);
812 if ($b->isValidCategory($catid) ) {
813 $catextra = ' and icat=' . $catid;
816 // get previous itemid and title
817 $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';
818 $res = sql_query($query);
820 $obj = mysql_fetch_object($res);
823 $itemidprev = $obj->inumber;
824 $itemtitleprev = $obj->ititle;
827 // get next itemid and title
828 $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';
829 $res = sql_query($query);
831 $obj = mysql_fetch_object($res);
834 $itemidnext = $obj->inumber;
835 $itemtitlenext = $obj->ititle;
838 } elseif ($archive) {
842 // get next and prev month links ...
843 global $archivenext, $archiveprev, $archivetype, $archivenextexists, $archiveprevexists;
845 // sql queries for the timestamp of the first and the last published item
846 $query = "SELECT UNIX_TIMESTAMP(itime) as result FROM ".sql_table('item')." WHERE idraft=0 AND iblog=".(int)($blogid ? $blogid : $CONF['DefaultBlog'])." ORDER BY itime ASC";
847 $first_timestamp=quickQuery ($query);
848 $query = "SELECT UNIX_TIMESTAMP(itime) as result FROM ".sql_table('item')." WHERE idraft=0 AND iblog=".(int)($blogid ? $blogid : $CONF['DefaultBlog'])." ORDER BY itime DESC";
849 $last_timestamp=quickQuery ($query);
851 sscanf($archive, '%d-%d-%d', $y, $m, $d);
854 $archivetype = _ARCHIVETYPE_DAY;
855 $t = mktime(0, 0, 0, $m, $d, $y);
856 // one day has 24 * 60 * 60 = 86400 seconds
857 $archiveprev = strftime('%Y-%m-%d', $t - 86400 );
858 // check for published items
859 if ($t > $first_timestamp) {
860 $archiveprevexists = true;
863 $archiveprevexists = false;
868 $archivenext = strftime('%Y-%m-%d', $t);
869 if ($t < $last_timestamp) {
870 $archivenextexists = true;
873 $archivenextexists = false;
877 $archivetype = _ARCHIVETYPE_MONTH;
878 $t = mktime(0, 0, 0, $m, 1, $y);
879 // one day before is in the previous month
880 $archiveprev = strftime('%Y-%m', $t - 86400);
881 if ($t > $first_timestamp) {
882 $archiveprevexists = true;
885 $archiveprevexists = false;
888 // timestamp for the next month
889 $t = mktime(0, 0, 0, $m+1, 1, $y);
890 $archivenext = strftime('%Y-%m', $t);
891 if ($t < $last_timestamp) {
892 $archivenextexists = true;
895 $archivenextexists = false;
899 } elseif ($archivelist) {
900 $type = 'archivelist';
902 if (is_numeric($archivelist)) {
903 $blogid = intVal($archivelist);
905 $blogid = getBlogIDFromName($archivelist);
909 doError(_ERROR_NOSUCHBLOG);
915 $query = stripslashes($query);
916 if(preg_match("/^(\xA1{2}|\xe3\x80{2}|\x20)+$/",$query)){
919 $order = (_CHARSET == 'EUC-JP') ? 'EUC-JP, UTF-8,' : 'UTF-8, EUC-JP,';
920 $query = mb_convert_encoding($query, _CHARSET, $order.' JIS, SJIS, ASCII');
921 if (is_numeric($blogid)) {
922 $blogid = intVal($blogid);
924 $blogid = getBlogIDFromName($blogid);
928 doError(_ERROR_NOSUCHBLOG);
931 } elseif ($memberid) {
934 if (!MEMBER::existsID($memberid) ) {
935 doError(_ERROR_NOSUCHMEMBER);
938 $memberinfo = $manager->getMember($memberid);
940 } elseif ($imagepopup) {
941 // media object (images etc.)
942 $type = 'imagepopup';
944 // TODO: check if media-object exists
945 // TODO: set some vars?
947 // show regular index page
952 // decide which blog should be displayed
954 $blogid = $CONF['DefaultBlog'];
957 $b =& $manager->getBlog($blogid);
958 $blog = $b; // references can't be placed in global variables?
960 if (!$blog->isValid) {
961 doError(_ERROR_NOSUCHBLOG);
964 // set catid if necessary
966 $blog->setSelectedCategory($catid);
969 // decide which skin should be used
970 if ($skinid != '' && ($skinid == 0) ) {
975 $skinid = $blog->getDefaultSkin();
978 $special = requestVar('special');
979 if (!empty($special) && isValidShortName($special)) {
980 $type = strtolower($special);
983 $skin = new SKIN($skinid);
985 if (!$skin->isValid) {
986 doError(_ERROR_NOSUCHSKIN);
992 // check to see we should throw JustPosted event
993 $blog->checkJustPosted();
997 * Show error skin with given message. An optional skin-object to use can be given
999 function doError($msg, $skin = '') {
1000 global $errormessage, $CONF, $skinid, $blogid, $manager;
1004 if (SKIN::existsID($skinid) ) {
1005 $skin = new SKIN($skinid);
1006 } elseif ($manager->existsBlogID($blogid) ) {
1007 $blog =& $manager->getBlog($blogid);
1008 $skin = new SKIN($blog->getDefaultSkin() );
1009 } elseif ($CONF['DefaultBlog']) {
1010 $blog =& $manager->getBlog($CONF['DefaultBlog']);
1011 $skin = new SKIN($blog->getDefaultSkin() );
1013 // this statement should actually never be executed
1014 $skin = new SKIN($CONF['BaseSkin']);
1019 $errormessage = $msg;
1020 $skin->parse('error');
1024 function getConfig() {
1027 $query = 'SELECT * FROM ' . sql_table('config');
1028 $res = sql_query($query);
1030 while ($obj = mysql_fetch_object($res) ) {
1031 $CONF[$obj->name] = $obj->value;
1035 // some checks for names of blogs, categories, templates, members, ...
1036 function isValidShortName($name) {
1037 return eregi('^[a-z0-9]+$', $name);
1040 function isValidDisplayName($name) {
1041 return eregi('^[a-z0-9]+[a-z0-9 ]*[a-z0-9]+$', $name);
1044 function isValidCategoryName($name) {
1048 function isValidTemplateName($name) {
1049 return eregi('^[a-z0-9/]+$', $name);
1052 function isValidSkinName($name) {
1053 return eregi('^[a-z0-9/]+$', $name);
1056 // add and remove linebreaks
1057 function addBreaks($var) {
1061 function removeBreaks($var) {
1062 return preg_replace("/<br \/>([\r\n])/", "$1", $var);
1065 // shortens a text string to maxlength ($toadd) is what needs to be added
1066 // at the end (end length is <= $maxlength)
1067 function shorten($text, $maxlength, $toadd) {
1068 // 1. remove entities...
1069 // $trans = get_html_translation_table(HTML_ENTITIES);
1070 $trans = get_html_translation_table(HTML_SPECIALCHARS); // for Japanese
1071 $trans = array_flip($trans);
1072 $text = strtr($text, $trans);
1074 // 2. the actual shortening
1075 if (strlen($text) > $maxlength)
1076 $text = mb_strimwidth($text, 0, $maxlength, $toadd, _CHARSET);
1081 * Converts a unix timestamp to a mysql DATETIME format, and places
1084 function mysqldate($timestamp) {
1085 return '"' . date('Y-m-d H:i:s', $timestamp) . '"';
1089 * functions for use in index.php
1091 function selectBlog($shortname) {
1092 global $blogid, $archivelist;
1093 $blogid = getBlogIDFromName($shortname);
1095 // also force archivelist variable, if it is set
1097 $archivelist = $blogid;
1101 function selectSkin($skinname) {
1103 $skinid = SKIN::getIdFromName($skinname);
1107 * Can take either a category ID or a category name (be aware that
1108 * multiple categories can have the same name)
1110 function selectCategory($cat) {
1112 if (is_numeric($cat) ) {
1113 $catid = intval($cat);
1115 $catid = getCatIDFromName($cat);
1119 function selectItem($id) {
1121 $itemid = intval($id);
1124 // force the use of a language file (warning: can cause warnings)
1125 function selectLanguage($language) {
1127 include($DIR_LANG . ereg_replace( '[\\|/]', '', $language) . '.php');
1130 function parseFile($filename, $includeMode = 'normal', $includePrefix = '') {
1131 $handler = new ACTIONS('fileparser');
1132 $parser = new PARSER(SKIN::getAllowedActionsForType('fileparser'), $handler);
1133 $handler->parser =& $parser;
1135 // set IncludeMode properties of parser
1136 PARSER::setProperty('IncludeMode', $includeMode);
1137 PARSER::setProperty('IncludePrefix', $includePrefix);
1139 if (!file_exists($filename) ) {
1140 doError('A file is missing');
1143 $fsize = filesize($filename);
1150 $fd = fopen ($filename, 'r');
1151 $contents = fread ($fd, $fsize);
1154 // parse file contents
1155 $parser->parse($contents);
1159 * Outputs a debug message
1161 function debug($msg) {
1162 echo '<p><b>' . $msg . "</b></p>\n";
1166 function addToLog($level, $msg) {
1167 ACTIONLOG::add($level, $msg);
1170 // shows a link to help file
1171 function help($id) {
1175 function helpHtml($id) {
1176 return helplink($id) . '<img src="documentation/icon-help.gif" width="15" height="15" alt="' . _HELP_TT . '" /></a>';
1179 function helplink($id) {
1180 return '<a href="documentation/help.html#'. $id . '" onclick="if (event && event.preventDefault) event.preventDefault(); return help(this.href);">';
1183 function getMailFooter() {
1184 $message = "\n\n-----------------------------";
1185 $message .= "\n Powered by Nucleus CMS";
1186 $message .= "\n(http://www.nucleuscms.org/)";
1191 * Returns the name of the language to use
1192 * preference priority: member - site
1193 * defaults to english when no good language found
1195 * checks if file exists, etc...
1197 function getLanguageName() {
1198 global $CONF, $member;
1200 if ($member && $member->isLoggedIn() ) {
1201 // try to use members language
1202 $memlang = $member->getLanguage();
1204 if (($memlang != '') && (checkLanguage($memlang) ) ) {
1209 // use default language
1210 if (checkLanguage($CONF['Language']) ) {
1211 return $CONF['Language'];
1218 * Includes a PHP file. This method can be called while parsing templates and skins
1220 function includephp($filename) {
1221 // make predefined variables global, so most simple scripts can be used here
1223 // apache (names taken from PHP doc)
1224 global $GATEWAY_INTERFACE, $SERVER_NAME, $SERVER_SOFTWARE, $SERVER_PROTOCOL;
1225 global $REQUEST_METHOD, $QUERY_STRING, $DOCUMENT_ROOT, $HTTP_ACCEPT;
1226 global $HTTP_ACCEPT_CHARSET, $HTTP_ACCEPT_ENCODING, $HTTP_ACCEPT_LANGUAGE;
1227 global $HTTP_CONNECTION, $HTTP_HOST, $HTTP_REFERER, $HTTP_USER_AGENT;
1228 global $REMOTE_ADDR, $REMOTE_PORT, $SCRIPT_FILENAME, $SERVER_ADMIN;
1229 global $SERVER_PORT, $SERVER_SIGNATURE, $PATH_TRANSLATED, $SCRIPT_NAME;
1230 global $REQUEST_URI;
1232 // php (taken from PHP doc)
1233 global $argv, $argc, $PHP_SELF, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS;
1234 global $HTTP_POST_FILES, $HTTP_ENV_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
1237 global $PATH_INFO, $HTTPS, $HTTP_RAW_POST_DATA, $HTTP_X_FORWARDED_FOR;
1239 if (@file_exists($filename) ) {
1245 * Checks if a certain language/plugin exists
1247 function checkLanguage($lang) {
1249 return file_exists($DIR_LANG . ereg_replace( '[\\|/]', '', $lang) . '.php');
1252 function checkPlugin($plug) {
1253 global $DIR_PLUGINS;
1254 return file_exists($DIR_PLUGINS . ereg_replace( '[\\|/]', '', $plug) . '.php');
1258 * Centralisation of the functions that generate links
1260 function createItemLink($itemid, $extra = '') {
1261 return createLink('item', array('itemid' => $itemid, 'extra' => $extra) );
1264 function createMemberLink($memberid, $extra = '') {
1265 return createLink('member', array('memberid' => $memberid, 'extra' => $extra) );
1268 function createCategoryLink($catid, $extra = '') {
1269 return createLink('category', array('catid' => $catid, 'extra' => $extra) );
1272 function createArchiveListLink($blogid = '', $extra = '') {
1273 return createLink('archivelist', array('blogid' => $blogid, 'extra' => $extra) );
1276 function createArchiveLink($blogid, $archive, $extra = '') {
1277 return createLink('archive', array('blogid' => $blogid, 'archive' => $archive, 'extra' => $extra) );
1280 function createBlogidLink($blogid, $params = '') {
1281 return createLink('blog', array('blogid' => $blogid, 'extra' => $params) );
1284 function createLink($type, $params) {
1285 global $manager, $CONF;
1288 $usePathInfo = ($CONF['URLMode'] == 'pathinfo');
1290 // ask plugins first
1298 'params' => $params,
1299 'completed' => &$created,
1305 // if a plugin created the URL, return it
1310 // default implementation
1314 $url = $CONF['ItemURL'] . '/' . $CONF['ItemKey'] . '/' . $params['itemid'];
1316 $url = $CONF['ItemURL'] . '?itemid=' . $params['itemid'];
1322 $url = $CONF['MemberURL'] . '/' . $CONF['MemberKey'] . '/' . $params['memberid'];
1324 $url = $CONF['MemberURL'] . '?memberid=' . $params['memberid'];
1330 $url = $CONF['CategoryURL'] . '/' . $CONF['CategoryKey'] . '/' . $params['catid'];
1332 $url = $CONF['CategoryURL'] . '?catid=' . $params['catid'];
1337 if (!$params['blogid']) {
1338 $params['blogid'] = $CONF['DefaultBlog'];
1342 $url = $CONF['ArchiveListURL'] . '/' . $CONF['ArchivesKey'] . '/' . $params['blogid'];
1344 $url = $CONF['ArchiveListURL'] . '?archivelist=' . $params['blogid'];
1350 $url = $CONF['ArchiveURL'] . '/' . $CONF['ArchiveKey'] . '/'.$params['blogid'].'/' . $params['archive'];
1352 $url = $CONF['ArchiveURL'] . '?blogid='.$params['blogid'].'&archive=' . $params['archive'];
1358 $url = $CONF['BlogURL'] . '/' . $CONF['BlogKey'] . '/' . $params['blogid'];
1360 $url = $CONF['BlogURL'] . '?blogid=' . $params['blogid'];
1365 return addLinkParams($url, (isset($params['extra'])? $params['extra'] : null));
1368 function createBlogLink($url, $params) {
1370 if ($CONF['URLMode'] == 'normal') {
1371 if (strpos($url, '?') === FALSE && is_array($params)) {
1372 $fParam = reset($params);
1373 $fKey = key($params);
1374 array_shift($params);
1375 $url .= '?' . $fKey . '=' . $fParam;
1377 } elseif ($CONF['URLMode'] == 'pathinfo' && substr($url, -1) == '/') {
1378 $url = substr($url, 0, -1);
1380 return addLinkParams($url, $params);
1383 function addLinkParams($link, $params) {
1386 if (is_array($params) ) {
1388 if ($CONF['URLMode'] == 'pathinfo') {
1390 foreach ($params as $param => $value) {
1391 $link .= '/' . $param . '/' . urlencode($value);
1396 foreach ($params as $param => $value) {
1397 $link .= '&' . $param . '=' . urlencode($value);
1408 * querystring to alter (e.g. foo=1&bar=2&x=y)
1410 * name of parameter to change (e.g. 'foo')
1412 * New value for that parameter (e.g. 3)
1414 * altered query string (for the examples above: foo=3&bar=2&x=y)
1416 function alterQueryStr($querystr, $param, $value) {
1417 $vars = explode('&', $querystr);
1420 for ($i = 0; $i < count($vars); $i++) {
1421 $v = explode('=', $vars[$i]);
1423 if ($v[0] == $param) {
1425 $vars[$i] = implode('=', $v);
1432 $vars[] = $param . '=' . $value;
1435 return ltrim(implode('&', $vars), '&');
1438 // passes one variable as hidden input field (multiple fields for arrays)
1439 // @see passRequestVars in varsx.x.x.php
1440 function passVar($key, $value) {
1442 if (is_array($value) ) {
1443 for ($i = 0; $i < sizeof($value); $i++) {
1444 passVar($key . '[' . $i . ']', $value[$i]);
1450 // other values: do stripslashes if needed
1451 ?><input type="hidden" name="<?php echo htmlspecialchars($key)?>" value="<?php echo htmlspecialchars(undoMagic($value) )?>" /><?php
1455 Date format functions (to be used from [%date(..)%] skinvars
1457 function formatDate($format, $timestamp, $defaultFormat, &$blog) {
1458 // apply blog offset (#42)
1459 $boffset = $blog ? $blog->getTimeOffset() * 3600 : 0;
1460 $offset = date('Z', $timestamp) + $boffset;
1471 $tz .= sprintf("%02d%02d", floor($offset / 3600), round(($offset % 3600) / 60) );
1472 return date('D, j M Y H:i:s ', $timestamp) . $tz;
1475 $timestamp -= $offset;
1476 return date('D, j M Y H:i:s ', $timestamp) . 'GMT';
1479 $timestamp -= $offset;
1480 return date('Y-m-d\TH:i:s\Z', $timestamp);
1490 $tz .= sprintf("%02d:%02d", floor($offset / 3600), round(($offset % 3600) / 60) );
1491 return date('Y-m-d\TH:i:s', $timestamp) . $tz;
1494 return strftime($format ? $format : $defaultFormat, $timestamp);
1498 function checkVars($aVars) {
1499 global $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_SESSION_VARS;
1501 foreach ($aVars as $varName) {
1503 if (phpversion() >= '4.1.0') {
1505 if ( isset($_GET[$varName])
1506 || isset($_POST[$varName])
1507 || isset($_COOKIE[$varName])
1508 || isset($_ENV[$varName])
1509 || isset($_SESSION[$varName])
1510 || isset($_FILES[$varName])
1512 die('Sorry. An error occurred.');
1517 if ( isset($HTTP_GET_VARS[$varName])
1518 || isset($HTTP_POST_VARS[$varName])
1519 || isset($HTTP_COOKIE_VARS[$varName])
1520 || isset($HTTP_ENV_VARS[$varName])
1521 || isset($HTTP_SESSION_VARS[$varName])
1522 || isset($HTTP_POST_FILES[$varName])
1524 die('Sorry. An error occurred.');
1533 * Sanitize parameters such as $_GET and $_SERVER['REQUEST_URI'] etc.
1536 function sanitizeParams()
1538 global $HTTP_SERVER_VARS;
1544 // REQUEST_URI of $HTTP_SERVER_VARS
1545 $str =& $HTTP_SERVER_VARS["REQUEST_URI"];
1546 serverStringToArray($str, $array, $frontParam);
1547 sanitizeArray($array);
1548 arrayToServerString($array, $frontParam, $str);
1550 // QUERY_STRING of $HTTP_SERVER_VARS
1551 $str =& $HTTP_SERVER_VARS["QUERY_STRING"];
1552 serverStringToArray($str, $array, $frontParam);
1553 sanitizeArray($array);
1554 arrayToServerString($array, $frontParam, $str);
1556 if (phpversion() >= '4.1.0') {
1557 // REQUEST_URI of $_SERVER
1558 $str =& $_SERVER["REQUEST_URI"];
1559 serverStringToArray($str, $array, $frontParam);
1560 sanitizeArray($array);
1561 arrayToServerString($array, $frontParam, $str);
1563 // QUERY_STRING of $_SERVER
1564 $str =& $_SERVER["QUERY_STRING"];
1565 serverStringToArray($str, $array, $frontParam);
1566 sanitizeArray($array);
1567 arrayToServerString($array, $frontParam, $str);
1571 convArrayForSanitizing($_GET, $array);
1572 sanitizeArray($array);
1573 revertArrayForSanitizing($array, $_GET);
1575 // $_REQUEST (only GET param)
1576 convArrayForSanitizing($_REQUEST, $array);
1577 sanitizeArray($array);
1578 revertArrayForSanitizing($array, $_REQUEST);
1582 * Check ticket when not checked in plugin's admin page
1584 * Also avoid the access to plugin/index.php by guest user.
1586 function ticketForPlugin(){
1587 global $CONF,$DIR_PLUGINS,$member,$ticketforplugin;
1590 $ticketforplugin=array();
1591 $ticketforplugin['ticket']=false;
1593 /* Check if using plugin's php file. */
1594 if ($p_translated=serverVar('PATH_TRANSLATED')) {
1595 if (!file_exists($p_translated)) $p_translated='';
1597 if (!$p_translated) {
1598 $p_translated=serverVar('SCRIPT_FILENAME');
1599 if (!file_exists($p_translated)) {
1600 header("HTTP/1.0 404 Not Found");
1604 $p_translated=str_replace('\\','/',$p_translated);
1605 $d_plugins=str_replace('\\','/',$DIR_PLUGINS);
1606 if (strpos($p_translated,$d_plugins)!==0) return;// This isn't plugin php file.
1608 /* Solve the plugin php file or admin directory */
1609 $phppath=substr($p_translated,strlen($d_plugins));
1610 $phppath=preg_replace('!^/!','',$phppath);// Remove the first "/" if exists.
1611 $path=preg_replace('/^NP_(.*)\.php$/','$1',$phppath); // Remove the first "NP_" and the last ".php" if exists.
1612 $path=preg_replace('!^([^/]*)/(.*)$!','$1',$path); // Remove the "/" and beyond.
1614 /* Solve the plugin name. */
1616 $query='SELECT pfile FROM '.sql_table('plugin');
1617 $res=sql_query($query);
1618 while($row=mysql_fetch_row($res)) {
1619 $name=substr($row[0],3);
1620 $plugins[strtolower($name)]=$name;
1622 mysql_free_result($res);
1623 if ($plugins[$path]) $plugin_name=$plugins[$path];
1624 else if (in_array($path,$plugins)) $plugin_name=$path;
1626 header("HTTP/1.0 404 Not Found");
1630 /* Return if not index.php */
1631 if ( $phppath!=strtolower($plugin_name).'/'
1632 && $phppath!=strtolower($plugin_name).'/index.php' ) return;
1634 /* Exit if not logged in. */
1635 if ( !$member->isLoggedIn() ) exit("You aren't logged in.");
1637 global $manager,$DIR_LIBS,$DIR_LANG,$HTTP_GET_VARS,$HTTP_POST_VARS;
1639 /* Check if this feature is needed (ie, if "$manager->checkTicket()" is not included in the script). */
1640 if (!($p_translated=serverVar('PATH_TRANSLATED'))) $p_translated=serverVar('SCRIPT_FILENAME');
1641 if ($file=@file($p_translated)) {
1643 foreach($file as $line) {
1644 if (preg_match('/[\$]manager([\s]*)[\-]>([\s]*)checkTicket([\s]*)[\(]/i',$prevline.$line)) return;
1649 /* Show a form if not valid ticket */
1650 if ( ( strstr(serverVar('REQUEST_URI'),'?') || serverVar('QUERY_STRING')
1651 || strtoupper(serverVar('REQUEST_METHOD'))=='POST' )
1652 && (!$manager->checkTicket()) ){
1654 if (!class_exists('PluginAdmin')) {
1655 $language = getLanguageName();
1656 include($DIR_LANG . ereg_replace( '[\\|/]', '', $language) . '.php');
1657 include($DIR_LIBS . 'PLUGINADMIN.php');
1659 if (!(function_exists('mb_strimwidth') || extension_loaded('mbstring'))) {
1660 if (file_exists($DIR_LIBS.'mb_emulator/mb-emulator.php')) {
1661 global $mbemu_internals;
1662 include_once($DIR_LIBS.'mb_emulator/mb-emulator.php');
1665 $oPluginAdmin = new PluginAdmin($plugin_name);
1666 $oPluginAdmin->start();
1667 echo '<p>' . _ERROR_BADTICKET . "</p>\n";
1669 /* Show the form to confirm action */
1670 // PHP 4.0.x support
1671 $get= (isset($_GET)) ? $_GET : $HTTP_GET_VARS;
1672 $post= (isset($_POST)) ? $_POST : $HTTP_POST_VARS;
1673 // Resolve URI and QUERY_STRING
1674 if ($uri=serverVar('REQUEST_URI')) {
1675 list($uri,$qstring)=explode('?',$uri);
1677 if ( !($uri=serverVar('PHP_SELF')) ) $uri=serverVar('SCRIPT_NAME');
1678 $qstring=serverVar('QUERY_STRING');
1680 if ($qstring) $qstring='?'.$qstring;
1681 echo '<p>'._SETTINGS_UPDATE.' : '._QMENU_PLUGINS.' <span style="color:red;">'.
1682 htmlspecialchars($plugin_name)."</span> ?</p>\n";
1683 switch(strtoupper(serverVar('REQUEST_METHOD'))){
1685 echo '<form method="POST" action="'.htmlspecialchars($uri.$qstring).'">';
1686 $manager->addTicketHidden();
1687 _addInputTags($post);
1690 echo '<form method="GET" action="'.htmlspecialchars($uri).'">';
1691 $manager->addTicketHidden();
1692 _addInputTags($get);
1696 echo '<input type="submit" value="'._YES.'" /> ';
1697 echo '<input type="button" value="'._NO.'" onclick="history.back(); return false;" />';
1700 $oPluginAdmin->end();
1704 /* Create new ticket */
1705 $ticket=$manager->addTicketToUrl('');
1706 $ticketforplugin['ticket']=substr($ticket,strpos($ticket,'ticket=')+7);
1708 function _addInputTags(&$keys,$prefix=''){
1709 foreach($keys as $key=>$value){
1710 if ($prefix) $key=$prefix.'['.$key.']';
1711 if (is_array($value)) _addInputTags($value,$key);
1713 if (get_magic_quotes_gpc()) $value=stripslashes($value);
1714 if ($key=='ticket') continue;
1715 echo '<input type="hidden" name="'.htmlspecialchars($key).
1716 '" value="'.htmlspecialchars($value).'" />'."\n";
1722 * Convert the server string such as $_SERVER['REQUEST_URI']
1723 * to arry like arry['blogid']=1 and array['page']=2 etc.
1725 function serverStringToArray($str, &$array, &$frontParam)
1731 // split front param, e.g. /index.php, and others, e.g. blogid=1&page=2
1732 if (strstr($str, "?")){
1733 list($frontParam, $args) = preg_split("/\?/", $str, 2);
1740 // If there is no args like blogid=1&page=2, return
1741 if (!strstr($str, "=") && !strlen($frontParam)) {
1746 $array = explode("&", $args);
1750 * Convert array like array['blogid'] to server string
1751 * such as $_SERVER['REQUEST_URI']
1753 function arrayToServerString($array, $frontParam, &$str)
1755 if (strstr($str, "?")) {
1756 $str = $frontParam . "?";
1760 if (count($array)) {
1761 $str .= implode("&", $array);
1766 * Sanitize array parameters.
1767 * This function checks both key and value.
1768 * - check key if it inclues " (double quote), remove from array
1769 * - check value if it includes \ (escape sequece), remove remaining string
1771 function sanitizeArray(&$array)
1773 $excludeListForSanitization = array('query');
1774 // $excludeListForSanitization = array();
1776 foreach ($array as $k => $v) {
1778 // split to key and value
1779 list($key, $val) = preg_split("/=/", $v, 2);
1784 // when magic quotes is on, need to use stripslashes,
1785 // and then addslashes
1786 if (get_magic_quotes_gpc()) {
1787 $val = stripslashes($val);
1789 $val = addslashes($val);
1791 // if $key is included in exclude list, skip this param
1792 if (!in_array($key, $excludeListForSanitization)) {
1795 @list($val, $tmp) = explode('\\', $val);
1797 // remove control code etc.
1798 $val = strtr($val, "\0\r\n<>'\"", " ");
1801 if (preg_match('/\"/i', $key)) {
1806 // set sanitized info
1807 $array[$k] = sprintf("%s=%s", $key, $val);
1813 * Convert array for sanitizeArray function
1815 function convArrayForSanitizing($src, &$array)
1818 foreach ($src as $key => $val) {
1819 if (key_exists($key, $_GET)) {
1820 array_push($array, sprintf("%s=%s", $key, $val));
1826 * Revert array after sanitizeArray function
1828 function revertArrayForSanitizing($array, &$dst)
1830 foreach ($array as $v) {
1831 list($key, $val) = preg_split("/=/", $v, 2);
1837 * Stops processing the request and redirects to the given URL.
1838 * - no actual contents should have been sent to the output yet
1839 * - the URL will be stripped of illegal or dangerous characters
1841 function redirect($url) {
1842 $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:@%]|i', '', $url);
1843 header('Location: ' . $url);
1848 * Strip HTML tags from a string
1849 * This function is a bit more intelligent than a regular call to strip_tags(),
1850 * because it also deletes the contents of certain tags and cleans up any
1851 * unneeded whitespace.
1853 function stringStripTags ($string) {
1854 $string = preg_replace("/<del[^>]*>.+<\/del[^>]*>/isU", '', $string);
1855 $string = preg_replace("/<script[^>]*>.+<\/script[^>]*>/isU", '', $string);
1856 $string = preg_replace("/<style[^>]*>.+<\/style[^>]*>/isU", '', $string);
1857 $string = str_replace('>', '> ', $string);
1858 $string = str_replace('<', ' <', $string);
1859 $string = strip_tags($string);
1860 $string = preg_replace("/\s+/", " ", $string);
1861 $string = trim($string);
1866 * Make a string containing HTML safe for use in a HTML attribute
1867 * Tags are stripped and entities are normalized
1869 function stringToAttribute ($string) {
1870 $string = stringStripTags($string);
1871 $string = entity::named_to_numeric($string);
1872 $string = entity::normalize_numeric($string);
1874 if (_CHARSET == 'UTF-8') {
1875 $string = entity::numeric_to_utf8($string);
1878 $string = entity::specialchars($string, 'html');
1879 $string = entity::numeric_to_named($string);
1884 * Make a string containing HTML safe for use in a XML document
1885 * Tags are stripped, entities are normalized and named entities are
1886 * converted to numeric entities.
1888 function stringToXML ($string) {
1889 $string = stringStripTags($string);
1890 $string = entity::named_to_numeric($string);
1891 $string = entity::normalize_numeric($string);
1893 if (_CHARSET == 'UTF-8') {
1894 $string = entity::numeric_to_utf8($string);
1897 $string = entity::specialchars($string, 'xml');
1901 // START: functions from the end of file BLOG.php
1902 // used for mail notification (html -> text)
1903 function toAscii($html) {
1904 // strip off most tags
1905 $html = strip_tags($html,'<a>');
1906 $to_replace = "/<a[^>]*href=[\"\']([^\"^']*)[\"\'][^>]*>([^<]*)<\/a>/i";
1908 $ascii = preg_replace_callback ($to_replace, '_links_add', $html);
1909 $ascii .= "\n\n" . _links_list();
1910 return strip_tags($ascii);
1913 function _links_init() {
1915 $tmp_links = array();
1918 function _links_add($match) {
1920 array_push($tmp_links, $match[1]);
1921 return $match[2] . ' [' . sizeof($tmp_links) .']';
1924 function _links_list() {
1928 foreach ($tmp_links as $current) {
1929 $output .= "[$i] $current\n";
1934 // END: functions from the end of file BLOG.php
1936 // START: functions from the end of file ADMIN.php
1938 * @todo document this
1940 function encode_desc(&$data)
1941 { //_$to_entities = get_html_translation_table(HTML_ENTITIES);
1942 $to_entities = get_html_translation_table(HTML_SPECIALCHARS);
1943 $from_entities = array_flip($to_entities);
1944 $data = str_replace('<br />','\n',$data); //hack
1945 $data = strtr($data,$from_entities);
1946 $data = strtr($data,$to_entities);
1947 $data = str_replace('\n','<br />',$data); //hack
1952 * Returns the Javascript code for a bookmarklet that works on most modern browsers
1956 function getBookmarklet($blogid) {
1960 $document = 'document';
1961 $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('";
1962 $bookmarkletline .= $CONF['AdminURL'] . "bookmarklet.php?blogid=$blogid";
1963 $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();";
1965 return $bookmarkletline;
1967 // END: functions from the end of file ADMIN.php
1970 * Returns a variable or null if not set
1972 * @param mixed Variable
1973 * @return mixed Variable
1975 function ifset(&$var) {
1984 * Returns number of subscriber to an event
1987 * @return number of subscriber(s)
1989 function numberOfEventSubscriber($event) {
1990 $query = 'SELECT COUNT(*) as count FROM ' . sql_table('plugin_event') . ' WHERE event=\'' . $event . '\'';
1991 $res = sql_query($query);
1992 $obj = mysql_fetch_object($res);