mirror of
https://gitlab.com/CodeSolutionsProject/CodeShare.git
synced 2026-02-15 09:31:33 +01:00
Initial commit
This commit is contained in:
101
Source/src/Config.php
Normal file
101
Source/src/Config.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
*
|
||||
* Project configuration static class
|
||||
*/
|
||||
class Config
|
||||
{
|
||||
|
||||
public static $config;
|
||||
private static $in=false;
|
||||
|
||||
/**
|
||||
* Initialize project
|
||||
*/
|
||||
public static function init(){
|
||||
if(!Config::initialized()) {
|
||||
$file = file_get_contents("data/project.json");
|
||||
Config::$config = json_decode($file, true);
|
||||
Config::$in=true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if project is initialize
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function initialized(){
|
||||
return Config::$in;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get lang
|
||||
*
|
||||
* @return string with lang (es, en, fr...)
|
||||
*/
|
||||
public static function getLang(){
|
||||
if(!Config::initialized())
|
||||
Config::init();
|
||||
return Config::$config["lang"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the project name
|
||||
*
|
||||
* @return string with the name
|
||||
*/
|
||||
public static function getProject(){
|
||||
if(!Config::initialized())
|
||||
Config::init();
|
||||
return Config::$config["project"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the master of this project. For example, ThiefBusters belongs SoftwareTalent
|
||||
*
|
||||
* @return string with master
|
||||
*/
|
||||
public static function getMaster(){
|
||||
if(!Config::initialized())
|
||||
Config::init();
|
||||
return Config::$config["master"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the company, usually "JKA Network"
|
||||
*
|
||||
* @return string whit company
|
||||
*/
|
||||
public static function getCompany(){
|
||||
if(!Config::initialized())
|
||||
Config::init();
|
||||
return Config::$config["company"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DB Connection
|
||||
*
|
||||
* @return mixed db conn
|
||||
*/
|
||||
public static function getDBConexion(){
|
||||
if(!Config::initialized())
|
||||
Config::init();
|
||||
$DB = Config::$config["database"];
|
||||
return dbw_connect($DB["SGBD"],$DB["path"],$DB["db"],$DB["user"],$DB["password"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get options of project if exists
|
||||
*
|
||||
* @return array array whit options
|
||||
*/
|
||||
public static function getOptions(){
|
||||
if(!Config::initialized())
|
||||
Config::init();
|
||||
return Config::$config["options"];
|
||||
}
|
||||
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
359
Source/src/app.php
Normal file
359
Source/src/app.php
Normal file
@@ -0,0 +1,359 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
require_once __DIR__ . '/dbwrapper.php';
|
||||
require_once __DIR__ . '/Config.php';
|
||||
require 'functions.php';
|
||||
require 'DB.php';
|
||||
require 'dbToTable.php';
|
||||
require 'users.php';
|
||||
|
||||
|
||||
$path = array(
|
||||
"html" => "assets/html/",
|
||||
"css" => "assets/css/",
|
||||
"js" => "assets/js/",
|
||||
"img" => "assets/img/",
|
||||
);
|
||||
$supported = ksonParse('data/supported.kson');
|
||||
$loader = new Twig_Loader_Filesystem($path['html']);
|
||||
$twig = new Twig_Environment($loader);
|
||||
$user = array();
|
||||
|
||||
function run(){
|
||||
global $user;
|
||||
if(isset($_COOKIE['sessionID'])){
|
||||
$db = new DB();
|
||||
if($db->checkCookie($_COOKIE['sessionID'],$_COOKIE['token'])){
|
||||
$user = $db->loadProfile($_COOKIE['sessionID']);
|
||||
}
|
||||
}
|
||||
if (isset($_POST["search"])){
|
||||
//echo $_POST["search"];
|
||||
//die();
|
||||
firstPage(false,true);
|
||||
}
|
||||
else if ($_GET) {
|
||||
if ($_GET["page"]) {
|
||||
switch ($_GET["page"]) {
|
||||
case "code":
|
||||
codeViewer();
|
||||
break;
|
||||
case "index":
|
||||
firstPage();
|
||||
break;
|
||||
case "register":
|
||||
case "login":
|
||||
loginRegister($_GET['page']);
|
||||
break;
|
||||
case "logout":
|
||||
logout();
|
||||
header("Location: index.php");
|
||||
break;
|
||||
case "about":
|
||||
about();
|
||||
break;
|
||||
case "add":
|
||||
add();
|
||||
break;
|
||||
case "doc":
|
||||
doc();
|
||||
break;
|
||||
case "tos":
|
||||
tos();
|
||||
break;
|
||||
case "user":
|
||||
user();
|
||||
break;
|
||||
case "del":
|
||||
deleteSource();
|
||||
break;
|
||||
default:
|
||||
sendHTTPError(404);
|
||||
}
|
||||
} else {
|
||||
sendHTTPError(404);
|
||||
}
|
||||
} else {
|
||||
//header("CodeShare");
|
||||
firstPage();
|
||||
}
|
||||
}
|
||||
|
||||
function sendHTTPError($code){
|
||||
global $twig, $path,$user;
|
||||
$page = array("title" => $code."");
|
||||
|
||||
header($code."");
|
||||
switch($code){
|
||||
case 401:
|
||||
$text = "Unauthorized user";
|
||||
break;
|
||||
case 403:
|
||||
$text = "Forbidden page";
|
||||
break;
|
||||
case 404:
|
||||
$text = "Page not found";
|
||||
break;
|
||||
case 500:
|
||||
$text = "Internal server error";
|
||||
break;
|
||||
default:
|
||||
$text = "Please, return to home page";
|
||||
$code = "Internal error";
|
||||
break;
|
||||
}
|
||||
|
||||
$error = array("number" => $code."","text"=>$text);
|
||||
echo $twig->render('error.twig',array("page" => $page, "path" => $path, "user" => $user,"error" => $error));
|
||||
}
|
||||
|
||||
function firstPage($filter=false,$globalSearch=false){
|
||||
global $supported,$twig,$path,$user;
|
||||
$db = new DB();
|
||||
if(!$filter and !$globalSearch)
|
||||
if(isset($_GET["p"]))
|
||||
$query = $db->loadLast($_GET["p"]);
|
||||
else
|
||||
$query = $db->loadLast();
|
||||
else
|
||||
$query = $db->loadFilter($_POST);;
|
||||
$last = lastToArray($db,$query,$supported);
|
||||
$page = array(
|
||||
"title" => "CodeShare",
|
||||
"description" => "Sharing your solution with all",
|
||||
"last" => $last,
|
||||
"supported" => $supported,
|
||||
"page" => "home"
|
||||
);
|
||||
if(!$filter)
|
||||
echo $twig->render("firstpage.twig",array("page" => $page, "path" => $path, "user" => $user));
|
||||
else
|
||||
echo $twig->render("firstCodes.twig",array("page" => $page, "path" => $path, "user" => $user));
|
||||
$db->close();
|
||||
}
|
||||
|
||||
function doc(){
|
||||
global $twig,$path,$user;
|
||||
$page = array(
|
||||
"title" => "Documentation",
|
||||
"description" => "All documentation about how to upload code in CodeShare",
|
||||
"page" => "doc"
|
||||
);
|
||||
echo $twig->render("doc.twig",array("page" => $page, "path" => $path, "user" => $user));
|
||||
}
|
||||
|
||||
function add(){
|
||||
global $twig,$path,$user,$supported;
|
||||
if(!$user['IDU']){
|
||||
header("Location: ?page=login");
|
||||
}
|
||||
if(isset($_POST['name'])){
|
||||
$db=new DB();
|
||||
$extlib = isset($_POST['extlib']) ? $_POST['extlib'] : null;
|
||||
$extlibver = isset($_POST['extlibver']) ? $_POST['extlibver'] : null;
|
||||
$db->addOrModifyCodes(
|
||||
0,
|
||||
$_POST['name'],
|
||||
$_POST['description'],
|
||||
htmlentities($_POST['input']),
|
||||
htmlentities($_POST['output']),
|
||||
$_POST['lang'],
|
||||
htmlentities($_POST['code']),
|
||||
$user['IDU'],
|
||||
$extlib,
|
||||
$extlibver);
|
||||
//die();
|
||||
$idc=$db->getLastIDC();
|
||||
header("Location: ?page=code&id=$idc&lang=$_POST[lang]&version=1");
|
||||
}
|
||||
$page=array(
|
||||
"title" => "Add your own solution",
|
||||
"description" => "Share now your snippet",
|
||||
"page" => "add",
|
||||
);
|
||||
echo $twig->render("add.twig",array("page" => $page,"path" => $path, "user" => $user, "supported" => $supported));
|
||||
|
||||
}
|
||||
|
||||
function codeViewer(){
|
||||
global $supported,$twig,$path,$user;
|
||||
$db = new DB();
|
||||
|
||||
if(isset($_POST['lang'])){
|
||||
$db->addSource($_GET['id'],$_POST['lang'],htmlentities($_POST['code']),$user["IDU"]);
|
||||
$db->close();
|
||||
header("Location: ?page=code&id=$_GET[id]&lang=$_POST[lang]&version=1");
|
||||
}else if(isset($_POST['name'])){
|
||||
$extlib = isset($_POST['extlib']) ? $_POST['extlib'] : null;
|
||||
$extlibver = isset($_POST['extlibver']) ? $_POST['extlibver'] : null;
|
||||
$version=$db->addOrModifyCodes(
|
||||
$_GET['id'],
|
||||
$_POST['name'],
|
||||
$_POST['description'],
|
||||
htmlentities($_POST['input']),
|
||||
htmlentities($_POST['output']),
|
||||
$_GET['lang'],
|
||||
htmlentities($_POST['code']),
|
||||
$user['IDU'],
|
||||
$extlib,
|
||||
$extlibver);
|
||||
//die();
|
||||
$db->close();
|
||||
if($version!=0){
|
||||
header("Location: ?page=code&id=$_GET[id]&lang=$_GET[lang]&version=$version");
|
||||
}else{
|
||||
header('Refresh:0');
|
||||
}
|
||||
}else if(isset($_POST['code'])){
|
||||
$version=$db->addSource($_GET['id'],$_GET['lang'],htmlentities($_POST['code']),$user['IDU'],$_POST['extlib'],$_POST['extlibver']);
|
||||
$db->close();
|
||||
header("Location: ?page=code&id=$_GET[id]&lang=$_GET[lang]&version=$version");
|
||||
}
|
||||
|
||||
if (!$_GET['lang'] || !$_GET['id']) {
|
||||
sendHTTPError(404);
|
||||
}elseif($_GET['version']){
|
||||
$array = $db->loadAll($_GET['id'],$_GET['lang'],$_GET['version']);
|
||||
if($array){
|
||||
$page = array(
|
||||
"title" => "$array[Name] by $array[nick]",
|
||||
"description" => "$array[Name] by $array[nick] in ".$supported[$_GET['lang']],
|
||||
"otherV" => false,
|
||||
"otherI" => false,
|
||||
"existedLangs" => $db->loadLangs($_GET['id']),
|
||||
"original" => $db->loadOriginalAuthor($_GET['id']),
|
||||
);
|
||||
$code = allCodeToArray($array,$supported);
|
||||
if($array['Version']!=1){
|
||||
$page['otherV'] = true;
|
||||
}
|
||||
$diff = $db->loadDiff($_GET["id"],$_GET["lang"]);
|
||||
if (dbw_fetch_array($db->conn, $diff)) {
|
||||
$page['otherI'] = true;
|
||||
}
|
||||
dbw_query_goto($db->conn,$diff);
|
||||
$otherImplementation = otherImplementationToArray($db,$diff,$supported);
|
||||
echo $twig->render("code.twig", array(
|
||||
"page" => $page,
|
||||
"code" => $code,
|
||||
"otherImplementation" => $otherImplementation,
|
||||
"path" => $path,
|
||||
"user" => $user,
|
||||
"supported" => $supported,
|
||||
));
|
||||
}else{
|
||||
sendHTTPError(404);
|
||||
}
|
||||
}else{
|
||||
$query = $db->loadOtherVersion($_GET['id'], $_GET['lang']);
|
||||
$other = otherVersionToArray($db,$query,$supported);
|
||||
$name = $other[0]['name'];
|
||||
$page = array(
|
||||
"title" => "Other versions of $name",
|
||||
"description" => "All differents version of $name uploaded in CodeShare",
|
||||
"last" => $other,
|
||||
"page" => ""
|
||||
);
|
||||
echo $twig->render("otherVersion.twig",array(
|
||||
"page" => $page,
|
||||
"path" => $path,
|
||||
"user" => $user
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function loginRegister($default){
|
||||
global $twig,$path;
|
||||
|
||||
$bool=false;
|
||||
|
||||
$page = array(
|
||||
"title" => ucfirst($default),
|
||||
"description" => "Start now in CodeShare",
|
||||
"page" => $default,
|
||||
"state" => 0, //0=no logged, 1 login fail, 2 register fail, 3 all correct (render home)
|
||||
);
|
||||
|
||||
if(array_key_exists('email',$_POST) or array_key_exists('emailre',$_POST)){
|
||||
if($_POST["email"]){
|
||||
$bool=checklogin($_POST['email'],$_POST['pwd']);
|
||||
if(!$bool){
|
||||
$page['state']=1;
|
||||
}
|
||||
}else{
|
||||
$bool=register($_POST["emailre"],$_POST["pwdre"],$_POST["name"]);
|
||||
if(!$bool){
|
||||
$page['state']=2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($bool) {
|
||||
header("Location: index.php");
|
||||
}else
|
||||
echo $twig->render("loginregister.twig", array("page" => $page, "path" => $path ));
|
||||
}
|
||||
|
||||
function about(){
|
||||
global $twig, $path, $user;
|
||||
$page = array(
|
||||
"title" => "About CodeShare",
|
||||
"description" => "Developed by JKA Network's Team",
|
||||
"page" => "about",
|
||||
);
|
||||
echo $twig->render("about.twig",array("page" => $page, "path" => $path,"user" => $user));
|
||||
}
|
||||
|
||||
function tos(){
|
||||
global $twig, $path, $user;
|
||||
$page = array(
|
||||
"title" => "Terms of service",
|
||||
"description" => "Terms of services and privacy policies"
|
||||
);
|
||||
echo $twig->render("TOS.twig",array("page" => $page, "path" => $path, "user" => $user));
|
||||
}
|
||||
|
||||
function user(){
|
||||
global $twig,$path,$user;
|
||||
|
||||
if($user['nick']) {
|
||||
$status=0;
|
||||
if(array_key_exists("passact",$_POST)){
|
||||
$passact = $_POST["passact"];
|
||||
$newpass = $_POST["newpass"];
|
||||
//echo "EEEEEOOO";
|
||||
$db = new DB();
|
||||
if($db->checkPass($user['email'],$passact)){
|
||||
$status=1;
|
||||
$db->updatePass($user['IDU'],$newpass);
|
||||
}else
|
||||
$status=2;
|
||||
$db->close();
|
||||
}
|
||||
$page = array(
|
||||
"title" => "$user[nick]",
|
||||
"description" => "NOT RELEVANT",
|
||||
"status" => $status
|
||||
);
|
||||
echo $twig->render("user.twig",array("page"=>$page,"path"=>$path, "user"=>$user));
|
||||
}else{
|
||||
sendHTTPError(403);
|
||||
}
|
||||
}
|
||||
|
||||
function deleteSource(){
|
||||
global $user;
|
||||
if(array_key_exists('ROLE',$user)){
|
||||
if($user['ROLE']==3){
|
||||
$db = new DB();
|
||||
$db->deleteSource($_GET['id'],$_GET['lang'],$_GET['version']);
|
||||
$db->close();
|
||||
header('Location: index.php');
|
||||
}else
|
||||
sendHTTPError(403);
|
||||
}else{
|
||||
sendHTTPError(403);
|
||||
}
|
||||
}
|
||||
64
Source/src/dbToTable.php
Normal file
64
Source/src/dbToTable.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: joselucross
|
||||
* Date: 7/07/17
|
||||
* Time: 11:42
|
||||
*/
|
||||
|
||||
function lastToArray($db,$query,$supported){
|
||||
$last = array();
|
||||
while($var = dbw_fetch_array($db->conn,$query)){
|
||||
$array = array
|
||||
( "idc" => $var["IDC"],
|
||||
"lang" => $var["Lang"],
|
||||
"version" => $var["Version"],
|
||||
"name" => $var["Name"],
|
||||
"nick" => $var["nick"],
|
||||
"lLang" => $supported[$var["Lang"]],
|
||||
"description" => $var["Description"],
|
||||
"code" => html_entity_decode($var["Code"]),
|
||||
);
|
||||
array_push($last,$array);
|
||||
}
|
||||
return $last;
|
||||
}
|
||||
|
||||
function allCodeToArray($query,$supported){
|
||||
$code = array(
|
||||
"idc" => $query['IDC'],
|
||||
"lang" => $query['Lang'],
|
||||
"version" => $query['Version'],
|
||||
"name" => $query['Name'],
|
||||
"nick" => $query['nick'],
|
||||
"idu" => $query['IDU'],
|
||||
"lLang" => $supported[$query['Lang']],
|
||||
"description" => $query['Description'],
|
||||
"code" => html_entity_decode($query['Code']),
|
||||
"input" => html_entity_decode($query['Input']),
|
||||
"output" => html_entity_decode($query['Output']),
|
||||
"rows" => substr_count($query['Code'],"\n"),
|
||||
"extlib" => $query['UseExtLib'],
|
||||
"extlibver" => $query['UseExtLibVer'],
|
||||
);
|
||||
return $code;
|
||||
|
||||
}
|
||||
|
||||
function otherImplementationToArray($db,$query,$supported){
|
||||
$other = array();
|
||||
while($var = dbw_fetch_array($db->conn,$query)){
|
||||
$array = array
|
||||
( "lang" => $var["Lang"],
|
||||
"version" => $var["Version"],
|
||||
"lLang" => $supported[$var["Lang"]],
|
||||
"code" => html_entity_decode($var["Code"]),
|
||||
);
|
||||
array_push($other,$array);
|
||||
}
|
||||
return $other;
|
||||
}
|
||||
|
||||
function otherVersionToArray($db,$query,$supported){
|
||||
return lastToArray($db,$query,$supported);
|
||||
}
|
||||
235
Source/src/dbwrapper.php
Normal file
235
Source/src/dbwrapper.php
Normal file
@@ -0,0 +1,235 @@
|
||||
<?php
|
||||
/* DBWrapper.php - Version 1.4
|
||||
This script is a simple wrapper for SQLite3, MySQL and PgSQL,
|
||||
for make possible to use different BD systems without changing the functions.
|
||||
For use, in dbw_connect you have to specify type of database (see below)
|
||||
Avaiable functions:
|
||||
dbw_connect ($tdb,$server,$database,$user,$password) -> some values are optional,
|
||||
except $tdb and $server. $server is location in SQLite3
|
||||
dbw_close ($conn) -> Closes connection
|
||||
|
||||
dbw_query ($conn,$query) -> Does a query
|
||||
dbw_multi_query($conn,$multiquery) -> This does a multiquery without returning nothing.
|
||||
Its used for load from a file/script
|
||||
dbw_query_fetch_array ($conn,$query[,$typearray]) -> This do a query and fetch array, all in one function
|
||||
($typearray optional, see below)
|
||||
|
||||
--After here, this functions works with result of a query--
|
||||
|
||||
dbw_fetch_array ($conn,$result[,$typearray]) -> Fetch a row. ($typearray optional, see below)
|
||||
dbw_escape_string($conn,$string) -> Escapes conflictive chars for inserting into database
|
||||
|
||||
dbw_fetch_row and dbw_fetch_assoc ($conn,$result) -> Wrappers of dbw_fetch_array with row or assoc arguments
|
||||
dbw_query_goto($conn,$result[,$row]) -> Goto X result of a query. If row is not specified, will be first row, 0
|
||||
dbw_num_rows($conn,$result) -> Return number of results of a query
|
||||
|
||||
--This doesnt need a query--
|
||||
dbw_last_id($conn) -> Returns last insert ID
|
||||
dbw_insert_id($conn) -> Alias of dbw_last_id
|
||||
|
||||
|
||||
$tdb (Type of database) can be:
|
||||
-mysql/mysqli -> MySQL or MariaDB
|
||||
-sqlite/sqlite3 -> Sqlite3
|
||||
-PostgreSQL/PgSQL/pg -> PostgreSQL
|
||||
|
||||
$conn is the connection stablished in dbw_connect (ie. $conn = dbw_connect('sqlite','file.sqlite'))
|
||||
$typearray is the form of array is returned, and not writed is default:
|
||||
-ASSOC -> Associative indexes
|
||||
-NUM -> Numeric indexes
|
||||
-BOTH -> (Default) Both types of indexes
|
||||
*/
|
||||
|
||||
|
||||
/** Connect with database */
|
||||
function dbw_connect($tdb,$server,$database=NULL,$user = NULL,$password=NULL){
|
||||
switch ($tdb){
|
||||
case "mysql":
|
||||
case "mysqli":
|
||||
$return[0] = mysqli_connect($server,$user,$password,$database) or die("Error de conexion");
|
||||
$return[1] = "mysqli"; //Return standard mysqli for other funcs.
|
||||
break;
|
||||
case "sqlite":
|
||||
case "sqlite3":
|
||||
$return[0] = new SQLite3($server);
|
||||
if (!$return[0]) die ("Error de conexion");
|
||||
$return[1] = "sqlite"; //Return standard SQLite3 for other funcs.
|
||||
break;
|
||||
case "PostgreSQL":
|
||||
case "pg":
|
||||
case "PgSQL":
|
||||
$return[0] = pg_connect("host=$server dbname=$database user=$user password=$password") or die ('Error de conexion: ' . pg_last_error());
|
||||
$return[1] = "PgSQL"; //Return standard PgSQL for other funcs.
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/** Escapes conflictive chars for inserting into database */
|
||||
function dbw_escape_string($conn,$string){
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
return mysqli_escape_string($conn[0],$string);
|
||||
case "sqlite":
|
||||
return SQLite3::escapeString($string);
|
||||
case "PgSQL":
|
||||
return pg_escape_string($string);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Make query */
|
||||
function dbw_query($conn,$query){
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
return mysqli_query($conn[0],$query);
|
||||
break;
|
||||
case "sqlite":
|
||||
return $conn[0]->query($query);
|
||||
break;
|
||||
case "PgSQL":
|
||||
return pg_query($query); //Last error (pg_last_error()) not implemented
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Fetch array from query */
|
||||
function dbw_fetch_array($conn,$result,$typearray = NULL){
|
||||
if ($result == false || $result == NULL){return false;}
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
if ($typearray == NULL || $typearray == "BOTH"){return mysqli_fetch_array($result);}
|
||||
if ($typearray == "ASSOC"){return mysqli_fetch_array($result,MYSQLI_ASSOC);}
|
||||
if ($typearray == "NUM"){return mysqli_fetch_array($result,MYSQLI_NUM);}
|
||||
break;
|
||||
case "sqlite":
|
||||
if ($typearray == NULL || $typearray == "BOTH"){return $result->fetchArray();}
|
||||
if ($typearray == "ASSOC"){return $result->fetchArray(SQLITE3_ASSOC);}
|
||||
if ($typearray == "NUM"){return $result->fetchArray(SQLITE3_NUM);}
|
||||
break;
|
||||
case "PgSQL":
|
||||
if ($typearray == NULL || $typearray == "BOTH"){return pg_fetch_array($result);}
|
||||
if ($typearray == "ASSOC"){return pg_fetch_array($result,NULL,PGSQL_ASSOC);}
|
||||
if ($typearray == "NUM"){return pg_fetch_array($result,NULL,PGSQL_NUM);}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Make query and fetch array */
|
||||
function dbw_query_fetch_array($conn,$query){
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
$query = mysqli_query($conn[0],$query);
|
||||
if ($query == false || $query == NULL){return false;}
|
||||
return mysqli_fetch_array($query);
|
||||
break;
|
||||
case "sqlite":
|
||||
$query = $conn[0]->query($query);
|
||||
if ($query == false || $query == NULL){return false;}
|
||||
return $query->fetchArray();
|
||||
break;
|
||||
case "PgSQL":
|
||||
$query = pg_query($query);
|
||||
if ($query == false || $query == NULL){return false;}
|
||||
return pg_fetch_array($query); //Last error (pg_last_error()) not implemented
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Goes a query to $row. $row starts in 0 as first row as if not specified */
|
||||
function dbw_query_goto($conn,$result,$row = 0){
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
mysqli_data_seek($result,$row);
|
||||
break;
|
||||
case "sqlite":
|
||||
$result->reset();
|
||||
$count = 0;
|
||||
while ($count != $row){
|
||||
$result->fetchArray();
|
||||
}
|
||||
break;
|
||||
case "PgSQL":
|
||||
pg_result_seek($result, $row);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Does multiple querys in one command */
|
||||
function dbw_multi_query($conn,$query){
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
mysqli_multi_query($conn[0],$query);
|
||||
break;
|
||||
case "sqlite":
|
||||
$conn[0]->exec($query);
|
||||
break;
|
||||
case "PgSQL":
|
||||
die ("No soportado aun"); // TODO
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the lastest Insert ID */
|
||||
function dbw_last_id($conn){
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
return mysqli_insert_id($conn[0]);
|
||||
break;
|
||||
case "sqlite":
|
||||
return $conn[0]->lastInsertRowID();
|
||||
break;
|
||||
case "PgSQL":
|
||||
return pg_fetch_array(pg_query("SELECT lastval();"))[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Returns number of results */
|
||||
function dbw_num_rows($conn,$result){
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
return mysqli_num_rows($result);
|
||||
break;
|
||||
case "sqlite":
|
||||
die ("No soportado aun"); // TODO
|
||||
break;
|
||||
case "PgSQL":
|
||||
die ("No soportado aun"); // TODO
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Close connection */
|
||||
function dbw_close($conn){
|
||||
switch ($conn[1]){
|
||||
case "mysqli":
|
||||
mysqli_close($conn[0]);
|
||||
break;
|
||||
case "sqlite":
|
||||
$conn[0]->close();
|
||||
break;
|
||||
case "PgSQL":
|
||||
pg_close($conn[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Some internal wrappers for functions that are equal to other with arguments */
|
||||
|
||||
function dbw_fetch_assoc($conn,$result){return dbw_fetch_array($conn,$result,"ASSOC");}
|
||||
function dbw_fetch_row($conn,$result){return dbw_fetch_array($conn,$result,"NUM");}
|
||||
function dbw_insert_id($conn){return dbw_last_id($conn);}
|
||||
|
||||
?>
|
||||
39
Source/src/functions.php
Normal file
39
Source/src/functions.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: joselucross
|
||||
* Date: 6/07/17
|
||||
* Time: 11:56
|
||||
*/
|
||||
function RandomString($length)
|
||||
{
|
||||
//https://phpes.wordpress.com/2007/06/12/generador-de-una-cadena-aleatoria/
|
||||
$source = 'abcdefghijklmnopqrstuvwxyz';
|
||||
$source .= '1234567890';
|
||||
if($length>0){
|
||||
$rstr = "";
|
||||
$source = str_split($source,1);
|
||||
for($i=1; $i<=$length; $i++){
|
||||
mt_srand((double)microtime() * 1000000);
|
||||
$num = mt_rand(1,count($source));
|
||||
$rstr .= $source[$num-1];
|
||||
}
|
||||
|
||||
}
|
||||
return $rstr;
|
||||
}
|
||||
|
||||
function ksonParse($path){
|
||||
$file = fopen($path,"r");
|
||||
$array = [];
|
||||
if($file){
|
||||
while(!feof($file)){
|
||||
$line = fgets($file);
|
||||
$part = explode(":",$line);
|
||||
$array[$part[0]]=$part[1];
|
||||
}
|
||||
return $array;
|
||||
}else{
|
||||
die('<h3>Error, kson file not exist</h3>');
|
||||
}
|
||||
}
|
||||
41
Source/src/users.php
Normal file
41
Source/src/users.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: joselucross
|
||||
* Date: 9/07/17
|
||||
* Time: 19:06
|
||||
*/
|
||||
|
||||
function checklogin($email,$pass){
|
||||
$db = new DB();
|
||||
$bool = $db->checkPass($email,$pass);
|
||||
if($bool){
|
||||
$idu = $db->loadIDU($email);
|
||||
$token = RandomString(50);
|
||||
$db->setToken($idu,$token);
|
||||
setcookie("token",$token);
|
||||
setcookie("sessionID",$idu);
|
||||
$db->close();
|
||||
return true;
|
||||
}
|
||||
$db->close();
|
||||
return false;
|
||||
}
|
||||
|
||||
function register($email,$pass,$nick){
|
||||
$db = new DB();
|
||||
$bool = $db->register($email,$pass,$nick);
|
||||
if($bool) {
|
||||
$db->close();
|
||||
return checklogin($email, $pass);
|
||||
}else {
|
||||
$db->close();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function logout(){
|
||||
setcookie("token","",time()-3600);
|
||||
setcookie("sessionID",0,time()-3600);
|
||||
}
|
||||
Reference in New Issue
Block a user