protected function get_files()
 {
     $server = $this->config->config_data['common']['host'];
     $user = $this->config->config_data['common']['user'];
     $password = $this->config->config_data['common']['password'];
     $directory_remote = rtrim($this->config->config_data['import']['remote_basedir'], '/');
     $directory_local = rtrim($this->config->config_data['import']['local_path'], '/');
     $port = 22;
     if (!function_exists("ssh2_connect")) {
         die("function ssh2_connect doesn't exist");
     }
     if (!($connection = ssh2_connect($server, $port))) {
         echo "fail: unable to establish connection\n";
     } else {
         // try to authenticate with username root, password secretpassword
         if (!ssh2_auth_password($connection, $user, $password)) {
             echo "fail: unable to authenticate\n";
         } else {
             // allright, we're in!
             echo "okay: logged in...<br/>";
             // Enter "sftp" mode
             $sftp = @ssh2_sftp($connection);
             // Scan directory
             $files = array();
             echo "Scanning {$directory_remote}<br/>";
             $dir = "ssh2.sftp://{$sftp}{$directory_remote}";
             $handle = opendir($dir);
             while (false !== ($file = readdir($handle))) {
                 if (is_dir($file)) {
                     echo "Directory: {$file}<br/>";
                     continue;
                 }
                 /*						if ($this->debug)
                 						{
                 							$size = filesize("ssh2.sftp://$sftp$directory_remote/$file");
                 							echo "File $file Size: $size<br/>";
                 
                 							$stream = @fopen("ssh2.sftp://$sftp$directory_remote/$file", 'r');
                 							$contents = fread($stream, filesize("ssh2.sftp://$sftp$directory_remote/$file"));
                 							@fclose($stream);
                 							echo "CONTENTS: $contents<br/><br/>";
                 						}
                 */
                 $files[] = $file;
             }
             if ($this->debug) {
                 _debug_array($files);
             } else {
                 foreach ($files as $file_name) {
                     if (stripos($file_name, 'Px205') === 0) {
                         //		_debug_array($file_name);
                         $file_remote = "{$directory_remote}/{$file_name}";
                         $file_local = "{$directory_local}/{$file_name}";
                         $stream = fopen("ssh2.sftp://{$sftp}{$file_remote}", 'r');
                         $contents = fread($stream, filesize("ssh2.sftp://{$sftp}{$file_remote}"));
                         fclose($stream);
                         $fp = fopen($file_local, "wb");
                         fwrite($fp, $contents);
                         if (fclose($fp)) {
                             echo "File remote: {$file_remote} was copied to local: {$file_local}<br/>";
                             if (ssh2_sftp_unlink($sftp, "{$directory_remote}/archive/{$file_name}")) {
                                 echo "Deleted duplicate File remote: {$directory_remote}/archive/{$file_name}<br/>";
                             }
                             if (ssh2_sftp_rename($sftp, $file_remote, "{$directory_remote}/archive/{$file_name}")) {
                                 echo "File remote: {$file_remote} was moved to remote: {$directory_remote}/archive/{$file_name}<br/>";
                             } else {
                                 echo "ERROR! File remote: {$file_remote} failed to move to remote: {$directory_remote}/archive/{$file_name}<br/>";
                                 if (unlink($file_local)) {
                                     echo "Lokal file was deleted: {$file_local}<br/>";
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * @param $filename
  */
 public function delete($filename)
 {
     if ($this->isDir($filename)) {
         ssh2_sftp_rmdir($this->getSftpResource(), $filename);
     } else {
         ssh2_sftp_unlink($this->getSftpResource(), $filename);
     }
 }
Ejemplo n.º 3
0
 public function delete($filename)
 {
     $sftpResult = @ssh2_sftp_unlink($this->sftpResource, $this->getRemotePath($filename));
     if ($sftpResult) {
         return $this;
     } else {
         throw new ConnectionException('Unable to delete file');
     }
 }
Ejemplo n.º 4
0
 /**
  * Find XE installed path on sftp
  */
 function getSFTPPath()
 {
     $ftp_info = Context::getRequestVars();
     if (!$ftp_info->ftp_host) {
         $ftp_info->ftp_host = "127.0.0.1";
     }
     if (!$ftp_info->ftp_port || !is_numeric($ftp_info->ftp_port)) {
         $ftp_info->ftp_port = '22';
     }
     $connection = ssh2_connect($ftp_info->ftp_host, $ftp_info->ftp_port);
     if (!ssh2_auth_password($connection, $ftp_info->ftp_user, $ftp_info->ftp_password)) {
         return new Object(-1, 'msg_ftp_invalid_auth_info');
     }
     $sftp = ssh2_sftp($connection);
     // create temp file
     $pin = $_SERVER['REQUEST_TIME'];
     FileHandler::writeFile('./files/cache/ftp_check', $pin);
     // create path candidate
     $xe_path = _XE_PATH_;
     $path_info = array_reverse(explode('/', _XE_PATH_));
     array_pop($path_info);
     // remove last '/'
     $path_candidate = array();
     $temp = '';
     foreach ($path_info as $path) {
         $temp = '/' . $path . $temp;
         $path_candidate[] = $temp;
     }
     // try
     foreach ($path_candidate as $path) {
         // upload check file
         if (!@ssh2_scp_send($connection, FileHandler::getRealPath('./files/cache/ftp_check'), $path . 'ftp_check.html')) {
             continue;
         }
         // get check file
         $result = FileHandler::getRemoteResource(getNotencodedFullUrl() . 'ftp_check.html');
         // delete temp check file
         @ssh2_sftp_unlink($sftp, $path . 'ftp_check.html');
         // found
         if ($result == $pin) {
             $found_path = $path;
             break;
         }
     }
     FileHandler::removeFile('./files/cache/ftp_check', $pin);
     if ($found_path) {
         $this->add('found_path', $found_path);
     }
 }
Ejemplo n.º 5
0
function run_script_on_server($script, $server, $user, $public_key_file, $private_key_file)
{
    $con = ssh2_connect($server);
    if ($con === false) {
        return array(false, false);
    }
    $result = ssh2_auth_pubkey_file($con, $user, $public_key_file, $private_key_file);
    if (!$result) {
        return array(false, false);
    }
    $remote_script = basename($script);
    $result = ssh2_scp_send($con, $script, $remote_script, 0700);
    if (!$result) {
        return array(false, false);
    }
    $io_stream = ssh2_exec($con, "( ./{$remote_script} ) 2>&1; echo \$?");
    if ($io_stream) {
        stream_set_blocking($io_stream, true);
        $output = '';
        while (true) {
            $line = fgets($io_stream);
            if ($line === false) {
                break;
            }
            $output .= $line;
        }
        fclose($io_stream);
        $output = rtrim($output);
        $last_newline_index = strrpos($output, "\n");
        $status_code = intval(substr($output, $last_newline_index + 1));
        $output = substr($output, 0, $last_newline_index);
    } else {
        $output = false;
        $status_code = false;
    }
    $sftp_con = ssh2_sftp($con);
    if ($sftp_con) {
        ssh2_sftp_unlink($sftp_con, $remote_script);
    }
    return array($output, $status_code);
}
 /**
  * @see Filesystem::delete
  */
 public function delete($path, $recursive = false)
 {
     if ($this->isFile($path)) {
         return ssh2_sftp_unlink($this->sftp_link, $path);
     }
     if (!$recursive) {
         return ssh2_sftp_rmdir($this->sftp_link, $path);
     }
     $filelist = $this->listDir($path);
     if (is_array($filelist)) {
         foreach ($filelist as $filename => $fileinf) {
             $this->delete($path . '/' . $filename, $recursive);
         }
     }
     return ssh2_sftp_rmdir($this->sftpConnection, $path);
 }
Ejemplo n.º 7
0
 public function unlink($path)
 {
     $path = $this->path($path);
     $return = @ssh2_sftp_unlink($this->getSftpResource(), $path);
     if (!$return) {
         throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to unlink "%s"', $path));
     }
     return true;
 }
Ejemplo n.º 8
0
 /**
  * @param string $identifier
  * @param bool $recursive
  * @return bool
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function delete($identifier, $recursive)
 {
     if (is_dir($this->sftpWrapper . $identifier)) {
         return ssh2_sftp_rmdir($this->sftp, $identifier);
     } elseif (is_file($this->sftpWrapper . $identifier)) {
         return ssh2_sftp_unlink($this->sftp, $identifier);
     } else {
         return false;
     }
 }
Ejemplo n.º 9
0
 $line_loop1 = 0;
 $line_loop2 = 0;
 //先取回10.10.1.102台鐵資料
 $SFtp_host = '10.10.1.102';
 $SFtp_user = "******";
 $SFtp_pass = "******";
 $File_Name = "Train_Out_" . date("Ymd") . ".txt";
 $File_Name_path = $DirUrlTxt . "/" . $File_Name;
 $connection = ssh2_connect($SFtp_host, 22);
 $login = ssh2_auth_password($connection, $SFtp_user, $SFtp_pass);
 if ($login) {
     $sftp = ssh2_sftp($connection);
     $data = file_get_contents("ssh2.sftp://{$sftp}/{$File_Name}", true);
     $sFTPok = file_put_contents($File_Name_path, $data);
     if ($sFTPok) {
         ssh2_sftp_unlink($sftp, $File_Name);
     } else {
         $FTPechoTO = "由SFTP 10.10.1.102 取回台鐵檔案失敗";
     }
 } else {
     $FTPechoTO = "SFTP 10.10.1.102 連線失敗";
 }
 if (!filesize($File_Name_path)) {
     $msg = $subject . ", " . $FTPechoTO . " 未取得台鐵 " . date("Ymd") . " 關帳檔. 很重要請檢查! by g_Close_china";
     exec_line(1, $msg, 'g_Close_china');
     die($subject . "-" . $FTPechoTO . " \r\n");
 }
 //檔案解密
 FileEncrypt($File_Name_path, 'decrypt');
 //中信新33一般 cup_us!='2'銀聯不處理
 //帳單名稱以 mid_tctb.webname 為主, 除了 GID = 907739 (統宣) 721730 (公司測試店)
Ejemplo n.º 10
0
 /**
  * Remove file
  *
  * @param string $path Path to remove
  * @return Object
  */
 function _removeFile($path)
 {
     if (substr($path, 0, 2) == "./") {
         $path = substr($path, 2);
     }
     $target_path = $this->ftp_info->ftp_root_path . $path;
     if (!@ssh2_sftp_unlink($this->sftp, $target_path)) {
         return new Object(-1, sprintf(Context::getLang('msg_delete_file_failed'), $path));
     }
     return new Object();
 }
Ejemplo n.º 11
0
 public function deleteFile(string $path) : bool
 {
     if (@ssh2_sftp_unlink($this->connect, $path)) {
         return true;
     } else {
         throw new FileNotFoundException($path);
     }
 }
Ejemplo n.º 12
0
 /**
  * Delete a file from remote server
  */
 public function rm($remoteFile)
 {
     $file = $this->_getFilename($remoteFile);
     if (!ssh2_sftp_unlink($this->_getSftp(), $file)) {
         throw new \Exception("Could not remove file '{$file}'");
     }
 }
Ejemplo n.º 13
0
 /**
  * Move a file (cut) instead of copy it
  * @return SplFileInfo the file downloaded
  */
 public function moveFile($file, $local)
 {
     $result = $this->downloadFile($file, $local);
     ssh2_sftp_unlink($this->resource, $file);
     return $result;
 }
Ejemplo n.º 14
0
 /**
  * Deletes a remote file.
  *
  * @param string  $remote_path
  * @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 delete($remote_path, $recursive = false)
 {
     if (null === $this->_sftp_resource) {
         throw new TransportException(sprintf('None SSH connection available.'));
     }
     if (true === $recursive) {
         // @todo
         throw new TransportException(sprintf('REcursive option not implemented yet.'));
     }
     $remote_path = $this->_getAbsoluteRemotePath($remote_path);
     if (false === @ssh2_sftp_stat($this->_sftp_resource, $remote_path)) {
         return $this->_trigger_error(sprintf('Remote file to delete does not exist: %s.', $remote_path));
     }
     if (false === ssh2_sftp_unlink($this->_sftp_resource, $remote_path)) {
         return ssh2_sftp_rmdir($this->_sftp_resource, $remote_path);
     }
     return true;
 }
Ejemplo n.º 15
0
 /**
  * Facade for ssh2_sftp_delete.
  *
  * @param resource $resource    The sftp resource to use.
  * @param string   $source_path The remote source path to delete.
  *
  * @return boolean True on success.
  */
 public function sftpDelete($resource, $source_path)
 {
     //@codeCoverageIgnoreStart
     return ssh2_sftp_unlink($resource, $source_path);
     //@codeCoverageIgnoreEnd
 }
Ejemplo n.º 16
0
 /**
  * Delete a file (remove it from the disk)
  *
  * @param   string  $fileName  The full path to the file
  *
  * @return  boolean  True on success
  */
 public function delete($fileName)
 {
     $targetFile = $this->translatePath($fileName);
     try {
         $ret = @ssh2_sftp_unlink($this->sftpHandle, $targetFile);
     } catch (\Exception $e) {
         $ret = false;
     }
     return $ret;
 }
Ejemplo n.º 17
0
 function delete_file($remoteFile)
 {
     $this->sftp = ssh2_sftp($this->conn);
     if (!$this->sftp) {
         $this->error = 'Can not open SFTP (delete)';
         return false;
     } else {
         ssh2_sftp_unlink($this->sftp, $remoteFile);
         return true;
     }
 }
 protected function doDelFile($remote_file)
 {
     $remote_file = ltrim($remote_file, '/');
     return ssh2_sftp_unlink($this->getSftpConnection(), $remote_file);
 }
Ejemplo n.º 19
0
 /**
  * sftp_delete_file($fileContainer)
  * 
  * The function deletes an image and its thumbnails from one or more SFTP accounts
  *
  * @param image
  * @return nothing, for now
  */
 function sftp_delete_file($fileContainer)
 {
     // adds the original url to the list of thumbs, as only this list of thumbnails is used to delete 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_all_servers($fileContainer);
     if (sizeof($servers)) {
         foreach ($servers as $server) {
             $conn_id = $this->get_sftp_connection_id($server);
             // gets the existing connection id for this SFTP account, or creates a new one
             if (sizeof($fileContainer->thumb_paths)) {
                 // if we have images to upload
                 foreach ($fileContainer->thumb_paths as $id => $local_file_path) {
                     if (!ssh2_sftp_unlink($conn_id, $server['root_path'] . $local_file_path)) {
                         echo "Couldn't delete " . $server['subfolder_path'] . $local_file_path . " from the " . $server['hostname'] . " FTP server<br>\n";
                         //return FALSE; // TODO: Better error handling (message output)
                         // TODO: Delete folders if they are empty?
                     }
                 }
             }
             // foreach($fileContainer->thumb_paths as $local_file_path)
             $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);
         }
         // foreach($servers as $server)
     }
     // if(sizeof($servers))
     $sql_pic2server = "DELETE FROM {$this->config['TABLE_SFTP_PIC2SERVER']} WHERE pic_id='{$fileContainer->id}'";
     //echo "del sql_pic2server: ".$sql_pic2server."<br>\n";
     cpg_db_query($sql_pic2server);
 }
Ejemplo n.º 20
0
 /**
  * Rename (or move) a file
  *
  * @version 1.0
  * @access  public
  * @param   string
  * @return  bool
  */
 public function delete_file($filepath)
 {
     if (!$this->_is_conn()) {
         return FALSE;
     }
     $result = @ssh2_sftp_unlink($this->sftp, $filepath);
     if ($result === FALSE) {
         if ($this->debug == TRUE) {
             $this->_error('ftp_unable_to_delete');
         }
         return FALSE;
     }
     return TRUE;
 }
Ejemplo n.º 21
0
 /**
  * Deletes a folder from the VFS.
  *
  * @param string $path        The parent folder.
  * @param string $name        The name of the folder to delete.
  * @param boolean $recursive  Force a recursive delete?
  *
  * @throws Horde_Vfs_Exception
  */
 public function deleteFolder($path, $name, $recursive = false)
 {
     $this->_connect();
     $isDir = false;
     foreach ($this->listFolder($path) as $file) {
         if ($file['name'] == $name && $file['type'] == '**dir') {
             $isDir = true;
             break;
         }
     }
     if ($isDir) {
         $file_list = $this->listFolder($path . '/' . $name);
         if (count($file_list) && !$recursive) {
             throw new Horde_Vfs_Exception(sprintf('Unable to delete "%s", the directory is not empty.', $this->_getPath($path, $name)));
         }
         foreach ($file_list as $file) {
             if ($file['type'] == '**dir') {
                 $this->deleteFolder($path . '/' . $name, $file['name'], $recursive);
             } else {
                 $this->deleteFile($path . '/' . $name, $file['name']);
             }
         }
         if (!@ssh2_sftp_rmdir($this->_sftp, $this->_getPath($path, $name))) {
             throw new Horde_Vfs_Exception(sprintf('Cannot remove directory "%s".', $this->_getPath($path, $name)));
         }
     } else {
         if (!@ssh2_sftp_unlink($this->_sftp, $this->_getPath($path, $name))) {
             throw new Horde_Vfs_Exception(sprintf('Cannot delete file "%s".', $this->_getPath($path, $name)));
         }
     }
 }
Ejemplo n.º 22
0
 /**
  * Facade for ssh2_sftp_delete.
  *
  * @param resource $resource    The sftp resource to use.
  * @param string   $source_path The remote source path to delete.
  *
  * @return boolean True on success.
  */
 public function sftpDelete($resource, $source_path)
 {
     return ssh2_sftp_unlink($resource, $source_path);
 }
Ejemplo n.º 23
0
 public function unlink($file)
 {
     $check = '/' . trim($this->dir, '/') . '/' . trim($file, '/');
     return @ssh2_sftp_unlink($this->handle, $check);
 }
Ejemplo n.º 24
0
 public function remove($remote)
 {
     $sftp = ssh2_sftp($this->conn_);
     $rc = false;
     if (is_dir("ssh2.sftp://{$sftp}/{$remote}")) {
         $rc = ssh2_sftp_rmdir($sftp, $remote);
     } else {
         $rc = ssh2_sftp_unlink($sftp, $remote);
     }
     return $rc;
 }
Ejemplo n.º 25
0
 /**
  * @{inheritDoc}
  */
 public function unlink($filename = '')
 {
     return $this->doRun(__METHOD__, func_get_args(), ssh2_sftp_unlink($this->sftp, $filename));
 }
Ejemplo n.º 26
0
 /**
  * Deletes a file
  *
  * @param  string $filename The name of the file that is being deleted
  *
  * @return Boolean TRUE on success, or FALSE on failure
  */
 public function unlink($filename)
 {
     return ssh2_sftp_unlink($this->getResource(), $filename);
 }
Ejemplo n.º 27
0
 /**
  * Remote unlink / delete.
  *
  * @param string $remote_file
  *
  * @return bool
  */
 public function unlink($remote_file)
 {
     ssh2_sftp_unlink($this->resource, $remote_file);
 }
Ejemplo n.º 28
0
 /**
  * Recursively remove a directory and all its subdirectories.
  *
  * Walks a directory structure, removing files recursively. An optional
  * exclusion pattern can be included. If a directory contains a file that
  * matches the exclusion pattern, the directory and its ancestors will not
  * be deleted.
  *
  * @param string $path
  * @param string $noDelPat PCRE pattern for excluding files in deletion.
  */
 public static function rrmdir($path, $noDelPat = null)
 {
     $useExclude = $noDelPat != null;
     $special = '/.*\\/?\\.+\\/?$/';
     $excluded = false;
     if (!realpath($path)) {
         return false;
     }
     $path = realpath($path);
     if (filetype($path) == 'dir') {
         $objects = scandir($path);
         foreach ($objects as $object) {
             if (!preg_match($special, $object)) {
                 if ($useExclude) {
                     if (!preg_match($noDelPat, $object)) {
                         $excludeThis = self::rrmdir($path . DIRECTORY_SEPARATOR . $object, $noDelPat);
                         $excluded = $excluded || $excludeThis;
                     } else {
                         $excluded = true;
                     }
                 } else {
                     self::rrmdir($path . DIRECTORY_SEPARATOR . $object, $noDelPat);
                 }
             }
         }
         reset($objects);
         if (!$excluded) {
             if (!preg_match($special, $path)) {
                 switch (self::$fileOper) {
                     case 'ftp':
                         $path = self::ftpStripChroot($path);
                         ftp_rmdir(self::$_ftpStream, $path);
                         break;
                     case 'scp':
                         ssh2_sftp_rmdir(ssh2_sftp(self::$_sshStream), $path);
                         break;
                     case 'php':
                     default:
                         rmdir($path);
                 }
             }
         }
     } else {
         switch (self::$fileOper) {
             case 'ftp':
                 $path = self::ftpStripChroot($path);
                 ftp_delete(self::$_ftpStream, $path);
                 break;
             case 'scp':
                 ssh2_sftp_unlink(ssh2_sftp(self::$_sshStream), $path);
                 break;
             case 'php':
             default:
                 unlink($path);
         }
     }
     return $excluded;
 }
Ejemplo n.º 29
0
 function delete($file, $recursive = false, $type = false)
 {
     if ('f' == $type || $this->is_file($file)) {
         return ssh2_sftp_unlink($this->sftp_link, $file);
     }
     if (!$recursive) {
         return ssh2_sftp_rmdir($this->sftp_link, $file);
     }
     $filelist = $this->dirlist($file);
     if (is_array($filelist)) {
         foreach ($filelist as $filename => $fileinfo) {
             $this->delete($file . '/' . $filename, $recursive, $fileinfo['type']);
         }
     }
     return ssh2_sftp_rmdir($this->sftp_link, $file);
 }
Ejemplo n.º 30
0
 /**
  * @inheritdoc
  */
 public function deleteFile($fileName)
 {
     $fullFileName = $this->composeFullFileName($fileName);
     $result = ssh2_sftp_unlink($this->getStorage()->getSftp(), $fullFileName);
     if ($result) {
         $this->log("file '{$fullFileName}' has been deleted");
     } else {
         $this->log("unable to delete file '{$fullFileName}'!", Logger::LEVEL_ERROR);
     }
     return $result;
 }