1<?php
2	/* libraries/prowl.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 prowl extends HTTPS {
10		private $server = "api.prowlapp.com";
11		private $application = null;
12		private $api_keys = null;
13		private $provider_key = null;
14
15		/* Constructor
16		 *
17		 * INPUT:  string application, array/string API keys[, string provider key]
18		 * OUTPUT: -
19		 * ERROR:  -
20		 */
21		public function __construct($application, $api_keys, $provider_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->provider_key = $provider_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]]
50		 * OUTPUT: true sending successful
51		 * ERROR:  false sending failed
52		 */
53		public function send_notification($event, $description, $priority = 0, $url = 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, 1024),
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->provider_key !== null) {
73				$data["provider_key"] = $this->provider_key;
74			}
75
76			if (($result = $this->POST("/publicapi/add", $data)) === false) {
77				return null;
78			} else if ($result["status"] != 200) {
79				return false;
80			}
81
82			return true;
83		}
84
85		/* Verify API key
86		 *
87		 * INPUT:  string API key
88		 * OUTPUT: bool valid key
89		 * ERROR:  null validation error
90		 */
91		public function valid_key($api_key) {
92			$params = "apikey=".$api_key;
93
94			if ($this->provider_key !== null) {
95				$params .= "&providerkey=".$this->provider_key;
96			}
97
98			if (($result = $this->GET("/publicapi/verify?".$params)) === false) {
99				return null;
100			} else if ($result["status"] != 200) {
101				return false;
102			}
103
104			return true;
105		}
106	}
107?>
108