Skip to content

Commit

Permalink
add monolog logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed Mar 2, 2024
1 parent 7de6a53 commit b44788e
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ db-data
vendor
config.php
composer.lock
var
4 changes: 2 additions & 2 deletions actions-sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,11 @@ function freeBikes($number)

function log_sms($sms_uuid, $sender, $receive_time, $sms_text, $ip)
{
global $dbserver, $dbuser, $dbpassword, $dbname;
global $dbserver, $dbuser, $dbpassword, $dbname, $logger;
/**
* @var DbInterface
*/
$localdb = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname);
$localdb = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$localdb->connect();

#TODO does it needed???
Expand Down
10 changes: 6 additions & 4 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@

use BikeShare\Authentication\Auth;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use BikeShare\User\User;
use Psr\Log\LoggerInterface;

require_once 'vendor/autoload.php';
require("config.php");
require('actions-web.php');

/**
* @var DbInterface $db
* @var LoggerInterface $logger
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
$user = new User($db);
$auth = new Auth($db);

$auth->refreshSession();

$userid = $auth->getUserId();

if ($user->findPrivileges($userid)<=0) exit(_('You need admin privileges to access this page.'));
if ($user->findPrivileges($userid)<=0) {
$logger->error('User has no privileges to access this page', ['userid' => $userid]);
exit(_('You need admin privileges to access this page.'));
}
?>
<!DOCTYPE html>
<html lang="en">
Expand Down
2 changes: 0 additions & 2 deletions agree.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
/**
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
?>
<!DOCTYPE html>
<html lang="en">
Expand Down
5 changes: 2 additions & 3 deletions command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

use BikeShare\Authentication\Auth;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use BikeShare\Purifier\PhonePurifier;
use BikeShare\Purifier\PhonePurifierInterface;
use BikeShare\User\User;
use Psr\Log\LoggerInterface;

require_once 'vendor/autoload.php';
require("config.php");
require('actions-web.php');

/**
* @var DbInterface $db
* @var LoggerInterface $logger
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
$auth = new Auth($db);
$user = new User($db);
/**
Expand Down
19 changes: 13 additions & 6 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@
use BikeShare\SmsConnector\DebugConnector;
use BikeShare\SmsConnector\SmsConnectorFactory;
use BikeShare\User\User;
use Monolog\ErrorHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

require_once 'vendor/autoload.php';

$logger = new Logger('BikeShare');
$logger->pushHandler(new RotatingFileHandler( __DIR__ . '/var/log/log.log', 30, Logger::WARNING));
ErrorHandler::register($logger);

$locale = $systemlang . ".utf8";
setlocale(LC_ALL, $locale);
putenv("LANG=" . $locale);
bindtextdomain("messages", dirname(__FILE__) . '/languages');
textdomain("messages");

$sms = (new SmsConnectorFactory())->getConnector(
$sms = (new SmsConnectorFactory($logger))->getConnector(
!empty($connectors["sms"]) ? $connectors["sms"] : 'disabled',
!empty($connectors["config"][$connectors["sms"]]) ? json_decode($connectors["config"][$connectors["sms"]], true) : array(),
DEBUG
Expand All @@ -32,7 +39,7 @@
/**
* @var DbInterface $db
*/
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname);
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$db->connect();

/**
Expand Down Expand Up @@ -78,11 +85,11 @@ function error($message)

function logrequest($userid)
{
global $dbserver,$dbuser,$dbpassword,$dbname, $user;
global $dbserver,$dbuser,$dbpassword,$dbname, $user, $logger;
/**
* @var DbInterface
*/
$localdb = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname);
$localdb = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$localdb->connect();

#TODO does it needed???
Expand All @@ -95,12 +102,12 @@ function logrequest($userid)

function logresult($userid, $text)
{
global $dbserver, $dbuser, $dbpassword, $dbname;
global $dbserver, $dbuser, $dbpassword, $dbname, $logger;

/**
* @var DbInterface
*/
$localdb = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname);
$localdb = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$localdb->connect();

#TODO does it needed???
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
"ext-json": "*",
"ext-mysqli": "*",
"ext-gettext": "*",
"ext-curl": "*"
"ext-curl": "*",
"monolog/monolog": "^1.27"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.0",
"phpunit/phpunit": "^5.7"
}
}
}
10 changes: 0 additions & 10 deletions cron.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
<?php

use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;

require_once 'vendor/autoload.php';
require("config.php");
require("actions-web.php");

/**
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();

checklongrental();
?>
5 changes: 2 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

use BikeShare\Authentication\Auth;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use BikeShare\User\User;
use Psr\Log\LoggerInterface;

require_once 'vendor/autoload.php';
require "config.php";
require "actions-web.php";

/**
* @var DbInterface $db
* @var LoggerInterface $logger
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
$user = new User($db);
$auth = new Auth($db);

Expand Down
12 changes: 9 additions & 3 deletions install/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use Monolog\ErrorHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;

require_once '../vendor/autoload.php';
if (file_exists("../config.php")) {
Expand All @@ -12,8 +16,12 @@

/**
* @var DbInterface $db
* @var LoggerInterface $logger
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$logger = new Logger('BikeShare');
$logger->pushHandler(new RotatingFileHandler(__DIR__ . '/../var/log/log.log', 30, Logger::WARNING));
ErrorHandler::register($logger);
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$db->connect();

// create new PDF document
Expand Down Expand Up @@ -78,5 +86,3 @@

//Close and output PDF document
$pdf->Output('qrcodes.pdf', 'D');

?>
17 changes: 12 additions & 5 deletions install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use Monolog\ErrorHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

if (file_exists("../config.php")) {
exit("Project already installed. If you want to reinstall, please remove config.php file.");
Expand All @@ -12,6 +15,10 @@
$configfilename = "../config.php.example";
require $configfilename;

$logger = new Logger('BikeShare');
$logger->pushHandler(new RotatingFileHandler( __DIR__ . '/../var/log/log.log', 30, Logger::WARNING));
ErrorHandler::register($logger);

function changeconfigvalue($configvar,$postvar)
{
global $configfile;
Expand Down Expand Up @@ -210,7 +217,7 @@ function return_bytes($val) {
/**
* @var DbInterface $db
*/
$db=new MysqliDb($_POST["dbserver"],$_POST["dbuser"],$_POST["dbpassword"],$_POST["dbname"]);
$db = new MysqliDb($_POST["dbserver"], $_POST["dbuser"], $_POST["dbpassword"], $_POST["dbname"], $logger);
$db->connect();
$sql=file_get_contents("../docker-data/mysql/create-database.sql");
$sql=explode(";",$sql);
Expand Down Expand Up @@ -241,7 +248,7 @@ function return_bytes($val) {
/**
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$db->connect();
$result=$db->query("REPLACE INTO users SET userName='".$_POST["username"]."',password=SHA2('".$_POST["password"]."',512),mail='".$_POST["email"]."',number='".$_POST["phone"]."',privileges=7");
$userid=$db->getLastInsertId();
Expand All @@ -265,7 +272,7 @@ function return_bytes($val) {
/**
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$db->connect();
$stands=explode(",",$_POST["stands"]);
foreach ($stands as $stand)
Expand Down Expand Up @@ -331,7 +338,7 @@ function return_bytes($val) {
/**
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$db->connect();
?>
<h2>Set system options</h2>
Expand Down Expand Up @@ -403,7 +410,7 @@ function return_bytes($val) {
/**
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$db->connect();
$configfile=file($configfilename);
foreach ($_POST as $variable=>$value)
Expand Down
15 changes: 4 additions & 11 deletions receive.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
<?php

use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use BikeShare\SmsConnector\SmsConnectorInterface;
use Psr\Log\LoggerInterface;

require_once 'vendor/autoload.php';
require("config.php");

/**
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();

require("actions-sms.php");

/**
* @var SmsConnectorInterface $sms
* @var LoggerInterface $logger
* @var DbInterface $db
*/
log_sms($sms->getUUID(),$sms->getNumber(),$sms->getTime(),$sms->getMessage(),$sms->getIPAddress());

$args=preg_split("/\s+/",$sms->getProcessedMessage());//preg_split must be used instead of explode because of multiple spaces

if(!validateNumber($sms->getNumber()))
{
trigger_error("Invalid number: ".$sms->getNumber(), E_USER_WARNING);
$logger->error("Invalid number", ["number" => $sms->getNumber(), 'sms' => $sms]);
####
#$smsSender->send($sms->getNumber(),_('Your number is not registered.'));
}
Expand Down Expand Up @@ -121,5 +116,3 @@

$db->commit();
$sms->respond();

?>
5 changes: 2 additions & 3 deletions scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

use BikeShare\Authentication\Auth;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use BikeShare\User\User;
use Psr\Log\LoggerInterface;

require_once 'vendor/autoload.php';
require("config.php");
require('actions-qrcode.php');

/**
* @var DbInterface $db
* @var LoggerInterface $logger
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
$user = new User($db);
$auth = new Auth($db);

Expand Down
Loading

0 comments on commit b44788e

Please sign in to comment.