incomprehension with a php regex [duplicate] - php

This question already has answers here:
Regular expression is being too greedy
(5 answers)
Non-greedy match is still too greedy
(2 answers)
Closed 3 years ago.
I have this text:
<< /T (D3K_indicatif[0]) /V () >> << /T (D3P_pays[0]) /V () >> << /T (D1H_homme[0]) /V / >> << /T (D3K_indicatif[0]) /V () >>
I would like to catch << /T (D3P_pays[0]) /V () >>
I did this regex
/<< \/T\s.{0,}\(D3P_pays\[0\]\) \/V \(\) >>/gm but this regex catch << /T (D3K_indicatif[0]) /V () >> << /T (D3P_pays[0]) /V () >>
I don't understand why I have << /T (D3K_indicatif[0]) /V () >> in my match.

Related

Unable to fill PDF fields when FDF contains Kids sections FPDF/FPDM

I can successfully fill PDF forms,but it looks to be breaking when there are Kids sections in them.
E.g. this is the file I'm working with:
$ pdftk test.pdf dump_data_fields | grep FieldName
FieldName: Text36.0
FieldName: Text36.1
FieldName: Text37.1
FieldName: Text37.0.0
FieldName: Text37.0.1.0
FieldName: Text37.0.1.1
FieldName: Text38
FieldName: Text39.0
...
FieldName: Text41.5.23.1.19.5
FieldName: Text41.5.23.1.19.6
FieldName: Text41.5.23.1.19.7
FieldName: Text41
$ pdftk test.pdf dump_data_fields | grep FieldName | wc -l
402
This works:
<?php
require_once 'fpdm-2.9.2/fpdm.php';
$fields = [
'Text38' => 'Joe'
];
$pdf = new FPDM('test.pdf', 'test.fdf', true);
$pdf->Load($fields);
$pdf->Merge();
$pdf->Output('F', 'test_out.pdf');
Output:
PDF parse
Starting to parse 14336 entries
PDF parse retrieved 1094 refs
Merge info:
26 Field entry values found for 1 field values to fill
Output
Write file test_out.pdf
This does not (same code as above, only different field name):
$fields = [
'Text36.0' => 'Joe'
];
FPDF-Merge Error: field Text36.0 not found
Snip from the FDF file:
/T (Text41)
>>
<<
/Kids [
<<
/V ()
/T (1)
>>
<<
/V ()
/T (0)
>>]
/T (Text36)
>>
<<
/Kids [
<<
/V ()
/T (1)
>>
<<
/Kids [
<<
/Kids [
<<
/V ()
/T (1)
>>
<<
/V ()
/T (0)
>>]
/T (1)
>>
<<
/V ()
/T (0)
>>]
/T (0)
>>]
/T (Text37)
>>
<<
/V ()
/T (Text38)
>>
<<
/Kids [
<<
/V ()
/T (19)
>>
<<
/V ()
/T (18)
>>
So I'm trying to figure out if I'm sending the wrong data in, or if this is a limitation of the FPDM library.

Trying to run a shell script from Qt with sudo rights

command line (works fine)
$ sudo chPermissions.sh
trying to do this from within a Qt program, using QProcess and have tried the following without success
code:
QString program = "/bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();
result: 0 (the script ran but without sudo rights & didn't work!)
code:
QString program = "sudo /bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();
result: 1
code:
QString program = "/usr/bin/sudo /bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();
result: 1
code:
QString program = "/bin/sh";
QStringList arguments;
arguments << "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();
result: 0 (the script ran! no sudo rights)
code:
QString program = "/bin/sh";
QStringList arguments;
arguments << sudo << "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();
result: 127 (command not found)
code:
QString program = "/bin/sh";
QStringList arguments;
arguments << /usr/bin/sudo << "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();
result: 2 (Misuse of shell builtins)
code:
QString program = "/bin/bash";
QStringList arguments;
arguments << "-c" << "\"/usr/bin/sudo /usr/bin/chPermissions.sh"\";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();
result: 127
code:
QString shellCommandLine = "/usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();
result: 0 (the script ran!)
code:
QString shellCommandLine = "sudo /usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();
result: 1
code:
QString shellCommandLine = "/usr/bin/sudo /usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();
result: 1
code:
QString shellCommandLine = "/usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();
result: 0 (script ran without sudo rights)
Anyone able to let me know the secret of how to do this? (or put me out of my misery if it's not possible)
TIA
Andy
Not solved as such.
I got round this by
using one of the options that actually runs the script (without su rights), and
granting permission in sudoers, to user, to run the command in the script without password prompt.
hope that makes sense.
not happy, but it works.

What makes Adobe Reader display the "Signed" panel?

I am signing PDF files server side with PHP and I want Adobe Reader to display this banner on my resulting PDF saying that the file has been successfuly signed :
I am using code from the TCPDF library to achieve this (I had to modify some code to fit my needs).
I based my work on these two documents from Adobe official documentation : doc1 and doc2.
What I do :
Add the signature tags to the PDF file :
// The ID of the last object of the PDF + 1
$sigobjid = preg_match_all("/([0-9]+) 0 obj/", $pdfdoc, $output_array);
$sigobjid = end($output_array[1]) + 1;
// Write the signature tags where needed
$index_to_write = strrpos($pdfdoc,"endobj") + 6;
$signature_tag = PHP_EOL . $sigobjid . ' 0 obj '. PHP_EOL . '<< /Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached '. TCPDF_STATIC::$byterange_string . ' /Contents<'.str_repeat('0', $tcpdf->get_signature_max_length()).'> >>' . PHP_EOL . 'endobj';
$pdfdoc = substr_replace($pdfdoc, $signature_tag, $index_to_write, 0);
Compute and replace the ByteRange
Compute the hash of the file like this :
$hash_result = hash('sha256', $pdfdoc);
Sign the resulting hash client side with forge.js : I use a PFX file that I parse and then create a PKCS7 using the data contained in the PFX.
Send the hash to server.
Add the signature to the PDF in the Content tag.
EDIT : Thanks to #mkl comment I also tried to reference my Sig object with the AcroForm with the following lines into my PDF file :
11 0 obj
<< /Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached /ByteRange[0 2846 14590 507] /Contents< ...
endobj
12 0 obj
<</AcroForm 11 0 R >>
endobj
It is not working either. How should I fill the AcroForm field ?
The resulting PDF is readable by Adobe Reader but the blue banner does not appear, why ?
Okay I solved it myself so for these who were wondering, you need those fields :
1 0 obj
<<
/Type /Catalog /AcroForm << /Fields [12 0 R 13 0 R] /NeedAppearances false /SigFlags 3 /DR << /Font << /F1 14 0 R >> >> /DA (/F1 0 Tf 0 g) /Q 0 >> /Perms << /DocMDP 11 0 R >>
>>
endobj
4 0 obj
<<
/Type /Page
...
endobj
11 0 obj
<< /Type /Sig /Filter /Adobe.PPKLite /SubFilter /adbe.pkcs7.detached /ByteRange[0 3153 14897 922] /Contents<...> /Reference [ << /Type /SigRef /DigestMethod /SHA256 /TransformMethod /DocMDP /TransformParams << /Type /TransformParams /P 2 /V /1.2 >> >> ] >>
endobj
12 0 obj
<< /Type /Annot /Subtype /Widget /Rect [510.236220 572.598661 552.755906 615.118346] /P 4 0 R /F 4 /FT /Sig /T (Signer Name) /Ff 0 >>
endobj
13 0 obj
<< /Type /Annot /Subtype /Widget /Rect [510.236220 572.598661 552.755906 615.118346] /P 4 0 R /F 4 /FT /Sig /T (Signer Name) /Ff 0 /V 11 0 R >>
endobj
14 0 obj
<</Type /Font /Subtype /Type1 /BaseFont /Helvetica /Name /F1 /Encoding /WinAnsiEncoding >>
endobj
You basically need :
1 Font object (obj 14 in this example)
1 Sig object (obj 11) containing the signature in the 'contents' field
1 Page object (obj 4)
2 Annot object (obj 12 and 13) referencing the first page object with the P flag and the Sig object with the V flag
1 AcroForm field inside your main Catalog (obj 1) referencing your 2 Annot object in the Fields array, referencing your font in the F1 field, and your signature in the DocMDP field.
EDIT : The DocMDP field is not mendatory and only one Annot is needed. No Font required either.

I want to convert C code block to equivalent PHP code

if (crc & 0x8000){
crc = crc << 1 ^ 0x1021;
}
Above code block is in C language.
what would be the equivalent php code.
You can use bitwise operators and inline hex numbers in PHP just fine. The equivalent should just be:
if ($crc & 0x8000){
$crc = $crc << 1 ^ 0x1021;
}

Swapping bytes of text in PHP

I basically need to port this piece of code to php
for (i = 0; i < 128/4; i++)
data32[i] = bswap_32(data32[i]);
But, there is no bswap function in php.
Would someone be kind enough to provide me with something that could solve the problem?
This should do it (untested):
function bswap_32($j)
{
return (($j & 255) << 24) | (($j & 0xff00) << 8) |
(($j & 0xff0000) >> 8) | (($j & 0xff000000) >> 24);
}
Or, if there is a sign extension problem, this should resolve it:
function bswap_32($j)
{
return (($j & 255) << 24) | (($j & 0xff00) << 8) |
(($j & 0xff0000) >> 8) | (255 & (($j & 0xff000000) >> 24));
}
It sounds like bswap_32 is swapping endianness of your 32-bit quantities.
I could just give you some code, but I'd prefer not to do people's work for them, so I'll explain the principle instead:
You can achieve that with bit-shifts and masks (so for instance, you need to mask out the 8 lowest bits, and shift them into the highest 8 bit positions of the result).
Shifting can be done with the << and >> operators. Masking can be done with the & operator. See the PHP manual page on operators for more details.

Categories