-
Notifications
You must be signed in to change notification settings - Fork 2
/
os2web_adlib_api.module
918 lines (859 loc) · 30.2 KB
/
os2web_adlib_api.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
<?php
/**
* @file
* Implements API to convert files to PDF or HTML through AdLib.
*/
define('ADLIB_TEST_FID', FALSE);
define('ADLIB_HTML_IMG_FOLDER', 'public://bpimg/');
/**
* Implements hook_menu().
*/
function os2web_adlib_api_menu() {
// Added fan for requeueing Adlib jobs.
$items['admin/config/os2web/reprocess'] = array(
'title' => 'AdLib requeue',
'description' => 'Requeue AdLib jobs.',
'access arguments' => array('administer os2web'),
'page callback' => 'drupal_get_form',
'page arguments' => array('os2web_adlib_api_reprocess_form'),
'type' => MENU_LOCAL_TASK,
'access callback' => TRUE,
);
$items['admin/config/os2web/adlibstatus'] = array(
'title' => 'AdLib Status',
'description' => 'Provides an overview of known AdLib jobs.',
'access arguments' => array('administer os2web'),
'page callback' => 'os2web_adlib_api_status',
'type' => MENU_LOCAL_TASK,
);
$items['os2web/adlib/ajax/status/%'] = array(
'page callback' => 'os2web_adlib_api_status_ajax',
'page arguments' => array(4),
'type' => MENU_CALLBACK,
'access arguments' => array('administer os2web'),
);
$items['os2web/adlib/test'] = array(
'title' => 'AdLib test page',
'access arguments' => array('administer os2web'),
'page callback' => 'os2web_adlib_api_page',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_FORM_alter().
*/
function os2web_adlib_api_form_os2web_settings_settings_form_alter(&$form, &$form_state) {
// Adlib API configuration.
$form['os2web_adlib_api_config_group'] = array(
'#type' => 'fieldset',
'#title' => t('AdLib Configuration'),
);
$form['os2web_adlib_api_config_group']['os2web_adlib_api_allowed_ip_host'] = array(
'#type' => 'checkbox',
'#title' => 'Enable AdLib conversion from this site!',
'#default_value' => variable_get('os2web_adlib_api_is_allowed', 0),
);
}
/**
* Ajax callback to update job status.
*
* @param int $fid
* File identifier.
*/
function os2web_adlib_api_status_ajax($fid) {
$file = db_select('os2web_adlib_api_doc_files', 'df')
->fields('df')
->condition('fid', $fid)
->execute()
->fetchObject();
if (is_object($file)) {
if (!in_array($file->job_id, array('', 'Complete', 'Requeue'))) {
$adlib = _os2web_adlib_api_get_instance();
$status = $adlib->getJobStatus($file->job_id);
drupal_json_output($status->status);
}
else {
drupal_json_output('Not queued.');
}
}
else {
drupal_json_output('Not found.');
}
}
/**
* Implements hook_form().
*
* Provides form for user to start reprocessing.
*/
function os2web_adlib_api_reprocess_form() {
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Re-konverter importerede dokumenter',
);
return $form;
}
/**
* Implements hook_form_submit().
*
* Starts reprocessing on submit
*/
function os2web_adlib_api_reprocess_form_submit() {
os2web_adlib_api_reprocess();
}
/**
* Sets up and starts batchjob.
*/
function os2web_adlib_api_reprocess() {
$ids = _os2web_adlib_api_bulletpoint_nid();
foreach ($ids as $nodeid) {
$operations[] = array('_os2web_adlib_api_reprocess_nid', array($nodeid));
}
$batch = array(
'operations' => $operations,
'title' => 'Re-konverter adlib dokumenter',
'init_message' => t('initializing'),
'progress_message' => t('finished @current of @total'),
'error_message' => t('something went wrong'),
'finished' => '_os2web_adlib_api_bulletpoint_finished',
'file' => drupal_get_path('module', 'os2web_adlib_api') . '/os2web_adlib_api.module',
);
batch_set($batch);
batch_process();
}
/**
* Provides userfeedback when batchjob is finished.
* @author Thomas Thune Hansen <[email protected]>
*/
function _os2web_adlib_api_bulletpoint_finished($success, $results, $operations) {
error_log("reschedule finished");
drupal_set_message(t('All bullet points have been rescheduled for conversion'));
}
/**
* Fetch all nid's for bulletspoints.
* @author Thomas Thune Hansen <[email protected]>
*
* @return array
* List of bulletpoint nids
*/
function _os2web_adlib_api_bulletpoint_nid() {
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'bullet_point')
->execute();
$nodeid = array();
// Entity query returns an object containing an array, containing arrays...
foreach ($result as $item) {
foreach ($item as $key => $value) {
$nodeid[] = $key;
}
}
return $nodeid;
}
/**
* Worker function, adds fid's attached to nid's to adlib queue.
* @author Thomas Thune Hansen <[email protected]>
*
* @param array $nid
* List of bulletpoint nids
* @param array $context
* Context information
*/
function _os2web_adlib_api_reprocess_nid($nid, &$context = array()) {
if (is_array($nid)) {
foreach ($nid as $n) {
_os2web_adlib_api_reprocess_nid($n);
}
return;
}
$node = node_load($nid);
if (isset($node->field_enclosures['und'])) {
foreach ($node->field_enclosures['und'] as $file) {
if (file_exists(drupal_realpath($file['uri'])) && _os2web_adlib_api_is_filetype_approved($file['fid'])) {
if (os2web_adlib_api_convert_to_pdf($file['fid'])) {
watchdog('adlib_api', 'readded fid to conversion queue:' . $file['fid'], NULL, WATCHDOG_DEBUG);
}
}
}
}
}
/**
* Provides an overview of known AdLib jobs.
*/
function os2web_adlib_api_status() {
if (isset($_GET['delete'])) {
if (is_numeric($_GET['delete'])) {
$query = db_delete('os2web_adlib_api_doc_files');
$query = $query->condition('fid', $_GET['delete']);
$query->execute();
}
drupal_goto('admin/config/os2web/adlibstatus');
}
if (isset($_GET['reset'])) {
$file = db_select('os2web_adlib_api_doc_files', 'df')
->fields('df')
->condition('fid', $_GET['reset'])
->execute()
->fetchObject();
$query = db_delete('os2web_adlib_api_doc_files');
$query = $query->condition('fid', $_GET['reset']);
$query->execute();
if (is_object($file) && !empty($file->did)) {
os2web_adlib_api_convert_to_pdf($_GET['reset'], $file->did);
}
else {
os2web_adlib_api_convert_to_pdf($_GET['reset']);
}
drupal_goto('admin/config/os2web/adlibstatus');
}
if (isset($_GET['process'])) {
// _os2web_adlib_api_do_processing();
// drupal_goto('admin/config/os2web/adlibstatus');
// Add set up queue for adlib processing.
$batch = array(
'operations' => array(),
'title' => 'Konvertere dokumenter via. AdLib',
'init_message' => t('Initializing'),
'progress_message' => t('Finished @current of @total'),
'error_message' => t('Something went wrong'),
);
$items = db_select('os2web_adlib_api_doc_files', 'f')
->fields('f')
->condition(db_or()->condition('job_id', 'Complete', '!=')->isNull('job_id'))
->execute()
->fetchAll();
$chunk_size = max(min((int) sqrt(count($items)), 5), 1);
$items = array_chunk($items, $chunk_size);
foreach ($items as $item) {
$batch['operations'][] = array('_os2web_adlib_api_process_fid', array($item));
}
batch_set($batch);
batch_process();
}
$adlib = _os2web_adlib_api_get_instance();
$head = array('Connector Name', 'status', 'Jobs');
$statuslist = $adlib->getConnectorStatusList();
$rows = array();
if (is_array($statuslist)) {
foreach ($statuslist as $status) {
$rows[] = array(
$status->ServerDetail['ServerName'],
($status->ServerDetail['Enabled'] == 'True' ? 'E' : 'Not e') . 'nabled - ' . $status->ServerInfo['Status'],
$status->ServerInfo['JobCount'],
);
}
}
drupal_add_js(drupal_get_path('module', 'os2web_adlib_api') . '/js/os2web_adlib_api.js', 'file');
$html = '<h2>Connector information</h2>';
$html .= theme('table', array('header' => $head, 'rows' => $rows));
$head = array('Fid', 'Job-id', 'Status', 'action');
$rows = array();
$count = db_query("SELECT COUNT(*) as count FROM {os2web_adlib_api_doc_files} WHERE job_id IS NULL OR job_id NOT LIKE 'Complete'")->fetch();
$files = db_query("SELECT * FROM {os2web_adlib_api_doc_files} WHERE job_id IS NULL OR job_id NOT LIKE 'Complete' LIMIT 50");
foreach ($files as $file) {
$fdata = file_load($file->fid);
$rows[] = array(
l($fdata->uri, file_create_url($fdata->uri)),
$file->job_id,
array('data' => 'Looking up..', 'class' => 'status', 'id' => $file->fid),
l(t('delete'), 'admin/config/os2web/adlibstatus', array('query' => array('delete' => $file->fid))) . ' ' . l(t('reset'), 'admin/config/os2web/adlibstatus', array('query' => array('reset' => $file->fid))),
);
}
$html .= '<h2>Active jobs (first 50)</h2>';
$html .= 'jobs total: ' . $count->count;
$html .= theme('table', array('header' => $head, 'rows' => $rows));
// $html .= l('Delete all', 'admin/config/os2web/adlibstatus', array('query' => array('delete' => 'all')));
$html .= ' - ' . l(t('Run processing'), 'admin/config/os2web/adlibstatus', array('query' => array('process' => TRUE)));
return $html;
}
/**
* Test page for abitrary test usage.
*/
function os2web_adlib_api_page() {
error_log('MARK - ' . basename(__FILE__) . ':' . __LINE__ . ' in ' . __FUNCTION__ . '()');
$fid = 236605;
$nid = 840980;
// os2web_adlib_api_file_delete($fid);
// db_delete('os2web_adlib_api_doc_files')->condition('fid', $fid)->execute();
// os2web_adlib_api_convert_to_pdf($fid);
// os2web_adlib_api_convert_to_html($fid, $nid);
// _os2web_adlib_api_process_pdf();
// sleep(5);
// _os2web_adlib_api_process_pdf();
// $file = db_select('os2web_adlib_api_doc_files', 'df')->fields('df')->condition('fid', $fid)->execute()->fetchObject();
// $adlib = _os2web_adlib_api_get_instance();
// $call = array();
// $call[] = explode('\\',$adlib->addJobFolder());
// $call[] = $adlib->deleteJobFolder($call[0][1]);
// $call = $adlib->getJobStatus($file->job_id);
// $call= file_load(ADLIB_TEST_FID);
// $call = node_load($nid,NULL,TRUE);
$files = db_select('file_managed', 'fm')->fields('fm')->execute();
$rows = array();
foreach ($files as $file) {
if (file_exists(drupal_realpath($file->uri))) {
$rows[] = array(l($file->fid, 'admin/config/os2web/adlibstatus', array(
'query' => array('reset' => $file->fid))),
$file->filename);
}
}
return theme('table', array('rows' => $rows));
}
/**
* Implements hook_cron().
*/
function os2web_adlib_api_cron() {
// Add set up queue for adlib processing.
_os2web_adlib_api_do_processing();
// Add set up queue for adlib requeueing.
$queue = DrupalQueue::get('os2web_adlib_reprocess');
if ($queue->numberOfItems() == 0) {
$items = _os2web_adlib_api_bulletpoint_nid();
$chunk_size = max(min((int) sqrt(count($items)), 25), 1);
$items = array_chunk($items, $chunk_size);
foreach ($items as $item) {
$queue->createItem($item);
}
}
}
/**
* Implements hook_cron_queue_info().
* @author Thomas Thune Hansen <[email protected]>
*/
function os2web_adlib_api_cron_queue_info() {
$queues['os2web_adlib_reprocess'] = array(
'worker callback' => '_os2web_adlib_api_reprocess_nid',
'time' => 300,
);
$queues['os2web_adlib_process'] = array(
'worker callback' => '_os2web_adlib_api_process_fid',
'time' => 300,
);
return $queues;
}
/**
* Implements hook_init().
*/
function os2web_adlib_api_init() {
// _os2web_adlib_api_process_pdf(1);
}
/**
* Converts a fid to html and injects into nid body field.
*
* @param int $fid
* File id
* @param int $nid
* Node id
*/
function os2web_adlib_api_convert_to_html($fid, $nid) {
if (0 === db_query('SELECT fid from {os2web_adlib_api_doc_files} where fid = :fid AND jobtype = :jobtype', array(':fid' => $fid, ':jobtype' => 1))->rowCount()) {
$record = array(
'fid' => $fid,
'did' => $nid,
'jobtype' => 1,
);
drupal_write_record('os2web_adlib_api_doc_files', $record);
return TRUE;
}
else {
// error_log(basename(__FILE__) . ':' . __LINE__ . " [email protected]: File already in queue, aborting.");
return FALSE;
}
}
/**
* Main call to queue file for converting.
*
* @param int $fid
* File id of a managed file
* @param int $docref
* Arbitrary identifier for later reference (ie. Doc id)
*
* @return boolean
* False if file was not queued.
*/
function os2web_adlib_api_convert_to_pdf($fid, $docref = NULL) {
$file = file_load($fid);
if (!is_object($file)) {
// File has been erased from drupals file_managed table.
watchdog('adlib_api', 'File fid: %fid no longer exists, so removed.', array('%fid' => $fid), WATCHDOG_WARNING);
db_delete('os2web_adlib_api_doc_files')->condition('fid', $fid)->execute();
return FALSE;
}
if (!file_exists(drupal_realpath($file->uri))) {
watchdog('adlib_api', 'Cannot queue file %file (fid: %fid). File does not exist. File record removed.', array('%fid' => $fid, '%file' => $file->filename), WATCHDOG_WARNING);
// file_delete($file);
return FALSE;
}
if (_os2web_adlib_api_is_filetype_approved($fid)) {
if (0 === db_query('SELECT fid from {os2web_adlib_api_doc_files} where fid = :fid AND jobtype = :jobtype', array(':fid' => $fid, ':jobtype' => 0))->rowCount()) {
$record = array(
'fid' => $fid,
'did' => $docref,
'jobtype' => 0,
);
db_delete('os2web_adlib_api_doc_files')
->condition('fid', $fid)
->condition('did', $docref)
->condition('jobtype', 0)
->execute();
drupal_write_record('os2web_adlib_api_doc_files', $record);
if (ADLIB_TEST_FID) {
error_log(basename(__FILE__) . ':' . __LINE__ . " [email protected]: File queued for processing.");
}
return TRUE;
}
else {
// error_log(basename(__FILE__) . ':' . __LINE__ . " [email protected]: File already in queue, aborting.");
return FALSE;
}
}
else {
// error_log(basename(__FILE__) . ':' . __LINE__ . " [email protected]: File already PDF.");
$record = array(
'fid' => $fid,
'did' => $docref,
'job_id' => 'Complete',
);
db_delete('os2web_adlib_api_doc_files')
->condition('fid', $fid)
->condition('did', $docref)
->condition('jobtype', 0)
->execute();
drupal_write_record('os2web_adlib_api_doc_files', $record);
return FALSE;
}
}
/**
* Return the fid of a file saved with a reference.
*
* @param int $ref
* Reference for the file
*
* @return int
* File id, false if none.
*/
function os2web_adlib_api_get_fid($ref) {
$fid = db_query('SELECT fid FROM {os2web_adlib_api_doc_files} WHERE did=:did ORDER BY fid DESC', array(':did' => $ref))->fetchField();
if ($fid) {
return $fid;
}
return false;
}
/**
* Factory for class instances. This allows easy changing between api classes.
*
* @return AdLibWS
* AdLib controller object.
*/
function _os2web_adlib_api_get_instance() {
$obj = &drupal_static(__FUNCTION__);
if (is_object($obj)) {
return $obj;
}
module_load_include('inc', 'os2web_adlib_api', 'AdLibWS.class');
return $obj = new AdLibWS();
}
/**
* This will run the list of files and call the neccesary workers.
*/
function _os2web_adlib_api_do_processing() {
// Add set up queue for adlib processing.
//
// Disable auto queue of missing files
$queue = DrupalQueue::get('os2web_adlib_process');
if ($queue->numberOfItems() == 0) {
$items = db_select('os2web_adlib_api_doc_files', 'f')
->fields('f')
->condition(db_or()->condition('job_id', 'Complete', '!=')->isNull('job_id'))
->execute()
->fetchAll();
$chunk_size = max(min((int) sqrt(count($items)), 25), 1);
$items = array_chunk($items, $chunk_size);
foreach ($items as $item) {
if ($item->job_id != 'Complete') {
$queue->createItem($item);
}
}
}
}
/**
* Worker process for a single file structure.
*
* @access: protected
*/
function _os2web_adlib_api_process_fid($file) {
if (is_array($file)) {
foreach ($file as $f) {
_os2web_adlib_api_process_fid($f);
}
return;
}
if (!is_object($file)) {
error_log("Var: \$file = " . print_r($file, 1));
error_log('MARK - ' . basename(__FILE__) . ':' . __LINE__ . ' - Wrong input for fid processing.');
return;
}
if (!lock_acquire(__FUNCTION__ . $file->fid)) {
lock_wait(__FUNCTION__ . $file->fid, 15);
return _os2web_adlib_api_process_fid($file);
}
if ($file->job_id == NULL || $file->job_id == 'Requeue') {
_os2web_adlib_api_queue_file($file);
}
elseif ($file->job_id != 'Complete') {
_os2web_adlib_api_process_file($file);
}
lock_release(__FUNCTION__ . $file->fid);
}
/**
* Worker process for an actual file.
*
* @access: protected
*/
function _os2web_adlib_api_process_file($file) {
if (in_array($file->job_id, array('Complete', 'Requeue'))) {
// We should not get here, but if we do, these posts would be deleted if we do not return here.
return;
}
$adlib = _os2web_adlib_api_get_instance();
$jobinfo = $adlib->getJobStatus($file->job_id);
switch ($jobinfo->status) {
case 'Not found':
case 'Failed':
os2web_adlib_api_destroy_job($file->job_id);
$jobfileinfo = unserialize($file->job_info);
$fdata = file_load($file->fid);
if (!file_exists(drupal_realpath($fdata->uri)) || file_uri_scheme($fdata->uri) == '' || $fdata->filemime == 'application/pdf') {
if ($fdata->filemime == 'application/pdf') {
$record = array(
'fid' => $fdata->fid,
'jobtype' => $fdata->jobtype,
'job_id' => 'Complete',
'job_info' => NULL,
);
drupal_write_record('os2web_adlib_api_doc_files', $record, array('fid', 'jobtype'));
watchdog('adlib_api', 'File %file (fid: %fid ) already pdf.', array('%fid' => $fdata->fid, '%file' => $fdata->filename), WATCHDOG_WARNING);
}
else {
watchdog('adlib_api', 'Cannot re-queue file %file. File does not exist or is invalid.', array('%file' => $fdata->filename), WATCHDOG_WARNING);
os2web_adlib_api_file_delete($file);
}
}
else {
$record = array(
'fid' => $file->fid,
'jobtype' => $file->jobtype,
'job_id' => 'Requeue',
'job_info' => NULL,
);
drupal_write_record('os2web_adlib_api_doc_files', $record, array('fid', 'jobtype'));
watchdog('adlib_api', 'Found a failed job for %file. Resetting.', array('%file' => $fdata->filename), WATCHDOG_WARNING);
}
break;
case 'Complete':
$jobfileinfo = unserialize($file->job_info);
$download = curl_init($jobfileinfo['out_url']);
if (variable_get('os2web_pws_proxy')) {
curl_setopt($download, CURLOPT_PROXY, variable_get('os2web_pws_proxy_host'));
curl_setopt($download, CURLOPT_PROXYPORT, variable_get('os2web_pws_proxy_port'));
curl_setopt($download, CURLOPT_HTTPPROXYTUNNEL, TRUE);
}
curl_setopt($download, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($download);
$info = curl_getinfo($download);
if ($info['http_code'] !== 200) {
// File not found ??
$record = array(
'fid' => $file->fid,
'jobtype' => $file->jobtype,
'job_id' => 'Requeue',
'job_info' => NULL,
);
os2web_adlib_api_destroy_job($file->job_id);
drupal_write_record('os2web_adlib_api_doc_files', $record, array('fid', 'jobtype'));
watchdog('adlib_api', 'Completed file (fid: %fid)could not be found on the server (Code: %code). Job reset.', array('%code' => $info['http_code'], '%fid' => $file->fid), WATCHDOG_WARNING);
break;
}
switch ($file->jobtype) {
// PDF.
case 0:
$upd_file = file_load($file->fid);
$uri = file_unmanaged_save_data($data, str_replace($upd_file->filename, $jobfileinfo['out_filename'], $upd_file->uri), FILE_EXISTS_REPLACE);
if (filesize(drupal_realpath($uri)) > 0) {
db_delete('file_managed')->condition('uri', $uri)->execute();
$upd_file->uri = $uri;
$upd_file->filename = basename($uri);
$upd_file->filemime = file_get_mimetype($upd_file->uri);
$upd_file->filesize = filesize(drupal_realpath($upd_file->uri));
$upd_file->timestamp = time();
os2web_adlib_api_destroy_job($file->job_id);
$record = array(
'fid' => $file->fid,
'jobtype' => $file->jobtype,
'job_id' => 'Complete',
);
drupal_write_record('os2web_adlib_api_doc_files', $record, array('fid', 'jobtype'));
drupal_write_record('file_managed', $upd_file, array('fid'));
field_cache_clear();
watchdog('adlib_api', 'File (fid: %fid) was converted successfully to pdf.', array('%fid' => $file->fid), WATCHDOG_INFO);
error_log(basename(__FILE__) . ':' . __LINE__ . " [email protected]: File saved, file record updated. New URI: " . $uri);
}
else {
error_log(basename(__FILE__) . ':' . __LINE__ . " [email protected]: Filesize was 0. Aborting update.");
}
break;
// HTML.
case 1:
$nid = $file->did;
if ($node = node_load($nid)) {
$node->field_bpa_body[LANGUAGE_NONE][0]['value'] = _os2web_adlib_api_process_html($data, $jobfileinfo['out_url']);
$node->field_bpa_body[LANGUAGE_NONE][0]['format'] = 'filtered_html';
$node->feeds = array();
node_save($node);
os2web_adlib_api_destroy_job($file->job_id);
$record = array(
'fid' => $file->fid,
'jobtype' => $file->jobtype,
'job_id' => 'Complete',
);
drupal_write_record('os2web_adlib_api_doc_files', $record, array('fid', 'jobtype'));
error_log('MARK - ' . basename(__FILE__) . ':' . __LINE__ . ' in ' . __FUNCTION__ . '() - File converted, node updated.');
watchdog('adlib_api', 'File (fid: %fid) was converted successfully.', array('%fid' => $file->fid), WATCHDOG_INFO);
}
break;
default:
watchdog('adlib_api', 'Unknown jobtype returned from AdLib.', NULL, WATCHDOG_WARNING);
}
break;
case 'Pending':
case 'InProgress':
break;
default:
watchdog('adlib_api', 'Unknown job status: %var', array('%var' => $jobinfo->status), WATCHDOG_WARNING);
break;
}
}
/**
* This function validates html and fixes all image links.
* @access: protected
*/
function _os2web_adlib_api_process_html($data, $base_url) {
$base_dir = ADLIB_HTML_IMG_FOLDER;
if (!file_prepare_directory($base_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
watchdog('adlib_api', 'Cannot get write permission for the AdLib image store folder.', NULL, WATCHDOG_ERROR);
return;
}
module_load_include('php', 'os2web_adlib_api', 'simple_html_dom');
$html = str_get_html($data);
$img = $html->find('img');
for ($i = 0; $i < count($img); $i++) {
watchdog('adlib_api', 'Importing image for HTML: %fname', array('%fname' => basename($img[$i]->src)));
$image_url = $base_url . '/' . rawurlencode($img[$i]->src);
$download = curl_init($image_url);
if (variable_get('os2web_pws_proxy')) {
curl_setopt($download, CURLOPT_PROXY, variable_get('os2web_pws_proxy_host'));
curl_setopt($download, CURLOPT_PROXYPORT, variable_get('os2web_pws_proxy_port'));
curl_setopt($download, CURLOPT_HTTPPROXYTUNNEL, TRUE);
}
curl_setopt($download, CURLOPT_RETURNTRANSFER, TRUE);
$image = curl_exec($download);
$info = curl_getinfo($download);
if ($info['http_code'] === 200) {
// AdLib uses Windows folder notation.
$file_uri = $base_dir . basename(str_replace('\\', '/', $img[$i]->src));
$file = file_save_data($image, $file_uri, FILE_EXISTS_REPLACE);
$img[$i]->src = file_create_url($file->uri);
}
else {
watchdog('adlib_api', 'Failed to fetch image for HTML: %fname', array('%fname' => basename($element->src)), WATCHDOG_WARNING);
}
}
$data = (string) $html;
// Clean up the html, since AdLib sometimes return less-than-valid html and hence can cause PDO exceptions.
if (!function_exists('tidy_repair_string')) {
watchdog('adlib_api', 'Tidy API not found! Cannot process HTML.', NULL, WATCHDOG_ERROR);
}
else {
$config = array(
'indent' => TRUE,
'output-xhtml' => TRUE,
'wrap' => 200,
'word-2000' => TRUE,
'show-body-only' => TRUE,
);
$data = tidy_repair_string($data, $config);
}
return $data;
}
/**
* Worker function that queues up a single file from a row in the job table.
*
* @param object $file
* Object representing a row from the job table
*
* @return boolean
* True on success
*/
function _os2web_adlib_api_queue_file($file) {
$file->file = file_load($file->fid);
if (is_file(drupal_realpath($file->file->uri)) && _os2web_adlib_api_is_filetype_approved($file->file->fid)) {
$adlib = _os2web_adlib_api_get_instance();
$upload = $adlib->uploadData($file->file->filename, file_get_contents(drupal_realpath($file->file->uri)));
if ($upload) {
// Normal priority.
$priority = 3;
if ($file->job_id === 'Requeue') {
// Make requeued jobs run on lower priority.
$priority = 5;
}
$job = $adlib->addJob($upload, $file->jobtype, $priority);
if ($job !== FALSE) {
$jobinfo = array(
'fid' => $file->fid,
'jobtype' => $file->jobtype,
'job_id' => (string) $job->JobSettings['JobID'],
'job_info' => $upload,
);
drupal_write_record('os2web_adlib_api_doc_files', $jobinfo, array('fid', 'jobtype'));
watchdog('adlib_api', 'Queued file %file for PDF.', array('%file' => $file->file->filename));
return TRUE;
}
else {
watchdog('adlib_api', 'Failed to add job for %file.', array('%file' => $file->file->filename));
}
}
else {
watchdog('adlib_api', 'Upload of %file failed. Check permissions.', array('%file' => $file->file->filename));
db_delete('os2web_adlib_api_doc_files')->condition('fid', $file->file->fid)->execute();
}
}
else {
if (!_os2web_adlib_api_is_filetype_approved($file->file->fid)) {
$jobinfo = array(
'fid' => $file->fid,
'jobtype' => $file->jobtype,
'job_id' => 'Complete',
'job_info' => NULL,
);
drupal_write_record('os2web_adlib_api_doc_files', $jobinfo, array('fid', 'jobtype'));
watchdog('adlib_api', 'Failed to add %file to adlib queue. File is already pdf.', array('%file' => $file->file->filename));
}
else {
watchdog('adlib_api', 'Failed to add %file to adlib queue. File not found.', array('%file' => $file->file->filename));
db_delete('os2web_adlib_api_doc_files')->condition('fid', $file->file->fid)->execute();
}
}
return FALSE;
}
/**
* Helper function, check if file type is correct.
* @access protected
*
* @param int $fid
* File id
*
* @return bool
* True if file is approved for processing.
*/
function _os2web_adlib_api_is_filetype_approved($fid) {
// Filetypes which are approved for processing.
$approved_files = array(
'docx',
'docm',
'doc',
'dotx',
'dotm',
'xps',
'rtf',
'xlsx',
'odt',
'xlsm',
'xlsb',
'xls',
'xltx',
'xltm',
'xlt',
'xps',
'ods',
'wps',
'pptx',
'pptm',
'ppt',
'potx',
'potm',
'ppsx',
'ppsm',
'pps',
'odp',
'msg',
);
if (is_array($fid)) {
$result = array();
$files = file_load_multiple($fid);
foreach ($files as $file) {
$result[$file->fid]['is_pdf'] = in_array(strtolower(pathinfo(file_load($fid)->filename, PATHINFO_EXTENSION)), $approved_files);
$result[$file->fid]['file'] = $file;
}
return $result;
}
else {
return in_array(strtolower(pathinfo(file_load($fid)->filename, PATHINFO_EXTENSION)), $approved_files);
}
}
/**
* Deletes a job related to a file.
*
* @param object $file
* Drupal file object
*/
function os2web_adlib_api_file_delete($file) {
if (is_numeric($file)) {
$file = db_select('os2web_adlib_api_doc_files', 'f')
->fields('f')
->condition('fid', $file)
->execute()
->fetchObject();
}
if (is_object($file)) {
if (isset($file->job_id)) {
os2web_adlib_api_destroy_job($file->job_id);
}
db_delete('os2web_adlib_api_doc_files')->condition('fid', $file->fid)->execute();
watchdog('adlib_api', 'File %file has been cleaned up from adlib conversion table.', array('%file' => $file->uri), WATCHDOG_DEBUG);
}
}
/**
* Removes the job and related folders from the AdLib server.
*
* @param string $job_id
* Job id
*/
function os2web_adlib_api_destroy_job($job_id) {
$file = db_select('os2web_adlib_api_doc_files', 'f')
->fields('f')
->condition('job_id', $job_id)
->execute()
->fetchObject();
if (is_object($file)) {
$adlib = _os2web_adlib_api_get_instance();
if (isset($file->job_id) && $file->job_id != 'Complete') {
$jobfileinfo = unserialize($file->job_info);
$adlib->deleteJob($file->job_id);
$folder = explode('\\', $jobfileinfo['basefolder']);
$adlib->deleteJobFolder($folder[1]);
}
watchdog('adlib_api', 'Deleted job for %fid', array('%fid' => $job_id));
}
}
/**
* Implements hook_os2web_help().
*/
function os2web_adlib_api_os2web_help($sections) {
// List of content.
$sections['list_of_content'] = t('<a href="#os2web_adlib_api">Adlib file conversion</a><br />');
// Module specific.
$sections['os2web_adlib_api'] = t('<h2 id="os2web_adlib_api">Adlib file conversion:</h2>');
$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')));
$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')));
return $sections;
}