Passed
Push — develop ( e0de08...f5bf18 )
by Портнов
04:43
created

ExtensionsInterception::testOriginate()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 21
rs 9.4222
cc 5
nc 8
nop 3
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2021 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\Util;
24
25
class ExtensionsInterception extends CoreConfigClass
26
{
27
    /**
28
     * Prepares additional contexts sections in the extensions.conf file
29
     *
30
     * @return string
31
     */
32
    public function extensionGenContexts(): string
33
    {
34
        return  '[interception-bridge]' . PHP_EOL.
35
                'exten => failed,1,Hangup()' . PHP_EOL.
36
                'exten => _[0-9*#+a-zA-Z][0-9*#+a-zA-Z]!,1,ExecIf($[ "${ORIGINATE_SRC_CHANNEL}x" != "x" ]?Wait(0.2))' . PHP_EOL."\t".
37
                'same => n,ExecIf($[ "${ORIGINATE_SRC_CHANNEL}x" != "x" ]?ChannelRedirect(${ORIGINATE_SRC_CHANNEL},${CONTEXT},${ORIGINATE_DST_EXTEN},1))' . PHP_EOL."\t".
38
                'same => n,ExecIf($[ "${ORIGINATE_SRC_CHANNEL}x" != "x" ]?Hangup())' . PHP_EOL."\t".
39
                'same => n,Set(FROM_CHAN=${INTECEPTION_CNANNEL})' . PHP_EOL."\t".
40
                'same => n,Set(MASTER_CHANNEL(M_TIMEOUT_CHANNEL)=${INTECEPTION_CNANNEL})' . PHP_EOL."\t".
41
                'same => n,AGI(/usr/www/src/Core/Asterisk/agi-bin/clean_timeout.php)' . PHP_EOL."\t".
42
                'same => n,Gosub(dial_interception,${EXTEN},1)' . PHP_EOL."\t".
43
                'same => n,Gosub(dial_answer,${EXTEN},1)' . PHP_EOL."\t".
44
                'same => n,Bridge(${INTECEPTION_CNANNEL},tk)' . PHP_EOL."\t".
45
                'same => n,Hangup()' . PHP_EOL;
46
    }
47
48
    public static function testOriginate($providerId = 'SIP-1611151795', $src = '201', $dest_number = '79257184233'):void{
49
        $am = Util::getAstManager('off');
50
        $channels=$am->GetChannels();
51
        $interceptionChannel = '';
52
        foreach ($channels as $linkedId => $linkedIdData){
53
            foreach ($linkedIdData as $tmpChannel){
54
                if(strpos($tmpChannel, 'PJSIP/'.$providerId) === false){
55
                    continue;
56
                }
57
                $interceptionChannel  = $tmpChannel;
58
                $interceptionLinkedId = $linkedId;
59
            }
60
        }
61
        if(empty($interceptionChannel)){
62
            echo("Chan not found..." .PHP_EOL);
63
            return;
64
        }
65
        $variable    = "pt1c_cid={$dest_number},ALLOW_MULTY_ANSWER=1,_INTECEPTION_CNANNEL={$interceptionChannel},_OLD_LINKEDID={$interceptionLinkedId}";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $interceptionLinkedId does not seem to be defined for all execution paths leading up to this point.
Loading history...
66
        $channel     = "Local/{$src}}@internal-originate";
67
        $context     = 'interception-bridge';
68
        $am->Originate($channel, $dest_number, $context, '1', null, null, null, $src, $variable, null, false);
69
    }
70
}