Advertisement
Guest User

DU

a guest
Dec 20th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. <?php
  2.  
  3. class DropboxUploader {
  4.     protected $email;
  5.     protected $password;
  6.     protected $caCertSourceType = self::CACERT_SOURCE_SYSTEM;
  7.     const CACERT_SOURCE_SYSTEM = 0;
  8.     const CACERT_SOURCE_FILE = 1;
  9.     const CACERT_SOURCE_DIR = 2;
  10.     protected $caCertSource;
  11.     protected $loggedIn = false;
  12.     protected $cookies = array();
  13.  
  14.     public function __construct($email, $password) {
  15.         // Check requirements
  16.         if (!extension_loaded('curl'))
  17.             throw new Exception('DropboxUploader requires the cURL extension.');
  18.        
  19.         $this->email = $email;
  20.         $this->password = $password;
  21.     }
  22.    
  23.     public function setCaCertificateFile($file)
  24.     {
  25.         $this->caCertSourceType = self::CACERT_SOURCE_FILE;
  26.         $this->caCertSource = $file;
  27.     }
  28.    
  29.     public function setCaCertificateDir($dir)
  30.     {
  31.         $this->caCertSourceType = self::CACERT_SOURCE_DIR;
  32.         $this->caCertSource = $dir;
  33.     }
  34.    
  35.     public function upload($filename, $remoteDir='/') {
  36.         if (!file_exists($filename) or !is_file($filename) or !is_readable($filename))
  37.             throw new Exception("File '$filename' does not exist or is not readable.");
  38.        
  39.         if (!is_string($remoteDir))
  40.             throw new Exception("Remote directory must be a string, is ".gettype($remoteDir)." instead.");
  41.        
  42.         if (!$this->loggedIn)
  43.             $this->login();
  44.        
  45.         $data = $this->request('https://www.dropbox.com/home');
  46.         $token = $this->extractToken($data, 'https://dl-web.dropbox.com/upload');
  47.        
  48.         $data = $this->request('https://dl-web.dropbox.com/upload', true, array('plain'=>'yes', 'file'=>'@'.$filename, 'dest'=>$remoteDir, 't'=>$token));
  49.         if (strpos($data, 'HTTP/1.1 302 FOUND') === false)
  50.             throw new Exception('Upload failed!');
  51.     }
  52.    
  53.     protected function login() {
  54.         $data = $this->request('https://www.dropbox.com/login');
  55.         $token = $this->extractToken($data, '/login');
  56.        
  57.         $data = $this->request('https://www.dropbox.com/login', true, array('login_email'=>$this->email, 'login_password'=>$this->password, 't'=>$token));
  58.        
  59.         if (stripos($data, 'location: /home') === false)
  60.             throw new Exception('Login unsuccessful.');
  61.        
  62.         $this->loggedIn = true;
  63.     }
  64.  
  65.     protected function request($url, $post=false, $postData=array()) {
  66.         $ch = curl_init();
  67.         curl_setopt($ch, CURLOPT_URL, $url);
  68.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  69.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  70.         switch ($this->caCertSourceType) {
  71.             case self::CACERT_SOURCE_FILE:
  72.                 curl_setopt($ch, CURLOPT_CAINFO, $this->caCertSource);
  73.                 break;
  74.             case self::CACERT_SOURCE_DIR:
  75.                 curl_setopt($ch, CURLOPT_CAPATH, $this->caCertSource);
  76.                 break;
  77.         }
  78.         curl_setopt($ch, CURLOPT_HEADER, 1);
  79.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  80.         if ($post) {
  81.             curl_setopt($ch, CURLOPT_POST, $post);
  82.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  83.         }
  84.        
  85.         // Send cookies
  86.         $rawCookies = array();
  87.         foreach ($this->cookies as $k=>$v)
  88.             $rawCookies[] = "$k=$v";
  89.         $rawCookies = implode(';', $rawCookies);
  90.         curl_setopt($ch, CURLOPT_COOKIE, $rawCookies);
  91.        
  92.         $data = curl_exec($ch);
  93.        
  94.         if ($data === false)
  95.             throw new Exception('Cannot execute request: '.curl_error($ch));
  96.        
  97.         // Store received cookies
  98.         preg_match_all('/Set-Cookie: ([^=]+)=(.*?);/i', $data, $matches, PREG_SET_ORDER);
  99.         foreach ($matches as $match)
  100.             $this->cookies[$match[1]] = $match[2];
  101.        
  102.         curl_close($ch);
  103.        
  104.         return $data;
  105.     }
  106.  
  107.     protected function extractToken($html, $formAction) {
  108.         if (!preg_match('/<form [^>]*'.preg_quote($formAction, '/').'[^>]*>.*?(<input [^>]*name="t" [^>]*value="(.*?)"[^>]*>).*?<\/form>/is', $html, $matches) || !isset($matches[2]))
  109.             throw new Exception("Cannot extract token! (form action=$formAction)");
  110.         return $matches[2];
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement