From 11fccdb710b33d012356fac52286d165a0dc6a61 Mon Sep 17 00:00:00 2001 From: Gabriele-V Date: Wed, 14 May 2014 23:42:14 +0200 Subject: [PATCH] Added function to send reports by mail --- API/main_stats_v1.php | 15 +------ Webservice/configuration.php | 9 +++- Webservice/mail_reports.php | 87 ++++++++++++++++++++++++++++++++++++ Webservice/send_mail.php | 33 ++++++++++++++ Webservice/utility.php | 38 ++++++++++++++++ 5 files changed, 167 insertions(+), 15 deletions(-) create mode 100644 Webservice/mail_reports.php create mode 100644 Webservice/utility.php diff --git a/API/main_stats_v1.php b/API/main_stats_v1.php index 8d24ae7..52e2e95 100644 --- a/API/main_stats_v1.php +++ b/API/main_stats_v1.php @@ -1,6 +1,7 @@ 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"]) @@ -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"]); diff --git a/Webservice/configuration.php b/Webservice/configuration.php index 4c405b6..eca7342 100644 --- a/Webservice/configuration.php +++ b/Webservice/configuration.php @@ -15,11 +15,16 @@ class Settings_Database class Settings_Mail { - public static $From = "noreply@example.com"; + public static $FromMail = "noreply@example.com"; + public static $FromName = "Example"; public static $ToAlert = "example@example.com"; } 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!"; } \ No newline at end of file diff --git a/Webservice/mail_reports.php b/Webservice/mail_reports.php new file mode 100644 index 0000000..790a0c2 --- /dev/null +++ b/Webservice/mail_reports.php @@ -0,0 +1,87 @@ + 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:"."
"; + foreach ($_GET as $Key => $Value) + { + $Message .= $Key." = '${Value}'"; + $Message .= "
"; + } + $Message .= "
"."
"."Server data:"."
"; + foreach ($_SERVER as $Key => $Value) + { + $Message .= $Key." = '${Value}'"; + $Message .= "
"; + } + + Send_Mail::OnError($Message); + } +} +else +{ + echo ("Required parameter missing"); +} +?> \ No newline at end of file diff --git a/Webservice/send_mail.php b/Webservice/send_mail.php index cc4a46c..eec36be 100644 --- a/Webservice/send_mail.php +++ b/Webservice/send_mail.php @@ -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 = "

Money Manager EX - Usage stats:

Report ${ReportName}

"; + $Body .= "Open it in Reportico HERE"; + $mail->Body = $Body; + + if(!$mail->send()) + { + echo "Mailer Error: " .$mail->ErrorInfo; + return false; + } + else + return true; } } ?> \ No newline at end of file diff --git a/Webservice/utility.php b/Webservice/utility.php new file mode 100644 index 0000000..b167408 --- /dev/null +++ b/Webservice/utility.php @@ -0,0 +1,38 @@ +geoplugin_countryName != null) + { + //$result["Country"] = $ip_data->geoplugin_countryName; + //$result["City"] = $ip_data->geoplugin_city; + $result = $ip_data->geoplugin_countryName; + } + return $result; + } +} +?> \ No newline at end of file