Projet

Général

Profil

Paste
Télécharger (38,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / feeds.module @ 5136ce55

1
<?php
2

    
3
/**
4
 * @file
5
 * Feeds - basic API functions and hook implementations.
6
 */
7

    
8
// Common request time, use as point of reference and to avoid calls to time().
9
define('FEEDS_REQUEST_TIME', time());
10
// Do not schedule a feed for refresh.
11
define('FEEDS_SCHEDULE_NEVER', -1);
12
// Never expire feed items.
13
define('FEEDS_EXPIRE_NEVER', -1);
14
// An object that is not persistent. Compare EXPORT_IN_DATABASE, EXPORT_IN_CODE.
15
define('FEEDS_EXPORT_NONE', 0x0);
16
// Status of batched operations.
17
define('FEEDS_BATCH_COMPLETE', 1.0);
18
define('FEEDS_BATCH_ACTIVE', 0.0);
19

    
20
/**
21
 * @defgroup hooks Hook and callback implementations
22
 * @{
23
 */
24

    
25
/**
26
 * Implements hook_hook_info().
27
 */
28
function feeds_hook_info() {
29
  $hooks = array(
30
    'feeds_plugins',
31
    'feeds_after_parse',
32
    'feeds_before_import',
33
    'feeds_before_update',
34
    'feeds_presave',
35
    'feeds_after_save',
36
    'feeds_after_import',
37
    'feeds_after_clear',
38
    'feeds_processor_targets',
39
    'feeds_processor_targets_alter',
40
    'feeds_parser_sources_alter',
41
  );
42

    
43
  return array_fill_keys($hooks, array('group' => 'feeds'));
44
}
45

    
46
/**
47
 * Implements hook_cron().
48
 */
49
function feeds_cron() {
50
  // Expire old log entries.
51
  db_delete('feeds_log')
52
    ->condition('request_time', REQUEST_TIME - 604800, '<')
53
    ->execute();
54

    
55
  // Find importers that need to be rescheduled.
56
  if (!$importers = feeds_reschedule()) {
57
    return;
58
  }
59

    
60
  // @todo Maybe we should queue this somehow as well. This could be potentially
61
  // very long.
62
  $sources = db_query("SELECT feed_nid, id FROM {feeds_source} WHERE id IN (:ids)", array(':ids' => $importers));
63

    
64
  foreach ($sources as $source) {
65
    feeds_source($source->id, $source->feed_nid)->schedule();
66
  }
67

    
68
  feeds_reschedule(FALSE);
69
}
70

    
71
/**
72
 * Implements hook_cron_job_scheduler_info().
73
 *
74
 * Compare queue names with key names in feeds_cron_queue_info().
75
 */
76
function feeds_cron_job_scheduler_info() {
77
  $info = array();
78
  $info['feeds_source_import'] = array(
79
    'queue name' => 'feeds_source_import',
80
  );
81
  $info['feeds_source_clear'] = array(
82
    'queue name' => 'feeds_source_clear',
83
  );
84
  $info['feeds_source_expire'] = array(
85
    'queue name' => 'feeds_source_expire',
86
  );
87
  $info['feeds_push_unsubscribe'] = array(
88
    'queue name' => 'feeds_push_unsubscribe',
89
  );
90
  return $info;
91
}
92

    
93
/**
94
 * Implements hook_cron_queue_info().
95
 */
96
function feeds_cron_queue_info() {
97
  $queues = array();
98
  $queues['feeds_source_import'] = array(
99
    'worker callback' => 'feeds_source_import',
100
    'time' => 60,
101
  );
102
  $queues['feeds_source_clear'] = array(
103
    'worker callback' => 'feeds_source_clear',
104
  );
105
  $queues['feeds_source_expire'] = array(
106
    'worker callback' => 'feeds_source_expire',
107
  );
108
  $queues['feeds_push_unsubscribe'] = array(
109
    'worker callback' => 'feeds_push_unsubscribe',
110
  );
111

    
112
  return $queues;
113
}
114

    
115
/**
116
 * Scheduler callback for importing from a source.
117
 */
118
function feeds_source_import(array $job) {
119
  $source = _feeds_queue_worker_helper($job, 'import');
120
  $source->scheduleImport();
121
}
122

    
123
/**
124
 * Scheduler callback for deleting all items from a source.
125
 */
126
function feeds_source_clear(array $job) {
127
  $source = _feeds_queue_worker_helper($job, 'clear');
128
  $source->scheduleClear();
129
}
130

    
131
/**
132
 * Scheduler callback for expiring content.
133
 */
134
function feeds_source_expire(array $job) {
135
  $source = _feeds_queue_worker_helper($job, 'expire');
136
  $source->scheduleExpire();
137
}
138

    
139
/**
140
 * Executes a method on a feed source.
141
 *
142
 * @param array $job
143
 *   The job being run.
144
 * @param string $method
145
 *   The method to execute.
146
 */
147
function _feeds_queue_worker_helper(array $job, $method) {
148
  $source = feeds_source($job['type'], $job['id']);
149
  try {
150
    $source->existing()->$method();
151
  }
152
  catch (FeedsNotExistingException $e) {
153
    // Do nothing.
154
  }
155
  catch (Exception $e) {
156
    $source->log($method, $e->getMessage(), array(), WATCHDOG_ERROR);
157
  }
158

    
159
  return $source;
160
}
161

    
162
/**
163
 * Scheduler callback for unsubscribing from PuSH hubs.
164
 */
165
function feeds_push_unsubscribe($job) {
166
  $source = feeds_source($job['type'], $job['id']);
167
  $fetcher = feeds_plugin('FeedsHTTPFetcher', $source->importer->id);
168
  $fetcher->unsubscribe($source);
169
}
170

    
171
/**
172
 * Batch API worker callback. Used by FeedsSource::startBatchAPIJob().
173
 *
174
 * @see FeedsSource::startBatchAPIJob().
175
 *
176
 * @todo Harmonize Job Scheduler API callbacks with Batch API callbacks?
177
 *
178
 * @param $method
179
 *   Method to execute on importer; one of 'import' or 'clear'.
180
 * @param $importer_id
181
 *   Identifier of a FeedsImporter object.
182
 * @param $feed_nid
183
 *   If importer is attached to content type, feed node id identifying the
184
 *   source to be imported.
185
 * @param $context
186
 *   Batch context.
187
 */
188
function feeds_batch($method, $importer_id, $feed_nid = 0, &$context) {
189
  $context['finished'] = FEEDS_BATCH_COMPLETE;
190
  try {
191
    $context['finished'] = feeds_source($importer_id, $feed_nid)->$method();
192
  }
193
  catch (Exception $e) {
194
    drupal_set_message($e->getMessage(), 'error');
195
  }
196
}
197

    
198
/**
199
 * Reschedule one or all importers.
200
 *
201
 * @param string $importer_id
202
 *   If TRUE, all importers will be rescheduled, if FALSE, no importers will
203
 *   be rescheduled, if an importer id, only importer of that id will be
204
 *   rescheduled.
205
 *
206
 * @return array
207
 *   An list of importers that need rescheduling.
208
 */
209
function feeds_reschedule($importer_id = NULL) {
210
  $reschedule = variable_get('feeds_reschedule', FALSE);
211

    
212
  if ($importer_id === TRUE || $importer_id === FALSE) {
213
    $reschedule = $importer_id;
214
  }
215
  elseif (is_string($importer_id) && $reschedule !== TRUE) {
216
    $reschedule = array_filter((array) $reschedule);
217
    $reschedule[$importer_id] = $importer_id;
218
  }
219

    
220
  if (isset($importer_id)) {
221
    variable_set('feeds_reschedule', $reschedule);
222
  }
223

    
224
  if ($reschedule === TRUE) {
225
    return feeds_enabled_importers();
226
  }
227
  elseif ($reschedule === FALSE) {
228
    return array();
229
  }
230

    
231
  return $reschedule;
232
}
233

    
234
/**
235
 * Implements feeds_permission().
236
 */
237
function feeds_permission() {
238
  $perms = array(
239
    'administer feeds' => array(
240
      'title' => t('Administer Feeds'),
241
      'description' => t('Create, update, delete importers, execute import and delete tasks on any importer.')
242
    ),
243
  );
244
  foreach (feeds_importer_load_all() as $importer) {
245
    $perms["import $importer->id feeds"] = array(
246
      'title' => t('Import @name feeds', array('@name' => $importer->config['name'])),
247
    );
248
    $perms["clear $importer->id feeds"] = array(
249
      'title' => t('Delete items from @name feeds', array('@name' => $importer->config['name'])),
250
    );
251
    $perms["unlock $importer->id feeds"] = array(
252
      'title' => t('Unlock imports from @name feeds', array('@name' => $importer->config['name'])),
253
      'description' => t('If a feed importation breaks for some reason, users with this permission can unlock them.')
254
    );
255
  }
256
  return $perms;
257
}
258

    
259
/**
260
 * Implements hook_forms().
261
 *
262
 * Declare form callbacks for all known classes derived from FeedsConfigurable.
263
 */
264
function feeds_forms() {
265
  $forms = array();
266
  $forms['FeedsImporter_feeds_form']['callback'] = 'feeds_form';
267
  $plugins = FeedsPlugin::all();
268
  foreach ($plugins as $plugin) {
269
    $forms[$plugin['handler']['class'] . '_feeds_form']['callback'] = 'feeds_form';
270
  }
271
  return $forms;
272
}
273

    
274
/**
275
 * Implements hook_menu().
276
 */
277
function feeds_menu() {
278
  $items = array();
279
  $items['import'] = array(
280
    'title' => 'Import',
281
    'page callback' => 'feeds_page',
282
    'access callback' => 'feeds_page_access',
283
    'file' => 'feeds.pages.inc',
284
  );
285
  $items['import/%feeds_importer'] = array(
286
    'title callback' => 'feeds_importer_title',
287
    'title arguments' => array(1),
288
    'page callback' => 'drupal_get_form',
289
    'page arguments' => array('feeds_import_form', 1),
290
    'access callback' => 'feeds_access',
291
    'access arguments' => array('import', 1),
292
    'file' => 'feeds.pages.inc',
293
  );
294
  $items['import/%feeds_importer/import'] = array(
295
    'title' => 'Import',
296
    'type' => MENU_DEFAULT_LOCAL_TASK,
297
    'weight' => -10,
298
  );
299
  $items['import/%feeds_importer/delete-items'] = array(
300
    'title' => 'Delete items',
301
    'page callback' => 'drupal_get_form',
302
    'page arguments' => array('feeds_delete_tab_form', 1),
303
    'access callback' => 'feeds_access',
304
    'access arguments' => array('clear', 1),
305
    'file' => 'feeds.pages.inc',
306
    'type' => MENU_LOCAL_TASK,
307
  );
308
  $items['import/%feeds_importer/unlock'] = array(
309
    'title' => 'Unlock',
310
    'page callback' => 'drupal_get_form',
311
    'page arguments' => array('feeds_unlock_tab_form', 1),
312
    'access callback' => 'feeds_access',
313
    'access arguments' => array('unlock', 1),
314
    'file' => 'feeds.pages.inc',
315
    'type' => MENU_LOCAL_TASK,
316
  );
317
  $items['import/%feeds_importer/template'] = array(
318
    'page callback' => 'feeds_importer_template',
319
    'page arguments' => array(1),
320
    'access callback' => 'feeds_access',
321
    'access arguments' => array('import', 1),
322
    'file' => 'feeds.pages.inc',
323
    'type' => MENU_CALLBACK,
324
  );
325
  $items['node/%node/import'] = array(
326
    'title' => 'Import',
327
    'page callback' => 'drupal_get_form',
328
    'page arguments' => array('feeds_import_tab_form', 1),
329
    'access callback' => 'feeds_access',
330
    'access arguments' => array('import', 1),
331
    'file' => 'feeds.pages.inc',
332
    'type' => MENU_LOCAL_TASK,
333
    'weight' => 10,
334
  );
335
  $items['node/%node/delete-items'] = array(
336
    'title' => 'Delete items',
337
    'page callback' => 'drupal_get_form',
338
    'page arguments' => array('feeds_delete_tab_form', NULL, 1),
339
    'access callback' => 'feeds_access',
340
    'access arguments' => array('clear', 1),
341
    'file' => 'feeds.pages.inc',
342
    'type' => MENU_LOCAL_TASK,
343
    'weight' => 11,
344
  );
345
  $items['node/%node/unlock'] = array(
346
    'title' => 'Unlock',
347
    'page callback' => 'drupal_get_form',
348
    'page arguments' => array('feeds_unlock_tab_form', NULL, 1),
349
    'access callback' => 'feeds_access',
350
    'access arguments' => array('unlock', 1),
351
    'file' => 'feeds.pages.inc',
352
    'type' => MENU_LOCAL_TASK,
353
    'weight' => 11,
354
  );
355
  // @todo Eliminate this step and thus eliminate clearing menu cache when
356
  // manipulating importers.
357
  foreach (feeds_importer_load_all() as $importer) {
358
    $items += $importer->fetcher->menuItem();
359
  }
360
  return $items;
361
}
362

    
363
/**
364
 * Implements hook_admin_paths().
365
 */
366
function feeds_admin_paths() {
367
  $paths = array(
368
    'import' => TRUE,
369
    'import/*' => TRUE,
370
    'node/*/import' => TRUE,
371
    'node/*/delete-items' => TRUE,
372
    'node/*/log' => TRUE,
373
  );
374
  return $paths;
375
}
376

    
377
/**
378
 * Menu loader callback.
379
 */
380
function feeds_importer_load($id) {
381
  try {
382
    return feeds_importer($id)->existing();
383
  }
384
  catch (FeedsNotExistingException $e) {}
385
  catch (InvalidArgumentException $e) {}
386

    
387
  return FALSE;
388
}
389

    
390
/**
391
 * Title callback.
392
 */
393
function feeds_importer_title(FeedsImporter $importer) {
394
  return $importer->config['name'];
395
}
396

    
397
/**
398
 * Implements hook_theme().
399
 */
400
function feeds_theme() {
401
  return array(
402
    'feeds_upload' => array(
403
      'file' => 'feeds.pages.inc',
404
      'render element' => 'element',
405
    ),
406
    'feeds_source_status' => array(
407
      'file' => 'feeds.pages.inc',
408
      'variables' => array(
409
        'progress_importing' => NULL,
410
        'progress_clearing' => NULL,
411
        'imported' => NULL,
412
        'count' => NULL,
413
      ),
414
    ),
415
  );
416
}
417

    
418
/**
419
 * Menu access callback.
420
 *
421
 * @param $action
422
 *   The action to be performed. Possible values are:
423
 *   - import
424
 *   - clear
425
 *   - unlock
426
 * @param $param
427
 *   Node object or FeedsImporter id.
428
 */
429
function feeds_access($action, $param) {
430
  if (!in_array($action, array('import', 'clear', 'unlock'))) {
431
    // If $action is not one of the supported actions, we return access denied.
432
    return FALSE;
433
  }
434

    
435
  $importer_id = FALSE;
436
  if (is_string($param)) {
437
    $importer_id = $param;
438
  }
439
  elseif ($param instanceof FeedsImporter) {
440
    $importer_id = $param->id;
441
  }
442
  elseif ($param->type) {
443
    $importer_id = feeds_get_importer_id($param->type);
444
  }
445

    
446
  // Check for permissions if feed id is present, otherwise return FALSE.
447
  if ($importer_id) {
448
    if (user_access('administer feeds') || user_access("{$action} {$importer_id} feeds")) {
449
      return TRUE;
450
    }
451
  }
452
  return FALSE;
453
}
454

    
455
/**
456
 * Access callback to determine if the user can import Feeds importers.
457
 *
458
 * Feeds imports require an additional access check because they are PHP
459
 * code and PHP is more locked down than administer feeds.
460
 */
461
function feeds_importer_import_access() {
462
  return user_access('administer feeds') && user_access('use PHP for settings');
463
}
464

    
465
/**
466
 * Menu access callback.
467
 */
468
function feeds_page_access() {
469
  if (user_access('administer feeds')) {
470
    return TRUE;
471
  }
472
  foreach (feeds_enabled_importers() as $id) {
473
    if (user_access("import $id feeds")) {
474
      return TRUE;
475
    }
476
  }
477
  return FALSE;
478
}
479

    
480
/**
481
 * Implements hook_exit().
482
 */
483
function feeds_exit() {
484
  // Process any pending PuSH subscriptions.
485
  $jobs = feeds_get_subscription_jobs();
486
  foreach ($jobs as $job) {
487
    if (!isset($job['fetcher']) || !isset($job['source'])) {
488
      continue;
489
     }
490
    $job['fetcher']->subscribe($job['source']);
491
  }
492

    
493
  if (drupal_static('feeds_log_error', FALSE)) {
494
    watchdog('feeds', 'Feeds reported errors, visit the Feeds log for details.', array(), WATCHDOG_ERROR, 'admin/reports/dblog/feeds');
495
  }
496
}
497

    
498
/**
499
 * Implements hook_views_api().
500
 */
501
function feeds_views_api() {
502
  return array(
503
    'api' => 3,
504
    'path' => drupal_get_path('module', 'feeds') . '/views',
505
  );
506
}
507

    
508
/**
509
 * Implements hook_ctools_plugin_api().
510
 */
511
function feeds_ctools_plugin_api($owner, $api) {
512
  if ($owner == 'feeds' && $api == 'plugins') {
513
    return array('version' => 1);
514
  }
515
}
516

    
517
/**
518
 * Implements hook_ctools_plugin_type().
519
 */
520
function feeds_ctools_plugin_type() {
521
  return array(
522
    'plugins' => array(
523
      'cache' => TRUE,
524
      'use hooks' => TRUE,
525
      'classes' => array('handler'),
526
    ),
527
  );
528
}
529

    
530
/**
531
 * Implements hook_feeds_plugins().
532
 */
533
function feeds_feeds_plugins() {
534
  module_load_include('inc', 'feeds', 'feeds.plugins');
535
  return _feeds_feeds_plugins();
536
}
537

    
538
/**
539
 * Gets the feed_nid for a single entity.
540
 *
541
 * @param int $entity_id
542
 *   The entity id.
543
 * @param string $entity_type
544
 *   The type of entity.
545
 *
546
 * @return int|bool
547
 *   The feed_nid of the entity, or FALSE if the entity doesn't belong to a
548
 *   feed.
549
 */
550
function feeds_get_feed_nid($entity_id, $entity_type) {
551
  return db_query("SELECT feed_nid FROM {feeds_item} WHERE entity_type = :type AND entity_id = :id", array(':type' => $entity_type, ':id' => $entity_id))->fetchField();
552
}
553

    
554
/**
555
 * Implements hook_entity_insert().
556
 */
557
function feeds_entity_insert($entity, $type) {
558
  list($id) = entity_extract_ids($type, $entity);
559
  feeds_item_info_insert($entity, $id);
560
}
561

    
562
/**
563
 * Implements hook_entity_update().
564
 */
565
function feeds_entity_update($entity, $type) {
566
  list($id) = entity_extract_ids($type, $entity);
567
  feeds_item_info_save($entity, $id);
568
}
569

    
570
/**
571
 * Implements hook_entity_delete().
572
 */
573
function feeds_entity_delete($entity, $type) {
574
  list($id) = entity_extract_ids($type, $entity);
575

    
576
  // Delete any imported items produced by the source.
577
  db_delete('feeds_item')
578
    ->condition('entity_type', $type)
579
    ->condition('entity_id', $id)
580
    ->execute();
581
}
582

    
583
/**
584
 * Implements hook_node_validate().
585
 */
586
function feeds_node_validate($node, $form, &$form_state) {
587
  if (!$importer_id = feeds_get_importer_id($node->type)) {
588
    return;
589
  }
590
  // Keep a copy of the title for subsequent node creation stages.
591
  // @todo: revisit whether $node still looses all of its properties
592
  // between validate and insert stage.
593
  $last_title = &drupal_static('feeds_node_last_title');
594
  $last_feeds = &drupal_static('feeds_node_last_feeds');
595

    
596
  // On validation stage we are working with a FeedsSource object that is
597
  // not tied to a nid - when creating a new node there is no
598
  // $node->nid at this stage.
599
  $source = feeds_source($importer_id);
600

    
601
  // Node module magically moved $form['feeds'] to $node->feeds :P.
602
  // configFormValidate may modify $last_feed, smuggle it to update/insert stage
603
  // through a static variable.
604
  $last_feeds = $node->feeds;
605
  $source->configFormValidate($last_feeds);
606

    
607
  // If node title is empty, try to retrieve title from feed.
608
  if (trim($node->title) == '') {
609
    try {
610
      $source->addConfig($last_feeds);
611
      if (!$last_title = $source->preview()->title) {
612
        throw new Exception();
613
      }
614
    }
615
    catch (Exception $e) {
616
      drupal_set_message($e->getMessage(), 'error');
617
      form_set_error('title', t('Could not retrieve title from feed.'));
618
    }
619
  }
620
}
621

    
622
/**
623
 * Implements hook_node_presave().
624
 */
625
function feeds_node_presave($node) {
626
  // Populate $node->title and $node->feed from result of validation phase.
627
  $last_title = &drupal_static('feeds_node_last_title');
628
  $last_feeds = &drupal_static('feeds_node_last_feeds');
629
  if (empty($node->title) && !empty($last_title)) {
630
    $node->title = $last_title;
631
  }
632
  if (!empty($last_feeds)) {
633
    $node->feeds = $last_feeds;
634
  }
635
  $last_title = NULL;
636
  $last_feeds = NULL;
637

    
638
  // Update "changed" value if there was mapped to that.
639
  if (isset($node->feeds_item->node_changed)) {
640
    $node->changed = $node->feeds_item->node_changed;
641
  }
642
}
643

    
644
/**
645
 * Implements hook_node_insert().
646
 */
647
function feeds_node_insert($node) {
648
  // Source attached to node.
649
  feeds_node_update($node);
650
  if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
651
    $source = feeds_source($importer_id, $node->nid);
652
    // Start import if requested.
653
    if (feeds_importer($importer_id)->config['import_on_create'] && !isset($node->feeds['suppress_import'])) {
654
      $source->startImport();
655
    }
656
    // Schedule the source.
657
    $source->schedule();
658
  }
659
}
660

    
661
/**
662
 * Implements hook_node_update().
663
 */
664
function feeds_node_update($node) {
665
  // Source attached to node.
666
  if (isset($node->feeds) && $importer_id = feeds_get_importer_id($node->type)) {
667
    $source = feeds_source($importer_id, $node->nid);
668
    $source->addConfig($node->feeds);
669
    $source->save();
670
  }
671
}
672

    
673
/**
674
 * Implements hook_node_delete().
675
 */
676
function feeds_node_delete($node) {
677
  // Source attached to node.
678
  // Make sure we don't leave any orphans behind: Do not use
679
  // feeds_get_importer_id() to determine importer id as the importer may have
680
  // been deleted.
681
  if ($importer_id = db_query("SELECT id FROM {feeds_source} WHERE feed_nid = :nid", array(':nid' => $node->nid))->fetchField()) {
682
    feeds_source($importer_id, $node->nid)->delete();
683
  }
684
}
685

    
686
/**
687
 * Implements hook_form_BASE_FORM_ID_alter().
688
 */
689
function feeds_form_node_form_alter(&$form, $form_state) {
690
  if ($importer_id = feeds_get_importer_id($form['#node']->type)) {
691
    // Set title to not required, try to retrieve it from feed.
692
    if (isset($form['title'])) {
693
      $form['title']['#required'] = FALSE;
694
    }
695

    
696
    // Enable uploads.
697
    $form['#attributes']['enctype'] = 'multipart/form-data';
698

    
699
    // Build form.
700
    $source = feeds_source($importer_id, empty($form['#node']->nid) ? 0 : $form['#node']->nid);
701
    $form['feeds'] = array(
702
      '#type' => 'fieldset',
703
      '#title' => t('Feed'),
704
      '#tree' => TRUE,
705
      '#weight' => 0,
706
    );
707
    $form['feeds'] += $source->configForm($form_state);
708
    $form['#feed_id'] = $importer_id;
709
  }
710
}
711

    
712
/**
713
 * Implements hook_field_extra_fields().
714
 */
715
function feeds_field_extra_fields() {
716
  $extras = array();
717
  foreach (node_type_get_names() as $type => $name) {
718
    if (feeds_get_importer_id($type)) {
719
      $extras['node'][$type]['form']['feeds'] = array(
720
        'label' => t('Feed'),
721
        'description' => t('Feeds module form elements'),
722
        'weight' => 0,
723
      );
724
    }
725
  }
726
  return $extras;
727
}
728

    
729
/**
730
 * Implements hook_features_pipe_COMPONENT_alter() for component "feeds_importer".
731
 *
732
 * Automatically adds dependencies when a Feed importer is selected in Features.
733
 */
734
function feeds_features_pipe_feeds_importer_alter(&$pipe, $data, &$export) {
735
  foreach ($data as $importer_id) {
736
    if ($importer = feeds_importer_load($importer_id)) {
737
      $export['dependencies'] = array_merge($export['dependencies'], $importer->dependencies());
738
    }
739
  }
740
}
741

    
742
/**
743
 * Implements hook_system_info_alter().
744
 *
745
 * Goes through a list of all modules that provide Feeds plugins and makes them
746
 * required if there are any importers using those plugins.
747
 */
748
function feeds_system_info_alter(array &$info, $file, $type) {
749
  if ($type !== 'module' || !module_hook($file->name, 'feeds_plugins')) {
750
    return;
751
  }
752

    
753
  // Don't make Feeds require itself, otherwise you can't disable Feeds until
754
  // all importers are deleted.
755
  if ($file->name === 'feeds' || !function_exists('ctools_include')) {
756
    return;
757
  }
758

    
759
  // Get the plugins that belong to the current module.
760
  ctools_include('plugins');
761
  $module_plugins = array();
762
  foreach (ctools_get_plugins('feeds', 'plugins') as $plugin_id => $plugin) {
763
    if ($file->name === $plugin['module']) {
764
      $module_plugins[$plugin_id] = TRUE;
765
    }
766
  }
767

    
768
  // Check if any importers are using any plugins from the current module.
769
  foreach (feeds_importer_load_all(TRUE) as $importer) {
770

    
771
    // Skip importers that are defined in code and are provided by the current
772
    // module. This ensures that modules that define both an importer and a
773
    // plugin can still be disabled.
774
    if ($importer->export_type == EXPORT_IN_CODE) {
775
      $configs = ctools_export_load_object('feeds_importer', 'names', array($importer->id));
776
      if (isset($configs[$importer->id]) && $configs[$importer->id]->export_module === $file->name) {
777
        continue;
778
      }
779
    }
780

    
781
    $configuration = $importer->getConfig();
782

    
783
    foreach (array('fetcher', 'parser', 'processor') as $plugin_type) {
784
      $plugin_key = $configuration[$plugin_type]['plugin_key'];
785
      if (isset($module_plugins[$plugin_key])) {
786
        $info['required'] = TRUE;
787
        break 2;
788
      }
789
    }
790
  }
791

    
792
  if (empty($info['required'])) {
793
    return;
794
  }
795

    
796
  if (module_exists('feeds_ui') && user_access('administer feeds')) {
797
    $info['explanation'] = t('Feeds is currently using this module for one or more <a href="@link">importers</a>', array('@link' => url('admin/structure/feeds')));
798
  }
799
  else {
800
    $info['explanation'] = t('Feeds is currently using this module for one or more importers');
801
  }
802
}
803

    
804
/**
805
 * Implements hook_module_implements_alter().
806
 */
807
function feeds_module_implements_alter(array &$implementations, $hook) {
808
  if ($hook === 'feeds_processor_targets_alter') {
809
    // We need two implementations of this hook, so we add one that gets
810
    // called first, and move the normal one to last.
811
    $implementations = array('_feeds' => FALSE) + $implementations;
812

    
813
    // Move normal implementation to last.
814
    $group = $implementations['feeds'];
815
    unset($implementations['feeds']);
816
    $implementations['feeds'] = $group;
817
  }
818
}
819

    
820
/**
821
 * Implements hook_feeds_processor_targets_alter().
822
 *
823
 * @see feeds_feeds_processor_targets()
824
 * @see feeds_feeds_processor_targets_alter()
825
 */
826
function _feeds_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
827
  // If hook_feeds_processor_targets() hasn't been called, for instance, by
828
  // older processors, invoke it ourself.
829
  if (!drupal_static('feeds_feeds_processor_targets', FALSE)) {
830
    $targets += module_invoke_all('feeds_processor_targets', $entity_type, $bundle);
831
  }
832
}
833

    
834
/**
835
 * Implements hook_flush_caches().
836
 */
837
function feeds_flush_caches() {
838
  // The update to add the table needs to have run. Taken from
839
  // https://www.drupal.org/node/2511858
840
  include_once DRUPAL_ROOT . '/includes/install.inc';
841

    
842
  if (drupal_get_installed_schema_version('feeds') >= 7212) {
843
    return array('cache_feeds_http');
844
  }
845
  return array();
846
}
847

    
848
/**
849
 * @}
850
 */
851

    
852
/**
853
 * @defgroup utility Utility functions
854
 * @{
855
 */
856

    
857
/**
858
 * Loads all importers.
859
 *
860
 * @param $load_disabled
861
 *   Pass TRUE to load all importers, enabled or disabled, pass FALSE to only
862
 *   retrieve enabled importers.
863
 *
864
 * @return
865
 *   An array of all feed configurations available.
866
 */
867
function feeds_importer_load_all($load_disabled = FALSE) {
868
  $feeds = array();
869
  // This function can get called very early in install process through
870
  // menu_router_rebuild(). Do not try to include CTools if not available.
871
  if (function_exists('ctools_include')) {
872
    ctools_include('export');
873
    $configs = ctools_export_load_object('feeds_importer', 'all');
874
    foreach ($configs as $config) {
875
      if (!empty($config->id) && ($load_disabled || empty($config->disabled))) {
876
        $feeds[$config->id] = feeds_importer($config->id);
877
      }
878
    }
879
    uasort($feeds, 'feeds_importer_name_sort');
880
  }
881
  return $feeds;
882
}
883

    
884
/**
885
 * Sorts importers by name.
886
 *
887
 * Callback for uasort().
888
 *
889
 * @param FeedsImporter $a
890
 *   The first FeedsImporter for comparison.
891
 * @param FeedsImporter $b
892
 *   The second FeedsImporter for comparison.
893
 *
894
 * @return int
895
 *   The comparison result for uasort().
896
 */
897
function feeds_importer_name_sort(FeedsImporter $a, FeedsImporter $b) {
898
  return strcasecmp($a->config['name'], $b->config['name']);
899
}
900

    
901
/**
902
 * Gets an array of enabled importer ids.
903
 *
904
 * @return
905
 *   An array where the values contain ids of enabled importers.
906
 */
907
function feeds_enabled_importers() {
908
  return array_keys(_feeds_importer_digest());
909
}
910

    
911
/**
912
 * Gets an enabled importer configuration by content type.
913
 *
914
 * @param $content_type
915
 *   A node type string.
916
 *
917
 * @return
918
 *   A FeedsImporter id if there is an importer for the given content type,
919
 *   FALSE otherwise.
920
 */
921
function feeds_get_importer_id($content_type) {
922
  $importers = array_flip(_feeds_importer_digest());
923
  return isset($importers[$content_type]) ? $importers[$content_type] : FALSE;
924
}
925

    
926
/**
927
 * Helper function for feeds_get_importer_id() and feeds_enabled_importers().
928
 */
929
function _feeds_importer_digest() {
930
  $importers = &drupal_static(__FUNCTION__);
931
  if ($importers === NULL) {
932
    if ($cache = cache_get(__FUNCTION__)) {
933
      $importers = $cache->data;
934
    }
935
    else {
936
      $importers = array();
937
      foreach (feeds_importer_load_all() as $importer) {
938
        $importers[$importer->id] = isset($importer->config['content_type']) ? $importer->config['content_type'] : '';
939
      }
940
      cache_set(__FUNCTION__, $importers);
941
    }
942
  }
943
  return $importers;
944
}
945

    
946
/**
947
 * Resets importer caches. Call when enabling/disabling importers.
948
 */
949
function feeds_cache_clear($rebuild_menu = TRUE) {
950
  cache_clear_all('_feeds_importer_digest', 'cache');
951
  drupal_static_reset('_feeds_importer_digest');
952
  cache_clear_all('plugins:feeds:plugins', 'cache');
953
  ctools_include('export');
954
  ctools_export_load_object_reset('feeds_importer');
955
  drupal_static_reset('_node_types_build');
956
  if ($rebuild_menu) {
957
    menu_rebuild();
958
  }
959
}
960

    
961
/**
962
 * Exports a FeedsImporter configuration to code.
963
 */
964
function feeds_export($importer_id, $indent = '') {
965
  ctools_include('export');
966
  $result = ctools_export_load_object('feeds_importer', 'names', array('id' => $importer_id));
967
  if (isset($result[$importer_id])) {
968
    return ctools_export_object('feeds_importer', $result[$importer_id], $indent);
969
  }
970
}
971

    
972
/**
973
 * Logs to a file like /tmp/feeds_my_domain_org.log in temporary directory.
974
 */
975
function feeds_dbg($msg) {
976
  if (variable_get('feeds_debug', FALSE)) {
977
    if (!is_string($msg)) {
978
      $msg = var_export($msg, TRUE);
979
    }
980
    $filename = trim(str_replace('/', '_', $_SERVER['HTTP_HOST'] . base_path()), '_');
981
    $handle = fopen("temporary://feeds_$filename.log", 'a');
982
    fwrite($handle, gmdate('c') . "\t$msg\n");
983
    fclose($handle);
984
  }
985
}
986

    
987
/**
988
 * Writes to feeds log.
989
 */
990
function feeds_log($importer_id, $feed_nid, $type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
991
  if ($severity < WATCHDOG_NOTICE) {
992
    $error = &drupal_static('feeds_log_error', FALSE);
993
    $error = TRUE;
994
  }
995
  db_insert('feeds_log')
996
    ->fields(array(
997
      'id' => $importer_id,
998
      'feed_nid' => $feed_nid,
999
      'log_time' => time(),
1000
      'request_time' => REQUEST_TIME,
1001
      'type' => $type,
1002
      'message' => $message,
1003
      'variables' => serialize($variables),
1004
      'severity' => $severity,
1005
    ))
1006
    ->execute();
1007
}
1008

    
1009
/**
1010
 * Loads an item info object.
1011
 *
1012
 * Example usage:
1013
 *
1014
 * $info = feeds_item_info_load('node', $node->nid);
1015
 */
1016
function feeds_item_info_load($entity_type, $entity_id) {
1017
  return db_select('feeds_item')
1018
    ->fields('feeds_item')
1019
    ->condition('entity_type', $entity_type)
1020
    ->condition('entity_id', $entity_id)
1021
    ->execute()
1022
    ->fetchObject();
1023
}
1024

    
1025
/**
1026
 * Inserts an item info object into the feeds_item table.
1027
 */
1028
function feeds_item_info_insert($entity, $entity_id) {
1029
  if (isset($entity->feeds_item)) {
1030
    $entity->feeds_item->entity_id = $entity_id;
1031
    drupal_write_record('feeds_item', $entity->feeds_item);
1032
  }
1033
}
1034

    
1035
/**
1036
 * Inserts or updates an item info object in the feeds_item table.
1037
 */
1038
function feeds_item_info_save($entity, $entity_id) {
1039
  if (isset($entity->feeds_item)) {
1040
    $entity->feeds_item->entity_id = $entity_id;
1041
    if (feeds_item_info_load($entity->feeds_item->entity_type, $entity_id)) {
1042
      drupal_write_record('feeds_item', $entity->feeds_item, array('entity_type', 'entity_id'));
1043
    }
1044
    else {
1045
      feeds_item_info_insert($entity, $entity_id);
1046
    }
1047
  }
1048
}
1049

    
1050
/**
1051
 * @}
1052
 */
1053

    
1054
/**
1055
 * @defgroup instantiators Instantiators
1056
 * @{
1057
 */
1058

    
1059
/**
1060
 * Gets an importer instance.
1061
 *
1062
 * @param $id
1063
 *   The unique id of the importer object.
1064
 *
1065
 * @return
1066
 *   A FeedsImporter object or an object of a class defined by the Drupal
1067
 *   variable 'feeds_importer_class'. There is only one importer object
1068
 *   per $id system-wide.
1069
 */
1070
function feeds_importer($id) {
1071
  return FeedsConfigurable::instance(variable_get('feeds_importer_class', 'FeedsImporter'), $id);
1072
}
1073

    
1074
/**
1075
 * Gets an instance of a source object.
1076
 *
1077
 * @param $importer_id
1078
 *   A FeedsImporter id.
1079
 * @param $feed_nid
1080
 *   The node id of a feed node if the source is attached to a feed node.
1081
 *
1082
 * @return
1083
 *   A FeedsSource object or an object of a class defiend by the Drupal
1084
 *   variable 'source_class'.
1085
 */
1086
function feeds_source($importer_id, $feed_nid = 0) {
1087
  return FeedsSource::instance($importer_id, $feed_nid);
1088
}
1089

    
1090
/**
1091
 * Gets an instance of a class for a given plugin and id.
1092
 *
1093
 * @param string $plugin
1094
 *   A string that is the key of the plugin to load.
1095
 * @param string $id
1096
 *   A string that is the id of the object.
1097
 *
1098
 * @return FeedsPlugin
1099
 *   A FeedsPlugin object.
1100
 */
1101
function feeds_plugin($plugin, $id) {
1102
  ctools_include('plugins');
1103

    
1104
  if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
1105
    return FeedsPlugin::instance($class, $id, ctools_get_plugins('feeds', 'plugins', $plugin));
1106
  }
1107

    
1108
  $args = array('%plugin' => $plugin, '@id' => $id);
1109
  if (user_access('administer feeds')) {
1110
    $args['@link'] = url('admin/structure/feeds/' . $id);
1111
    drupal_set_message(t('Missing Feeds plugin %plugin. See <a href="@link">@id</a>. Check whether all required libraries and modules are installed properly.', $args), 'warning', FALSE);
1112
  }
1113
  else {
1114
    drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
1115
  }
1116

    
1117
  $class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');
1118

    
1119
  return FeedsPlugin::instance($class, $id);
1120
}
1121

    
1122
/**
1123
 * @}
1124
 */
1125

    
1126
/**
1127
 * @defgroup include Funtions for loading libraries
1128
 * @{
1129
 */
1130

    
1131
/**
1132
 * Includes a library file.
1133
 *
1134
 * @param string $file
1135
 *   The filename to load from.
1136
 * @param string $library
1137
 *   The name of the library. If libraries module is installed,
1138
 *   feeds_include_library() will look for libraries with this name managed by
1139
 *   libraries module.
1140
 */
1141
function feeds_include_library($file, $library) {
1142
  static $included = array();
1143

    
1144
  $key = $library . '/' . $file;
1145

    
1146
  if (!isset($included[$key])) {
1147
    $included[$key] = FALSE;
1148

    
1149
    $library_dir = variable_get('feeds_library_dir', FALSE);
1150
    $feeds_library_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file";
1151
    $libraries_path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
1152

    
1153
    // Try first whether libraries module is present and load the file from
1154
    // there. If this fails, require the library from the local path.
1155
    if ($libraries_path && is_file("$libraries_path/$file")) {
1156
      require "$libraries_path/$file";
1157
      $included[$key] = TRUE;
1158
    }
1159
    elseif (is_file(DRUPAL_ROOT . '/sites/all/libraries/' . $key)) {
1160
      require DRUPAL_ROOT . '/sites/all/libraries/' . $key;
1161
      $included[$key] = TRUE;
1162
    }
1163
    elseif ($library_dir && is_file($library_dir . '/' . $key)) {
1164
      require $library_dir . '/' . $key;
1165
      $included[$key] = TRUE;
1166
    }
1167
    elseif (is_file($feeds_library_path)) {
1168
      // @todo: Throws "Deprecated function: Assigning the return value of new
1169
      // by reference is deprecated."
1170
      require $feeds_library_path;
1171
      $included[$key] = TRUE;
1172
    }
1173
  }
1174

    
1175
  return $included[$key];
1176
}
1177

    
1178
/**
1179
 * Checks whether a library is present.
1180
 *
1181
 * @param string $file
1182
 *   The filename to load from.
1183
 * @param string $library
1184
 *   The name of the library. If libraries module is installed,
1185
 *   feeds_library_exists() will look for libraries with this name managed by
1186
 *   libraries module.
1187
 */
1188
function feeds_library_exists($file, $library) {
1189
  $path = module_exists('libraries') ? libraries_get_path($library) : FALSE;
1190
  if ($path && is_file($path . '/' . $file)) {
1191
    return TRUE;
1192
  }
1193
  elseif (is_file(DRUPAL_ROOT . "/sites/all/libraries/$library/$file")) {
1194
    return TRUE;
1195
  }
1196
  elseif (is_file(DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {
1197
    return TRUE;
1198
  }
1199
  elseif ($library_dir = variable_get('feeds_library_dir', FALSE)) {
1200
    if (is_file("$library_dir/$library/$file")) {
1201
      return TRUE;
1202
    }
1203
  }
1204

    
1205
  return FALSE;
1206
}
1207

    
1208
 /**
1209
 * Checks whether simplepie exists.
1210
 */
1211
function feeds_simplepie_exists() {
1212
  return (
1213
    feeds_library_exists('autoloader.php', 'simplepie') ||
1214
    feeds_library_exists('simplepie.compiled.php', 'simplepie') ||
1215
    feeds_library_exists('simplepie.mini.php', 'simplepie') ||
1216
    feeds_library_exists('simplepie.inc', 'simplepie')
1217
  );
1218
}
1219

    
1220
/**
1221
 * Includes the simplepie library.
1222
 */
1223
function feeds_include_simplepie() {
1224
  $files = array(
1225
    'autoloader.php',
1226
    'simplepie.mini.php',
1227
    'simplepie.compiled.php',
1228
    'simplepie.inc',
1229
  );
1230

    
1231
  foreach ($files as $file) {
1232
    if (feeds_include_library($file, 'simplepie')) {
1233
      return TRUE;
1234
    }
1235
  }
1236

    
1237
  return FALSE;
1238
}
1239

    
1240
/**
1241
 * @deprecated
1242
 *
1243
 * Simplified drupal_alter().
1244
 *
1245
 * - None of that 'multiple parameters by ref' crazyness.
1246
 * - Don't use module_implements() to allow hot including on behalf
1247
 *   implementations (see mappers/).
1248
 *
1249
 * @todo This needs to be removed and drupal_alter() used. This is crazy dumb.
1250
 */
1251
function feeds_alter($type, &$data) {
1252
  $args = array(&$data);
1253
  $additional_args = func_get_args();
1254
  array_shift($additional_args);
1255
  array_shift($additional_args);
1256
  $args = array_merge($args, $additional_args);
1257

    
1258
  $hook = $type . '_alter';
1259
  foreach (module_list() as $module) {
1260
    if (module_hook($module, $hook)) {
1261
      call_user_func_array($module . '_' . $hook, $args);
1262
    }
1263
  }
1264
}
1265

    
1266
/**
1267
 * @}
1268
 */
1269

    
1270
/**
1271
 * Copy of valid_url() that supports the webcal scheme.
1272
 *
1273
 * @see valid_url().
1274
 *
1275
 * @todo Replace with valid_url() when http://drupal.org/node/295021 is fixed.
1276
 */
1277
function feeds_valid_url($url, $absolute = FALSE) {
1278
  if ($absolute) {
1279
    return (bool) preg_match("
1280
      /^                                                      # Start at the beginning of the text
1281
      (?:ftp|https?|feed|webcal):\/\/                         # Look for ftp, http, https, feed or webcal schemes
1282
      (?:                                                     # Userinfo (optional) which is typically
1283
        (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password
1284
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@          # combination
1285
      )?
1286
      (?:
1287
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address
1288
        |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\])         # or a well formed IPv6 address
1289
      )
1290
      (?::[0-9]+)?                                            # Server port number (optional)
1291
      (?:[\/|\?]
1292
        (?:[|\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})   # The path and query (optional)
1293
      *)?
1294
    $/xi", $url);
1295
  }
1296
  else {
1297
    return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
1298
  }
1299
}
1300

    
1301
/**
1302
 * Registers a feed subscription job for execution on feeds_exit().
1303
 *
1304
 * @param array $job
1305
 *   Information about a new job to queue; or if set to NULL (default), leaves
1306
 *   the current queued jobs unchanged.
1307
 *
1308
 * @return
1309
 *   An array of subscribe jobs to process.
1310
 *
1311
 * @see feeds_exit()
1312
 * @see feeds_get_subscription_jobs()
1313
 */
1314
function feeds_set_subscription_job(array $job = NULL) {
1315
  $jobs = &drupal_static(__FUNCTION__, array());
1316
  if (isset($job)) {
1317
    $jobs[] = $job;
1318
  }
1319
  return $jobs;
1320
}
1321

    
1322
/**
1323
 * Returns the list of queued jobs to be run.
1324
 *
1325
 * @return
1326
 *   An array of subscribe jobs to process.
1327
 *
1328
 * @see feeds_set_subscription_job()
1329
 */
1330
function feeds_get_subscription_jobs() {
1331
  return feeds_set_subscription_job();
1332
}
1333

    
1334
/**
1335
 * Implements hook_entity_property_info_alter().
1336
 */
1337
function feeds_entity_property_info_alter(&$info) {
1338

    
1339
  foreach ($info as $entity_type => $entity_info) {
1340
    $info[$entity_type]['properties']['feed_nid'] = array(
1341
      'label' => 'Feed NID',
1342
      'type' => 'integer',
1343
      'description' => t('Nid of the Feed Node that imported this entity.'),
1344
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
1345
    );
1346
    $info[$entity_type]['properties']['feed_node'] = array(
1347
      'label' => 'Feed node',
1348
      'type' => 'node',
1349
      'description' => t('Feed Node that imported this entity.'),
1350
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
1351
    );
1352
  }
1353
}
1354

    
1355
/**
1356
 * Gets the feed_nid for an entity for use in entity metadata.
1357
 */
1358
function feeds_get_feed_nid_entity_callback($entity, array $options, $name, $entity_type) {
1359
  list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
1360

    
1361
  $feed_nid = NULL;
1362
  if ($entity_id) {
1363
    $feed_nid = feeds_get_feed_nid($entity_id, $entity_type);
1364
    if ($feed_nid === FALSE) {
1365
      return NULL;
1366
    }
1367
  }
1368
  // If the entity has no ID (yet) try read the feed nid from the object
1369
  // directly.
1370
  elseif (isset($entity->feeds_item->feed_nid)) {
1371
    $feed_nid = $entity->feeds_item->feed_nid;
1372
  }
1373
  return $feed_nid;
1374
}
1375

    
1376
/**
1377
 * Implements hook_file_download().
1378
 */
1379
function feeds_file_download($uri) {
1380
  $id = db_query("SELECT id FROM {feeds_source} WHERE source = :uri", array(':uri' => $uri))->fetchField();
1381

    
1382
  if (!$id) {
1383
    // File is not associated with a feed.
1384
    return;
1385
  }
1386

    
1387
   // Get the file record based on the URI. If not in the database just return.
1388
  $files = file_load_multiple(array(), array('uri' => $uri));
1389
  foreach ($files as $item) {
1390
    // Since some database servers sometimes use a case-insensitive comparison
1391
    // by default, double check that the filename is an exact match.
1392
    if ($item->uri === $uri) {
1393
      $file = $item;
1394
      break;
1395
    }
1396
  }
1397
  if (!isset($file)) {
1398
    return;
1399
  }
1400

    
1401
  // Check if this file belongs to Feeds.
1402
  $usage_list = file_usage_list($file);
1403
  if (!isset($usage_list['feeds'])) {
1404
    return;
1405
  }
1406

    
1407
  if (!feeds_access('import', $id)) {
1408
    // User does not have permission to import this feed.
1409
    return -1;
1410
  }
1411

    
1412
  // Return file headers.
1413
  return file_get_content_headers($file);
1414
}
1415

    
1416
/**
1417
 * Feeds API version.
1418
 */
1419
function feeds_api_version() {
1420
  $version = feeds_ctools_plugin_api('feeds', 'plugins');
1421
  return $version['version'];
1422
}