From 3ab239f6b58143ff6764e3345abca4e7bf467084 Mon Sep 17 00:00:00 2001 From: sHa Date: Mon, 12 Feb 2018 02:56:07 +0200 Subject: [PATCH] vomposer rebase --- .gitignore | 1 + lib/lng/en.php | 22 ++++++ lib/lng/ru.php | 22 ++++++ lib/lng/ua.php | 22 ++++++ lib/sLetter.php | 190 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 257 insertions(+) create mode 100644 .gitignore create mode 100644 lib/lng/en.php create mode 100644 lib/lng/ru.php create mode 100644 lib/lng/ua.php create mode 100644 lib/sLetter.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/lib/lng/en.php b/lib/lng/en.php new file mode 100644 index 0000000..aba7a84 --- /dev/null +++ b/lib/lng/en.php @@ -0,0 +1,22 @@ + + * @package sLetter + * @version 18.2.12 + * + */ + +return [ + 'subject' => '📨 Letter from site', + 'name' => 'From', + 'phone' => 'Phone', + 'email' => 'Email', + 'referer' => 'Referer', + 'senderIP' => 'Sender IP', + 'flag' => 'Flag', + 'country' => 'Country', + 'region' => 'Region', + 'city' => 'City', + 'provider' => 'Internet Provider', +]; diff --git a/lib/lng/ru.php b/lib/lng/ru.php new file mode 100644 index 0000000..bc41311 --- /dev/null +++ b/lib/lng/ru.php @@ -0,0 +1,22 @@ + + * @package sLetter + * @version 18.2.12 + * + */ + +return [ + 'subject' => '📨 Сообщение с сайта', + 'name' => 'От кого', + 'phone' => 'Телефон', + 'email' => 'Email', + 'referer' => 'Реферал', + 'senderIP' => 'IP-адрес отправителя', + 'flag' => 'Флаг', + 'country' => 'Страна', + 'region' => 'Регион', + 'city' => 'Населенный пункт', + 'provider' => 'Провайдер', +]; diff --git a/lib/lng/ua.php b/lib/lng/ua.php new file mode 100644 index 0000000..f123866 --- /dev/null +++ b/lib/lng/ua.php @@ -0,0 +1,22 @@ + + * @package sLetter + * @version 18.2.12 + * + */ + +return [ + 'subject' => '📨 Повідомлення з сайту', + 'name' => 'Від кого', + 'phone' => 'Телефон', + 'email' => 'Email', + 'referer' => 'Реферал', + 'senderIP' => 'IP-адреса відправника', + 'flag' => 'Прапор', + 'country' => 'Країна', + 'region' => 'Регіон', + 'city' => 'Населенний пункт', + 'provider' => 'Провайдер', +]; diff --git a/lib/sLetter.php b/lib/sLetter.php new file mode 100644 index 0000000..7855055 --- /dev/null +++ b/lib/sLetter.php @@ -0,0 +1,190 @@ + + * @package shadoll/sLetter + * @version 18.2.12 + * + */ + +namespace shadoll; + +use Mailgun\Mailgun; + +class sLetter{ + + public $error = false; + public $status = ""; + + private $sender = "mail"; + private $header = ""; + private $message = ""; + private $subject = ""; + + private $language = "ua"; + private $lang = []; + + private $fromMail = ""; + private $fromName = ""; + private $toMail = ""; + private $senderIP = null; + + public $fields = null; + private $logoUri = ""; + + function __construct(){ + $this->setLang(require_once(__DIR__."/lng/".$this->language.".php")); + } + + function setData($data,$return=false){ + if(empty($data) || !is_array($data)) + return $return?$this->fields:$this; + + if(is_null($this->fields)) + $this->fields = []; + + foreach($data as $key=>$val) + if(!empty($val)) + $this->fields[$key] = is_string($val)?trim(stripslashes($val)):$val; + + return $return?$this->fields:$this; + } + + function setLang($data,$return=false){ + if(empty($data) || !is_array($data)) + return $return?$this->lang:$this; + + if(is_null($this->lang)) + $this->lang = []; + + foreach($data as $key=>$val) + if(!empty($val)) + $this->lang[$key] = is_string($val)?trim(stripslashes($val)):$val; + + return $return?$this->lang:$this; + } + + function set($data){ + if(!empty($data) && is_array($data)) + foreach($data as $key=>$val) + if(!empty($key) && !empty($val)) + $this->{$key} = is_string($val)?trim(stripslashes($val)):$val; + return $this; + } + + function get($val){ + if(property_exists($this,$val)) + return $this->{$val}; + return null; + } + + function header($return=false){ + $this->header .= "From:".(!empty($this->fromName)?($this->fromName."<".strip_tags($this->fromMail).">"):strip_tags($this->fromMail))."\r\n"; + $this->header .= "Reply-To: ".strip_tags($this->fromMail)."\r\n"; + $this->header .= "MIME-Version: 1.0\r\n"; + $this->header .= "Content-Type: text/html;charset=utf-8 \r\n"; + + return $return?$this->header:$this; + } + + function message($return=false){ + $this->message .= ""; + + if(!empty($this->logoUri)) + $this->message .= "logo"; + + $this->message .= "

".$this->subject."

\r\n"; + + foreach($this->fields as $name=>$val){ + if(!empty($val)){ + $this->message .= "

".(array_key_exists($name,$this->lang)?$this->lang[$name]:$name).": ".$val."

\r\n"; + } + } + + $this->message .= ""; + + return $return?$this->message:$this; + } + + function detect($return=false){ + if(!empty($this->senderIP)){ + $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$this->senderIP)); + if($query && $query['status'] == 'success'){ + + + $this->setData([ + 'senderIP' => $this->senderIP, + 'flag' => "", + 'country' => $query['country'], + 'region' => $query['regionName'], + 'city' => $query['city'], + 'provider' => $query['isp'], + ]); + } + } + return $return?$this->fields:$this; + } + + function validate($data,$return=false){ + if(!empty($data) && is_array($data)) + foreach($data as $val) + if(empty($val)){ + $this->error = true; + $this->status = "Empty value"; + } + + return $return?$this->error:$this; + } + + function subject($return=false){ + if(empty($this->subject)) + $this->subject = (array_key_exists("subject",$this->lang)?$this->lang["subject"]:"Message from")." ".$_SERVER['SERVER_NAME']; + + return $return?$this->subject:$this; + } + + function sendMail($return=false){ + $this->header(); + + $status = @mail($this->toMail, $this->subject(true), $this->message, $this->header); + + + return $return?$status:$this; + } + + function sendMailgun($return=false){ + $mg = new Mailgun($this->mailgun_apikey); + + $status = $mg->sendMessage($this->mailgun_domain, array( + 'from' => !empty($this->fromName)?($this->fromName."<".strip_tags($this->fromMail).">"):strip_tags($this->fromMail), + 'to' => $this->toMail, + 'subject' => $this->subject(true), + 'html' => $this->message, + )); + + return $return?$status:$this; + } + + function send($return=false){ + if($this->error===false){ + if(empty($this->message)) + $this->message(); + + if(!empty($this->sender)){ + $method = "send".ucfirst($this->sender); + + if(method_exists($this,$method)) + $status = $this->{$method}(true); + + } + } + else + $status = ""; + + return $return?$status:$this; + } + + function state(){ + echo json_encode(['success'=>!$this->error,'message'=>$this->status,'answer'=>'']); + } +}