forked from vexim/vexim2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
arne
committed
Feb 28, 2014
1 parent
3fdf27f
commit bb3473c
Showing
5 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() );*/ | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters