ponepaste/includes/common.php

155 lines
4.7 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__ . '/config.php');
require_once(__DIR__ . '/functions.php');
require_once(__DIR__ . '/DatabaseHandle.class.php');
require_once(__DIR__ . '/User.class.php');
2021-07-10 17:36:15 -04:00
/* View functions */
function urlForPaste($paste_id) : string {
if (PP_MOD_REWRITE) {
return "/${paste_id}";
}
return "/paste.php?id=${paste_id}";
}
function urlForMember(string $member_name) : string {
if (PP_MOD_REWRITE) {
return '/user/' . urlencode($member_name);
}
return '/user.php?name=' . urlencode($member_name);
}
2021-07-26 17:42:43 -04:00
/* 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-20 12:15:41 -04:00
function getSiteAds(DatabaseHandle $conn) : array|bool {
2021-07-10 18:21:03 -04:00
return $conn->query('SELECT text_ads, ads_1, ads_2 FROM ads LIMIT 1')->fetch();
}
2021-07-20 12:15:41 -04:00
function getSiteTotalPastes(DatabaseHandle $conn) : int {
return intval($conn->query('SELECT COUNT(*) FROM pastes')->fetch(PDO::FETCH_NUM)[0]);
}
2021-07-20 12:15:41 -04:00
function getSiteTotalviews(DatabaseHandle $conn) : int {
2021-07-12 11:49:17 +01:00
return intval($conn->query('SELECT tpage FROM page_view ORDER BY id DESC LIMIT 1')->fetch(PDO::FETCH_NUM)[0]);
}
2021-07-20 12:15:41 -04:00
function getSiteTotal_unique_views(DatabaseHandle $conn) : int {
2021-07-12 11:49:17 +01:00
return intval($conn->query('SELECT tvisit FROM page_view ORDER BY id DESC LIMIT 1')->fetch(PDO::FETCH_NUM)[0]);
}
2021-07-10 18:21:03 -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 htmlentities($unescaped, ENT_QUOTES, 'UTF-8', false);
}
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-07-20 12:15:41 -04:00
$conn = new DatabaseHandle("mysql:host=$db_host;dbname=$db_schema;charset=utf8", $db_user, $db_pass);
2021-07-10 17:36:15 -04:00
// Setup site info
$site_info = getSiteInfo();
$row = $site_info['site_info'];
$title = Trim($row['title']);
$des = Trim($row['description']);
$baseurl = Trim($row['baseurl']);
$keyword = Trim($row['keywords']);
$site_name = Trim($row['site_name']);
$email = Trim($row['email']);
$ga = Trim($row['google_analytics']);
$additional_scripts = Trim($row['additional_scripts']);
2021-07-10 17:36:15 -04:00
// Setup theme and language
$lang_and_theme = $site_info['interface'];
$default_lang = $lang_and_theme['language'];
$default_theme = $lang_and_theme['theme'];
2021-07-10 17:36:15 -04:00
2021-07-10 18:21:03 -04:00
// site permissions
$site_permissions = $site_info['permissions'];
2021-07-10 18:21:03 -04:00
if ($site_permissions) {
2021-07-13 08:57:07 -04:00
$siteprivate = $site_permissions['private'];
$disableguest = $site_permissions['disable_guest'];
2021-07-10 18:21:03 -04:00
} else {
$siteprivate = 'off';
2021-07-12 08:23:14 -04:00
$disableguest = 'off';
2021-07-10 18:21:03 -04:00
}
$privatesite = $siteprivate;
2021-07-12 08:23:14 -04:00
$noguests = $disableguest;
2021-08-09 04:21:39 -04:00
// CAPTCHA configuration
$captcha_config = $site_info['captcha'];
$captcha_enabled = (bool) $captcha_config['enabled'];
2021-07-10 17:36:15 -04:00
// Prevent a potential LFI (you never know :p)
2021-07-13 08:57:07 -04:00
$lang_file = "${default_lang}.php";
2021-08-05 08:18:32 -04:00
if (in_array($lang_file, scandir(__DIR__ . '/../langs/'))) {
require_once(__DIR__ . "/../langs/${lang_file}");
2021-07-10 17:36:15 -04:00
}
// Check if IP is banned
$ip = $_SERVER['REMOTE_ADDR'];
if (is_banned($conn, $ip)) die($lang['banned']); // "You have been banned from ".$site_name;
2021-07-10 17:36:15 -04:00
2021-07-10 18:21:03 -04:00
$site_ads = getSiteAds($conn);
2021-07-12 11:49:17 +01:00
$total_pastes = getSiteTotalPastes($conn);
$total_page_views = getSiteTotalviews($conn);
2021-07-16 09:53:34 -04:00
$total_unique_views = getSiteTotal_unique_views($conn);
2021-07-20 12:15:41 -04:00
$current_user = User::current($conn);
if ($current_user) {
$noguests = "off";
}