(.*?)<\/noinclude>/is', '', $message);
+
+ return $message;
+ }
+ function get_pageid_from_url()
+ {
+ if(isset($_GET['title']))
+ {
+ if( $_GET['title'] == '' && getConfig('main_page') != '' )
+ {
+ $this->main_page();
+ }
+ if(strstr($_GET['title'], ' '))
+ {
+ $loc = urldecode(rawurldecode($_SERVER['REQUEST_URI']));
+ $loc = str_replace(' ', '_', $loc);
+ $loc = str_replace('+', '_', $loc);
+ header('Location: '.$loc);
+ exit;
+ }
+ $ret = $_GET['title'];
+ }
+ elseif(isset($_SERVER['PATH_INFO']))
+ {
+ $pi = explode('/', $_SERVER['PATH_INFO']);
+
+ if(!isset($pi[1]) || (isset($pi[1]) && $pi[1] == ''))
+ {
+ return false;
+ }
+
+ if(strstr($pi[1], ' '))
+ {
+ $loc = urldecode(rawurldecode($_SERVER['REQUEST_URI']));
+ $loc = str_replace(' ', '_', $loc);
+ $loc = str_replace('+', '_', $loc);
+ header('Location: '.$loc);
+ exit;
+ }
+ if( !( substr($pi[1], 0, strlen($this->nslist['Special'])) == $this->nslist['Special'] ) )
+ {
+ unset($pi[0]);
+ $pi[1] = implode('/', $pi);
+ }
+ $ret = $pi[1];
+ }
+ else
+ {
+ $k = array_keys($_GET);
+ foreach($k as $c)
+ {
+ if(substr($c, 0, 1) == '/')
+ {
+ $ret = substr($c, 1, strlen($c));
+ if(substr($ret, 0, strlen($this->nslist['Special'])) == $this->nslist['Special'] ||
+ substr($ret, 0, strlen($this->nslist['Admin'])) == $this->nslist['Admin'])
+ {
+ $ret = explode('/', $ret);
+ $ret = $ret[0];
+ }
+ break;
+ }
+ }
+ }
+
+ return ( isset($ret) ) ? $ret : false;
+ }
+ // Parses a (very carefully formed) array into Javascript code compatible with the Tigra Tree Menu used in the admin menu
+ function parseAdminTree()
+ {
+ $k = array_keys($this->admin_tree);
+ $i = 0;
+ $ret = '';
+ $ret .= "var TREE_ITEMS = [\n ['Administration panel home', 'javascript:ajaxPage(\'".$this->nslist['Admin']."Home\');',\n ";
+ foreach($k as $key)
+ {
+ $i++;
+ $ret .= "['".$key."', 'javascript:trees[0].toggle($i)', \n";
+ foreach($this->admin_tree[$key] as $c)
+ {
+ $i++;
+ $ret .= " ['".$c['name']."', 'javascript:ajaxPage(\\'".$this->nslist['Admin'].$c['pageid']."\\');'],\n";
+ }
+ $ret .= " ],\n";
+ }
+ $ret .= " ['Log out of admin panel', 'javascript:ajaxPage(\\'".$this->nslist['Admin']."AdminLogout\\');'],\n";
+ // I used this while I painstakingly wrote the Runt code that auto-expands certain nodes based on the value of a bitfield stored in a cookie. *shudders*
+ // $ret .= " ['(debug) Clear menu bitfield', 'javascript:createCookie(\\'admin_menu_state\\', \\'1\\', 365);'],\n";
+ $ret .= "]\n];";
+ return $ret;
+ }
+ function addAdminNode($section, $page_title, $url)
+ {
+ if(!isset($this->admin_tree[$section]))
+ {
+ $this->admin_tree[$section] = Array();
+ }
+ $this->admin_tree[$section][] = Array(
+ 'name'=>$page_title,
+ 'pageid'=>$url
+ );
+ }
+ function getParam($id = 0)
+ {
+ if(isset($_SERVER['PATH_INFO']))
+ {
+ $pi = explode('/', $_SERVER['PATH_INFO']);
+ $id = $id + 2;
+ return isset($pi[$id]) ? $pi[$id] : false;
+ }
+ else if( isset($_GET['title']) )
+ {
+ $pi = explode('/', $_GET['title']);
+ $id = $id + 1;
+ return isset($pi[$id]) ? $pi[$id] : false;
+ }
+ else
+ {
+ $k = array_keys($_GET);
+ foreach($k as $c)
+ {
+ if(substr($c, 0, 1) == '/')
+ {
+
+ // Bugfix for apache somehow passing dots as underscores
+ global $mime_types;
+ $exts = array_keys($mime_types);
+ $exts = '(' . implode('|', $exts) . ')';
+ if ( preg_match( '#_'.$exts.'#i', $c ) )
+ $c = preg_replace( '#_'.$exts.'#i', '.\\1', $c );
+
+ $pi = explode('/', $c);
+ $id = $id + 2;
+ return isset($pi[$id]) ? $pi[$id] : false;
+ }
+ }
+ return false;
+ }
+ }
+
+ function getAllParams()
+ {
+ if(isset($_SERVER['PATH_INFO']))
+ {
+ $pi = explode('/', $_SERVER['PATH_INFO']);
+ unset($pi[0], $pi[1]);
+ return implode('/', $pi);
+ }
+ else if( isset($_GET['title']) )
+ {
+ $pi = explode('/', $_GET['title']);
+ unset($pi[0]);
+ return implode('/', $pi);
+ }
+ else
+ {
+ $k = array_keys($_GET);
+ foreach($k as $c)
+ {
+ if(substr($c, 0, 1) == '/')
+ {
+ // Bugfix for apache somehow passing dots as underscores
+ global $mime_types;
+ $exts = array_keys($mime_types);
+ $exts = '(' . implode('|', $exts) . ')';
+ if ( preg_match( '#_'.$exts.'#i', $c ) )
+ $c = preg_replace( '#_'.$exts.'#i', '.\\1', $c );
+
+ $pi = explode('/', $c);
+ unset($pi[0], $pi[1]);
+ return implode('/', $pi);
+ }
+ }
+ return false;
+ }
+ }
+
+ /**
+ * Creates a new namespace in memory
+ * @param string $id the namespace ID
+ * @param string $prefix the URL prefix, must not be blank or already used
+ * @return bool true on success false on failure
+ */
+
+ function create_namespace($id, $prefix)
+ {
+ if(in_array($prefix, $this->nslist))
+ {
+ // echo 'Warning: pathManager::create_namespace: Prefix "'.$prefix.'" is already taken
';
+ return false;
+ }
+ if( isset($this->nslist[$id]) )
+ {
+ // echo 'Warning: pathManager::create_namespace: Namespace ID "'.$prefix.'" is already taken
';
+ return false;
+ }
+ $this->nslist[$id] = $prefix;
+ }
+
+ /**
+ * Fetches the page texts for searching
+ */
+
+ function fetch_page_search_texts()
+ {
+ global $db, $session, $paths, $template, $plugins; // Common objects
+ $texts = Array();
+ $q = $db->sql_query('SELECT t.page_id,t.namespace,t.page_text,t.char_tag FROM '.table_prefix.'page_text AS t
+ LEFT JOIN '.table_prefix.'pages AS p
+ ON t.page_id=p.urlname
+ WHERE p.namespace=t.namespace
+ AND ( p.password=\'\' OR p.password=\'da39a3ee5e6b4b0d3255bfef95601890afd80709\' )
+ AND p.visible=1;'); // Only indexes "visible" pages
+
+ if( !$q )
+ {
+ return false;
+ }
+ while($row = $db->fetchrow())
+ {
+ $pid = $this->nslist[$row['namespace']] . $row['page_id'];
+ $texts[$pid] = $row['page_text'];
+ }
+ $db->free_result();
+
+ return $texts;
+ }
+
+ /**
+ * Fetches a MySQL search query to use for Searcher::searchMySQL()
+ */
+
+ function fetch_page_search_resource()
+ {
+ global $db, $session, $paths, $template, $plugins; // Common objects
+ // sha1('') returns "da39a3ee5e6b4b0d3255bfef95601890afd80709"
+ $texts = 'SELECT t.page_text,CONCAT(\'ns=\',t.namespace,\';pid=\',t.page_id) FROM '.table_prefix.'page_text AS t
+ LEFT JOIN '.table_prefix.'pages AS p
+ ON ( t.page_id=p.urlname AND t.namespace=p.namespace )
+ WHERE p.namespace=t.namespace
+ AND ( p.password=\'\' OR p.password=\'da39a3ee5e6b4b0d3255bfef95601890afd80709\' )
+ AND p.visible=1;'; // Only indexes "visible" pages
+ return $texts;
+ }
+
+ /**
+ * Rebuilds the search index
+ */
+
+ function rebuild_search_index()
+ {
+ global $db, $session, $paths, $template, $plugins; // Common objects
+ $search = new Searcher();
+ $texts = Array();
+ $textq = $db->sql_unbuffered_query($this->fetch_page_search_resource());
+ if(!$textq) $db->_die('');
+ while($row = $db->fetchrow_num())
+ {
+ $texts[(string)$row[1]] = $row[0];
+ }
+ $search->buildIndex($texts);
+ // echo ''.print_r($search->index, true).'
';
+ // return;
+ $q = $db->sql_query('DELETE FROM '.table_prefix.'search_index');
+ if(!$q) return false;
+ $secs = Array();
+ $q = 'INSERT INTO '.table_prefix.'search_index(word,page_names) VALUES';
+ foreach($search->index as $word => $pages)
+ {
+ $secs[] = '(\''.$db->escape($word).'\', \''.$db->escape($pages).'\')';
+ }
+ $q .= implode(',', $secs);
+ unset($secs);
+ $q .= ';';
+ $result = $db->sql_query($q);
+ $db->free_result();
+ if($result)
+ return true;
+ else
+ $db->_die('The search index was trying to rebuild itself when the error occured.');
+ }
+
+ /**
+ * Partially rebuilds the search index, removing/inserting entries only for the current page
+ * @param string $page_id
+ * @param string $namespace
+ */
+
+ function rebuild_page_index($page_id, $namespace)
+ {
+ global $db, $session, $paths, $template, $plugins; // Common objects
+ if(!$db->sql_query('SELECT page_text FROM '.table_prefix.'page_text
+ WHERE page_id=\''.$db->escape($page_id).'\' AND namespace=\''.$db->escape($namespace).'\';'))
+ {
+ return $db->get_error();
+ }
+ $row = $db->fetchrow();
+ $db->free_result();
+ $search = new Searcher();
+ $search->buildIndex(Array("ns={$namespace};pid={$page_id}"=>$row['page_text']));
+ $new_index = $search->index;
+
+ $keys = array_keys($search->index);
+ foreach($keys as $i => $k)
+ {
+ $c =& $keys[$i];
+ $c = hexencode($c, '', '');
+ }
+ $keys = "word=0x" . implode ( " OR word=0x", $keys ) . "";
+
+ // Zap the cache
+ $cache = array_keys($search->index);
+ if ( count($cache) < 1 )
+ {
+ return false;
+ }
+ $cache = "query LIKE '%" . implode ( "%' OR query LIKE '%", $cache ) . "%'";
+ $db->sql_query('DELETE FROM '.table_prefix.'search_cache WHERE '.$cache);
+
+ $query = $db->sql_query('SELECT word,page_names FROM '.table_prefix.'search_index WHERE '.$keys.';');
+
+ while($row = $db->fetchrow())
+ {
+ $row['word'] = rtrim($row['word'], "\0");
+ $new_index[ $row['word'] ] = $row['page_names'] . ',' . $search->index[ $row['word'] ];
+ }
+ $db->free_result();
+
+ $db->sql_query('DELETE FROM '.table_prefix.'search_index WHERE '.$keys.';');
+
+ $secs = Array();
+ $q = 'INSERT INTO '.table_prefix.'search_index(word,page_names) VALUES';
+ foreach($new_index as $word => $pages)
+ {
+ $secs[] = '(\''.$db->escape($word).'\', \''.$db->escape($pages).'\')';
+ }
+ $q .= implode(',', $secs);
+ unset($secs);
+ $q .= ';';
+ if(!$db->check_query($q))
+ {
+ die('BUG: PathManager::rebuild_page_index: Query rejected by SQL parser:'.$q.'
');
+ }
+ $result = $db->sql_query($q);
+ if($result)
+ return true;
+ else
+ $db->_die('The search index was trying to rebuild itself when the error occured.');
+
+ }
+
+ /**
+ * Creates an instance of the Searcher class, including index info
+ * @return object
+ */
+
+ function makeSearcher($match_case = false)
+ {
+ global $db, $session, $paths, $template, $plugins; // Common objects
+ $search = new Searcher();
+ $q = $db->sql_query('SELECT word,page_names FROM '.table_prefix.'search_index;');
+ if(!$q)
+ {
+ echo $db->get_error();
+ return false;
+ }
+ $idx = Array();
+ while($row = $db->fetchrow($q))
+ {
+ $row['word'] = rtrim($row['word'], "\0");
+ $idx[$row['word']] = $row['page_names'];
+ }
+ $db->free_result();
+ $search->index = $idx;
+ if($match_case)
+ $search->match_case = true;
+ return $search;
+ }
+
+ /**
+ * Creates an associative array filled with the values of all the page titles
+ * @return array
+ */
+
+ function get_page_titles()
+ {
+ $texts = Array();
+ for ( $i = 0; $i < sizeof($this->pages) / 2; $i++ )
+ {
+ $texts[$this->pages[$i]['urlname']] = $this->pages[$i]['name'];
+ }
+ return $texts;
+ }
+
+ /**
+ * Creates an instance of the Searcher class, including index info for page titles
+ * @return object
+ */
+
+ function makeTitleSearcher($match_case = false)
+ {
+ global $db, $session, $paths, $template, $plugins; // Common objects
+ $search = new Searcher();
+ $texts = $this->get_page_titles();
+ $search->buildIndex($texts);
+ if($match_case)
+ $search->match_case = true;
+ return $search;
+ }
+
+}
+
+?>