| 1 |
<?php |
|---|
| 2 |
class NJManager |
|---|
| 3 |
{ |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
function findAccount($name) |
|---|
| 7 |
{ |
|---|
| 8 |
$result = null; |
|---|
| 9 |
|
|---|
| 10 |
if(($managers = manager_list()) !== null) |
|---|
| 11 |
{ |
|---|
| 12 |
|
|---|
| 13 |
foreach($managers as $manager) |
|---|
| 14 |
{ |
|---|
| 15 |
if($manager['name'] == $name ) |
|---|
| 16 |
{ |
|---|
| 17 |
$result = $manager; |
|---|
| 18 |
break; |
|---|
| 19 |
} |
|---|
| 20 |
} |
|---|
| 21 |
} |
|---|
| 22 |
return $result; |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
function createAccount($name) |
|---|
| 26 |
{ |
|---|
| 27 |
$manager = $this->findAccount($name); |
|---|
| 28 |
if ($manager == null) |
|---|
| 29 |
{ |
|---|
| 30 |
$this->addAccount($name); |
|---|
| 31 |
} |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
function deleteAccount($name) |
|---|
| 35 |
{ |
|---|
| 36 |
$manager = $this->findAccount($name); |
|---|
| 37 |
if ($manager != null) |
|---|
| 38 |
{ |
|---|
| 39 |
manager_del($name); |
|---|
| 40 |
needreload(); |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
function addAccount($name, $public=true) |
|---|
| 47 |
{ |
|---|
| 48 |
|
|---|
| 49 |
$name = $name; |
|---|
| 50 |
$secret = $this->generatePassword(12); |
|---|
| 51 |
$deny = '0.0.0.0/0.0.0.0'; |
|---|
| 52 |
if ($public == true) |
|---|
| 53 |
$permit = '127.0.0.1/255.255.255.0&10.0.0.0/255.0.0.0&172.16.0.0/255.240.0.0&192.168.0.0/255.255.0.0'; |
|---|
| 54 |
else |
|---|
| 55 |
$permit = '127.0.0.1/255.255.255.0'; |
|---|
| 56 |
|
|---|
| 57 |
// $write = 'system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate'; |
|---|
| 58 |
|
|---|
| 59 |
$read = 'call,command,command,dtmf,dialplan,originate'; |
|---|
| 60 |
$write = 'system,call,command,user,command,dtmf,dialplan,originate'; |
|---|
| 61 |
|
|---|
| 62 |
manager_add($name,$secret,$deny,$permit,$read,$write); |
|---|
| 63 |
needreload(); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
function generatePassword ($length = 8) |
|---|
| 67 |
{ |
|---|
| 68 |
|
|---|
| 69 |
$password = ""; |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
// picked for use in the password, so if you want to put vowels back in |
|---|
| 73 |
// or add special characters such as exclamation marks, this is where |
|---|
| 74 |
// you should do it |
|---|
| 75 |
$possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ"; |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
$maxlength = strlen($possible); |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
if ($length > $maxlength) { |
|---|
| 82 |
$length = $maxlength; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
$i = 0; |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
while ($i < $length) |
|---|
| 90 |
{ |
|---|
| 91 |
|
|---|
| 92 |
$char = substr($possible, mt_rand(0, $maxlength-1), 1); |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
if (!strstr($password, $char)) |
|---|
| 96 |
{ |
|---|
| 97 |
|
|---|
| 98 |
$password .= $char; |
|---|
| 99 |
|
|---|
| 100 |
$i++; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
return $password; |
|---|
| 107 |
|
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
} |
|---|