message_queue.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. //only allow command line
  3. if (defined('STDIN')) {
  4. //set the include path
  5. $conf = glob("{/usr/local/etc,/etc}/fusionpbx/config.conf", GLOB_BRACE);
  6. set_include_path(parse_ini_file($conf[0])['document.root']);
  7. }
  8. else {
  9. exit;
  10. }
  11. //increase limits
  12. set_time_limit(0);
  13. ini_set('max_execution_time', 0);
  14. ini_set('memory_limit', '128M');
  15. //save the arguments to variables
  16. $script_name = $argv[0];
  17. if (!empty($argv[1])) {
  18. parse_str($argv[1], $_GET);
  19. }
  20. //print_r($_GET);
  21. //echo __line__."\n";
  22. //set the variables
  23. if (isset($_GET['hostname'])) {
  24. $hostname = urldecode($_GET['hostname']);
  25. }
  26. if (isset($_GET['debug'])) {
  27. $debug = $_GET['debug'];
  28. }
  29. $debug = false;
  30. //set the hostname if it wasn't provided
  31. if (!isset($hostname) || (isset($hostname) && strlen($hostname) == 0)) {
  32. $hostname = gethostname();
  33. }
  34. //includes
  35. require_once "resources/require.php";
  36. include "resources/classes/permissions.php";
  37. //define the process id file
  38. $pid_file = "/var/run/fusionpbx/".basename( $argv[0], ".php") .".pid";
  39. //echo "pid_file: ".$pid_file."\n";
  40. //function to check if the process exists
  41. function process_exists($file = false) {
  42. //set the default exists to false
  43. $exists = false;
  44. //check to see if the process is running
  45. if (file_exists($file)) {
  46. $pid = file_get_contents($file);
  47. if (posix_getsid($pid) === false) {
  48. //process is not running
  49. $exists = false;
  50. }
  51. else {
  52. //process is running
  53. $exists = true;
  54. }
  55. }
  56. //return the result
  57. return $exists;
  58. }
  59. //check to see if the process exists
  60. $pid_exists = process_exists($pid_file);
  61. //prevent the process running more than once
  62. if ($pid_exists) {
  63. echo "Cannot lock pid file {$pid_file}\n";
  64. exit;
  65. }
  66. //create the process id file if the process doesn't exist
  67. if (!$pid_exists) {
  68. //remove the old pid file
  69. if (file_exists($pid_file)) {
  70. unlink($pid_file);
  71. }
  72. //show the details to the user
  73. //echo "The process id is ".getmypid()."\n";
  74. //save the pid file
  75. file_put_contents($pid_file, getmypid());
  76. }
  77. //get the messages waiting in the queue
  78. while (true) {
  79. //get the messages that are waiting to send
  80. $sql = "select message_queue_uuid, message_direction, hostname from v_message_queue ";
  81. $sql .= "where message_status = 'waiting' ";
  82. $sql .= "and (message_type = 'sms' or message_type = 'mms') ";
  83. $sql .= "and hostname = :hostname ";
  84. $sql .= "and message_from is not null ";
  85. $sql .= "and message_to is not null ";
  86. $sql .= "order by domain_uuid asc ";
  87. $sql .= "limit 300; ";
  88. //echo $sql."\n";
  89. if (isset($hostname)) {
  90. $parameters['hostname'] = $hostname;
  91. }
  92. else {
  93. $parameters['hostname'] = gethostname();
  94. }
  95. //print_r($parameters);
  96. $database = new database;
  97. $message_queue = $database->select($sql, $parameters, 'all');
  98. //view_array($message_queue, false);
  99. unset($parameters);
  100. //process the messages
  101. if (is_array($message_queue) && @sizeof($message_queue) != 0) {
  102. foreach($message_queue as $row) {
  103. //direction inbound, send SIP messages to registered phones
  104. if ($row['message_direction'] == 'inbound') {
  105. $command = "/usr/bin/php /var/www/fusionpbx/app/messages/resources/service/message_send_inbound.php ";
  106. $command .= "'message_queue_uuid=".$row['message_queue_uuid']."&hostname=".$row['hostname']."'";
  107. }
  108. //direction outbound, send message to the provider
  109. if ($row['message_direction'] == 'outbound') {
  110. // /usr/bin/php /var/www/fusionpbx/app/messages/resources/service/message_send_outbound.php action=send&message_queue_uuid=&hostname=voip.fusionpbx.com
  111. $command = "/usr/bin/php /var/www/fusionpbx/app/messages/resources/service/message_send_outbound.php ";
  112. $command .= "'message_queue_uuid=".$row['message_queue_uuid']."&hostname=".$row['hostname']."'";
  113. }
  114. //send the command
  115. if (isset($command)) {
  116. if (isset($debug)) {
  117. //run process inline to see debug info
  118. echo $command."\n";
  119. $result = system($command);
  120. echo $result."\n";
  121. }
  122. else {
  123. //starts process rapidly doesn't wait for previous process to finish (used for production)
  124. $handle = popen($command." > /dev/null &", 'r');
  125. echo "'$handle'; " . gettype($handle) . "\n";
  126. $read = fread($handle, 2096);
  127. echo $read;
  128. pclose($handle);
  129. }
  130. }
  131. unset($command);
  132. }
  133. }
  134. //pause to prevent excessive database queries
  135. sleep(1);
  136. }
  137. //remove the old pid file
  138. if (file_exists($pid_file)) {
  139. unlink($pid_file);
  140. }
  141. //save output to
  142. //$fp = fopen(sys_get_temp_dir()."/mailer-app.log", "a");
  143. //prepare the output buffers
  144. //ob_end_clean();
  145. //ob_start();
  146. //message divider for log file
  147. //echo "\n\n=============================================================================================================================================\n\n";
  148. //get and save the output from the buffer
  149. //$content = ob_get_contents(); //get the output from the buffer
  150. //$content = str_replace("<br />", "", $content);
  151. //ob_end_clean(); //clean the buffer
  152. //fwrite($fp, $content);
  153. //fclose($fp);
  154. //notes
  155. //echo __line__."\n";
  156. // if not keeping the email then need to delete it after the voicemail is emailed
  157. //how to use this feature
  158. // cd /var/www/fusionpbx && /usr/bin/php /var/www/fusionpbx/app/messages/resources/service/message_queue.php
  159. ?>