Skip to content

Commit

Permalink
Jetzt mit Logging!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
arne committed Feb 28, 2014
1 parent 3fdf27f commit bb3473c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions vexim/adminuserchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class="textfield">
?>
<tr>
<td colspan="2" class="button">
<input type="hidden" name="localpart" value="<?php echo $row['localpart']; ?>" />
<input name="submit" type="submit" value="Submit">
</td>
</tr>
Expand Down
48 changes: 48 additions & 0 deletions vexim/config/Logging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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
*/
class Logging {
// declare log file and file pointer as private properties
private $log_file, $fp;
// set log file (path and name)
public function lfile($path) {
$this->log_file = $path;
}
// write message to the log file
public function lwrite($message) {
// if file pointer doesn't exist, then open log file
if (!is_resource($this->fp)) {
$this->lopen();
}
// define script name
$script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
// define current time and suppress E_WARNING if using the system TZ settings
// (don't forget to set the INI setting date.timezone)
$time = @date('[d/M/Y:H:i:s]');
$address = $_SERVER['REMOTE_ADDR'];
$user = $_SESSION['localpart'];
// write current time, script name and message to the log file
fwrite($this->fp, "$time ($address($user) -> $script_name) $message\r\n");
}
// close log file (it's always a good idea to close a file when you're done with it)
public function lclose() {
fclose($this->fp);
}
// open log file (private method)
private function lopen() {
$log_file_default = '/var/log/vexim.log';
// define log file from lfile method or use previously set default
$lfile = $this->log_file ? $this->log_file : $log_file_default;
// open log file for writing only and place file pointer at the end of the file
// (if the file does not exist, try to create it)
$this->fp = fopen($lfile, 'a') or exit("Can't open $lfile!");
}
}
?>
82 changes: 82 additions & 0 deletions vexim/config/db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
class Database extends PDOStatement {
protected $_debugValues = null;

protected function __construct() {
// need this empty construct()!
}

public function execute($values=array()) {
global $log;
$this->_debugValues = $values;
try {
$t = parent::execute($values);
$raw = $this->replaceQuery();
$method = substr($raw, 0, 6);
if (strtolower($method) == 'insert' || strtolower($method) == 'update' || strtolower($method) == 'delete') {
$log->lwrite(preg_replace( "/\r|\n/", "", preg_replace('/\s+/', ' ',$raw)));
$log->lclose();
}
} catch (PDOException $e) {
// maybe do some logging here?
throw $e;
}

return $t;
}

public function _debugQuery($replaced=true) {
$q = $this->queryString;
if (!$replaced) {
return $q;
}

return preg_replace_callback('/:([0-9a-z_]+)/i', array($this, '_debugReplace'), $q);
}

protected function replaceQuery() {
$q = $this->queryString;
$keys = array_keys($this->_debugValues);
foreach ($keys as $key) {
$q = str_replace($key, $this->_debugValues[$key], $q);
}
return $q;
}

protected function _debugReplace($m) {
$v = $this->_debugValues[$m[1]];
if ($v === null) {
return "NULL";
}
if (!is_numeric($v)) {
$v = str_replace("'", "''", $v);
}

return "'". $v ."'";
}
}
/*
// have a look at http://www.php.net/manual/en/pdo.constants.php
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_STATEMENT_CLASS => array('MyPDOStatement', array()),
);
// create PDO with custom PDOStatement class
$pdo = new PDO($dsn, $username, $password, $options);
// prepare a query
$query = $pdo->prepare("INSERT INTO mytable (column1, column2, column3)
VALUES (:col1, :col2, :col3)");
// execute the prepared statement
$query->execute(array(
'col1' => "hello world",
'col2' => 47.11,
'col3' => null,
));
// output the query and the query with values inserted
var_dump( $query->queryString, $query->_debugQuery() );*/

?>
5 changes: 4 additions & 1 deletion vexim/config/variables.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
/* SQL Database login information */
include_once dirname(__FILE__) . "/i18n.php";
include_once dirname(__FILE__) . "/db.php";
include_once dirname(__FILE__) . "/Logging.php";

$sqlserver = "localhost";
$sqltype = "mysql";
Expand All @@ -9,7 +11,8 @@
$sqlpass = "yu0ZbzTkg7";

$dsn = "$sqltype:host=$sqlserver;dbname=$sqldb";
$dboptions = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
$log = new Logging();
$dboptions = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8', PDO::ATTR_STATEMENT_CLASS => array('Database', array()));

try {
$dbh = new PDO($dsn, $sqluser, $sqlpass, $dboptions);
Expand Down
3 changes: 3 additions & 0 deletions vexim/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@
</body>
</html>
<!-- Layout and CSS tricks obtained from http://www.bluerobot.com/web/layouts/ -->
<?php
$log->lclose();
?>

0 comments on commit bb3473c

Please sign in to comment.