mirror of
https://gitlab.com/CodeSolutionsProject/CodeShare.git
synced 2026-02-16 01:51:33 +01:00
Initial commit
This commit is contained in:
446
Source/src/DB.php
Normal file
446
Source/src/DB.php
Normal file
@@ -0,0 +1,446 @@
|
||||
<?php
|
||||
|
||||
class DB
|
||||
{
|
||||
|
||||
/*
|
||||
* DB Basic
|
||||
*/
|
||||
|
||||
/**
|
||||
* @var bool connection with database
|
||||
*/
|
||||
public $conn;
|
||||
|
||||
/**
|
||||
* DB constructor
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
$this->conn = Config::getDBConexion();
|
||||
$this->createTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connection
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
dbw_close($this->conn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first array of a query result
|
||||
*
|
||||
* @param $query sql query
|
||||
* @return array array
|
||||
*/
|
||||
private function getQuery($query)
|
||||
{
|
||||
return dbw_fetch_array($this->conn, dbw_query($this->conn, $query));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total of codes in database
|
||||
*
|
||||
* @return int total of codes
|
||||
*/
|
||||
private function numOfCodes()
|
||||
{
|
||||
return $this->getQuery("SELECT COUNT(*) FROM Sources")[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create table if not exist in deploy (Database must be exist).
|
||||
*/
|
||||
private function createTable()
|
||||
{
|
||||
$query = "CREATE TABLE IF NOT EXISTS `Codes`( `IDC` int(11) NOT NULL AUTO_INCREMENT, `UserCreator` int(11) NOT NULL, `Name` varchar(80) NOT NULL, `Description` text NOT NULL, `Input` text NOT NULL, `Output` text NOT NULL, PRIMARY KEY (`IDC`), KEY `UserCreator` (`UserCreator`), CONSTRAINT `Codes_ibfk_1` FOREIGN KEY (`UserCreator`) REFERENCES `Users` (`IDU`) ON DELETE SET NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `Sources` ( `IDC` int(11) NOT NULL, `IDU` int(11) NOT NULL, `Lang` varchar(15) NOT NULL, `Version` int(11) NOT NULL, `Modification` int(11) NOT NULL, `Code` text NOT NULL, `UseExtLib` text, `UseExtLibVer` varchar(55) DEFAULT NULL, PRIMARY KEY (`IDC`,`Lang`,`Version`), KEY `IDU` (`IDU`), CONSTRAINT `Sources_ibfk_1` FOREIGN KEY (`IDU`) REFERENCES `Users` (`IDU`), CONSTRAINT `Sources_ibfk_2` FOREIGN KEY (`IDC`) REFERENCES `Codes` (`IDC`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `Users` ( `IDU` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(64) NOT NULL, `pass` varchar(64) NOT NULL, `nick` varchar(40) NOT NULL, `token` varchar(50) DEFAULT NULL, `ROLE` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`IDU`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
||||
dbw_query($this->conn, $query);
|
||||
}
|
||||
|
||||
/*
|
||||
* Code - Source
|
||||
*/
|
||||
|
||||
//SQL SELECT
|
||||
|
||||
/**
|
||||
* Load the user creator of code, is needed for know if the logged user is the same of creator.
|
||||
*
|
||||
* @param $IDC Code identifier
|
||||
* @return int User creator id
|
||||
*/
|
||||
public function loadOriginalAuthor($IDC)
|
||||
{
|
||||
$query = "SELECT UserCreator FROM Codes WHERE IDC='$IDC'";
|
||||
return $this->getQuery($query)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Select all from a Snippet.
|
||||
*
|
||||
* @param $id code identifier
|
||||
* @param $lang lang of snippet
|
||||
* @param $version version of snippet
|
||||
* @return array all data from snippet specified
|
||||
*/
|
||||
public function loadAll($id, $lang, $version)
|
||||
{
|
||||
$query = "SELECT * FROM Users NATURAL JOIN Sources NATURAL JOIN Codes WHERE IDC=$id AND Lang='$lang' AND Version=$version";
|
||||
$code = $this->getQuery($query);
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the last codes uploaded.
|
||||
*
|
||||
* @return mysqli_result the las 10 snippets hosted
|
||||
*/
|
||||
public function loadLast()
|
||||
{
|
||||
if (isset($_GET["o"]))
|
||||
$first = $_GET["o"] * 10;
|
||||
else
|
||||
$first = 0;
|
||||
$query = "SELECT IDC,Name,nick,Lang,Description,Code,Version FROM Users NATURAL JOIN Sources as S NATURAL JOIN Codes WHERE Version = (SELECT MAX(Version) FROM Sources WHERE S.Lang = Lang AND S.IDC = IDC) ORDER BY Modification DESC LIMIT $first, 10";
|
||||
$code = dbw_query($this->conn, $query);
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load other versions of solution.
|
||||
*
|
||||
* @param $id code identifier
|
||||
* @param $lang snippet lang
|
||||
* @return mysqli_result all versions history
|
||||
*/
|
||||
public function loadOtherVersion($id, $lang)
|
||||
{
|
||||
$query = "SELECT * FROM Users NATURAL JOIN Sources NATURAL JOIN Codes WHERE IDC='$id' AND Lang='$lang' ORDER BY Version ASC";
|
||||
return dbw_query($this->conn, $query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all snippets from a code with different lang.
|
||||
*
|
||||
* @param $id code identifier
|
||||
* @param $lang snippet lang
|
||||
* @return mysqli_result all snippet with different lang
|
||||
*/
|
||||
public function loadDiff($id, $lang)
|
||||
{
|
||||
$query = "SELECT Lang,Code,Version FROM Sources NATURAL JOIN Codes WHERE IDC='$id' AND Lang<>'$lang' AND Version = (SELECT MAX(Version) FROM Sources NATURAL JOIN Codes WHERE IDC='$id' AND Lang<>'$lang')";
|
||||
$code = dbw_query($this->conn, $query);
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all langs in which a solution is implemented.
|
||||
*
|
||||
* @param $IDC solution identifier
|
||||
* @return array all langs
|
||||
*/
|
||||
public function loadLangs($IDC)
|
||||
{
|
||||
$query = "SELECT DISTINCT Lang FROM Sources WHERE IDC='$IDC'";
|
||||
$toFetch = dbw_query($this->conn, $query);
|
||||
$toReturn = array();
|
||||
while ($var = dbw_fetch_array($this->conn, $toFetch)) {
|
||||
array_push($toReturn, $var["Lang"]);
|
||||
}
|
||||
return $toReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last version of a snippet.
|
||||
*
|
||||
* @param $IDC code identifier
|
||||
* @param $lang snippet lang
|
||||
* @return int the last snippet's version
|
||||
*/
|
||||
public function getLastVersion($IDC, $lang)
|
||||
{
|
||||
$query = "SELECT MAX(Version) FROM Sources WHERE IDC='$IDC' AND Lang='$lang'";
|
||||
return $this->getQuery($query)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last IDC put.
|
||||
*
|
||||
* @return int the last code identifier
|
||||
*/
|
||||
public function getLastIDC()
|
||||
{
|
||||
$query = "SELECT MAX(IDC) FROM Codes";
|
||||
return $this->getQuery($query)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all data from code.
|
||||
*
|
||||
* @param $idc code identifier
|
||||
* @return array all code data
|
||||
*/
|
||||
public function loadCode($idc)
|
||||
{
|
||||
$query = "SELECT * FROM Codes WHERE IDC=" . $idc;
|
||||
return $this->getQuery($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load 10 codes according the user's filter
|
||||
*
|
||||
* @param $array Array whit all (Post mensage)
|
||||
* @return mysqli_result ten codes
|
||||
*/
|
||||
public function loadFilter($array)
|
||||
{
|
||||
$query = "SELECT IDC,Name,nick,Lang,Description,Code,Version FROM Users NATURAL JOIN Sources as S NATURAL JOIN Codes ";
|
||||
$where = "WHERE (";
|
||||
$count = false;
|
||||
foreach ($array as $key => $value) {
|
||||
if ($key != "search" and $key != "o") {
|
||||
if ($count) {
|
||||
$where = $where . "OR S.Lang='$value' ";
|
||||
} else {
|
||||
$where = $where . "S.Lang='$value' ";
|
||||
$count = !$count;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(count($array)==1)
|
||||
$where = $where . "S.Lang<>''";
|
||||
|
||||
if (isset($array["o"]))
|
||||
$first = $array["o"] * 10;
|
||||
else
|
||||
$first = 0;
|
||||
$queryLast = ") AND Version = (SELECT MAX(Version) FROM Sources WHERE S.Lang = Lang AND S.IDC = IDC)" . $this->search($array["search"]) . " ORDER BY Modification DESC LIMIT $first, 10";
|
||||
//echo $query.$where.$queryLast;
|
||||
return dbw_query($this->conn, $query . $where . $queryLast);
|
||||
}
|
||||
|
||||
/**
|
||||
* make a fragment of a query based in the text which user inputs
|
||||
*
|
||||
* @param $text input by user
|
||||
* @return string sql query fragment
|
||||
*/
|
||||
public function search($text)
|
||||
{
|
||||
$text = dbw_escape_string($this->conn, $text);
|
||||
if ($text != "") {
|
||||
$query = "";
|
||||
$textExplode = explode(" ", $text);
|
||||
$value = true;
|
||||
foreach ($textExplode as $find) {
|
||||
if ($find != "") {
|
||||
if ($value) {
|
||||
$query = $query . "AND (Description LIKE '%" . $find . "%' OR Name LIKE '%" . $find . "%'";
|
||||
$value = false;
|
||||
} else {
|
||||
$query = $query . "AND Description LIKE '%" . $find . "%' OR Name LIKE '%" . $find . "%'";
|
||||
}
|
||||
}
|
||||
}
|
||||
return $query . ")";
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
//SQL INSERT/UPDATE
|
||||
|
||||
/**
|
||||
* Add a source of a code.
|
||||
*
|
||||
* @param $IDC code identifier
|
||||
* @param $lang snippet's lang
|
||||
* @param $code the source code
|
||||
* @param $IDU User identifier
|
||||
* @param null $extlib external library
|
||||
* @param null $extlibver external library version
|
||||
* @return int return the snippet's version
|
||||
*/
|
||||
public function addSource($IDC, $lang, $code, $IDU, $extlib = null, $extlibver = null)
|
||||
{
|
||||
$version = $this->getLastVersion($IDC, $lang);
|
||||
if ($version == null)
|
||||
$version = 0;
|
||||
$version++;
|
||||
$modification = time();
|
||||
|
||||
$query = "INSERT INTO Sources (`IDC`,`Lang`,`Version`,`Modification`,`Code`,`UseExtLib`,`UseExtLibVer`,`IDU`) VALUES ('$IDC','$lang',$version,$modification,'$code','$extlib','$extlibver','$IDU')";
|
||||
echo $query . ';';
|
||||
dbw_query($this->conn, $query);
|
||||
return $version;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or modify a code in database.
|
||||
*
|
||||
* @param $IDC code identifier (0 if new)
|
||||
* @param $name solutions's name
|
||||
* @param $description solution's description
|
||||
* @param $input solution's input example
|
||||
* @param $output solution's output example
|
||||
* @param $lang source's lang
|
||||
* @param $code source's code
|
||||
* @param $IDU user identifier
|
||||
* @param null $extlib external library
|
||||
* @param null $extlibver external library version
|
||||
* @return int the snippet's version
|
||||
*/
|
||||
public function addOrModifyCodes($IDC, $name, $description, $input, $output, $lang, $code, $IDU, $extlib = null, $extlibver = null)
|
||||
{
|
||||
$in = dbw_escape_string($this->conn, $input);
|
||||
$out = dbw_escape_string($this->conn, $output);
|
||||
$_code = dbw_escape_string($this->conn, $code);
|
||||
if ($IDC == 0) {
|
||||
$query = "INSERT INTO Codes (`UserCreator`,`Name`,`Description`,`Input`,`Output`) VALUES ('$IDU','$name','$description','$in','$out') ";
|
||||
//echo $query.';';
|
||||
//die();
|
||||
echo $query . ';';
|
||||
dbw_query($this->conn, $query);
|
||||
$this->addSource($this->getLastIDC(), $lang, $_code, $IDU, $extlib, $extlibver);
|
||||
} else {
|
||||
$arr = $this->loadAll($IDC, $lang, $this->getLastVersion($IDC, $lang));
|
||||
$codewrite = $arr["Code"];
|
||||
$version = 0;
|
||||
if ($code != $codewrite) {
|
||||
$version = $this->addSource($IDC, $lang, $_code, $IDU, $extlib, $extlibver);
|
||||
} else if ($extlib != $arr["UseExtLib"] || $extlibver != $arr["UseExtLibVer"]) {
|
||||
$query = "UPDATE Sources SET `UseExtLib`='$extlib', `UseExtLibVer`='$extlibver' WHERE IDC=$IDC AND Lang='$arr[Lang]' AND Version=$arr[Version]";
|
||||
//echo $query.';';
|
||||
dbw_query($this->conn, $query);
|
||||
}
|
||||
$query = "UPDATE Codes SET `Name`='$name', `Description`='$description', `Input`='$in', `Output`='$out' WHERE IDC='$IDC'";
|
||||
//echo $query.';';
|
||||
dbw_query($this->conn, $query);
|
||||
return $version;
|
||||
}
|
||||
}
|
||||
|
||||
//SQL DELETE
|
||||
|
||||
/*public function deleteCode($idc){
|
||||
$query = "DELETE FROM SCALE WHERE IDC=$idc";
|
||||
dbw_query($this->conn,$query);
|
||||
$query = "DELETE FROM Codes WHERE IDC=$idc";
|
||||
dbw_query($this->conn,$query);
|
||||
}*/
|
||||
|
||||
/*public function deleteSource($idc,$lang,$ver){
|
||||
$query = "DELETE FROM Sources WHERE IDC=$idc AND Lang='$lang' AND Version=$ver";
|
||||
dbw_query($this->conn,$query);
|
||||
}*/
|
||||
|
||||
/*
|
||||
* Users
|
||||
*/
|
||||
|
||||
//SQL SELECT
|
||||
|
||||
/**
|
||||
* Load user profile
|
||||
*
|
||||
* @param $id user identifier
|
||||
* @return array all user profile
|
||||
*/
|
||||
public function loadProfile($id)
|
||||
{
|
||||
$query = "SELECT * FROM Users WHERE IDU=" . $id;
|
||||
return $this->getQuery($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load user IDU from $email
|
||||
*
|
||||
* @param $email user's email
|
||||
* @return int user's identifier
|
||||
*/
|
||||
public function loadIDU($email)
|
||||
{
|
||||
$query = "SELECT IDU FROM Users WHERE email='" . $email . "'";
|
||||
return $this->getQuery($query)["IDU"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user cookie token is the same saved in database.
|
||||
*
|
||||
* @param $IDU user's identifier
|
||||
* @param $token token in cookie
|
||||
* @return bool true if same, false if not
|
||||
*/
|
||||
public function checkCookie($IDU, $token)
|
||||
{
|
||||
$tokenDB = $this->getQuery("SELECT token FROM Users WHERE IDU=" . $IDU)["token"];
|
||||
if ($tokenDB == $token)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if password is correct.
|
||||
*
|
||||
* @param $email user email
|
||||
* @param $pass pass password input
|
||||
* @return bool true if match, false if not
|
||||
*/
|
||||
public function checkPass($email, $pass)
|
||||
{
|
||||
$query = "SELECT pass FROM Users WHERE email='$email'";
|
||||
$passDB = $this->getQuery($query)["pass"];
|
||||
if ($passDB == hash('sha256', $pass))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
//SQL INSERT/UPDATE
|
||||
|
||||
/**
|
||||
* Register a new user in platform.
|
||||
*
|
||||
* @param $email user's email
|
||||
* @param $pass user's password
|
||||
* @param $nick user's nickname
|
||||
* @return bool true if do not exist the email, false if it exists
|
||||
*/
|
||||
public function register($email, $pass, $nick)
|
||||
{
|
||||
if ($this->loadIDU($email))
|
||||
return false;
|
||||
else {
|
||||
$password = hash('sha256', $pass);
|
||||
dbw_query($this->conn, "INSERT INTO Users (`email`,`pass`,`nick`) VALUES ('$email','$password','$nick')");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set token
|
||||
*
|
||||
* @param $IDU user's identifier
|
||||
* @param $token autogenerated token
|
||||
*/
|
||||
public function setToken($IDU, $token)
|
||||
{
|
||||
dbw_query($this->conn, "UPDATE Users SET token='$token' WHERE IDU='$IDU'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Change password
|
||||
*
|
||||
* @param $idu user identifier
|
||||
* @param $pass new password to update
|
||||
*/
|
||||
public function updatePass($idu, $pass)
|
||||
{
|
||||
$query = "UPDATE Users SET password='" . hash('sha256', $pass) . "' WHERE IDU='$idu'";
|
||||
dbw_query($this->conn, $query);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user