ninjaMail_public/ninjamail.class.php

328 lines
5.7 KiB
PHP

<?php
class ninjaMail
{
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;
}
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;
}
public function login()
{
return $this->process('login', ['rkey' => md5(time().rand(0,65500).rand(0,10))]);
}
}
// Send Email
class ninjaMailSend extends ninjaMail
{
private $to;
private $subject;
private $message;
private $message_text;
public function to($to)
{
$this->to = $to;
return true;
}
public function subject($s)
{
$this->subject = $s;
return true;
}
public function message($m, $t = false)
{
$this->message = $m;
if (!$t) $this->message_text = trim(strip_tags($m));
else $this->message_text = $t;
return true;
}
public function send()
{
$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;
}
}
// Subscribe
class ninjaMailSubscription extends ninjaMail
{
private $list;
private $activated = 0;
private $forcenamechange = 0;
public function list($id)
{
if (is_numeric($id))
{
$this->list = $id;
return true;
}
return false;
}
public function activated($s)
{
if ($s) $this->activated = 1;
else $this->activated = 0;
return true;
}
public function namechange($s)
{
if ($s) $this->forcenamechange = 1;
else $this->forcenamechange = 0;
return true;
}
public function subscribe($email, $name = '')
{
if (!$this->list || !is_numeric($this->activated))
return false;
$post = [
'list' => $this->list,
'name' => $name,
'email' => $email,
'activated' => $this->activated,
'forcenamechange' => $this->forcenamechange
];
return $this->process('subscribe', $post, true)->status == 'success' ? true : false;
}
public function unsubscribe($email)
{
if (!$this->list)
return false;
$post = [
'list' => $this->list,
'email' => $email
];
return $this->process('unsubscribe', $post, true)->status == 'success' ? true : false;
}
}
// 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)
{
$this->subject = $s;
return true;
}
public function message($m, $t = false)
{
$this->message = $m;
if (!$t) $this->message_text = trim(strip_tags($m));
else $this->message_text = $t;
return true;
}
public function query($update = false)
{
if (!$this->message)
return false;
$post = [
'new' => true,
'id' => $update ? $this->newsletter : false,
'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);
}
public function send($time = false)
{
if (!$this->newsletter)
return false;
$post = [
'send' => true,
'id' => $this->newsletter,
'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,
'id' => $this->campaign,
'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);
}
}