-
Notifications
You must be signed in to change notification settings - Fork 14
/
class.doccounter.php
311 lines (271 loc) · 9.59 KB
/
class.doccounter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/*
* A collection of simple tools for analysing
* .PDF, .DOCX, .DOC and .TXT docs.
*
* Copyright (C) 2016-2017
* Joseph Blurton (http://github.com/joeblurton)
* And other contributors (see attrib below)
*
* Version 1.0.2
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ATTRIBUTIONS
*
* PageCount_PDF and
* PageCount_DOCX by Whiteflash
* http://stackoverflow.com/questions/5540886/extract-text-from-doc-and-docx/
*
* Paragraph tweak by JoshB
* http://stackoverflow.com/questions/5607594/find-linebreaks-in-a-docx-file-using-php
*
* read_word_doc by
* Davinder Singh
* http://stackoverflow.com/questions/7358637/reading-doc-file-in-php
*
* Jonny 5's simple word splitter
* http://php.net/manual/en/function.str-word-count.php#107363
*
* Line Count method by K2xL
* http://stackoverflow.com/questions/7955402/count-lines-in-a-posted-string
*
* RTFTOOLS by
* Christian Vigh
* https://github.com/christian-vigh-phpclasses/RtfTools
*
* PDF Parser by
* Smalot GPL 3
* https://github.com/smalot/pdfparser
*/
class DocCounter {
// Class Variables
private $file;
private $filetype;
// Set file
public function setFile($filename)
{
$this->file = $filename;
$this->filetype = pathinfo($this->file, PATHINFO_EXTENSION);
}
// Get file
public function getFile()
{
return $this->file;
}
// Get file information object
public function getInfo()
{
// Function variables
$ft = $this->filetype;
// Let's construct our info response object
$obj = new stdClass();
$obj->format = $ft;
$obj->wordCount = null;
$obj->lineCount = null;
$obj->pageCount = null;
// Let's set our function calls based on filetype
switch($ft)
{
case "doc":
$doc = $this->read_doc_file();
$obj->wordCount = $this->str_word_count_utf8($doc);
$obj->lineCount = $this->lineCount($doc);
$obj->pageCount = $this->pageCount($doc);
break;
case "docx":
$obj->wordCount = $this->str_word_count_utf8($this->docx2text());
$obj->lineCount = $this->lineCount($this->docx2text());
$obj->pageCount = $this->PageCount_DOCX();
break;
case "pdf":
$obj->wordCount = $this->str_word_count_utf8($this->pdf2text());
$obj->lineCount = $this->lineCount($this->pdf2text());
$obj->pageCount = $this->PageCount_PDF();
break;
case "txt":
$textContents = file_get_contents($this->file);
$obj->wordCount = $this->str_word_count_utf8($textContents);
$obj->lineCount = $this->lineCount($textContents);
$obj->pageCount = $this->pageCount($textContents);
break;
default:
$obj->wordCount = "unsupported file format";
$obj->lineCount = "unsupported file format";
$obj->pageCount = "unsupported file format";
}
return $obj;
}
// Convert: Word.doc to Text String
function read_doc_file() {
$path = getcwd();
$f = $path."/".$this->file;
if(file_exists($f))
{
if(($fh = fopen($f, 'r')) !== false )
{
$headers = fread($fh, 0xA00);
// 1 = (ord(n)*1) ; Document has from 0 to 255 characters
$n1 = ( ord($headers[0x21C]) - 1 );
// 1 = ((ord(n)-8)*256) ; Document has from 256 to 63743 characters
$n2 = ( ( ord($headers[0x21D]) - 8 ) * 256 );
// 1 = ((ord(n)*256)*256) ; Document has from 63744 to 16775423 characters
$n3 = ( ( ord($headers[0x21E]) * 256 ) * 256 );
// 1 = (((ord(n)*256)*256)*256) ; Document has from 16775424 to 4294965504 characters
$n4 = ( ( ( ord($headers[0x21F]) * 256 ) * 256 ) * 256 );
// Total length of text in the document
$textLength = ($n1 + $n2 + $n3 + $n4);
$extracted_plaintext = fread($fh, $textLength);
$extracted_plaintext = mb_convert_encoding($extracted_plaintext,'UTF-8');
// simple print character stream without new lines
//echo $extracted_plaintext;
// if you want to see your paragraphs in a new line, do this
return nl2br($extracted_plaintext);
// need more spacing after each paragraph use another nl2br
}
}
}
// Jonny 5's simple word splitter
function str_word_count_utf8($str) {
return count(preg_split('~[^\p{L}\p{N}\']+~u',$str));
}
// Convert: Word.docx to Text String
function docx2text()
{
return $this->readZippedXML($this->file, "word/document.xml");
}
function readZippedXML($archiveFile, $dataFile)
{
// Create new ZIP archive
$zip = new ZipArchive;
// set absolute path
$path = getcwd();
$f = $path."/".$archiveFile;
// Open received archive file
if (true === $zip->open($f)) {
// If done, search for the data file in the archive
if (($index = $zip->locateName($dataFile)) !== false) {
// If found, read it to the string
$data = $zip->getFromIndex($index);
// Close archive file
$zip->close();
// Load XML from a string
// Skip errors and warnings
$xml = new DOMDocument();
$xml->loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
$xmldata = $xml->saveXML();
// Newline Replacement
$xmldata = str_replace("</w:p>", "\r\n", $xmldata);
// Return data without XML formatting tags
return strip_tags($xmldata);
}
$zip->close();
}
// In case of failure return empty string
return "";
}
// Convert: Word.doc to Text String
function read_doc()
{
$path = getcwd();
$f = $path."/".$this->file;
$fileHandle = fopen($f, "r");
$line = @fread($fileHandle, filesize($this->file));
$lines = explode(chr(0x0D),$line);
$outtext = "";
foreach($lines as $thisline)
{
$pos = strpos($thisline, chr(0x00));
if (($pos !== FALSE)||(strlen($thisline)==0))
{
} else {
$outtext .= $thisline." ";
}
}
$outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);
return $outtext;
}
// Convert: Adobe.pdf to Text String
function pdf2text()
{
//absolute path for file
$path = getcwd();
$f = $path."/".$this->file;
if (file_exists($f)) {
include('vendor/autoload.php');
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile($f);
$text = $pdf->getText();
return $text;
}
return null;
}
// Page Count: DOCX using XML Metadata
function PageCount_DOCX()
{
$pageCount = 0;
$zip = new ZipArchive();
$path = getcwd();
$f = $path."/".$this->file;
if($zip->open($f) === true) {
if(($index = $zip->locateName('docProps/app.xml')) !== false) {
$data = $zip->getFromIndex($index);
$zip->close();
$xml = new SimpleXMLElement($data);
$pageCount = $xml->Pages;
}
}
return intval($pageCount);
}
// Page Count: PDF using FPDF and FPDI
function PageCount_PDF()
{
//absolute path for file
$path = getcwd();
$f = $path."/".$this->file;
$pageCount = 0;
if (file_exists($f)) {
require_once('lib/fpdf/fpdf.php');
require_once('lib/fpdi/fpdi.php');
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($f); // returns page count
}
return $pageCount;
}
// Page Count: General
function pageCount($text)
{
require_once('lib/fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->MultiCell(0,5,$text);
//$pdf->Output();
$filename="tmp.pdf";
$pdf->Output($filename,'F');
require_once('lib/fpdi/fpdi.php');
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($filename);
unlink($filename);
return $pageCount;
}
// Line Count: General
function lineCount($text)
{
$lines_arr = preg_split('/\n|\r/',$text);
$num_newlines = count($lines_arr);
return $num_newlines;
}
}
?>