2013-01-14 20 views
6

ठीक है, तो अपने सर्वर से या तो ftp या sftp के माध्यम से अन्य सर्वरों तक पहुंच रहा है ... मैंने या तो संभालने के लिए एक छोटी कक्षा लिखी है .. यह स्पष्ट रूप से नया है और आसानी से सुधार किया जा सकता है इसलिए सोचा कि मैं चाहता हूं इसे यहां फेंक दो और देखें कि अन्य लोग क्या सोचते हैं (स्टैक ओवरफ्लो को बहुत सारे विचारों का नरक मिल जाता है, इसलिए उम्मीद है कि यह किसी और की मदद कर सकता है), और वे इस पर कैसे सुधार कर सकते हैं ... तो मुझे लगता है कि सवाल यह है कि ... कैसे हो सकता है यह सुधार किया जा सकता है?PHP एफ़टीपी/एसएफटीपी स्विच क्लास

class ftp_sftp{ 
//determine, if ssh, to use phpseclib or php's inbuilt ssh_sftp 'libssh' 
public $ssh_type = 'phpseclib'; 
//set ths path to the directory containing the entire phpseclib files 
public $phpseclib_path = 'scripts/phpseclib0.3.0'; 

//private vars generated by this class 
public $host; 
public $username; 
public $password; 
public $connection_type; 
public $port_number; 
public $connection = false; 

//contruct method which will attempt to set the connection details and automatically attempt to establisha connection to the server 
public function __construct($host, $username, $password, $connection_type, $port_number = false){ 

    //add the webroot to the beginning of the $this->phpseclib_path (this is bespoke to my own configuration) 
    $this->phpseclib_path = WEBROOT_PRIVATE.$this->phpseclib_path; 

    //setting the classes vars 
    $this->host   = $host; 
    $this->username  = $username; 
    $this->password  = $password; 
    $this->connection_type = $connection_type; 

    //set the port number to defaults based on connection type if none passed 
    if($port_number === false){ 
     if($connection_type == 'ftp'){ 
      $port_number = 21; 
     } else { 
      $port_number = 22; 
     } 
    } 
    $this->port_number = $port_number; 

    //now set the server connection into this classes connection var 
    $this->connection = $this->connect(); 
} 

//tests the details passed and tries to establish a connection, returns false on fail. 
function connect(){ 
    br($this->connection_type); 
    switch($this->connection_type) 
     { 
      case 'ftp': 
         $connection = ftp_connect($this->host); 
         $login = ftp_login($connection, $this->username, $this->password); 

         //if no connection was possible return false and leave $this-connection as false 
         if(!$connection || !$login){ 
          return false; 
         } else { 
          // enabling passive mode 
          ftp_pasv($connection, true); 
          return $connection; 
         } 
      break; 

      case 'sftp': 
         //decide which ssh type to use 
         switch($this->ssh_type){ 
          case 'phpseclib': 
            //inlcude the phpseclib path in the include array and include the ssh2 class 
            set_include_path($this->phpseclib_path); 
            if(!include('Net/SSH2.php')){ 
             echo 'Sorry failed to load SSH2 class'; 
             br(); 
            } 
            if(!include('Net/SFTP.php')){ 
             echo 'Sorry failed to load SFTP class'; 
             br(); 
            } 

            $connection = new Net_SFTP($this->host, $this->port_number); 
            $login = $connection->login($this->username, $this->password); 
          break; 

          case 'libssh2': 
            $connection = ssh2_connect($this->host, $this->port_number); 
            $login = ssh2_auth_password($connection, 'username', 'secret'); 
          break; 

          default: 
            echo 'No ssh method defined, please define one in: $ftp_sftp->ssh_type'; 
            exit(); 
          break; 
         } 


         //if no connection was possible return false and leave $this-connection as false 
         if (!$connection || !$login) { 
          return false; 
         } else { 
          return $connection; 
         } 
      break; 

      default: echo 'No connection type set cannot choose a method to connect'; 
      break; 
     } 
} 

//acces the phpseclib errors 
public function errors(){ 
if($this->connection_type == 'sftp' && $this->ssh_type == 'phpseclib'){ 
     print_r($this->connection->getErrors()); 
    } else { 
     echo 'no error logs available'; 
    } 
} 

//function used by this class to check certain values are set 
public function connection_check(){ 
    if($this->connection === false){ 
     echo 'Sorry there seems to be a connection problem please try again'; 
     br(); 
    } 

    if($this->connection_type === false){ 
     echo 'Sorry there seems to be a no connection type set'; 
    } 

    if($this->connection === false || $this->connection_type === false){ 
     exit(); 
    } 
} 

//transfers a file to the connected server 
public function put($targetLocationToSendTo, $existingLocationToSendFrom){ 

    //check the connection 
    $this->connection_check(); 

    switch($this->connection_type) 
     { 
      case 'ftp': 
         //ftp_put the file across 
         $put = ftp_put($this->connection, $targetLocationToSendTo, $existingLocationToSendFrom, FTP_BINARY); 
      break; 

      case 'sftp': 
         //decide which ssh type to use 
         switch($this->ssh_type){ 
          case 'phpseclib': 
            $put = $this->connection->put($targetLocationToSendTo, $existingLocationToSendFrom, NET_SFTP_LOCAL_FILE); 
          break; 

          case 'libssh2': 
            $put = ssh2_scp_send($this->connection, $targetLocationToSendTo, $existingLocationToSendFrom, 0755); 
          break; 
         } 
      break; 
     } 

    return $put; 
} 

//list the contents of a remote directory 
public function dir_list($dirToList){ 

    //check the connection 
    $this->connection_check(); 

    //run appropriate list 
    switch($this->connection_type) 
     { 
      case 'ftp': 
         $list = $this->connection = ftp_nlist($this->connection, $dirToList); 
      break; 

      case 'sftp': 
         //decide which ssh type to use 
         switch($this->ssh_type){ 
          case 'phpseclib': 
            $list = $this->connection->nlist($dirToList); 
          break; 

          case 'libssh2': 
            echo 'Sorry there is no support for nlist with libssh2, however this link has a possible answer: http://randomdrake.com/2012/02/08/listing-and-downloading-files-over-sftp-with-php-and-ssh2/'; 
          break; 
         } 
      break; 
     } 

    return $list; 
} 

//get the timestamp of the file on another server 
public function remote_filemtime($pathToFile){ 

    //check the connection 
    $this->connection_check(); 

    //run appropriate list 
    switch($this->connection_type) 
     { 
      case 'ftp': 
         $timeStamp = ftp_mdtm($this->connection, $pathToFile); 
      break; 

      case 'sftp': 
         //decide which ssh type to use 
         switch($this->ssh_type){ 
          case 'phpseclib': 
            $statinfo = $this->connection->stat($pathToFile); 
          break; 

          case 'libssh2': 
            $statinfo = ssh2_sftp_stat($this->connection, $pathToFile); 
          break; 
         } 

         if($statinfo['mtime']){ 
          $timeStamp = $statinfo['mtime']; 
         } else { 
          $timeStamp = false; 
         } 
      break; 
     } 

    return $timeStamp; 
} 

//make a directory on the remote server 
public function make_dir($dirToMake){ 
    //check the connection 
    $this->connection_check(); 

    //run appropriate list 
    switch($this->connection_type) 
     { 
      case 'ftp': 
         $dir_made = ftp_mkdir($this->connection, $dirToMake); 
      break; 

      case 'sftp': 
         //decide which ssh type to use 
         switch($this->ssh_type){ 
          case 'phpseclib': 
            $statinfo = $this->connection->mkdir($dirToMake); 
          break; 

          case 'libssh2': 
            $statinfo = ssh2_sftp_mkdir($this->connection, $dirToMake, 0755); 
          break; 
         } 
      break; 
     } 

    return $dir_made; 
} 

//change directory 
public function change_dir($dirToMoveTo){ 
    //check the connection 
    $this->connection_check(); 

    //run appropriate list 
    switch($this->connection_type) 
     { 
      case 'ftp': $chdir = ftp_chdir($this->connection, $dirToMoveTo); 
      break; 

      case 'sftp': 
         //decide which ssh type to use 
         switch($this->ssh_type){ 
          case 'phpseclib': 
            $chdir = $this->connection->chdir($dirToMoveTo); 
          break; 

          case 'libssh2': 
            echo 'Sorry this feature does exist yet for when using libssh2 with the ftp_sftp class'; 
            exit(); 
          break; 
         } 
      break; 
     } 

    return $chdir; 
} 

//curent directory we are looking in 
public function pwd(){ 

    //check the connection 
    $this->connection_check(); 

    //run appropriate list 
    switch($this->connection_type) 
     { 
      case 'ftp': $pwd = ftp_pwd($this->connection); 
      break; 

      case 'sftp': 
         //decide which ssh type to use 
         switch($this->ssh_type){ 
          case 'phpseclib': 
            $pwd = $this->connection->pwd(); 
          break; 

          case 'libssh2': 
            echo 'Sorry this feature does exist yet for when using libssh2'; 
            exit(); 
          break; 
         } 
      break; 
     } 

    return $pwd; 
} 

//delete file 
public function delete_file($fileToDelete){ 
    //check the connection 
    $this->connection_check(); 

    //run appropriate list 
    switch($this->connection_type) 
     { 
      case 'ftp': $unlink = ftp_delete($this->connection, $fileToDelete); 
      break; 

      case 'sftp': 
         //decide which ssh type to use 
         switch($this->ssh_type){ 
          case 'phpseclib': 
            $unlink = $this->connection->delete($fileToDelete); 
          break; 

          case 'libssh2': 
            $unlink = ssh2_sftp_unlink($this->connection, $fileToDelete); 
          break; 
         } 
      break; 
     } 

    return $unlink; 
} }//end of class 

वर्ग का उपयोग करना:

$ftp_sftp = new ftp_sftp('92.21.627.163', 'ftpuser', 'yourpassword', '', 'ftp', '21'); 
echo $ftp_sftp->pwd(); 

मैं एक छोटे से मुसीबत मेरी Win7 मशीन easyPHP के प्रयोग पर कनेक्ट करने के लिए phpseclib हो रही हो रहा है और एक क्यू शुरू कर दिया है .. मैं हो सकता है अगर किसी को भी किसी भी विचार है बहुत आभारी ... Cannot get phpseclib to connect - error 10060

+1

के विरुद्ध टाइप-संकेतों पर विचार करें, मैं उन स्विच स्टेटमेंट्स पर कटौती करने के लिए विरासत की अनुशंसा करता हूं। – quickshiftin

+0

क्या आप थोड़ा सा विस्तार कर सकते हैं? – John

+1

मैं जवाब पर बाहर चला गया, क्योंकि जंगली में यह एक आम मुद्दा है। उम्मीद है कि यह समझ में आता है। यदि आप स्ट्रैटजी पैटर्न मास्टर करते हैं, तो सबसे सरल डिजाइन पैटर्न में से एक, मैं आपको आश्वस्त करता हूं कि आप कौशल में अपने आसपास के 75% डेवलपर्स को तत्काल बाईपास कर देंगे! यदि आप पैटर्न पर लगाए गए हैं तो मानक पैक उठाएं, और आप निंजा बन जाएंगे। – quickshiftin

उत्तर

13

मुख्य समस्या इस कोड के साथ यह स्केल लंबवत है। मान लीजिए कि आप 'फ़ाइल' के $connection_type के लिए एक तीसरा संभावित मूल्य पेश करने का एक और कार्यान्वयन, एक स्थानीय फाइल सिस्टम जोड़ने का निर्णय लेते हैं। अब आपको case 'file': हर जगह स्थित कोड जोड़ने के माध्यम से जाना है, जिससे ftp_sftp नियंत्रण से बाहर हो जाता है। आपको अभी कोड की 'sftp' शाखा के अंदर एक ही समस्या है, $ssh_type से निपटना।

मूल कोड

switch($connection_type) { 
    case 'ftp' : // ... 
    case 'stfp' : // ... 
} 

मन में हो जाता है

switch($connection_type) { 
    case 'ftp' : // ... 
    case 'stfp' : // ... 
    case 'file' : // ... 
} 

भालू इस हर स्विच बयान कोड कि $connection_type पर स्विच में लिए है। कल्पना कीजिए कि हम ftp_sftp वर्ग प्रत्येक अतिरिक्त व्यवहार को शामिल करने के लिए जोड़ना होगा कोड में से कितने लाइनों ...

वास्तव में, ऐसा लगता है कि आप वास्तव में 3 रणनीतियों अभी, एफ़टीपी, SFTP & ssh

तुम यहाँ क्या करना चाहते हैं क्या क्षैतिज बजायखड़ी और तरीका है कि Strategy design pattern के माध्यम से है करने के लिए विस्तार करने के लिए कोड निर्धारित है।

अनिवार्य रूप से आप जो कर रहे हैं वह आपके कोड में सामान्य इंटरफ़ेस खींच रहा है, फिर FTP & SFTP के लिए अलग-अलग कार्यान्वयन बना रहा है। इस पर वास्तविक कार्यान्वयन पर जाने के कई अलग-अलग तरीके हैं, इसलिए अपने दिल की सामग्री में बदलाव करें!

एक आम इंटरफेस

यह वही है किसी भी 'एफ़टीपी वर्ग' ऐसा करने में सक्षम होना चाहिए की परिभाषा है।

interface PhpFtp 
{ 
    public function __construct($host, $username, $password, $port_number = false); 
    public function connect(); 
    public function errors(); 
    public function connection_check(); 
    public function put($targetLocationToSendTo, $existingLocationToSendFrom); 
    public function dir_list($dirToList); 
    public function remote_filemtime($pathToFile); 
    public function make_dir($dirToMake); 
    public function change_dir($dirToMoveTo); 
    public function pwd(); 
    public function delete_file($fileToDelete); 
} 

एक कारखाने

अब कोड है कि $connection_type की जाँच करता है भर में एक भी स्विच बयान है। आप default मामले को अलग-अलग संभालने का निर्णय ले सकते हैं।

class PhpFtpFactory 
{ 
    public static function create($connection_type) 
    { 
     switch($connection_type) { 
      case 'ftp': 
       $oFtp = new Ftp(); 
       break; 
      case 'sftp': 
       $oFtp = new Sftp(); 
       break; 
      default: 
       throw new UnexpectedValueExcpetion(
        'No connection type set cannot choose a method to connect'); 
     } 

     // Potential follow-up construction steps 
     return $oFtp; 
    } 
} 

एक आधार वर्ग

सख्ती से आवश्यक नहीं है, लेकिन बहुत उपयोगी। इसके अलावा, हम यहां एक और डिजाइन पैटर्न में घुसपैठ कर रहे हैं, जिसे Template Method कहा जाता है। BaseFtp::pwd एक टेम्पलेट विधि है, यह कुल pwd एल्गोरिदम नियंत्रित करता है, लेकिन इसके एक हिस्से के लिए ठोस बच्चों को प्रतिनिधि करता है।

abstract class BaseFtp implements PhpFtp 
{ 
    public function pwd() 
    { 
     $this->connection_check(); 
     return $this->_pwd(); 
    } 

    abstract protected function _pwd(); 
} 

कंक्रीट एफ़टीपी कार्यान्वयन

अब हम एक संक्षिप्त एफ़टीपी वर्ग है कि केवल एफ़टीपी संचालन करता है, यह SFTP के बारे में कुछ भी नहीं जानता है।

class Ftp extends BaseFtp 
{ 
    protected function _pwd() 
    { 
     return ftp_pwd($this->connection); 
    } 

    public function connect() 
    { 
     $connection = ftp_connect($this->host); 
     $login  = ftp_login($connection, $this->username, $this->password); 

     // if no connection was possible return false and leave $this-connection as false 
     if(!$connection || !$login) 
      return false; 

     // enabling passive mode 
     ftp_pasv($connection, true); 
     return $connection; 
    } 
} 

कंक्रीट SFTP वर्ग

अब हम एक स्टैंडअलोन SFTP वर्ग है, हालांकि नोटिस यह $ssh_type मूल ftp_sftp वर्ग $connection_type की ही तरह पर स्विच करता है। एक बार जब आप रणनीति पैटर्न पर एक संभाल पाने के लिए, और Ftp & Sftp कक्षाएं समाप्त हो गया है, तो आप में वापस आ गया है, ताकि आप, 2 वर्गों Phpseclib & Libssh2 है उदाहरण के लिए जाने के लिए और Sftp कार्यान्वयन के भीतर रणनीति पैटर्न लागू कर सकते हैं। इसके 10X क्या क्या, हर वर्ग के केंद्रित यह विशिष्ट डोमेन है और कोड बढ़ता क्षैतिज पर है देखने के लिए आसान

class Sftp extends BaseFtp 
{ 
    protected function _pwd() 
    { 
     // decide which ssh type to use 
     switch($this->ssh_type) { 
      case 'phpseclib': 
       return $this->connection->pwd(); 

      case 'libssh2': 
       echo 'Sorry this feature does exist yet for when using libssh2'; 
       return false; 
     } 
    } 

    public function connect() 
    { 
     // decide which ssh type to use 
     switch($this->ssh_type) { 
      case 'phpseclib': 
       // inlcude the phpseclib path in the include array 
       // and include the ssh2 class 
       set_include_path($this->phpseclib_path); 
       if(!include('Net/SSH2.php')){ 
        echo 'Sorry failed to load SSH2 class'; 
        br(); 
       } 
       if(!include('Net/SFTP.php')){ 
        echo 'Sorry failed to load SFTP class'; 
        br(); 
       } 

       $connection = new Net_SFTP($this->host, $this->port_number); 
       $login = $connection->login($this->username, $this->password); 
       break; 

      case 'libssh2': 
       $connection = ssh2_connect($this->host, $this->port_number); 
       $login = ssh2_auth_password($connection, 'username', 'secret'); 
       break; 

      default: 
       echo 'No ssh method defined, please define one in: $ftp_sftp->ssh_type'; 
       exit(); 
       break; 
     } 

     if(!$connection || !$login) 
      return false; 
    } 
} 

लाना नए कोड के स्थान पर उपयोग करने के लिए

नई प्रणाली के साथ। इसका क्या मतलब है? ऊपर याद रखें, जब हमने मूल प्रतिमान में तीसरा $connection_type'फ़ाइल' जोड़ने पर विचार किया था?

class PhpFtpFactory 
{ 
    public static function create($connection_type) 
    { 
     switch($connection_type) { 
      case 'ftp': 
       $oFtp = new Ftp(); 
       break; 
      case 'sftp': 
       $oFtp = new Sftp(); 
       break; 
      case 'file': 
       $oFtp = new File(); 
       break; 
      default: 
       throw new UnexpectedValueExcpetion(
        'No connection type set cannot choose a method to connect'); 
     } 

     // Potential follow-up construction steps 
     return $oFtp; 
    } 
} 

और फिर बेशक आप नई ठोस कार्यान्वयन

class File extends PhpFtp 
{ 
    // ... 
} 

चूंकि नए File कक्षा में जा सकते हैं जोड़ें: नई व्यवस्था में हम एक स्विच बयान, कारखाने में अपडेट करते यह अपनी जगह है, हम एक केंद्रीय वर्ग का विस्तार नहीं कर रहे हैं, अर्थात् मूल ftp_sftp कक्षा।

आदेश नई प्रणाली का उपयोग करने के लिए, आप एक उदाहरण के लिए कारखाना मारा और वहाँ

// get an instance of the Ftp class 
$ftp = PhpFtpFactory::create('ftp'); 

// get an instance of the Sftp class 
$sftp = PhpFtpFactory::create('sftp'); 

से जाना इन उदाहरणों से किसी सब PhpFtp कार्य का समर्थन करते हैं क्योंकि वे इंटरफ़ेस को लागू! यह आपको पॉलिमॉर्फिक कोड लिखने की क्षमता भी देता है। एक फ़ंक्शन पर विचार करें जो इंटरफ़ेस

// This is polymorphic code 
function doFtpStuff(PhpFtp $oFtp) { 
    // As mentioned above $oFtp can be an instance of any class that implements PhpFtp 
    $oFtp->connect(); 
    $oFtp->pwd(); 
} 
+0

वाह। वहां मदद के लिए बहुत बहुत धन्यवाद। मैं इसे शुक्रवार तक लागू करने का प्रयास नहीं कर सकता लेकिन आपको बताएगा कि मैं कैसे साथ आता हूं – John

0

वर्डप्रेस का एसएफटीपी कार्यान्वयन एक अजीब तरीके से chdir और pwd करता है। वे ssh2_exec() करते हैं। पता नहीं है कि यह फाइल डाउनलोड/अपलोड करने के साथ काम करता है या नहीं। यहाँ उनके कार्यान्वयन है:

http://core.svn.wordpress.org/trunk/wp-admin/includes/class-wp-filesystem-ssh2.php

मुझे लगता है कि phpseclib बेहतर लेकिन बस FYI है।

इसके अलावा, phpseclib का नवीनतम संस्करण 0.3.1 है। idk ... बस एक और fyi lol।

अंत में, शायद शामिल करने के बजाय include_once() शामिल करें()? इस तरह कक्षा को कई बार तत्काल किया जा सकता है?

संबंधित मुद्दे