Neues File system_check_sql

Wenn im Ordner system/sql .sql-Files vorhanden sind, werden diese Ausgeführt und das Ergebnis per Mail an den Administrator geschickt. Dies soll als Check für systemkritische Abfragen, Checks, Inkonsistenzen, etc. dienen
This commit is contained in:
Manfred Kindl
2018-04-27 16:00:12 +02:00
parent e58c8110ce
commit ecf17a180b
+149
View File
@@ -0,0 +1,149 @@
<?php
/* Copyright (C) 2015 FH Technikum-Wien
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Authors: Manfred kindl <manfred.kindl@technikum-wien.at>
*/
/*
* Dieses Skript durchläuft alle Dateien vom Typ .sql im Ordner system/sql.
* Wenn von diesen SQLs Datensätze retourniert werden oder Fehler auftreten, werden diese in ein Mail gepackt und an MAIL_ADMIN geschickt.
*/
require_once('../config/global.config.inc.php');
require_once('../config/vilesci.config.inc.php');
require_once('../include/functions.inc.php');
require_once('../include/benutzerberechtigung.class.php');
require_once('../include/mail.class.php');
// Datenbankverbindung herstellen
if (! $db = new basis_db ())
die ( 'Es konnte keine Verbindung zum Server aufgebaut werden.' );
$slq_import_path = DOC_ROOT.'system/sql';
$mailcontent = '';
$mailheader = '
<style type="text/css">
.error
{
color: red;
font-weight: bold;
}
.table
{
font-size: small;
cellpadding: 3px;
}
.table th
{
background: #DCE4EF;
border: 1px solid #FFF;
padding: 4px;
text-align: left;
}
.table td
{
background-color: #EEEEEE;
padding: 4px;
vertical-align: top;
}
</style>';
// Wenn das Script ueber die Kommandozeile aufgerufen wird, erfolgt keine Authentifizierung
if (php_sapi_name() != 'cli')
{
$uid = get_uid();
$rechte = new benutzerberechtigung();
$rechte->getBerechtigungen($uid);
if(!$rechte->isBerechtigt('admin'))
{
exit($rechte->errormsg);
}
}
$files = glob($slq_import_path.'/*.sql');
$filename = '';
$db_row = '';
if (($files = glob($slq_import_path.'/*.sql')) != false)
{
foreach ($files as $file)
{
$filename = (basename($file));
$sql = file_get_contents($file);
// Checken, ob letztes Zeichen ; ist, wenn nicht, hinzufügen
if (substr($sql, -1, 1) != ';')
$sql = $sql .= '; ';
// Wenn Fehler auftritt, diesen ins Mail schreiben
if (! $result = $db->db_query ($sql))
{
$mailcontent .= '<h3>Die Abfrage der Datei "'.$filename.'" hat folgenden Fehler geliefert:</h3>';
$mailcontent .= '<span class="error">'.$db->db_last_error () . '</span><br>';
continue;
}
// Wenn rows vom SQL zurückkommen, diese ins Mail schreiben
if ($db->db_num_rows($result) > 0)
{
$mailcontent .= '<h3>Die Abfrage der Datei "'.$filename.'" hat folgendes Ergebnis geliefert:</h3>';
// Wenn zu viele Datensätze retourniert werden, abbrechen und Meldung ausgeben
if ($db->db_num_rows($result) > 1000)
$mailcontent .= '<span class="error">ACHTUNG! Es wurden mehr als 1000 Datensätze zurückgegeben</span><br>';
$mailcontent .= '<table class="table"><thead><tr>';
foreach($db->db_fetch_object($result) AS $key => $value)
{
$mailcontent .= '<th>'.$key.'</th>';
continue;
}
$mailcontent .= '</tr></thead><tbody>';
$counter = 0; // Wenn mehr als 1000 Datensätze retourniert werden, abbrechen
while($row = $db->db_fetch_object($result))
{
if ($counter == 1000)
break;
$mailcontent .= '<tr>';
foreach ($row AS $column)
{
$mailcontent .= '<td>'.($column === 'f' ? 'false' : ($column === 't' ? 'true' : $column)).'</td>';
}
$mailcontent .= '</tr>';
$counter++;
}
$mailcontent .= '</tr></tbody></table>';
}
}
}
else
$mailcontent = '';
// Wenn Mailcontent nicht leer ist, Mail senden
if ($mailcontent != '')
{
$mailcontent = $mailheader.$mailcontent;
echo $mailcontent;
$mail = new mail(MAIL_ADMIN, 'no-reply', 'Fehler in System Check "addons/bewerbung/cronjobs/system_check.php"', 'Bitte sehen Sie sich die Nachricht in HTML Sicht an, um den Inhalt vollständig darzustellen.');
$mail->setHTMLContent($mailcontent);
$mail->send();
}
else
echo 'Es sind keine Fehler aufgetreten';