Convert to local file access only for TCPDF and dompdf
[project/print.git] / print_pdf / print_pdf.admin.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Contains the administrative functions of the PDF version module.
7 *
8 * This file is included by the PDF version module, and includes the
9 * settings form.
10 *
11 * @ingroup print
12 */
13
14 /**
15 * Menu callback for the PDF version module settings form.
16 *
17 * @ingroup forms
18 * @see _print_pdf_tools()
19 */
20 function print_pdf_settings() {
21 $pdf_tools = _print_pdf_tools();
22
23 if ($pdf_tools != -1) {
24 $form['settings'] = array(
25 '#type' => 'fieldset',
26 '#title' => t('PDF options'),
27 );
28
29 $form['settings']['print_pdf_pdf_tool'] = array(
30 '#type' => 'radios',
31 '#title' => t('PDF generation tool'),
32 '#options' => $pdf_tools,
33 '#default_value' => variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT),
34 '#description' => t('This option selects the PDF generation tool being used by this module to create the PDF version.'),
35 );
36
37 $form['settings']['print_pdf_link_pos'] = array(
38 '#type' => 'checkboxes',
39 '#title' => t('PDF version link'),
40 '#default_value' => variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT)),
41 '#options' => array('link' => t('Links area'), 'corner' => t('Content corner')),
42 '#description' => t('Choose the location of the link(s) to the PDF version. The Links area is usually below the node content, whereas the Content corner is placed in the upper-right corner of the node content. Unselect all options to disable the link. Even if the link is disabled, you can still view the PDF version of a node by going to !path/nid where nid is the numeric id of the node.', array('!path' => PRINTPDF_PATH)),
43 );
44
45 $form['settings']['print_pdf_link_teaser'] = array(
46 '#type' => 'checkbox',
47 '#title' => t('Display link to the PDF version in teaser'),
48 '#default_value' => variable_get('print_pdf_link_teaser', PRINT_PDF_LINK_TEASER_DEFAULT),
49 '#description' => t('Enabling this will display the link in teaser mode.'),
50 );
51
52 $form['settings']['adv_link'] = array(
53 '#type' => 'fieldset',
54 '#title' => t('Advanced link options'),
55 '#collapsible' => TRUE,
56 '#collapsed' => FALSE,
57 );
58
59 $form['settings']['adv_link']['print_pdf_show_link'] = array(
60 '#type' => 'radios',
61 '#title' => t('Link style'),
62 '#default_value' => variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT),
63 '#options' => array(1 => t('Text only'), 2 => t('Icon only'), 3 => t('Icon and Text')),
64 '#description' => t('Select the visual style of the link.'),
65 );
66
67 $form['settings']['adv_link']['print_pdf_link_use_alias'] = array(
68 '#type' => 'checkbox',
69 '#title' => t('Use URL alias instead of node ID'),
70 '#default_value' => variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT),
71 '#description' => t('Enabling this will create the link using the URL alias instead of the node ID.'),
72 );
73
74 $form['settings']['adv_link']['print_pdf_link_class'] = array(
75 '#type' => 'textfield',
76 '#title' => t('Link class'),
77 '#default_value' => variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT),
78 '#size' => 60,
79 '#maxlength' => 250,
80 '#description' => t('This can be used by themers to change the link style or by jQuery modules to open in a new window (e.g. greybox or thickbox). Multiple classes can be specified, separated by spaces.'),
81 );
82
83 $form['settings']['adv_link']['print_pdf_node_link_visibility'] = array(
84 '#type' => 'radios',
85 '#title' => t('Link visibility'),
86 '#default_value' => variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT),
87 '#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')),
88 );
89
90 $form['settings']['adv_link']['print_pdf_node_link_pages'] = array(
91 '#type' => 'textarea',
92 '#default_value' => variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT),
93 '#rows' => 3,
94 '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
95 );
96
97 $access = user_access('use PHP for link visibility');
98
99 if ($form['settings']['adv_link']['print_pdf_node_link_visibility']['#default_value'] == 2 && !$access) {
100 $form['settings']['adv_link']['print_pdf_node_link_visibility'] = array('#type' => 'value', '#value' => 2);
101 $form['settings']['adv_link']['print_pdf_node_link_pages'] = array('#type' => 'value', '#value' => $form['settings']['adv_link']['print_pdf_node_link_pages']['#default_value']);
102 }
103 elseif ($access) {
104 $form['settings']['adv_link']['print_pdf_node_link_visibility']['#options'][] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
105 $form['settings']['adv_link']['print_pdf_node_link_pages']['#description'] .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
106 }
107
108 $form['settings']['adv_link']['print_pdf_sys_link_visibility'] = array(
109 '#type' => 'radios',
110 '#title' => t('Show link in system (non-content) pages'),
111 '#default_value' => variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT),
112 '#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')),
113 );
114
115 $form['settings']['adv_link']['print_pdf_sys_link_pages'] = array(
116 '#type' => 'textarea',
117 '#default_value' => variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT),
118 '#rows' => 3,
119 '#description' => t('Setting this option will add a PDF version page link on pages created by Drupal or the enabled modules.') .'<br />'.
120 t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
121 );
122
123 if ($form['settings']['adv_link']['print_pdf_sys_link_visibility']['#default_value'] == 2 && !$access) {
124 $form['settings']['adv_link']['print_pdf_sys_link_visibility'] = array('#type' => 'value', '#value' => 2);
125 $form['settings']['adv_link']['print_pdf_sys_link_pages'] = array('#type' => 'value', '#value' => $form['settings']['adv_link']['print_pdf_sys_link_pages']['#default_value']);
126 }
127 elseif ($access) {
128 $form['settings']['adv_link']['print_pdf_sys_link_visibility']['#options'][] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
129 $form['settings']['adv_link']['print_pdf_sys_link_pages']['#description'] .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
130 }
131
132 $form['settings']['adv_link']['print_pdf_book_link'] = array(
133 '#type' => 'radios',
134 '#title' => t('Link in book hierarchy nodes'),
135 '#default_value' => variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT),
136 '#options' => array(t('No link'), t('Current page and sub-pages'), t('Current page only')),
137 );
138
139 $form['settings']['print_pdf_content_disposition'] = array(
140 '#type' => 'radios',
141 '#title' => t('Open PDF in'),
142 '#options' => array(t('Same browser window'), t('New browser window'), t('Save dialog')),
143 '#default_value' => variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT),
144 '#description' => t("Select the desired method for opening the PDF in the user's browser."),
145 );
146
147 $form['settings']['print_pdf_paper_size'] = array(
148 '#type' => 'select',
149 '#title' => t('Paper size'),
150 '#options' => array('4A0' => '4A0', '2A0' => '2A0', 'A0' => 'A0',
151 'A1' => 'A1', 'A2' => 'A2', 'A3' => 'A3', 'A4' => 'A4',
152 'A5' => 'A5', 'A6' => 'A6', 'A7' => 'A7', 'A8' => 'A8',
153 'A9' => 'A9', 'A10' => 'A10', 'B0' => 'B0', 'B1' => 'B1',
154 'B2' => 'B2', 'B3' => 'B3', 'B4' => 'B4', 'B5' => 'B5',
155 'B6' => 'B6', 'B7' => 'B7', 'B8' => 'B8', 'B9' => 'B9',
156 'B10' => 'B10', 'C0' => 'C0', 'C1' => 'C1', 'C2' => 'C2',
157 'C3' => 'C3', 'C4' => 'C4', 'C5' => 'C5', 'C6' => 'C6',
158 'C7' => 'C7', 'C8' => 'C8', 'C9' => 'C9', 'C10' => 'C10',
159 'RA0' => 'RA0', 'RA1' => 'RA1', 'RA2' => 'RA2',
160 'RA3' => 'RA3', 'RA4' => 'RA4', 'SRA0' => 'SRA0',
161 'SRA1' => 'SRA1', 'SRA2' => 'SRA2', 'SRA3' => 'SRA3',
162 'SRA4' => 'SRA4', 'LETTER' => 'Letter', 'LEGAL' => 'Legal',
163 'EXECUTIVE' => 'Executive', 'FOLIO' => 'Folio',
164 ),
165 '#default_value' => variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT),
166 '#description' => t('Choose the paper size of the generated PDF.'),
167 );
168
169 $form['settings']['print_pdf_page_orientation'] = array(
170 '#type' => 'select',
171 '#title' => t('Page orientation'),
172 '#options' => array('portrait' => t('Portrait'), 'landscape' => t('Landscape')),
173 '#default_value' => variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT),
174 '#description' => t('Choose the page orientation of the generated PDF.'),
175 );
176 $form['settings']['print_pdf_autoconfig'] = array(
177 '#type' => 'checkbox',
178 '#title' => t('Auto-configure the PDF tool settings'),
179 '#default_value' => variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT),
180 '#description' => t('If you disable this option, the pdf tool settings must be configured manually. For TCDPF, edit the tcpdf/config/tcpdf_config.php file. For dompdf, edit the dompdf/dompdf_config.inc.php file.'),
181 );
182 $form['settings']['print_pdf_font_family'] = array(
183 '#type' => 'textfield',
184 '#title' => t('Font Family'),
185 '#default_value' => variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT),
186 '#size' => 60,
187 '#maxlength' => 250,
188 '#description' => t('(TCPDF only) Set the font family to be used. Examples: %examples.', array('%examples' => 'helvetica, times, courier, dejavusans, dejavuserif, freesans, freeserif, freemono')) .'<br />'.
189 t("CAUTION: TCPDF embeds the complete font in the generated PDF. If you're not using Unicode, then helvetica or times are safe choices that will keep the PDF small. Unicode fonts can increase the size of the PDF to the 1MB region."),
190 );
191 $form['settings']['print_pdf_font_size'] = array(
192 '#type' => 'textfield',
193 '#title' => t('Font Size'),
194 '#default_value' => variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT),
195 '#size' => 2,
196 '#maxlength' => 3,
197 '#description' => t('(TCPDF only) Set the font size to be used for normal text. This is the base value for the scaling applied to other text styles.'),
198 );
199 $form['settings']['print_pdf_dompdf_unicode'] = array(
200 '#type' => 'checkbox',
201 '#title' => t("Use dompdf's Unicode Mode"),
202 '#default_value' => variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT),
203 '#description' => t("If enabled, dompdf's Unicode mode is used. If not, the module will attempt to convert some non-ASCII chars to ISO-8859-1."),
204 );
205 $form['settings']['print_pdf_wkhtmltopdf_options'] = array(
206 '#type' => 'textfield',
207 '#title' => t('wkhtmltopdf options'),
208 '#size' => 60,
209 '#maxlength' => 250,
210 '#default_value' => variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS),
211 '#description' => t('(wkhtmltopdf only) Set any additional options to be passed to the wkhtmltopdf executable. Tokens may be used in these options (see list below).'),
212 );
213
214 if (module_exists('token')) {
215 $form['settings']['print_pdf_filename'] = array(
216 '#type' => 'textfield',
217 '#title' => t('PDF File Name'),
218 '#default_value' => variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT),
219 '#description' => t("If left empty the generated filename defaults to the node's path. Tokens may be used to build the filename (see following list). The .pdf extension will be appended automatically."),
220 );
221 $form['settings']['print_pdf_filename_patterns'] = array(
222 '#type' => 'fieldset',
223 '#title' => t('Replacement Patterns'),
224 '#collapsible' => TRUE,
225 '#collapsed' => TRUE,
226 );
227 $descriptions = '';
228 $tokens = token_get_list('node');
229 foreach ($tokens as $token) {
230 foreach ($token as $key => $val) {
231 $descriptions .= '<dt>['. $key .']</dt><dd>'. $val ."</dd>\n";
232 }
233 }
234 $form['settings']['print_pdf_filename_patterns']['descriptions'] = array(
235 '#type' => 'markup',
236 '#value' => "<dl>\n". $descriptions ."</dl>\n",
237 );
238 }
239 $form['#validate']['_print_pdf_settings_validate'] = array();
240 }
241 else {
242 $form['settings'] = array(
243 '#type' => 'markup',
244 '#value' => '<p>'. t("No PDF generation tool found! Please dowload a supported PHP PDF generation tool. Check this module's INSTALL.txt for more details.") .'</p>',
245 );
246 }
247
248 return system_settings_form($form);
249 }
250
251 /**
252 * Validate print_pdf_settings form.
253 */
254 function _print_pdf_settings_validate($form_id, $form_values, $form) {
255 if (empty($form_values['print_pdf_pdf_tool'])) {
256 form_set_error('print_pdf_pdf_tool', t("No PDF tool selected"));
257 }
258 if ($form_values['print_pdf_font_size'] < 1) {
259 form_set_error('print_pdf_font_size', t("Font size must be at least 1."));
260 }
261 }
262
263 /**
264 * Auxiliary function to locate suitable PDF generation tools
265 *
266 * @return
267 * array of filenames with the include-able PHP file of the located tools
268 */
269 function _print_pdf_tools() {
270 $tools = array_keys(file_scan_directory(drupal_get_path('module', 'print'), '^dompdf_config.inc.php$'));
271 $tools = array_merge($tools, array_keys(file_scan_directory(drupal_get_path('module', 'print'), '^tcpdf.php$')));
272 $tools = array_merge($tools, array_keys(file_scan_directory(drupal_get_path('module', 'print'), '^wkhtmltopdf')));
273 $tools = array_merge($tools, array_keys(file_scan_directory(PRINT_PDF_LIB_PATH, '^dompdf_config.inc.php$')));
274 $tools = array_merge($tools, array_keys(file_scan_directory(PRINT_PDF_LIB_PATH, '^tcpdf.php$')));
275 $tools = array_merge($tools, array_keys(file_scan_directory(PRINT_PDF_LIB_PATH, '^wkhtmltopdf')));
276
277 $num_tools = count($tools);
278
279 if ($num_tools == 0) {
280 variable_set('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
281 return -1;
282 }
283 else {
284 // Instead of array_combine(), use this to maintain PHP4 compatibility
285 $tools2 = array();
286 foreach ($tools as $key => $val) {
287 $tools2[$val] = $val;
288 }
289
290 return $tools2;
291 }
292 }
293
294 /**
295 * Menu callback for the PDF version module text strings settings form.
296 *
297 * @ingroup forms
298 */
299 function print_pdf_strings_settings() {
300 drupal_set_message(t("Saving these strings will disable their translation via Drupal's language system. Use the reset button to return them to the original state."), 'warning');
301
302 $form['print_pdf_text'] = array(
303 '#type' => 'fieldset',
304 '#title' => t('Text strings'),
305 );
306
307 $form['print_pdf_text']['print_pdf_link_text'] = array(
308 '#type' => 'textfield',
309 '#title' => t('Link text'),
310 '#default_value' => variable_get('print_pdf_link_text', t('PDF version')),
311 '#description' => t('Text used in the link to the PDF version.'),
312 );
313
314 return system_settings_form($form);
315 }