187 lines
No EOL
5.9 KiB
PHP
187 lines
No EOL
5.9 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($urlbeforestring=false) {
|
|
if ($this->json["html"] && $this->json["htmlurl"]) {
|
|
if ($urlbeforestring) {
|
|
trigger_error("Both `html` and `htmlurl` provided - using `htmlurl` as `urlbeforestring` config key was also provided", E_USER_WARNING);
|
|
} else {
|
|
trigger_error("Both `html` and `htmlurl` provided - using `html`", E_USER_WARNING);
|
|
}
|
|
}
|
|
if ($this->json["html"] && !$urlbeforestring) {
|
|
$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());
|
|
}
|
|
} else if ($this->json["html"]) {
|
|
$html = $this->json["html"];
|
|
}
|
|
|
|
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_subject() {
|
|
return $this->json["subject"];
|
|
}
|
|
|
|
function get_text($conversion=true, $urlbeforestring=false) {
|
|
if ($this->json["text"] && $this->json["texturl"]) {
|
|
if ($urlbeforestring) {
|
|
trigger_error("Both `text` and `texturl` provided - using `texturl` as `urlbeforestring` config key was also provided", E_USER_WARNING);
|
|
} else {
|
|
trigger_error("Both `text` and `texturl` provided - using `text`", E_USER_WARNING);
|
|
}
|
|
}
|
|
if ($this->json["text"] && !$urlbeforestring) {
|
|
$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 ($this->json["text"]) {
|
|
$text = $this->json["text"];
|
|
} 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($urlbeforestring=false) {
|
|
return $this->prepare_string($this->get_html($urlbeforestring));
|
|
}
|
|
|
|
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, $urlbeforestring=false) {
|
|
return $this->prepare_string($this->get_text($conversion, $urlbeforestring));
|
|
}
|
|
|
|
} |