emailtracking/php/lib/class.EmailTracker.php

121 lines
3.1 KiB
PHP
Raw Normal View History

2015-04-13 18:11:52 +02:00
<?php
require __DIR__ . '/medoo.php';
require_once __DIR__ . '/../../config.php';
class EmailTracker {
/**
* @var Database connector object. A medoo object.
*/
private $dbObj;
/**
* Contructor
*/
function __construct() {
$dbOptions = array(
'database_type' => 'mysql',
'database_name' => PLUGIN_EMAILTRACKING_DB_DB,
'server' => PLUGIN_EMAILTRACKING_DB_HOST,
'username' => PLUGIN_EMAILTRACKING_DB_USER,
'password' => PLUGIN_EMAILTRACKING_DB_PASS,
'port' => PLUGIN_EMAILTRACKING_DB_PORT,
'charset' => 'utf8',
);
$this->dbObj = new medoo($dbOptions);
}
/**
* Generates a new ID out of some values
*
* @param $emailId
* @param $destAddr
* @param $srcAddr
* @param $subject
* @return string
*/
public function getNewTrackingCode ($emailId, $destAddr, $srcAddr, $subject) {
$currentTimeStamp = time();
$trackingHash = md5($emailId . $destAddr . $subject . $currentTimeStamp);
$data = array(
"email_id" => $emailId,
"destination_addr" => $destAddr,
"source_addr" => $srcAddr,
"subject" => $subject,
"current_time" => date('Y-m-d H:i:s',$currentTimeStamp),
"generated_id" => $trackingHash
);
$this->dbObj->insert("trackingid", $data);
return $trackingHash;
}
/**
* Get the database ID for the given hash
*
* @param $trackingHash
* @return int
*/
private function getTrackingIDbyCode ($trackingHash) {
$filter = array(
"generated_id" => $trackingHash,
);
$data = $this->dbObj->select("trackingid", "id", $filter);
if (is_array($data) && count($data) > 0) {
return $data[0];
} else {
return -1;
}
}
/**
* Stores a new log entry to db
*
* @param $trackingHash
* @param $data
*/
public function addLog($trackingHash, $data) {
// first get the id - to add it to our new entry
$dbID = $this->getTrackingIDbyCode($trackingHash);
// check if trackingHash was valid
if($dbID != -1) {
// build our tracking entry
$data["trackingid_id"] = $dbID;
$this->dbObj->insert("trackinglog", $data);
}
}
/**
* Get all Tracking logs for the specified hash.
*
* @param $trackingHash
* @return array|bool
*/
public function getAllLogs($trackingHash) {
// first get the id - to add it to our new entry
$dbID = $this->getTrackingIDbyCode($trackingHash);
// check if trackingHash was valid
if($dbID != -1) {
$data = $this->dbObj->select("trackinglog", [
"ip_addr",
"timestamp",
"referrer",
"client",
"tracking_type"
], [
"trackingid_id" => $dbID
]);
return $data;
}
return false;
}
}