detect_switch.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Copyright (C) 2010-2015
  17. All Rights Reserved.
  18. Contributor(s):
  19. Matthew Vale <[email protected]>
  20. */
  21. require_once "root.php";
  22. require_once "resources/classes/event_socket.php";
  23. //define the install class
  24. class detect_switch {
  25. // cached data
  26. protected $_dirs;
  27. protected $_vdirs;
  28. public function get_dirs() { return $this->_dirs; }
  29. public function get_vdirs() { return $this->_vdirs; }
  30. // version information
  31. protected $_major;
  32. protected $_minor;
  33. protected $_build;
  34. protected $_bits;
  35. public function major() { return $this->_major; }
  36. public function minor() { return $this->_minor; }
  37. public function build() { return $this->_build; }
  38. public function bits() { return $this->_bits; }
  39. public function version() { return $this->_major.".".$this->_minor.".".$this->_build." (".$this->_bits.")"; }
  40. // dirs - detected by from the switch
  41. protected $_base_dir = '';
  42. protected $_cache_dir = '';
  43. protected $_conf_dir = '';
  44. protected $_db_dir = '';
  45. protected $_grammar_dir = '';
  46. protected $_htdocs_dir = '';
  47. protected $_log_dir = '';
  48. protected $_mod_dir = '';
  49. protected $_recordings_dir = '';
  50. protected $_run_dir = '';
  51. protected $_script_dir = '';
  52. protected $_sounds_dir = '';
  53. protected $_storage_dir = '';
  54. protected $_temp_dir = '';
  55. public function base_dir() { return $this->_base_dir; }
  56. public function cache_dir() { return $this->_cache_dir; }
  57. public function conf_dir() { return $this->_conf_dir; }
  58. public function db_dir() { return $this->_db_dir; }
  59. public function grammar_dir() { return $this->_grammar_dir; }
  60. public function htdocs_dir() { return $this->_htdocs_dir; }
  61. public function log_dir() { return $this->_log_dir; }
  62. public function mod_dir() { return $this->_mod_dir; }
  63. public function recordings_dir() { return $this->_recordings_dir; }
  64. public function run_dir() { return $this->_run_dir; }
  65. public function script_dir() { return $this->_script_dir; }
  66. public function sounds_dir() { return $this->_sounds_dir; }
  67. public function storage_dir() { return $this->_storage_dir; }
  68. public function temp_dir() { return $this->_temp_dir; }
  69. // virtual dirs - assumed based on the detected dirs
  70. protected $_voicemail_vdir = '';
  71. protected $_phrases_vdir = '';
  72. protected $_extensions_vdir = '';
  73. protected $_sip_profiles_vdir = '';
  74. protected $_dialplan_vdir = '';
  75. protected $_backup_vdir = '';
  76. public function voicemail_vdir() { return $this->_voicemail_vdir; }
  77. public function phrases_vdir() { return $this->_phrases_vdir; }
  78. public function extensions_vdir() { return $this->_extensions_vdir; }
  79. public function sip_profiles_vdir() { return $this->_sip_profiles_vdir; }
  80. public function dialplan_vdir() { return $this->_dialplan_vdir; }
  81. public function backup_vdir() { return $this->_backup_vdir; }
  82. // event socket
  83. public $event_host = 'localhost';
  84. public $event_port = '8021';
  85. public $event_password = 'ClueCon';
  86. protected $event_socket;
  87. public function __construct($event_host, $event_port, $event_password) {
  88. //do not take these settings from session as they be detecting a new switch
  89. if($event_host){ $this->event_host = $event_host; }
  90. if($event_port){ $this->event_port = $event_port; }
  91. if($event_password){ $this->event_password = $event_password; }
  92. $this->connect_event_socket();
  93. if(!$this->event_socket){
  94. $this->detect_event_socket();
  95. }
  96. $this->_dirs = preg_grep ('/.*_dir$/', get_class_methods('detect_switch') );
  97. sort( $this->_dirs );
  98. $this->_vdirs = preg_grep ('/.*_vdir$/', get_class_methods('detect_switch') );
  99. sort( $this->_vdirs );
  100. }
  101. protected function detect_event_socket() {
  102. //perform searches for user's config here
  103. }
  104. public function detect() {
  105. $this->connect_event_socket();
  106. if(!$this->event_socket){
  107. throw new Exception('Failed to use event socket');
  108. }
  109. $FS_Version = $this->event_socket_request('api version');
  110. preg_match("/FreeSWITCH Version (\d+)\.(\d+)\.(\d+(?:\.\d+)?).*\(.*?(\d+\w+)\s*\)/", $FS_Version, $matches);
  111. $this->_major = $matches[1];
  112. $this->_minor = $matches[2];
  113. $this->_build = $matches[3];
  114. $this->_bits = $matches[4];
  115. $FS_Vars = $this->event_socket_request('api global_getvar');
  116. foreach (explode("\n",$FS_Vars) as $FS_Var){
  117. preg_match("/(\w+_dir)=(.*)/", $FS_Var, $matches);
  118. if(count($matches) > 0 and property_exists($this, "_" . $matches[1])){
  119. $field = "_" . $matches[1];
  120. $this->$field = normalize_path($matches[2]);
  121. }
  122. }
  123. $this->_voicemail_vdir = normalize_path($this->_storage_dir . DIRECTORY_SEPARATOR . "voicemail");
  124. $this->_phrases_vdir = normalize_path($this->_conf_dir . DIRECTORY_SEPARATOR . "lang");
  125. $this->_extensions_vdir = normalize_path($this->_conf_dir . DIRECTORY_SEPARATOR . "directory");
  126. $this->_sip_profiles_vdir = normalize_path($this->_conf_dir . DIRECTORY_SEPARATOR . "sip_profiles");
  127. $this->_dialplan_vdir = normalize_path($this->_conf_dir . DIRECTORY_SEPARATOR . "dialplan");
  128. $this->_backup_vdir = normalize_path(sys_get_temp_dir());
  129. }
  130. protected function connect_event_socket(){
  131. $esl = new event_socket;
  132. if ($esl->connect($this->event_host, $this->event_port, $this->event_password)) {
  133. $this->event_socket = $esl->reset_fp();
  134. return true;
  135. }
  136. return false;
  137. }
  138. protected function event_socket_request($cmd) {
  139. $esl = new event_socket($this->event_socket);
  140. $result = $esl->request($cmd);
  141. $esl->reset_fp();
  142. return $result;
  143. }
  144. public function restart_switch() {
  145. $this->connect_event_socket();
  146. if(!$this->event_socket){
  147. throw new Exception('Failed to use event socket');
  148. }
  149. $this->event_socket_request('api fsctl shutdown restart elegant');
  150. }
  151. }
  152. ?>