1<?php
2	/* libraries/nma.php
3	 *
4	 * Copyright (C) by Hugo Leisink <hugo@leisink.net>
5	 * This file is part of the Banshee PHP framework
6	 * http://www.banshee-php.org/
7	 */
8
9	class NMA extends HTTPS {
10		private $server = "www.notifymyandroid.com";
11		private $application = null;
12		private $api_keys = null;
13		private $developer_key = null;
14
15		/* Constructor
16		 *
17		 * INPUT:  string application, array/string API keys[, string developer key]
18		 * OUTPUT: -
19		 * ERROR:  -
20		 */
21		public function __construct($application, $api_keys, $developer_key = null) {
22			$this->application = $this->truncate($application, 256);
23			if (is_array($api_keys) == false) {
24				$this->api_keys = array($api_keys);
25			} else {
26				$this->api_keys = $api_keys;
27			}
28			$this->developer_key = $developer_key;
29
30			parent::__construct($this->server);
31		}
32
33		/* Truncate text
34		 *
35		 * INPUT:  string text, integer maximum text length
36		 * OUTPUT: string truncated text
37		 * ERROR:  -
38		 */
39		private function truncate($text, $size) {
40			if (strlen($text) > $size) {
41				$text = substr($text, 0, $size - 3)."...";
42			}
43
44			return $text;
45		}
46
47		/* Send push notification
48		 *
49		 * INPUT:  string event, string description[, int priority[, string url[, string content type]]]
50		 * OUTPUT: true sending successful
51		 * ERROR:  false sending failed
52		 */
53		public function send_notification($event, $description, $priority = 0, $url = null, $content_type = null) {
54			if ((is_int($priority) == false) || ($priority < -2) || ($priority > 2)) {
55				return false;
56			}
57
58			$data = array(
59				"apikey"      => implode(",", $this->api_keys),
60				"application" => $this->application,
61				"event"       => $this->truncate($event, 1000),
62				"description" => $this->truncate($description, 10000));
63
64			if ($priority != 0) {
65				$data["priority"] = $priority;
66			}
67
68			if ($url !== null) {
69				$data["url"] = $this->truncate($url, 512);
70			}
71
72			if ($this->developer_key !== null) {
73				$data["developerkey"] = $this->developer_key;
74			}
75
76			if ($content_type !== null) {
77				$data["content-type"] = $content_type;
78			}
79
80			if (($result = $this->POST("/publicapi/notify", $data)) === false) {
81				return false;
82			}
83
84			return $result["status"];
85		}
86
87		/* Verify API key
88		 *
89		 * INPUT:  string API key
90		 * OUTPUT: bool valid key
91		 * ERROR:  null validation error
92		 */
93		public function valid_key($api_key) {
94			$params = "apikey=".$api_key;
95
96			if ($this->developer_key !== null) {
97				$params .= "&developerkey=".$this->developer_key;
98			}
99
100			if (($result = $this->GET("/publicapi/verify?".$params)) === false) {
101				return false;
102			}
103
104			return $result["status"];
105		}
106	}
107?>
108