Skip to content

Commit

Permalink
Added function to send reports by mail
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriele-v committed May 14, 2014
1 parent 0d9b611 commit 11fccdb
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 15 deletions.
15 changes: 2 additions & 13 deletions API/main_stats_v1.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
require_once "../Webservice/db_functions.php";
require_once "../Webservice/send_mail.php";
require_once "../Webservice/utility.php";

function getIpAddress()
{
Expand All @@ -15,18 +16,6 @@ function getIpAddress()
return $_SERVER['REMOTE_ADDR'];
}

function getLocationInfoByIp()
{
$ip = getIpAddress();
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null){
//$result["Country"] = $ip_data->geoplugin_countryName;
//$result["City"] = $ip_data->geoplugin_city;
$result = $ip_data->geoplugin_countryName;
}
return $result;
}

if (
isset($_GET["User_ID"]) && isset($_GET["Version"])
&& isset($_GET["Platform"]) && isset($_GET["OperatingSystem"])
Expand All @@ -50,7 +39,7 @@ function getLocationInfoByIp()
if ($_GET["Country"] != "")
$Country = $_GET["Country"];
else
$Country = getLocationInfoByIp();
$Country = Localize::getLocationInfoByIp(getIpAddress());
$Resolution = $_GET["Resolution"];
$Start_Time = date ($_GET["Start_Time"]);
$End_Time = date ($_GET["End_Time"]);
Expand Down
9 changes: 7 additions & 2 deletions Webservice/configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ class Settings_Database

class Settings_Mail
{
public static $From = "[email protected]";
public static $FromMail = "[email protected]";
public static $FromName = "Example";
public static $ToAlert = "[email protected]";
}

class Settings_Report
{
public static $Base_URL = "http:\\example.com";
public static $BaseURL = "http://example.com/Reports/run.php";
public static $ProjectName = "Project";
public static $ProjectPassword = "Password";
public static $VariableToday = "!TODAY!";
public static $VariableAnd = "!AND!";
}
87 changes: 87 additions & 0 deletions Webservice/mail_reports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
require_once "../Webservice/send_mail.php";
require_once "../Webservice/utility.php";


if (isset($_GET["MailTo"]) && isset($_GET["ReportName"]) && isset($_GET["Filters"]))
{
if (!empty($_GET["MailTo"]) && !empty($_GET["ReportName"]))
{
//Assign variables
$MailTo = $_GET["MailTo"];
$ReportName = $_GET["ReportName"];
if (!empty($_GET["Filters"]))
$Filters = $_GET["Filters"];
else
$Filters = "";

//Format dates
$VarToday = Settings_Report::$VariableToday;
$VarAnd = Settings_Report::$VariableAnd;
while (substr_count($Filters, $VarToday) > 0)
{
$OccurencePos = strpos($Filters, $VarToday);
if ($OccurencePos > 0)
{
if (strpos($Filters,"$VarAnd",$OccurencePos) > 0)
$OccurenceEnd = strpos ($Filters,"$VarAnd",$OccurencePos);
else
$OccurenceEnd = strlen ($Filters);
$Operator = substr ($Filters, $OccurencePos + strlen($VarToday), 1);
$Days = substr ($Filters, $OccurencePos + 1 + strlen($VarToday), $OccurenceEnd - $OccurencePos - 1 - strlen($VarToday));
if ($Operator == "-")
$Date = date ("Y-m-d", strtotime ("-${Days} days"));
else
$Date = date ("Y-m-d");
$Filters = substr_replace ($Filters, $Date, $OccurencePos, $OccurenceEnd - $OccurencePos);
}
}

//Replace "&" sign
$Filters = str_replace($VarAnd,"&",$Filters);

//Create URL
$URL = Settings_Report::$BaseURL;
$URL .= "?execute_mode=EXECUTE&target_format=PDF";
$URL .= "&project=".Settings_Report::$ProjectName;
$URL .= "&project_password=".Settings_Report::$ProjectPassword;
$URL .= "&xmlin=${ReportName}.xml";
if ($Filters <> "")
$URL .= "&".$Filters;

$Time = str_replace(".","",microtime(true));
$FilePath = "../Temp/${ReportName}_${Time}.pdf";

if (ManageFile::DownloadFile($URL,$FilePath))
{
$URL = str_replace("&project_password=".Settings_Report::$ProjectPassword,"",$URL);
if (Send_Mail::ReportPDF($MailTo,$ReportName,$URL,$FilePath))
echo "Mail sent successfully";
if (file_exists($FilePath))
unlink ($FilePath);
}
}
else
{
echo ("Required parameter empty");
$Message = "Required parameter empty:"."<br>";
foreach ($_GET as $Key => $Value)
{
$Message .= $Key." = '${Value}'";
$Message .= "<br>";
}
$Message .= "<br>"."<br>"."Server data:"."<br>";
foreach ($_SERVER as $Key => $Value)
{
$Message .= $Key." = '${Value}'";
$Message .= "<br>";
}

Send_Mail::OnError($Message);
}
}
else
{
echo ("Required parameter missing");
}
?>
33 changes: 33 additions & 0 deletions Webservice/send_mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,40 @@ public function OnError ($ErrorMessage)
$mail->Body = $ErrorMessage;

if(!$mail->send())
{
echo "Mailer Error: " .$mail->ErrorInfo;
return false;
}
else
return true;
}

public function ReportPDF ($ToAddress, $ReportName, $URL, $FilePath)
{
$ReportName = str_replace("_"," ",$ReportName);

$mail = new PHPMailer;
$mail->From = Settings_Mail::$FromMail;
$mail->FromName = Settings_Mail::$FromName;
$mail->addAddress($ToAddress);

$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = $ReportName;
if (file_exists($FilePath))
$mail->addAttachment ($FilePath, "${ReportName}.pdf");

$Body = "<h2>Money Manager EX - Usage stats:</h2><b>Report ${ReportName}</b><br><br>";
$Body .= "Open it in Reportico <a href='${URL}'>HERE</a>";
$mail->Body = $Body;

if(!$mail->send())
{
echo "Mailer Error: " .$mail->ErrorInfo;
return false;
}
else
return true;
}
}
?>
38 changes: 38 additions & 0 deletions Webservice/utility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
class ManageFile
{
public function DownloadFile ($URL,$FilePath)
{
$Length = 5120;

$Handle = fopen($URL, "rb");
$Write = fopen($FilePath, "w");

while (!feof($Handle))
{
$Buffer = fread($Handle, $Length);
fwrite($Write, $Buffer);
}

fclose($Handle);
fclose($Write);

return file_exists ($FilePath);
}
}

class Localize
{
public function getLocationInfoByIp ($IpAddress)
{
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$IpAddress));
if($ip_data && $ip_data->geoplugin_countryName != null)
{
//$result["Country"] = $ip_data->geoplugin_countryName;
//$result["City"] = $ip_data->geoplugin_city;
$result = $ip_data->geoplugin_countryName;
}
return $result;
}
}
?>

0 comments on commit 11fccdb

Please sign in to comment.