school_bells_cron.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. //restrict to command line only
  3. if(defined('STDIN')) {
  4. $document_root = dirname(__DIR__, 2);
  5. set_include_path($document_root);
  6. require_once "resources/require.php";
  7. require_once "resources/classes/text.php";
  8. $_SERVER["DOCUMENT_ROOT"] = $document_root;
  9. $format = 'text'; //html, text
  10. //add multi-lingual support
  11. $language = new text;
  12. $text = $language->get();
  13. }
  14. else {
  15. die('access denied');
  16. }
  17. //show debug
  18. $debug = false;
  19. set_time_limit(55); // Cannot run more than 55 seconds
  20. $current_timestamp = time();
  21. $current_minute = (int)date('i', $current_timestamp);
  22. $sql = "SELECT v_domains.domain_name AS context,";
  23. $sql .= " v_school_bells.domain_uuid AS domain_uuid,";
  24. $sql .= " school_bell_leg_a_data AS extension,";
  25. $sql .= " school_bell_leg_b_type AS full_path,";
  26. $sql .= " school_bell_ring_timeout AS ring_timeout,";
  27. $sql .= " school_bell_min as min,";
  28. $sql .= " school_bell_hour as hour,";
  29. $sql .= " school_bell_dom as dom,";
  30. $sql .= " school_bell_mon as mon,";
  31. $sql .= " school_bell_dow as dow,";
  32. $sql .= " school_bell_timezone as timezone ";
  33. $sql .= "FROM v_school_bells ";
  34. $sql .= "JOIN v_domains ON v_domains.domain_uuid = v_school_bells.domain_uuid ";
  35. $sql .= " WHERE school_bell_min = :current_minute";
  36. $sql .= " OR school_bell_min = -1";
  37. $parameters['current_minute'] = $current_minute;
  38. $database = new database;
  39. $school_bells = $database->select($sql, $parameters, 'all');
  40. if ($debug) {
  41. echo $sql."\n";
  42. print_r($parameters);
  43. print_r($school_bells);
  44. }
  45. unset($sql, $parameters);
  46. /*
  47. //used for fusionpbx 4.4
  48. $prep_statement = $db->prepare(check_sql($sql));
  49. if (!$prep_statement) {
  50. die('SQL forming error');
  51. }
  52. $prep_statement->bindValue('current_minute', $current_minute);
  53. if (!$prep_statement->execute()) {
  54. die('SQL execute error');
  55. }
  56. $school_bells = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  57. */
  58. if (count($school_bells) == 0) {
  59. echo "no bells\n";
  60. return;
  61. }
  62. $freeswitch_event_socket = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  63. if (!$freeswitch_event_socket) {
  64. die("Cannot connect to Event socket");
  65. }
  66. //$switch_result = event_socket_request($fp, 'api sofia status');
  67. //print($switch_result);
  68. foreach ($school_bells as $school_bell) {
  69. $school_bell_timezone = $school_bell['timezone'];
  70. date_default_timezone_set($school_bell_timezone);
  71. $school_bell_hour = (int)$school_bell['hour'];
  72. $current_hour = (int)date('G', $current_timestamp);
  73. if ((int)$current_hour != (int)$school_bell_hour) { // Hour is not matched
  74. if ($debug) {
  75. echo "Hour is not matched current {$current_hour} != bell {$school_bell_hour}\n";
  76. }
  77. continue;
  78. }
  79. $school_bell_dom = (int)$school_bell['dom'];
  80. $current_dom = (int)date('j', $current_timestamp);
  81. if ($school_bell_dom != -1 && (int)$current_dom != (int)$school_bell_dom) { // Day of the month is not matched
  82. if ($debug) {
  83. echo "Day of the month is not matched {$current_dom} bell {$school_bell_dom}\n";
  84. }
  85. continue;
  86. }
  87. $school_bell_mon = (int)$school_bell['mon'];
  88. $current_mon = (int)date('n', $current_timestamp);
  89. if ($school_bell_mon != -1 && (int)$current_mon != (int)$school_bell_mon) { // Month is not matched
  90. if ($debug) {
  91. echo "Month is not matched current {$current_mon} != bell {$school_bell_mon}\n";
  92. }
  93. continue;
  94. }
  95. $school_bell_dow = (int)$school_bell['dow'];
  96. $current_dow = (int)date('w', $current_timestamp);
  97. if ($school_bell_dow != -1 && (int)$current_dow != (int)$school_bell_dow) { // Day of the week is not matched
  98. if ($debug) {
  99. echo "Day of the week is not matched current {$current_dow} != bell {$school_bell_dow}\n";
  100. }
  101. continue;
  102. }
  103. // We got our signal!
  104. $school_bell_ring_timeout = (int)$school_bell['ring_timeout'];
  105. if ($school_bell_ring_timeout > 60) {
  106. $school_bell_ring_timeout = ($school_bell['min'] == "-1") ? 55: $school_bell_ring_timeout;
  107. }
  108. $school_bell_ring_timeout = ($school_bell_ring_timeout == 0) ? 5 : $school_bell_ring_timeout;
  109. $switch_cmd = "bgapi ";
  110. $switch_cmd .= "originate {ignore_early_media=true,";
  111. $switch_cmd .= "hangup_after_bridge=true,";
  112. $switch_cmd .= "domain_name=".$school_bell['context'].",";
  113. $switch_cmd .= "domain_uuid=".$school_bell['domain_uuid'].",";
  114. $switch_cmd .= "call_timeout=".$school_bell_ring_timeout."}";
  115. $switch_cmd .= "loopback/".$school_bell['extension']."/".$school_bell['context'];
  116. $switch_cmd .= " &playback(".$school_bell['full_path'].")";
  117. if ($debug) {
  118. echo $switch_cmd."\n";
  119. }
  120. event_socket_request($freeswitch_event_socket, $switch_cmd);
  121. }
  122. ?>