PHP-Script deletes lines in .dat file - php

I'm struggling with the following problem since 9 hours and can't find the solution. I'm using a very old script from here (http://www.teralaser.net/invitephp/) to build an invitation-script. I already did all the styling within the past week and it looks great, now.
the script stores the single datasets in a .dat file. Now I realized, that it always keeps just the first 125 lines of the database and deletes the rest. That's a hug he problem, as it is for an invitation with nearly 1000 persons.
After some hours I found out (better I guess), that it's the following line, which destroys it:
case "adminadd":
if (isset($_POST["pwd"])) $pwd = $_POST["pwd"];
else $pwd="";
$pwdchk = 1;
if (isset($_GET["pwd2"])) $pwdchk = strcasecmp(md5( date("z") . $admin_password),$_GET["pwd2"]);
if (strcasecmp($pwd,$admin_password) != 0 && $pwdchk != 0) {
echo "<HTML><HEAD><META HTTP-EQUIV='REFRESH' CONTENT='1; URL=$PHP_SELF?do=admin'></HEAD><BODY bgcolor='#ffffff'><CENTER><H2>Falsches Passwort!</H2>Bitte warten...</CENTER></BODY><HTML>";
exit;
}
// Get all administrator changes
$record = file($data_file);
rsort($record);
$jmlrec = count($record);
// Delete duplicates (the dum way...) and get host email if any
$hostemail = "";
for ($i = 0; $i < $jmlrec; $i++) {
if (isset($record[$i])) {
$r = $record[$i];
$row = explode("|~~|",$r);
$chkmail = $row[2];
if ($row[4] == "Gastgeber") $hostemail = $row[2];
for($j = $i+1; $j < $jmlrec; $j++) {
$r = $record[$j];
$row = explode("|~~|",$r);
if (strcasecmp($row[2],$chkmail)==0 ) { $record[$j] = ""; }
}
}
}
$updno = 0;
$err = 0;
for ($i = 0; $i < $jmlrec; $i++) { // Geändert von $jmlrec+12
// Changes ?
if (isset($_POST["vemail".$i])) $chkmail = $_POST["vemail".$i]; else $chkmail ="";
$found = -1;
if ($chkmail != "") {
// Find existing record if possible...
$recno = count($record);
for($j = 0; $j < $recno; $j++) {
$r = $record[$j];
$row = explode("|~~|",$r);
if (strcasecmp($row[2],$chkmail) == 0) { $found = $j; break; }
}
$changed = 1;
if ($found >= 0) {
// Changed ?
if ( $row[2] == $_POST["vemail".$i] && $row[3] == $_POST["vname".$i] && $row[4] == $_POST["vreply".$i] && $row[5] == $_POST["vamount".$i] &&
$row[6] == $_POST["vcompany".$i] && $row[7] == $_POST["vcomment".$i] ) $changed = 0;
}
if ( $changed != 0 ) {
$updno++;
$idx = date("YmdHis");
$tgl = date("d.m.Y H:i");
$vemail = str_replace("<","",$_POST["vemail".$i]);
$vemail = str_replace(">","",$vemail);
$vemail = str_replace("~~","--",$vemail);
$vemail = str_replace("\"",""",$vemail);
$vname = str_replace("<","",$_POST["vname".$i]);
$vname = str_replace(">","",$vname);
$vname = str_replace("~","-",$vname);
$vname = str_replace("\"",""",$vname);
$vreply = str_replace("<","",$_POST["vreply".$i]);
$vreply = str_replace(">","",$vreply);
if ($vreply != "ungesehen" && $vreply != "gesehen" && $vreply != "unentschlossen" && $vreply != "abgesagt" && $vreply != "zugesagt" && $vreply != "Gastgeber") $vreply = "ungesehen";
if ($vreply != "Gastgeber" && $vreply != "ungesehen" && $found >= 0 ) $tgl = $tgl . "*"; // admin change
$vamount = str_replace("<","<",$_POST["vamount".$i]);
$vamount = str_replace(">",">",$vamount);
$vamount = str_replace("~","-",$vamount);
$vamount = str_replace("\"",""",$vamount);
$vcomment = str_replace("<","<",$_POST["vcomment".$i]);
$vcomment = str_replace(">",">",$vcomment);
$vcomment = str_replace("~","-",$vcomment);
$vcomment = str_replace("\"",""",$vcomment);
$vcompany = str_replace("<","<",$_POST["vcompany".$i]);
$vcompany = str_replace(">",">",$vcompany);
$vcompany = str_replace("~","-",$vcompany);
$vcompany = str_replace("\"",""",$vcompany);
if (strtoupper($os) == "WIN") {
$vcomment = str_replace("\r\n","<BR>",$_POST["vcomment".$i]);
$vcomment = str_replace("\r","",$vcomment);
$vcomment = str_replace("\n","",$vcomment);
} else {
$vcomment = str_replace("\n","<BR>",$vcomment);
$vcomment = str_replace("\r","",$vcomment);
}
$validemail = 1;
if (!preg_match("/([\w\.\-]+)(\#[\w\.\-]+)(\.[a-z]{2,4})+/i", $vemail)) $validemail = 0;
if ( $vname != "" && $validemail == 0 ) { echo "Email not valid for $vname : $vemail <br>"; $err++; }
if ( $vname != "" && $validemail == 1 ) {
if ($found < 0 ) { $record[] = "\n"; $found = count($record) - 1; }
$newdata = "<?//|~~|$idx|~~|$vemail|~~|$vname|~~|$vreply|~~|$vamount|~~|$vcompany|~~|$vcomment|~~|$tgl|~~|?>\n";
$newdata = "!" . stripslashes($newdata);
$record[$found] = $newdata;
} else {
if ( $vname != "") { $updno--; $changed = 0; }
}
}
if ( $changed == 0 ) {
if ($found >= 0) $record[$found] = "!".$record[$found];
}
}
}
// Quick add
$quickadd = "";
if (isset($_POST["quickadd"]) && $_POST["quickadd"] != "") $quickadd = stripslashes($_POST["quickadd"]);
if (trim($quickadd) != "") {
$qadd = explode("\"",$quickadd);
$qaddn = count($qadd);
$nametoadd = "";
$companytoadd = "";
for($i = 0; $i < $qaddn; $i++) {
if ($i % 2 == 0) {
$q2 = explode(",",$qadd[$i]);
$q2n = count($q2);
for($j = 0; $j < $q2n; $j++) {
$mailtoadd = trim($q2[$j]);
$mailtoadd = str_replace("<","",$mailtoadd);
$mailtoadd = str_replace(">","",$mailtoadd);
$mailtoadd = str_replace("~~","--",$mailtoadd);
if ($mailtoadd != "") {
$validemail = 1;
if (!preg_match("/([\w\.\-]+)(\#[\w\.\-]+)(\.[a-z]{2,4})+/i", $mailtoadd)) $validemail = 0;
if ($validemail == 0 ) { echo "Fehler bei $mailtoadd (Keine gültige eMail-Addresse)"; $err++; } else {
if ($nametoadd == "") { $nametoaddarr = explode("#",$mailtoadd); $nametoadd = $nametoaddarr[0]; }
$idx = date("YmdHis");
$newdata = "<?//|~~|$idx|~~|$mailtoadd|~~|$nametoadd|~~|ungesehen|~~|1|~~|$companytoadd|~~||~~|?>\n";
$newdata = "!" . stripslashes($newdata);
$record[]= $newdata;
$nametoadd = "";
$companytoadd = "";
}
}
}
}
if ($i % 2 == 1) {
$data = $qadd[$i];
list($nametoadd, $companytoadd) = explode(";", $data);
$nametoadd = str_replace("<","" ,$nametoadd);
$nametoadd = str_replace(">","" ,$nametoadd);
$nametoadd = str_replace("~","-",$nametoadd);
$companytoadd = str_replace("|","" ,$companytoadd);
$companytoadd = str_replace("<","" ,$companytoadd);
$companytoadd = str_replace(">","" ,$companytoadd);
$companytoadd = str_replace("~","-",$companytoadd);
}
}
}
/* Hier liegt der tote Fuchs begraben!!! */
$jmlrec = count($record);
$update_data = fopen($data_file,"w");
if (strtoupper($os) == "UNIX") {
if (flock($update_data,LOCK_EX)) {
for ($j=0; $j<$jmlrec; $j++) {
if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
if ($record[$j] != "" && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));
}
flock($update_data,LOCK_UN);
}
} else {
for ($j=0; $j<$jmlrec; $j++) {
if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
if ($record[$j] != "" && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));
}
}
fclose($update_data);
I'm looking forward for some ideas. I'll love to invite you for a coffee... ;) I can also send the whole script, if someone is interested.
Best!
Philipp
EDIT: I'm still totally lost in the code, so I want to share the other parts of the code with you and maybe someone finds the problem. ;) Thanks!
$jml_page = intval($jmlrec/$max_entry_per_page);
$sisa = $jmlrec%$max_entry_per_page;
if ($sisa > 0) $jml_page++;
// Set $nomaybe, $nono, $noyes DEBUG
$nomaybe = 0;
$nono = 0;
$noyes = 0;
$countmaybe = 0;
$countno = 0;
$countyes = 0;
$counttotal = 0;
$no = 0;
if ($jmlrec == 0) echo "<tr><td align='center'>Noch keine Einträge.</td></tr>";
$w = 0; //--Color
for ($i=0; $i<$max_entry_per_page; $i++) {
// Find the lowest next possible record.
$no = $jmlrec + 1;
if ($nomaybe < $jmlrec ) $no = $nomaybe + 1;
if ($nono < $no - 1 ) $no = $nono + 1;
if ($noyes < $no - 1 ) $no = $noyes + 1;
// Check this is valid.
do {
while (($no < $jmlrec + 1) && (!isset($record[$no-1]) || $record[$no-1] == "")) { $no++; }
$recno = $no - 1;
$cont = 0;
if ($no < $jmlrec + 1) {
$row = explode("|~~|",$record[$recno]);
$vr = $row[4];
$cont = 0;
if (($vr == "ungesehen" || $vr == "gesehen" || $vr == "unentschlossen") && ($no <= $nomaybe)) $cont = 1;
if (($vr == "abgesagt" ) && ($no <= $nono)) $cont = 1;
if (($vr == "zugesagt" || $vr == "Gastgeber") && ($no <= $noyes)) $cont = 1;
if ($cont == 1) $no++;
}
} while ($cont == 1);
if (isset($record[$recno]) && $record[$recno] != "") {
if ($w==0) {
$warna = $table_content_1a;
$warna2 = $table_content_1b;
$w=1;
} else {
$warna = $table_content_2a;
$warna2 = $table_content_2b;
$w=0;
}
do {
$nomaybe++;
$recno = $nomaybe-1;
$row = explode("|~~|",$record[$recno]);
}
while ($nomaybe <= $jmlrec && $row[4] != "ungesehen" && $row[4] != "gesehen" && $row[4] != "unentschlossen" );
if ($row[4] == "ungesehen" || $row[4] == "gesehen" || $row[4]=="unentschlossen") {
echo "<tr><td>$row[3]</td>";
if (isset($row[2]) && $row[2] != "" ) echo "<td><p>$row[2]</p></td>"; else echo "<td><p></p></td>";
if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
$countmaybe++;
}
if ($row[4] == "gesehen") echo "<td><b>gesehen</b></td><td>$row[8]</td>";
if ($row[4] == "unentschlossen") echo "<td><b>unentschlossen</b></td><td>$row[8]</td>";
if ($row[4] == "ungesehen") echo "<td><b>ungesehen</b></td><td></td>";
echo "<td><span class='date'><b> </b></span></td>";
if (isset($row[7]) && ($row[4]=="gesehen" || $row[4]=="unentschlossen" || $row[4]=="ungesehen" ) && $row[7] != "")
echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
do {
$nono++;
$recno = $nono-1;
$row = explode("|~~|",$record[$recno]);
}
while ($nono <= $jmlrec && $row[4] != "abgesagt" && $row != "gesehen" && $row[5] != "unentschlossen" );
$row = explode("|~~|",$record[$recno]);
if ($row[4] == "abgesagt" ) { echo "<tr><td>$row[3]</td><td><p>$row[6]</p></td><td><b>abgesagt</b></td><td>$row[8]</td>";
echo "<td><span class='date'><b> </b></span></td>";
if (isset($row[7]) && $row[7] != "") echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
$countno++;
}
do {
$noyes++;
$recno = $noyes-1;
$row = explode("|~~|",$record[$recno]);
} while ($noyes <= $jmlrec && $row[4] != "zugesagt" && $row[4] != "Gastgeber" );
if ($row[4] == "zugesagt" || $row[4] == "Gastgeber" ) {
echo "<tr><td>$row[3]";
if ($row[4] == "Gastgeber") print " (Gastgeber) ";
echo "</td>";
if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
echo "<td><b>zugesagt</b></td><td>$row[8]</td>";
if (isset($row[5]) && $row[5] != "") echo "<td><p>$row[5]</p></td>"; else echo "<td><p>1</p></td>";
if (isset($row[7]) && $row[7] != "" ) echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
$countyes++;
if (isset($row[5]) && $row[5] != "") $counttotal = $counttotal + intval($row[5]);
}
} //--end if
} //--end for

It would appear that updated entries are marked by prepending them with !. This loop appears to only be saving the updates, not every record.
Perhaps there was a typo - fopen with mode "w" will truncate the data file at 0 bytes before writing, if only the updated records are to be written, shouldn't that be mode "a" to append them to the existing file?
It is likely there is also a limit on how large a data file is allowed to grow, make sure you have set it large enough to allow 1000 records.

Related

Check and modify Capitalized letters in string

I'm trying to figure out how to make this code work.
I input some text via variable into code:
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
You will see that for the Genome there is a Nn and for the Fenome there is a Ee
Upon this happening I need it to give a 50/50 chance for a compared result to be EITHER Ee OR Nn - The code I have right now can only check them individually (so sometimes I ger Ee AND Nn, other times I will get ee and nn) and I feel there could be a much easier method of achieving this than what I'm trying:
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
if ($gepieces[0] === $fepieces[0]) {
$ss = $gepieces[0];
} else {
$ss = rand(1,2);
if ($ss == 1) {
$ss = $gepieces[0];
} else {
$ss = $fepieces[0];
}
}
if ($gepieces[1] === $fepieces[1]) {
$ee = $gepieces[1];
} else {
$ee = rand(1,2);
if ($ee == 1) {
$ee = $gepieces[1];
} else {
$ee = $fepieces[1];
}
}
if ($gepieces[2] === $fepieces[2]) {
$ff = $gepieces[2];
} else {
$ff = rand(1,2);
if ($ff == 1) {
$ff = $gepieces[2];
} else {
$ff = $fepieces[2];
}
}
if ($gepieces[3] === $fepieces[3]) {
$nn = $gepieces[3];
} else {
$nn = rand(1,2);
if ($nn == 1) {
$nn = $gepieces[3];
} else {
$nn = $fepieces[3];
}
}
if ($gepieces[4] === $fepieces[4]) {
$oo = $gepieces[4];
} else {
$oo = rand(1,2);
if ($oo == 1) {
$oo = $gepieces[4];
} else {
$oo = $fepieces[4];
}
}
echo $cubgender." - ".$ss."/".$ee."/".$ff."/".$nn."/".$oo."<br/>";
}
I'm a colossal idiot!
Figured out my own issue
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
if ($genome === $fenome) {
$cubgeno = $genome;
} else {
$cubgeno = rand(1,2);
if ($cubgeno == 1) {
$cubgeno = $genome;
} else {
$cubgeno = $fenome;
}
}
echo $cubgender." - ".$cubgeno."<br/>";
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
$sequence = "";
for ($i = 0;$i < count($fepieces); $i++) {
rand(1,2) == 1 ? $sequence .= $gepieces[$i] : $sequence .= $fepieces[$i];
}
echo $sequence;

Is there a way to process PDF files as text files in PHP?

My company receives coupons codes from another company and I need to process them to use them in my company's website. The problem is that they send me a PDF file with the codes, and they say their system can only export them in PDF. Strange.
I've tried several times to let them know that I need those coupons in plain text separated by something (a.k.a CSV) and they doesn't take any notice of that.
My website is written in PHP, and uses MySQL. I would like to offer a way to upload that coupons file and add those to the database. It would be easy considering it is a CSV file but it's not.
Is there any way I can programatically -not manually- process PDF files as text files? or, any workaround for this situation?
I have found pdf2text to have worked well for quite some time.
Will not work with image (TIFF) PDF. will work with text searchable PDF.
include('/home/user/php/class.pdf2text.php');
$p2t = new PDF2Text();
$p2t ->setFilename($pdf);
$p2t ->decodePDF();
$data = $p2t ->output();
$pos = strpos($data,$search);
if (pos){...}
Source:
<?php
class PDF2Text {
// Some settings
var $multibyte = 4; // Use setUnicode(TRUE|FALSE)
var $convertquotes = ENT_QUOTES; // ENT_COMPAT (double-quotes), ENT_QUOTES (Both), ENT_NOQUOTES (None)
var $showprogress = true; // TRUE if you have problems with time-out
// Variables
var $filename = '';
var $decodedtext = '';
function setFilename($filename) {
// Reset
$this->decodedtext = '';
$this->filename = $filename;
}
function output($echo = false) {
if($echo) echo $this->decodedtext;
else return $this->decodedtext;
}
function setUnicode($input) {
// 4 for unicode. But 2 should work in most cases just fine
if($input == true) $this->multibyte = 4;
else $this->multibyte = 2;
}
function decodePDF() {
// Read the data from pdf file
$infile = #file_get_contents($this->filename, FILE_BINARY);
if (empty($infile))
return "";
// Get all text data.
$transformations = array();
$texts = array();
// Get the list of all objects.
preg_match_all("#obj[\n|\r](.*)endobj[\n|\r]#ismU", $infile . "endobj\r", $objects);
$objects = #$objects[1];
// Select objects with streams.
for ($i = 0; $i < count($objects); $i++) {
$currentObject = $objects[$i];
// Prevent time-out
#set_time_limit ();
if($this->showprogress) {
// echo ". ";
flush(); ob_flush();
}
// Check if an object includes data stream.
if (preg_match("#stream[\n|\r](.*)endstream[\n|\r]#ismU", $currentObject . "endstream\r", $stream )) {
$stream = ltrim($stream[1]);
// Check object parameters and look for text data.
$options = $this->getObjectOptions($currentObject);
if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"])) )
// if ( $options["Image"] && $options["Subtype"] )
// if (!(empty($options["Length1"]) && empty($options["Subtype"])) )
continue;
// Hack, length doesnt always seem to be correct
unset($options["Length"]);
// So, we have text data. Decode it.
$data = $this->getDecodedStream($stream, $options);
if (strlen($data)) {
if (preg_match_all("#BT[\n|\r](.*)ET[\n|\r]#ismU", $data . "ET\r", $textContainers)) {
$textContainers = #$textContainers[1];
$this->getDirtyTexts($texts, $textContainers);
} else
$this->getCharTransformations($transformations, $data);
}
}
}
// Analyze text blocks taking into account character transformations and return results.
$this->decodedtext = $this->getTextUsingTransformations($texts, $transformations);
}
function decodeAsciiHex($input) {
$output = "";
$isOdd = true;
$isComment = false;
for($i = 0, $codeHigh = -1; $i < strlen($input) && $input[$i] != '>'; $i++) {
$c = $input[$i];
if($isComment) {
if ($c == '\r' || $c == '\n')
$isComment = false;
continue;
}
switch($c) {
case '\0': case '\t': case '\r': case '\f': case '\n': case ' ': break;
case '%':
$isComment = true;
break;
default:
$code = hexdec($c);
if($code === 0 && $c != '0')
return "";
if($isOdd)
$codeHigh = $code;
else
$output .= chr($codeHigh * 16 + $code);
$isOdd = !$isOdd;
break;
}
}
if($input[$i] != '>')
return "";
if($isOdd)
$output .= chr($codeHigh * 16);
return $output;
}
function decodeAscii85($input) {
$output = "";
$isComment = false;
$ords = array();
for($i = 0, $state = 0; $i < strlen($input) && $input[$i] != '~'; $i++) {
$c = $input[$i];
if($isComment) {
if ($c == '\r' || $c == '\n')
$isComment = false;
continue;
}
if ($c == '\0' || $c == '\t' || $c == '\r' || $c == '\f' || $c == '\n' || $c == ' ')
continue;
if ($c == '%') {
$isComment = true;
continue;
}
if ($c == 'z' && $state === 0) {
$output .= str_repeat(chr(0), 4);
continue;
}
if ($c < '!' || $c > 'u')
return "";
$code = ord($input[$i]) & 0xff;
$ords[$state++] = $code - ord('!');
if ($state == 5) {
$state = 0;
for ($sum = 0, $j = 0; $j < 5; $j++)
$sum = $sum * 85 + $ords[$j];
for ($j = 3; $j >= 0; $j--)
$output .= chr($sum >> ($j * 8));
}
}
if ($state === 1)
return "";
elseif ($state > 1) {
for ($i = 0, $sum = 0; $i < $state; $i++)
$sum += ($ords[$i] + ($i == $state - 1)) * pow(85, 4 - $i);
for ($i = 0; $i < $state - 1; $i++) {
try {
if(false == ($o = chr($sum >> ((3 - $i) * 8)))) {
throw new Exception('Error');
}
$output .= $o;
} catch (Exception $e) { /*Dont do anything*/ }
}
}
return $output;
}
function decodeFlate($data) {
return #gzuncompress($data);
}
function getObjectOptions($object) {
$options = array();
if (preg_match("#<<(.*)>>#ismU", $object, $options)) {
$options = explode("/", $options[1]);
#array_shift($options);
$o = array();
for ($j = 0; $j < #count($options); $j++) {
$options[$j] = preg_replace("#\s+#", " ", trim($options[$j]));
if (strpos($options[$j], " ") !== false) {
$parts = explode(" ", $options[$j]);
$o[$parts[0]] = $parts[1];
} else
$o[$options[$j]] = true;
}
$options = $o;
unset($o);
}
return $options;
}
function getDecodedStream($stream, $options) {
$data = "";
if (empty($options["Filter"]))
$data = $stream;
else {
$length = !empty($options["Length"]) ? $options["Length"] : strlen($stream);
$_stream = substr($stream, 0, $length);
foreach ($options as $key => $value) {
if ($key == "ASCIIHexDecode")
$_stream = $this->decodeAsciiHex($_stream);
elseif ($key == "ASCII85Decode")
$_stream = $this->decodeAscii85($_stream);
elseif ($key == "FlateDecode")
$_stream = $this->decodeFlate($_stream);
elseif ($key == "Crypt") { // TO DO
}
}
$data = $_stream;
}
return $data;
}
function getDirtyTexts(&$texts, $textContainers) {
for ($j = 0; $j < count($textContainers); $j++) {
if (preg_match_all("#\[(.*)\]\s*TJ[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
elseif (preg_match_all("#T[d|w|m|f]\s*(\(.*\))\s*Tj[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
elseif (preg_match_all("#T[d|w|m|f]\s*(\[.*\])\s*Tj[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
}
}
function getCharTransformations(&$transformations, $stream) {
preg_match_all("#([0-9]+)\s+beginbfchar(.*)endbfchar#ismU", $stream, $chars, PREG_SET_ORDER);
preg_match_all("#([0-9]+)\s+beginbfrange(.*)endbfrange#ismU", $stream, $ranges, PREG_SET_ORDER);
for ($j = 0; $j < count($chars); $j++) {
$count = $chars[$j][1];
$current = explode("\n", trim($chars[$j][2]));
for ($k = 0; $k < $count && $k < count($current); $k++) {
if (preg_match("#<([0-9a-f]{2,4})>\s+<([0-9a-f]{4,512})>#is", trim($current[$k]), $map))
$transformations[str_pad($map[1], 4, "0")] = $map[2];
}
}
for ($j = 0; $j < count($ranges); $j++) {
$count = $ranges[$j][1];
$current = explode("\n", trim($ranges[$j][2]));
for ($k = 0; $k < $count && $k < count($current); $k++) {
if (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+<([0-9a-f]{4})>#is", trim($current[$k]), $map)) {
$from = hexdec($map[1]);
$to = hexdec($map[2]);
$_from = hexdec($map[3]);
for ($m = $from, $n = 0; $m <= $to; $m++, $n++)
$transformations[sprintf("%04X", $m)] = sprintf("%04X", $_from + $n);
} elseif (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+\[(.*)\]#ismU", trim($current[$k]), $map)) {
$from = hexdec($map[1]);
$to = hexdec($map[2]);
$parts = preg_split("#\s+#", trim($map[3]));
for ($m = $from, $n = 0; $m <= $to && $n < count($parts); $m++, $n++)
$transformations[sprintf("%04X", $m)] = sprintf("%04X", hexdec($parts[$n]));
}
}
}
}
function getTextUsingTransformations($texts, $transformations) {
$document = "";
for ($i = 0; $i < count($texts); $i++) {
$isHex = false;
$isPlain = false;
$hex = "";
$plain = "";
for ($j = 0; $j < strlen($texts[$i]); $j++) {
$c = $texts[$i][$j];
switch($c) {
case "<":
$hex = "";
$isHex = true;
$isPlain = false;
break;
case ">":
$hexs = str_split($hex, $this->multibyte); // 2 or 4 (UTF8 or ISO)
for ($k = 0; $k < count($hexs); $k++) {
$chex = str_pad($hexs[$k], 4, "0"); // Add tailing zero
if (isset($transformations[$chex]))
$chex = $transformations[$chex];
$document .= html_entity_decode("&#x".$chex.";");
}
$isHex = false;
break;
case "(":
$plain = "";
$isPlain = true;
$isHex = false;
break;
case ")":
$document .= $plain;
$isPlain = false;
break;
case "\\":
$c2 = $texts[$i][$j + 1];
if (in_array($c2, array("\\", "(", ")"))) $plain .= $c2;
elseif ($c2 == "n") $plain .= '\n';
elseif ($c2 == "r") $plain .= '\r';
elseif ($c2 == "t") $plain .= '\t';
elseif ($c2 == "b") $plain .= '\b';
elseif ($c2 == "f") $plain .= '\f';
elseif ($c2 >= '0' && $c2 <= '9') {
$oct = preg_replace("#[^0-9]#", "", substr($texts[$i], $j + 1, 3));
$j += strlen($oct) - 1;
$plain .= html_entity_decode("&#".octdec($oct).";", $this->convertquotes);
}
$j++;
break;
default:
if ($isHex)
$hex .= $c;
elseif ($isPlain)
$plain .= $c;
break;
}
}
$document .= "\n";
}
return $document;
}
}
?>

How to see the array results of the generated table in php

So this is the some part of the code I study:
if ($handle) {
while (($line = fgets($handle)) !== false) {
$exploded_line = explode("::", $line);
$day = date("j", $exploded_line[0]);
$action = trim($exploded_line[4]);
if ($action == LOGIN_ACTION) {
$details[$exploded_line[3]][$day] = isset($details[$exploded_line[3]][$day]) ? $details[$exploded_line[3]][$day] + 1 : 1;
} else if ($action == COMPANY_TICKETS_ACTION || $action == MYTICKETS_ACTION) {
$company_mytickets_action[$day] = isset($company_mytickets_action[$day]) ? $company_mytickets_action[$day] + 1 : 1;
} else if ($action == NEW_TICKET_ACTION) {
$new_ticket_action[$day] = isset($new_ticket_action[$day]) ? $new_ticket_action[$day] + 1 : 1;
} else if ($action == SEARCH_ACTION) {
$search_action[$day] = isset($search_action[$day]) ? $search_action[$day] + 1 : 1;
} else if ($action == PREFERENCES_ACTION) {
$preferences_action[$day] = isset($preferences_action[$day]) ? $preferences_action[$day] + 1 : 1;
}
}
} else {
$data['html'] = ERR_MSG;
}
fclose($handle);
print_r();
foreach ($details as $users => $value) {
$data['html'] .= "<tr><td>$users</td>";
for ($x = 1; $x <= $calendar_days; $x++){
//$details[$users][$x] = empty($details[$users][$x]) ? SPACE : $details[$users][$x];
if(empty($details[$users][$x]))
$details[$users][$x]='';
else
$details[$users][$x];
$data['html'] .= "<td>" . $details[$users][$x] . "</td>";
$unique[$x] = $details[$users][$x] == SPACE ? $unique[$x] : $unique[$x] + 1;
$total[$x] = $details[$users][$x] == SPACE ? $total[$x] : $total[$x] + $details[$users][$x];
}
$data['html'] . "</tr>";
}
Now I want to var_dump the results to array. I try this print_r($details[$exploded_line[3]][$day]); but there is no display in the browser. How can I get the results in array? Thanks

I can't show multiple days of absent

my client ask me to show the in and out of their employees from biometrics..i already did it but it only shows data for 1 day. Can somebody help me..If you can see I had a lot of codes there and it doesn't satisfy my client.I'am working on it for almost a month
this is my code..I'm a beginner.
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(isset($_REQUEST['startdate']) && isset($_REQUEST['enddate']))
{
$startdate = $_REQUEST['startdate'];
$enddate = $_REQUEST['enddate'];
$shiftdate = $_REQUEST['shiftdate'];
$category= $_REQUEST['category'];
$counterless = 0;
$ncounter = 0;
$count =0 ;
$loop=0;
$holder=0;
$checker=0;
$wala = 0;
$present = 0;
/*$_SESSION['nightname'] = array();
$_SESSION['nighttime'] = array();
$_SESSION['testname'] = array();
$_SESSION['testtime'] = array();*/
$con = mysqli_connect("localhost","root","","testingdb");
$connect = mysqli_connect("localhost","root","","inoutchecking");
mysqli_query($con,"delete from forprint");
mysqli_query($con,"delete from absentforprint");
$result = mysqli_query($con,"select * from wawart where Column_2 = '$startdate' order by Column_2 asc");
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_array($result))
{
$time = $row['Column_3'];
$times = (int)($time);
$inout = $row['Column_4'];
$dates = $row['Column_2'];
if(($times <= 14 and $inout == '1') or ($times >=17 and $inout == '4'))
{
$_SESSION['testname'][$counterless] = $row['Column_1'];
$_SESSION['testtime'][$counterless] = $time;
$counterless++;
}
if(($times <= 20 and $times >= 15) and $inout == '1')
{
$_SESSION['nightname'][$ncounter] = $row['Column_1'];
$_SESSION['nighttime'][$ncounter] = $time;
$ncounter++;
}
}
$wala=1;
}
else
{
$wala=0;
}
if($category == 'dayshift')
{
$shift = 'Day Shift';
}
else if($category == 'nightshift')
{
$shift = 'Night Shift';
}
else
{
$shift = 'Night Shift and Day Shift';
}
echo "<label class='cat'>".$shift."</label>";
echo "<label class='absent'>Absentees ".$startdate."</label>";
if($category == 'dayshift')
{
mysqli_query($connect,"insert into forprint (empid,lastname,firstname,department,section,timein,timeout,stat) values('Employee Number','Lastname','Firstname','Department','Section','Time in','Time out','Shift')");
echo "<div class='dayshift'> <table class='CSSTableGenerator'><tr><th>Emp#</th><th>LASTNAME</th><th>FIRSTNAME</th><th>DEPARTMENT</th><th>SECTION</th><th>DATE</th><th>IN</th><th>OUT</th></tr>";
for($x = 0; $x<$counterless;$x++)
{
$dempid = $_SESSION['testname'][$x];
$day = mysqli_query($connect,"select * from nightshiftlist where empid = '$dempid' and shift ='dayshift' and datefrom = '$shiftdate' and datehired <= '$startdate'");
while($empinfo = mysqli_fetch_array($day))
{
$count++;
if($loop == '1')
{
echo "<td>NO OUT</td></tr>";
$loop=0;
}
$empid = $empinfo['empid'];
$empshift = $empinfo['shift'];
$name = $empinfo['lastname'];
$fname = $empinfo['firstname'];
$dept = $empinfo['department'];
$section = $empinfo['section'];
$checking = mysqli_query($con,"select * from wawart where Column_1='$empid' and (Column_2 between '$startdate' and '$enddate') order by Column_2 asc");
if($holder == $empinfo['empid'])
{
break;
}
echo"<tr><td>".$empinfo['empid']."</td><td>".$empinfo['lastname']."</td><td>".$empinfo['firstname']."</td><td>".$empinfo['department']."</td><td>".$empinfo['section']."</td>";
if(mysqli_num_rows($checking)>0)
{
while($row=mysqli_fetch_array($checking))
{
$time = $row['Column_3'];
$times = (int)$time;
if($holder == $empinfo['empid'])
{
break;
}
else
{
$inout = $row['Column_4'];
$time = $row['Column_3'];
if($loop ==0)
{
$date = $row['Column_2'];
echo"<td>".$row['Column_2']."</td>";
}
if($loop ==0 )
{
if($inout == '1')
{
$timein = $time;
echo "<td>".$time."</td>";
$loop = 1;
}
if($inout == '4')
{
$timein = 'NO IN';
$timeout = $time;
echo "<td><B>NO IN</td><td>".$time."</td></tr>";
$holder = $empinfo['empid'];
$loop = 0;
}
}
else
{
if($loop =='1' && $inout == '4')
{
$timeout = $time;
echo "<td>".$time."</td></tr>";
}
if($loop =='1' && $inout == '1')
{
$timeout = 'NO OUT';
echo "<td><B>NO OUT</td></tr>";
}
$loop = 0;
$counter = 0;
$holder = $empinfo['empid'];
$checker = 2;
if($checker == 2)
{
mysqli_query($connect,"insert into forprint (empid,lastname,firstname,department,section,timein,timeout,stat) values('$empid','$name','$fname','$dept','$section','$timein','$timeout','$empshift')");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
}
}
}
}
ini_set('max_execution_time', 999999);
}
}
echo"</table></div>";
/*absentees*/
if($wala == 1)
{
$count = 0;
mysqli_query($connect,"insert into absentforprint (empid,lastname,firstname,department,section) values ('Employee Number','Lastname','Firstname','Department','Section')");
echo "<div class='absentees'> <table class='CSSTableGenerator'><tr><th>Emp#</th><th>LASTNAME</th><th>FIRSTNAME</th><th>DEPARTMENT</th><th>SECTION</th></tr>";
$day = mysqli_query($connect,"select * from nightshiftlist where shift ='dayshift' and datefrom = '$shiftdate' and datehired <= '$startdate'");
while($empinfo = mysqli_fetch_array($day))
{
$id = $empinfo['empid'];
$shift = $empinfo['shift'];
$lname = $empinfo['lastname'];
$fname = $empinfo['firstname'];
$date = $empinfo['datefrom'];
$dept = $empinfo['department'];
$section = $empinfo['section'];
for($x = 0; $x<$counterless;$x++)
{
$dempid = $_SESSION['testname'][$x];
if($id == $dempid)
{
$present = 1;
break;
}
else
{
$present = 0;
}
ini_set('max_execution_time', 999999);
}
if($present == 0)
{
echo "<tr><td>".$id."</td><td>".$lname."</td><td>".$fname."</td><td>".$dept."</td><td>".$section."</td></tr>";
mysqli_query($connect,"insert into absentforprint (empid,lastname,firstname,department,section) values ('$id','$lname','$fname','$dept','$section')");
ini_set('max_execution_time', 999999);
$count++;
}
}
echo"</table></div>";
echo"<label class='totalabs'>Total Number of Absentees ".$count."</label>";
}
}/*end of absentees*/
if($category == 'nightshift')
{
mysqli_query($connect,"insert into forprint (empid,lastname,firstname,department,section,timein,timeout,stat) values('Employee Number','Lastname','Firstname','Department','Section','Time in','Time out','Shift')");
echo "<div class='dayshift'> <table class='CSSTableGenerator'><tr><th>Emp#</th><th>LASTNAME</th><th>FIRSTNAME</th><th>DATE</th><th>DEPARTMENT</th><th>SECTION</th><th>IN</th><th>OUT</th></tr>";
for($x = 0; $x<$ncounter;$x++)
{
$dempid = $_SESSION['nightname'][$x];
$day = mysqli_query($connect,"select * from nightshiftlist where empid = '$dempid' and shift = 'nightshift' and datefrom = '$shiftdate' and datehired <= '$startdate'");
while($empinfo = mysqli_fetch_array($day))
{
$count++;
if($loop == '1')
{
echo "<td>NO OUT</td></tr>";
$loop=0;
}
$empid = $empinfo['empid'];
$empshift = $empinfo['shift'];
$name = $empinfo['lastname'];
$fname = $empinfo['firstname'];
$dept = $empinfo['department'];
$section = $empinfo['section'];
$checking = mysqli_query($con,"select * from wawart where Column_1='$empid' and (Column_2 between '$startdate' and '$enddate') order by Column_2 asc");
if($holder == $empinfo['empid'])
{
break;
}
echo"<tr><td>".$empinfo['empid']."</td><td>".$empinfo['lastname']."</td><td>".$empinfo['firstname']."</td><td>".$empinfo['department']."</td><td>".$empinfo['section']."</td>";
if(mysqli_num_rows($checking)>0)
{
while($row=mysqli_fetch_array($checking))
{
$time = $row['Column_3'];
$times = (int)$time;
if($holder == $empinfo['empid'])
{
break;
}
else
{
$inout = $row['Column_4'];
$time = $row['Column_3'];
$dates = $row['Column_2'];
if($loop ==0 && $inout == '1'&& $dates == $startdate)
{
$date = $row['Column_2'];
echo"<td>".$row['Column_2']."</td>";
}
if($loop ==0 && $dates == $startdate)
{
if($inout == '1')
{
$timein = $time;
echo "<td>".$time."</td>";
$loop = 1;
}
if($inout == '4')
{
$timein = 'NO IN';
$loop = 0;
}
}
else
{
if($loop =='1' && $inout == '4')
{
$timeout = $time;
echo "<td>".$time."</td></tr>";
}
if($loop =='1' && $inout == '1')
{
$timeout = 'NO OUT';
echo "<td><B>NO OUT</td></tr>";
}
$loop = 0;
$counter = 0;
$holder = $empinfo['empid'];
$checker = 2;
if($checker == 2)
{
mysqli_query($connect,"insert into forprint (empid,lastname,firstname,department,section,timein,timeout,stat) values('$dempid','$name','$fname','$dept','$section','$timein','$timeout','$empshift')");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
}
}
}
}
ini_set('max_execution_time', 999999);
}
}
echo"</table></div>";
/*absentees*/
if($wala == 1)
{
$count = 0;
mysqli_query($connect,"insert into absentforprint (empid,lastname,firstname,department,section) values ('Employee Number','Lastname','Firstname','Department','Section')");
echo "<div class='absentees'> <table class='CSSTableGenerator'><tr><th>Emp#</th><th>LASTNAME</th><th>FIRSTNAME</th><th>DEPARTMENT</th><th>SECTION</th></tr>";
$day = mysqli_query($connect,"select * from nightshiftlist where shift ='nightshift' and datefrom = '$shiftdate' and datehired <= '$startdate'");
while($empinfo = mysqli_fetch_array($day))
{
$id = $empinfo['empid'];
$shift = $empinfo['shift'];
$lname = $empinfo['lastname'];
$fname = $empinfo['firstname'];
$dept = $empinfo['department'];
$section = $empinfo['section'];
for($x = 0; $x<$ncounter;$x++)
{
$dempid = $_SESSION['nightname'][$x];
if($id == $dempid)
{
$present = 1;
break;
}
else
{
$present = 0;
}
ini_set('max_execution_time', 999999);
}
if($present == 0)
{
echo "<tr><td>".$id."</td><td>".$lname."</td><td>".$fname."</td><td>".$dept."</td><td>".$section."</td></tr>";
mysqli_query($connect,"insert into absentforprint (empid,lastname,firstname,department,section) values ('$id','$lname','$fname','$dept','$section')");
$count++;
}
ini_set('max_execution_time', 999999);
}
echo"</table></div>";
echo"<label class='totalabs'>Total Number of Absentees ".$count."</label>";
}
}/*end of absentees*/
}
?>
The first sql select query you are executing needs to be corrected.Your select sql query does not considering end date. Try the below query
$result = mysqli_query($con,"select * from wawart where Column_2 >= '$startdate' and Column_2 <= '$enddate' order by Column_2 asc");

How can I extract text from PDF files using PHP?

I know there are a lot of PDF extraction methods/techniques, but I'm after a reliable text extractor for PDFs in PHP. All I want is to extract words, but not numbers and no special characters.
Any ideas of solid techniques to achieve this?
The Zend Framework provides Zend_Pdf, a php class that will load and parse pdf documents.
Here is a script that shows how to extract the text from a loaded Zend_Pdf object.
If you use laravel, you can try my library...
<?php
namespace App\Helpers;
/*
SYNTAX:
include('class.pdf2text.php');
$a = new PDF2Text();
$a->setFilename('test.pdf');
$a->decodePDF();
echo $a->output();
*/
class PDF2Text {
// Some settings
var $multibyte = 4; // Use setUnicode(TRUE|FALSE)
var $convertquotes = ENT_QUOTES; // ENT_COMPAT (double-quotes), ENT_QUOTES (Both), ENT_NOQUOTES (None)
var $showprogress = false; // TRUE if you have problems with time-out
// Variables
var $filename = '';
var $decodedtext = '';
function setFilename($filename) {
$this->decodedtext = '';
$this->filename = $filename;
}
function output($echo = false) {
if($echo) echo $this->decodedtext;
else return $this->decodedtext;
}
function setUnicode($input) {
// 4 for unicode. But 2 should work in most cases just fine
if($input == true) $this->multibyte = 4;
else $this->multibyte = 2;
}
function decodePDF() {
// Read the data from pdf file
$infile = #file_get_contents($this->filename, FILE_BINARY);
if (empty($infile))
return "";
// Get all text data.
$transformations = array();
$texts = array();
// Get the list of all objects.
preg_match_all("#obj[\n|\r](.*)endobj[\n|\r]#ismU", $infile . "endobj\r", $objects);
$objects = #$objects[1];
// Select objects with streams.
for ($i = 0; $i < count($objects); $i++) {
$currentObject = $objects[$i];
// Prevent time-out
#set_time_limit ();
if($this->showprogress) {
flush(); ob_flush();
}
// Check if an object includes data stream.
if (preg_match("#stream[\n|\r](.*)endstream[\n|\r]#ismU", $currentObject . "endstream\r", $stream )) {
$stream = ltrim($stream[1]);
// Check object parameters and look for text data.
$options = $this->getObjectOptions($currentObject);
if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"])) )
continue;
unset($options["Length"]);
// So, we have text data. Decode it.
$data = $this->getDecodedStream($stream, $options);
if (strlen($data)) {
if (preg_match_all("#BT[\n|\r](.*)ET[\n|\r]#ismU", $data . "ET\r", $textContainers)) {
$textContainers = #$textContainers[1];
$this->getDirtyTexts($texts, $textContainers);
} else
$this->getCharTransformations($transformations, $data);
}
}
}
// Analyze text blocks taking into account character transformations and return results.
$this->decodedtext = $this->getTextUsingTransformations($texts, $transformations);
}
function decodeAsciiHex($input) {
$output = "";
$isOdd = true;
$isComment = false;
for($i = 0, $codeHigh = -1; $i < strlen($input) && $input[$i] != '>'; $i++) {
$c = $input[$i];
if($isComment) {
if ($c == '\r' || $c == '\n')
$isComment = false;
continue;
}
switch($c) {
case '\0': case '\t': case '\r': case '\f': case '\n': case ' ': break;
case '%':
$isComment = true;
break;
default:
$code = hexdec($c);
if($code === 0 && $c != '0')
return "";
if($isOdd)
$codeHigh = $code;
else
$output .= chr($codeHigh * 16 + $code);
$isOdd = !$isOdd;
break;
}
}
if($input[$i] != '>')
return "";
if($isOdd)
$output .= chr($codeHigh * 16);
return $output;
}
function decodeAscii85($input) {
$output = "";
$isComment = false;
$ords = array();
for($i = 0, $state = 0; $i < strlen($input) && $input[$i] != '~'; $i++) {
$c = $input[$i];
if($isComment) {
if ($c == '\r' || $c == '\n')
$isComment = false;
continue;
}
if ($c == '\0' || $c == '\t' || $c == '\r' || $c == '\f' || $c == '\n' || $c == ' ')
continue;
if ($c == '%') {
$isComment = true;
continue;
}
if ($c == 'z' && $state === 0) {
$output .= str_repeat(chr(0), 4);
continue;
}
if ($c < '!' || $c > 'u')
return "";
$code = ord($input[$i]) & 0xff;
$ords[$state++] = $code - ord('!');
if ($state == 5) {
$state = 0;
for ($sum = 0, $j = 0; $j < 5; $j++)
$sum = $sum * 85 + $ords[$j];
for ($j = 3; $j >= 0; $j--)
$output .= chr($sum >> ($j * 8));
}
}
if ($state === 1)
return "";
elseif ($state > 1) {
for ($i = 0, $sum = 0; $i < $state; $i++)
$sum += ($ords[$i] + ($i == $state - 1)) * pow(85, 4 - $i);
for ($i = 0; $i < $state - 1; $i++) {
try {
if(false == ($o = chr($sum >> ((3 - $i) * 8)))) {
throw new Exception('Error');
}
$output .= $o;
} catch (Exception $e) { /*Dont do anything*/ }
}
}
return $output;
}
function decodeFlate($data) {
return #gzuncompress($data);
}
function getObjectOptions($object) {
$options = array();
if (preg_match("#<<(.*)>>#ismU", $object, $options)) {
$options = explode("/", $options[1]);
#array_shift($options);
$o = array();
for ($j = 0; $j < #count($options); $j++) {
$options[$j] = preg_replace("#\s+#", " ", trim($options[$j]));
if (strpos($options[$j], " ") !== false) {
$parts = explode(" ", $options[$j]);
$o[$parts[0]] = $parts[1];
} else
$o[$options[$j]] = true;
}
$options = $o;
unset($o);
}
return $options;
}
function getDecodedStream($stream, $options) {
$data = "";
if (empty($options["Filter"]))
$data = $stream;
else {
$length = !empty($options["Length"]) ? $options["Length"] : strlen($stream);
$_stream = substr($stream, 0, $length);
foreach ($options as $key => $value) {
if ($key == "ASCIIHexDecode")
$_stream = $this->decodeAsciiHex($_stream);
elseif ($key == "ASCII85Decode")
$_stream = $this->decodeAscii85($_stream);
elseif ($key == "FlateDecode")
$_stream = $this->decodeFlate($_stream);
elseif ($key == "Crypt") { // TO DO
}
}
$data = $_stream;
}
return $data;
}
function getDirtyTexts(&$texts, $textContainers) {
for ($j = 0; $j < count($textContainers); $j++) {
if (preg_match_all("#\[(.*)\]\s*TJ[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
elseif (preg_match_all("#T[d|w|m|f]\s*(\(.*\))\s*Tj[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
elseif (preg_match_all("#T[d|w|m|f]\s*(\[.*\])\s*Tj[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
}
}
function getCharTransformations(&$transformations, $stream) {
preg_match_all("#([0-9]+)\s+beginbfchar(.*)endbfchar#ismU", $stream, $chars, PREG_SET_ORDER);
preg_match_all("#([0-9]+)\s+beginbfrange(.*)endbfrange#ismU", $stream, $ranges, PREG_SET_ORDER);
for ($j = 0; $j < count($chars); $j++) {
$count = $chars[$j][1];
$current = explode("\n", trim($chars[$j][2]));
for ($k = 0; $k < $count && $k < count($current); $k++) {
if (preg_match("#<([0-9a-f]{2,4})>\s+<([0-9a-f]{4,512})>#is", trim($current[$k]), $map))
$transformations[str_pad($map[1], 4, "0")] = $map[2];
}
}
for ($j = 0; $j < count($ranges); $j++) {
$count = $ranges[$j][1];
$current = explode("\n", trim($ranges[$j][2]));
for ($k = 0; $k < $count && $k < count($current); $k++) {
if (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+<([0-9a-f]{4})>#is", trim($current[$k]), $map)) {
$from = hexdec($map[1]);
$to = hexdec($map[2]);
$_from = hexdec($map[3]);
for ($m = $from, $n = 0; $m <= $to; $m++, $n++)
$transformations[sprintf("%04X", $m)] = sprintf("%04X", $_from + $n);
} elseif (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+\[(.*)\]#ismU", trim($current[$k]), $map)) {
$from = hexdec($map[1]);
$to = hexdec($map[2]);
$parts = preg_split("#\s+#", trim($map[3]));
for ($m = $from, $n = 0; $m <= $to && $n < count($parts); $m++, $n++)
$transformations[sprintf("%04X", $m)] = sprintf("%04X", hexdec($parts[$n]));
}
}
}
}
function getTextUsingTransformations($texts, $transformations) {
$document = "";
for ($i = 0; $i < count($texts); $i++) {
$isHex = false;
$isPlain = false;
$hex = "";
$plain = "";
for ($j = 0; $j < strlen($texts[$i]); $j++) {
$c = $texts[$i][$j];
switch($c) {
case "<":
$hex = "";
$isHex = true;
$isPlain = false;
break;
case ">":
$hexs = str_split($hex, $this->multibyte); // 2 or 4 (UTF8 or ISO)
for ($k = 0; $k < count($hexs); $k++) {
$chex = str_pad($hexs[$k], 4, "0"); // Add tailing zero
if (isset($transformations[$chex]))
$chex = $transformations[$chex];
$document .= html_entity_decode("&#x".$chex.";");
}
$isHex = false;
break;
case "(":
$plain = "";
$isPlain = true;
$isHex = false;
break;
case ")":
$document .= $plain;
$isPlain = false;
break;
case "\\":
$c2 = $texts[$i][$j + 1];
if (in_array($c2, array("\\", "(", ")"))) $plain .= $c2;
elseif ($c2 == "n") $plain .= '\n';
elseif ($c2 == "r") $plain .= '\r';
elseif ($c2 == "t") $plain .= '\t';
elseif ($c2 == "b") $plain .= '\b';
elseif ($c2 == "f") $plain .= '\f';
elseif ($c2 >= '0' && $c2 <= '9') {
$oct = preg_replace("#[^0-9]#", "", substr($texts[$i], $j + 1, 3));
$j += strlen($oct) - 1;
$plain .= html_entity_decode("&#".octdec($oct).";", $this->convertquotes);
}
$j++;
break;
default:
if ($isHex)
$hex .= $c;
elseif ($isPlain)
$plain .= $c;
break;
}
}
$document .= "\n";
//$document .= "<br>";
}
return $document;
}
}

Categories