Zend Framework: From a folder (not /) - php

i notice that when i run a zend framework app from a server, there are alot of side effects. main issue is where i use urls like
/auth/login
i need to use
$this->baseUrl('/auth/login');
thats simple to fix. but when i use
$request->getRequestUri()
for use in redirects. eg after login, i want to redirect the user back to the prev page, it goes to the wrong place. eg. my app root is "http://localhost/app1", $request->getRequestUri() will give /app1. when i try to redirect back, it will goto http://localhost/app1/app1. btw, i am using Zend Server + IIS7 and my app is configured to run from the url stated above. maybe i shld be going to "/" instead. how can i resolve this?
update
this is in my Zend_Form class
// (Zend_Form) Login.php init()
$req = Zend_Controller_Front::getInstance()->getRequest();
$returnUrl = $req->getParam('returnUrl', $req->getRequestUri());
$this->addElement('hidden', 'returnUrl', array(
'value' => $returnUrl
));
// AuthController after login
$returnUrl = urldecode($form->getElement('returnUrl')->getValue());
if (!empty($returnUrl)) {
$this->_helper->getHelper('Redirector')->setGotoUrl($returnUrl);
}

Based on you update:
Its the prependBase-Option in the Redirector what you are looking for:
prependBase: boolean flag indicating whether or not to prepend the base URL when a relative URL is provided
So your fix is:
$this->_helper->getHelper('Redirector')->setGotoUrl($returnUrl, array('prependBase' => false));

If you are using Zend_Application you can use this in your application.ini, and wont need to specify anything else.
resources.frontController.baseUrl = "/your/public/path/"

I have solved this problem with help of Apache configs. In file \usr\local\apache\conf\vhosts.conf find a block with your site and change ways and public folder.

Related

webroot entire path in Yii2

I have some trouble with an Yii2 application.
I need to put into a variabile the complete webroot path of my application, in my case www.mysite.it/language/catalogue/. When I call the function to retrieve this information, I get www.mysite.it/application/web where application/web is the root of my project.
I have tried this function:
$home = Yii::$app->request->getAbsoluteUrl();
It is any way to do what I want?
Thanks in advance.
You should use yii\helpers\Url features
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
http://www.yiiframework.com/doc-2.0/guide-helper-url.html
and eg_ for home Url::home()
$absoluteHomeUrl = Url::home(true);
or for Url:to()
$url = Url::toRoute(['language/catalogue/']);
the use of the UrlHelpers prevent your code from different url result related
to your urlManager config (with or without pretty url)

Facebook API: redirect_uri

I've readeded that FB API have problems with redirect_uri - and i've try to make it work on my framework. Problem is easy, but i haven't found working solution so i think this is right place, and someone help me.
I've creating login link:
$loginUrl = $helper->getLoginUrl( $app_url . 'fb/oauth/' , $permissions );
where $app_url . 'fb/oauth/' looks like this http://domain.com/fb/oauth/
This path is redirected thru htaccess > index.php (routing) > plugins/facebook/facebook-callback.php
And in oauth.php file i've valid code - but i've get error:
Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request
Main problem is that i don't want to redirect to domain.com/fb_oauth.php - i prefer to use rewrite/route way to be more pretty. Everything is correct, i've checked slash at the end (because some have problems with that).
I also think that error may be in the way that route redirect to callback file:
if($route->path = 'fb/oauth') {
include $app_path . "plugins/Facebook/facebook-callback.php";
} else {
die("Unknown");
}
Please help me - i don't have other ideas. I prefer to not use direct link to .php, and use my way (my variables are defined before include destination file)
Thank You !
EDIT:
I've also try domain.com/oauth and domain.com/oauth/
same error :-(

Drupal hook_url_inbound_alter not altering my url

I have in my module named 'categorie' a implementation of:
function categorie_url_inbound_alter(&$result, $path, $path_language) {
if ($path == 'e') {
$result = 'user';
}
}
I'm planning to do something a bit more advanced but I can't even seem to get the basic one working.
With this implementation I expect that if a user goes to mysite.com/e , he gets the user page. But I'm getting a 404.
In this topic:Using module: url_alter and it's hook: hook_url_outbound_alter() they also suggested you need to implement the hook_boot() with nothing in it. However I did implement this hook and this didn't change the behavior.
UPDATE:
I have the "Path" module installed from core and I declared some url aliases in the clean url section. (Maybe this gives a conflict?)
UPDATE2: cleaning the cache didn't do the trick either.
UPDATE3:
I also tries doing the url rewriting in the htaccess file. But when the url was rewritten it destroyed the theming. (https://drupal.stackexchange.com/questions/76475/drupal-does-rewrite-url-but-cant-load-css-themes/76493?noredirect=1#76493). So that's why I'm now trying to do it with the custom module.
What cache did you clear ? It probably not sufficient
to just clear the page cache. You can try a call to
drupal_lookup_path('wipe');
To clear the alias cache. This fixes many problems with the alias system.
Other things to check are the alias database table to be sure that the alias are being
set in the first place.
I'm not 100% sure, cause i'm also very new to drupal. But i think you need to implement hook_url_outbound_alter as well. Good luck. If i find a better answer i'll post it.
Cheers

Codeigniter Loading external site in default controller

I'm not able to figure this out on my own so here I am asking for your help.
How do I load a website that I already made as a view in the code igniter default controller?
I put my website under a folder name site, and in the default controller I loaded the view site/index , but then in my site there are problems with the includes and redirects... I don't know why, I guess the way the site usually works with redirecting isn't compatible with code igniter style
edit: I guess I would have to turn off CI engine for this site, but I don't know why, because I would still need codeingiter to manage other parts of my application
"CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'Blog';
Where Blog is the name of the controller class you want used. If you now load your main index.php file without specifying any URI segments you'll see your Hello World message by default."
http://codeigniter.com/user_guide/general/controllers.html
fragment copied from that link , you should put the controllers classname in that config, not the view
I guess it's better to choose one of these options:
Modify the existing site to a CodeIgniter site.
Keep your site separate from the CodeIgniter site, and just link between the two sites.
The way you are trying to do it seems very useless and causing a lot of extra trouble.
You can simply use the redirect function in your controller. If you supply a full URL you can go to any other page. You will, of course, leave your CI app.
redirect('http://www.example.net/page_in_external_site/');
Try using the APPPATH constant when defining the paths for the includes.
I know it's an old question, but you can try using a view template with an iframe, and you can pass the URL to the src property of the iframe. That way you can display your site inside a view, but still can't get access to the vars passed to the view from your site.
In system/Core/Loader.php change the line 141 to look like this:
$this->_ci_view_paths = array(APPPATH . 'views/' => TRUE, FCPATH => TRUE);
and to get the view is simple:
$this->load->view('application/ PATH_TO_VIEW');

How to find out programmatically if a web server instance supports url rewrite

What I want to ask is if there is a way to find out if a web-server instance has URL Rewriting enabled. I need this in order to be able to instantiate the correct type of URL handler.
Theoretically you know in advance if you have it enabled or not and can use something to configure it. I would like, however, to be able to detect this setting automatically at runtime.
The URL rewrite rule would be something very simple like:
^/(.*)$ => /bootstrap.php
This guarantees that the relevant string is present in the REQUEST_URI, but doesn't pollute the _GET array.
Where did my research took me so far:
Apache.
In my opinion Apache has a very quirky approach, since it sets the REDIRECT_SCRIPT_URI header for rewrote URLs, but not for the ones that are not rewrote.
E.g. http://host/ana/are/mere would be re-wrote to index.php so the aforementioned header would be present, but http://host/ wouldn't be re-wrote.
Lighttpd.
Lighttpd with fast-cgi behaves OK, setting the REDIRECT_URI header if URL Rewrite is enabled for the current host. This is reliable.
Cherokee.
Well, for Cherokee there is no method that I found out, as it uses (in my opinion) a more complicated method for obtaining URL rewriting. (I.e., it's called internal redirect – and the fcgi process doesn't know that the request was redirected)
Also I haven't tested other http servers, as nginx, so if someone has some input on this matter I would love to hear it.
Not the most elegant solution, but you could create a directory, insert a .htaccess and a small php file and try to open it with curl/file_get_contents() from your actual code:
.htaccess
RewriteEngine on
RewriteRule ^(.*?)$ index.php?myparam=$1
index.php
<?php
//open with file_get_contents("http://yoursite/directory/test")
if($_GET['myparam']){die("active");}
?>
Although this might be acceptable during an installation, for performance reasons this shouldn't be used for every request on your site! Save the information somewhere (sqlite/textfile).
Update
Apache specific, but apache_get_modules()/phpinfo() in combination with array_search/strpos is maybe helpful to you.
It's already touched upon below, but I believe the following recipe is a rather waterproof solution to this problem:
Set up the redirection
Request a page through its rewritten url
If the request returns the page in question, you have redirection set up correctly, if you get HTTP 404 response, then it's not working.
The idea is basically that this works with just about any redirection method. It has already been mentioned, but bears reiterating, such tricks add quite a bit of overhead and are better performed only once (installation or from the settings panel) and then saved in the settings.
Some implementation details, choices to make and a little on how I came to this solution:
I remembered Drupal did such a check during the installing process, so I looked up how they did it. They had the javascript on the install page do an ajax request (synchronously, to prevent concurrency issues with the database). This requires the user installing the software to have javascript turned on, but I don't think that's an unreasonable requirement.
However, I do think using php to request the page might be a cleaner solution. Alongside not bothering with a javascript requirement, it also needs less data to be sent back and forth and just doesn't require the logic of the action to be spread over multiple files. I don't know if there are other (dis)advantage for either method, but this should get you going and let you explore the alternative choices yourself.
There is another choice to be made: whether to test in a test environment or on the normal site. The thing Drupal does is just have the redirection always turned on (such as in the apache case, have the .htaccess file that does redirects just be part of the Drupal download) but only write the fancy urls if the redirection is turned on in the settings. This has the disadvantage that it takes more work to detect which type of redirection is used, but it's still possible (you can for example add a GET variable showing the redirection engine either on a specific test page or even on every page, or you can redirect to a page that sets $redirectionEngine and then includes the real index). Though I don't have much experience with redirection other than with mod_rewrite on apache, I believe this should work with just about every redirection engine.
The other option here is to use a test environment. Basically the idea is to either create a folder and set up redirection for it, or remove the need for file system write access and instead have a folder (or a folder for each redirection engine). This has some disadvantages: you still need write access to set up the redirection for the main site (though maybe not for all redirection engine, I don't really know how you all set them up properly - but for apache you will need write access if you are going to turn on redirection), it might be easier for a bot to detect what software and what version of it you are using through accessing the tests (unless you remove the test folders after testing) and you need to be able to rewrite for only a part of the site (which makes sense for any redirection engine to be a possibility, but I'm not blindly going to assume this functionality). However, this does come with the advantage of it being easier to find out which rewrite engine is being used or basically any other aspect of the redirection. There might also be other advantages I don't know of, so I just give the options and let you pick your method yourself.
With some options left to the user, I believe this should help you set up the system in the manner that you like.
PHP has server-specific functions for Apache, IIS and NSAPI servers. I only have Apache but as merkuro suggested this works as expected:
<?php
if (in_array('mod_rewrite',#apache_get_modules()))
echo 'mod_rewrite enabled';
else
echo 'mod_rewrite not enabled';
?>
As PHP server-specific functions don't cover all the servers you'd like to test in this probably isn't the best solution.
I'd recommend merkuro's first answer - implementing then testing it in script. I believe it's the only way to get a good result.
Hope that helps!
You can programmatically check for the existence of mod_rewrite if the server is Apache by using the apache_get_modules() function in PHP:
$modules = apache_get_modules();
echo in_array('mod_rewrite', $modules) ? 'mod_rewrite detected' : 'mod_rewrite not detected';
This could be used as the first step, but it is not a full proof method by any means. Just because mod_rewrite is loaded does not mean it is available for your environment. This also doesn't help if you are on a server that is not Apache.
There are not many consistent methods that will work across all platform combinations. But since the result is consistent, you can test for that. Setup a special redirect, and have a script use PHP's cURL or file_get_contents() to check a test URL. If the redirect was successful, you will get the expected content, and you can test easily for this.
This is a basic .htaccess I setup to redirect ajax to ajax.php:
RewriteEngine On
RewriteRule ajax ajax.php [L]
The following PHP script will attempt to get the contents of ajax. The real script name is ajax.php. If the redirect fails, then it will not get the expected contents.
error_reporting(E_ALL | E_STRICT);
$url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).'/ajax';
$result = json_decode(#file_get_contents($url));
echo ($result === "foobar") ? 'mod_rewrite test was successful' : 'mod_rewrite test failed';
Lastly, here is the final piece of the script, ajax.php. This returns an the expected response when the redirect is successful:
echo json_encode('foobar');
I have setup a live example of this test, and I have also made available the full sources.
As all the awnser already mention, actually testing it is the only way to be sure it works. But instead of actually redirecting to an actual page and waiting for it to load, I would just check the header.
In my opinion this is quickly enough to be even used at runtime at a regular site. If it realy needs to be high performance, then ofcourse caching it is better.
Just put something like the following in your .htaccess file
RewriteEngine on
RewriteRule ^/redir/My/Super/Special/Hidden/Url/To/Test/$ /redir/longload.php [L,R=307]
And then you can use the following php code to check if mod_rewrite is enabled.
<?php
function HasModRewrite() {
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
$protocol = substr($sp, 0, strpos($sp, "/")) . $s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$options['http'] = array(
'method' => "HEAD",
'follow_location' => 0,
'ignore_errors' => 1,
'timeout' => 0.2
);
$context = stream_context_create($options);
$body = file_get_contents($protocol . "://" . $_SERVER['SERVER_NAME'] . $port .'/redir/My/Super/Special/Hidden/Url/To/Test/', NULL, $context);
if (!empty($http_response_header))
{
return substr_count($http_response_header[0], ' 307')>0;
}
return false;
}
$st = microtime();
$x = HasModRewrite();
$t = microtime()-$st;
echo 'Loaded in: '.$t.'<hr>';
var_dump($x);
?>
output:
Loaded in: 0.002657
---------------------
bool(true)

Categories