function gs_huntgroups_get() { # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get queues # $rs = $db->execute('SELECT `number` FROM `huntgroups` GROUP BY `number` ORDER BY `number`'); if (!$rs) { return new GsError('Error.'); } $huntgroups = array(); while ($r = $rs->fetchRow()) { $huntgroups[] = $r; } return $huntgroups; }
function gs_clir_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$user_id) { return new GsError('Unknown user.'); } # get state # $rs = $db->execute('SELECT `internal_restrict`, `external_restrict` FROM `clir` WHERE `user_id`=' . $user_id); if ($r = $rs->fetchRow()) { if (!in_array($r['internal_restrict'], array('no', 'yes', 'once'), true)) { $r['internal_restrict'] = 'no'; } if (!in_array($r['external_restrict'], array('no', 'yes', 'once'), true)) { $r['external_restrict'] = 'no'; } return $r; } return array('internal_restrict' => 'no', 'external_restrict' => 'no'); }
function gs_vm_activate($user, $source, $active) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } if (!in_array($source, array('internal', 'external'), true)) { return new GsError('Source must be internal|external.'); } $active = (bool) $active; # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$user_id) { return new GsError('Unknown user.'); } # check if has mailbox # $num = $db->executeGetOne('SELECT COUNT(*) FROM `vm` WHERE `user_id`=' . $user_id); if ($num < 1) { return new GsError('User does not have a mailbox.'); } # (de)activate # $ok = $db->execute('UPDATE `vm` SET `' . $source . '_active`=' . (int) $active . ' WHERE `user_id`=' . $user_id); if (!$ok) { return new GsError('Failed to set state.'); } return true; }
function gs_agents_get() { # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get users # $rs = $db->execute('SELECT `id`, `name`, `firstname`, `number`,`pin`,`user_id` FROM `agents` ORDER BY `name`'); if (!$rs) { return new GsError('Error.'); } $agents = array(); while ($r = $rs->fetchRow()) { $agents[] = $r; } return $agents; }
function gs_callwaiting_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$user_id) { return new GsError('Unknown user.'); } # get state # $rs = $db->execute('SELECT `active` FROM `callwaiting` WHERE `user_id`=' . $user_id); if ($r = $rs->fetchRow()) { return (bool) $r['active']; } return false; }
function gs_hosts_get($foreign = false, $group_id = null) { # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get hosts # $where = array(); if ($foreign !== null) { $where[] = '`is_foreign`=' . ($foreign ? '1' : '0'); } if ($group_id !== null) { $where[] = '`group_id`=' . (int) $group_id; } $query = 'SELECT `id`, `host`, `comment`, `is_foreign`, `group_id` FROM `hosts` ' . (count($where) === 0 ? '' : 'WHERE ' . implode(' AND ', $where)) . ' ORDER BY `is_foreign`,`host`'; $rs = $db->execute($query); if (!$rs) { return new GsError('Error.'); } $hosts = array(); while ($r = $rs->fetchRow()) { $hosts[] = $r; } return $hosts; }
function gs_queues_get() { # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get queues # $rs = $db->execute('SELECT `q`.`_id` `id`, `q`.`name`, `q`.`_title` `title`, `q`.`maxlen`, COUNT(`m`.`_user_id`) `num_members` FROM `ast_queues` `q` LEFT JOIN `ast_queue_members` `m` ON (m._queue_id=q._id) GROUP BY `q`.`_id` ORDER BY `q`.`name`'); if (!$rs) { return new GsError('Error.'); } $queues = array(); while ($r = $rs->fetchRow()) { $queues[] = $r; } return $queues; }
function gs_host_del($host, $force = FALSE) { if (!preg_match('/^[0-9\\.]+$/', $host)) { return new GsError('Host must be a numeric ID or IP address.'); } $host = gs_host_by_id_or_ip($host); if (isGsError($host)) { return new GsError($host->getMsg()); } if (!is_array($host)) { return new GsError('Cannot retrieve host ID.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } $count_users = $db->executeGetOne('SELECT COUNT(`id`) FROM `users` WHERE `host_id`=\'' . $db->escape($host['id']) . '\''); if ($count_users > 0) { return new GsError('Cannot delete host. Delete ' . $count_users . ' user(s) on this host first.'); } #delete host from all groups # gs_group_members_purge_by_type('host', array($host['id'])); # delete host # $rs = $db->execute('DELETE from `hosts` WHERE `id`=\'' . $db->escape($host['id']) . '\''); if (!$rs) { return new GsError('Could not delete host ' . $host['id']); } return true; }
function gs_screensaver_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$user_id) { return new GsError('Unknown user.'); } $screensaver = array(); # get screensaver picture's # $rs = $db->execute('SELECT `pic_nr`, `file` FROM `screensaver` WHERE `user_id`=' . $user_id . ' ORDER BY `pic_nr` ASC'); if (!$rs) { return new GsError('Failed to get screensaver pictures.'); } while ($r = $rs->fetchRow()) { $pic_nr = (int) $r['pic_nr']; $screensaver[$pic_nr] = $r['file']; } return $screensaver; }
function gs_queue_agent_del($queue_id, $agent) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $agent)) { return new GsError('User must be alphanumeric.'); } $queue_id = (int) $queue_id; if ($queue_id < 1) { return new GsError('Bad queue ID.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # check queue id # $num = (int) $db->executeGetOne('SELECT COUNT(*) FROM `ast_queues` WHERE `_id`=' . $queue_id); if ($num < 1) { return new GsError('Unknown queue ID.'); } # get agent_id # $agent_id = (int) $db->executeGetOne('SELECT `id` FROM `agents` WHERE `number`=\'' . $db->escape($agent) . '\''); if (!$agent_id) { return new GsError('Unknown agent.'); } # delete agent to the queue # $ok = $db->execute('DELETE FROM `agent_queues` WHERE `agent_id`=' . $agent_id . ' AND `queue_id`= ' . $queue_id); if (!$ok) { return new GsError('Failed to delete agent from queue.'); } return true; }
function gs_user_comment_set($user, $comment = '') { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } $comment = mb_subStr($comment, 0, 200); # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$user_id) { return new GsError('Unknown user.'); } # set comment # $ok = $db->execute('UPDATE `users` SET `user_comment`=\'' . $db->escape($comment) . '\' WHERE `id`=' . $user_id); if (!$ok) { return new GsError('Failed to set comment.'); } return true; }
function gs_user_phonemodel_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = (int) $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if ($user_id < 1) { return new GsError('Unknown user.'); } # get phone-model # $type = $db->executeGetOne('SELECT `type` FROM `phones` WHERE `user_id`=' . $user_id); if (!$type) { return 'none'; } return $type; }
function gs_huntgroup_del($hgroup_number) { $hgroup_number = (int) $hgroup_number; if ($hgroup_number < 1) { return new GsError('Bad group ID.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # check group id # $num = (int) $db->executeGetOne('SELECT COUNT(*) FROM `huntgroups` WHERE `number`=' . $hgroup_number); if ($num < 1) { return new GsError('Unknown hunt group.'); } # delete hun group # $ok = $db->execute('DELETE FROM `huntgroups` WHERE `number`=' . $hgroup_number); if (!$ok) { return new GsError('Failed to remove hunt group.'); } return true; }
function gs_agent_del($agent) { if (!preg_match('/^\\d+$/', $agent)) { return new GsError('Agent-number must be numeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $agent_id = (int) $db->executeGetOne('SELECT `id` FROM `agents` WHERE `number`=\'' . $db->escape($agent) . '\''); if (!$agent_id) { return new GsError('Unknown agent.'); } # delete agent_queues # $ok = $db->execute('DELETE FROM `agent_queues` WHERE `agent_id`=' . $agent_id); # delete user # $ok = $db->execute('DELETE FROM `agents` WHERE `id`=' . $agent_id); return true; }
function gs_agent_pause_unpause($agent_id, $pause, $reason = '') { # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `user_id` FROM `agents` WHERE `id`=\'' . $db->escape($agent_id) . '\''); if (!$user_id || $user_id <= 0) { return new GsError('Unknown agent.'); } # get sip user # $user_name = $db->executeGetOne('SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`=\'' . $db->escape($user_id) . '\''); if (!$user_name) { return new GsError('Unknown user.'); } # switch pause state # gs_queuepause_unpause($user_name, $pause, $reason); return true; }
function gs_vm_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$user_id) { return new GsError('Unknown user.'); } # get state # $rs = $db->execute('SELECT `internal_active`, `external_active` FROM `vm` WHERE `user_id`=' . $user_id); if ($r = $rs->fetchRow()) { $r['internal_active'] = (bool) $r['internal_active']; $r['external_active'] = (bool) $r['external_active']; return $r; } return array('internal_active' => false, 'external_active' => false); }
function gs_user_callerid_add($user, $number, $dest) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } if (!preg_match('/^[\\d]+$/', $number)) { return new GsError('Number must be numeric.'); } if ($dest != 'internal' && $dest != 'external') { return new GsError('No destination.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if ($user_id < 1) { return new GsError('Unknown user.'); } # add number # $ok = $db->execute('REPLACE INTO `users_callerids` (`user_id`, `number`, `dest`) VALUES (' . $user_id . ', \'' . $db->escape($number) . '\', \'' . $db->escape($dest) . '\')'); if (!$ok) { return new GsError('Failed to add callerid.'); } return true; }
function gs_users_get() { # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get users # $rs = $db->execute('SELECT `u`.`id`, `u`.`user`, `u`.`pin`, `u`.`lastname`, `u`.`firstname`, `u`.`honorific`, `u`.`email`, `s`.`name` `ext`, `s`.`callerid`, `s`.`mailbox`, `h`.`host`, `ug`.`name` `group` FROM `users` `u` JOIN `ast_sipfriends` `s` ON (`s`.`_user_id`=`u`.`id`) LEFT JOIN `hosts` `h` ON (`h`.`id`=`u`.`host_id`) LEFT JOIN `user_groups` `ug` ON (`ug`.`id`=`u`.`group_id`) ORDER BY `u`.`lastname`, `u`.`firstname`, `u`.`honorific`, `u`.`id`'); if (!$rs) { return new GsError('Error.'); } $users = array(); while ($r = $rs->fetchRow()) { $users[] = $r; } return $users; }
function gs_callblocking_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if ($user_id < 1) { return new GsError('Unknown user.'); } # get call blocking # $rs = $db->execute('SELECT `regexp`, `pin` FROM `callblocking` WHERE `user_id`=' . $user_id . ' ORDER BY LENGTH(`regexp`) DESC'); if (!$rs) { return new GsError('Failed to get outgoing call blocking entry.'); } $callblocking = array(); while ($r = $rs->fetchRow()) { $callblocking[] = $r; } return $callblocking; }
function gs_huntgroup_callforward_get($huntgroup) { if (!preg_match('/^[\\d]+$/', $huntgroup)) { return new GsError('Hunt group must be numeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get states # $sources = array('internal', 'external'); $cases = array('always', 'full', 'timeout', 'empty'); $forwards = array(); foreach ($sources as $source) { foreach ($cases as $case) { $rs = $db->execute('SELECT `active`, `number_std`, `number_var`, `timeout` FROM `huntgroups_callforwards` WHERE `huntgroup`=' . $huntgroup . ' AND `source`=\'' . $source . '\' AND `case`=\'' . $case . '\''); if ($r = $rs->fetchRow()) { if (!in_array($r['active'], array('no', 'std', 'var'), true)) { $r['active'] = 'no'; } $forwards[$source][$case] = $r; } else { $forwards[$source][$case] = array('active' => 'no', 'number_std' => '', 'number_var' => '', 'timeout' => 20); } if ($case != 'timeout') { $forwards[$source][$case]['timeout'] = null; } } } return $forwards; }
function gs_agent_update($agent, $pin = '', $name, $firstname) { if (!preg_match('/^\\d+$/', $agent)) { return new GsError('User must be numeric.'); } if (strlen($pin) > 0 && !preg_match('/^[0-9]+$/', $pin)) { return new GsError('PIN must be numeric.'); } elseif (strLen($pin) > 10) { return new GsError('PIN too long (max. 10 digits).'); } $name = preg_replace('/\\s+/', ' ', trim($name)); $firstname = preg_replace('/\\s+/', ' ', trim($firstname)); # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get agent_id # $agent_id = $db->executeGetOne('SELECT `id` FROM `agents` WHERE `number`=\'' . $db->escape($agent) . '\''); if (!$agent_id) { return new GsError('Unknown user.'); } # set PIN # $ok = $db->execute('UPDATE `agents` SET `pin`=\'' . $db->escape($pin) . '\', `name`=\'' . $db->escape($name) . '\', `firstname`=\'' . $db->escape($firstname) . '\' WHERE `id`=' . $agent_id); if (!$ok) { return new GsError('Failed to set PIN.'); } return true; }
function gs_user_watchedmissed($user_id, $queue = false) { //if (! preg_match( '/^[a-zA-Z\d]+$/', $user )) // return new GsError( 'User must be alphanumeric.' ); # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # //$user_id = $db->executeGetOne( 'SELECT `id` FROM `users` WHERE `user`=\''. $db->escape($user) .'\'' ); //if (! $user_id) // return new GsError( 'Unknown user.' ); # set comment # if (!$queue) { $ok = $db->execute('UPDATE `dial_log` SET `read`= 1 WHERE `queue_id` IS NULL AND `read`=0 AND `user_id`=' . $user_id); } else { $ok = $db->execute('UPDATE `dial_log` SET `read`= 1 WHERE `queue_id` IS NOT NULL AND `read`=0 AND `user_id`=' . $user_id); } if (!$ok) { return new GsError('Failed to set update missed calles.'); } return true; }
function gs_user_callerids_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if ($user_id < 1) { return new GsError('Unknown user.'); } # get callerids # $rs = $db->execute('SELECT `number`,`selected`, `dest` FROM `users_callerids` WHERE `user_id`=' . $user_id . ' ORDER BY `number`'); if (!$rs) { return new GsError('Failed to get callerids.'); } $numbers = array(); while ($r = $rs->fetchRow()) { $numbers[] = array('number' => $r['number'], 'dest' => $r['dest'], 'selected' => $r['selected']); } return $numbers; }
function gs_host_by_id_or_ip($id_or_ip) { # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } $id_or_ip = trim($id_or_ip); if ((string) $id_or_ip === (string) (int) $id_or_ip) { $rs = $db->execute('SELECT `id`, `host`, `comment`, `is_foreign` FROM `hosts` WHERE `id`=' . (int) $id_or_ip); } else { if (!preg_match('/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/', $id_or_ip)) { return new GsError('Unknown host.'); } $rs = $db->execute('SELECT `id`, `host`, `comment`, `is_foreign` FROM `hosts` WHERE `host`=\'' . $db->escape($id_or_ip) . '\''); } if (!$rs) { return new GsError('Failed to get host.'); } $host = $rs->fetchRow(); if (!is_array($host)) { return new GsError('Unknown host.'); } return $host; }
function gs_callforward_get_by_uid($user_id) { if (!preg_match('/^[0-9]+$/', $user_id)) { return new GsError('User ID must be numeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get states # $sources = array('internal', 'external'); $cases = array('always', 'busy', 'unavail', 'offline'); $forwards = array(); foreach ($sources as $source) { foreach ($cases as $case) { $rs = $db->execute('SELECT `active`, `number_std`, `number_var`, `number_vml`, `timeout`, `vm_rec_id` FROM `callforwards` WHERE `user_id`=' . $user_id . ' AND `source`=\'' . $source . '\' AND `case`=\'' . $case . '\''); if ($r = $rs->fetchRow()) { if (!in_array($r['active'], array('no', 'std', 'var', 'vml', 'trl', 'par'), true)) { $r['active'] = 'no'; } $forwards[$source][$case] = $r; } else { $forwards[$source][$case] = array('active' => 'no', 'number_std' => '', 'number_var' => '', 'number_vml' => '', 'timeout' => 20, 'vm_rec_id' => 0); } if ($case != 'unavail') { $forwards[$source][$case]['timeout'] = null; } } } return $forwards; }
function gs_user_ip_by_ext($ext) { if (!preg_match('/^[\\d]+$/', $ext)) { return new GsError('Extension must be numeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = (int) $db->executeGetOne('SELECT `_user_id` FROM `ast_sipfriends` WHERE `name`=\'' . $db->escape($ext) . '\''); if (!$user_id) { return new GsError('Unknown extension.'); } # get IP address # $current_ip = $db->executeGetOne('SELECT `current_ip` FROM `users` WHERE `id`=' . $user_id); if (!$current_ip) { return null; } # check if the user is actually logged in at a phone (or if that # IP address is an outdated entry) # $phone_id = (int) $db->executeGetOne('SELECT `id` FROM `phones` WHERE `user_id`=' . $user_id . ' LIMIT 1'); if ($phone_id < 1) { return null; } return $current_ip; }
function gs_user_ip_by_user($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = (int) $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$user_id) { return new GsError('Unknown user.'); } # get IP address # $current_ip = $db->executeGetOne('SELECT `current_ip` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$current_ip) { return null; } # check if the user is actually logged in at a phone (or if that # IP address is an outdated entry) # $phone_id = (int) $db->executeGetOne('SELECT `id` FROM `phones` WHERE `user_id`=' . $user_id . ' LIMIT 1'); if ($phone_id < 1) { return null; } return $current_ip; }
function gs_pickupgroup_del($pgroup_id) { $pgroup_id = (int) $pgroup_id; if ($pgroup_id < 1) { return new GsError('Bad group ID.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # check group id # $num = (int) $db->executeGetOne('SELECT COUNT(*) FROM `pickupgroups` WHERE `id`=' . $pgroup_id); if ($num < 1) { return new GsError('Unknown pickup group ID.'); } # remove users from the group # $ok = $db->execute('DELETE FROM `pickupgroups_users` WHERE `group_id`=' . $pgroup_id); if (!$ok) { return new GsError('Failed to remove users from the group.'); } $ok = $db->execute('DELETE FROM `pickupgroups` WHERE `id`=' . $pgroup_id); if (!$ok) { return new GsError('Failed to remove pickup group.'); } return true; }
function gs_keys_snom_get($user) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get user_id # $user_id = (int) $db->executeGetOne('SELECT `id` FROM `users` WHERE `user`=\'' . $db->escape($user) . '\''); if (!$user_id) { return new GsError('Unknown user.'); } # init keys array # $default_key = array('type' => 'line', 'val' => '', 'rw' => true, 'ds' => '', 'dl' => ''); $keys = array(); for ($i = 0; $i <= 53; ++$i) { $keys['f' . $i] = $default_key; } $keys['f0']['rw'] = false; # key "fkey0"/"P1" should be set to "line" # set keys for pickup groups # $rs = $db->execute('SELECT DISTINCT(`p`.`id`) `id`, `p`.`title` FROM `pickupgroups_users` `pu` JOIN `pickupgroups` `p` ON (`p`.`id`=`pu`.`group_id`) WHERE `pu`.`user_id`=' . $user_id . ' ORDER BY `p`.`id` LIMIT 6'); $k = 6; # start at key "fkey6"/"P7" while ($r = $rs->fetchRow()) { $keys['f' . $k]['type'] = 'dest'; $keys['f' . $k]['val'] = '*8*' . str_pad($r['id'], 5, '0', STR_PAD_LEFT); $keys['f' . $k]['rw'] = false; $keys['f' . $k]['ds'] = 'Grp.' . $r['id']; $keys['f' . $k]['dl'] = 'Grp. ' . (trim($r['title']) != '' ? trim($r['title']) : $r['id']); ++$k; } # get user defined keys # $rs = $db->execute('SELECT `key`, `number` FROM `softkeys` WHERE `user_id`=' . $user_id . ' AND `phone`=\'snom\''); while ($r = $rs->fetchRow()) { $key = $r['key']; if (!is_array($keys[$key])) { $keys[$key] = $default_key; } if (@$keys[$key]['rw']) { $keys[$key]['type'] = 'dest'; $keys[$key]['val'] = $r['number']; } } return $keys; }
function gs_queue_callforward_activate($queue, $source, $case, $active) { if (!preg_match('/^[\\d]+$/', $queue)) { return new GsError('Queue must be numeric.'); } if (!in_array($source, array('internal', 'external'), true)) { return new GsError('Source must be internal|external.'); } if (!in_array($case, array('always', 'full', 'timeout', 'empty'), true)) { return new GsError('Case must be always|full|timeout|empty.'); } if (!in_array($active, array('no', 'std', 'var', 'vml', 'trl', 'par'), true)) { return new GsError('Active must be no|std|var|vml|trl|par.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get queue_id # $queue_id = $db->executeGetOne('SELECT `_id` FROM `ast_queues` WHERE `name`=\'' . $db->escape($queue) . '\''); if (!$queue_id) { return new GsError('Unknown queue.'); } # check if queue has an entry # $num = $db->executeGetOne('SELECT COUNT(*) FROM `queue_callforwards` WHERE `queue_id`=' . $queue_id . ' AND `source`=\'' . $db->escape($source) . '\' AND `case`=\'' . $db->escape($case) . '\''); if ($num < 1) { $ok = $db->execute('INSERT INTO `queue_callforwards` (`queue_id`, `source`, `case`, `number_std`, `number_var`, `number_vml`, `active`) VALUES (' . $queue_id . ', \'' . $db->escape($source) . '\', \'' . $db->escape($case) . '\', \'\', \'\', \'no\')'); } else { $ok = true; } # set state # $ok = $ok && $db->execute('UPDATE `queue_callforwards` SET `active`=\'' . $db->escape($active) . '\' WHERE `queue_id`=' . $queue_id . ' AND `source`=\'' . $db->escape($source) . '\' AND `case`=\'' . $db->escape($case) . '\' LIMIT 1'); if (!$ok) { return new GsError('Failed to set call forwarding status.'); } # do not allow an empty number to be active # if ($active != 'no' && $active != 'trl' && $active != 'par') { $field = 'number_' . $active; $number = $db->executeGetOne('SELECT `' . $field . '` FROM `queue_callforwards` WHERE `queue_id`=' . $queue_id . ' AND `source`=\'' . $db->escape($source) . '\' AND `case`=\'' . $db->escape($case) . '\''); if (trim($number) == '') { $db->execute('UPDATE `queue_callforwards` SET `active`=\'no\' WHERE `queue_id`=' . $queue_id . ' AND `source`=\'' . $db->escape($source) . '\' AND `case`=\'' . $db->escape($case) . '\''); return new GsError('Number is empty. Cannot activate call forward.'); } } return true; }