Skip to content

Commit 21a0b80

Browse files
committed
Merge develop from syddjurs legacy os2web repo.
1 parent d6fa49b commit 21a0b80

File tree

2 files changed

+95
-27
lines changed

2 files changed

+95
-27
lines changed

AdLibWS.class.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
define('ADLIB_JOBTYPE_PDF', 0);
99
define('ADLIB_JOBTYPE_HTML', 1);
10-
define('ADLIB_DISABLED', $_SERVER['SERVER_ADDR'] != '10.255.1.46');
11-
//define('ADLIB_DISABLED', TRUE);
10+
define('ADLIB_DISABLED', !in_array($_SERVER['SERVER_NAME'], explode(',', variable_get('os2web_adlib_api_allowed_ip_host'))) && !in_array(ip_address(), explode(',', variable_get('os2web_adlib_api_allowed_ip_host'))));
11+
1212

1313
/**
1414
* Description of AdLibWS
@@ -108,7 +108,7 @@ class AdLibWS {
108108
<JOB:SETTINGS>
109109
<JOB:HEADER ENABLED="Yes" TEXTRIGHT="&amp;[Page] of &amp;[Pages]" LAYER="Foreground" />
110110
<JOB:NATIVEAPPSETTINGS>
111-
<JOB:MSOUTLOOK CONVERSIONMODE="IFTS" />
111+
<JOB:MSOUTLOOK CONVERSIONMODE="IFTS" PROCESSATTACHMENTS="No"/>
112112
</JOB:NATIVEAPPSETTINGS>
113113
</JOB:SETTINGS>
114114
</JOB>
@@ -120,7 +120,7 @@ class AdLibWS {
120120
'!outfolder' => self::$basePath . $file_info['out_folder'],
121121
'!jobtype' => ($type == ADLIB_JOBTYPE_HTML ? 'HTML' : 'PDF'),
122122
));
123-
$result = simplexml_load_string($this->client->addJob(array('job_info' => $job_info, 'job_ticket' => $job_ticket))->AddJobResult);
123+
$result = simplexml_load_string($this->client->addJob(array('jobInfo' => $job_info, 'jobTicket' => $job_ticket))->AddJobResult);
124124
if (is_object($result) && isset($result->JobSettings['JobID'])) {
125125
return $result;
126126
}

os2web_adlib_api.module

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @file
45
* Implements API to convert files to PDF or HTML through AdLib.
@@ -43,6 +44,24 @@ function os2web_adlib_api_menu() {
4344
return $items;
4445
}
4546

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+
4665
/**
4766
* Ajax callback to update job status.
4867
*
@@ -167,7 +186,7 @@ function _os2web_adlib_api_reprocess_nid($nid, &$context = array()) {
167186

168187
if (isset($node->field_enclosures['und'])) {
169188
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'])) {
171190
if (os2web_adlib_api_convert_to_pdf($file['fid'])) {
172191
watchdog('adlib_api', 'readded fid to conversion queue:' . $file['fid'], NULL, WATCHDOG_DEBUG);
173192
}
@@ -189,10 +208,22 @@ function os2web_adlib_api_status() {
189208
drupal_goto('admin/config/os2web/adlibstatus');
190209
}
191210
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+
192217
$query = db_delete('os2web_adlib_api_doc_files');
193218
$query = $query->condition('fid', $_GET['reset']);
194219
$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+
}
196227
drupal_goto('admin/config/os2web/adlibstatus');
197228
}
198229
if (isset($_GET['process'])) {
@@ -232,7 +263,7 @@ function os2web_adlib_api_status() {
232263
);
233264
}
234265
}
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');
236267
$html = '<h2>Connector information</h2>';
237268
$html .= theme('table', array('header' => $head, 'rows' => $rows));
238269
$head = array('Fid', 'Job-id', 'Status', 'action');
@@ -298,15 +329,15 @@ function os2web_adlib_api_cron() {
298329
_os2web_adlib_api_do_processing();
299330

300331
// 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+
}
310341
}
311342

312343
/**
@@ -380,7 +411,7 @@ function os2web_adlib_api_convert_to_pdf($fid, $docref = NULL) {
380411
// file_delete($file);
381412
return FALSE;
382413
}
383-
if (!_os2web_adlib_api_is_file_pdf($fid)) {
414+
if (_os2web_adlib_api_is_filetype_approved($fid)) {
384415
if (0 === db_query('SELECT fid from {os2web_adlib_api_doc_files} where fid = :fid AND jobtype = :jobtype', array(':fid' => $fid, ':jobtype' => 0))->rowCount()) {
385416
$record = array(
386417
'fid' => $fid,
@@ -417,9 +448,10 @@ function os2web_adlib_api_convert_to_pdf($fid, $docref = NULL) {
417448
* Reference for the file
418449
*
419450
* @return int
420-
* File id
451+
* File id, false if none.
421452
*/
422453
function os2web_adlib_api_get_fid($ref) {
454+
423455
$fid = db_query('SELECT fid FROM {os2web_adlib_api_doc_files} WHERE did=:did ORDER BY fid DESC', array(':did' => $ref))->fetchField();
424456
if ($fid) {
425457
return $fid;
@@ -447,14 +479,16 @@ function _os2web_adlib_api_get_instance() {
447479
*/
448480
function _os2web_adlib_api_do_processing() {
449481
// Add set up queue for adlib processing.
482+
//
483+
// Disable auto queue of missing files
450484
$queue = DrupalQueue::get('os2web_adlib_process');
451485
if ($queue->numberOfItems() == 0) {
452486
$items = db_select('os2web_adlib_api_doc_files', 'f')
453487
->fields('f')
454488
->condition(db_or()->condition('job_id', 'Complete', '!=')->isNull('job_id'))
455489
->execute()
456490
->fetchAll();
457-
$chunk_size = max(min((int) sqrt(count($items)), 5), 1);
491+
$chunk_size = max(min((int) sqrt(count($items)), 25), 1);
458492
$items = array_chunk($items, $chunk_size);
459493
foreach ($items as $item) {
460494
if ($item->job_id != 'Complete') {
@@ -516,6 +550,7 @@ function _os2web_adlib_api_process_file($file) {
516550
$fdata = file_load($file->fid);
517551
if (!file_exists(drupal_realpath($fdata->uri)) || file_uri_scheme($fdata->uri) == '' || $fdata->filemime == 'application/pdf') {
518552
if ($fdata->filemime == 'application/pdf') {
553+
519554
$record = array(
520555
'fid' => $fdata->fid,
521556
'jobtype' => $fdata->jobtype,
@@ -702,7 +737,7 @@ function _os2web_adlib_api_process_html($data, $base_url) {
702737
function _os2web_adlib_api_queue_file($file) {
703738
$file->file = file_load($file->fid);
704739

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)) {
706741
$adlib = _os2web_adlib_api_get_instance();
707742
$upload = $adlib->uploadData($file->file->filename, file_get_contents(drupal_realpath($file->file->uri)));
708743
if ($upload) {
@@ -734,7 +769,7 @@ function _os2web_adlib_api_queue_file($file) {
734769
}
735770
}
736771
else {
737-
if (_os2web_adlib_api_is_file_pdf($file->file->fid)) {
772+
if (!_os2web_adlib_api_is_filetype_approved($file->file->fid)) {
738773
$jobinfo = array(
739774
'fid' => $file->fid,
740775
'jobtype' => $file->jobtype,
@@ -753,27 +788,60 @@ function _os2web_adlib_api_queue_file($file) {
753788
}
754789

755790
/**
756-
* Helper function, check if file is already PDF.
791+
* Helper function, check if file type is correct.
757792
* @access protected
758793
*
759794
* @param int $fid
760795
* File id
761796
*
762-
* @return boolean
763-
* True if file is pdf.
797+
* @return bool
798+
* True if file is approved for processing.
764799
*/
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+
766834
if (is_array($fid)) {
767835
$result = array();
768836
$files = file_load_multiple($fid);
769837
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);
771839
$result[$file->fid]['file'] = $file;
772840
}
773841
return $result;
774842
}
775843
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);
777845
}
778846
}
779847

0 commit comments

Comments
 (0)