18 require_once dirname(__FILE__).
'/../../form_question/form_question.inc';
19 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
73 $extras = $this->
attr(
'extra');
75 $name =
'q'.$this->id;
78 $extras = $extras.
' id="'.str_replace(
':',
'_', $name).
'"';
80 if ($this->
attr(
'tabindex')) {
81 $extras .=
' tabindex="'.$this->attr(
'tabindex').
'"';
85 $temp_path = array_get_index($this->extra_data,
'temp_filesystem_path',
'');
86 if (!empty($temp_path)) {
87 ?><p>Current file: <?php echo basename($temp_path); ?>
88 <?php
if (!$this->
attr(
'is_required')) {
89 echo check_box($name.
'_clear', 1, FALSE) ?> Clear?<?php
93 file_upload($name, $extras);
94 $html = ob_get_contents();
110 if (is_null($this->value)) {
142 if (isset($this->_tmp[
'file_info']) && $this->_tmp[
'file_info'] === FALSE) {
144 $this->failed_rules[] = translate(
'core_form_file_upload_error', $this->
attr(
'name'));
150 $none_uploaded = !isset($this->_tmp[
'file_info']) || (is_array($this->_tmp[
'file_info']) && empty($this->_tmp[
'file_info']));
151 $previously_uploaded = !empty($this->extra_data[
'temp_filesystem_path']);
153 if ($none_uploaded && !$previously_uploaded && $this->
attr(
'is_required')) {
155 if (strlen($this->
attr(
'cust_required_error'))) {
156 $this->failed_rules[] = $this->
attr(
'cust_required_error');
158 $this->failed_rules[] = translate(
'core_form_file_upload_none_rule', $this->
attr(
'name'));
164 return parent::hasValidValue($answer, $mute_errors);
180 $this->_tmp[
'file_info'] = get_file_upload_info(
'q'.$this->
id);
182 if (isset($this->_tmp[
'file_info'][
'name'])) {
183 $this->
setValue(array_get_index($this->_tmp[
'file_info'],
'name', translate(
'core_form_file_upload_none')));
189 $existing_file = FALSE;
191 require_once SQ_FUDGE_PATH.
'/standards_lists/mime_types.inc';
193 if (is_dir($submission_path)) {
194 $dir =
new DirectoryIterator($submission_path);
195 foreach ($dir as $dir_entry) {
196 if ($dir_entry->isFile()) {
197 $this->
setValue($dir_entry->getFilename());
200 $existing_file = TRUE;
206 if (!$existing_file) {
207 $this->
setValue(translate(
'core_form_file_upload_none'));
224 return Array(
'file_size',
'file_type',
'file_virus_check');
238 if (!empty($this->extra_data)) {
239 $value = reset($this->extra_data);
240 $key = key($this->extra_data);
242 require SQ_FUDGE_PATH.
'/standards_lists/mime_types.inc';
245 case 'temp_filesystem_path' :
247 if (is_file($filepath)) {
248 $type = array_get_index($standards_lists_mime_types, get_file_type($filepath),
'');
249 if (!empty($type)) $type =
', type '.$type;
250 $new_summary = basename($value).$type.
', '.easy_filesize(filesize($filepath));
252 $new_summary = translate(
'core_form_file_upload_none');
256 case 'filesystem_path' :
258 if (is_file($filepath)) {
259 $type = array_get_index($standards_lists_mime_types, get_file_type($filepath),
'');
260 if (!empty($type)) $type =
', type '.$type;
261 $new_summary = basename($value).$type.
', '.easy_filesize(filesize($filepath));
263 $new_summary = translate(
'core_form_file_upload_none');
267 case 'new_file_assetid' :
268 case 'existing_file_assetid' :
270 if (SQ_IN_BACKEND || SQ_IN_LIMBO) {
271 $new_summary = get_asset_tag_line($value);
273 $file_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($value);
274 $file_info = $file_asset->getExistingFile();
275 $type = array_get_index($standards_lists_mime_types, get_file_type($file_info[
'filename']),
'');
276 if (!empty($type)) $type =
', type '.$type;
277 $new_summary = basename($file_info[
'filename']).$type.
', '.easy_filesize($file_info[
'size']);
280 $new_summary = translate(
'core_form_file_upload_none');
286 $this->_tmp[
'summary'] = $new_summary;
289 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
290 require_once SQ_LIB_PATH.
'/html_form/html_form.inc';
291 $valid_names = make_valid_web_paths(Array($this->
getValue()));
292 $name = array_shift($valid_names);
297 '.SQ_TABLE_RUNNING_PREFIX.
'ast a
298 JOIN '.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk l
299 ON a.assetid = l.minorid
309 }
catch (Exception $e) {
310 throw new Exception(
'Unable to get summary of answers for: '.$name.
' due to database error: '.$e->getMessage());
313 if (!empty($existing_file)) {
314 $existing_file_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($existing_file);
315 $info = $existing_file_asset->getExistingFile();
317 if (trim($assetid) !=
'') {
318 $this->_tmp[
'summary'] = get_asset_tag_line($assetid);
319 return $this->_tmp[
'summary'];
321 $this->_tmp[
'summary'] = $this->
getValue().
', '.easy_filesize($info[
'size']);
323 $this->_tmp[
'summary'] = translate(
'core_form_file_upload_none');
329 return $this->_tmp[
'summary'];
357 $prefix =
'q'.$this->id;
358 if (isset($_POST[$prefix.
'_clear'])) {
359 if (isset($this->extra_data[
'temp_filesystem_path'])) {
363 $temp_path = $this->extra_data[
'temp_filesystem_path'];
364 delete_directory(dirname($temp_path));
367 unset($this->_tmp[
'file_info']);
368 unset($this->extra_data[
'mime_type']);
369 unset($this->extra_data[
'temp_filesystem_path']);
373 if (empty($this->_tmp[
'file_info'])) {
377 $file_info = $this->_tmp[
'file_info'];
380 if ($file_info[
'error'] === UPLOAD_ERR_OK) {
383 $this->extra_data = Array(
384 'temp_filesystem_path' => $temp_path,
385 'mime_type' => $file_info[
'type'],
389 unset($this->_tmp[
'file_info']);
390 }
else if ($file_info[
'error'] !== UPLOAD_ERR_NO_FILE) {
395 if (!isset($this->extra_data[
'temp_filesystem_path'])) {
412 $question_create_loc = $this->
attr(
'create_location');
414 $mime_type = array_get_index($this->extra_data,
'mime_type',
'');
415 $temp_path = array_get_index($this->extra_data,
'temp_filesystem_path',
'');
417 if (!empty($question_create_loc) && !empty($temp_path)) {
420 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
423 $valid_names = make_valid_web_paths(Array($this->
getValue()));
424 $name = array_shift($valid_names);
426 $create_location = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($question_create_loc);
431 $sql =
'SELECT assetid
432 FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast_path
435 FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast a
436 JOIN '.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk l
437 ON a.assetid = l.minorid
450 }
catch (Exception $e) {
451 throw new Exception(
'Unable to check for web path conflicts for filename: '.$name.
' due to database error: '.$e->getMessage());
455 if(empty($existing_assetid) && ($this->
attr(
'overwrite_rule') ==
'O' || $this->
attr(
'overwrite_rule') ==
'R')) {
457 $parent_urls = $create_location->getLookups();
458 for ($j = 0, $num_parent_urls = count($parent_urls); $j < $num_parent_urls; $j++) {
459 $new_url = $parent_urls[$j][
'url'].
'/'.$name;
461 $orphaned = $GLOBALS[
'SQ_SYSTEM']->am->getAssetFromURL(NULL, $new_url, TRUE, TRUE);
465 if(!empty($orphaned)) {
467 $GLOBALS[
'SQ_SYSTEM']->am->createAssetLink($create_location, $orphaned, SQ_LINK_TYPE_2,
'');
468 $existing_assetid = $orphaned->id;
472 $existing_asset = NULL;
473 if (!empty($existing_assetid)) {
474 switch ($this->
attr(
'overwrite_rule')) {
477 $this->failed_rules[] = translate(
'core_form_file_upload_already_exists', $this->
attr(
'name'));
483 $existing_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($existing_assetid);
485 if (!($existing_asset instanceof
File)) {
487 $this->failed_rules[] =
'Refusing to overwrite a non-File asset at web path '.$name.
' - an uploaded file can only overwrite a File asset';
495 $suggested_paths = $GLOBALS[
'SQ_SYSTEM']->am->webPathsInUse($create_location, Array($name), 0, TRUE);
496 $name = array_shift($suggested_paths);
504 if ($existing_asset) {
506 $edit_fns = $existing_asset->getEditFns();
509 'tmp_name' => $temp_path,
511 'non_uploaded_file' => TRUE,
516 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
517 if (!$edit_fns->processFileUpload($existing_asset, $o, $existing_asset->getPrefix(), $file_info)) {
520 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
522 $this->extra_data = Array(
523 'existing_file_assetid' => $existing_asset->id,
524 'mime_type' => $mime_type,
525 'temp_filesystem_path' => $temp_path,
529 $file_ext = get_file_type($this->
getValue());
532 if (in_array($file_ext, Array(
'gif',
'jpg',
'jpeg',
'png'))) {
533 $file_type =
'Image';
535 }
else if (preg_match(
'/^do[ct][xm]?$/', $file_ext)) {
538 $file_type =
'Word_Doc';
540 }
else if (preg_match(
'/^xl[st][xm]?$/', $file_ext)) {
543 $file_type =
'Excel_Doc';
545 }
else if (preg_match(
'/^p(pt|ps|ot)[xm]?$/', $file_ext)) {
549 $file_type =
'PowerPoint_Doc';
551 }
else if ($file_ext ===
'pdf') {
552 $file_type =
'PDF_File';
554 }
else if ($file_ext ===
'rtf') {
555 $file_type =
'RTF_File';
557 }
else if ($file_ext ===
'txt') {
558 $file_type =
'Text_File';
560 }
else if (substr($mime_type, 0, 5) ===
'text/') {
561 $file_type =
'Text_File';
568 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset($file_type);
570 $file_asset =
new $file_type();
571 $file_asset->_tmp[
'uploading_file'] = TRUE;
574 'asset' => $GLOBALS[
'SQ_SYSTEM']->am->getAsset($question_create_loc),
575 'link_type' => SQ_LINK_TYPE_2,
579 'tmp_name' => $temp_path,
581 'non_uploaded_file' => TRUE,
587 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
588 if (!$file_asset->create($link, $file_info)) {
589 $this->failed_rules[] = translate(
'core_form_file_upload_error', $this->
attr(
'name'));
592 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
594 unset($file_asset->_tmp[
'uploading_file']);
596 $this->extra_data = Array(
597 'new_file_assetid' => $file_asset->id,
598 'mime_type' => $mime_type,
599 'temp_filesystem_path' => $temp_path,
625 $filepath = array_get_index($this->extra_data,
'temp_filesystem_path', NULL);
626 if (!empty($filepath)) {
627 delete_directory(dirname($filepath));
644 $file_info = $this->_tmp[
'file_info'];
646 if ($file_info[
'error'] === UPLOAD_ERR_OK) {
648 create_directory($submission_path);
652 $dir =
new DirectoryIterator($submission_path);
653 foreach ($dir as $dir_entry) {
654 if ($dir_entry->isFile()) {
655 unlink($dir_entry->getPathname());
659 $submission_file_name = $submission_path.
'/'.$file_info[
'name'];
660 move_uploaded_file($file_info[
'tmp_name'], $submission_file_name);
661 $this->
setValue(basename($submission_file_name));
674 if (method_exists($parent,
'getTempFileUploadPath')) {
675 $submission_path = $parent->getTempFileUploadPath();
677 $submission_path = $this->data_path.
'/attachments/'.session_id().
'_'.$parent->id;
680 $submission_path .=
'/'.str_replace(
':',
'_', $this->
id);
682 return $submission_path;
694 $existing_file = NULL;
695 if ($this->value != translate(
'core_form_file_upload_none') && !empty($this->value)){
696 if (!empty($this->extra_data)) {
698 if (isset($this->extra_data[
'filesystem_path'])) {
701 $existing_file = array_get_index($this->extra_data,
'new_file_assetid', NULL);
702 if (is_null($existing_file)) {
703 $existing_file = array_get_index($this->extra_data,
'existing_file_assetid', NULL);
708 if (is_null($existing_file)) {
709 $create_loc = $this->
attr(
'create_location');
710 if ($create_loc == 0)
return NULL;
711 $create_loc_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($create_loc);
712 if (is_null($create_loc_asset))
return NULL;
716 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
717 $valid_names = make_valid_web_paths(Array($this->
getValue()));
718 $name = array_shift($valid_names);
723 '.SQ_TABLE_RUNNING_PREFIX.
'ast a
724 JOIN '.SQ_TABLE_RUNNING_PREFIX.
'ast_lnk l
725 ON a.assetid = l.minorid
727 l.majorid = :create_loc
735 }
catch (Exception $e) {
736 throw new Exception(
'Unable to get the uploaded file assetid due to database error: '.$e->getMessage());
741 return $existing_file;