Просмотр файла reforum/mainclasses.php

Размер файла: 16.38Kb
<?

 /* Программное обеспечение для создания форумов ReForum.
  * Версия 1.0 . Разработано Дроздовским Михаилом в 2005
  * году. Все права защищены.
  *
  * Подробности по адресу: http://drozd.artotron.com/reforum/
  */

 /* Базовые классы */


class ReForum_Enc {

function GetHash ($data) {
return md5($data);
}

function GetString ($data) {
return mysql_escape_string($data);
}

function GetRandomLogin ($length) {
$res = "";
for ($x=0;$x<$length;$x++) {
$a = mt_rand(0, 61);

switch ($a) {
case 1:
$res .= "q";
break;

case 2:
$res .= "w";
break;

case 3:
$res .= "e";
break;

case 4:
$res .= "r";
break;

case 5:
$res .= "t";
break;

case 6:
$res .= "y";
break;

case 7:
$res .= "u";
break;

case 8:
$res .= "i";
break;

case 9:
$res .= "o";
break;

case 10:
$res .= "p";
break;

case 11:
$res .= "a";
break;

case 12:
$res .= "s";
break;

case 13:
$res .= "d";
break;

case 14:
$res .= "f";
break;

case 15:
$res .= "g";
break;

case 16:
$res .= "h";
break;

case 17:
$res .= "j";
break;

case 18:
$res .= "k";
break;

case 19:
$res .= "l";
break;

case 20:
$res .= "z";
break;

case 21:
$res .= "x";
break;

case 22:
$res .= "c";
break;

case 23:
$res .= "v";
break;

case 24:
$res .= "b";
break;

case 25:
$res .= "n";
break;

case 26:
$res .= "m";
break;

case 27:
$res .= "Q";
break;

case 28:
$res .= "W";
break;

case 29:
$res .= "E";
break;

case 30:
$res .= "R";
break;

case 31:
$res .= "T";
break;

case 32:
$res .= "Y";
break;

case 33:
$res .= "U";
break;

case 34:
$res .= "I";
break;

case 35:
$res .= "O";
break;

case 36:
$res .= "P";
break;

case 37:
$res .= "A";
break;

case 38:
$res .= "S";
break;

case 39:
$res .= "D";
break;

case 40:
$res .= "F";
break;

case 41:
$res .= "G";
break;

case 42:
$res .= "H";
break;

case 43:
$res .= "J";
break;

case 44:
$res .= "K";
break;

case 45:
$res .= "L";
break;

case 46:
$res .= "Z";
break;

case 47:
$res .= "X";
break;

case 48:
$res .= "C";
break;

case 49:
$res .= "V";
break;

case 50:
$res .= "B";
break;

case 51:
$res .= "N";
break;

case 52:
$res .= "M";
break;

default:
$res .= mt_rand(0, 9);
break;
}

}

return $res;
}

function GetRandomPwd ($length) {
$res = "";
for ($x=0;$x<$length;$x++) {
$a = mt_rand(0, 52);

switch ($a) {
case 1:
$res .= "q";
break;

case 2:
$res .= "w";
break;

case 3:
$res .= "e";
break;

case 4:
$res .= "r";
break;

case 5:
$res .= "t";
break;

case 6:
$res .= "y";
break;

case 7:
$res .= "u";
break;

case 8:
$res .= "i";
break;

case 9:
$res .= "o";
break;

case 10:
$res .= "p";
break;

case 11:
$res .= "a";
break;

case 12:
$res .= "s";
break;

case 13:
$res .= "d";
break;

case 14:
$res .= "f";
break;

case 15:
$res .= "g";
break;

case 16:
$res .= "h";
break;

case 17:
$res .= "j";
break;

case 18:
$res .= "k";
break;

case 19:
$res .= "l";
break;

case 20:
$res .= "z";
break;

case 21:
$res .= "x";
break;

case 22:
$res .= "c";
break;

case 23:
$res .= "v";
break;

case 24:
$res .= "b";
break;

case 25:
$res .= "n";
break;

case 26:
$res .= "m";
break;

case 27:
$res .= "Q";
break;

case 28:
$res .= "W";
break;

case 29:
$res .= "E";
break;

case 30:
$res .= "R";
break;

case 31:
$res .= "T";
break;

case 32:
$res .= "Y";
break;

case 33:
$res .= "U";
break;

case 34:
$res .= "I";
break;

case 35:
$res .= "O";
break;

case 36:
$res .= "P";
break;

case 37:
$res .= "A";
break;

case 38:
$res .= "S";
break;

case 39:
$res .= "D";
break;

case 40:
$res .= "F";
break;

case 41:
$res .= "G";
break;

case 42:
$res .= "H";
break;

case 43:
$res .= "J";
break;

case 44:
$res .= "K";
break;

case 45:
$res .= "L";
break;

case 46:
$res .= "Z";
break;

case 47:
$res .= "X";
break;

case 48:
$res .= "C";
break;

case 49:
$res .= "V";
break;

case 50:
$res .= "B";
break;

case 51:
$res .= "N";
break;

case 52:
$res .= "M";
break;

case 53:
$res .= "?";
break;

case 54:
$res .= ".";
break;

case 55:
$res .= "]";
break;

case 56:
$res .= "}";
break;

case 57:
$res .= "[";
break;

case 58:
$res .= "{";
break;

case 59:
$res .= ";";
break;

case 60:
$res .= ":";
break;

default:
$res .= mt_rand(0, 9);
break;
}

}

return $res;
}

}

class ReForum_Control {

var $conn;
var $DBPREFIX;

function ReForum_Control ($connection, $dbpr) {
$this->conn = $connection;
$this->DBPREFIX = $dbpr;
}

function GetFreeUserId () {
$sqls1 = "SELECT uid FROM ".$this->DBPREFIX."_ref_users ORDER BY uid";
$sqls1 = mysql_query($sqls1, $this->conn);

$sqlf = 0;

while ($aaf = mysql_fetch_array($sqls1)) {
$sqlf = $aaf["uid"];
}

return $sqlf + 1;
}

function GetFreeThemeId () {
$sqls1 = "SELECT uid FROM ".$this->DBPREFIX."_ref_themes ORDER BY uid";
$sqls1 = mysql_query($sqls1, $this->conn);

$sqlf = 0;

while ($aaf = mysql_fetch_array($sqls1)) {
$sqlf = $aaf["uid"];
}

return $sqlf + 1;
}

function AddUser ($uid, $username, $userlogin, $userpwd, $email, $site, $isb) {        // добавляем нового юзера на форум
$uid = ReForum_Enc::GetString($uid);
$username = ReForum_Enc::GetString($username);
$userlogin = ReForum_Enc::GetHash($userlogin);
$userpwd = ReForum_Enc::GetHash($userpwd);
$email = ReForum_Enc::GetString($email);
$site = ReForum_Enc::GetString($site);
$isb = ReForum_Enc::GetString($isb);

$sqls1 = "SELECT date_registered FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid OR uname='$username' OR ulogin='$userlogin' OR uemail='$email' LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_result($sqls1, 0);

if (!$sqls1) {
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_users VALUES($uid, '$username', '$userlogin', '$userpwd', '$email', '$site', '".date("d.m.Y h:i")."', '$isb')";
if (mysql_query($sqli1, $this->conn)) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}

function RewriteUser ($uid, $username, $userlogin, $userpwd, $email, $site, $isb) {
$uid = ReForum_Enc::GetString($uid);
$username = ReForum_Enc::GetString($username);
$userlogin = ReForum_Enc::GetHash($userlogin);
$userpwd = ReForum_Enc::GetHash($userpwd);
$email = ReForum_Enc::GetString($email);
$site = ReForum_Enc::GetString($site);
$isb = ReForum_Enc::GetString($isb);

$sqls1 = "SELECT date_registered FROM ".$this->DBPREFIX."_ref_users WHERE uid!=$uid AND ( uname='$username' OR ulogin='$userlogin' OR uemail='$email') LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_result($sqls1, 0);

if (!$sqls1) {

$sqls2 = "SELECT date_registered FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid LIMIT 1";
$sqls2 = mysql_query($sqls2, $this->conn);
$sqls2 = mysql_fetch_array($sqls2);

if ($sqls2["date_registered"]) {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid";
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_users VALUES($uid, '$username', '$userlogin', '$userpwd', '$email', '$site', '".$sqls2["date_registered"]."', '$isb')";
//echo $sqli1;
mysql_query($sqld1, $this->conn);
mysql_query($sqli1, $this->conn);

return 1;
} else {
return 0;
}

} else {
return 0;
}
}

function DeleteUser ($uid) {
$uid = ReForum_Enc::GetString($uid);

$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid";
mysql_query($sqld1, $this->conn);

return 1;
}

function BlockUser ($uid) {
$uid = ReForum_Enc::GetString($uid);

$sqls1 = "SELECT * FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_fetch_array($sqls1);

if ($sqls1["date_registered"]) {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid";
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_users VALUES($uid, '".$sqls1["uname"]."', '".$sqls1["ulogin"]."', '".$sqls1["upwd"]."', '".$sqls1["uemail"]."', '".$sqls1["usite"]."', '".$sqls1["date_registered"]."', 'yes')";
//echo $sqli1;
mysql_query($sqld1, $this->conn);
mysql_query($sqli1, $this->conn);

return 1;
} else {
return 0;
}

}

function UnblockUser ($uid) {
$uid = ReForum_Enc::GetString($uid);

$sqls1 = "SELECT * FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_fetch_array($sqls1);

if ($sqls1["date_registered"]) {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid";
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_users VALUES($uid, '".$sqls1["uname"]."', '".$sqls1["ulogin"]."', '".$sqls1["upwd"]."', '".$sqls1["uemail"]."', '".$sqls1["usite"]."', '".$sqls1["date_registered"]."', 'no')";
//echo $sqli1;
mysql_query($sqld1, $this->conn);
mysql_query($sqli1, $this->conn);

return 1;
} else {
return 0;
}

}

function AddMessage ($uid, $themeid, $parent_id, $user_id, $username, $header, $text, $ip, $isbl) {        // добавляем новое сообщение на форум
$uid = ReForum_Enc::GetString($uid);
$themeid = ReForum_Enc::GetString($themeid);
$parent_id = ReForum_Enc::GetString($parent_id);
$user_id = ReForum_Enc::GetString($user_id);
$username = ReForum_Enc::GetString($username);
$header = ReForum_Enc::GetString($header);
$text = ReForum_Enc::GetString($text);
$ip = ReForum_Enc::GetString($ip);
$isbl = ReForum_Enc::GetString($isbl);

if ($user_id == "") {
$user_id = 0;
}
if ($parent_id == "") {
$parent_id = 0;
}

$sqls1 = "SELECT xdate FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_result($sqls1, 0);

if (!$sqls1) {
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_themes VALUES($uid, $themeid, $parent_id, $user_id, '$username', '$header', '$text', '$ip', '".date("d.m.Y h:i")."', '$isbl')";
//echo $sqli1;
if (mysql_query($sqli1, $this->conn)) {
return 1;
} else {
return 0;
}
} else {
// такое сообщение уже есть

return 0;
}
}

function RewriteMessage ($uid, $themeid, $parent_id, $user_id, $username, $header, $text, $ip, $isbl) {
$uid = ReForum_Enc::GetString($uid);
$themeid = ReForum_Enc::GetString($themeid);
$parent_id = ReForum_Enc::GetString($parent_id);
$user_id = ReForum_Enc::GetString($user_id);
$username = ReForum_Enc::GetString($username);
$header = ReForum_Enc::GetString($header);
$text = ReForum_Enc::GetString($text);
$ip = ReForum_Enc::GetString($ip);
$isbl = ReForum_Enc::GetString($isbl);

if ($user_id == "") {
$user_id = 0;
}

$datx = "Последний раз редактировалось ".date("d.m.Y h:i");

$sqls1 = "SELECT xdate FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_result($sqls1, 0);

if ($sqls1) {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid";
mysql_query($sqld1, $this->conn);
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_themes VALUES($uid, $themeid, $parent_id, $user_id, '$username', '$header', '$text', '$ip', '".date("d.m.Y h:i")."', '$isbl')";
if (mysql_query($sqli1, $this->conn)) {
return 1;
} else {
return 0;
}
} else {
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_themes VALUES($uid, $themeid, $parent_id, $user_id, '$username', '$header', '$text', '$ip', '".date("d.m.Y h:i")."', '$isbl')";
if (mysql_query($sqli1, $this->conn)) {
return 1;
} else {
return 0;
}
}
}

function DeleteMessage ($uid) {
$uid = ReForum_Enc::GetString($uid);

$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid";
if (mysql_query($sqld1, $this->conn)) {
return 1;
} else {
return 0;
}
}

function BlockMessage ($uid) {
$uid = ReForum_Enc::GetString($uid);

$sqls1 = "SELECT * FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_fetch_array($sqls1);

if ($sqls1["xdate"]) {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid";
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_themes VALUES($uid, ".$sqls1["themeid"].", ".$sqls1["parent_id"].", ".$sqls1["user_id"].", '".$sqls1["uname"]."', '".$sqls1["hheader"]."', '".$sqls1["htext"]."', '".$sqls1["uip"]."', '".$sqls1["xdate"]."', 'yes')";
mysql_query($sqld1, $this->conn);
if (mysql_query($sqli1, $this->conn)) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}

function UnblockMessage ($uid) {
$uid = ReForum_Enc::GetString($uid);

$sqls1 = "SELECT * FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_fetch_array($sqls1);

if ($sqls1["xdate"]) {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid";
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_themes VALUES($uid, ".$sqls1["themeid"].", ".$sqls1["parent_id"].", ".$sqls1["user_id"].", '".$sqls1["uname"]."', '".$sqls1["hheader"]."', '".$sqls1["htext"]."', '".$sqls1["uip"]."', '".$sqls1["xdate"]."', 'no')";
mysql_query($sqld1, $this->conn);
if (mysql_query($sqli1, $this->conn)) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}


function LoginUser ($ulogin, $upwd, $uip, $ubrowser) {        // логиним юзера (возвращает SESSID)
$uip = ReForum_Enc::GetString($uip);
$ulogin = ReForum_Enc::GetHash($ulogin);
$upwd = ReForum_Enc::GetHash($upwd);
$ubrowser = ReForum_Enc::GetString($ubrowser);

$sqls1 = "SELECT upwd, uid FROM ".$this->DBPREFIX."_ref_users WHERE ulogin='$ulogin' AND isb='no' LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_fetch_array($sqls1);

if ($sqls1["upwd"] === $upwd) {
$sqls2 = "SELECT date_logged_in FROM ".$this->DBPREFIX."_ref_logged WHERE uid=".$sqls1["uid"]." LIMIT 1";
$sqls2 = mysql_query($sqls2, $this->conn);
$sqls2 = mysql_result($sqls2, 0);

if (!$sqls2) {
$sessid = md5($sqls1["uid"].crc32(mt_rand(0, 1415362432))."DROZD".$uip.$ubrowser.date("d.M.Y h:i"));
$sqli1 = "INSERT INTO ".$this->DBPREFIX."_ref_logged VALUES(".$sqls1["uid"].", '".date("d.m.Y h:i")."', '$uip', '$ubrowser', '$sessid')";
echo $sqli1;
if (mysql_query($sqli1, $this->conn)) {
return $sessid;
} else {
return 0;
}
} else {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_logged WHERE uid=".$sqls1["uid"]." AND date_logged_in='$sqls2'";
mysql_query($sqld1, $this->conn);
return 0;
}
} else {
return 0;
}
}

function LogoutUser ($sessid) {       // разлогиним юзера
$sessid = ReForum_Enc::GetString($sessid);

$sqls1 = "SELECT uid FROM ".$this->DBPREFIX."_ref_logged WHERE sessid='$sessid' LIMIT 1";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_result($sqls1, 0);

if ($sqls1) {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_logged WHERE uid=$sqls1";
if (mysql_query($sqld1, $this->conn)) {
return 1;
} else {
return 0;
}
} else {
// такой юзер не вошел

return 0;
}
}

function IsUserLoggedIn ($uip, $sessid, $ubrowser) {       // проверяем, залогинен юзер или нет
$uip = ReForum_Enc::GetString($uip);
$sessid = ReForum_Enc::GetString($sessid);

$sqla1 = "SELECT uid, ubrowser FROM ".$this->DBPREFIX."_ref_logged WHERE uip='$uip' AND sessid='$sessid' LIMIT 1";
$sqlf1 = mysql_query($sqla1, $this->conn);
$sqls1 = mysql_fetch_array($sqlf1);

if ($sqls1["uid"]) {
if ($sqls1["ubrowser"] === $ubrowser) {
return $sqls1["uid"];
} else {
$sqld1 = "DELETE FROM ".$this->DBPREFIX."_ref_logged WHERE uid=".$sqls1["uid"];
mysql_query($sqld1, $this->conn);
return 0;
}
} else {
return 0;
}
}

function GetMessageInfo ($uid) {       // возвращает массив с информацией о сообщении
$uid = ReForum_Enc::GetString($uid);

$sqls1 = "SELECT * FROM ".$this->DBPREFIX."_ref_themes WHERE uid=$uid";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_fetch_array($sqls1);

if ($sqls1["isb"] != "") {
return $sqls1;
} else {
return 0;
}
}

function GetUserInfo ($uid) {           // получает массив с информацией о пользователе
$uid = ReForum_Enc::GetString($uid);

$sqls1 = "SELECT * FROM ".$this->DBPREFIX."_ref_users WHERE uid=$uid";
$sqls1 = mysql_query($sqls1, $this->conn);
$sqls1 = mysql_fetch_array($sqls1);

if ($sqls1["isb"] != "") {
return $sqls1;
} else {
return 0;
}
}

}





// Тестируем работу главной библиотеки классов


//include "config.php";
//$conn = mysql_connect(MYSQL_SERVER, MYSQL_LOGIN, MYSQL_PWD);
//$cf = mysql_select_db(MYSQL_DBNAME, $conn);

//$rrc = new ReForum_Control($conn, DBPREFIX);

//$rrc->AddUser(1, "lolz", "perdun", "perduz", "[email protected]", "", "no");
//echo $rrc->RewriteUser(1, "pookz", "perdun", "perduz", "[email protected]", "", "no");
//$rrc->UnblockUser(1);

?>