Exemplo n.º 1
0
 public function test_same_singletons()
 {
     $a = input_manager::instance();
     $b = input_manager::instance();
     $this->assertSame($a, $b);
 }
Exemplo n.º 2
0
    /**
     * Choose the location of the current plugin folder backup
     *
     * @param string $path full path to the current folder
     * @return string
     */
    protected function backup_location($path) {

        $dataroot = $this->input->get_option('dataroot');
        $pool = $dataroot.'/mdeploy/archive';

        if (!is_dir($pool)) {
            mkdir($pool, 02777, true);
        }

        $target = $pool.'/'.basename($path).'_'.time();

        $suffix = 0;
        while (file_exists($target.'.'.$suffix)) {
            $suffix++;
        }

        return $target.'.'.$suffix;
    }
Exemplo n.º 3
0
 /**
  * Downloads the given file into the given destination.
  *
  * This is basically a simplified version of {@link download_file_content()} from
  * Moodle itself, tuned for fetching files from moodle.org servers.
  *
  * @param string $source file url starting with http(s)://
  * @param string $target store the downloaded content to this file (full path)
  * @return bool true on success, false otherwise
  * @throws download_file_exception
  */
 protected function download_file($source, $target)
 {
     $newlines = array("\r", "\n");
     $source = str_replace($newlines, '', $source);
     if (!preg_match('|^https?://|i', $source)) {
         throw new download_file_exception('Unsupported transport protocol.');
     }
     if (!($ch = curl_init($source))) {
         $this->log('Unable to init cURL.');
         return false;
     }
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
     // verify the peer's certificate
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
     // check the existence of a common name and also verify that it matches the hostname provided
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     // return the transfer as a string
     curl_setopt($ch, CURLOPT_HEADER, false);
     // don't include the header in the output
     curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
     // nah, moodle.org is never unavailable! :-p
     curl_setopt($ch, CURLOPT_URL, $source);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     // Allow redirection, we trust in ssl.
     curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
     if ($cacertfile = $this->get_cacert()) {
         // Do not use CA certs provided by the operating system. Instead,
         // use this CA cert to verify the ZIP provider.
         $this->log('Using custom CA certificate ' . $cacertfile);
         curl_setopt($ch, CURLOPT_CAINFO, $cacertfile);
     } else {
         $this->log('Using operating system CA certificates.');
     }
     $proxy = $this->input->get_option('proxy', false);
     if (!empty($proxy)) {
         curl_setopt($ch, CURLOPT_PROXY, $proxy);
         $proxytype = $this->input->get_option('proxytype', false);
         if (strtoupper($proxytype) === 'SOCKS5') {
             $this->log('Using SOCKS5 proxy');
             curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
         } else {
             if (!empty($proxytype)) {
                 $this->log('Using HTTP proxy');
                 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
             }
         }
         $proxyuserpwd = $this->input->get_option('proxyuserpwd', false);
         if (!empty($proxyuserpwd)) {
             curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyuserpwd);
             curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
         }
     }
     $targetfile = fopen($target, 'w');
     if (!$targetfile) {
         throw new download_file_exception('Unable to create local file ' . $target);
     }
     curl_setopt($ch, CURLOPT_FILE, $targetfile);
     $result = curl_exec($ch);
     // try to detect encoding problems
     if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) {
         curl_setopt($ch, CURLOPT_ENCODING, 'none');
         $result = curl_exec($ch);
     }
     fclose($targetfile);
     $this->curlerrno = curl_errno($ch);
     $this->curlerror = curl_error($ch);
     $this->curlinfo = curl_getinfo($ch);
     if (!$result or $this->curlerrno) {
         $this->log('Curl Error.');
         return false;
     } else {
         if (is_array($this->curlinfo) and (empty($this->curlinfo['http_code']) or $this->curlinfo['http_code'] != 200)) {
             $this->log('Curl remote error.');
             $this->log(print_r($this->curlinfo, true));
             return false;
         }
     }
     return true;
 }
Exemplo n.º 4
0
    /**
     * Get the location of ca certificates.
     * @return string absolute file path or empty if default used
     */
    protected function get_cacert() {
        $dataroot = $this->input->get_option('dataroot');

        // Bundle in dataroot always wins.
        if (is_readable($dataroot.'/moodleorgca.crt')) {
            return realpath($dataroot.'/moodleorgca.crt');
        }

        // Next comes the default from php.ini
        $cacert = ini_get('curl.cainfo');
        if (!empty($cacert) and is_readable($cacert)) {
            return realpath($cacert);
        }

        // Windows PHP does not have any certs, we need to use something.
        if (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) {
            if (is_readable(__DIR__.'/lib/cacert.pem')) {
                return realpath(__DIR__.'/lib/cacert.pem');
            }
        }

        // Use default, this should work fine on all properly configured *nix systems.
        return null;
    }