public function generate() { if (PHP_SAPI != 'cli') { throw new \Exception("This script only can be used in CLI"); } $config = $this->config->database; system('/usr/bin/mysqldump -u ' . $config->username . ' -p' . $config->password . ' -r /tmp/phosphorum.sql ' . $config->dbname); system('bzip2 /tmp/phosphorum.sql'); $sourcePath = '/tmp/phosphorum.sql.bz2'; if (!file_exists($sourcePath)) { throw new \Exception("Backup could not be created"); } list($accessToken, $host) = AuthInfo::loadFromJsonFile(APP_PATH . '/app/config/backup.auth'); $client = new Client($accessToken, "phosphorum", null, $host); $dropboxPath = '/phosphorum.sql.bz2'; $pathError = Path::findErrorNonRoot($dropboxPath); if ($pathError !== null) { throw new \Exception("Invalid <dropbox-path>: {$pathError}"); } try { $client->delete($dropboxPath); } catch (\Exception $e) { // ... } $size = null; if (\stream_is_local($sourcePath)) { $size = \filesize($sourcePath); } $fp = fopen($sourcePath, "rb"); $client->uploadFile($dropboxPath, WriteMode::add(), $fp, $size); fclose($fp); @unlink($sourcePath); }
/** * (non-PHPDoc) * * @see \phpbu\App\Backup\Sync::sync() * @param \phpbu\App\Backup\Target $target * @param \phpbu\App\Result $result * @throws \phpbu\App\Backup\Sync\Exception */ public function sync(Target $target, Result $result) { $sourcePath = $target->getPathname(); $dropboxPath = $this->path . $target->getFilename(); $client = new DropboxApi\Client($this->token, "phpbu/1.1.0"); $pathError = DropboxApi\Path::findErrorNonRoot($dropboxPath); if (substr(__FILE__, 0, 7) == 'phar://') { DropboxApi\RootCertificates::useExternalPaths(); } if ($pathError !== null) { throw new Exception(sprintf('Invalid \'dropbox-path\': %s', $pathError)); } $size = null; if (stream_is_local($sourcePath)) { $size = filesize($sourcePath); } try { $fp = fopen($sourcePath, 'rb'); $res = $client->uploadFile($dropboxPath, DropboxApi\WriteMode::add(), $fp, $size); fclose($fp); } catch (\Exception $e) { throw new Exception($e->getMessage(), null, $e); } $result->debug('upload: done (' . $res['size'] . ')'); }
public function getLastCursor($pathPrefix = '/') { dbx\Path::checkArgOrNull("pathPrefix", $pathPrefix); $response = $this->doPost($this->host->getApi(), "1/delta/latest_cursor", array("path_prefix" => $pathPrefix)); if ($response->statusCode !== 200) { throw dbx\RequestUtil::unexpectedStatus($response); } if ($result = dbx\RequestUtil::parseResponseJson($response->body)) { $result['path'] = $pathPrefix; } return $result; }
/** * {@inheritdoc} */ public function upload($archive) { $fileName = explode('/', $archive); $pathError = Dropbox\Path::findErrorNonRoot($this->remotePath); if ($pathError !== null) { throw new UploadException(sprintf('Invalid path "%s".', $archive)); } $client = new Dropbox\Client($this->access_token, 'CloudBackupBundle'); $size = filesize($archive); $fp = fopen($archive, 'rb'); $client->uploadFile($this->remotePath . '/' . end($fileName), Dropbox\WriteMode::add(), $fp, $size); fclose($fp); }
function testPath() { $good = array("/", "/hello", "/", "/hello-⢬"); $bad = array("hello", "/hello/", "/hello-𐍈", "/hello-�", "/hello-�", "/hello-�", "/hello-��"); foreach ($good as $path) { dbx\Path::checkArg('whatever', $path); } foreach ($bad as $path) { try { dbx\Path::checkArg('whatever', $path); assert(false, "Failed on " . $path); } catch (\InvalidArgumentException $ex) { // This is what we expect. } } }
require_once __DIR__ . '/helper.php'; use Dropbox as dbx; list($client, $dropboxPath) = parseArgs("get-metadata", $argv, array(array("dropbox-path", "The path (on Dropbox) that you want metadata for."))); $pathError = dbx\Path::findError($dropboxPath); if ($pathError !== null) { fwrite(STDERR, "Invalid <dropbox-path>: {$pathError}\n"); die; } $metadata = $client->getMetadataWithChildren($dropboxPath); if ($metadata === null) { fwrite(STDERR, "No file or folder at that path.\n"); die; } // If it's a folder, remove the 'contents' list from $metadata; print that stuff out after. $children = null; if ($metadata['is_dir']) { $children = $metadata['contents']; unset($metadata['contents']); } print_r($metadata); if ($children !== null && count($children) > 0) { print "Children:\n"; foreach ($children as $child) { $name = dbx\Path::getName($child['path']); if ($child['is_dir']) { $name = "{$name}/"; } // Put a "/" after folder names. print "- {$name}\n"; } }
#!/usr/bin/env php <?php require_once __DIR__ . '/helper.php'; use Dropbox as dbx; list($client, $dropboxPath, $localPath) = parseArgs("download-file", $argv, array(array("dropbox-path", "The path of the file (on Dropbox) to download."), array("local-path", "The local path to save the downloaded file contents to."))); $pathError = dbx\Path::findErrorNonRoot($dropboxPath); if ($pathError !== null) { fwrite(STDERR, "Invalid <dropbox-path>: {$pathError}\n"); die; } $metadata = $client->getFile($dropboxPath, fopen($localPath, "wb")); if ($metadata === null) { fwrite(STDERR, "File not found on Dropbox.\n"); die; } print_r($metadata); echo "File contents written to \"{$localPath}\"\n";
$errors = ''; foreach ($all_entries as $key => $value) { foreach ($all_entries[$key] as $entry) { list($dropbox_path, $metadata) = $entry; if (preg_match($preg_match_filter, $dropbox_path)) { if ($metadata === null) { // (-) $count_removes++; } else { // (+) if ($metadata['is_dir'] == 1) { $count_dirs++; } else { // add_file $local_path = $output_path . $metadata['path']; $path_error = dbx\Path::findErrorNonRoot($dropbox_path); if ($path_error !== null) { $error = "invalid <dropbox_path>: {$dropbox_path} ({$path_error}) \n"; $errors .= $error; } $original_bytes = $dropbox_path_file_to_meta[$dropbox_path]['bytes']; if (file_exists($local_path) && $original_bytes == filesize($local_path)) { $count_old_adds++; } else { $count_new_adds++; // if the dir does not exist create it $dirname = dirname($local_path); if (!is_dir($dirname)) { mkdir($dirname, 0755, true); } if ($report_to_screen == 1) {
#!/usr/bin/env php <?php require_once __DIR__ . '/helper.php'; use Dropbox as dbx; /* @var dbx\Client $client */ /* @var string $dropboxPath */ list($client, $dropboxPath) = parseArgs("link", $argv, array(array("dropbox-path", "The path (on Dropbox) to create a link for."))); $pathError = dbx\Path::findError($dropboxPath); if ($pathError !== null) { fwrite(STDERR, "Invalid <dropbox-path>: {$pathError}\n"); die; } $link = $client->createShareableLink($dropboxPath); print $link . "\n";
public function create_object($is_file, $folder, $objname, $str, $mimetype) { // http://dropbox.github.io/dropbox-sdk-php/api-docs/v1.1.x/class-Dropbox.Client.html#_uploadFile if ($is_file) { $pathError = dbx\Path::findErrorNonRoot($folder . "/" . $objname); if ($pathError !== null) { fwrite(STDERR, "Invalid <dropbox-path>: {$pathError}\n"); die; } $size = null; if (\stream_is_local($sourcePath)) { $size = \filesize($sourcePath); } $fp = fopen($objname, "rb"); $metadata = $this->store->uploadFile($this->key_url($folder, $objname), dbx\WriteMode::add(), $fp, $size); fclose($fp); //print_r($metadata); } else { $metadata = $this->store->uploadFileFromString($this->key_url($folder, $objname), dbx\WriteMode::add(), '' . $str); //print_r($metadata); } }
public function compile() { if (TL_MODE == 'FE') { $strHref = \Environment::get('request'); // Remove an existing file parameter (see #5683) if (preg_match('/(&(amp;)?|\\?)' . $this->strQsParam . '=/', $strHref)) { $strHref = preg_replace('/(&(amp;)?|\\?)' . $this->strQsParam . '=[^&]+/', '', $strHref); } $strDownloadBaseUrl = $strHref . (\Config::get('disableAlias') || strpos($strHref, '?') !== false ? '&' : '?') . $this->strQsParam . '='; //$appInfo = new dbx\AppInfo(\Config::get('dropboxApiKey'), \Config::get('dropboxApiSecret')); //$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0"); //$authorizeUrl = $webAuth->start(); $accessToken = \Config::get('dropboxAccessToken'); $dbxClient = new \DropboxClient($accessToken, "PHP-Example/1.0"); // ========================================================================================================= // get metadata by chooser // ========================================================================================================= if ($this->dropboxChooserFiles !== '') { $children = json_decode($this->dropboxChooserFiles, true); // foreach ($filesList as $f) // { // $f['path'] = $f['name']; // //$f['download_link'] = $dbxClient->createTemporaryDirectLink($f['link']); // } //$this->Template->filesInFolder = $filesList; } // $previewLink = 'https://www.dropbox.com/s/wq6u9zx1psa9nrh/2012-11-26%201%20Battesimo.pdf?dl=0'; // $metadata = $dbxClient->getMetadataSharedLink($previewLink); // ========================================================================================================= // get metadata by path // ========================================================================================================= //$metadata = $dbxClient->getMetadataWithChildren(urldecode($this->dropboxPath)); // $pathError = dbx\Path::findError($this->dropboxPath); // if ($pathError !== null) // { // $this->Template->errors = "Invalid <dropbox-path>: $pathError"; // return; // } Dump($children); if ($this->dropboxPath !== '') { if ($metadata === null) { $this->Template->errors = "No file or folder at that path."; return; } // If it's a folder, remove the 'contents' list from $metadata; print that stuff out after. $children = null; if ($metadata['is_dir']) { $children = $metadata['contents']; unset($metadata['contents']); } $this->Template->metadata = $metadata; } $ff = array(); foreach ($children as $f) { if (array_key_exists('path', $f)) { $name = dbx\Path::getName($f['path']); } else { $name = $f['name']; } // Put a "/" after folder names. $f['name'] = $name . ($f['is_dir'] ? '/' : ''); if ($f['is_dir']) { $f['download_link'] = ''; } else { $f['download_link'] = $strDownloadBaseUrl . \System::urlEncode($name); } $ff[] = $f; } $this->Template->filesInFolder = $ff; Dump($GLOBALS['TL_DCA']['tl_content']['palettes']['__selector__']); } }
public function getMetadataWithChildren($path, $params = array("list" => "true", "file_limit" => "25000", "include_media_info" => "true")) { Path::checkArg("path", $path); return $this->_getMetadata($path, $params); }