public function geocode()
 {
     if ($this->skipGeocode) {
         return -100;
     }
     // check if address was already geocoded
     if ($this->geocodedAddress == $this->address) {
         return $this->status;
     }
     $this->geocodedAddress = $this->address;
     $url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($this->address);
     $apiKey = $this->modules->get('FieldtypeMapMarker')->get('googleApiKey');
     if ($apiKey) {
         $url .= "&key={$apiKey}";
     }
     $http = new WireHttp();
     $json = $http->getJSON($url, true);
     if (empty($json['status']) || $json['status'] != 'OK') {
         $this->error("Error geocoding address");
         if (isset($json['status'])) {
             $this->status = (int) array_search($json['status'], $this->geocodeStatuses);
         } else {
             $this->status = -1;
         }
         $this->lat = 0;
         $this->lng = 0;
         return $this->status;
     }
     $geometry = $json['results'][0]['geometry'];
     $location = $geometry['location'];
     $locationType = $geometry['location_type'];
     $this->lat = $location['lat'];
     $this->lng = $location['lng'];
     $statusString = $json['status'] . '_' . $locationType;
     $status = array_search($statusString, $this->geocodeStatuses);
     if ($status === false) {
         $status = 1;
     }
     // OK
     $this->status = $status;
     $this->message("Geocode {$this->statusString}: '{$this->address}'");
     return $this->status;
 }
 /**
  * Given a URL to a ZIP file, download it, unzip it, and move to /site/modules/[ModuleName]
  *
  * @param $url
  * @param string $destinationDir Optional destination path for files (omit to auto-determine)
  * @return bool|string Returns destinationDir on success, false on failure.
  *
  */
 public function downloadModule($url, $destinationDir = '')
 {
     if (!$this->canUploadDownload()) {
         $this->error($this->_('Unable to complete download'));
         return false;
     }
     if (!preg_match('{^https?://}i', $url)) {
         $this->error($this->_('Invalid download URL specified'));
         return false;
     }
     $tempDir = $this->getTempDir();
     $tempName = 'module-temp.zip';
     // if there is a recognizable ZIP filename in the URL, use that rather than module-temp.zip
     if (preg_match('/([-._a-z0-9]+\\.zip)$/i', $url, $matches)) {
         $tempName = $matches[1];
     }
     $tempZIP = $tempDir . $tempName;
     // download the zip file and save it in assets directory
     $success = false;
     $http = new WireHttp();
     try {
         $file = $http->download($url, $tempZIP);
         // throws exceptions on any error
         $this->message(sprintf($this->_('Downloaded ZIP file: %s (%d bytes)'), $url, filesize($file)));
         $destinationDir = $this->unzipModule($file, $destinationDir);
         if ($destinationDir) {
             $success = true;
             $this->modules->resetCache();
         }
     } catch (Exception $e) {
         $this->error($e->getMessage());
         @unlink($tempZIP);
     }
     return $success ? $destinationDir : false;
 }
Пример #3
0
 /**
  * Get Core Branches with further informations
  */
 protected function getCoreBranches()
 {
     $branches = array();
     $http = new \WireHttp();
     $http->setHeader('User-Agent', 'ProcessWireUpgrade');
     $json = $http->get(self::branchesURL);
     if (!$json) {
         $error = "Error loading GitHub branches " . self::branchesURL;
         throw new \WireException($error);
         $this->error($error);
         return array();
     }
     $data = json_decode($json, true);
     if (!$data) {
         $error = "Error JSON decoding GitHub branches " . self::branchesURL;
         throw new \WireException($error);
         $this->error($error);
         return array();
     }
     foreach ($data as $key => $info) {
         $name = $info['name'];
         $branch = array('name' => $name, 'title' => ucfirst($name), 'zipURL' => str_replace('{branch}', $name, self::zipURL), 'version' => '', 'versionURL' => str_replace('{branch}', $name, self::versionURL));
         if ($name == 'dev') {
             $branch['title'] = 'Development';
         }
         if ($name == 'master') {
             $branch['title'] = 'Stable/Master';
         }
         $content = $http->get($branch['versionURL']);
         if (!preg_match_all('/const\\s+version(Major|Minor|Revision)\\s*=\\s*(\\d+)/', $content, $matches)) {
             $branch['version'] = '?';
             continue;
         }
         $version = array();
         foreach ($matches[1] as $key => $var) {
             $version[$var] = (int) $matches[2][$key];
         }
         $branch['version'] = "{$version['Major']}.{$version['Minor']}.{$version['Revision']}";
         $branches[$name] = $branch;
     }
     return $branches;
 }