function _getHostByAddr_timeout($ip, $timeout = 2, $fallback = false) { static $host_bin = null; if ($host_bin === null) { $host_bin = find_executable('host', array('/usr/bin/', '/usr/sbin/', '/bin/', '/sbin/')); if (empty($host_bin)) { $host_bin = false; } } if ($host_bin) { $err = 0; $out = array(); exec('LANG=C ' . $host_bin . ' -W ' . (int) abs($timeout) . ' ' . qsa($ip) . ' 2>>/dev/null', $out, $err); if ($err == 0) { if (preg_match('/pointer ([a-z0-9.\\-_]+)/i', implode("\n", $out), $m)) { $host = $m[1]; if (subStr($host, -1) === '.') { $host = subStr($host, 0, -1); } return $host; } } } else { if ($fallback) { $host = getHostByAddr($ip); if (empty($host) || $host == $ip) { return false; } return $host; } } return false; }
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; }
function gs_user_logout($user, $reboot = true) { $ret = gs_user_is_valid_name($user); if (isGsError($ret)) { return $ret; } elseif (!$ret) { return new GsError('Invalid username.'); } # 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.'); } $ip_addr = $db->executeGetOne('SELECT `current_ip` FROM `users` WHERE `id`=' . $user_id); $rs = $db->execute('SELECT `id`, `mac_addr`, `nobody_index` FROM `phones` WHERE `user_id`=' . $user_id); while ($phone = $rs->fetchRow()) { # assign the default nobody # $phone['nobody_index'] = (int) $phone['nobody_index']; if ($phone['nobody_index'] < 1) { $new_user_id = null; } else { $new_user_id = (int) $db->executeGetOne('SELECT `id` FROM `users` WHERE `nobody_index`=' . $phone['nobody_index']); if ($new_user_id < 1) { //? } } $db->execute('UPDATE `phones` SET `user_id`=' . ($new_user_id > 0 ? $new_user_id : 'NULL') . ' WHERE `id`=' . (int) $phone['id'] . ' AND `user_id`=' . $user_id); } # log out of all queues # $user_ext = $db->executeGetOne('SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`=' . $user_id); $user_ext = preg_replace('/[^0-9]/', '', $user_ext); if ($user_ext != '') { ob_start(); @exec(GS_DIR . 'dialplan-scripts/fake-agi-env.php' . ' ' . qsa(GS_DIR . 'dialplan-scripts/queue-login-logout.agi') . ' ' . qsa($user_ext) . ' 0 logoutall 1>>/dev/null 2>>/dev/null'); ob_end_clean(); } # restart phone # if ($ip_addr != '') { $ret = @gs_prov_phone_checkcfg_by_ip($ip_addr, $reboot); } if (isGsError($ret)) { gs_script_error($ret->getMsg()); } return true; }
function conv_ringtone($infile, $outbase) { $outfile = $outbase . '.wav'; if (is_executable('/usr/local/bin/mpg123')) { $mpg123 = '/usr/local/bin/mpg123'; } elseif (is_executable('/usr/bin/mpg123')) { $mpg123 = '/usr/bin/mpg123'; } elseif (is_executable('/bin/mpg123')) { $mpg123 = '/bin/mpg123'; } else { $mpg123 = 'mpg123'; } if (strToLower(subStr($infile, -4, 4)) === '.mp3') { # convert mp3 to wav first $wavfile = $infile . '.wav'; $cmd = $mpg123 . ' -m -w - -n 1000 -q ' . qsa($infile) . ' > ' . qsa($wavfile) . ' 2>>/dev/null'; # cuts file after 1000 frames (around 2.3 MB, depending on the rate) # don't use -r 8000 as that doesn't really work for VBR encoded MP3s @exec($cmd, $out, $err); if ($err != 0) { if (is_file($wavfile)) { @unlink($wavfile); } return false; } $infile = $wavfile; $rm_tmp = $wavfile; } else { $rm_tmp = false; } $cmd = 'sox ' . qsa($infile) . ' -c 1 -U ' . qsa($outfile) . ' rate 8000 trim 0 200000s 2>>/dev/null'; # WAV, uLaw, 8 kHz, 16 bit, mono # "The time for loading the file should not be longer then 3 seconds. # Size < 200 KByte." # cuts file after 200000 samples (around 200 kB) @exec($cmd, $out, $err); if ($err != 0) { # $err == 2 would be unknown format if (is_file($outfile)) { @unlink($outfile); } if ($rm_tmp && is_file($rm_tmp)) { @unlink($rm_tmp); } return false; } return $outfile; //return false; //return null; # not implemented }
function remote_exec($host, $cmd, $timeout = 10, &$out, &$err) { $host = trim($host); if ($host == '') { return false; } $cmd = trim($cmd); if ($cmd == '') { return false; } $full_cmd = GS_DIR . 'sbin/remote-exec-do ' . qsa($host) . ' ' . qsa($cmd) . ' ' . (int) $timeout; @exec($full_cmd, $buf_out, $buf_err); $out = $buf_out; $err = (int) $buf_err; return true; }
function gs_mysql_find_socket($db_host) { $socket = null; # never use socket for remote databases if (!in_array((string) $db_host, array('127.0.0.1', 'localhost', ''), true)) { return null; } /* wo der Socket liegt findet man so heraus: mysqladmin variables | grep sock oder es steht auch in der MySQL-Konfiguration: cat /etc/my.cnf | grep sock bzw. cat /etc/mysql/my.cnf | grep sock */ $err = 0; $out = array(); @exec('sed -e ' . qsa('/^\\[\\(mysqld_safe\\|safe_mysqld\\)\\]/,/^\\[/!d') . ' /etc/mysql/my.cnf 2>>/dev/null | grep \'^socket\' 2>>/dev/null', $out, $err); // Debian if ($err === 0) { $socket = _grep_mysql_socket(implode("\n", $out)); } if ($socket === null) { $err = 0; $out = array(); @exec('sed -e ' . qsa('/^\\[\\(mysqld_safe\\|safe_mysqld\\)\\]/,/^\\[/!d') . ' /etc/my.cnf 2>>/dev/null | grep \'^socket\' 2>>/dev/null', $out, $err); // CentOS if ($err === 0) { $socket = _grep_mysql_socket(implode("\n", $out)); } if ($socket === null) { $err = 0; $out = array(); @exec('mysqladmin -s variables | grep socket 2>>/dev/null', $out, $err); // should work everywhere if mysqladmin is available if ($err === 0) { $socket = _grep_mysql_socket(implode("\n", $out)); } if ($socket === null) { gs_log(GS_LOG_WARNING, 'Could not find MySQL socket'); } } } return $socket !== null ? $socket : null; }
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; }
`key_7_value` = \'' . $value['key_7_value'] . '\', `key_8_type` = \'' . $value['key_8_type'] . '\', `key_8_value` = \'' . $value['key_8_value'] . '\', `key_9_type` = \'' . $value['key_9_type'] . '\', `key_9_value` = \'' . $value['key_9_value'] . '\', `key_pound_type` = \'' . $value['key_pound_type'] . '\', `key_pound_value` = \'' . $value['key_pound_value'] . '\', `key_star_type` = \'' . $value['key_star_type'] . '\', `key_star_value` = \'' . $value['key_star_value'] . '\', `t_action_type` = \'' . $value['key_t_type'] . '\', `t_action_value` = \'' . $value['key_t_value'] . '\', `i_action_type` = \'' . $value['key_i_type'] . '\', `i_action_value` = \'' . $value['key_i_value'] . '\' WHERE `id`=' . $ivr_id); } @exec('sudo ' . qsa(GS_DIR . 'sbin/start-asterisk') . ' --dialplan' . ' 1>>/dev/null 2>>/dev/null &'); $action = 'edit'; } ##################################################################### # save } ##################################################################### ##################################################################### # edit { ##################################################################### if ($action === 'edit') { /* // its also possible to make a target selection for the keys for queue, user, ivr // but proberly bad when having lots of users $rs = $DB->execute( 'SELECT `name` FROM `ast_queues`'); $queues_ext = array(); while ($r = $rs->fetchRow()) {
&& $info['orig_mbox'] != '') { $id3_comment .= '<< '.$info['orig_mbox']; } */ $id3_comment = subStr(_to_id3tag_ascii($id3_comment), 0, 28); $sox = find_executable('sox', array('/usr/bin/', '/usr/local/bin/', '/usr/sbin/', '/usr/local/sbin/')); if (!$sox) { gs_log(GS_LOG_WARNING, 'sox - command not found.'); _server_error('Failed to convert file.'); } $lame = find_executable('lame', array('/usr/local/bin/', '/usr/bin/', '/usr/local/sbin/', '/usr/sbin/')); if (!$lame) { gs_log(GS_LOG_WARNING, 'lame - command not found.'); _server_error('Failed to convert file.'); } $cmd = $sox . ' -q -t al ' . qsa($origfile) . ' -r 8000 -c 1 -s -b 16 -t wav - 2>>/dev/null | ' . $lame . ' --preset fast standard -m m -a -b 32 -B 96 --quiet --ignore-tag-errors --tt ' . qsa($id3_title) . ' --ta ' . qsa($id3_artist) . ' --tl ' . qsa($id3_album) . ' --tc ' . qsa($id3_comment) . ' --tg 101 - ' . qsa($outfile) . ' 2>&1 1>>/dev/null'; # (ID3 tag genre 101 = "Speech") $err = 0; $out = array(); @exec($cmd, $out, $err); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to convert voicemail file to ' . $fmt . '. (' . trim(implode(' - ', $out)) . ')'); _server_error('Failed to convert file.'); } $DB->execute('UPDATE `vm_msgs` SET `listened_to`=1 WHERE `id`=' . (int) @$_GET['id']); @header('Content-Type: audio/mpeg'); $fake_filename = preg_replace('/[^0-9a-z\\-_.]/i', '', 'vm_' . $userinfo['ext'] . '_' . date('Ymd_Hi', $r['ts']) . '_' . subStr(md5(date('s', $r['ts']) . $r['cidnum']), 0, 4) . '.mp3'); @header('Content-Disposition: ' . ($attach ? 'attachment' : 'inline') . '; filename="' . $fake_filename . '"');
$DB->execute($query); } } if (count($sql_values) > 0) { $query = $query_start . implode(",\n", $sql_values); $sql_values = array(); $DB->execute($query); } //$ok = (gs_get_conf('GS_DB_MASTER_TRANSACTIONS') ? @$DB->completeTrans() : true); $ok = gs_db_commit_trans($DB); $file = @$_SESSION['sudo_user']['pb-csv-file']; @($_SESSION['sudo_user']['pb-csv-file'] = null); if (@is_file($file)) { $err = 0; $out = array(); @exec('rm -f ' . qsa($file)); } echo '<br />', "\n"; echo '<p class="text">', $ok ? __('Die Daten wurden in Ihr persönliches Telefonbuch importiert.') : 'DB Error.', '</p>', "\n"; } } if ($action == '') { echo "<h3>" . __('CSV-Export') . "</h3>\n<br />"; echo '<a href="', GS_URL_PATH, 'srv/csv-export.php', $sudo_url, '" title="', __('CSV-Datei des Telefonbuches herunterladen'), '">' . __('CSV-Datei herunterladen') . '</a>'; echo "<br /><br /><br /><h3>" . __('CSV-Import') . "</h3>\n<br />"; echo '<form method="post" action="', GS_URL_PATH, '" enctype="multipart/form-data">', "\n"; echo gs_form_hidden($SECTION, $MODULE), "\n"; echo '<input type="hidden" name="action" value="upload" />', "\n"; echo '<p class="text">', __('Sie haben hier die Möglichkeit, eine Datei im CSV-Format hochzuladen, um die Einträge in Ihr persönliches Telefonbuch zu übernehmen.'), '</p>', "\n"; echo '<p class="text">', __('Vor dem Import der Datensätze wird Ihnen zur Kontrolle eine Vorschau angezeigt.'), '</p>', "\n"; echo '<p class="text">', htmlEnt(__("In der Vorschau kann eingestellt werden, ob das bestehende Telefonbuch erweitert oder ersetzt werden soll.")), '</p>', "\n";
function gs_keyval_set($key, $val) { if (!gs_keyval_is_valid_key($key)) { return false; } if ($val === gs_keyval_get($key)) { return true; } # unchanged /* $val = gs_keyval_enc($val); $fh = @fOpen( '/var/lib/gemeinschaft/vars/'.$key, 'wb' ); if (! $fh) return false; @stream_set_write_buffer($fh, 0); $ok = (bool)@fWrite($fh, $val, strLen($val)); @fClose($fh); return $ok; */ $cmd = 'echo -n ' . qsa(gs_keyval_enc($val)) . ' > ' . qsa('/var/lib/gemeinschaft/vars/' . $key) . ' 2>>/dev/null'; $err = 0; $out = array(); @exec('sudo sh -c ' . qsa($cmd) . ' 2>>/dev/null', $out, $err); return $err === 0; }
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; } } }
$lang = strToLower(subStr($lang, 0, 2)); switch ($lang) { case 'de': $l = array('de_DE.UTF-8', 'de_DE.utf8', 'de_DE.iso88591', 'de_DE.iso885915@euro', 'de_DE.ISO8859-1', 'de_DE.ISO8859-15', 'de_DE@euro', 'de_DE', 'de'); break; case 'en': $l = array('en_US.utf8', 'en_US.iso88591', 'en_US.ISO8859-1', 'en_US.US-ASCII', 'en_US', 'en'); break; default: $l = array('C'); } $lfound = setLocale(LC_TIME, $l); if ($lfound === false) { $err = 0; $out = array(); exec('locale -a | grep -i ' . qsa('^' . $lang . '_') . ' 2>>/dev/null', $out, $err); if ($err != 0) { gs_log(GS_LOG_NOTICE, 'Failed to find locales on your system'); } else { $lfound = setLocale(LC_TIME, $out); if ($lfound === false) { gs_log(GS_LOG_NOTICE, 'Your system does not have any locales like "' . $lang . '_*"'); } else { gs_log(GS_LOG_NOTICE, 'Using locale "' . $lfound . '" as a fallback'); } } } $wdays = array('mo' => 'Mon', 'tu' => 'Tue', 'we' => 'Wed', 'th' => 'Thu', 'fr' => 'Fri', 'sa' => 'Sat', 'su' => 'Sun'); $wdaysl = array(); foreach ($wdays as $col => $wdca) { $wdaysl[$col] = mb_subStr(strFTime('%a', strToTime('last ' . $wdca)), 0, 1);
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"]); } } } }
$rs = $DB->execute('SELECT `host` FROM `hosts` WHERE `is_foreign` = 0'); while ($r = $rs->fetchRow()) { /* if ($r['host'] === '127.0.0.1') { //FIXME gs_log(GS_LOG_DEBUG, "Reloading local Asterisk"); echo "Reloading <b>local</b> Asterisk\n"; @ob_flush(); @flush(); passThru( 'sudo '. qsa(GS_DIR.'sbin/start-asterisk'), $err ); } else { */ gs_log(GS_LOG_DEBUG, "Reloading Asterisk on " . $r['host']); echo "Reloading Asterisk on <b>", $r['host'], "</b>\n"; @ob_flush(); @flush(); passThru('sudo ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=8 -l root ' . qsa($r['host']) . ' ' . qsa(GS_DIR . 'sbin/start-asterisk'), $err); /* } */ echo "\n", '→ <b>', $err == 0 ? 'OK' : 'ERR', '</b>', "\n\n"; } @ob_implicit_flush(0); echo '</pre>'; } /* elseif ($action === 'shutdown' && $shutdown_enabled) { ?> <form method="post" action="<?php echo GS_URL_PATH; ?>"> <?php echo gs_form_hidden($SECTION, $MODULE); ?> <input type="hidden" name="action" value="shutdown2" /> <br />
function _snom_reboot($ip, $https, $user, $pass) { $cmd = 'curl -q --silent --retry 0 --insecure --proxy-anyauth --max-redirs 5 --anyauth' . ($user == '' ? '' : ' --user ' . qsa($user . ':' . $pass)) . ' --max-time 4 --write-out ' . qsa('### %{http_code} ###') . ' ' . qsa(($https ? 'https' : 'http') . '://' . $ip . '/dummy.htm?REBOOT=true') . ' 2>>/dev/null'; $err = 0; $out = array(); @exec($cmd, $out, $err); }
} } if (count($dhcpd_ranges) < 1) { $dhcpd_ranges[] = 'range 192.168.1.10 192.168.1.254;'; } } $tpl = preg_replace('/__GEMEINSCHAFT_DHCPD_RANGES__/', implode("\n\t", $dhcpd_ranges), $tpl); $data = '# AUTO-GENERATED BY GEMEINSCHAFT' . "\n"; $data .= '# template:' . "\n"; $data .= '# ' . $tpl_file . "\n"; $data .= "\n"; $data .= $tpl . "\n"; $cmd = 'echo -n ' . qsa($data) . ' > ' . qsa('/etc/dhcp3/dhcpd.conf') . ' 2>>/dev/null'; $err = 0; $out = array(); @exec('sudo sh -c ' . qsa($cmd) . ' 2>>/dev/null', $out, $err); # update database table "hosts" # require_once GS_DIR . 'inc/db_connect.php'; $db = gs_db_master_connect(); if ($db) { $tmp = $db->escape($form_ipaddr); switch ($GS_INSTALLATION_TYPE) { case 'gpbx': $tmp2 = 'GPBX'; break; default: $tmp2 = 'Gemeinschaft (single)'; } $tmp2 = $db->escape($tmp2); @$db->execute('UPDATE `hosts` SET `host`=\'' . $tmp . '\', `comment`=\'' . $tmp2 . '\' WHERE `id`=1');
@exec($cmd . ' >>/dev/null 2>>/dev/null', $out, $ping_err); $time = (microtime_float() - $start) * 0.5; # script startup time if ($ping_err === 0) { echo '<span style="color:#0a0;">', round($time * 1000), ' ms</span>'; } else { echo '<b style="color:#f00;">?</b>'; } echo '</td>', "\n"; if ($nodes[$ip]['active']) { @ob_flush(); @flush(); echo '<td class="r">'; if ($ping_err === 0) { $timeout = 2; $cmd = 'PATH=$PATH:/usr/local/bin; ' . GS_DIR . 'sbin/check-sip-alive ' . qsa('sip:checkalive@' . $ip) . ' ' . $timeout; $out = array(); $start = microtime_float(); @exec($cmd . ' 2>&1', $out, $err); $time = (microtime_float() - $start) * 0.8; # script startup time $out = strToUpper(trim(implode("\n", $out))); if ($err === 0 && subStr($out, 0, 2) === 'OK') { echo '<span style="color:#0a0;">', round($time * 1000), ' ms</span>'; } else { if ($out === 'FAIL') { echo '<b style="color:#f00;">>', $timeout, ' s</b>'; } else { echo '<b style="color:#f00;">?</b>'; } }
return; } $content_length_mb = ceil((int) $m[1] / 1000000); if ($disk_free_mb < $content_length_mb + $disk_free_spare_mb) { echo 'Zu wenig Speicherplatz. (weniger als ' . round($content_length_mb + $disk_free_spare_mb) . ' MB)'; return; } $download_script = '/usr/local/bin/gpbx-upgrade-download'; if (!file_exists($download_script)) { echo 'Error.'; return; } //$download_script = '/opt/gpbx-svn/trunk/deb-factory/custom/gemeinschaft/usr-local-bin-gpbx-upgrade-download'; $err = 0; $out = array(); @exec('sudo sh -c ' . qsa($download_script . ' ' . qsa($gpbx_upgrade_script) . ' ' . qsa($gpbx_upgrade_file) . ' ' . qsa(($content_length_mb + 4) * 1000000) . ' ' . qsa('GPBX') . ' 1>>/dev/null 2>>/dev/null &') . ' 0<&- 1>&- 2>&- &', $out, $err); //echo $err; //echo "<pre>", implode("\n",$out) ,"</pre>"; if ($err !== 0) { echo 'Fehler.'; return; } sleep(4); clearStatCache(); } # download in progress? # if (file_exists('/tmp/gpbx-downloading-upgrade.pid') || (int) @shell_exec('sudo ps ax 2>>/dev/null | grep gpbx-upgrade-download | grep -v grep | wc -l') > 0) { echo '<br /><p>', 'Momentan wird ein Upgrade heruntergeladen.', '</p>', "\n"; $upgrade_info = @gs_file_get_contents($gpbx_userdata . 'upgrades/upgrade-info'); //$upgrade_info = ' gpbx_upgrade_size = 250420000 ';
function gs_db_setup_replication($master_host, $slave_host, $user, $pass) { if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { return new GsError('Not allowed on single server systems.'); } # are we root? do we have to sudo? $uid = @posix_geteuid(); $uinfo = @posix_getPwUid($uid); $uname = @$uinfo['name']; $sudo = $uname === 'root' ? '' : 'sudo '; # get binlog position # $master = null; $ok = gs_db_connect($master, 'master', $master_host, $user, $pass, GS_DB_MASTER_DB, 1); if (!$ok) { return new GsError('Failed to connect to master database.'); } $rs = $master->execute('SHOW MASTER STATUS'); if (!$rs) { return new GsError('DB error.'); } $master_status = $rs->fetchRow(); if (!$master_status) { return new GsError('DB error.'); } # Stop Slave # $slave = null; $ok = gs_db_connect($slave, 'slave', $slave_host, $user, $pass, GS_DB_SLAVE_DB, 1); if (!$ok) { return new GsError('Failed to connect to slave database.'); } $ok = $slave->execute('STOP SLAVE'); if (!$ok) { return new GsError('Failed to stop database slave replication'); } $dump_filename = '/tmp/gs-db-resync-dump-' . rand() . '.sql'; # dump Master database # $cmd = 'mysqldump --opt --lock-tables --databases asterisk'; $cmd = 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa('root@' . $master_host) . ' ' . qsa($cmd) . ' > ' . qsa($dump_filename) . ' 2>>/dev/null'; $err = 0; $out = array(); @exec($sudo . $cmd, $out, $err); if ($err != 0) { return new GsError('Failed to save dump of master database!'); } # restore dump on Slave # $cmd = 'cat ' . qsa($dump_filename) . ' | ssh -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa('root@' . $slave_host) . ' ' . qsa('mysql asterisk'); //FIXME - instead of cat, copy the dump to the slave first $err = 0; $out = array(); @exec($sudo . $cmd, $out, $err); if ($err != 0) { return new GsError('Failed to restore database dump on slave!'); } # start replication on Slave # $query = 'CHANGE MASTER TO ' . 'MASTER_HOST=\'' . $master->escape($master_host) . '\', ' . 'MASTER_USER=\'' . $master->escape($user) . '\', ' . 'MASTER_PASSWORD=\'' . $master->escape($pass) . '\', ' . 'MASTER_LOG_FILE=\'' . $master->escape($master_status['File']) . '\', ' . 'MASTER_LOG_POS=' . (int) $master_status['Position']; $ok = $slave->execute($query); if (!$ok) { return new GsError('Failed to Change Master on Slave!'); } $ok = $slave->execute('START SLAVE'); if (!$ok) { return new GsError('Failed to Start Slave Replication Process'); } return true; }
function _settings_out() { global $settings, $fw_vers_nrml, $prov_url_snom; if (_snomAppCmp($fw_vers_nrml, '7.1.8') < 0) { header('Content-Type: text/plain; charset=utf-8'); # the Content-Type header is ignored by the Snom foreach ($settings as $name => $a1) { if (subStr($name, 0, 1) === '_') { continue; } if (!array_key_exists('_is_array', $a1)) { echo $name, $a1['w'] ? '$' : '&', ': ', $a1['v'], "\n"; } else { if ($name !== 'fkey') { foreach ($a1 as $idx => $a2) { if ($idx === '_is_array') { continue; } echo $name, $idx, $a2['w'] ? '$' : '&', ': ', $a2['v'], "\n"; } } else { foreach ($a1 as $idx => $a2) { if ($idx === '_is_array') { continue; } echo $name, $idx, $a2['w'] ? '$' : '&', ': ', $a2['v'], "\n"; if (is_array($a2['a']) && array_key_exists('context', $a2['a'])) { echo $name, '_context', $idx, $a2['w'] ? '$' : '&', ': ', $a2['a']['context'], "\n"; } } } } /* ! means writeable by the user, but will not overwrite existing $ means writeable by the user, but will overwrite existing (available since version 4.2) & (or no flag) means read only, but will overwrite existing */ } unset($settings); } else { header('Content-Type: application/xml; charset=utf-8'); echo '<', '?xml version="1.0" encoding="utf-8"?', '>', "\n"; echo '<settings>', "\n\n"; $xml_cats = array(); foreach ($settings as $name => $a1) { if (!array_key_exists('_is_array', $a1)) { $attrstr = ''; if (is_array($a1['a'])) { foreach ($a1['a'] as $attr => $attrval) { $attrstr .= ' ' . $attr . '="' . _snomCnfXmlEsc($attrval) . '"'; } } $line = '<' . $name . $attrstr . ' perm="' . ($a1['w'] ? 'RW' : 'R') . '">' . _snomCnfXmlEsc($a1['v']) . '</' . $name . '>'; _add_to_cat($xml_cats, $name, $line); } else { if ($name === '_gui_lang' || $name === '_web_lang') { foreach ($a1 as $idx => $a2) { if ($idx === '_is_array') { continue; } $attrstr = ''; if (is_array($a2['a'])) { foreach ($a2['a'] as $attr => $attrval) { $attrstr .= ' ' . $attr . '="' . _snomCnfXmlEsc($attrval) . '"'; } } $line = '<language url="' . $prov_url_snom . 'sw/' . _snomCnfXmlEsc($a2['v']) . '" name="' . _snomCnfXmlEsc($idx) . '"' . $attrstr . ' />'; $langfile = GS_DIR . 'htdocs/prov/snom/sw/' . $a2['v']; $langfile_lock = $langfile . '.lock'; $langfiles_dir = dirName($langfile) . '/'; $langfile_url = 'http://provisioning.snom.com/config/snom' . $a2['v']; if (file_exists($langfile) && is_readable($langfile) && file_exists($langfile . '.done')) { //gs_log(GS_LOG_DEBUG, '---1'); _add_to_cat($xml_cats, $name, $line); } else { /* gs_log(GS_LOG_DEBUG, '---2'); if (! file_exists($langfiles_dir)) { gs_log(GS_LOG_DEBUG, '---3'); @exec( 'sudo mkdir -p '. qsa($langfiles_dir) .' 1>>/dev/null 2>>/dev/null' ); } if (file_exists($langfiles_dir)) { gs_log(GS_LOG_DEBUG, '---4'); $err=0; $out=array(); @exec( 'sudo touch '. qsa($langfile_lock) .' && sudo chmod 666 '. qsa($langfile_lock) .' 1>>/dev/null 2>>/dev/null', $out, $err ); if ($err==0) { gs_log(GS_LOG_DEBUG, '---5'); $lockfh = @fOpen( $langfile_lock, 'wb' ); if ($lockfh) { gs_log(GS_LOG_DEBUG, '---6'); $would_block = false; if (@flock($lockfh, LOCK_SH, $would_block)) { gs_log(GS_LOG_DEBUG, '---7'); if (! $would_block) { gs_log(GS_LOG_DEBUG, '---8'); gs_log( GS_LOG_NOTICE, 'Trying to wget '. qsa($langfile_url) ); @exec( 'sudo cd '. qsa($langfiles_dir) .' && sudo wget -q -a /dev/null -t 1 -T 300 -nc -c '. qsa($langfile_url) .' && sudo touch '. qsa($langfile.'.done') .' 1>>/dev/null 2>>/dev/null &' ); } @flock($lockfh, LOCK_UN); } @fClose($lockfh); } } } */ gs_log(GS_LOG_NOTICE, 'Please cd ' . qsa($langfiles_dir) . ' && wget ' . qsa($langfile_url) . ' && touch ' . qsa(baseName($langfile) . '.done')); } } } else { foreach ($a1 as $idx => $a2) { if ($idx === '_is_array') { continue; } $attrstr = ''; if (is_array($a2['a'])) { foreach ($a2['a'] as $attr => $attrval) { $attrstr .= ' ' . $attr . '="' . _snomCnfXmlEsc($attrval) . '"'; } } $line = '<' . $name . $attrstr . ' idx="' . $idx . '" perm="' . ($a2['w'] ? 'RW' : 'R') . '">' . _snomCnfXmlEsc($a2['v']) . '</' . $name . '>'; _add_to_cat($xml_cats, $name, $line); } } } } unset($settings); foreach ($xml_cats as $cat => $lines) { echo "\t", '<', $cat, '>', "\n"; foreach ($lines as $line) { echo $line, "\n"; } echo "\t", '</', $cat, '>', "\n\n"; } unset($xml_cats); echo '</settings>', "\n"; } }
function ReleaseIP($node_id) { global $node; $exec_string = '/sbin/ifconfig ' . qsa($node[$node_id]['local_interface']) . ' down'; echo "Execute: {$exec_string}\n"; exec($exec_string, $ret_array, $ret_val); return $ret_val; }
function takeOverIP($node_id) { global $node; $cmd = '/sbin/ifconfig ' . qsa($node[$node_id]['local_interface']) . ' ' . qsa($node[$node_id]['dynamic_ip']) . ' netmask ' . qsa($node[$node_id]['netmask']) . ' broadcast ' . qsa($node[$node_id]['broadcast']); write_log("Execute {$cmd}"); $err = 0; $out = array(); @exec($cmd . ' 1>>/dev/null', $out, $err); $cmd = '/sbin/route add -host ' . qsa($node[$node_id]['dynamic_ip']) . ' ' . qsa($node[$node_id]['local_interface']); write_log("Execute {$cmd}"); $err = 0; $out = array(); @exec($cmd . ' 1>>/dev/null', $out, $err); }
function gs_ringtone_set($user, $src, $bellcore, $change_file = false, $file = null) { if (!preg_match('/^[a-zA-Z\\d]+$/', $user)) { return new GsError('User must be alphanumeric.'); } if (!in_array($src, array('internal', 'external'), true)) { return new GsError('Source must be internal|external.'); } $bellcore = (int) $bellcore; if ($bellcore < 0 || $bellcore > 10) { return new GsError('Bellcore must be between 1 and 10 or 0 for silent.'); } if (!$change_file) { $file = null; } else { if (!$file) { # to remove a custom ringer $file = null; } else { $file = @realPath($file); if (!@file_exists($file)) { $file = @realPath(@$_ENV['PWD'] . '/' . $file); if (!@file_exists($file)) { return new GsError('File not found.'); } } //if (strToLower(subStr($file,-4)) != '.mp3') // return new GsError( 'File is not an mp3.' ); } } # 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.'); } # make sure there is an entry in the db and set the bellcore ringer # $num = (int) $db->executeGetOne('SELECT COUNT(*) `num` FROM `ringtones` WHERE `user_id`=' . $user_id . ' AND `src`=\'' . $src . '\''); if ($num < 1) { $ok = $db->execute('INSERT INTO `ringtones` (`user_id`, `src`, `bellcore`, `file`) VALUES (' . $user_id . ', \'' . $src . '\', ' . $bellcore . ', NULL)'); } else { $ok = $db->execute('UPDATE `ringtones` SET `bellcore`=' . $bellcore . ' WHERE `user_id`=' . $user_id . ' AND `src`=\'' . $src . '\''); } if (!$ok) { return new GsError('DB error.'); } if (!$change_file) { return true; } # are we the web server? # if (!gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { $our_host_ips = @gs_get_listen_to_ips(); if (!is_array($our_host_ips)) { return new GsError('Failed to get our host IPs.'); } $we_are_the_webserver = in_array(GS_PROV_HOST, $our_host_ips); } else { $we_are_the_webserver = true; } # remove old ringer from htdocs/prov/ringtones/ dir # if ($we_are_the_webserver) { # local @exec('sudo rm -rf ' . GS_DIR . 'htdocs/prov/ringtones/' . $user . '-' . subStr($src, 0, 3) . '-* 1>>/dev/null 2>>/dev/null'); } else { # remotely $cmd = 'rm -rf /opt/gemeinschaft/htdocs/prov/ringtones/' . $user . '-' . subStr($src, 0, 3) . '-* 1>>/dev/null 2>>/dev/null &'; @exec('sudo ssh -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa('root@' . GS_PROV_HOST) . ' ' . qsa($cmd) . ' 1>>/dev/null 2>>/dev/null'); } # just remove custom ringer? # if (!$file) { $ok = $db->execute('UPDATE `ringtones` SET `file`=NULL WHERE `user_id`=' . $user_id . ' AND `src`=\'' . $src . '\''); if (!$ok) { return new GsError('DB error.'); } return true; } # convert sound file to the formats needed for each phone type # $to_sox_format = array('alaw' => 'al', 'ulaw' => 'ul'); $pinfo = pathInfo($file); //$base = $pinfo['basename']; $ext = strToLower(@$pinfo['extension']); if (array_key_exists($ext, $to_sox_format)) { $ext = $to_sox_format[$ext]; } $rand = base_convert(rand(1296, 46655), 10, 36); # 100(36) - zzz(36) $tmpbase = '/tmp/gs-ring-' . $user . '-' . $rand; $infile = $tmpbase . '-in.' . $ext; $outbase = $tmpbase . '-out'; $ok = @copy($file, $infile); @chmod($infile, 0666); if (!$ok) { return new GsError('Failed to copy file to "' . $infile . '".'); } include_once GS_DIR . 'inc/phone-capability.php'; $phone_types = glob(GS_DIR . 'htdocs/prov/*/capability.php'); if (!is_array($phone_types)) { $phone_types = array(); } for ($i = 0; $i < count($phone_types); ++$i) { $phone_types[$i] = baseName(dirName($phone_types[$i])); } gs_log(GS_LOG_DEBUG, 'Ringtone conversion: Found phone types: ' . implode(', ', $phone_types)); $errors = array(); $new_ringer_basename = $user . '-' . subStr($src, 0, 3) . '-' . $rand; foreach ($phone_types as $phone_type) { include_once GS_DIR . 'htdocs/prov/' . $phone_type . '/capability.php'; $class = 'PhoneCapability_' . $phone_type; if (!class_exists($class)) { gs_log(GS_LOG_WARNING, $phone_type . ': Class broken.'); $errors[] = $phone_type . ': Class broken.'; continue; } $PhoneCapa = new $class(); $outfile = $PhoneCapa->conv_ringtone($infile, $outbase); if (isGsError($outfile)) { gs_log(GS_LOG_WARNING, 'Ringtone conversion: ' . $phone_type . ': ' . $outfile->getMsg()); $errors[] = $phone_type . ': ' . $outfile->getMsg(); } elseif ($outfile === null) { gs_log(GS_LOG_DEBUG, 'Ringtone conversion: ' . $phone_type . ': Not implemented.'); continue; } elseif (!$outfile) { gs_log(GS_LOG_WARNING, 'Ringtone conversion: ' . $phone_type . ': Failed to convert file.'); $errors[] = $phone_type . ': ' . 'Failed to convert file.'; continue; } if (!file_exists($outfile)) { gs_log(GS_LOG_WARNING, 'Ringtone conversion: ' . $phone_type . ': Failed to convert file.'); $errors[] = $phone_type . ': ' . 'Failed to convert file.'; continue; } gs_log(GS_LOG_DEBUG, 'Ringtone conversion: ' . $phone_type . ': Converted.'); @chmod($outfile, 0666); $pinfo = pathInfo($outfile); $ext = strToLower(@$pinfo['extension']); $newbase = $new_ringer_basename . '-' . $phone_type . '.' . $ext; if ($phone_type === 'siemens' && !gs_get_conf('GS_SIEMENS_PROV_PREFER_HTTP')) { # if this is a Siemens phone, push the file on the FTP server @copy($infile, '/tmp/' . $newbase); //FIXME - why? $ok = $PhoneCapa->_upload_ringtone('/tmp/' . $newbase); if (!$ok) { gs_log(GS_LOG_WARNING, 'Failed to upload ringtone to FTP server.'); } if (is_file('/tmp/' . $newbase)) { @unlink('/tmp/' . $newbase); } } else { if ($we_are_the_webserver) { # local //rename( $outfile, GS_DIR .'htdocs/prov/ringtones/'. $newbase ); @exec('sudo mv ' . qsa($outfile) . ' ' . qsa(GS_DIR . 'htdocs/prov/ringtones/' . $newbase), $out, $err); } else { # remotely @exec('sudo scp -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa($outfile) . ' ' . qsa('root@' . GS_PROV_HOST . ':/opt/gemeinschaft/htdocs/prov/ringtones/' . $newbase) . ' >>/dev/null 2>>/dev/null', $out, $err); //@exec( 'sudo rm -f '. qsa($outfile) .' >>/dev/null 2>&1' ); @unlink($outfile); } if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to mv ringtone.'); } } } if (is_file($infile)) { @unlink($infile); } @exec('rm -rf ' . $tmpbase . '-* 1>>/dev/null 2>>/dev/null &'); if (count($errors) > 0) { return new GsError("Failed to convert ringtone for some or all phone types: " . implode(", ", $errors)); } $ok = $db->execute('UPDATE `ringtones` SET `file`=\'' . $db->escape($new_ringer_basename) . '\' WHERE `user_id`=' . $user_id . ' AND `src`=\'' . $src . '\''); if (!$ok) { return new GsError('DB error.'); } return true; // OLD STUFF: /* # remove old ringer # $files = @glob( GS_DIR .'htdocs/prov/ringtones/'. $user .'/'. $src .'-*' ); if (is_array($files)) { foreach ($files as $f) { unlink(); } } die(); shell_exec( 'rm -f /opt/ast/htdocs/prov/ringtones/'. $ext .'-*' ); # get SIP name # $ext = $db->executeGetOne( 'SELECT `name` FROM `ast_sipfriends` WHERE `_user_id`='. $user_id ); if (! $ext) return new GsError( 'DB error.' ); if ($file) { $rand = rand(10000,99999).time(); shell_exec( 'mpg123 -m -r 8000 -w - -n 500 -q \''. $file .'\' > \'/opt/gemeinschaft/htdocs/prov/ringtones/'. $rand .'.wav\'' ); shell_exec( 'sox \'/opt/gemeinschaft/htdocs/prov/ringtones/'. $rand .'.wav\' -r 8000 -c 1 -w \'/opt/gemeinschaft/htdocs/prov/ringtones/'. $ext .'-'. time() .'.wav\'' ); shell_exec( 'rm \'/opt/gemeinschaft/htdocs/prov/ringtones/'. $rand .'.wav\'' ); } else { //shell_exec( 'rm -f /opt/gemeinschaft/htdocs/prov/ringtones/'. $ext .'-*' ); } return true; */ }
' . $allow_in . ', \'' . $DB->escape($in_dest_search) . '\', \'' . $DB->escape($in_dest_replace) . '\', \'' . $DB->escape($in_cid_search) . '\', \'' . $DB->escape($in_cid_replace) . '\', \'' . $DB->escape($out_cid_search) . '\', \'' . $DB->escape($out_cid_replace) . '\' )'); $ggid = (int) $DB->getLastInsertId(); if ($ggid < 1) { $ggid = 0; } $_REQUEST['ggid'] = $ggid; } $cmd = '/opt/gemeinschaft/sbin/start-asterisk 1>>/dev/null 2>>/dev/null'; @exec('sudo sh -c ' . qsa($cmd) . ' 1>>/dev/null 2>>/dev/null &'); $action = 'gedit'; } ##################################################################### ##################################################################### if ($action === 'ggdel') { $DB->execute('UPDATE `gates` SET `grp_id`=NULL WHERE `grp_id`=' . $ggid); $DB->execute('DELETE FROM `gate_cids` WHERE `grp_id`=' . $ggid); $DB->execute('DELETE FROM `gate_grps` WHERE `id`=' . $ggid); $action = ''; } ##################################################################### ##################################################################### if ($action === 'gaddcid') { $ggid = (int) @$_REQUEST['gg-id']; $cid_int = preg_replace('/[^0-9]/', '', @$_REQUEST['cid-int']);
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; }
# 90400 $wav_alaw_header = 'RIFF' . pack('V', 50 + $dlen) . 'WAVE' . 'fmt ' . pack('V', 18) . pack('v', 6) . pack('v', 1) . pack('V', 8000) . pack('V', 8000) . pack('v', 1) . pack('v', 8) . pack('v', 0) . 'fact' . pack('V', 4) . pack('V', $dlen) . 'data' . pack('V', $dlen); $outfile = $origfile; # to soothe the file_exists() check } elseif ($fmt === 'sun-pcmu') { $cmd = $sox . ' -q -t al ' . qsa($origfile) . ' -r 8000 -c 1 -U -b 8 -t au ' . qsa($outfile) . ' 1>>/dev/null'; $err = 0; $out = array(); @exec($cmd, $out, $err); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to convert voicemail file to ' . $fmt . '. (' . trim(implode(' - ', $out)) . ')'); _server_error('Failed to convert file.'); } } elseif ($fmt === 'wav-pcm') { # signed linear PCM in WAVE container $cmd = $sox . ' -q -t al ' . qsa($origfile) . ' -r 8000 -c 1 -s -b 16 -t wav ' . qsa($outfile) . ' 1>>/dev/null'; $err = 0; $out = array(); @exec($cmd, $out, $err); if ($err != 0) { gs_log(GS_LOG_WARNING, 'Failed to convert voicemail file to ' . $fmt . '. (' . trim(implode(' - ', $out)) . ')'); _server_error('Failed to convert file.'); } } if (!file_exists($outfile)) { gs_log(GS_LOG_WARNING, 'Failed to convert voicemail file.'); _server_error('Failed to convert file.'); } @header('Content-Type: ' . $formats[$fmt]['mime']); $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']); @header('Content-Disposition: ' . ($attach ? 'attachment' : 'inline') . '; filename="' . $fake_filename . '"');
if ($err != 0) { @unlink($filename); gs_log(GS_LOG_WARNING, 'Failed to move call file "' . $filename . '" to "' . '/var/spool/asterisk/outgoing/' . baseName($filename) . '"'); die_error('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']); die_error('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 . '"'); die_error('Failed to mv call file on remote host.'); } } } else { include_once GS_DIR . 'inc/boi-soap/boi-api.php'; $api = gs_host_get_api($user['host_id']); switch ($api) { case 'm01': case 'm02': if (!extension_loaded('soap')) { gs_log(GS_LOG_NOTICE, 'Failed to initiate call on foreign host (SoapClient not available).');
echo '</td>', "\n"; echo '</tr>', "\n"; ?> </tbody> </table> <br /> <br /> <?php echo __('Registrierungs-Status'); echo ' (', __('lokaler Asterisk'), ')'; ?> <br /> <div style="font-family:monospace; white-space:pre; background:#eee; border:1px solid #e9e9e9; padding:1px 3px;"><?php $err = 0; $out = array(); @exec('sudo asterisk -rx ' . qsa('iax2 show registry') . ' 2>>/dev/null', $out, $err); if ($err !== 0) { echo '?'; } else { foreach ($out as $line) { echo preg_replace(array('/^(\\s*)([a-zA-Z0-9.\\-_]+[.\\-_][a-zA-Z0-9.\\-_]+)/', '/\\b(Registered|Unregistered|Rejected)\\b/'), array('$1<b>$2</b>', '<b>$1</b>'), rTrim($line)), "\n"; } } ?> </div> <?php } #####################################################################
function gs_hylafax_authfile_sync($authfile = '') { # It will be assumed that the server is accessible, that the file # exists and that the admin account exists otherwise the # connection will fail. # There is a fallback mechanism which copies the file instead of # FTP put'ting it if the fax server is running on the local machine. if (!$authfile) { $authfile = '/tmp/gs-hylafax-hosts.hfaxd-' . rand(100000, 999999); } # create authfile locally # $result = gs_hylafax_authfile_create($authfile); if ($result !== true) { clearStatCache(); if (file_exists($authfile)) { @exec('sudo rm -f ' . qsa($authfile) . ' 2>>/dev/null'); } return $result; } # put authfile to the fax server # $ret = gs_hylafax_authfile_put($authfile); if ($ret !== true) { # if ftp put fails, try to copy it locally. //FIXME: Will fail if the fax host is not "127.0.0.1" or "localhost" # if (gs_get_conf('GS_FAX_HYLAFAX_HOST') === '127.0.0.1' || gs_get_conf('GS_FAX_HYLAFAX_HOST') === 'localhost') { $authfile_dst = gs_get_conf('GS_FAX_HYLAFAX_PATH', '/var/spool/hylafax/') . 'etc/hosts.hfaxd'; $err = 0; $out = array(); @exec('sudo mv ' . qsa($authfile) . ' ' . qsa($authfile_dst) . ' 2>>/dev/null', $out, $err); if ($err !== 0) { @exec('sudo rm -f ' . qsa($authfile) . ' 2>>/dev/null'); return new GsError('Error updating fax authentication on localhost.'); } $err = 0; $out = array(); @exec('sudo chown ' . qsa(gs_get_conf('GS_FAX_HYLAFAX_USER', 'uucp')) . ' ' . qsa($authfile_dst) . ' 2>>/dev/null', $out, $err); if ($err != 0) { @exec('sudo rm -f ' . qsa($authfile) . ' 2>>/dev/null'); return new GsError('Error updating fax authentication on localhost.'); } $err = 0; $out = array(); @exec('sudo chmod ' . '0600' . ' ' . qsa($authfile_dst) . ' 2>>/dev/null', $out, $err); if ($err !== 0) { @exec('sudo rm -f ' . qsa($authfile) . ' 2>>/dev/null'); return new GsError('Error updating fax authentication on localhost.'); } $ret = true; @exec('sudo rm -f ' . qsa($authfile) . ' 2>>/dev/null'); if (@is_dir('/etc/hylafax')) { # Debian @exec('sudo cp ' . qsa($authfile_dst) . ' ' . qsa('/etc/hylafax/hosts.hfaxd') . ' 2>>/dev/null'); } } } clearStatCache(); if (file_exists($authfile)) { @exec('sudo rm -f ' . qsa($authfile) . ' 2>>/dev/null'); } return $ret; }