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,23 @@ 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' => 'checkbox',
59
+ '#title' => 'Enable AdLib conversion from this site!',
60
+ '#default_value' => variable_get('os2web_adlib_api_is_allowed', 0),
61
+ );
62
+ }
63
+
46
64
/**
47
65
* Ajax callback to update job status.
48
66
*
@@ -167,7 +185,7 @@ function _os2web_adlib_api_reprocess_nid($nid, &$context = array()) {
167
185
168
186
if (isset($node->field_enclosures['und'])) {
169
187
foreach ($node->field_enclosures['und'] as $file) {
170
- if (file_exists(drupal_realpath($file['uri'])) && !_os2web_adlib_api_is_file_pdf ($file['fid'])) {
188
+ if (file_exists(drupal_realpath($file['uri'])) && _os2web_adlib_api_is_filetype_approved ($file['fid'])) {
171
189
if (os2web_adlib_api_convert_to_pdf($file['fid'])) {
172
190
watchdog('adlib_api', 'readded fid to conversion queue:' . $file['fid'], NULL, WATCHDOG_DEBUG);
173
191
}
@@ -189,10 +207,22 @@ function os2web_adlib_api_status() {
189
207
drupal_goto('admin/config/os2web/adlibstatus');
190
208
}
191
209
if (isset($_GET['reset'])) {
210
+ $file = db_select('os2web_adlib_api_doc_files', 'df')
211
+ ->fields('df')
212
+ ->condition('fid', $_GET['reset'])
213
+ ->execute()
214
+ ->fetchObject();
215
+
192
216
$query = db_delete('os2web_adlib_api_doc_files');
193
217
$query = $query->condition('fid', $_GET['reset']);
194
218
$query->execute();
195
- os2web_adlib_api_convert_to_pdf($_GET['reset']);
219
+
220
+ if (is_object($file) && !empty($file->did)) {
221
+ os2web_adlib_api_convert_to_pdf($_GET['reset'], $file->did);
222
+ }
223
+ else {
224
+ os2web_adlib_api_convert_to_pdf($_GET['reset']);
225
+ }
196
226
drupal_goto('admin/config/os2web/adlibstatus');
197
227
}
198
228
if (isset($_GET['process'])) {
@@ -232,7 +262,7 @@ function os2web_adlib_api_status() {
232
262
);
233
263
}
234
264
}
235
- drupal_add_js(drupal_get_path('module', 'os2web_adlib_api') . '/js/ os2web_adlib_api.js', 'file');
265
+ drupal_add_js(drupal_get_path('module', 'os2web_adlib_api') . '/os2web_adlib_api.js', 'file');
236
266
$html = '<h2>Connector information</h2>';
237
267
$html .= theme('table', array('header' => $head, 'rows' => $rows));
238
268
$head = array('Fid', 'Job-id', 'Status', 'action');
@@ -298,15 +328,15 @@ function os2web_adlib_api_cron() {
298
328
_os2web_adlib_api_do_processing();
299
329
300
330
// 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
- // }
331
+ $queue = DrupalQueue::get('os2web_adlib_reprocess');
332
+ if ($queue->numberOfItems() == 0) {
333
+ $items = _os2web_adlib_api_bulletpoint_nid();
334
+ $chunk_size = max(min((int) sqrt(count($items)), 25), 1);
335
+ $items = array_chunk($items, $chunk_size);
336
+ foreach ($items as $item) {
337
+ $queue->createItem($item);
338
+ }
339
+ }
310
340
}
311
341
312
342
/**
@@ -380,7 +410,7 @@ function os2web_adlib_api_convert_to_pdf($fid, $docref = NULL) {
380
410
// file_delete($file);
381
411
return FALSE;
382
412
}
383
- if (!_os2web_adlib_api_is_file_pdf ($fid)) {
413
+ if (_os2web_adlib_api_is_filetype_approved ($fid)) {
384
414
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
415
$record = array(
386
416
'fid' => $fid,
@@ -417,10 +447,15 @@ function os2web_adlib_api_convert_to_pdf($fid, $docref = NULL) {
417
447
* Reference for the file
418
448
*
419
449
* @return int
420
- * File id
450
+ * File id, false if none.
421
451
*/
422
452
function os2web_adlib_api_get_fid($ref) {
423
- return (int) db_query('SELECT fid FROM {os2web_adlib_api_doc_files} WHERE did=:did', array(':did' => $ref))->fetchField();
453
+
454
+ $fid = db_query('SELECT fid FROM {os2web_adlib_api_doc_files} WHERE did=:did ORDER BY fid DESC', array(':did' => $ref))->fetchField();
455
+ if ($fid) {
456
+ return $fid;
457
+ }
458
+ return false;
424
459
}
425
460
426
461
/**
@@ -443,14 +478,16 @@ function _os2web_adlib_api_get_instance() {
443
478
*/
444
479
function _os2web_adlib_api_do_processing() {
445
480
// Add set up queue for adlib processing.
481
+ //
482
+ // Disable auto queue of missing files
446
483
$queue = DrupalQueue::get('os2web_adlib_process');
447
484
if ($queue->numberOfItems() == 0) {
448
485
$items = db_select('os2web_adlib_api_doc_files', 'f')
449
486
->fields('f')
450
487
->condition(db_or()->condition('job_id', 'Complete', '!=')->isNull('job_id'))
451
488
->execute()
452
489
->fetchAll();
453
- $chunk_size = max(min((int) sqrt(count($items)), 5 ), 1);
490
+ $chunk_size = max(min((int) sqrt(count($items)), 25 ), 1);
454
491
$items = array_chunk($items, $chunk_size);
455
492
foreach ($items as $item) {
456
493
if ($item->job_id != 'Complete') {
@@ -512,6 +549,7 @@ function _os2web_adlib_api_process_file($file) {
512
549
$fdata = file_load($file->fid);
513
550
if (!file_exists(drupal_realpath($fdata->uri)) || file_uri_scheme($fdata->uri) == '' || $fdata->filemime == 'application/pdf') {
514
551
if ($fdata->filemime == 'application/pdf') {
552
+
515
553
$record = array(
516
554
'fid' => $fdata->fid,
517
555
'jobtype' => $fdata->jobtype,
@@ -698,7 +736,7 @@ function _os2web_adlib_api_process_html($data, $base_url) {
698
736
function _os2web_adlib_api_queue_file($file) {
699
737
$file->file = file_load($file->fid);
700
738
701
- if (is_file(drupal_realpath($file->file->uri)) && !_os2web_adlib_api_is_file_pdf ($file->file->fid)) {
739
+ if (is_file(drupal_realpath($file->file->uri)) && _os2web_adlib_api_is_filetype_approved ($file->file->fid)) {
702
740
$adlib = _os2web_adlib_api_get_instance();
703
741
$upload = $adlib->uploadData($file->file->filename, file_get_contents(drupal_realpath($file->file->uri)));
704
742
if ($upload) {
@@ -730,7 +768,7 @@ function _os2web_adlib_api_queue_file($file) {
730
768
}
731
769
}
732
770
else {
733
- if (_os2web_adlib_api_is_file_pdf ($file->file->fid)) {
771
+ if (!_os2web_adlib_api_is_filetype_approved ($file->file->fid)) {
734
772
$jobinfo = array(
735
773
'fid' => $file->fid,
736
774
'jobtype' => $file->jobtype,
@@ -749,27 +787,60 @@ function _os2web_adlib_api_queue_file($file) {
749
787
}
750
788
751
789
/**
752
- * Helper function, check if file is already PDF .
790
+ * Helper function, check if file type is correct .
753
791
* @access protected
754
792
*
755
793
* @param int $fid
756
794
* File id
757
795
*
758
- * @return boolean
759
- * True if file is pdf .
796
+ * @return bool
797
+ * True if file is approved for processing .
760
798
*/
761
- function _os2web_adlib_api_is_file_pdf($fid) {
799
+ function _os2web_adlib_api_is_filetype_approved($fid) {
800
+
801
+ // Filetypes which are approved for processing.
802
+ $approved_files = array(
803
+ 'docx',
804
+ 'docm',
805
+ 'doc',
806
+ 'dotx',
807
+ 'dotm',
808
+ 'xps',
809
+ 'rtf',
810
+ 'xlsx',
811
+ 'odt',
812
+ 'xlsm',
813
+ 'xlsb',
814
+ 'xls',
815
+ 'xltx',
816
+ 'xltm',
817
+ 'xlt',
818
+ 'xps',
819
+ 'ods',
820
+ 'wps',
821
+ 'pptx',
822
+ 'pptm',
823
+ 'ppt',
824
+ 'potx',
825
+ 'potm',
826
+ 'ppsx',
827
+ 'ppsm',
828
+ 'pps',
829
+ 'odp',
830
+ 'msg',
831
+ );
832
+
762
833
if (is_array($fid)) {
763
834
$result = array();
764
835
$files = file_load_multiple($fid);
765
836
foreach ($files as $file) {
766
- $result[$file->fid]['is_pdf'] = strcasecmp( pathinfo($file ->filename, PATHINFO_EXTENSION), 'pdf') === 0 ;
837
+ $result[$file->fid]['is_pdf'] = in_array(strtolower( pathinfo(file_load($fid) ->filename, PATHINFO_EXTENSION)), $approved_files) ;
767
838
$result[$file->fid]['file'] = $file;
768
839
}
769
840
return $result;
770
841
}
771
842
else {
772
- return strcasecmp( pathinfo(file_load($fid)->filename, PATHINFO_EXTENSION), 'pdf') === 0 ;
843
+ return in_array(strtolower( pathinfo(file_load($fid)->filename, PATHINFO_EXTENSION)), $approved_files) ;
773
844
}
774
845
}
775
846
@@ -819,3 +890,19 @@ function os2web_adlib_api_destroy_job($job_id) {
819
890
watchdog('adlib_api', 'Deleted job for %fid', array('%fid' => $job_id));
820
891
}
821
892
}
893
+
894
+ /**
895
+ * Implements hook_os2web_help().
896
+ */
897
+ function os2web_adlib_api_os2web_help($sections) {
898
+
899
+ // List of content.
900
+ $sections['list_of_content'] = t('<a href="#os2web_adlib_api">Adlib file conversion</a><br />');
901
+
902
+ // Module specific.
903
+ $sections['os2web_adlib_api'] = t('<h2 id="os2web_adlib_api">Adlib file conversion:</h2>');
904
+ $sections['os2web_adlib_api'] .= t('<p><b>Description:</b><br />Adlib is a file conversion unit on your network. This module is often used to convert misc files to either .html or .pdf files.<br />See the status of the current queue of files at the <a href="@status" target="_blank">adlib status page</a>.<br />You can requeue files for conversion at the <a href="@requeue" target="_blank">requeue page</a>.</p>', array('@status' => url('admin/config/os2web/adlibstatus'), '@requeue' => url('admin/config/os2web/reprocess')));
905
+ $sections['os2web_adlib_api'] .= t('<p><b>Configuration:</b><br /> Setup your AdLib server at the <a href="@settings" target="_blank">OS2web Settings Page</a>. Enter the AdLib service endpoint address.</p>', array('@settings' => url('admin/config/os2web/adlibstatus')));
906
+
907
+ return $sections;
908
+ }
0 commit comments