1
1
<?php
2
+
2
3
/**
3
4
* @file
4
5
* Implements API to convert files to PDF or HTML through AdLib.
@@ -43,6 +44,24 @@ function os2web_adlib_api_menu() {
43
44
return $items;
44
45
}
45
46
47
+ /**
48
+ * Implements hook_FORM_alter().
49
+ */
50
+ function os2web_adlib_api_form_os2web_settings_settings_form_alter(&$form, &$form_state) {
51
+ // Adlib API configuration.
52
+
53
+ $form['os2web_adlib_api_config_group'] = array(
54
+ '#type' => 'fieldset',
55
+ '#title' => t('AdLib Configuration'),
56
+ );
57
+ $form['os2web_adlib_api_config_group']['os2web_adlib_api_allowed_ip_host'] = array(
58
+ '#type' => 'textfield',
59
+ '#title' => 'IP addresser der kan udløse AdLib konvertering',
60
+ '#description' => 'Komma separeret liste af ip-addresser/servernavne der kan anvende AdLib. Husk AdLib kan køres fra admin og fra cron.',
61
+ '#default_value' => variable_get('os2web_adlib_api_allowed_ip_host', ip_address()),
62
+ );
63
+ }
64
+
46
65
/**
47
66
* Ajax callback to update job status.
48
67
*
@@ -167,7 +186,7 @@ function _os2web_adlib_api_reprocess_nid($nid, &$context = array()) {
167
186
168
187
if (isset($node->field_enclosures['und'])) {
169
188
foreach ($node->field_enclosures['und'] as $file) {
170
- if (file_exists(drupal_realpath($file['uri'])) && !_os2web_adlib_api_is_file_pdf ($file['fid'])) {
189
+ if (file_exists(drupal_realpath($file['uri'])) && _os2web_adlib_api_is_filetype_approved ($file['fid'])) {
171
190
if (os2web_adlib_api_convert_to_pdf($file['fid'])) {
172
191
watchdog('adlib_api', 'readded fid to conversion queue:' . $file['fid'], NULL, WATCHDOG_DEBUG);
173
192
}
@@ -189,10 +208,22 @@ function os2web_adlib_api_status() {
189
208
drupal_goto('admin/config/os2web/adlibstatus');
190
209
}
191
210
if (isset($_GET['reset'])) {
211
+ $file = db_select('os2web_adlib_api_doc_files', 'df')
212
+ ->fields('df')
213
+ ->condition('fid', $_GET['reset'])
214
+ ->execute()
215
+ ->fetchObject();
216
+
192
217
$query = db_delete('os2web_adlib_api_doc_files');
193
218
$query = $query->condition('fid', $_GET['reset']);
194
219
$query->execute();
195
- os2web_adlib_api_convert_to_pdf($_GET['reset']);
220
+
221
+ if (is_object($file) && !empty($file->did)) {
222
+ os2web_adlib_api_convert_to_pdf($_GET['reset'], $file->did);
223
+ }
224
+ else {
225
+ os2web_adlib_api_convert_to_pdf($_GET['reset']);
226
+ }
196
227
drupal_goto('admin/config/os2web/adlibstatus');
197
228
}
198
229
if (isset($_GET['process'])) {
@@ -232,7 +263,7 @@ function os2web_adlib_api_status() {
232
263
);
233
264
}
234
265
}
235
- drupal_add_js(drupal_get_path('module', 'os2web_adlib_api') . '/js/ os2web_adlib_api.js', 'file');
266
+ drupal_add_js(drupal_get_path('module', 'os2web_adlib_api') . '/os2web_adlib_api.js', 'file');
236
267
$html = '<h2>Connector information</h2>';
237
268
$html .= theme('table', array('header' => $head, 'rows' => $rows));
238
269
$head = array('Fid', 'Job-id', 'Status', 'action');
@@ -298,15 +329,15 @@ function os2web_adlib_api_cron() {
298
329
_os2web_adlib_api_do_processing();
299
330
300
331
// Add set up queue for adlib requeueing.
301
- // $queue = DrupalQueue::get('os2web_adlib_reprocess');
302
- // if ($queue->numberOfItems() == 0) {
303
- // $items = _os2web_adlib_api_bulletpoint_nid();
304
- // $chunk_size = max(min((int) sqrt(count($items)), 25), 1);
305
- // $items = array_chunk($items, $chunk_size);
306
- // foreach ($items as $item) {
307
- // $queue->createItem($item);
308
- // }
309
- // }
332
+ $queue = DrupalQueue::get('os2web_adlib_reprocess');
333
+ if ($queue->numberOfItems() == 0) {
334
+ $items = _os2web_adlib_api_bulletpoint_nid();
335
+ $chunk_size = max(min((int) sqrt(count($items)), 25), 1);
336
+ $items = array_chunk($items, $chunk_size);
337
+ foreach ($items as $item) {
338
+ $queue->createItem($item);
339
+ }
340
+ }
310
341
}
311
342
312
343
/**
@@ -380,7 +411,7 @@ function os2web_adlib_api_convert_to_pdf($fid, $docref = NULL) {
380
411
// file_delete($file);
381
412
return FALSE;
382
413
}
383
- if (!_os2web_adlib_api_is_file_pdf ($fid)) {
414
+ if (_os2web_adlib_api_is_filetype_approved ($fid)) {
384
415
if (0 === db_query('SELECT fid from {os2web_adlib_api_doc_files} where fid = :fid AND jobtype = :jobtype', array(':fid' => $fid, ':jobtype' => 0))->rowCount()) {
385
416
$record = array(
386
417
'fid' => $fid,
@@ -417,9 +448,10 @@ function os2web_adlib_api_convert_to_pdf($fid, $docref = NULL) {
417
448
* Reference for the file
418
449
*
419
450
* @return int
420
- * File id
451
+ * File id, false if none.
421
452
*/
422
453
function os2web_adlib_api_get_fid($ref) {
454
+
423
455
$fid = db_query('SELECT fid FROM {os2web_adlib_api_doc_files} WHERE did=:did ORDER BY fid DESC', array(':did' => $ref))->fetchField();
424
456
if ($fid) {
425
457
return $fid;
@@ -447,14 +479,16 @@ function _os2web_adlib_api_get_instance() {
447
479
*/
448
480
function _os2web_adlib_api_do_processing() {
449
481
// Add set up queue for adlib processing.
482
+ //
483
+ // Disable auto queue of missing files
450
484
$queue = DrupalQueue::get('os2web_adlib_process');
451
485
if ($queue->numberOfItems() == 0) {
452
486
$items = db_select('os2web_adlib_api_doc_files', 'f')
453
487
->fields('f')
454
488
->condition(db_or()->condition('job_id', 'Complete', '!=')->isNull('job_id'))
455
489
->execute()
456
490
->fetchAll();
457
- $chunk_size = max(min((int) sqrt(count($items)), 5 ), 1);
491
+ $chunk_size = max(min((int) sqrt(count($items)), 25 ), 1);
458
492
$items = array_chunk($items, $chunk_size);
459
493
foreach ($items as $item) {
460
494
if ($item->job_id != 'Complete') {
@@ -516,6 +550,7 @@ function _os2web_adlib_api_process_file($file) {
516
550
$fdata = file_load($file->fid);
517
551
if (!file_exists(drupal_realpath($fdata->uri)) || file_uri_scheme($fdata->uri) == '' || $fdata->filemime == 'application/pdf') {
518
552
if ($fdata->filemime == 'application/pdf') {
553
+
519
554
$record = array(
520
555
'fid' => $fdata->fid,
521
556
'jobtype' => $fdata->jobtype,
@@ -702,7 +737,7 @@ function _os2web_adlib_api_process_html($data, $base_url) {
702
737
function _os2web_adlib_api_queue_file($file) {
703
738
$file->file = file_load($file->fid);
704
739
705
- if (is_file(drupal_realpath($file->file->uri)) && !_os2web_adlib_api_is_file_pdf ($file->file->fid)) {
740
+ if (is_file(drupal_realpath($file->file->uri)) && _os2web_adlib_api_is_filetype_approved ($file->file->fid)) {
706
741
$adlib = _os2web_adlib_api_get_instance();
707
742
$upload = $adlib->uploadData($file->file->filename, file_get_contents(drupal_realpath($file->file->uri)));
708
743
if ($upload) {
@@ -734,7 +769,7 @@ function _os2web_adlib_api_queue_file($file) {
734
769
}
735
770
}
736
771
else {
737
- if (_os2web_adlib_api_is_file_pdf ($file->file->fid)) {
772
+ if (!_os2web_adlib_api_is_filetype_approved ($file->file->fid)) {
738
773
$jobinfo = array(
739
774
'fid' => $file->fid,
740
775
'jobtype' => $file->jobtype,
@@ -753,27 +788,60 @@ function _os2web_adlib_api_queue_file($file) {
753
788
}
754
789
755
790
/**
756
- * Helper function, check if file is already PDF .
791
+ * Helper function, check if file type is correct .
757
792
* @access protected
758
793
*
759
794
* @param int $fid
760
795
* File id
761
796
*
762
- * @return boolean
763
- * True if file is pdf .
797
+ * @return bool
798
+ * True if file is approved for processing .
764
799
*/
765
- function _os2web_adlib_api_is_file_pdf($fid) {
800
+ function _os2web_adlib_api_is_filetype_approved($fid) {
801
+
802
+ // Filetypes which are approved for processing.
803
+ $approved_files = array(
804
+ 'docx',
805
+ 'docm',
806
+ 'doc',
807
+ 'dotx',
808
+ 'dotm',
809
+ 'xps',
810
+ 'rtf',
811
+ 'xlsx',
812
+ 'odt',
813
+ 'xlsm',
814
+ 'xlsb',
815
+ 'xls',
816
+ 'xltx',
817
+ 'xltm',
818
+ 'xlt',
819
+ 'xps',
820
+ 'ods',
821
+ 'wps',
822
+ 'pptx',
823
+ 'pptm',
824
+ 'ppt',
825
+ 'potx',
826
+ 'potm',
827
+ 'ppsx',
828
+ 'ppsm',
829
+ 'pps',
830
+ 'odp',
831
+ 'msg',
832
+ );
833
+
766
834
if (is_array($fid)) {
767
835
$result = array();
768
836
$files = file_load_multiple($fid);
769
837
foreach ($files as $file) {
770
- $result[$file->fid]['is_pdf'] = strcasecmp( pathinfo($file ->filename, PATHINFO_EXTENSION), 'pdf') === 0 ;
838
+ $result[$file->fid]['is_pdf'] = in_array(strtolower( pathinfo(file_load($fid) ->filename, PATHINFO_EXTENSION)), $approved_files) ;
771
839
$result[$file->fid]['file'] = $file;
772
840
}
773
841
return $result;
774
842
}
775
843
else {
776
- return strcasecmp( pathinfo(file_load($fid)->filename, PATHINFO_EXTENSION), 'pdf') === 0 ;
844
+ return in_array(strtolower( pathinfo(file_load($fid)->filename, PATHINFO_EXTENSION)), $approved_files) ;
777
845
}
778
846
}
779
847
0 commit comments