-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfield.class.php
218 lines (199 loc) · 10.2 KB
/
field.class.php
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
<?php
class data_field_multimedia extends data_field_base {
var $type = 'multimedia';
var $imageprocessed = false;
function data_field_multimedia($field=0, $data=0) {
parent::data_field_base($field, $data);
}
function display_add_field($recordid=0) {
global $CFG;
require_once($CFG->libdir.'/filelib.php');
if ($recordid){
if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
$contents[0] = $content->content;
$contents[1] = $content->content1;
$contents[2] = $content->content2;
$contents[3] = $content->content3;
} else {
$contents[0] = '';
$contents[1] = '';
$contents[2] = '';
$contents[3] = '';
}
$src = empty($contents[0]) ? '' : $contents[0];
$width = empty($contents[1]) ? 0 : $contents[1];
$height = empty($contents[2]) ? 0 : $contents[2];
$image = empty($contents[3]) ? '' : $contents[3];
require_once($CFG->libdir.'/filelib.php');
$source = get_file_url($this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/multimedia/'.$this->field->id.'/'.$recordid);
} else {
$src = '';
$width = 0;
$height = 0;
$source = '';
$image = '';
}
$str = '<div title="' . s($this->field->description) . '">';
$str .= '<fieldset><legend>'.s($this->field->description).'</legend>';
$str .= '<input type="hidden" name ="field_'.$this->field->id.'_file" value="fakevalue" />';
$str .= '<input type="hidden" name ="field_'.$this->field->id.'_image" value="fakevalue" />';
$str .= '<table border="0">';
$str .= '<tr><td align="right">'.get_string('file','data'). '</td>'.
'<td align="left"> <input type="file" name ="field_'.$this->field->id.'_realfile"
id="field_'.$this->field->id.'_realfile" title="'.s($this->field->description).'" /></td></tr>';
if ($recordid and isset($content) and !empty($content->content)) {
$icon = mimeinfo('icon', $src);
$str .= '<tr><td> </td><td align="left">'.
'<img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.
'<a href="'.$source.'/'.$src.'" >'.$src.'</a></td></tr>';
}
$str .= '<tr><td align="right">'.get_string('fieldwidth', 'data').'</td>'.
'<td align="left"> <input type="text" name="field_' .$this->field->id.'_width"
id="field_'.$this->field->id.'_width" value="'.s($width).'" /></td></tr>';
$str .= '<tr><td align="right">'.get_string('fieldheight', 'data').'</td>'.
'<td align="left"> <input type="text" name="field_' .$this->field->id.'_height"
id="field_'.$this->field->id.'_height" value="'.s($height).'" /></td></tr>';
$str .= '<tr><td align="right">'.get_string('picture', 'data').'</td>'.
'<td align="left"> <input type="file" name="field_' .$this->field->id.'_realimage"
id="field_'.$this->field->id.'_realimage" value="'.s($image).'" /></td></tr>';
if ($recordid and isset($content) and !empty($content->content3)) {
$icon = mimeinfo('icon', $image);
$str .= '<tr><td> </td><td align="left">'.
'<img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.
'<a href="'.$source.'/'.$image.'" >'.$image.'</a></td></tr>';
}
$str .= '</table>';
$str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field->param3).'" />';
$str .= '</fieldset>';
$str .= '</div>';
return $str;
}
function display_search_field($value = '') {
return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
}
function generate_sql($tablealias, $value) {
return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') ";
}
function parse_search_field() {
return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
}
function display_browse_field($recordid, $template) {
global $CFG;
if (!$content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
return false;
}
if (empty($content->content)) {
return '';
}
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->dirroot.'/filter/mediaplugin/filter.php');
$src = $content->content;
$parts = explode('.', $src);
$ext = $parts[count($parts)-1];
$width = empty($content->content1) ? 0 : $content->content1;
$height = empty($content->content2) ? 0 : $content->content2;
$source = get_file_url($this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/multimedia/'.$this->field->id.'/'.$recordid);
$result = '';
if ($ext === 'flv') {
if ($CFG->filter_mediaplugin_enable_flv) {
$picurl = empty($content->content3) ? '' : '&image='.$source.'/'.$content->content3;
$src .= $picurl; //hacked
$link = '<a href="'. $source. '/'. $src. '?d='. $width. 'x'. $height. '"></a>';
$search = '/<a.*?href="([^<]+\.flv[^"?]*)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is'; //hacked
$result = preg_replace_callback($search, 'mediaplugin_filter_flv_callback', $link);
if ($result == $link) {
return '';
}
}
} else {
$dim = in_array(strtolower($ext), array('mp3', 'ram', 'rpm', 'rm')) ? '' : ('?d='. $width. 'x'. $height);
$link = '<a href="'. $source. '/'. $src. $dim. '"></a>';
$result = mediaplugin_filter($this->data->course, $link);
if ($result == $link) {
// link was not processed (not a media file etc.)
return '';
}
}
return $result;
}
function update_content($recordid, $value, $name) {
global $CFG;
if (!($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid))) {
// Quickly make one now!
$oldcontent = new object;
$oldcontent->fieldid = $this->field->id;
$oldcontent->recordid = $recordid;
if (!($oldcontent->id = insert_record('data_content', $oldcontent))) {
error('Could not make an empty record!');
}
}
$content = new object;
$content->id = $oldcontent->id;
$names = explode('_',$name);
switch ($names[2]) {
case 'image':
if (!$this->imageprocessed) {
$filename = $_FILES[$names[0].'_'.$names[1].'_realimage']['name'];
$dir = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/multimedia/'.$this->field->id.'/'.$recordid;
require_once($CFG->libdir.'/uploadlib.php');
$course = get_record("course", "id", "{$this->data->course}");
//Upload image but don't delete existing files
$um = new upload_manager($names[0].'_'.$names[1].'_realimage',false,false,$course,false,$this->field->param3);
if ($um->process_file_uploads($dir)) {
$content->content3 = $um->get_new_filename();
update_record('data_content',$content);
}
}
break;
case 'file':
$filename = $_FILES[$names[0].'_'.$names[1].'_realfile']['name'];
$dir = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/multimedia/'.$this->field->id.'/'.$recordid;
// only use the manager if file is present, to avoid "are you sure you selected a file to upload" msg
if ($filename) {
require_once($CFG->libdir.'/uploadlib.php');
$course = get_record("course", "id", "{$this->data->course}");
$noimage = empty($_FILES[$names[0].'_'.$names[1].'_realimage']['name']);
if ($noimage) {
//Aviod php notification. It seems like a bug for multiply uploads (/lib/uploadlib.php at 225)
unset($_FILES[$names[0].'_'.$names[1].'_realimage']);
}
//Upload all choosed files with deletion all outdated
$um = new upload_manager('',true,false,$course,false,$this->field->param3);
if ($um->process_file_uploads($dir)) {
$content->content = $um->files[$names[0].'_'.$names[1].'_realfile']['name'];
$content->content3 = $noimage ? '' : $um->files[$names[0].'_'.$names[1].'_realimage']['name'];
update_record('data_content',$content);
//We should not process image separately if it has already processed here
$this->imageprocessed = true;
}
}
break;
case 'width':
$content->content1 = clean_param($value, PARAM_NOTAGS);
update_record('data_content', $content);
break;
case 'height':
$content->content2 = clean_param($value, PARAM_NOTAGS);
update_record('data_content', $content);
break;
default:
break;
}
}
function notemptyfield($value, $name) {
$names = explode('_',$name);
if ($names[2] == 'file') {
$filename = $_FILES[$names[0].'_'.$names[1].'_real'.$names[2]];
return !empty($filename['name']);
// if there's a file in $_FILES, not empty
}
return false;
}
function text_export_supported() {
return false;
}
}
function multimedia_field_moddata_trusted() {
return true;
}
?>