JWE and PHPseclib without composer - php

I need to use jwe in my code. I found a couple of jwe libraries (here and here) that also requires phpseclib to be installed. However, we are not allowed to install composer in our area of work.
How do i reference the jwe and phpseclib libraries without composer? Thanks.

You could use Composer's autoloader without using the full Composer. eg.
<?php
include 'autoload.php';
$loader = new \Composer\Autoload\ClassLoader();
$loader->addPsr4('phpseclib\\', __DIR__ . '/path/to/phpseclib2.0');
$loader->register();
// insert your code here
Where autoload.php is this:
https://raw.githubusercontent.com/composer/composer/master/src/Composer/Autoload/ClassLoader.php
So at that point instead of having to code review the whole of Composer you just code review that one file.
You could also use the auto-loader by PHP-FIG:
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
<?php
include('autoloader.php');
$loader = new \Example\Psr4AutoloaderClass;
$loader->register();
$loader->addNamespace('phpseclib', __DIR__.'/phpseclib');
That said, I do think your companies policies are silly. If you're not going to trust Composer than why would you trust any third-party PHP library? So it's causing you problems with phpseclib today. What other libraries might you want to use in the future that this policy will also cause you problems?

Related

HybridAuth 3.0 - fresh install - getting: Class 'Hybridauth\Hybridauth\Provider\Facebook' not found

all. I've installed HybridAuth 3.0 and went step-by-step through the Install instructions using composer. I've used the example code and am able to get an initial HybridAuth class instantiated. However, when I continue on to the example with using the Facebook provider class, I'm getting the error, "Class 'Hybridauth\Hybridauth\Provider\Facebook' not found". I'm also using this in a Drupal 7 environment but have other classes like this that autoload just fine so I'm hopeful that little extra detail has no bearing on the problem.
I tried this both with the composer autoloader and the basic autoloader included with the library (as described in the Installation instructions). They both have the same error when attempting to find the Facebook class which is down in the /src/Provider directory. It does this for other classes as well.
That said, using the Hybridauth\Hybridauth "unified interface" works fine and is my current workaround as it allows one to work with multiple providers at one time. But I'm wondering what I'm doing wrong to not be able to load a specific provider as shown in their Introduction documentation.
This works:
// Include Composer's autoloader
include 'vendor/autoload.php';
// Import Hybridauth's namespace
use Hybridauth\Hybridauth;
// Now we may proceed and instantiate Hybridauth's classes
$instance = new Hybridauth([ /* ... */ ]);
This gives an error:
// Include Composer's autoloader
include 'vendor/autoload.php';
// Import Hybridauth's namespace
use Hybridauth\Hybridauth;
$adapter = new Hybridauth\Provider\Facebook([ /* ... */ ]);

How Can I use two "use" keyword on same page php

I am using two libraries on the same page. One is for pdf generation and one is for sending emails. But, this giving me error 500 - llease advise .
After some debugging, I found that it's phpmailer mails not working because of dompdf:
require_once 'lib/dompdf/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/phpmailer/vendor/autoload.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct(){
parent::__construct();
}
}
By using alias:
use \PHPMailer\PHPMailer\{PHPMailer as mailerClass, Exception as mailerException}; // PHP 7+
I think the problem in your code is the duplicate autoloader. The best way is when you install both packages over composer and use the composer autoloader.
In both libs you can find an example how to install them over composer.
composer require dompdf/dompdf
composer require phpmailer/phpmailer
Then you have to include the autoloader for composer.
require 'vendor/autoload.php';
After that you can use the autoloader to load all packages. In the DomPDF you find good examples how to use.
https://github.com/dompdf/dompdf
500 errors are a bit hard to find. You should enable your error logs and check directly your logs. In the log a php error should be shown.

Sentry: How to configure sentry in php?

This is for Sentry (Open-source error tracking) users.
I have tried some code, but I don't get success. I hope you could look into this.
Thanks to everyone in advance.
I have download the SDK zip and upload it on servers. Well, I have read from some stuff for autoloader and raven_client but still, I don't find autoloder.php.
I am using sentry/sdk:2.0.3
require_once 'sentry-php-master/src/Sdk.php';
Sentry\init(['dsn' => '___DSN___' ]);
throw new Exception("My first Sentry error!");
I am expecting it works and I can trace the errors.
Why aren't you using composer ?
Composer solves a number of problems including dependency resolution for PHP packages, keeping all packages updated and another benefit of using composer is autoloading.
I would suggest the following approach:
composer require sentry/sdk:2.1.0 in your project's root directory.
require_once __DIR__ . '/vendor/autoload.php';
Connect the SDK to Sentry
Sentry\init(['dsn' =>
'https://6cxxx20aa1xxx5axx47xxxb8#sentry.io/18xxx47']);
Lastly, verify your setup by triggering a PHP exception by throwing one as you've already done with throw new Exception("My first Sentry error!");
Docs

Cannot redeclare Laravel's __() function when using oscarotero/gettext translation library

I have a set of translations in PHP array. Using Oscarotero's gettext library, I'm getting an error of:
"Cannot redeclare __() (previously declared in
D:\LocaleTesting\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:907)"
when the execution of the code $t->register()
$aTranslation = Translations::fromJsonFile(public_path() . '/locale/'.$sLocale.'/LC_MESSAGES/admin.json');
$oTranslator = new Translator();
$oTranslator->loadTranslations($aTranslation);
$oTranslator->register();
Also, I've search that this error only occur when your Laravel version is 5.4 and above. Any help will do. Thank you! Please Oscarotero/gettext's github for more information about the library.
It seems this is a know issue with the library:
https://github.com/oscarotero/Gettext/issues/180
One way around it is loading the translator functions before laravel helpers are loaded (solution from the issue above):
I'm using the package with a require before vendor/autoload.php on public/index.php and artisan command.
# public/index.php
require __DIR__.'/../vendor/gettext/gettext/src/translator_functions.php';
require __DIR__.'/../vendor/autoload.php';
# artisan
require __DIR__.'/vendor/gettext/gettext/src/translator_functions.php';
require __DIR__.'/vendor/autoload.php';

How do I load Zend Framework 2 database module only

Zend Framework 2 claims to have a "use-at-will" design that allows you to easily use any of its modules without committing to the full stack. I need a good database access layer, and from the docs and recommendations online I like the look of Zend\Db. I've placed the Zend/Db folder in my /lib directory, but I'm having trouble getting PHP to recognize the Zend\Db\Adapter\Adapter class. I keep getting a fatal error when I try to use it:
Fatal error: Class 'Zend\Db\Adapter\Adapter' not found in /home/username/public_html/test.php on line 6
I've tried setting ZF2_PATH in my .htaccess:
SetEnv ZF2_PATH /home/username/public_html/lib/Zend
I've tried setting the include path in my code:
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/lib' . PATH_SEPARATOR . get_include_path() );
I've tried explicitly loading and instantiating the Zend\Loader:
require_once 'lib/Zend/Loader/StandardAutoloader.php';
$zendLoader = new Zend\Loader\StandardAutoloader();
$zendLoader->register();
None of these have had any effect. I tried explicitly requiring Zend/Db/Adapter/Adapter.php, and that did fix the error I'm seeing, but then I just get the same error for one of its dependencies, so that's not a practical solution.
What am I doing wrong here? Is ZF2 just not really designed for this kind of modular usage, or am I missing something?
EDIT: I got this to work by writing my own autoload function:
function autoloader($class) {
$path = explode('\\', $class);
foreach ($path as $p) {
$cp .= DIRECTORY_SEPARATOR . $p;
}
include __DIR__ . '/lib/' . $cp . '.php';
}
spl_autoload_register(autoloader);
This sort of makes sense -- obviously if I'm using the db module without the rest of the framework, I can't expect the framework to do autoloading for me -- except I still don't understand why manually loading Zend\Loader didn't solve the problem. Isn't handling autoloading the point of Zend\Loader? Anyway, I have a workable solution for now, but if there's a better solution I'd love to hear it.
I strongly recommend you check out composer. It really simplifies managing dependencies between a ton of modern PHP libraries, as well as autoloading.
For instance, if you were starting you project, and you knew you wanted to pull in zend-db, you'd do something like this:
$ mkdir myproject
$ cd myproject
$ curl -s https://getcomposer.org/installer | php
$ ./composer.phar require zendframework/zend-db:2.1.1
That last line will cause composer to spring into action. It will create a directory called "vendor" where it keeps all the libraries it manages. It then will check out version 2.1.1 of zend-db in there, and set up an vendor/autoload.php which you'll require in your project.
Then you can test it out. Create myproject/index.php like so:
<?php
require_once "vendor/autoload.php";
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Pdo_Sqlite',
'database' => 'path/to/sqlite.db'
));
And it just works.
Later, if you decide you need Zend\Mail too, you just:
$ ./composer.phar require zendframework/zend-mail:2.1.1
and composer will install it, along with a couple of dependencies, and make sure it's available to the autoloader.
Composer is not ZF-specific, either. There's a whole ecosystem of code to explore. Packagist is a good place to start.

Categories