Jump to content

$config->httpHosts Programmatically


joer80
 Share

Recommended Posts

You can also just leave config httpHosts blank.

You could update it via API like this

        $hosts = wire("config")->httpHosts;
        $hosts[] = "ww.domain.ch";
        wire("config")->set("httpHosts", $hosts);

This should be in a autoload init(). But I'm not sure it be early enough to work.

Link to comment
Share on other sites

No you can (with my example) add entries to the $config->httpHosts but it's won't have an effect cause the whitelist is checked before you can do this in a module or template or whatever. I'm not sure why you'd want this. It's not like you change this all the time.

Link to comment
Share on other sites

You can change it, but wherever you do so it's most likely to late to be picked up by the internal check, that compares the actual httpHost to the whitelisted ones. The changes would only be available to everything running later. The only way to have it pick up for the internal check, too, is by editing the config file itself, which could be made automatically. 

Another option would be supplying no httpHosts all together and call the check manually after adding all the needed domains. But it's a question for Ryan to answer if this would actually be of use for the security.

@Soma 

I can understand that need. If you'd build some kind of SaaS software, where the user should be able to add their domains, you wouldn't want to edit those manually.

  • Like 2
Link to comment
Share on other sites

I mentioned in the initial post I am working on a multi site install where I already have to create the url list in the module manually.  

That means every time I add a users website to my multisite, I have to manually add it 2 places.  One using the cms and one hand editing the config file.  It would be nice if I could add it manually only one time and make one read the other.

Anytime you have 2 manual lists, it creates an opportunity for something to become out of sync.  If I am going to update one time, I would prefer it to be via cms/database instead of file to edit as well.

  • Like 1
Link to comment
Share on other sites

so what's wrong with changing

$config->httpHosts = array('domain1.com', 'domain1.com');

to something like that

$config->httpHosts = explode(PHP_EOL, __multisite_domains_textarea_content__);

in config.php?

Link to comment
Share on other sites

If I need to, I could open my own database connection and look up:
 
SELECT data FROM `modules` WHERE class = 'Multisite' LIMIT 0 , 1
 
That would return:
 
{"subdomains":"www.domainname1.com\nwww.domainname2.com"}
 
Then I could set the $config->httpHosts variable with that.

I am sure this query would be cached by mysql into ram since it would be ran so much.
Link to comment
Share on other sites

That would be great, but I dont think the module would be accessible yet.  I know very little about processwire though so I could be wrong!

so what's wrong with changing

$config->httpHosts = array('domain1.com', 'domain1.com');

to something like that

$config->httpHosts = explode(PHP_EOL, __multisite_domains_textarea_content__);

in config.php?

Link to comment
Share on other sites

Soma, I am using your version of multisite module, so it may be best if we add this to your module.  

We could have it load urls from config instead of its own box.   Even if there is a select box to tell it where to load them from and give the user the option.

Then when Ryan allows cms to edit config file to stable branch, there will be only one place required.

Link to comment
Share on other sites

Found it! ProcessWireConfig

https://processwire.com/blog/posts/processwire-core-updates-2.5.10/

I uploaded this module and now I can edit config settings.  

Instead of editing the file, it simply creates a config.json in your assets folder and adds code to the end of your config file to check for and read this new file.

It adds the contents of that file to your $config array.

What improvements / features are you adding to your new version of multi site?

Link to comment
Share on other sites

This feels like a bad idea, but I can't figure out why. Fiddling around I found that...

$config->httpHosts = '*';

...works. It allows my addon domains through. Is this a bad idea? Why?

Another way around it, although not all that neat, would be to edit an external CSV hosts file. Then...

$config->httpHosts = explode(',', file_get_contents("hosts.txt"));

...would read it in the config. You'd have to build your own way of updating that file which in my case wouldn't be a problem because I'm building a kind of second custom CMS for my site's users.

Still don't know how I'm going about this. I don't want to skip on good practices.

Link to comment
Share on other sites

I think you can leave it blank and it will work, but it is not as safe security wise.

What you describe is what I talked about above.  ProcessWireConfig Allows you to do that.

This feels like a bad idea, but I can't figure out why. Fiddling around I found that...

$config->httpHosts = '*';

...works. It allows my addon domains through. Is this a bad idea? Why?

Another way around it, although not all that neat, would be to edit an external CSV hosts file. Then...

$config->httpHosts = explode(',', file_get_contents("hosts.txt"));

...would read it in the config. You'd have to build your own way of updating that file which in my case wouldn't be a problem because I'm building a kind of second custom CMS for my site's users.

Still don't know how I'm going about this. I don't want to skip on good practices.

Link to comment
Share on other sites

  • 6 months later...

I tried to use this in a new project and ProcessWireConfig is no longer there.  It says:

Error reported by web service: Unable to find that module

Is this still a good way to do things?   

I don't think it was ever in the modules directory, was it?

You can get it from here: https://github.com/ryancramerdesign/ProcessWireConfig

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...