This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
np-functions.php
517 lines (446 loc) · 15.6 KB
/
np-functions.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php
/*
* NPChan
* Author: Aadarsha Paudel
* License: GNU GPL v3
* URL: http://github.com/npchan/npchan
* -----------------------------------------
* Functions (PHP)
* -----------------------------------------
* Contributions:
*
* -----------------------------------------
* This file contains all the PHP functions.
* Note: Just to let you know, I have no idea how some of the functions below work.
* They are here because they just works and I am fine with it.
*/
// prevent direct access to script
if(!defined("AUTH")){die;}
/*
* Unicodify
* Copied from vichan
* Licensed Under MIT
* Project Upstream URL: https://github.com/vichan-devel/vichan
*/
function unicodify($body) {
$body = str_replace('...', '…', $body);
$body = str_replace('<--', '←', $body);
$body = str_replace('-->', '→', $body);
$body = str_replace('---', '—', $body);
$body = str_replace('--', '–', $body);
$body = str_replace('<', '<', $body);
return $body;
}
/*
* UTF8 to HTML
* Copied from vichan
* Licensed Under MIT
* Project Upstream URL: https://github.com/vichan-devel/vichan
*/
function utf8tohtml($utf8) {
return htmlspecialchars($utf8, ENT_NOQUOTES, 'UTF-8');
}
/*
* Sanitize user Input Data
* Why Required? For future "filtering" if any bugs found. Its easy to edit anything here
* than to change the whole code
*/
function filter_text($text){
return trim(strip_tags($text));
}
/* Get Exact User IP (Bypass possible proxies) */
function GetIP(){
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
if (array_key_exists($key, $_SERVER) === true){
foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip){
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
return $ip;
}
}
}
}
}
/*
* Time Until
* Copied from vichan
* Licensed Under MIT
* Project Upstream URL: https://github.com/vichan-devel/vichan
*/
function until($timestamp) {
$difference = $timestamp - time();
switch(TRUE){
case ($difference < 60):
return $difference . ' ' . ngettext('second', 'seconds', $difference);
case ($difference < 3600): //60*60 = 3600
return ($num = round($difference/(60))) . ' ' . ngettext('minute', 'minutes', $num);
case ($difference < 86400): //60*60*24 = 86400
return ($num = round($difference/(3600))) . ' ' . ngettext('hour', 'hours', $num);
case ($difference < 604800): //60*60*24*7 = 604800
return ($num = round($difference/(86400))) . ' ' . ngettext('day', 'days', $num);
case ($difference < 31536000): //60*60*24*365 = 31536000
return ($num = round($difference/(604800))) . ' ' . ngettext('week', 'weeks', $num);
default:
return ($num = round($difference/(31536000))) . ' ' . ngettext('year', 'years', $num);
}
}
/*
* Time Ago
* Copied from vichan
* Licensed Under MIT
* Project Upstream URL: https://github.com/vichan-devel/vichan
*/
function ago($timestamp) {
$difference = time() - $timestamp;
switch(TRUE){
case ($difference < 60) :
return $difference . ' ' . ngettext('second', 'seconds', $difference);
case ($difference < 3600): //60*60 = 3600
return ($num = round($difference/(60))) . ' ' . ngettext('minute', 'minutes', $num);
case ($difference < 86400): //60*60*24 = 86400
return ($num = round($difference/(3600))) . ' ' . ngettext('hour', 'hours', $num);
case ($difference < 604800): //60*60*24*7 = 604800
return ($num = round($difference/(86400))) . ' ' . ngettext('day', 'days', $num);
case ($difference < 31536000): //60*60*24*365 = 31536000
return ($num = round($difference/(604800))) . ' ' . ngettext('week', 'weeks', $num);
default:
return ($num = round($difference/(31536000))) . ' ' . ngettext('year', 'years', $num);
}
}
/* File Size Conversion: Copied from Vichan */
function f_size($size){
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return round($size, 2).$units[$i];
}
/* Random Code Generator */
function random_code($characters) {
// List of all possible characters
$possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return trim($code);
}
/* Image Resize (For Thumbnail Generator)
* This function manipulate the image.
*/
function img_resize( $tmpname, $size, $save_dir, $save_name ){
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = GetImageSize($tmpname);
$type = $gis[2];
switch($type){
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}
$x = imageSX($imorig);
$y = imageSY($imorig);
if($gis[0] <= $size){
$av = $x;
$ah = $y;
}
else{
$yc = $y*1.3333333;
$d = $x>$yc?$x:$yc;
$c = $d>$size ? $size/$d : $size;
$av = $x*$c;
$ah = $y*$c;
}
$im = imagecreate($av, $ah);
$im = imagecreatetruecolor($av,$ah);
if (imagecopyresampled($im,$imorig , 0,0,0,0,$av,$ah,$x,$y)){
if (imagejpeg($im, $save_dir.$save_name)){
return true;
}
else{
return false;
}
}
}
/* Cache Image Generator */
function cache_image($url){
if(!file_exists($url)){
return $url;
}
/* Get File Information */
$img=getimagesize($url);
// convert the image to JPEG first
//
// 1. This will save image in temporary directory first
// 2. The saved image will be compressed later and removed
// 3. JPEG Image can be compressed efficiently!
//
$originalFile=$url;
$outputFile="cdn/temp/".md5(mt_rand(1,10000)).".jpg";
if($img['mime']=="image/gif"){ $image= ImageCreateFromGIF($originalFile); }
elseif($img['mime']=="image/png") { $image= ImageCreateFromPNG($originalFile); }
else{$image=imageCreateFromJPEG($originalFile); }
@imageJPEG($image, $outputFile, 30); // save original file converting to JPEG
$url=$outputFile; // replace with converted image
// calculate aspect ratio
$aspect_ratio = (float) $img[1] / $img[0];
$thumb_height = round(160 * $aspect_ratio);
$thumb_width = 160;
$output=md5(mt_rand(1, 10000000)-mt_rand(100, 1000)).".jpg";
// Save File On New Area
img_resize($outputFile, 160, "cdn/thumbnail/", $output);
// unlink temporary file
if(file_exists($outputFile)){
unlink($outputFile); // delete temporary cache
}
return "cdn/thumbnail/".$output;
}
/*
* Markdown Editor of Thread Posts and Replies
* --------------------------------------
* Just does some formatting and shows to user. Magic happens with JS.
*/
function markup($text){
global $pdo,$board,$thread,$post_reply;
$text = utf8tohtml($text);
$text = unicodify($text);
// $text=str_replace("<", "<", $text);
$text=nl2br($text);
// brute force replace >>>/board/ to board links.
$stmt=$pdo->query("SELECT `board` FROM `boards`");
$stmt->execute();
foreach($stmt->fetchAll() as $row){
$find = ">>>/".$row['board']."/";
$replace = "<a href=\"/".$row['board']."/\" id=\"board-link\" data-id=\"".$row['board']."\">x→xx→xx→x/".$row['board']."/</a>";
$text = str_replace($find, $replace, $text);
}
// suprise meme pages (Just for fun lol)
foreach(["out", "reddit", "r/eddit", "facebook", "fb"] as $row){
$find = ">>>/".$row."/";
$replace = "<a href=\"/".$row."/\" id=\"board-link\" data-id=\"".$row."\">x→xx→xx→x/".$row."/</a>";
$text = str_replace($find, $replace, $text);
}
// check all thread IDs (Cites)
preg_match_all("/>>(?P<digit>\d+)/", $text, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
$valx=(int)$val[1]; // converting to integer (just for safety)
// where this cite belongs (is it OP?)
if($valx==$thread){
$replace='<a href="/'.$board.'/'.$thread.'/#'.$thread.'" id="c'.$thread.'" class="cite" data-id="'.$thread.'" data-board="'.$board.'" onclick="highlight(\''.$thread.'\');">x→xx→x'.$thread.' (OP)</a>';
}
else{
// not OP? Search Database first
$stmt=$pdo->prepare("SELECT count(*) FROM `threads` WHERE `id`=?");
$stmt->execute([$valx]);
if($stmt->fetchColumn()==1){
// get where it belongs
$stmt=$pdo->prepare("SELECT * FROM `threads` WHERE `id`=?");
$stmt->execute([$valx]);
$t_data=$stmt->fetch();
$bo=$t_data['board'];
if($t_data['thread_start']=="Y"){
$th=$valx;
}
else{
$th=$t_data['thread_reply'];
}
$thx=$valx;
$replace='<a href="/'.$bo.'/'.$th.'/#'.$thx.'" id="c'.$thx.'" class="cite" data-id="'.$thx.'" data-board="'.$bo.'" onclick="highlight(\''.$thx.'\');">x→xx→x'.$thx.'</a>';
}
else{
$th=$val[1];
$replace='<a href="/go.php?id='.$th.'" id="c'.$th.'" class="cite" data-id="'.$th.'" data-board="NOT_FOUND" onclick="highlight(\''.$th.'\');">x→xx→x'.$th.'</a>';
}
}
// get new thread ID
$stmt=$pdo->query("SELECT `id` FROM `threads` ORDER BY `id` DESC LIMIT 1");
$stmt->execute();
$t_idx=$stmt->fetch();
$c_by=$t_idx['id']+1;
$stmt=$pdo->prepare("INSERT INTO `cite`(`by`, `to`) VALUES(?, ?)");
$stmt->execute([$c_by, $valx]);
$text=str_replace($val[0], $replace, $text);
}
// making greentext
$text = preg_replace("/^\s*>.*$/m", '<span class="quote">$0</span>', $text);
// changing back the x←x to >
$text=str_replace("x→x", ">", $text);
return $text;
}
/* Trunctuate Text
* Source: https://www.thewebtaylor.com/articles/simple-php-truncate-function
*/
function truncate($text, $chars = 120) {
if(strlen($text) > $chars) {
$text = $text.' ';
$text = substr($text, 0, $chars);
$text = substr($text, 0, strrpos($text ,' '));
$text = $text."(...)";
}
return $text;
}
/*
* Clear EXIF Data
* This will clear all EXIF data of image. (Device, GPS Location, Date/Time, Camera Information...)
* This will also largely reduce the file size since exif data can take significant amount of space.
* Testing: 12.3MB image reduced to 11.5MB
* Requires imagick module.
* Please install php-imagemagik and imagemagik modules (linux only, probably won't work on windows easily)
*
*/
function clear_exif($url){
// only run this if the extension is enabled
if(extension_loaded('imagick')){
// start the imagick engine
$img = new Imagick(realpath($url));
// get icc profiles (Colors, rotation and stuffs (basic ones))
$profiles = $img->getImageProfiles("icc", true);
// remove the naughty stuffs
// this will remove any metadata (device, location, gps or any hidden exif values)
$img->stripImage();
// this will add back basic profiles (icc ones) if it had any.
if(!empty($profiles)) {
$img->profileImage("icc", $profiles['icc']);
}
// finally write the image
$img->writeImage($url);
}
}
/*
* Display Text
*
*/
function display_text($text, $thread, $board){
// is this long?
if(strlen($text)>300){
$text=truncate($text, 300);
$text=$text."<div class=\"too-long\">Text too Long! <a href=\"/".$board."/".$thread."/\">Click Here to See More</a>.</div>";
}
// returning the value
return $text;
}
/*
* Get Video Attributes
* Height, Width, Length (Time), Codec
*
*/
function _get_video_attributes($video, $ffmpeg="/usr/bin/ffmpeg") {
$command = $ffmpeg . ' -i ' . $video . ' -vstats 2>&1';
$output = shell_exec($command);
$regex_sizes = "/Video: ([^,]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; // or : $regex_sizes = "/Video: ([^\r\n]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/"; (code from @1owk3y)
if (preg_match($regex_sizes, $output, $regs)) {
$codec = $regs [1] ? $regs [1] : null;
$width = $regs [3] ? $regs [3] : null;
$height = $regs [4] ? $regs [4] : null;
}
$regex_duration = "/Duration: ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).([0-9]{1,2})/";
if (preg_match($regex_duration, $output, $regs)) {
$hours = $regs [1] ? $regs [1] : null;
$mins = $regs [2] ? $regs [2] : null;
$secs = $regs [3] ? $regs [3] : null;
$ms = $regs [4] ? $regs [4] : null;
}
if(empty($codec)){$codec=NULL;}
if(empty($width)){$width=NULL;}
if(empty($height)){$height=NULL;}
if(empty($hours)){$hours=NULL;}
if(empty($mins)){$mins=NULL;}
if(empty($secs)){$secs=NULL;}
if(empty($ms)){$ms=NULL;}
return array('codec' => $codec,
'width' => $width,
'height' => $height,
'hours' => $hours,
'mins' => $mins,
'secs' => $secs,
'ms' => $ms
);
}
/*
* Generate Video Thumbnail
* This funtion will generate video thumbnail (Screencap) for certain period of time.
* Not actually a thumbnail as it will generate full size image.
*/
function video_thumb($video){
$path="cdn/temp/".md5(rand(1,100).mt_rand(1, 500)).".jpg";
$ffmpeg="/usr/bin/ffmpeg";
$cmd = "{$ffmpeg} -i {$video} -deinterlace -an -ss 1 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg {$path} 2>&1";
$output=shell_exec($cmd);
if(file_exists($path)){
return $path;
} else {
return "assets/images/video_thumb.png";
}
}
/*
* Function: Get User's Country (API)
* This will fetch the user's API from other properiatory (freemium) services.
*
*/
function get_country($ip){
// source 1 GeoPlugin.NET
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
if(property_exists($ipdat, 'geoplugin_countryName')){
$country = $ipdat->geoplugin_countryName;
}
else{
// failover for GeoPlugin.NET
$ipdat= @json_decode(file_get_contents("http://ip-api.com/json/".$ip));
if(property_exists($ipdat, 'success')){
if($ipdat->success=="success"){
$country=$ipdat->country;
}
else{
// failover for failover of GP.net (ip-api.com)'s failover
$ipdat= @json_decode(file_get_contents("http://api.ipdata.co/".$ip));
if(property_exists($ipdat, "country_name")){
$country=$ipdat->country_name;
}
else{
$country="Nepal";
}
}
} // if for failover for gp (ip-api.com)
else{
$country="Nepal";
} // failover for everything. (IP not verified)
} // else for source 1
return $country;
}
/*
* Function: Get Country
* This will return user's country name.
* If it exists on DB, it pulls from there, else makes a new one.
* ---------------------------------
* FAQ:
* 1. Why track this?
* - NPChan is meant for Nepalese users. To prevent misuse of NPChan from foreign IP address,
* some features are limited only to Nepalese IP address only. This help in controlling spam
* as well as reduce chance of someone uploading illegal content using TOR or VPN
*
*/
function country(){
global $pdo,$user_ip;
if(empty($user_ip)){
$user_ip=getIP();
}
if($user_ip=="::1" || $user_ip==""){
return "Nepal"; // local machine bypass
}
// check if user's IP is on DB or not
$stmt=$pdo->prepare("SELECT count(*) FROM `ip_logs` WHERE `ip_address`=?");
$stmt->execute([$user_ip]);
if($stmt->fetchColumn()>0){
$stmt=$pdo->prepare("SELECT `country` FROM `ip_logs` WHERE `ip_address`=?");
$stmt->execute([$user_ip]);
$cont=$stmt->fetch();
$country=$cont['country'];
}
// if we don't have it on database, make it.
else{
$country = get_country($user_ip);
// making it
$stmt=$pdo->prepare("INSERT INTO `ip_logs`(`ip_address`, `country`, `timestamp`) VALUES(?, ?, ?);");
$stmt->execute([$user_ip, $country, time()]);
}
return $country;
}