[horde] Using curl wrappers instead of fopen

Daniel Reich me at danielreich.com
Mon Jan 20 22:17:42 PST 2003


I've been using Horde for a while but this is my first input.   On my
setup, I enforce a rather strict security policy at the firewall.  I
allow the webserver to open ports to nowhere except if authenticated or
I allow a specific IP.  That being said, http out to the world is closed
by default.  One can make arguments for and against this policy but
that's another discussion for a different mail list.  

Anyways, I'm not up to speed on where to submit code so if I'm wrong,
please accept my apologies and turn me in the correct direction.

I'd like to offer up an alternative to using fopen.  Granted curl is yet
another module to install, but I think it'd be useful.  I wrote a quick
function that uses curl to grab a URL via proxy (authenticated too).  

I thought this could go into the Horde library.  And if this is ok, I'd
like to write another function which will act as a wrapper so that if
you have curl extensions installed it will use curl, otherwise it will
use fopen.  Other horde modules could utilize this to pull down lots of
URLs (ie Jonah).  I should probably extend this to handle cookies in
some fashion too now that I'm looking back...

   /**
     *
     * Open a URL using curl extension (if its installed) and return the
output.
     *
     * @access public
     * @param string $url       The URL to fetch
     *
     * @return string   The output of the URL we fetched
     */

     function curlOutput($url)
     {

        global $conf;

        if (is_null($url)) {
            return false;
        }

        // maybe this should return a more voiceful error to help the
user?
        if (!extension_loaded('curl')) {
            return false;
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        if (isset($conf['curl']['proxy'])) {
            curl_setopt($ch, CURLOPT_PROXY, $conf['curl']['proxy']);
        }

        if (isset($conf['curl']['auth'])) {
           curl_setopt($ch, CURLOPT_PROXYUSERPWD,
$conf['curl']['auth']);
           curl_setopt($ch, CURLOPT_USERPWD, $conf['curl']['auth']);
        }

        // Try 5 times to fetch URL
        for ($i=0;((curl_getinfo($ch,
CURLINFO_HTTP_CODE)!=200)&&($i<5));$i++) {
           $ce = curl_exec($ch);
        }

        if (curl_getinfo($ch, CURLINFO_HTTP_CODE)!=200) {
           echo 'http error -' . curl_getinfo($ch, CURLINFO_HTTP_CODE) .
'-    ';
           curl_close($ch);
           return false;
        }

        curl_close($ch);
        return $ce;

     }


and then to use this, we just need to add the following to
horde/config/conf.php

/**
 ** Horde CURL Settings
 **/

// If Horde should use a proxy server when retrieving remote URLs,
// What is the IP and port to use?  The format should be IPAddress:Port
// Example:  '192.168.1.1:8080'
$conf['curl']['proxy'] = "";

// If the proxy server requires authentication, or if you are running
// a transparent proxy server that will authenticate, enter the
// credentials here.  The format is username:password.  Example:
// 'horde:mypass'.
$conf['curl']['auth'] = "";





More information about the horde mailing list