Curl FTP Wrapper

Hey, I found a pretty-nice curl ftp wrapper :)


PHP Code:

  1. /*
  2. * Project:        Absynthe cURL
  3. * File:        acurl.class.php5
  4. * Author:        Sylvain 'Absynthe' Rabot <sylvain@abstraction.fr>
  5. * Website:        http://absynthe.is.free.fr/acurl/
  6. * Version:        alpha 1
  7. * Date:        05/06/2007
  8. * License:        LGPL
  9. */
  10.  
  11. /**
  12. * Take a look at the declaration of methods.
  13. * All parameters initialized to false in declarations are optional if you set them with set_option().
  14. * If you set values with both set_option and argument, arguments not FALSE will be taken.
  15. * Puting arguments doesn't overwrite values set by set_option, arguments are temporary.
  16. *
  17. * Location header is taken into account automatically,
  18. * you will receive headers and content of the page pointed by the redirection
  19. *
  20. * @example :
  21. * <code>
  22. *
  23. * $acurl = new acurl();
  24. *
  25. * $acurl->set_option('url', 'http://absynthe.is.free.fr/');
  26. * $acurl->set_option('port', 80);
  27. * $acurl->set_option('login', 'Absynthe');
  28. * $acurl->set_option('password', 'mypassword');
  29. * $acurl->set_option('headers', true);
  30. * $acurl->set_option('cookie', '/path/to/my/cookie.txt');
  31. * $acurl->set_option('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4');
  32. *
  33. * $answer = $acurl->ping();
  34. * $answer = $acurl->http_request();
  35. * $answer = $acurl->http_auth_request();
  36. * $answer = $acurl->http_post_request('var=foo&var1=fooo');
  37. * $answer = $acurl->http_post_request(array('var' => 'foo', 'var1' => 'fooo'));
  38. * $answer = $acurl->http_post_auth_request('var=foo&var1=fooo');
  39. * $answer = $acurl->http_post_auth_request(array('var' => 'foo', 'var1' => 'fooo'));
  40. *
  41. * $answer = $acurl->ftp_upload('/path/to/my/file.txt', 'ftp.abstraction.fr/path/to/file.txt', 21, 'Absynthe', 'mypassword');
  42. * $answer = $acurl->ftp_download('/path/to/my/file.txt', 'ftp.abstraction.fr/path/to/file.txt', 21, 'Absynthe', 'mypassword');
  43. *
  44. * </code>
  45. */
  46.  
  47. class acurl
  48. {
  49.     /* Init
  50.     ---------------------------------- */
  51.     private $handler    = false;
  52.     private $url        = false;
  53.     private $port        = false;
  54.     private $login        = false;
  55.     private $password    = false;
  56.     private $headers    = false;
  57.     private $cookie        = false;
  58.     private $user_agent    = false;   
  59.  
  60.     /**
  61.      * Constructor of the object
  62.      *
  63.      * @param string $url :    URL of the page
  64.      * @param int $port : Port used to get the page
  65.      * @param string $login : HTTP login
  66.      * @param string $password : HTTP password
  67.      * @param boolean $headers : set it to true if you want headers in the answer
  68.      * @param string $cookie : path to the cookie file if you want to use the object like a web browser
  69.      * @param string $user_agent : if you need to look like a web browser
  70.      */
  71.     public function __construct($url = false, $port = false, $login = false, $password = false, $headers = false, $cookie = false, $user_agent = false)
  72.     {
  73.         if (!function_exists('curl_init'))
  74.         {
  75.             trigger_error('Sorry but PHP is not compiled with cURL', E_USER_ERROR);
  76.             exit;
  77.         }
  78.        
  79.         $this->handler        = curl_init();
  80.         $this->url            = $url;
  81.         $this->port            = $port;
  82.         $this->login        = $login;
  83.         $this->password        = $password;
  84.         $this->headers        = $headers;
  85.         $this->cookie        = $cookie;
  86.         $this->user_agent    = $user_agent;
  87.     }
  88.    
  89.     /**
  90.      * Destructor
  91.      */
  92.     public function __destruct()
  93.     {
  94.         if (is_resource($this->handler))
  95.             curl_close($this->handler);
  96.     }
  97.    
  98.     /**
  99.      * Wake up method to enable unserialization
  100.      */
  101.     public function __wakeup()
  102.     {
  103.         if (!is_resource($this->handler))
  104.             $this->handler = curl_init();
  105.     }
  106.    
  107.     /**
  108.      * Set object options
  109.      *
  110.      * @param string $var
  111.      * @param string $value
  112.      */
  113.     public function set_option($var, $value)
  114.     {
  115.         $var        = strtolower($var);
  116.         $this->$var    = $value;
  117.     }
  118.    
  119.     /**
  120.      * Ping an URL
  121.      *
  122.      * @param string $url :    URL to ping
  123.      * @param int $port : Port used to ping
  124.      * @param int $post : Ping with post request
  125.      * @return boolean
  126.      */
  127.     public function ping($url = false, $port = false, $post = false)
  128.     {
  129.         /* Init
  130.         ---------------------------------- */
  131.         $this->test_var($url, $this->url);
  132.         $this->test_var($port, $this->port);
  133.        
  134.         /* cURL Setup
  135.         ---------------------------------- */ 
  136.         curl_setopt($this->handler, CURLOPT_URL, $url);
  137.         curl_setopt($this->handler, CURLOPT_HEADER, false);
  138.         curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, false);
  139.         curl_setopt($this->handler, CURLOPT_NOBODY, true);
  140.        
  141.         if ($port !== false)
  142.             curl_setopt($this->handler, CURLOPT_PORT, $port);
  143.        
  144.         /* Preparation of post request
  145.         ---------------------------------- */
  146.         if ($post !== false)
  147.         {
  148.             curl_setopt($this->handler, CURLOPT_POST, true);
  149.            
  150.             if (is_array($post))
  151.             {
  152.                 $string = '';
  153.                
  154.                 foreach ($post as $key => $value)
  155.                     $string .= "$key=$value&";
  156.                
  157.                 curl_setopt($this->handler, CURLOPT_POSTFIELDS, $string);
  158.             }
  159.             else
  160.             {
  161.                 curl_setopt($this->handler, CURLOPT_POSTFIELDS, $post);
  162.             }
  163.         }
  164.            
  165.         /* Execution
  166.         ---------------------------------- */
  167.         return curl_exec($this->handler);
  168.     }
  169.    
  170.     /**
  171.      * Retrieve content through HTTP
  172.      *
  173.      * @param string $url :    URL of the page
  174.      * @param int $port : Port used to get the page
  175.      * @param boolean $headers : set it to true if you want headers in the answer
  176.      * @param string $cookie : path to the cookie file if you want to use the object like a web browser
  177.      * @param string $user_agent : if you need to look like a web browser
  178.      * @return string / array
  179.      */
  180.     public function http_request($url = false, $port = false, $headers = false, $cookie = false, $user_agent = false)
  181.     {
  182.         /* Init
  183.         ---------------------------------- */
  184.         $this->test_var($url, $this->url);
  185.         $this->test_var($port, $this->port);
  186.         $this->test_var($headers, $this->headers);
  187.         $this->test_var($cookie, $this->cookie);
  188.         $this->test_var($user_agent, $this->user_agent);
  189.        
  190.         /* cURL Setup
  191.         ---------------------------------- */ 
  192.         curl_setopt($this->handler, CURLOPT_URL, $url);
  193.         curl_setopt($this->handler, CURLOPT_HEADER, $headers);
  194.         curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
  195.         curl_setopt($this->handler, CURLOPT_FOLLOWLOCATION, true);
  196.         curl_setopt($this->handler, CURLOPT_AUTOREFERER, true);
  197.        
  198.         if ($port !== false)
  199.             curl_setopt($this->handler, CURLOPT_PORT, $port);
  200.            
  201.         if (is_file($cookie))
  202.         {
  203.             curl_setopt($this->handler, CURLOPT_COOKIE, $cookie);
  204.             curl_setopt($this->handler, CURLOPT_COOKIEJAR, $cookie);
  205.             curl_setopt($this->handler, CURLOPT_COOKIEFILE, $cookie);
  206.         }
  207.        
  208.         if ($user_agent !== false)
  209.             curl_setopt($this->handler, CURLOPT_USERAGENT, $this->user_agent);
  210.        
  211.         /* Execution
  212.         ---------------------------------- */ 
  213.         if ($headers)
  214.             return array("headers" => curl_exec($this->handler), "content" => curl_exec($this->handler));
  215.         else
  216.             return curl_exec($this->handler);
  217.     }
  218.    
  219.     /**
  220.      * Retrieve content through HTTP with authentication
  221.      *
  222.      * @param string $url :    URL of the page
  223.      * @param int $port : Port used to get the page
  224.      * @param string $login : HTTP login
  225.      * @param string $password : HTTP password
  226.      * @param boolean $headers : set it to true if you want headers in the answer
  227.      * @param string $cookie : path to the cookie file if you want to use the object like a web browser
  228.      * @param string $user_agent : if you need to look like a web browser
  229.      * @return string / array
  230.      */
  231.     public function http_auth_request($url = false, $port = false, $login = false, $password = false, $headers = false, $cookie = false, $user_agent = false)
  232.     {
  233.         /* Init
  234.         ---------------------------------- */
  235.         $this->test_var($url, $this->url);
  236.         $this->test_var($port, $this->port);
  237.         $this->test_var($login, $this->login);
  238.         $this->test_var($password, $this->password);
  239.         $this->test_var($headers, $this->headers);
  240.         $this->test_var($cookie, $this->cookie);
  241.         $this->test_var($user_agent, $this->user_agent);
  242.        
  243.         /* cURL Setup
  244.         ---------------------------------- */ 
  245.         curl_setopt($this->handler, CURLOPT_URL, $url);
  246.         curl_setopt($this->handler, CURLOPT_HEADER, $headers);
  247.         curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
  248.         curl_setopt($this->handler, CURLOPT_FOLLOWLOCATION, true);
  249.         curl_setopt($this->handler, CURLOPT_AUTOREFERER, true);
  250.         curl_setopt($this->handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  251.         curl_setopt($this->handler, CURLOPT_USERPWD, "$login:$password");       
  252.        
  253.         if ($port !== false)
  254.             curl_setopt($this->handler, CURLOPT_PORT, $this->port);
  255.            
  256.         if (is_file($cookie))
  257.         {
  258.             curl_setopt($this->handler, CURLOPT_COOKIE, $cookie);
  259.             curl_setopt($this->handler, CURLOPT_COOKIEJAR, $cookie);
  260.             curl_setopt($this->handler, CURLOPT_COOKIEFILE, $cookie);
  261.         }
  262.        
  263.         /* Execution
  264.         ---------------------------------- */ 
  265.         if ($headers)
  266.             return array("headers" => curl_exec($this->handler), "content" => curl_exec($this->handler));
  267.         else
  268.             return curl_exec($this->handler);
  269.     }
  270.    
  271.     /**
  272.      * Send a post request and retrieve the content
  273.      *
  274.      * @param string/array $post : Post request
  275.      * @param string $url :    URL of the page
  276.      * @param int $port : Port used to get the page
  277.      * @param boolean $headers : set it to true if you want headers in the answer
  278.      * @param string $cookie : path to the cookie file if you want to use the object like a web browser
  279.      * @param string $user_agent : if you need to look like a web browser
  280.      * @return string/array
  281.      */
  282.     public function http_post_request($post, $url = false, $port = false, $headers = false, $cookie = false, $user_agent = false)
  283.     {
  284.         /* Init
  285.         ---------------------------------- */
  286.         $this->test_var($url, $this->url);
  287.         $this->test_var($port, $this->port);
  288.         $this->test_var($headers, $this->headers);
  289.         $this->test_var($cookie, $this->cookie);
  290.         $this->test_var($user_agent, $this->user_agent);
  291.        
  292.         /* cURL Setup
  293.         ---------------------------------- */ 
  294.         curl_setopt($this->handler, CURLOPT_URL, $url);
  295.         curl_setopt($this->handler, CURLOPT_HEADER, $headers);
  296.         curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
  297.         curl_setopt($this->handler, CURLOPT_FOLLOWLOCATION, true);
  298.         curl_setopt($this->handler, CURLOPT_AUTOREFERER, true);
  299.         curl_setopt($this->handler, CURLOPT_POST, true);
  300.        
  301.         if (is_string($post))
  302.             curl_setopt($this->handler, CURLOPT_POSTFIELDS, $post);
  303.        
  304.         if ($port !== false)
  305.             curl_setopt($this->handler, CURLOPT_PORT, $port);
  306.        
  307.         if ($user_agent !== false)
  308.             curl_setopt($this->handler, CURLOPT_USERAGENT, $this->user_agent);
  309.        
  310.         if (is_file($cookie))
  311.         {
  312.             curl_setopt($this->handler, CURLOPT_COOKIE, $cookie);
  313.             curl_setopt($this->handler, CURLOPT_COOKIEJAR, $cookie);
  314.             curl_setopt($this->handler, CURLOPT_COOKIEFILE, $cookie);
  315.         }
  316.        
  317.         /* Preparation of post request
  318.         ---------------------------------- */ 
  319.         if (is_array($post))
  320.         {
  321.             $string = '';
  322.            
  323.             foreach ($post as $key => $value)
  324.                 $string .= "$key=$value&";
  325.            
  326.             curl_setopt($this->handler, CURLOPT_POSTFIELDS, $string);
  327.         }
  328.         else
  329.         {
  330.             curl_setopt($this->handler, CURLOPT_POSTFIELDS, $post);
  331.         }
  332.        
  333.         /* Execution
  334.         ---------------------------------- */ 
  335.         if ($headers)
  336.             return array("headers" => curl_exec($this->handler), "content" => curl_exec($this->handler));
  337.         else
  338.             return curl_exec($this->handler);
  339.     }
  340.    
  341.     /**
  342.      * Send a post request and retrieve the content with HTTP authentication
  343.      *
  344.      * @param string/array $post : Post request
  345.      * @param string $url :    URL of the page
  346.      * @param int $port : Port used to get the page
  347.      * @param string $login : HTTP login
  348.      * @param string $password : HTTP password
  349.      * @param boolean $headers : set it to true if you want headers in the answer
  350.      * @param string $cookie : path to the cookie file if you want to use the object like a web browser
  351.      * @param string $user_agent : if you need to look like a web browser
  352.      * @return string/array
  353.      */
  354.     public function http_auth_post_request($post, $url = false, $port = false, $login = false, $password = false, $headers = false, $cookie = false, $user_agent = false)
  355.     {
  356.         /* Init
  357.         ---------------------------------- */
  358.         $this->test_var($url, $this->url);
  359.         $this->test_var($port, $this->port);
  360.         $this->test_var($login, $this->login);
  361.         $this->test_var($password, $this->password);
  362.         $this->test_var($headers, $this->headers);
  363.         $this->test_var($cookie, $this->cookie);
  364.         $this->test_var($user_agent, $this->user_agent);
  365.        
  366.         /* cURL Setup
  367.         ---------------------------------- */         
  368.         curl_setopt($this->handler, CURLOPT_URL, $url);
  369.         curl_setopt($this->handler, CURLOPT_HEADER, $headers);
  370.         curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
  371.         curl_setopt($this->handler, CURLOPT_FOLLOWLOCATION, true);
  372.         curl_setopt($this->handler, CURLOPT_AUTOREFERER, true);
  373.         curl_setopt($this->handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  374.         curl_setopt($this->handler, CURLOPT_USERPWD, "$login:$password");
  375.         curl_setopt($this->handler, CURLOPT_POST, true);           
  376.        
  377.         if (is_string($post))
  378.             curl_setopt($this->handler, CURLOPT_POSTFIELDS, $post);
  379.            
  380.         if ($user_agent !== false)
  381.             curl_setopt($this->handler, CURLOPT_USERAGENT, $this->user_agent);
  382.        
  383.         if (is_file($cookie))
  384.         {
  385.             curl_setopt($this->handler, CURLOPT_COOKIE, $cookie);
  386.             curl_setopt($this->handler, CURLOPT_COOKIEJAR, $cookie);
  387.             curl_setopt($this->handler, CURLOPT_COOKIEFILE, $cookie);
  388.         }
  389.        
  390.         /* Preparation of post request
  391.         ---------------------------------- */ 
  392.         if (is_array($post))
  393.         {
  394.             $string = '';
  395.            
  396.             foreach ($post as $key => $value)
  397.                 $string .= "$key=$value&";
  398.            
  399.             curl_setopt($this->handler, CURLOPT_POSTFIELDS, $string);
  400.         }
  401.         else
  402.         {
  403.             curl_setopt($this->handler, CURLOPT_POSTFIELDS, $post);
  404.         }
  405.        
  406.         /* Execution
  407.         ---------------------------------- */ 
  408.         if ($headers)
  409.             return array("headers" => curl_exec($this->handler), "content" => curl_exec($this->handler));
  410.         else
  411.             return curl_exec($this->handler);
  412.     }
  413.    
  414.    
  415.     /**
  416.      * Upload a file to a ftp
  417.      *
  418.      * @param string $path : path to the file to upload
  419.      * @param string $url : url of the ftp with the path where to upload
  420.      * @param int $port : Port used to get the page
  421.      * @param string $login : FTP login
  422.      * @param string $password : FTP password
  423.      * @return boolean
  424.      */
  425.     public function ftp_upload($path, $url = false, $port = false, $login = false, $password = false)
  426.     {
  427.         /* Init
  428.         ---------------------------------- */
  429.         $this->test_var($url, $this->url);
  430.         $this->test_var($port, $this->port);
  431.         $this->test_var($login, $this->login);
  432.         $this->test_var($password, $this->password);
  433.        
  434.         /* Checkpoint
  435.         ---------------------------------- */
  436.         if (is_file($path))
  437.         {
  438.             if (!$fhandler = fopen($path, 'r'));
  439.                 return false;
  440.         }
  441.         else
  442.         {
  443.             return false;
  444.         }
  445.        
  446.         /* Var updates if needed
  447.         ---------------------------------- */
  448.         $file = pathinfo($path);
  449.        
  450.         if (!preg_match("#^(ftp:\/\/)#i", $url))
  451.                 $url = "ftp://".$url;
  452.        
  453.         if ($login !== false && $password !== false)
  454.             if (!preg_match("#ftp:\/\/([[:alnum:]-\._])+:([[:alnum:]-\._])+@(.*)#i", $url))
  455.                 $url = preg_replace("#(ftp:\/\/)(.*)#i", "\\1$login:$password@\\2", $url);
  456.            
  457.         if (!preg_match("#(\/)^#i", $url))
  458.             $url .= "/".$file['basename'];
  459.         else
  460.             $url .= $file['basename'];
  461.        
  462.         /* cURL Setup
  463.         ---------------------------------- */ 
  464.         curl_setopt($this->handler, CURLOPT_URL, $url);
  465.         curl_setopt($this->handler, CURLOPT_UPLOAD, true);
  466.         curl_setopt($this->handler, CURLOPT_INFILE, $fhandler);
  467.         curl_setopt($this->handler, CURLOPT_INFILESIZE, filesize($path));
  468.        
  469.         if ($port !== false)
  470.             curl_setopt($this->handler, CURLOPT_PORT, $port);
  471.        
  472.         /* Execution
  473.         ---------------------------------- */ 
  474.         return curl_exec($this->handler);
  475.     }
  476.    
  477.     /**
  478.      * Download a file from a FTP
  479.      *
  480.      * @param string $path : path where to ulpoad
  481.      * @param string $url : url of the ftp with the path of the file to download
  482.      * @param int $port : Port used to get the page
  483.      * @param string $login : FTP login
  484.      * @param string $password : FTP password
  485.      * @return boolean
  486.      */
  487.     public function ftp_download($path, $url = false, $port = false, $login = false, $password = false)
  488.     {
  489.         /* Init
  490.         ---------------------------------- */
  491.         $this->test_var($url, $this->url);
  492.         $this->test_var($port,