Jump to content
gebeer

Mirror files from live server for htaccess protected sites

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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Paul Greinke
      Hi there. I wrote a custom module for one of my projects. In fact I maybe want to use my module in other projects too. In order to be variable and customizable  I need to implement some custom hooks into my module. So I can afterwards hook into the my functions in order to modify them to match the needs of the new project.
      I tried simply defining functions with the '__' prefix. But that did not work. I'm imagining something like the following:
      <?php class MyClass { public function ___someFunction() { // Do something } } // ready.php $this->addHookBefore('MyClass::someFunction', function($event) { // some customization }); Is there a way to accomplish that? 
    • By dotnetic
      Hi folks, I published "Simple file downloads with ProcessWire tutorial"  today which explains how to make a simple download function with ProcessWire (tested with version 3.0+).
      Basically this is based on my post here in the forums 
       
    • By picarica
      Hello so am trying to make a hook so that all checkbox field is defaul;ty checked when making new page, soo i made hook in init.php and the contents are
      <?php $wire->addHookAfter("Pages::added", function($event) { $page = $event->arguments(0); // check for page template if necessary here $page->checkboxfield('check', 1); }); ?> and what i get
      which is weird because "table" is a repeat field where checkboxfield is nowhere present, but table repeat field and checkboxfield are in the same template, so why does it outputs error like that? any ideas ?
    • By jploch
      Hi folks,
      i try to use a hook to change some markup inside admin, this hook works fine inside a ready.php file, but its not working inside my module:
      <?php namespace ProcessWire; class InputfieldPageTableExtendedGrid extends InputfieldPageTable { public static function getModuleInfo() { return array( 'title' => __('Inputfield for PageTableExtendedGrid', __FILE__), // Module Title 'summary' => __('Adds Inputfield for PageTableExtendedGrid', __FILE__), // Module Summary 'version' => 233, 'requires' => array('FieldtypePageTableExtendedGrid'), 'permanent' => false, ); } public function ready() { $this->addHookAfter('Page::render', function($event) { $value = $event->return; // Return Content $style = "<style type='text/css'>". $this->pages->get((int) wire('input')->get('id'))->style ."</style>"; // Add Style inside bottom head $event->return = str_replace("</head>", "\n\t$style</head>", $value); // Return All Changes }); } } whats wrong here?
    • By Sten
      Hello
      Till now I hacked something with the twig template but it works no more with new PW versions so I look forward to create a module. I am working on a site in multiple languages : French, English, Italian, German, Spanish, Portuguese, Hebrew, Russian. The new posts are entered in any language with a field for language. Till now, I got twig files to get the translations with constants defined for each part of the pages.
      So I'd like to create a module to include theses files added according to the url /fr/en/...
      Have you some observations to do before I begin about the direction to take ?
      Thank you
×
×
  • Create New...