/**
  * Returns cryptographically strong secure random bytes (as a PHP string).
  *
  * @param int $numBytes
  *    The number of bytes of random data to return.
  *
  * @return string
  */
 static function getRandomBytes($numBytes)
 {
     Checker::argIntPositive("numBytes", $numBytes);
     // openssl_random_pseudo_bytes had some issues prior to PHP 5.3.4
     if (function_exists('openssl_random_pseudo_bytes') && version_compare(PHP_VERSION, '5.3.4') >= 0) {
         $s = openssl_random_pseudo_bytes($numBytes, $isCryptoStrong);
         if ($isCryptoStrong) {
             return $s;
         }
     }
     if (function_exists('mcrypt_create_iv')) {
         return mcrypt_create_iv($numBytes);
     }
     // Hopefully the above two options cover all our users.  But if not, there are
     // other platform-specific options we could add.
     assert(False, "no suitable random number source available");
 }
Example #2
0
 /**
  * Creates a file on Dropbox, using the data from $inStream as the file contents.
  *
  * This version of <code>uploadFile</code> splits uploads the file ~4MB chunks at a time and
  * will retry a few times if one chunk fails to upload.  Uses {@link chunkedUploadStart()},
  * {@link chunkedUploadContinue()}, and {@link chunkedUploadFinish()}.
  *
  * @param string $path
  *    The Dropbox path to save the file to (UTF-8).
  *
  * @param WriteMode $writeMode
  *    What to do if there's already a file at the given path.
  *
  * @param resource $inStream
  *    The data to use for the file contents.
  *
  * @param int|null $numBytes
  *    The number of bytes available from $inStream.
  *    You can pass in <code>null</code> if you don't know.
  *
  * @param int|null $chunkSize
  *    The number of bytes to upload in each chunk.  You can omit this (or pass in
  *    <code>null</code> and the library will use a reasonable default.
  *
  * @return mixed
  *    The <a href="https://www.dropbox.com/developers/core/docs#metadata-details>metadata
  *    object</a> for the newly-added file.
  *
  * @throws Exception
  */
 function uploadFileChunked($path, $writeMode, $inStream, $numBytes = null, $chunkSize = null)
 {
     if ($chunkSize === null) {
         $chunkSize = self::$DEFAULT_CHUNK_SIZE;
     }
     Path::checkArgNonRoot("path", $path);
     WriteMode::checkArg("writeMode", $writeMode);
     Checker::argResource("inStream", $inStream);
     Checker::argNatOrNull("numBytes", $numBytes);
     Checker::argIntPositive("chunkSize", $chunkSize);
     return $this->_uploadFileChunked($path, $writeMode, $inStream, $numBytes, $chunkSize);
 }
 /**
  * Creates a file on Dropbox, using the data from $inStream as the file contents.
  *
  * This version of <code>uploadFile</code> splits uploads the file ~4MB chunks at a time and
  * will retry a few times if one chunk fails to upload.  Uses {@link chunkedUploadStart()},
  * {@link chunkedUploadContinue()}, and {@link chunkedUploadFinish()}.
  *
  * @param string $path
  *    The Dropbox path to save the file to (UTF-8).
  *
  * @param WriteMode $writeMode
  *    What to do if there's already a file at the given path.
  *
  * @param resource $inStream
  *    The data to use for the file contents.  This stream will be closed with
  *    <code>fclose</code>, whether the upload succeeds or not.
  *
  * @param int|null $numBytes
  *    The number of bytes available from $inStream.
  *    You can pass in <code>null</code> if you don't know.
  *
  * @param int|null $chunkSize
  *    The number of bytes to upload in each chunk.  You can omit this (or pass in
  *    <code>null</code> and the library will use a reasonable default.
  *
  * @return mixed
  *    The <a href="https://www.dropbox.com/developers/core/api#metadata-details>metadata
  *    object</a> for the newly-added file.
  *
  * @throws Exception
  */
 function uploadFileChunked($path, $writeMode, $inStream, $numBytes = null, $chunkSize = null)
 {
     try {
         if ($chunkSize === null) {
             $chunkSize = self::$DEFAULT_CHUNK_SIZE;
         }
         Path::checkArgNonRoot("path", $path);
         WriteMode::checkArg("writeMode", $writeMode);
         Checker::argResource("inStream", $inStream);
         Checker::argNatOrNull("numBytes", $numBytes);
         Checker::argIntPositive("chunkSize", $chunkSize);
         $metadata = $this->_uploadFileChunked($path, $writeMode, $inStream, $numBytes, $chunkSize);
     } catch (\Exception $ex) {
         fclose($inStream);
         throw $ex;
     }
     fclose($inStream);
     return $metadata;
 }