Including wp-load.php causes fatal error inside a class method - php

Im doing a direct .php call (ajax). Due to my classes, loading system, I would need to load wp-load later, not at the start.
So, if my file has only this:
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
it runs ok. But if its wrapped inside a class:
class Paff
{
public function x()
{
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
}
}
(new Paff())->x();
it dies with a fatal error:
PHP Fatal error: Call to undefined method stdClass::escape() in /var/www/wp-content/sunrise.php on line 11
I cant see any reason why!

It seems like you're trying to load a WordPress multisite outside of the normal parameters (judging by sunrise.php being the issue here). Try loading this file instead:
include $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php';
This skips index.php which defines the WP_USE_THEMES constant and assumes you're not loading the front-end of the site (at least not traditionally, which you're not).
That should load everything and in the right order for you now.

I also could not see anything wrong with your code. So I tested it with one change just for confirmation that it does work.
<?php
class Paff
{
public function x()
{
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
echo 'wp-load.php included. ABSPATH=' . ABSPATH;
}
}
(new Paff())->x();
?>
It runs without error! and displays the following in the browser.
wp-load.php included. ABSPATH=D:\bronce/
I displayed the ABSPATH just to confirm that wp-load.php was included as it defines ABSPATH.
So this works as it should on my installation which is very standard as I just installed PHP a few weeks ago and I know I didn't change the defaults very much. So why does it not work on your system? It must be configured different. Is your system running any pre-execution handlers?
I just did a search for 'sunrise.php' which I originally thought was the name of your code. It may actually be an infection (or not) see https://premium.wpmudev.org/blog/removing-backdoor-exploits/. I think given its name it is configured to run as a pre-execution handler. Some plugins legitimately install pre-execution handlers, e.g. WordFence. Unfortunately, I did not install WordFence this time and I don't recall the name of its pre-execution handler.
I just read the source code to WordFence. It does install a pre-execution handler but its name is ABSPATH . 'wordfence-waf.php'. However, WordFence also has a reference to '.../wp-content/sunrise.php' apparently executed before multisite loading. Are you using WordFence? Is your site a multisite?
The important point is: The problem is not in your code but in a pre-execution handler. A pre-execution handler can be a huge amount of PHP code that can significantly change the PHP execution environment, which is why plugins such as WordFence use it to monitor your code execution.

Related

Class not found after "require_once"

I get the following error:
`Fatal error: Class 'DummyClass' not found in...`
<?php
require_once("3rdparty/simplesaml/lib/_autoload.php");
class login extends DummyClass { (this is the line the error refers to)
[...]
}
?>
If I comment out the require_once it works perfectly fine.
DummyClass is defined externally and can be found in the prepend-file. (I don't think it matters for this problem as it works as expected if I comment out require_once)
The path to the file should also be correct as it gives me a "Failed opening required..." Error if I change the path.
I also tried switching between PHP 5.6 and 7 - no difference.
So, I would like to ask you for help. Do you have any hints / ideas, why I might get that error?
Problem solved.
The old framework was using the old __autoload function, which is deprecated.
SimpleSAMLPHP used the new function. Those autoload-combinations cause one of them to override the other.
Solution:
Switch from __autoload to spl_autoload_register.
Similar Question: Override vendor autoload composer

Weird Zend Framework issue - include_once is looking in the wrong folder

I've spent a lot of time troubleshooting this myself but none of what I've read solves my issue so I'm hoping I get some help here.
So anyway, I have written a PHP script that provides various functions to connect with Google Calendar. When I run this script directly using some inline test code to call my functions, everything runs fine. However, when I call the function from other scripts using 'require_once' to include it I get the following errors:
Warning: include_once(Zend\Gdata\Calendar\Extension\EventQuery.php) [function.include-once]: failed to open stream: No such file or directory in C:\Users\Luke Franklin\Clients\Tiers For Tea\Development\TiersForTea.com v1.1\lib\Zend\Loader.php on line 134
Warning: include_once() [function.include]: Failed opening 'Zend\Gdata\Calendar\Extension\EventQuery.php' for inclusion (include_path='.;C:\xampp\php\PEAR;C:\Users\Luke Franklin\Clients\Tiers For Tea\Development\TiersForTea.com v1.1\lib;C:\xampp\htdocs\TiersForTea.com\lib') in C:\Users\Luke Franklin\Clients\Tiers For Tea\Development\TiersForTea.com v1.1\lib\Zend\Loader.php on line 134
Your first thoughts might be that I'm not using the correct include path, but I have checked and rechecked this many times. I even tried hard coding the path. I'm quite sure that I'm using the correct path.
Now for the weird bit. If you look at the error you will notice the file Zend is trying to include: Zend\Gdata\Calendar\Extension\EventQuery.php. This file does not actually exist in the 'Extension' folder. It does exist in the parent folder though. If I just copy 'EventQuery.php' into the 'Extension' folder my script runs as expected. Weird, right?
So that does sorta solve my problem, but I would like to know what's going on here in-case it creates further issues. I should also note that I'm calling this script into an OpenCart module.
You might want to see some of my code so here's a snippet of the important bits, if you want more details just let me know:
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . realpath('\\lib') . PATH_SEPARATOR . "C:\\xampp\\htdocs\\TiersForTea.com\\lib");
require_once('Zend/Loader.php');
function connect() {
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
I doubt the error exists in any of the Zend files as you have indicated it works correctly in your isolation tests.
The error is thrown from Zend_Loader which means something in your code is making reference to Zend_Gdata_Calendar_Extension_EventQuery.
Check the stack trace for the error if available to pinpoint the location. If that's not available, do a global find for that string. If you find any matches, you will need to change them to Zend_Gdata_Calendar_EventQuery.
Update
Seems this is a known bug(s)
http://framework.zend.com/issues/browse/ZF-7013
http://framework.zend.com/issues/browse/ZF-11959
It's an issue when your code registers an error handler using ErrorException. Apparently it's fixed in the 1.12 branch but hasn't made it to a release yet.
There's a patch in the 11959 bug report that fixes the issue

PHP included class not updating if modified

I create a simple class from a different file and include it
to a page that used to create an object of that class.
Everything works fine, the problems occurs is when I update
the class, I need to manually access the class from my browser
then the page that I create the object will get the latest modified class
or it will receive Error.
below is the page code I use to create an object of the class
<?php
function __autoload($class_name) {
require '/classes/'.$class_name . '.php';
}
$rain = new myClass;
echo $rain->TestMethod(12345,123451);
?>
If I update my class, and without accessing it manually from my browser I will receive this error from my Apache2.
PHP Fatal error: require(): Failed opening required '/classes/myClass.php' (include_path='.:/usr/share/php:/usr/share/pear')
Check if you are using a PHP op cache, or any Apache PHP cache or something that makes your php files not been readead from disk when you change them.
I usually have many problems with shared servers because these modules.
(i see the / is the root folder. It's a common mistake if your classes are not in the root folder of your webserver.)

drupal undefined function static html with post to php

I have placed my own php file in /sites/all/modules/<myfolder> and call it via $.post(<myphppage>) from a static html page (javascript function) also in /sites/all/modules/<myfolder>.
However, the php does not appear to be executing as expected, but is getting called (access logs in Apache HTTPD show it is post'ed to).
If I try to manually request the same php page, I receive this error:
Fatal error: Call to undefined function db_insert() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x.
The echo statement I have in the php page is outputted properly above this error, and simply uses $_GET['paramname']. (And _POST, changed for testing direct request) to print a few query string parameters for debugging).
Also, when I added a call to watchdog, I receive:
Fatal error: Call to undefined function watchdog() in /full/path/to/sites/modules/<myfolder>/<myphppage> on line x.
Is it not possible to access the PHP page directly? Or is there a Drupal library I need to import? Or am I just plain missing something else with how Drupal works?
You are getting those errors about undefined functions because your PHP file is independent from Drupal and it is not bootstrapping it.
The Drupal way of doing what you are trying to do is to create a module (see Creating Drupal 7.x modules, and Creating Drupal 6.x modules) that defines a URL it handles with hook_menu(); that URL is then used with $.post().
As example of the way of bootstrapping Drupal, see the content of the index.php file that comes with every Drupal installation. (The following code is the one used from Drupal 7.)
/**
* Root directory of Drupal installation.
*/
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
The code changes from Drupal 6 to Drupal 7, but drupal_bootstrap() is defined in both Drupal versions.
The correct way is to create a module, anyway. For a list of reasons why a module should use a PHP file that bootstraps Drupal, see Are there cases where a third-party module would need to use its own file similar to xmlrp.php, cron.php, or authenticate.php?

Can no longer install WordPress on MAMP- "Fatal error: Cannot redeclare class wpdb"

I've been developing WordPress sites for a long time on my local MAMP server. Now all of a sudden, it won't let me create any new sites. Whenever I set up a new one, instead of running the install script I get:
Fatal error: Cannot redeclare class
wpdb in
/Applications/MAMP/htdocs/[my_site]/wp-includes/wp-db.php
on line 52
This happens with all WordPress versions. I cannot thing of anything that would have caused this. I've done everything short of re-install MAMP. Does anyone have any ideas? I'm desperate at this point..
Check out the include path for php. It's likely that a second instance of wordpress is on the include path and is therefore conflicting with the one you're trying to load. It's also possible that a different package on the include path has a class called wpdb and is therefore causing a conflict.
wpdb is being created again some where, if this happened all of a sudden i suggest that you disable any plugins you've recently added. Or even better do a global find for the term class wpdb and see if that appears within more than 1 file. Also, check your functions.php file for a loop that might be loading wp-db.php more than once.
A workaround fix is to wrap the wpdp class in wp-includes/wp-db.php in the following:
Line 52:
if(class_exists('wpdb') != true)
{
class wpdb {
...
}
}
This solved the install issue- You could probably remove it after that, although it can't hurt to leave it I guess.
Still don't understand why this problem has popped up- If someone has an explanation I'd be eager to hear it.

Categories