- <?php
-
-
- * @file
- * Grabs the primary image from the feed-item and
- * stores into a CCK imagefield.
- */
-
- define('FEEDAPI_IMAGEGRABBER_CRON_FEEDS', 20);
-
- * Implementation of hook_help().
- */
- function feedapi_imagegrabber_help($path, $arg) {
- switch ($path) {
- case 'admin/help#feedapi_imagegrabber':
- $output = '<p>'. t('Provides a parser for <a href="@feedapi">FeedAPI</a>, which visits the original posts of a feed and downloads the main image from the post.', array('@feedapi' => url('admin/help/feedapi'))) .'</p>';
- $output .= '<p>'. t('FeedAPI ImageGrabber mimics the thumbnail display of Google Reader on your Drupal website.') .'</p>';
- $output .= '<p>'. t('After downloading the main image from the post, it is stored with the node created by FeedAPI Node, inside an Imagefield. You can add new imagefields to a content type by clicking <i>manage fields</i> on the <a href="@content-types">content types</a> page. You can also use ImageCache and ImageAPI to add presets to the imagefields', array('@content-types' => url('admin/content/types'))) .'</p>';
- $output .= '<p>'. t('Usually ImageGrabber will search the whole of the web-page for an appropriate image, but you can restrict its search by specifying the id/class of the tag between which, you think, is the appropriate image. Please look <a href="@id-class">here</a> for a tutorial on id/class', array('@id-class' => url('http://www.w3.org/TR/html401/struct/global.html#h-7.5.2'))) .'</p>';
- $output .= '<p>'. t('I have prepared a turorial for FeedAPI ImageGrabber. Please look <a href="@tutorial">here</a> for help. Please open issues in the support forums if you face any issues.', array('@tutorial' => url('http://publicmind.in/blog/tutorial-for-feedapi-imagegrabber'))) .'</p>';
- return $output;
- }
- }
-
- * Implementation of hook_menu().
- */
- function feedapi_imagegrabber_menu() {
- $items = array();
- $items['node/%node/grabber'] = array(
- 'title' => 'Grab Images',
- 'page callback' => 'feedapi_imagegrabber_download_images',
- 'page arguments' => array(1),
- 'type' => MENU_LOCAL_TASK,
- 'access callback' => '_feedapi_imagegrabber_op_access',
- 'access arguments' => array(1),
- );
-
- return $items;
- }
-
- * helper access function for hook_menu().
- */
- function _feedapi_imagegrabber_op_access($node) {
- $enabled = feedapi_imagegrabber_is_enabled($node->nid);
- if (!isset($enabled) || $enabled == 0 || $enabled == '0') {
- return FALSE;
- }
- global $user;
- $own_feed = $node->uid == $user->uid && user_access('edit own '. $node->type .' content') ? TRUE : FALSE;
- return user_access('administer imagegrabber') || $own_feed;
- }
-
- * Implementation of hook_perm().
- */
- function feedapi_imagegrabber_perm() {
- return array('administer imagegrabber', 'enable imagegrabber');
- }
-
- * validates and uploads the image file into the cck image field
- *
- * @param $filename path to the image to be uploaded
- * @param $field cck image field instance to insert into which image is
- * uploaded (check before passing that this field exists)
- *
- * @return
- * fid of the image if the image is uploaded successfuly,
- * FALSE if any error occurs.
- */
- function feedapi_imagegrabber_image_upload($filename, $field, $replace = FILE_EXISTS_RENAME) {
-
- if (empty($filename)) {
- return 0;
- }
-
- $field_instance = content_fields($field);
- $dest = filefield_widget_file_path($field_instance);
-
- if (!field_file_check_directory($dest, FILE_CREATE_DIRECTORY)) {
- drupal_set_message(t('ImageGrabber: %dir directory is not writable, Please check the permissions', array('%dir' => $dest)), 'error', FALSE);
- return 0;
- }
-
- global $user;
-
- $validators1 = imagefield_widget_upload_validators($field_instance);
- $validators2 = filefield_widget_upload_validators($field_instance);
- $validators = array_merge($validators1, $validators2);
-
- $validators['file_validate_name_length'] = array();
- $image_extensions = 'jpg jpeg gif png';
-
-
- $file = new stdClass();
- $file->filename = file_munge_filename(trim(basename($filename)), $image_extensions);
- $file->filepath = $filename;
- $file->filemime = file_get_mimetype($file->filename);
-
-
-
- if (preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
- return 0;
- }
-
- $file->source = $field ."_imagegrabber";
-
- $destination_path = file_destination(file_create_path($dest .'/'. $file->filename), $replace);
- $path_original = $filename;
- if (file_copy($filename, $destination_path, FILE_EXISTS_REPLACE)) {
- if (!($path_original == $destination_path || file_delete($path_original))) {
- drupal_set_message(t('ImageGrabber: Please remove the file %file, ImageGrabber was unable to do so.', array('%file' => $path_original)), 'warning');
- }
- }
- else {
- return 0;
- }
-
- $filename = $destination_path;
- $file->filename = file_munge_filename(trim(basename($filename)), $image_extensions);
- $file->destination = $filename;
- $file->filepath = $filename;
- chmod($filename, 0744);
- if (!($filesize = filesize($filename))) {
- drupal_set_message(t('ImageGrabber: Filesize for some files couldn\'t be calculated and aren\'t saved.'), 'error', FALSE);
- file_delete($filename);
- return 0;
- }
-
- $file->filesize = $filesize;
-
-
- $errors = array();
- foreach ($validators as $function => $args) {
- array_unshift($args, $file);
- $errors = array_merge($errors, call_user_func_array($function, $args));
- }
-
-
- if (!empty($errors)) {
- $message = t('ImageGrabber: The selected file %name could not be uploaded.', array('%name' => $file->filename));
- if (count($errors) > 1) {
- $message .= '<ul><li>'. implode('</li><li>', $errors) .'</li></ul>';
- }
- else {
- $message .= ' '. array_pop($errors);
- }
- drupal_set_message($message, 'error', FALSE);
- file_delete($filename);
- return 0;
- }
-
- $file->uid = $user->uid;
- $file->status = FILE_STATUS_TEMPORARY;
- $file->timestamp = time();
-
- drupal_write_record('files', $file);
-
-
-
- foreach (module_implements('file_insert') as $module) {
- $function = $module .'_file_insert';
- $function($file);
- }
-
- return $file;
- }
-
- * Implementation of hook_form_alter().
- */
- function feedapi_imagegrabber_form_alter(&$form, $form_state, $form_id) {
-
- if (user_access('enable imagegrabber')) {
- if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id && feedapi_enabled_type($form['type']['#value'])) {
-
-
- if (!isset($form['imagegrabber'])) {
- $form['imagegrabber'] = array();
- }
- $form['imagegrabber'] += array(
- '#type' => 'fieldset',
- '#title' => t('ImageGrabber'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#tree' => TRUE,
- );
-
- $node = $form['#node'];
- if (isset($node->nid)) {
- $feedapi_settings = feedapi_get_settings($node->type, $node->vid);
- if (isset($feedapi_settings['processors']['feedapi_node'])) {
- $is_feedapi_node_enabled = TRUE;
- }
- else $is_feedapi_node_enabled = FALSE;
- }
- else {
- $feedapi_settings = feedapi_get_settings($node->type);
- if (isset($feedapi_settings['processors']['feedapi_node']['enabled'])) {
- $is_feedapi_node_enabled = $feedapi_settings['processors']['feedapi_node']['enabled'];
- }
- else $is_feedapi_node_enabled = FALSE;
- }
-
- if (!$is_feedapi_node_enabled) {
- $form['imagegrabber']['value'] = array(
- '#type' => 'value',
- '#value' => $is_feedapi_node_enabled,
- );
-
- $form['imagegrabber']['message'] = array(
- '#title' => t('Enable the feedapi_node processor for this feed to use Imagegrabber'),
- '#type' => 'item',
- '#description' => t('ImageGrabber settings have been removed, because it needs the feedapi_node processor. Enable the feedapi_node processor for this feed to use ImageGrabber.'),
- );
-
- return;
- }
-
- if (!isset($node->nid) || feedapi_imagegrabber_get_settings($node->nid) == FALSE) {
-
- $enabled = 0;
- $image_field = '0';
- $id_class = 0;
- $id_class_description = '';
- $feeling_lucky = 0;
- }
- else {
-
- $imagegrabber_settings = feedapi_imagegrabber_get_settings($node->nid);
- $enabled = $imagegrabber_settings['enabled'];
- $image_field = $imagegrabber_settings['image_field'];
- $id_class = $imagegrabber_settings['id_class'];
- $id_class_description = $imagegrabber_settings['id_class_description'];
- $feeling_lucky = $imagegrabber_settings['feeling_lucky'];
- }
-
- $content_type_processor = $feedapi_settings['processors']['feedapi_node']['content_type'];
- $options = array(t('Select an Image field'),
- );
-
- if (isset($content_type_processor) && !empty($content_type_processor)) {
- $type = content_types($content_type_processor);
- $fields = $type['fields'];
- foreach ($fields as $type => $args) {
- if ($args['widget']['type'] == 'imagefield_widget') {
- $imagefield = $args['field_name'];
- $options[$imagefield] = $args['field_name'];
- }
- }
- if (count($options) == 1) {
- $form['imagegrabber']['value'] = array(
- '#type' => 'value',
- '#value' => 0,
- );
-
- $form['imagegrabber']['message'] = array(
- '#title' => t('There are no imagefields associated with <i>@node</i> content type.', array('@node' => $content_type_processor)),
- '#type' => 'item',
- '#description' => t('ImageGrabber stores the image in an image field of the feed item node. The content type for the feed-item of this feed is <i>@node</i>. Add Imagefields to <i>@node</i> content type in order to enable ImageGrabber.', array('@node' => $content_type_processor)),
- );
-
- return;
- }
- }
- else {
- $form['imagegrabber']['value'] = array(
- '#type' => 'value',
- '#value' => 0,
- );
-
- $form['imagegrabber']['message'] = array(
- '#title' => t('ImageGraber could not find the associated content type for the feed-item. Please try re-loading the page, otherwise report on the support forums.'),
- '#type' => 'item',
- '#description' => t('This is an un-expected error. It might be due to change in APIs of other dependent modules. Please report the issue at the earliest.'),
- );
- return;
- }
-
- $form['imagegrabber']['content_type_node'] = array(
- '#type' => 'value',
- '#value' => $content_type_processor,
- );
-
- $form['imagegrabber']['enabled'] = array(
- '#type' => 'checkbox',
- '#title' => t('Enable ImageGrabber for this feed'),
- '#description' => t('Check if you want to download the images of feed items to your site.'),
- '#default_value' => $enabled,
- '#weight' => -17,
- );
-
- if ($enabled == 1 || $enabled == '1') {
- $default = $image_field;
- }
- else {
- $default = $options[0];
- }
-
- $form['imagegrabber']['image_field'] = array(
- '#type' => 'select',
- '#title' => t('Store the image in: (Select an Imagefield)'),
- '#description' => t('ImageGrabber stores the image into this image field of the feed item. The content type for the feed-item of this feed is <i>@node</i>', array('@node' => $content_type_processor)),
- '#options' => $options,
- '#default_value' => $default,
- '#weight' => -16,
- );
-
- $form['imagegrabber']['id_class'] = array(
- '#type' => 'radios',
- '#title' => t('Search for an image between the tag which is identified by '),
- '#options' => array(t('None, search the whole web-page for the image.'),
- t('an ID'),
- t('a Class'),
- ),
- '#description' => t('Select <i>None</i> if you are not sure what this means. For help, click <a href="@feedapi-imagegrabber">here</a>. Enter the <i>ID</i> or <i>CLASS</i> of the tag in the textfield below. ', array('@feedapi-imagegrabber' => url('http://publicmind.in/blog/tutorial-for-feedapi-imagegrabber'))),
- '#default_value' => $id_class,
- '#weight' => -15,
- );
-
-
- $form['imagegrabber']['id_class_description'] = array(
- '#type' => 'textfield',
- '#title' => t('<i>ID</i> or <i>CLASS</i> of the HTML tag (Leave empty if you selected <i>None</i> above.)'),
- '#description' => t('Enter the <i>ID</i> or <i>CLASS</i> of the html element between which you want ImageGrabber to search for the image. In case of <i>CLASS</i>, the first tag matching the specified <i>CLASS</i> will be selected. Note that both <i>ID</i> and <i>CLASS</i> are CASE-SENSITIVE. For help, click <a href="@feedapi-imagegrabber">here</a>.', array('@feedapi-imagegrabber' => url('http://publicmind.in/blog/tutorial-for-feedapi-imagegrabber'))),
- '#default_value' => $id_class_description,
- '#maxlength' => 100,
- '#weight' => -14,
- );
-
-
- $form['imagegrabber']['feeling_lucky'] = array(
- '#type' => 'radios',
- '#title' => t('Feeling Lucky, huh?'),
- '#options' => array(t('No, Select the largest image between the tag.'),
- t('Yes, Select the first image between the tag. (Recommended)'),
- ),
- '#default_value' => $feeling_lucky,
- '#weight' => -13,
- );
-
-
- $form['imagegrabber']['value'] = array(
- '#type' => 'value',
- '#value' => 1,
- );
-
- $form['#validate'][] = 'feedapi_imagegrabber_form_node_validate';
- }
- }
- }
-
- * validates the form altered for the feedapi enabled content type node.
- */
- function feedapi_imagegrabber_form_node_validate($form, &$form_state) {
- $value = $form_state['values']['imagegrabber']['value'];
- if ($value == 1) {
- $enabled = $form['imagegrabber']['enabled']['#value'];
- $image_field = $form['imagegrabber']['image_field']['#value'];
-
- if ($enabled == 1) {
- if ($image_field == '0') {
- form_set_error("imagegrabber']['image_field", "ImageGrabber is enabled but no image field is selected");
- return;
- }
-
- $id_class = $form['imagegrabber']['id_class']['#value'];
- $id_class_desc = $form['imagegrabber']['id_class_description']['#value'];
-
- if ($id_class != 0) {
- if (!isset($id_class_desc) || empty($id_class_desc) || $id_class_desc == '') {
- form_set_error("imagegrabber']['id_class_description", "ImageGrabber: ID or Class field can not be empty.");
- return;
- }
- if (!preg_match('/^[a-zA-Z]+[_a-zA-Z0-9-]*$/', $id_class_desc)) {
- form_set_error("imagegrabber']['id_class_description", "ImageGrabber: ID/CLASS string is not valid. Only alphabets, digits, hyphens and underscores are allowed.");
- return;
- }
- }
-
- if (!isset($form_state['values']['feedapi']['processors']['feedapi_node'])) {
- form_set_value($form['imagegrabber']['enabled'], 0, $form_state);
- drupal_set_message(t('ImageGrabber has been disabled for this feed as it depends on feedapi_node processor and it was unfortunately disabled.'), 'warning');
- return;
- }
- $content_type_node_new = $form['feedapi']['processors']['feedapi_node']['content_type']['#value'];
-
- $content_type_node_old = $form['imagegrabber']['content_type_node']['#value'];
- if (strcmp($content_type_node_new, $content_type_node_old) != 0) {
- form_set_value($form['imagegrabber']['enabled'], 0, $form_state);
- drupal_set_message(t('ImageGrabber has been disabled for this feed becuase you changed the content type of the feed items. Please edit the feed again to enable ImageGrabber'), 'warning');
- return;
- }
- }
- }
- }
-
- * Implementation of hook_nodeapi().
- */
- function feedapi_imagegrabber_nodeapi(&$node, $op, $teaser, $page) {
- if (feedapi_enabled_type($node->type)) {
- switch ($op) {
- case 'insert':
- case 'update':
- $value = $node->imagegrabber['value'];
- $nid = $node->nid;
- if ($value && isset($nid)) {
- $enabled = $node->imagegrabber['enabled'];
- if ($enabled == 1) {
- $image_field = $node->imagegrabber['image_field'];
- $id_class = $node->imagegrabber['id_class'];
- if ($id_class != 0) {
- $id_class_desc = $node->imagegrabber['id_class_description'];
- }
- else {
- $id_class_desc = '';
- }
- $feeling_lucky = $node->imagegrabber['feeling_lucky'];
- db_query("UPDATE {feedapi_imagegrabber} SET enabled = %d , image_field = '%s' , id_class = %d , id_class_description = '%s' , feeling_lucky = %d WHERE nid = %d", $enabled, $image_field, $id_class, $id_class_desc, $feeling_lucky, $nid);
- if (!db_affected_rows()) {
- @db_query("INSERT INTO {feedapi_imagegrabber} (nid, enabled, image_field, id_class, id_class_description, feeling_lucky) VALUES (%d, %d, '%s', %d, '%s', %d)", $nid, $enabled, $image_field, $id_class, $id_class_desc, $feeling_lucky);
- }
- }
- else {
- db_query("UPDATE {feedapi_imagegrabber} SET enabled = %d WHERE nid = %d", $enabled, $nid);
- }
- }
- break;
-
- case 'delete':
- $nid = $node->nid;
- @db_query("DELETE FROM {feedapi_imagegrabber} where nid = %d", $nid);
- @db_query("DELETE FROM {feedapi_imagegrabber_cron_nodes} where feed_nid = %d", $nid);
- break;
- }
- }
- }
-
- * Implementation of hook_feedapi_after_refresh().
- *
- * Called after feed is refreshed. It downloads the images
- * and stores it into the cck image field.
- *
- * @param $feed the feed object which contains all the feed items.
- * $is_cron if it is called by us or feedapi
- */
- function feedapi_imagegrabber_feedapi_after_refresh($feed, $is_cron = FALSE) {
-
- if (!user_access('administer imagegrabber')) {
- return;
- }
-
- $feed = (array)$feed;
- if (!isset($feed['nid'])) {
- return;
- }
- $feed_nid = $feed['nid'];
- $settings = feedapi_imagegrabber_get_settings($feed_nid);
- if ($settings == FALSE) {
- return;
- }
- if (!function_exists('curl_exec')) {
- drupal_set_message(t('ImageGrabber: The cURL Library for PHP is missing or outdated. Go to <a href="!admin-reports-status">Status Report page</a>', array('!admin-reports-status' => url('admin/reports/status'))), 'error');
- return;
- }
- $path = drupal_get_path('module', 'feedapi_imagegrabber') .'/url_to_absolute.php';
- if (!file_exists($path)) {
- drupal_set_message(t('ImageGrabber: The url conversion script is missing. Go to <a href="!admin-reports-status">Status Report page</a>', array('!admin-reports-status' => url('admin/reports/status'))), 'error');
- return;
- }
- require_once($path);
-
- $enabled = $settings['enabled'];
- $image_field = $settings['image_field'];
- $id_class = $settings['id_class'];
- $id_class_description = $settings['id_class_description'];
- $feeling_lucky = $settings['feeling_lucky'];
-
- if (!isset($enabled) || $enabled == 0 || $enabled == '0') {
- return;
- }
-
- if ($enabled == 1 && (!isset($image_field) || empty($image_field) || $image_field == '0')) {
- return;
- }
-
- if (($id_class == 1 || $id_class == 2) && (!isset($id_class_description) || empty($id_class_description) || $id_class_description == '')) {
- return;
- }
-
- $cron_items = array();
- $cron_empty = TRUE;
- foreach ($feed['items'] as $item => $argument) {
- if (isset($argument->nid)) {
- $nid = $argument->nid;
- }
- else {
- $original_url = $argument->options->original_url;
- if (!isset($original_url) || empty($original_url)) {
- continue;
- }
- $nid = db_fetch_object(db_query("SELECT nid FROM {feedapi_node_item} WHERE url = '%s'", $original_url));
- if (!isset($nid) || empty($nid)) {
- continue;
- }
- }
-
- if (!$cron_empty || !feedapi_imagegrabber_cron_time()) {
- $cron_items[$nid] = $argument->options->original_url;
- $cron_empty = FALSE;
- continue;
- }
- $original_url = $argument->options->original_url;
- if (!($node = node_load($nid))) {
- continue;
- }
- $type = content_types($node->type);
- $fields = $type['fields'];
- if (!(array_key_exists($image_field, $fields))) {
- continue;
- }
- $filename = feedapi_imagegrabber_select_image($original_url, $id_class, $id_class_description, $feeling_lucky);
- if (!$filename) {
- continue;
- }
- if (!($file = feedapi_imagegrabber_image_upload($filename, $image_field))) {
- continue;
- }
- if (isset($file->fid)) {
- $node->$image_field = array(
- array(
- 'fid' => $file->fid,
- 'filename' => $file->filename,
- 'filepath' => $file->filepath,
- 'status' => $file->status,
- 'filesize' => $file->filesize,
- 'list' => 1,
- ),
- );
-
-
- if (module_exists('filefield_paths')) {
- $node->form_id = $node->type .'_node_form';
- }
-
- node_save($node);
- }
- }
- if ($is_cron) {
- @db_query("DELETE FROM {feedapi_imagegrabber_cron_nodes} where feed_nid = %d", $feed_nid);
- }
- if (!$cron_empty) {
- $data = db_fetch_array(db_query("SELECT feed_nid, nid FROM {feedapi_imagegrabber_cron_nodes} WHERE feed_nid = %d", $feed_nid));
- if ($data != FALSE) {
- $data_nid = unserialize($data['nid']);
- foreach ($cron_items as $node => $url) {
- $data_nid[$node] = $url;
- }
- @db_query("UPDATE {feedapi_imagegrabber_cron_nodes} SET nid = '%s' WHERE feed_nid = %d", serialize($data_nid), $feed_nid);
- }
- else {
- @db_query("INSERT INTO {feedapi_imagegrabber_cron_nodes} (feed_nid, nid) VALUES (%d, '%s')", $feed_nid, serialize($cron_items));
- }
- drupal_set_message(t('ImageGrabber: PHP execution time limit for system is !limit seconds, due to which images for some feed items couldn\'t be downloaded. Please click on \'Grab Images\' to refresh those feed-items.', array('!limit' => ini_get('max_execution_time'))), 'warning');
- }
- else {
- drupal_set_message(t('ImageGrabber completed successfully!!'));
- }
- }
-
- * Implementation of hook_cron().
- */
- function feedapi_imagegrabber_cron() {
- $process = 0;
- while (!$process && feedapi_imagegrabber_cron_time()) {
- $process = FEEDAPI_IMAGEGRABBER_CRON_FEEDS;
- $result = db_query_range("SELECT feed_nid, nid FROM {feedapi_imagegrabber_cron_nodes}", FEEDAPI_CRON_FEEDS);
-
- while (feedapi_imagegrabber_cron_time() && $data = db_fetch_array($result)) {
- $feed_items = unserialize($data['nid']);
- $feed_nid = $data['feed_nid'];
- $feed = feedapi_imagegrabber_create_feed($feed_nid, $feed_items);
- feedapi_imagegrabber_feedapi_after_refresh($feed, TRUE);
- $process--;
- }
- }
- }
-
- * Check for time limits in feed processing.
- *
- * @return boolean FALSE (time exceeded)
- */
- function feedapi_imagegrabber_cron_time() {
-
- $max_exec_time = ini_get('max_execution_time') == 0 ? 120 : ini_get('max_execution_time') * 2 / 3;
- if ((timer_read('page') / 1000) > $max_exec_time) {
- return FALSE;
- }
- else return TRUE;
- }
-
- * checks whether the ImageGrabber is enabled for this node or not.
- *
- * @param $nid node to detect
- *
- * @return
- * TRUE if ImageGrabber enabled else FALSE
- */
- function feedapi_imagegrabber_is_enabled($nid) {
- $enabled = db_result(db_query("SELECT enabled FROM {feedapi_imagegrabber} WHERE nid = %d", $nid));
- return $enabled;
- }
-
- * returns the image field of the feed-item content type in which
- * ImageGrabber stores the image for the feed-item.
- *
- * @param $nid node to select
- *
- * @return
- * the image field or else FALSE
- */
- function feedapi_imagegrabber_image_field($nid) {
-
- $image_field = db_result(db_query("SELECT image_field FROM {feedapi_imagegrabber} WHERE nid = %d", $nid));
- return $image_field;
- }
-
- * returns the ImageGrabber settings associated with a feed
- *
- * @param $nid node to select
- *
- * @return
- * the settings array or else FALSE
- */
- function feedapi_imagegrabber_get_settings($nid) {
-
- $settings = db_fetch_array(db_query("SELECT enabled, image_field, refresh_on_update, id_class, id_class_description, feeling_lucky FROM {feedapi_imagegrabber} WHERE nid = %d", $nid));
- return $settings;
- }
-
- * Processes the feed options for ImageGrabber and call the appropriate helper
- * function to download the image for the node.
- *
- * @param string $original_url url of the feed-item
- * @param integer $id_class whether tag identified by id or class or none
- * @param string $id_class_description the id or class of the tag
- * @param integer $feeling_lucky
- *
- * @return selected file on success, FALSE on failure
- */
- function feedapi_imagegrabber_select_image($original_url, $id_class, $id_class_description, $feeling_lucky) {
-
- $filename = '';
- $options = array(
-
- CURLOPT_RETURNTRANSFER => TRUE,
-
- CURLOPT_FAILONERROR => TRUE,
-
- CURLOPT_HEADER => FALSE,
-
- CURLOPT_FOLLOWLOCATION => TRUE,
-
- CURLOPT_ENCODING => "",
-
- CURLOPT_USERAGENT => "feedapi_imagegrabber",
-
- CURLOPT_AUTOREFERER => TRUE,
-
- CURLOPT_CONNECTTIMEOUT => 5,
-
- CURLOPT_TIMEOUT => 5,
-
- CURLOPT_MAXREDIRS => 2,
- );
-
- $ch = curl_init($original_url);
- curl_setopt_array($ch, $options);
- $file = curl_exec($ch);
- curl_close($ch);
- if ($file === FALSE) {
- return FALSE;
- }
- $doc = new DOMDocument();
- @$doc->loadHTML($file);
-
- if ($id_class == 0) {
- $items = $doc->getElementsByTagName("body");
- if ($items != NULL && $items->length > 0) {
- $dist = $items->item(0);
- }
- else $dist = NULL;
- }
- elseif ($id_class == 1) {
- $dist = $doc->getElementById($id_class_description);
- }
- elseif ($id_class == 2) {
- $xpath = new DOMXPath($doc);
- $items = $xpath->query("//*[@class and contains(concat(' ',normalize-space(@class),' '),' $id_class_description ')]");
- if ($items != NULL && $items->length > 0) {
- $dist = $items->item(0);
- }
- else $dist = NULL;
- }
- else {
-
- return FALSE;
- }
- if ($dist == NULL) {
- return FALSE;
- }
-
- $xml = $dist->ownerDocument->saveXML($dist);
- if ($feeling_lucky == 0) {
- $filename = feedapi_imagegrabber_search_images_to_select($original_url, $xml);
- return $filename;
- }
- else {
- $filename = feedapi_imagegrabber_download_image_feeling_lucky($original_url, $xml);
- return $filename;
- }
- }
-
- * Browse through all the images between the user defined tag to select the image.
- *
- * @param string $feed_item_url
- * @param string $xml
- *
- * @return filename on suceess, FALSE on failure.
- */
- function feedapi_imagegrabber_search_images_to_select($feed_item_url, $xml) {
-
- $image_found = 0;
- $selected_file = '';
- $selected_url = '';
- $doc = new DOMDocument();
- @$doc->loadXML($xml);
- $xpath = new DOMXPath($doc);
- $hrefs = $xpath->evaluate("//img");
- $temp_dir = file_directory_temp();
- $size_image_found = 0;
- for ($i = 0; $i < $hrefs->length; $i++) {
- $href = $hrefs->item($i);
- $url = $href->getAttribute('src');
- if (!isset($url) || empty($url) || $url == '') {
- continue;
- }
- $abs = url_to_absolute($feed_item_url, $url);
- if ($abs != FALSE) {
- if (feedapi_imagegrabber_download_image_heuristics($abs, $size_image_found)) {
- $image_found = 1;
- $selected_url = $abs;
- }
- }
- }
- if ($image_found) {
- $selected_file = feedapi_imagegrabber_create_filename($temp_dir, $selected_url);
- if (!feedapi_imagegrabber_get_image($selected_url, $selected_file)) {
- return FALSE;
- }
- else return $selected_file;
- }
- else return FALSE;
- }
-
- * Heuristics for selecting the right image from the original feed item
- * of the URL when $feeling_lucky is FALSE
- *
- * 1) select the image greater than some minimum resolution.(100x150)
- * 2) select the largest amongst the images found.
- *
- * @param $image_url url of the image.
- * @param $imagesize size of the largest image found till now
- *
- * @return boolean
- * TRUE if image url was selected.
- * FALSE if rejected or any other error as timeout, invalid url, etc.
- *
- */
- function feedapi_imagegrabber_download_image_heuristics($image_url, &$imagesize) {
-
- if (!feedapi_imagegrabber_remote_image_exists($image_url)) {
-
- return FALSE;
-
- }
-
- $info = @getimagesize($image_url);
-
- if (isset($info) && is_array($info)) {
-
-
- $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png', '4' => 'jpeg');
- if (array_key_exists($info[2], $extensions)) {
- $height = $info[0];
- $width = $info[1];
- $resolution = $height * $width;
-
- if ($resolution < 15000 || $resolution < $imagesize) {
- return FALSE;
- }
- $imagesize = $resolution;
- return TRUE;
- }
- }
- return FALSE;
- }
-
- * Selects the first image between the user defined tag.
- *
- * @param string $feed_item_url
- * @param string $xml
- *
- * @return filename on suceess, FALSE on failure.
- */
- function feedapi_imagegrabber_download_image_feeling_lucky($feed_item_url, $xml) {
- $temp_dir = file_directory_temp();
- $doc = new DOMDocument();
- @$doc->loadXML($xml);
- $xpath = new DOMXPath($doc);
- $hrefs = $xpath->evaluate("//img");
- if ($hrefs->length <= 0) {
- return FALSE;
- }
- for ($i = 0; $i < $hrefs->length; $i++) {
- $href = $hrefs->item($i);
- $url = $href->getAttribute('src');
- if (!isset($url) || empty($url) || $url == '') {
- continue;
- }
- $abs = url_to_absolute($feed_item_url, $url);
- if ($abs != FALSE) {
- $image_file = feedapi_imagegrabber_create_filename($temp_dir, $abs);
- if (!feedapi_imagegrabber_get_image($abs, $image_file)) {
- continue;
- }
- else {
- return $image_file;
- }
- }
- }
- return FALSE;
- }
-
- * Downloads an image from the given URL and stores it into the given
- * filename.
- *
- * @param $image_url the url of the image
- * @param $filename the name of the file.
- *
- * @return boolean
- * true on sucess else false for timeout, invalid response error or if the
- * downloaded file is not a drupal recognized image.
- */
- function feedapi_imagegrabber_get_image($image_url, $filename) {
- if (function_exists('curl_exec')) {
- $options = array(
-
- CURLOPT_RETURNTRANSFER => TRUE,
-
- CURLOPT_FAILONERROR => TRUE,
-
- CURLOPT_HEADER => FALSE,
-
- CURLOPT_FOLLOWLOCATION => TRUE,
-
- CURLOPT_USERAGENT => "feedapi_imagegrabber",
-
- CURLOPT_AUTOREFERER => TRUE,
-
- CURLOPT_CONNECTTIMEOUT => 5,
-
- CURLOPT_TIMEOUT => 5,
-
- CURLOPT_MAXREDIRS => 2,
- );
- $ch = curl_init($image_url);
- curl_setopt_array($ch, $options);
- $content = curl_exec($ch);
- $err = curl_errno($ch);
- curl_close($ch);
- if (!$err && (file_put_contents($filename, $content) !== FALSE)) {
- return feedapi_imagegrabber_is_image($filename);
- }
- }
- return FALSE;
-
- }
-
- * Checks whether is file is a valid drupal recognized image or not.
- *
- * @param $filepath
- * Path to the file to be checked
- *
- * @return boolean
- */
- function feedapi_imagegrabber_is_image($filepath) {
-
- $info = image_get_info($filepath);
- if (!$info || empty($info['extension'])) {
- return FALSE;
- }
- return TRUE;
- }
-
- * creates a unique filename for the file,
- * Look at [#554980] for more
- *
- * @param string $dir the directory in which file is to created
- * @param string $url the url of the filenme
- * @param integer $count the unique file count
- *
- * @return destination filepath
- */
- function feedapi_imagegrabber_create_filename($dir, $url) {
- $count = time();
- $arr = parse_url($url);
- if ($arr != FALSE) {
- $filename = basename($arr['path']);
- if (isset($filename) && !empty($filename) && $filename != '') {
- $filename_parts = explode('.', $filename);
-
- $new_filename = array_shift($filename_parts);
-
- $final_extension = array_pop($filename_parts);
- $short_filename = drupal_strlen($new_filename) > 100 ? drupal_substr($new_filename, 0, 100) : $new_filename;
- return file_destination(file_create_path($dir .'/'. $short_filename .'_'. $count .'.'. $final_extension), FILE_EXISTS_RENAME);
- }
- }
- return file_destination(file_create_path($dir .'/feedapi_imagegrabber_'. $count), FILE_EXISTS_RENAME);
- }
-
- * page callback for hook_menu().
- */
- function feedapi_imagegrabber_download_images($node) {
- return drupal_get_form('feedapi_imagegrabber_download_confirm', $node);
- }
-
- * confirms the downloading of the images from where it was paused
- */
- function feedapi_imagegrabber_download_confirm($form_state, $node) {
- $output = confirm_form(
- array('nid' => array('#type' => 'hidden', '#value' => $node->nid)),
- t('Grab images for the feed-items of !name', array('!name' => $node->title)), isset($_GET['destination']) ? $_GET['destination'] : 'node/'. $node->nid,
- t("Due to PHP execution time limit, some feed-items might have been left by ImageGrabber during last refresh. Are you sure you want to proceed with the download of those feed-items?"),
- t('Yes'), t('No'),
- 'feedapi_imagegrabber_download_confirm'
- );
- return $output;
- }
-
- * submit for the grab images form.
- */
- function feedapi_imagegrabber_download_confirm_submit($form, &$form_state) {
-
- $nid = $form_state['values']['nid'];
- $data = db_fetch_array(db_query("SELECT feed_nid, nid FROM {feedapi_imagegrabber_cron_nodes} WHERE feed_nid = %d", $nid));
- if ($data == FALSE) {
- drupal_set_message(t('No feed-items available for downloading images.'));
- $form_state['redirect'] = 'node/'. $form_state['values']['nid'];
- return;
- }
- $feed_items = unserialize($data['nid']);
- $feed = feedapi_imagegrabber_create_feed($nid, $feed_items);
- feedapi_imagegrabber_feedapi_after_refresh($feed, TRUE);
- $form_state['redirect'] = 'node/'. $form_state['values']['nid'];
- }
-
- * Creates a feed object from a node => url array.
- *
- * @param int $feed_nid the node of the feed
- * @param mixed $array array of feed items
- *
- * @return $feed, as created by feedapi
- */
- function feedapi_imagegrabber_create_feed($feed_nid, $array) {
- $feed = new stdClass();
- $feed->nid = $feed_nid;
- $feed->items = new stdClass();
- $item = 0;
- foreach ($array as $node => $url) {
- $temp_item = new stdClass();
- $temp_item->nid = $node;
- $options = new stdClass();
- $options->original_url = $url;
- $temp_item->options = $options;
- $feed->items->$item = $temp_item;
- $item++;
- }
- return $feed;
- }
-
- * Checks if a remote file exists and is below some specific size (1 MB)
- *
- * @param $url
- *
- * @return boolean
- */
- function feedapi_imagegrabber_remote_image_exists($url) {
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_NOBODY, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- curl_setopt($ch, CURLOPT_FAILONERROR, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, "feedapi_imagegraber");
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
- curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
-
- $result = curl_exec($ch);
- $ret = FALSE;
- if ($result !== FALSE) {
-
- $max_imagesize = 1024 * 1024;
- $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $content_length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
- if ($statusCode == 200 && is_numeric($content_length) && $content_length < $max_imagesize) {
- $ret = TRUE;
- }
- }
- curl_close($ch);
- return $ret;
- }