在 drupal 目录内的非 drupal.php 文件中使用特定的 drupal 相关函数

发布于 2024-11-26 22:26:39 字数 1808 浏览 1 评论 0原文

大家早上好。 我在尝试使函数“field_file_load”在我处理 AJAX 调用的 php 脚本中工作时遇到了一些问题。

我读过有关引导 drupal 核心元素的内容,但它似乎不起作用。

到目前为止,我已经成功地使用另一个选择框的数据填充了一个选择框,并对这个 php 文件进行了 AJAX 调用(位于 drupal 目录文件夹中,准确地说是在主题中)

<?php
$var = $_GET['q'];

$con = mysql_connect('*******', '******', '********');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("drupal", $con);

$sql="SELECT DISTINCT xc.field_brand_value FROM node 
      INNER JOIN term_node AS tn ON node.vid = tn.vid
      LEFT JOIN content_type_extra_content AS xc ON node.vid = xc.vid 
      WHERE tn.tid IN (SELECT th.tid FROM term_hierarchy AS th WHERE th.parent = '149')
      AND xc.field_location_value = '".$var."'";

$result = mysql_query($sql);
      echo(' <select name="brand" id="brand">
      <option value="default" selected>Select a brand</option>
      ');

while($row = mysql_fetch_array($result))
      {
      echo('<option value="'.$row['field_brand_value'].'">'.$row['field_brand_value'].'</option>');
      }

echo('</select>');
mysql_close($con);
?>

,这就像一个魅力,因为我所要做的就是连接到 drupal 数据库并获取所需的值。

当我想获取一些图片的 url(使用使用第一个和第二个下拉列表中的值的查询)并使用“file_field_load”加载给定图片的 url 时,就会出现问题。

我(显然)收到“调用未定义函数”错误。 所以我尝试引导drupal。

但无论如何都行不通。

/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

由于我没有对托管站点的服务器的完全访问权限,假设 drupal 方便地安装在根目录中,我如何找出 drupal 站点 htdocs 的路径?

此外,调用完整的引导程序(而不仅仅是所需的部分)是否会导致一些问题?

所以,简单来说: 1]如何在驻留在drupal目录中的非drupal php脚本中调用drupal函数(在本例中来自filefiled模块)?

2] 哪种引导方式是正确的? 3]除了引导之外,我还需要连接到数据库(就像前面的工作示例一样)吗?

或者,最后。有没有一种不同的、更快的方法你知道如何做我需要做的事情?

预先感谢您的回复。

Good morning all.
I'm having some issues while trying to make the function "field_file_load" work in a php script I've done to process an AJAX call.

I've read about bootstrapping drupal core elements inside, but it doesn't seem to work.

So far I've succesfully populated a Select Box using the data from another Select Box, making an AJAX call to this php file (which is in the drupal directory folder, in a theme to be precise)

<?php
$var = $_GET['q'];

$con = mysql_connect('*******', '******', '********');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("drupal", $con);

$sql="SELECT DISTINCT xc.field_brand_value FROM node 
      INNER JOIN term_node AS tn ON node.vid = tn.vid
      LEFT JOIN content_type_extra_content AS xc ON node.vid = xc.vid 
      WHERE tn.tid IN (SELECT th.tid FROM term_hierarchy AS th WHERE th.parent = '149')
      AND xc.field_location_value = '".$var."'";

$result = mysql_query($sql);
      echo(' <select name="brand" id="brand">
      <option value="default" selected>Select a brand</option>
      ');

while($row = mysql_fetch_array($result))
      {
      echo('<option value="'.$row['field_brand_value'].'">'.$row['field_brand_value'].'</option>');
      }

echo('</select>');
mysql_close($con);
?>

And this is working like a charm because all I have to do is connecting to the drupal db and fetch the desired values.

The problem arises when I want to fetch the url of some pictures (with a query that uses values from the first and second dropdown) and use the "file_field_load" to load the url of the given picture.

I get (obviously) a "call to undefined function" error.
So I tried bootstrapping drupal.

But it doesn't work anyway.

/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Since I don't have full access to the server where the site is hosted, assuming that drupal is convenientrly installed in the root, how can I figure out the path to drupal site htdocs ?

Moreover, does calling a full bootstrap (instead of just the needed part) can cause some problems?

So, to be brief:
1] how can I call a drupal function (in this case which comes from the filefiled module) in a non-drupal php script which resides however in the drupal directory?

2] Which is the correct way of bootstrapping?
3] Do I need to connect to the db (like in the previous working example) IN ADDITION to bootstrapping?

Or, finally. there's a different, speedier way you know how to do what I need to do?

Thanks in advance for any reply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏雪无痕 2024-12-03 22:26:39

嗯,这很奇怪。如果启用了 FileField 模块,则该功能应该可用。那么也许 FileField 实际上并未启用?

如果是这种情况,您将必须手动添加包含函数定义的文件,即模块目录中的 field_file.inc 文件,因此您需要将该依赖项添加到引导代码中:

<?php
/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('filefield', 'inc', 'field_file');

据我所知,您从外部脚本引导 Drupal 所做的事情是“正确”的方式。

现在,我不确定,从大局来看,无论你想做的事情是否都是一个好主意......也就是说:你正在制作一个小的非Drupal脚本,它:

  1. 使用普通的 mysql 函数而不是 Drupal 的 DB API 函数手动连接到 Drupal 数据库,以便
  2. 使用 100% 容易受到 SQL 注入攻击的查询获取 CCK 信息,并且
  3. 所有这些都放在主题目录中!

所以你可能想重新考虑一下你的攻击角度,你知道吗?也许为此制作一个自定义模块。

但如果你只是必须这样做(出于我无法想到的原因),那么至少使用db_query 所以你不必做整个 mysql_connect() 的事情,并做类似的事情

<?php 
db_query("YOUR BIG QUERY HERE... xc.field_location_value = '%s'", $var); 

......至少某种程度的安全性。

我还建议您稍微浏览一下所涉及的模块(FileField 等),看看它们是否具有 API(或至少一些内部函数),这些 API 可能会返回您试图通过普通数据库查询获取的内容。

Hmm that's weird. If the FileField module is enabled, the function should be available. So maybe FileField is not actually enabled?

If that's the case you're gonna have to manually add the file that contains the function definition, which is the field_file.inc file in the module's directory, so you'd add that dependency to your bootstrapping code:

<?php
/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('filefield', 'inc', 'field_file');

AFAIK what you're doing for bootstrapping Drupal from an outside script is the "correct" way.

Now, I'm not sure if, on a big picture level, whatever you're trying to do is a good idea at all... That is: You're making a little nonDrupal script which:

  1. manually connects to the Drupal database with plain mysql functions instead of Drupal's DB API functions, in order to
  2. fetch CCK information using a query that's 100% vulnerable to SQL injection, and
  3. all of this put in a theme directory no less!

So you might want to rethink your angle of attack here, you know?. Maybe making a custom module for this.

But if you just have to do things this way (for reasons I can't think of), then at least use db_query so you don't have to do the whole mysql_connect() stuff, and do something like

<?php 
db_query("YOUR BIG QUERY HERE... xc.field_location_value = '%s'", $var); 

...for at least some degree of security.

I would also recommend that you browse the involved modules a bit (FileField, etc) to see if they have APIs (or at least some internal functions) that might return what you're trying to get through plain DB querying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文