#13 and #18 finished and PreReleased launch

This commit is contained in:
José Luis Garrido Labrador
2018-10-21 20:48:35 +02:00
parent cb7a16984b
commit d1353bd1cf
15 changed files with 450 additions and 100 deletions

View File

@@ -0,0 +1,26 @@
.myAlert-top{
position: fixed;
top: 5px;
left:2%;
width: 96%;
}
.myAlert-bottom{
position: fixed;
bottom: 5px;
left:2%;
width: 96%;
}
div.fullscreen {
position: absolute;
width:100%;
height:960px;
top: 0;
left: 0;
background-color: lightblue;
}
.alert{
display: none;
}

View File

@@ -5,8 +5,10 @@
<link type="text/css" rel="stylesheet" href="{{ app.frontcss }}/bootstrap-paper.css"/> <link type="text/css" rel="stylesheet" href="{{ app.frontcss }}/bootstrap-paper.css"/>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="{{ app.frontcss }}/main.css"/> <link type="text/css" rel="stylesheet" href="{{ app.frontcss }}/main.css"/>
<link type="text/css" rel="stylesheet" href="{{ app.frontcss }}/alert.css"/>
<link type="text/css" rel="stylesheet" href="{{ app.frontcss }}/codeStyles/vs.css"/> <link type="text/css" rel="stylesheet" href="{{ app.frontcss }}/codeStyles/vs.css"/>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" /> <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
{#<link rel="shortcut icon" href="{{ path.img }}favicon.ico" type="image/x-icon"/>#} {#<link rel="shortcut icon" href="{{ path.img }}favicon.ico" type="image/x-icon"/>#}
{% block css %}{% endblock %} {% block css %}{% endblock %}
@@ -19,6 +21,7 @@
<script src="{{ app.frontjs }}/bootstrap.min.js"></script> <script src="{{ app.frontjs }}/bootstrap.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js'></script> <script src='https://www.google.com/recaptcha/api.js'></script>
<script src="{{ app.frontjs }}/functions.js"></script> <script src="{{ app.frontjs }}/functions.js"></script>
<script src="{{ app.frontjs }}/like.js"></script>
</head> </head>
<body> <body>
{% include app.fronthtml~'/nav.twig' %} {% include app.fronthtml~'/nav.twig' %}
@@ -26,6 +29,10 @@
{% block content %}{% endblock %} {% block content %}{% endblock %}
<div class="myAlert-bottom alert alert-dismissible alert-danger">
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Oh snap!</strong> Something wrong
</div>
{% include app.fronthtml~"/footer.twig" %} {% include app.fronthtml~"/footer.twig" %}
{% block js %} {% block js %}

View File

@@ -1,35 +1,45 @@
<table style="width: 100%; text-align:center"> <div class="container-fluid" style="width: 100%; text-align:center">
<tr> <div class="row">
<th> <div class="col-lg-offset-2 col-md-offset-2 col-sm-offset-2 col-xs-offset-2 col-lg-8 col-md-8 col-sm-8 col-xs-8">
<a href="{{ path('code',{'idc': code.idc,'lang': code.lang,'version': code.version}) }}">{{ code.name }}</a> <h5><a href="{{ path('code',{'idc': code.idc,'lang': code.lang,'version': code.version}) }}">{{ code.name }}</a></h5>
</th> </div>
</tr> <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2" style="text-align: right">
<tr> <h4>
<td><strong>Author:</strong>&nbsp{{ code.nick }} <strong> Version: </strong>{{ code.version }} <strong> <a href="javascript:;" onclick="likeAndSave('l',{{ code.idc }},'{{ code.lang }}'{% if user.nick is defined %},'{{ user.nick }}'{% endif %})"
Lang: </strong>{{ code.lLang }}</td> data-toggle="tooltip" data-placement="top" title="" data-original-title="Like">
</tr> <i class="{% if code.like == 1 %}fas{% else %}far{% endif %} fa-heart" id="l-{{ code.idc }}-{{ code.lang|raw }}" style="padding-right: 0.05em;"></i>
{% if code.extlib is defined and code.extlib != "" %} </a>
<tr> <a href="javascript:;" onclick="likeAndSave('s',{{ code.idc }},'{{ code.lang }}'{% if user.nick is defined %},'{{ user.nick }}'{% endif %})"
<td> data-toggle="tooltip" data-placement="top" title="" data-original-title="Save">
<strong>External library:</strong>&nbsp{{ code.extlib }} <i class="{% if code.save == 1 %}fas{% else %}far{% endif %} fa-bookmark" id="s-{{ code.idc }}-{{ code.lang }}"></i>
{% if code.extlibver is not null %} </a>
<strong> Version used: </strong>{{ code.extlibver }} </h4>
</div>
</div>
{% if page.page!="home"%}
{% if code.extlib is defined and code.extlib != "" %}
<div class="row">
<strong>External library:</strong>&nbsp{{ code.extlib }}
{% if code.extlibver is not null %}
<strong> Version used: </strong>{{ code.extlibver }}
{% endif %}
<div/>
</div>
{% endif %} {% endif %}
</td>
</tr>
{% endif %} {% endif %}
<tr> <div class="row">
<th colspan="6">Description</th> <div class="col-lg-offset-3 col-md-offset-3 col-sm-offset-3 col-xs-offset-3 col-lg-6 col-md-6 col-sm-6 col-xs-6">
</tr> {% if page.page!="home"%}<strong>Description:</strong> {{ code.description }}{% endif %}
<tr> </div>
<td colspan="6">{{ code.description }}</td>
</tr> </div>
<tr> <div class="row">
<th colspan="6">Code</th> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="text-align: left"><strong>{{ code.lLang }}</strong></div>
</tr> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="text-align: right"><strong>{{ code.nick }}</strong></div>
<tr> </div>
<td colspan="6"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<pre class="highlight"><code class='{{ code.lang }} codeto toHightlight'>{{ code.code }}</code></pre> <pre class="highlight"><code class='{{ code.lang }} codeto toHightlight'>{{ code.code }}</code></pre>
</td> </div>
</tr> </div>
</table> </div>

View File

@@ -0,0 +1,48 @@
{#<table style="width: 100%; text-align:center">
<tr>
<th>
<h4><a href="{{ path('code',{'idc': code.idc,'lang': code.lang,'version': code.version}) }}">{{ code.name }}</a></h4>
</th>
</tr>
<tr>
<td><strong>Author:</strong>&nbsp{{ code.nick }} <strong> Version: </strong>{{ code.version }} <strong>
Lang: </strong>{{ code.lLang }}</td>
</tr>
<tr>
<td colspan="6">
<pre class="highlight"><code class='{{ code.lang }} codeto toHightlight'>{{ code.code }}</code></pre>
</td>
</tr>
</table>#}
<div class="container-fluid" style="width: 100%; text-align:center">
<div class="row">
<div class="col-lg-12"><h5><a href="{{ path('code',{'idc': code.idc,'lang': code.lang,'version': code.version}) }}">{{ code.name }}</a></h5></div>
</div>
<div class="row">
<div class="col-lg-12"></div>
</div>
{% if page.page!=home%}
{% if code.extlib is defined and code.extlib != "" %}
<div class="row">
<strong>External library:</strong>&nbsp{{ code.extlib }}
{% if code.extlibver is not null %}
<strong> Version used: </strong>{{ code.extlibver }}
{% endif %}
<div/>
</div>
{% endif %}
<div class="row">
<strong>Description:</strong> {{ code.description }}
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="text-align: left"><strong>{{ code.lLang }}</strong></div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="text-align: right"><strong>{{ code.nick }}</strong></div>
</div>
{% endif %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<pre class="highlight"><code class='{{ code.lang }} codeto toHightlight'>{{ code.code }}</code></pre>
</div>
</div>
</div>

View File

@@ -12,6 +12,7 @@
<p class="text-danger loginMessage" id="POST_ERROR">Error during sign-up, check your data</p> <p class="text-danger loginMessage" id="POST_ERROR">Error during sign-up, check your data</p>
<p class="text-danger loginMessage" id="MAIL_IN_USE">The e-mail is currently in use</p> <p class="text-danger loginMessage" id="MAIL_IN_USE">The e-mail is currently in use</p>
<p class="text-danger loginMessage" id="NICK_IN_USE">The nickname is currently in use</p> <p class="text-danger loginMessage" id="NICK_IN_USE">The nickname is currently in use</p>
<p class="text-danger loginMessage" id="CONFIRM_MAIL">Please, confirm your email before</p>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -25,7 +25,7 @@
<script> <script>
$(document).ready(function() { $(document).ready(function() {
var options = {success: function(data){ var options = {success: function(data){
console.log(data); window.location.href = '{{path('home')}}';
}, },
url: '{{ path("restore")}}', url: '{{ path("restore")}}',
} ; } ;

View File

@@ -0,0 +1,16 @@
<div class="container">
<div class="row">
<h5 class="col-sm-offset-1">Saved codes</h5>
</div>
<div class="row">
<div class="col-sm-offset-1">
<ul class="list-group">
{% for s in saved %}
<li class="list-group-item d-flex justify-content-between align-items-center">
<a href="{{ path('code',{'idc': s.idc,'lang': s.lang,'version': s.version}) }}">{{ s.name }} on {{ s.lLang }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>

View File

@@ -3,50 +3,77 @@
<h4 style="text-align: center" xmlns="http://www.w3.org/1999/html">{{ user.nick }}</h4> <h4 style="text-align: center" xmlns="http://www.w3.org/1999/html">{{ user.nick }}</h4>
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<p class="col-md-1"><strong>Nickname</strong></p> <ul class="nav nav-tabs" role="tablist">
<p class="col-md-11">{{ user.nick }}</p> <li class="nav-item">
</div> <a class="navbar-link active" href="#data" role="tab" data-toggle="tab">My information</a>
<div class="row"> </li>
<p class="col-md-1"><strong>Email</strong></p> <li class="nav-item">
<p class="col-md-11">{{ user.email }}</p> <a class="navbar-link" href="#saved" role="tab" data-toggle="tab">Saved codes</a>
</div> </li>
<hr/> <li class="nav-item">
</div> <a class="navbar-link" href="#password" role="tab" data-toggle="tab">Change password</a>
<div class="container"> </li>
{% if page.status == 1 %} </ul>
<p class="text-success">Password updated successfully</p> <div class="tab-content">
{% elseif page.status == 2 %} <div role="tabpanel" class="tab-pane fade in active" id="data">
<p class="text-danger">Old password incorrect</p> <div class="container">
{% endif %} <div class="row">
<div class="row"> <h5 class="col-sm-offset-1">My information</h5>
<h5 class="col-sm-offset-1">Change password</h5> </div>
</div> <div class="row">
<div class="row"> <p class="col-md-1 col-sm-offset-1"><strong>Nickname</strong></p>
<form class="form-horizontal" method="post"> <p class="col-md-10">{{ user.nick }}</p>
<div class="form-group"> </div>
<label class="control-label col-sm-2" for="passact">Current password:</label> <div class="row">
<div class="col-sm-10"> <p class="col-md-1 col-sm-offset-1"><strong>Email</strong></p>
<input type="password" class="form-control" required="required" name="passact" id="passact" placeholder="Current password"> <p class="col-md-10">{{ user.email }}</p>
</div>
<hr/>
</div> </div>
</div> </div>
<div class="form-group"> <div role="tabpanel" class="tab-pane fade" id="saved">
<label class="control-label col-sm-2" for="newpass">New password:</label> {% include app.fronthtml~'/saved.twig' %}
<div class="col-sm-10"> </div>
<input type="password" class="form-control" required="required" name="newpass" id="newpass" onchange="onPasswordChange()" placeholder="New password"> <div role="tabpanel" class="tab-pane fade" id="password">
<div class="container">
{% if page.status == 1 %}
<p class="text-success">Password updated successfully</p>
{% elseif page.status == 2 %}
<p class="text-danger">Old password incorrect</p>
{% endif %}
<div class="row">
<h5 class="col-sm-offset-1">Change password</h5>
</div>
<div class="row">
<form class="form-horizontal" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="passact">Current password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" required="required" name="passact" id="passact" placeholder="Current password">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="newpass">New password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" required="required" name="newpass" id="newpass" onchange="onPasswordChange()" placeholder="New password">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="newpass-re">Repeat new password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" required="required" name="newpass-re" id="newpass-re" onchange="onPasswordChange()" placeholder="Repear new password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</div> </div>
</div> </div>
<div class="form-group"> </div>
<label class="control-label col-sm-2" for="newpass-re">Repeat new password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" required="required" name="newpass-re" id="newpass-re" onchange="onPasswordChange()" placeholder="Repear new password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

69
Source/assets/js/like.js Normal file
View File

@@ -0,0 +1,69 @@
function likeAndSave(m,c,l,i=null){
var b=$('#'+m+"-"+c+"-"+l);
var data = {};
switch (m) {
case 'l':
if(b.hasClass('fas')){
data.mode = "unvote";
data.before = 'fas';
data.after = 'far';
}else{
data.mode = "vote";
data.before = 'far';
data.after = 'fas';
}
break;
case 's':
if(b.hasClass('fas')){
data.mode = "unsave";
data.before = 'fas';
data.after = 'far';
}else{
data.mode = "save";
data.before = 'far';
data.after = 'fas';
}
break;
}
if (i==null){
window.location.replace('/login')
}
data.idc=c;
data.lang=l;
$.ajax({
url: '/vote-save',
type: 'POST',
contentType: "application/json",
data: JSON.stringify(data),
traditional: true,
success: function(result) {
console.log(result);
if (result == 0){
b.addClass(data.after);
b.removeClass(data.before);
}
else
myAlertBottom();
},
error: function (result) {
myAlertBottom();
}
});
}
function myAlertTop(){
$(".myAlert-top").fadeToggle();
setTimeout(function(){
$(".myAlert-top").fadeToggle();
}, 2000);
}
function myAlertBottom(){
$(".myAlert-bottom").fadeToggle();
setTimeout(function(){
$(".myAlert-bottom").fadeToggle();
}, 2000);
}

2
Source/composer.lock generated
View File

@@ -1,7 +1,7 @@
{ {
"_readme": [ "_readme": [
"This file locks the dependencies of your project to a known state", "This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "1d57e016803e2095db2110462e070da3", "content-hash": "1d57e016803e2095db2110462e070da3",

View File

@@ -497,9 +497,12 @@ class DB
$libV = ""; $libV = "";
} }
$save = false;
$version = $this->getLastVersion($IDC, $lang); $version = $this->getLastVersion($IDC, $lang);
if ($version == null) if ($version == null){
$save = true;
$version = 0; $version = 0;
}
$version++; $version++;
$modification = time(); $modification = time();
$queryBuilder = $this->newQueryBuilder(); $queryBuilder = $this->newQueryBuilder();
@@ -519,6 +522,9 @@ class DB
) )
->setParameters(array($IDC,$lang,$version,$modification,$code,$lib,$libV,$IDU)); ->setParameters(array($IDC,$lang,$version,$modification,$code,$lib,$libV,$IDU));
$this->execute($queryBuilder); $this->execute($queryBuilder);
if($save){
$this->save($IDU, $IDC, $lang);
}
return $version; return $version;
} }
@@ -705,6 +711,7 @@ class DB
*/ */
public function register($email, $pass, $nick) public function register($email, $pass, $nick)
{ {
$token = RandomString(50);
if ($this->loadIDU($email)) if ($this->loadIDU($email))
return 'MAIL_IN_USE'; return 'MAIL_IN_USE';
else if ($this->loadIDU($nick,'nick')) else if ($this->loadIDU($nick,'nick'))
@@ -717,10 +724,12 @@ class DB
->values(array( ->values(array(
'email' => '?', 'email' => '?',
'pass' => '?', 'pass' => '?',
'nick' => '?' 'nick' => '?',
'token' => '?'
)) ))
->setParameters(array($email,$password,$nick)); ->setParameters(array($email,$password,$nick,hash('sha256',$token)));
$this->execute($queryBuilder); $this->execute($queryBuilder);
newUser($email, $nick, $token);
return 'CORRECT'; return 'CORRECT';
} }
} }
@@ -743,6 +752,24 @@ class DB
$this->execute($queryBuilder); $this->execute($queryBuilder);
} }
/**
* Set role
*
* @param $IDU user's identifier
* @param $role Role
*/
public function setRole($IDU, $role=1)
{
$queryBuilder = $this->newQueryBuilder();
$queryBuilder
->update('Users')
->set('ROLE','?')
->where($queryBuilder->expr()->eq('IDU','?'))
->setParameter(0,$role)
->setParameter(1,$IDU);
$this->execute($queryBuilder);
}
/** /**
* Change password * Change password
* *
@@ -764,9 +791,9 @@ class DB
/** /**
* Check if token to restore password is correct * Check if token to restore password is correct
* *
* @param string $nick string nick to restore password * @param $nick string $nick string nick to restore password
* @param string $token random string generate * @param $token string $token random string generate
* @param int $timestamp moment when restore activate * @param $timestamp int $timestamp moment when restore activate
* @return bool true is are similar * @return bool true is are similar
*/ */
public function checkRestoreToken($nick,$token,$timestamp){ public function checkRestoreToken($nick,$token,$timestamp){
@@ -780,6 +807,24 @@ class DB
return $tk == hash('sha256',"$token-$timestamp"); return $tk == hash('sha256',"$token-$timestamp");
} }
/**
* Check if token to confirm is correct
*
* @param $token string $token random string generate
* @param $idu int user identifier
* @return bool true is are similar
*/
public function checkConfirmToken($idu,$token){
$queryBuilder = $this->newQueryBuilder();
$queryBuilder
->select('token')
->from('Users')
->where($queryBuilder->expr()->eq('idu','?'))
->setParameter(0,$idu);
$tk = $this->getData($queryBuilder)[0]['token'];
return $tk == hash('sha256',"$token");
}
/** /**
* Generate a random token to restore password * Generate a random token to restore password
* *
@@ -841,7 +886,7 @@ class DB
* @return int 0 if correct, 1 if error * @return int 0 if correct, 1 if error
*/ */
public function vote($idu,$idc,$lang){ public function vote($idu,$idc,$lang){
return $this->voteOrSave($idu,$idc,$lang,"Votes"); return $this->voteOrSave($idu,$idc,$lang,"Likes");
} }
/** /**
@@ -892,7 +937,7 @@ class DB
* @return int 0 if correct, 1 if error * @return int 0 if correct, 1 if error
*/ */
public function unvote($idu,$idc,$lang){ public function unvote($idu,$idc,$lang){
return $this->unvoteOrUnsave($idu,$idc,$lang,"Votes"); return $this->unvoteOrUnsave($idu,$idc,$lang,"Likes");
} }
/** /**
@@ -917,18 +962,20 @@ class DB
$queryBuilder = $this->newQueryBuilder(); $queryBuilder = $this->newQueryBuilder();
$subQuery = $this->newQueryBuilder(); $subQuery = $this->newQueryBuilder();
$queryBuilder $queryBuilder
->select(array('s.IDC','s.Lang','s.Version')) ->select(array('s.IDC','s.Lang','s.Version','c.Name'))
->from('Saves','sa') ->from('Saves','sa')
->join('sa','Sources','s', ->join('sa','Sources','s',
$queryBuilder->expr()->andX( $queryBuilder->expr()->andX(
$queryBuilder->expr()->eq("sa.IDC","s.IDC"), $queryBuilder->expr()->eq("sa.IDC","s.IDC"),
$queryBuilder->expr()->eq("sa.Lang","s.Lang") $queryBuilder->expr()->eq("sa.Lang","s.Lang")
)) ))
->join('s','Codes','c',
$queryBuilder->expr()->eq("s.IDC",'c.IDC'))
->where( ->where(
$queryBuilder->expr()->andX( $queryBuilder->expr()->andX(
$queryBuilder->expr()->eq("IDU",'?'), $queryBuilder->expr()->eq("sa.IDU",'?'),
$queryBuilder->expr()->eq( $queryBuilder->expr()->eq(
'so.Version','('. 's.Version','('.
$subQuery $subQuery
->select('MAX(Version)') ->select('MAX(Version)')
->from('Sources') ->from('Sources')
@@ -993,7 +1040,7 @@ class DB
* @return int 0 or 1 * @return int 0 or 1
*/ */
public function iVote($idu,$idc,$lang){ public function iVote($idu,$idc,$lang){
return $this->iVoteSave($idu,$idc,$lang,"Votes"); return $this->iVoteSave($idu,$idc,$lang,"Likes");
} }
/** /**

View File

@@ -7,11 +7,13 @@ require_once __DIR__ .'/../vendor/autoload.php';
require_once __DIR__ . '/Config.php'; require_once __DIR__ . '/Config.php';
require 'functions.php'; require 'functions.php';
require 'domails.php';
require 'DB.php'; require 'DB.php';
require 'dbToTable.php'; require 'dbToTable.php';
require 'users.php'; require 'users.php';
$app = new Silex\Application(); $app = new Silex\Application();
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@@ -54,6 +56,11 @@ $app->boot();
$app->get('/', function (Request $request) use($app) { $app->get('/', function (Request $request) use($app) {
$user = getUser($app); $user = getUser($app);
$idu=0;
if($user != null){
$idu=$app['data']->loadIDU($user['nick'],'nick');
}
$ajax=false; $ajax=false;
if($request->getMethod()=="POST"){ if($request->getMethod()=="POST"){
if($request->get('globalSearch')!=null){ if($request->get('globalSearch')!=null){
@@ -65,10 +72,10 @@ $app->get('/', function (Request $request) use($app) {
$ajax=true; $ajax=true;
} }
$last = $app['data']->loadFilter($data,$type); $last = $app['data']->loadFilter($data,$type);
$last = lastToArray($last,$app['supported']); $last = lastToArray($last,$app['supported'],$app['data'],$idu);
}else{ }else{
$query = $app['data']->loadLast(); $query = $app['data']->loadLast();
$last = lastToArray($query,$app['supported']); $last = lastToArray($query,$app['supported'],$app['data'],$idu);
} }
$page = array( 'title' => "CodeShare", $page = array( 'title' => "CodeShare",
"description" => "Sharing your solutions with all", "description" => "Sharing your solutions with all",
@@ -177,8 +184,11 @@ function loginOrRegister($state,$request){
case 3: case 3:
return loginRegister('login','BAD_CREDENTIAL'); return loginRegister('login','BAD_CREDENTIAL');
case 1: case 1:
if(checklogin($request->get('email'),$request->get('pwd'),$app['data'])==''){ $result = checklogin($request->get('email'),$request->get('pwd'),$app['data']);
if($result==''){
return 'redirect'; return 'redirect';
}else if($result=='VALIDATE'){
return loginRegister('login','CONFIRM_MAIL');
}else{ }else{
return loginRegister("login",'BAD_CREDENTIAL'); return loginRegister("login",'BAD_CREDENTIAL');
} }
@@ -221,7 +231,8 @@ $app->get('/user', function(Request $request) use ($app){
"description" => "NOT RELEVANT", "description" => "NOT RELEVANT",
"status" => $status "status" => $status
); );
return $app['twig']->render($app['fronthtml']."/user.twig",array("page"=>$page,"user"=>$user)); $saved = $app['data']->allSaves($app['data']->loadIDU($user['nick'],'nick'));
return $app['twig']->render($app['fronthtml']."/user.twig",array("page"=>$page,"user"=>$user,"saved"=>savedToArray($saved,$app['supported'])));
})->bind('user')->method('GET|POST'); })->bind('user')->method('GET|POST');
@@ -300,7 +311,11 @@ $app->get('/code/{lang}-{idc}-{version}', function (Request $request, $lang, $id
"existedLangs" => $app['data']->loadLangs($idc), "existedLangs" => $app['data']->loadLangs($idc),
"original" => $app['data']->loadOriginalAuthor($idc), "original" => $app['data']->loadOriginalAuthor($idc),
); );
$code = allCodeToArray($array,$app['supported']); $idu=0;
if($user != null){
$idu=$app['data']->loadIDU($user['nick'],'nick');
}
$code = allCodeToArray($array,$app['supported'],$app['data'],$idu);
if($array['Version']!=1) if($array['Version']!=1)
$page['otherV'] = true; $page['otherV'] = true;
$diff = $app['data']->loadDiff($idc,$lang); $diff = $app['data']->loadDiff($idc,$lang);
@@ -418,7 +433,17 @@ $app->get('/add', function(Request $request) use($app){
$app->get('/restore', function(Request $request) use($app){ $app->get('/restore', function(Request $request) use($app){
if($request->getMethod()=="POST"){ if($request->getMethod()=="POST"){
return $app['data']->createRestoreToken($request->get('emailre')); $parts = $app['data']->createRestoreToken($request->get('emailre'));
$parts = explode("-", $parts);
$token = $parts[0];
$timestamp = $parts[1];
restorePassword(
$request->get('emailre'),
$app['data']->loadProfile($app['data']->loadIDU($request->get('emailre')))['nick'],
$token,
$timestamp
);
return "UPDATE";
} }
$page = array("title"=>"Restore your password", $page = array("title"=>"Restore your password",
"description"=>"None", "description"=>"None",
@@ -464,6 +489,16 @@ $app->get('/restore/{nick}/{token}-{timestamp}', function(Request $request, $nic
} }
})->bind('restorePass')->method('GET|POST'); })->bind('restorePass')->method('GET|POST');
$app->get('/confirm/{nick}-{token}', function(Request $request, $nick, $token) use($app){
$idu=$app['data']->loadIDU($nick,'nick');
$res = $app['data']->checkConfirmToken($idu,$token);
if($res)
$app['data']->setRole($idu);
else
$app->abort(401);
return $app->redirect($app['url_generator']->generate('login'));
})->bind('confirm');
/* Votes and saves */ /* Votes and saves */
$app->post('/vote-save',function(Request $request) use($app){ $app->post('/vote-save',function(Request $request) use($app){
@@ -474,15 +509,16 @@ $app->post('/vote-save',function(Request $request) use($app){
$data = str_replace('&quot;','"',$request->getContent()); $data = str_replace('&quot;','"',$request->getContent());
$data = json_decode($data,true); $data = json_decode($data,true);
$idu = $app['data']->loadIDU($user['nick'],'nick');
switch($data['mode']){ switch($data['mode']){
case "vote": case "vote":
return $app['data']->vote($user['idu'],$data['idc'],$data['lang']); return $app['data']->vote($idu,$data['idc'],$data['lang']);
case "unvote": case "unvote":
return $app['data']->unvote($user['idu'],$data['idc'],$data['lang']); return $app['data']->unvote($idu,$data['idc'],$data['lang']);
case "save": case "save":
return $app['data']->save($user['idu'],$data['idc'],$data['lang']); return $app['data']->save($idu,$data['idc'],$data['lang']);
case "unsave": case "unsave":
return $app['data']->unsave($user['idu'],$data['idc'],$data['lang']); return $app['data']->unsave($idu,$data['idc'],$data['lang']);
} }
return 1; return 1;

View File

@@ -11,10 +11,16 @@
* Methods to get an array to send to twig * Methods to get an array to send to twig
*/ */
function lastToArray(&$query,$supported){ function lastToArray(&$query,$supported,$db,$idu=0){
$last = array(); $last = array();
$list = $query->fetchAll(); $list = $query->fetchAll();
foreach($list as $var){ foreach($list as $var){
$like=0;
$save=0;
if($idu!=0){
$like = $db->iVote($idu,$var['IDC'],$var['Lang']);
$save = $db->iSave($idu,$var['IDC'],$var['Lang']);
}
$array = array $array = array
( "idc" => $var["IDC"], ( "idc" => $var["IDC"],
"lang" => $var["Lang"], "lang" => $var["Lang"],
@@ -24,13 +30,21 @@ function lastToArray(&$query,$supported){
"lLang" => $supported[$var["Lang"]][0], "lLang" => $supported[$var["Lang"]][0],
"description" => $var["Description"], "description" => $var["Description"],
"code" => html_entity_decode($var["Code"]), "code" => html_entity_decode($var["Code"]),
"like" => $like,
"save" => $save
); );
array_push($last,$array); array_push($last,$array);
} }
return $last; return $last;
} }
function allCodeToArray(&$query,$supported){ function allCodeToArray(&$query,$supported,$db,$idu=0){
$like=0;
$save=0;
if($idu!=0){
$like = $db->iVote($idu,$query['IDC'],$query['Lang']);
$save = $db->iSave($idu,$query['IDC'],$query['Lang']);
}
$code = array( $code = array(
"idc" => $query['IDC'], "idc" => $query['IDC'],
"lang" => $query['Lang'], "lang" => $query['Lang'],
@@ -46,6 +60,8 @@ function allCodeToArray(&$query,$supported){
"rows" => substr_count($query['Code'],"\n"), "rows" => substr_count($query['Code'],"\n"),
"extlib" => $query['UseExtLib'], "extlib" => $query['UseExtLib'],
"extlibver" => $query['UseExtLibVer'], "extlibver" => $query['UseExtLibVer'],
"like" => $like,
"save" => $save
); );
return $code; return $code;
@@ -65,6 +81,22 @@ function otherImplementationToArray(&$query,$supported){
return $other; return $other;
} }
function savedToArray($query,$supported){
$saved = array();
foreach($query as $var){
$array = array
(
"idc" => $var['IDC'],
"lang" => $var['Lang'],
"version" => $var['Version'],
"lLang" => $supported[$var["Lang"]][0],
"name" => $var['Name']
);
array_push($saved,$array);
}
return $saved;
}
function otherVersionToArray(&$query,$supported){ function otherVersionToArray(&$query,$supported){
return lastToArray($query,$supported); return lastToArray($query,$supported);
} }

29
Source/src/domails.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
function newUser($email,$nick,$token){
global $app;
$header = 'From: CodeShare ' . "contacto@jkanetwork.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=UTF-8";
$to = $email;
$url = $app['url_generator']->generate('confirm',array('nick'=>$nick,'token'=>$token));
$correo = "<p>Wellcome to CodeShare $nick, the distribution platform for code fragments</p>
<p>To confirm your account please, click in the next link: <a href='https://codeshare.jkanetwork.com/$url'>https://codeshare.jkanetwork.com/$url</a></p>";
mail($to,"Confirm account to $nick",$correo,$header);
}
function restorePassword($email,$nick,$token,$timestamp){
global $app;
$header = 'From: CodeShare ' . "contacto@jkanetwork.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=UTF-8";
$to = $email;
$url = $app['url_generator']->generate('restorePass',array('nick'=>$nick,'token'=>$token,'timestamp' => $timestamp));
$correo = "<p>Someone requested that the password be reset for you</p>
<p>If this was a mistake, just ignore this email and nothing will happen.</p>
<p>To restore your password please, click in the next link: <a href='https://codeshare.jkanetwork.com/$url'>https://codeshare.jkanetwork.com/$url</a></p>";
mail($to,"Restore password to $nick",$correo,$header);
}

View File

@@ -24,11 +24,13 @@ function checkInfo($request){
* @param $pass user's input password * @param $pass user's input password
* @return bool true if email-password combination is correct, false if not * @return bool true if email-password combination is correct, false if not
*/ */
function checklogin($email,$pass,$db){ function checklogin($email,$pass,DB $db){
$bool = $db->checkPass($email,$pass); $bool = $db->checkPass($email,$pass);
if($bool){ if($bool){
$idu = $db->loadIDU($email); $idu = $db->loadIDU($email);
$ROLE = $db->loadProfile($idu)['ROLE'];
if($ROLE==0)
return 'VALIDATE';
$token = RandomString(50); $token = RandomString(50);
$db->setToken($idu,$token); $db->setToken($idu,$token);
$_SESSION['token'] = $token; $_SESSION['token'] = $token;