PHP PEAR Validate Package - Fatal error: Class 'Validate' not found - php

This is the error I am receiving:
Fatal error: Class 'Validate' not found in C:\xampp\htdocs\final_project\validate.php on line 5
And here is my PHP code:
<?php
require_once 'Validate.php';
foreach($_POST as $name => $value)
{
$valid = Validate::string($value);
}
?>
I do not understand what I am missing. I installed --alldeps for the validate package, and the PEAR include path is also correct. Validate_CA is not giving me any errors, but it is not properly validating either.

PHP parses the include_path in order of precedence. This means that when a relative path is passed to require(), include(), fopen(), file(), readfile() or file_get_contents(), PHP will start looking in the first directory. If the file is found, it includes it. If not, it will continue to the next and repeats the process.
Consider the following include path:
include_path = ".:/php/includes:/php/pear"
and the following PHP script:
<?php
require('MyFile.php');
PHP will look for MyFile.php in the following order:
./MyFile.php (Current Directory)
/php/includes/MyFile.php
/php/pear/MyFile.php
The reason why you cannot load Validate.php is you already have a file called validate.php (remember, paths are not case-sensitive on Windows, but are on UNIX) in your current directory. Therefore, PHP includes your file instead of the file corresponding to PEAR::Validate since yours is found before PEAR's in the include_path order of precedence.
Simply renaming your file to something else than validate.php should fix your problem. If it still doesn't work, try echoing the return value of get_include_path() to make sure it really is set right.

Related

PHP Considers my absolute path a relative one

Currently I have PHP 7.2 installed and I was trying to get spatie/image-optimizer to work but somehow it considers my literal paths as non existent ones. If I give it the following path:
/data/www/MY DOMAIN/images/thumbnails/700/615/detailed/1/83-221-343-V01.jpg
It will output the following:
2018/07/13 14:44:26 [error] 18931#18931: *3749 FastCGI sent in stderr:
"PHP message: PHP Fatal error: Uncaught InvalidArgumentException:
/data/www/MY DOMAIN/images/thumbnails/700/615/detailed/1/83-221-343-V01.jpg
does not exist in
/data/www/MY DOMAIN/app/addons/theme/lib/vendor/spatie/image-optimizer/src/Image.php:14
But if I check the directory I can confirm that the file is there with the correct permissions.
Any ideas?
I just looked at the Image class on Github. Here's where the exception is thrown.
public function __construct(string $pathToImage)
{
if (! file_exists($pathToImage)) {
throw new InvalidArgumentException("`{$pathToImage}` does not exist");
}
$this->pathToImage = $pathToImage;
}
A good way of checking you have it properly set is to use realpath()
Realpath creates absolute paths from relative ones, returning false if the path doesn't exist.
$path = __DIR__ .'/../../something'; // (imagine __DIR__ is /some/random/path to begin with)
echo $path; // outputs /some/something
You can also check in your terminal simply by typing:
ls /path/to/image.jpg
If it lists, it does indeed exist and shouldn't throw an error.
Switch to the user your php is running as and try to access the file. It could be that you are missing the execute permissions on one of the folders on the way to the file.

PEAR module (HTTP_Request2) installed, but not recognized in php file?

I have HTTP_Request2 installed on my server; pear list shows it in the list of installed packages. But the following php file:
<?php
ini_set("include_path", '/path/to/php:' . ini_get("include_path"));
if (class_exists('HTTP_Request2')) {
print("true");
} else {
print("false");
}
...returns false. I've also tried replacing the ini_set line with
include '/path/to/php/HTTP/Request2.php';
...but I get the same result. Is there something I'm missing, or something else I can check?
Simply setting the include path doesn't implicitly give you access to the code. You need to either set the include path and then use relative includes:
ini_set("include_path", ...);
require_once 'HTTP/Request2.php';
Or just use fully qualified includes:
require_once '/path/to/HTTP/Request2.php';
That said, HTTP_Request2 is kinda old and I'd instead recommend using something like Guzzle via composer.

PHPSECLIB call to undefined function crypt_random_string()

I'm trying to upload a file to sftp server using phpseclib library. Here's the code I've written
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib0.3.5');
$fileName = "sImg3.jpeg";
$fileData = file_get_contents($fileName);
$b64Data = base64_encode($fileData);
// upload the file to ftp server
include('Net/SFTP.php');
include_once('include/config.php'); // contains sftp connection details
$sftp = new Net_SFTP($ftpHost);
if (!$sftp->login($ftpUser, $ftpPasswd))
{
die("Failed to connect to SFTP server");
}
else
{
$sftp->put($filename,$b64Data);
echo "Uploaded the file (".$filename.")\n";
}
When I try to run this, I'm getting:
PHP Fatal error: Call to undefined function crypt_random_string() in
/var/www/phpseclib0.3.5/Net/SSH2.php on line 1014
I've installed php5-mcrypt and phpseclib. Do I need to install any other dependencies for phpseclib? Please let me know.
Thanks much in advance,
Regards,
Sudheer
crypt_string_random is defined in Crypt/Random.php:
https://github.com/phpseclib/phpseclib/blob/0.3.5/phpseclib/Crypt/Random.php
But then again Net_SSH2 auto-includes Crypt/Random.php:
https://github.com/phpseclib/phpseclib/blob/0.3.5/phpseclib/Net/SSH2.php#L749
And Net_SFTP extends Net_SSH2:
https://github.com/phpseclib/phpseclib/blob/0.3.5/phpseclib/Net/SFTP.php#L253
And Net_SSH2 does a require_once so if anything you should be getting some sort of "file not found" error, if you're getting an error at all, not a "function not defined" error..
The problem is caused by how PHP handles nested include statements and relative paths. If you look at the current phpseclib programming, at least 'some' of the includes still use include() instead of require_once() which silently fail - which is why you don't see the errors about failing includes.
It all comes down to how PHP resolves relative pathing of nested includes - using the starting location of the parent script or the starting location of the current/child script. More info on the conundrum of PHP nested includes with relative pathing here:
php nested include behavior
When I ran into this problem myself, I worked around it by promoting /phpseclib out of it's own subfolder and into my existing /includes folder. Then I only ever include phpseclib files from within php scripts in my /includes folder so that the relative paths in phpseclib's nested include statements will work.

Mounting a folder in a phar doesn't work

I've created a phar of a Symfony2 web application, but I'm having some troubles with the cache-folders.
I found out that I could mount an external file/folder into a phar. That would fix my issue, but I can't get the example on the PHP site working.
I have a phar which contains an index.php:
<?php
$configuration = simplexml_load_string(file_get_contents(
Phar::running(false) . '/config.xml'));
?>
Then I include the .phar with the following code:
<?php
// first set up the association between the abstract config.xml
// and the actual one on disk
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
// now run the application
include 'phar-archive.phar';
?>
All files exists, but I get the following error:
PHP Fatal error: Uncaught exception 'PharException' with message 'config.xml is not a phar archive, cannot mount' in /var/www/$projectname/index.php:3
I already tried relative/absolute paths, changing permissions but can't get this to work. Additionally a working example of how I could mount a folder into a phar would be great !
First check that variable $projectname exist in this place (just run echo $projectname; before Phar::mount). If all ok, then change
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
to
Phar::mount('phar://config.xml', "/var/www/{$projectname}/config.xml");
It seems that variable $projectname not converted to its value because you used single quotes.

XMLdiff for php installation

I am trying to install xml diff ; https://github.com/mmacia/XMLdiff and i have not managed yet to make it work.Whenever i run any test example,i get
Fatal error: Interface 'PHPUnit_Framework_Test' not found in
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php on line 85
Has anyone managed to install and use the library.I am using xampp on windows.
I believe your problem has to do with PHPUnit's Autoloader.php not being included. This file sets the php spl_autoloadspl_register function which is responsible for loading in interfaces and classes like PHPUnit_Framework_Test.
According to this SO question, you have to include the autoloader file manually. Without knowing more about your set-up and how that particular library works, I would say do something like this in the appropriate file(s):
// define phpunit path
if ( ! defined('PHPUNIT_PATH')) {
// define an absolute path to your PHPUnit dir
// CHECK THIS, i'm not good with php on windows:
define('PHPUNIT_PATH','C:\xampp\php\PEAR\PHPUnit');
}
// Then include the autoloader like this:
include PHPUNIT_PATH.'Autoloader.php';
I hope this helps you or anyone else out.
Check execution flags for C:\xampp\php\PEAR\PHPUnit\Framework\Framework\Test.php
The file needs to be executable by the user who is launching tests (probably you).

Categories