/** * Export to Yaml file. * * @param string $file * @param bool $autoCreate * * @return Result */ public function exportToYaml($file, $autoCreate = true) { Craft::app()->config->maxPowerCaptain(); $result = new Result(); $dataModel = $this->exportDataModel(); $yaml = Data::toYaml($dataModel); if (!IOHelper::writeToFile($file, $yaml, $autoCreate)) { // Do not auto create $result->addError('errors', "Failed to write contents to \"{$file}\""); } return $result; }
/** * Partially downloads a file from a remote source * * @param string $url The file's URL. * @param string $path The path to write the file to. * @param int $size The max. bytes to be downloaded before the file gets truncated. * * @return bool */ private function _downloadRemoteFile($url, $path, $size) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if ($size != null) { curl_setopt($ch, CURLOPT_RANGE, '0-' . $size); } $response = curl_exec($ch); curl_close($ch); IOHelper::writeToFile($path, $response); return true; }
/** * Download */ public function download($pluginHandle) { Craft::log(__METHOD__, LogLevel::Info, true); // ------------------------------- // Get ready to download & unzip // ------------------------------- $return = array('success' => false); $pluginComponent = craft()->plugins->getPlugin($pluginHandle, false); // plugin path $pluginZipDir = CRAFT_PLUGINS_PATH . "_" . $pluginHandle . "/"; $pluginZipPath = CRAFT_PLUGINS_PATH . "_" . $pluginHandle . ".zip"; // remote plugin zip url $remotePlugin = $this->_getRemotePlugin($pluginHandle); if (!$remotePlugin) { $return['msg'] = "Couldn't get plugin last version"; Craft::log(__METHOD__ . ' : Could not get last version', LogLevel::Info, true); return $return; } $remotePluginZipUrl = $remotePlugin['xml']->enclosure['url']; // ------------------------------- // Download & Install // ------------------------------- try { // download remotePluginZipUrl to pluginZipPath $client = new \Guzzle\Http\Client(); $request = $client->get($remotePluginZipUrl); $response = $request->send(); $body = $response->getBody(); // Make sure we're at the beginning of the stream. $body->rewind(); // Write it out to the file IOHelper::writeToFile($pluginZipPath, $body->getStream(), true); // Close the stream. $body->close(); // unzip pluginZipPath into pluginZipDir Zip::unzip($pluginZipPath, $pluginZipDir); $contents = IOHelper::getFiles($pluginZipDir); $pluginUnzipDir = false; foreach ($contents as $content) { if (strrpos($content, "__MACOSX") !== 0) { $pluginUnzipDir = $content; } } // move files we want to keep from their current location to unzipped location // keep : .git if (file_exists(CRAFT_PLUGINS_PATH . $pluginHandle . '/.git') && !$pluginUnzipDir . '/.git') { IOHelper::rename(CRAFT_PLUGINS_PATH . $pluginHandle . '/.git', $pluginUnzipDir . '/.git'); } //rename($path, $newName, $suppressErrors = false) // remove current files // make a backup of existing plugin (to storage ?) ? //deleteFolder($path, $suppressErrors = false) IOHelper::deleteFolder(CRAFT_PLUGINS_PATH . $pluginHandle); // move new files to final destination IOHelper::rename($pluginUnzipDir . '/' . $pluginHandle . '/', CRAFT_PLUGINS_PATH . $pluginHandle); // delete zip IOHelper::deleteFile($pluginZipPath); } catch (\Exception $e) { $return['msg'] = $e->getMessage(); Craft::log(__METHOD__ . ' : Crashed : ' . $e->getMessage(), LogLevel::Info, true); return $return; } // remove download files try { IOHelper::deleteFolder($pluginZipDir); IOHelper::deleteFolder($pluginZipPath); } catch (\Exception $e) { $return['msg'] = $e->getMessage(); Craft::log(__METHOD__ . ' : Crashed : ' . $e->getMessage(), LogLevel::Info, true); return $return; } Craft::log(__METHOD__ . ' : Success : ', LogLevel::Info, true); $return['success'] = true; return $return; }