Why can't i import ciphersweet StringProvider - php

I installed ciphersweet on my server using composer but when i try to import the library i'm getting this error.
Fatal error: Uncaught Error: Class 'ParagonIE\CipherSweet\KeyProvider\StringProvider' not found in index.php.
Seems like the dependency didn't install correcty, i'm lost can you help please.
It's a php error.
here's my code :
use ParagonIE\CipherSweet\EncryptedRow;
use ParagonIE\CipherSweet\Transformation\AlphaCharactersOnly;
use ParagonIE\CipherSweet\Transformation\FirstCharacter;
use ParagonIE\CipherSweet\Transformation\Lowercase;
use ParagonIE\CipherSweet\Backend\FIPSCrypto;
use ParagonIE\CipherSweet\KeyProvider\StringProvider;
$provider = new StringProvider('a981d3894b5884f6965baea64a09bb5b4b59c10e857008fc814923cf2f2de558');
$engine = new CipherSweet($provider, new FIPSCrypto());
/** #var CipherSweet $engine */
$row = (new EncryptedRow($engine, 'contacts'))
->addTextField('first_name')
->addTextField('last_name')
->addFloatField('latitude')
->addFloatField('longitude');
// Notice the ->addRowTransform() method:
$row->addCompoundIndex(
$row->createCompoundIndex(
'contact_first_init_last_name',
['first_name', 'last_name'],
64, // 64 bits = 8 bytes
true
)
->addTransform('first_name', new AlphaCharactersOnly())
->addTransform('first_name', new Lowercase())
->addTransform('first_name', new FirstCharacter())
->addTransform('last_name', new AlphaCharactersOnly())
->addTransform('last_name', new Lowercase())
);
$prepared = $row->prepareRowForStorage([
'first_name' => 'Jane',
'last_name' => 'Doe',
'latitude' => 52.52,
'longitude' => -33.106,
'extraneous' => true
]);
var_dump($prepared);
?>

You need to load the vendor/autoload.php in order for the installed packages to work.
For example, add require_once __DIR__ . '/vendor/autoload.php'; to the top of your file.
This will make php aware of the namespaces in your packages.
You might need to change this if your files are not in the root directory of your application. For example, if your files are in app/ directory, those files need to use require_once __DIR__ . '/../vendor/autoload.php'
See https://getcomposer.org/doc/01-basic-usage.md#autoloading for more details.

Related

How to call a class from vendor folder when using a composer?

I am investigating jwt token examples from this link - https://github.com/firebase/php-jwt
So i run:
composer require firebase/php-jwt
And now i have new files and folders in my root directory of my site: vendor, composer.json and composer.lock. Folder "vendor" contents "firebase/php-jwt" folder.
So i tried to run an example script inside my root site's folder (test.php, for example) with this content:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
use Firebase\JWT\JWT;
$key = "example_key";
$payload = array(
"iss" => "http://example.org",
"aud" => "http://example.com",
"iat" => 1356999524,
"nbf" => 1357000000
);
/**
* IMPORTANT:
* You must specify supported algorithms for your application. See
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
* for a list of spec-compliant algorithms.
*/
$jwt = JWT::encode($payload, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));
print_r($decoded);
$decoded_array = (array) $decoded;
JWT::$leeway = 60; // $leeway in seconds
$decoded = JWT::decode($jwt, $key, array('HS256'));
?>
And when i run it, i see:
Fatal error: Uncaught Error: Class 'Firebase\JWT\JWT' not found in
/var/www/html/jwt/test.php:20 Stack trace: #0 {main} thrown in
/var/www/html/jwt/test.php on line 20
So how can i use a class Firebase\JWT\JWT right?
From composer's Autoloading page:
"For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work:"
require __DIR__ . '/vendor/autoload.php';

storageclient class can't be found?

I'm trying desperately to figure out how to create a simple audio transcription script (for longer audio files) via PHP (the only language I know). I'm getting the error Class 'Google\Cloud\Storage\StorageClient' not found
I'm using the gcloud console code editor and everything should be installed (unless there is a separate composer install just for cloud storage, although I haven't been able to find anything about it in the documentation if there is).
I also entered gcloud auth application-default print-access-token which printed out an access token, but I don't know what (if any) I'm supposed to do with that other than the "set GOOGLE_APPLICATION_CREDENTIALS" command that I copied and pasted into the console shell prompt.
Here's the php code:
<?php
namespace Google\Cloud\Samples\Speech;
require __DIR__ . '/vendor/autoload.php';
use Exception;
# [START speech_transcribe_async_gcs]
use Google\Cloud\Speech\SpeechClient;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\ExponentialBackoff;
$projectId = 'xxxx';
$speech = new SpeechClient([
'projectId' => $projectId,
'languageCode' => 'en-US',
]);
$filename = "20180925_184741_L.mp3";
# The audio file's encoding and sample rate
$options = [
'encoding' => 'LINEAR16',
'sampleRateHertz' => 16000,
'languageCode' => 'en-US',
'enableWordTimeOffsets' => false,
'enableAutomaticPunctuation' => true,
'model' => 'video',
];
function transcribe_async_gcs($bucketName, $objectName, $languageCode = 'en-US', $options = [])
{
// Create the speech client
$speech = new SpeechClient([
'languageCode' => $languageCode,
]);
// Fetch the storage object
$storage = new StorageClient();
$object = $storage->bucket($bucketName)->object($objectName);
// Create the asyncronous recognize operation
$operation = $speech->beginRecognizeOperation(
$object,
$options
);
// Wait for the operation to complete
$backoff = new ExponentialBackoff(10);
$backoff->execute(function () use ($operation) {
print('Waiting for operation to complete' . PHP_EOL);
$operation->reload();
if (!$operation->isComplete()) {
throw new Exception('Job has not yet completed', 500);
}
});
// Print the results
if ($operation->isComplete()) {
$results = $operation->results();
foreach ($results as $result) {
$alternative = $result->alternatives()[0];
printf('Transcript: %s' . PHP_EOL, $alternative['transcript']);
printf('Confidence: %s' . PHP_EOL, $alternative['confidence']);
}
}
}
# [END speech_transcribe_async_gcs]
transcribe_async_gcs("session_audio", $filename, "en-US", $options);
With apologies, PHP is not a language I'm proficient with but, I suspect you haven't (and must) install the client library for Cloud Storage so that your code may access it. This would explain its report that the Class is missing.
The PHP client library page includes two alternatives. One applies if you're using Composer, the second -- possibly what you want -- a direct download which you'll need to path correctly for your code.
Some time ago, I wrote a short blog post providing a simple example (using Cloud Storage) for each of Google's supported languages. Perhaps it will help you too.

unable to load the GetMyMessagesRequestType class when calling the namespace

I am unable to load the GetMyMessagesRequestType class from the ebay-sdk which is in the following namespace :
use DTS\eBaySDK\Trading\Services;
use DTS\ebaySDK\Trading\Types;
when i call the following:
use DTS\eBaySDK\Shopping\Services;
use DTS\ebaySDK\Shopping\Types;
the class in question which ebayTime loads perfectly :
$service = new Services\ShoppingService();
// Create the request object.
$request = new Types\GeteBayTimeRequestType();
// Send the request to the service operation.
$response = $service->geteBayTime($request);
// Output the result of calling the service operation.
printf("The official eBay time is: %s\n", $response->Timestamp->format('H:i (\G\M\T) \o\n l jS Y'));
this code works.
but the code i have changed now looks like this :
$service = new Services\TradingService([
'authToken'=> "",
'credentials' => [
'appId' => '',
'certId' => '',
'devId' => ''
],
'siteId'=>0
]);
$request = new Types\GetMyMessagesRequestType();
and i get the following error message :
Attempted to load class "GetMyMessagesRequestType" from namespace "DTS\ebaySDK\Trading\Types".
Did you forget a "use" statement for "DTS\eBaySDK\Trading\Types\GetMyMessagesRequestType"
any ideas this is one little thing i am unable to fix to complete the project?
When you changed the use statements to DTS\ebaySDK\Shopping\Types, the class you're trying to instantiate is looking in the wrong namespace: DTS\ebaySDK\Shopping\Types\GetMyMessagesRequestType does not exist.
Either change the offending line from:
$request = new Types\GetMyMessagesRequestType();
into (note the leading backslash):
$request = new \DTS\eBaySDK\Trading\Types\GetMyMessagesRequestType();
Or add a use statement to the correct namespace:
use DTS\eBaySDK\Trading\Types\GetMyMessagesRequestType;
$request = new GetMyMessagesRequestType();

TYPO3 6.2 ext_autoload with non-namespaced classes

I'm trying to create a new Extension on Extbase in TYPO3 6.2 and im failing at including an existing Class/Framework Module.
My ext_autoload.php (ofc located in my extension dir)
$extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('couponprinter');
return array(
'ZendPdf' => $extensionPath . '/Classes/Utility/Zend/Pdf.php',
);
I'm trying to load the class in my controller via
$pdf = $this->objectManager->create('ZendPdf');
But im gettin the error "Could not analyse class:ZendPdf maybe not loaded or no autoloader?"
The Zend class itself has tons of includes which I cant refactor all, so I need the autoloader. Here is a short snippet:
/** Internally used classes */
require_once 'Zend/Pdf/Element.php';
require_once 'Zend/Pdf/Element/Array.php';
require_once 'Zend/Pdf/Element/String/Binary.php';
require_once 'Zend/Pdf/Element/Boolean.php';
require_once 'Zend/Pdf/Element/Dictionary.php';
require_once 'Zend/Pdf/Element/Name.php';
require_once 'Zend/Pdf/Element/Null.php';
require_once 'Zend/Pdf/Element/Numeric.php';
require_once 'Zend/Pdf/Element/String.php';
class Zend_Pdf{
// code of the class
}
Since TYPO3 6.2 changed some old methods, I can't include anymore. Does anyone have a idea how I can load a not-namespaced class into a extbase extension?
You need to create a ext_autoload.php file and fill it with something like
<?php
$extensionClassesPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('news') . 'Classes/';
$default = array(
'tx_news_domain_model_dto_emconfiguration' => $extensionClassesPath . 'Domain/Model/Dto/EmConfiguration.php',
'tx_news_hooks_suggestreceiver' => $extensionClassesPath . 'Hooks/SuggestReceiver.php',
'tx_news_hooks_suggestreceivercall' => $extensionClassesPath . 'Hooks/SuggestReceiverCall.php',
'tx_news_utility_compatibility' => $extensionClassesPath . 'Utility/Compatibility.php',
'tx_news_utility_importjob' => $extensionClassesPath . 'Utility/ImportJob.php',
'tx_news_utility_emconfiguration' => $extensionClassesPath . 'Utility/EmConfiguration.php',
'tx_news_service_cacheservice' => $extensionClassesPath . 'Service/CacheService.php',
);
return $default;
?>
Found in the documentation at http://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Autoloading/Index.html
I guess it should be
$extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('couponprinter');
return array(
'zendpdf' => $extensionPath . '/Classes/Utility/Zend/Pdf.php',
);
The left side of the array (keys) must be lowercase.

Error using Slim framework and Twig template

I'm trying to make Slim work with Twig template system, this is part of my index.php
// Twig [Template]
require 'Extras/Views/Twig.php';
TwigView::$twigDirectory = __DIR__ . '/vendor/Twig/lib/Twig/';
//Slim
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim(array(
'view' => $twigView
));
And this is my structure
Extras
|_Views
|_Twig.php
Slim
templates
vendor
|_Twig
|_lib
|_Twig
index.php
I try several times with other configurations and searching buy I ALLWAYS get this error:
Fatal error: Class 'Slim\View' not found in C:\wamp\www\slim\Extras\Views\Twig.php on line 43
Can anyone help me here? All the examples I had found was using composer
Ok, I solve it.
This is the solution:
// Slim PHP
require "Slim/Slim.php";
\Slim\Slim::registerAutoloader();
// Twig
require "Twig/lib/Twig/Autoloader.php";
Twig_Autoloader::register();
// Start Slim.
/** #var $app Slim */
$app = new \Slim\Slim(array(
"view" => new \Slim\Extras\Views\Twig()
));
And this is my structure now.
Slim
|_Extras
|_Views
|_Twig.php
|_Slim
templates
Twig
|_lib
|_Twig
|_Autoloader.php
index.php
¡I hope this help someone else!
Now Slim-Extras is DEPRECATED, we must use Slim-Views (https://github.com/codeguy/Slim-Views):
require "Slim/Slim.php";
\Slim\Slim::registerAutoloader();
$slim = new \Slim\Slim( array(
'debug' => false,
'templates.path' => 'fooDirTemplates',
'view' => '\Slim\Views\Twig'
));
$twigView = $slim->view();
$twigView->parserOptions = array(
'debug' => false
);
$twigView->parserDirectory = 'Twig';
$twigView->parserExtensions = array(
'\Slim\Views\TwigExtension'
);
$slim->notFound( 'fooNotFoundFunction' );
$slim->error( 'fooErrorFunction' );
// SLIM routes...
$slim->run();
If someone is still running in to this issue.
The problem for me was that I had installed both slim/views AND slim/twig-view.
I uninstalled slim/views and it worked

Categories