$adminuser = "";
include "settings.inc.php";
include "languages/english.inc.php";
if(isset($language)) @include "languages/$language.inc.php";
if($connected != 1) {
if(file_exists("common_db.inc.php")) include "common_db.inc.php";
error_reporting(0);
$link_id = db_connect();
if(!$link_id) die(sql_error());
if(!mysql_select_db($dbname)) die(sql_error());
$connected = 1;
}
// Check log-in before we do anything else
$loggedin = "0";
unset($rights);
unset($userid);
if(isset($_COOKIE[faquser]) || isset($_POST[user])) {
if(isset($_POST[user])) $user = mysql_real_escape_string($_POST[user]);
else $user = mysql_real_escape_string($_COOKIE[faquser]);
if(isset($_POST[pass])) $pass = md5($_POST[pass]);
else $pass = mysql_real_escape_string($_COOKIE[faqpass]);
$query = mysql_query("SELECT * FROM ${table_prefix}users WHERE useremail='$user'");
$dbpwd = mysql_result($query, 0, "userpass");
if($dbpwd == $pass && $dbpwd != "") {
$verified = mysql_result($query, 0, "verified");
if(empty($verified)) {
if(!isset($_COOKIE[faquser])) {
setcookie("faquser", $user);
setcookie("faqpass", $pass);
header("Location: index.php");
}
$loggedin = "1";
$username = mysql_result($query, 0, "username");
$userid = mysql_result($query, 0, "userid");
$useroffset = mysql_result($query, 0, "offset");
if(!empty($useroffset)) $offset = $useroffset;
$recemail = mysql_result($query, 0, "mailnotify");
$rights[view] = mysql_result($query, 0, "canview");
$rights[ask] = mysql_result($query, 0, "canask");
$rights[answer] = mysql_result($query, 0, "cananswer");
$rights[edit] = mysql_result($query, 0, "canedit");
}
else {
setcookie("faquser", "");
setcookie("faqpass", "");
header("Location: login.php?err=2");
exit;
}
}
else {
setcookie("faquser", "");
setcookie("faqpass", "");
header("Location: login.php?err=1");
exit;
}
}
if($loggedin == "0" && $mustlogin == "1" && $curpage != "login") {
header("Location: login.php");
exit;
}
function loadquestions($type, $startpos, $loadno) {
global $link_id, $dbname, $table_prefix;
if($type == "mostpop") $query = mysql_query("SELECT questionid FROM ${table_prefix}views ORDER BY views DESC");
elseif($type == "mostrec") $query = mysql_query("SELECT questionid FROM ${table_prefix}questions WHERE isanswered='1' ORDER BY askedtime DESC");
if($type == "allq") $query = mysql_query("SELECT questionid FROM ${table_prefix}questions ORDER BY askedtime DESC");
elseif($type == "unansw") $query = mysql_query("SELECT questionid FROM ${table_prefix}questions WHERE isanswered='0' ORDER BY askedtime DESC");
$range1 = $startpos;
$range2 = $startpos + $loadno;
$j = $range1 + 1;
if($type == "mostrec") {
for($i = 0; $i < mysql_num_rows($query); $i++) {
$curid = mysql_result($query, $i, 0);
$answertime = mysql_query("SELECT answeredtime FROM ${table_prefix}answers WHERE questionid='$curid'");
$answers[] = mysql_result($answertime, 0, 0);
$curpos[] = $i;
}
@array_multisort($answers, SORT_DESC, $curpos);
for($i = $range1; $i < $range2; $i++) {
if(isset($curpos[$i])) $useid = $curpos[$i];
else unset($useid);
if(isset($useid)) $questionid = mysql_result($query, $useid, 0);
if(!empty($questionid) && isset($useid)) {
$questionq = mysql_query("SELECT question FROM ${table_prefix}questions WHERE questionid='$questionid'");
$theq = mysql_result($questionq, 0, 0);
$theq = str_replace("\\'", "'", $theq);
$theq = str_replace("\\\"", """, $theq);
$theq = parsetext($theq);
echo "
$theq\n";
$j++;
}
}
}
else {
if($type == "allq") $range2 = mysql_num_rows($query);
for($i = $range1; $i < $range2; $i++) {
$questionid = mysql_result($query, $i, 0);
if(!empty($questionid)) {
$questionq = mysql_query("SELECT question FROM ${table_prefix}questions WHERE questionid='$questionid'");
$theq = mysql_result($questionq, 0, 0);
$theq = str_replace("\\'", "'", $theq);
$theq = str_replace("\\\"", """, $theq);
$theq = parsetext($theq);
echo "$theq\n";
$j++;
}
}
}
global $txt_none;
if(mysql_num_rows($query) == "0") echo "-- $txt_none --\n";
return mysql_num_rows($query);
}
function loadsearch($search, $type) {
global $link_id, $dbname, $table_prefix;
$search = mysql_real_escape_string($search);
if($type == "tags") {
$query = mysql_query("SELECT * FROM ${table_prefix}questions WHERE tags LIKE '%$search%' ORDER BY askedtime DESC");
}
elseif($type == "user") {
$query = mysql_query("SELECT * FROM ${table_prefix}questions WHERE askedby='$search' ORDER BY askedtime DESC");
}
elseif($type == "link") {
$query = mysql_query("SELECT * FROM ${table_prefix}questions WHERE questionid='$search'");
}
else {
$rebuilt = "";
$search = str_replace(" ", "%", $search);
$search = str_replace("\\\\\\'", "\'", $search);
$search = str_replace("\\\\\\\"", "\"", $search);
while(strstr($search, "\"")) {
$pos = strpos($search, "\"");
$rebuilt .= substr($search, 0, $pos);
$search = "%" . substr($search, $pos + 1);
if(strstr($search, "\"")) {
$pos = strpos($search, "\"");
$thatbit = substr($search, 0, $pos);
$search = "%" . substr($search, $pos + 1);
$thatbit = str_replace("%", " ", $thatbit);
$rebuilt .= $thatbit;
}
else {
$search = str_replace("%", " ", $search);
$rebuilt .= $search;
}
}
if(!empty($rebuilt)) $search = $rebuilt . $search;
$search = "%$search";
$search = str_replace("%%", "%", $search);
$search = str_replace("% ", "%", $search);
$search = str_replace(" %", "%", $search);
$search = explode("%", $search);
$string = "";
for($i = 1; $i < count($search); $i++) {
if($i != "1") {
$string .= "AND ";
$string2 .= "AND ";
}
$string .= "(tags LIKE '%$search[$i]%' OR question LIKE '%$search[$i]%' OR details LIKE '%$search[$i]%') ";
$string2 .= "(answer LIKE '%$search[$i]%') ";
}
$query = mysql_query("SELECT * FROM ${table_prefix}questions WHERE $string ORDER BY askedtime DESC");
}
$range1 = $startpos;
$range2 = $startpos + $loadno;
$j = $range1 + 1;
for($i = 0; $i < mysql_num_rows($query); $i++) {
$questionid = mysql_result($query, $i, "questionid");
if(!empty($questionid)) {
$usedid .= "$questionid, ";
$theq = mysql_result($query, $i, "question");
$theq = str_replace("\\'", "'", $theq);
$theq = str_replace("\\\"", """, $theq);
$theq = parsetext($theq);
echo "$theq\n";
$j++;
}
}
$query2 = mysql_query("SELECT * FROM ${table_prefix}answers WHERE $string2 ORDER BY answeredtime DESC");
for($i = 0; $i < mysql_num_rows($query2); $i++) {
$questionid = mysql_result($query2, $i, "questionid");
if(!empty($questionid) && !strstr($usedid, "$questionid, ")) {
$query3 = mysql_query("SELECT question FROM ${table_prefix}questions WHERE questionid='$questionid'");
echo "" . mysql_result($query3, 0, 0) . "\n";
$j++;
}
}
return $j - 1;
}
function parsetext($text) {
$text = str_replace("\r", "", $text);
$text = str_replace("\n", "
", $text);
$text = str_replace("
", "\n\n", $text);
$text = str_replace("
", "
\n", $text);
$text = preg_replace( "/([^\/=\"\]])((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "\\1\\2", $text);
$text = preg_replace('/([^\/=\"\]])(www\.)(\S+)/', '\\1\\2\\3', $text);
// Now check if any of the new URLs are really long
unset($matches);
preg_match_all("/()([^\"\]]+)(<\/a>)/", $text, $matches);
for($i = 0; $i < count($matches[4]); $i++) {
$cururl = $matches[4][$i];
if(strlen($cururl) > 70) {
$newurl = substr($cururl, 0, 30) . "..." . substr($cururl, strlen($cururl) - 30);
$text= str_replace(">$cururl<", ">$newurl<", $text);
}
}
return $text;
}
function entryprep($input) {
$input = stripslashes($input);
$input = htmlentities($input, ENT_QUOTES, UTF-8);
$input = str_replace("Â&", "&", $input);
return $input;
}
?>