function InitRecordCall($filename, $index, $comment) { //FIXME $user = gs_user_get($_SESSION['sudo_user']['name']); $call = "Channel: SIP/" . $_SESSION['sudo_user']['info']['ext'] . "\n" . "MaxRetries: 0\n" . "WaitTime: 15\n" . "Context: vm-rec-multiple\n" . "Extension: webdialrecord\n" . "Callerid: {$comment} <Aufnahme>\n" . "Setvar: __user_id=" . $_SESSION['sudo_user']['info']['id'] . "\n" . "Setvar: __user_name=" . $_SESSION['sudo_user']['info']['ext'] . "\n" . "Setvar: CHANNEL(language)=" . gs_get_conf('GS_INTL_ASTERISK_LANG', 'de') . "\n" . "Setvar: __is_callfile_origin=1\n" . "Setvar: __callfile_from_user="******"\n" . "Setvar: __record_file=" . $filename . "\n"; $filename = '/tmp/gs-' . $_SESSION['sudo_user']['info']['id'] . '-' . _pack_int(time()) . rand(100, 999) . '.call'; $cf = @fOpen($filename, 'wb'); if (!$cf) { gs_log(GS_LOG_WARNING, 'Failed to write call file "' . $filename . '"'); echo 'Failed to write call file.'; die; } @fWrite($cf, $call, strLen($call)); @fClose($cf); @chmod($filename, 0666); $spoolfile = '/var/spool/asterisk/outgoing/' . baseName($filename); if (!gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { $our_host_ids = @gs_get_listen_to_ids(); if (!is_array($our_host_ids)) { $our_host_ids = array(); } $user_is_on_this_host = in_array($_SESSION['sudo_user']['info']['host_id'], $our_host_ids); } else { $user_is_on_this_host = true; } if ($user_is_on_this_host) { # the Asterisk of this user and the web server both run on this host $err = 0; $out = array(); @exec('sudo mv ' . qsa($filename) . ' ' . qsa($spoolfile) . ' 1>>/dev/null 2>>/dev/null', $out, $err); if ($err != 0) { @unlink($filename); gs_log(GS_LOG_WARNING, 'Failed to move call file "' . $filename . '" to "' . '/var/spool/asterisk/outgoing/' . baseName($filename) . '"'); echo 'Failed to move call file.'; die; } } else { $cmd = 'sudo scp -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa($filename) . ' ' . qsa('root@' . $user['host'] . ':' . $filename); //echo $cmd, "\n"; @exec($cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err); @unlink($filename); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to scp call file "' . $filename . '" to ' . $user['host']); echo 'Failed to scp call file.'; die; } //remote_exec( $user['host'], $cmd, 10, $out, $err ); // <-- does not use sudo! $cmd = 'sudo ssh -o StrictHostKeyChecking=no -o BatchMode=yes -l root ' . qsa($user['host']) . ' ' . qsa('mv ' . qsa($filename) . ' ' . qsa($spoolfile)); //echo $cmd, "\n"; @exec($cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to mv call file "' . $filename . '" on ' . $user['host'] . ' to "' . $spoolfile . '"'); echo 'Failed to mv call file on remote host.'; die; } } }
function gs_asterisks_reload($host_ids, $dialplan_only) { $dialplan_only = !!$dialplan_only; if (!$host_ids || !is_array($host_ids)) { $host_ids = false; } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get hosts # $hosts = @gs_hosts_get(); if (isGsError($hosts)) { return new GsError($hosts->getMsg()); } if (!is_array($hosts)) { return new GsError('Failed to get hosts.'); } $GS_INSTALLATION_TYPE_SINGLE = gs_get_conf('GS_INSTALLATION_TYPE_SINGLE'); if (!$GS_INSTALLATION_TYPE_SINGLE) { # get our host IDs # $our_host_ids = @gs_get_listen_to_ids(); if (isGsError($our_host_ids)) { return new GsError($our_host_ids->getMsg()); } if (!is_array($our_host_ids)) { return new GsError('Failed to get our host IDs.'); } } # are we root? do we have to sudo? # $uid = @posix_geteuid(); $uinfo = @posix_getPwUid($uid); $uname = @$uinfo['name']; $sudo = $uname == 'root' ? '' : 'sudo '; $ok = true; foreach ($hosts as $host) { if (!$host_ids || in_array($host['id'], $host_ids)) { $cmd = '/opt/gemeinschaft/sbin/start-asterisk' . ($dialplan_only ? ' --dialplan' : ''); if (!$GS_INSTALLATION_TYPE_SINGLE && !in_array($host['id'], $our_host_ids)) { # this is not the local node $cmd = $sudo . 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes -l root ' . qsa($host['host']) . ' ' . qsa($cmd); } @exec($sudo . $cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err); $ok = $ok && $err == 0; } } if (!$ok) { return new GsError('Failed to reload Asterisks.'); } return true; }
$info = $rs->fetchRow(); if (!$info) { _not_found(); } $etag = gmDate('Ymd') . '-' . md5($user_id . '-' . $fld . '-' . $file . '-' . $info['host_id'] . '-' . $info['orig_time'] . '-' . $info['dur'] . '-' . $info['cidnum']) . '-' . $fmt; $fake_filename = preg_replace('/[^0-9a-z\\-_.]/i', '', 'vm_' . $ext . '_' . date('Ymd_Hi', $info['orig_time']) . '_' . subStr(md5(date('s', $info['orig_time']) . $info['cidnum']), 0, 4) . '.' . $formats[$fmt]['ext']); if (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag) { _not_modified($etag, $attach, $fake_filename); } if ($info['dur'] > 900) { # 900 s = 15 min gs_log(GS_LOG_NOTICE, 'Voicemail too long for web.'); _server_error('File too long.'); } if (!gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { $our_host_ids = @gs_get_listen_to_ids(); if (!is_array($our_host_ids)) { gs_log(GS_LOG_WARNING, 'Failed to get our host IDs.'); _server_error('Failed to get our host IDs.'); } $vmmsg_is_on_this_host = in_array($info['host_id'], $our_host_ids, true); } else { $vmmsg_is_on_this_host = true; } $vm_dir = '/var/spool/asterisk/voicemail/'; $origorigfile = $vm_dir . 'default/' . $ext . '/' . $fld . '/' . $file . '.alaw'; $tmpfile_base = '/tmp/gs-vm-' . preg_replace('/[^0-9]/', '', $ext) . '-' . $fld . '-' . $file; # delete files like /tmp/gs-vm-* with mtime < time()-10 minutes # @exec('find \'/tmp/\' -maxdepth 1 -name \'gs-vm-*\' -type f -mmin +10 | xargs rm -f 1>>/dev/null 2>>/dev/null'); # get file from remote host if necessary
function distribute_remove($localfile) { global $errormsgs; $hostlist = gs_hosts_get(); $thishost = @gs_get_listen_to_ids(); foreach($hostlist as $currenthost) { unset($islocalhost); $islocalhost = FALSE; foreach($thishost as $hostid) { if($currenthost["id"] == $hostid) $islocalhost = TRUE; } if($islocalhost == FALSE) { unset($cmd); $cmd = "sudo ssh -o StrictHostKeyChecking=no -o BatchMode=yes -l root ". qsa($currenthost["host"]) ." ". qsa("rm ". qsa($localfile)); @exec($cmd ." 1>>/dev/null 2>>/dev/null", $out, $err); if($err != 0) { gs_log(GS_LOG_WARNING, "Failed to remove system recording '". $localfile ."' from ". $currenthost["host"]); $errormsgs[] = sprintf(__('Audiodatei kann nicht von Node %s gelöscht werden'), $currenthost["host"]); } } } }
function gs_asterisks_prune_peer($peer, $host_ids = false) { if (!$host_ids || !is_array($host_ids)) { $host_ids = false; } # check peer if ($peer === 'all' || $peer == '') { $peer = 'all'; } elseif (!preg_match('/^[1-9][0-9]{1,9}$/', $peer)) { return new GsError('Invalid peer name.'); } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } # get hosts # $hosts = @gs_hosts_get(); if (isGsError($hosts)) { return new GsError($hosts->getMsg()); } if (!is_array($hosts)) { return new GsError('Failed to get hosts.'); } $GS_INSTALLATION_TYPE_SINGLE = gs_get_conf('GS_INSTALLATION_TYPE_SINGLE'); if (!$GS_INSTALLATION_TYPE_SINGLE) { # get our host IDs # $our_host_ids = @gs_get_listen_to_ids(); if (isGsError($our_host_ids)) { return new GsError($our_host_ids->getMsg()); } if (!is_array($our_host_ids)) { return new GsError('Failed to get our host IDs.'); } } # are we root? do we have to sudo? # $uid = @posix_geteuid(); $uinfo = @posix_getPwUid($uid); $uname = @$uinfo['name']; $sudo = $uname == 'root' ? '' : 'sudo '; $ok = true; foreach ($hosts as $host) { if (!$host_ids || in_array($host['id'], $host_ids)) { $cmd = 'asterisk -rx \'sip prune realtime ' . $peer . '\' '; if (!$GS_INSTALLATION_TYPE_SINGLE && !in_array($host['id'], $our_host_ids)) { # this is not the local node $cmd = $sudo . 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes -l root ' . qsa($host['host']) . ' ' . qsa($cmd); } @exec($sudo . $cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err); $ok = $ok && $err == 0; } } if (!$ok) { return new GsError('Failed to prune peer "' . $peer . '".'); } return true; }
function gs_callforward_activate($user, $source, $case, $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.'); } if (!in_array($case, array('always', 'busy', 'unavail', 'offline'), true)) { return new GsError('Case must be always|busy|unavail|offline.'); } if (!in_array($active, array('no', 'std', 'var', 'vml', 'ano', 'trl', 'par'), true)) { return new GsError('Active must be no|std|var|vml|ano|trl|par.'); } # 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 user_ext # $user_ext = $db->executeGetOne('SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`=\'' . $db->escape($user_id) . '\''); if (!$user_ext) { return new GsError('Unknown user extension.'); } # check if user has an entry # $num = $db->executeGetOne('SELECT COUNT(*) FROM `callforwards` WHERE `user_id`=' . $user_id . ' AND `source`=\'' . $db->escape($source) . '\' AND `case`=\'' . $db->escape($case) . '\''); if ($num < 1) { $ok = $db->execute('INSERT INTO `callforwards` (`user_id`, `source`, `case`, `number_std`, `number_var`, `number_vml`, `active`) VALUES (' . $user_id . ', \'' . $db->escape($source) . '\', \'' . $db->escape($case) . '\', \'\', \'\', \'\', \'no\')'); } else { $ok = true; } # do not allow time rules if no time rules are defined # if ($active == 'trl') { $id = (int) $db->executeGetOne('SELECT `_user_id` from `cf_timerules` WHERE `_user_id`=' . $user_id); if (!$id) { return new GsError('No time rules defined. Cannot activate call forward.'); } } else { if ($active == 'par') { $id = (int) $db->executeGetOne('SELECT `_user_id` from `cf_parallelcall` WHERE `_user_id`=' . $user_id); if (!$id) { return new GsError('No parsllel call tragets. Cannot activate call forward.'); } } } # set state # $ok = $ok && $db->execute('UPDATE `callforwards` SET `active`=\'' . $db->escape($active) . '\' WHERE `user_id`=' . $user_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 == 'std' || $active == 'var') { $field = 'number_' . $active; $number = $db->executeGetOne('SELECT `' . $field . '` FROM `callforwards` WHERE `user_id`=' . $user_id . ' AND `source`=\'' . $db->escape($source) . '\' AND `case`=\'' . $db->escape($case) . '\''); if (trim($number) == '') { $db->execute('UPDATE `callforwards` SET `active`=\'no\' WHERE `user_id`=' . $user_id . ' AND `source`=\'' . $db->escape($source) . '\' AND `case`=\'' . $db->escape($case) . '\''); return new GsError('Number is empty. Cannot activate call forward.'); } } if ($case === 'always') { $filename = '/tmp/gs-' . $user_id . '-' . time() . '-' . rand(10000, 99999) . '.call'; $call = "Channel: local/toggle@toggle-cfwd-hint\n" . "MaxRetries: 0\n" . "WaitTime: 15\n" . "Context: toggle-cfwd-hint\n" . "Extension: toggle\n" . "Callerid: {$user} <Toggle>\n" . "Setvar: __user_id=" . $user_id . "\n" . "Setvar: __user_name=" . $user_ext . "\n" . "Setvar: CHANNEL(language)=" . gs_get_conf('GS_INTL_ASTERISK_LANG', 'de') . "\n" . "Setvar: __is_callfile_origin=1\n" . "Setvar: __callfile_from_user="******"\n" . "Setvar: __record_file=" . $filename . "\n"; $cf = @fOpen($filename, 'wb'); if (!$cf) { gs_log(GS_LOG_WARNING, 'Failed to write call file "' . $filename . '"'); return new GsError('Failed to write call file.'); } @fWrite($cf, $call, strLen($call)); @fClose($cf); @chmod($filename, 0666); $spoolfile = '/var/spool/asterisk/outgoing/' . baseName($filename); if (!gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { $our_host_ids = @gs_get_listen_to_ids(); if (!is_array($our_host_ids)) { $our_host_ids = array(); } $user_is_on_this_host = in_array($_SESSION['sudo_user']['info']['host_id'], $our_host_ids); } else { $user_is_on_this_host = true; } if ($user_is_on_this_host) { # the Asterisk of this user and the web server both run on this host $err = 0; $out = array(); @exec('sudo mv ' . qsa($filename) . ' ' . qsa($spoolfile) . ' 1>>/dev/null 2>>/dev/null', $out, $err); if ($err != 0) { @unlink($filename); gs_log(GS_LOG_WARNING, 'Failed to move call file "' . $filename . '" to "' . $spoolfile . '"'); return new GsError('Failed to move call file.'); } } else { $cmd = 'sudo scp -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa($filename) . ' ' . qsa('root@' . $user['host'] . ':' . $filename); //echo $cmd, "\n"; @exec($cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err); @unlink($filename); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to scp call file "' . $filename . '" to ' . $user['host']); return new GsError('Failed to scp call file.'); } //remote_exec( $user['host'], $cmd, 10, $out, $err ); // <-- does not use sudo! $cmd = 'sudo ssh -o StrictHostKeyChecking=no -o BatchMode=yes -l root ' . qsa($user['host']) . ' ' . qsa('mv ' . qsa($filename) . ' ' . qsa($spoolfile)); //echo $cmd, "\n"; @exec($cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to mv call file "' . $filename . '" on ' . $user['host'] . ' to "' . $spoolfile . '"'); return new GsError('Failed to mv call file on remote host.'); } } } return true; }
function gs_callwaiting_activate($user, $active) { if (!preg_match('/^[a-z0-9\\-_.]+$/', $user)) { return new GsError('User must be alphanumeric.'); } $active = !!$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.'); } # get user_ext $user_ext = $db->executeGetOne('SELECT `s`.`name` `ext` FROM `users` `u` JOIN `ast_sipfriends` `s` ON (`s`.`_user_id`=`u`.`id`) WHERE `u`.`user`=\'' . $db->escape($user) . '\''); if (!$user_ext) { return new GsError('Unknown user.'); } # (de)activate # $num = $db->executeGetOne('SELECT COUNT(*) FROM `callwaiting` WHERE `user_id`=' . $user_id); if ($num < 1) { $ok = $db->execute('INSERT INTO `callwaiting` (`user_id`, `active`) VALUES (' . $user_id . ', 0)'); } else { $ok = true; } $ok = $ok && $db->execute('UPDATE `callwaiting` SET `active`=' . (int) $active . ' WHERE `user_id`=' . $user_id); if (!$ok) { return new GsError('Failed to set call waiting.'); } $call = "Channel: local/toggle@toggle-cwait-hint\n" . "MaxRetries: 0\n" . "WaitTime: 15\n" . "Context: toggle-cwait-hint\n" . "Extension: toggle\n" . "Callerid: {$user} <Toggle>\n" . "Setvar: __user_id=" . $user_id . "\n" . "Setvar: __user_name=" . $user_ext . "\n" . "Setvar: CHANNEL(language)=" . gs_get_conf('GS_INTL_ASTERISK_LANG', 'de') . "\n" . "Setvar: __is_callfile_origin=1\n" . "Setvar: __callfile_from_user="******"\n" . "Setvar: __record_file=" . $filename . "\n"; $filename = '/tmp/gs-' . $user_id . '-' . time() . '-' . rand(10000, 99999) . '.call'; $cf = @fOpen($filename, 'wb'); if (!$cf) { gs_log(GS_LOG_WARNING, 'Failed to write call file "' . $filename . '"'); return new GsError('Failed to write call file.'); } @fWrite($cf, $call, strLen($call)); @fClose($cf); @chmod($filename, 0666); $spoolfile = '/var/spool/asterisk/outgoing/' . baseName($filename); if (!gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { $our_host_ids = @gs_get_listen_to_ids(); if (!is_array($our_host_ids)) { $our_host_ids = array(); } $user_is_on_this_host = in_array($_SESSION['sudo_user']['info']['host_id'], $our_host_ids); } else { $user_is_on_this_host = true; } if ($user_is_on_this_host) { # the Asterisk of this user and the web server both run on this host $err = 0; $out = array(); @exec('sudo mv ' . qsa($filename) . ' ' . qsa($spoolfile) . ' 1>>/dev/null 2>>/dev/null', $out, $err); if ($err != 0) { @unlink($filename); gs_log(GS_LOG_WARNING, 'Failed to move call file "' . $filename . '" to "' . $spoolfile . '"'); return new GsError('Failed to move call file.'); } } else { $cmd = 'sudo scp -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa($filename) . ' ' . qsa('root@' . $user['host'] . ':' . $filename); //echo $cmd, "\n"; @exec($cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err); @unlink($filename); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to scp call file "' . $filename . '" to ' . $user['host']); return new GsError('Failed to scp call file.'); } //remote_exec( $user['host'], $cmd, 10, $out, $err ); // <-- does not use sudo! $cmd = 'sudo ssh -o StrictHostKeyChecking=no -o BatchMode=yes -l root ' . qsa($user['host']) . ' ' . qsa('mv ' . qsa($filename) . ' ' . qsa($spoolfile)); //echo $cmd, "\n"; @exec($cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to mv call file "' . $filename . '" on ' . $user['host'] . ' to "' . $spoolfile . '"'); return new GsError('Failed to mv call file on remote host.'); } } # reload phone config # //$user_name = $db->executeGetOne( 'SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`='. $user_id ); //@ exec( 'asterisk -rx \'sip notify snom-reboot '. $user_name .'\'' ); //@ gs_prov_phone_checkcfg_by_user( $user, false ); //FIXME return true; }
function gs_get_listen_to_primary_id() { $ids = @gs_get_listen_to_ids(true); if (!is_array($ids)) { gs_log(GS_LOG_DEBUG, "Failed to get our primary IP address"); return null; } if (count($ids) < 1) { gs_log(GS_LOG_DEBUG, "Failed to get our primary IP address"); return null; } return $ids[0]; }