Révision ca0757b9
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/media/modules/media_internet/media_internet.module | ||
---|---|---|
19 | 19 |
'title' => 'Web', |
20 | 20 |
'description' => 'Add internet files to your media library.', |
21 | 21 |
'page callback' => 'drupal_get_form', |
22 |
'page arguments' => array('media_internet_add'), |
|
22 |
'page arguments' => array('media_internet_add_upload'),
|
|
23 | 23 |
'access callback' => 'media_internet_access', |
24 | 24 |
'type' => MENU_LOCAL_TASK, |
25 |
'file' => 'media_internet.pages.inc', |
|
25 |
'file' => 'file_entity.pages.inc', |
|
26 |
'file path' => drupal_get_path('module', 'file_entity'), |
|
26 | 27 |
); |
27 | 28 |
|
28 | 29 |
return $items; |
... | ... | |
47 | 48 |
); |
48 | 49 |
} |
49 | 50 |
|
51 |
/** |
|
52 |
* Implements hook_theme(). |
|
53 |
*/ |
|
54 |
function media_internet_theme() { |
|
55 |
return array( |
|
56 |
// media_internet.pages.inc. |
|
57 |
'media_internet_embed_help' => array( |
|
58 |
'variables' => array('description' => NULL, 'supported_providers' => NULL), |
|
59 |
), |
|
60 |
); |
|
61 |
} |
|
62 |
|
|
50 | 63 |
/** |
51 | 64 |
* Gets the list of providers. |
52 | 65 |
* |
... | ... | |
102 | 115 |
throw new MediaInternetNoHandlerException(t('Unable to handle the provided embed string or URL.')); |
103 | 116 |
} |
104 | 117 |
|
105 |
class MediaInternetFileHandler extends MediaInternetBaseHandler { |
|
118 |
/** |
|
119 |
* Returns HTML for help text based on supported internet media providers. |
|
120 |
* |
|
121 |
* @param $variables |
|
122 |
* An associative array containing: |
|
123 |
* - description: The normal description for this field, specified by the |
|
124 |
* user. |
|
125 |
* - supported_providers: A string of supported providers. |
|
126 |
* |
|
127 |
* @ingroup themeable |
|
128 |
*/ |
|
129 |
function theme_media_internet_embed_help($variables) { |
|
130 |
$description = $variables['description']; |
|
131 |
$supported_providers = $variables['supported_providers']; |
|
106 | 132 |
|
107 |
public $fileObject;
|
|
133 |
$descriptions = array();
|
|
108 | 134 |
|
109 |
public function preSave(&$file_obj) { |
|
110 |
// Coppies the remote file locally. |
|
111 |
$remote_uri = $file_obj->uri; |
|
112 |
//@TODO: we should follow redirection here an save the final filename, not just the basename. |
|
113 |
$local_filename = basename($remote_uri); |
|
114 |
$local_filename = file_munge_filename($local_filename, variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'), FALSE); |
|
115 |
$local_uri = file_stream_wrapper_uri_normalize('temporary://' . $local_filename); |
|
116 |
if (!@copy($remote_uri, $local_uri)) { |
|
117 |
throw new Exception('Unable to add file ' . $remote_uri); |
|
118 |
return; |
|
119 |
} |
|
120 |
// Make the current fileObject point to the local_uri, not the remote one. |
|
121 |
$file_obj = file_uri_to_object($local_uri); |
|
135 |
if (strlen($description)) { |
|
136 |
$descriptions[] = $description; |
|
122 | 137 |
} |
123 |
|
|
124 |
public function postSave(&$file_obj) { |
|
125 |
$scheme = variable_get('file_default_scheme', 'public') . '://'; |
|
126 |
module_load_include('inc', 'file_entity', 'file_entity.pages'); |
|
127 |
$destination_uri = file_entity_upload_destination_uri(array()); |
|
128 |
$uri = file_stream_wrapper_uri_normalize($destination_uri . '/' . $file_obj->filename); |
|
129 |
// Now to its new home. |
|
130 |
$file_obj = file_move($file_obj, $uri, FILE_EXISTS_RENAME); |
|
138 |
if (!empty($supported_providers)) { |
|
139 |
$descriptions[] = t('Supported internet media providers: !providers.', array('!providers' => '<strong>' . $supported_providers . '</strong>')); |
|
131 | 140 |
} |
132 | 141 |
|
133 |
public function getFileObject() { |
|
134 |
if (!$this->fileObject) { |
|
135 |
$this->fileObject = file_uri_to_object($this->embedCode); |
|
136 |
} |
|
137 |
return $this->fileObject; |
|
142 |
return implode('<br />', $descriptions); |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* Implements hook_forms(). |
|
147 |
*/ |
|
148 |
function media_internet_forms($form_id, $args) { |
|
149 |
$forms = array(); |
|
150 |
|
|
151 |
// Create a copy of the upload wizard form for internet media. |
|
152 |
if ($form_id == 'media_internet_add_upload') { |
|
153 |
$forms[$form_id] = array( |
|
154 |
'callback' => 'file_entity_add_upload', |
|
155 |
); |
|
138 | 156 |
} |
139 | 157 |
|
140 |
public function claim($embedCode) { |
|
141 |
// Claim only valid URLs using a supported scheme. |
|
142 |
if (!valid_url($embedCode, TRUE) || !in_array(file_uri_scheme($embedCode), variable_get('media__fromurl_supported_schemes', array('http', 'https', 'ftp', 'smb', 'ftps')))) { |
|
143 |
return FALSE; |
|
158 |
return $forms; |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* Implements hook_form_FORM_ID_alter(). |
|
163 |
*/ |
|
164 |
function media_internet_form_file_entity_add_upload_alter(&$form, &$form_state, $form_id) { |
|
165 |
$step = $form['#step']; |
|
166 |
$options = $form['#options']; |
|
167 |
|
|
168 |
// Swap the upload field for an embed field when on the first step of the web |
|
169 |
// tab. |
|
170 |
if ($form_id == 'media_internet_add_upload' && $step == 1) { |
|
171 |
unset($form['upload']); |
|
172 |
|
|
173 |
$form['embed_code'] = array( |
|
174 |
'#type' => 'textfield', |
|
175 |
'#title' => t('File URL'), |
|
176 |
'#description' => t('Enter a URL to a file.'), |
|
177 |
'#attributes' => array('class' => array('media-add-from-url')), |
|
178 |
// There is no standard specifying a maximum length for a URL. Internet |
|
179 |
// Explorer supports up to 2083 (http://support.microsoft.com/kb/208427) |
|
180 |
// so we assume publicly available media URLs are within this limit. |
|
181 |
'#maxlength' => 2083, |
|
182 |
'#required' => TRUE, |
|
183 |
'#default_value' => isset($form_state['storage']['embed_code']) ? $form_state['storage']['embed_code'] : NULL, |
|
184 |
); |
|
185 |
|
|
186 |
// Create an array to hold potential Internet media providers. |
|
187 |
$providers = array(); |
|
188 |
|
|
189 |
// Determine if there are any visible providers. |
|
190 |
foreach (media_internet_get_providers() as $key => $provider) { |
|
191 |
if (empty($provider['hidden']) || $provider['hidden'] != TRUE) { |
|
192 |
$providers[] = check_plain($provider['title']); |
|
193 |
} |
|
144 | 194 |
} |
145 | 195 |
|
146 |
// This handler is intended for regular files, so don't claim URLs |
|
147 |
// containing query strings or fragments. |
|
148 |
if (preg_match('/[\?\#]/', $embedCode)) { |
|
149 |
return FALSE; |
|
196 |
$form['#providers'] = $providers; |
|
197 |
|
|
198 |
// Notify the user of any available providers. |
|
199 |
if ($providers) { |
|
200 |
// If any providers are enabled it is assumed that some kind of embed is supported. |
|
201 |
$form['embed_code']['#title'] = t('File URL or media resource'); |
|
202 |
$form['embed_code']['#description'] = t('Enter a URL to a file or media resource. Many media providers also support identifying media via the embed code used to embed the media into external websites.'); |
|
203 |
|
|
204 |
$form['embed_code']['#description'] = theme('media_internet_embed_help', array('description' => $form['embed_code']['#description'], 'supported_providers' => implode(' ', $providers))); |
|
150 | 205 |
} |
151 | 206 |
|
152 |
// Since this handler copies the remote file to the local web server, do not |
|
153 |
// claim a URL with an extension disallowed for media uploads. |
|
154 |
$regex = '/\.(' . preg_replace('/ +/', '|', preg_quote(variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'))) . ')$/i'; |
|
155 |
if (!preg_match($regex, basename($embedCode))) { |
|
156 |
return FALSE; |
|
207 |
$form['#validators'] = array(); |
|
208 |
|
|
209 |
if (!empty($options['types'])) { |
|
210 |
$form['#validators']['media_file_validate_types'] = array($options['types']); |
|
157 | 211 |
} |
158 | 212 |
|
159 |
return TRUE; |
|
213 |
// Add validation and submission handlers to the form and ensure that they |
|
214 |
// run first. |
|
215 |
array_unshift($form['#validate'], 'media_internet_add_validate'); |
|
216 |
array_unshift($form['#submit'], 'media_internet_add_submit'); |
|
160 | 217 |
} |
161 | 218 |
} |
162 | 219 |
|
163 |
/* |
|
164 |
* Base MediaInternetBaseHandler class. |
|
220 |
/** |
|
221 |
* Allow stream wrappers to have their chance at validation. |
|
222 |
* |
|
223 |
* Any module that implements hook_media_parse will have an |
|
224 |
* opportunity to validate this. |
|
165 | 225 |
* |
166 |
* Classes extending this class manage the addition of Internet media. To |
|
167 |
* achieve this, the class should parse user-submitted embed code, claim it |
|
168 |
* when appropriate and save it as a managed file. |
|
226 |
* @see media_parse_to_uri() |
|
169 | 227 |
*/ |
170 |
abstract class MediaInternetBaseHandler { |
|
171 |
|
|
172 |
/** |
|
173 |
* The constructor for the MediaInternetBaseHandler class. This method is also called |
|
174 |
* from the classes that extend this class and override this method. |
|
175 |
*/ |
|
176 |
public function __construct($embedCode) { |
|
177 |
$this->embedCode = $embedCode; |
|
178 |
} |
|
228 |
function media_internet_add_validate($form, &$form_state) { |
|
229 |
// Supporting providers can now claim this input. It might be a URL, but it |
|
230 |
// might be an embed code as well. |
|
231 |
$embed_code = $form_state['values']['embed_code']; |
|
179 | 232 |
|
180 |
/** |
|
181 |
* Determines if this handler should claim the item. |
|
182 |
* |
|
183 |
* @param string $embed_code |
|
184 |
* A string of user-submitted embed code. |
|
185 |
* |
|
186 |
* @return boolean |
|
187 |
* Pass TRUE to claim the item. |
|
188 |
*/ |
|
189 |
abstract public function claim($embed_code); |
|
190 |
|
|
191 |
/** |
|
192 |
* Returns a file object which can be used for validation. |
|
193 |
* |
|
194 |
* @return StdClass |
|
195 |
*/ |
|
196 |
abstract public function getFileObject(); |
|
197 |
|
|
198 |
/** |
|
199 |
* If required, implementors can validate the embedCode. |
|
200 |
*/ |
|
201 |
public function validate() { |
|
233 |
try { |
|
234 |
$provider = media_internet_get_provider($embed_code); |
|
235 |
$provider->validate(); |
|
202 | 236 |
} |
203 |
|
|
204 |
/** |
|
205 |
* Before the file has been saved, implementors may do additional operations. |
|
206 |
* |
|
207 |
* @param object $file_obj |
|
208 |
*/ |
|
209 |
public function preSave(&$file_obj) { |
|
237 |
catch (MediaInternetNoHandlerException $e) { |
|
238 |
form_set_error('embed_code', $e->getMessage()); |
|
239 |
return; |
|
210 | 240 |
} |
211 |
|
|
212 |
/** |
|
213 |
* Saves a file to the file_managed table (with file_save). |
|
214 |
* |
|
215 |
* @return StdClass |
|
216 |
*/ |
|
217 |
public function save() { |
|
218 |
$file_obj = $this->getFileObject(); |
|
219 |
$this->preSave($file_obj); |
|
220 |
file_save($file_obj); |
|
221 |
$this->postSave($file_obj); |
|
222 |
return $file_obj; |
|
241 |
catch (MediaInternetValidationException $e) { |
|
242 |
form_set_error('embed_code', $e->getMessage()); |
|
243 |
return; |
|
223 | 244 |
} |
224 | 245 |
|
225 |
/** |
|
226 |
* After the file has been saved, implementors may do additional operations. |
|
227 |
* |
|
228 |
* @param object $file_obj |
|
229 |
*/ |
|
230 |
public function postSave(&$file_obj) { |
|
246 |
$validators = $form['#validators']; |
|
247 |
$file = $provider->getFileObject(); |
|
248 |
|
|
249 |
if ($validators) { |
|
250 |
try { |
|
251 |
$file = $provider->getFileObject(); |
|
252 |
} |
|
253 |
catch (Exception $e) { |
|
254 |
form_set_error('embed_code', $e->getMessage()); |
|
255 |
return; |
|
256 |
} |
|
257 |
|
|
258 |
// Check for errors. @see media_add_upload_validate calls file_save_upload(). |
|
259 |
// this code is ripped from file_save_upload because we just want the validation part. |
|
260 |
// Call the validation functions specified by this function's caller. |
|
261 |
$errors = file_validate($file, $validators); |
|
262 |
|
|
263 |
if (!empty($errors)) { |
|
264 |
$message = t('%url could not be added.', array('%url' => $embed_code)); |
|
265 |
if (count($errors) > 1) { |
|
266 |
$message .= theme('item_list', array('items' => $errors)); |
|
267 |
} |
|
268 |
else { |
|
269 |
$message .= ' ' . array_pop($errors); |
|
270 |
} |
|
271 |
form_set_error('embed_code', $message); |
|
272 |
return FALSE; |
|
273 |
} |
|
231 | 274 |
} |
232 |
} |
|
233 | 275 |
|
234 |
class MediaInternetValidationException extends Exception { |
|
276 |
// @TODO: Validate that if we have no $uri that this is a valid file to |
|
277 |
// save. For instance, we may only be interested in images, and it would |
|
278 |
// be helpful to let the user know they passed the HTML page containing |
|
279 |
// the image accidentally. That would also save us from saving the file |
|
280 |
// in the submit step. |
|
281 |
|
|
282 |
// This is kinda a hack of the same. |
|
235 | 283 |
|
284 |
// This should use the file_validate routines that the upload form users. |
|
285 |
// We need to fix the media_parse_to_file routine to allow for a validation. |
|
236 | 286 |
} |
237 | 287 |
|
238 |
class MediaInternetNoHandlerException extends Exception { |
|
288 |
/** |
|
289 |
* Upload a file from a URL. |
|
290 |
* |
|
291 |
* This will copy a file from a remote location and store it locally. |
|
292 |
* |
|
293 |
* @see media_parse_to_uri() |
|
294 |
* @see media_parse_to_file() |
|
295 |
*/ |
|
296 |
function media_internet_add_submit($form, &$form_state) { |
|
297 |
$embed_code = $form_state['values']['embed_code']; |
|
239 | 298 |
|
299 |
try { |
|
300 |
// Save the remote file |
|
301 |
$provider = media_internet_get_provider($embed_code); |
|
302 |
// Providers decide if they need to save locally or somewhere else. |
|
303 |
// This method returns a file object |
|
304 |
$file = $provider->save(); |
|
305 |
} |
|
306 |
catch (Exception $e) { |
|
307 |
form_set_error('embed_code', $e->getMessage()); |
|
308 |
return; |
|
309 |
} |
|
310 |
|
|
311 |
if (!$file->fid) { |
|
312 |
form_set_error('embed_code', t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $embed_code))); |
|
313 |
return; |
|
314 |
} |
|
315 |
else { |
|
316 |
$form_state['storage']['upload'] = $file->fid; |
|
317 |
} |
|
240 | 318 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules