Getting the domain dynamically in a Symfony Console Command - php

I am encountering precisely this problem:
Symfony Docs - How to Generate URLs and Send Emails from the Console
Our email templates are being filled with "localhost" instead of "my.real-domain.name". When constructing links to the application using twig's "url('some/path')".
However, where Symfony is usually "one installation per domain", our application is designed so a single instance can handle multiple domains. It constructs the necessary configuration through various configuration channels, with each customer being one channel.
Thus I would like to avoid configuring "router.request_context.host" and others for every single customer channel.
So I would like to grab the domain to be used from a "--domain" console parameter that we give to every console command instad.
But instead of doing it in every single command, I would need to do this in one central location that grabs the domain and configures "router.request_context" dynamically according to the console parameter.
Is there any way I can do that?

You can register a listener on the event dispatcher which listens to the ConsoleEvents::COMMAND and configure it further before the run method on the command is called.
For example, you can use a setter or change the input on the ConsoleCommandEvent instance.
https://symfony.com/doc/3.4/components/console/events.html
I'm not sure this is an ideal solution, but this is what I could think of at the moment.

You can tell to the router which domain use when generating an absolute URL, as example:
$domain = $input->getArgument('domain');
$context = $this->getContainer()->get('router')->getContext();
$context->setHost($domain);
$route = $this->getContainer()->get('router')->generate('welcome_page', $params, UrlGeneratorInterface::ABSOLUTE_URL);
$output->writeln('Use the following url to show the welcome page for the provided domain');
$output->writeln(sprintf('<info>%s</info>', $route));
Hope this help

Related

Difference between match parameter and RequestContext in Symfony Router UrlMatcher

I don't understand the semantics of the Symfony Routing Component's API.
From the first code example on the Routing Component documentation page:
$context = new RequestContext('/');
$matcher = new UrlMatcher($routes, $context);
$parameters = $matcher->match('/foo');
Why is the hostname and HTTP method passed in via $context and the path via a parameter to match()? Or is it? There is also a path parameter in the RequestContext constructor.
One gets the impression match() is supposed to be called multiple times with different paths within one request, which I can't imagine would ever happen.
After integrating the Routing Component into my application, I now have a hunch why
it was done like that.
Most of the properties of the RequestContext - method, request body, get parameters - can be used without modification, but depending on the desired path structure and server configuration (rewrite rules, etc.) there are multiple ways in which the path needs to be preprocessed.
This doesn't explain why the path is passed to the match() function and the request object is passed to the constructor, but it does explain why they are passed in separately.

How to use multiple endpoints for Symfony NexySlackBundle

I am using the nexylan/slack Bundle for my symfony 3.4 application. I configured the slack Incoming WebHook for #general channel and it's working as expected. The bundle configuration looks something like:
nexy_slack:
# If you want to use an another httplug client service.
http:
client: httplug.client
# The Slack API Incoming WebHooks URL.
endpoint: https://hooks.slack.com/services/ABCD/987ABC
channel: null
username: null
icon: null
link_names: false
unfurl_links: false
unfurl_media: true
allow_markdown: true
markdown_in_attachments: []
Now I have another channel called #dev and I've added the Incoming WebHook and received the endpoint. I also want to send messages to the dev channel.
My question is, how can I configure the dev channel endpoint too in order to use it. Is there any way I can do this?
Here is the Slack Bundle
It looks like the bundle only supports 1 endpoint. If you want to have multiple endpoints you either have to fork or send in a PR.
Basically what you need to do, is adjust both files in src/DependencyInjection.
In Configuration.php you need to ensure that you can define multiple endpoints by adding a parent array node, e.g. called endpoints. Then inside NexySlackExtension you can foreach through each endpoint configuration and do the same configuration as before just add a prefix or suffix. So something like:
$configuration = new Configuration();
$endpointConfigs = $this->processConfiguration($configuration, $configs);
foreach ($endpointConfigs['endpoints'] as $config) {
// ....
}
You might also want do add some special handling for a "default" endpoint. This should already do the trick, although it might need some adjustments as I haven't looked into the Bundle in detail. Maybe you can also contact the author via a ticket in the Issue tracker and they can help you write a PR.

Symfony2 cache and parameters

I have an odd app set up, multiple sub domains all pointing to the same Symfony2 install.
The subdomain simply changes a single parameter within the application so that different content is loaded.
This parameter is being cached however, so visiting a.site.com and then b.site.com makes A's content appear rather than B's.
Is there another approach I can use?
Is there a way I can add this parameter the cache's hash or something similar so that when the parameter changes within the app the content can change.
I am using this method to pass the constant from the sub domain into the Symfony2 application
How to pass a PHP constant as service argument in Symfony 2?
You can use Symfony's Request Object to get the URL and parse that for the subdomain. Use that to create your configuration parameter because it won't be cached.
Like this:
echo $request->getUri();
Symfony's API can be a great resource, but a bit hard to read. http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Request.html
I ended up writing a service to parse the host name, that service was then available within my Controllers.
class GameSystem {
public function getGameSystemName()
{
$ar = explode(".", $_SERVER['HTTP_HOST']);
$first = reset($ar);
$game_system_short_name = strtolower($first);
return $game_system_short_name;
}
}
Initially I had tried to use the constant value set in each subdomain's index file. But this was failing and having some really odd effects.

PHP ldap_* API: Limiting to base scope?

So The Direct Project strikes again. I'm no expert in LDAP, but I'm trying to set up a test environment since the standard requires any package to support getting certificates from LDAP as well as DNS CERT, regardless of which method is implemented by the package.
According to the documentation, the prescribed sequence of events (trimmed for relevance) from section "3.3.3 LDAP query":
* Discover the Base DNs
Branches in LDAP must be defined by a “Base DN”. The list of Base DNs that are
provided by a LDAP directory are found by doing a LDAP Query with a NULL (i.e.
“”) Base DN, and ObjectClass=”DN”.
* Query across the Base DN for entries where "Mail" contains the endpoint address
I'm trying to implement this process in php, using the ldap_* functions, but their way doesn't seem to work. Obviously, NULL is not the same as an empty string (the latter makes any call to ldap_search return a "No such object" error), and "DN" isn't a valid value for an ObjectClass attribute.
So, TL;DR, is there another way an anonymous remote user retrieve the (list of?) base DNs that I'm missing?
UPDATE: Reworded the title to reflect the root cause of my problem: Reading the rootDSE from PHP when the ldap_* api doesn't allow you to specify 'base' scope.
So another read through the docs answered my question for me.
Apparently, the only difference between ldap_search(), ldap_list(), and ldap_read() are the scopes (LDAP_SCOPE_SUBTREE (sub), LDAP_SCOPE_ONELEVEL (one), and LDAP_SCOPE_BASE (base), respectively). So using ldap_read() instead of the others will allow one to get the rootDSE.
In the root dse. See "namingContexts".
Update:
In java:
LDAPConnection conn = new LDAPConnection(hostname,port);
SearchRequest req = new SearchRequest("",SearchScope.BASE,"(&)","+");
SearchResult result = conn.search(req);
// If the search succeeds, the result will comprise one entry,
// and that entry is the Root DSE:
dn:
subschemaSubentry: cn=schema
namingContexts: C=us
vendorName: UnboundID Corp.
vendorVersion: UnboundID Directory Server 4.1.0.6

Using Kohana in File outside Kohana

Here's the situation:
I have a catch-all on my domain email (so *#domain.com) redirecting to a piping script located at /home/domain/scripts/piper.php. This piper script is not within the Kohana ORM, but all of my other files are. I want to try to use Kohana inside this piper.php file.
I have tried (unsuccessfully) all of the following:
Including Kohana
I couldn't figure out what needed to be included, and more importantly how to override the url variable that Kohana uses to determine the right controller. Also, this is a catch-all piper, so it isn't using HTTP (to my knowledge), so much as executing a command.
Piping
I tried piping to the following:
/home/domain/public_html/index.php --uri="piper"
But cPanel makes this impossible, as you can only specify the destination script, and not the proper flags and such (unless I am missing something).
PHP exec()
I tried using the following line:
exec("php /home/domain/public_html/index.php --uri=\"/piper\"")
I was hoping that the stdin data would be maintained across the exec() command, but I could never get it to recognize the uri command, though I can run this on my localhost and it works just fine.
I was using http://www.coderelic.com/2011/10/creating-cron-jobs-in-kohana-3-x-is-a-piece-of-cake/ as a reference, but can't get anything to work.
I'm happy with either one of these solutions such that I can see an incoming email, parse it, then send emails based on the parameters.
Let me know if you need more information! I'm le stumped.
/home/domain/public_html/index.php --uri="piper" would be a valid way to do it. If your host sucks and doesn't let you specify that, put it into a bash script instead and reference that.
If you are on any recent version of kohana (3.2 or 3.3), a better way to do this would be to use Minion to run the command line task. This is what Minion was designed for.
All you need to do is to:
modify your piper.php script to be a valid PHP class;
place it in /application/classes/ folder;
Kohana will automatically load your class file (like include) during initialization.
Then you can use your piper class as usual class by $piper = new Piper; ....
UPD
You have to serve your emails trough Kohana.
Create controller, for example pipe (route it with /pipe URL):
public function action_pipe() {
$pipe = new Pipe; // This creates new Pipe object (your emails serving class)
$pipe->serve(); // Sserve emails within `serve()` method of Pipe class
}
Although admittedly, I'm not sure if these other answers are correct because I can't figure out how to reproduce the results.
What ended up working for my situation was to create a Controller_Piper class that is called in the /home/domain/scripts/piper.php. What I did was to copy the code from /home/domain/public_html/index.php and changed the following:
echo Request::factory("/piper")
->execute()
->send_headers(TRUE)
->body();
This loads the piper controller and executes everything very nicely. Not sure if it's the cleanest, but it does work.

Categories