-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwysiwyg_imageupload.file.inc
288 lines (257 loc) · 9.03 KB
/
wysiwyg_imageupload.file.inc
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
// $Id$
// Copyright (c) 2010 KontextWork
// Author: Eugen Mayer
/**
* Uploading a image and calling the file_insert hooks.
* @return: Returns the uploaded file as an object
*/
function _wysiwyg_imageupload_upload_file() {
global $user;
$dest = _wysiwyg_imageupload_get_dest_dir();
// create the path if not existent
if (!file_check_directory($dest)) {
mkdir($dest, 0777, TRUE);
}
if (!file_check_directory($dest, FILE_CREATE_DIRECTORY)) {
drupal_set_message('Destination path is not writeable, cant upload', 'error');
return NULL;
}
$validators = array(
'file_validate_is_image' => array()
);
$max_filesize = variable_get('wysiwyg_imageupload_max_filesize', 0) * 1000;
if($max_filesize > 0) {
$validators['file_validate_size'] = array( $max_filesize , 0);
}
$file = NULL;
if (user_access('use wysiwyg image upload') && $file = file_save_upload('wysiwyg_imageupload_file', $validators, $dest, FILE_EXISTS_RENAME)) {
foreach (module_implements('wysiwyg_imageupload_image_uploaded') as $module) {
$function = $module .'_wysiwyg_imageupload_image_uploaded';
$function($file);
}
$file->list = 0; // do not list the images
$file->description = $file->filename;
$file->weight = 0;
$file->new = TRUE;
if (!@chmod($file->filepath, 0664)) {
watchdog('wysiwyg_imageupload', 'Could not set permissons on destination file: %file', array('%file' => $file->filepath));
}
}
else {
// Upload failed.
return NULL;
}
drupal_alter('wysiwyg_imageupload_file_uploaded', $file);
return (object) $file;
}
/**
* Get the dest dir by parsing the settings and calling other modules
*
*/
function _wysiwyg_imageupload_get_dest_dir() {
global $user;
$dest = file_directory_path();
// Add the global relativ path.
$dest = "$dest/" . variable_get('wysiwyg_imageupload_dest_root', 'wysiwyg_imageupload');
// If its not a guest and we have set a userpath, add it relativly.
if ($user->uid > 0) {
$userpath = variable_get('wysiwyg_imageupload_relative_user', '$uid');
if ($userpath != '') {
$userpath = str_replace("\$uid", $user->uid, variable_get('wysiwyg_imageupload_relative_user', '$uid'));
$dest = "$dest/$userpath";
}
}
// Finally let other modules alter the path
drupal_alter('wysiwyg_imageupload_dest_path', $dest);
return $dest;
}
/**
* Loading all files uploaded to a node
*/
function _wysiwyg_imageupload_load($node) {
$files = array();
if ($node->vid) {
$result = db_query('SELECT * FROM {wysiwyg_imageupload_entity_revision} as r LEFT JOIN {wysiwyg_imageupload_entity} AS e ON r.iid=e.iid LEFT JOIN {files} as f ON f.fid = e.fid WHERE r.vid = %d ', $node->vid);
while ($file = db_fetch_object($result)) {
$files[$file->iid] = $file;
}
}
return $files;
}
/**
* Loading all files uploaded to a comment
*/
function _wysiwyg_imageupload_commment_load($cid) {
$files = array();
$result = db_query('SELECT * FROM {files} f INNER JOIN {wysiwyg_imageupload_entity} r ON f.fid = r.fid WHERE r.cid = %d', $cid);
while ($file = db_fetch_object($result)) {
$files[$file->iid] = $file;
}
return $files;
}
/**
* Share from the upload.module
* Adds, removes or updates files in the image property.
* This is actually the place where a file object get attached to a node, the
* relation is saved in the wysiwyg_imageupload table
*/
function _wysiwyg_imageupload_save(&$entity) {
if (!is_object($entity)) {
// comments might be submitted as array.
$entity = (object)$entity;
}
// how does that look like for a comment?
if (!empty($entity->cid) && $entity->cid > 0) {
_wysiwyg_imagegupload_save_images($entity, $entity->comment);
}
else {
_wysiwyg_imagegupload_save_images($entity, $entity->body);
$node = (array) $entity;
foreach ($node as $key => $field) {
if (strpos($key, 'field_') === 0) {
foreach ($field as $index => $field_item) {
if (!empty($field_item['value'])) {
$value = $field_item['value'];
_wysiwyg_imagegupload_save_images($entity, $value);
}
}
}
}
}
}
/**
* Auxiliary function for _wysiwyg_imageupload_save().
*/
function _wysiwyg_imagegupload_save_images(&$entity, $value) {
$images = array();
preg_match_all(_wysiwyg_imageupload_filter_pattern(), $value, $matches);
if(empty($matches)) {
return;
}
for($i=0; $i<count($matches[0]); $i++) {
$iid =$matches[1][$i];
// remove the widget from the array as all the restovers will be deleted then
$images[$iid] = _wysiwyg_imageupload_load_inline_entity($iid);
}
foreach ($images as $iid => $file) {
// Convert file to object for compatibility.
$file = (object)$file;
file_set_status($file, FILE_STATUS_PERMANENT);
$data = array(
'iid' => $file->iid,
'temporary' => 0,
);
// It's a comment.
if (!empty($entity->cid) && $entity->cid > 0) {
$data['nid'] = $entity->nid;
$data['cid'] = $entity->cid;
}
else {
// Its a node.
$data['nid'] = $entity->nid;
$data['cid'] = 0;
// Create a new revision, or associate a new file needed.
_wysiwyg_imageupload_create_revision($iid, $entity->vid);
}
drupal_write_record('wysiwyg_imageupload_entity', $data, array('iid'));
// Call the alter hooks to react on the current attached image entity.
$data = (object) $data;
drupal_alter('wysiwyg_imageupload_image_entity_attached', $data);
}
}
/**
* Cleanup all wui entries which have no corresponding file objects anymore
*/
function _wysiwyg_imageupload_cleanup_temps() {
db_query('DELETE wui,wuir FROM {wysiwyg_imageupload_entity} AS wui LEFT JOIN {wysiwyg_imageupload_entity_revision} AS wuir ON wui.iid=wuir.iid WHERE NOT EXISTS (SELECT fid from {files} as f WHERE wui.fid=f.fid)');
}
/**
* Deletes all inline entities for a specific node
* If the correspinding files are not using in any other node, they will be deleted
*/
function _wysiwyg_imageupload_handle_node_delete($nid) {
$entities = array();
// Get all current entities to later be able to look if their files are still in use.
$result = db_query('SELECT * FROM {wysiwyg_imageupload_entity} WHERE nid=%d', $nid);
while ($entity = db_fetch_object($result)) {
$entities[] = $entity;
}
// Delete all inline entities for all version.
db_query('DELETE e,r FROM {wysiwyg_imageupload_entity} AS e LEFT JOIN {wysiwyg_imageupload_entity_revision} AS r ON e.iid=r.iid WHERE nid=%d', $nid);
// Cleanup files which are no longer used.
foreach ($entities as $entity) {
// Calling other modules to veto the file deletion
$result = module_invoke_all('wysiwyg_imageupload_veto_file_deletion', $entity->fid);
$do_not_delete = FALSE;
if (is_array($result)) {
foreach ($result as $val) {
$do_not_delete |= $val;
}
}
if ($do_not_delete) {
continue;
}
$filepath = db_result(db_query('SELECT filepath FROM {files} WHERE fid = %d', $entity->fid));
if (!empty($filepath)) {
file_delete($filepath);
db_query('DELETE FROM {files} WHERE fid = %d', $entity->fid);
}
}
}
/*
* Implementation of hook_wysiwyg_imageupload_veto_file_deletion().
* Check if the image is used in a other node (inserted using the browser) and deny deletion
*/
function wysiwyg_imageupload_wysiwyg_imageupload_veto_file_deletion($fid) {
$count = db_result(db_query('SELECT COUNT(fid) FROM {wysiwyg_imageupload_entity} WHERE fid=%d', $fid));
// If this file is used somewhere else ( this image) dont delete it
if ($count > 0) {
return TRUE;
}
return FALSE;
}
/**
* Updates or revision table for inline images and adds a new revision
*/
function _wysiwyg_imageupload_create_revision($iid, $vid) {
$idd = db_result(db_query_range('SELECT iid FROM {wysiwyg_imageupload_entity_revision} WHERE iid=%d AND vid=%d', $iid, $vid, 0, 1));
if ($idd) {
// version already exists, dont create a new one
return FALSE;
}
$data = array(
'iid' => $iid,
'vid' => $vid
);
drupal_write_record('wysiwyg_imageupload_entity_revision', $data);
return TRUE;
}
/**
* Loads the inline-image object with all its details
*/
function _wysiwyg_imageupload_load_inline_entity($iid) {
$result = db_query_range('SELECT inl.*,f.*,er.vid FROM {wysiwyg_imageupload_entity} AS inl LEFT JOIN {wysiwyg_imageupload_entity_revision} as er ON inl.iid=er.iid LEFT JOIN {files} AS f ON inl.fid=f.fid WHERE inl.iid=%d', $iid, 0, 1);
if ($result !== FALSE) {
$img_obj = db_fetch_object($result);
drupal_alter('wysiwyg_imageupload_entity_load', $img_obj, $iid);
return $img_obj;
}
// else
return FALSE;
}
/**
* Create a basic / dummy inline-image entity which will be filled later
*/
function _wysiwyg_imageupload_create_inline_entity($file, $temporary = TRUE, $options = array()) {
$record = array(
'fid' => $file->fid,
'temporary' => $temporary,
);
$record = array_merge($record, $options);
drupal_write_record('wysiwyg_imageupload_entity', $record);
return $record['iid'];
}
function wysiwyg_imageupload_cron() {
_wysiwyg_imageupload_cleanup_temps();
}