Azure: create SAS manually - azure

I'm trying to implement a Perl function for creating SAS tokens. Doing all according to documentation (https://learn.microsoft.com/en-us/rest/api/storageservices/create-service-sas) I always get error "Signature fields not well formed".
My testing resource is "https://ribdkstoragepoc.blob.core.windows.net/project-poc/foo/koteyka.jpg". Variables populated by my code:
signedpermissions = 'r'
signedexpiry = '2020-05-01T07:59:00Z'
canonicalizedresource = '/blob/ribdkstoragepoc/project-poc/foo/koteyka.jpg'
signedresource = 'b'
signedversion = '2018-03-28'
Other variables are just empty lines. I generate a string using the following pattern:
my $stringToSign =
$permissions . "\n" .
$start . "\n" .
$expiry . "\n" .
$canonicalizedResource . "\n" .
$identifier . "\n" .
$IP . "\n" .
$protocol . "\n" .
$version . "\n" .
$resource . "\n" .
$snapshotTime . "\n" .
$rscc . "\n" .
$rscd . "\n" .
$rsce . "\n" .
$rscl . "\n" .
$rsct;
The string to sign:
r
2020-05-01T07:59:00Z
/blob/ribdkstoragepoc/project-poc/foo/koteyka.jpg
2018-03-28
b
Calculationg of signature: my $sig = Digest::SHA::hmac_sha256_base64($stringToSign, $key);
At last the final URL looks like: https://ribdkstoragepoc.blob.core.windows.net/project-poc/foo/koteyka.jpg?sig=YcwWvOT2FtOZGbXQxMAoSxvA2HhRmMAUp%2B6WUY%2Bjbjg&sv=2018-03-28&se=2020-05-01T07%3A59%3A00Z&sr=b&sp=r
As I have already said that does not work. Does anyone have ideas what could be wrong?

Figured out what's wrong with your code. Basically you're using 2018-03-28 version of Storage REST API so you don't need to include $resource and $snapshotTime in your $stringToSign.
Furthermore, you will need to pad your signature with 1-4 = (Ref: https://github.com/smarx/waz-storage-perl/blob/master/WindowsAzure/Storage.pm#L42).
Based on these, here's the code:
use strict;
use warnings;
use Digest::SHA qw(hmac_sha256_base64);
use MIME::Base64;
my $permissions = 'r';
my $start = '';
my $expiry = '2020-01-31T00:00:00Z';
my $canonicalizedResource = '/blob/ribdkstoragepoc/project-poc/foo/koteyka.jpg';
my $identifier = '';
#my $resource = 'b';
my $IP = '';
my $version = '2018-03-28';
my $protocol = '';
#my $snapshotTime = '';
my $rscc = '';
my $rscd = '';
my $rsce = '';
my $rscl = '';
my $rsct = '';
my $stringToSign = $permissions . "\n" . $start . "\n" . $expiry . "\n" . $canonicalizedResource . "\n" . $identifier . "\n" . $IP . "\n" . $protocol . "\n" . $version . "\n" . $rscc . "\n" . $rscd . "\n" . $rsce . "\n" . $rscl . "\n" . $rsct;
print $stringToSign;
my $accountKey = 'your-base64-account-key';
my $sig = Digest::SHA::hmac_sha256_base64($stringToSign, decode_base64($accountKey));
$sig .= '=' x (4 - (length($sig) % 4));
print "\n---------------------\n";
print $sig;
print "\n";
Give this a try, it should work.

Related

phpmailer write debug inside log file

I tried to create a new log file if there is a phpmailer error. But the file is not created. I check my directory (777).
Do you have an idea ?
Thank you
if (DEBUG_EMAIL == 'true'){
$this->phpMail->SMTPDebug = $this->debug_level; //2
$this->phpMail->Debugoutput = function($str, $level) {
$filename = CLICSHOPPING::BASE_DIR . 'Work/Log/phpmail_error-' . date('Ymd') . '.log';
$data = date('Y-m-d H:i:s') . "\t" . "\t$level\t$str\n";
$flags = FILE_APPEND | LOCK_EX;
file_put_contents($filename, $data, $flags);
};
// $this->phpMail->SMTPDebug = SMTP::DEBUG_SERVER;
}
Double check all your values, don't assume they are what you think they are. For example what is in CLICSHOPPING::BASE_DIR? Also, it's usual for directory env vars to not have a trailing slash, so you may be missing one when you append Work/... to it. Try this:
$filename = CLICSHOPPING::BASE_DIR . '/Work/Log/phpmail_error-' . date('Ymd') . '.log';
$data = date('Y-m-d H:i:s') . "\t" . "\t$level\t$str\n";

Joomla 2.5 / Virtuemart 2.0 search custom fields

I am trying to solve this missing feature for virtuemart. The default joomla search or the virtuemart search modules do not look into custom fields of the products.
I did try to change the plugin for search as follows:
$wheres = array();
switch ($phrase) {
case 'exact':
$text = $db->Quote ('%' . $db->getEscaped ($text, TRUE) . '%', FALSE);
$wheres2 = array();
$wheres2[] = 'p.product_sku LIKE ' . $text;
$wheres2[] = 'a.product_name LIKE ' . $text;
$wheres2[] = 'a.product_s_desc LIKE ' . $text;
$wheres2[] = 'a.product_desc LIKE ' . $text;
$wheres2[] = 'b.category_name LIKE ' . $text;
$wheres2[] = '#__virtuemart_product_customfields.custom_value LIKE ' . $text;
$where = '(' . implode (') OR (', $wheres2) . ')';
break;
case 'all':
case 'any':
default:
$words = explode (' ', $text);
$wheres = array();
foreach ($words as $word) {
$word = $db->Quote ('%' . $db->getEscaped ($word, TRUE) . '%', FALSE);
$wheres2 = array();
$wheres2[] = 'p.product_sku LIKE ' . $word;
$wheres2[] = 'a.product_name LIKE ' . $word;
$wheres2[] = 'a.product_s_desc LIKE ' . $word;
$wheres2[] = 'a.product_desc LIKE ' . $word;
$wheres2[] = 'b.category_name LIKE ' . $word;
$wheres2[] = '#__virtuemart_product_customfields.custom_value LIKE ' . $word;
$wheres[] = implode (' OR ', $wheres2);
}
$where = '(' . implode (($phrase == 'all' ? ') AND (' : ') OR ('), $wheres) . ')';
break;
}
The #__virtuemart_product_customfields has a shortname pf i think too.
Any insight?

Strings with WPML and Wordpress

I have a really impossible task for me. I know how put string content in wordpress ( )
but i don't know how put in this code. I want delete call and show text string.
if (!is_page_template('splash.php')) {
echo '<!-- _________________________ Start Custom HTML _________________________ -->' .
'<div class="header_html">' . "\n" .
'<div class="header_html_outer">' . "\n" .
'<div class="header_html_inner">' . "\n";
if ($cmsms_option[CMSMS_SHORTNAME . '_header_custom_html']) {
echo stripslashes($cmsms_option[CMSMS_SHORTNAME . '_header_html']) . "\n";
}
echo '<div class="cl"></div>' .
'</div>' . "\n" .
'</div>' . "\n" .
'</div>' . "\n" .
'<!-- _________________________ Finish Custom HTML _________________________ -->';
}
i can change it with the nexxt code, but show in front : _e( "text i want translate", "comercialdelmotor");
if (!is_page_template('splash.php')) {
echo '<!-- _________________________ Start Custom HTML _________________________ -->' .
'<div class="header_html">' . "\n" .
'<div class="header_html_outer">' . "\n" .
'<div class="header_html_inner">' . "\n" . '_e( "text i want translate", "comercialdelmotor");';
echo '<div class="cl"></div>' .
'</div>' . "\n" .
'</div>' . "\n" .
'</div>' . "\n" .
'<!-- _________________________ Finish Custom HTML _________________________ -->';
}
I hope I understood your question correctly. You are correct in using _e() to echo a string. Your syntax use is wrong though. Never ever use " to enclose a translatable string, the translator does not recognise it, so your string is skipped/ignored.
The correct way is to use '. So your string should look something like this _e( 'string to be translated', 'domainname' ); Note that domainname is optional, so using _e( 'string to be translated' ); is also correct.
You can go and read more about translating your theme in this great tutorial. http://code.tutsplus.com/tutorials/translating-your-theme--wp-25014

Why does Perl string become undefined?

What is the proper way to concatenate several scalar values into one Perl string?
The following code is deliberately a series of statements for debugging reasons.
my $bill_record;
$bill_record = $acct_no . " |";
$bill_record = $bill_record . defined($w_ptWtrMtrRecRef->{"mtr_addr_no"}) ? $w_ptWtrMtrRecRef->{"mtr_addr_no"} : " " . " |" ;
$bill_record = $bill_record . defined($w_ptWtrMtrRecRef->{"mtr_addr_str"}) ? $w_ptWtrMtrRecRef->{"mtr_addr_str"} : " " . " |" ;
$bill_record = $bill_record . defined($w_ptWtrMtrRecRef->{"mtr_addr_apt"}) ? $w_ptWtrMtrRecRef->{"mtr_addr_apt"} : " " . " |" ;
$bill_record = $bill_record . $issue_date . " |";
The | character is serving as a delimiter. Each line will be '\n terminated.
After the last line $bill_record = $bill_record . $issue_date . " |";
This error appears:
Use of uninitialized value $bill_record in concatenation (.) or string at /home/ics/include/WsBillFunc.pm line 1022.
at /home/ics/include/WsBillFunc.pm line 1022
$issue_date is defined when assigned.
What could be causing $bill_record to become undefined, and what is the proper way to concatenate a bunch of scalar values into one string?
I don't know specifically why $bill_record is undefined. But if I understand what you're trying to do, you're running into a precedence problem: the ?: operator has lower precedence than concatenation ., so that
$bill_record = $bill_record . defined($w_ptWtrMtrRecRef->{"mtr_addr_no"}) ? $w_ptWtrMtrRecRef->{"mtr_addr_no"} : " " . " |" ;
is treated as
$bill_record = ($bill_record . defined($w_ptWtrMtrRecRef->{"mtr_addr_no"})) ? $w_ptWtrMtrRecRef->{"mtr_addr_no"} : (" " . " |") ;
which I suspect is not what you want. Try adding parentheses:
$bill_record = $bill_record . (defined($w_ptWtrMtrRecRef->{"mtr_addr_no"}) ? $w_ptWtrMtrRecRef->{"mtr_addr_no"} : " ") . " |" ;
(Or use .= as another commenter suggested.)
I'd probably do that in one statement with a join:
$bill_record = join ' |',
map( {
defined( $_ ) ? $_ : ' '
} #{ $w_ptWtrMtrRecRef }{ qw( mtr_addr_no mtr_addr_str mtr_addr_apt ) }
),
$issue_date,
'';
In the map I limit with parens because I only want to apply it to the hash slice. After that is the $issue_date and the empty string. That empty string gets the final | you have.
But, for your problem, it looks like you have a precedence problem. One way to see this is to ask Perl to compile then deparse your program to see what it thinks you wanted. The B::Deparse module does this and I use the -p argument to add extra parentheses.
Here's a cut down version of your original program with the added call to the deparser at the top (it's the B::Deparse module but the namespace is O:
#!/usr/bin/perl
use O qw(Deparse -p);
my $b;
$b = $acct_no . " |";
$b = $b . defined($w->{"no"}) ? $w->{"no"} : " " . " |" ;
$b = $b . defined($w->{"str"}) ? $w->{"str"} : " " . " |" ;
$b = $b . defined($w->{"apt"}) ? $w->{"apt"} : " " . " |" ;
$b = $b . $issue_date . " |";
It outputs:
my($b);
($b = ($acct_no . ' |'));
($b = (($b . defined($$w{'no'})) ? $$w{'no'} : ' |'));
($b = (($b . defined($$w{'str'})) ? $$w{'str'} : ' |'));
($b = (($b . defined($$w{'apt'})) ? $$w{'apt'} : ' |'));
($b = (($b . $issue_date) . ' |'));
The key part is the (($b . defined($$w{'no'})). The current value of $b is concatenated with the return value of defined, then the conditional operator (? :) is done. If the test value is true, it returns the first value in the conditional.
When it gets to mgr_apt_no, there are probably many records that don't have that value set. However, the combined value of the previous $b and $$w{'apt'} is defined because $b is not empty. Thus, it chooses the value of $$w{'apt'} to assign to $b. When it does the last line, $b is empty for the concatenation with $issue_date.

Cakephp- Date Conversion while reading from excel sheet

Using Cakephp, I am reading from an Excel sheet (I'm using Libre Office) and the date is not converted properly.
I'm using PHP spreadsheet excel reader to import the excel sheet into the database.
I'm not using any functions to convert the date given in the excel sheet.
In the excel sheet the date which I have given like this 04/10/2013 is converted to this format (AprApr/WedWed/2013201320132013). I want the exact date which I have given in the excel sheet.
I'm adding my code also here:
$excel = new Spreadsheet_Excel_Reader(WWW_ROOT . 'files/excel/' . $this->request->data['Request']['file_name'], true);
$excel->setUTFEncoder('iconv');
$excel->setOutputEncoding('UTF-8');
$excel->read(WWW_ROOT . 'files/excel/' . $this->request->data['Request']['file_name']);
$x = 2;
$sep = ",";
ob_start();
while ($x <= $excel->sheets[0]['numRows']) {
$y = 1;
$row = "";
while ($y <= $excel->sheets[0]['numCols']) {
echo $excel->sheets[0]['cells'][$x][4];
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
$row.=($row == "") ? "\"" . $cell . "\"" : "" . $sep . "\"" . $cell . "\"";
$y++;
}
echo $row . "\n";
$x++;
}
$fp = fopen(WWW_ROOT . "files/excel/data.csv", 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_clean();
$filehandle = fopen(WWW_ROOT . "files/excel/data.csv", "r");
fgetcsv($filehandle, 1000, ",");
while (($data = fgetcsv($filehandle, ",")) !== FALSE) {
pr($data);
}
Please help!!! Thanks in advance

Resources