1: <?php
2:
3: namespace Alpha\Util\Extension;
4:
5: use Alpha\Util\Config\ConfigProvider;
6: use Alpha\Util\Logging\Logger;
7: use Alpha\View\Widget\Image;
8: use Alpha\Exception\AlphaException;
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: class TCPDFFacade
54: {
55: 56: 57: 58: 59: 60: 61:
62: private $content;
63:
64: 65: 66: 67: 68: 69: 70:
71: private $pdf;
72:
73: 74: 75: 76: 77: 78: 79:
80: private $BO = null;
81:
82: 83: 84: 85: 86: 87: 88:
89: private $PDFFilename;
90:
91: 92: 93: 94: 95: 96: 97:
98: private $HTMLFilename;
99:
100: 101: 102: 103: 104: 105: 106:
107: private static $logger = null;
108:
109: 110: 111: 112: 113: 114: 115:
116: public function __construct($BO)
117: {
118: self::$logger = new Logger('TCPDFFacade');
119: self::$logger->debug('>>__construct()');
120:
121: $config = ConfigProvider::getInstance();
122:
123: $this->BO = $BO;
124:
125: $reflect = new \ReflectionClass($this->BO);
126: $classname = $reflect->getShortName();
127:
128: $this->PDFFilename = $config->get('app.file.store.dir').'cache/pdf/'.$classname.'_'.$this->BO->getID().'_'.$this->BO->getVersion().'.pdf';
129: $PDFDownloadName = str_replace(' ', '_', $this->BO->get('title').'.pdf');
130: $this->HTMLFilename = $config->get('app.file.store.dir').'cache/html/'.$classname.'_'.$this->BO->getID().'_'.$this->BO->getVersion().'.html';
131:
132:
133: if ($this->checkPDFCache()) {
134: return;
135: }
136:
137: if (method_exists($this->BO, 'getAttachmentsURL')) {
138: $attachURL = $this->BO->getAttachmentsURL();
139: } else {
140: $attachURL = '';
141: }
142:
143: if ($this->checkHTMLCache()) {
144: $this->loadHTMLCache();
145: } else {
146: $this->content = $this->markdown($this->BO->get('content', true), $attachURL);
147: $this->HTMLCache();
148: }
149:
150:
151: $attachments = array();
152: preg_match_all('/href\=\"\$attachURL\/.*\"/', $this->content, $attachments);
153:
154: foreach ($attachments[0] as $attachmentURL) {
155: $start = mb_strpos($attachmentURL, '/');
156: $end = mb_strrpos($attachmentURL, '"');
157: $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1));
158:
159: if (method_exists($this->BO, 'getAttachmentSecureURL')) {
160: $this->content = str_replace($attachmentURL, 'href='.$this->BO->getAttachmentSecureURL($fileName), $this->content);
161: }
162: }
163:
164:
165: $attachments = array();
166: preg_match_all('/\<img\ src\=\"\$attachURL\/.*\".*\>/', $this->content, $attachments);
167:
168: foreach ($attachments[0] as $attachmentURL) {
169: $start = mb_strpos($attachmentURL, '/');
170: $end = mb_strrpos($attachmentURL, '" alt');
171: $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1));
172:
173: if ($config->get('cms.images.widget')) {
174:
175: $path = $this->BO->getAttachmentsLocation().'/'.$fileName;
176: $image_details = getimagesize($path);
177: $imgType = $image_details[2];
178: if ($imgType == 1) {
179: $type = 'gif';
180: } elseif ($imgType == 2) {
181: $type = 'jpg';
182: } elseif ($imgType == 3) {
183: $type = 'png';
184: }
185:
186: $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (boolean) $config->get('cms.images.widget.secure'));
187: $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content);
188: } else {
189:
190: if (method_exists($this->BO, 'getAttachmentSecureURL')) {
191: $this->content = str_replace($attachmentURL, '<img src="'.$this->BO->getAttachmentSecureURL($fileName).'">', $this->content);
192: }
193: }
194: }
195:
196: $this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
197: $this->pdf->SetCreator(PDF_CREATOR);
198: $this->pdf->SetAuthor($this->BO->get('author'));
199: $this->pdf->SetTitle($this->BO->get('title'));
200: $this->pdf->SetSubject($this->BO->get('description'));
201:
202:
203: $this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
204: $this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
205: $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
206:
207:
208: $this->pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
209:
210:
211: $this->pdf->setImageScale(2.5);
212:
213:
214: $this->pdf->AddPage();
215:
216:
217: $title = '<h1>'.$this->BO->get('title').'</h1>';
218:
219: $footer = '<br><p>Article URL: <a href="'.$this->BO->get('URL').'">'.$this->BO->get('URL').'</a><br>Title: '.$this->BO->get('title').'<br>Author: '.$this->BO->get('author').'</p>';
220:
221:
222: self::$logger->debug('Writing the title ['.$title.'] to the PDF');
223: $this->pdf->writeHTML(utf8_encode($title), true, false, true, false, '');
224:
225: self::$logger->debug('Writing the content ['.$this->content.'] to the PDF');
226: $this->pdf->writeHTML(utf8_encode($this->content), true, false, true, false, '');
227:
228: $this->pdf->writeHTML(utf8_encode($footer), true, false, true, false, '');
229: self::$logger->debug('Writing the footer ['.$footer.'] to the PDF');
230:
231:
232: $this->pdf->Output($this->PDFFilename, 'F');
233:
234: self::$logger->debug('<<__construct()');
235: }
236:
237: 238: 239: 240: 241:
242: private function markdown($text, $attachURL = '')
243: {
244: $config = ConfigProvider::getInstance();
245:
246: 247: 248: 249:
250: static $parser;
251:
252: if (!isset($parser)) {
253: $parser = new \Alpha\Util\Extension\Markdown();
254: }
255:
256: 257: 258: 259:
260: $text = str_replace('$sysURL', $config->get('app.url'), $text);
261:
262:
263: return $parser->transform($text);
264: }
265:
266: 267: 268: 269: 270: 271: 272:
273: public function getContent()
274: {
275: return $this->content;
276: }
277:
278: 279: 280: 281: 282: 283: 284:
285: private function HTMLCache()
286: {
287:
288: if ($this->BO->getID() != '00000000000') {
289: $fp = fopen($this->HTMLFilename, 'w');
290: if (!$fp) {
291: throw new AlphaException('Failed to open the cache file for writing, directory permissions my not be set correctly!');
292: } else {
293: flock($fp, 2);
294: fwrite($fp, $this->content);
295: flock($fp, 3);
296: fclose($fp);
297: }
298: }
299: }
300:
301: 302: 303: 304: 305: 306: 307:
308: private function checkHTMLCache()
309: {
310: $config = ConfigProvider::getInstance();
311:
312: return file_exists($this->HTMLFilename);
313: }
314:
315: 316: 317: 318: 319: 320: 321:
322: private function loadHTMLCache()
323: {
324: $fp = fopen($this->HTMLFilename, 'r');
325:
326: if (!$fp) {
327: throw new AlphaException('Failed to open the cache file for reading, directory permissions my not be set correctly!', 'loadHTMLCache()');
328: } else {
329: $this->content = fread($fp, filesize($this->HTMLFilename));
330: fclose($fp);
331: }
332: }
333:
334: 335: 336: 337: 338: 339: 340:
341: private function checkPDFCache()
342: {
343: return file_exists($this->PDFFilename);
344: }
345:
346: 347: 348: 349: 350: 351:
352: private function serveCachedPDF($name)
353: {
354:
355: $handle = fopen($this->PDFFilename, 'r');
356: $data = fread($handle, filesize($this->PDFFilename));
357: fclose($handle);
358:
359: $filesize = strlen($data);
360: $mimetype = 'application/octet-stream';
361:
362:
363: header('Pragma: public');
364: header('Expires: 0');
365: header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
366: header('Cache-Control: private', false);
367: header('Content-Transfer-Encoding: binary');
368: header('Content-Type: '.$mimetype);
369: header('Content-Length: '.$filesize);
370: header('Content-Disposition: attachment; filename="'.$name.'";');
371:
372:
373: echo $data;
374: die();
375: }
376:
377: 378: 379: 380: 381: 382: 383:
384: public function getPDFData()
385: {
386: $PDFDownloadName = str_replace(' ', '-', $this->BO->get('title').'.pdf');
387:
388: $handle = fopen($this->PDFFilename, 'r');
389: $data = fread($handle, filesize($this->PDFFilename));
390: fclose($handle);
391:
392: return $data;
393: }
394: }
395: