notification.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace phpnotify;
  3. require_once('fetch.php');
  4. class notification {
  5. private array $tags;
  6. private array $actions;
  7. private fetch $request;
  8. public function __construct(string $location, string $content) {
  9. $this->tags = array();
  10. $this->actions = array();
  11. $this->request = new fetch($location);
  12. $this->request->set_method('POST');
  13. $this->request->send_raw($content, 'text/plain');
  14. }
  15. public function set_title(string $title): object {
  16. $this->request->add_header('Title', $title);
  17. return $this;
  18. }
  19. public function set_priority(int $priority): object {
  20. if ($priority < 1 || $priority > 5) {
  21. throw new TypeError('Priority must be between 1 and 5.');
  22. }
  23. $this->request->add_header('Priority', strval($priority));
  24. return $this;
  25. }
  26. public function add_tag(string $tag): object {
  27. $tag = trim($tag);
  28. if (strpos($tag, ' ') !== false) {
  29. throw new TypeError('Tags could not contain white chars.');
  30. }
  31. if (strpos($tag, ',') !== false) {
  32. throw new TypeError('Tags could not contain ",".');
  33. }
  34. array_push($this->tags, $tag);
  35. return $this;
  36. }
  37. private function get_tags(): string {
  38. return join(',', $this->tags);
  39. }
  40. public function set_markdown(): object {
  41. $this->request->add_header('Markdown', 'yes');
  42. return $this;
  43. }
  44. public function delivery_time(string $when): object {
  45. $this->request->add_header('At', $when);
  46. return $this;
  47. }
  48. public function set_email(string $email): object {
  49. if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  50. throw new TypeError('Email "'.$email.'" is invalid.');
  51. }
  52. $this->request->add_header('Email', $email);
  53. return $this;
  54. }
  55. public function click_url(string $url): object {
  56. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  57. throw new TypeError('"'.$url.'" is not valid URL.');
  58. }
  59. $this->request->add_header('Action', $url);
  60. return $this;
  61. }
  62. public function attach_url(string $url): object {
  63. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  64. throw new TypeError('"'.$url.'" is not valid URL.');
  65. }
  66. $this->request->add_header('Attach', $url);
  67. return $this;
  68. }
  69. public function icon_url(string $url): object {
  70. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  71. throw new TypeError('"'.$url.'" is not valid URL.');
  72. }
  73. $this->request->add_header('Icon', $url);
  74. return $this;
  75. }
  76. public function call(string|true $phone_number): object {
  77. if ($phone_number === true) {
  78. $this->request->add_header('Call', 'yes');
  79. return $this;
  80. }
  81. $check = str_replace(' ', '', $phone_number);
  82. if ($check[0] === '+') {
  83. $check = substr($check, 1);
  84. }
  85. if (!is_numeric($phone_number)) {
  86. throw new TypeError('"'.$phone_number.'" is invalid number.');
  87. }
  88. $this->request->add_header('Call', $phone_number);
  89. return $this;
  90. }
  91. public function save_as(string $filename): object {
  92. if (basename($filename) !== $filename) {
  93. throw new TypeError('"'.$filename.'" is invalid filename.');
  94. }
  95. $this->request->add_header('Filename', $filename);
  96. return $this;
  97. }
  98. public function login(string $nick, string $password): object {
  99. $decoded = $nick.':'.$password;
  100. $encoded = base64_encode($decoded);
  101. $param = 'Basic '.$encoded;
  102. $this->request->add_header('Authorization', $param);
  103. return $this;
  104. }
  105. public function token(string $token): object {
  106. $token = trim($token);
  107. $param = 'Bearer '.$token;
  108. $this->request->add_header('Authorization', $param);
  109. return $this;
  110. }
  111. public function disable_firebase(): object {
  112. $this->request->add_header('Firebase', 'no');
  113. return $this;
  114. }
  115. public function disable_cache(): object {
  116. $this->request->add_header('Cache', 'no');
  117. return $this;
  118. }
  119. public function set_poll_id(string $id): object {
  120. $this->request->add_header('Poll-ID', $id);
  121. return $this;
  122. }
  123. public function enable_unified_push(): object {
  124. $this->request->add_header('UnifiedPush', 'yes');
  125. return $this;
  126. }
  127. public function add_action(
  128. string $action,
  129. string $label,
  130. ?array $param = null
  131. ): object {
  132. $separator = ', ';
  133. $content = $action.$separator.$label;
  134. if ($param === null) {
  135. array_push($this->actions, $content);
  136. return $this;
  137. }
  138. foreach ($param as $key => $value) {
  139. $content .= $separator;
  140. $content .= $key.'='.$value;
  141. }
  142. array_push($this->actions, $content);
  143. return $this;
  144. }
  145. private function get_actions(): string {
  146. return join('; ', $this->actions);
  147. }
  148. public function send(bool $throws = false): bool {
  149. $this->request->set_header('Actions', $this->get_actions());
  150. $this->request->set_header('Tags', $this->get_tags());
  151. try {
  152. $result = $this->request->request()->receive_array();
  153. if (array_key_exists('id', $result)) {
  154. return true;
  155. }
  156. if (count($result) === 0) {
  157. throw new RuntimeException('Can not fetch. General error.');
  158. }
  159. if (array_key_exists('error', $result)) {
  160. $error = (
  161. 'Can not publish: "'
  162. .$result['error']
  163. .'"'
  164. );
  165. throw new RuntimeException($error);
  166. }
  167. } catch (Exception $error) {
  168. if ($throws) {
  169. throw $error;
  170. }
  171. return false;
  172. }
  173. }
  174. }