ninjaMail_public/ninjamail.class.php

319 lines
5.9 KiB
PHP
Raw Normal View History

2019-04-05 20:59:23 +02:00
<?php
class ninjaMail
{
2021-01-31 21:10:13 +01:00
private $host;
private $key;
public $data;
public function __construct($host, $key = false)
{
$this->host = $host;
if ($key) $this->key = $key;
}
public function check()
{
if (!$this->host || !$this->key)
return false;
return true;
}
2019-04-05 20:59:23 +02:00
2021-01-31 21:10:13 +01:00
public function process($f, $data, $post_method = true)
{
$ch = curl_init($this->host.'/a/'.$f.'/?key='.$this->key);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$data = curl_exec($ch);
curl_close($ch);
$data = @(object)json_decode($data);
$this->data = $data;
return $data;
}
2019-04-05 20:59:23 +02:00
2021-01-31 21:10:13 +01:00
public function login()
{
return $this->process('login', ['rkey' => md5(time().rand(0,65500).rand(0,10))]);
}
2019-04-05 20:59:23 +02:00
}
2019-08-06 06:48:53 +02:00
2019-04-05 20:59:23 +02:00
// Send Email
class ninjaMailSend extends ninjaMail
{
private $to;
private $subject;
private $message;
private $message_text;
public function to($to)
{
2021-01-31 21:10:13 +01:00
$this->to = $to;
return true;
2019-04-05 20:59:23 +02:00
}
public function subject($s)
{
2021-01-31 21:10:13 +01:00
$this->subject = $s;
return true;
2019-04-05 20:59:23 +02:00
}
public function message($m, $t = false)
{
2021-01-31 21:10:13 +01:00
$this->message = $m;
if (!$t) $this->message_text = trim(strip_tags($m));
else $this->message_text = $t;
return true;
2019-04-05 20:59:23 +02:00
}
public function send()
{
2021-01-31 21:10:13 +01:00
$post = [
'to' => $this->to,
'subject' => $this->subject,
'message' => $this->message,
'message_text' => $this->message_text
];
return $this->process('send', $post, true)->status == 'message_queued' ? true : false;
2019-04-05 20:59:23 +02:00
}
}
2019-08-06 06:48:53 +02:00
2019-04-05 20:59:23 +02:00
// Subscribe
class ninjaMailSubscription extends ninjaMail
{
private $list;
private $activated = false;
public function list($id)
{
2021-01-31 21:10:13 +01:00
if (is_numeric($id))
{
$this->list = $id;
return true;
}
return false;
2019-04-05 20:59:23 +02:00
}
public function activated($s)
{
2021-01-31 21:10:13 +01:00
if ($s) $this->activated = 1;
else $this->activated = 0;
return true;
2019-04-05 20:59:23 +02:00
}
public function subscribe($email, $name = '')
{
2021-01-31 21:10:13 +01:00
if (!$this->list || !is_numeric($this->activated))
return false;
$post = [
'list' => $this->list,
'name' => $name,
'email' => $email,
'activated' => $this->activated
];
return $this->process('subscribe', $post, true)->status == 'success' ? true : false;
2019-04-05 20:59:23 +02:00
}
public function unsubscribe($email)
{
2021-01-31 21:10:13 +01:00
if (!$this->list)
return false;
$post = [
'list' => $this->list,
'email' => $email
];
return $this->process('unsubscribe', $post, true)->status == 'success' ? true : false;
2019-04-05 20:59:23 +02:00
}
}
2019-08-06 06:48:53 +02:00
// Newsletter
class ninjaMailNewsletter extends ninjaMail
{
private $newsletter;
private $subject;
private $message;
private $message_text;
public function newsletter($id = false)
{
if (is_numeric($id))
{
$this->newsletter = $id;
return true;
}
return $this->newsletter;
}
public function subject($s)
{
2021-01-31 21:10:13 +01:00
$this->subject = $s;
return true;
2019-08-06 06:48:53 +02:00
}
public function message($m, $t = false)
{
2021-01-31 21:10:13 +01:00
$this->message = $m;
if (!$t) $this->message_text = trim(strip_tags($m));
else $this->message_text = $t;
return true;
2019-08-06 06:48:53 +02:00
}
public function query($update = false)
{
if (!$this->message)
return false;
$post = [
2021-01-31 21:10:13 +01:00
'new' => true,
'id' => $update ? $this->newsletter : false,
2019-08-06 06:48:53 +02:00
'subject' => $this->subject,
'message' => $this->message,
'message_text' => $this->message_text
];
$data = $this->process('newsletter', $post, true);
if ($data->status == 'success')
{
$this->newsletter = $data->id;
return $data->id;
}
return false;
}
public function create()
{
return $this->query(false);
}
public function update()
{
if (!$this->newsletter)
return false;
return $this->query(true);
}
2019-04-05 20:59:23 +02:00
2019-08-06 06:48:53 +02:00
public function send($time = false)
{
if (!$this->newsletter)
return false;
$post = [
'send' => true,
2021-01-31 21:10:13 +01:00
'id' => $this->newsletter,
2019-08-06 06:48:53 +02:00
'start' => $time ? $time : 0
];
$data = $this->process('newsletter', $post, true);
if ($data->status == 'success' || $this->status == 'already_queued')
return true;
return false;
}
public function get()
{
return $this->process('newsletter', ['get' => true], true);
}
}
// Campaign
class ninjaMailCampaign extends ninjaMail
{
private $campaign;
public function campaign($id = false)
{
if (!$id)
return $this->campaign;
if (is_numeric($id))
{
$this->campaign = $id;
return true;
}
return false;
}
public function create($name)
{
$post = [
'new' => true,
'name' => $name
];
$data = $this->process('campaign', $post, true);
if ($data->status == 'success')
{
$this->campaign = $data->id;
return $data->id;
}
return false;
}
public function remove()
{
$post = [
'remove' => true,
'id' => $this->campaign
];
return $this->process('campaign', $post, true)->status == 'success' ? true : false;
}
public function update($lists)
{
if (!is_array($lists))
return false;
$post = [
'update' => true,
'id' => $this->campaign,
'lists' => $lists
];
return $this->process('campaign', $post, true)->status == 'success' ? true : false;
}
public function attach($newsletter)
{
if (!is_numeric($newsletter))
return false;
$post = [
'relations' => true,
2021-01-31 21:10:13 +01:00
'id' => $this->campaign,
2019-08-06 06:48:53 +02:00
'newsletter' => $newsletter
];
return $this->process('campaign', $post, true)->status == 'success' ? true : false;
}
}
// Statistics
class ninjaMailStatistics extends ninjaMail
{
public function get($id)
{
if (!is_numeric($id))
return false;
$post = [
'newsletter' => $id,
'type' => 1
];
return $this->process('statistics', $post, true);
}
}