PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Bindings/network/netgrowl.php

https://bitbucket.org/mightymarc/growl
PHP | 117 lines | 104 code | 13 blank | 0 comment | 5 complexity | f1b9bb5999d9fec52f0ccda667d89466 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. define( 'GROWL_UDP_PORT', 9887 );
  2. define( 'GROWL_PROTOCOL_VERSION', 1 );
  3. define( 'GROWL_TYPE_REGISTRATION', 0 );
  4. define( 'GROWL_TYPE_NOTIFICATION', 1 );
  5. class GrowlRegistrationPacket { // {{{
  6. var $m_szApplication;
  7. var $m_aNotifications;
  8. var $m_szPassword;
  9. var $m_szData;
  10. function GrowlRegistrationPacket(
  11. $szApplication = "growlnotify",
  12. $szPassword = "" ) {
  13. $this->m_szApplication = utf8_encode($szApplication);
  14. $this->m_szPassword = $szPassword;
  15. $this->m_aNotifications = array();
  16. } // GrowlRegistrationPacket
  17. function addNotification(
  18. $szNotification = "Command-Line Growl Notification",
  19. $bEnabled = True) {
  20. $this->m_aNotifications[$szNotification] = $bEnabled;
  21. } // addNotification
  22. function payload() {
  23. $szEncoded = $szDefaults = "";
  24. $nCount = $nDefaults = 0;
  25. foreach( $this->m_aNotifications as $szName => $bEnabled ) {
  26. $szName = utf8_encode( $szName );
  27. $szEncoded .= pack( "n", strlen($szName) ) . $szName;
  28. $nCount++;
  29. if( $bEnabled ) {
  30. $szDefaults .= pack( "c", $nCount-1 );
  31. $nDefaults++;
  32. }
  33. }
  34. $this->m_szData = pack( "c2nc2",
  35. GROWL_PROTOCOL_VERSION,
  36. GROWL_TYPE_REGISTRATION,
  37. strlen($this->m_szApplication),
  38. $nCount,
  39. $nDefaults );
  40. $this->m_szData .= $this->m_szApplication . $szEncoded . $szDefaults;
  41. if( $this->m_szPassword )
  42. $szChecksum = pack( "H32", md5( $this->m_szData . $this->m_szPassword ) );
  43. else
  44. $szChecksum = pack( "H32", md5( $this->m_szData ));
  45. $this->m_szData .= $szChecksum;
  46. return $this->m_szData;
  47. } // payload
  48. } // GrowlNotificationPacket }}}
  49. class GrowlNotificationPacket { // {{{
  50. var $m_szApplication;
  51. var $m_szNotification;
  52. var $m_szTitle;
  53. var $m_szDescription;
  54. var $m_szData;
  55. function GrowlNotificationPacket(
  56. $szApplication = "growlnotify",
  57. $szNotification = "Command-Line Growl Notification",
  58. $szTitle = "Title",
  59. $szDescription = "Description",
  60. $nPriority = 0,
  61. $bSticky = False,
  62. $szPassword = "" ) {
  63. $this->m_szApplication = utf8_encode($szApplication);
  64. $this->m_szNotification = utf8_encode($szNotification);
  65. $this->m_szTitle = utf8_encode($szTitle);
  66. $this->m_szDescription = utf8_encode($szDescription);
  67. $nFlags = ($nPriority & 7) * 2;
  68. if( $nPriority < 0 )
  69. $nFlags |= 8;
  70. if( $bSticky )
  71. $nFlags |= 1;
  72. $this->m_szData = pack( "c2n5",
  73. GROWL_PROTOCOL_VERSION,
  74. GROWL_TYPE_NOTIFICATION,
  75. $nFlags,
  76. strlen($this->m_szNotification),
  77. strlen($this->m_szTitle),
  78. strlen($this->m_szDescription),
  79. strlen($this->m_szApplication) );
  80. $this->m_szData .= $this->m_szNotification;
  81. $this->m_szData .= $this->m_szTitle;
  82. $this->m_szData .= $this->m_szDescription;
  83. $this->m_szData .= $this->m_szApplication;
  84. if( $szPassword )
  85. $szChecksum = pack( "H32", md5( $this->m_szData . $szPassword ) );
  86. else
  87. $szChecksum = pack( "H32", md5( $this->m_szData ));
  88. $this->m_szData .= $szChecksum;
  89. } // GrowlNotificationPacket
  90. function payload() {
  91. return $this->m_szData;
  92. } // payload
  93. } // GrowlNotificationPacket }}}
  94. $s = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
  95. $p = new GrowlRegistrationPacket("PHP Notifier");
  96. $p->addNotification("Informational", false);
  97. $p->addNotification("Warning");
  98. $szBuffer = $p->payload();
  99. socket_sendto( $s, $szBuffer, strlen($szBuffer), 0x100, "192.168.0.42", GROWL_UDP_PORT );
  100. $p = new GrowlNotificationPacket("PHP Notifier", "Warning", "Apache",
  101. "PHP Warning", -2, True );
  102. $szBuffer = $p->payload();
  103. socket_sendto( $s, $szBuffer, strlen($szBuffer), 0x100, "192.168.0.42", GROWL_UDP_PORT );
  104. socket_close( $s );