Beispiel #1
0
 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);
 }
Beispiel #2
0
/**
 * A helper function that checks for the correct number of command line arguments,
 * loads the auth-file, and creates a \Dropbox\Client object.
 *
 * It returns an array where the first element is the \Dropbox\Client object and the
 * rest of the elements are the arguments you wanted.
 */
function parseArgs($exampleName, $argv, $requiredParams = null, $optionalParams = null)
{
    if ($requiredParams === null) {
        $requiredParams = array();
    }
    if ($optionalParams === null) {
        $optionalParams = array();
    }
    $minArgs = 1 + count($requiredParams);
    $maxArgs = $minArgs + count($optionalParams);
    $programName = $argv[0];
    $args = \array_slice($argv, 1);
    // If no args.  Print help message.
    if (count($args) === 0) {
        // Construct the param list for the "Usage" line.
        $paramSpec = "";
        foreach ($requiredParams as $p) {
            $paramSpec .= " " . $p[0];
        }
        if (count($optionalParams) > 0) {
            $paramSpec .= " [";
            foreach ($optionalParams as $p) {
                $paramSpec .= " " . $p[0];
            }
            $paramSpec .= " ]";
        }
        echo "\n";
        echo "Usage: {$programName} auth-file{$paramSpec}\n";
        echo "\n";
        // Print out help for each param.
        printParamHelp("auth-file", "A file with authorization information.  You can use the \"examples/authorize.php\" " . "program to generate this file.");
        foreach (array_merge($requiredParams, $optionalParams) as $param) {
            list($paramName, $paramDesc) = $param;
            printParamHelp($paramName, $paramDesc);
        }
        echo "\n";
        echo "OPTIONS:\n";
        echo "\n";
        echo "--locale=...   (example: --locale=fr)\n";
        echo "     The locale you want the Dropbox API to use for localized strings.\n";
        echo "\n";
        exit(0);
    }
    $locale = null;
    // Parse out the option args.
    $nonOptionArgs = array();
    for ($i = 0; $i < count($args); $i++) {
        $arg = $args[$i];
        if ($arg === "-" || $arg === "--") {
            // No more options.  Put the rest on the list of non-option args.
            for ($i++; $i < count($args); $i++) {
                \array_push($nonOptionArgs, $args[$i]);
            }
            break;
        }
        if (startsWith($arg, "-")) {
            if (startsWith($arg, "--")) {
                $option = substr($arg, 2);
            } else {
                $option = substr($arg, 1);
            }
            $equalPos = \strpos($option, "=");
            if ($equalPos === false) {
                $optionName = $option;
                $optionArg = null;
            } else {
                $optionName = \substr($option, 0, $equalPos);
                $optionArg = \substr($option, $equalPos + 1);
            }
            if ($optionName === 'locale') {
                if ($optionArg === null) {
                    fwrite(STDERR, "\"locale\" option requires an argument.\n");
                    fwrite(STDERR, "Run with no arguments for help.\n");
                    die;
                }
                $locale = $optionArg;
                if (count($locale) === 0) {
                    fwrite(STDERR, "\"locale\" must not be empty.\n");
                    fwrite(STDERR, "Run with no arguments for help.\n");
                    die;
                }
            } else {
                fwrite(STDERR, "Unknown option: \"{$optionName}\".\n");
                fwrite(STDERR, "Run with no arguments for help.\n");
                die;
            }
        } else {
            \array_push($nonOptionArgs, $arg);
        }
    }
    $givenArgs = count($nonOptionArgs);
    // Make sure the argument count is compatible with the parameter count.
    if ($minArgs === $maxArgs) {
        if (count($nonOptionArgs) !== $minArgs) {
            fwrite(STDERR, "Expecting exactly {$minArgs} non-option arguments, got {$givenArgs}.\n");
            fwrite(STDERR, "Run with no arguments for help.\n");
            die;
        }
    } else {
        if ($givenArgs < $minArgs) {
            fwrite(STDERR, "Expecting at least {$minArgs} non-option arguments, got {$givenArgs}.\n");
            fwrite(STDERR, "Run with no arguments for help.\n");
            die;
        } else {
            if ($givenArgs > $maxArgs) {
                fwrite(STDERR, "Expecting at most {$maxArgs} non-option arguments, got {$givenArgs}.\n");
                fwrite(STDERR, "Run with no arguments for help.\n");
                die;
            }
        }
    }
    try {
        list($accessToken, $host) = dbx\AuthInfo::loadFromJsonFile($nonOptionArgs[0]);
    } catch (dbx\AuthInfoLoadException $ex) {
        fwrite(STDERR, "Error loading <auth-file>: " . $ex->getMessage() . "\n");
        die;
    }
    $client = new dbx\Client($accessToken, "examples-{$exampleName}", $locale, $host);
    // Fill in the extra/optional arg slots with nulls.
    $ret = array_slice($nonOptionArgs, 1);
    while (count($ret) < $maxArgs) {
        array_push($ret, null);
    }
    // Return the args they need, plus the $client object in front.
    array_unshift($ret, $client);
    return $ret;
}
 function testBadAuthJsonFields()
 {
     $minimal = array("access_token" => "an_access_token");
     // check that we detect every missing field
     foreach ($minimal as $key => $value) {
         $tmp = $minimal;
         unset($tmp[$key]);
         file_put_contents("test.json", json_encode($tmp, true));
         try {
             dbx\AuthInfo::loadFromJsonFile("test.json");
             $this->fail("Expected exception");
         } catch (dbx\AuthInfoLoadException $e) {
             // Expecting this exception.
         }
     }
     $correct = array("access_token" => "an_access_token", "host" => "test-server.com");
     // check that we detect non-string fields
     foreach ($correct as $key => $value) {
         $tmp = $correct;
         $tmp[$key] = 123;
         file_put_contents("test.json", json_encode($tmp, true));
         try {
             dbx\AuthInfo::loadFromJsonFile("test.json");
             $this->fail("Expected exception");
         } catch (dbx\AuthInfoLoadException $e) {
             // Expecting this exception.
         }
     }
 }
<?php

require_once "config/config.php";
require_once "dropbox-sdk/Dropbox/autoload.php";
use Dropbox as dbx;
header('Content-type: application/json');
if ((empty($access_code) || isset($_POST['access_code']) && $_POST['access_code'] == $access_code) && isset($_POST['id']) && is_numeric($_POST['id']) && isset($_FILES['files'])) {
    try {
        list($accessToken, $host) = dbx\AuthInfo::loadFromJsonFile("./config/dropbox.json");
        $locale = null;
        $client = new dbx\Client($accessToken, "upload-file", $locale, $host);
        if (is_uploaded_file($_FILES['files']['tmp_name'][0])) {
            $sourcePath = $_FILES['files']['tmp_name'][0];
            $dropboxPath = "/" . $_FILES['files']['name'][0];
        } else {
            print '{"files":[{"id":' . $_POST['id'] . ',"name":"","size":"","error":"PHP upload error #' . $_FILES['files']['error'][0] . ' occurred."}]}';
            die;
        }
        $pathError = dbx\Path::findErrorNonRoot($dropboxPath);
        if ($pathError !== null) {
            print '{"files":[{"id":' . $_POST['id'] . ',"name":"' . $dropboxPath . '","size":"","error":"Invalid <dropbox-path>: ' . $pathError . '"}]}';
            die;
        }
        $size = null;
        if (\stream_is_local($sourcePath)) {
            $size = \filesize($sourcePath);
        }
        $fp = fopen($sourcePath, "rb");
        $metadata = $client->uploadFile($dropboxPath, dbx\WriteMode::add(), $fp, $size);
        fclose($fp);
        if ($logging) {