/modules/growl/growl.gntp.php

https://gitlab.com/x33n/ampache · PHP · 107 lines · 67 code · 12 blank · 28 comment · 5 complexity · e33ed12dcc2aaefe25c5cf237350ace9 MD5 · raw file

  1. <?php
  2. /*
  3. Title: Growl GNTP
  4. URL: http://github.com/jamiebicknell/Growl-GNTP
  5. Author: Jamie Bicknell
  6. Twitter: @jamiebicknell
  7. */
  8. /*
  9. Copyright (c) 2012-2014 Jamie Bicknell
  10. This software is provided 'as-is', without any express or implied
  11. warranty. In no event will the authors be held liable for any damages
  12. arising from the use of this software.
  13. Permission is granted to anyone to use this software for any purpose,
  14. including commercial applications, and to alter it and redistribute it
  15. freely, subject to the following restrictions:
  16. 1. The origin of this software must not be misrepresented; you must not
  17. claim that you wrote the original software. If you use this software
  18. in a product, an acknowledgment in the product documentation would be
  19. appreciated but is not required.
  20. 2. Altered source versions must be plainly marked as such, and must not be
  21. misrepresented as being the original software.
  22. 3. This notice may not be removed or altered from any source
  23. distribution.
  24. */
  25. class Growl {
  26. private $port = 23053;
  27. private $time = 5;
  28. public function Growl($host, $pass) {
  29. $this->host = $host;
  30. $this->pass = $pass;
  31. $this->salt = md5(uniqid());
  32. $this->application = '';
  33. $this->notification = '';
  34. }
  35. public function createHash() {
  36. $pass_hex = bin2hex($this->pass);
  37. $salt_hex = bin2hex($this->salt);
  38. $pass_bytes = pack('H*',$pass_hex);
  39. $salt_bytes = pack('H*',$salt_hex);
  40. return strtoupper('md5:'.md5(md5($pass_bytes.$salt_bytes,true)).'.'.$salt_hex);
  41. }
  42. public function setApplication($application, $notification) {
  43. $this->application = $application;
  44. $this->notification = $notification;
  45. }
  46. public function registerApplication($icon = NULL) {
  47. $data = 'GNTP/1.0 REGISTER NONE ' . $this->createHash() . "\r\n";
  48. $data .= 'Application-Name: ' . $this->application . "\r\n";
  49. if($icon!=NULL) {
  50. $data .= 'Application-Icon: ' . $icon . "\r\n";
  51. }
  52. $data .= 'Notifications-Count: 1' . "\r\n\r\n";
  53. $data .= 'Notification-Name: ' . $this->notification . "\r\n";
  54. $data .= 'Notification-Enabled: True' . "\r\n";
  55. $data .= "\r\n\r\n";
  56. $data .= 'Origin-Software-Name: growl.gntp.php' . "\r\n";
  57. $data .= 'Origin-Software-Version: 1.0' . "\r\n";
  58. $this->send($data);
  59. }
  60. public function notify($title, $text = '', $icon = NULL, $url = NULL) {
  61. $data = 'GNTP/1.0 NOTIFY NONE ' . $this->createHash() . "\r\n";
  62. $data .= 'Application-Name: ' . $this->application . "\r\n";
  63. $data .= 'Notification-Name: ' . $this->notification . "\r\n";
  64. $data .= 'Notification-Title: ' . $title . "\r\n";
  65. $data .= 'Notification-Text: ' . $text . "\r\n";
  66. $data .= 'Notification-Sticky: False' . "\r\n";
  67. if($icon!=NULL) {
  68. $data .= 'Notification-Icon: ' . $icon . "\r\n";
  69. }
  70. if($url!=NULL) {
  71. $data .= 'Notification-Callback-Target-Method: GET' . "\r\n";
  72. $data .= 'Notification-Callback-Target: ' . $url . "\r\n";
  73. }
  74. $data .= "\r\n\r\n";
  75. $data .= 'Origin-Software-Name: growl.gntp.php' . "\r\n";
  76. $data .= 'Origin-Software-Version: 1.0' . "\r\n";
  77. $this->send($data);
  78. }
  79. public function send($data) {
  80. $fp = fsockopen($this->host,$this->port,$errno,$errstr,$this->time);
  81. if(!$fp) {
  82. echo $errstr . ' (' . $errno . ')';
  83. }
  84. else {
  85. fwrite($fp,$data);
  86. fread($fp,12);
  87. fclose($fp);
  88. }
  89. }
  90. }