View source
<?php
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;
}
function ocupload_init() {
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();
}
}
}
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;
}
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;
}
function ocupload_preprocess_html(&$vars) {
$scripts = drupal_add_js();
$item = menu_get_item();
$module_path = _ocupload_get_path();
$add_library = FALSE;
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;
}
if (module_exists('ckeditor') && isset($scripts[ckeditor_path('url') . '/ckeditor.js'])) {
$add_library = TRUE;
}
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');
}
}
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';
}
}
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;
}
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');
}
}
}
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) {
if (strpos($field_value, $file_info['url']) !== FALSE) {
$file = file_load($fid);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
$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);
}
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();
}
}
function ocupload_template_load($tid) {
return db_select('ocupload_templates', 'ot')
->fields('ot')
->condition('ot.tid', $tid)
->execute()
->fetch();
}
function ocupload_templates() {
static $templates;
if ($templates === NULL) {
$templates = db_select('ocupload_templates', 'ot')
->fields('ot')
->execute()
->fetchAllAssoc('tid');
}
return $templates;
}
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'),
),
),
);
}
}
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',
),
),
),
);
}
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,
),
);
}
function ocupload_cron() {
$temporary_dir = 'temporary://ocupload';
if (file_exists($temporary_dir)) {
_ocupload_load_flowphp();
\Flow\Uploader::pruneChunks($temporary_dir, 3600);
}
}
function _ocupload_get_path() {
static $module_path;
if ($module_path === NULL) {
$module_path = drupal_get_path('module', 'ocupload');
}
return $module_path;
}
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;
}
function _ocupload_load_flowphp() {
require_once __DIR__ . '/flowphp/src/Flow/Autoloader.php';
\Flow\Autoloader::register();
}