Jump to content

Mirror files from live server for htaccess protected sites


gebeer
 Share

Recommended Posts

Hello all,

wasn't sure where to put this, so it goes in General section.

Ryan shows a hook that we can use to mirror files on demand from live server to development environment to be up to date with the files on the server without having to download complete site/assets/files folder.

I just implemented this but had problems getting files to load from a site in development that is secured with user/password via htaccess.

First I tried to use WireHttp setHeader method for basic authentication like this

    function mirrorFilesfromLiveServer(HookEvent $event)
    {
        $config = $event->wire('config');
        $file = $event->return;

        if ($event->method == 'url') {
            // convert url to disk path
            $file = $config->paths->root . substr($file, strlen($config->urls->root));
        }

        if (!file_exists($file)) {
            // download file from source if it doesn't exist here
            $src = 'http://mydomain.com/site/assets/files/';
            $url = str_replace($config->paths->files, $src, $file);
            $http = new WireHttp();
            // basic authentication
            $u = 'myuser';
            $pw = 'mypassword';
            $http->setHeader('Authorization: Basic', base64_encode("$u:$pw"));
            $http->download($url, $file);
        }
    }

But, unfortunately this didn't work.

So now I am using curl to do the download. My hook function now looks like this

    function mirrorFilesfromLiveServer(HookEvent $event)
    {
        $config = $event->wire('config');
        $file = $event->return;

        if ($event->method == 'url') {
            // convert url to disk path
            $file = $config->paths->root . substr($file, strlen($config->urls->root));
        }

        if (!file_exists($file)) {
            // download file from source if it doesn't exist here
            $src = 'http://mydomain.com/site/assets/files/';
            $fp = fopen($file, 'w+'); // init file pointer
            $url = str_replace($config->paths->files, $src, $file);
            $u = 'myuser';
            $pw = 'mypassword';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 50); // crazy high timeout just in case there are very large files
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERPWD, "$u:$pw"); // authentication
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // authentication
            curl_setopt($ch, CURLOPT_FILE, $fp); // give curl the file pointer so that it can write to it
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            $data = curl_exec($ch);
            curl_close($ch);
        }
    }

Now I can load files and images from the htaccess protected development server ?

If anyone knows how to get this to work with WireHttp, please let me know. Thank you.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...