Example #1
0
 /**
  * Saves the image to the remote server. If the folder structure doesn't exist, create it.
  *
  * @param string $relFilename	path (with filename) from the CDN root
  * @param string $tempfile		temp file name to upload
  * @return bool
  */
 protected function _save($relFilename, $tempfile)
 {
     $base = Mage::getStoreConfig('imagecdn/ftp/base');
     $remotePath = str_replace('\\', '/', str_replace('//', '/', '/' . $base . '/' . $relFilename));
     ssh2_sftp_mkdir(ssh2_sftp($this->auth()), substr($remotePath, 0, strrpos($remotePath, '/')), 0777, true);
     $result = ssh2_scp_send($this->auth(), $tempfile, $remotePath, 0644);
     return $result ? true : false;
 }
Example #2
0
 /**
  * sftp_store_file($fileContainer)
  * 
  * This function stores an image and its thumbnails to the right
  * SFTP accounts.
  *
  * @param image
  * @return nothing, for now
  */
 function sftp_store_file($fileContainer)
 {
     // adds the original url to the list of thumbs, as only this list of thumbnails is used to upload files
     if (isset($fileContainer->original_path) && strlen($fileContainer->original_path)) {
         $fileContainer->thumb_paths[] = $fileContainer->original_path;
     }
     // gets one or more servers where this image will be uploaded. The servers are selected based on different
     // factors and rules, which are configured in config.inc.php
     $servers = $this->get_target_servers($fileContainer);
     if (sizeof($servers) && sizeof($fileContainer->thumb_paths)) {
         $relative_path_to_store = dirname(reset($fileContainer->thumb_paths));
         //echo "rel: ".$relative_path_to_store."<br>\n";
         foreach ($servers as $server) {
             $conn_id = $this->get_sftp_connection_id($server);
             // this queries the open connections list for the right $conn_id.
             // If a connection to this server does not exist it will be created, then the connection id will be returned
             ssh2_sftp_mkdir($conn_id, $server['root_path'] . $relative_path_to_store, 0775, true);
             foreach ($fileContainer->thumb_paths as $local_file_path) {
                 $remote_filename = basename($local_file_path);
                 // gets only the filename from the path
                 if (!$this->sftp_put_file($conn_id, $server, $local_file_path, $server['root_path'] . $local_file_path)) {
                     echo "Couldn't upload file " . $local_file_path . "<br>\n";
                     return FALSE;
                     // TODO: Better error handling
                 }
             }
             // TODO: Check if all ids exist
             $sql_pic2server = "INSERT INTO {$this->config['TABLE_SFTP_PIC2SERVER']} SET pic_id='{$fileContainer->id}', server_id='{$server['id']}'";
             //echo "sql_pic2server: ".$sql_pic2server."<br>\n";
             cpg_db_query($sql_pic2server);
             $sql_quota = "UPDATE {$this->config['TABLE_SFTP_SERVERS']} SET used=used+{$fileContainer->total_filesize}, free=free-{$fileContainer->total_filesize} WHERE id='{$server['id']}'";
             //echo "sql_quota: ".$sql_quota."<br>\n";
             cpg_db_query($sql_quota);
             // TODO: Error handling
             // TODO: Rollback in case of errors
         }
         // foreach($fileContainer->thumb_paths as $fileContainer)
         // if $this->CONFIG['storage_sftp_keep_local_copy'] is set to false, delete the local files
         if (isset($this->config['storage_sftp_keep_local_copy']) && $this->config['storage_sftp_keep_local_copy'] == false) {
             if (sizeof($fileContainer->thumb_paths)) {
                 foreach ($fileContainer->thumb_paths as $local_file_path) {
                     if (is_file($local_file_path)) {
                         unlink($local_file_path);
                     }
                 }
             }
         }
     }
     // if(sizeof($servers) && sizeof($fileContainer->thumb_paths))// TODO: Else what? Error message?
 }
function sftp_walk($con, $sftp, $local_dir, $remote_dir)
{
    $dir = opendir($local_dir);
    ssh2_sftp_mkdir($sftp, $remote_dir, 0755, true);
    while (($file = readdir($dir)) !== false) {
        $local_file = $local_dir . '/' . $file;
        $remote_file = $remote_dir . '/' . $file;
        if (!is_dir($local_file)) {
            echo "Transferring {$local_file} to {$remote_file}\n";
            $scp_ret = ssh2_scp_send($con, $local_file, $remote_file, 0755);
            if (!$scp_ret) {
                fwrite(STDERR, "Failed to transfer {$local_file}.\n");
                exit(8);
            }
        } else {
            if ($file != "." && $file != "..") {
                sftp_walk($con, $sftp, $local_file, $remote_file);
            }
        }
    }
}
Example #4
0
 /**
  * Creates a new directory.
  * @param string $path directory path to be created.
  * @return bool whether the directory is created successfully
  */
 protected function createDirectory($path)
 {
     $storage = $this->getStorage();
     $sftp = $storage->getSftp();
     if (file_exists($this->composeSftpPath($path))) {
         return true;
     }
     $parentPath = dirname($path);
     // recurse if parent dir does not exist and we are not at the root of the file system.
     if (!file_exists($this->composeSftpPath($parentPath)) && $parentPath !== $path) {
         $this->createDirectory($parentPath);
     }
     if (!ssh2_sftp_mkdir($sftp, $path, $storage->filePermission)) {
         return false;
     }
     return ssh2_sftp_chmod($sftp, $path, $storage->filePermission);
 }
Example #5
0
 function mkdir($path, $chmod = false, $chown = false, $chgrp = false)
 {
     $path = $this->untrailingslashit($path);
     if (empty($path)) {
         return false;
     }
     if (!$chmod) {
         $chmod = FS_CHMOD_DIR;
     }
     if (!ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true)) {
         return false;
     }
     if ($chown) {
         $this->chown($path, $chown);
     }
     if ($chgrp) {
         $this->chgrp($path, $chgrp);
     }
     return true;
 }
 /**
  * Copies a file to the remote system
  *
  * @param string $local
  * @param string $remote 
  */
 protected function copyFile($localEndpoint, $remoteEndpoint)
 {
     ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), 2775, true);
     $ret = ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint);
     if ($ret === false) {
         throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'");
     }
 }
Example #7
0
 /**
  * Remote mkdir.
  *
  * @param string $dirname
  * @param int    $mode
  * @param bool   $recursive
  *
  * @return bool
  */
 public function mkdir($dirname, $mode, $recursive = false)
 {
     return ssh2_sftp_mkdir($this->resource, $dirname, $mode, $recursive);
 }
Example #8
0
 /**
  * Create a directory in remote server
  */
 public function mkdir($dirName)
 {
     $dir = $this->dir . '/' . $dirName;
     if (!ssh2_sftp_mkdir($this->_getSftp(), $dir)) {
         throw new \Exception("Could not create directory '{$dir}'");
     }
     return true;
 }
 function mkdir($path, $chmod = null, $chown = false, $chgrp = false)
 {
     $this->debug("mkdir();");
     $path = untrailingslashit($path);
     if (!ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true)) {
         return false;
     }
     if ($chown) {
         $this->chown($path, $chown);
     }
     if ($chgrp) {
         $this->chgrp($path, $chgrp);
     }
     return true;
 }
 /**
  * @param $local
  * @param $remote
  *
  * @throws BuildException
  */
 protected function copyFile($local, $remote)
 {
     $path = rtrim($this->todir, "/") . "/";
     if ($this->fetch) {
         $localEndpoint = $path . $remote;
         $remoteEndpoint = $local;
         $this->log('Will fetch ' . $remoteEndpoint . ' to ' . $localEndpoint, $this->logLevel);
         $ret = @ssh2_scp_recv($this->connection, $remoteEndpoint, $localEndpoint);
         if ($ret === false) {
             throw new BuildException("Could not fetch remote file '" . $remoteEndpoint . "'");
         }
     } else {
         $localEndpoint = $local;
         $remoteEndpoint = $path . $remote;
         if ($this->autocreate) {
             ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), is_null($this->mode) ? 0777 : $this->mode, true);
         }
         $this->log('Will copy ' . $localEndpoint . ' to ' . $remoteEndpoint, $this->logLevel);
         $ret = false;
         // If more than "$this->heuristicDecision" successfully send files by "ssh2.sftp" over "ssh2_scp_send"
         // then ship this step (task finish ~40% faster)
         if ($this->heuristicScpSftp < $this->heuristicDecision) {
             if (null !== $this->mode) {
                 $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint, $this->mode);
             } else {
                 $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint);
             }
         }
         // sometimes remote server allow only create files via sftp (eg. phpcloud.com)
         if (false === $ret && $this->sftp) {
             // mark failure of "scp"
             --$this->heuristicScpSftp;
             // try create file via ssh2.sftp://file wrapper
             $fh = @fopen("ssh2.sftp://{$this->sftp}/{$remoteEndpoint}", 'wb');
             if (is_resource($fh)) {
                 $ret = fwrite($fh, file_get_contents($localEndpoint));
                 fclose($fh);
                 // mark success of "sftp"
                 $this->heuristicScpSftp += 2;
             }
         }
         if ($ret === false) {
             throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'");
         }
     }
     $this->counter++;
 }
Example #11
0
 /**
  * Create a directory if it doesn't exist. The operation is implicitly recursive, i.e. it will create all
  * intermediate directories if they do not already exist.
  *
  * @param   string   $dirName      The full path of the directory to create
  * @param   integer  $permissions  The permissions of the created directory
  *
  * @return  boolean  True on success
  */
 public function mkdir($dirName, $permissions = 0755)
 {
     $targetDir = $this->translatePath($dirName);
     $targetDir = trim($targetDir, '/');
     $ret = @ssh2_sftp_mkdir($this->sftpHandle, $targetDir, $permissions, true);
     return true;
 }
 protected function doMkDir($remote_path)
 {
     $remote_path = ltrim($remote_path, '/');
     return ssh2_sftp_mkdir($this->getSftpConnection(), $remote_path);
 }
Example #13
0
 /**
  * @param $dirName
  * @param int $mode
  * @param bool $recursive
  * @return bool
  */
 public function mkdir($dirName, $mode = 0777, $recursive = FALSE)
 {
     return ssh2_sftp_mkdir($this->getSftpResource(), $dirName, $mode, $recursive);
 }
Example #14
0
 /**
  * Create a directory
  *
  * @version 1.0
  * @access  public
  * @param   string
  * @return  bool
  */
 public function mkdir($path = '', $permissions = '0777')
 {
     if ($path == '' || !$this->_is_conn()) {
         return FALSE;
     }
     $result = @ssh2_sftp_mkdir($this->sftp, $path, $permissions, TRUE);
     if ($result === FALSE) {
         if ($this->debug == TRUE) {
             $this->_error('sftp_unable_to_mkdir');
         }
         return FALSE;
     }
     return TRUE;
 }
Example #15
0
 public function createFolder(string $path, int $mode = 0777, bool $recursive = true) : bool
 {
     if (@ssh2_sftp_mkdir($this->connect, $path, $mode, $recursive)) {
         return true;
     } else {
         throw new FolderAllreadyException($path);
     }
 }
Example #16
0
 /**
  * Creates a new remote directory.
  *
  * @param string  $dir
  * @param boolean $recursive
  *
  * @return boolean Returns TRUE on success or FALSE on error
  *
  * @throws \BackBee\Util\Transport\Exception\TransportException Occures if SSH connection is invalid
  */
 public function mkdir($dir, $recursive = false)
 {
     if (null === $this->_sftp_resource) {
         throw new TransportException(sprintf('None SSH connection available.'));
     }
     $dir = $this->_getAbsoluteRemotePath($dir);
     if (false === @ssh2_sftp_mkdir($this->_sftp_resource, $dir, 0777, $recursive)) {
         return $this->_trigger_error(sprintf('Unable to make directory: %s.', $dir));
     }
     return true;
 }
Example #17
0
 private function _makeDirectory($dir)
 {
     $alldirs = explode('/', $dir);
     $previousDir = substr($this->_initdir, -1) == '/' ? substr($this->_initdir, 0, strlen($this->_initdir) - 1) : $this->_initdir;
     $previousDir = substr($previousDir, 0, 1) == '/' ? $previousDir : '/' . $previousDir;
     foreach ($alldirs as $curdir) {
         $check = $previousDir . '/' . $curdir;
         if (!@ssh2_sftp_stat($this->_sftphandle, $check)) {
             if (ssh2_sftp_mkdir($this->_sftphandle, $check, 0755, true) === false) {
                 $this->setError('Could not create directory ' . $check);
                 return false;
             }
         }
         $previousDir = $check;
     }
     return true;
 }
Example #18
0
 public function makeDirectory($directory, $recursive = false)
 {
     $directory = $this->path($directory);
     if ($this->isDirectory($directory)) {
         return true;
     }
     $return = @ssh2_sftp_mkdir($this->getSftpResource(), $directory, $this->getUmask(0777), $recursive);
     if (!$return) {
         throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to make directory "%s"', $directory));
     }
     return $return;
 }
Example #19
0
 /**
  * Attempts to open a connection to the SSH2 server.
  *
  * @throws Horde_Vfs_Exception
  */
 protected function _connect()
 {
     if ($this->_stream !== false) {
         return;
     }
     if (!extension_loaded('ssh2')) {
         throw new Horde_Vfs_Exception('The SSH2 PECL extension is not available.');
     }
     if (!is_array($this->_params)) {
         throw new Horde_Vfs_Exception('No configuration information specified for SSH2 VFS.');
     }
     $required = array('hostspec', 'username', 'password');
     foreach ($required as $val) {
         if (!isset($this->_params[$val])) {
             throw new Horde_Vfs_Exception(sprintf('Required "%s" not specified in VFS configuration.', $val));
         }
     }
     /* Connect to the ssh2 server using the supplied parameters. */
     if (empty($this->_params['port'])) {
         $this->_stream = @ssh2_connect($this->_params['hostspec']);
     } else {
         $this->_stream = @ssh2_connect($this->_params['hostspec'], $this->_params['port']);
     }
     if (!$this->_stream) {
         $this->_stream = false;
         throw new Horde_Vfs_Exception('Connection to SSH2 server failed.');
     }
     if (!@ssh2_auth_password($this->_stream, $this->_params['username'], $this->_params['password'])) {
         $this->_stream = false;
         throw new Horde_Vfs_Exception('Authentication to SSH2 server failed.');
     }
     /* Create sftp resource. */
     $this->_sftp = @ssh2_sftp($this->_stream);
     if (!empty($this->_params['vfsroot']) && !@ssh2_sftp_stat($this->_sftp, $this->_params['vfsroot']) && !@ssh2_sftp_mkdir($this->_sftp, $this->_params['vfsroot'])) {
         throw new Horde_Vfs_Exception(sprintf('Unable to create VFS root directory "%s".', $this->_params['vfsroot']));
     }
 }
Example #20
0
 public function mkdir($path)
 {
     $sftp = ssh2_sftp($this->conn_);
     return ssh2_sftp_mkdir($sftp, $path);
 }
Example #21
0
 public function createDirRecursive($dirName, $perms)
 {
     // Strip absolute filesystem path to website's root
     $removePath = AKFactory::get('kickstart.setup.destdir', '');
     if (!empty($removePath)) {
         // UNIXize the paths
         $removePath = str_replace('\\', '/', $removePath);
         $dirName = str_replace('\\', '/', $dirName);
         // Make sure they both end in a slash
         $removePath = rtrim($removePath, '/\\') . '/';
         $dirName = rtrim($dirName, '/\\') . '/';
         // Process the path removal
         $left = substr($dirName, 0, strlen($removePath));
         if ($left == $removePath) {
             $dirName = substr($dirName, strlen($removePath));
         }
     }
     if (empty($dirName)) {
         $dirName = '';
     }
     // 'cause the substr() above may return FALSE.
     $check = '/' . trim($this->dir, '/ ') . '/' . trim($dirName, '/');
     if ($this->is_dir($check)) {
         return true;
     }
     $alldirs = explode('/', $dirName);
     $previousDir = '/' . trim($this->dir, '/ ');
     foreach ($alldirs as $curdir) {
         if (!$curdir) {
             continue;
         }
         $check = $previousDir . '/' . $curdir;
         if (!$this->is_dir($check)) {
             // Proactively try to delete a file by the same name
             @ssh2_sftp_unlink($this->handle, $check);
             if (@ssh2_sftp_mkdir($this->handle, $check) === false) {
                 // If we couldn't create the directory, attempt to fix the permissions in the PHP level and retry!
                 $this->fixPermissions($check);
                 if (@ssh2_sftp_mkdir($this->handle, $check) === false) {
                     // Can we fall back to pure PHP mode, sire?
                     if (!@mkdir($check)) {
                         $this->setError(AKText::sprintf('FTP_CANT_CREATE_DIR', $check));
                         return false;
                     } else {
                         // Since the directory was built by PHP, change its permissions
                         @chmod($check, "0777");
                         return true;
                     }
                 }
             }
             @ssh2_sftp_chmod($this->handle, $check, $perms);
         }
         $previousDir = $check;
     }
     return true;
 }
Example #22
0
 /**
  * Creates a directory
  *
  * @param  string  $dirname   The name of the new directory
  * @param  int     $mod       The permissions on the new directory
  * @param  Boolean $recursive Whether to automatically create any required
  *                            parent directory
  *
  * @return Boolean
  */
 public function mkdir($dirname, $mod = 0777, $recursive = false)
 {
     return ssh2_sftp_mkdir($this->getResource(), $dirname, $mod, $recursive);
 }
Example #23
0
 /**
  * @{inheritDoc}
  */
 public function mkdir($dirname = '', $mode = 0777, $recursive = false)
 {
     return $this->doRun(__METHOD__, func_get_args(), ssh2_sftp_mkdir($this->sftp, $dirname, $mode, $recursive));
 }
Example #24
0
 protected function copyFile($local, $remote)
 {
     $path = rtrim($this->todir, "/") . "/";
     if ($this->fetch) {
         $localEndpoint = $path . $remote;
         $remoteEndpoint = $local;
         $this->log('Will fetch ' . $remoteEndpoint . ' to ' . $localEndpoint, $this->logLevel);
         $ret = @ssh2_scp_recv($this->connection, $remoteEndpoint, $localEndpoint);
         if ($ret === false) {
             throw new BuildException("Could not fetch remote file '" . $remoteEndpoint . "'");
         }
     } else {
         $localEndpoint = $local;
         $remoteEndpoint = $path . $remote;
         if ($this->autocreate) {
             ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), is_null($this->mode) ? 0777 : $this->mode, true);
         }
         $this->log('Will copy ' . $localEndpoint . ' to ' . $remoteEndpoint, $this->logLevel);
         if (!is_null($this->mode)) {
             $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint, $this->mode);
         } else {
             $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint);
         }
         if ($ret === false) {
             throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'");
         }
     }
     $this->counter++;
 }
	function mkdir($path, $chmod = null, $chown = false, $chgrp = false) {
		$path = untrailingslashit($path);
		$chmod = !empty($chmod) ? $chmod : $this->permission;
		if ( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) )
			return false;
		if ( $chown )
			$this->chown($path, $chown);
		if ( $chgrp )
			$this->chgrp($path, $chgrp);
		return true;
	}
Example #26
0
 /**
  * Create a new directory according to the current file operation method
  * @param string $path Path to new directory
  */
 public static function mkdir($path)
 {
     switch (self::$fileOper) {
         case 'ftp':
             if (!@ftp_mkdir(self::$_ftpStream, self::ftpStripChroot($path))) {
                 throw new Exception("Failed to create directory {$path}", self::ERR_FTPOPER);
             }
             break;
         case 'scp':
             if (!ssh2_sftp_mkdir(self::$_sftpStream, self::scpXlateRelpath($path))) {
                 throw new Exception("Failed to create directory {$path}", self::ERR_SCPOPER);
             }
             break;
         case 'php':
         default:
             if (!@mkdir($path)) {
                 throw new Exception("Failed to create directory {$path}");
             }
     }
 }
Example #27
0
 /**
  * Copy directory
  *
  * @param array $file_list File list to copy
  * @return Object
  */
 function _copyDir(&$file_list)
 {
     if (!$this->ftp_password) {
         return new Object(-1, 'msg_ftp_password_input');
     }
     $output = $this->_connect();
     if (!$output->toBool()) {
         return $output;
     }
     $target_dir = $this->ftp_info->ftp_root_path . $this->target_path;
     if (is_array($file_list)) {
         foreach ($file_list as $k => $file) {
             $org_file = $file;
             if ($this->package->path == ".") {
                 $file = substr($file, 3);
             }
             $path = FileHandler::getRealPath("./" . $this->target_path . "/" . $file);
             $pathname = dirname($target_dir . "/" . $file);
             if (!file_exists(FileHandler::getRealPath($real_path))) {
                 ssh2_sftp_mkdir($this->sftp, $pathname, 0755, TRUE);
             }
             ssh2_scp_send($this->connection, FileHandler::getRealPath($this->download_path . "/" . $org_file), $target_dir . "/" . $file);
         }
     }
     return new Object();
 }
Example #28
0
 /**
  * @param string $identifier
  * @param bool $recursive
  * @return string
  */
 public function createFolder($identifier, $recursive = true)
 {
     ssh2_sftp_mkdir($this->sftp, $identifier, $this->configuration[SftpDriver::CONFIG_FOLDER_MODE], $recursive);
     return $identifier;
 }
 /**
  * @see Filesystem::makeDir
  */
 public function makeDir($path, $chmod = false, $chown = false, $chgrp = false)
 {
     $path = rtrim($path, '/');
     if (!$chmod) {
         $chmod = FS_CHMOD_DIR;
     }
     if (!ssh2_sftp_mkdir($this->sftpConnection, $path, $chmod, true)) {
         return false;
     }
     if ($chown) {
         $this->chown($path, $chown);
     }
     if ($chgrp) {
         $this->chgrp($path, $chgrp);
     }
     return true;
 }
 /**
  * Copies a file to the remote system
  *
  * @param string $local
  * @param string $remote 
  */
 protected function copyFile($localEndpoint, $remoteEndpoint)
 {
     $port = $this->port ? " -P {$this->port} " : '';
     $this->exec("scp -i {$this->privkeyfile} {$port} {$localEndpoint} {$this->username}@{$this->host}:{$remoteEndpoint}");
     return;
     ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), 2775, true);
     $ret = ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint);
     if ($ret === false) {
         throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'");
     }
 }