You are here

ocupload.module in One Click Upload 7.2

Same filename and directory in other branches
  1. 7 ocupload.module

File

ocupload.module
View source
<?php

/**
 * Implements hook_menu().
 */
function ocupload_menu() {
  $items = array();
  $items['admin/config/content/ocupload'] = array(
    'title' => 'One click upload',
    'description' => 'Configure module',
    'page callback' => 'ocupload_config_page',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ocupload.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/content/ocupload/add'] = array(
    'title' => 'Add template',
    'description' => 'Add template form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ocupload_form_template',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ocupload.inc',
    'type' => MENU_LOCAL_ACTION,
  );
  $items['admin/config/content/ocupload/edit/%ocupload_template'] = array(
    'title' => 'Edit template',
    'description' => 'Edit template form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ocupload_form_template',
      5,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ocupload.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/content/ocupload/delete/%ocupload_template'] = array(
    'title' => 'Delete template',
    'description' => 'Delete template confirm form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ocupload_delete_confirm',
      5,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ocupload.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['ocupload/upload'] = array(
    'title' => 'Upload file',
    'page callback' => 'ocupload_upload',
    'access callback' => TRUE,
    'file' => 'ocupload.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_init().
 */
function ocupload_init() {

  // Remove unused files from session
  if (isset($_SESSION['ocupload'])) {
    $run_clean = FALSE;
    foreach ($_SESSION['ocupload'] as $form_id => $fields) {
      foreach ($fields as $field_name => $files) {
        foreach ($files as $fid => $file) {
          if (REQUEST_TIME - $file['uploaded'] > DRUPAL_MAXIMUM_TEMP_FILE_AGE) {
            unset($_SESSION['ocupload'][$form_id][$field_name][$fid]);
            $run_clean = TRUE;
          }
        }
      }
    }
    if ($run_clean) {
      module_load_include('inc', 'ocupload');
      _ocupload_clean_session();
    }
  }
}

/**
 * Implements hook_permission().
 */
function ocupload_permission() {
  module_load_include('inc', 'ocupload');
  $permissions = array();
  foreach (ocupload_templates() as $template) {
    $permissions['upload files use template ' . $template->tid] = array(
      'title' => t('Upload !extensions files', array(
        '!extensions' => $template->mask,
      )),
      'restrict access' => TRUE,
    );
  }
  return $permissions;
}

/**
 * Implements hook_library().
 */
function ocupload_library() {
  $module_path = _ocupload_get_path();
  $allowed_ext = _ocupload_get_allowed_ext();
  $settings = array(
    'ocupload' => array(
      'allowedExt' => $allowed_ext,
    ),
  );
  $libraries['ocupload'] = array(
    'title' => 'One Click Upload',
    'website' => 'https://www.drupal.org/project/ocupload',
    'version' => '2',
    'js' => array(
      array(
        'data' => $settings,
        'type' => 'setting',
      ),
      $module_path . '/flowjs/dist/flow.js' => array(),
      $module_path . '/js/ocupload.js' => array(),
    ),
    'css' => array(
      $module_path . '/ocupload.css' => array(),
    ),
  );
  if (variable_get('ocupload_textarea_dragndrop', 1)) {
    $libraries['ocupload']['js'][$module_path . '/js/textarea.js'] = array();
  }
  return $libraries;
}

/**
 * Implements hook_preprocess_html().
 */
function ocupload_preprocess_html(&$vars) {
  $scripts = drupal_add_js();
  $item = menu_get_item();
  $module_path = _ocupload_get_path();
  $add_library = FALSE;

  // Check BUEditor
  if (module_exists('bueditor') && (isset($scripts[drupal_get_path('module', 'bueditor') . '/bueditor.js']) || $item['path'] == 'admin/config/content/bueditor/%')) {
    drupal_add_js($module_path . '/js/bueditor.js', array(
      'weight' => 100,
    ));
    $add_library = TRUE;
  }

  // Ckeck CKEditor
  if (module_exists('ckeditor') && isset($scripts[ckeditor_path('url') . '/ckeditor.js'])) {
    $add_library = TRUE;
  }

  // Check Wysiwyg with CKEditor
  if (module_exists('wysiwyg')) {
    foreach ($scripts['settings']['data'] as $data) {
      if (isset($data['wysiwyg']['configs']['ckeditor'])) {
        $add_library = TRUE;
        break;
      }
    }
  }
  if ($add_library && _ocupload_get_allowed_ext()) {
    drupal_add_library('ocupload', 'ocupload');
  }
}

/**
 * Implements hook_element_info_alter().
 */
function ocupload_element_info_alter(&$type) {
  if (isset($type['text_format']) && variable_get('ocupload_textarea_dragndrop', 1)) {
    $type['text_format']['#process'][] = 'ocupload_element_text_format_process';
  }
}

/**
 * text_format process callback.
 */
function ocupload_element_text_format_process($element) {
  if (_ocupload_get_allowed_ext()) {
    $element['#attributes']['class'][] = 'ocupload-drop';
    if (isset($element['value'])) {
      $element['value']['#attributes']['class'][] = 'ocupload-drop';
    }
    if (isset($element['summary'])) {
      $element['summary']['#attributes']['class'][] = 'ocupload-drop';
    }
    $element['#attached']['library'][] = array(
      'ocupload',
      'ocupload',
    );
  }
  return $element;
}

/**
 * Implements hook_form_alter().
 */
function ocupload_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#method'] == 'post') {
    if (!isset($form['#submit'])) {
      $form['#submit'][] = 'ocupload_change_files_status';
    }
    else {
      array_unshift($form['#submit'], 'ocupload_change_files_status');
    }
  }
}

/**
 * Custom form submit. Change status uploaded files to "permanent".
 */
function ocupload_change_files_status($form, &$form_state) {
  $form_id = $form['#form_id'];
  if (isset($_SESSION['ocupload'][$form_id])) {
    module_load_include('inc', 'ocupload');
    $fields_map = array(
      'user_profile_form' => array(
        'signature[value]' => 'signature',
      ),
    );
    drupal_alter('ocupload_fields_map', $fields_map, $form, $form_state);
    $permanent_files = array();
    foreach ($_SESSION['ocupload'][$form_id] as $field_name => $files) {
      $form_state_field_name = isset($fields_map[$form_id][$field_name]) ? $fields_map[$form_id][$field_name] : $field_name;
      $field_value = _ocupload_field_value_by_html_name($form_state, $form_state_field_name);
      if (!$field_value) {
        continue;
      }
      foreach ($files as $fid => $file_info) {

        // Check file in text
        if (strpos($field_value, $file_info['url']) !== FALSE) {

          // Change file status to permanent
          $file = file_load($fid);
          $file->status = FILE_STATUS_PERMANENT;
          file_save($file);

          // Attach file to field
          $file_extension = _ocupload_get_file_extension($file_info['uri']);
          $template = _ocupload_get_appropriate_template($file_extension);
          if ($template->field && isset($form['#entity_type']) && isset($form['#bundle']) && isset($form_state['values'][$template->field])) {
            $form_state['values'][$template->field][LANGUAGE_NONE][] = array(
              'fid' => $file->fid,
              'display' => 0,
              '_weight' => 100,
            );
          }
          unset($_SESSION['ocupload'][$form_id][$field_name][$fid]);
          $permanent_files[] = $file;
        }
      }
    }
    if ($permanent_files) {
      drupal_alter('ocupload_saved_data', $permanent_files, $form, $form_state);
    }

    // Write unused files in log and status message
    if ($_SESSION['ocupload'][$form_id]) {
      $temp_files = array();
      foreach ($_SESSION['ocupload'][$form_id] as $field_name => $files) {
        foreach ($files as $fid => $file_info) {
          if (!$file_info['noticed']) {
            $temp_files[] = l($file_info['url'], $file_info['url']);
            $_SESSION['ocupload'][$form_id][$field_name][$fid]['noticed'] = TRUE;
          }
        }
      }
      if ($temp_files) {
        $temp_files_list = theme('item_list', array(
          'items' => $temp_files,
        ));
        drupal_set_message(t('The following files are not used in text and will be deleted: !files', array(
          '!files' => $temp_files_list,
        )), 'warning');
        watchdog('ocupload', 'The following files are not used in text and will be deleted: !files', array(
          '!files' => $temp_files_list,
        ), WATCHDOG_NOTICE);
      }
    }
    _ocupload_clean_session();
  }
}

/**
 * Return template object by template id (tid).
 */
function ocupload_template_load($tid) {
  return db_select('ocupload_templates', 'ot')
    ->fields('ot')
    ->condition('ot.tid', $tid)
    ->execute()
    ->fetch();
}

/**
 * Return all templates.
 */
function ocupload_templates() {
  static $templates;
  if ($templates === NULL) {
    $templates = db_select('ocupload_templates', 'ot')
      ->fields('ot')
      ->execute()
      ->fetchAllAssoc('tid');
  }
  return $templates;
}

/**
 * Implements hook_wysiwyg_plugin().
 */
function ocupload_wysiwyg_plugin($editor, $version) {
  if ($editor == 'ckeditor') {
    return array(
      'OCUpload' => array(
        'url' => 'https://drupal.org/project/ocupload',
        'path' => _ocupload_get_path() . '/js',
        'load' => TRUE,
        'buttons' => array(
          'OCUpload' => t('One Click Upload'),
        ),
      ),
    );
  }
}

/**
 * Implements hook_ckeditor_plugin().
 */
function ocupload_ckeditor_plugin() {
  return array(
    'ocupload' => array(
      'name' => 'OCUpload',
      'desc' => 'One Click Upload',
      'path' => _ocupload_get_path() . '/js/',
      'buttons' => array(
        'OCUpload' => array(
          'icon' => '../img/icon-ckeditor.png',
          'label' => 'One Click Upload',
        ),
      ),
    ),
  );
}

/**
 * Implements hook_features_api().
 */
function ocupload_features_api() {
  return array(
    'ocupload_templates' => array(
      'name' => t('One Click Upload'),
      'file' => _ocupload_get_path() . '/ocupload.features.inc',
      'default_hook' => 'ocupload_templates',
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
    ),
  );
}

/**
 * Implements hook_cron().
 */
function ocupload_cron() {
  $temporary_dir = 'temporary://ocupload';
  if (file_exists($temporary_dir)) {
    _ocupload_load_flowphp();
    \Flow\Uploader::pruneChunks($temporary_dir, 3600);
  }
}

/**
 * Return module path.
 */
function _ocupload_get_path() {
  static $module_path;
  if ($module_path === NULL) {
    $module_path = drupal_get_path('module', 'ocupload');
  }
  return $module_path;
}

/**
 * Return current user allowed file ext.
 */
function _ocupload_get_allowed_ext() {
  static $allowed_ext;
  if ($allowed_ext === NULL) {
    $allowed_ext = array();
    foreach (ocupload_templates() as $template) {
      if (user_access('upload files use template ' . $template->tid)) {
        $allowed_ext = array_merge($allowed_ext, explode(',', $template->mask));
      }
    }
  }
  return $allowed_ext;
}

/**
 * Load Flow-php library.
 */
function _ocupload_load_flowphp() {
  require_once __DIR__ . '/flowphp/src/Flow/Autoloader.php';
  \Flow\Autoloader::register();
}

Functions

Namesort descending Description
ocupload_change_files_status Custom form submit. Change status uploaded files to "permanent".
ocupload_ckeditor_plugin Implements hook_ckeditor_plugin().
ocupload_cron Implements hook_cron().
ocupload_element_info_alter Implements hook_element_info_alter().
ocupload_element_text_format_process text_format process callback.
ocupload_features_api Implements hook_features_api().
ocupload_form_alter Implements hook_form_alter().
ocupload_init Implements hook_init().
ocupload_library Implements hook_library().
ocupload_menu Implements hook_menu().
ocupload_permission Implements hook_permission().
ocupload_preprocess_html Implements hook_preprocess_html().
ocupload_templates Return all templates.
ocupload_template_load Return template object by template id (tid).
ocupload_wysiwyg_plugin Implements hook_wysiwyg_plugin().
_ocupload_get_allowed_ext Return current user allowed file ext.
_ocupload_get_path Return module path.
_ocupload_load_flowphp Load Flow-php library.