OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / globalfunctions.php
index 2c443f6..e6448a8 100644 (file)
+<<<<<<< HEAD
+<?php\r
+\r
+/*\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
+ * Copyright (C) 2002-2009 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ */\r
+/**\r
+ * @license http://nucleuscms.org/license.txt GNU General Public License\r
+ * @copyright Copyright (C) 2002-2009 The Nucleus Group\r
+ * @version $Id: globalfunctions.php 1856 2012-05-18 09:30:16Z sakamocchi $
+ */\r
+\r
+/* needed if we include globalfunctions from install.php */\r
+global $nucleus, $CONF, $DIR_LIBS, $DIR_LOCALES, $manager, $member, $MYSQL_HANDLER, $StartTime;
+
+/* just for benchmark tag */
+$StartTime = microtime(TRUE);
+\r
+$nucleus['version'] = 'v4.00 SVN';\r
+$nucleus['codename'] = '';\r
+\r
+/*\r
+ * make sure there's no unnecessary escaping:\r
+ * set_magic_quotes_runtime(0);\r
+ */\r
+if ( version_compare(PHP_VERSION, '5.3.0', '<') )\r
+{\r
+       ini_set('magic_quotes_runtime', '0');\r
+}\r
+\r
+/* check and die if someone is trying to override internal globals (when register_globals turn on) */\r
+checkVars(array('nucleus', 'CONF', 'DIR_LIBS',
+'MYSQL_HOST', 'MYSQL_USER', 'MYSQL_PASSWORD', 'MYSQL_DATABASE',
+'DIR_LOCALES', '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'));
+\r
+if ( !isset($CONF) )\r
+{\r
+       $CONF = array();\r
+}\r
+\r
+/* debug mode */\r
+if ( array_key_exists('debug', $CONF) && $CONF['debug'] )\r
+{\r
+       /* report all errors! */\r
+       error_reporting(E_ALL);\r
+}\r
+else\r
+{\r
+       ini_set('display_errors','0');\r
+       error_reporting(E_ERROR | E_WARNING | E_PARSE);\r
+}\r
+\r
+/*\r
+ * alertOnHeadersSent\r
+ *  Displays an error when visiting a public Nucleus page and headers have\r
+ *  been sent out to early. This usually indicates an error in either a\r
+ *  configuration file or a translation file, and could cause Nucleus to\r
+ *  malfunction\r
+ */\r
+if ( !array_key_exists('alertOnHeadersSent', $CONF) )\r
+{\r
+       $CONF['alertOnHeadersSent'] = 1;\r
+}\r
+\r
+/*\r
+ * alertOnSecurityRisk\r
+ * Displays an error only when visiting the admin area, and when one or\r
+ *  more of the installation files (install.php, install.sql, upgrades/\r
+ *  directory) are still on the server.\r
+ */\r
+if ( !array_key_exists('alertOnSecurityRisk', $CONF) )\r
+{\r
+       $CONF['alertOnSecurityRisk'] = 1;\r
+}\r
+\r
+/*\r
+ * Set these to 1 to allow viewing of future items or draft items\r
+ * Should really never do this, but can be useful for some plugins that might need to\r
+ * Could cause some other issues if you use future posts otr drafts\r
+ * So use with care\r
+ */\r
+$CONF['allowDrafts'] = 0;\r
+$CONF['allowFuture'] = 0;\r
+\r
+if ( getNucleusPatchLevel() > 0 )\r
+{\r
+       $nucleus['version'] .= '/' . getNucleusPatchLevel();\r
+}\r
+\r
+/* Avoid notices */\r
+if ( !array_key_exists('installscript', $CONF) || empty($CONF['installscript']) )\r
+{\r
+       $CONF['installscript'] = 0;\r
+}\r
+if ( !array_key_exists('UsingAdminArea', $CONF) )\r
+{\r
+       $CONF['UsingAdminArea'] = 0;\r
+}\r
+\r
+if ( !headers_sent() )\r
+{\r
+       header('Generator: Nucleus CMS ' . $nucleus['version']);\r
+}\r
+\r
+\r
+/* TODO: This is for compatibility since 4.0, should be obsoleted at future release. */\r
+if ( !isset($DIR_LOCALES) )\r
+{\r
+       $DIR_LOCALES = $DIR_NUCLEUS . 'locales/';\r
+}\r
+global $DIR_LANG;\r
+if ( !isset($DIR_LANG) )\r
+{\r
+       $DIR_LANG = $DIR_LOCALES;\r
+}\r
+\r
+/* load and initialize i18n class */\r
+if (!class_exists('i18n', FALSE))\r
+{\r
+       include($DIR_LIBS . 'i18n.php');\r
+}\r
+if ( !i18n::init('UTF-8', $DIR_LOCALES) )\r
+{\r
+       exit('Fail to initialize i18n class.');\r
+}\r
+\r
+/* TODO: This is just for compatibility since 4.0, should be obsoleted at future release. */\r
+define('_CHARSET', i18n::get_current_charset());\r
+\r
+\r
+/*\r
+ * NOTE: Since 4.0 release, Entity class becomes to be important class\r
+ *  with some wrapper functions for htmlspechalchars/htmlentity PHP's built-in function\r
+ */\r
+include($DIR_LIBS . 'ENTITY.php');\r
+\r
+/* we will use postVar, getVar, ... methods instead of $_GET, $_POST ... */\r
+if ( $CONF['installscript'] != 1 )\r
+{\r
+       /* vars were already included in install.php */\r
+       include_once($DIR_LIBS . 'vars4.1.0.php');\r
+       \r
+       /* added for 4.0 DB::* wrapper and compatibility sql_* */\r
+       include_once($DIR_LIBS . 'sql/sql.php');\r
+}\r
+\r
+/* include core classes that are needed for login & plugin handling */\r
+include($DIR_LIBS . 'MEMBER.php');\r
+include($DIR_LIBS . 'ACTIONLOG.php');\r
+include($DIR_LIBS . 'MANAGER.php');\r
+include($DIR_LIBS . 'PLUGIN.php');\r
+\r
+$manager =& MANAGER::instance();\r
+\r
+/* only needed when updating logs */\r
+if ( $CONF['UsingAdminArea'] )\r
+{\r
+       /* XML-RPC client classes */\r
+       include($DIR_LIBS . 'xmlrpc.inc.php');\r
+       include($DIR_LIBS . 'ADMIN.php');\r
+}\r
+\r
+\r
+/* connect to database */\r
+if ( !isset($MYSQL_HANDLER) )\r
+{\r
+       $MYSQL_HANDLER = array('mysql','');\r
+}\r
+if ( $MYSQL_HANDLER[0] == '' )\r
+{\r
+       $MYSQL_HANDLER[0] = 'mysql';\r
+}\r
+DB::setConnectionInfo($MYSQL_HANDLER[1], $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE);\r
+\r
+\r
+/* force locale or charset */\r
+$locale = '';\r
+$charset = i18n::get_current_charset();\r
+\r
+$data = array(\r
+       'locale'        => &$locale,\r
+       'charset'       => &$charset\r
+);\r
+$manager->notify('ForceLocale', $data);\r
+\r
+if ( $data['locale'] !== '' )\r
+{\r
+       i18n::set_forced_locale($data['locale']);\r
+}\r
+if ( $data['charset'] !== '' )\r
+{\r
+       i18n::set_forced_charset($data['charset']);\r
+}\r
+unset($locale);\r
+unset($charset);\r
+\r
+\r
+/* convert forced charset to current charset */\r
+if ( i18n::get_forced_charset() != i18n::get_current_charset() )\r
+{\r
+       $_POST          = i18n::convert_array($_POST, i18n::get_forced_charset());\r
+       $_GET           = i18n::convert_array($_GET, i18n::get_forced_charset());\r
+       $_REQUEST       = i18n::convert_array($_REQUEST, i18n::get_forced_charset());\r
+       $_COOKIE        = i18n::convert_array($_COOKIE, i18n::get_forced_charset());\r
+       $_FILES         = i18n::convert_array($_FILES, i18n::get_forced_charset());\r
+       \r
+       if ( session_id() !== '' )\r
+       {\r
+               $_SESSION = i18n::convert_array($_SESSION, i18n::get_forced_charset());\r
+       }\r
+}\r
+\r
+\r
+/* sanitize option */\r
+$bLoggingSanitizedResult = 0;\r
+$bSanitizeAndContinue = 0;\r
+$orgRequestURI = serverVar('REQUEST_URI');\r
+sanitizeParams();\r
+\r
+/* logs sanitized result if need */\r
+if ( $orgRequestURI !== serverVar('REQUEST_URI') )\r
+{\r
+       $msg = "Sanitized [" . serverVar('REMOTE_ADDR') . "] ";\r
+       $msg .= $orgRequestURI . " -> " . serverVar('REQUEST_URI');\r
+       if ( $bLoggingSanitizedResult )\r
+       {\r
+               addToLog(WARNING, $msg);\r
+       }\r
+       if ( !$bSanitizeAndContinue )\r
+       {\r
+               die("");\r
+       }\r
+}\r
+\r
+/* get all variables that can come from the request and put them in the global scope */\r
+$blogid                = requestVar('blogid');\r
+$itemid                = intRequestVar('itemid');\r
+$catid         = intRequestVar('catid');\r
+$skinid                = requestVar('skinid');\r
+$memberid      = requestVar('memberid');\r
+$archivelist = requestVar('archivelist');\r
+$imagepopup    = requestVar('imagepopup');\r
+$archive       = requestVar('archive');\r
+$query         = requestVar('query');\r
+$highlight     = requestVar('highlight');\r
+$amount                = requestVar('amount');\r
+$action                = requestVar('action');\r
+$nextaction    = requestVar('nextaction');\r
+$maxresults    = requestVar('maxresults');\r
+$startpos      = intRequestVar('startpos');\r
+$errormessage = '';\r
+$error         = '';\r
+$special       = requestVar('special');\r
+\r
+\r
+/* read config */\r
+getConfig();\r
+\r
+\r
+/* Properly set $CONF['Self'] and others if it's not set...\r
+ * usually when we are access from admin menu\r
+ */\r
+if ( !array_key_exists('Self', $CONF) )\r
+{\r
+       $CONF['Self'] = $CONF['IndexURL'];\r
+       /* strip trailing */\r
+       if ( $CONF['Self'][i18n::strlen($CONF['Self']) -1] == "/" )\r
+       {\r
+               $CONF['Self'] = i18n::substr($CONF['Self'], 0, i18n::strlen($CONF['Self']) -1);\r
+       }\r
+}\r
+\r
+$CONF['ItemURL']               = $CONF['Self'];\r
+$CONF['ArchiveURL']            = $CONF['Self'];\r
+$CONF['ArchiveListURL']        = $CONF['Self'];\r
+$CONF['MemberURL']             = $CONF['Self'];\r
+$CONF['SearchURL']             = $CONF['Self'];\r
+$CONF['BlogURL']               = $CONF['Self'];\r
+$CONF['CategoryURL']   = $CONF['Self'];\r
+\r
+/* automatically use simpler toolbar for mozilla */\r
+if ( ($CONF['DisableJsTools'] == 0)\r
+   && i18n::strpos(serverVar('HTTP_USER_AGENT'), 'Mozilla/5.0') !== FALSE\r
+   && i18n::strpos(serverVar('HTTP_USER_AGENT'), 'Gecko') !== FALSE )\r
+{\r
+       $CONF['DisableJsTools'] = 2;\r
+}\r
+\r
+/* login processing */
+$member = new Member();\r
+\r
+if ( $action == 'login' )\r
+{\r
+       $login = postVar('login');\r
+       $password = postVar('password');\r
+       $shared = intPostVar('shared');\r
+       $member->login($login, $password, $shared);\r
+}\r
+elseif ( ($action == 'logout') )\r
+{\r
+       $member->logout();\r
+}\r
+else\r
+{\r
+       $member->cookielogin();\r
+}\r
+\r
+\r
+/* TODO: This is for backward compatibility, should be obsoleted near future. */\r
+if ( !preg_match('#^(.+)_(.+)_(.+)$#', $CONF['Locale'])\r
+  && ($CONF['Locale'] = i18n::convert_old_language_file_name_to_locale($CONF['Locale'])) === FALSE )\r
+{\r
+       $CONF['Locale'] = 'en_Latn_US';\r
+}\r
+if ( !array_key_exists('Language', $CONF) )\r
+{\r
+       $CONF['Language'] = i18n::convert_locale_to_old_language_file_name($CONF['Locale']);\r
+}\r
+$locale = $CONF['Locale'];\r
+\r
+\r
+/* NOTE: include translation file and set locale */\r
+if ( $member->isLoggedIn() )\r
+{\r
+       if ( $member->getLocale() )\r
+       {\r
+               $locale = $member->getLocale();\r
+       }\r
+}\r
+else\r
+{\r
+       if ( i18n::get_forced_locale() !== '' )\r
+       {\r
+               $locale = i18n::get_forced_locale();\r
+       }\r
+}\r
+include_translation($locale);\r
+i18n::set_current_locale($locale);\r
+\r
+\r
+/* login completed */\r
+$manager->notify('PostAuthentication', array('loggedIn' => $member->isLoggedIn() ) );\r
+\r
+/* next action */\r
+if ( $member->isLoggedIn() && $nextaction )\r
+{\r
+       $action = $nextaction;\r
+}\r
+\r
+/* first, let's see if the site is disabled or not. always allow admin area access. */\r
+if ( $CONF['DisableSite'] && !$member->isAdmin() && !$CONF['UsingAdminArea'] )\r
+{\r
+       redirect($CONF['DisableSiteURL']);\r
+       exit;\r
+}\r
+\r
+/* load other classes */\r
+include($DIR_LIBS . 'PARSER.php');\r
+include($DIR_LIBS . 'SKIN.php');\r
+include($DIR_LIBS . 'TEMPLATE.php');\r
+include($DIR_LIBS . 'BLOG.php');\r
+include($DIR_LIBS . 'BODYACTIONS.php');\r
+include($DIR_LIBS . 'COMMENTS.php');\r
+include($DIR_LIBS . 'COMMENT.php');\r
+include($DIR_LIBS . 'NOTIFICATION.php');\r
+include($DIR_LIBS . 'BAN.php');\r
+include($DIR_LIBS . 'PAGEFACTORY.php');\r
+include($DIR_LIBS . 'SEARCH.php');\r
+include($DIR_LIBS . 'LINK.php');\r
+\r
+/* set lastVisit cookie (if allowed) */\r
+if ( !headers_sent() )\r
+{\r
+       if ( $CONF['LastVisit'] )\r
+       {\r
+               setcookie($CONF['CookiePrefix'] . 'lastVisit', time(), time() + 2592000, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);\r
+       }\r
+       else\r
+       {\r
+               setcookie($CONF['CookiePrefix'] . 'lastVisit', '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);\r
+       }\r
+}\r
+\r
+if ( !defined('_ARCHIVETYPE_MONTH') )\r
+{\r
+       define('_ARCHIVETYPE_DAY', 'day');\r
+       define('_ARCHIVETYPE_MONTH', 'month');\r
+       define('_ARCHIVETYPE_YEAR', 'year');\r
+}\r
+\r
+/* for path resolving */
+$virtualpath = getVar('virtualpath');
+if ( getVar('virtualpath') == '' )
+                                       {\r
+       $virtualpath = serverVar('PATH_INFO');
+                                       }\r
+                               \r
+/*
+ * switch URLMode back to normal when $CONF['Self'] ends in .php
+ * this avoids urls like index.php/item/13/index.php/item/15
+ */
+if ( !array_key_exists('URLMode', $CONF) || ($CONF['URLMode'] != 'pathinfo') )
+                                       {\r
+       $CONF['URLMode'] = 'normal';
+                                       }\r
+else
+                                       {\r
+       if ( i18n::substr($CONF['Self'], i18n::strlen($CONF['Self']) - 4) != '.php' )
+                                       {\r
+               decodePathInfo($virtualpath);
+       }\r
+}\r
+\r
+/*\r
+ * PostParseURL is a place to cleanup any of the path-related global variables before the selector function is run.\r
+ * It has 2 values in the data in case the original virtualpath is needed, but most the use will be in tweaking\r
+ * global variables to clean up (scrub out catid or add catid) or to set someother global variable based on\r
+ * the values of something like catid or itemid\r
+ * New in 3.60\r
+ */\r
+$data = array(\r
+       'type' => basename(serverVar('SCRIPT_NAME')),\r
+       'info' => $virtualpath\r
+);\r
+$manager->notify('PostParseURL', $data);\r
+\r
+/*\r
+ * NOTE: Here is the end of initialization\r
+ */\r
+\r
+/**\r
+ * include_libs()\r
+ * This function includes or requires the specified library file\r
+ * \r
+ * @param      string  $file\r
+ * @param      boolean $once use the _once() version\r
+ * @param      boolean $require use require() instead of include()\r
+ * @return     void\r
+ */\r
+function include_libs($file, $once = TRUE, $require = TRUE)\r
+{\r
+       global $DIR_LIBS;\r
+       \r
+       // $DIR_LIBS isn't a directory\r
+       if ( !is_dir($DIR_LIBS) )\r
+       {\r
+               exit;\r
+       }\r
+       \r
+       $lib_path = $DIR_LIBS . $file;\r
+       \r
+       if ( $once && $require )\r
+       {\r
+               require_once($lib_path);\r
+       }\r
+       else if ( $once && !$require )\r
+       {\r
+               include_once($lib_path);\r
+       }\r
+       else if ( $require )\r
+       {\r
+               require($lib_path);\r
+       }\r
+       else\r
+       {\r
+               include($lib_path);\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * include_plugins()\r
+ * This function includes or requires the specified plugin file\r
+ * \r
+ * @param      string  $file\r
+ * @param      boolean $once use the _once() version\r
+ * @param      boolean $require use require() instead of include()\r
+ * @return     \r
+ */\r
+function include_plugins($file, $once = TRUE, $require = TRUE)\r
+{\r
+       global $DIR_PLUGINS;\r
+       \r
+       // begin if: $DIR_LIBS isn't a directory\r
+       if ( !is_dir($DIR_PLUGINS) )\r
+       {\r
+               exit;\r
+       }\r
+       \r
+       $plugin_path = $DIR_PLUGINS . $file;\r
+       \r
+       // begin if: \r
+       if ( $once && $require )\r
+       {\r
+               require_once($plugin_path);\r
+       }\r
+       else if ( $once && !$require )\r
+       {\r
+               include_once($plugin_path);\r
+       }\r
+       elseif ( $require )\r
+       {\r
+               require($plugin_path);\r
+       }\r
+       else\r
+       {\r
+               include($plugin_path);\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * include_translation()\r
+ * This function decide which locale is used and include translation\r
+ * \r
+ * @param      string  &$locale        locale name referring to 'language tags' defined in RFC 5646\r
+ * @return     void\r
+ */\r
+function include_translation(&$locale)\r
+{\r
+       global $DIR_LOCALES;\r
+       \r
+       $translation_file = $DIR_LOCALES . $locale . '.' . i18n::get_current_charset() . '.php';\r
+       if ( !file_exists($translation_file) )\r
+       {\r
+               $locale = 'en_Latn_US';\r
+               $translation_file = $DIR_LOCALES . 'en_Latn_US.ISO-8859-1.php';\r
+       }\r
+       include($translation_file);\r
+       return;\r
+}\r
+\r
+/**\r
+ * intPostVar()\r
+ * This function returns the integer value of $_POST for the variable $name\r
+ * \r
+ * @param      string  $name   field to get the integer value of\r
+ * @return     integer\r
+ */\r
+function intPostVar($name)\r
+{\r
+       return (integer) postVar($name);\r
+}\r
+\r
+\r
+/**\r
+ * intGetVar()\r
+ * This function returns the integer value of $_GET for the variable $name\r
+ * \r
+ * @param      string  $name   field to get the integer value of\r
+ * @return     integer\r
+ */\r
+function intGetVar($name)\r
+{\r
+       return (integer) getVar($name);\r
+}\r
+\r
+\r
+/**\r
+ * intRequestVar()\r
+ * This function returns the integer value of $_REQUEST for the variable $name. Also checks $_GET and $_POST if not found in $_REQUEST\r
+ * \r
+ * @param string $name field to get the integer value of\r
+ * @return int\r
+ */\r
+function intRequestVar($name)\r
+{\r
+       return (integer) requestVar($name);\r
+}\r
+\r
+\r
+/**\r
+ * intCookieVar()\r
+ * This function returns the integer value of $_COOKIE for the variable $name\r
+ * \r
+ * @param      string  $name   field to get the integer value of\r
+ * @return     integer\r
+ */\r
+function intCookieVar($name)\r
+{\r
+       return (integer) cookieVar($name);\r
+}\r
+\r
+/**\r
+ * getNucleusVersion()\r
+ * This function returns the current Nucleus version (100 = 1.00, 101 = 1.01, etc...)\r
+ * \r
+ * @param      void\r
+ * @return     integer\r
+ */\r
+function getNucleusVersion()\r
+{\r
+       return 400;\r
+}\r
+\r
+/**\r
+ * getNucleusPatchLevel()\r
+ * TODO: Better description of this function.\r
+ *\r
+ * Power users can install patches in between nucleus releases. These patches\r
+ * usually add new functionality in the plugin API and allow those to\r
+ * be tested without having to install CVS.\r
+ *\r
+ *@param       void\r
+ * @return     integer\r
+ */\r
+function getNucleusPatchLevel()\r
+{\r
+       return 0;\r
+}\r
+\r
+/**\r
+ * getLatestVersion()\r
+ * This function returns the latest Nucleus version available for download from nucleuscms.org or FALSE if unable to attain data\r
+ * Format will be major.minor/patachlevel e.g. 3.41 or 3.41/02\r
+ * \r
+ * @param      void\r
+ * @return     mixed\r
+ */\r
+function getLatestVersion()\r
+{\r
+       // begin if: cURL is not available in this PHP installation\r
+       if ( !function_exists('curl_init') )\r
+       {\r
+               return FALSE;\r
+       }\r
+       \r
+       $curl = curl_init();\r
+       $timeout = 5;\r
+       \r
+       curl_setopt ($curl, CURLOPT_URL, 'http://nucleuscms.org/version_check.php');\r
+       curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);\r
+       curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);\r
+       \r
+       $return = curl_exec($curl);\r
+       \r
+       curl_close($curl);\r
+       \r
+       return $return;\r
+}\r
+\r
+/**\r
+ * sql_table()\r
+ * This function returns a Nucleus table name with the appropriate prefix\r
+ * @param string $name\r
+ * @return string\r
+ */\r
+function sql_table($name)\r
+{\r
+       global $MYSQL_PREFIX;\r
+       \r
+       // begin if: no MySQL prefix\r
+       if ( empty($MYSQL_PREFIX) )\r
+       {\r
+               return 'nucleus_' . $name;\r
+       }\r
+       // else: use MySQL prefix\r
+       else\r
+       {\r
+               return $MYSQL_PREFIX . 'nucleus_' . $name;\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * sendContentType()\r
+ * This function sends the Content-Type header if headers have not already been sent\r
+ * It also determines if the browser can accept application/xhtml+xml and sends it only to those that can.\r
+ * \r
+ * if content type is application/xhtml+xml, only send it to browsers\r
+ * that can handle it (IE6 cannot). Otherwise, send text/html\r
+ *\r
+ * v2.5:\r
+ * For admin area pages, keep sending text/html (unless it's a debug version)\r
+ * application/xhtml+xml still causes too much problems with the javascript implementations\r
+ *\r
+ * v3.3:\r
+ * ($CONF['UsingAdminArea'] && !$CONF['debug']) gets removed,\r
+ * application/xhtml+xml seems to be working, so we're going to use it if we can.\r
+ * \r
+ * @param      string  $content_type   MIME media type registered to IANA, http://www.iana.org/assignments/media-types/index.html\r
+ * @param      string  $page_type              \r
+ * @param      string  $charset                Deprecated. This has no meaning.\r
+ * @return     void\r
+ * \r
+ */\r
+function sendContentType($content_type, $page_type = '', $charset = '')\r
+{\r
+       global $manager, $CONF;\r
+       \r
+       if ( headers_sent() )\r
+       {\r
+               return;\r
+       }\r
+       \r
+       /* NOTE: MIME Media Type */\r
+       if ( ($content_type == 'application/xhtml+xml')\r
+               && (!stristr(serverVar('HTTP_ACCEPT'), 'application/xhtml+xml') ) )\r
+       {\r
+               $content_type = 'text/html';\r
+       }\r
+       \r
+       /* NOTE: generate event */\r
+       $data = array(\r
+               'pageType'              =>  $page_type,\r
+               'contentType'   => &$content_type\r
+       );\r
+       $manager->notify('PreSendContentType', $data);\r
+       \r
+       /* NOTE: confirm MIME Media Type */\r
+       $content_type = preg_replace('#[^a-zA-Z0-9-+./]#', '', $content_type);\r
+       \r
+       /* NOTE: confirm character set */\r
+       $charset = i18n::get_current_charset();\r
+       if ( i18n::get_forced_charset() !== '' )\r
+       {\r
+               $charset = i18n::get_forced_charset();\r
+       }\r
+       \r
+       /* NOTE: send HTTP 1.1 header */\r
+       header("Content-Type: {$content_type}; charset={$charset}");\r
+       \r
+       /* NOTE: set handler for translating character set */\r
+       if ( $charset != i18n::get_current_charset() )\r
+       {\r
+               ob_start(array('i18n', 'convert_handler'));\r
+       }\r
+       \r
+       return;\r
+}\r
+\r
+/**\r
+ * parseHighlight()\r
+ * This function parses a query into an array of expressions that can be passed on to the highlight method\r
+ * @param      string  $query\r
+ * @return     void\r
+ */\r
+function parseHighlight($query)\r
+{\r
+       // TODO: add more intelligent splitting logic\r
+       \r
+       // get rid of quotes\r
+       $query = preg_replace('/\'|"/', '', $query);\r
+       \r
+       if ( !$query )\r
+       {\r
+               return array();\r
+       }\r
+       \r
+       $aHighlight = preg_split('# #', $query);\r
+       \r
+       for ( $i = 0; $i < count($aHighlight); $i++ )\r
+       {\r
+               $aHighlight[$i] = trim($aHighlight[$i]);\r
+               \r
+               if ( i18n::strlen($aHighlight[$i]) < 3 )\r
+               {\r
+                       unset($aHighlight[$i]);\r
+               }\r
+       }\r
+       \r
+       if ( count($aHighlight) == 1 )\r
+       {\r
+               return $aHighlight[0];\r
+       }\r
+       else\r
+       {\r
+               return $aHighlight;\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * getConfig()\r
+ * \r
+ * @param      void\r
+ * @return     void\r
+ */\r
+function getConfig()\r
+{\r
+       global $CONF;\r
+       \r
+       $query = sprintf('SELECT * FROM %s', sql_table('config'));\r
+       $res = DB::getResult($query);\r
+       \r
+       foreach ( $res as $row )\r
+       {\r
+               $CONF[$row['name']] = $row['value'];\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * This function gets the blog ID from the blog name\r
+ * @param string $name\r
+ * @return\r
+ */\r
+function getBlogIDFromName($name)\r
+{\r
+       $query = sprintf('SELECT bnumber AS result FROM %s WHERE bshortname=%s', sql_table('blog'), DB::quoteValue($name));\r
+       return DB::getValue($query);\r
+}\r
+\r
+/**\r
+ * This function gets the blog name from the blog ID\r
+ * @param int $id\r
+ * @return object\r
+ */\r
+function getBlogNameFromID($id)\r
+{\r
+       $query = sprintf('SELECT bname AS result FROM %s WHERE bnumber=%d', sql_table('blog'), intval($id));\r
+       return DB::getValue($query);\r
+}\r
+\r
+/**\r
+ * This function gets the blog ID from the item ID\r
+ * @param int $item_id\r
+ * @return object\r
+ */\r
+function getBlogIDFromItemID($item_id)\r
+{\r
+       $query = sprintf('SELECT iblog AS result FROM %s WHERE inumber=%d', sql_table('item'), intval($item_id));\r
+       return DB::getValue($query);\r
+}\r
+\r
+/**\r
+ * This function gets the blog ID from the comment ID\r
+ * @param int $comment_id\r
+ * @return object\r
+ */\r
+function getBlogIDFromCommentID($comment_id)\r
+{\r
+       $query = sprintf('SELECT cblog AS result FROM %s WHERE cnumber=%d', sql_table('comment'), intval($comment_id));\r
+       return DB::getValue($query);\r
+}\r
+\r
+/**\r
+ * This function gets the blog ID from the category ID\r
+ * @param int $category_id\r
+ * @return object\r
+ */\r
+function getBlogIDFromCatID($category_id)\r
+{\r
+       $query = sprintf('SELECT cblog AS result FROM %s WHERE catid=%d', sql_table('category'), intval($category_id));\r
+       return DB::getValue($query);\r
+}\r
+\r
+/**\r
+ * This function gets the category ID from the category name\r
+ * @param int $name\r
+ * @return object\r
+ */\r
+function getCatIDFromName($name)\r
+{\r
+       $query = sprintf('SELECT catid AS result FROM %s WHERE cname=%s', sql_table('category'), DB::quoteValue($name));\r
+       return DB::getValue($query);\r
+}\r
+\r
+\r
+/**\r
+ * functions to be used in index.php to select something\r
+ */\r
+function selectBlog($shortname)\r
+{\r
+       global $blogid, $archivelist;\r
+       $blogid = getBlogIDFromName($shortname);\r
+       \r
+       // also force archivelist variable, if it is set\r
+       if ( $archivelist )\r
+       {\r
+               $archivelist = $blogid;\r
+       }\r
+       return;\r
+}\r
+function selectSkin($skinname)\r
+{\r
+       global $skinid;\r
+       $skinid = SKIN::getIdFromName($skinname);\r
+       return;\r
+}\r
+function selectCategory($cat)\r
+{\r
+       global $catid;\r
+       if ( is_numeric($cat) )\r
+       {\r
+               $catid = (integer) $cat;\r
+       }\r
+       else\r
+       {\r
+               $catid = getCatIDFromName($cat);\r
+       }\r
+       return;\r
+}\r
+function selectItem($id)\r
+{\r
+       global $itemid;\r
+       $itemid = (integer) $id;\r
+       return;\r
+}\r
+function selectSpecialSkinType($id)\r
+{\r
+       global $special;\r
+       $special = strtolower($id);\r
+       return;\r
+}\r
+function selector()\r
+{\r
+       global $archive, $archivelist, $archivenext, $archivenextexists, $archiveprev, $archiveprevexists, $archivetype;\r
+       global $blog, $blogid;\r
+       global $catid;\r
+       global $itemid, $itemidnext, $itemidprev, $itemtitlenext, $itemtitleprev;\r
+       global $CONF, $DIR_LIBS, $amount, $errormessage, $imagepopup;\r
+       global $manager, $maxresults, $query;\r
+       global $member, $memberid, $memberinfo;\r
+       global $skinid, $skinpart, $special;\r
+       \r
+       $actionNames = array('addcomment', 'sendmessage', 'createaccount', 'forgotpassword', 'votepositive', 'votenegative', 'plugin');\r
+       $action = requestVar('action');\r
+       \r
+       if ( in_array($action, $actionNames) )\r
+       {\r
+               include_once($DIR_LIBS . 'ACTION.php');\r
+               $a = new Action();\r
+               $errorInfo = $a->doAction($action);\r
+               \r
+               if ( $errorInfo )\r
+               {\r
+                       $errormessage = $errorInfo['message'];\r
+               }\r
+       }\r
+       \r
+       // show error when headers already sent out\r
+       if ( headers_sent() && $CONF['alertOnHeadersSent'] )\r
+       {\r
+               // try to get line number/filename (extra headers_sent params only exists in PHP 4.3+)\r
+               if ( function_exists('version_compare') && version_compare('4.3.0', phpversion(), '<=') )\r
+               {\r
+                       headers_sent($hsFile, $hsLine);\r
+                       $extraInfo = ' in <code>' . $hsFile . '</code> line <code>' . $hsLine . '</code>';\r
+               }\r
+               else\r
+               {\r
+                       $extraInfo = '';\r
+               }\r
+               \r
+               startUpError(\r
+                  "<p>The page headers have already been sent out{$extraInfo}. This could cause Nucleus not to work in the expected way.</p>"\r
+                . "<p>Usually, this is caused by spaces or newlines at the end of the <code>config.php</code> file, "\r
+                . "at the end of the translation file or at the end of a plugin file.</p>"\r
+                . "<p>Please check this and try again.</p>"\r
+                . "<p>If you don't want to see this error message again, without solving the problem, "\r
+                . "set <code>{$CONF['alertOnHeadersSent']}</code> in <code>globalfunctions.php</code> to <code>0</code></p>"\r
+                . "Page headers already sent"\r
+               );\r
+               exit;\r
+       }\r
+       \r
+       // make is so ?archivelist without blogname or blogid shows the archivelist\r
+       // for the default weblog\r
+       if ( serverVar('QUERY_STRING') == 'archivelist' )\r
+       {\r
+               $archivelist = $CONF['DefaultBlog'];\r
+       }\r
+       \r
+       // now decide which type of skin we need\r
+       if ( $itemid )\r
+       {\r
+               // itemid given -> only show that item\r
+               $type = 'item';\r
+               \r
+               if ( !$manager->existsItem($itemid,intval($CONF['allowFuture']),intval($CONF['allowDrafts'])) )\r
+               {\r
+                       doError(_ERROR_NOSUCHITEM);\r
+               }\r
+               \r
+               // 1. get timestamp, blogid and catid for item\r
+               $query = 'SELECT itime, iblog, icat FROM %s WHERE inumber=%d';\r
+               $query = sprintf($query, sql_table('item'), intval($itemid));\r
+               $row = DB::getRow($query);\r
+               \r
+               // if a different blog id has been set through the request or selectBlog(),\r
+               // deny access\r
+               \r
+               if ( $blogid && (intval($blogid) != $row['iblog']) )\r
+               {\r
+                       doError(_ERROR_NOSUCHITEM);\r
+               }\r
+               \r
+               // if a category has been selected which doesn't match the item, ignore the\r
+               // category. #85\r
+               if ( ($catid != 0) && ($catid != $row['icat']) )\r
+               {\r
+                       $catid = 0;\r
+               }\r
+               \r
+               $blogid = $row['iblog'];\r
+               $timestamp = strtotime($row['itime']);\r
+               \r
+               $b =& $manager->getBlog($blogid);\r
+               \r
+               if ( !$b->isValidCategory($catid) )\r
+               {\r
+                       $query = "SELECT inumber, ititle FROM %s WHERE itime<%s AND idraft=0 AND iblog=%d ORDER BY itime DESC LIMIT 1";\r
+                       $query = sprintf($query, sql_table('item'), DB::formatDateTime($timestamp), intval($blogid));\r
+               }\r
+               else\r
+               {\r
+                       $query = "SELECT inumber, ititle FROM %s WHERE itime<%s AND idraft=0 AND iblog=%d AND icat=%d ORDER BY itime DESC LIMIT 1";\r
+                       $query = sprintf($query, sql_table('item'), DB::formatDateTime($timestamp), intval($blogid), intval($catid));\r
+               }\r
+               $row = DB::getRow($query);\r
+               \r
+               if ( $row )\r
+               {\r
+                       $itemidprev = $row['inumber'];\r
+                       $itemtitleprev = $row['ititle'];\r
+               }\r
+               \r
+               // get next itemid and title\r
+               if ( !$b->isValidCategory($catid) )\r
+               {\r
+                       $query = "SELECT inumber, ititle FROM %s WHERE itime>%s AND itime<=%s AND idraft=0 AND iblog=%d ORDER BY itime ASC LIMIT 1";\r
+                       $query = sprintf($query, sql_table('item'), DB::formatDateTime($timestamp), DB::formatDateTime($b->getCorrectTime()), intval($blogid));\r
+               }\r
+               else\r
+               {\r
+                       $query = "SELECT inumber, ititle FROM %s WHERE itime>%s AND itime<=%s AND idraft=0 AND iblog=%d AND icat=%d ORDER BY itime ASC LIMIT 1";\r
+                       $query = sprintf($query, sql_table('item'), DB::formatDateTime($timestamp), DB::formatDateTime($b->getCorrectTime()), intval($blogid), intval($catid));\r
+               }\r
+               $row = DB::getRow($query);\r
+               \r
+               if ( $row )\r
+               {\r
+                       $itemidnext = $row['inumber'];\r
+                       $itemtitlenext = $row['ititle'];\r
+               }\r
+       }\r
+       elseif ( $archive )\r
+       {\r
+               // show archive\r
+               $type = 'archive';\r
+               \r
+               // sql queries for the timestamp of the first and the last published item\r
+               $query = sprintf('SELECT UNIX_TIMESTAMP(itime) as result FROM %s WHERE idraft=0 ORDER BY itime ASC', sql_table('item'));\r
+               $first_timestamp = DB::getValue($query);\r
+               $query = sprintf('SELECT UNIX_TIMESTAMP(itime) as result FROM %s WHERE idraft=0 ORDER BY itime DESC', sql_table('item'));\r
+               $last_timestamp = DB::getValue($query);\r
+               \r
+               sscanf($archive, '%d-%d-%d', $y, $m, $d);\r
+               \r
+               if ( $d != 0 )\r
+               {\r
+                       $archivetype = _ARCHIVETYPE_DAY;\r
+                       $t = mktime(0, 0, 0, $m, $d, $y);\r
+                       // one day has 24 * 60 * 60 = 86400 seconds\r
+                       $archiveprev = i18n::formatted_datetime('%Y-%m-%d', $t - 86400 );\r
+                       // check for published items\r
+                       if ( $t > $first_timestamp )\r
+                       {\r
+                               $archiveprevexists = true;\r
+                       }\r
+                       else\r
+                       {\r
+                               $archiveprevexists = false;\r
+                       }\r
+                       \r
+                       // one day later\r
+                       $t += 86400;\r
+                       $archivenext = i18n::formatted_datetime('%Y-%m-%d', $t);\r
+                       if ( $t < $last_timestamp )\r
+                       {\r
+                               $archivenextexists = true;\r
+                       }\r
+                       else\r
+                       {\r
+                               $archivenextexists = false;\r
+                       }\r
+               }\r
+               elseif ( $m == 0 )\r
+               {\r
+                       $archivetype = _ARCHIVETYPE_YEAR;\r
+                       $t = mktime(0, 0, 0, 12, 31, $y - 1);\r
+                       // one day before is in the previous year\r
+                       $archiveprev = i18n::formatted_datetime('%Y', $t);\r
+                       if ( $t > $first_timestamp )\r
+                       {\r
+                               $archiveprevexists = true;\r
+                       }\r
+                       else\r
+                       {\r
+                               $archiveprevexists = false;\r
+                       }\r
+\r
+                       // timestamp for the next year\r
+                       $t = mktime(0, 0, 0, 1, 1, $y + 1);\r
+                       $archivenext = i18n::formatted_datetime('%Y', $t);\r
+                       if ( $t < $last_timestamp )\r
+                       {\r
+                               $archivenextexists = true;\r
+                       }\r
+                       else\r
+                       {\r
+                               $archivenextexists = false;\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       $archivetype = _ARCHIVETYPE_MONTH;\r
+                       $t = mktime(0, 0, 0, $m, 1, $y);\r
+                       // one day before is in the previous month\r
+                       $archiveprev = i18n::formatted_datetime('%Y-%m', $t - 86400);\r
+                       if ( $t > $first_timestamp )\r
+                       {\r
+                               $archiveprevexists = true;\r
+                       }\r
+                       else\r
+                       {\r
+                               $archiveprevexists = false;\r
+                       }\r
+                       \r
+                       // timestamp for the next month\r
+                       $t = mktime(0, 0, 0, $m+1, 1, $y);\r
+                       $archivenext = i18n::formatted_datetime('%Y-%m', $t);\r
+                       if ( $t < $last_timestamp )\r
+                       {\r
+                               $archivenextexists = true;\r
+                       }\r
+                       else\r
+                       {\r
+                               $archivenextexists = false;\r
+                       }\r
+               }\r
+       }\r
+       elseif ( $archivelist )\r
+       {\r
+               $type = 'archivelist';\r
+               \r
+               if ( is_numeric($archivelist) )\r
+               {\r
+                       $blogid = intVal($archivelist);\r
+               }\r
+               else\r
+               {\r
+                       $blogid = getBlogIDFromName($archivelist);\r
+               }\r
+       \r
+               if ( !$blogid )\r
+               {\r
+                       doError(_ERROR_NOSUCHBLOG);\r
+               }\r
+       }\r
+       elseif ( $query )\r
+       {\r
+               global $startpos;\r
+               $type = 'search';\r
+               $query = stripslashes($query);\r
+               \r
+               if ( is_numeric($blogid) )\r
+               {\r
+                       $blogid = intVal($blogid);\r
+               }\r
+               else\r
+               {\r
+                       $blogid = getBlogIDFromName($blogid);\r
+               }\r
+               \r
+               if ( !$blogid )\r
+               {\r
+                       doError(_ERROR_NOSUCHBLOG);\r
+               }\r
+       }\r
+       elseif ( $memberid )\r
+       {\r
+               $type = 'member';\r
+               \r
+               if ( !Member::existsID($memberid) )\r
+               {\r
+                       doError(_ERROR_NOSUCHMEMBER);\r
+               }\r
+               $memberinfo = $manager->getMember($memberid);\r
+       }\r
+       elseif ( $imagepopup )\r
+       {\r
+               // media object (images etc.)\r
+               $type = 'imagepopup';\r
+               \r
+               // TODO: check if media-object exists\r
+               // TODO: set some vars?\r
+       }\r
+       else\r
+       {\r
+               // show regular index page\r
+               global $startpos;\r
+               $type = 'index';\r
+       }\r
+       \r
+       // any type of skin with catid\r
+       if ( $catid && !$blogid )\r
+       {\r
+               $blogid = getBlogIDFromCatID($catid);\r
+       }\r
+       \r
+       // decide which blog should be displayed\r
+       if ( !$blogid )\r
+       {\r
+               $blogid = $CONF['DefaultBlog'];\r
+       }\r
+       \r
+       $b =& $manager->getBlog($blogid);\r
+       $blog = $b; // references can't be placed in global variables?\r
+       \r
+       if ( !$blog->isValid )\r
+       {\r
+               doError(_ERROR_NOSUCHBLOG);\r
+       }\r
+       \r
+       // set catid if necessary\r
+       if ( $catid )\r
+       {\r
+               // check if the category is valid\r
+               if ( !$blog->isValidCategory($catid) )\r
+               {\r
+                       doError(_ERROR_NOSUCHCATEGORY);\r
+               }\r
+               else\r
+               {\r
+                       $blog->setSelectedCategory($catid);\r
+               }\r
+       }\r
+       \r
+       if ( !$skinid )\r
+       {\r
+               $skinid = $blog->getDefaultSkin();\r
+       }\r
+       \r
+       if ( !empty($special) && isValidShortName($special) )\r
+       {\r
+               $type = strtolower($special);\r
+       }\r
+       \r
+       $skin = new SKIN($skinid);\r
+       \r
+       if ( !$skin->isValid() )\r
+       {\r
+               doError(_ERROR_NOSUCHSKIN);\r
+       }\r
+       \r
+       // set global skinpart variable so can determine quickly what is being parsed from any plugin or phpinclude\r
+       $skinpart = $type;\r
+       \r
+       // parse the skin\r
+       $skin->parse($type);\r
+       \r
+       // check to see we should throw JustPosted event\r
+       $blog->checkJustPosted();\r
+       return;\r
+}\r
+\r
+/**\r
+ * doError()\r
+ * Show error skin with given message. An optional skin-object to use can be given\r
+ * \r
+ * @param      string  $msg\r
+ * @param      string  $skin\r
+ * @return     void\r
+ */\r
+function doError($msg, $skin = '')\r
+{\r
+       global $errormessage, $CONF, $skinid, $blogid, $manager;\r
+       \r
+       if ( $skin == '' )\r
+       {\r
+               if ( Skin::existsID($skinid) )\r
+               {\r
+                       $id = $skinid;\r
+               }\r
+               elseif ( $manager->existsBlogID($blogid) )\r
+               {\r
+                       $blog =& $manager->getBlog($blogid);\r
+                       $id = $blog->getDefaultSkin();\r
+               }\r
+               elseif ($CONF['DefaultBlog'] )\r
+               {\r
+                       $blog =& $manager->getBlog($CONF['DefaultBlog']);\r
+                       $id = $blog->getDefaultSkin();\r
+               }\r
+               else\r
+               {\r
+                       // this statement should actually never be executed\r
+                       $id = $CONF['BaseSkin'];\r
+               }\r
+               $skin = new Skin($id);\r
+       }\r
+       \r
+       $errormessage = $msg;\r
+       $skin->parse('error');\r
+       return;
+}\r
+\r
+/**\r
+ * Errors before the database connection has been made\r
+ * \r
+ * @param      string  $msg    message to notify\r
+ * @param      string  $title  page title\r
+ * @return     void\r
+ */\r
+function startUpError($msg, $title)\r
+{\r
+       header('Content-Type: text/xml; charset=' . i18n::get_current_charset());\r
+       echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";\r
+       echo "<head>\n";\r
+       echo "<title>{$title}</title></head>\n";\r
+       echo "<body>\n";\r
+       echo "<h1>{$title}</h1>\n";\r
+       echo $msg;\r
+       echo "</body>\n";\r
+       echo "</html>\n";\r
+       exit;\r
+}\r
+\r
+function isValidShortName($name)\r
+{\r
+       return preg_match('#^[a-z0-9]+$#i', $name);\r
+}\r
+function isValidDisplayName($name)\r
+{\r
+       return preg_match('#^[a-z0-9]+[a-z0-9 ]*[a-z0-9]+$#i', $name);\r
+}\r
+function isValidCategoryName($name)\r
+{\r
+       return 1;\r
+}\r
+function isValidTemplateName($name)\r
+{\r
+       return preg_match('#^[a-z0-9/_\-]+$#i', $name);\r
+}\r
+function isValidSkinName($name)\r
+{\r
+       return preg_match('#^[a-z0-9/_\-]+$#i', $name);\r
+}\r
+\r
+// add and remove linebreaks\r
+function addBreaks($var)\r
+{\r
+       return nl2br($var);\r
+}\r
+function removeBreaks($var)\r
+{\r
+       return preg_replace("/<br \/>([\r\n])/", "$1", $var);\r
+}\r
+\r
+/**\r
+ * parseFile()\r
+ * \r
+ * @param      string  $filename\r
+ * @param      string  $includeMode\r
+ * @param      string  $includePrefix\r
+ * @return     void\r
+ */\r
+function parseFile($filename, $includeMode = 'normal', $includePrefix = '')\r
+{\r
+       global $skinid;\r
+       \r
+       if ( !$skinid || !existsID($skinid) )\r
+       {\r
+               $skin = new Skin($CONF['BaseSkin']);\r
+       }\r
+       else\r
+       {\r
+               $skin = new Skin($skinid);\r
+       }\r
+       \r
+       $oldIncludeMode = Parser::getProperty('IncludeMode');\r
+       $oldIncludePrefix = Parser::getProperty('IncludePrefix');\r
+       \r
+       $skin->parse('fileparse', $filename);\r
+       \r
+       Parser::setProperty('IncludeMode', $oldIncludeMode);\r
+       Parser::setProperty('IncludePrefix', $oldIncludePrefix);\r
+       \r
+       return;\r
+}\r
+\r
+/**\r
+ * debug()\r
+ * Outputs a debug message\r
+ * \r
+ * @param      string  $msg\r
+ * @return     void\r
+ */\r
+function debug($msg)\r
+{\r
+       echo '<p><b>' . $msg . "</b></p>\n";\r
+       return;\r
+}\r
+\r
+// shows a link to help file\r
+function help($id)\r
+{\r
+       echo helpHtml($id);\r
+       return;\r
+}\r
+function helpHtml($id)\r
+{\r
+       global $CONF;\r
+       return helplink($id) . '<img src="' . $CONF['AdminURL'] . 'documentation/icon-help.gif" width="15" height="15" alt="' . _HELP_TT . '" title="' . _HELP_TT . '" /></a>';\r
+}\r
+function helplink($id)\r
+{\r
+       global $CONF;\r
+       return '<a href="' . $CONF['AdminURL'] . 'documentation/help.html#'. $id . '" onclick="if (event &amp;&amp; event.preventDefault) event.preventDefault(); return help(this.href);">';\r
+}\r
+\r
+/**\r
+ * includephp()\r
+ * Includes a PHP file. This method can be called while parsing templates and skins\r
+ * \r
+ * @param      string  $filename       name of file to parse\r
+ * @return     void\r
+ */\r
+function includephp($filename)\r
+{\r
+       // make predefined variables global, so most simple scripts can be used here\r
+       \r
+       // apache (names taken from PHP doc)\r
+       global $GATEWAY_INTERFACE, $SERVER_NAME, $SERVER_SOFTWARE, $SERVER_PROTOCOL;\r
+       global $REQUEST_METHOD, $QUERY_STRING, $DOCUMENT_ROOT, $HTTP_ACCEPT;\r
+       global $HTTP_ACCEPT_CHARSET, $HTTP_ACCEPT_ENCODING, $HTTP_ACCEPT_LANGUAGE;\r
+       global $HTTP_CONNECTION, $HTTP_HOST, $HTTP_REFERER, $HTTP_USER_AGENT;\r
+       global $REMOTE_ADDR, $REMOTE_PORT, $SCRIPT_FILENAME, $SERVER_ADMIN;\r
+       global $SERVER_PORT, $SERVER_SIGNATURE, $PATH_TRANSLATED, $SCRIPT_NAME;\r
+       global $REQUEST_URI;\r
+       \r
+       // php (taken from PHP doc)\r
+       global $argv, $argc, $PHP_SELF, $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS;\r
+       global $HTTP_POST_FILES, $HTTP_ENV_VARS, $HTTP_SERVER_VARS, $HTTP_SESSION_VARS;\r
+       \r
+       // other\r
+       global $PATH_INFO, $HTTPS, $HTTP_RAW_POST_DATA, $HTTP_X_FORWARDED_FOR;\r
+       \r
+       if ( @file_exists($filename) )\r
+       {\r
+               include($filename);\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * Checks if a certain plugin exists\r
+ * @param      string  $plug   name of plugin\r
+ * @return     boolean exists or not\r
+ */\r
+function checkPlugin($name)\r
+{\r
+       global $DIR_PLUGINS;\r
+       return file_exists($DIR_PLUGINS . preg_replace('#[\\\\|/]#', '', $name) . '.php');\r
+}\r
+\r
+/**\r
+ * alterQueryStr()\r
+ * \r
+ * @param      string  $querystr       querystring to alter (e.g. foo=1&bar=2&x=y)\r
+ * @param      string  $param  name of parameter to change (e.g. 'foo')\r
+ * @param      string  $value  New value for that parameter (e.g. 3)\r
+ * @return     string  altered query string (for the examples above: foo=3&bar=2&x=y)\r
+ */\r
+function alterQueryStr($querystr, $param, $value)\r
+{\r
+       $vars = preg_split('#&#', $querystr);\r
+       $set = FALSE;\r
+       \r
+       for ( $i = 0; $i < count($vars); $i++ )\r
+       {\r
+               $v = preg_split('#=#', $vars[$i]);\r
+               \r
+               if ( $v[0] == $param )\r
+               {\r
+                       $v[1] = $value;\r
+                       $vars[$i] = implode('=', $v);\r
+                       $set = true;\r
+                       break;\r
+               }\r
+       }\r
+       if ( !$set )\r
+       {\r
+               $vars[] = "{$param}={$value}";\r
+       }\r
+       return ltrim(implode('&', $vars), '&');\r
+}\r
+\r
+/**\r
+ * passVar()\r
+ * passes one variable as hidden input field (multiple fields for arrays)\r
+ * @see passRequestVars in varsx.x.x.php\r
+ * \r
+ * @param      string  $key\r
+ * @param      string  $value\r
+ * @return     void\r
+ */\r
+function passVar($key, $value)\r
+{\r
+       // array ?\r
+       if ( is_array($value) )\r
+       {\r
+               for ( $i = 0; $i < sizeof($value); $i++ )\r
+               {\r
+                       passVar($key . '[' . $i . ']', $value[$i]);\r
+               }\r
+               return;\r
+       }\r
+       \r
+       // other values: do stripslashes if needed\r
+       echo '<input type="hidden" name="' . Entity::hsc($key) . '" value="' . Entity::hsc(undoMagic($value)) . '" />' . "\n";\r
+       return;\r
+}\r
+\r
+/**\r
+ * checkVars()\r
+ * \r
+ * @param      string  $variables\r
+ * @return     void\r
+ */\r
+function checkVars($variables)\r
+{\r
+       foreach ( $variables as $variable )\r
+       {\r
+               if ( array_key_exists($variable, $_GET)\r
+                 || array_key_exists($variable, $_POST)\r
+                 || array_key_exists($variable, $_COOKIE)\r
+                 || array_key_exists($variable, $_ENV)\r
+                 || (session_id() !== '' && array_key_exists($variable, $_SESSION))\r
+                 || array_key_exists($variable, $_FILES) )\r
+               {\r
+                       die('Sorry. An error occurred.');\r
+               }\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * sanitizeParams()\r
+ * Sanitize parameters such as $_GET and $_SERVER['REQUEST_URI'] etc.\r
+ * to avoid XSS.\r
+ * \r
+ * @param      void\r
+ * @return     void\r
+ */\r
+function sanitizeParams()\r
+{\r
+       $array = array();\r
+       $str = '';\r
+       $frontParam = '';\r
+       \r
+       // REQUEST_URI of $_SERVER\r
+       $str =& $_SERVER["REQUEST_URI"];\r
+       serverStringToArray($str, $array, $frontParam);\r
+       sanitizeArray($array);\r
+       arrayToServerString($array, $frontParam, $str);\r
+       \r
+       // QUERY_STRING of $_SERVER\r
+       $str =& $_SERVER["QUERY_STRING"];\r
+       serverStringToArray($str, $array, $frontParam);\r
+       sanitizeArray($array);\r
+       arrayToServerString($array, $frontParam, $str);\r
+       \r
+       // $_GET\r
+       convArrayForSanitizing($_GET, $array);\r
+       sanitizeArray($array);\r
+       revertArrayForSanitizing($array, $_GET);\r
+       \r
+       // $_REQUEST (only GET param)\r
+       convArrayForSanitizing($_REQUEST, $array);\r
+       sanitizeArray($array);\r
+       revertArrayForSanitizing($array, $_REQUEST);\r
+       \r
+       return;\r
+}\r
+\r
+function _addInputTags(&$keys,$prefix='')\r
+{\r
+       foreach ( $keys as $key=>$value )\r
+       {\r
+               if ( $prefix )\r
+               {\r
+                       $key=$prefix.'['.$key.']';\r
+               }\r
+               if ( is_array($value) )\r
+               {\r
+                       _addInputTags($value,$key);\r
+               }\r
+               else\r
+               {\r
+                       if ( get_magic_quotes_gpc() )\r
+                               {$value=stripslashes($value);\r
+                       }\r
+                       if ( $key == 'ticket' )\r
+                       {\r
+                               continue;\r
+                       }\r
+                       echo '<input type="hidden" name="'.Entity::hsc($key).\r
+                            '" value="'.Entity::hsc($value).'" />'."\n";\r
+               }\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * serverStringToArray()\r
+ * Convert the server string such as $_SERVER['REQUEST_URI']\r
+ * to arry like arry['blogid']=1 and array['page']=2 etc.\r
+ * \r
+ * @param      string   $uri                           string\r
+ * @param      string  &$query_elements        elements of query according to application/x-www-form-urlencoded\r
+ * @param      string  &$hier_part                     hierarchical part includes path\r
+ * \r
+ * NOTE:\r
+ * RFC 3986: Uniform Resource Identifiers (URI): Generic Syntax\r
+ * 3.  Syntax Components\r
+ * http://www.ietf.org/rfc/rfc3986.txt\r
+ * \r
+ * Hypertext Markup Language - 2.0\r
+ * 8.2.1. The form-urlencoded Media Type\r
+ * http://tools.ietf.org/html/rfc1866#section-8.2.1\r
+ * \r
+ * $_SERVER > Language Reference > Predefined Variables > PHP Manual\r
+ * http://www.php.net/manual/en/reserved.variables.server.php\r
+ */\r
+function serverStringToArray($uri, &$query_elements, &$hier_part)\r
+{\r
+       // init param\r
+       $query_elements = array();\r
+       $hier_part = "";\r
+       \r
+       // split hierarchical part, e.g. /index.php, query and fragment, e.g. blogid=1&page=2#section1\r
+       if ( i18n::strpos($uri, "?") > 0 )\r
+       {\r
+               list($hier_part, $query_and_fragment) = preg_split("#\?#", $uri, 2);\r
+       }\r
+       else\r
+       {\r
+               $query_and_fragment = $uri;\r
+               $hier_part = '';\r
+       }\r
+       \r
+       // If there is no query like blogid=1&page=2, return\r
+       if ( i18n::strpos($uri, "=") == FALSE && !i18n::strlen($hier_part) )\r
+       {\r
+               $hier_part = $uri;\r
+               return;\r
+       }\r
+       \r
+       $query_elements = preg_split("#&#", $query_and_fragment);\r
+       return;\r
+}\r
+\r
+/**\r
+ * arrayToServerString()\r
+ * Convert array like array['blogid'] to server string\r
+ * such as $_SERVER['REQUEST_URI']\r
+ * \r
+ * @param      array    $query_elements        elements of query according to application/x-www-form-urlencoded\r
+ * @param      string   $hier_part                     hier-part defined in RFC3986\r
+ * @param      string  &$uri                           return value\r
+ * @return     void\r
+ * \r
+ * NOTE:\r
+ * RFC 3986: Uniform Resource Identifiers (URI): Generic Syntax\r
+ * 3.  Syntax Components\r
+ * http://www.ietf.org/rfc/rfc3986.txt\r
+ * \r
+ * Hypertext Markup Language - 2.0\r
+ * 8.2.1. The form-urlencoded Media Type\r
+ * http://tools.ietf.org/html/rfc1866#section-8.2.1\r
+ * \r
+ * $_SERVER > Language Reference > Predefined Variables > PHP Manual\r
+ * http://www.php.net/manual/en/reserved.variables.server.php\r
+ */\r
+function arrayToServerString($query_elements, $hier_part, &$uri)\r
+{\r
+       if ( i18n::strpos($uri, "?") !== FALSE )\r
+       {\r
+               $uri = $hier_part . "?";\r
+       }\r
+       else\r
+       {\r
+               $uri = $hier_part;\r
+       }\r
+       if ( count($query_elements) > 0 )\r
+       {\r
+               $uri .= implode("&", $query_elements);\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * sanitizeArray()\r
+ * Sanitize array parameters.\r
+ * This function checks both key and value.\r
+ * - check key if it inclues " (double quote),  remove from array\r
+ * - check value if it includes \ (escape sequece), remove remaining string\r
+ * \r
+ * @param      array   &$array elements of query according to application/x-www-form-urlencoded\r
+ * @return     void\r
+ */\r
+function sanitizeArray(&$array)\r
+{\r
+       $excludeListForSanitization = array('query');\r
+       \r
+       foreach ( $array as $k => $v )\r
+       {\r
+               // split to key and value\r
+               list($key, $val) = preg_split("#=#", $v, 2);\r
+               if ( !isset($val) )\r
+               {\r
+                       continue;\r
+               }\r
+               \r
+               // when magic quotes is on, need to use stripslashes,\r
+               // and then addslashes\r
+               if ( get_magic_quotes_gpc() )\r
+               {\r
+                       $val = stripslashes($val);\r
+               }\r
+               \r
+               // note that we must use addslashes here because this function is called before the db connection is made\r
+               // and sql_real_escape_string needs a db connection\r
+               $val = addslashes($val);\r
+               \r
+               // if $key is included in exclude list, skip this param\r
+               if ( !in_array($key, $excludeListForSanitization) )\r
+               {\r
+                       // check value\r
+                       if ( i18n::strpos($val, '\\') > 0 )\r
+                       {\r
+                               list($val, $tmp) = preg_split('#\\\\#', $val);\r
+                       }\r
+                       \r
+                       // remove control code etc.\r
+                       $val = strtr($val, "\0\r\n<>'\"", "       ");\r
+                       \r
+                       // check key\r
+                       if ( preg_match('#\"#', $key) > 0 )\r
+                       {\r
+                               unset($array[$k]);\r
+                               continue;\r
+                       }\r
+                       \r
+                       // set sanitized info\r
+                       $array[$k] = sprintf("%s=%s", $key, $val);\r
+               }\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * convArrayForSanitizing()\r
+ * Convert array for sanitizeArray function\r
+ * \r
+ * @param      string  $src    array to be sanitized\r
+ * @param      array   &$array array to be temporarily stored\r
+ * @return     void\r
+ */\r
+function convArrayForSanitizing($src, &$array)\r
+{\r
+       $array = array();\r
+       foreach ( $src as $key => $val )\r
+       {\r
+               if ( !key_exists($key, $_GET) )\r
+               {\r
+                       continue;\r
+               }\r
+               $array[] = sprintf("%s=%s", $key, $val);\r
+               continue;\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+ * revertArrayForSanitizing()\r
+ * Revert array after sanitizeArray function\r
+ * \r
+ * @param      array   $array  element of query according to application/x-www-form-urlencoded\r
+ * @param      array   &$dst   combination of key and value\r
+ * @return     void\r
+ */\r
+function revertArrayForSanitizing($array, &$dst)\r
+{\r
+       foreach ( $array as $v )\r
+       {\r
+               list($key, $val) = preg_split("#=#", $v, 2);\r
+               $dst[$key] = $val;\r
+               continue;\r
+       }\r
+       return;\r
+}\r
+\r
+/**\r
+=======
 <?php
 
 /*
@@ -1834,6 +3642,7 @@ function revertArrayForSanitizing($array, &$dst)
 }
 
 /**
+>>>>>>> skinnable-master
  * decodePathInfo()
  * 
  * @param      string  $virtualpath
@@ -1978,6 +3787,404 @@ function decodePathInfo($virtualpath)
 
 
 /**
+<<<<<<< HEAD
+ * redirect()\r
+ * Stops processing the request and redirects to the given URL.\r
+ * - no actual contents should have been sent to the output yet\r
+ * - the URL will be stripped of illegal or dangerous characters\r
+ * \r
+ * @param      string  $uri\r
+ * @return     void\r
+ */\r
+function redirect($url)\r
+{\r
+       $url = preg_replace('#[^a-z0-9-~+_.?\#=&;,/:@%*]#i', '', $url);\r
+       header('Location: ' . $url);\r
+       exit;\r
+}\r
+\r
+/**\r
+ * getBookmarklet()\r
+ * Returns the Javascript code for a bookmarklet that works on most modern browsers\r
+ * \r
+ * @param      integer $blogid ID for weblog\r
+ * @return     script to call Bookmarklet\r
+ */\r
+function getBookmarklet($blogid, $width=600,  $height=500)\r
+{\r
+       global $CONF;\r
+       \r
+       $script = "Q='';"\r
+               . "x=document;"\r
+               . "y=window;"\r
+               . "if ( x.selection )"\r
+               . "{"\r
+               . " Q=x.selection.createRange().text;"\r
+               . "}"\r
+               . "else if ( y.getSelection )"\r
+               . "{"\r
+               . " Q=y.getSelection();"\r
+               . "}"\r
+               . "else if ( x.getSelection )"\r
+               . "{"\r
+               . " Q=x.getSelection();"\r
+               . "}"\r
+               . "wingm = window.open('{$CONF['AdminURL']}bookmarklet.php?blogid={$blogid}"\r
+               . " &logtext=' + encodeURIComponent(Q) +"\r
+               . " '&loglink=' + encodeURIComponent(x.location.href) +"\r
+               . " '&loglinktitle=' + encodeURIComponent(x.title),"\r
+               . " 'nucleusbm',"\r
+               . " 'scrollbars=yes,width={$width},height={$height},left=10,top=10,status=yes,resizable=yes');"\r
+               . "wingm.focus();";\r
+       \r
+       return $script;\r
+}\r
+\r
+/**\r
+ * cleanFileName()\r
+ * cleans filename of uploaded file for writing to file system\r
+ *\r
+ * @param      string  $str\r
+ * @return     string  $cleaned filename ready for use\r
+ */\r
+function cleanFileName($str)\r
+{\r
+       $str = strtolower($str);\r
+       $ext_point = i18n::strrpos($str,".");\r
+       if ( $ext_point === FALSE )\r
+       {\r
+               return FALSE;\r
+       }\r
+       $ext = i18n::substr($str,$ext_point,i18n::strlen($str));\r
+       $str = i18n::substr($str,0,$ext_point);\r
+       \r
+       return preg_replace("#[^a-z0-9-]#", "_", $str) . $ext;\r
+}\r
+\r
+/**\r
+ * use Notification class instead of this\r
+ * Deprecated since 4.0:\r
+ */\r
+function getMailFooter()\r
+{\r
+       NOTIFICATION::get_mail_footer();\r
+}\r
+function isValidMailAddress($address)\r
+{\r
+       return NOTIFICATION::address_validation($address);\r
+}\r
+/**\r
+ * use Entity class instead of this\r
+ * Deprecated since 4.0:\r
+ */\r
+function highlight($text, $expression, $highlight)\r
+{\r
+       return Entity::highlight($text, $expression, $highlight);\r
+}\r
+function shorten($string, $maxlength, $suffix)\r
+{\r
+       return Entity::shorten($string, $maxlength, $suffix);\r
+}\r
+function stringStripTags ($string)\r
+{\r
+       return Entity::strip_tags($string);\r
+}\r
+function toAscii($string)\r
+{\r
+       return Entity::anchor_footnoting($string);\r
+}\r
+function stringToAttribute ($string)\r
+{\r
+       return Entity::hsc($string);\r
+}\r
+function stringToXML ($string)\r
+{\r
+       return Entity::hen($string);\r
+}\r
+function encode_desc($data)\r
+{\r
+       return Entity::hen($data);\r
+}\r
+/**\r
+ * Centralisation of the functions that deals with locales\r
+ * This functions is based on the old way to deal with languages\r
+ * Deprecated since 4.0:\r
+ */\r
+function getLanguageName()\r
+{\r
+       if( ($language = i18n::convert_locale_to_old_language_file_name(i18n::get_current_locale())) === FALSE )\r
+       {\r
+               $language ='english';\r
+       }\r
+       return $language;\r
+}\r
+function selectLanguage($language)\r
+{\r
+       global $DIR_LANG;\r
+       include($DIR_LANG . preg_replace('#[\\\\|/]#', '', $language) . '.php');\r
+       return;\r
+}\r
+/**\r
+ * use i18n class instead of these\r
+ * Deprecated since 4.0\r
+ */\r
+function checkLanguage($lang)\r
+{\r
+       return ( preg_match('#^(.+)_(.+)_(.+)$#', $lang)\r
+         || i18n::convert_old_language_file_name_to_locale($lang) );\r
+}\r
+function formatDate($format, $timestamp, $default_format, &$blog)\r
+{\r
+       $offset = date('Z', $timestamp);\r
+       if ( $blog )\r
+       {\r
+               $offset += $blog->getTimeOffset() * 3600;\r
+       }\r
+       return i18n::formatted_datetime($format, $timestamp, $offset, $default_format);\r
+}\r
+\r
+/**\r
+ * use DB class instead of these\r
+ * Deprecated since 4.0\r
+ */\r
+function quickQuery($query)\r
+{\r
+       $row = DB::getRow($query);\r
+       return $row['result'];\r
+}\r
+function mysqldate($timestamp)\r
+{\r
+       return DB::formatDateTime($timestamp);\r
+ }\r
+/**\r
+ * Centralisation of the functions that generate links\r
+ * Deprecated since 4.0:\r
+ * Please use Link::FunctionName(...) instead\r
+ */\r
+function createItemLink($itemid, $extra = '')\r
+{\r
+       return Link::create_item_link($itemid, $extra);\r
+}\r
+function createMemberLink($memberid, $extra = '')\r
+{\r
+       return Link::create_member_link($memberid, $extra);\r
+}\r
+function createCategoryLink($catid, $extra = '')\r
+{\r
+       return Link::create_category_link($catid, $extra);\r
+}\r
+function createArchiveListLink($blogid = '', $extra = '')\r
+{\r
+       return Link::create_archivelist_link($blogid, $extra);\r
+}\r
+function createArchiveLink($blogid, $archive, $extra = '')\r
+{\r
+       return Link::create_archive_link($blogid, $archive, $extra);\r
+}\r
+function createBlogidLink($blogid, $params = '')\r
+{\r
+       return Link::create_blogid_link($blogid, $params = '');\r
+}\r
+function createLink($type, $params)\r
+{\r
+       return Link::create_link($type, $params);\r
+}\r
+function createBlogLink($url, $params)\r
+{\r
+       return Link::create_blog_link($url, $params);\r
+}\r
+/**\r
+ * use ActionLog class instead of this\r
+ * Deprecated since 4.0\r
+ */\r
+function addToLog($level, $msg)\r
+{\r
+       ActionLog::add($level, $msg);\r
+}\r
+/**\r
+ * use PHP's implement\r
+ * Deprecated since 4.0\r
+ */\r
+function ifset(&$var)\r
+{\r
+       if ( isset($var) )\r
+       {\r
+               return $var;\r
+       }\r
+       \r
+       return NULL;\r
+}\r
+/**\r
+ * use Manager::getPluginNameFromPid() instead of this\r
+ * Deprecated since 4.0\r
+ */\r
+function getPluginNameFromPid($pid)\r
+{\r
+       global $manager;\r
+       return $manager->getPluginNameFromPid($pid);\r
+}\r
+/**\r
+ * use Manager::numberOfEventSubscribers() instead of this\r
+ * Deprecated since 4.0\r
+ */\r
+function numberOfEventSubscribers($event)\r
+{\r
+       global $manager;\r
+       return $manager->getNumberOfSubscribers($event);\r
+}\r
+\r
+/**\r
+ * PluginAdmin has already the alternative implement\r
+ * Deprecated since 4.0\r
+ */\r
+function ticketForPlugin()\r
+{\r
+       global $CONF, $DIR_LIBS, $DIR_LOCALES, $DIR_PLUGINS, $manager, $member, $ticketforplugin;\r
+       \r
+       /* initialize */\r
+       $ticketforplugin = array();\r
+       $ticketforplugin['ticket'] = FALSE;\r
+       \r
+       /* Check if using plugin's php file. */\r
+       $p_translated = serverVar('SCRIPT_FILENAME');\r
+       \r
+       if (!file_exists($p_translated) )\r
+       {\r
+               header("HTTP/1.0 404 Not Found");\r
+               exit('');\r
+       }\r
+       \r
+       // check whether this is plugin or not\r
+       $p_translated = str_replace('\\', '/', $p_translated);\r
+       $d_plugins = str_replace('\\', '/', $DIR_PLUGINS);\r
+       if ( i18n::strpos($p_translated, $d_plugins) !== 0 )\r
+       {\r
+               return;\r
+       }\r
+       \r
+       // Solve the plugin php file or admin directory\r
+       $phppath = i18n::substr($p_translated, i18n::strlen($d_plugins) );\r
+       // Remove the first "/" if exists.\r
+       $phppath = preg_replace('#^/#', '', $phppath);\r
+       // Remove the first "NP_" and the last ".php" if exists.\r
+       $path = preg_replace('#^NP_(.*)\.php$#', '$1', $phppath);\r
+       // Remove the "/" and beyond.\r
+       $path = preg_replace('#^([^/]*)/(.*)$#', '$1', $path);\r
+       \r
+       // Solve the plugin name.\r
+       $plugins = array();\r
+       $query = sprintf('SELECT pfile FROM %s;', sql_table('plugin'));\r
+       $res = DB::getResult($query);\r
+       \r
+       foreach ( $res as $row )\r
+       {\r
+               $name = i18n::substr($row['pfile'], 3);\r
+               $plugins[strtolower($name)] = $name;\r
+       }\r
+       \r
+       $res->closeCursor();\r
+       \r
+       if ( !array_key_exists($path, $plugins) )\r
+       {\r
+               header("HTTP/1.0 404 Not Found");\r
+               exit('');\r
+       }\r
+       else\r
+       {\r
+               $plugin_name = $plugins[$path];\r
+       }\r
+       \r
+       /* Return if not index.php */\r
+       if ( ($phppath != strtolower($plugin_name) . '/')\r
+         && ($phppath != strtolower($plugin_name) . '/index.php') )\r
+       {\r
+               return;\r
+       }\r
+       \r
+       /* Exit if not logged in. */\r
+       if ( !$member->isLoggedIn() )\r
+       {\r
+               exit('You aren\'t logged in.');\r
+       }\r
+       \r
+       /* Check if this feature is needed (ie, if "$manager->checkTicket()" is not included in the script). */\r
+       if ( $file = @file($p_translated) )\r
+       {\r
+               $prevline = '';\r
+               \r
+               foreach($file as $line)\r
+               {\r
+                       if (preg_match('#[\$]manager([\s]*)[\-]>([\s]*)checkTicket([\s]*)[\(]#i', $prevline . $line) )\r
+                       {\r
+                               return;\r
+                       }\r
+                       \r
+                       $prevline = $line;\r
+               }\r
+       }\r
+       \r
+       /* Show a form if not valid ticket */\r
+       if ( (i18n::strpos(serverVar('REQUEST_URI'), '?') !== FALSE\r
+         || serverVar('QUERY_STRING')\r
+         || strtoupper(serverVar('REQUEST_METHOD') ) == 'POST')\r
+         && !$manager->checkTicket() )\r
+       {\r
+               $oPluginAdmin = new PluginAdmin($plugin_name);\r
+               $oPluginAdmin->start();\r
+               \r
+               echo '<p>' . _ERROR_BADTICKET . "</p>\n";\r
+               \r
+               // Resolve URI and QUERY_STRING\r
+               if ($uri = serverVar('REQUEST_URI') )\r
+               {\r
+                       list($uri, $qstring) = preg_split('#\?#', $uri);\r
+               }\r
+               else\r
+               {\r
+                       if ( !($uri = serverVar('PHP_SELF') ) )\r
+                       {\r
+                               $uri = serverVar('SCRIPT_NAME');\r
+                       }\r
+                       $qstring = serverVar('QUERY_STRING');\r
+               }\r
+               if ($qstring)\r
+               {\r
+                       $qstring = '?' . $qstring;\r
+               }\r
+               \r
+               echo '<p>' . _SETTINGS_UPDATE . ' : ' . _QMENU_PLUGINS . ' <span style="color:red;">' . Entity::hsc($plugin_name) . "</span> ?</p>\n";\r
+               \r
+               switch(strtoupper(serverVar('REQUEST_METHOD') ) )\r
+               {\r
+                       case 'POST':\r
+                               echo '<form method="POST" action="'.Entity::hsc($uri.$qstring).'">';\r
+                               $manager->addTicketHidden();\r
+                               _addInputTags($_POST);\r
+                               break;\r
+                       \r
+                       case 'GET':\r
+                               echo '<form method="GET" action="'.Entity::hsc($uri).'">';\r
+                               $manager->addTicketHidden();\r
+                               _addInputTags($_GET);\r
+                       \r
+                       default:\r
+                               break;\r
+               }\r
+               \r
+               echo '<input type="submit" value="' . _YES . '" />&nbsp;&nbsp;&nbsp;&nbsp;';\r
+               echo '<input type="button" value="' . _NO . '" onclick="history.back(); return false;" />';\r
+               echo "</form>\n";\r
+               \r
+               $oPluginAdmin->end();\r
+               exit;\r
+       }\r
+       \r
+       /* Create new ticket */\r
+       $ticket=$manager->addTicketToUrl('');\r
+       $ticketforplugin['ticket'] = preg_split($ticket, i18n::strpos($ticket, 'ticket=') + 7);\r
+       return;\r
+}\r
+=======
  * redirect()
  * Stops processing the request and redirects to the given URL.
  * - no actual contents should have been sent to the output yet
@@ -2374,3 +4581,4 @@ function ticketForPlugin()
        $ticketforplugin['ticket'] = preg_split($ticket, i18n::strpos($ticket, 'ticket=') + 7);
        return;
 }
+>>>>>>> skinnable-master