expmail/Request.class.php
Klaus-Uwe Mitterer 2eed78b83e Complete refactor: Moving stuff to classes, making sender tiny
Adding two config options to modify behavior
Allow messages with empty body to be sent
Automatically convert HTML to plain if none provided
Version bump to 0.4
2020-09-04 22:01:34 +02:00

165 lines
No EOL
4.8 KiB
PHP

<?php
require_once("config.php");
require_once("File.class.php");
class Request {
public $json;
function __construct($data, $authorize=True) {
if (!$json=json_decode($data, true)) {
throw new Exception("There is nothing here.");
};
$this->json = array_change_key_case($json, CASE_LOWER);
if ($authorize) {
if (!$this->authorize()) {
throw new Exception("Who are you?");
}
}
}
function authorize() {
global $API_KEYS;
return in_array($this->get_key(), $API_KEYS);
}
function get_attachments($resolve=true, $content=true, $ignoredlfails=false) {
$outachments = array();
foreach ($this->json["attachments"] as $attachment) {
$attachment["content"] = "";
if ($resolve || $content) {
$file = new File($attachment["url"]);
if ($resolve && !$attachment["filename"]) {
$attachment["filename"] = $file->get_filename();
}
if ($content) {
try {
$attachment["content"] = $file->fetch_file();
} catch (\Exception $e) {
if (!$ignoredlfails) {
throw $e;
}
}
}
}
if ($attachment["content"] || !$content) {
array_push($outachments, $attachment);
}
}
return $outachments;
}
function get_bccs($default=true) {
global $ALWAYS_BCC;
$bccs = $this->json["bccs"] ?: array();
if ($default) {
foreach ($ALWAYS_BCC as $bcc) {
array_push($bccs, array("email"=>$bcc));
}
}
return $bccs;
}
function get_ccs($default=true) {
global $ALWAYS_CC;
$ccs = $this->json["ccs"] ?: array();
if ($default) {
foreach ($ALWAYS_CC as $cc) {
array_push($ccs, array("email"=>$cc));
}
}
return $ccs;
}
function get_config() {
return $this->json["config"];
}
function get_html() {
if ($this->json["html"]) {
$html = $this->json["html"];
} else if ($this->json["htmlurl"]) {
try {
$htmlfile = new File($this->json["htmlurl"]);
$html = $htmlfile->fetch_file();
} catch (\Exception $e) {
throw new Exception("Could not fetch URL for HTML message: " . $e->getMessage());
}
}
return $html;
}
function get_key() {
return $this->json["key"];
}
function get_placeholders() {
return $this->json["placeholders"];
}
function get_recipients($default=true) {
global $ALWAYS_TO;
$recipients = $this->json["recipients"] ?: array();
if ($default) {
foreach ($ALWAYS_TO as $recipient) {
array_push($recipients, array("email"=>$recipient));
}
}
return $recipients;
}
function get_sender($fallback=true) {
global $MAIL_FROM_MAIL, $MAIL_FROM_NAME, $MAIL_USER;
if ($this->json["sender"]) {
return $this->json["sender"];
} else if ($fallback) {
return array(
"email" => $MAIL_FROM_MAIL ? $MAIL_FROM_MAIL : $MAIL_USER,
"name" => $MAIL_FROM_NAME
);
}
}
function get_text($conversion=true) {
if ($this->json["text"]) {
$text = $this->json["text"];
} else if ($this->json["texturl"]) {
try {
$textfile = new File($this->json["texturl"]);
$text = $textfile->fetch_file();
} catch (\Exception $e) {
throw new Exception("Could not fetch URL for plain text message: " . $e->getMessage());
}
} else if ($conversion) {
if ($html=$this->get_html()) {
$text = html_entity_decode(
trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/si', '', $html))),
ENT_QUOTES
);
}
}
return $text;
}
function has_config($key) {
return in_array($key, $this->get_config());
}
function prepare_html() {
return $this->prepare_string($this->get_html());
}
function prepare_string($string) {
foreach ($this->get_placeholders() as $placeholder) $string = str_replace("{".strtoupper($placeholder["name"])."}", $placeholder["value"], $string);
return $string;
}
function prepare_text($conversion=true) {
return $this->prepare_string($this->get_text($conversion));
}
}