Commit b029b2d8 authored by dmorley's avatar dmorley

add some basic logging to add.php

parent 4c9aea1a
......@@ -2,27 +2,29 @@
<?php
$valid=0;
include('config.php');
include('../logging.php');
$log = new Logging();
$log->lfile($log_dir."/add.php.log");
if (!$_POST['url']){
echo "no url given";
echo "no url given";$log->lwrite('no url given '.$_POST['domain']);
die;
}
if (!$_POST['email']){
echo "no email given";
echo "no email given";$log->lwrite('no email given '.$_POST['domain']);
die;
}
if (!$_POST['domain']){
echo "no pod domain given";
echo "no pod domain given";$log->lwrite('no domain given '.$_POST['domain']);
die;
}
if (!$_POST['url']){
echo "no API key for your stats";
echo "no API key for your stats";$log->lwrite('no api given '.$_POST['domain']);
die;
}
if (strlen($_POST['url']) < 14){
echo "API key bad needs to be like m58978-80abdb799f6ccf15e3e3787ee";
echo "API key bad needs to be like m58978-80abdb799f6ccf15e3e3787ee";$log->lwrite('api key too short '.$_POST['domain']);
die;
}
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
......@@ -34,10 +36,10 @@ if (strlen($_POST['url']) < 14){
}
while ($row = pg_fetch_array($result)) {
if ($row["domain"] == $_POST['domain']) {
echo "domain already exists";die;
echo "domain already exists";$log->lwrite('domain already exists '.$_POST['domain']);die;
}
if ($row["pingdomurl"] == $_POST['url']) {
echo "API key already exists";die;
echo "API key already exists";$log->lwrite('API key already exists '.$_POST['domain']);die;
}
}
......@@ -64,16 +66,16 @@ echo "API key already exists";die;
curl_close($ch);
if (stristr($outputssl, 'nodeName')) {
echo "Your pod has ssl and is valid<br>";
echo "Your pod has ssl and is valid<br>";$log->lwrite('Your pod has ssl and is valid '.$_POST['domain']);
$valid=1;
}
if (stristr($output, 'nodeName')) {
echo "Your pod does not have ssl but is a valid pod<br>";
echo "Your pod does not have ssl but is a valid pod<br>";$log->lwrite('Your pod does not have ssl but is a valid pod '.$_POST['domain']);
$valid=1;
}
if ($valid=="1") {
$sql = "INSERT INTO pods (domain, pingdomurl, email) VALUES($1, $2, $3)";
$sql = "INSERT INTO pods (domain, pingdomurl, email) VALUES($1, $2, $3)";$log->lwrite($sql);
$result = pg_query_params($dbh, $sql, array($_POST['domain'], $_POST['url'], $_POST['email']));
if (!$result) {
die("Error in SQL query: " . pg_last_error());
......@@ -92,7 +94,7 @@ if ($valid=="1") {
pg_close($dbh);
} else {
echo "Could not validate your pod on http or https, check your setup!";
echo "Could not validate your pod on http or https, check your setup!<br>Take a look at <a href='https://".$_POST['domain']."/nodeinfo/1.0'>your /nodeinfo</a>";$log->lwrite('Could not validate your pod on http or https, check your setup! '.$_POST['domain']);
}
$log->lclose();
?>
......@@ -2,6 +2,8 @@
/* Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */
//backup directory
$backup_dir = "/var/www/podup/backup";
//log directory
$log_dir = "/var/www/podup/log";
//location of pg dump
$pg_dump_dir = "/usr/bin";
//db username
......
<?php
/**
* Logging class:
* - contains lfile, lwrite and lclose public methods
* - lfile sets path and name of log file
* - lwrite writes message to the log file (and implicitly opens log file)
* - lclose closes log file
* - first call of lwrite method will open log file implicitly
* - message is written with the following format: [d/M/Y:H:i:s] (script name) message
* - http://www.redips.net/php/write-to-log-file/
*/
class Logging {
private $log_file, $fp;
public function lfile($path) {
$this->log_file = $path;
}
public function lwrite($message) {
if (!is_resource($this->fp)) {
$this->lopen();
}
$script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
$time = @date('[d/M/Y:H:i:s]');
fwrite($this->fp, "$time ($script_name) $message" . PHP_EOL);
}
public function lclose() {
fclose($this->fp);
}
private function lopen() {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$log_file_default = 'c:/php/logfile.txt';
}
else {
$log_file_default = '/tmp/logfile.txt';
}
$lfile = $this->log_file ? $this->log_file : $log_file_default;
$this->fp = fopen($lfile, 'a') or exit("Can't open $lfile!");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment