'Server Not Found' error when using sfDomainRoutePlugin with Symfony - apache

I am trying to create a site with subdomains, using the sfDomainRoutePlugin plugin. I am using SF version 1.4.12 on Linux, with Apache as the web server.
I am following the online instructions and have created the following routing file:
homepage:
url: /
class: sfDomainRoute
param: { module: foo, action: index }
requirements:
sf_host: [portal.localhost]
#Sample route limited to one subdomain
blog:
url: /
class: sfDomainRoute
param: { module: foo, action: foo1 }
requirements:
sf_host: blog.portal.localhost
#Sample route that will capture the subdomain name as a parameter
user_page:
url: /
class: sfDomainRoute
param: { module: foo, action: foo2 }
#Sample route that will not receive a subdomain and will default to www.greenanysite.com
install:
url: /install
class: sfDomainRoute
param: { module: foo, action: foo3 }
My foo module code has the methods foo1, foo2 and foo3 implemented as stub functions, and each have their template which simply contains text confirming which method was executed (e.g. 'foo::Foo1 was called') etc.
The template for the index method (in the foo module) looks like this:
<html>
<head><title>Test subdomains</title></head>
<body>
<ul>
<li><?php echo link_to('homepage', '#homepage'); ?></li>
<li><?php echo link_to('blog', '#blog'); ?></li>
<li><?php echo link_to('zzzrbyte', '#user_page?subdomain=zzzrbyte'); ?></li>
<li><?php echo link_to('install', '#install'); ?></li>
</ul>
</body>
</html>
The urls are generated correctly (i.e. with subdomains as specified in the routing.yml file), however when I click on the 'blog' or 'zzzrbyte' link, I get the error message: 'Server Not Found'
For example, I got this message:
Server not found Firefox can't find the server at
blog.portal.localhost.
AFAICT, I am following the online instructions exactly, so I can't see where I am going wrong. Can anyone spot what is likely to be causing this problem?.
[[UPDATE]]
I just realized that by adding the subdomain to my hosts file, this seems to get rid of the problem. I am not sure if this is the fix or simply a temporary workaround. If this is the way to do things, I wonder why such a vital piece of information was left out of the notes?
If this is the way to get things to work, it means that subdomains will have to be known before hand (i.e. not generated dynamically and resolved at run time), also - I am not sure how such a solution works for a remote server, since I am running multiple websites (as virtual servers) on one physical machine and I am not using a hosts file on the server.
Any help will be much appreciated.

Adding the subdomain to the hosts is the correct way to solve this issue

Related

php-curl times out when calling a php on the same domain

I do have a very strange problem where I cannot find any way for debugging. I have a server running Arch Linux, Apache 2.4 and PHP versions 7.4, 8.0 and recent 8.1 as php-fpm. All PHP configurations are identically (modules and configs). But 8.1 has a very strange behaviour regarding php-curl.
I install this php (curltest.php) on my server:
<?php
// this was the originally intended code:
// define ('HTTP_URL', 'https://'.$_SERVER['HTTP_HOST'].'/curltest.php?test=1');
// this doeas not work, too:
define ('HTTP_URL', 'https://'.$_SERVER['HTTP_HOST'].'/any_php_script.php');
// all commented following lines are for testing if $_GET is the cause
// if (empty($_GET['test'])) {
$curlHandle = curl_init();
$fp = fopen('/tmp/error.txt', 'w+');
curl_setopt_array($curlHandle, array(
CURLOPT_URL => HTTP_URL,
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => $fp,
CURLOPT_FAILONERROR => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_RETURNTRANSFER => true
));
$response = curl_exec($curlHandle);
if (curl_errno($curlHandle)) {
$response = 'Curl error: ' . curl_error($curlHandle);
}
curl_close($curlHandle);
//} else {
$response = 'Erfolg';
//}
echo $response;
The script works perfectly if I run it with PHP 7.4 and 8.0. If I use PHP8.1 I get a "timeout" - no response, nothing in the logs. I dug a bit deeper and tested with calling a text or html file instead of the script itself: this works flawlessly. If I call another php on the same domain the issue pops up again. But if I call a php on the same server but a different domain it works! So the problem only exists:
when using PHP 8.1 (8.1.13-1)
calling a php script which resides on the same domain as my testscript
So it cannot be a dns issue (calling text or html files is working). And it cannot be a problem that I cannot access php via a php-curl: using different domain (on the same server running the same php version) is also working. Any hint for debugging this is welcome.
As already stated I tested with different file types and different domains (on the same server using the same php version). I increased php log level and activated curl to be verbose writing STDERR to file output without success.
You are getting a timeout because you are running in an endless loop. It looks like in that one setup the $_GET variable is not populated and so the if condition always evaluates to true, opening another curl request.
The reason for this can be a misconfiguration of variables_order (php.ini) or FPM parameters (sometimes in a file like fastcgiparams or fastcgi_param params in nginx).
You can verify this by putting a small message after opening the error log:
fwrite($fp, 'Opening socket ' . (string)$curlHandle . PHP_EOL);
Make sure you got G in ini_get('variables_order');

angular2 call remote rest api [duplicate]

I'm making an Ajax.request to a remote PHP server in a Sencha Touch 2 application (wrapped in PhoneGap).
The response from the server is the following:
XMLHttpRequest cannot load http://nqatalog.negroesquisso.pt/login.php. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin.
How can I fix this problem?
I wrote an article on this issue a while back, Cross Domain AJAX.
The easiest way to handle this if you have control of the responding server is to add a response header for:
Access-Control-Allow-Origin: *
This will allow cross-domain Ajax. In PHP, you'll want to modify the response like so:
<?php header('Access-Control-Allow-Origin: *'); ?>
You can just put the Header set Access-Control-Allow-Origin * setting in the Apache configuration or htaccess file.
It should be noted that this effectively disables CORS protection, which very likely exposes your users to attack. If you don't know that you specifically need to use a wildcard, you should not use it, and instead you should whitelist your specific domain:
<?php header('Access-Control-Allow-Origin: http://example.com') ?>
If you don't have control of the server, you can simply add this argument to your Chrome launcher: --disable-web-security.
Note that I wouldn't use this for normal "web surfing". For reference, see this post: Disable same origin policy in Chrome.
One you use Phonegap to actually build the application and load it onto the device, this won't be an issue.
If you're using Apache just add:
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
in your configuration. This will cause all responses from your webserver to be accessible from any other site on the internet. If you intend to only allow services on your host to be used by a specific server you can replace the * with the URL of the originating server:
Header set Access-Control-Allow-Origin: http://my.origin.host
If you have an ASP.NET / ASP.NET MVC application, you can include this header via the Web.config file:
<system.webServer>
...
<httpProtocol>
<customHeaders>
<!-- Enable Cross Domain AJAX calls -->
<remove name="Access-Control-Allow-Origin" />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
This was the first question/answer that popped up for me when trying to solve the same problem using ASP.NET MVC as the source of my data. I realize this doesn't solve the PHP question, but it is related enough to be valuable.
I am using ASP.NET MVC. The blog post from Greg Brant worked for me. Ultimately, you create an attribute, [HttpHeaderAttribute("Access-Control-Allow-Origin", "*")], that you are able to add to controller actions.
For example:
public class HttpHeaderAttribute : ActionFilterAttribute
{
public string Name { get; set; }
public string Value { get; set; }
public HttpHeaderAttribute(string name, string value)
{
Name = name;
Value = value;
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.AppendHeader(Name, Value);
base.OnResultExecuted(filterContext);
}
}
And then using it with:
[HttpHeaderAttribute("Access-Control-Allow-Origin", "*")]
public ActionResult MyVeryAvailableAction(string id)
{
return Json( "Some public result" );
}
As Matt Mombrea is correct for the server side, you might run into another problem which is whitelisting rejection.
You have to configure your phonegap.plist. (I am using a old version of phonegap)
For cordova, there might be some changes in the naming and directory. But the steps should be mostly the same.
First select Supporting files > PhoneGap.plist
then under "ExternalHosts"
Add a entry, with a value of perhaps "http://nqatalog.negroesquisso.pt"
I am using * for debugging purposes only.
This might be handy for anyone who needs to an exception for both 'www' and 'non-www' versions of a referrer:
$referrer = $_SERVER['HTTP_REFERER'];
$parts = parse_url($referrer);
$domain = $parts['host'];
if($domain == 'google.com')
{
header('Access-Control-Allow-Origin: http://google.com');
}
else if($domain == 'www.google.com')
{
header('Access-Control-Allow-Origin: http://www.google.com');
}
If you're writing a Chrome Extension and get this error, then be sure you have added the API's base URL to your manifest.json's permissions block, example:
"permissions": [
"https://itunes.apple.com/"
]
I will give you a simple solution for this one. In my case I don't have access to a server. In that case you can change the security policy in your Google Chrome browser to allow Access-Control-Allow-Origin. This is very simple:
Create a Chrome browser shortcut
Right click short cut icon -> Properties -> Shortcut -> Target
Simple paste in "C:\Program Files\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --disable-web-security.
The location may differ. Now open Chrome by clicking on that shortcut.
I've run into this a few times when working with various APIs. Often a quick fix is to add "&callback=?" to the end of a string. Sometimes the ampersand has to be a character code, and sometimes a "?": "?callback=?" (see Forecast.io API Usage with jQuery)
This is because of same-origin policy. See more at Mozilla Developer Network or Wikipedia.
Basically, in your example, you to need load the http://nqatalog.negroesquisso.pt/login.php page only from nqatalog.negroesquisso.pt, not localhost.
if you're under apache, just add an .htaccess file to your directory with this content:
Header set Access-Control-Allow-Origin: *
Header set Access-Control-Allow-Headers: content-type
Header set Access-Control-Allow-Methods: *
In Ruby on Rails, you can do in a controller:
headers['Access-Control-Allow-Origin'] = '*'
If you get this in Angular.js, then make sure you escape your port number like this:
var Project = $resource(
'http://localhost\\:5648/api/...', {'a':'b'}, {
update: { method: 'PUT' }
}
);
See here for more info on it.
You may make it work without modifiying the server by making the broswer including the header Access-Control-Allow-Origin: * in the HTTP OPTIONS' responses.
In Chrome, use this extension. If you are on Mozilla check this answer.
We also have same problem with phonegap application tested in chrome.
One windows machine we use below batch file everyday before Opening Chrome.
Remember before running this you need to clean all instance of chrome from task manager or you can select chrome to not to run in background.
BATCH: (use cmd)
cd D:\Program Files (x86)\Google\Chrome\Application\chrome.exe --disable-web-security
In Ruby Sinatra
response['Access-Control-Allow-Origin'] = '*'
for everyone or
response['Access-Control-Allow-Origin'] = 'http://yourdomain.name'
When you receive the request you can
var origin = (req.headers.origin || "*");
than when you have to response go with something like that:
res.writeHead(
206,
{
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': origin,
}
);

Defining Variable Configuration in _config.yml in jekyll powered website

There are multiple sets of configuration which you may want to execute when you are running the site locally while when your site is running on server (say Github).
I have defined similar set of configuration in my _config.yml file like this
title: Requestly
locale: en_US
description: Chrome Extension to modify HTTP(s) Requests
logo: site-logo.png
search: true
env: dev
config:
dev:
url: http://localhost:4000
prod:
url: http://requestly.github.io/blog
url: site.config[site.env].url // Does not work
I have used {{ site.url }} everywhere else in my templates, layouts and
posts.
How can I define site.url in my _config.yml file whose value depends upon the config and env defined in the same file.
PS: I know one of the ways is to change {{ site.url }} to {{ site.config[site.env].url }} in all the files. That should probably work.
I just want to know how to use variables in _config.yml. Is that even possible ?
No you cannot use variables in a _config file.
You can find more informations here : Change site.url to localhost during jekyll local development
Yes you can with Jekyll 3.8.0 or later version now. Please give that a try

Magento SOAP API: wsdl soap:address location incorrect

I'm trying to connect to a Magento instance using the SOAP API v2, and although I can see the wsdl when I visit http://www.domain.loc/api/v2_soap?type=soap&wsdl=1 in my browser, I am unable to execute the login() call, an error is always thrown.
Code:
$options = array(
'trace' => 1,
'cache_wsdl' => WSDL_CACHE_NONE,
//'location' => 'http://www.domain.loc/index.php/api/v2_soap?wsdl=1',
'soap_version' => SOAP_1_2,
'connection_timeout' => 120,
'exception' => 0,
'encoding' => 'utf-8',
);
try {
$client = new soapClient('http://www.domain.loc/api/v2_soap?type=soap&wsdl=1', $options);
$client->login('username', 'password');
//$result = $client->catalogProductList($sessionId);
//$result = $client->call($session, 'catalog_product.list');
var_dump($client);
} catch (SoapFault $e) {
var_dump($e);
}
Which yields "SoapFault: looks like we got no XML document"
When I uncomment the location option I get "SoapFault: Wrong Version"
When I view the wsdl file, I see the soap:address location set as
<port name="Mage_Api_Model_Server_HandlerPort" binding="typens:Mage_Api_Model_Server_HandlerBinding">
<soap:address location="http://www.domain.loc/index.php/?SID=7o7mn7iiu9tr8u1b9163r305d4dp1l1jrcn1hmnr34utgnhgb6i0&type=soap"/>
Which seems incorrect as it is the URL to the homepage, with the SID and a query param.
Magento version EE1.14
Things I have tried:
Disable local modules - no change
Using Zend_Soap_Client - no change
Various different URL configurations - tried everything under the
sun, no change
Using SOAP v1 - same results
Tried using this on a remote instance instead of my local - same
result
Checked phpinfo - yes SOAP is installed
Tried debugging - When I turn on Xdebug and run my test script it
seems to prevent the script from even running. Browser just
indicates loading forever
Tried to uncomment line in .htaccess to rewrite RewriteRule
^api/([a-z][0-9a-z_]+)/?$api.php?type=$1 [QSA,L] - No change
(yes mod rewrite is on and working)
Tried fetching wsdl and saving locally and passing it into SOAP
client constructor - no change
Anyone know how to troubleshoot this? I see several threads with these errors, but either unanswered or have tried their solution and it did not work in this case. Why is my location in the wsdl incorrect? Any help at all would be appreciated. Thanks!

Code works in PHP 5.3.2. In PHP 5.2.17 get Invalid argument supplied for foreach()

I am using this code:-
<?php // Load and parse the XML document
$rss = simplexml_load_file('http://partners.userland.com/nytRss/nytHomepage.xml');
$title = $rss->channel->title;
?>
<html xml:lang="en" lang="en">
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<?php
// Here we'll put a loop to include each item's title and description
foreach ($rss->channel->item as $item) {
echo "<h2><a href='" . $item->link . "'>" . $item->title . "</a></h2>";
echo "<p>" . $item->description . "</p>";
}
?>
</body>
</html>
Which I got from this site www.ibm.com/developerworks/library/x-simplexml.html
I have one puzzling issue.
When I run the code on my development server it works with no problem.
When I run it on my web host server I get this error report:-
Warning: Invalid argument supplied for foreach() in /web1/............../test3.php on line 15
My development server is a TurnKey Linux LAMP server with PHP 5.3.2.
My web host has PHP 5.2.17 running on Linux.
Looking up the error message on the web seems to indicate that the data read from the XML feed is not being treated as an array by PHP 5.2.17.
The solutions on here under 'Invalid argument....foreach()' that I have tried do not resolve the issue.
Any ideas as to how to get around this?
It looks as though Torstein's instincts are 'on the button'; my webhost seems to be blocking downloading files from the internet.
I used this code segment to try to load the distant file to a local copy:-
if(!#copy('http://partners.userland.com/nytRss/nytHomepage.xml','./buffer.xml'))
The resulting file buffer.xml contained this:-
The URL you requested has been blocked. URL = partners.userland.com/nytRss/nytHomepage.xml
This is not specific to this url, I get the same on a BBC newsfeed url.
So, indications are that it is not a PHP problem!
I have raised the issue with my webhost provider.
One thing this problem has reminded me of is that the PHP error message & line number may be far removed from the actual problem!
Thanks to Torstein, mario & GusDe Cool for helping me get my head around this problem.
BillP
It looks like your webhost has disabled opening of files on the internet.
I'm not sure how simplexml_load_file() works, but if you run phpinfo() on the website and options like allow_url_fopen and allow_url_include are disabled, thats a good indication.