-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.php
executable file
·226 lines (180 loc) · 8.58 KB
/
index.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
<?php
/*
@author: Ademcan ([email protected])
@name: index.php
@description: initial page
*/
include "config.php";
$lng = language();
switch ($lng) {
case "en":
require("./en.php");
break;
case "fr":
require("./fr.php");
break;
}
date_default_timezone_set("UTC");
session_start();
// date_default_timezone_set("UTC");
if (!file_exists("snippets.sqlite")) {
echo "<script>location.href='install.php';</script>";
} else {
include 'includes/menu.php';
$mytable ="snippets";
$base=new SQLite3($config["dbname"]);
$count_query = "SELECT count(*) as count FROM $mytable";
$results_count = $base->query($count_query);
$row_count = $results_count->fetchArray();
$snippets_count = $row_count['count'];
$limit = 10;
$page = 1;
if (isset($_GET['page'])){
$page = intval($_GET['page']);
}
$start_count = $limit * ($page-1);
// if logged in, the user can see the private snippets
// if(isset($_SESSION['valid']) && $_SESSION['valid']){
// $query_name = "SELECT * FROM $mytable ORDER BY date DESC LIMIT $start_count,$limit";
// }
// // if not logged in, select only public snippets
// else {
// $query_name = "SELECT * FROM $mytable WHERE private != 'on' ORDER BY date DESC LIMIT $start_count,$limit";
// }
// check the snippets that were already scored by the user
$loggedin_username = $_SESSION['username'];
$query_scoring = "SELECT * FROM scoring WHERE username = '$loggedin_username' ";
$results_scoring = $base->query($query_scoring);
$score_array = array();
// echo $results_scoring;
while($row = $results_scoring->fetchArray()){
// echo "".$row['snippet']."";
$score_array[] = $row['snippet'];
}
// $date = date("F j, Y - H:i");
// DATE_FORMAT(STR_TO_DATE(date, '%M %e, %Y - %H:%i'),'%M %e, %Y - %H:%i' )
// cast([date] as datetime)
$query_name = "SELECT * FROM $mytable WHERE private != 'on' ORDER BY date DESC LIMIT $start_count,$limit";
$results_name = $base->query($query_name);
$settingsQuery = "SELECT * FROM settings";
$settingsInfo = $base->query($settingsQuery);
$settings = $settingsInfo->fetchArray();
$title = $settings["title"];
echo '<h1>{ '.$title.' }</h1>';
if($snippets_count == 0){
echo $messages['nosnippetyet'];
}
// 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;padding-top:20px;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.'" title="Copy to clipboard"><img src="images/copy.png" style="height:25px;"></button></div>';
if ($language=="html"){
$languageClass = "language-markup";
}
else if ($language=="text"){
$languageClass = "language-markup";
}
else{
$languageClass = "language-".$language;
}
$date=date_create($date);
$formattedDate = date_format($date,"d.m.Y");
echo '<div style="word-wrap: break-word;"><img src="images/calendar.png" style="padding-right:5px;"/>'.$formattedDate.' <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);
$lowtag = str_replace(' ', '', $var);
if ($lowtag == ""){
echo $messages['notag'],' ';
}
else{
echo '<a href="tags.php?tag='.rawurlencode($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()){
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><hr ><br>';
}
echo "<br><br>";
// Pagination
// First page
if($snippets_count > $limit & $page == 1){
echo '<center><a href= "index.php?page=2"> ',$messages['oldersnippets'],' >>> </a></center>';
}
// Last page
if($page > 1 & $snippets_count <= ($limit*$page) & $snippets_count > ($limit*($page-1)) ){
echo '<center><a href= "index.php?page='.($page-1).'"> <<< ',$messages['oldersnippets'],' </a></center>';
}
// Middle page
if($page > 1 & $snippets_count > ($limit*$page)){
echo '<center><a href= "index.php?page='.($page-1).'"> <<< ',$messages['newestsnippets'],'</a> -- <a href="index.php?page='.($page+1).'">',$messages['oldersnippets'],' >>></a></center>';
}
echo '<center><font size="3">Powered by <a href="https://www.cansnippet.org/">canSnippet</a> CE - by <a href="https://ademcan.net/">ademcan</a><font></center>';
echo '<br></div> </body></html>';
}
echo '';
?>
<script>
var clipboard = new Clipboard('.btn');
</script>
<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+"");
// only one radio can be logically checked, don't check the rest
}
</script>