mirror of
https://gitlab.com/CodeSolutionsProject/CodeShare.git
synced 2026-04-06 02:22:03 +02:00
#5 Finished
This commit is contained in:
@@ -173,9 +173,9 @@ class DB
|
||||
*
|
||||
* @return int the last code identifier
|
||||
*/
|
||||
public function getLastIDC()
|
||||
public function getLastIDC($name)
|
||||
{
|
||||
$query = "SELECT MAX(IDC) FROM Codes";
|
||||
$query = "SELECT MAX(IDC) FROM Codes WHERE name='$name'";
|
||||
return $this->getQuery($query)[0];
|
||||
}
|
||||
|
||||
@@ -308,7 +308,7 @@ class DB
|
||||
* @param $IDU user identifier
|
||||
* @param null $extlib external library
|
||||
* @param null $extlibver external library version
|
||||
* @return int the snippet's version
|
||||
* @return int the snippet's version or IDC
|
||||
*/
|
||||
public function addOrModifyCodes($IDC, $name, $description, $input, $output, $lang, $code, $IDU, $extlib = null, $extlibver = null)
|
||||
{
|
||||
@@ -319,9 +319,10 @@ class DB
|
||||
$query = "INSERT INTO Codes (`UserCreator`,`Name`,`Description`,`Input`,`Output`) VALUES ('$IDU','$name','$description','$in','$out') ";
|
||||
//echo $query.';';
|
||||
//die();
|
||||
|
||||
dbw_query($this->conn, $query);
|
||||
$this->addSource($this->getLastIDC(), $lang, $_code, $IDU, $extlib, $extlibver);
|
||||
$myID = $this->getLastIDC($name);
|
||||
$this->addSource($myID, $lang, $_code, $IDU, $extlib, $extlibver);
|
||||
return $myID;
|
||||
} else {
|
||||
$arr = $this->loadAll($IDC, $lang, $this->getLastVersion($IDC, $lang));
|
||||
$codewrite = $arr["Code"];
|
||||
|
||||
@@ -94,61 +94,6 @@ $app->get('/about', function (Request $request) use($app){
|
||||
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');
|
||||
}
|
||||
$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;
|
||||
}
|
||||
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();
|
||||
@@ -264,6 +209,9 @@ $app->get('/user', function(Request $request) use ($app){
|
||||
/* Error Codes */
|
||||
function HTTPError($code){
|
||||
switch($code){
|
||||
case 400:
|
||||
$text = "Bad Request";
|
||||
break;
|
||||
case 401:
|
||||
$text = "Unauthorized user";
|
||||
break;
|
||||
@@ -309,3 +257,141 @@ $app->error(function (\Exception $e,$request, $code) use ($app) {
|
||||
});
|
||||
|
||||
|
||||
/* 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($request->getMethod()=='POST'){
|
||||
return updateCode($request,$idc,$lang,$version,$user);
|
||||
}
|
||||
$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;
|
||||
}
|
||||
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')->method('GET|POST');
|
||||
|
||||
function updateCode($request,$idc,$lang,$version,$user){
|
||||
global $app;
|
||||
|
||||
$lang2 = $request->request->all()['lang'];
|
||||
$extlib = $request->get('extlib');
|
||||
$extlibver = $request->get('extlibver');
|
||||
if($extlibver == null and $extlib != null)
|
||||
$app->abort(400);
|
||||
$code = htmlentities($request->get('code'));
|
||||
if ($code == null or $lang == null)
|
||||
$app->abort(400);
|
||||
if($lang2 != null){
|
||||
$app['db']->addSource($idc,$lang2,$code,$user['IDU'],$extlib,$extlibver);
|
||||
$app['db']->close();
|
||||
return $app->redirect($app['url_generator']->generate('code',array("idc"=>$idc,"version"=>1,"lang"=>$lang2)));
|
||||
}else{
|
||||
$name = $request->get("name");
|
||||
$description = $request->get("description");
|
||||
$input = htmlentities($request->get("input"));
|
||||
$output = htmlentities($request->get("output"));
|
||||
if($name == null || $description == null || $input == null ||$output == null)
|
||||
$app->abort(400);
|
||||
$version2 = $app['db']->addOrModifyCodes($idc,$name,$description,$input,$output,$lang,$code,$user['IDU'],$extlibver,$extlibver);
|
||||
$app['db']->close();
|
||||
if($version2!=0)
|
||||
return $app->redirect($app['url_generator']->generate('code',array("idc"=>$idc,"version"=>$version2,"lang"=>$lang)));
|
||||
else
|
||||
return $app->redirect($app['url_generator']->generate('code',array("idc"=>$idc,"version"=>$version,"lang"=>$lang)));
|
||||
}
|
||||
}
|
||||
|
||||
$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');
|
||||
|
||||
$app->get('/add', function(Request $request) use($app){
|
||||
connectDB();
|
||||
$user = getUser($app);
|
||||
if ($user == null)
|
||||
return $app->redirect($app['url_generator']->generate('login'));
|
||||
if($request->getMethod()=="POST"){
|
||||
$extlib = $request->get('extlib');
|
||||
$extlibver = $request->get('extlibver');
|
||||
if($extlib == null or $extlibver != null){
|
||||
$name = $request->get('name');
|
||||
$description = $request->get('description');
|
||||
$input = htmlentities($request->get('input'));
|
||||
$output = htmlentities($request->get('output'));
|
||||
$lang = $request->get('lang');
|
||||
$code = htmlentities($request->get('code'));
|
||||
if($name == null || $description == null || $input == null ||
|
||||
$output == null || $lang == null || $code == null){
|
||||
$app->abort(400);
|
||||
}
|
||||
else{
|
||||
$idc = $app['db']->addOrModifyCodes(
|
||||
0,
|
||||
$name,
|
||||
$description,
|
||||
$input,
|
||||
$output,
|
||||
$lang,
|
||||
$code,
|
||||
$user['IDU'],
|
||||
$extlib,
|
||||
$extlibver
|
||||
);
|
||||
return $app->redirect($app['url_generator']->generate('code',array(
|
||||
"lang"=>$lang,"idc"=>$idc, "version"=>1
|
||||
)));
|
||||
}
|
||||
}else{
|
||||
$app->abort(400);
|
||||
}
|
||||
}else{
|
||||
$page=array(
|
||||
"title" => "Add your own solution",
|
||||
"description" => "Share now your snippet",
|
||||
"page" => "add",
|
||||
);
|
||||
return $app['twig']->render($app['fronthtml']."/add.twig", array("page" => $page, "user"=>$user, "supported" => $app["supported"]));
|
||||
}
|
||||
|
||||
})->bind('add')->method('GET|POST');
|
||||
Reference in New Issue
Block a user