/**
  * will ask the target server to return the file list and the data object list
  * @param type $request
  */
 public function run($request)
 {
     if (!$this->config()->target) {
         throw new Exception('Target not found in yml file. See readme.md for installation instructions.');
     }
     if (!$this->config()->key) {
         throw new Exception('Key not found in yml file. See readme.md for installation instructions.');
     }
     $myurl = Director::absoluteURL('/remoteassetdiff') . '/' . urlencode($this->config()->key);
     $downloadurl = Director::absoluteURL('/remoteassetdownload') . '/' . urlencode($this->config()->key) . '?m=' . time();
     // download without javascript
     if (Director::is_cli()) {
         ini_set('memory_limit', '1024M');
         set_time_limit(0);
         echo "Creating list of files to download" . PHP_EOL;
         $listoffiles = RemoteAssetTask::DownloadFile($myurl);
         $fullist = json_decode($listoffiles);
         if (!is_array($fullist->download)) {
             throw new Exception('Failure to download list of files');
         }
         foreach ($fullist->download as $file) {
             echo "Downloading {$file} ... ";
             try {
                 RemoteAssetTask::DownloadFile($downloadurl . '&download=' . $file);
                 echo "Success" . PHP_EOL;
             } catch (Exception $e) {
                 echo "Failure" . PHP_EOL;
             }
         }
         echo "Done" . PHP_EOL;
         return;
     }
     echo ArrayData::create(array('FetchURL' => $myurl, 'DownloadURL' => $downloadurl, 'Target' => $this->config()->target, 'ToMachine' => Director::absoluteURL('/')))->renderWith('RemoteAssetTask');
 }
 /**
  * Outputs the list of files, and the ID and edit dates of the Data Objects
  * @param SS_HTTPRequest $request checks the access code to make sure no
  * public spoofing
  */
 public function view(SS_HTTPRequest $request)
 {
     // close the session to allow concurrent requests
     session_write_close();
     // bump up the memory
     ini_set('memory_limit', '1024M');
     set_time_limit(0);
     $params = $request->allParams();
     // note the url format for the key code is domain.com/remoteassetsync/<keyhere>
     if (!isset($params['AccessKey'])) {
         throw new Exception('Access key not set. See Readme.md for instructions on how to set this in your yml file.');
     }
     $config = Config::inst()->forClass('RemoteAssetTask');
     if ($config->key != $params['AccessKey']) {
         throw new Exception('Key missmatch');
     }
     $myurl = Controller::join_links($config->target, 'remoteassetlist/', urlencode($config->key)) . '?m=' . time();
     // fetch the remote list of files to check
     try {
         $remotefiles = RemoteAssetTask::DownloadFile($myurl, 60000);
     } catch (Exception $e) {
         throw new SS_HTTPResponse_Exception(json_encode($e->getMessage()), 400);
     }
     // decode
     $remotejson = json_decode($remotefiles);
     // list local files
     $list = array();
     RemoteAssetListController::recurseDir('../assets', $list);
     // these are the files that are different from your list
     //$downloadlist = array_diff($remotejson, $list);
     //http://stackoverflow.com/questions/2985799/handling-large-arrays-with-array-diff
     $downloadlist = $this->leo_array_diff($remotejson, $list);
     // if you're ignoring a file, remove it from the download list
     $ignorelist = array();
     $downloadlistlen = strlen('../assets');
     foreach ($downloadlist as $key => $file) {
         foreach ($config->excludedfolders as $ignoredterm) {
             if (strpos($file, $ignoredterm) === $downloadlistlen) {
                 $ignorelist[] = $file;
                 unset($downloadlist[$key]);
             }
         }
     }
     echo '{"download":';
     print_r(json_encode(array_values($downloadlist)));
     echo ',"ignored":';
     print_r(count($ignorelist));
     echo ',"synced":';
     print_r(count($remotejson) - count($downloadlist));
     echo '}';
 }
 /**
  * Downloads a single file
  * @param SS_HTTPRequest $request checks the access code to make sure no
  * public spoofing
  */
 public function view(SS_HTTPRequest $request)
 {
     // close the session to allow concurrent requests
     session_write_close();
     // bump up the memory
     ini_set('memory_limit', '1024M');
     set_time_limit(0);
     $params = $request->allParams();
     // note the url format for the key code is domain.com/remoteassetsync/<keyhere>
     if (!isset($params['AccessKey'])) {
         throw new Exception('Access key not set. See Readme.md for instructions on how to set this in your yml file.');
     }
     $config = Config::inst()->forClass('RemoteAssetTask');
     if ($config->key != $params['AccessKey']) {
         throw new Exception('Key missmatch');
     }
     // download a file
     if (!$request->getVar('download')) {
         throw new Exception('Download file note set.');
     }
     $targetfile = preg_replace('/^\\.\\.\\//', '', $request->getVar('download'));
     $targetfileencoded = implode('/', array_map('rawurlencode', explode('/', $targetfile)));
     $myurl = Controller::join_links($config->target, $targetfileencoded);
     try {
         $filecontents = RemoteAssetTask::DownloadFile($myurl);
     } catch (Exception $e) {
         throw new SS_HTTPResponse_Exception($e->getMessage(), 400);
     }
     if (!$filecontents) {
         throw new SS_HTTPResponse_Exception('Failed to download.', 400);
     }
     // create new folder if none exists
     $dirname = dirname($request->getVar('download'));
     if (!is_dir($dirname)) {
         mkdir($dirname, 0777, true);
     }
     // remove old file before saving
     if (file_exists($request->getVar('download'))) {
         unlink($request->getVar('download'));
     }
     if (!file_put_contents($request->getVar('download'), $filecontents)) {
         throw new SS_HTTPResponse_Exception('Failed to write contents.', 400);
     }
     return json_encode('Success!');
 }