ponepaste/includes/common.php

215 lines
5.9 KiB
PHP
Raw Normal View History

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
}
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/config.php');
require_once(__DIR__ . '/functions.php');
require_once(__DIR__ . '/DatabaseHandle.class.php');
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
/* View functions */
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 {
if (PP_MOD_REWRITE) {
2021-08-29 01:26:29 -04:00
return "/{$paste->id}";
}
2021-08-29 01:26:29 -04:00
return "/paste.php?id={$paste->id}";
}
function urlForMember(User $user) : string {
if (PP_MOD_REWRITE) {
return '/user/' . urlencode($user->username);
}
return '/user.php?name=' . urlencode($user->username);
}
2021-07-26 17:42:43 -04:00
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;
}
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);
}
array_push($_SESSION['flashes'][$level], $message);
}
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;
}
/* Database functions */
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 {
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 */
2021-07-20 12:15:41 -04:00
function updatePageViews(DatabaseHandle $conn) : void {
2021-07-10 17:54:43 -04:00
$ip = $_SERVER['REMOTE_ADDR'];
$date = date('jS F Y');
2021-07-10 17:54:43 -04:00
$data_ip = file_get_contents('tmp/temp.tdata');
$last_page_view = $conn->query('SELECT * FROM page_view ORDER BY id DESC LIMIT 1')->fetch();
$last_date = $last_page_view['date'];
if ($last_date == $date) {
2021-07-17 18:29:36 -04:00
$last_tpage = intval($last_page_view['tpage']) + 1;
2021-07-10 17:54:43 -04:00
2021-07-17 18:29:36 -04:00
if (str_contains($data_ip, $ip)) {
2021-07-10 17:54:43 -04:00
// IP already exists, Update view count
$statement = $conn->prepare("UPDATE page_view SET tpage = ? WHERE id = ?");
$statement->execute([$last_tpage, $last_page_view['id']]);
} else {
$last_tvisit = intval($last_page_view['tvisit']) + 1;
// Update both tpage and tvisit.
$statement = $conn->prepare("UPDATE page_view SET tpage = ?,tvisit = ? WHERE id = ?");
$statement->execute([$last_tpage, $last_tvisit, $last_page_view['id']]);
file_put_contents('tmp/temp.tdata', $data_ip . "\r\n" . $ip);
}
} else {
// Delete the file and clear data_ip
unlink("tmp/temp.tdata");
// New date is created
$statement = $conn->prepare("INSERT INTO page_view (date, tpage, tvisit) VALUES (?, '1', '1')");
$statement->execute([$date]);
// Update the IP
file_put_contents('tmp/temp.tdata', $ip);
}
}
2021-07-11 12:18:57 -04:00
session_start();
2021-08-18 16:32:19 -04:00
$conn = new DatabaseHandle("mysql:host=$db_host;dbname=$db_schema;charset=utf8mb4", $db_user, $db_pass);
$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();
2021-07-10 17:36:15 -04:00
// Setup site info
$site_info = getSiteInfo();
2021-08-22 21:45:26 -04:00
$global_site_info = $site_info['site_info'];
$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
$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) {
$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-07-10 17:36:15 -04:00
// Check if IP is banned
$ip = $_SERVER['REMOTE_ADDR'];
2021-11-01 16:56:17 -04:00
if (IPBan::where('ip', $ip)->first()) {
2021-08-26 05:35:21 -04:00
die('You have been banned.');
2021-08-17 13:19:10 -04:00
}
2021-07-10 17:36:15 -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
$current_user = SessionHelper::currentUser();
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();