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.23 2007-04-09 23:21:34 kmorimatsu Exp $
17 * $NucleusJP: globalfunctions.php,v 1.22 2007/04/06 22:08:39 kmorimatsu Exp $
20 // needed if we include globalfunctions from install.php
21 global $nucleus, $CONF, $DIR_LIBS, $DIR_LANG, $manager, $member;
23 //$nucleus['version'] = 'v3.3SVN';
24 //$nucleus['codename'] = 'Lithium';
25 $nucleus['version'] = 'v3.3';
26 $nucleus['codename'] = '';
28 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'));
32 error_reporting(E_ALL); // report all errors!
34 error_reporting(E_ERROR | E_WARNING | E_PARSE);
38 if (!isset($CONF['Self'])) {
39 $CONF['Self'] = $_SERVER['PHP_SELF'];
43 Indicates when Nucleus should display startup errors. Set to 1 if you want
44 the error enabled (default), false otherwise
47 Displays an error when visiting a public Nucleus page and headers have
48 been sent out to early. This usually indicates an error in either a
49 configuration file or a language file, and could cause Nucleus to
52 Displays an error only when visiting the admin area, and when one or
53 more of the installation files (install.php, install.sql, upgrades/
54 directory) are still on the server.
56 $CONF['alertOnHeadersSent'] = 1;
57 $CONF['alertOnSecurityRisk'] = 1;
58 $CONF['ItemURL'] = $CONF['Self'];
59 $CONF['ArchiveURL'] = $CONF['Self'];
60 $CONF['ArchiveListURL'] = $CONF['Self'];
61 $CONF['MemberURL'] = $CONF['Self'];
62 $CONF['SearchURL'] = $CONF['Self'];
63 $CONF['BlogURL'] = $CONF['Self'];
64 $CONF['CategoryURL'] = $CONF['Self'];
66 // switch URLMode back to normal when $CONF['Self'] ends in .php
67 // this avoids urls like index.php/item/13/index.php/item/15
68 if (!isset($CONF['URLMode']) || (($CONF['URLMode'] == 'pathinfo') && (substr($CONF['Self'], strlen($CONF['Self']) - 4) == '.php'))) {
69 $CONF['URLMode'] = 'normal';
72 if (getNucleusPatchLevel() > 0) {
73 $nucleus['version'] .= '/' . getNucleusPatchLevel();
77 if (!isset($CONF['installscript'])) {
78 $CONF['installscript'] = 0;
81 // we will use postVar, getVar, ... methods instead of HTTP_GET_VARS or _GET
82 if ($CONF['installscript'] != 1) { // vars were already included in install.php
83 if (phpversion() >= '4.1.0') {
84 include_once($DIR_LIBS . 'vars4.1.0.php');
86 include_once($DIR_LIBS . 'vars4.0.6.php');
91 $bLoggingSanitizedResult=0;
92 $bSanitizeAndContinue=0;
94 $orgRequestURI = serverVar('REQUEST_URI');
97 // get all variables that can come from the request and put them in the global scope
98 $blogid = requestVar('blogid');
99 $itemid = intRequestVar('itemid');
100 $catid = intRequestVar('catid');
101 $skinid = requestVar('skinid');
102 $memberid = requestVar('memberid');
103 $archivelist = requestVar('archivelist');
104 $imagepopup = requestVar('imagepopup');
105 $archive = requestVar('archive');
106 $query = requestVar('query');
107 $highlight = requestVar('highlight');
108 $amount = requestVar('amount');
109 $action = requestVar('action');
110 $nextaction = requestVar('nextaction');
111 $maxresults = requestVar('maxresults');
112 $startpos = intRequestVar('startpos');
115 $virtualpath = ((getVar('virtualpath') != null) ? getVar('virtualpath') : serverVar('PATH_INFO'));
117 if (!headers_sent() ) {
118 header('Generator: Nucleus CMS ' . $nucleus['version']);
121 // include core classes that are needed for login & plugin handling
122 include($DIR_LIBS . 'mysql.php');
123 include($DIR_LIBS . 'MEMBER.php');
124 include($DIR_LIBS . 'ACTIONLOG.php');
125 include($DIR_LIBS . 'MANAGER.php');
126 include($DIR_LIBS . 'PLUGIN.php');
128 $manager =& MANAGER::instance();
130 // make sure there's no unnecessary escaping:
131 set_magic_quotes_runtime(0);
134 if (!isset($CONF['UsingAdminArea'])) {
135 $CONF['UsingAdminArea'] = 0;
138 // only needed when updating logs
139 if ($CONF['UsingAdminArea']) {
140 include($DIR_LIBS . 'xmlrpc.inc.php'); // XML-RPC client classes
141 include_once($DIR_LIBS . 'ADMIN.php');
144 // connect to database
148 // logs sanitized result if need
149 if ($orgRequestURI!==serverVar('REQUEST_URI')) {
150 $msg = "Sanitized [" . serverVar('REMOTE_ADDR') . "] ";
151 $msg .= $orgRequestURI . " -> " . serverVar('REQUEST_URI');
152 if ($bLoggingSanitizedResult) {
153 addToLog(WARNING, $msg);
155 if (!$bSanitizeAndContinue) {
160 // makes sure database connection gets closed on script termination
161 register_shutdown_function('sql_disconnect');
166 // automatically use simpler toolbar for mozilla
167 if (($CONF['DisableJsTools'] == 0) && strstr(serverVar('HTTP_USER_AGENT'), 'Mozilla/5.0') && strstr(serverVar('HTTP_USER_AGENT'), 'Gecko') ) {
168 $CONF['DisableJsTools'] = 2;
171 // login if cookies set
172 $member = new MEMBER();
174 // secure cookie key settings (either 'none', 0, 8, 16, 24, or 32)
175 if (!isset($CONF['secureCookieKey'])) $CONF['secureCookieKey']=24;
176 switch($CONF['secureCookieKey']){
178 $CONF['secureCookieKeyIP']=preg_replace('/\.[0-9]+\.[0-9]+\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
181 $CONF['secureCookieKeyIP']=preg_replace('/\.[0-9]+\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
184 $CONF['secureCookieKeyIP']=preg_replace('/\.[0-9]+$/','',serverVar('REMOTE_ADDR'));
187 $CONF['secureCookieKeyIP']=serverVar('REMOTE_ADDR');
190 $CONF['secureCookieKeyIP']='';
193 // login/logout when required or renew cookies
194 if ($action == 'login') {
195 // Form Authentication
196 $login = postVar('login');
197 $pw = postVar('password');
198 $shared = intPostVar('shared'); // shared computer or not
200 $pw=substr($pw,0,40); // avoid md5 collision by using a long key
202 if ($member->login($login, $pw) ) {
204 $member->newCookieKey();
205 $member->setCookies($shared);
207 if ($CONF['secureCookieKey']!=='none') {
209 $member->setCookieKey(md5($member->getCookieKey().$CONF['secureCookieKeyIP']));
213 // allows direct access to parts of the admin area after logging in
215 $action = $nextaction;
218 $manager->notify('LoginSuccess', array('member' => &$member) );
220 ACTIONLOG::add(INFO, "Login successful for $login (sharedpc=$shared)");
222 // errormessage for [%errordiv%]
223 $errormessage = 'Login failed for ' . $login;
225 $manager->notify('LoginFailed', array('username' => $login) );
226 ACTIONLOG::add(INFO, $errormessage);
230 Backed out for now: See http://forum.nucleuscms.org/viewtopic.php?t=3684 for details
232 } elseif (serverVar('PHP_AUTH_USER') && serverVar('PHP_AUTH_PW')) {
233 // HTTP Authentication
234 $login = serverVar('PHP_AUTH_USER');
235 $pw = serverVar('PHP_AUTH_PW');
237 if ($member->login($login, $pw) ) {
238 $manager->notify('LoginSuccess',array('member' => &$member));
239 ACTIONLOG::add(INFO, "HTTP authentication successful for $login");
241 $manager->notify('LoginFailed',array('username' => $login));
242 ACTIONLOG::add(INFO, 'HTTP authentication failed for ' . $login);
244 //Since bad credentials, generate an apropriate error page
245 header("WWW-Authenticate: Basic realm=\"Nucleus CMS {$nucleus['version']}\"");
246 header('HTTP/1.0 401 Unauthorized');
247 echo 'Invalid username or password';
252 } elseif (($action == 'logout') && (!headers_sent() ) && cookieVar($CONF['CookiePrefix'] . 'user') ) {
253 // remove cookies on logout
254 setcookie($CONF['CookiePrefix'] . 'user', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
255 setcookie($CONF['CookiePrefix'] . 'loginkey', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
256 $manager->notify('Logout', array('username' => cookieVar($CONF['CookiePrefix'] . 'user') ) );
257 } elseif (cookieVar($CONF['CookiePrefix'] . 'user') ) {
258 // Cookie Authentication
259 $ck=cookieVar($CONF['CookiePrefix'] . 'loginkey');
261 $ck=substr($ck,0,32); // avoid md5 collision by using a long key
262 if ($CONF['secureCookieKey']!=='none') $ck=md5($ck.$CONF['secureCookieKeyIP']);
263 $res = $member->cookielogin(cookieVar($CONF['CookiePrefix'] . 'user'), $ck );
266 // renew cookies when not on a shared computer
267 if ($res && (cookieVar($CONF['CookiePrefix'] . 'sharedpc') != 1) && (!headers_sent() ) ) {
268 $member->setCookieKey(cookieVar($CONF['CookiePrefix'] . 'loginkey'));
269 $member->setCookies();
274 $manager->notify('PostAuthentication', array('loggedIn' => $member->isLoggedIn() ) );
277 // first, let's see if the site is disabled or not. always allow admin area access.
278 if ($CONF['DisableSite'] && !$member->isAdmin() && !$CONF['UsingAdminArea']) {
279 redirect($CONF['DisableSiteURL']);
283 // load other classes
284 include($DIR_LIBS . 'PARSER.php');
285 include($DIR_LIBS . 'SKIN.php');
286 include($DIR_LIBS . 'TEMPLATE.php');
287 include($DIR_LIBS . 'BLOG.php');
288 include($DIR_LIBS . 'BODYACTIONS.php');
289 include($DIR_LIBS . 'COMMENTS.php');
290 include($DIR_LIBS . 'COMMENT.php');
291 //include($DIR_LIBS . 'ITEM.php');
292 include($DIR_LIBS . 'NOTIFICATION.php');
293 include($DIR_LIBS . 'BAN.php');
294 include($DIR_LIBS . 'PAGEFACTORY.php');
295 include($DIR_LIBS . 'SEARCH.php');
296 include($DIR_LIBS . 'entity.php');
299 // set lastVisit cookie (if allowed)
300 if (!headers_sent() ) {
301 if ($CONF['LastVisit']) {
302 setcookie($CONF['CookiePrefix'] . 'lastVisit', time(), time() + 2592000, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
304 setcookie($CONF['CookiePrefix'] . 'lastVisit', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
308 // read language file, only after user has been initialized
309 $language = getLanguageName();
310 include($DIR_LANG . ereg_replace( '[\\|/]', '', $language) . '.php');
313 Backed out for now: See http://forum.nucleuscms.org/viewtopic.php?t=3684 for details
315 // To remove after v2.5 is released and language files have been updated.
316 // Including this makes sure that language files for v2.5beta can still be used for v2.5final
317 // without having weird _SETTINGS_EXTAUTH string showing up in the admin area.
318 if (!defined('_MEMBERS_BYPASS'))
320 define('_SETTINGS_EXTAUTH', 'Enable External Authentication');
321 define('_WARNING_EXTAUTH', 'Warning: Enable only if needed.');
322 define('_MEMBERS_BYPASS', 'Use External Authentication');
327 // make sure the archivetype skinvar keeps working when _ARCHIVETYPE_XXX not defined
328 if (!defined('_ARCHIVETYPE_MONTH') ) {
329 define('_ARCHIVETYPE_DAY', 'day');
330 define('_ARCHIVETYPE_MONTH', 'month');
334 if ($CONF['URLMode'] == 'pathinfo') {
335 // initialize keywords if this hasn't been done before
336 if ($CONF['ItemKey'] == '') {
337 $CONF['ItemKey'] = 'item';
340 if ($CONF['ArchiveKey'] == '') {
341 $CONF['ArchiveKey'] = 'archive';
344 if ($CONF['ArchivesKey'] == '') {
345 $CONF['ArchivesKey'] = 'archives';
348 if ($CONF['MemberKey'] == '') {
349 $CONF['MemberKey'] = 'member';
352 if ($CONF['BlogKey'] == '') {
353 $CONF['BlogKey'] = 'blog';
356 if ($CONF['CategoryKey'] == '') {
357 $CONF['CategoryKey'] = 'category';
360 if ($CONF['SpecialskinKey'] == '') {
361 $CONF['SpecialskinKey'] = 'special';
368 'type' => basename(serverVar('SCRIPT_NAME') ), // e.g. item, blog, ...
369 'info' => $virtualpath,
370 'complete' => &$parsed
375 // default implementation
376 $data = explode("/", $virtualpath );
377 for ($i = 0; $i < sizeof($data); $i++) {
379 case $CONF['ItemKey']: // item/1 (blogid)
382 if ($i < sizeof($data) ) {
383 $itemid = intval($data[$i]);
387 case $CONF['ArchivesKey']: // archives/1 (blogid)
390 if ($i < sizeof($data) ) {
391 $archivelist = intval($data[$i]);
395 case $CONF['ArchiveKey']: // two possibilities: archive/yyyy-mm or archive/1/yyyy-mm (with blogid)
396 if ((($i + 1) < sizeof($data) ) && (!strstr($data[$i + 1], '-') ) ) {
397 $blogid = intval($data[++$i]);
402 if ($i < sizeof($data) ) {
403 $archive = $data[$i];
407 case 'blogid': // blogid/1
408 case $CONF['BlogKey']: // blog/1
411 if ($i < sizeof($data) ) {
412 $blogid = intval($data[$i]);
416 case $CONF['CategoryKey']: // category/1 (catid)
420 if ($i < sizeof($data) ) {
421 $catid = intval($data[$i]);
425 case $CONF['MemberKey']:
428 if ($i < sizeof($data) ) {
429 $memberid = intval($data[$i]);
433 case $CONF['SpecialskinKey']:
436 if ($i < sizeof($data) ) {
437 $_REQUEST['special'] = $data[$i];
448 function intPostVar($name) {
449 return intval(postVar($name) );
452 function intGetVar($name) {
453 return intval(getVar($name) );
456 function intRequestVar($name) {
457 return intval(requestVar($name) );
460 function intCookieVar($name) {
461 return intval(cookieVar($name) );
465 * returns the currently used version (100 = 1.00, 101 = 1.01, etc...)
467 function getNucleusVersion() {
472 * power users can install patches in between nucleus releases. These patches
473 * usually add new functionality in the plugin API and allow those to
474 * be tested without having to install CVS.
476 function getNucleusPatchLevel() {
481 * Connects to mysql server
483 function sql_connect() {
484 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
486 $MYSQL_CONN = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('<p>Could not connect to MySQL database.</p>', 'Connect Error');
487 mysql_select_db($MYSQL_DATABASE) or startUpError('<p>Could not select database: ' . mysql_error() . '</p>', 'Connect Error');
493 * returns a prefixed nucleus table name
495 function sql_table($name) {
496 global $MYSQL_PREFIX;
499 return $MYSQL_PREFIX . 'nucleus_' . $name;
501 return 'nucleus_' . $name;
505 function sendContentType($contenttype, $pagetype = '', $charset = _CHARSET) {
506 global $manager, $CONF;
508 if (!headers_sent() ) {
509 // if content type is application/xhtml+xml, only send it to browsers
510 // that can handle it (IE6 cannot). Otherwise, send text/html
512 // v2.5: For admin area pages, keep sending text/html (unless it's a debug version)
513 // application/xhtml+xml still causes too much problems with the javascript implementations
515 // v3.3: ($CONF['UsingAdminArea'] && !$CONF['debug']) gets removed,
516 // application/xhtml+xml seems to be working, so we're going to use it if we can.
518 // Note: reverted the following function in JP version
523 ($contenttype == 'application/xhtml+xml')
524 && (!stristr(serverVar('HTTP_ACCEPT'), 'application/xhtml+xml') )
526 $contenttype = 'text/html';
531 ($contenttype == 'application/xhtml+xml')
532 && (($CONF['UsingAdminArea'] && !$CONF['debug']) || !stristr(serverVar('HTTP_ACCEPT'),'application/xhtml+xml'))
535 $contenttype = 'text/html';
539 'PreSendContentType',
541 'contentType' => &$contenttype,
542 'charset' => &$charset,
543 'pageType' => $pagetype
547 // strip strange characters
548 $contenttype = preg_replace('|[^a-z0-9-+./]|i', '', $contenttype);
549 $charset = preg_replace('|[^a-z0-9-_]|i', '', $charset);
551 if ($charset != '') {
552 header('Content-Type: ' . $contenttype . '; charset=' . $charset);
554 header('Content-Type: ' . $contenttype);
560 * Errors before the database connection has been made
562 function startUpError($msg, $title) {
564 <html xmlns="http://www.w3.org/1999/xhtml">
565 <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
566 <title><?php echo htmlspecialchars($title)?></title></head>
568 <h1><?php echo htmlspecialchars($title)?></h1>
576 * disconnects from SQL server
578 function sql_disconnect() {
583 * executes an SQL query
585 function sql_query($query) {
588 $res = mysql_query($query) or print("mySQL error with query $query: " . mysql_error() . '<p />');
594 * Highlights a specific query in a given HTML text (not within HTML tags) and returns it
597 * text to be highlighted
599 * regular expression to be matched (can be an array of expressions as well)
601 * highlight to be used (use \\0 to indicate the matched expression)
604 function highlight($text, $expression, $highlight) {
605 if (!$highlight || !$expression) {
609 if (is_array($expression) && (count($expression) == 0) ) {
613 // add a tag in front (is needed for preg_match_all to work correct)
614 $text = '<!--h-->' . $text;
616 // split the HTML up so we have HTML tags
617 // $matches[0][i] = HTML + text
618 // $matches[1][i] = HTML
619 // $matches[2][i] = text
620 preg_match_all('/(<[^>]+>)([^<>]*)/', $text, $matches);
622 // throw it all together again while applying the highlight to the text pieces
624 for ($i = 0; $i < sizeof($matches[2]); $i++) {
626 $result .= $matches[1][$i];
629 if (is_array($expression) ) {
630 foreach ($expression as $regex) {
632 $matches[2][$i] = @eregi_replace($regex, $highlight, $matches[2][$i]);
636 $result .= $matches[2][$i];
638 $result .= @eregi_replace($expression, $highlight, $matches[2][$i]);
646 * Parses a query into an array of expressions that can be passed on to the highlight method
648 function parseHighlight($query) {
649 // TODO: add more intelligent splitting logic
652 $query = preg_replace('/\'|"/', '', $query);
658 $aHighlight = explode(' ', $query);
660 for ($i = 0; $i < count($aHighlight); $i++) {
661 $aHighlight[$i] = trim($aHighlight[$i]);
663 if (strlen($aHighlight[$i]) < 3) {
664 unset($aHighlight[$i]);
668 if (count($aHighlight) == 1) {
669 return $aHighlight[0];
676 * Checks if email address is valid
678 function isValidMailAddress($address) {
679 if (preg_match('/^[a-zA-Z+0-9\._-]+@[a-zA-Z0-9\._-]+\.[A-Za-z]{2,5}$/', $address)) {
687 // some helper functions
688 function getBlogIDFromName($name) {
689 return quickQuery('SELECT bnumber as result FROM ' . sql_table('blog') . ' WHERE bshortname="' . addslashes($name) . '"');
692 function getBlogNameFromID($id) {
693 return quickQuery('SELECT bname as result FROM ' . sql_table('blog') . ' WHERE bnumber=' . intval($id) );
696 function getBlogIDFromItemID($itemid) {
697 return quickQuery('SELECT iblog as result FROM ' . sql_table('item') . ' WHERE inumber=' . intval($itemid) );
700 function getBlogIDFromCommentID($commentid) {
701 return quickQuery('SELECT cblog as result FROM ' . sql_table('comment') . ' WHERE cnumber=' . intval($commentid) );
704 function getBlogIDFromCatID($catid) {
705 return quickQuery('SELECT cblog as result FROM ' . sql_table('category') . ' WHERE catid=' . intval($catid) );
708 function getCatIDFromName($name) {
709 return quickQuery('SELECT catid as result FROM ' . sql_table('category') . ' WHERE cname="' . addslashes($name) . '"');
712 function quickQuery($q) {
713 $res = sql_query($q);
714 $obj = mysql_fetch_object($res);
718 function getPluginNameFromPid($pid) {
719 $res = sql_query('SELECT pfile FROM ' . sql_table('plugin') . ' WHERE pid=' . intval($pid) );
720 $obj = mysql_fetch_object($res);
724 function selector() {
725 global $itemid, $blogid, $memberid, $query, $amount, $archivelist, $maxresults;
726 global $archive, $skinid, $blog, $memberinfo, $CONF, $member;
727 global $imagepopup, $catid;
730 $actionNames = array('addcomment', 'sendmessage', 'createaccount', 'forgotpassword', 'votepositive', 'votenegative', 'plugin');
731 $action = requestVar('action');
733 if (in_array($action, $actionNames) ) {
734 global $DIR_LIBS, $errormessage;
735 include_once($DIR_LIBS . 'ACTION.php');
737 $errorInfo = $a->doAction($action);
740 $errormessage = $errorInfo['message'];
744 // show error when headers already sent out
745 if (headers_sent() && $CONF['alertOnHeadersSent']) {
747 // try to get line number/filename (extra headers_sent params only exists in PHP 4.3+)
748 if (function_exists('version_compare') && version_compare('4.3.0', phpversion(), '<=') ) {
749 headers_sent($hsFile, $hsLine);
750 $extraInfo = ' in <code>' . $hsFile . '</code> line <code>' . $hsLine . '</code>';
756 '<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>',
757 'Page headers already sent'
762 // make is so ?archivelist without blogname or blogid shows the archivelist
763 // for the default weblog
764 if (serverVar('QUERY_STRING') == 'archivelist') {
765 $archivelist = $CONF['DefaultBlog'];
768 // now decide which type of skin we need
770 // itemid given -> only show that item
773 if (!$manager->existsItem($itemid,0,0) ) {
774 doError(_ERROR_NOSUCHITEM);
777 global $itemidprev, $itemidnext, $catid, $itemtitlenext, $itemtitleprev;
779 // 1. get timestamp, blogid and catid for item
780 $query = 'SELECT itime, iblog, icat FROM ' . sql_table('item') . ' WHERE inumber=' . intval($itemid);
781 $res = sql_query($query);
782 $obj = mysql_fetch_object($res);
784 // if a different blog id has been set through the request or selectBlog(),
786 // if ($blogid && (intval($blogid) != $obj->iblog) ) {
787 // doError(_ERROR_NOSUCHITEM);
789 if ($blogid && (intval($blogid) != $obj->iblog)) {
790 if (!headers_sent()) {
791 $b =& $manager->getBlog($obj->iblog);
792 $CONF['ItemURL'] = $b->getURL();
793 if ($CONF['URLMode'] == 'pathinfo' and substr($CONF['ItemURL'],-1) == '/')
794 $CONF['ItemURL'] = substr($CONF['ItemURL'], 0, -1);
795 $correctURL = createItemLink($itemid, '');
796 redirect($correctURL);
799 doError(_ERROR_NOSUCHITEM);
803 // if a category has been selected which doesn't match the item, ignore the
805 if (($catid != 0) && ($catid != $obj->icat) ) {
809 $blogid = $obj->iblog;
810 $timestamp = strtotime($obj->itime);
812 $b =& $manager->getBlog($blogid);
814 if ($b->isValidCategory($catid) ) {
815 $catextra = ' and icat=' . $catid;
818 // get previous itemid and title
819 $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';
820 $res = sql_query($query);
822 $obj = mysql_fetch_object($res);
825 $itemidprev = $obj->inumber;
826 $itemtitleprev = $obj->ititle;
829 // get next itemid and title
830 $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';
831 $res = sql_query($query);
833 $obj = mysql_fetch_object($res);
836 $itemidnext = $obj->inumber;
837 $itemtitlenext = $obj->ititle;
840 } elseif ($archive) {
844 // get next and prev month links ...
845 global $archivenext, $archiveprev, $archivetype, $archivenextexists, $archiveprevexists;
847 // sql queries for the timestamp of the first and the last published item
848 $query = "SELECT UNIX_TIMESTAMP(itime) as result FROM ".sql_table('item')." WHERE idraft=0 ORDER BY itime ASC";
849 $first_timestamp=quickQuery ($query);
850 $query = "SELECT UNIX_TIMESTAMP(itime) as result FROM ".sql_table('item')." WHERE idraft=0 ORDER BY itime DESC";
851 $last_timestamp=quickQuery ($query);
853 sscanf($archive, '%d-%d-%d', $y, $m, $d);
856 $archivetype = _ARCHIVETYPE_DAY;
857 $t = mktime(0, 0, 0, $m, $d, $y);
859 $archiveprev = strftime('%Y-%m-%d', $t - (24 * 60 * 60) );
860 // check for published items
861 if ($t > $first_timestamp) {
862 $archiveprevexists = true;
865 $archiveprevexists = false;
870 // $archivenext = strftime('%Y-%m-%d', $t);
871 $archivenext = strftime('%Y-%m-%d', $t + (24 * 60 * 60) );
872 if ($t + (24 * 60 * 60) < $last_timestamp) {
873 $archivenextexists = true;
876 $archivenextexists = false;
880 $archivetype = _ARCHIVETYPE_MONTH;
881 $t = mktime(0, 0, 0, $m, 1, $y);
883 $archiveprev = strftime('%Y-%m', $t - (1 * 24 * 60 * 60) );
884 if ($t > $first_timestamp) {
885 $archiveprevexists = true;
888 $archiveprevexists = false;
891 // $archivenext = strftime('%Y-%m', $t + (32 * 24 * 60 * 60) );
892 // timestamp for the next month
893 $t = mktime(0, 0, 0, $m+1, 1, $y);
894 $archivenext = strftime('%Y-%m', $t);
895 if ($t < $last_timestamp) {
896 $archivenextexists = true;
899 $archivenextexists = false;
903 } elseif ($archivelist) {
904 $type = 'archivelist';
906 if (is_numeric($archivelist)) {
907 $blogid = intVal($archivelist);
909 $blogid = getBlogIDFromName($archivelist);
913 doError(_ERROR_NOSUCHBLOG);
919 $query = stripslashes($query);
920 if(preg_match("/^(\xA1{2}|\xe3\x80{2}|\x20)+$/",$query)){
923 $order = (_CHARSET == 'EUC-JP') ? 'EUC-JP, UTF-8,' : 'UTF-8, EUC-JP,';
924 $query = mb_convert_encoding($query, _CHARSET, $order.' JIS, SJIS, ASCII');
925 if (is_numeric($blogid)) {
926 $blogid = intVal($blogid);
928 $blogid = getBlogIDFromName($blogid);
932 doError(_ERROR_NOSUCHBLOG);
935 } elseif ($memberid) {
938 if (!MEMBER::existsID($memberid) ) {
939 doError(_ERROR_NOSUCHMEMBER);
942 $memberinfo = $manager->getMember($memberid);
944 } elseif ($imagepopup) {
945 // media object (images etc.)
946 $type = 'imagepopup';
948 // TODO: check if media-object exists
949 // TODO: set some vars?
951 // show regular index page
956 // decide which blog should be displayed
958 $blogid = $CONF['DefaultBlog'];
961 $b =& $manager->getBlog($blogid);
962 $blog = $b; // references can't be placed in global variables?
964 if (!$blog->isValid) {
965 doError(_ERROR_NOSUCHBLOG);
968 // set catid if necessary
970 $blog->setSelectedCategory($catid);
973 // decide which skin should be used
974 if ($skinid != '' && ($skinid == 0) ) {
979 $skinid = $blog->getDefaultSkin();
982 $special = requestVar('special');
983 if (!empty($special) && isValidShortName($special)) {
984 $type = strtolower($special);
987 $skin = new SKIN($skinid);
989 if (!$skin->isValid) {
990 doError(_ERROR_NOSUCHSKIN);
998 * Show error skin with given message. An optional skin-object to use can be given
1000 function doError($msg, $skin = '') {
1001 global $errormessage, $CONF, $skinid, $blogid, $manager;
1005 if (SKIN::existsID($skinid) ) {
1006 $skin = new SKIN($skinid);
1007 } elseif ($manager->existsBlogID($blogid) ) {
1008 $blog =& $manager->getBlog($blogid);
1009 $skin = new SKIN($blog->getDefaultSkin() );
1010 } elseif ($CONF['DefaultBlog']) {
1011 $blog =& $manager->getBlog($CONF['DefaultBlog']);
1012 $skin = new SKIN($blog->getDefaultSkin() );
1014 // this statement should actually never be executed
1015 $skin = new SKIN($CONF['BaseSkin']);
1020 $errormessage = $msg;
1021 $skin->parse('error');
1025 function getConfig() {
1028 $query = 'SELECT * FROM ' . sql_table('config');
1029 $res = sql_query($query);
1031 while ($obj = mysql_fetch_object($res) ) {
1032 $CONF[$obj->name] = $obj->value;
1036 // some checks for names of blogs, categories, templates, members, ...
1037 function isValidShortName($name) {
1038 return eregi('^[a-z0-9]+$', $name);
1041 function isValidDisplayName($name) {
1042 return eregi('^[a-z0-9]+[a-z0-9 ]*[a-z0-9]+$', $name);
1045 function isValidCategoryName($name) {
1049 function isValidTemplateName($name) {
1050 return eregi('^[a-z0-9/]+$', $name);
1053 function isValidSkinName($name) {
1054 return eregi('^[a-z0-9/]+$', $name);
1057 // add and remove linebreaks
1058 function addBreaks($var) {
1062 function removeBreaks($var) {
1063 return preg_replace("/<br \/>([\r\n])/", "$1", $var);
1066 // shortens a text string to maxlength ($toadd) is what needs to be added
1067 // at the end (end length is <= $maxlength)
1068 function shorten($text, $maxlength, $toadd) {
1069 // 1. remove entities...
1070 // $trans = get_html_translation_table(HTML_ENTITIES);
1071 $trans = get_html_translation_table(HTML_SPECIALCHARS); // for Japanese
1072 $trans = array_flip($trans);
1073 $text = strtr($text, $trans);
1075 // 2. the actual shortening
1076 if (strlen($text) > $maxlength)
1077 $text = mb_strimwidth($text, 0, $maxlength, $toadd, _CHARSET);
1082 * Converts a unix timestamp to a mysql DATETIME format, and places
1085 function mysqldate($timestamp) {
1086 return '"' . date('Y-m-d H:i:s', $timestamp) . '"';
1090 * functions for use in index.php
1092 function selectBlog($shortname) {
1093 global $blogid, $archivelist;
1094 $blogid = getBlogIDFromName($shortname);
1096 // also force archivelist variable, if it is set
1098 $archivelist = $blogid;
1102 function selectSkin($skinname) {
1104 $skinid = SKIN::getIdFromName($skinname);
1108 * Can take either a category ID or a category name (be aware that
1109 * multiple categories can have the same name)
1111 function selectCategory($cat) {
1113 if (is_numeric($cat) ) {
1114 $catid = intval($cat);
1116 $catid = getCatIDFromName($cat);
1120 function selectItem($id) {
1122 $itemid = intval($id);
1125 // force the use of a language file (warning: can cause warnings)
1126 function selectLanguage($language) {
1128 include($DIR_LANG . ereg_replace( '[\\|/]', '', $language) . '.php');
1131 function parseFile($filename, $includeMode = 'normal', $includePrefix = '') {
1132 $handler = new ACTIONS('fileparser');
1133 $parser = new PARSER(SKIN::getAllowedActionsForType('fileparser'), $handler);
1134 $handler->parser =& $parser;
1136 // set IncludeMode properties of parser
1137 PARSER::setProperty('IncludeMode', $includeMode);
1138 PARSER::setProperty('IncludePrefix', $includePrefix);
1140 if (!file_exists($filename) ) {
1141 doError('A file is missing');
1144 $fsize = filesize($filename);
1151 $fd = fopen ($filename, 'r');
1152 $contents = fread ($fd, $fsize);
1155 // parse file contents
1156 $parser->parse($contents);
1160 * Outputs a debug message
1162 function debug($msg) {
1163 echo '<p><b>' . $msg . "</b></p>\n";
1167 function addToLog($level, $msg) {
1168 ACTIONLOG::add($level, $msg);
1171 // shows a link to help file
1172 function help($id) {
1176 function helpHtml($id) {
1177 return helplink($id) . '<img src="documentation/icon-help.gif" width="15" height="15" alt="' . _HELP_TT . '" /></a>';
1180 function helplink($id) {
1181 return '<a href="documentation/help.html#'. $id . '" onclick="if (event && event.preventDefault) event.preventDefault(); return help(this.href);">';
1184 function getMailFooter() {
1185 $message = "\n\n-----------------------------";
1186 $message .= "\n Powered by Nucleus CMS";
1187 $message .= "\n(http://www.nucleuscms.org/)";
1192 * Returns the name of the language to use
1193 * preference priority: member - site
1194 * defaults to english when no good language found
1196 * checks if file exists, etc...
1198 function getLanguageName() {
1199 global $CONF, $member;
1201 if ($member && $member->isLoggedIn() ) {
1202 // try to use members language
1203 $memlang = $member->getLanguage();
1205 if (($memlang != '') && (checkLanguage($memlang) ) ) {
1210 // use default language
1211 if (checkLanguage($CONF['Language']) ) {
1212 return $CONF['Language'];
1219 * Includes a PHP file. This method can be called while parsing templates and skins
1221 function includephp($filename) {
1222 // make predefined variables global, so most simple scripts can be used here
1224 // apache (names taken from PHP doc)
1225 global $GATEWAY_INTERFACE, $SERVER_NAME, $SERVER_SOFTWARE, $SERVER_PROTOCOL;
1226 global $REQUEST_METHOD, $QUERY_STRING, $DOCUMENT_ROOT, $HTTP_ACCEPT;
1227 global $HTTP_ACCEPT_CHARSET, $HTTP_ACCEPT_ENCODING, $HTTP_ACCEPT_LANGUAGE;
1228 global $HTTP_CONNECTION, $HTTP_HOST, $HTTP_REFERER, $HTTP_USER_AGENT;
1229 global $REMOTE_ADDR, $REMOTE_PORT, $SCRIPT_FILENAME, $SERVER_ADMIN;
1230 global $SERVER_PORT, $SERVER_SIGNATURE, $PATH_TRANSLATED, $SCRIPT_NAME;
1231 global $REQUEST_URI;
1233 // php (taken from PHP doc)
1234 global $argv, $argc, $PHP_SELF, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS;
1235 global $HTTP_POST_FILES, $HTTP_ENV_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;
1238 global $PATH_INFO, $HTTPS, $HTTP_RAW_POST_DATA, $HTTP_X_FORWARDED_FOR;
1240 if (@file_exists($filename) ) {
1246 * Checks if a certain language/plugin exists
1248 function checkLanguage($lang) {
1250 return file_exists($DIR_LANG . ereg_replace( '[\\|/]', '', $lang) . '.php');
1253 function checkPlugin($plug) {
1254 global $DIR_PLUGINS;
1255 return file_exists($DIR_PLUGINS . ereg_replace( '[\\|/]', '', $plug) . '.php');
1259 * Centralisation of the functions that generate links
1261 function createItemLink($itemid, $extra = '') {
1262 return createLink('item', array('itemid' => $itemid, 'extra' => $extra) );
1265 function createMemberLink($memberid, $extra = '') {
1266 return createLink('member', array('memberid' => $memberid, 'extra' => $extra) );
1269 function createCategoryLink($catid, $extra = '') {
1270 return createLink('category', array('catid' => $catid, 'extra' => $extra) );
1273 function createArchiveListLink($blogid = '', $extra = '') {
1274 return createLink('archivelist', array('blogid' => $blogid, 'extra' => $extra) );
1277 function createArchiveLink($blogid, $archive, $extra = '') {
1278 return createLink('archive', array('blogid' => $blogid, 'archive' => $archive, 'extra' => $extra) );
1281 function createBlogidLink($blogid, $params = '') {
1282 return createLink('blog', array('blogid' => $blogid, 'extra' => $params) );
1285 function createLink($type, $params) {
1286 global $manager, $CONF;
1289 $usePathInfo = ($CONF['URLMode'] == 'pathinfo');
1291 // ask plugins first
1299 'params' => $params,
1300 'completed' => &$created,
1306 // if a plugin created the URL, return it
1311 // default implementation
1315 $url = $CONF['ItemURL'] . '/' . $CONF['ItemKey'] . '/' . $params['itemid'];
1317 $url = $CONF['ItemURL'] . '?itemid=' . $params['itemid'];
1323 $url = $CONF['MemberURL'] . '/' . $CONF['MemberKey'] . '/' . $params['memberid'];
1325 $url = $CONF['MemberURL'] . '?memberid=' . $params['memberid'];
1331 $url = $CONF['CategoryURL'] . '/' . $CONF['CategoryKey'] . '/' . $params['catid'];
1333 $url = $CONF['CategoryURL'] . '?catid=' . $params['catid'];
1338 if (!$params['blogid']) {
1339 $params['blogid'] = $CONF['DefaultBlog'];
1343 $url = $CONF['ArchiveListURL'] . '/' . $CONF['ArchivesKey'] . '/' . $params['blogid'];
1345 $url = $CONF['ArchiveListURL'] . '?archivelist=' . $params['blogid'];
1351 $url = $CONF['ArchiveURL'] . '/' . $CONF['ArchiveKey'] . '/'.$params['blogid'].'/' . $params['archive'];
1353 $url = $CONF['ArchiveURL'] . '?blogid='.$params['blogid'].'&archive=' . $params['archive'];
1359 $url = $CONF['BlogURL'] . '/' . $CONF['BlogKey'] . '/' . $params['blogid'];
1361 $url = $CONF['BlogURL'] . '?blogid=' . $params['blogid'];
1366 return addLinkParams($url, (isset($params['extra'])? $params['extra'] : null));
1369 function createBlogLink($url, $params) {
1371 if ($CONF['URLMode'] == 'normal') {
1372 if (strpos($url, '?') === FALSE && is_array($params)) {
1373 $fParam = reset($params);
1374 $fKey = key($params);
1375 array_shift($params);
1376 $url .= '?' . $fKey . '=' . $fParam;
1378 } elseif ($CONF['URLMode'] == 'pathinfo' && substr($url, -1) == '/') {
1379 $url = substr($url, 0, -1);
1381 return addLinkParams($url, $params);
1384 function addLinkParams($link, $params) {
1387 if (is_array($params) ) {
1389 if ($CONF['URLMode'] == 'pathinfo') {
1391 foreach ($params as $param => $value) {
1392 $link .= '/' . $param . '/' . urlencode($value);
1397 foreach ($params as $param => $value) {
1398 $link .= '&' . $param . '=' . urlencode($value);
1409 * querystring to alter (e.g. foo=1&bar=2&x=y)
1411 * name of parameter to change (e.g. 'foo')
1413 * New value for that parameter (e.g. 3)
1415 * altered query string (for the examples above: foo=3&bar=2&x=y)
1417 function alterQueryStr($querystr, $param, $value) {
1418 $vars = explode('&', $querystr);
1421 for ($i = 0; $i < count($vars); $i++) {
1422 $v = explode('=', $vars[$i]);
1424 if ($v[0] == $param) {
1426 $vars[$i] = implode('=', $v);
1433 $vars[] = $param . '=' . $value;
1436 return ltrim(implode('&', $vars), '&');
1439 // passes one variable as hidden input field (multiple fields for arrays)
1440 // @see passRequestVars in varsx.x.x.php
1441 function passVar($key, $value) {
1443 if (is_array($value) ) {
1444 for ($i = 0; $i < sizeof($value); $i++) {
1445 passVar($key . '[' . $i . ']', $value[$i]);
1451 // other values: do stripslashes if needed
1452 ?><input type="hidden" name="<?php echo htmlspecialchars($key)?>" value="<?php echo htmlspecialchars(undoMagic($value) )?>" /><?php
1456 Date format functions (to be used from [%date(..)%] skinvars
1458 function formatDate($format, $timestamp, $defaultFormat, &$blog) {
1459 // apply blog offset (#42)
1460 $boffset = $blog ? $blog->getTimeOffset() * 3600 : 0;
1461 $offset = date('Z', $timestamp) + $boffset;
1472 $tz .= sprintf("%02d%02d", floor($offset / 3600), round(($offset % 3600) / 60) );
1473 return date('D, j M Y H:i:s ', $timestamp) . $tz;
1476 $timestamp -= $offset;
1477 return date('D, j M Y H:i:s ', $timestamp) . 'GMT';
1480 $timestamp -= $offset;
1481 return date('Y-m-d\TH:i:s\Z', $timestamp);
1491 $tz .= sprintf("%02d:%02d", floor($offset / 3600), round(($offset % 3600) / 60) );
1492 return date('Y-m-d\TH:i:s', $timestamp) . $tz;
1495 return strftime($format ? $format : $defaultFormat, $timestamp);
1499 function checkVars($aVars) {
1500 global $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_SESSION_VARS;
1502 foreach ($aVars as $varName) {
1504 if (phpversion() >= '4.1.0') {
1506 if ( isset($_GET[$varName])
1507 || isset($_POST[$varName])
1508 || isset($_COOKIE[$varName])
1509 || isset($_ENV[$varName])
1510 || isset($_SESSION[$varName])
1511 || isset($_FILES[$varName])
1513 die('Sorry. An error occurred.');
1518 if ( isset($HTTP_GET_VARS[$varName])
1519 || isset($HTTP_POST_VARS[$varName])
1520 || isset($HTTP_COOKIE_VARS[$varName])
1521 || isset($HTTP_ENV_VARS[$varName])
1522 || isset($HTTP_SESSION_VARS[$varName])
1523 || isset($HTTP_POST_FILES[$varName])
1525 die('Sorry. An error occurred.');
1534 * Sanitize parameters such as $_GET and $_SERVER['REQUEST_URI'] etc.
1537 function sanitizeParams()
1539 global $HTTP_SERVER_VARS;
1545 // REQUEST_URI of $HTTP_SERVER_VARS
1546 $str =& $HTTP_SERVER_VARS["REQUEST_URI"];
1547 serverStringToArray($str, $array, $frontParam);
1548 sanitizeArray($array);
1549 arrayToServerString($array, $frontParam, $str);
1551 // QUERY_STRING of $HTTP_SERVER_VARS
1552 $str =& $HTTP_SERVER_VARS["QUERY_STRING"];
1553 serverStringToArray($str, $array, $frontParam);
1554 sanitizeArray($array);
1555 arrayToServerString($array, $frontParam, $str);
1557 if (phpversion() >= '4.1.0') {
1558 // REQUEST_URI of $_SERVER
1559 $str =& $_SERVER["REQUEST_URI"];
1560 serverStringToArray($str, $array, $frontParam);
1561 sanitizeArray($array);
1562 arrayToServerString($array, $frontParam, $str);
1564 // QUERY_STRING of $_SERVER
1565 $str =& $_SERVER["QUERY_STRING"];
1566 serverStringToArray($str, $array, $frontParam);
1567 sanitizeArray($array);
1568 arrayToServerString($array, $frontParam, $str);
1572 convArrayForSanitizing($_GET, $array);
1573 sanitizeArray($array);
1574 revertArrayForSanitizing($array, $_GET);
1576 // $_REQUEST (only GET param)
1577 convArrayForSanitizing($_REQUEST, $array);
1578 sanitizeArray($array);
1579 revertArrayForSanitizing($array, $_REQUEST);
1583 * Check ticket when not checked in plugin's admin page
1585 * Also avoid the access to plugin/index.php by guest user.
1587 function ticketForPlugin(){
1588 global $CONF,$DIR_PLUGINS,$member,$ticketforplugin;
1591 $ticketforplugin=array();
1592 $ticketforplugin['ticket']=false;
1594 /* Check if using plugin's php file. */
1595 if ($p_translated=serverVar('PATH_TRANSLATED')) {
1596 if (!file_exists($p_translated)) $p_translated='';
1598 if (!$p_translated) {
1599 $p_translated=serverVar('SCRIPT_FILENAME');
1600 if (!file_exists($p_translated)) {
1601 header("HTTP/1.0 404 Not Found");
1605 $p_translated=str_replace('\\','/',$p_translated);
1606 $d_plugins=str_replace('\\','/',$DIR_PLUGINS);
1607 if (strpos($p_translated,$d_plugins)!==0) return;// This isn't plugin php file.
1609 /* Solve the plugin php file or admin directory */
1610 $phppath=substr($p_translated,strlen($d_plugins));
1611 $phppath=preg_replace('!^/!','',$phppath);// Remove the first "/" if exists.
1612 $path=preg_replace('/^NP_(.*)\.php$/','$1',$phppath); // Remove the first "NP_" and the last ".php" if exists.
1613 $path=preg_replace('!^([^/]*)/(.*)$!','$1',$path); // Remove the "/" and beyond.
1615 /* Solve the plugin name. */
1617 $query='SELECT pfile FROM '.sql_table('plugin');
1618 $res=sql_query($query);
1619 while($row=mysql_fetch_row($res)) {
1620 $name=substr($row[0],3);
1621 $plugins[strtolower($name)]=$name;
1623 mysql_free_result($res);
1624 if ($plugins[$path]) $plugin_name=$plugins[$path];
1625 else if (in_array($path,$plugins)) $plugin_name=$path;
1627 header("HTTP/1.0 404 Not Found");
1631 /* Return if not index.php */
1632 if ( $phppath!=strtolower($plugin_name).'/'
1633 && $phppath!=strtolower($plugin_name).'/index.php' ) return;
1635 /* Exit if not logged in. */
1636 if ( !$member->isLoggedIn() ) exit("You aren't logged in.");
1638 global $manager,$DIR_LIBS,$DIR_LANG,$HTTP_GET_VARS,$HTTP_POST_VARS;
1640 /* Check if this feature is needed (ie, if "$manager->checkTicket()" is not included in the script). */
1641 if (!($p_translated=serverVar('PATH_TRANSLATED'))) $p_translated=serverVar('SCRIPT_FILENAME');
1642 if ($file=@file($p_translated)) {
1644 foreach($file as $line) {
1645 if (preg_match('/[\$]manager([\s]*)[\-]>([\s]*)checkTicket([\s]*)[\(]/i',$prevline.$line)) return;
1650 /* Show a form if not valid ticket */
1651 if ( ( strstr(serverVar('REQUEST_URI'),'?') || serverVar('QUERY_STRING')
1652 || strtoupper(serverVar('REQUEST_METHOD'))=='POST' )
1653 && (!$manager->checkTicket()) ){
1655 if (!class_exists('PluginAdmin')) {
1656 $language = getLanguageName();
1657 include($DIR_LANG . ereg_replace( '[\\|/]', '', $language) . '.php');
1658 include($DIR_LIBS . 'PLUGINADMIN.php');
1660 if (!(function_exists('mb_strimwidth') || extension_loaded('mbstring'))) {
1661 if (file_exists($DIR_LIBS.'mb_emulator/mb-emulator.php')) {
1662 global $mbemu_internals;
1663 include_once($DIR_LIBS.'mb_emulator/mb-emulator.php');
1666 $oPluginAdmin = new PluginAdmin($plugin_name);
1667 $oPluginAdmin->start();
1668 echo '<p>' . _ERROR_BADTICKET . "</p>\n";
1670 /* Show the form to confirm action */
1671 // PHP 4.0.x support
1672 $get= (isset($_GET)) ? $_GET : $HTTP_GET_VARS;
1673 $post= (isset($_POST)) ? $_POST : $HTTP_POST_VARS;
1674 // Resolve URI and QUERY_STRING
1675 if ($uri=serverVar('REQUEST_URI')) {
1676 list($uri,$qstring)=explode('?',$uri);
1678 if ( !($uri=serverVar('PHP_SELF')) ) $uri=serverVar('SCRIPT_NAME');
1679 $qstring=serverVar('QUERY_STRING');
1681 if ($qstring) $qstring='?'.$qstring;
1682 echo '<p>'._SETTINGS_UPDATE.' : '._QMENU_PLUGINS.' <span style="color:red;">'.
1683 htmlspecialchars($plugin_name)."</span> ?</p>\n";
1684 switch(strtoupper(serverVar('REQUEST_METHOD'))){
1686 echo '<form method="POST" action="'.htmlspecialchars($uri.$qstring).'">';
1687 $manager->addTicketHidden();
1688 _addInputTags($post);
1691 echo '<form method="GET" action="'.htmlspecialchars($uri).'">';
1692 $manager->addTicketHidden();
1693 _addInputTags($get);
1697 echo '<input type="submit" value="'._YES.'" /> ';
1698 echo '<input type="button" value="'._NO.'" onclick="history.back(); return false;" />';
1701 $oPluginAdmin->end();
1705 /* Create new ticket */
1706 $ticket=$manager->addTicketToUrl('');
1707 $ticketforplugin['ticket']=substr($ticket,strpos($ticket,'ticket=')+7);
1709 function _addInputTags(&$keys,$prefix=''){
1710 foreach($keys as $key=>$value){
1711 if ($prefix) $key=$prefix.'['.$key.']';
1712 if (is_array($value)) _addInputTags($value,$key);
1714 if (get_magic_quotes_gpc()) $value=stripslashes($value);
1715 if ($key=='ticket') continue;
1716 echo '<input type="hidden" name="'.htmlspecialchars($key).
1717 '" value="'.htmlspecialchars($value).'" />'."\n";
1723 * Convert the server string such as $_SERVER['REQUEST_URI']
1724 * to arry like arry['blogid']=1 and array['page']=2 etc.
1726 function serverStringToArray($str, &$array, &$frontParam)
1732 // split front param, e.g. /index.php, and others, e.g. blogid=1&page=2
1733 if (strstr($str, "?")){
1734 list($frontParam, $args) = preg_split("/\?/", $str, 2);
1741 // If there is no args like blogid=1&page=2, return
1742 if (!strstr($str, "=") && !strlen($frontParam)) {
1747 $array = explode("&", $args);
1751 * Convert array like array['blogid'] to server string
1752 * such as $_SERVER['REQUEST_URI']
1754 function arrayToServerString($array, $frontParam, &$str)
1756 if (strstr($str, "?")) {
1757 $str = $frontParam . "?";
1761 if (count($array)) {
1762 $str .= implode("&", $array);
1767 * Sanitize array parameters.
1768 * This function checks both key and value.
1769 * - check key if it inclues " (double quote), remove from array
1770 * - check value if it includes \ (escape sequece), remove remaining string
1772 function sanitizeArray(&$array)
1774 $excludeListForSanitization = array('query');
1775 // $excludeListForSanitization = array();
1777 foreach ($array as $k => $v) {
1779 // split to key and value
1780 list($key, $val) = preg_split("/=/", $v, 2);
1785 // when magic quotes is on, need to use stripslashes,
1786 // and then addslashes
1787 if (get_magic_quotes_gpc()) {
1788 $val = stripslashes($val);
1790 $val = addslashes($val);
1792 // if $key is included in exclude list, skip this param
1793 if (!in_array($key, $excludeListForSanitization)) {
1796 list($val, $tmp) = explode('\\', $val);
1798 // remove control code etc.
1799 $val = strtr($val, "\0\r\n<>'\"", " ");
1802 if (preg_match('/\"/i', $key)) {
1807 // set sanitized info
1808 $array[$k] = sprintf("%s=%s", $key, $val);
1814 * Convert array for sanitizeArray function
1816 function convArrayForSanitizing($src, &$array)
1819 foreach ($src as $key => $val) {
1820 if (key_exists($key, $_GET)) {
1821 array_push($array, sprintf("%s=%s", $key, $val));
1827 * Revert array after sanitizeArray function
1829 function revertArrayForSanitizing($array, &$dst)
1831 foreach ($array as $v) {
1832 list($key, $val) = preg_split("/=/", $v, 2);
1838 * Stops processing the request and redirects to the given URL.
1839 * - no actual contents should have been sent to the output yet
1840 * - the URL will be stripped of illegal or dangerous characters
1842 function redirect($url) {
1843 $url = preg_replace('|[^a-z0-9-~+_.?#=&;,/:@%]|i', '', $url);
1844 header('Location: ' . $url);
1849 * Strip HTML tags from a string
1850 * This function is a bit more intelligent than a regular call to strip_tags(),
1851 * because it also deletes the contents of certain tags and cleans up any
1852 * unneeded whitespace.
1854 function stringStripTags ($string) {
1855 $string = preg_replace("/<del[^>]*>.+<\/del[^>]*>/isU", '', $string);
1856 $string = preg_replace("/<script[^>]*>.+<\/script[^>]*>/isU", '', $string);
1857 $string = preg_replace("/<style[^>]*>.+<\/style[^>]*>/isU", '', $string);
1858 $string = str_replace('>', '> ', $string);
1859 $string = str_replace('<', ' <', $string);
1860 $string = strip_tags($string);
1861 $string = preg_replace("/\s+/", " ", $string);
1862 $string = trim($string);
1867 * Make a string containing HTML safe for use in a HTML attribute
1868 * Tags are stripped and entities are normalized
1870 function stringToAttribute ($string) {
1871 $string = stringStripTags($string);
1872 $string = entity::named_to_numeric($string);
1873 $string = entity::normalize_numeric($string);
1875 if (_CHARSET == 'UTF-8') {
1876 $string = entity::numeric_to_utf8($string);
1879 $string = entity::specialchars($string, 'html');
1880 $string = entity::numeric_to_named($string);
1885 * Make a string containing HTML safe for use in a XML document
1886 * Tags are stripped, entities are normalized and named entities are
1887 * converted to numeric entities.
1889 function stringToXML ($string) {
1890 $string = stringStripTags($string);
1891 $string = entity::named_to_numeric($string);
1892 $string = entity::normalize_numeric($string);
1894 if (_CHARSET == 'UTF-8') {
1895 $string = entity::numeric_to_utf8($string);
1898 $string = entity::specialchars($string, 'xml');
1902 // START: functions from the end of file BLOG.php
1903 // used for mail notification (html -> text)
1904 function toAscii($html) {
1905 // strip off most tags
1906 $html = strip_tags($html,'<a>');
1907 $to_replace = "/<a[^>]*href=[\"\']([^\"^']*)[\"\'][^>]*>([^<]*)<\/a>/i";
1909 $ascii = preg_replace_callback ($to_replace, '_links_add', $html);
1910 $ascii .= "\n\n" . _links_list();
1911 return strip_tags($ascii);
1914 function _links_init() {
1916 $tmp_links = array();
1919 function _links_add($match) {
1921 array_push($tmp_links, $match[1]);
1922 return $match[2] . ' [' . sizeof($tmp_links) .']';
1925 function _links_list() {
1929 foreach ($tmp_links as $current) {
1930 $output .= "[$i] $current\n";
1935 // END: functions from the end of file BLOG.php
1937 // START: functions from the end of file ADMIN.php
1939 * @todo document this
1941 function encode_desc(&$data)
1942 { //_$to_entities = get_html_translation_table(HTML_ENTITIES);
1943 $to_entities = get_html_translation_table(HTML_SPECIALCHARS);
1944 $from_entities = array_flip($to_entities);
1945 $data = str_replace('<br />','\n',$data); //hack
1946 $data = strtr($data,$from_entities);
1947 $data = strtr($data,$to_entities);
1948 $data = str_replace('\n','<br />',$data); //hack
1953 * Returns the Javascript code for a bookmarklet that works on most modern browsers
1957 function getBookmarklet($blogid) {
1961 $document = 'document';
1962 $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('";
1963 $bookmarkletline .= $CONF['AdminURL'] . "bookmarklet.php?blogid=$blogid";
1964 $bookmarkletline .="&logtext='+escape(Q)+'&loglink='+escape(x.location.href)+'&loglinktitle='+escape(x.title),'nucleusbm','scrollbars=yes,width=600,height=500,left=10,top=10,status=yes,resizable=yes');wingm.focus();";
1966 return $bookmarkletline;
1968 // END: functions from the end of file ADMIN.php
1971 * Returns a variable or null if not set
1973 * @param mixed Variable
1974 * @return mixed Variable
1976 function ifset(&$var) {