2021-07-10 17:36:15 -04:00
|
|
|
<?php
|
|
|
|
if (!defined('IN_PONEPASTE')) {
|
2021-07-10 17:54:43 -04:00
|
|
|
die('This file may not be accessed directly.');
|
2021-07-10 17:36:15 -04:00
|
|
|
}
|
2021-08-20 15:53:06 -04:00
|
|
|
require_once(__DIR__ . '/../vendor/autoload.php');
|
2021-07-17 12:26:33 -04:00
|
|
|
require_once(__DIR__ . '/config.php');
|
|
|
|
require_once(__DIR__ . '/functions.php');
|
2022-03-14 15:43:01 -04:00
|
|
|
require_once(__DIR__ . '/passwords.php');
|
2021-07-17 18:17:29 -04:00
|
|
|
require_once(__DIR__ . '/DatabaseHandle.class.php');
|
2021-08-27 06:46:27 -04:00
|
|
|
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
use PonePaste\Helpers\SessionHelper;
|
2021-11-01 16:56:17 -04:00
|
|
|
use PonePaste\Models\IPBan;
|
|
|
|
use PonePaste\Models\PageView;
|
2021-08-29 01:26:29 -04:00
|
|
|
use PonePaste\Models\Paste;
|
|
|
|
use PonePaste\Models\User;
|
2021-07-10 17:36:15 -04:00
|
|
|
|
2021-07-13 13:32:28 -04:00
|
|
|
/* View functions */
|
2021-11-23 03:17:29 -05:00
|
|
|
function javascriptIncludeTag(string $name) : string {
|
|
|
|
if (PP_DEBUG) {
|
|
|
|
return "<script src=\"/assets/bundle/${name}.js\"></script>";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<script src=\"/assets/bundle/${name}.min.js\"></script>";
|
|
|
|
}
|
|
|
|
|
2021-08-26 05:58:37 -04:00
|
|
|
function urlForPage($page = '') : string {
|
|
|
|
if (!PP_MOD_REWRITE) {
|
|
|
|
$page .= '.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . '/' . $page;
|
|
|
|
}
|
|
|
|
|
2021-08-29 01:26:29 -04:00
|
|
|
function urlForPaste(Paste $paste) : string {
|
2021-07-26 17:41:54 -04:00
|
|
|
if (PP_MOD_REWRITE) {
|
2021-08-29 01:26:29 -04:00
|
|
|
return "/{$paste->id}";
|
2021-07-13 13:32:28 -04:00
|
|
|
}
|
|
|
|
|
2021-08-29 01:26:29 -04:00
|
|
|
return "/paste.php?id={$paste->id}";
|
2021-07-13 13:32:28 -04:00
|
|
|
}
|
|
|
|
|
2021-10-22 21:43:35 -04:00
|
|
|
function urlForMember(User $user) : string {
|
2021-07-26 17:41:54 -04:00
|
|
|
if (PP_MOD_REWRITE) {
|
2021-10-22 21:43:35 -04:00
|
|
|
return '/user/' . urlencode($user->username);
|
2021-07-13 13:32:28 -04:00
|
|
|
}
|
|
|
|
|
2021-10-22 21:43:35 -04:00
|
|
|
return '/user.php?name=' . urlencode($user->username);
|
2021-07-13 13:32:28 -04:00
|
|
|
}
|
2021-07-26 17:42:43 -04:00
|
|
|
|
2022-03-12 13:56:32 -05:00
|
|
|
/**
|
|
|
|
* @throws Exception if the names and values aren't the same length
|
|
|
|
*/
|
2021-08-25 20:22:17 -04:00
|
|
|
function optionsForSelect(array $displays, array $values, string $currentSelection = null) : string {
|
|
|
|
$size = count($displays);
|
|
|
|
|
|
|
|
if (count($values) !== $size) {
|
|
|
|
throw new Exception('Option names and option values must be the same count');
|
|
|
|
}
|
|
|
|
|
|
|
|
$html = '';
|
|
|
|
|
|
|
|
for ($i = 0; $i < $size; $i++) {
|
|
|
|
$html .= '<option value="' . pp_html_escape($values[$i]) . '"';
|
|
|
|
|
|
|
|
if ($currentSelection === $values[$i]) {
|
|
|
|
$html .= ' selected="selected"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$html .= '>' . pp_html_escape($displays[$i]) . '</option>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2022-03-12 13:56:32 -05:00
|
|
|
/**
|
|
|
|
* @throws Exception if the flash level is invalid
|
|
|
|
*/
|
2021-08-27 06:46:27 -04:00
|
|
|
function flash(string $level, string $message) {
|
|
|
|
if (!isset($_SESSION['flashes'])) {
|
|
|
|
$_SESSION['flashes'] = [
|
|
|
|
'success' => [],
|
|
|
|
'warning' => []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists($level, $_SESSION['flashes'])) {
|
|
|
|
throw new Exception('Invalid flash level ' . $level);
|
|
|
|
}
|
|
|
|
|
2022-03-26 23:57:28 -04:00
|
|
|
$_SESSION['flashes'][$level][] = $message;
|
2021-08-27 06:46:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function flashError(string $message) {
|
|
|
|
flash('error', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
function flashSuccess(string $message) {
|
|
|
|
flash('success', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFlashes() {
|
|
|
|
if (!isset($_SESSION['flashes'])) {
|
|
|
|
return ['success' => [], 'error' => []];
|
|
|
|
}
|
|
|
|
|
|
|
|
$flashes = $_SESSION['flashes'];
|
|
|
|
|
|
|
|
unset($_SESSION['flashes']);
|
|
|
|
|
|
|
|
return $flashes;
|
|
|
|
}
|
|
|
|
|
2021-07-13 13:32:28 -04:00
|
|
|
/* Database functions */
|
2021-07-12 12:53:39 -04:00
|
|
|
function getSiteInfo() : array {
|
2021-08-05 08:18:32 -04:00
|
|
|
return require(__DIR__ . '/../config/site.php');
|
2021-07-10 17:36:15 -04:00
|
|
|
}
|
|
|
|
|
2021-07-16 09:53:34 -04:00
|
|
|
/**
|
|
|
|
* Specialization of `htmlentities()` that avoids double escaping and uses UTF-8.
|
|
|
|
*
|
|
|
|
* @param string $unescaped String to escape
|
|
|
|
* @return string HTML-escaped string
|
|
|
|
*/
|
|
|
|
function pp_html_escape(string $unescaped) : string {
|
2021-08-20 15:53:06 -04:00
|
|
|
return htmlspecialchars($unescaped, ENT_QUOTES, 'UTF-8', false);
|
2021-07-16 09:53:34 -04:00
|
|
|
}
|
|
|
|
|
2021-11-02 08:46:40 -04:00
|
|
|
/* I think there is one row for each day, and in that row, tpage = non-unique, tvisit = unique page views for that day */
|
2022-03-12 13:56:32 -05:00
|
|
|
function updatePageViews() : void {
|
2021-11-02 19:09:46 -04:00
|
|
|
global $redis;
|
|
|
|
|
2021-07-10 17:54:43 -04:00
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
2021-07-12 09:03:02 -04:00
|
|
|
$date = date('jS F Y');
|
2021-07-10 17:54:43 -04:00
|
|
|
|
2021-11-02 19:09:46 -04:00
|
|
|
$last_page_view = PageView::orderBy('id', 'desc')->limit(1)->first();
|
2021-07-10 17:54:43 -04:00
|
|
|
|
2021-11-02 19:09:46 -04:00
|
|
|
if ($last_page_view && $last_page_view->date == $date) {
|
|
|
|
if (!$redis->sIsMember('page_view_ips', $ip)) {
|
|
|
|
$last_page_view->tvisit++;
|
|
|
|
$redis->sAdd('page_view_ips', $ip);
|
2021-07-10 17:54:43 -04:00
|
|
|
}
|
2021-11-02 19:09:46 -04:00
|
|
|
|
|
|
|
$last_page_view->tpage++;
|
|
|
|
$last_page_view->save();
|
2021-07-10 17:54:43 -04:00
|
|
|
} else {
|
2021-11-02 19:09:46 -04:00
|
|
|
$redis->del('page_view_ips');
|
2021-07-10 17:54:43 -04:00
|
|
|
|
|
|
|
// New date is created
|
2021-11-02 19:09:46 -04:00
|
|
|
$new_page_view = new PageView(['date' => $date]);
|
|
|
|
$new_page_view->save();
|
2021-07-10 17:54:43 -04:00
|
|
|
|
2021-11-02 19:09:46 -04:00
|
|
|
$redis->sAdd('page_view_ips', $ip);
|
2021-07-10 17:54:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 15:43:01 -04:00
|
|
|
function setupCsrfToken() : string {
|
|
|
|
if (isset($_SESSION[SessionHelper::CSRF_TOKEN_KEY])) {
|
|
|
|
return $_SESSION[SessionHelper::CSRF_TOKEN_KEY];
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = pp_random_token();
|
|
|
|
$_SESSION[SessionHelper::CSRF_TOKEN_KEY] = $token;
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyCsrfToken($token = null) : bool {
|
|
|
|
if ($token === null) {
|
|
|
|
$token = $_POST[SessionHelper::CSRF_TOKEN_KEY];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($token) || empty($_SESSION[SessionHelper::CSRF_TOKEN_KEY])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = hash_equals($_SESSION[SessionHelper::CSRF_TOKEN_KEY], $token);
|
|
|
|
|
|
|
|
unset($_SESSION[SessionHelper::CSRF_TOKEN_KEY]);
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2021-07-11 12:18:57 -04:00
|
|
|
session_start();
|
|
|
|
|
2021-11-02 19:09:46 -04:00
|
|
|
/* Set up the database and Eloquent ORM */
|
2021-08-27 06:46:27 -04:00
|
|
|
$capsule = new Capsule();
|
|
|
|
|
|
|
|
$capsule->addConnection([
|
|
|
|
'driver' => 'mysql',
|
|
|
|
'host' => $db_host,
|
|
|
|
'database' => $db_schema,
|
|
|
|
'username' => $db_user,
|
|
|
|
'password' => $db_pass ,
|
|
|
|
'charset' => 'utf8mb4',
|
|
|
|
'prefix' => ''
|
|
|
|
]);
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
2022-03-12 13:56:32 -05:00
|
|
|
// Check if IP is banned
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
if (IPBan::where('ip', $ip)->first()) {
|
|
|
|
die('You have been banned.');
|
|
|
|
}
|
|
|
|
|
2021-11-02 19:09:46 -04:00
|
|
|
/* Set up Redis */
|
|
|
|
$redis = new Redis();
|
|
|
|
$redis->pconnect(PP_REDIS_HOST);
|
2021-07-17 18:17:29 -04:00
|
|
|
|
2021-07-10 17:36:15 -04:00
|
|
|
// Setup site info
|
2021-07-12 12:53:39 -04:00
|
|
|
$site_info = getSiteInfo();
|
2021-08-22 21:45:26 -04:00
|
|
|
$global_site_info = $site_info['site_info'];
|
2021-07-12 12:53:39 -04:00
|
|
|
$row = $site_info['site_info'];
|
2021-11-01 16:56:17 -04:00
|
|
|
$title = trim($row['title']);
|
|
|
|
$baseurl = trim($row['baseurl']);
|
|
|
|
$site_name = trim($row['site_name']);
|
|
|
|
$email = trim($row['email']);
|
|
|
|
$additional_scripts = trim($row['additional_scripts']);
|
2021-07-10 17:36:15 -04:00
|
|
|
|
2021-08-26 05:35:21 -04:00
|
|
|
// Setup theme
|
|
|
|
$default_theme = 'bulma';
|
2021-07-10 17:36:15 -04:00
|
|
|
|
2021-08-22 21:57:14 -04:00
|
|
|
// Site permissions
|
2021-07-12 12:53:39 -04:00
|
|
|
$site_permissions = $site_info['permissions'];
|
2021-07-10 18:21:03 -04:00
|
|
|
|
2021-08-25 02:08:30 -04:00
|
|
|
$site_is_private = false;
|
|
|
|
$site_disable_guests = false;
|
|
|
|
|
2021-07-10 18:21:03 -04:00
|
|
|
if ($site_permissions) {
|
2021-08-22 21:54:46 -04:00
|
|
|
$site_is_private = (bool) $site_permissions['private'];
|
2021-08-23 02:49:54 -04:00
|
|
|
$site_disable_guests = (bool) $site_permissions['disable_guest'];
|
2021-07-10 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2021-08-09 04:21:39 -04:00
|
|
|
// CAPTCHA configuration
|
|
|
|
$captcha_config = $site_info['captcha'];
|
2021-08-22 21:57:14 -04:00
|
|
|
$captcha_enabled = (bool) $captcha_config['enabled'];
|
2021-08-09 04:21:39 -04:00
|
|
|
|
2021-11-01 16:56:17 -04:00
|
|
|
$total_pastes = Paste::count();
|
|
|
|
$total_page_views = PageView::select('tpage')->orderBy('id', 'desc')->first()->tpage;
|
|
|
|
$total_unique_views = PageView::select('tvisit')->orderBy('id', 'desc')->first()->tvisit;
|
2021-07-16 09:53:34 -04:00
|
|
|
|
2021-08-27 06:46:27 -04:00
|
|
|
$current_user = SessionHelper::currentUser();
|
2021-07-17 18:17:29 -04:00
|
|
|
|
2022-03-14 15:43:01 -04:00
|
|
|
//SessionHelper::setupCsrfToken();
|
|
|
|
|
2021-11-23 03:17:29 -05:00
|
|
|
$script_bundles = [];
|
|
|
|
|
2021-08-20 16:17:13 -04:00
|
|
|
/* Security headers */
|
|
|
|
header('X-Frame-Options: SAMEORIGIN');
|
|
|
|
header('X-Content-Type-Options: nosniff');
|
2021-08-22 21:45:26 -04:00
|
|
|
|
|
|
|
//ob_start();
|