protected function doProcess($arguments = array(), $options = array()) { # DISABLED //$this->logBlock('DISABLED.', 'ERROR'); // exit; if (!$options['go']) $this->logBlock('Running in dry mode, no files will be uploaded.', 'ERROR'); $consumerKey = sfConfig::get("app_dropbox_consumer_key"); $consumerSecret = sfConfig::get("app_dropbox_consumer_secret"); $token = sfConfig::get("app_dropbox_token"); $token_secret = sfConfig::get("app_dropbox_token_secret"); $dropboxRoot = sfConfig::get("app_dropbox_root"); ProjectConfiguration::registerDropboxAPI(); // If the PHP OAuth extension is not available, you can try // PEAR's HTTP_OAUTH instead. $oauth = new Dropbox_OAuth_PEAR($consumerKey, $consumerSecret); $dropbox = new Dropbox_API( $oauth ); $tokens = array( "token" => $token, "token_secret" => $token_secret ); $oauth->setToken($tokens); // get DROPBOX directory listing $metadata = $dropbox->getMetaData($dropboxRoot, $list = true, $hash = null, $fileLimit = null, $root = null); $dropbox_list = array(); $this->logBlock(sprintf("Connected dropbox in %s", $dropboxRoot), 'COMMENT'); if (is_array( $metadata['contents'] )) { foreach( $metadata['contents'] as $info) { /* if ($info["mime_type"] == "application/pdf") { $filename = strtolower( basename( $info["path"] ) ); */ $filename = strtolower( basename( $info["path"] ) ); // https://www.dropbox.com/developers/docs $timestamp = strtotime($info["modified"]); /* $ftime = strptime($info["modified"], "%a, %d %b %Y %H:%M:%S %z"); $timestamp = mktime( $ftime['tm_hour'], $ftime['tm_min'], $ftime['tm_sec'], 1 , $ftime['tm_yday'] + 1, $ftime['tm_year'] + 1900 ); */ // time zone problem ?!? //$timestamp = strtotime("+2 hours", $timestamp); $dropbox_list["$filename"] = $timestamp; // } } } // compare DROPBOX contents to "live" files $push_files = array(); $dir = sfConfig::get('sf_data_dir').sfConfig::get('app_rockstep_pdf_dir'); $weeds = array('.', '..','.DS_Store'); $directory = array_diff(scandir($dir), $weeds); if (!$options['force']) { $this->logBlock(sprintf("Checking %d files in %s", count($directory), $dir), 'COMMENT'); } else { $this->logBlock(sprintf("Pushing %d files from %s", count($directory), $dir), 'COMMENT'); } foreach( $directory as $id => $filename ) { if (is_file($dir.$filename)) { if (!$options['force']) { if (array_key_exists(strtolower($filename), $dropbox_list)) { $this->logSection('file', sprintf("%s %s %s", $filename, filemtime($dir.$filename), $dropbox_list[strtolower($filename)] )); //echo date('Y-m-d H:m:s u', strtotime( $dropbox_list[strtolower($filename)] ))."--".date('Y-m-d H:m:s u', filemtime($dir.$filename)); if ( filemtime($dir.$filename) > $dropbox_list[strtolower($filename)] ) { //echo date('Y-m-d H:m:s', $dropbox_list[strtolower($filename)] )."--".date('Y-m-d H:m:s', filemtime($dir.$filename)); $push_files[] = $filename; } } else { $push_files[] = $filename; } } else { $push_files[] = $filename; } } } // PUSH to dropbox $error = 0; $success = 0; if (count($push_files) == 0) { $this->logBlock("Everything is up to date.", 'INFO'); $this->task->setErrorCode(304); $this->setOk(); } else { if (!$options['force']) $this->logBlock(sprintf("%d files have been modified", count($push_files)), 'INFO'); foreach ($push_files as $filename) { if ($options['go']) { // sync $this->logBlock(sprintf("try: %s => %s", $dir.$filename, $dropboxRoot.$filename), 'INFO'); $result = $dropbox->putFile($dropboxRoot.$filename, $dir.$filename); if($result['httpStatus'] == 200 ) { // set datemodified to the dropbox-date $metadata = $dropbox->getMetaData($dropboxRoot.$filename, $list = true, $hash = null, $fileLimit = null, $root = null); touch($dir.$filename, strtotime($metadata['modified']) ); //echo date('Y-m-d H:m:s u', strtotime($metadata['modified']))."--".date('Y-m-d H:m:s u', filemtime($dir.$filename)); //echo strtotime($metadata['modified']); $this->logSection('file+', sprintf("%s", $filename)); $success += 1; } else { $this->logSection('error', sprintf("%s", $filename)); $error += 1; } } else { // dry run $this->logSection('file+', sprintf("%s", $filename)); $success += 1; } } if ($options['go']) { if ($error > 0) { $this->logBlock(sprintf('Error Uploading on %d out of %d Files', $error, $success, $dropboxRoot), 'ERROR'); $this->task->setErrorCode(500); $this->setNOk($error); } else { $this->logBlock(sprintf('Uploaded %d Files to %s', $success, $dropboxRoot), 'INFO'); $this->task->setErrorCode(200); $this->setOk(); } } else { // dry run } } }