1ab3808331165e5cc162294bdf3ce0448dbd48a0
[atutor.git] / mods / pdf_converter / pdf.php
1 <?php\r
2 /**\r
3 * @version $Id: pdf.php 4562 2006-08-18 23:26:32Z stingrey $\r
4 * @package Joomla\r
5 * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.\r
6 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php\r
7 * Joomla! is free software. This version may have been modified pursuant\r
8 * to the GNU General Public License, and as distributed it includes or\r
9 * is derivative of works licensed under the GNU General Public License or\r
10 * other free or open source software licenses.\r
11 * See COPYRIGHT.php for copyright notices and details.\r
12 * Created by Phil Taylor me@phil-taylor.com\r
13 * Support file to display PDF Text Only using class from - http://www.ros.co.nz/pdf/readme.pdf\r
14 * HTMLDoc is available from: http://www.easysw.com/htmldoc and needs installing on the server for better HTML to PDF conversion\r
15 **/\r
16 \r
17 // no direct access\r
18 defined( '_VALID_MOS' ) or die( 'Restricted access' );\r
19 \r
20 function dofreePDF() {\r
21         global $mosConfig_live_site, $mosConfig_sitename, $mosConfig_offset;\r
22         global $mainframe, $database, $my;\r
23         \r
24         $id             = intval( mosGetParam( $_REQUEST, 'id', 1 ) );\r
25         \r
26         $gid            = $my->gid;\r
27         $now            = _CURRENT_SERVER_TIME;\r
28         $nullDate       = $database->getNullDate();\r
29         \r
30         // query to check for state and access levels\r
31         $query = "SELECT a.*, cc.name AS category, s.name AS section, s.published AS sec_pub, cc.published AS cat_pub,"\r
32         . "\n  s.access AS sec_access, cc.access AS cat_access, s.id AS sec_id, cc.id as cat_id"\r
33         . "\n FROM #__content AS a"\r
34         . "\n LEFT JOIN #__categories AS cc ON cc.id = a.catid"\r
35         . "\n LEFT JOIN #__sections AS s ON s.id = cc.section AND s.scope = 'content'"\r
36         . "\n WHERE a.id = $id"\r
37         . "\n AND a.state = 1"\r
38         . "\n AND a.access <= $gid"\r
39         . "\n AND ( a.publish_up = '$nullDate' OR a.publish_up <= '$now' )"\r
40         . "\n AND ( a.publish_down = '$nullDate' OR a.publish_down >= '$now' )" \r
41         ;       \r
42         $database->setQuery( $query );\r
43         $row = NULL;\r
44         \r
45         if ( $database->loadObject( $row ) ) {\r
46                 /*\r
47                 * check whether category is published\r
48                 */\r
49                 if ( !$row->cat_pub && $row->catid ) {\r
50                         mosNotAuth();  \r
51                         return;\r
52                 }\r
53                 /*\r
54                 * check whether section is published\r
55                 */\r
56                 if ( !$row->sec_pub && $row->sectionid ) {\r
57                         mosNotAuth(); \r
58                         return;\r
59                 }\r
60                 /*\r
61                 * check whether category access level allows access\r
62                 */\r
63                 if ( ($row->cat_access > $gid) && $row->catid ) {\r
64                         mosNotAuth();  \r
65                         return;\r
66                 }\r
67                 /*\r
68                 * check whether section access level allows access\r
69                 */\r
70                 if ( ($row->sec_access > $gid) && $row->sectionid ) {\r
71                         mosNotAuth();  \r
72                         return;\r
73                 }\r
74                 \r
75                 include( 'includes/class.ezpdf.php' );\r
76         \r
77                 $params = new mosParameters( $row->attribs );   \r
78                 $params->def( 'author',         !$mainframe->getCfg( 'hideAuthor' ) );\r
79                 $params->def( 'createdate', !$mainframe->getCfg( 'hideCreateDate' ) );\r
80                 $params->def( 'modifydate', !$mainframe->getCfg( 'hideModifyDate' ) );\r
81                 \r
82                 $row->fulltext  = pdfCleaner( $row->fulltext );\r
83                 $row->introtext = pdfCleaner( $row->introtext );\r
84         \r
85                 $pdf = new Cezpdf( 'a4', 'P' );  //A4 Portrait\r
86                 $pdf -> ezSetCmMargins( 2, 1.5, 1, 1);\r
87                 $pdf->selectFont( './fonts/Helvetica.afm' ); //choose font\r
88         \r
89                 $all = $pdf->openObject();\r
90                 $pdf->saveState();\r
91                 $pdf->setStrokeColor( 0, 0, 0, 1 );\r
92         \r
93                 // footer\r
94                 $pdf->addText( 250, 822, 6, $mosConfig_sitename );\r
95                 $pdf->line( 10, 40, 578, 40 );\r
96                 $pdf->line( 10, 818, 578, 818 );\r
97                 $pdf->addText( 30, 34, 6, $mosConfig_live_site );\r
98                 $pdf->addText( 250, 34, 6, _PDF_POWERED );\r
99                 $pdf->addText( 450, 34, 6, _PDF_GENERATED .' '. date( 'j F, Y, H:i', time() + $mosConfig_offset * 60 * 60 ) );\r
100         \r
101                 $pdf->restoreState();\r
102                 $pdf->closeObject();\r
103                 $pdf->addObject( $all, 'all' );\r
104                 $pdf->ezSetDy( 30 );\r
105         \r
106                 $txt1 = $row->title;\r
107                 $pdf->ezText( $txt1, 14 );\r
108         \r
109                 $txt2 = AuthorDateLine( $row, $params );\r
110         \r
111                 $pdf->ezText( $txt2, 8 );\r
112                 \r
113                 $txt3 = $row->introtext ."\n". $row->fulltext;\r
114                 $pdf->ezText( $txt3, 10 );\r
115                 \r
116                 $pdf->ezStream();\r
117         } else {\r
118                 mosNotAuth();\r
119                 return;\r
120         }\r
121 }\r
122 \r
123 function decodeHTML( $string ) {\r
124         $string = strtr( $string, array_flip(get_html_translation_table( HTML_ENTITIES ) ) );\r
125         $string = preg_replace( "/&#([0-9]+);/me", "chr('\\1')", $string );\r
126         \r
127         return $string;\r
128 }\r
129 \r
130 function get_php_setting ($val ) {\r
131         $r = ( ini_get( $val ) == '1' ? 1 : 0 );\r
132         \r
133         return $r ? 'ON' : 'OFF';\r
134 }\r
135 \r
136 function pdfCleaner( $text ) {  \r
137         // Ugly but needed to get rid of all the stuff the PDF class cant handle\r
138         \r
139         $text = str_replace( '<p>',                     "\n\n",         $text );\r
140         $text = str_replace( '<P>',                     "\n\n",         $text );\r
141         $text = str_replace( '<br />',                  "\n",           $text );\r
142         $text = str_replace( '<br>',                    "\n",           $text );\r
143         $text = str_replace( '<BR />',                  "\n",           $text );\r
144         $text = str_replace( '<BR>',                    "\n",           $text );\r
145         $text = str_replace( '<li>',                    "\n - ",        $text );\r
146         $text = str_replace( '<LI>',                    "\n - ",        $text );\r
147         $text = str_replace( '{mosimage}',              '',             $text );\r
148         $text = str_replace( '{mospagebreak}',  '',                     $text );\r
149         \r
150         $text = strip_tags( $text );\r
151         $text = decodeHTML( $text );\r
152 \r
153         return $text;\r
154 }\r
155 \r
156 function AuthorDateLine( &$row, &$params ) {\r
157         global $database;\r
158         \r
159         $text = '';\r
160         \r
161         if ( $params->get( 'author' ) ) {\r
162                 // Display Author name\r
163                 \r
164                 //Find Author Name\r
165                 $users_rows = new mosUser( $database );\r
166                 $users_rows->load( $row->created_by );\r
167                 $row->author    = $users_rows->name;\r
168                 $row->usertype  = $users_rows->usertype;                \r
169                 \r
170                 if ($row->usertype == 'administrator' || $row->usertype == 'superadministrator') {\r
171                         $text .= "\n";\r
172                         $text .=  _WRITTEN_BY .' '. ( $row->created_by_alias ? $row->created_by_alias : $row->author );\r
173                 } else {\r
174                         $text .= "\n";\r
175                         $text .=  _AUTHOR_BY .' '. ( $row->created_by_alias ? $row->created_by_alias : $row->author );\r
176                 }\r
177         }\r
178         \r
179         if ( $params->get( 'createdate' ) && $params->get( 'author' ) ) {\r
180                 // Display Separator\r
181                 $text .= "\n";\r
182         }\r
183         \r
184         if ( $params->get( 'createdate' ) ) {\r
185                 // Display Created Date\r
186                 if ( intval( $row->created ) ) {\r
187                         $create_date    = mosFormatDate( $row->created );\r
188                         $text                   .= $create_date;\r
189                 }                               \r
190         }       \r
191         \r
192         if ( $params->get( 'modifydate' ) && ( $params->get( 'author' ) || $params->get( 'createdate' ) ) ) {\r
193                 // Display Separator\r
194                 $text .= "\n";\r
195         }\r
196         \r
197         if ( $params->get( 'modifydate' ) ) {\r
198                 // Display Modified Date\r
199                 if ( intval( $row->modified ) ) {\r
200                         $mod_date       = mosFormatDate( $row->modified );\r
201                         $text           .= _LAST_UPDATED .' '. $mod_date;\r
202                         \r
203                 }\r
204         }       \r
205         \r
206         $text .= "\n\n";\r
207 \r
208         return $text;\r
209 }\r
210 \r
211 dofreePDF();\r
212 ?>