PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/freeswitch-1.1/plugins/freeswitch.php

https://github.com/robertleeplummerjr/bluebox
PHP | 291 lines | 196 code | 68 blank | 27 comment | 25 complexity | 18b01e1325f78a2870bfb456f2a73970 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * freeswitch.php - Provides logic for the installer telephony configuration step
  4. * @author K Anderson
  5. * @license MPL
  6. * @package Bluebox
  7. */
  8. class Freeswitch_Plugin extends Bluebox_Plugin
  9. {
  10. // A list of possible directories that may have the freeswitch.xml
  11. // file (denoting the FS directory.
  12. public static $scanDirs = array(
  13. '/usr/local/freeswitch',
  14. '/usr/local/freeswitch-trunk',
  15. '/opt/freeswitch',
  16. '/opt/freeswitch-trunk',
  17. 'C:\FreeSWITCH',
  18. 'C:\freeswitch'
  19. );
  20. /**
  21. * Setup the subview for the address plugin
  22. */
  23. public function install()
  24. {
  25. $subview = new View('freeswitch/install');
  26. $subview->tab = 'main';
  27. $subview->section = 'general';
  28. $cfg_root = $this->session->get('installer.cfg_root', FALSE);
  29. $audio_root = $this->session->get('installer.audio_root', FALSE);
  30. $fsDefaultCfg = Kohana::config('freeswitch.cfg_root');
  31. if (!$cfg_root)
  32. {
  33. foreach (self::$scanDirs as $testDir)
  34. {
  35. $testPath = rtrim($testDir) . '/conf/freeswitch.xml';
  36. if (file_exists($testPath))
  37. {
  38. $cfg_root = rtrim($testDir) . '/conf';
  39. break;
  40. }
  41. }
  42. if (empty($cfg_root))
  43. {
  44. $cfg_root = $fsDefaultCfg;
  45. }
  46. }
  47. if (!$audio_root)
  48. {
  49. foreach (self::$scanDirs as $testDir)
  50. {
  51. $testPath = rtrim($testDir) .'/sounds';
  52. if (is_dir($testPath))
  53. {
  54. $audio_root = $testPath;
  55. break;
  56. }
  57. }
  58. if (empty($audio_root))
  59. {
  60. # $audio_root = $fsDefaultAudioRoot;
  61. }
  62. }
  63. $subview->cfg_root = $cfg_root;
  64. $subview->audio_root = $audio_root;
  65. $subview->esl_host = $this->session->get('installer.esl_host', Kohana::config('freeswitch.ESLHost'));
  66. $subview->esl_port = $this->session->get('installer.esl_port', Kohana::config('freeswitch.ESLPort'));
  67. $subview->esl_auth = $this->session->get('installer.esl_auth', Kohana::config('freeswitch.ESLAuth'));
  68. // Get a list of existing sip_profiles and warn the user that these will be deleted
  69. $sipProfiles = glob(rtrim($subview->cfg_root, '/') . '/sip_profiles/*.xml', GLOB_MARK);
  70. // See if any xml files that bluebox uses exist already and warn the user they will be deleted
  71. $filemaps = Kohana::config('freeswitch.filemap');
  72. $oldXmlFiles = array();
  73. foreach ($filemaps as $filemap)
  74. {
  75. if ($fsDefaultCfg != $subview->cfg_root)
  76. {
  77. $filemap['filename'] = str_replace($fsDefaultCfg, $subview->cfg_root, $filemap['filename']);
  78. }
  79. if (file_exists($filemap['filename']))
  80. {
  81. $oldXmlFiles[] = $filemap['filename'];
  82. }
  83. }
  84. // Create one unique list of all bluebox xml and extra sip profiles
  85. $conflictXmlFiles = array_unique(array_merge($oldXmlFiles, $sipProfiles));
  86. $subview->conflictXmlFiles = $conflictXmlFiles;
  87. $this->session->set('installer.conflictXmlFiles', $conflictXmlFiles);
  88. // Add our view to the main application
  89. $this->views[] = $subview;
  90. }
  91. public function save()
  92. {
  93. if (!class_exists('DOMDocument'))
  94. {
  95. message::set('This driver requires '
  96. . html::anchor('http://us3.php.net/manual/en/class.domdocument.php', 'DOMDocument', array('target' => '_new'))
  97. . ' to be installed and active');
  98. return false;
  99. }
  100. // This array maps the telephony returns to the telephony file
  101. $telephonyOptions = array(
  102. 'cfg_root' => rtrim($this->session->get('installer.cfg_root'), '/'),
  103. 'audio_root' => rtrim($this->session->get('installer.audio_root'), '/'),
  104. 'ESLHost' => $this->session->get('installer.esl_host'),
  105. 'ESLPort' => $this->session->get('installer.esl_port'),
  106. 'ESLAuth' => $this->session->get('installer.esl_auth')
  107. );
  108. if ( ! is_dir($telephonyOptions['cfg_root']))
  109. {
  110. message::set('Unable to access directory <pre>' .$telephonyOptions['cfg_root'] .'</pre>');
  111. return false;
  112. }
  113. if ( ! is_dir($telephonyOptions['audio_root']))
  114. {
  115. message::set('Unable to access directory <pre>' .$telephonyOptions['audio_root'] .'</pre>');
  116. return false;
  117. }
  118. // Write $telephonyOptions to freeswitch.php
  119. if ( ! Installer_Controller::updateConfig($telephonyOptions, 'freeswitch'))
  120. {
  121. return false;
  122. }
  123. // Set the driver name in telephony.php
  124. if (!Installer_Controller::updateConfig(array('driver' => 'FreeSwitch'), 'telephony'))
  125. {
  126. return false;
  127. }
  128. // Reload the new telephony options so we can use the filemap
  129. Kohana::config_clear('freeswitch');
  130. Kohana::config_load('freeswitch');
  131. $filemaps = Kohana::config('freeswitch.filemap');
  132. $notWritable = array();
  133. foreach ($filemaps as $filemap)
  134. {
  135. if ( ! filesystem::is_writable($filemap['filename']))
  136. {
  137. $notWritable[] = $filemap['filename'];
  138. }
  139. }
  140. if ( ! empty($notWritable))
  141. {
  142. $notWritable = array_unique($notWritable);
  143. if (empty($this->template->error))
  144. {
  145. message::set('Ensure the web server has write permission on these files, and SELINUX allows this action.'
  146. .'<div class="write_help">Unable to write to the following file(s):</div>'
  147. .'<div>'
  148. .arr::arrayToUL($notWritable, array(), array('class' => 'error_details', 'style' => 'text-align:left;'))
  149. .'</div>'
  150. );
  151. }
  152. return false;
  153. }
  154. // Make sure that if the user changed the directory and any conflicts were found that the user
  155. // knows these will be deleted
  156. $existingProfiles = glob(rtrim($telephonyOptions['cfg_root'], '/') . '/sip_profiles/*.xml', GLOB_MARK);
  157. $oldXmlFiles = array();
  158. foreach ($filemaps as $filemap)
  159. {
  160. if (file_exists($filemap['filename']))
  161. {
  162. $oldXmlFiles[] = $filemap['filename'];
  163. }
  164. }
  165. $conflictXmlFiles = $this->session->get('installer.conflictXmlFiles');
  166. foreach (array_unique(array_merge($existingProfiles, $oldXmlFiles)) as $existingFile)
  167. {
  168. if ( ! in_array($existingFile, $conflictXmlFiles))
  169. {
  170. message::set('Conflicting configuration files will be permanently erased if you continue!');
  171. message::set('Click continue again to proceed...', 'alert');
  172. // This session var lets the user continue the second time around (after the warning)
  173. $this->session->set('installer.confirm_delete', true);
  174. return false;
  175. }
  176. }
  177. // If there are conflictXmlFile in the session then the user has seen this list
  178. // so they SHOULD be aware we are about to delete them... should
  179. $conflictXmlFiles = $this->session->get('installer.conflictXmlFiles');
  180. if ( ! empty($conflictXmlFiles) && is_array($conflictXmlFiles))
  181. {
  182. $confirmDelete = $this->session->get('installer.confirm_delete', false);
  183. if (empty($confirmDelete))
  184. {
  185. message::set('Conflicting configuration files will be permanently erased if you continue!');
  186. message::set('Click continue again to proceed...', 'alert');
  187. // This session var lets the user continue the second time around (after the warning)
  188. $this->session->set('installer.confirm_delete', true);
  189. return false;
  190. }
  191. foreach ($conflictXmlFiles as $conflictXmlFile)
  192. {
  193. if ( ! filesystem::delete($conflictXmlFile))
  194. {
  195. Kohana::log('error', 'Unable to unlink ' . $conflictXmlFile);
  196. $unlinkErrors[] = $conflictXmlFile;
  197. }
  198. }
  199. }
  200. // If there are files that could not be deleted, inform the user
  201. if ( ! empty($unlinkErrors))
  202. {
  203. message::set('Manually remove these files or change the file permissions.'
  204. .'<div class="write_help">Unable to delete incompatible file(s):</div>'
  205. .'<div>'
  206. .arr::arrayToUL($unlinkErrors, array(), array('class' => 'error_details', 'style' => 'text-align:left;'))
  207. .'</div>'
  208. );
  209. return false;
  210. }
  211. $this->session->set('installer.default_packages', kohana::config('freeswitch.default_packages'));
  212. return true;
  213. }
  214. public function postInstallReload()
  215. {
  216. if (class_exists('EslManager', TRUE))
  217. {
  218. $esl = new EslManager();
  219. $esl->reloadacl();
  220. $esl->reloadxml();
  221. }
  222. }
  223. }