Getting digital signature Error "Error Encountered while BER decoding" using TCPDF - pdf

I use TCPDF to create digital signature on a PDF document.
I use almost the exact code mentioned here:
http://www.tcpdf.org/examples/example_052.phps
and I use the same crt file that I use for the SSL on my server when browsing to on my server (linux).
But no matter if the file exists or not, no matter which path it is or which server, I always get the same error
I get the error:
"Error Encountered while BER decoding"
My server is Centos 6.3(64bit), Apache/2.2.15 (CentOS) , PHP Version 5.3.3, MySQLi 5.1.67
My code is:
$pdf = new TCPDF();
// set document information
$pdf->SetCreator('DejaVuSansCondensed');
$pdf->SetAuthor('Soft1');
$pdf->SetTitle('Soft1 Verified');
$pdf->SetSubject('Soft1 Verified');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set font
$pdf->SetFont('DejaVuSansCondensed', '', 10);
// set certificate file
$certificatePath = 'file://home/tcpdf.crt';
// set additional information
$info = array
(
'Name' => 'Soft1',
'Location' => 'Server',
'Reason' => 'Signing Document',
'ContactInfo' => 'http://www.soft1.com',
);
// set document signature
$pdf->setSignature($certificatePath, $certificatePath, 'tcpdfdemo', '', 2, $info);
// add a page
$pdf->AddPage();
// set LTR direction for english translation
$pdf->setRTL(true);
// output the HTML content
$pdf->writeHTML($printable, true, 0, true, 0);
// create content for signature (image and/or text)
$pdf->Image('themes/default/images/digital_signature.png', 25, 11, 15, 15, 'PNG');
$pdf->setSignatureAppearance(25, 11, 15, 15);
if($task == 'pdf'){
//$GLOBALS['log']->fatal("file_name = " . $file_name);
//$file_name = "hello.pdf";
$pdf->Output($file_name, "D");
}
Can anyone tell me what is wrong?
Searched the web for this error. Almost nothing except old forum posts.

Had the same issue and solved it by replacing:
$certificatePath = 'file:///home/tcpdf.crt';
Note the extra '/' in the path

Related

html2PDF work on local , but wont create a pdf on server (blank page)

I'm coding since 2 year and learning cakePHP since a few weeks , and i'm actually in internship trying to deploy a website.
I have trouble with Html2PDF , as it work well on the local version, but not on the server (a new page is opened , with the right url , but the pdf is blank).
the newly opened blank page contain this error message (displayed on the f12 debug console) :
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
the Html2PDF code :
function exportFicheVie() {
$Equipement = $this->Equipement->getEquipement($_GET['Id']);
$PersonnesRessources = $this->Equipement->getPersonnesRessourcesOfEquipement($_GET['Id']);
$Prestataire = $this->Equipement->getPrestataireOfEquipement($_GET['Id']);
$Verifications = $this->Equipement->getVerif($_GET['Id']);
$Contrat = $this->Equipement->getContrat($_GET['Id']);
if (count($Contrat)==0)
$Contrat[0] = "Non";
$Vide = array(
"Prenom"=>"",
"Nom"=>"",
"NumPoste"=>"",
"Mail"=>"",
"Adresse"=>"",
"Fax"=>"",
"Telephone"=>""
);
if (count($PersonnesRessources)==0)
$PersonnesRessources = array($Vide, $Vide);
elseif (count($PersonnesRessources)==1)
$PersonnesRessources[] = $Vide;
if (count($Prestataire)==0)
$Prestataire = $Vide;
// convert in PDF
require_once(__DIR__.'/../webroot/theme/plugins/html2pdf/vendor/autoload.php');
try
{
$Html2pdf = new HTML2PDF('P', 'A4', 'fr');
$NbPages = $Html2pdf->pdf->getAliasNbPages();
$NumPage = $Html2pdf->pdf->getAliasNumPage();
include($Html2pdf->getHtmlFromPage(__DIR__.'/../View/Equipements/export_fiche_vie.ctp', $Equipement, $PersonnesRessources, $Prestataire, $NbPages, $NumPage));
$Content = ob_get_clean();
//$Html2pdf->setModeDebug();
$Html2pdf->addFont('Century Gothic', 'normal', __DIR__.'/../webroot/theme/plugins/html2pdf/vendor/tecnickcom/tcpdf/fonts/centurygothic.php');
$Html2pdf->addFont('Century Gothic Bold', 'normal', __DIR__.'/../webroot/theme/plugins/html2pdf/vendor/tecnickcom/tcpdf/fonts/centurygothic.php');
$Html2pdf->setDefaultFont('Century Gothic');
$Html2pdf->writeHTML($Content, isset($_GET['vuehtml']));
$Html2pdf->Output('\FS-'.$_GET['Id'].'.pdf');
}
catch(HTML2PDF_exception $e)
{
echo $e;
exit;
}
}
I don't expect an miracle answer , because this question appear a lot without any clear solution , but any hint, advice or supposition would be welcome .it's my first time putting a website on a real server they might be some obvious things I miss.

Add .docx, .pdf, .txt etc as attachment with PHPMailer

I need to make possible for people to send their documents, be it .docx, .pdf or whatever from their computers, using PHPMailer. Of every solution I found, none of them worked for me. The error Could not access file: keeps showing up when using $mailer->ErrorInfo.
This is the code I have:
$mailer->From = $mail1;
$mailer->FromName = $name1;
$mailer->addAddress("gmfernandes#neo-e.com.br");
$mailer->Subject = $name1;
$mailer->ContentType = "Content-type: text/html; charset=utf-8";
$mailer->msgHTML($template);
$mailer->addAttachment($_FILES['anexoTrabalho']['tmp_name'], $_FILES['anexoTrabalho']['name']);
Thank you in advance
You need to learn how to handle uploads correctly. Don't access $_FILES directly; use move_uploaded_file first. To save you the hassle of looking it all up, adapt the example provided with PHPMailer, the important bit of which I reproduce here:
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Upload handled successfully
// Now create a message
// This should be somewhere in your include_path
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('from#example.com', 'First Last');
$mail->addAddress('whoto#example.com', 'John Doe');
$mail->Subject = 'PHPMailer file sender';
$mail->msgHTML("My message body");
// Attach the uploaded file
$mail->addAttachment($uploadfile, 'My uploaded file');
if (!$mail->send()) {
$msg = "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg = "Message sent!";
}
} else {
$msg = 'Failed to move file to ' . $uploadfile;
}
}

How to store file and retrive it correctly with moodle file api?

i'm programming a mod activity for moodle which load files and show'em to any student who can access to the course.
The problem is that handing files in moodle is damn hard.
this is what i have done so far:
option page with importers
$mform->addElement('filepicker', 'slidesyncmedia', get_string('slidesyncmedia', 'slidesync'), null, array('maxbytes' => $maxbytes, 'accepted_types' => '*'));
$mform->addElement('filemanager', 'slidesyncslides', get_string('slidesyncslides', 'slidesync'), null, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50, 'accepted_types' => array('*') ));
after submit the files are stored in draft
and everything is loaded in another page that save all on db
if ($draftitemid = file_get_submitted_draft_itemid('slidesyncmedia')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_slidesync', 'slidesyncmedia', 0, array('subdirs' => 0, 'maxfiles' => 1));
}
if ($draftitemid = file_get_submitted_draft_itemid('slidesyncslides')) {
file_save_draft_area_files($draftitemid, $context->id, 'mod_slidesync', 'slidesyncslides', 0, array('subdirs' => 0, 'maxfiles' => 50));
}
in the end i use again the first page in another place (if files are there, then shows them)
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'mod_slidesync', 'slidesyncslides', '0', 'sortorder', false)) {
// Look through each file being managed
foreach ($files as $file) {
// Build the File URL. Long process! But extremely accurate.
$fileurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
echo $fileurl;
}
} else {
echo '<p>Please upload an image first</p>';
}
this make an url but if clicked moodle says that file does not exist
mysite.com/pluginfile.php/53/mod_slidesync/slidesyncslides/0/Koala.jpg
in the db the file are correctly saved!!!
53 mod_slidesync slidesyncslides 0 / Koala.jpg
what i'm missing?
thanks
A long time passed but I was working on a plugin and had the same problem.
I manage to solve it.
To provide the file you need to create the:
function MYPLUGIN_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array())
Here is the example function: https://docs.moodle.org/dev/File_API#Serving_files_to_users
Remember change the last call to send_file to send_stored_file in Moodle 2.3+

401 Error "oauth_problem=nonce_used" Adding Products To Magento w/ Rest API

Getting a 401 status with "oauth_problem=nonce_used" message return when attempting to add products to Magento using the rest api. Oddly, the products are still get imported but it's really throwing me off because I'm not getting the product id's back in which to update the stock info.
Magento install is brand new (crucialwebhost installer) 1.7.0.2 and the code I'm using is pretty much copied and pasted from magento site...
$callbackUrl = '****';
$temporaryCredentialsRequestUrl = "*****/oauth/initiate?oauth_callback=".urlencode($callbackUrl);
$adminAuthorizationUrl = '*****/admin/oauth_authorize';
$accessTokenRequestUrl = '*****/oauth/token';
$apiUrl = '*****/api/rest';
$consumerKey = '*****';
$consumerSecret = '******';
try
{
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if(!isset($_GET['oauth_token']) && !$_SESSION['state'])
{
$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$adminAuthorizationUrl.'?oauth_token='.$requestToken['oauth_token']);
exit;
} else if($_SESSION['state'] == 1)
{
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: '.$callbackUrl);
exit;
} else
{
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = "$apiUrl/products";
$productData = json_encode(array(
'type_id' => 'simple',
'attribute_set_id' => 4,
'sku' => $local_product['sku'],
'weight' => 1,
'status' => 1,
'visibility' => 4,
'name' => $local_product['name'],
'description' => $local_product['description'],
'short_description' => $local_product['description'],
'price' => $local_product['price'],
'tax_class_id' => 0,
));
$headers = array('Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
$respHeader = $oauthClient->getLastResponseHeaders();
}
} catch(OAuthException $e)
{
print_r($e);
}
}
session_destroy();
Exact error: {"messages":{"error":[{"code":401,"message":"oauth_problem=nonce_used"}]}}
In Mage_Api2_Model_Resource, about line 227, locate
$this->getResponse()->setHeader('Location', $newItemLocation);
and insert just after this:
$this->getResponse()->setHttpResponseCode(202);
Ref: Wikipedia "HTTP Location":
The HTTP Location header field is returned in responses from an HTTP
server under two circumstances:
To ask a web browser to load a different web page. In this
circumstance, the Location header should be sent with an HTTP status
code of 3xx.
To provide information about the location of a newly
created resource. In this circumstance, the Location header should
be sent with an HTTP status code of 201 or 202
I had exactly the same problem and spend weeks tracking down the problem. It seems to be a strange combination of Apache with PHP and Rewriting. In the end I created a clean installation and the problem was gone. I also tried to create a second installation where the problem could be observed but failed - the error appeared only in my production system, not in any of test installations...
I looked at this and from what I see in the code, it looks like OAuth register all your calls and if it find out that the exact same nonce was actually used with the exact same timestamp as some previous call, it will just discard it with this very specific oauth_problem=nonce_used error.
Code from app/code/core/Mage/Oauth/Model/Server.php
/**
* Validate nonce request data
*
* #param string $nonce Nonce string
* #param string|int $timestamp UNIX Timestamp
*/
protected function _validateNonce($nonce, $timestamp)
{
$timestamp = (int) $timestamp;
if ($timestamp <= 0 || $timestamp > (time() + self::TIME_DEVIATION)) {
$this->_throwException('', self::ERR_TIMESTAMP_REFUSED);
}
/** #var $nonceObj Mage_Oauth_Model_Nonce */
$nonceObj = Mage::getModel('oauth/nonce');
$nonceObj->load($nonce, 'nonce');
if ($nonceObj->getTimestamp() == $timestamp) {
$this->_throwException('', self::ERR_NONCE_USED);
}
$nonceObj->setNonce($nonce)
->setTimestamp($timestamp)
->save();
}
So I would say, when you do calls through Magento API in REST you should take extra care that each and every request you make have its own unique generated combinaison timestamp / nonce value.
Also see
oauth_nonce. A random value, uniquely generated by the application.
oauth_timestamp. A positive integer, expressed in the number of seconds since January 1, 1970 00:00:00 GMT.
And
nonce_used: The nonce-timestamp combination has already been used.
From this source : http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-oauth.html
I had the exact same problem and to solve it I looked at the mod_rewrite apache module and turned on logging for this module which is done by adding this to your apache httpd.conf file (this is for apache 2.4x , 2.2x needs to be done differently
<IfModule mod_rewrite.c>
LogLevel mod_rewrite.c:trace8
</IfModule>
The errors are then logged out to the apache standard error_log
When I looked at the rewrite here I could see that my post request was being rewritten twice, the first time it add the products to magento and the second time it failed to add the product again as the nonce was used, obviously.
I could see that the rewrite rule that was causing this in the .htaccess was the one
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
I check my configuration and I was indeed running fast cgi php and I checked this by checking the value of Server API from a php info script. I had spent so long trying to solve this that I knowing the root cause I simply changed PHP from CGI php to an apache module and hey preto my post request now is only rewritten once and returns that all elusive 200 response code.
Work Around:
Use SOAP API.
Reason for not using it before:
SOAP API didn't provide ability to at custom product attributes or product quantity increment fields.
Fix:
Add any field you want to the product using the SOAP api by first creating an array of objects for them like this (last 4 lines of code below repeated for each field added):
$additionalAttrs = array();
$per_item = new stdClass();
$per_item->key = 'price_per_item';
$per_item->value = $local_product['price'];
$additionalAttrs['single_data'][] = $per_item;
And then adding it to your product array with the key "additional_attributes" like:
'additional_attributes' => $additionalAttrs,
I know this work around only helps people that were avoiding the SOAP API for the same reason I was but hopefully it helps some of you. That error we're seeing where it tries to add a product twice seems to be server configuration specific and very hard to track down.

Yii + Zend gdata. Youtube upload

I want to upload videos with next way:
I just upload file to server (as usual)
My server-side Yii-application takes that video and uploads it on Youtube from a special account on youTube
What do i have:
My YouTube (google) account name and email. "name" or "name#gmail.com"
My password
A developer Key, which I found in Google's "Product Dashboard"
A name of the application, which names 'myapp':
Product Dashboard: myapp
So, I read some docs in google and decided that best way for me is to use ClientLogin auth type, because I have only one account to use and I have all necessary data. I found an example for ZendFramework's GData and I imported it into my Yii application.
I specially simplified the code just to upload one single video from /upload directory to test that it works. I expect to find a video in my YT account uploaded. Of course there is no video and here I am :-) Complete code of the action is below:
Yii::import('application.vendors.*');
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
$yt_user = 'myYTname';
$yt_pass = 'myYTpass';
$yt_source = 'myapp';
$yt_api_key = 'veryVERYlongKEYhere';
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = $yt_user,
$password = $yt_pass,
$service = 'youtube',
$client = null,
$source = $yt_source,
$loginToken = null,
$loginCaptcha = null,
$authenticationURL
);
$yt = new Zend_Gdata_YouTube($httpClient, $yt_source, null, $yt_api_key);
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$filesource = $yt->newMediaFileSource(Yii::getpathOfAlias('webroot').'/upload/videos/video.mp4');
$filesource->setContentType('video/mp4');
$filesource->setSlug('video.mp4');
$myVideoEntry->setMediaSource($filesource);
$myVideoEntry->setVideoTitle('My Test Movie');
$myVideoEntry->setVideoDescription('My Test Movie description');
$myVideoEntry->setVideoCategory('Autos');
$myVideoEntry->SetVideoTags('cars, funny');
$myVideoEntry->setVideoDeveloperTags(array('mydevtag', 'anotherdevtag'));
$uploadUrl = "http://uploads.gdata.youtube.com/feeds/api/users/{$yt_user}/uploads";
try {
$newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');
} catch (Zend_Gdata_App_HttpException $httpException) {
echo $httpException->getRawResponseBody();
} catch (Zend_Gdata_App_Exception $e) {
echo $e->getMessage();
}
As you can see, there is a lot of default code from the official example. But it doesn't work. Noone echo shows me information. But when I deleted try-catch, I got an error:
Zend_Gdata_App_HttpException
Read timed out after 10 seconds
So, this problem is solved by myself :)
First of all: don't try to upload from localhost!
Then in my case I got an error, that I didn't say my dev-key! So, if you got the same error, try to change this:
$newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');
by adding the 4th parameter - extra headers:
$yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry', array(
'X-GData-Key' => 'key=yourBIGbigBIGdeveloperKEYhere'
));
Good luck and have fun with youtube API!