/**
  * Execute a direct database query
  *
  * @param	string		Database query
  * @param	boolean		[Optional] Do not convert table prefix
  * @return	@e resource
  */
 public function query($the_query, $bypass = false)
 {
     //-----------------------------------------
     // Debug?
     //-----------------------------------------
     if ($this->obj['debug'] or $this->obj['use_debug_log'] and $this->obj['debug_log'] or $this->obj['use_bad_log'] and $this->obj['bad_log']) {
         IPSDebug::startTimer();
         $_MEMORY = IPSDebug::getMemoryDebugFlag();
     }
     //-----------------------------------------
     // Stop sub selects? (UNION)
     //-----------------------------------------
     if (!IPS_DB_ALLOW_SUB_SELECTS) {
         # On the spot allowance?
         if (!$this->allow_sub_select) {
             $_tmp = strtolower($this->_removeAllQuotes($the_query));
             if (preg_match("#(?:/\\*|\\*/)#i", $_tmp)) {
                 $this->throwFatalError("You are not allowed to use comments in your SQL query.\nAdd \\ipsRegistry::DB()->allow_sub_select=1; before any query construct to allow them");
                 return false;
             }
             if (preg_match("#[^_a-zA-Z]union[^_a-zA-Z]#s", $_tmp)) {
                 $this->throwFatalError("UNION query joins are not allowed.\nAdd \\ipsRegistry::DB()->allow_sub_select=1; before any query construct to allow them");
                 return false;
             } else {
                 if (preg_match_all("#[^_a-zA-Z](select)[^_a-zA-Z]#s", $_tmp, $matches)) {
                     if (count($matches) > 1) {
                         $this->throwFatalError("SUB SELECT query joins are not allowed.\nAdd \\ipsRegistry::DB()->allow_sub_select=1; before any query construct to allow them");
                         return false;
                     }
                 }
             }
         }
     }
     //-----------------------------------------
     // Run the query
     //-----------------------------------------
     $this->_tmpQ = substr($the_query, 0, 100) . '...';
     $this->query_id = mysqli_query($this->connection_id, $the_query);
     //-----------------------------------------
     // Reset array...
     //-----------------------------------------
     $this->resetDataTypes();
     $this->allow_sub_select = false;
     if (!$this->query_id) {
         $this->throwFatalError("mySQL query error: {$the_query}");
     }
     //-----------------------------------------
     // Logging?
     //-----------------------------------------
     if ($this->obj['use_debug_log'] and $this->obj['debug_log'] or $this->obj['use_bad_log'] and $this->obj['bad_log'] or $this->obj['use_slow_log'] and $this->obj['slow_log']) {
         $endtime = IPSDebug::endTimer();
         $_data = '';
         if (preg_match("/^(?:\\()?select/i", $the_query)) {
             $eid = mysqli_query($this->connection_id, "EXPLAIN {$the_query}");
             $_bad = false;
             while ($array = mysqli_fetch_array($eid)) {
                 $array['extra'] = isset($array['extra']) ? $array['extra'] : '';
                 $_data .= "\n+------------------------------------------------------------------------------+";
                 $_data .= "\n|Table: " . $array['table'];
                 $_data .= "\n|Type: " . $array['type'];
                 $_data .= "\n|Possible Keys: " . $array['possible_keys'];
                 $_data .= "\n|Key: " . $array['key'];
                 $_data .= "\n|Key Len: " . $array['key_len'];
                 $_data .= "\n|Ref: " . $array['ref'];
                 $_data .= "\n|Rows: " . $array['rows'];
                 $_data .= "\n|Extra: " . $array['Extra'];
                 //$_data .= "\n+------------------------------------------------------------------------------+";
                 if ($this->obj['use_bad_log'] and $this->obj['bad_log'] and (stristr($array['Extra'], 'filesort') or stristr($array['Extra'], 'temporary'))) {
                     $this->writeDebugLog($the_query, $_data, $endtime, $this->obj['bad_log'], TRUE);
                 }
                 if ($this->obj['use_slow_log'] and $this->obj['slow_log'] and $endtime >= $this->obj['use_slow_log']) {
                     $this->writeDebugLog($the_query, $_data, $endtime, $this->obj['slow_log'], TRUE);
                 }
             }
             if ($this->obj['use_debug_log'] and $this->obj['debug_log']) {
                 $this->writeDebugLog($the_query, $_data, $endtime);
             }
         } else {
             if ($this->obj['use_debug_log'] and $this->obj['debug_log']) {
                 $this->writeDebugLog($the_query, $_data, $endtime);
             }
         }
     }
     //-----------------------------------------
     // Debugging?
     //-----------------------------------------
     if ($this->obj['debug']) {
         $endtime = IPSDebug::endTimer();
         $memoryUsed = IPSDebug::setMemoryDebugFlag('', $_MEMORY);
         $memory = '';
         $shutdown = $this->is_shutdown ? 'SHUTDOWN QUERY: ' : '';
         if (preg_match("/^(?:\\()?select/i", $the_query)) {
             $eid = mysqli_query($this->connection_id, "EXPLAIN {$the_query}");
             $this->debug_html .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FFE8F3' align='center'>\r\n\t\t\t\t\t\t\t\t\t\t   <tr>\r\n\t\t\t\t\t\t\t\t\t\t   \t <td colspan='8' style='font-size:14px' bgcolor='#FFC5Cb'><b>{$shutdown}Select Query</b></td>\r\n\t\t\t\t\t\t\t\t\t\t   </tr>\r\n\t\t\t\t\t\t\t\t\t\t   <tr>\r\n\t\t\t\t\t\t\t\t\t\t    <td colspan='8' style='font-family:courier, monaco, arial;font-size:14px;color:black'>{$the_query}</td>\r\n\t\t\t\t\t\t\t\t\t\t   </tr>\r\n\t\t\t\t\t\t\t\t\t\t   <tr bgcolor='#FFC5Cb'>\r\n\t\t\t\t\t\t\t\t\t\t\t <td><b>table</b></td><td><b>type</b></td><td><b>possible_keys</b></td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td><b>key</b></td><td><b>key_len</b></td><td><b>ref</b></td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td><b>rows</b></td><td><b>Extra</b></td>\r\n\t\t\t\t\t\t\t\t\t\t   </tr>\n";
             while ($array = mysqli_fetch_array($eid)) {
                 $type_col = '#FFFFFF';
                 if ($array['type'] == 'ref' or $array['type'] == 'eq_ref' or $array['type'] == 'const') {
                     $type_col = '#D8FFD4';
                 } else {
                     if ($array['type'] == 'ALL') {
                         $type_col = '#FFEEBA';
                     }
                 }
                 $this->debug_html .= "<tr bgcolor='#FFFFFF'>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['table']}&nbsp;</td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td bgcolor='{$type_col}'>{$array['type']}&nbsp;</td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['possible_keys']}&nbsp;</td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key']}&nbsp;</td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key_len']}&nbsp;</td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['ref']}&nbsp;</td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['rows']}&nbsp;</td>\r\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['Extra']}&nbsp;</td>\r\n\t\t\t\t\t\t\t\t\t\t   </tr>\n";
             }
             $this->sql_time += $endtime;
             if ($endtime > 0.1) {
                 $endtime = "<span style='color:red'><b>{$endtime}</b></span>";
             }
             if ($memoryUsed) {
                 $memory = '<br />Memory Used: ' . IPSLib::sizeFormat($memoryUsed, TRUE);
             }
             $this->debug_html .= "<tr>\r\n\t\t\t\t\t\t\t\t\t\t  <td colspan='8' bgcolor='#FFD6DC' style='font-size:14px'><b>MySQL time</b>: {$endtime}{$memory}</b></td>\r\n\t\t\t\t\t\t\t\t\t\t  </tr>\r\n\t\t\t\t\t\t\t\t\t\t  </table>\n<br />\n";
         } else {
             $this->debug_html .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FEFEFE'  align='center'>\r\n\t\t\t\t\t\t\t\t\t\t <tr>\r\n\t\t\t\t\t\t\t\t\t\t  <td style='font-size:14px' bgcolor='#EFEFEF'><b>{$shutdown}Non Select Query</b></td>\r\n\t\t\t\t\t\t\t\t\t\t </tr>\r\n\t\t\t\t\t\t\t\t\t\t <tr>\r\n\t\t\t\t\t\t\t\t\t\t  <td style='font-family:courier, monaco, arial;font-size:14px'>{$the_query}</td>\r\n\t\t\t\t\t\t\t\t\t\t </tr>\r\n\t\t\t\t\t\t\t\t\t\t <tr>\r\n\t\t\t\t\t\t\t\t\t\t  <td style='font-size:14px' bgcolor='#EFEFEF'><b>MySQL time</b>: {$endtime}</span></td>\r\n\t\t\t\t\t\t\t\t\t\t </tr>\r\n\t\t\t\t\t\t\t\t\t\t</table><br />\n\n";
         }
     }
     $this->query_count++;
     $this->obj['cached_queries'][] = $the_query;
     return $this->query_id;
 }
 /**
  * Fetches the output
  *
  * @access	public
  * @param	string		Output gathered
  * @param	string		Title of the document
  * @param	array 		Navigation gathered
  * @param	array 		Array of document head items
  * @param	array 		Array of JS loader items
  * @param	array 		Array of extra data
  * @return	string		Output to be printed to the client
  */
 public function fetchOutput($output, $title, $navigation, $documentHeadItems, $jsLoaderItems, $extraData = array())
 {
     //-----------------------------------------
     // INIT
     //-----------------------------------------
     $system_vars_cache = $this->caches['systemvars'];
     $pmData = FALSE;
     $notificationLatest = array();
     //-----------------------------------------
     // NORMAL
     //-----------------------------------------
     if ($this->_outputType == 'normal') {
         //-----------------------------------------
         // Do we have a notification show?
         //-----------------------------------------
         if (!empty($this->memberData['msg_show_notification']) and $this->memberData['_cache']['show_notification_popup']) {
             if (!$this->settings['board_offline'] or $this->memberData['g_access_offline']) {
                 if (strpos(ipsRegistry::$settings['query_string_real'], 'module=messaging') === false) {
                     IPSMember::save($this->memberData['member_id'], array('core' => array('msg_show_notification' => 0)));
                     /* Grab inline notifications... */
                     $classToLoad = IPSLib::loadLibrary(IPS_ROOT_PATH . '/sources/classes/member/notifications.php', 'notifications');
                     $notifyLibrary = new $classToLoad($this->registry);
                     $notifyLibrary->setMember($this->memberData);
                     $tmp = $notifyLibrary->getLatestNotificationForInlinePopUp();
                     $weNeed = array('notify_title', 'member_member_id', 'member_PhotoTag', 'member_members_display_name', 'date_parsed', 'title', 'url', 'type', 'content', 'member_HoverCard');
                     foreach ($weNeed as $k) {
                         $notificationLatest[$k] = $tmp[$k];
                     }
                 }
             }
         }
         //-----------------------------------------
         // Add identifier URL
         //-----------------------------------------
         $http = 'http://';
         if (strpos($this->settings['board_url'], 'https://') === 0) {
             $http = 'https://';
         }
         $this->addMetaTag('identifier-url', $http . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
         //-----------------------------------------
         // Add in task image?
         //-----------------------------------------
         $task = '';
         $system_vars_cache['task_next_run'] = isset($system_vars_cache['task_next_run']) ? $system_vars_cache['task_next_run'] : 0;
         if (!$this->settings['task_use_cron'] and time() >= $system_vars_cache['task_next_run'] or defined('FORCE_TASK_KEY')) {
             $_url = !$this->registry->getClass('output')->isHTTPS ? $this->settings['base_url'] : $this->settings['base_url_https'];
             $task = "<div><img src='" . $_url . "app=core&amp;module=task' alt='' style='border: 0px;height:1px;width:1px;' /></div>";
         }
         //-----------------------------------------
         // Grab output
         //-----------------------------------------
         /* Inline msg */
         $inlineMsg = $this->member->sessionClass()->getInlineMessage();
         $templateName = 'globalTemplate';
         $templateGroup = 'global';
         if ($this->useMinimalWrapper) {
             $templateName = 'globalTemplateMinimal';
             $templateGroup = 'global_other';
         }
         /* Do pagination */
         if (strstr($title, '<%pageNumber%>')) {
             $replace = $this->_current_page_title ? ' ' . $this->lang->words['page_title_page'] . ' ' . $this->_current_page_title : '';
             $title = str_replace('<%pageNumber%>', $replace, $title);
         } else {
             if ($this->_current_page_title) {
                 $title = $title . ' ' . $this->lang->words['page_title_page'] . ' ' . $this->_current_page_title;
             }
         }
         $finalOutput = $this->output->getTemplate($templateGroup)->{$templateName}($output, $documentHeadItems, $this->_css, $jsLoaderItems, $this->_metaTags, array('title' => $title, 'applications' => $this->core_fetchApplicationData(), 'page' => $this->_current_page_title, 'notifications' => $notificationLatest ? IPSText::jsonEncodeForTemplate($notificationLatest) : '', 'inlineMsg' => $inlineMsg), array('navigation' => $navigation, 'adHeaderCode' => !empty($extraData['adHeaderCode']) ? $extraData['adHeaderCode'] : '', 'adFooterCode' => !empty($extraData['adFooterCode']) ? $extraData['adFooterCode'] : ''), array('time' => $this->registry->getClass('class_localization')->getDate(time(), 'SHORT', 1), 'mark_read_apps' => IPSLib::getEnabledApplications('itemMarking'), 'lang_chooser' => $this->html_buildLanguageDropDown(), 'skin_chooser' => $this->html_fetchSetsDropDown(), 'copyright' => $this->html_fetchCopyright()), array('ex_time' => (isset($this->request['faster']) and $this->request['faster'] == 'yes') ? $this->_getFasterText() : sprintf("%.4f", IPSDebug::endTimer()), 'gzip_status' => $this->settings['disable_gzip'] == 1 ? $this->lang->words['gzip_off'] : $this->lang->words['gzip_on'], 'server_load' => ipsRegistry::$server_load, 'queries' => $this->DB->getQueryCount(), 'task' => $task));
     } else {
         if ($this->_outputType == 'redirect') {
             $extraData['full'] = 1;
             # SEO?
             if ($extraData['seoTitle']) {
                 $extraData['url'] = $this->output->buildSEOUrl($extraData['url'], 'none', $extraData['seoTitle'], $extraData['seoTemplate']);
             }
             $finalOutput = $this->output->getTemplate('global_other')->redirectTemplate($documentHeadItems, $this->_css, $jsLoaderItems, $extraData['text'], $extraData['url'], $extraData['full']);
         } else {
             if ($this->_outputType == 'popup') {
                 $finalOutput = $this->output->getTemplate('global_other')->displayPopUpWindow($documentHeadItems, $this->_css, $jsLoaderItems, $title, $output);
             }
         }
     }
     //-----------------------------------------
     // Set a class on the body for print
     //-----------------------------------------
     if ($this->_printOnly) {
         $finalOutput = str_replace("<body", "<body class='printpreview'", $finalOutput);
     }
     //-----------------------------------------
     // Return
     //-----------------------------------------
     return $finalOutput;
 }
 /**
  * Fetches the output
  *
  * @access	public
  * @param	string		Output gathered
  * @param	string		Title of the document
  * @param	array 		Navigation gathered
  * @param	array 		Array of document head items
  * @param	array 		Array of JS loader items
  * @param	array 		Array of extra data
  * @return	string		Output to be printed to the client
  */
 public function fetchOutput($output, $title, $navigation, $documentHeadItems, $jsLoaderItems, $extraData = array())
 {
     //-----------------------------------------
     // INIT
     //-----------------------------------------
     $system_vars_cache = $this->caches['systemvars'];
     $showPMBox = '';
     $currentCharSet = $this->settings['gb_char_set'];
     /* Force UTF-8 for the skin */
     $this->settings['gb_char_set'] = 'UTF-8';
     //-----------------------------------------
     // NORMAL
     //-----------------------------------------
     if ($this->_outputType == 'normal') {
         //-----------------------------------------
         // Grab output
         //-----------------------------------------
         $finalOutput = $this->output->getTemplate('global')->globalTemplate($output, $documentHeadItems, $this->_css, $jsLoaderItems, $this->_metaTags, array('title' => $title, 'applications' => $this->core_fetchApplicationData(), 'page' => $this->_current_page_title), array('navigation' => $navigation), array('time' => $this->registry->getClass('class_localization')->getDate(time(), 'SHORT', 1), 'lang_chooser' => $this->html_buildLanguageDropDown(), 'skin_chooser' => $this->html_fetchSetsDropDown(), 'stats' => $this->html_showDebugInfo(), 'copyright' => $this->html_fetchCopyright()), array('ex_time' => sprintf("%.4f", IPSDebug::endTimer()), 'gzip_status' => $this->settings['disable_gzip'] == 1 ? $this->lang->words['gzip_off'] : $this->lang->words['gzip_on'], 'server_load' => ipsRegistry::$server_load, 'queries' => $this->DB->getQueryCount()));
     } else {
         if ($this->_outputType == 'redirect') {
             # SEO?
             if ($extraData['seoTitle']) {
                 $extraData['url'] = $this->output->buildSEOUrl($extraData['url'], 'none', $extraData['seoTitle']);
                 $extraData['full'] = 1;
             }
             $finalOutput = $this->output->getTemplate('global')->redirectTemplate($documentHeadItems, $this->_css, $jsLoaderItems, $extraData['text'], $extraData['url'], $extraData['full']);
         } else {
             if ($this->_outputType == 'popup') {
                 $finalOutput = $this->output->getTemplate('global')->displayPopUpWindow($documentHeadItems, $this->_css, $jsLoaderItems, $title, $output);
             }
         }
     }
     //-----------------------------------------
     // Return
     //-----------------------------------------
     $finalOutput = $this->parseIPSTags($finalOutput);
     /* Attempt to clean HTML */
     return IPSText::stripNonUtf8(IPSText::convertCharsets($finalOutput, $currentCharSet, 'UTF-8'));
 }
 /**
  * Fetches the output
  *
  * @access	public
  * @param	string		Output gathered
  * @param	string		Title of the document
  * @param	array 		Navigation gathered
  * @param	array 		Array of document head items
  * @param	array 		Array of JS loader items
  * @param	array 		Array of extra data
  * @return	string		Output to be printed to the client
  */
 public function fetchOutput($output, $title, $navigation, $documentHeadItems, $jsLoaderItems, $extraData = array())
 {
     //-----------------------------------------
     // INIT
     //-----------------------------------------
     $system_vars_cache = $this->caches['systemvars'];
     $pmData = FALSE;
     //-----------------------------------------
     // NORMAL
     //-----------------------------------------
     if ($this->_outputType == 'normal') {
         //-----------------------------------------
         // Do we have a PM show?
         //-----------------------------------------
         if (($this->memberData['msg_count_reset'] or $this->memberData['msg_show_notification']) and !$this->memberData['members_disable_pm']) {
             IPSMember::save($this->memberData['member_id'], array('core' => array('msg_show_notification' => 0)));
             if ($this->request['module'] != 'messaging' and (!$this->settings['board_offline'] or $this->memberData['g_access_offline'])) {
                 /* Grab PM Data. We init if we need to recount... */
                 require_once IPSLib::getAppDir("members") . '/sources/classes/messaging/messengerFunctions.php';
                 $messengerFunctions = new messengerFunctions($this->registry);
                 /* Only collect data if we have notifications to show */
                 if ($this->memberData['msg_show_notification']) {
                     $_data = $messengerFunctions->fetchUnreadNotifications($this->memberData['member_id']);
                     if (count($_data)) {
                         $pmData = array_shift($_data);
                     }
                 }
             }
         }
         //-----------------------------------------
         // Add identifier URL
         //-----------------------------------------
         $this->addMetaTag('identifier-url', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
         //-----------------------------------------
         // Add in task image?
         //-----------------------------------------
         $task = '';
         $system_vars_cache['task_next_run'] = isset($system_vars_cache['task_next_run']) ? $system_vars_cache['task_next_run'] : 0;
         if (time() >= $system_vars_cache['task_next_run']) {
             $_url = !$this->registry->getClass('output')->isHTTPS ? $this->settings['base_url'] : $this->settings['base_url_https'];
             $task = "<div><img src='" . $_url . "app=core&amp;module=task' alt='' style='border: 0px;height:1px;width:1px;' /></div>";
         }
         //-----------------------------------------
         // Grab output
         //-----------------------------------------
         $finalOutput = $this->output->getTemplate('global')->globalTemplate($output, $documentHeadItems, $this->_css, $jsLoaderItems, $this->_metaTags, array('title' => $title, 'applications' => $this->core_fetchApplicationData(), 'page' => $this->_current_page_title), array('navigation' => $navigation, 'pmData' => $pmData), array('time' => $this->registry->getClass('class_localization')->getDate(time(), 'SHORT', 1), 'lang_chooser' => $this->html_buildLanguageDropDown(), 'skin_chooser' => $this->html_fetchSetsDropDown(), 'copyright' => $this->html_fetchCopyright()), array('ex_time' => sprintf("%.4f", IPSDebug::endTimer()), 'gzip_status' => $this->settings['disable_gzip'] == 1 ? $this->lang->words['gzip_off'] : $this->lang->words['gzip_on'], 'server_load' => ipsRegistry::$server_load, 'queries' => $this->DB->getQueryCount(), 'task' => $task));
     } else {
         if ($this->_outputType == 'redirect') {
             $extraData['full'] = 1;
             # SEO?
             if ($extraData['seoTitle']) {
                 $extraData['url'] = $this->output->buildSEOUrl($extraData['url'], 'none', $extraData['seoTitle']);
             }
             $finalOutput = $this->output->getTemplate('global_other')->redirectTemplate($documentHeadItems, $this->_css, $jsLoaderItems, $extraData['text'], $extraData['url'], $extraData['full']);
         } else {
             if ($this->_outputType == 'popup') {
                 $finalOutput = $this->output->getTemplate('global_other')->displayPopUpWindow($documentHeadItems, $this->_css, $jsLoaderItems, $title, $output);
             }
         }
     }
     //-----------------------------------------
     // Return
     //-----------------------------------------
     //print IPSLib::sizeFormat( IPSLib::strlenToBytes( strlen( $finalOutput ) ) );
     return $this->parseIPSTags($finalOutput);
 }
 /**
  * Execute a direct database query
  *
  * @access	public
  * @param	string		Database query
  * @param	boolean		[Optional] Do not convert table prefix
  * @return	resource	Query id
  */
 public function query($the_query, $bypass = false)
 {
     //-----------------------------------------
     // Change the table prefix if needed
     //-----------------------------------------
     if ($this->no_prefix_convert) {
         $bypass = 1;
     }
     if (!$bypass) {
         if ($this->obj['sql_tbl_prefix'] != "ibf_" and !$this->prefix_changed) {
             //$the_query = preg_replace("/\sibf_(\S+?)([\s\.,]|$)/", " ".$this->obj['sql_tbl_prefix']."\\1\\2", $the_query);
         }
     }
     //-----------------------------------------
     // Debug?
     //-----------------------------------------
     if ($this->obj['debug'] or $this->obj['use_debug_log'] and $this->obj['debug_log']) {
         IPSDebug::startTimer();
     }
     //-----------------------------------------
     // Stop sub selects? (UNION)
     //-----------------------------------------
     if (!IPS_DB_ALLOW_SUB_SELECTS) {
         # On the spot allowance?
         if (!$this->allow_sub_select) {
             $_tmp = strtolower($this->_removeAllQuotes($the_query));
             if (preg_match("#(?:/\\*|\\*/)#i", $_tmp)) {
                 $this->throwFatalError("You are not allowed to use comments in your SQL query.\nAdd \\ipsRegistry::DB()->allow_sub_select=1; before any query construct to allow them\n{$the_query}");
                 return false;
             }
             if (preg_match("#[^_a-zA-Z]union[^_a-zA-Z]#s", $_tmp)) {
                 $this->throwFatalError("UNION query joins are not allowed.\nAdd \\ipsRegistry::DB()->allow_sub_select=1; before any query construct to allow them\n{$the_query}");
                 return false;
             } else {
                 if (preg_match_all("#[^_a-zA-Z](select)[^_a-zA-Z]#s", $_tmp, $matches)) {
                     if (count($matches) > 1) {
                         $this->throwFatalError("SUB SELECT query joins are not allowed.\nAdd \\ipsRegistry::DB()->allow_sub_select=1; before any query construct to allow them\n{$the_query}");
                         return false;
                     }
                 }
             }
         }
     }
     //-----------------------------------------
     // Run the query
     //-----------------------------------------
     #I had to switch this around... The query goes first, connection id second. Otherwise it just breaks - KF
     #$this->query_id = mysql_query($this->connection_id, $the_query );
     $this->query_id = mysql_query($the_query, $this->connection_id);
     //-----------------------------------------
     // Reset array...
     //-----------------------------------------
     $this->force_data_type = array();
     $this->allow_sub_select = false;
     if (!$this->query_id) {
         $this->throwFatalError("mySQL query error: {$the_query}");
     }
     //-----------------------------------------
     // Debug?
     //-----------------------------------------
     if ($this->obj['use_debug_log'] and $this->obj['debug_log']) {
         $endtime = IPSDebug::endTimer();
         if (preg_match("/^(?:\\()?select/i", $the_query)) {
             $eid = mysql_query("EXPLAIN {$the_query}", $this->connection_id);
             while ($array = mysql_fetch_array($eid)) {
                 $_data .= "\n+------------------------------------------------------------------------------+";
                 $_data .= "\n|Table: " . $array['table'];
                 $_data .= "\n|Type: " . $array['type'];
                 $_data .= "\n|Possible Keys: " . $array['possible_keys'];
                 $_data .= "\n|Key: " . $array['key'];
                 $_data .= "\n|Key Len: " . $array['key_len'];
                 $_data .= "\n|Ref: " . $array['ref'];
                 $_data .= "\n|Rows: " . $array['rows'];
                 $_data .= "\n|Extra: " . $array['extra'];
                 $_data .= "\n+------------------------------------------------------------------------------+";
             }
             $this->writeDebugLog($the_query, $_data, $endtime);
         } else {
             $this->writeDebugLog($the_query, $_data, $endtime);
         }
     } else {
         if ($this->obj['debug']) {
             $endtime = IPSDebug::endTimer();
             $shutdown = $this->is_shutdown ? 'SHUTDOWN QUERY: ' : '';
             if (preg_match("/^(?:\\()?select/i", $the_query)) {
                 $eid = mysql_query("EXPLAIN {$the_query}", $this->connection_id);
                 $this->debug_html .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FFE8F3' align='center'>\n\t\t\t\t\t\t\t\t\t\t   <tr>\n\t\t\t\t\t\t\t\t\t\t   \t <td colspan='8' style='font-size:14px' bgcolor='#FFC5Cb'><b>{$shutdown}Select Query</b></td>\n\t\t\t\t\t\t\t\t\t\t   </tr>\n\t\t\t\t\t\t\t\t\t\t   <tr>\n\t\t\t\t\t\t\t\t\t\t    <td colspan='8' style='font-family:courier, monaco, arial;font-size:14px;color:black'>{$the_query}</td>\n\t\t\t\t\t\t\t\t\t\t   </tr>\n\t\t\t\t\t\t\t\t\t\t   <tr bgcolor='#FFC5Cb'>\n\t\t\t\t\t\t\t\t\t\t\t <td><b>table</b></td><td><b>type</b></td><td><b>possible_keys</b></td>\n\t\t\t\t\t\t\t\t\t\t\t <td><b>key</b></td><td><b>key_len</b></td><td><b>ref</b></td>\n\t\t\t\t\t\t\t\t\t\t\t <td><b>rows</b></td><td><b>Extra</b></td>\n\t\t\t\t\t\t\t\t\t\t   </tr>\n";
                 while ($array = mysql_fetch_array($eid)) {
                     $type_col = '#FFFFFF';
                     if ($array['type'] == 'ref' or $array['type'] == 'eq_ref' or $array['type'] == 'const') {
                         $type_col = '#D8FFD4';
                     } else {
                         if ($array['type'] == 'ALL') {
                             $type_col = '#FFEEBA';
                         }
                     }
                     $this->debug_html .= "<tr bgcolor='#FFFFFF'>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['table']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td bgcolor='{$type_col}'>{$array['type']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['possible_keys']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key_len']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['ref']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['rows']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['Extra']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t   </tr>\n";
                 }
                 $this->sql_time += $endtime;
                 if ($endtime > 0.1) {
                     $endtime = "<span style='color:red'><b>{$endtime}</b></span>";
                 }
                 $this->debug_html .= "<tr>\n\t\t\t\t\t\t\t\t\t\t  <td colspan='8' bgcolor='#FFD6DC' style='font-size:14px'><b>MySQL time</b>: {$endtime}</b></td>\n\t\t\t\t\t\t\t\t\t\t  </tr>\n\t\t\t\t\t\t\t\t\t\t  </table>\n<br />\n";
             } else {
                 $this->debug_html .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FEFEFE'  align='center'>\n\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t  <td style='font-size:14px' bgcolor='#EFEFEF'><b>{$shutdown}Non Select Query</b></td>\n\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t  <td style='font-family:courier, monaco, arial;font-size:14px'>{$the_query}</td>\n\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t  <td style='font-size:14px' bgcolor='#EFEFEF'><b>MySQL time</b>: {$endtime}</span></td>\n\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t</table><br />\n\n";
             }
         }
     }
     $this->query_count++;
     $this->obj['cached_queries'][] = $the_query;
     return $this->query_id;
 }
 /**
  * Execute a direct database query
  *
  * @param	string		Database query
  * @param	boolean		[Optional] Do not convert table prefix
  * @return	@e resource
  */
 public function query($the_query, $bypass = false)
 {
     //-----------------------------------------
     // Debug?
     //-----------------------------------------
     if ($this->obj['debug'] or $this->obj['use_debug_log'] and $this->obj['debug_log']) {
         IPSDebug::startTimer();
     }
     //-----------------------------------------
     // Stop sub selects? (UNION)
     //-----------------------------------------
     if (!IPS_DB_ALLOW_SUB_SELECTS) {
         # On the spot allowance?
         if (!$this->allow_sub_select) {
             $_tmp = strtolower($this->_removeAllQuotes($the_query));
             if (preg_match("#(?:/\\*|\\*/)#i", $_tmp)) {
                 $this->throwFatalError("Не разрешается использовать комментарии в SQL запросе.\nДобавьте \\ipsRegistry::DB()->allow_sub_select=1; перед запросом, чтобы разрешить их\n{$the_query}");
                 return false;
             }
             if (preg_match("#[^_a-zA-Z]union[^_a-zA-Z]#s", $_tmp)) {
                 $this->throwFatalError("Использование UNION в запросах запрещено.\nДобавьте \\ipsRegistry::DB()->allow_sub_select=1; перед запросом, чтобы разрешить использование\n{$the_query}");
                 return false;
             } else {
                 if (preg_match_all("#[^_a-zA-Z](select)[^_a-zA-Z]#s", $_tmp, $matches)) {
                     if (count($matches) > 1) {
                         $this->throwFatalError("Вложенные SELECT в запросах запрещены.\nДобавьте \\ipsRegistry::DB()->allow_sub_select=1; перед запросом, чтобы разрешить их\n{$the_query}");
                         return false;
                     }
                 }
             }
         }
     }
     //-----------------------------------------
     // Run the query
     //-----------------------------------------
     #I had to switch this around... The query goes first, connection id second. Otherwise it just breaks - KF
     #$this->query_id = mysql_query($this->connection_id, $the_query );
     $this->query_id = mysql_query($the_query, $this->connection_id);
     //-----------------------------------------
     // Reset array...
     //-----------------------------------------
     $this->resetDataTypes();
     $this->allow_sub_select = false;
     if (!$this->query_id) {
         $this->throwFatalError("mySQL query error: {$the_query}");
     }
     //-----------------------------------------
     // Debug?
     //-----------------------------------------
     if ($this->obj['use_debug_log'] and $this->obj['debug_log'] or $this->obj['use_bad_log'] and $this->obj['bad_log'] or $this->obj['use_slow_log'] and $this->obj['slow_log']) {
         $endtime = IPSDebug::endTimer();
         $_data = '';
         if (preg_match("/^(?:\\()?select/i", $the_query)) {
             $eid = mysql_query("EXPLAIN {$the_query}", $this->connection_id);
             $_bad = false;
             while ($array = mysql_fetch_array($eid)) {
                 $array['extra'] = isset($array['extra']) ? $array['extra'] : '';
                 $_data .= "\n+------------------------------------------------------------------------------+";
                 $_data .= "\n|Table: " . $array['table'];
                 $_data .= "\n|Type: " . $array['type'];
                 $_data .= "\n|Possible Keys: " . $array['possible_keys'];
                 $_data .= "\n|Key: " . $array['key'];
                 $_data .= "\n|Key Len: " . $array['key_len'];
                 $_data .= "\n|Ref: " . $array['ref'];
                 $_data .= "\n|Rows: " . $array['rows'];
                 $_data .= "\n|Extra: " . $array['Extra'];
                 //$_data .= "\n+------------------------------------------------------------------------------+";
                 if ($this->obj['use_bad_log'] and $this->obj['bad_log'] and (stristr($array['Extra'], 'filesort') or stristr($array['Extra'], 'temporary'))) {
                     $this->writeDebugLog($the_query, $_data, $endtime, $this->obj['bad_log'], TRUE);
                 }
                 if ($this->obj['use_slow_log'] and $this->obj['slow_log'] and $endtime >= $this->obj['use_slow_log']) {
                     $this->writeDebugLog($the_query, $_data, $endtime, $this->obj['slow_log'], TRUE);
                 }
             }
             if ($this->obj['use_debug_log'] and $this->obj['debug_log']) {
                 $this->writeDebugLog($the_query, $_data, $endtime);
             }
         } else {
             if ($this->obj['use_debug_log'] and $this->obj['debug_log']) {
                 $this->writeDebugLog($the_query, $_data, $endtime);
             }
         }
     }
     //-----------------------------------------
     // Debugging?
     //-----------------------------------------
     if ($this->obj['debug']) {
         $endtime = IPSDebug::endTimer();
         $shutdown = $this->is_shutdown ? 'SHUTDOWN QUERY: ' : '';
         if (preg_match("/^(?:\\()?select/i", $the_query)) {
             $eid = mysql_query("EXPLAIN {$the_query}", $this->connection_id);
             $this->debug_html .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FFE8F3' align='center'>\n\t\t\t\t\t\t\t\t\t\t   <tr>\n\t\t\t\t\t\t\t\t\t\t   \t <td colspan='8' style='font-size:14px' bgcolor='#FFC5Cb'><b>{$shutdown}Select Query</b></td>\n\t\t\t\t\t\t\t\t\t\t   </tr>\n\t\t\t\t\t\t\t\t\t\t   <tr>\n\t\t\t\t\t\t\t\t\t\t    <td colspan='8' style='font-family:courier, monaco, arial;font-size:14px;color:black'>{$the_query}</td>\n\t\t\t\t\t\t\t\t\t\t   </tr>\n\t\t\t\t\t\t\t\t\t\t   <tr bgcolor='#FFC5Cb'>\n\t\t\t\t\t\t\t\t\t\t\t <td><b>table</b></td><td><b>type</b></td><td><b>possible_keys</b></td>\n\t\t\t\t\t\t\t\t\t\t\t <td><b>key</b></td><td><b>key_len</b></td><td><b>ref</b></td>\n\t\t\t\t\t\t\t\t\t\t\t <td><b>rows</b></td><td><b>Extra</b></td>\n\t\t\t\t\t\t\t\t\t\t   </tr>\n";
             while ($array = mysql_fetch_array($eid)) {
                 $type_col = '#FFFFFF';
                 if ($array['type'] == 'ref' or $array['type'] == 'eq_ref' or $array['type'] == 'const') {
                     $type_col = '#D8FFD4';
                 } else {
                     if ($array['type'] == 'ALL') {
                         $type_col = '#FFEEBA';
                     }
                 }
                 $this->debug_html .= "<tr bgcolor='#FFFFFF'>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['table']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td bgcolor='{$type_col}'>{$array['type']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['possible_keys']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['key_len']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['ref']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['rows']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t\t <td>{$array['Extra']}&nbsp;</td>\n\t\t\t\t\t\t\t\t\t\t   </tr>\n";
             }
             $this->sql_time += $endtime;
             if ($endtime > 0.1) {
                 $endtime = "<span style='color:red'><b>{$endtime}</b></span>";
             }
             $this->debug_html .= "<tr>\n\t\t\t\t\t\t\t\t\t\t  <td colspan='8' bgcolor='#FFD6DC' style='font-size:14px'><b>MySQL time</b>: {$endtime}</b></td>\n\t\t\t\t\t\t\t\t\t\t  </tr>\n\t\t\t\t\t\t\t\t\t\t  </table>\n<br />\n";
         } else {
             $this->debug_html .= "<table width='95%' border='1' cellpadding='6' cellspacing='0' bgcolor='#FEFEFE'  align='center'>\n\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t  <td style='font-size:14px' bgcolor='#EFEFEF'><b>{$shutdown}Non Select Query</b></td>\n\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t  <td style='font-family:courier, monaco, arial;font-size:14px'>{$the_query}</td>\n\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t <tr>\n\t\t\t\t\t\t\t\t\t\t  <td style='font-size:14px' bgcolor='#EFEFEF'><b>MySQL time</b>: {$endtime}</span></td>\n\t\t\t\t\t\t\t\t\t\t </tr>\n\t\t\t\t\t\t\t\t\t\t</table><br />\n\n";
         }
     }
     $this->query_count++;
     $this->obj['cached_queries'][] = $the_query;
     return $this->query_id;
 }
 /**
  * Fetches the output
  *
  * @access	public
  * @param	string		Output gathered
  * @param	string		Title of the document
  * @param	array 		Navigation gathered
  * @param	array 		Array of document head items
  * @param	array 		Array of JS loader items
  * @param	array 		Array of extra data
  * @return	string		Output to be printed to the client
  */
 public function fetchOutput($output, $title, $navigation, $documentHeadItems, $jsLoaderItems, $extraData = array())
 {
     //-----------------------------------------
     // INIT
     //-----------------------------------------
     $system_vars_cache = $this->caches['systemvars'];
     $showPMBox = '';
     //-----------------------------------------
     // NORMAL
     //-----------------------------------------
     if ($this->_outputType == 'normal') {
         //-----------------------------------------
         // Grab output
         //-----------------------------------------
         $finalOutput = $this->output->getTemplate('global')->globalTemplate($output, $documentHeadItems, $this->_css, $jsLoaderItems, array('title' => $title, 'applications' => $this->core_fetchApplicationData(), 'page' => $this->_current_page_title), array('navigation' => $navigation, 'pmData' => $pmData, 'showReportBox' => $memberCache['report_last_updated'] < $reportsCache['last_updated'] && $memberCache['report_num'] > 0 ? 1 : 0), array('time' => $this->registry->getClass('class_localization')->getDate(time(), 'SHORT', 1), 'lang_chooser' => $this->html_buildLanguageDropDown(), 'skin_chooser' => $this->html_fetchSetsDropDown(), 'stats' => $this->html_showDebugInfo(), 'copyright' => $this->html_fetchCopyright()), array('ex_time' => sprintf("%.4f", IPSDebug::endTimer()), 'gzip_status' => $this->settings['disable_gzip'] == 1 ? $this->lang->words['gzip_off'] : $this->lang->words['gzip_on'], 'server_load' => ipsRegistry::$server_load, 'queries' => $this->DB->getQueryCount()));
     } else {
         if ($this->_outputType == 'redirect') {
             # SEO?
             if ($extraData['seoTitle']) {
                 $extraData['url'] = $this->output->buildSEOUrl($extraData['url'], 'none', $extraData['seoTitle']);
                 $extraData['full'] = 1;
             }
             $finalOutput = $this->output->getTemplate('global')->redirectTemplate($documentHeadItems, $this->_css, $jsLoaderItems, $extraData['text'], $extraData['url'], $extraData['full']);
         } else {
             if ($this->_outputType == 'popup') {
                 $finalOutput = $this->output->getTemplate('global')->displayPopUpWindow($documentHeadItems, $this->_css, $jsLoaderItems, $title, $output);
             }
         }
     }
     //-----------------------------------------
     // Return
     //-----------------------------------------
     return $this->parseIPSTags($finalOutput);
 }