mirror of
https://gitlab.com/CodeSolutionsProject/CodeShare.git
synced 2026-02-16 01:51:33 +01:00
Merge
This commit is contained in:
@@ -16,7 +16,7 @@ class Config
|
||||
*/
|
||||
public static function init(){
|
||||
if(!Config::initialized()) {
|
||||
$file = file_get_contents("data/project.json");
|
||||
$file = file_get_contents(__DIR__."/../data/project.json");
|
||||
Config::$config = json_decode($file, true);
|
||||
Config::$in=true;
|
||||
}
|
||||
|
||||
@@ -132,7 +132,8 @@ class DB
|
||||
*/
|
||||
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')";
|
||||
$lang=dbw_escape_string($this->conn,$lang);
|
||||
$query = "SELECT Lang,Code,Version FROM Sources WHERE IDC=$id AND Lang<>'$lang' AND Version = (SELECT MAX(Version) FROM Sources WHERE IDC=$id AND Lang='$lang')";
|
||||
$code = dbw_query($this->conn, $query);
|
||||
return $code;
|
||||
}
|
||||
@@ -286,7 +287,8 @@ class DB
|
||||
$version++;
|
||||
$modification = time();
|
||||
|
||||
$query = "INSERT INTO Sources (`IDC`,`Lang`,`Version`,`Modification`,`Code`,`UseExtLib`,`UseExtLibVer`,`IDU`) VALUES ('$IDC','$lang',$version,$modification,'$code','$lib','$libV','$IDU')";
|
||||
|
||||
$query = "INSERT INTO Sources (`IDC`,`Lang`,`Version`,`Modification`,`Code`,`UseExtLib`,`UseExtLibVer`,`IDU`) VALUES ('$IDC','$lang',$version,$modification,'$code','$lib','$libV','$IDU')";
|
||||
//echo $query . ';';
|
||||
dbw_query($this->conn, $query);
|
||||
return $version;
|
||||
@@ -324,8 +326,8 @@ class DB
|
||||
$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);
|
||||
if ($_code != dbw_escape_string($this->conn,$codewrite)) {
|
||||
$version = $this->addSource($IDC, $lang, $_code, $IDU, $extlib, $extlibver,$codewrite);
|
||||
} 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]";
|
||||
@@ -458,7 +460,7 @@ class DB
|
||||
*/
|
||||
public function updatePass($idu, $pass)
|
||||
{
|
||||
$query = "UPDATE Users SET password='" . hash('sha256', $pass) . "' WHERE IDU='$idu'";
|
||||
$query = "UPDATE Users SET pass='" . hash('sha256', $pass) . "' WHERE IDU='$idu'";
|
||||
dbw_query($this->conn, $query);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,8 @@ if(isset($_COOKIE['cookieconsent_status'])) {
|
||||
session_start(); //Start session in app, before loading vars ($_SESSION)
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
//require_once __DIR__ . '/dbwrapper.php';
|
||||
require_once __DIR__ .'/../vendor/autoload.php';
|
||||
|
||||
require_once __DIR__ . '/Config.php';
|
||||
require 'functions.php';
|
||||
require 'DB.php';
|
||||
@@ -12,90 +12,257 @@ require 'dbToTable.php';
|
||||
require 'users.php';
|
||||
|
||||
|
||||
$path = array(
|
||||
"html" => "assets/html/",
|
||||
"css" => "assets/css/",
|
||||
"js" => "assets/js/",
|
||||
"img" => "assets/img/",
|
||||
);
|
||||
$app = new Silex\Application();
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
$app['debug'] = true;
|
||||
$app->register(new Silex\Provider\TwigServiceProvider(), array(
|
||||
'twig.path' => __DIR__.'/../',
|
||||
));
|
||||
|
||||
|
||||
$app['url'] = '/';
|
||||
|
||||
$app['front'] = $app['url'] . 'assets';
|
||||
$valores = array('js', 'img', 'css', 'fonts', 'html');
|
||||
foreach($valores as $asset){
|
||||
$app['front'.$asset] = $app['front'].'/'.$asset;
|
||||
}
|
||||
|
||||
$string = file_get_contents(__DIR__."/../data/supported.json");
|
||||
$supported = json_decode($string, true);
|
||||
$loader = new Twig_Loader_Filesystem($path['html']);
|
||||
$twig = new Twig_Environment($loader);
|
||||
$user = array();
|
||||
$db=null;
|
||||
try {
|
||||
$db = new DB();
|
||||
}catch (Exception $ex){
|
||||
sendHTTPError(503);
|
||||
exit();
|
||||
$app["supported"] = json_decode($string, true);
|
||||
|
||||
$app['db']=null;
|
||||
function connectDB(){
|
||||
global $app;
|
||||
try {
|
||||
$app['db'] = new DB();
|
||||
}catch (Exception $ex){
|
||||
$app->abort('503','ERROR en la Base de Datos');
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function run(){
|
||||
global $user;
|
||||
if(isset($_SESSION['sessionID'])){
|
||||
global $db;
|
||||
if($db->checkCookie($_SESSION['sessionID'],$_SESSION['token'])){
|
||||
$user = $db->loadProfile($_SESSION['sessionID']);
|
||||
}
|
||||
/* Security */
|
||||
$app['security.role_hierarchy'] = array(
|
||||
'ROLE_DELETE' => array('ROLE_COMMON'),
|
||||
'ROLE_COMMON' => array('ROLE_USER'),
|
||||
);
|
||||
/* End Security */
|
||||
|
||||
$app->boot();
|
||||
|
||||
/* Basics */
|
||||
$app->get('/', function (Request $request) use($app) {
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
$query = $app['db']->loadLast();
|
||||
$last = lastToArray($app["db"],$query,$app['supported']);
|
||||
|
||||
return $app['twig']->render($app['fronthtml'] . '/firstpage.twig' ,array(
|
||||
'page' => array('title' => "CodeShare",
|
||||
"description" => "Sharing your solutions with all",
|
||||
"last" => $last,
|
||||
"supported" => $app["supported"],
|
||||
"page" => "home",
|
||||
"grouped" => groupByCategory($app["supported"])),
|
||||
'user' => $user
|
||||
));
|
||||
})->bind('home');
|
||||
|
||||
$app->get('/doc', function (Request $request) use($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
$page = array(
|
||||
"title" => "Documentation",
|
||||
"description" => "All documentation about how to upload code in CodeShare",
|
||||
"page" => "doc",
|
||||
);
|
||||
return $app['twig']->render($app['fronthtml'].'/doc.twig', array("page" => $page, "user" => $user));
|
||||
})->bind('doc');
|
||||
|
||||
$app->get('/about', function (Request $request) use($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
$page = array(
|
||||
"title" => "About CodeShare",
|
||||
"description" => "Developed by JKA Network's Team",
|
||||
"page" => "about",
|
||||
);
|
||||
return $app['twig']->render($app['fronthtml'].'/about.twig', array("page" => $page,'user'=>$user));
|
||||
})->bind('about');
|
||||
|
||||
/* Codes */
|
||||
$app->get('/code/{lang}-{idc}-{version}', function (Request $request, $lang, $idc, $version) use($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
|
||||
$array = $app['db']->loadAll($idc,$lang,$version);
|
||||
if(!$array){
|
||||
$app->abort('404');
|
||||
}
|
||||
if (isset($_POST["search"])){
|
||||
firstPage(false,true);
|
||||
$page = array(
|
||||
"page" => 'code',
|
||||
"title" => "$array[Name] by $array[nick]",
|
||||
"description" => "$array[Name] by $array[nick] in ".$app['supported'][$lang][0],
|
||||
"otherV" => false,
|
||||
"otherI" => false,
|
||||
"existedLangs" => $app['db']->loadLangs($idc),
|
||||
"original" => $app['db']->loadOriginalAuthor($idc),
|
||||
);
|
||||
$code = allCodeToArray($array,$app['supported']);
|
||||
if($array['Version']!=1)
|
||||
$page['otherV'] = true;
|
||||
$diff = $app['db']->loadDiff($idc,$lang);
|
||||
if(dbw_fetch_array($app['db']->conn,$diff)){
|
||||
$page['otherI'] = 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 "error":
|
||||
sendHTTPError($_GET['error']);
|
||||
/*case "del":
|
||||
deleteSource();*/
|
||||
break;
|
||||
default:
|
||||
sendHTTPError(404);
|
||||
dbw_query_goto($app['db']->conn,$diff);
|
||||
$otherImplementation = otherImplementationToArray($app['db'],$diff,$app['supported']);
|
||||
return $app['twig']->render($app['fronthtml'].'/code.twig', array(
|
||||
"page" => $page,
|
||||
"code" => $code,
|
||||
"otherImplementation" => $otherImplementation,
|
||||
"supported" => $app['supported'],
|
||||
"user" => $user
|
||||
));
|
||||
})->bind('code');
|
||||
|
||||
$app->get('/code/{lang}-{idc}', function(Request $request, $lang, $idc) use($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
|
||||
$array = $app['db']->loadOtherVersion($idc,$lang);
|
||||
$other = otherVersionToArray($app['db'],$array,$app['supported']);
|
||||
$name = $other[0]['name'];
|
||||
|
||||
$page = array(
|
||||
"title" => "Other versions of $name",
|
||||
"description" => "All differents version of $name uploaded in CodeShare",
|
||||
"last" => $other,
|
||||
"page" => ""
|
||||
);
|
||||
return $app['twig']->render($app['fronthtml'].'/otherVersion.twig', array(
|
||||
"page" => $page, "user" => $user
|
||||
));
|
||||
})->bind('codeVer');
|
||||
|
||||
/* User */
|
||||
$app->get('/tos',function(Request $request) use($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
$page = array(
|
||||
"title" => "Terms of service",
|
||||
"description" => "Terms of services and privacy policies",
|
||||
"page" => ""
|
||||
);
|
||||
|
||||
return $app['twig']->render($app['fronthtml'].'/TOS.twig', array("page" => $page, "user" => $user));
|
||||
})->bind('tos');
|
||||
|
||||
$app->get('/login', function(Request $request) use ($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
if($user != null){
|
||||
return $app->redirect($app["url_generator"]->generate('home'));
|
||||
}
|
||||
if ($request->getMethod() == 'GET'){
|
||||
return loginRegister('login');
|
||||
}
|
||||
else{
|
||||
$state = checkInfo($request);
|
||||
return loginOrRegister($state,$request);
|
||||
}
|
||||
|
||||
})->bind('login')->method('GET|POST');
|
||||
|
||||
$app->get('/signup', function(Request $request) use ($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
if($user != null){
|
||||
return $app->redirect($app["url_generator"]->generate('home'));
|
||||
}
|
||||
|
||||
if ($request->getMethod() == 'GET'){
|
||||
return loginRegister('sign-up');
|
||||
}else{
|
||||
$state = checkInfo($request);
|
||||
return loginOrRegister($state,$request);
|
||||
}
|
||||
|
||||
})->bind('register')->method('GET|POST');
|
||||
|
||||
function loginRegister($default,$state=0){
|
||||
global $app;
|
||||
|
||||
$page = array(
|
||||
"title" => ucfirst($default),
|
||||
"description" => "Start now in CodeShare",
|
||||
"page" => "$default",
|
||||
"state" => $state, //0=no logged, 1 login fail, 2 register fail, 3 all correct (render home)
|
||||
);
|
||||
return $app['twig']->render($app['fronthtml']."/loginregister.twig", array("page" => $page));
|
||||
}
|
||||
|
||||
function loginOrRegister($state,$request){
|
||||
global $app;
|
||||
|
||||
switch ($state){
|
||||
case 3:
|
||||
return loginRegister('login',1);
|
||||
case 1:
|
||||
if(checklogin($request->get('email'),$request->get('pwd'))){
|
||||
return $app->redirect($app["url_generator"]->generate('home'));
|
||||
}else{
|
||||
return loginRegister("login",1);
|
||||
}
|
||||
case 2:
|
||||
if(register($request->get('emailre'),$request->get('emailre-re'),
|
||||
$request->get('pwdre'), $request->get('pwdre-re'),$request->get('nick'))){
|
||||
return $app->redirect($app["url_generator"]->generate('home'));
|
||||
}else{
|
||||
return loginRegister("sing-up",2);
|
||||
}
|
||||
} else {
|
||||
sendHTTPError(404);
|
||||
}
|
||||
} else {
|
||||
//header("CodeShare");
|
||||
firstPage();
|
||||
}
|
||||
}
|
||||
|
||||
function sendHTTPError($code){
|
||||
global $twig, $path,$user;
|
||||
$page = array("title" => $code."");
|
||||
$app->get('logout',function(Request $request) use ($app){
|
||||
logout();
|
||||
return $app->redirect($app['url_generator']->generate('home'));
|
||||
})->bind('logout');
|
||||
|
||||
header($code."");
|
||||
$app->get('/user', function(Request $request) use ($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
if($user == null)
|
||||
$app->abort('403');
|
||||
|
||||
$status = 0;
|
||||
if($request->getMethod() == 'POST'){
|
||||
$passact = $request->get("passact");
|
||||
$newpass = $request->get("newpass");
|
||||
$newpassre = $request->get("newpass-re");
|
||||
if($app['db']->checkPass($user['email'],$passact) && $newpass == $newpassre){
|
||||
$status=1;
|
||||
$app['db']->updatePass($user['IDU'],$newpass);
|
||||
}else
|
||||
$status=2;
|
||||
$app['db']->close();
|
||||
}
|
||||
$page = array(
|
||||
"page" => "user",
|
||||
"title" => "$user[nick]",
|
||||
"description" => "NOT RELEVANT",
|
||||
"status" => $status
|
||||
);
|
||||
return $app['twig']->render($app['fronthtml']."/user.twig",array("page"=>$page,"user"=>$user));
|
||||
|
||||
})->bind('user')->method('GET|POST');
|
||||
|
||||
/* Error Codes */
|
||||
function HTTPError($code){
|
||||
switch($code){
|
||||
case 401:
|
||||
$text = "Unauthorized user";
|
||||
@@ -117,269 +284,28 @@ function sendHTTPError($code){
|
||||
$code = "Internal error";
|
||||
break;
|
||||
}
|
||||
|
||||
$error = array("number" => $code."","text"=>$text);
|
||||
echo $twig->render('error.twig',array("page" => $page, "path" => $path, "user" => $user,"error" => $error));
|
||||
return array("text"=>$text,"number"=>$code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Index page
|
||||
*
|
||||
* @param bool $filter if filters are enabled (when ajax)
|
||||
* @param bool $globalSearch if filters are in nav-bar search box
|
||||
*/
|
||||
function firstPage($filter=false,$globalSearch=false){
|
||||
global $supported,$twig,$path,$user;
|
||||
global $db;
|
||||
//var_dump($_POST);
|
||||
if(!$filter and !$globalSearch) //Common index
|
||||
if(isset($_GET["p"])) //If page (not 0)
|
||||
$query = $db->loadLast($_GET["p"]);
|
||||
else
|
||||
$query = $db->loadLast();
|
||||
else
|
||||
$query = $db->loadFilter($_POST,$globalSearch);
|
||||
$last = lastToArray($db,$query,$supported);
|
||||
$page = array(
|
||||
"title" => "CodeShare",
|
||||
"description" => "Sharing your solution with all",
|
||||
"last" => $last,
|
||||
"supported" => $supported,
|
||||
"page" => "home",
|
||||
"grouped" => groupByCategory($supported)
|
||||
);
|
||||
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(!isset($user['IDU'])){
|
||||
header("Location: ?page=login");
|
||||
$app->error(function (\Exception $e,$request, $code) use ($app) {
|
||||
$user = null;
|
||||
try{
|
||||
$user = getUser($app);
|
||||
}catch(Throwable $ex){
|
||||
//Nothing
|
||||
}
|
||||
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;
|
||||
global $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']][0],
|
||||
"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);
|
||||
}
|
||||
if ($app['debug']) {
|
||||
return;
|
||||
}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
|
||||
|
||||
$error = HTTPError($code);
|
||||
return $app['twig']->render($app['fronthtml'].'/error.twig', Array(
|
||||
'page' => array("title"=>$code),
|
||||
'error' => $error,
|
||||
'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), 4 captcha failed
|
||||
);
|
||||
|
||||
$response = checkCaptcha($_POST['g-recaptcha-response']);
|
||||
|
||||
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($response)
|
||||
|
||||
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";
|
||||
global $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);
|
||||
}
|
||||
}*/
|
||||
|
||||
382
Source/src/appOld.php
Normal file
382
Source/src/appOld.php
Normal file
@@ -0,0 +1,382 @@
|
||||
<?php
|
||||
if(isset($_COOKIE['cookieconsent_status'])) {
|
||||
session_start(); //Start session in app, before loading vars ($_SESSION)
|
||||
}
|
||||
|
||||
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/",
|
||||
);
|
||||
$string = file_get_contents(__DIR__."/../data/supported.json");
|
||||
$supported = json_decode($string, true);
|
||||
$loader = new Twig_Loader_Filesystem($path['html']);
|
||||
$twig = new Twig_Environment($loader);
|
||||
$user = array();
|
||||
$db=null;
|
||||
try {
|
||||
$db = new DB();
|
||||
}catch (Exception $ex){
|
||||
sendHTTPError(503);
|
||||
exit();
|
||||
}
|
||||
|
||||
function run(){
|
||||
global $user;
|
||||
if(isset($_SESSION['sessionID'])){
|
||||
global $db;
|
||||
if($db->checkCookie($_SESSION['sessionID'],$_SESSION['token'])){
|
||||
$user = $db->loadProfile($_SESSION['sessionID']);
|
||||
}
|
||||
}
|
||||
if (isset($_POST["search"])){
|
||||
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 "error":
|
||||
sendHTTPError($_GET['error']);
|
||||
/*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 503:
|
||||
$text = "Database unavailable";
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Index page
|
||||
*
|
||||
* @param bool $filter if filters are enabled (when ajax)
|
||||
* @param bool $globalSearch if filters are in nav-bar search box
|
||||
*/
|
||||
function firstPage($filter=false,$globalSearch=false){
|
||||
global $supported,$twig,$path,$user;
|
||||
global $db;
|
||||
//var_dump($_POST);
|
||||
if(!$filter and !$globalSearch) //Common index
|
||||
if(isset($_GET["p"])) //If page (not 0)
|
||||
$query = $db->loadLast($_GET["p"]);
|
||||
else
|
||||
$query = $db->loadLast();
|
||||
else
|
||||
$query = $db->loadFilter($_POST,$globalSearch);
|
||||
$last = lastToArray($db,$query,$supported);
|
||||
$page = array(
|
||||
"title" => "CodeShare",
|
||||
"description" => "Sharing your solution with all",
|
||||
"last" => $last,
|
||||
"supported" => $supported,
|
||||
"page" => "home",
|
||||
"grouped" => groupByCategory($supported)
|
||||
);
|
||||
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(!isset($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;
|
||||
global $db;
|
||||
|
||||
if(isset($_POST['lang'])){
|
||||
$extlib = isset($_POST['extlib']) ? $_POST['extlib'] : null;
|
||||
$extlibver = isset($_POST['extlibver']) ? $_POST['extlibver'] : null;
|
||||
$db->addSource($_GET['id'],$_POST['lang'],htmlentities($_POST['code']),$user["IDU"],$extlib,$extlibver);
|
||||
$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']][0],
|
||||
"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"];
|
||||
global $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);
|
||||
}
|
||||
}*/
|
||||
@@ -11,7 +11,7 @@
|
||||
* Methods to get an array to send to twig
|
||||
*/
|
||||
|
||||
function lastToArray($db,&$query,&$supported){
|
||||
function lastToArray($db,&$query,$supported){
|
||||
$last = array();
|
||||
while($var = dbw_fetch_array($db->conn,$query)){
|
||||
$array = array
|
||||
@@ -29,7 +29,7 @@ function lastToArray($db,&$query,&$supported){
|
||||
return $last;
|
||||
}
|
||||
|
||||
function allCodeToArray(&$query,&$supported){
|
||||
function allCodeToArray(&$query,$supported){
|
||||
$code = array(
|
||||
"idc" => $query['IDC'],
|
||||
"lang" => $query['Lang'],
|
||||
@@ -50,7 +50,7 @@ function allCodeToArray(&$query,&$supported){
|
||||
|
||||
}
|
||||
|
||||
function otherImplementationToArray($db,&$query,&$supported){
|
||||
function otherImplementationToArray($db,&$query,$supported){
|
||||
$other = array();
|
||||
while($var = dbw_fetch_array($db->conn,$query)){
|
||||
$array = array
|
||||
@@ -64,6 +64,6 @@ function otherImplementationToArray($db,&$query,&$supported){
|
||||
return $other;
|
||||
}
|
||||
|
||||
function otherVersionToArray($db,&$query,&$supported){
|
||||
function otherVersionToArray($db,$query,$supported){
|
||||
return lastToArray($db,$query,$supported);
|
||||
}
|
||||
@@ -54,7 +54,7 @@ function ksonParse($path){
|
||||
/**
|
||||
* Group by $supported
|
||||
*/
|
||||
function groupByCategory(&$supported){
|
||||
function groupByCategory($supported){
|
||||
$grouped = Array();
|
||||
foreach($supported as $key => $val){
|
||||
if(!array_key_exists($val[1],$grouped)){
|
||||
|
||||
@@ -6,6 +6,17 @@
|
||||
* Time: 19:06
|
||||
*/
|
||||
|
||||
function checkInfo($request){
|
||||
$email = $request->get('email');
|
||||
$emailre = $request->get('emailre');
|
||||
if ($email != null)
|
||||
return 1;
|
||||
else if ($emailre != null)
|
||||
return 2;
|
||||
else
|
||||
return 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if login is correct
|
||||
*
|
||||
@@ -37,8 +48,10 @@ function checklogin($email,$pass){
|
||||
* @param $nick new user's nickname
|
||||
* @return bool true if email never exists before, else false
|
||||
*/
|
||||
function register($email,$pass,$nick){
|
||||
function register($email,$emailre,$pass,$passre,$nick){
|
||||
$db = new DB();
|
||||
if($emailre != $email || $pass != $passre)
|
||||
return false;
|
||||
$bool = $db->register($email,$pass,$nick);
|
||||
if($bool) {
|
||||
$db->close();
|
||||
@@ -55,4 +68,14 @@ function register($email,$pass,$nick){
|
||||
*/
|
||||
function logout(){
|
||||
session_destroy(); //Logout
|
||||
}
|
||||
|
||||
function getUser($app){
|
||||
$user=null;
|
||||
if(isset($_SESSION['sessionID'])){
|
||||
if($app['db']->checkCookie($_SESSION['sessionID'],$_SESSION['token'])){
|
||||
$user = $app['db']->loadProfile($_SESSION['sessionID']);
|
||||
}
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
Reference in New Issue
Block a user