root/contributed_modules/modules/noojee/NJManager.php

Revision 13916, 2.6 kB (checked in by bsutton, 1 year ago)

First commit of Noojee's FreePBX module.
Noojee's FreePBX module provides automatic configuration for Noojee's products including Noojee Click, Noojee Receptionist and Noojee Fax (comming).

Line 
1 <?php
2 class NJManager
3 {
4     // FreePBX 2.10 + uses a class for "SOME" of the manager interface.
5     
6     function findAccount($name)
7     {
8         $result = null;
9         // Get the list of managers
10         if(($managers = manager_list()) !== null)
11         {
12             //Search for manager connection
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         // Create a default manager account
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         //     $read = 'system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate';
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         // start with a blank password
69         $password = "";
70     
71         // define possible characters - any character in this string can be
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         // we refer to the length of $possible a few times, so let's grab it now
78         $maxlength = strlen($possible);
79     
80         // check for length overflow and truncate if necessary
81         if ($length > $maxlength) {
82             $length = $maxlength;
83         }
84     
85         // set up a counter for how many characters are in the password so far
86         $i = 0;
87     
88         // add random characters to $password until $length is reached
89         while ($i < $length)
90         {
91             // pick a random character from the possible ones
92             $char = substr($possible, mt_rand(0, $maxlength-1), 1);
93     
94             // have we already used this character in $password?
95             if (!strstr($password, $char))
96             {
97                 // no, so it's OK to add it onto the end of whatever we've already got...
98                 $password .= $char;
99                 // ... and increase the counter by one
100                 $i++;
101             }
102     
103         }
104     
105         // done!
106         return $password;
107     
108     }
109     
110
111 }
Note: See TracBrowser for help on using the browser.