| 1 |
#!/usr/bin/php -q |
|---|
| 2 |
<?php |
|---|
| 3 |
|
|---|
| 4 |
require_once "phpagi.php"; |
|---|
| 5 |
require_once "phpagi-asmanager.php"; |
|---|
| 6 |
|
|---|
| 7 |
/* Usage: |
|---|
| 8 |
* |
|---|
| 9 |
* AGI(user_login_out.agi,login|logout,[user]) |
|---|
| 10 |
* |
|---|
| 11 |
* ARG1: action |
|---|
| 12 |
* ARG2: device |
|---|
| 13 |
* ARG3: user |
|---|
| 14 |
* |
|---|
| 15 |
* login: |
|---|
| 16 |
* Login the requested user and set hints appropriately as well as adjust |
|---|
| 17 |
* hints on any previous user that was logged in. |
|---|
| 18 |
* |
|---|
| 19 |
* logout: |
|---|
| 20 |
* Logout the current user and set hints appropriately for that user |
|---|
| 21 |
* as well as logging in the default user (if applicable) and set hints |
|---|
| 22 |
* accordingly |
|---|
| 23 |
*/ |
|---|
| 24 |
|
|---|
| 25 |
$agi = new AGI(); |
|---|
| 26 |
|
|---|
| 27 |
// get manager credentials out of the channel |
|---|
| 28 |
// |
|---|
| 29 |
$ampmgruser = get_var( "AMPMGRUSER" ); |
|---|
| 30 |
$ampmgrpass = get_var( "AMPMGRPASS" ); |
|---|
| 31 |
$astspooldir = get_var( "ASTSPOOLDIR" ); |
|---|
| 32 |
/* |
|---|
| 33 |
$ampmgruser = 'admin'; |
|---|
| 34 |
$ampmgrpass = 'amp111'; |
|---|
| 35 |
*/ |
|---|
| 36 |
$astman = new AGI_AsteriskManager( ); |
|---|
| 37 |
if (!$astman->connect("127.0.0.1", $ampmgruser , $ampmgrpass)) { |
|---|
| 38 |
exit (1); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
$action = get_action(); |
|---|
| 42 |
$this_device = get_this_device(); |
|---|
| 43 |
|
|---|
| 44 |
switch ($action) { |
|---|
| 45 |
case 'login': |
|---|
| 46 |
case 'logon': |
|---|
| 47 |
$new_user = get_login_user(); |
|---|
| 48 |
debug("Logging in user $new_user to device $this_device"); |
|---|
| 49 |
user_login($this_device, $new_user); |
|---|
| 50 |
break; |
|---|
| 51 |
case 'logout': |
|---|
| 52 |
debug("Logging out current user from device $this_device"); |
|---|
| 53 |
user_logout($this_device); |
|---|
| 54 |
break; |
|---|
| 55 |
default: |
|---|
| 56 |
debug("Got unknown action: $action, exiting"); |
|---|
| 57 |
} |
|---|
| 58 |
exit; |
|---|
| 59 |
|
|---|
| 60 |
/* |
|---|
| 61 |
exten => s,n,System(rm -f ${ASTSPOOLDIR}/voicemail/device/${CALLERID(number)}) |
|---|
| 62 |
exten => s,n,System(/bin/ln -s ${ASTSPOOLDIR}/voicemail/${DB(AMPUSER/${AMPUSER}/voicemail)}/${AMPUSER}/ ${ASTSPOOLDIR}/voicemail/device/${CALLERID(number)}) |
|---|
| 63 |
*/ |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
// Get the requested action (login or logout) |
|---|
| 67 |
// |
|---|
| 68 |
function get_action() { |
|---|
| 69 |
global $argv; |
|---|
| 70 |
return strtolower(trim($argv['1'])); |
|---|
| 71 |
//return get_var('ARG1'); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
// Get the device passed in (basically CID) |
|---|
| 75 |
// |
|---|
| 76 |
function get_this_device() { |
|---|
| 77 |
global $argv; |
|---|
| 78 |
return trim($argv['2']); |
|---|
| 79 |
//return get_var('ARG2'); |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
// Get the user to login to a device |
|---|
| 83 |
// |
|---|
| 84 |
function get_login_user() { |
|---|
| 85 |
global $argv; |
|---|
| 86 |
return trim($argv['3']); |
|---|
| 87 |
//return get_var('ARG3'); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
// Login a new user to a device. If there is a current user |
|---|
| 91 |
// log them out first. |
|---|
| 92 |
// |
|---|
| 93 |
function user_login($this_device, $new_user) { |
|---|
| 94 |
debug("user_login: this_device: $this_device, new_user: $new_user",8); |
|---|
| 95 |
|
|---|
| 96 |
$current_user = get_logged_in_user($this_device); |
|---|
| 97 |
if ($current_user != $new_user) { |
|---|
| 98 |
if ($current_user != '') { |
|---|
| 99 |
debug("Logging out current user $current_user from device $this_device so $new_user can login",5); |
|---|
| 100 |
remove_user($this_device); |
|---|
| 101 |
} |
|---|
| 102 |
insert_user($new_user, $this_device); |
|---|
| 103 |
} else { |
|---|
| 104 |
debug("User $new_user is already logged into device $this_device",5); |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
// Logout the current user. If device has a default user, log |
|---|
| 109 |
// them in. |
|---|
| 110 |
// |
|---|
| 111 |
function user_logout($this_device) { |
|---|
| 112 |
debug("user_logout: this_device: $this_device",8); |
|---|
| 113 |
|
|---|
| 114 |
$current_user = get_logged_in_user($this_device); |
|---|
| 115 |
$default_user = get_default_user($this_device); |
|---|
| 116 |
|
|---|
| 117 |
debug("current_user: $current_user, default_user: $default_user",8); |
|---|
| 118 |
|
|---|
| 119 |
if ($current_user != $default_user) { |
|---|
| 120 |
remove_user($this_device); |
|---|
| 121 |
if ($default_user != '') { |
|---|
| 122 |
debug("Logging in default user $default_user to device $this_device",5); |
|---|
| 123 |
insert_user($default_user, $this_device); |
|---|
| 124 |
} |
|---|
| 125 |
} |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
// Insert a specificed user into a specified device and then |
|---|
| 129 |
// update the hint for that user to reflect the new device(s) |
|---|
| 130 |
// and update the voicemial link |
|---|
| 131 |
// |
|---|
| 132 |
function insert_user($user, $device) { |
|---|
| 133 |
debug("insert_user: user: $user, device: $device",8); |
|---|
| 134 |
global $agi; |
|---|
| 135 |
global $astspooldir; |
|---|
| 136 |
|
|---|
| 137 |
set_device_user($device, $user); |
|---|
| 138 |
$previous_devices = get_devices($user); |
|---|
| 139 |
$new_devices = insert_device($previous_devices,$device); |
|---|
| 140 |
debug("insert_user: Setting user $user to devices $new_devices",5); |
|---|
| 141 |
set_user_devices($user, $new_devices); |
|---|
| 142 |
set_hint($user, $new_devices); |
|---|
| 143 |
$vmcontext = get_voicemail_context($user); |
|---|
| 144 |
if ($vmcontext != 'novm') { |
|---|
| 145 |
exec("/bin/ln -s {$astspooldir}/voicemail/{$vmcontext}/{$user}/ {$astspooldir}/voicemail/device/$device", $output, $ret); |
|---|
| 146 |
if ($ret) { |
|---|
| 147 |
debug("Got Return code: $ret trying to: /bin/ln -s {$astspooldir}/voicemail/{$vmcontext}/{$user}/ {$astspooldir}/voicemail/device/$device",5); |
|---|
| 148 |
} |
|---|
| 149 |
} |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
// Remove the current user from a device and then update |
|---|
| 153 |
// the hint of that current user to reflect their current devices |
|---|
| 154 |
// |
|---|
| 155 |
function remove_user($device) { |
|---|
| 156 |
debug("remove_user: device: $device",8); |
|---|
| 157 |
global $agi; |
|---|
| 158 |
global $astspooldir; |
|---|
| 159 |
|
|---|
| 160 |
$current_user = get_user($device); |
|---|
| 161 |
if ($current_user != '') { |
|---|
| 162 |
$current_devices = get_devices($current_user); |
|---|
| 163 |
$new_devices = remove_device($current_devices,$device); |
|---|
| 164 |
debug("remove_user: Setting user $current_user to devices $new_devices",5); |
|---|
| 165 |
set_user_devices($current_user, $new_devices); |
|---|
| 166 |
set_hint($current_user, $new_devices); |
|---|
| 167 |
exec("/bin/rm -f {$astspooldir}/voicemail/device/$device",$output, $ret); |
|---|
| 168 |
if ($ret) { |
|---|
| 169 |
debug("Got Return code: $ret trying to remove: {$astspooldir}/voicemail/device/$device",5); |
|---|
| 170 |
} |
|---|
| 171 |
set_device_user($device, 'none'); |
|---|
| 172 |
} |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
// Set the hint for a user based on the devices in their AMPUSER object |
|---|
| 176 |
// |
|---|
| 177 |
function set_hint($user, $devices) { |
|---|
| 178 |
debug("set_hint: user: $user, devices: $devices",8); |
|---|
| 179 |
global $astman; |
|---|
| 180 |
global $agi; |
|---|
| 181 |
|
|---|
| 182 |
if ($devices) { |
|---|
| 183 |
$dial_string = get_dial_string($devices); |
|---|
| 184 |
debug("Setting user $user hint to $dial_string",5); |
|---|
| 185 |
$response = $astman->send_request('Command',array('Command'=>"add extension {$user},hint,{$dial_string} into ext-local replace")); |
|---|
| 186 |
} else { |
|---|
| 187 |
debug("Removing hint for user $user",5); |
|---|
| 188 |
$response = $astman->send_request('Command',array('Command'=>"remove extension {$user}@ext-local hint")); |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
// Get the actual technology dialstrings from the DEVICE objects (used |
|---|
| 193 |
// to create proper hints) |
|---|
| 194 |
// |
|---|
| 195 |
function get_dial_string($devices) { |
|---|
| 196 |
debug("get_dial_string: devices: $devices",8); |
|---|
| 197 |
global $agi; |
|---|
| 198 |
|
|---|
| 199 |
$device_array = explode( '&', $devices ); |
|---|
| 200 |
foreach ($device_array as $adevice) { |
|---|
| 201 |
$dds = $agi->database_get('DEVICE',$adevice.'/dial'); |
|---|
| 202 |
$dialstring .= $dds['data'].'&'; |
|---|
| 203 |
} |
|---|
| 204 |
return trim($dialstring," &"); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
// Insert a new device into a devices string and return the new string |
|---|
| 208 |
// |
|---|
| 209 |
function insert_device($devices, $new_device) { |
|---|
| 210 |
debug("insert_device: devices: $devices, new_device: $new_device",8); |
|---|
| 211 |
|
|---|
| 212 |
// We could just append it but this assures no bugs duplicate the device |
|---|
| 213 |
// |
|---|
| 214 |
if (trim($new_device) != '') { |
|---|
| 215 |
$device_arr = explode('&',$devices); |
|---|
| 216 |
$device_arr[] = $new_device; |
|---|
| 217 |
$device_arr = array_unique($device_arr); |
|---|
| 218 |
return implode('&',$device_arr); |
|---|
| 219 |
} else { |
|---|
| 220 |
return $devices; |
|---|
| 221 |
} |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
// Remove a new device from a devices string and return the new string |
|---|
| 225 |
// |
|---|
| 226 |
function remove_device($devices, $remove_device) { |
|---|
| 227 |
debug("remove_device: devices: $devices, remove_device: $remove_device",8); |
|---|
| 228 |
|
|---|
| 229 |
$device_arr = explode('&',$devices); |
|---|
| 230 |
$device_arr_hash = array_flip($device_arr); |
|---|
| 231 |
unset($device_arr_hash[$remove_device]); |
|---|
| 232 |
$new_device_arr = array_flip($device_arr_hash); |
|---|
| 233 |
$new_devices = implode('&', $new_device_arr); |
|---|
| 234 |
return $new_devices; |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
// Get the currently logged in user of a device returning blank |
|---|
| 238 |
// in no logins |
|---|
| 239 |
// |
|---|
| 240 |
function get_logged_in_user($device) { |
|---|
| 241 |
debug("get_logged_in_user: device: $device",8); |
|---|
| 242 |
global $agi; |
|---|
| 243 |
|
|---|
| 244 |
$user = $agi->database_get('DEVICE',$device.'/user'); |
|---|
| 245 |
if ($user['result'] == 1 && trim($user['data']) != 'none') { |
|---|
| 246 |
debug("get_logged_in_user: got user: ".$user['data'],8); |
|---|
| 247 |
return trim($user['data']); |
|---|
| 248 |
} |
|---|
| 249 |
return ''; |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
// Get the designated default user for the device or blank if none |
|---|
| 253 |
// |
|---|
| 254 |
function get_default_user($device) { |
|---|
| 255 |
debug("get_default_user: device: $device",8); |
|---|
| 256 |
global $agi; |
|---|
| 257 |
|
|---|
| 258 |
$default_user = $agi->database_get('DEVICE',$device.'/default_user'); |
|---|
| 259 |
if ($default_user['result'] == 1 && trim($default_user['data']) != 'none') { |
|---|
| 260 |
return trim($default_user['data']); |
|---|
| 261 |
} |
|---|
| 262 |
return ''; |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
// Get the list of current devices for this user |
|---|
| 266 |
// |
|---|
| 267 |
function get_devices($user) { |
|---|
| 268 |
debug("get_devices: user: $user", 8); |
|---|
| 269 |
global $agi; |
|---|
| 270 |
|
|---|
| 271 |
$devices = $agi->database_get('AMPUSER',$user.'/device'); |
|---|
| 272 |
if ($devices['result'] == 1) { |
|---|
| 273 |
debug("get_devices: got: ".$devices['data'], 8); |
|---|
| 274 |
return trim($devices['data']); |
|---|
| 275 |
} |
|---|
| 276 |
return ''; |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
// Get the voicemail context for this user |
|---|
| 280 |
// |
|---|
| 281 |
function get_voicemail_context($user) { |
|---|
| 282 |
debug("get_voicemail_context: user: $user", 8); |
|---|
| 283 |
global $agi; |
|---|
| 284 |
|
|---|
| 285 |
$devices = $agi->database_get('AMPUSER',$user.'/voicemail'); |
|---|
| 286 |
if ($devices['result'] == 1) { |
|---|
| 287 |
return trim($devices['data']); |
|---|
| 288 |
} |
|---|
| 289 |
return 'novm'; |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
// Get the user currently associated with this device or blank if none |
|---|
| 293 |
// |
|---|
| 294 |
function get_user($device) { |
|---|
| 295 |
debug("get_user: device: $device", 8); |
|---|
| 296 |
global $agi; |
|---|
| 297 |
|
|---|
| 298 |
$user = $agi->database_get('DEVICE',$device.'/user'); |
|---|
| 299 |
if ($user['result'] == 1 && trim($user['data']) != 'none') { |
|---|
| 300 |
debug("get_user: got: ".$user['data'], 8); |
|---|
| 301 |
return trim($user['data']); |
|---|
| 302 |
} |
|---|
| 303 |
debug("get_user: no user found", 8); |
|---|
| 304 |
return ''; |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
// Inserts device info into AMPUSER object for specificed user |
|---|
| 308 |
// |
|---|
| 309 |
function set_user_devices($user, $devices) { |
|---|
| 310 |
debug("set_user_devices: user: $user, devices: $devices", 8); |
|---|
| 311 |
global $agi; |
|---|
| 312 |
if (empty($devices)) { |
|---|
| 313 |
debug("No more devices associated with $user, deletting /device key", 8); |
|---|
| 314 |
$agi->database_del('AMPUSER',$user.'/device'); |
|---|
| 315 |
} else { |
|---|
| 316 |
debug("set_user_devices: assigning $devices to $user /device key", 8); |
|---|
| 317 |
$agi->database_put('AMPUSER',$user.'/device',$devices); |
|---|
| 318 |
} |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
// Inserts user into DEVICE object for specified device |
|---|
| 322 |
// |
|---|
| 323 |
function set_device_user($device, $user) { |
|---|
| 324 |
debug("set_device_user: device: $device, user: $user", 8); |
|---|
| 325 |
global $agi; |
|---|
| 326 |
$agi->database_put('DEVICE',$device.'/user',$user); |
|---|
| 327 |
} |
|---|
| 328 |
|
|---|
| 329 |
// Get a channel variable |
|---|
| 330 |
// |
|---|
| 331 |
function get_var($value) { |
|---|
| 332 |
global $agi; |
|---|
| 333 |
|
|---|
| 334 |
$r = $agi->get_variable( $value ); |
|---|
| 335 |
if ($r['result'] == 1) { |
|---|
| 336 |
$result = $r['data']; |
|---|
| 337 |
return trim($result); |
|---|
| 338 |
} |
|---|
| 339 |
return ''; |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
function debug($string, $level=3) { |
|---|
| 343 |
global $agi; |
|---|
| 344 |
$agi->verbose($string, $level); |
|---|
| 345 |
} |
|---|