| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * MikoPBX - free phone system for small business | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * the Free Software Foundation; either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * You should have received a copy of the GNU General Public License along with this program. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * If not, see <https://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | namespace MikoPBX\Core\Asterisk\Configs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use MikoPBX\Common\Models\SoundFiles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use MikoPBX\Core\System\Processes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use MikoPBX\Core\System\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use MikoPBX\PBXCoreREST\Lib\SystemManagementProcessor; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | class MusicOnHoldConf extends CoreConfigClass | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     protected string $description = 'musiconhold.conf'; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     protected function generateConfigProtected(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $mohpath    = $this->config->path('asterisk.mohdir'); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $conf = "[default]\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             "mode=files\n" . | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             "directory=$mohpath\n\n"; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         Util::fileWriteContent($this->config->path('asterisk.astetcdir') . '/musiconhold.conf', $conf); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->checkMohFiles(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * Проверка существования MOH файлов. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     protected function checkMohFiles():void{ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $path    = $this->config->path('asterisk.mohdir'); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $mask    = '/*.mp3'; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $fList   = glob("{$path}{$mask}"); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         if(count($fList) !== 0 ){ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             foreach ($fList as $resultMp3){ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 $this->checkAddFileToDB($resultMp3); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         Util::sysLogMsg(static::class, 'Attempt to restore MOH from default...'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $filesList    = glob("/offload/asterisk/sounds/moh{$mask}"); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $cpPath       = Util::which('cp'); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         foreach ($filesList as $srcFile){ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $resultMp3 = "{$path}/".basename($srcFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $resultWav = Util::trimExtensionForFile($resultMp3).'.wav'; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             Processes::mwExec("{$cpPath} $srcFile {$resultMp3}"); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             SystemManagementProcessor::convertAudioFile($resultMp3); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             if(!file_exists($resultWav)){ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 Util::sysLogMsg(static::class, "Failed to convert file {$resultWav}..."); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             $this->checkAddFileToDB($resultMp3); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected function checkAddFileToDB($resultMp3):void{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         /** @var SoundFiles $sf */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $sf = SoundFiles::findFirst("path='{$resultMp3}'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if(!$sf) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $sf = new SoundFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $sf->category = SoundFiles::CATEGORY_MOH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $sf->name     = basename($resultMp3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $sf->path     = $resultMp3; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             if(!$sf->save()){ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |                 Util::sysLogMsg(static::class, "Error save SoundFiles record {$sf->name}..."); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  | } |