/[drupal]/contributions/modules/mediafield/multimediafile.inc
ViewVC logotype

Contents of /contributions/modules/mediafield/multimediafile.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Thu Jun 26 09:38:36 2008 UTC (17 months ago) by acm
Branch: MAIN
CVS Tags: DRUPAL-5--1-01-RC1, DRUPAL-5--1-1, HEAD
Changes since 1.7: +7 -1 lines
File MIME type: text/x-php
fixing upper case extension bug (http://drupal.org/node/234469) and improving theme_[audio/video]field_icon() functions.
1 <?php
2
3 /**
4 * Temp function to allow use of getId3 module while we wait for a patch
5 */
6 function mediafield_getid3_analyze($path){
7 getid3_load();
8 return (GETID3_VERSION != 'GETID3_VERSION'?getid3_analyze($path):array());
9 }
10
11
12 /**
13 * Wrapper function for _file_check_directory that accepts a form element
14 * to validate - if user specified one. Won't allow form submit unless the
15 * directory exists & is writable
16 *
17 * @param $form_element
18 * The form element containing the name of the directory to check.
19 */
20 function _file_form_check_directory($form_element) {
21 if(!empty($form_element['#value'])) {
22 _file_check_directory($form_element['#value'], $form_element);
23 }
24 return $form_element;
25 }
26
27 /**
28 * Create the directory relative to the 'files' dir recursively for every
29 * directory in the path.
30 *
31 * @param $directory
32 * The directory path under files to check, such as 'media/path/here'
33 * @param $form_element
34 * A form element to throw an error on if the directory is not writable
35 */
36 function _file_check_directory($directory, $form_element = array()) {
37 foreach (explode('/', $directory) as $dir) {
38 $dirs[] = $dir;
39 file_check_directory(file_create_path(implode($dirs,'/')), FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);
40 }
41 return TRUE;
42 }
43
44 /**
45 * Protects server from uploading files like file.php.mp3. Should be moved into core.
46 */
47 function _file_munge_filename($filename, $extensions, $alerts = 1) {
48 $original = $filename;
49 $whitelist = array_unique(explode(' ', trim($extensions)));
50
51 $filename_parts = explode('.', $filename);
52
53 $new_filename = array_shift($filename_parts); // Remove file basename.
54 $final_extension = array_pop($filename_parts); // Remove final extension.
55
56 foreach($filename_parts as $filename_part) {
57 $new_filename .= ".$filename_part";
58 if (!in_array($filename_part, $whitelist) && preg_match("/^[a-zA-Z]{2,5}\d?$/", $filename_part)) {
59 $new_filename .= '_';
60 }
61 }
62 $filename = "$new_filename.$final_extension";
63
64 if ($alerts && $original != $filename) {
65 $message = t('Your filename has been renamed to conform to site policy.');
66 drupal_set_message($message, 'warning');
67 }
68
69 return $filename;
70 }
71
72 /**
73 * Insert a file into the database.
74 *
75 * @param $node
76 * node object file will be associated with.
77 * @param $file
78 * file to be inserted, passed by reference since fid should be attached.
79 *
80 */
81 function _field_file_insert($node, &$file, $field) {
82 $fieldname = $field['field_name'];
83 $safe_filename = _file_munge_filename($file['filename'], trim($field['widget']['file_extensions']));
84 $filepath = file_create_path($field['widget']['upload_path']) . '/' . $safe_filename;
85
86 if (_file_check_directory($field['widget']['upload_path']) && $file = file_save_upload((object)$file, $filepath)) {
87 $file = (array)$file;
88 $file['fid'] = db_next_id('{files}_fid');
89 db_query(
90 "INSERT into {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)",
91 $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']
92 );
93 return (array)$file;
94 }
95 else {
96 form_set_error(NULL, t('File upload was unsuccessful!'));
97 return FALSE;
98 }
99 }
100
101 /**
102 * Update the video file record if necessary.
103 */
104 function _field_file_update($node, &$file, $field) {
105 $file = (array)$file;
106 if ($file['flags']['delete'] == TRUE) {
107 _field_file_delete($file, $field['field_name'], $field['type']);
108 return array();
109 }
110 if ($file['fid'] == 'upload') {
111 return _field_file_insert($node, $file, $field);
112 }
113 else {
114 }
115 return $file;
116 }
117
118 /**
119 * Delete a file from the files table and disk.
120 */
121 function _field_file_delete($file, $fieldname, $type) {
122 if (is_numeric($file['fid'])) {
123 db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']);
124 }
125 else {
126 unset($_SESSION[$type][$fieldname][$file['sessionid']]);
127 }
128 return file_delete($file['filepath']);
129 }
130
131 function _field_clear_session($type) {
132 if (is_array($_SESSION[$type]) && count($_SESSION[$type])) {
133 foreach (array_keys($_SESSION[$type]) as $fieldname) {
134 _field_clear_field_session($fieldname, $type);
135 }
136 unset($_SESSION[$type]);
137 }
138 }
139
140 function _field_clear_field_session($fieldname, $type) {
141 if (count($_SESSION[$type][$fieldname])) {
142 foreach ($_SESSION[$type][$fieldname] as $files) {
143 if (is_file($file['filepath'])) {
144 file_delete($file['filepath']);
145 }
146 }
147 unset($_SESSION[$type][$fieldname]);
148 }
149 }
150
151 /**
152 * Load file information from files table.
153 */
154 function _field_file_load($fid = NULL) {
155 if (isset($fid)) {
156 if (is_numeric($fid)) {
157 $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid);
158 $file = db_fetch_array($result);
159 return ($file) ? $file : array();
160 }
161 }
162 return array();
163 }
164
165 /**
166 * Formats audio sample rate.
167 */
168 function format_samplerate($samplerate) {
169 return sprintf("%.1f kHz", $samplerate/1000);
170 }
171
172 /**
173 * Formats audio bit rate.
174 */
175 function format_bitrate($bitrate) {
176 return sprintf("%d Kbps", $bitrate/1000);
177 }
178
179 /**
180 * Formats file size.
181 */
182 function format_filesize($size) {
183 $size = $size/1024;
184 $result = '';
185 if ($size > 0) {
186 $result = sprintf("%.02f MB", $size/1024);
187 } else {
188 $result = sprintf("%.02f Kb", $size);
189 }
190 return $result;
191 }
192
193 /**
194 * Searchs is there a specified resource in the current theme directory.
195 * If not, get the path of the parent module.
196 *
197 * @param string $filename
198 * File name to search for.
199 * @param string $module
200 * A name of the module which is a parent module of this file.
201 * @param string $ext
202 * File extension.
203 * @param string $default
204 * Default file to use if selected file is not found
205 * @return string
206 * A path of the file.
207 */
208 function _file_get_resource_path($filename, $module, $ext = 'png', $default = false) {
209 $resource = path_to_theme() .'/mediaicons/'. "$filename.$ext";
210 if (!file_exists($resource)) {
211 $resource = drupal_get_path('module', $module) .'/mediaicons/'. "$filename.$ext";
212 }
213
214 if ($default !== false AND !file_exists($resource)) {
215 $resource = _file_get_resource_path($default, $module, $ext);
216 }
217 return $resource;
218 // return check_url(base_path(). $resource);
219 }

  ViewVC Help
Powered by ViewVC 1.1.2