How to update Apple Wallet pass using push notification in PHP - laravel-5

I was making a pass in php with expiredate parameter.I want to update pass using Apple push notification. According to the Passbook docs you need to use the Apple Push Notification Service to trigger a pull from the iOS device in order to update the Passbook.

This is my PHP codes to push notification to APNS. You can refer.
$apnsHost = 'gateway.push.apple.com';
$apnsPort = 2195;
$apnsCert = base_path('certificates.pem');
$push_token = 'device token';
$passIdentify = 'pass indentify';
$payload = '{}';
$msg = chr(0) . pack('n', 32) . pack('H*', $push_token) . pack('n', strlen($payload)) . $payload . pack('n', strlen($passIdentify)) . $passIdentify;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
fwrite($apns, $msg);
#socket_close($apns);
fclose($apns);
The certificates.pem is the same certificate you use to sign your pass in .p12 extension. So you need to export it to .pem by using the following codes
$ cd ~/Desktop
$ openssl pkcs12 -in WenderCastPush.p12 -out WenderCastPush.pem -nodes -clcerts
According to this tutorial https://www.raywenderlich.com/123862/push-notifications-tutorial#comments.

Related

Storage/File delete methods don't delete the file from storage

I am uploading images to storage/uploads and have a queue job that uploads those images to AWS. Here is the code of the job:
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
if (Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'))) {
File::delete($path);
}
Once the image has been uploaded to AWS the job is supposed to delete the image from storage/uploads but it's not doing that. The images are successfully uploaded to AWS. I tried to delete specific files directly without the if-statement but nothing seems to work. I even tried with Storage::delete but that didn't work either. Could someone point me in the right direction please?
You may use
Storage::delete('upload/profile/' . $fileName);
See Documentation
If your specifying the full directory path of the file you can use unlink a native way to delete files from the server.
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
$isUploaded = Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'));
if ($isUploaded) {
unlink($path);
}
Documentation: unlink php

Laravel 5.7 + Intervention Image : Image source not readable

I've create an app that upload image along with title, description & etc. However, i'm having a problem in some of the images to upload, it returns an error ("Image source not readable") as shown below:
Here's my Code:
$image = $request->file('image');
// $image = Input::file('image'); // already tried this one still same problem
$orginal_filename = $image->getClientOriginalName();
$ext = $image->getClientOriginalExtension();
$fileName = md5(microtime() . $orginal_filename) . '.' . $ext;
$img = Image::make($image->getRealPath());
$img->stream();
$img->resize(1200, null, function ($constraint) {
$constraint->aspectRatio();
});
Storage::disk('storage_dir')->put($dir . $fileName, $img, 'public');
Already tried following solutions:
Change to Input::file('file')
Check if Request Content-Type has multipart/form-data (Request already has multipart/form-data Content-Type)
Change Intervention Image driver from "gd" to "imagick"
but still have the "Image source not readable" error.
Note: Error only occurs in some images. (I've also tried moving the image(w/c produced the errors) into another directory but still error occurs).
Thank you so much for the help!
You can try running my code
if ($request->file('photo')->isValid()) {
$avatar = $request->file('photo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) );
}
/uploads/avatars/ is my directory
If the problem occours in a Laravel 5.7 project, and you store your pictures in the storage folder, it might solve the problem to enter this into the terminal:
php artisan storage:link
(The problem occurs if you have cloned the project from github og bitbucket)
Sorry for bothering guys! It seemed that it was all my fault not realizing php post_max_size and php upload_max_file_size. Since i was trying to upload an image larger than 8MB i only increased the post_max_size > than the current image file size, but not the upload_max_file_size coz i only increased it by 2 (stated: 4MB).
Thanks btw for the help and suggestions!
Replace
$resize = Image::make('storage/app/public/'.$user->image)->resize(300,300);
with
$resize = Image::make(storage_path('app/public/'.$user->image))->resize(300,300);
Give storage full path will solve the problem.
If you got this error on your server, you need to be sure you pushed the all images to server. You are facing this error because of server could not find or read to image/images.
-> Be sure image/images uploaded
-> Make readable to image/images.

Ruby HMAC signing issue

I got an issue with HMAC.
I have to sign a form before sending it to a bank.
They only provide an example in PHP in their documentation.
I have a hex key to sign my data (e.g. FCEBA61A884A938E7E7FE4F5C68AA7F4A349768EE5957DDFBE99C1D05A09CBACF1FCF0A7084CB2E4CBA95193176C4395DE7F39EA9DBEBEF0907D77192AAE3E8A).
In the PHP exemple, they do this with the key before signing the data:
$key = "FCEBA61A884A938E7E7FE4F5C68AA7F4A349768EE5957DDFBE99C1D05A09CBACF1FCF0A7084CB2E4CBA95193176C4395DE7F39EA9DBEBEF0907D77192AAE3E8A";
$message = "param1=a&param2=b";
$binKey = pack('H*', $key);
$signature = hash_hmac('sha512', $msg, $binKey);
echo $signature;
// => a3efb70368bee502ea57a1a4708cac8912a5172075ea8dec2de2770dfbb4c8fb587f03fdadc0ca4f9e1bb024cfda12866295b259f5fb4df2fe14d960874a68ab
I don't understand why they pack the key and if I should do something similar with my key.
I did the following in my Ruby code:
key = "FCEBA61A884A938E7E7FE4F5C68AA7F4A349768EE5957DDFBE99C1D05A09CBACF1FCF0A7084CB2E4CBA95193176C4395DE7F39EA9DBEBEF0907D77192AAE3E8A"
message = "param1=a&param2=b"
digest = OpenSSL::Digest.new('sha512')
signature = OpenSSL::HMAC.hexdigest(digest, key, message)
puts signature
# => d817611845246640d1224a0874bf60fed0956a367aa3069b7947cbec56903bb5d8c54df170f5504c586dad55e4f879c70cf1a40526cfc9f35411195822c535ed
You need to do this in Ruby:
hash = OpenSSL::HMAC.hexdigest(digest, [key].pack('H*'), message)
The real issue here is that your PHP code uses two variable names for the message. You set $message, then use $msg, which means you're computing the hash for an undefined variable.
The packing of the hex representation of the key back into a binary form is the bit you're missing.
See this post for example: https://blog.bigbinary.com/2011/07/20/ruby-pack-unpack.html
You'll want something like this:
signature = OpenSSL::HMAC.hexdigest(digest, key.pack('H'), message)
I'm using this in my project:
bin_key = Array(keyTest).pack 'H*'
#hmac = OpenSSL::HMAC.hexdigest("SHA512", bin_key, msg).upcase
This works fine for me.

RFC 2822, 3.6.2 error when sending email using Laravel

I'm sending an email using Laravel:
$userAndEmail = '"' . $user->name() . '" <' . $user->email . '>';
Mail::to($userAndEmail)->send(new UserCreated($user, $userUnencryptedPassword));
This is returning the following error message:
Address in mailbox given ["Bob asdf" <rava#gmail.com>] does not comply with RFC 2822, 3.6.2.
This works if $userAndEmail is just an email address, such as rava#gmail.com. Any tips on what might be wrong?
Thanks

notifications push ios: Connection reset by peer

I’m trying to do a notifications system for apple devices, but I’m getting the following errors when I try to run it on the server:
Warning: stream_socket_client(): SSL: Connection reset by peer in
/home/empresa/public_html/simplepush/push.php on line 30
Warning: stream_socket_client(): Failed to enable crypto in
/home/empresa /public_html/push/push.php on line 30
Warning: stream_socket_client(): unable to connect to
ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in
/home/empresa /public_html/push/push.php on line 30 Failed to connect:
0
My code is this:
<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
$deviceToken= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$passphrase = ' ';
$message = 'my first notification';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
What could be happening? Thanks.
I can't be sure of a particular reason
But please make sure, you are not doing any of the below things wrong:
Don't make many connections in parallel. Either reuse the same
connection or close the connection after delivering Push
Notifications. Actually, servers have a limit for maximum number of
parallel connections, which might leave you in trouble, once you
reach threshold. Also Apple suggests leave a connection open unless
you know it will be idle.
Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave a connection open unless you know it will be idle for an extended period of time—for example, if you only send notifications to your users once a day it is ok to use a new connection each day.
Don't send out developer profile tokens to LIVE APNS. Keep
distribution and development app tokens separate. It could result in
error, if you try to send sandbox tokens to LIVE APNS or vice versa.
SOURCE - APNS - notifications push ios: Connection reset by peer PHP

Resources