forked from akempler/simpleuploads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathckeditor_simpleuploads.module
More file actions
240 lines (212 loc) · 7.65 KB
/
ckeditor_simpleuploads.module
File metadata and controls
240 lines (212 loc) · 7.65 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* @file
* Provides integration with the simpleuploads/imagepaste ckeditor plugins.
*
* NOTE: This module does not include the plugins themselves. It just supports the use
* of the plugin using either ckeditor module or wysiwyg (+ckeditor) module in Drupal 7.
*
* @link http://ckeditor.com/addon/simpleuploads
* @link http://ckeditor.com/addon/imagepaste
*
* @author Adam Kempler <akempler@gmail.com>
* @author Jānis Bebrītis <janis.bebritis@wunderkraut.com>
*
*/
/**
* Implements hook_menu().
*/
function ckeditor_simpleuploads_menu() {
$items = array();
$items['admin/config/content/ckeditor-simpleuploads'] = array(
'title' => 'CKEditor SimpleUploads settings',
'description' => 'Configure the CKEditor SimpleUploads module',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ckeditor_simpleuploads_settings_form',
),
'access arguments' => array('administer ckeditor_simpleuploads'),
'file' => 'ckeditor_simpleuploads.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['simpleupload'] = array(
'title' => 'Simpleuploads',
'page callback' => '_ckeditor_simpleupload',
'access arguments' => array('use ckeditor_simpleuploads'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function ckeditor_simpleuploads_permission() {
return array(
'administer ckeditor_simpleuploads' => array(
'title' => t('Administer the CKEditor SimpleUploads module'),
),
'use ckeditor_simpleuploads' => array(
'title' => t('Use CKEditor SimpleUploads for file uploads'),
),
);
}
/**
* Provide integration with the ckeditor simpleuploads ckeditor plugin.
* Menu callback.
*/
function _ckeditor_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('ckeditor_simpleuploads_uploaddir', '');
file_prepare_directory($uploaddir, FILE_CREATE_DIRECTORY);
if (!empty($uploaddir)) $uploaddir .= DIRECTORY_SEPARATOR;
$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('ckeditor_simpleuploads_imagestyle', 'none');
$errors = file_validate_is_image($file);
if(count($errors) < 1 && $imagestyle != 'none') {
$styledef = image_style_load($imagestyle);
if (count($styledef)) {
$success = image_style_create_derivative(
$styledef,
$upload_uri . $file_name,
image_style_path($imagestyle, $upload_uri . DIRECTORY_SEPARATOR . $file_name)
);
}
// Used by the ckeditor_simpleuploads plugin.
$url = image_style_url($imagestyle, $upload_uri . DIRECTORY_SEPARATOR . $file_name);
}
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 . $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>";
$GLOBALS['devel_shutdown'] = FALSE;
}
/**
* Implements hook_page_build().
*/
function ckeditor_simpleuploads_page_build(&$page) {
if (arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit') && variable_get('ckeditor_simpleuploads_responsive', 0)) {
drupal_add_js(drupal_get_path('module', 'ckeditor_simpleuploads') . '/js/simpleuploads.js');
}
}
function ckeditor_simpleuploads_wysiwyg_plugin($editor, $version) {
switch ($editor) {
case 'ckeditor':
return array(
'simpleuploads' => array(
'url' => 'http://ckeditor.com/addon/simpleuploads',
// 'path' => drupal_get_path('module', 'simpleuploads').'/plugins/simpleuploads',
'path' => libraries_get_path('simpleuploads'),
'filename' => 'plugin.js',
'buttons' => array(
'imagepaste' => t('simpleuploads'),
),
'load' => TRUE,
),
'imagepaste' => array(
'url' => 'http://ckeditor.com/addon/imagepaste',
// 'path' => drupal_get_path('module', 'simpleuploads').'/plugins/imagepaste',
'path' => libraries_get_path('imagepaste'),
'filename' => 'plugin.js',
'buttons' => array(
'imagepaste' => t('imagepaste'),
),
'load' => TRUE,
),
);
break;
}
}
/*
* Implements hook_wysiwyg_editor_settings_alter
* Add uploads URL to ckeditor config
*/
function ckeditor_simpleuploads_wysiwyg_editor_settings_alter(&$settings, $context) {
if ($context['profile']->editor == 'ckeditor') {
global $base_url;
$settings['filebrowserImageUploadUrl'] = $base_url.'/simpleupload';
}
}
/**
* Implementing the Wysiwyg API
* Register a directory containing Wysiwyg plugins.
*
* @param $type
* The type of objects being collected: either 'plugins' or 'editors'.
* @return
* A sub-directory of the implementing module that contains the corresponding
* plugin files. This directory must only contain integration files for
* Wysiwyg module.
*/
function ckeditor_simpleuploads_wysiwyg_include_directory($type) {
switch ($type) {
case 'plugins':
// You can just return $type, if you place your Wysiwyg plugins into a
// sub-directory named 'plugins'.
return $type;
}
}
/**
* Implements hook_libraries_info().
*/
function ckeditor_simpleuploads_libraries_info() {
$libraries = array(
'simpleuploads' => array(
'name' => 'simpleuploads',
'vendor url' => 'http://ckeditor.com/addon/simpleuploads',
'download url' => 'http://alfonsoml.blogspot.com/p/simpleuploads-plugin-for-ckeditor.html',
'version callback' => 'simpleuploads_library_version',
'files' => array(
'js' => array(
'plugin.js',
),
),
),
'imagepaste' => array(
'name' => 'imagepaste',
'vendor url' => 'http://ckeditor.com/addon/imagepaste',
'download url' => 'http://ckeditor.com/addon/imagepaste',
'version callback' => 'simpleuploads_library_version',
'files' => array(
'js' => array(
'plugin.js',
),
),
),
);
return $libraries;
}
/**
* Short-circuit the version argument.
*/
function ckeditor_simpleuploads_library_version() {
return TRUE;
}