Search code examples
freepbx

Freepbx: Add Extension Programmatically


I was trying to add extension programatically. There I was facing some issues. My code was

<?php
$bootstrap_settings = array();
$bootstrap_settings['freepbx_auth'] = false;
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) { 
  include_once('/etc/asterisk/freepbx.conf'); 
}

$vars = array(
"extension" => 9876543200,
    "name" => "Test",
    "devinfo_secret" => "testing",
    "devinfo_mediaencryption" => "sdes",
);

core_users_add($vars);
core_devices_add(9876543200,'pjsip','PJSIP/9876543200','fixed',9876543200,'Test');

And output

[root@sg html]# php a.php
[FATAL] Fields are empty

Trace Back:

/var/www/html/admin/modules/core/Core.class.php:1389 die_freepbx()
 [0]: Fields are empty

/var/www/html/admin/modules/core/functions.inc.php:4227 FreePBX\modules\Core->convertRequest2Array()
 [0]: 9876543200
 [1]: pjsip
 [2]: 2

/var/www/html/a.php:18 core_devices_add()
 [0]: 9876543200
 [1]: pjsip
 [2]: PJSIP/9876543200
 [3]: fixed
 [4]: 9876543200
 [5]: Test

I tried to add PJSIP extension but in GUI I was getting virtual device added. I need to add media encryption also. Can anyone guide me?


Solution

  • I've found it easier to do this with direct entry to the database. I haven't done anything with pjsip, but this is how I have created a sip extension. To figure it out, it's easiest to enable the general log in MySQL and record the query when you create a user. Afterwards, make sure to do a reload.

    INSERT INTO users (
        extension, password, name, voicemail, ringtimer, noanswer, recording,
        outboundcid, sipname, noanswer_cid, busy_cid, chanunavail_cid,
        noanswer_dest, busy_dest, chanunavail_dest, mohclass, auditor_exttype
    )
    VALUES (
        '12345', '', 'Test', 'novm', '0', '', '', '',
        'test', '', '', '', '', '', '', 'default', 99
    );
    
    INSERT INTO devices (
        id, tech, dial, devicetype, user, description, emergency_cid
    )
    VALUES
        ('12345', 'sip', 'SIP/12345', 'fixed', '12345', 'Test', '')
    ;
    
    INSERT INTO sip (
        id, keyword, data, flags
    )
    VALUES
        ('12345', 'secret', 'supersecre†', 2),
        ('12345', 'dtmfmode', 'rfc2833', 3),
        ('12345', 'canreinvite', 'no', 4),
        ('12345', 'context', 'from-internal', 5),
        ('12345', 'host', 'dynamic', 6),
        ('12345', 'trustrpid', 'yes', 7),
        ('12345', 'sendrpid', 'yes', 8),
        ('12345', 'type', 'friend', 9),
        ('12345', 'nat', 'yes', 10),
        ('12345', 'port', '5060', 11),
        ('12345', 'qualify', 'yes', 12),
        ('12345', 'qualifyfreq', '60', 13),
        ('12345', 'transport', 'udp', 14),
        ('12345', 'avpf', 'no', 15),
        ('12345', 'icesupport', 'no', 16),
        ('12345', 'encryption', 'yes', 17),
        ('12345', 'callgroup', '', 18),
        ('12345', 'pickupgroup', '', 19),
        ('12345', 'disallow', '', 20),
        ('12345', 'allow', '', 21),
        ('12345', 'dial', 'SIP/12345', 22),
        ('12345', 'mailbox', '12345@device', 23),
        ('12345', 'deny', '0.0.0.0/0.0.0.0', 24),
        ('12345', 'permit', '0.0.0.0/0.0.0.0', 25),
        ('12345', 'account', '12345', 26),
        ('12345', 'callerid', 'device <12345>', 27)
    ;