| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: upload_element.pages.inc,v 1.1.2.4 2009/06/24 10:31:06 aland Exp $ 3 4 /** 5 * @file 6 * Handles image previews from both temperary and perminant file directories. 7 */ 8 9 /** 10 * Generates a temp image for image preview. 11 * 12 * This uses core Drupal image processing. 13 */ 14 function image_upload_element_thumb() { 15 $GLOBALS['devel_shutdown'] = FALSE; 16 list($form_build_id, $name, $fid) = func_get_args(); 17 // Load the element and pull out the defaults. 18 $form_state = array('submitted' => FALSE); 19 if($form = form_get_cache($form_build_id, $form_state)) { 20 $form += array('#post' => array(), '#programmed' => TRUE); 21 // calling form_builder would trigger the session to be cleared 22 $element = locate_upload_element($form, $name); 23 if ($element && $image = image_upload_element_preview_path($form_build_id, $name, $element['#image_preview_default_image'])) { 24 $info = image_get_info($image->filepath); 25 if ($info) { 26 // create temp file, minimise conflicts by instant touch 27 $tmp_file = file_destination(file_directory_temp() .'/upload_element_'. $fid .'.'. $info['extension'], FILE_EXISTS_RENAME); 28 29 $width = 100; 30 $height = 100; 31 if ($element['#image_preview_size'] && preg_match('/^\d+x\d+$/', $element['#image_preview_size'])) { 32 list($width, $height) = explode('x', $element['#image_preview_size'], 2); 33 } 34 35 if (!image_scale($image->filepath, $tmp_file, $width, $height)) { 36 $tmp_file = $image->filepath; 37 } 38 $tmp_info = image_get_info($tmp_file); 39 if ($tmp_info) { 40 $headers = array( 41 'Content-type: '. $tmp_info['mime_type'], 42 'Content-Length: '. $tmp_info['file_size'], 43 'Cache-Control: max-age=0, no-cache', 44 'Expires: '. gmdate('D, d M Y H:i:s', time()) .' GMT', 45 'Last-Modified: '. time(), 46 ); 47 ob_end_clean(); 48 foreach ($headers as $header) { 49 $header = preg_replace('/\r?\n(?!\t| )/', '', $header); 50 drupal_set_header($header); 51 } 52 53 // Transfer file in 1024 byte chunks to save memory usage. 54 if ($fd = fopen($tmp_file, 'rb')) { 55 while (!feof($fd)) { 56 print fread($fd, 1024); 57 } 58 fclose($fd); 59 } 60 else { 61 watchdog('Upload element', 'Error generating the image thumbnail.', array(), WATCHDOG_ERROR); 62 } 63 } 64 else { 65 watchdog('Upload element', 'Error generating the image thumbnail.', array(), WATCHDOG_ERROR); 66 } 67 68 // We're using the original if it is too small - do not delete! 69 if ($image->filepath != $tmp_file) { 70 @unlink($tmp_file); 71 } 72 } 73 else { 74 watchdog('Upload element', 'No image information could be parsed from the upload.', array(), WATCHDOG_ERROR); 75 } 76 } 77 else { 78 watchdog('Upload element', 'No preview path was created.', array(), WATCHDOG_ERROR); 79 } 80 } 81 else { 82 watchdog('Upload element', 'Form element was not found. This form may not be compatible with this module.', array(), WATCHDOG_ERROR); 83 } 84 if (!headers_sent()) { 85 drupal_set_header('HTTP/1.1 404 Not Found'); 86 } 87 exit; 88 } 89 90 /** 91 * This parses the session to pull out the filepath of the image. 92 * 93 * @param string $form_build_id The build id of the current form. 94 * @param string $name The key of the element. 95 * @param string $default A default image path to override the modules default image. 96 * @return object A file object, though the only property may be the filepath. 97 */ 98 function image_upload_element_preview_path($form_build_id, $name, $default = FALSE) { 99 if (is_array($_SESSION['files']['upload_element'][$form_build_id])) { 100 $files = $_SESSION['files']['upload_element'][$form_build_id]; 101 $file = (isset($files[$name]) && is_object($files[$name])) ? $files[$name] : $files[$name . '_default']; 102 if (is_object($file)) { 103 if (!(isset($file->submit_action) && $file->submit_action == UPLOAD_ELEMENT_DELETE)) { 104 return $file; 105 } 106 } 107 } 108 // This allows you to overwrite the default image 109 $image= new stdClass(); 110 if ($default && image_get_info($default)) { 111 $image->filepath = $default; 112 } 113 else { 114 $image->filepath = drupal_get_path('module', 'upload_element') .'/no_image.gif'; 115 } 116 return $image; 117 } 118 119 /** 120 * Handles the AHAH upload request. 121 * 122 * @param string $name The elements name. 123 */ 124 function upload_element_js($form_build_id, $form_id, $name) { 125 $GLOBALS['devel_shutdown'] = FALSE; 126 $form_state = array('submitted' => FALSE); 127 // The only way to detect if the max post size was exceeded is to check the $_POST 128 // The $for_build_id is potentially stale (from changing form_build_id) 129 // but is the only reference we have if the $_POST has been dropped by 130 // PHP by exceeding the MAX_FORM_SIZE limit. 131 // TODO: Find out what is happening to the AHAH during rebuild. 132 if (empty($_POST)) { 133 form_set_error($name, t("The file %file could not be saved, because it exceeds this form's size limit %size for file uploads.", array('%file' => $_FILES['files']['name'][$name], '%size' => format_size(file_upload_max_size()))), 'error'); 134 } 135 else { 136 $form_build_id = $_POST['form_build_id']; 137 } 138 if ($form_build_id && $form = form_get_cache($form_build_id, $form_state)) { 139 $form += array('#post' => $_POST); 140 if ($element = locate_upload_element($form, $name, array('#is_ahah' => TRUE))) { 141 $form = form_builder($form_id, $form, $form_state); 142 $element = locate_upload_element($form, $name); 143 $element['#messages'] = theme('status_messages'); 144 $output = drupal_render($element); 145 print drupal_to_js(array('data' => $output)); 146 exit(); 147 } 148 } 149 else { 150 form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.')); 151 } 152 $output = theme('status_messages'); 153 print drupal_to_js(array('status' => TRUE, 'data' => $output)); 154 exit(); 155 } 156 157 158 /** 159 * This does a recursive check on the cached form 160 * to locate a given element. 161 * 162 * @param array $form The form. 163 * @param string $name The element name. 164 * @return mixed Either the found element or FALSE. 165 */ 166 function locate_upload_element(&$form, $name, $extras = array()) { 167 foreach (element_children($form) as $key) { 168 // Various modules were causing false positives & errors 169 if (isset($form[$key]) && is_array($form[$key])) { 170 if ($key == $name && isset($form[$key]['#type']) 171 && ($form[$key]['#type'] == 'upload_element' || $form[$key]['#type'] == 'image_upload_element')) { 172 $form[$key] += $extras; 173 return $form[$key]; 174 } 175 else { 176 if ($element = locate_upload_element($form[$key], $name, $extras)) { 177 return $element; 178 } 179 } 180 } 181 } 182 return FALSE; 183 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |