| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace FMUP\Ftp\Driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use FMUP\Ftp\FtpAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use FMUP\Ftp\Exception as FtpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use FMUP\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Sftp extends FtpAbstract implements Logger\LoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     use Logger\LoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const METHODS = 'methods'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const CALLBACKS = 'callbacks'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const USE_INCLUDE_PATH = 'use_include_path'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const GET_CONTENT_CONTEXT = 'get_content_context'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const PUT_CONTENT_CONTEXT = 'put_content_context'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const PUT_CONTENT_FLAGS = 'put_content_flags'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const OFFSET = 'offset'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const MAXLEN = 'maxlen'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const CREATE_MODE = 'create_mode'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $sftpSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @throws FtpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |     protected function getSftpSession() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |         if (!$this->sftpSession) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |             $this->sftpSession = $this->ssh2Sftp($this->getSession()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         return $this->sftpSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 2 |  |     protected function getMethods() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |         return isset($this->settings[self::METHODS]) ? $this->settings[self::METHODS] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |     protected function getCallbacks() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |         return isset($this->settings[self::CALLBACKS]) ? $this->settings[self::CALLBACKS] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Optional param for file_get_content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |     protected function getUseIncludePath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         return isset($this->settings[self::USE_INCLUDE_PATH]) ? $this->settings[self::USE_INCLUDE_PATH] : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Optional param for file_get_content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return resource|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |     protected function getGetContentContext() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         return isset($this->settings[self::GET_CONTENT_CONTEXT]) ? $this->settings[self::GET_CONTENT_CONTEXT] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Optional param for file_get_content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |     protected function getOffset() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         return isset($this->settings[self::OFFSET]) ? $this->settings[self::OFFSET] : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Optional param for file_get_content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return int|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |     protected function getMaxLen() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         return isset($this->settings[self::MAXLEN]) ? $this->settings[self::MAXLEN] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Optional param for file_put_content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |     protected function getPutContentFlags() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         return isset($this->settings[self::PUT_CONTENT_FLAGS]) ? $this->settings[self::PUT_CONTENT_FLAGS] : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Optional param for file_put_content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return resource|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |     protected function getPutContentContext() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         return isset($this->settings[self::PUT_CONTENT_CONTEXT]) ? $this->settings[self::PUT_CONTENT_CONTEXT] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Optional param for file_get_content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |     protected function getCreateMode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         return isset($this->settings[self::CREATE_MODE]) ? $this->settings[self::CREATE_MODE] : 0644; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param string $host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param int $port | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @throws FtpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |     public function connect($host, $port = 22) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         $this->setSession($this->ssh2Connect($host, $port, $this->getMethods(), $this->getCallbacks())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param string $host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param int $port | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param array|null $methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param array|null $callbacks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     protected function ssh2Connect($host, $port = 22, array $methods = null, array $callbacks = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return ssh2_connect($host, $port, $methods, $callbacks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param string $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param string $pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @throws FtpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 2 |  |     public function login($user, $pass) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 2 |  |         $ret = $this->ssh2AuthPassword($this->getSession(), $user, $pass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 2 |  |         if (!$ret) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 1 |  |             $this->log(Logger::ERROR, "Unable to login to SFTP server", (array)$this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 1 |  |             throw new FtpException('Unable to login to the SFTP server'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |         return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @param resource $session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @param string $username | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @param string $password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     protected function ssh2AuthPassword($session, $username, $password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         return ssh2_auth_password($session, $username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param string $localFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param string $remoteFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 2 |  |     public function get($localFile, $remoteFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 2 |  |         if ($this->getMaxLen() === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 1 |  |             $fileContent = $this->fileGetContents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 1 |  |                 'ssh2.sftp://' . $this->getSftpSession() . '/' . $remoteFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 1 |  |                 $this->getUseIncludePath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 1 |  |                 $this->getGetContentContext(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 1 |  |                 $this->getOffset() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 1 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 1 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 1 |  |             $fileContent = $this->fileGetContents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 1 |  |                 'ssh2.sftp://' . $this->getSftpSession() . '/' . $remoteFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 1 |  |                 $this->getUseIncludePath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 1 |  |                 $this->getGetContentContext(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 1 |  |                 $this->getOffset(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 1 |  |                 $this->getMaxLen() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 1 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 2 |  |         return $this->filePutContents( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 2 |  |             $localFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 2 |  |             $fileContent, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 2 |  |             $this->getPutContentFlags(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 2 |  |             $this->getPutContentContext() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * Put file on ftp server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      * @param string $remoteFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      * @param string $localFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 2 |  |     public function put($remoteFile, $localFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 2 |  |         if ($this->getMaxLen() === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 1 |  |             $fileContent = $this->fileGetContents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 | 1 |  |                 $localFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 1 |  |                 $this->getUseIncludePath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 1 |  |                 $this->getGetContentContext(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 | 1 |  |                 $this->getOffset() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 1 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 1 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 | 1 |  |             $fileContent = $this->fileGetContents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 | 1 |  |                 $localFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 1 |  |                 $this->getUseIncludePath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 | 1 |  |                 $this->getGetContentContext(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 1 |  |                 $this->getOffset(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 1 |  |                 $this->getMaxLen() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 | 1 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 | 2 |  |         return $this->filePutContents( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 | 2 |  |             'ssh2.sftp://' . intval($this->getSftpSession()) . '/' . $remoteFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 | 2 |  |             $fileContent, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 | 2 |  |             $this->getPutContentFlags(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 2 |  |             $this->getPutContentContext() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |      * @param resource $session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |      * @param string $localFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      * @param string $remoteFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      * @param int $createMode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 239 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 240 |  |  |     protected function ssh2ScpSend($session, $localFile, $remoteFile, $createMode) | 
            
                                                                        
                            
            
                                    
            
            
                | 241 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 242 |  |  |         return ssh2_scp_send($session, $localFile, $remoteFile, $createMode); | 
            
                                                                        
                            
            
                                    
            
            
                | 243 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |     protected function fileGetContents() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         return call_user_func_array('file_get_contents', func_get_args()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |      * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |      * @param mixed $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |      * @param int $flags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |      * @param resource $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |     protected function filePutContents($fileName, $data, $flags = 0, $context = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |         return file_put_contents($fileName, $data, $flags, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |      * @param string $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 | 1 |  |     public function delete($file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 | 1 |  |         return $this->ssh2SftpUnlink($this->getSftpSession(), $file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |      * @param resource $sftpSession | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |     protected function ssh2SftpUnlink($sftpSession, $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |         return ssh2_sftp_unlink($sftpSession, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 | 1 |  |     public function close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 1 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * @param resource $session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      * @return resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |     protected function ssh2Sftp($session) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |         return ssh2_sftp($session); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 305 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 306 |  |  |  | 
            
                        
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.