You are here

function _ftp_cdn_cron_sync_deletion in CDN 5

1 call to _ftp_cdn_cron_sync_deletion()
ftp_cdn_cron_perform_sync in sync_plugins/ftp.inc
Implementation of pseudo-hook hook_cdn_cron_perform_sync().

File

sync_plugins/ftp.inc, line 91
FTP synchronization plugin for the CDN integration module.

Code

function _ftp_cdn_cron_sync_deletion($fs, $files) {
  $deletes = 0;
  $files_on_server = _ftp_cdn_get_filelist($fs, '.');
  if (!empty($files_on_server)) {
    $files_to_be_deleted = array_diff(array_keys($files_on_server), array_values($files));
    foreach ($files_to_be_deleted as $filepath) {
      $dirs = explode('/', $filepath);
      $file = end($dirs);
      unset($dirs[count($dirs) - 1]);
      $path = implode('/', $dirs);
      ftp_chdir($fs, $path);
      if (ftp_delete($fs, $file)) {
        cdn_log("Deleted {$path}/{$file}.");
        $deletes++;
      }
      else {
        cdn_log("Failed to deleted {$path}/{$file}.");
      }

      // Go back to the original working directory, deleting empty directories
      // on the go.
      $success = TRUE;
      for ($i = count($dirs) - 1; $i >= 0; $i--) {
        ftp_chdir($fs, '..');
        $list = ftp_nlist($fs, $dirs[$i]);
        if ($success && empty($list)) {
          if ($success = ftp_rmdir($fs, $dirs[$i])) {
            cdn_log("Deleted empty directory " . implode('/', $dirs) . ".");
            $deletes++;
          }
          else {
            cdn_log("Failed to deleted empty directory " . implode('/', $dirs) . ".");
          }
          unset($dirs[$i]);
        }
      }
    }
  }
  return $deletes;
}