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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Robin S
      This module is sort of an upgrade to my earlier ImageToMarkdown module, and might be useful to anyone working with Markdown in ProcessWire.
      Copy Markdown
      Adds icons to images and files that allow you to copy a Markdown string to the clipboard. When you click the icon a message at the top left of the screen notifies you that the copying has occurred.
      Screencast

      Note: in the screencast an EasyMDE inputfield is used to preview the Markdown. It's not required to use EasyMDE - an ordinary textarea field could be used.
      Usage: Images
      When you hover on an item in an Images field an asterisk icon appears on the thumbnail. Click the icon to copy an image Markdown string to clipboard. If the "Description" field is populated it is used as the alt text.
      You can also open the "Variations" modal for an image and click the asterisk icon to copy an image Markdown string for an individual variation.
      Usage: Files
      When you hover on an item in a Files field an asterisk icon appears next to the filename. Click the icon to copy a link Markdown string to the clipboard. If the "Description" field is populated it is used as the link text, otherwise the filename is used.
       
      https://github.com/Toutouwai/CopyMarkdown
      https://processwire.com/modules/copy-markdown/
    • By picarica
      So hello i am trying to get a .png file from file field and put it automatically to image field, why png image is in the file field is because i already have a hook that extracts .zip and uploads all content into file field, but i just realizes i cant use size() function on image in file field so i am tryin got reupload it to images field
      i already have something like  this in ready.php
      $word = ".png"; foreach($page->subor_hry as $file) { if(strpos($file, $word) !== false){ $page->images_thumb = $file->url; } } by my logic it should work but it dosnt i get error ProcessPageEdit: Unable to read: /site-hry/assets/files/1027/flash_fishy_screenshot.png
      when i remove url from $file->url i just get ProcessPageEdit: Item added to ProcessWire\Pageimages is not an allowed type
      so what am i doing wrong? is there some other way to do this ?
      also can i have all this in
      $this->addHookAfter('Pages::saveReady', function(HookEvent $event) { whats the correct function to have it apply on all pages ?
    • By rooofl
      Hi! I am trying to create a hook that takes the value of a form file field, and add it to an image gallery of an existing page.
      $forms->addHookBefore('FormBuilderProcessor::processInputDone', function($e) { $form = $e->arguments(0); if($form->name == 'new-inuse') { // related_font is a page selector $font = $form->getChildByName('related_font'); // getting the page name $fontName = $font->attr('value')->name; // finds the page from its name $fontPage = wire('pages')->find("name=$fontName"); // in_use_image is a file upload field $image = $form->getChildByName('in_use_image'); // trying to add the image to the existing image gallery in_use field $fontPage->in_use->add('$image'); // error here $fontPage->save(); } }); This outputs the following error: `Call to a member function add() on null`. Any idea what’s wrong with my code?
    • By donatas
      Hi,
      I am trying (wondering if even possible) to set a very different file folder path for my PW install. The path I want to set exist in the same server, but in another domain. Can the PW API see that far into file system? It is a shared hosting environment so no permission management options available.
      Also can this be achieved through .htaccess redirection? If you request a file (images mostly, but translation json files are important too) from `domain-A.com/site/assets/files/` to grab them from `domain-B.com/site/assets/files/`? I would prefer to do this through PW API, I can see the function `$config->setPath()` but it didn't work when set in  config.php like `$config->setPath("files", "/domains/domain-B/public_html/site/assets/files/")`.
      Is there some other option to do this?
      My ultimate goal is to have two PW installs on different domains but one is just a "mirror" that is using the same database as the other and should use the same files structure if editors upload any images to the main (domain-B.com) website. I could use domain parking function but it then needs a more expensive SSL certificate for two domains , which I am trying to avoid if possible 🙂 (I'd like to use single domain Let's Encrypt certificates, thus I need to PW installs).
      Would appreciate any insight! Thanks!
    • By picarica
      so what i am trying to do is that i uploded some files into file field, and then i want hook to get MD5sum and other stuff from .xml and put it into text field into it coresponding pages, pages like this are gonna be many, for each one i want it to output it into its fields
×
×
  • Create New...