-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaction.php
executable file
·334 lines (260 loc) · 14.5 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/*
@author: Ademcan ([email protected])
@name: action.php
@description: action class for browse snippets page
*/
include "config.php";
$lng = language();
switch ($lng) {
case "en":
require("./en.php");
break;
case "fr":
require("./fr.php");
break;
}
session_start();
$base=new SQLite3($config["dbname"]);
if(isset($_POST["action"]) && $_POST['action']=="getcode"){
$id = $_POST["id"];
// get entry
$query_language = "SELECT * FROM snippets where id = '$id' and private != 'on' ";
$results_language = $base->query($query_language);
while($row = $results_language->fetchArray())
{
// $code = iconv("ISO-8859-15", "UTF-8",$row['code']);
$code = $row['code'];
$language = $row['language'];
$username = $row['username'];
$date = $row['date'];
// $name = iconv("ISO-8859-15", "UTF-8",$row['name']);
$name = $row['name'];
$private = $row['private'];
// $description = iconv("ISO-8859-15", "UTF-8",$row['description']);
$description = $row['description'];
$rate_counter = $row['rate_counter'];
$score = $row['score'];
$query_scoring = "SELECT * FROM scoring WHERE username = '$username' ";
$results_scoring = $base->query($query_scoring);
$score_array = array();
// echo $results_scoring;
while($row2 = $results_scoring->fetchArray()){
// echo "".$row['snippet']."";
$score_array[] = $row2['snippet'];
}
echo '<div style="margin-bottom:5px;display:inline-flex;word-break: break-all;"><h2><a href="details.php?id='.$id.'">',$name ,'</a> ['.$language.'] </h2><button style="background-color:transparent; border-color:transparent;" class="btn" id="btn" data-clipboard-text="'.$code.'"><img src="images/copy.png" style="height:25px;" alt="Copy to clipboard"></button></div>';
if ($language=="html"){
$languageClass = "language-markup";
}
else if ($language=="text"){
$languageClass = "language-markup";
}
else{
$languageClass = "language-".$language;
}
echo '<div style="word-wrap: break-word;"><img src="images/calendar.png" style="padding-right:5px;"/>'.$date.' <img src="images/author.png" style="padding-left:10px;padding-right:5px;"/>'.$username.' <img src="images/tag.png" style="padding-left:10px;padding-right:5px;"/> ';
$tags = $row['tags'];
$tagsList=explode(",",$tags);
foreach($tagsList as $var){
// $lowtag = strtolower($var);
// $lowtag = str_replace(' ', '', $var);
$lowtag=trim($var);
if ($lowtag == ""){
echo 'Aucun tag ';
}
else{
echo '<a href="tags.php?tag='.$lowtag.'" style="color:black;text-decoration:underline">'.$lowtag.'</a> ';
}
}
echo "</div><br>";
echo '<img src="images/comment.png" style="vertical-align: middle;"/>';
echo ' '.nl2br($description);
echo '<section class="'.$languageClass.'"> <pre class="line-numbers"'.(($highlight!="")?" data-line=\"$highlight\"":"").'><code>'.$code.'</code></pre> </section>';
if(isset($_SESSION['valid']) && $_SESSION['valid']){
if (!in_array($id, $score_array)){
echo '<span class="rating" id="rating_div'.$id.'">
<input type="radio" class="rating-input" id="rating-input-1-5" name="rating-input-1">
<label for="rating-input-1-5" class="rating-star" score=5 snippetid="'.$id.'" onclick="rating(this);"></label>
<input type="radio" class="rating-input" id="rating-input-1-4" name="rating-input-1">
<label for="rating-input-1-4" class="rating-star" score=4 snippetid="'.$id.'" onclick="rating(this);"></label>
<input type="radio" class="rating-input" id="rating-input-1-3" name="rating-input-1">
<label for="rating-input-1-3" class="rating-star" score=3 snippetid="'.$id.'" onclick="rating(this);"></label>
<input type="radio" class="rating-input" id="rating-input-1-2" name="rating-input-1">
<label for="rating-input-1-2" class="rating-star" score=2 snippetid="'.$id.'" onclick="rating(this);"></label>
<input type="radio" class="rating-input" id="rating-input-1-1" name="rating-input-1">
<label for="rating-input-1-1" class="rating-star" score=1 snippetid="'.$id.'" onclick="rating(this);"></label>
</span>';
}
}
echo '<div id="scores'.$id.'"><b> '.$score.'</b>/5 - ['.$rate_counter.' ';
if ($rate_counter < 2){
echo ' ',$messages['rating'],'] </div>';
}
else {
echo ' ',$messages['ratings'],'] </div>';
}
echo '<br><br />';
}
}
if(isset($_POST["action"]) && $_POST['action']=="getname" ){
$language = SQLite3::escapeString($_POST["language"]);
// get entry
// if(isset($_SESSION['valid']) && $_SESSION['valid']){
// $query_language = "SELECT * FROM snippets where language = '$language' ";
// }
// else {
// $query_language = "SELECT * FROM snippets where language = '$language' AND private != 'on' ";
// }
$query_language = "SELECT * FROM snippets where language = '$language' AND private != 'on' ";
$results_language = $base->query($query_language);
echo $messages['choosesnippet'],'<br><br>
<center>
<select name="selectCode" id="selectCode" class="box" >
</center>
<option selected >'.$messages['choosesnippet'].' </option>';
while($row = $results_language->fetchArray())
{
// $name = iconv("ISO-8859-15", "UTF-8",$row['name']);
$name=$row['name'];
$id = $row['ID'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo '</select>';
}
// Scoring a snippet
if(isset($_POST["action"]) && $_POST['action']=="updatescore" ){
$rating = SQLite3::escapeString($_POST["rating"]);
$id = $_POST["id"];
$loggedin_username = $_POST["username"];
$increment_score = $_POST["rating"];
$query_language = "SELECT * FROM snippets where id = '$id' ";
$results_language = $base->query($query_language);
while($row = $results_language->fetchArray())
{
$score = $row['score'];
if ($score == 0){ $new_score = $increment_score; }
else {$new_score = ($score + $increment_score) / 2.0;}
$rate_counter = $row['rate_counter'];
$new_counter = $rate_counter + 1;
if ($new_counter < 2){echo "<b>$new_score</b>/5 - [$new_counter rating]";}
else {echo "<b>$new_score</b>/5 - [$new_counter ratings]";}
$query = "UPDATE snippets SET score='$new_score', rate_counter='$new_counter' WHERE ID='$id' ";
$results = $base->exec($query);
$update_scoring = "INSERT INTO scoring(username, snippet) VALUES('$loggedin_username', '$id')";
$results_updating = $base->exec($update_scoring);
}
}
// Search snippet
if( isset($_POST["search"]) ){
include 'includes/menu.php';
$mytable ="snippets";
$base=new SQLite3($config["dbname"]);
$search = SQLite3::escapeString($_POST['search']);
echo '<h1> Résultat de la recherche pour "'.$_POST['search'].'"</h1>';
$count_query = "SELECT count(*) as count FROM $mytable WHERE (name LIKE '%$search%' OR description LIKE '%$search%' OR language LIKE '%$search%' OR tags LIKE '%$search%' OR code LIKE '%$search%') AND private != 'on' ";
$results_count = $base->query($count_query);
$row_count = $results_count->fetchArray();
$snippets_count = $row_count['count'];
if ($snippets_count > 0 ){
$query_name = "SELECT * FROM $mytable WHERE (name LIKE '%$search%' OR description LIKE '%$search%' OR language LIKE '%$search%' OR tags LIKE '%$search%' OR code LIKE '%$search%') AND private != 'on' ";
$results_name = $base->query($query_name);
// Loop and write all the recent snippets
while($row = $results_name->fetchArray())
{
$username = $row['username'];
$name = $row['name'];
$code = $row['code'];
$language = $row['language'];
$private = $row['private'];
$lines = $row['lines'];
$highlight = $row['highlight'];
$date = $row['date'];
$id = $row['ID'];
$description = $row['description'];
$rate_counter = $row['rate_counter'];
$score = $row['score'];
echo '<div style="margin-bottom:5px;display:inline-flex; word-break: break-all;"><h2><a href="details.php?id='.$id.'">',$name ,'</a> ['.$language.'] </h2></div>';
if ($language=="html"){
$languageClass = "language-markup";
}
else if ($language=="text"){
$languageClass = "language-markup";
}
else{
$languageClass = "language-".$language;
}
echo '<div style="word-wrap: break-word;"><img src="images/calendar.png" style="padding-right:5px;"/>'.$date.' <img src="images/author.png" style="padding-left:10px;padding-right:5px;"/>'.$username.' <img src="images/tag.png" style="padding-left:10px;padding-right:5px;"/> ';
$tags = $row['tags'];
$tagsList=explode(",",$tags);
foreach($tagsList as $var){
// $lowtag = strtolower($var);
$lowtag = str_replace(' ', '', $var);
echo '<a href="tags.php?tag='.$lowtag.'" style="color:black;text-decoration:underline">'.$lowtag.'</a> ';
}
echo "</div><br>";
echo '<img src="images/comment.png" style="vertical-align: middle;"/>';
echo ' '.nl2br($description);
echo '<section class="'.$languageClass.'"> <pre class="line-numbers "'.(($highlight!="")?" data-line=\"$highlight\"":"").'><code>'.$code.'</code></pre> </section>';
if (isLoggedIn()){
$username = $_SESSION['username'];
$query_scoring = "SELECT * FROM scoring WHERE username = '$username' ";
$results_scoring = $base->query($query_scoring);
$score_array = array();
// echo $results_scoring;
while($row2 = $results_scoring->fetchArray()){
// echo "".$row['snippet']."";
$score_array[] = $row2['snippet'];
}
if (!in_array($id, $score_array)){
echo '<span class="rating" id="rating_div'.$id.'">
<input type="radio" class="rating-input" id="rating-input-1-5" name="rating-input-1">
<label for="rating-input-1-5" class="rating-star" score=5 snippetid="'.$id.'" onclick="rating(this);"></label>
<input type="radio" class="rating-input" id="rating-input-1-4" name="rating-input-1">
<label for="rating-input-1-4" class="rating-star" score=4 snippetid="'.$id.'" onclick="rating(this);"></label>
<input type="radio" class="rating-input" id="rating-input-1-3" name="rating-input-1">
<label for="rating-input-1-3" class="rating-star" score=3 snippetid="'.$id.'" onclick="rating(this);"></label>
<input type="radio" class="rating-input" id="rating-input-1-2" name="rating-input-1">
<label for="rating-input-1-2" class="rating-star" score=2 snippetid="'.$id.'" onclick="rating(this);"></label>
<input type="radio" class="rating-input" id="rating-input-1-1" name="rating-input-1">
<label for="rating-input-1-1" class="rating-star" score=1 snippetid="'.$id.'" onclick="rating(this);"></label>
</span>';
}
}
echo '<div id="scores'.$id.'"><b> '.$score.'</b>/5 - ['.$rate_counter.' ';
if ($rate_counter < 2){
echo ' rating] </div>';
}
else {
echo ' ratings] </div>';
}
echo '<br><hr ><br>';
}
}
else {
echo '<center><h3>Aucun snippet ne correspond à votre recherche</h3></center>';
}
echo '</div></body>
<script type="text/javascript">
function rating(event){
var rating= event.getAttribute("score");
var id= event.getAttribute("snippetId");
var loggedin_username = "<?php echo $loggedin_username; ?>";
// do whatever you want with the checked radio
var xhr2 = new XMLHttpRequest();
xhr2.onreadystatechange = function() {
if (xhr2.readyState == 4 && (xhr2.status == 200 || xhr2.status == 0)) {
document.getElementById("rating_div"+id.toString()+"").innerHTML = "";
document.getElementById("scores"+id.toString()+"").innerHTML = "";
document.getElementById("scores"+id.toString()+"").innerHTML = xhr2.responseText;
Prism.highlightAll();
}
};
xhr2.open("POST", "action.php", true);
xhr2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr2.send("rating="+rating+"&action=updatescore&id="+id+"&username="+loggedin_username+"");
}
</script>
</html>';
}
?>