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