'phpseclib 2.0 can not use Composer to create autoload
I work for a company that has our servers and computer locked down tighter than ... well imagine something really tight. This means I can not use composer or install it without mountains of paperwork. I am a developer, normally programming in JS and not php, but for this project I need to get CAC X509 information off smart cards. I have been at it for about 4 hours now trying to get the phpseclib to work. Mind you, I barely understand php. I am following the directions from github and the website, mainly I have downloaded and unzipped the files for 2.0 and placed them in my directory, I am following the examples from the website such as http://phpseclib.sourceforge.net/x509/2.0/examples.html#getpublickey . but when ever I do:
$x509 = new X509();
$x509->loadX509($_SERVER['SSL_CLIENT_CERT']);
echo $x509->getPublicKey();
I get the error: Fatal error: Class 'X509' not found in ...
I have also tried other things such as including the directory to the file like:
$x509 = new phpseclib\File\X509();
$x509->loadX509($_SERVER['SSL_CLIENT_CERT']);
echo $x509->getPublicKey();
and (new phpseclib_File_X509) get the error: Fatal error: Class 'phpseclib\File\X509' not found in ...
Please help! I have never had to do an autoload and I have a feeling this is the issue? Like the files aren't loading? I have also done both below at separate times as well to no avail.
include'phpseclib/File/X509.php';
use phpseclib\File\X509;
When I put the error log in X509 to check there I get that ASN1 not found as well.
EDIT: Also would help to know that my php version is 5.5.26
Solution 1:[1]
You could use the latest 1.0.x version, which doesn't require an autoloader at all (and is still maintained):
http://sourceforge.net/projects/phpseclib/files/phpseclib1.0.18.zip/download
Alternatively, you /can/ use the latest 2.0.x version if you supply your own autoloader. http://phpseclib.sourceforge.net/2.0.html demos this:
<?php
// autoload.php's content can be copy / pasted from https://github.com/composer/composer/blob/master/src/Composer/Autoload/ClassLoader.php
include 'autoload.php';
$loader = new \Composer\Autoload\ClassLoader();
$loader->addPsr4('phpseclib\\', __DIR__ . '/path/to/phpseclib2.0');
$loader->register();
use phpseclib\Crypt\RSA;
use phpseclib\Net\SSH2;
$key = new RSA();
$key->loadKey(file_get_contents('private-key.txt'));
// Domain can be an IP too
$ssh = new SSH2('www.domain.tld');
if (!$ssh->login('username', $key)) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
This approach lets you use 2.0.x without needing to do composer require phpseclib/phpseclib on the CLI.
Solution 2:[2]
You need to follow standard Composer project setup instructions, Larry. After that, you follow instructions on phpseclib with Composer by doing
composer require phpseclib/phpseclib:~2.0
from within root project directory. After that you will not be required to use include/require from examples on the Sourceforge site as those do not use autoload feature of Composer - you follow this example to make use of it.
Solution 3:[3]
Any Composer library can be used as a stand-alone application include, and can be obtained from any computer with Composer installed, then uploaded to another machine.
Linux shell example:
# Navigate to desired download location:
cd /var/www/html/
# Remove prior directory if it exists
sudo rm -rf vendor-downloads
# Make directory
mkdir -p vendor-downloads
# Navigate into directory
cd vendor-downloads
# Get the software library via Composer (version 3.0 or 2.0 if desired)
composer require phpseclib/phpseclib:~3.0
This will result in a folder structure as follows:
/var/www/html/vendor-downloads/
- composer.json
- composer.lock
- vendor/
-- autoload.php
-- composer
-- paragonie
-- phpseclib
The files composer.json
and composer.lock
can be ignored. The "vendor" directory can be changed to any other name if desired. In this example, say it's changed to the application name, "phpseclib" for clarity.
- phpseclib/
-- autoload.php
-- composer
-- paragonie
-- phpseclib
In any PHP application, use the Composer downloaded library as a stand-alone include as follows:
<?php
include_once('/path/to/phpseclib/autoload.php');
// Test
use phpseclib3\Net\SSH2;
$ssh = new SSH2('localhost', 22);
echo '<pre>'.print_r($ssh,true).'</pre>';
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | neubert |
Solution 2 | Daniel Protopopov |
Solution 3 | Eric P |