-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimpleuploads.module
137 lines (117 loc) · 4.55 KB
/
simpleuploads.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @file
* Provides integration with the simpleuploads ckeditor plugin.
* http://alfonsoml.blogspot.com/p/simpleuploads-plugin-for-ckeditor.html
* NOTE: This module does not include the plugin. It just supports the use
* of the plugin using ckeditor in Drupal 7.
*
* @author: Adam Kempler <[email protected]>
*/
/**
* Implements hook_menu().
*/
function simpleuploads_menu() {
$items = array();
$items['admin/config/content/simpleuploads'] = array(
'title' => 'SimpleUploads settings',
'description' => 'Configure the SimpleUploads module',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'simpleuploads_settings_form',
),
'access arguments' => array('administer simpleuploads'),
'file' => 'simpleuploads.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['simpleupload'] = array(
'title' => 'Simpleuploads',
'page callback' => 'simpleupload',
'access arguments' => array('use simpleuploads'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function simpleuploads_permission() {
return array(
'administer simpleuploads' => array(
'title' => t('Administer the SimpleUploads module'),
),
'use simpleuploads' => array(
'title' => t('Use SimpleUploads for file uploads'),
),
);
}
/**
* Provide integration with the simpleuploads ckeditor plugin.
* Menu callback.
*/
function simpleupload() {
if (isset($_FILES['upload'])) {
// Get and clean the filename.
$file_name = isset($_FILES['upload']['name']) ? $_FILES['upload']['name'] : '';
$file_name = preg_replace('/[^\w\._]+/', '', $file_name);
// Some tweaking because
// file_save_upload() expects the $_FILES to be populated like this:
$_FILES['files']['name']['upload'] = $file_name;
$_FILES['files']['type']['upload'] = $_FILES['upload']['type'];
$_FILES['files']['tmp_name']['upload'] = $_FILES['upload']['tmp_name'];
$_FILES['files']['size']['upload'] = $_FILES['upload']['size'];
$_FILES['files']['error']['upload'] = $_FILES['upload']['error'];
$_FILES['files']['upload'] = $_FILES['upload'];
// TODO currently assumes public files.
$uploaddir = variable_get('simpleuploads_uploaddir', '') . '/';
$uploaddir = str_ireplace('//', '/', $uploaddir);
$upload_uri = file_default_scheme() . '://' . $uploaddir;
$created = file_prepare_directory($upload_uri, FILE_CREATE_DIRECTORY);
//$upload_uri = 'public://' . $uploaddir . '/';
if ($file = file_save_upload('upload', array('file_validate_is_image' => array()), $upload_uri, FILE_EXISTS_RENAME)) {
$file->status = 1;
// Update the file status into the database
// (file_save_upload is temporary by default).
$file = file_save($file);
$imagestyle = variable_get('simpleuploads_imagestyle', 'none');
$errors = file_validate_is_image($file);
if(count($errors) < 1 && $imagestyle != 'none') {
$styledef = image_style_load($imagestyle);
$src = $upload_uri . $file_name;
$dst = image_style_path($imagestyle, $src);
if (count($styledef)) {
$success = image_style_create_derivative(
$styledef,
$src,
$dst
);
}
// Used by the simpleuploads plugin.
//$url = image_style_url($imagestyle, $upload_uri . DIRECTORY_SEPARATOR . $file_name);
$url = file_create_url($dst);
}
else {
$import_path = file_stream_wrapper_get_instance_by_uri('public://')->getDirectoryPath() . DIRECTORY_SEPARATOR . $uploaddir;
$to = $import_path . $file_name;
$url = file_create_url($to);
}
$message = t('File was successfully uploaded.');
drupal_set_message('File successfully uploaded', 'status');
}
else {
$message = 'Image upload failed for: ' . check_plain(file_stream_wrapper_get_instance_by_uri('public://')->getDirectoryPath() . DIRECTORY_SEPARATOR . $uploaddir . DIRECTORY_SEPARATOR . $file_name);
drupal_set_message('File upload was unsuccessful', 'error');
}
}
// Required: Function number as indicated by CKEditor.
$funcnum = $_GET['CKEditorFuncNum'];
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcnum, '$url', '$message')</script>";
}
/**
* Implements hook_page_build().
*/
function simpleuploads_page_build(&$page) {
if (arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit') && variable_get('simpleuploads_responsive', 0)) {
drupal_add_js(drupal_get_path('module', 'simpleuploads') . '/js/simpleuploads.js');
}
}