I am trying to implement the following openpgp-php library to encrypt my text. But when I'm trying to decrypt, it gives null!
https://github.com/singpolyma/openpgp-php
My code implementation
<?php
require_once 'vendor/autoload.php';
require_once 'lib/openpgp.php';
require_once 'lib/openpgp_crypt_rsa.php';
require_once 'lib/openpgp_crypt_symmetric.php';
$public_key_ascii = file_get_contents('mypubkey.pgp');
$key = OpenPGP_Message::parse($public_key_ascii);
$data = new OpenPGP_LiteralDataPacket('Hello world!');
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
echo '<pre>Encrypted object: <br/>';
var_dump($encrypted);
$decryptor = new OpenPGP_Crypt_RSA($key);
$decrypted = $decryptor->decrypt($encrypted);
echo '<br/>Decrepted: ';
var_dump($decrypted);
Now when I'm running the script I'm getting the following output:
Encrypted object:
object(OpenPGP_Message)#10 (2) {
["uri"]=>
NULL
["packets"]=>
array(1) {
[0]=>
object(OpenPGP_IntegrityProtectedDataPacket)#7 (4) {
["version"]=>
int(1)
["tag"]=>
int(18)
["size"]=>
NULL
["data"]=>
string(68) "u��0��Y
�W~�-rS}e�#�2DJ�p����l�k�FakΩ-���pk� #Y�ft�Nm�S�zt�"
}
}
}
Decrepted: NULL
Any suggestions will be appreciated. Thanks
Related
Hello fellow developers,
Im currently trying to read all emails with imap. Im trying to get the header infos with imap_headerinfo and the body with imap_fetchbody.
However, I get all the headerinfo but I only get an empty string as a return from the fetchbody function. I hope someone can help me
$this->inbox = imap_open($server, $user, $password);
// Header und Body der Email auslesen
$emails = imap_search($this->inbox,'ALL');
foreach($emails as $k) {
$this->email_contents[$k] = array(
'header' => imap_headerinfo($this->inbox, $k),
'body' => imap_fetchbody($this->inbox, $k, '1')
);
}
imap_close($this->inbox);
fetch_structure
object(stdClass)#65 (11) {
["type"]=>
int(1)
["encoding"]=>
int(0)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(5) "MIXED"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["ifdisposition"]=>
int(0)
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(1) {
[0]=>
object(stdClass)#66 (2) {
["attribute"]=>
string(8) "boundary"
["value"]=>
string(36) "------------CC4C1146391EA6C129642420"
}
}
["parts"]=>
array(2) {
[0]=>
object(stdClass)#67 (12) {
["type"]=>
int(0)
["encoding"]=>
int(1)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(5) "PLAIN"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["lines"]=>
int(30)
["bytes"]=>
int(590)
["ifdisposition"]=>
int(0)
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(2) {
[0]=>
object(stdClass)#68 (2) {
["attribute"]=>
string(7) "charset"
["value"]=>
string(5) "utf-8"
}
[1]=>
object(stdClass)#69 (2) {
["attribute"]=>
string(6) "format"
["value"]=>
string(6) "flowed"
}
}
}
[1]=>
object(stdClass)#70 (13) {
["type"]=>
int(5)
["encoding"]=>
int(3)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(4) "JPEG"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["bytes"]=>
int(1036)
["ifdisposition"]=>
int(1)
["disposition"]=>
string(10) "attachment"
["ifdparameters"]=>
int(1)
["dparameters"]=>
array(1) {
[0]=>
object(stdClass)#71 (2) {
["attribute"]=>
string(8) "filename"
["value"]=>
string(11) "logo_sm.jpg"
}
}
["ifparameters"]=>
int(1)
["parameters"]=>
array(1) {
[0]=>
object(stdClass)#72 (2) {
["attribute"]=>
string(4) "name"
["value"]=>
string(11) "logo_sm.jpg"
}
}
}
}
}
imap-fetchbody() works unusually when handling attached email messages and it's behaviour is inconsistent.
I was going to re-write this but to be honest the author atamido does a fantastic job of showing why this happens so i'll humbly cite his lead comment from this:: http://php.net/manual/en/function.imap-fetchbody.php he also includes an example of how to extract the body
imap-fetchbody() will decode attached email messages inline with the
rest of the email parts, however the way it works when handling
attached email messages is inconsistent with the main email message.
With an email message that only has a text body and does not have any mime attachments, imap-fetchbody() will return the following
for each requested part number:
(empty) - Entire message
0 - Message header
1 - Body text
With an email message that is a multi-part message in MIME format, and contains the message text in plain text and HTML, and has
a file.ext attachment, imap-fetchbody() will return something like the
following for each requested part number:
(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - file.ext
Now if you attach the above email to an email with the message text in plain text and HTML, imap_fetchbody() will use this type of
part number system:
(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - MESSAGE/RFC822 (entire attached message)
2.0 - Attached message header
2.1 - TEXT/PLAIN
2.2 - TEXT/HTML
2.3 - file.ext
Note that the file.ext is on the same level now as the plain text and
HTML, and that there is no way to access the MULTIPART/ALTERNATIVE in
the attached message.
Here is a modified version of some of the code from previous posts
that will build an easily accessible array that includes accessible
attached message parts and the message body if there aren't multipart
mimes. The $structure variable is the output of the
imap_fetchstructure() function. The returned $part_array has the
field 'part_number' which contains the part number to be fed directly
into the imap_fetchbody() function.
<?php
function create_part_array($structure, $prefix="") {
//print_r($structure);
if (sizeof($structure->parts) > 0) { // There some sub parts
foreach ($structure->parts as $count => $part) {
add_part_to_array($part, $prefix.($count+1), $part_array);
}
}else{ // Email does not have a seperate mime attachment for text
$part_array[] = array('part_number' => $prefix.'1', 'part_object' => $obj);
}
return $part_array;
}
// Sub function for create_part_array(). Only called by create_part_array() and itself.
function add_part_to_array($obj, $partno, & $part_array) {
$part_array[] = array('part_number' => $partno, 'part_object' => $obj);
if ($obj->type == 2) { // Check to see if the part is an attached email message, as in the RFC-822 type
//print_r($obj);
if (sizeof($obj->parts) > 0) { // Check to see if the email has parts
foreach ($obj->parts as $count => $part) {
// Iterate here again to compensate for the broken way that imap_fetchbody() handles attachments
if (sizeof($part->parts) > 0) {
foreach ($part->parts as $count2 => $part2) {
add_part_to_array($part2, $partno.".".($count2+1), $part_array);
}
}else{ // Attached email does not have a seperate mime attachment for text
$part_array[] = array('part_number' => $partno.'.'.($count+1), 'part_object' => $obj);
}
}
}else{ // Not sure if this is possible
$part_array[] = array('part_number' => $prefix.'.1', 'part_object' => $obj);
}
}else{ // If there are more sub-parts, expand them out.
if (sizeof($obj->parts) > 0) {
foreach ($obj->parts as $count => $p) {
add_part_to_array($p, $partno.".".($count+1), $part_array);
}
}
}
}
?>
I have XML string $input which is received from SOAP service
$xml = new SimpleXMLElement($input);
echo var_dump($xml). "<br />";
gives output:
object(SimpleXMLElement)#3 (2) {
["result"]=>
string(5) "False"
["error"]=>
string(13) "Login Failure"
How to get 'False' value or any in its place into an variable?
Just use:
$yourvar = $xml->result;
To get "False" (or the value of result in your Xml).
To get the value of result from
object(SimpleXMLElement)#3 (2) {
["result"]=>
string(5) "False"
["error"]=>
string(13) "Login Failure"
you can use:
$xmlObject = new SimpleXMLElement($input);
echo $xmlObject->result;
Output:
False
Learn more about SimpleXML with this example
I have problem with parsing email message with PHP IMAP. Problem is that I have message signed with pkcs#7 signature. Mail contains some text and 2 attachments first one is smime.p7s and second one is message.htm which is html attachment I would like to parse.
To be honest I have no idea how can I access content of this file.
$hostname = '{host}INBOX';
$username = 'name';
$password = 'pass';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'UNSEEN');
$msg = Array();
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
$structure = imap_fetchstructure ( $inbox,$email_number,FT_UID);
echo "<pre>";
var_dump($structure);
echo "</pre>";
break;
}
}
I get full structure and I can find there part:
object(stdClass)#16 (14) {
["type"]=>
int(0)
["encoding"]=>
int(4)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(4) "HTML"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["lines"]=>
int(123)
["bytes"]=>
int(4473)
["ifdisposition"]=>
int(1)
["disposition"]=>
string(10) "attachment"
["ifdparameters"]=>
int(1)
["dparameters"]=>
array(1) {
[0]=>
object(stdClass)#17 (2) {
["attribute"]=>
string(8) "filename"
["value"]=>
string(37) "message.htm"
}
}
["ifparameters"]=>
int(1)
["parameters"]=>
array(2) {
[0]=>
object(stdClass)#18 (2) {
["attribute"]=>
string(4) "name"
["value"]=>
string(37) "message.htm"
}
[1]=>
object(stdClass)#19 (2) {
["attribute"]=>
string(7) "charset"
["value"]=>
string(8) "us-ascii"
}
}
}
Can anyone give me a hint how can I access content of message.htm ?
Since the structure does not have any parts defined, then the message is "simple".
Try using:
$message = imap_fetchbody($inbox,$email_number,0);
This will fetch the "0th" part of the message, which should be the body.
Check out the docs here: http://www.php.net/manual/en/function.imap-fetchstructure.php
Here is my code:
$this->view->assign('mail', $mail);
$mg = new Mailgun($this->getMailgunAPIKey());
$domain = "sandbox1111.mailgun.org";
$res = $mg->sendMessage($domain, array('from' => 'bob#sandbox3445.mailgun.org',
'to' => 'mee#xxxxx.com',
'subject' => $mail->getSubject(),
'text' => $mail->getBody()));
var_dump( $res);
and here is what gets printed out by the var_dump:
object(stdClass)#228 (2) { ["http_response_body"]=> object(stdClass)#223 (2) { ["message"]=> string(18) "Queued. Thank you." ["id"]=> string(52) "<20131211155824.31559.48115#sandbox1111.mailgun.org>" } ["http_response_code"]=> int(200) }
I tried var_dump( json_decode($res)); but that prints out NULL. How do I access the ["http_response_code"] for instance?
ANSWER:
var_dump( $res);
echo __LINE__.'<br/><br/>';
var_dump( $res->http_response_body );
echo __LINE__.$res->http_response_code.'<br/><br/>';
echo $res->http_response_body->message.'<br/><br/>';
prints
object(stdClass)#228 (2) { ["http_response_body"]=> object(stdClass)#223 (2) { ["message"]=> string(18) "Queued. Thank you." ["id"]=> string(52) "<20131211161740.16663.18744#sandbox3445.mailgun.org>" } ["http_response_code"]=> int(200) } 150
object(stdClass)#223 (2) { ["message"]=> string(18) "Queued. Thank you." ["id"]=> string(52) "<20131211161740.16663.18744#sandbox3445.mailgun.org>" } 152200
Queued. Thank you.
Your variable is already json_decoded, with the second parameter to false.
json_decode() works with objects by default (default = false in this case), if you want an array, use the 2nd parameter true, see http://php.net/json_decode.
I prefere object notation.
How do I access the ["http_response_code"] for instance?
$res->http_response_body
echo $res->http_response_body->message
//prints "Queued. Thank you."
EDIT: I don't think the final output looks like JSON. If not, what is it?
I have this code:
$url = 'http://eligibility.sc.egov.usda.gov/eligibility/eligibilityservice?eligibilityType=Property&requestString=<?xml version="1.0"?><Eligibility xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/var/lib/tomcat5/webapps/eligibility/Eligibilitywsdl.xsd"><PropertyRequest StreetAddress1="'.$street.'" StreetAddress2="" StreetAddress3="" City="'.$city.'" State="'.$state.'" County="" Zip="'.$zip.'" Program="RBS"></PropertyRequest></Eligibility>';
$url_arr = explode( 'requestString=', $url );
$xml = simplexml_load_string( $url_arr[ 1 ] ); // requires allow_url_fopen to be on
$elg = (string)$xml->Property[Eligibility];
var_dump($xml);
$xml = simplexml_load_file($url); // requires allow_url_fopen to be on
$elg = json_encode((string)$xml->Property[Eligibility]);
var_dump($elg);
Which out puts this array:
object(SimpleXMLElement)#1 (1) {
["PropertyRequest"]=> object(SimpleXMLElement)#2 (1) {
["#attributes"]=> array(8) {
["StreetAddress1"]=> string(13) "7865 ILLINOIS"
["StreetAddress2"]=> string(0) ""
["StreetAddress3"]=> string(0) ""
["City"]=> string(10) "CASEYVILLE"
["State"]=> string(2) "IL"
["County"]=> string(0) ""
["Zip"]=> string(5) "62232"
["Program"]=> string(3) "RBS"
}
}
}
string(12) ""InEligible""
I just want to grab the string(12) ""InEligible"" portion. How would I put this into a variable?
From your code there $elg already holds this value, and it is a variable.
EDIT: I don't think the final output looks like JSON. If not, what is it?
string(12) ""InEligible""
That's JSON. A string in JSON is enclosed in quotes, no more, no less.