예제 #1
0
/** like file_get_contents designed to work with url only, will try
 * wget if fails or if MODULEADMINWGET set to true. If it detects
 * failure, will set MODULEADMINWGET to true for future improvements.
 *
 * @param   mixed   url to be fetches or array of multiple urls to try
 * @return  mixed   content of first successful url, boolean false if it failed.
 */
function file_get_contents_url($file_url)
{
    global $amp_conf;
    $contents = '';
    if (!is_array($file_url)) {
        $file_url = array($file_url);
    }
    foreach ($file_url as $fn) {
        if (!$amp_conf['MODULEADMINWGET']) {
            ini_set('user_agent', 'Wget/1.10.2 (Red Hat modified)');
            $contents = @file_get_contents($fn);
        }
        if (empty($contents)) {
            $fn2 = str_replace('&', '\\&', $fn);
            FreePBX::Curl()->setEnvVariables();
            exec("wget --tries=1 --timeout=30 -O - {$fn2} 2>> /dev/null", $data_arr, $retcode);
            if ($retcode) {
                // if server isn't available for some reason should return non-zero
                // so we return and we don't set the flag below
                freepbx_log(FPBX_LOG_ERROR, sprintf(_('Failed to get remote file, mirror site may be down: %s'), $fn));
                continue;
                // We are here if contents were blank. It's possible that whatever we were getting were suppose to be blank
                // so we only auto set the WGET var if we received something so as to not false trigger. If there are issues
                // with content filters that this is designed to get around, we will eventually get a non-empty file which
                // will trigger this for now and the future.
            } elseif (!empty($data_arr) && !$amp_conf['MODULEADMINWGET']) {
                $freepbx_conf =& freepbx_conf::create();
                $freepbx_conf->set_conf_values(array('MODULEADMINWGET' => true), true);
                $nt =& notifications::create($db);
                $text = sprintf(_("Forced %s to true"), 'MODULEADMINWGET');
                $extext = sprintf(_("The system detected a problem trying to access external server data and changed internal setting %s (Use wget For Module Admin) to true, see the tooltip in Advanced Settings for more details."), 'MODULEADMINWGET');
                $nt->add_warning('freepbx', 'MODULEADMINWGET', $text, $extext, '', false, true);
            }
            $contents = implode("\n", $data_arr);
            return $contents;
        } else {
            return $contents;
        }
        // we get here if all wget's fail
        return false;
    }
}
예제 #2
0
 function url_get_contents($url, $request, $verb = 'get', $params = array())
 {
     $params['sv'] = 2;
     global $amp_conf;
     $verb = strtolower($verb);
     $contents = null;
     if (!$amp_conf['MODULEADMINWGET']) {
         $pest = FreePBX::Curl()->pest($url);
         try {
             $contents = $pest->{$verb}($url . $request, $params);
             if (isset($pest->last_headers['x-regenerate-id'])) {
                 $this->_regenerate_unique_id();
             }
             return $contents;
         } catch (Exception $e) {
             freepbx_log(FPBX_LOG_ERROR, sprintf(_('Failed to get remote file, error was:'), (string) $e->getMessage()));
         }
     }
     $fn = $url . $request;
     if (empty($contents)) {
         $fn2 = str_replace('&', '\\&', $fn);
         $p = !empty($params) ? "--post-data '" . http_build_query($params) . "'" : "";
         FreePBX::Curl()->setEnvVariables();
         exec("wget --tries=1 --timeout=30 {$p} -O - {$fn2} 2> /dev/null", $data_arr, $retcode);
         if ($retcode) {
             // if server isn't available for some reason should return non-zero
             // so we return and we don't set the flag below
             freepbx_log(FPBX_LOG_ERROR, sprintf(_('Failed to get remote file, mirror site may be down: %s'), $fn));
             // We are here if contents were blank. It's possible that whatever we were getting were suppose to be blank
             // so we only auto set the WGET var if we received something so as to not false trigger. If there are issues
             // with content filters that this is designed to get around, we will eventually get a non-empty file which
             // will trigger this for now and the future.
             return null;
         } elseif (!empty($data_arr) && !$amp_conf['MODULEADMINWGET']) {
             $freepbx_conf = freepbx_conf::create();
             $freepbx_conf->set_conf_values(array('MODULEADMINWGET' => true), true);
             $nt = notifications::create($db);
             $text = sprintf(_("Forced %s to true"), 'MODULEADMINWGET');
             $extext = sprintf(_("The system detected a problem trying to access external server data and changed internal setting %s (Use wget For Module Admin) to true, see the tooltip in Advanced Settings for more details."), 'MODULEADMINWGET');
             $nt->add_warning('freepbx', 'MODULEADMINWGET', $text, $extext, '', false, true);
         }
         $headers = get_headers_assoc($fn2);
         if (isset($headers['x-regenerate-id'])) {
             $this->_regenerate_unique_id();
         }
         $contents = implode("\n", $data_arr);
         return $contents;
     }
 }
예제 #3
0
 /**
  * Retrieve a remote file
  * Stores file into memory
  * @param  string $path The full path to said file
  * @return string       binary representation of file
  */
 private function getRemoteFile($path)
 {
     $modulef =& \module_functions::create();
     $contents = null;
     $mirrors = $modulef->generate_remote_urls($path, true);
     $params = $mirrors['options'];
     $params['sv'] = 2;
     foreach ($mirrors['mirrors'] as $url) {
         set_time_limit($this->maxTimeLimit);
         try {
             $pest = \FreePBX::Curl()->pest($url);
             $contents = $pest->post($url . $path, $params);
             if (isset($pest->last_headers['x-regenerate-id'])) {
                 $modulef->_regenerate_unique_id();
             }
             if (!empty($contents)) {
                 return $contents;
             }
         } catch (\Exception $e) {
             freepbx_log(FPBX_LOG_ERROR, sprintf(_('Failed to get remote file, error was:'), (string) $e->getMessage()));
         }
     }
 }
예제 #4
0
 /**
  * Actually run GPG
  * @param string Params to pass to gpg
  * @param fd File Descriptor to feed to stdin of gpg
  * @return array returns assoc array consisting of (array)status, (string)stdout, (string)stderr and (int)exitcode
  */
 public function runGPG($params, $stdin = null)
 {
     // Ensure our proxy settings are set, if needed.
     \FreePBX::Curl()->setEnvVariables();
     $fds = array(array("file", "/dev/null", "r"), array("pipe", "w"), array("pipe", "w"), array("pipe", "w"));
     // If we need to send stuff to stdin, then do it!
     if ($stdin) {
         $fds[0] = $stdin;
     }
     $webuser = \FreePBX::Freepbx_conf()->get('AMPASTERISKWEBUSER');
     $home = $this->getGpgLocation();
     // We need to ensure that our environment variables are sane.
     // Luckily, we know just the right things to say...
     if (!isset($this->gpgenv)) {
         $this->gpgenv['PATH'] = "/bin:/usr/bin";
         $this->gpgenv['USER'] = $webuser;
         $this->gpgenv['HOME'] = sys_get_temp_dir();
         $this->gpgenv['SHELL'] = "/bin/bash";
     }
     $homedir = "--homedir {$home}";
     $cmd = $this->gpg . " {$homedir} " . $this->gpgopts . " --status-fd 3 {$params}";
     $proc = proc_open($cmd, $fds, $pipes, "/tmp", $this->gpgenv);
     if (!is_resource($proc)) {
         // Unable to start!
         freepbx_log(FPBX_LOG_FATAL, "Tried to run command and failed: " . $cmd);
         throw new \Exception(sprintf(_("Unable to start GPG, the command was: [%s]"), $cmd));
     }
     // Wait $timeout seconds for it to finish.
     $tmp = null;
     $r = array($pipes[3]);
     if (!stream_select($r, $tmp, $tmp, $this->timeout)) {
         freepbx_log(FPBX_LOG_FATAL, "Tried to run command and failed: " . $cmd);
         throw new \RuntimeException(sprintf(_("GPG took too long to run the command: [%s]"), $cmd));
     }
     // We grab stdout and stderr first, as the status fd won't
     // have completed and closed until those FDs are emptied.
     $retarr['stdout'] = stream_get_contents($pipes[1]);
     $retarr['stderr'] = stream_get_contents($pipes[2]);
     $status = explode("\n", stream_get_contents($pipes[3]));
     array_pop($status);
     // Remove trailing blank line
     $retarr['status'] = $status;
     $exitcode = proc_close($proc);
     $retarr['exitcode'] = $exitcode;
     return $retarr;
 }