plugin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // PLUGIN INFORMATION
  3. use Pusher\PusherException;
  4. $GLOBALS['plugins'][]['Chat'] = array( // Plugin Name
  5. 'name' => 'Chat', // Plugin Name
  6. 'author' => 'CauseFX', // Who wrote the plugin
  7. 'category' => 'Utilities', // One to Two Word Description
  8. 'link' => '', // Link to plugin info
  9. 'license' => 'personal,business', // License Type use , for multiple
  10. 'idPrefix' => 'CHAT', // html element id prefix
  11. 'configPrefix' => 'CHAT', // config file prefix for array items without the hypen
  12. 'version' => '1.0.0', // SemVer of plugin
  13. 'image' => 'api/plugins/chat/logo.png', // 1:1 non transparent image for plugin
  14. 'settings' => true, // does plugin need a settings modal?
  15. 'bind' => true, // use default bind to make settings page - true or false
  16. 'api' => 'api/v2/plugins/chat/settings', // api route for settings page
  17. 'homepage' => false // Is plugin for use on homepage? true or false
  18. );
  19. class Chat extends Organizr
  20. {
  21. public function _chatPluginGetSettings()
  22. {
  23. return array(
  24. 'custom' => '
  25. <div class="row">
  26. <div class="col-lg-12">
  27. <div class="panel panel-info">
  28. <div class="panel-heading">
  29. <span lang="en">Notice</span>
  30. </div>
  31. <div class="panel-wrapper collapse in" aria-expanded="true">
  32. <div class="panel-body">
  33. <ul class="list-icons">
  34. <li><i class="fa fa-chevron-right text-danger"></i> <a href="https://dashboard.pusher.com/accounts/sign_up" target="_blank"><span lang="en">Signup for Pusher [FREE]</span></a></li>
  35. <li><i class="fa fa-chevron-right text-danger"></i> <span lang="en">Create an App called whatever you like and choose a cluster (Close to you)</span></li>
  36. <li><i class="fa fa-chevron-right text-danger"></i> <span lang="en">Frontend (JQuery) - Backend (PHP)</span></li>
  37. <li><i class="fa fa-chevron-right text-danger"></i> <span lang="en">Click the overview tab on top left</span></li>
  38. <li><i class="fa fa-chevron-right text-danger"></i> <span lang="en">Copy and paste the 4 values into Organizr</span></li>
  39. <li><i class="fa fa-chevron-right text-danger"></i> <span lang="en">Save and reload!</span></li>
  40. </ul>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. ',
  47. 'Options' => array(
  48. array(
  49. 'type' => 'select',
  50. 'name' => 'CHAT-Auth-include',
  51. 'label' => 'Minimum Authentication',
  52. 'value' => $this->config['CHAT-Auth-include'],
  53. 'options' => $this->groupSelect()
  54. ),
  55. array(
  56. 'type' => 'number',
  57. 'name' => 'CHAT-messageLoadLimit',
  58. 'label' => '# of Previous Messages',
  59. 'value' => $this->config['CHAT-messageLoadLimit'],
  60. 'placeholder' => ''
  61. ),
  62. array(
  63. 'type' => 'select',
  64. 'name' => 'CHAT-userRefreshTimeout',
  65. 'label' => 'Refresh Seconds',
  66. 'value' => $this->config['CHAT-userRefreshTimeout'],
  67. 'options' => $this->timeOptions()
  68. ),
  69. array(
  70. 'type' => 'select',
  71. 'name' => 'CHAT-newMessageSound-include',
  72. 'label' => 'Message Sound',
  73. 'value' => $this->config['CHAT-newMessageSound-include'],
  74. 'options' => $this->getSounds()
  75. ),
  76. array(
  77. 'type' => 'switch',
  78. 'name' => 'CHAT-useSSL',
  79. 'label' => 'Use Pusher SSL',
  80. 'help' => 'If messages get stuck sending, please turn this option off.',
  81. 'value' => $this->config['CHAT-useSSL']
  82. )
  83. ),
  84. 'Connection' => array(
  85. array(
  86. 'type' => 'password-alt',
  87. 'name' => 'CHAT-authKey-include',
  88. 'label' => 'Auth Key',
  89. 'value' => $this->config['CHAT-authKey-include']
  90. ),
  91. array(
  92. 'type' => 'password-alt',
  93. 'name' => 'CHAT-secret',
  94. 'label' => 'API Secret',
  95. 'value' => $this->config['CHAT-secret']
  96. ),
  97. array(
  98. 'type' => 'input',
  99. 'name' => 'CHAT-appID-include',
  100. 'label' => 'App ID',
  101. 'value' => $this->config['CHAT-appID-include']
  102. ),
  103. array(
  104. 'type' => 'input',
  105. 'name' => 'CHAT-cluster-include',
  106. 'label' => 'App Cluster',
  107. 'value' => $this->config['CHAT-cluster-include']
  108. ),
  109. )
  110. );
  111. }
  112. public function _chatPluginSendChatMessage($array)
  113. {
  114. $message = isset($array['message']) ? $array['message'] : null;
  115. if (!$message) {
  116. $this->setAPIResponse('error', 'No message supplied', 409);
  117. return false;
  118. }
  119. $message = htmlspecialchars($message, ENT_QUOTES);
  120. $now = date("Y-m-d H:i:s");
  121. $currentIP = $this->userIP();
  122. $newMessage = [
  123. 'username' => $this->user['username'],
  124. 'gravatar' => $this->user['image'],
  125. 'uid' => $this->user['uid'],
  126. 'date' => $now,
  127. 'ip' => $currentIP,
  128. 'message' => $message
  129. ];
  130. $response = [
  131. array(
  132. 'function' => 'query',
  133. 'query' => array(
  134. 'INSERT INTO [chatroom]',
  135. $newMessage
  136. )
  137. ),
  138. ];
  139. $query = $this->processQueries($response);
  140. if ($query) {
  141. $options = array(
  142. 'cluster' => $this->config['CHAT-cluster-include'],
  143. 'useTLS' => $this->config['CHAT-useSSL']
  144. );
  145. try {
  146. $pusher = new Pusher\Pusher(
  147. $this->config['CHAT-authKey-include'],
  148. $this->config['CHAT-secret'],
  149. $this->config['CHAT-appID-include'],
  150. $options
  151. );
  152. $pusher->trigger('org_channel', 'my-event', $newMessage);
  153. $this->setAPIResponse('success', 'Chat message accepted', 200);
  154. return true;
  155. } catch (PusherException $e) {
  156. $this->setAPIResponse('error', 'Chat message error', 500);
  157. }
  158. }
  159. $this->setAPIResponse('error', 'Chat error occurred', 409);
  160. return false;
  161. }
  162. public function _chatPluginGetChatMessages()
  163. {
  164. $response = [
  165. array(
  166. 'function' => 'fetchAll',
  167. 'query' => array(
  168. 'SELECT `username`, `gravatar`, `uid`, `date`, `message` FROM (SELECT `username`, `gravatar`, `uid`, `date`, `message` FROM chatroom ORDER BY date DESC LIMIT ?) ORDER BY date ASC',
  169. $this->config['CHAT-messageLoadLimit']
  170. )
  171. ),
  172. ];
  173. return $this->processQueries($response);
  174. }
  175. }