popup.php calculates a base_url from CGI vars. Don't do that, use $base_url from sites/default/settings.php.

In particular, the url to jquery.js will be bad if there is any url rewriting, aliasing, or reverse-proxying.

My patch does a set_include_path() with an obnoxious and brittle relative path, and then includes the settings.php. Ideally, the include_path would point to the drupal directory. I don't see a good way to do that auto-magically (presumably via .htaccess).

CommentFileSizeAuthor
#2 popup_load_script.patch2.01 KBstborchert
popup_php.patch1.46 KBawgrover

Comments

stborchert’s picture

Component: Code » Code: Javascript
Assigned: Unassigned » stborchert

Hi.
Thanks for your work and thoughts.
I've thought and tested a very long time for a fail-safe way to get the drupal installation path within popup.php and talked to others how to do this.
The included code is the result of this. (Btw: its the same way drupal "calculates" $base_url.)

popup.php calculates a base_url from CGI vars. Don't do that, use $base_url from sites/default/settings.php.

But you can't include settings.php without having the real installation path ($base_url).
set_include_path('../../../../../..' . PATH_SEPARATOR . get_include_path()); is not really an alternative because modules aren't restricted to be installed in

sites/.../modules

. If you install tinymce in drupalinst/modules/ the pat(c)h wont work anymore.

There isn't a better solution (yet). In 4.7 I've packaged jquery.js with linktonode but I didn't want to do this because drupal.js includes all the needed functionality.
Don't know how to calculate the base_url in a better way.

Perhaps I can use old js-code from functions.js (4.7.x):

var url = tinyMCE.baseURL.substring(0, tinyMCE.baseURL.indexOf('modules/'));
if (url.indexOf('sites/') > -1)
  url = url.substring(0, url.indexOf('sites/'));

With these lines the path to drupal.js can be build easily. After this I have to include jquery.js with javascript (sound weird; and it is indeed ;-) )
Should be something like this:

var script = document.createElement("script");
script.type = "text/javascript";
script.src = url . 'misc/drupal.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(script);

I will check this...

stborchert’s picture

Status: Active » Needs review
StatusFileSize
new2.01 KB

Oh damn. Why the hell I try to include drupal.js? I need jquery.js. tsts

The attached patch is not tested (did not have a running drupal inst here).
It loads jquery.js based on the baseURL tinyMCE calculates (this on should be correct) and after this functions.js.
Should work.

awgrover’s picture

I thought of, but didn't propose something in the same spirit:

Use the environment variable SCRIPT_NAME, and the same sort of substring'ing to find the drupal install directory. Then use that as the set_include_path. Assuming that the install dir can be reliably deduced (as you do for the url). Since you assume the url is reliable for purposes of deduction, and it is mapped to a directory, I think the we can make the same assumption about the SCRIPT_NAME. This avoids the include-via-javascript.

stborchert’s picture

Hi.
You're right, that should do the same. I will test this and create a new patch.

stborchert’s picture

Well, I've tested this approach but it wont work:
I can include settings.php but
- you do not know if it the correct settings.php (keyword: multisite installations)
- settings.php does not necessarily set a variable called $base_url so there's no win in including settings.php

To avoid the js stuff we only can go back to do something like this:

<?php 
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
$base_url .= '://'. $_SERVER['HTTP_HOST'];
$dir = substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], 'modules/'));
if (strpos($dir, 'sites/') !== FALSE) {
  $dir = substr($dir, 0, strpos($dir, 'sites/'));
}
$base_url .= $dir;
?>
...
<script type="text/javascript" src="<?php print $base_url ?>misc/jquery.js"></script>
stborchert’s picture

Status: Needs review » Fixed

Used the patch for the new release. Using the javascript functions is the easiest and best working solution.

stborchert’s picture

Status: Fixed » Closed (fixed)