Jump to content

Access module from another domain via API?


Harmen
 Share

Recommended Posts

Hello,

Currently I have a domain setup for 4 languages, each language has its own domain. Databases are seperated.

Per domain I have a module that is able to export the data of all products on the website in an excel file. Now this only exports the single language of the domain the module is installed on, so far so good.

Now my sales colleagues want one portal to download the data file from domain x while they are logged in at domain y instead of logging in to every domain to get the file they want, which is only logical. 

The module is ready and the necessary functions are accessible from the front-end of the website. So one way would be to create a page on each domain that takes the necessary variables, loads the module on its domain and download the file to the user. However, I was wondering if it is possible to access via the API the module on domain y from domain x?

Thanks in advance,

Harmen

Edited by Harmen
title
Link to comment
Share on other sites

30 minutes ago, dragan said:

If the sites are all on the same server, you could try to bootstrap PW.

Could work if it was accessible, path to domain y is not withing the allowed paths from domain x according to the errow below:

Quote

Warning: is_file(): open_basedir restriction in effect. File(/xxxx/www/xxxxxx/xxxxxxxxx/index.php) is not within the allowed path(s): (xxxx/www/vhosts/xxxxx.com/:/tmp/) in /xxxx/www/vhosts/xxxxxxxxx/xxxxxxx/xxxxxxxxxxxxxxxx/wire/core/FileCompiler.php on line 31

Doesn't seem like the safest option...

 

Scratch that, used the wrong path. Seems like you can't bootstrap Processwire in an existing PW installation because of class declarations:

Quote

Compile Error: Cannot declare class ProcessWire\ProcessWire, because the name is already in use (line 0 of /var/www/vhosts/xxxxxxxxxx.com/xxxxxxxx/processwire_EN/site/assets/cache/FileCompiler/var/www/vhosts/xxxxxxxxxxx/xxxxxxxxx/processwire_NL/wire/core/ProcessWire.php)

Or am I doing something wrong?

Edited by Harmen
filepath edit + spelling
Link to comment
Share on other sites

@dragan Yes, still it isn't working, if I include the other domain from the current directory the module is installed in, I get the following error for this piece of code:

require "../../../processwire_NL/wire/core/ProcessWire.php";
$site = new \ProcessWire\ProcessWire('../../../processwire_NL/site/', "https://www.domain.nl");

// ERROR: Compile Error: Cannot declare class ProcessWire\ProcessWire, because the name is already in use (line 0 of /var/www/vhosts/r-go-tools.com/httpdocs/processwire_EN/site/assets/cache/FileCompiler/var/www/vhosts/r-go-tools.com/httpdocs/processwire_NL/wire/core/ProcessWire.php)

If I include the other domain from the root directory I get the following error with this piece of code:

require "./var/www/httpdocs/processwire_NL/wire/core/ProcessWire.php";
$site = new \ProcessWire\ProcessWire('./var/www/httpdocs/processwire_NL/site/', "https://www.domain.nl");

// ERROR: Warning: require(/var/www/vhosts/r-go-tools.com/httpdocs/processwire_EN/site/modules/ExportProductData/var/www/httpdocs/processwire_NL/wire/core/ProcessWire.php): failed to open stream: No such file or directory in /var/www/vhosts/r-go-tools.com/httpdocs/processwire_EN/site/assets/cache/FileCompiler/site/modules/ExportProductData/ExportProductData.module on line 446

Now this is the first time I am trying to achieve something with bootstrapping PW so please forgive any mistakes

Link to comment
Share on other sites

This is what I tried at first but then I get the following error, it just doesn't seem to work. Any code after that doesn't execute because of this line.

Quote

include "../../../processwire_NL/index.php";

Compile Error: Cannot declare class ProcessWire\ProcessWire, because the name is already in use (line 0 of /var/www/vhosts/domain.com/httpdocs/processwire_EN/site/assets/cache/FileCompiler/var/www/vhosts/domain.com/httpdocs/processwire_NL/wire/core/ProcessWire.php)

Any ideas?

Link to comment
Share on other sites

So, I tried it out today myself, and it turns out... it can indeed be tricky. 

In one instance I was only able to query pages that have no access restrictions whatsoever. Others gave me a Nullpage as result. 

Maybe somebody else who has used this in a real-life project can shed some more light on that.

Link to comment
Share on other sites

@Harmen So finally I found some more time to investigate and try out a few things.

Here's what works and what doesn't for me:

Spoiler

<?php namespace ProcessWire;

$site = new ProcessWire("../pw/");

// regular page (non-hidden, published)
$regular = $site->pages->get(1002);
d($regular);

// this works too: get hidden pages that don't have physical view template (only used for page references):
$kantone = $site->pages->find("parent=1001, include=all");
d($kantone);

// this doesn't work: getting an unpublished page:
$unpub = $site->pages->find("id=1329, include=all");
a($unpub); // count: 0

// this shows: guest
d($site->user);
// it would be interesting to know whether it is technically possible (without using CURL) to login as the superuser of the site you are trying to pull infos from. If that was possible, I am sure you could get unpublished pages as well. Perhaps that's the missing link why some stuff works and other doesn't. Ryan is keen on security, so I wouldn't wonder if that is the reason you can't query everything with the bootstrapping method.

My setup:

Two PW sites installed locally (one of the latest PW versions from dev branch).

Both are accessible locally via dummy domains instead of localhost (pw.test + pw2.test).

Windows, Apache, PHP 7.3.13, Laragon.

Both sites have the relevant other domains in the site/config.php $config->httpHosts whitelist array. Not sure if that is necessary.

I have also temporarily switched from DB-sessions to regular sessions, because I was getting fatal PHP session errors. Googling around I only found various pointers to bugs in PHP 7.2, so I installed PHP 7.3.13.

tldr: There are lots of "ifs" and strange things going on trying to make this work. I really wish the docs would be more helpful with that bootstrapping feature. And as mentioned before - I hope someone else chimes in who managed to make it all work as intended.

  • Like 1
Link to comment
Share on other sites

OK, here's an update...

The above code was running inside a PW template.

If I create a file in the root of my PW installation and access it directly @ foo.tld/bootstrap.php, all of the following stuff will work:

Spoiler

<?php namespace ProcessWire;

include_once "../pw/index.php";

$regular = $pages->get(1002);
echo $regular->title;

// this works: hidden pages without physical view template (only used for page references):
$kantone = $pages->find("parent=1001, include=all");
foreach($kantone as $k) {
    echo "$k->title <br>";
}

// this works: getting an unpublished page:
$unpub = $pages->find("id=1329, include=all"); // probably also works now with simple get()
echo $unpub->title;

// this works as well: getting $config data
echo "<pre>";
print_r($config->httpHosts);
echo "</pre>";

 

So I guess the main remaining question is: Can we access other PW sites from within a PW "regular" template, and let those two communicate with each other? Or do we have to build some interface between the two, so PW doesn't get confused?

I guess we still have to wait for this.

  • Like 1
Link to comment
Share on other sites

@dragan Thank you so much for trying all these options. Using a template I can get the same results as you. Running this in the module file for some reason it keeps listing the pages of the domain the module is installed on instead of the domain I am trying to access.

I really hope this gets easier in the future. For now I think I am going to create a dummy template for every domain that takes some variables and returns the file I need.

 

Again thanks so much for looking into this. Let's hope this will be an option in the near future.

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.
×
×
  • Create New...