Browse Source

CodeSniffer cleaning second pass

develop
acab.io 1 year ago
parent
commit
295b39ebde
4 changed files with 243 additions and 201 deletions
  1. +26
    -24
      controller.inc.php
  2. +30
    -30
      controller.php
  3. +1
    -1
      index.php
  4. +186
    -146
      models/Reports.class.php

+ 26
- 24
controller.inc.php View File

@ -1,8 +1,10 @@
<?php
require_once 'models/Reports.class.php';
// https://gist.github.com/rchrd2/c94eb4701da57ce9a0ad4d2b00794131
function require_auth()
/**
* https://gist.github.com/rchrd2/c94eb4701da57ce9a0ad4d2b00794131
*/
function Require_auth()
{
$AUTH_USER = 'admin';
$AUTH_PASS = 'admin';
@ -20,9 +22,9 @@ function require_auth()
}
}
function listrss_action($cont,$twig,$message)
function Listrss_action($cont,$twig,$message)
{
$reports = $cont->get_rss();
$reports = $cont->Get_rss();
$template = $twig->load('feed.xml.twig');
$page_title="acab.io::status feed";
$page_url="https://status.acab.io";
@ -36,14 +38,14 @@ function listrss_action($cont,$twig,$message)
)
);
$xmlfile = fopen('rss.xml', 'r+');
fseek($xmlfile, 0); // On remet le curseur au début du fichier
fputs($xmlfile, $content); // On écrit le nouveau nombre de pages vues
fseek($xmlfile, 0);
fputs($xmlfile, $content);
fclose($xmlfile);
}
function listpublic_action($cont,$twig,$message)
function Listpublic_action($cont,$twig,$message)
{
$reports = $cont->get_public();
$reports = $cont->Get_public();
$template = $twig->load('public.html.twig');
$page_title="Last 5 reports";
echo $template->render(
@ -55,9 +57,9 @@ function listpublic_action($cont,$twig,$message)
);
}
function listlast_action($cont,$twig,$message,$now)
function Listlast_action($cont,$twig,$message,$now)
{
$reports = $cont->get_last_reports();
$reports = $cont->Get_last_reports();
$template = $twig->load('reports.html.twig');
$page_title="Last 5 reports";
echo $template->render(
@ -70,9 +72,9 @@ function listlast_action($cont,$twig,$message,$now)
);
}
function listall_action($cont,$twig,$message,$now)
function Listall_action($cont,$twig,$message,$now)
{
$reports = $cont->get_all_reports();
$reports = $cont->Get_all_reports();
$template = $twig->load('reports.html.twig');
$page_title="All reports";
echo $template->render(
@ -85,9 +87,9 @@ function listall_action($cont,$twig,$message,$now)
);
}
function detail_action($cont,$twig,$now,$id,$message='')
function Detail_action($cont,$twig,$now,$id,$message='')
{
$report = $cont->get_report_by_id($id);
$report = $cont->Get_report_by_id($id);
$template = $twig->load('report.html.twig');
$page_title="Update report";
echo $template->render(
@ -100,25 +102,25 @@ function detail_action($cont,$twig,$now,$id,$message='')
);
}
function suppr_action($cont,$id)
function Suppr_action($cont,$id)
{
return ($cont->delete_report_by_id($id));
return ($cont->Delete_report_by_id($id));
}
function suppr_description_action($cont, $id)
function Suppr_Description_action($cont, $id)
{
return ($cont->delete_description_by_id($id));
return ($cont->Delete_description_by_id($id));
}
function update_action($cont,$id,$date,$page_title,$maindescription,$state)
function Update_action($cont,$id,$date,$page_title,$maindescription,$state)
{
return ($cont->update($id, $date, $page_title, $maindescription, $state));
return ($cont->Update($id, $date, $page_title, $maindescription, $state));
}
function add_action($cont,$report)
function Add_action($cont,$report)
{
return ($cont->add_report($report));
return ($cont->Add_report($report));
}
function add_description_action($cont,$report)
function Add_Description_action($cont,$report)
{
return ($cont->add_description_report($report));
return ($cont->Add_description_report($report));
}

+ 30
- 30
controller.php View File

@ -9,78 +9,78 @@ $twig = new Twig\Environment($loader);
require 'controller.inc.php';
// Checking actions and parameters
// default to listpublic
// default to Listpublic
$action = $_GET['action'] ?? 'listpublic';
$message = "";
$now = date("Y-m-d H:i:s");
switch ($action) {
case "listpublic":
listpublic_action($cont, $twig, $message);
Listpublic_action($cont, $twig, $message);
break;
case "listrss":
listrss_action($cont, $twig, $message);
Listrss_action($cont, $twig, $message);
break;
case "listlast":
require_auth();
listlast_action($cont, $twig, $message, $now);
Require_auth();
Listlast_action($cont, $twig, $message, $now);
break;
case "listall":
require_auth();
listall_action($cont, $twig, $message, $now);
Require_auth();
Listall_action($cont, $twig, $message, $now);
break;
case "detail":
require_auth();
detail_action($cont, $twig, $now, $_GET['id']);
Require_auth();
Detail_action($cont, $twig, $now, $_GET['id']);
break;
case "suppr":
require_auth();
if (suppr_action($cont, $_GET['id'])) {
Require_auth();
if (Suppr_action($cont, $_GET['id'])) {
$message = "Report successfully removed ! !";
} else { $message = "Something went wrong !";
}
listlast_action($cont, $twig, $message, $now);
listrss_action($cont, $twig, $message);
Listlast_action($cont, $twig, $message, $now);
Listrss_action($cont, $twig, $message);
break;
case "suppr_description":
require_auth();
if (suppr_description_action($cont, $_GET['id'])) {
Require_auth();
if (Suppr_Description_action($cont, $_GET['id'])) {
$message = "Report detail successfully removed ! !";
} else { $message = "Something went wrong !";
}
listlast_action($cont, $twig, $message, $now);
listrss_action($cont, $twig, $message);
Listlast_action($cont, $twig, $message, $now);
Listrss_action($cont, $twig, $message);
break;
case "update":
require_auth();
Require_auth();
if (!empty($_GET['id']) and !empty($_GET['date']) and !empty($_GET['title']) and !empty($_GET['maindescription'])) {
$res = update_action($cont, $_GET['id'], $_GET['date'], $_GET['title'], $_GET['maindescription'], $_GET['state']);
$res = Update_action($cont, $_GET['id'], $_GET['date'], $_GET['title'], $_GET['maindescription'], $_GET['state']);
}
if (!empty($res)) {
$message = "Report successfully updated !";
} else {
$message = "Something went wrong !";
}
listlast_action($cont, $twig, $message, $now);
listrss_action($cont, $twig, $message);
Listlast_action($cont, $twig, $message, $now);
Listrss_action($cont, $twig, $message);
break;
case "add":
require_auth();
if (add_action($cont, $_GET)) {
Require_auth();
if (Add_action($cont, $_GET)) {
$message = "Report successfully added !";
} else { $message = "Something went wrong !";
}
listlast_action($cont, $twig, $message, $now);
listrss_action($cont, $twig, $message);
Listlast_action($cont, $twig, $message, $now);
Listrss_action($cont, $twig, $message);
break;
case "add_description":
require_auth();
if (add_description_action($cont, $_GET)) {
Require_auth();
if (Add_Description_action($cont, $_GET)) {
$message = "Report detail successfully added !";
} else { $message = "Something went wrong !";
}
listlast_action($cont, $twig, $message, $now);
listrss_action($cont, $twig, $message);
Listlast_action($cont, $twig, $message, $now);
Listrss_action($cont, $twig, $message);
break;
default:
listpublic_action($cont, $twig, $message, $now);
Listpublic_action($cont, $twig, $message, $now);
}

+ 1
- 1
index.php View File

@ -1,2 +1,2 @@
<?php
include 'controller.php';
require 'controller.php';

+ 186
- 146
models/Reports.class.php View File

@ -1,146 +1,186 @@
<?php
/** Class to manage reports */
class Reports {
/** DB connection */
private static $db;
/** DB connection contructor */
function __construct()
{
$dsn = 'sqlite:db/reports.sqlite3';
try{
self::$db=new PDO($dsn);
# We can now log any exceptions on Fatal error.
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
# Disable emulation of prepared statements, use REAL prepared statements instead.
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$db->exec('PRAGMA foreign_keys = ON;');
}
catch(PDOException $e){
printf("Échec de la connexion : %s\n", $e->getMessage());
$this->db = NULL;
}
}
/** Get last 5 entries */
function get_public()
{
$sql="SELECT REPORTS.DATE, REPORTS.TITLE, REPORTS.MAINDESCRIPTION, REPORTS.STATE,
GROUP_CONCAT(REPORT_DETAILS.DATE,'|') AS MSGDATE,
GROUP_CONCAT(REPORT_DETAILS.DESCRIPTION,'|') AS MSGDESCRIPTION
FROM REPORTS
LEFT JOIN REPORT_DETAILS ON REPORT_ID = REPORTS.ID
GROUP BY REPORTS.ID
ORDER BY REPORTS.DATE DESC
LIMIT 5";
$data=self::$db->query($sql);
return $data;
}
/** Get last 10 entries */
function get_rss()
{
$sql="SELECT REPORTS.DATE, REPORTS.TITLE, REPORTS.MAINDESCRIPTION, REPORTS.STATE,
GROUP_CONCAT(REPORT_DETAILS.DATE,'|') AS MSGDATE,
GROUP_CONCAT(REPORT_DETAILS.DESCRIPTION,'|') AS MSGDESCRIPTION
FROM REPORTS
LEFT JOIN REPORT_DETAILS ON REPORT_ID = REPORTS.ID
GROUP BY REPORTS.ID
ORDER BY REPORTS.DATE DESC
LIMIT 10";
$data=self::$db->query($sql);
return $data;
}
/** Get last 5 entries */
function get_last_reports()
{
$sql="SELECT REPORTS.ID, REPORTS.DATE, REPORTS.TITLE, REPORTS.MAINDESCRIPTION, REPORTS.STATE,
GROUP_CONCAT(REPORT_DETAILS.ID,'|') AS MSGID,
GROUP_CONCAT(REPORT_DETAILS.DATE,'|') AS MSGDATE,
GROUP_CONCAT(REPORT_DETAILS.DESCRIPTION,'|') AS MSGDESCRIPTION
FROM REPORTS
LEFT JOIN REPORT_DETAILS ON REPORT_ID = REPORTS.ID
GROUP BY REPORTS.ID
ORDER BY REPORTS.DATE DESC
LIMIT 5";
$data=self::$db->query($sql);
return $data;
}
/** Get all DB entries */
function get_all_reports()
{
$sql="SELECT REPORTS.ID, REPORTS.DATE, REPORTS.TITLE, REPORTS.MAINDESCRIPTION, REPORTS.STATE,
GROUP_CONCAT(REPORT_DETAILS.ID,'|') AS MSGID,
GROUP_CONCAT(REPORT_DETAILS.DATE,'|') AS MSGDATE,
GROUP_CONCAT(REPORT_DETAILS.DESCRIPTION,'|') AS MSGDESCRIPTION
FROM REPORTS
LEFT JOIN REPORT_DETAILS ON REPORT_ID = REPORTS.ID
GROUP BY REPORTS.ID
ORDER BY REPORTS.DATE DESC";
$data=self::$db->query($sql);
return $data;
}
/** Add report */
function add_report($data)
{
$sql = "INSERT INTO REPORTS(DATE,TITLE,MAINDESCRIPTION) values (?,?,?)";
$stmt = self::$db->prepare($sql);
return $stmt->execute(array($data['date'], $data['title'], $data['maindescription']));
}
/** Add report description */
function add_description_report($data)
{
$sql = "INSERT INTO REPORT_DETAILS(REPORT_ID,DATE,DESCRIPTION) values (?,?,?)";
$stmt = self::$db->prepare($sql);
return $stmt->execute(array($data['report_id'], $data['date'], $data['description']));
}
/** Get report by ID */
function get_report_by_id($id)
{
$sql="SELECT * FROM REPORTS WHERE ID=:id";
$stmt=self::$db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
}
/** Remove report by ID */
function delete_report_by_id($id)
{
$sql="DELETE FROM REPORTS WHERE REPORTS.ID=:id";
$stmt=self::$db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
return $stmt->execute();
}
/** Remove report description by ID */
function delete_description_by_id($id)
{
$sql="DELETE FROM REPORT_DETAILS WHERE ID=:id";
$stmt=self::$db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
return $stmt->execute();
}
/** Update report by ID */
function update($id, $date, $title, $maindescription, $state)
{
$sql = "UPDATE `REPORTS`
SET `DATE` = :date,
`TITLE` = :title,
`MAINDESCRIPTION` = :maindescription,
`STATE` = :state
WHERE `REPORTS`.`ID` = :id";
$stmt = self::$db->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->bindParam(':date', $date);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':maindescription', $maindescription);
$stmt->bindParam(':state', $state);
return $stmt->execute();
}
}
<?php
/**
*
* Class to manage reports
*/
class Reports
{
/**
*
* DB connection
*/
private static $db;
/**
*
* DB connection contructor
*/
function __construct()
{
$dsn = 'sqlite:db/reports.sqlite3';
try{
self::$db=new PDO($dsn);
// We can now log any exceptions on Fatal error.
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Disable emulation of prepared statements, use REAL prepared statements instead.
self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$db->exec('PRAGMA foreign_keys = ON;');
}
catch(PDOException $e){
printf("Échec de la connexion : %s\n", $e->getMessage());
$this->db = null;
}
}
/**
*
* Get last 5 entries
*/
function Get_public()
{
$sql="SELECT REPORTS.DATE, REPORTS.TITLE, REPORTS.MAINDESCRIPTION, REPORTS.STATE,
GROUP_CONCAT(REPORT_DETAILS.DATE,'|') AS MSGDATE,
GROUP_CONCAT(REPORT_DETAILS.DESCRIPTION,'|') AS MSGDESCRIPTION
FROM REPORTS
LEFT JOIN REPORT_DETAILS ON REPORT_ID = REPORTS.ID
GROUP BY REPORTS.ID
ORDER BY REPORTS.DATE DESC
LIMIT 5";
$data=self::$db->query($sql);
return $data;
}
/**
*
* Get last 10 entries
*/
function Get_rss()
{
$sql="SELECT REPORTS.DATE, REPORTS.TITLE, REPORTS.MAINDESCRIPTION, REPORTS.STATE,
GROUP_CONCAT(REPORT_DETAILS.DATE,'|') AS MSGDATE,
GROUP_CONCAT(REPORT_DETAILS.DESCRIPTION,'|') AS MSGDESCRIPTION
FROM REPORTS
LEFT JOIN REPORT_DETAILS ON REPORT_ID = REPORTS.ID
GROUP BY REPORTS.ID
ORDER BY REPORTS.DATE DESC
LIMIT 10";
$data=self::$db->query($sql);
return $data;
}
/**
*
* Get last 5 entries
*/
function Get_last_reports()
{
$sql="SELECT REPORTS.ID, REPORTS.DATE, REPORTS.TITLE, REPORTS.MAINDESCRIPTION, REPORTS.STATE,
GROUP_CONCAT(REPORT_DETAILS.ID,'|') AS MSGID,
GROUP_CONCAT(REPORT_DETAILS.DATE,'|') AS MSGDATE,
GROUP_CONCAT(REPORT_DETAILS.DESCRIPTION,'|') AS MSGDESCRIPTION
FROM REPORTS
LEFT JOIN REPORT_DETAILS ON REPORT_ID = REPORTS.ID
GROUP BY REPORTS.ID
ORDER BY REPORTS.DATE DESC
LIMIT 5";
$data=self::$db->query($sql);
return $data;
}
/**
*
* Get all DB entries
*/
function Get_all_reports()
{
$sql="SELECT REPORTS.ID, REPORTS.DATE, REPORTS.TITLE, REPORTS.MAINDESCRIPTION, REPORTS.STATE,
GROUP_CONCAT(REPORT_DETAILS.ID,'|') AS MSGID,
GROUP_CONCAT(REPORT_DETAILS.DATE,'|') AS MSGDATE,
GROUP_CONCAT(REPORT_DETAILS.DESCRIPTION,'|') AS MSGDESCRIPTION
FROM REPORTS
LEFT JOIN REPORT_DETAILS ON REPORT_ID = REPORTS.ID
GROUP BY REPORTS.ID
ORDER BY REPORTS.DATE DESC";
$data=self::$db->query($sql);
return $data;
}
/**
*
* Add report
*/
function Add_report($data)
{
$sql = "INSERT INTO REPORTS(DATE,TITLE,MAINDESCRIPTION) values (?,?,?)";
$stmt = self::$db->prepare($sql);
return $stmt->execute(array($data['date'], $data['title'], $data['maindescription']));
}
/**
*
* Add report description
*/
function Add_description_report($data)
{
$sql = "INSERT INTO REPORT_DETAILS(REPORT_ID,DATE,DESCRIPTION) values (?,?,?)";
$stmt = self::$db->prepare($sql);
return $stmt->execute(array($data['report_id'], $data['date'], $data['description']));
}
/**
*
* Get report by ID
*/
function Get_report_by_id($id)
{
$sql="SELECT * FROM REPORTS WHERE ID=:id";
$stmt=self::$db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
}
/**
*
* Remove report by ID
*/
function Delete_report_by_id($id)
{
$sql="DELETE FROM REPORTS WHERE REPORTS.ID=:id";
$stmt=self::$db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
return $stmt->execute();
}
/**
*
* Remove report description by ID
*/
function Delete_description_by_id($id)
{
$sql="DELETE FROM REPORT_DETAILS WHERE ID=:id";
$stmt=self::$db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
return $stmt->execute();
}
/**
*
* Update report by ID
*/
function Update($id, $date, $title, $maindescription, $state)
{
$sql = "UPDATE `REPORTS`
SET `DATE` = :date,
`TITLE` = :title,
`MAINDESCRIPTION` = :maindescription,
`STATE` = :state
WHERE `REPORTS`.`ID` = :id";
$stmt = self::$db->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->bindParam(':date', $date);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':maindescription', $maindescription);
$stmt->bindParam(':state', $state);
return $stmt->execute();
}
}

Loading…
Cancel
Save