2020-04-27 23:27:39 +02:00
|
|
|
<?php
|
2020-05-02 12:06:45 +02:00
|
|
|
require_once 'vendor/autoload.php';
|
2021-03-04 09:52:11 +01:00
|
|
|
use DebugBar\StandardDebugBar;
|
2021-04-25 17:19:48 +02:00
|
|
|
use MinistryOfWeb\OsmTiles\Converter;
|
|
|
|
use MinistryOfWeb\OsmTiles\LatLng;
|
2020-04-27 23:27:39 +02:00
|
|
|
|
2020-12-20 21:40:53 +01:00
|
|
|
if(!file_exists("config.php") && !file_exists("../../config.php")) {
|
|
|
|
header('Location: install/install.php');
|
2021-03-24 17:10:44 +01:00
|
|
|
exit();
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2020-07-03 12:10:41 +02:00
|
|
|
|
|
|
|
require_once 'config.php';
|
2020-05-02 12:06:45 +02:00
|
|
|
|
2021-03-01 16:35:56 +01:00
|
|
|
if(SENTRY_ENABLED){
|
|
|
|
\Sentry\init([
|
|
|
|
'dsn' => SENTRY_DSN,
|
|
|
|
'traces_sample_rate' => 0.8,
|
|
|
|
'environment' => SENTRY_ENV
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:45 +02:00
|
|
|
session_start();
|
|
|
|
date_default_timezone_set('Europe/Rome');
|
|
|
|
|
2021-03-05 23:37:45 +01:00
|
|
|
function bdump($message){
|
|
|
|
global $debugbar;
|
|
|
|
if(!is_null($debugbar)){
|
|
|
|
$debugbar["messages"]->addMessage($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
class tools
|
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
public $db;
|
2020-11-13 18:57:47 +01:00
|
|
|
public $profiler_enabled;
|
|
|
|
public $profiler_last_name = "";
|
2021-05-16 21:32:43 +02:00
|
|
|
public $script_nonce = null;
|
|
|
|
|
|
|
|
public function generateNonce($bytes_lenght = 16, $base64_encode = false){
|
|
|
|
$nonce = bin2hex(random_bytes($bytes_lenght));
|
|
|
|
if($base64_encode){
|
|
|
|
$nonce = base64_encode($nonce);
|
|
|
|
}
|
|
|
|
return $nonce;
|
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function __construct($db, $profiler_enabled)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db = $db;
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->profiler_enabled = $profiler_enabled;
|
2021-05-16 21:32:43 +02:00
|
|
|
$this->script_nonce = $this->generateNonce(16);
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 11:29:19 +01:00
|
|
|
public function validate_form($data, $expected_value=null, $data_source=null)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
2020-11-25 11:29:19 +01:00
|
|
|
if(is_array($data)){
|
|
|
|
foreach($data as $element){
|
2021-03-19 22:35:37 +01:00
|
|
|
if (!$this->validate_form($element, $expected_value, $data_source)) return false;
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2020-11-25 11:29:19 +01:00
|
|
|
return true;
|
2020-11-13 18:57:47 +01:00
|
|
|
} else {
|
2020-11-25 11:29:19 +01:00
|
|
|
if(is_null($data_source) || !is_array($data_source)){
|
|
|
|
$data_source = $_POST;
|
|
|
|
}
|
|
|
|
return !is_null($data) && isset($data_source[$data]) && !is_null($data_source[$data]) && (!is_null($expected_value) ? $data_source[$data] == $expected_value : true);
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get_ip()
|
|
|
|
{
|
|
|
|
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
|
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
|
|
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
|
}else{
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2021-05-03 23:29:27 +02:00
|
|
|
if(get_option("check_cf_ip")) {
|
2020-11-13 18:57:47 +01:00
|
|
|
if(!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
|
|
|
|
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
|
|
|
|
}
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
return $ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_page_url()
|
|
|
|
{
|
|
|
|
if(!empty($_SERVER["HTTPS"])) {
|
|
|
|
if($_SERVER["HTTPS"] == "on") {
|
|
|
|
$protocol = "https";
|
|
|
|
} else {
|
|
|
|
$protocol = "http";
|
|
|
|
}
|
2020-05-02 12:06:45 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
$protocol = "http";
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
|
|
|
|
return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
public function redirect($url)
|
|
|
|
{
|
2021-03-04 09:52:11 +01:00
|
|
|
global $debugbar;
|
|
|
|
if(!is_null($debugbar)) $debugbar->stackData();
|
2020-11-13 18:57:47 +01:00
|
|
|
if (!headers_sent()) {
|
|
|
|
header('Location: '.$url);
|
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
echo '<script type="text/javascript">';
|
|
|
|
echo 'window.location.href="'.$url.'";';
|
|
|
|
echo '</script>';
|
|
|
|
echo '<noscript>';
|
|
|
|
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
|
|
|
|
echo '</noscript>';
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 23:43:30 +01:00
|
|
|
|
2021-03-24 17:10:44 +01:00
|
|
|
public function rickroll()
|
|
|
|
{
|
2021-03-04 23:43:30 +01:00
|
|
|
$rickrolls = [
|
|
|
|
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
|
|
"https://www.youtube.com/watch?v=ub82Xb1C8os",
|
|
|
|
"https://www.youtube.com/watch?v=Wjy3o0FoLYc",
|
|
|
|
"https://www.youtube.com/watch?v=bxqLsrlakK8",
|
|
|
|
"https://www.youtube.com/watch?v=Lrj2Hq7xqQ8"
|
|
|
|
];
|
|
|
|
$this->redirect($rickrolls[array_rand($rickrolls)]); //Dear attacker/bot, have fun!
|
|
|
|
}
|
2021-03-24 17:10:44 +01:00
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
function extract_unique($data)
|
|
|
|
{
|
|
|
|
$this->profiler_start("Extract unique");
|
|
|
|
$array2=[];
|
|
|
|
foreach($data as $arr){
|
|
|
|
if(is_array($arr)) {
|
|
|
|
$tmp = $this->extract_unique($arr);
|
|
|
|
foreach($tmp as $temp){
|
|
|
|
if(!is_array($temp)) {
|
|
|
|
if(!in_array($temp, $array2)) {
|
|
|
|
$array2[] = $temp;
|
|
|
|
}
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(!in_array($arr, $array2)) {
|
|
|
|
$array2[] = $arr;
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->profiler_stop();
|
|
|
|
return $array2;
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:01:54 +01:00
|
|
|
public function createKey($lenght=32)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
2021-02-23 15:01:54 +01:00
|
|
|
return bin2hex(random_bytes($lenght));
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2020-10-27 01:15:23 +01:00
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
public function sanitize($string, $htmlAllowed=false, $htmlPurifierOptions=[])
|
|
|
|
{
|
|
|
|
$this->profiler_start("Sanitize");
|
2020-12-31 00:03:19 +01:00
|
|
|
$htmlAllowed=false; //TODO: fix HTMLPurifier_Config
|
2020-11-13 18:57:47 +01:00
|
|
|
if($htmlAllowed) {
|
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
foreach ($htmlPurifierOptions as $key => $value) {
|
|
|
|
$config->set($key, $value);
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
$purifier = new HTMLPurifier($config);
|
|
|
|
$string = $purifier->purify($string);
|
2020-05-02 12:06:45 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
$string = htmlspecialchars($string);
|
|
|
|
}
|
|
|
|
$this->profiler_stop();
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function profiler_start($name=null)
|
|
|
|
{
|
2021-03-04 09:52:11 +01:00
|
|
|
global $debugbar;
|
|
|
|
if($this->profiler_enabled && !is_null($debugbar)) {
|
2020-11-13 18:57:47 +01:00
|
|
|
if(is_null($name)) {
|
|
|
|
$name = $this->profiler_last_name;
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2021-03-04 09:52:11 +01:00
|
|
|
if($name !== "") $debugbar['time']->startMeasure($name);
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
|
|
|
|
public function profiler_stop($name=null)
|
|
|
|
{
|
2021-03-04 09:52:11 +01:00
|
|
|
global $debugbar;
|
|
|
|
if($this->profiler_enabled && !is_null($debugbar)) {
|
|
|
|
if(is_null($name) || $name == "") {
|
2020-11-13 18:57:47 +01:00
|
|
|
$name = $this->profiler_last_name;
|
|
|
|
}
|
2021-03-04 09:52:11 +01:00
|
|
|
if($name !== "") $debugbar['time']->stopMeasure($name);
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2020-09-11 18:49:47 +02:00
|
|
|
}
|
2020-12-31 00:03:19 +01:00
|
|
|
|
2021-03-24 17:10:44 +01:00
|
|
|
public function ajax_page_response($response)
|
|
|
|
{
|
2021-03-04 09:52:11 +01:00
|
|
|
global $debugbar;
|
2020-12-31 00:03:19 +01:00
|
|
|
$json_response = json_encode($response);
|
|
|
|
$response_data = substr(crc32($json_response), 0, 10);
|
|
|
|
header("data: ".$response_data);
|
|
|
|
header("Content-type: application/json");
|
2021-03-04 23:43:30 +01:00
|
|
|
if(!is_null($debugbar)) $debugbar->sendDataInHeaders(true);
|
2021-04-05 18:57:52 +02:00
|
|
|
if(isset($_GET["oldData"]) && $_GET["oldData"] !== $response_data){
|
2020-12-31 00:03:19 +01:00
|
|
|
print($json_response);
|
|
|
|
} else {
|
|
|
|
print("{}");
|
|
|
|
}
|
|
|
|
}
|
2021-04-25 17:19:48 +02:00
|
|
|
|
2021-05-02 13:32:44 +02:00
|
|
|
public function convertMapAddressToUrl($lat, $lng, $zoom){
|
2021-05-03 23:29:27 +02:00
|
|
|
switch (get_option("map_preview_generator")) {
|
2021-05-02 13:32:44 +02:00
|
|
|
case 'osm':
|
|
|
|
$converter = new Converter();
|
|
|
|
$point = new LatLng($lat, $lng);
|
|
|
|
$tile = $converter->toTile($point, $zoom);
|
|
|
|
$tile_servers = ["a", "b", "c"];
|
|
|
|
$tileServer = $tile_servers[array_rand($tile_servers)];
|
|
|
|
return sprintf("https://{$tileServer}.tile.openstreetmap.org/{$zoom}/%d/%d.png", $tile->getX(), $tile->getY());
|
2021-04-25 17:19:48 +02:00
|
|
|
|
2021-05-02 13:32:44 +02:00
|
|
|
case 'custom':
|
|
|
|
default:
|
2021-05-03 23:29:27 +02:00
|
|
|
if(get_option("map_preview_generator_add_marker") && get_option("map_preview_generator_url_marker") && get_option("map_preview_generator_url_marker") !== ""){
|
|
|
|
$url = get_option("map_preview_generator_url_marker");
|
2021-05-02 13:32:44 +02:00
|
|
|
} else {
|
2021-05-03 23:29:27 +02:00
|
|
|
$url = get_option("map_preview_generator_url");
|
2021-05-02 13:32:44 +02:00
|
|
|
}
|
|
|
|
$url = str_replace("{{LAT}}", $lat, $url);
|
|
|
|
$url = str_replace("{{LNG}}", $lng, $url);
|
|
|
|
$url = str_replace("{{ZOOM}}", $zoom, $url);
|
|
|
|
return $url;
|
|
|
|
}
|
2021-04-25 17:19:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cachePreviewMap($filename, $lat, $lng, $zoom=16){
|
2021-05-02 13:32:44 +02:00
|
|
|
$url = $this->convertMapAddressToUrl($lat, $lng, $zoom);
|
2021-04-25 17:19:48 +02:00
|
|
|
$options = ['http' => [
|
|
|
|
'user_agent' => 'AllertaVVF dev version (cached map previews generator)'
|
|
|
|
]];
|
|
|
|
$context = stream_context_create($options);
|
|
|
|
$data = file_get_contents($url, false, $context);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!file_exists('resources/images/map_cache')) {
|
|
|
|
mkdir('resources/images/map_cache', 0755, true);
|
|
|
|
}
|
2021-04-25 19:53:37 +02:00
|
|
|
$filePath = "resources/images/map_cache/".$filename.".png";
|
|
|
|
file_put_contents($filePath, $data);
|
|
|
|
if(extension_loaded('gd')){
|
2021-05-03 23:29:27 +02:00
|
|
|
$img = imagecreatefromstring(file_get_contents($filePath));
|
|
|
|
if(get_option("map_preview_generator_add_marker") && (!get_option("map_preview_generator_url_marker") || get_option("map_preview_generator_url_marker") == "")){
|
2021-05-02 13:32:44 +02:00
|
|
|
$marker = imagecreatefromgif("resources/images/marker.gif");
|
|
|
|
imagecopy($img, $marker, 120, 87, 0, 0, 25, 41);
|
|
|
|
}
|
2021-05-03 23:29:27 +02:00
|
|
|
if(get_option("map_preview_generator") == "osm"){
|
2021-05-02 13:32:44 +02:00
|
|
|
$textcolor = imagecolorallocate($img, 0, 0, 0);
|
|
|
|
imagestring($img, 5, 0, 236, ' OpenStreetMap contributors', $textcolor);
|
|
|
|
}
|
2021-04-25 19:53:37 +02:00
|
|
|
imagepng($img, $filePath);
|
|
|
|
imagedestroy($img);
|
|
|
|
}
|
2021-04-25 17:19:48 +02:00
|
|
|
} catch (\Throwable $th) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkPlaceParam($place){
|
2021-05-03 23:29:27 +02:00
|
|
|
if(get_option("generate_map_preview")){
|
2021-05-02 13:32:44 +02:00
|
|
|
if(preg_match('/[+-]?\d+([.]\d+)?[;][+-]?\d+([.]\d+)?/', $place)){
|
|
|
|
$lat = explode(";", $place)[0];
|
|
|
|
$lng = explode(";", $place)[1];
|
|
|
|
$mapImageID = \Delight\Auth\Auth::createUuid();
|
|
|
|
$this->cachePreviewMap($mapImageID, $lat, $lng);
|
|
|
|
$place = $place . "#" . $mapImageID;
|
|
|
|
}
|
2021-04-25 17:19:48 +02:00
|
|
|
}
|
|
|
|
return $place;
|
|
|
|
}
|
2021-05-04 15:31:10 +02:00
|
|
|
|
|
|
|
public function savePlaceReverse($place){
|
|
|
|
if(strpos($place, ";") === false) return 0;
|
|
|
|
$lat = explode(";", $place)[0];
|
|
|
|
$lng = explode("#", explode(";", $place)[1])[0];
|
|
|
|
|
|
|
|
$url = sprintf("https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=%s&lon=%s", $lat, $lng);
|
|
|
|
$options = ['http' => [
|
|
|
|
'user_agent' => 'AllertaVVF dev version (place info downloader)'
|
|
|
|
]];
|
|
|
|
$context = stream_context_create($options);
|
|
|
|
$data = file_get_contents($url, false, $context);
|
|
|
|
|
|
|
|
$this->db->insert(
|
|
|
|
DB_PREFIX."_places_info",
|
|
|
|
["reverse_json" => $data]
|
|
|
|
);
|
|
|
|
return $this->db->getLastInsertId();
|
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2020-10-05 23:32:39 +02:00
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
class options
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
protected $db;
|
2020-11-13 18:57:47 +01:00
|
|
|
public $load_from_file = true;
|
|
|
|
public $options = [];
|
|
|
|
public $options_cache_file = null;
|
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function __construct($db){
|
|
|
|
$this->db = $db;
|
2020-11-13 18:57:47 +01:00
|
|
|
$file_infos = pathinfo(array_reverse(debug_backtrace())[0]['file']);
|
|
|
|
if(strpos($file_infos['dirname'], 'resources') !== false) {
|
|
|
|
$this->options_cache_file = "../../options.txt";
|
2020-07-07 18:02:46 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->options_cache_file = "options.txt";
|
|
|
|
}
|
|
|
|
if($this->load_from_file) {
|
|
|
|
if(file_exists($this->options_cache_file)/* && time()-@filemtime($this->options_cache_file) < 604800*/) {
|
2021-04-05 23:26:40 +02:00
|
|
|
$this->options = json_decode(file_get_contents($this->options_cache_file), true);
|
2020-11-13 18:57:47 +01:00
|
|
|
} else {
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->options = $db->select("SELECT * FROM `".DB_PREFIX."_options` WHERE `enabled` = 1");
|
2021-04-05 23:26:40 +02:00
|
|
|
file_put_contents($this->options_cache_file, json_encode($this->options));
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->options = $db->select("SELECT * FROM `".DB_PREFIX."_options` WHERE `enabled` = 1");
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2021-05-03 23:29:27 +02:00
|
|
|
if(empty($this->options)) header('Location: install/install.php');
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function get($name)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
|
|
|
if(defined($name)) {
|
|
|
|
return constant($name);
|
|
|
|
} else {
|
|
|
|
foreach($this->options as $option){
|
|
|
|
if($name == $option["name"]) {
|
|
|
|
return empty($option["value"]) ? false : $option["value"];
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 00:03:19 +01:00
|
|
|
return false;
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
final class Role
|
|
|
|
{
|
|
|
|
//https://github.com/delight-im/PHP-Auth/blob/master/src/Role.php
|
|
|
|
const GUEST = \Delight\Auth\Role::AUTHOR;
|
|
|
|
const BASIC_VIEWER = \Delight\Auth\Role::COLLABORATOR;
|
|
|
|
const FULL_VIEWER = \Delight\Auth\Role::CONSULTANT;
|
|
|
|
const EDITOR = \Delight\Auth\Role::CONSUMER;
|
|
|
|
const SUPER_EDITOR = \Delight\Auth\Role::CONTRIBUTOR;
|
|
|
|
const DEVELOPER = \Delight\Auth\Role::DEVELOPER;
|
|
|
|
const TESTER = \Delight\Auth\Role::CREATOR;
|
|
|
|
const EXTERNAL_VIEWER = \Delight\Auth\Role::REVIEWER;
|
|
|
|
const ADMIN = \Delight\Auth\Role::ADMIN;
|
|
|
|
const SUPER_ADMIN = \Delight\Auth\Role::SUPER_ADMIN;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
2020-05-25 22:43:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
class user
|
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
private $db = null;
|
2020-11-13 18:57:47 +01:00
|
|
|
private $tools = null;
|
2021-03-05 00:30:24 +01:00
|
|
|
private $profile_names = null;
|
2020-11-13 18:57:47 +01:00
|
|
|
public $auth = null;
|
|
|
|
public $authenticated = false;
|
2021-04-04 01:11:46 +02:00
|
|
|
public $holidays = null;
|
2020-11-13 18:57:47 +01:00
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function __construct($db, $tools)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db = $db;
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->tools = $tools;
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->auth = new \Delight\Auth\Auth($this->db, $tools->get_ip(), DB_PREFIX."_", false);
|
2021-04-03 17:47:34 +02:00
|
|
|
\header_remove('X-Frame-Options');
|
|
|
|
if(isset($_REQUEST["apiKey"]) && !is_null($_REQUEST["apiKey"])){
|
2021-05-03 23:29:27 +02:00
|
|
|
//var_dump("SELECT * FROM \`".DB_PREFIX."_api_keys\` WHERE apikey = :apikey");
|
|
|
|
//exit();
|
|
|
|
$api_key_row = $this->db->select("SELECT * FROM `".DB_PREFIX."_api_keys` WHERE apikey = :apikey", [":apikey" => $_REQUEST["apiKey"]]);
|
2021-02-22 18:36:20 +01:00
|
|
|
if(!empty($api_key_row)){
|
2021-05-03 23:29:27 +02:00
|
|
|
$user = $this->db->select("SELECT * FROM `".DB_PREFIX."_profiles` WHERE id = :id", [":id" => $api_key_row[0]["user"]]);
|
2021-02-22 18:36:20 +01:00
|
|
|
$user_id = $user[0]["id"];
|
|
|
|
$this->auth->admin()->logInAsUserById($user_id);
|
|
|
|
if(!empty($user)) {
|
|
|
|
if(is_null($user[0]["name"])) {
|
|
|
|
$_SESSION['_user_name'] = $this->auth->getUsername();
|
|
|
|
} else {
|
|
|
|
$_SESSION['_user_name'] = $user[0]["name"];
|
|
|
|
}
|
|
|
|
$_SESSION['_user_hidden'] = $user[0]["hidden"];
|
|
|
|
$_SESSION['_user_disabled'] = $user[0]["disabled"];
|
|
|
|
$_SESSION['_user_chief'] = $user[0]["chief"];
|
|
|
|
setcookie("authenticated", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->authenticated = $this->auth->isLoggedIn();
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->profile_names = $this->db->select("SELECT `id`, `name` FROM `".DB_PREFIX."_profiles`");
|
|
|
|
$this->user_names = $this->db->select("SELECT `id`, `username` FROM `".DB_PREFIX."_users`");
|
|
|
|
$this->holidays = Yasumi\Yasumi::create(get_option("holidays_provider") ?: "USA", date("Y"), get_option("holidays_language") ?: "en_US");
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function authenticated()
|
|
|
|
{
|
|
|
|
return $this->authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requirelogin($redirect=true)
|
|
|
|
{
|
|
|
|
$this->tools->profiler_start("Require login");
|
|
|
|
if(!$this->authenticated()) {
|
|
|
|
if($redirect) {
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->tools->redirect(get_option("web_url"));
|
2020-11-13 18:57:47 +01:00
|
|
|
} else {
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->tools->profiler_stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requireRole($role, $adminGranted=true)
|
|
|
|
{
|
|
|
|
return $this->auth->hasRole($role) || $adminGranted && $role !== Role::DEVELOPER && $this->auth->hasRole(Role::ADMIN) || $role !== Role::DEVELOPER && $this->auth->hasRole(Role::SUPER_ADMIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function name($replace=false)
|
|
|
|
{
|
|
|
|
if(isset($_SESSION['_user_name'])) {
|
|
|
|
$return_name = $_SESSION['_user_name'];
|
2020-05-02 12:06:45 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
$check_name = $this->nameById($this->auth->getUserId());
|
|
|
|
if($check_name) {
|
|
|
|
$return_name = $check_name;
|
|
|
|
} else {
|
|
|
|
$return_name = "not authenticated";
|
|
|
|
}
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
if($replace) {
|
|
|
|
return str_replace(" ", "_", $return_name);
|
2020-05-25 22:43:56 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
return $return_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function nameById($id)
|
|
|
|
{
|
2021-03-19 22:35:37 +01:00
|
|
|
$name = null;
|
2021-03-05 00:30:24 +01:00
|
|
|
foreach($this->profile_names as $profile) {
|
2021-03-05 23:00:52 +01:00
|
|
|
if($profile["id"] == $id && !is_null($profile["name"])) {
|
2021-03-19 22:35:37 +01:00
|
|
|
$name = s($profile["name"], false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_null($name)){
|
|
|
|
foreach($this->user_names as $user) {
|
|
|
|
if($user["id"] == $id){
|
|
|
|
return(s($user["username"], false));
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 22:35:37 +01:00
|
|
|
if(is_null($name)) return false;
|
2020-05-25 22:43:56 +02:00
|
|
|
}
|
2021-03-19 22:35:37 +01:00
|
|
|
return $name;
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-10-27 01:15:23 +01:00
|
|
|
|
2021-04-28 15:00:43 +02:00
|
|
|
public function hidden($user = null)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
2021-04-28 15:00:43 +02:00
|
|
|
if(is_null($user)){
|
|
|
|
$user = $this->auth->getUserId();
|
|
|
|
}
|
2021-05-03 23:29:27 +02:00
|
|
|
$result = $this->db->select("SELECT `hidden` FROM `".DB_PREFIX."_profiles` WHERE id = :id", [":id" => $user]);
|
2021-04-28 15:00:43 +02:00
|
|
|
if(isset($result[0]) && isset($result[0]["hidden"])){
|
|
|
|
return boolval($result[0]["hidden"]);
|
|
|
|
}
|
|
|
|
return false;
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2020-10-27 01:15:23 +01:00
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function available($id)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
$user = $this->db->select("SELECT available FROM `".DB_PREFIX."_users` WHERE id = :id", [":id" => $id]);
|
2020-11-13 18:57:47 +01:00
|
|
|
if(empty($user)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $user[0]["available"];
|
|
|
|
}
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-10-27 01:15:23 +01:00
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
public function info()
|
|
|
|
{
|
|
|
|
return array("autenticated" => $this->authenticated(), "id" => $this->auth->getUserId(), "name" => $this->name(), "full_viewer" => $this->requireRole(Role::FULL_VIEWER), "tester" => $this->requireRole(Role::TESTER), "developer" => $this->requireRole(Role::DEVELOPER));
|
|
|
|
}
|
2020-05-02 12:06:45 +02:00
|
|
|
|
2021-04-04 21:44:35 +02:00
|
|
|
public function login($name, $password, $remember_me)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
|
|
|
$this->tools->profiler_start("Login");
|
|
|
|
if(!empty($name)) {
|
|
|
|
if(!empty($password)) {
|
|
|
|
try {
|
|
|
|
if ($remember_me) {
|
|
|
|
// keep logged in for one year
|
|
|
|
$rememberDuration = (int) (60 * 60 * 24 * 365.25);
|
|
|
|
} else {
|
|
|
|
// do not keep logged in after session ends
|
|
|
|
$rememberDuration = null;
|
|
|
|
}
|
|
|
|
$this->auth->loginWithUsername($name, $password, $rememberDuration);
|
|
|
|
}
|
|
|
|
catch (\Delight\Auth\InvalidEmailException $e) {
|
|
|
|
$this->tools->profiler_stop();
|
|
|
|
return ["status" => "error", "code" => 010, "text" => "Wrong email address"];
|
|
|
|
}
|
|
|
|
catch (\Delight\Auth\InvalidPasswordException $e) {
|
|
|
|
$this->tools->profiler_stop();
|
|
|
|
return ["status" => "error", "code" => 011, "text" => "Wrong password"];
|
|
|
|
}
|
|
|
|
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
|
|
|
$this->tools->profiler_stop();
|
|
|
|
return ["status" => "error", "code" => 012, "text" => "Email not verified"];
|
|
|
|
}
|
2020-11-22 01:31:43 +01:00
|
|
|
catch (\Delight\Auth\UnknownUsernameException $e) {
|
|
|
|
$this->tools->profiler_stop();
|
|
|
|
return ["status" => "error", "code" => 013, "text" => "Wrong username"];
|
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
catch (\Delight\Auth\TooManyRequestsException $e) {
|
|
|
|
$this->tools->profiler_stop();
|
|
|
|
return ["status" => "error", "code" => 020, "text" => "Too many requests"];
|
|
|
|
}
|
|
|
|
if($this->auth->isLoggedIn()) {
|
2021-03-24 17:10:44 +01:00
|
|
|
$this->log("Login", $this->auth->getUserId());
|
2021-05-03 23:29:27 +02:00
|
|
|
$user = $this->db->select("SELECT * FROM `".DB_PREFIX."_profiles` WHERE id = :id", [":id" => $this->auth->getUserId()]);
|
2020-11-13 18:57:47 +01:00
|
|
|
if(!empty($user)) {
|
|
|
|
if(is_null($user[0]["name"])) {
|
|
|
|
$_SESSION['_user_name'] = $this->auth->getUsername();
|
|
|
|
} else {
|
|
|
|
$_SESSION['_user_name'] = $user[0]["name"];
|
|
|
|
}
|
|
|
|
$_SESSION['_user_hidden'] = $user[0]["hidden"];
|
|
|
|
$_SESSION['_user_disabled'] = $user[0]["disabled"];
|
|
|
|
$_SESSION['_user_chief'] = $user[0]["chief"];
|
|
|
|
$this->tools->profiler_stop();
|
|
|
|
setcookie("authenticated", true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 22:43:56 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->tools->profiler_stop();
|
|
|
|
return ["status" => "error", "code" => 002];
|
2020-05-25 22:43:56 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
} else {
|
2020-10-05 23:32:39 +02:00
|
|
|
$this->tools->profiler_stop();
|
2020-11-13 18:57:47 +01:00
|
|
|
return ["status" => "error", "code" => 001];
|
2020-05-02 12:06:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2021-03-24 17:10:44 +01:00
|
|
|
public function log($action, $changed=null, $editor=null, $timestamp=null)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
|
|
|
$this->tools->profiler_start("Log");
|
2021-03-24 15:20:42 +01:00
|
|
|
if(is_null($timestamp)){
|
|
|
|
$date = new Datetime('now');
|
|
|
|
$timestamp = $date->format('Y-m-d H:i:s');
|
|
|
|
}
|
2021-03-24 17:10:44 +01:00
|
|
|
if(is_null($changed)){
|
|
|
|
$changed = $this->auth->getUserId();
|
|
|
|
}
|
|
|
|
if(is_null($editor)){
|
|
|
|
$editor = $changed;
|
|
|
|
}
|
2021-04-28 15:00:43 +02:00
|
|
|
if(!$this->hidden($editor)){
|
2021-05-03 23:29:27 +02:00
|
|
|
if(get_option("log_save_ip")){
|
2021-04-28 15:00:43 +02:00
|
|
|
$ip = $this->tools->get_ip();
|
|
|
|
} else {
|
|
|
|
$ip = null;
|
|
|
|
}
|
|
|
|
$source_type = defined("REQUEST_USING_API") ? "api" : "web";
|
|
|
|
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? mb_strimwidth($_SERVER['HTTP_USER_AGENT'], 0, 200, "...") : null;
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->insert(
|
|
|
|
DB_PREFIX."_log",
|
|
|
|
["action" => $action, "changed" => $changed, "editor" => $editor, "timestamp" => $timestamp, "ip" => $ip, "source_type" => $source_type, "user_agent" => $user_agent]
|
|
|
|
);
|
2021-03-24 15:20:42 +01:00
|
|
|
}
|
2020-10-05 23:32:39 +02:00
|
|
|
$this->tools->profiler_stop();
|
2020-06-17 22:24:14 +02:00
|
|
|
}
|
2020-06-17 22:44:29 +02:00
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
try {
|
2021-03-24 17:10:44 +01:00
|
|
|
$this->log("Logout");
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->auth->logOut();
|
|
|
|
$this->auth->destroySession();
|
|
|
|
setcookie("authenticated", false, time() - 3600);
|
|
|
|
}
|
|
|
|
catch (\Delight\Auth\NotLoggedInException $e) {
|
|
|
|
die('Not logged in');
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 12:06:45 +02:00
|
|
|
|
2020-12-30 23:57:56 +01:00
|
|
|
public function add_user($email, $name, $username, $password, $phone_number, $birthday, $chief, $driver, $hidden, $disabled, $inserted_by)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
2021-04-04 21:44:35 +02:00
|
|
|
//TODO: save birthday in db
|
|
|
|
bdump($birthday);
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->tools->profiler_start("Add user");
|
|
|
|
$userId = $this->auth->admin()->createUserWithUniqueUsername($email, $password, $username);
|
|
|
|
if($userId) {
|
|
|
|
$hidden = $hidden ? 1 : 0;
|
|
|
|
$disabled = $disabled ? 1 : 0;
|
2020-11-25 11:29:19 +01:00
|
|
|
$chief = $chief ? 1 : 0;
|
|
|
|
$driver = $driver ? 1 : 0;
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->insert(
|
|
|
|
DB_PREFIX."_profiles",
|
|
|
|
["hidden" => $hidden, "disabled" => $disabled, "name" => $name, "phone_number" => $phone_number, "chief" => $chief, "driver" => $driver]
|
|
|
|
);
|
2020-11-25 11:29:19 +01:00
|
|
|
if($chief == 1) {
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->auth->admin()->addRoleForUserById($userId, Role::FULL_VIEWER);
|
|
|
|
}
|
2021-03-24 17:10:44 +01:00
|
|
|
$this->log("User added", $userId, $inserted_by);
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->tools->profiler_stop();
|
|
|
|
return $userId;
|
|
|
|
} else {
|
|
|
|
$this->tools->profiler_stop();
|
2020-12-30 23:57:56 +01:00
|
|
|
return false;
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2020-09-02 16:57:56 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
|
|
|
|
public function remove_user($id, $removed_by)
|
|
|
|
{
|
|
|
|
$this->tools->profiler_start("Remove user");
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->delete(
|
|
|
|
DB_PREFIX."_users",
|
|
|
|
["id" => $id]
|
|
|
|
);
|
|
|
|
$this->db->delete(
|
|
|
|
DB_PREFIX."_profiles",
|
|
|
|
["id" => $id]
|
|
|
|
);
|
2021-03-24 15:20:42 +01:00
|
|
|
$this->log("User removed", null, $removed_by);
|
2020-11-13 18:57:47 +01:00
|
|
|
$this->tools->profiler_stop();
|
|
|
|
}
|
2020-11-14 15:25:24 +01:00
|
|
|
|
|
|
|
public function online_time_update($id=null){
|
|
|
|
$this->tools->profiler_start("Update online timestamp");
|
|
|
|
if(is_null($id)) $id = $this->auth->getUserId();
|
|
|
|
$time = time();
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->update(
|
|
|
|
DB_PREFIX."_profiles",
|
|
|
|
["online_time" => $time],
|
|
|
|
["id" => $id]
|
|
|
|
);
|
2020-11-14 15:25:24 +01:00
|
|
|
bdump(["id" => $id, "time" => $time]);
|
|
|
|
$this->tools->profiler_stop();
|
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-24 17:10:44 +01:00
|
|
|
class crud
|
|
|
|
{
|
|
|
|
public $tools = null;
|
2021-05-03 23:29:27 +02:00
|
|
|
public $db = null;
|
2021-03-24 17:10:44 +01:00
|
|
|
public $user = null;
|
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function __construct($tools, $db, $user)
|
2021-03-24 17:10:44 +01:00
|
|
|
{
|
|
|
|
$this->tools = $tools;
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db = $db;
|
2021-03-24 17:10:44 +01:00
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function increment_services($increment)
|
2021-03-24 17:10:44 +01:00
|
|
|
{
|
|
|
|
bdump($increment);
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->exec(
|
|
|
|
"UPDATE `".DB_PREFIX."_profiles` SET `services`= services + 1 WHERE id IN ($increment)"
|
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function getIncrement_services($id)
|
2021-03-24 17:10:44 +01:00
|
|
|
{
|
|
|
|
bdump($id);
|
2021-05-03 23:29:27 +02:00
|
|
|
$increment = $this->db->selectValue(
|
|
|
|
"SELECT `increment` FROM `".DB_PREFIX."_services` WHERE `id` = :id LIMIT 0, 1",
|
|
|
|
["id" => $id]
|
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
bdump($increment);
|
|
|
|
return $increment;
|
|
|
|
}
|
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
public function decrease_services($id)
|
2021-03-24 17:10:44 +01:00
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
$increment = $this->getIncrement_services($id);
|
|
|
|
$this->db->exec(
|
|
|
|
"UPDATE `".DB_PREFIX."_profiles` SET `services`= services - 1 WHERE id IN ($increment)"
|
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function increment_trainings($increment)
|
|
|
|
{
|
|
|
|
bdump($increment);
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->exec(
|
|
|
|
"UPDATE `".DB_PREFIX."_profiles` SET `trainings`= trainings + 1 WHERE id IN ($increment)"
|
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getIncrement_trainings($id)
|
|
|
|
{
|
|
|
|
bdump($id);
|
2021-05-03 23:29:27 +02:00
|
|
|
$increment = $this->db->selectValue(
|
|
|
|
"SELECT `increment` FROM `".DB_PREFIX."_trainings` WHERE `id` = :id LIMIT 0, 1",
|
|
|
|
["id" => $id]
|
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
bdump($increment);
|
|
|
|
return $increment;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function decrease_trainings($id)
|
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
$increment = $this->getIncrement_trainings($id);
|
|
|
|
$this->db->exec(
|
|
|
|
"UPDATE `".DB_PREFIX."_profiles` SET `trainings`= trainings - 1 WHERE id IN ($increment)"
|
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function add_service($date, $code, $beginning, $end, $chief, $drivers, $crew, $place, $notes, $type, $increment, $inserted_by)
|
|
|
|
{
|
|
|
|
$drivers = implode(",", $drivers);
|
|
|
|
bdump($drivers);
|
|
|
|
$crew = implode(",", $crew);
|
|
|
|
bdump($crew);
|
|
|
|
$increment = implode(",", $increment);
|
|
|
|
bdump($increment);
|
|
|
|
$date = date('Y-m-d H:i:s', strtotime($date));
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->insert(
|
|
|
|
DB_PREFIX."_services",
|
2021-05-04 15:31:10 +02:00
|
|
|
["date" => $date, "code" => $code, "beginning" => $beginning, "end" => $end, "chief" => $chief, "drivers" => $drivers, "crew" => $crew, "place" => $place, "place_reverse" => $this->tools->savePlaceReverse($place), "notes" => $notes, "type" => $type, "increment" => $increment, "inserted_by" => $inserted_by]
|
2021-05-03 23:29:27 +02:00
|
|
|
);
|
|
|
|
$this->increment_services($increment);
|
2021-03-24 17:10:44 +01:00
|
|
|
$this->user->log("Service added");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove_service($id)
|
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->decrease_services($id);
|
|
|
|
$this->db->delete(
|
|
|
|
DB_PREFIX."_services",
|
|
|
|
["id" => $id]
|
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
$this->user->log("Service removed");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function edit_service($id, $date, $code, $beginning, $end, $chief, $drivers, $crew, $place, $notes, $type, $increment, $inserted_by)
|
|
|
|
{
|
|
|
|
$this->remove_service($id);
|
|
|
|
$this->add_service($date, $code, $beginning, $end, $chief, $drivers, $crew, $place, $notes, $type, $increment, $inserted_by);
|
|
|
|
$this->user->log("Service edited");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_training($date, $name, $start_time, $end_time, $chief, $crew, $place, $notes, $increment, $inserted_by)
|
|
|
|
{
|
|
|
|
$crew = implode(",", $crew);
|
|
|
|
bdump($crew);
|
|
|
|
$increment = implode(",", $increment);
|
|
|
|
bdump($increment);
|
|
|
|
$date = date('Y-m-d H:i:s', strtotime($date));
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->insert(
|
|
|
|
DB_PREFIX."_trainings",
|
2021-05-04 15:31:10 +02:00
|
|
|
["date" => $date, "name" => $name, "beginning" => $start_time, "end" => $end_time, "chief" => $chief, "crew" => $crew, "place" => $place, "place_reverse" => $this->tools->savePlaceReverse($place), "notes" => $notes, "increment" => $increment, "inserted_by" => $inserted_by]
|
2021-05-03 23:29:27 +02:00
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
$this->increment_trainings($increment);
|
|
|
|
$this->user->log("Training added");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove_training($id)
|
|
|
|
{
|
|
|
|
$this->decrease_trainings($id);
|
|
|
|
bdump($id);
|
2021-05-03 23:29:27 +02:00
|
|
|
$this->db->delete(
|
|
|
|
DB_PREFIX."_trainings",
|
|
|
|
["id" => $id]
|
|
|
|
);
|
2021-03-24 17:10:44 +01:00
|
|
|
$this->user->log("Training removed");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function edit_training($id, $date, $name, $start_time, $end_time, $chief, $crew, $place, $notes, $increment, $inserted_by)
|
|
|
|
{
|
|
|
|
$this->remove_training($id);
|
|
|
|
$this->add_training($date, $name, $start_time, $end_time, $chief, $crew, $place, $notes, $increment, $inserted_by);
|
|
|
|
$this->user->log("Training edited");
|
|
|
|
}
|
2021-05-03 23:29:27 +02:00
|
|
|
|
|
|
|
public function exists($table, $id)
|
|
|
|
{
|
|
|
|
$result = $this->db->select("SELECT id FROM `".DB_PREFIX."_{$table}` WHERE id = :id", [":id" => $id]);
|
|
|
|
return !empty($result);
|
|
|
|
}
|
2021-03-24 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
class translations
|
|
|
|
{
|
|
|
|
public $loaded_languages = ["en", "it"];
|
|
|
|
public $default_language = "en";
|
|
|
|
public $language = null;
|
|
|
|
public $client_languages = ["en"];
|
|
|
|
public $loaded_translations = [];
|
|
|
|
public $filename = "";
|
|
|
|
|
|
|
|
public function client_languages()
|
|
|
|
{
|
|
|
|
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
|
|
|
$client_languages = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
|
2020-07-02 21:45:45 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
$client_languages = "en-US;q=0.5,en;q=0.3";
|
2020-07-02 21:45:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
if(strpos($client_languages, ';') == false) {
|
|
|
|
if(strpos($client_languages, '-') !== false) {
|
|
|
|
return [substr($client_languages, 0, 5)];
|
2020-07-02 21:45:45 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
return [substr($client_languages, 0, 2)];
|
2020-07-02 21:45:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
} else {
|
|
|
|
$client_languages = explode(",", $client_languages);
|
|
|
|
$tmp_languages = [];
|
2021-04-04 21:44:35 +02:00
|
|
|
foreach($client_languages as $language){
|
2020-11-13 18:57:47 +01:00
|
|
|
if(strpos($language, ';') == false) {
|
|
|
|
$tmp_languages[$language] = 1;
|
|
|
|
} else {
|
|
|
|
$tmp_languages[explode(";q=", $language)[0]] = (float) explode(";q=", $language)[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arsort($tmp_languages);
|
|
|
|
return array_keys($tmp_languages);
|
2020-07-02 21:45:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 00:03:19 +01:00
|
|
|
public function __construct($force_language)
|
2020-11-13 18:57:47 +01:00
|
|
|
{
|
|
|
|
$this->client_languages = $this->client_languages();
|
2020-12-20 21:40:53 +01:00
|
|
|
if(isset($_COOKIE["forceLanguage"]) && in_array($_COOKIE["forceLanguage"], $this->loaded_languages)){
|
2020-12-16 17:59:47 +01:00
|
|
|
$this->language = $_COOKIE["forceLanguage"];
|
2020-12-31 00:03:19 +01:00
|
|
|
} else if($force_language && in_array($force_language, $this->loaded_languages)){
|
|
|
|
$this->language = $force_language;
|
2020-12-16 17:59:47 +01:00
|
|
|
} else {
|
|
|
|
foreach($this->client_languages as $language){
|
|
|
|
if(in_array($language, $this->loaded_languages) && $this->language == null) {
|
|
|
|
$this->language = $language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($this->language == null) {
|
|
|
|
$this->language = "en";
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$file_infos = pathinfo(array_reverse(debug_backtrace())[0]['file']);
|
|
|
|
if(strpos($file_infos['dirname'], 'resources') !== false) {
|
|
|
|
$this->filename = "../../translations/".$this->language."/".$file_infos['basename'];
|
|
|
|
} else {
|
|
|
|
$this->filename = "translations/".$this->language."/".$file_infos['basename'];
|
|
|
|
}
|
|
|
|
if (file_exists($this->filename)) {
|
|
|
|
$this->loaded_translations = array_merge(include "translations/".$this->language."/base.php", include $this->filename);
|
|
|
|
} else {
|
|
|
|
try{
|
|
|
|
$this->loaded_translations = include "translations/".$this->language."/base.php";
|
|
|
|
} catch(Exception $e) {
|
|
|
|
$this->loaded_translations = include "../../translations/".$this->language."/base.php";
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 21:45:45 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
|
|
|
|
public function translate($string)
|
|
|
|
{
|
|
|
|
bdump($string);
|
|
|
|
try {
|
|
|
|
if (!array_key_exists($string, $this->loaded_translations)) {
|
|
|
|
throw new Exception('string does not exist');
|
|
|
|
}
|
|
|
|
return $this->loaded_translations[$string];
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
bdump($this->filename);
|
|
|
|
bdump($e, $string);
|
|
|
|
return $string;
|
|
|
|
}
|
2020-07-03 14:27:00 +02:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2021-05-03 23:29:27 +02:00
|
|
|
|
|
|
|
function init_db(){
|
|
|
|
global $db;
|
|
|
|
|
|
|
|
$dataSource = new \Delight\Db\PdoDataSource('mysql');
|
|
|
|
$dataSource->setHostname(DB_HOST);
|
|
|
|
$dataSource->setPort(3306);
|
|
|
|
$dataSource->setDatabaseName(DB_NAME);
|
|
|
|
$dataSource->setCharset('utf8mb4');
|
|
|
|
$dataSource->setUsername(DB_USER);
|
|
|
|
$dataSource->setPassword(DB_PASSWORD);
|
|
|
|
$db = \Delight\Db\PdoDatabase::fromDataSource($dataSource);
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:12:07 +02:00
|
|
|
$webpack_manifest_path = realpath("resources/dist/assets-manifest.json");
|
2020-11-13 18:57:47 +01:00
|
|
|
function init_class($enableDebugger=true, $headers=true)
|
|
|
|
{
|
2021-05-03 23:29:27 +02:00
|
|
|
global $tools, $options, $db, $user, $crud, $translations, $debugbar;
|
|
|
|
init_db();
|
|
|
|
$options = new options($db);
|
|
|
|
$tools = new tools($db, $enableDebugger);
|
|
|
|
$user = new user($db, $tools);
|
|
|
|
$crud = new crud($tools, $db, $user);
|
|
|
|
$translations = new translations(get_option("force_language"));
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
if($headers) {
|
2021-02-20 21:05:03 +01:00
|
|
|
//TODO adding require-trusted-types-for 'script';
|
2021-05-16 21:32:43 +02:00
|
|
|
$csp_rules = [
|
|
|
|
"default-src 'self' data: *.tile.openstreetmap.org nominatim.openstreetmap.org",
|
|
|
|
"connect-src 'self' *.sentry.io nominatim.openstreetmap.org",
|
2021-05-27 23:47:47 +02:00
|
|
|
"script-src 'nonce-{$tools->script_nonce}' 'self' 'unsafe-eval'",
|
2021-05-16 21:32:43 +02:00
|
|
|
"img-src 'self' data: *.tile.openstreetmap.org",
|
2021-05-27 23:47:47 +02:00
|
|
|
"object-src 'self'",
|
2021-05-16 21:32:43 +02:00
|
|
|
"style-src 'self' 'unsafe-inline'",
|
|
|
|
"base-uri 'self'"
|
|
|
|
];
|
2021-02-20 21:05:03 +01:00
|
|
|
if(defined(SENTRY_CSP_REPORT_URI) && SENTRY_CSP_REPORT_URI !== false){
|
2021-05-16 21:32:43 +02:00
|
|
|
$csp_rules[] = "report-uri ".SENTRY_CSP_REPORT_URI;
|
|
|
|
}
|
|
|
|
$csp = implode("; ", $csp_rules);
|
|
|
|
if(!isset($_COOKIE["JSless"]) && (isset($_GET["JSless"]) ? !$_GET["JSless"] : true)){
|
|
|
|
header("Content-Security-Policy: $csp");
|
|
|
|
header("X-XSS-Protection: 1; mode=block");
|
|
|
|
header("X-Content-Type-Options: nosniff");
|
|
|
|
header("Permissions-Policy: interest-cohort=(), camera=(), microphone=(), payment=(), usb=()");
|
|
|
|
header("Referrer-Policy: no-referrer");
|
|
|
|
header("X-Frame-Options: DENY");
|
2021-02-20 21:05:03 +01:00
|
|
|
}
|
2020-11-13 18:57:47 +01:00
|
|
|
}
|
2021-03-01 16:35:56 +01:00
|
|
|
|
|
|
|
if(SENTRY_ENABLED){
|
|
|
|
Sentry\configureScope(function (Sentry\State\Scope $scope): void {
|
|
|
|
global $user, $translations;
|
2021-03-01 22:39:49 +01:00
|
|
|
if($user->authenticated()){
|
|
|
|
$id = $user->auth->getUserId();
|
|
|
|
$username = $user->nameById($id);
|
|
|
|
if($username !== false){
|
|
|
|
$scope->setUser([
|
|
|
|
'id' => $id,
|
|
|
|
'username' => $username
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 16:35:56 +01:00
|
|
|
$scope->setTag('page.locale', $translations->client_languages[0]);
|
|
|
|
});
|
2021-03-04 09:52:11 +01:00
|
|
|
} else {
|
|
|
|
//TODO: add Monolog here
|
|
|
|
}
|
2021-03-01 16:35:56 +01:00
|
|
|
|
2021-03-04 09:52:11 +01:00
|
|
|
if($enableDebugger && $user->requireRole(Role::DEVELOPER)) {
|
|
|
|
$debugbar = new StandardDebugBar();
|
2021-03-05 23:27:17 +01:00
|
|
|
bdump(__DIR__);
|
|
|
|
$dir = str_replace("resources\ajax\\", "", __DIR__).DIRECTORY_SEPARATOR.'debug_storage';
|
2021-03-04 23:43:30 +01:00
|
|
|
$debugbar->setStorage(new DebugBar\Storage\FileStorage($dir));
|
2021-05-03 23:29:27 +02:00
|
|
|
//TODO: debug PDO
|
|
|
|
//$debugbar->addCollector(new DebugBar\DataCollector\PDO\PDOCollector($database->connection));
|
|
|
|
$debugbar->addCollector(new DebugBar\DataCollector\ConfigCollector($options->options));
|
2020-12-31 00:03:19 +01:00
|
|
|
} else {
|
2021-03-04 09:52:11 +01:00
|
|
|
$debugbar = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function customErrorPage() {
|
2021-04-01 22:29:18 +02:00
|
|
|
global $webpack_manifest_path;
|
2021-03-04 09:52:11 +01:00
|
|
|
$error = error_get_last();
|
|
|
|
if ($error) {
|
2021-04-04 21:44:35 +02:00
|
|
|
bdump($webpack_manifest_path);
|
2021-04-01 22:29:18 +02:00
|
|
|
require("error_page.php");
|
|
|
|
show_error_page(500);
|
2021-03-01 16:35:56 +01:00
|
|
|
}
|
2020-09-01 12:27:32 +02:00
|
|
|
}
|
2021-03-04 09:52:11 +01:00
|
|
|
register_shutdown_function('customErrorPage');
|
2020-12-29 15:55:36 +01:00
|
|
|
|
|
|
|
if(isset($_GET["disableSW"])){
|
|
|
|
setcookie("disableServiceWorkerInstallation", true);
|
|
|
|
$tools->redirect("?");
|
|
|
|
} else if(isset($_GET["enableSW"])){
|
|
|
|
setcookie("disableServiceWorkerInstallation", false, time() - 3600);
|
|
|
|
setcookie("forceServiceWorkerInstallation", false, time() - 3600);
|
|
|
|
$tools->redirect("?");
|
|
|
|
} else if(isset($_GET["forceSW"])){
|
|
|
|
setcookie("disableServiceWorkerInstallation", false, time() - 3600);
|
|
|
|
setcookie("forceServiceWorkerInstallation", true);
|
|
|
|
$tools->redirect("?");
|
|
|
|
}
|
2020-07-02 21:45:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
function t($string, $echo=true)
|
|
|
|
{
|
|
|
|
global $translations;
|
|
|
|
if($echo) {
|
|
|
|
echo $translations->translate($string);
|
2020-10-05 23:32:39 +02:00
|
|
|
} else {
|
2020-11-13 18:57:47 +01:00
|
|
|
return $translations->translate($string);
|
2020-10-05 23:32:39 +02:00
|
|
|
}
|
2020-07-02 23:49:41 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
function s($string, $echo=true, $htmlAllowed=false, $htmlPurifierOptions=[])
|
|
|
|
{
|
|
|
|
global $tools;
|
|
|
|
if($echo) {
|
|
|
|
echo $tools->sanitize($string, $htmlAllowed, $htmlPurifierOptions);
|
|
|
|
} else {
|
|
|
|
return $tools->sanitize($string, $htmlAllowed, $htmlPurifierOptions);
|
|
|
|
}
|
2020-09-11 18:49:47 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 23:29:27 +02:00
|
|
|
function get_option($option){
|
|
|
|
global $options;
|
|
|
|
return $options->get($option);
|
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
function p_start($name=null)
|
|
|
|
{
|
|
|
|
global $tools;
|
|
|
|
$tools->profiler_start($name);
|
2020-10-05 23:32:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 18:57:47 +01:00
|
|
|
function p_stop($name=null)
|
|
|
|
{
|
|
|
|
global $tools;
|
|
|
|
$tools->profiler_stop($name);
|
2020-10-05 23:32:39 +02:00
|
|
|
}
|