emthumb.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/emfield/contrib/emthumb/emthumb.module
    2. 6 contributions/emfield/contrib/emthumb/emthumb.module

Functions & methods

NameDescription
emthumb_check_directoryCreate the image directory relative to the 'files' dir recursively for every directory in the path.
emthumb_clear_field_session
emthumb_clear_session
emthumb_emfield_field_extra
emthumb_emfield_widget_extrawhen editing a node with an emfield, this will add our custom thumbnail upload form if allowed.
emthumb_emfield_widget_settings_extraThis provides extra widget settings to emfields. A checkbox to allow custom thumbnails, max resolution, image path, allow custom alt tags, allow custom title tags.
emthumb_file_insertinsert a file into the database.
emthumb_file_updateupdate the file record if necessary
emthumb_form_check_directoryWrapper function for emthumb_check_directory that accepts a form element to validate - if user specified one. Won't allow form submit unless the directory exists & is writable
emthumb_menu
emthumb_thumbnail_url
theme_emthumb_edit_image_row
theme_emthumb_image
theme_emthumb_multipleformats an array of images.
theme_emthumb_view_image
_emthumb_file_delete
_emthumb_file_load
_emthumb_previewtransfer a file that is in a 'preview' state. @todo multiple support
_emthumb_scale_image
_emthumb_widget_form

File

View source
  1. <?php
  2. function emthumb_menu($maycache) {
  3. $items = array();
  4. if (!$maycache && $_SESSION['emthumb']) {
  5. // Add handlers for previewing new uploads.
  6. foreach ($_SESSION['emthumb'] as $fieldname => $files) {
  7. if (is_array($files)) {
  8. foreach ($files as $delta => $file) {
  9. if ($file['preview']) {
  10. $items[] = array(
  11. 'path' => $file['preview'],
  12. 'callback' => '_emthumb_preview',
  13. 'access' => TRUE,
  14. 'type' => MENU_CALLBACK,
  15. );
  16. }
  17. }
  18. }
  19. }
  20. }
  21. return $items;
  22. }
  23. /**
  24. * transfer a file that is in a 'preview' state.
  25. * @todo multiple support
  26. */
  27. function _emthumb_preview() {
  28. foreach ($_SESSION['emthumb'] as $fieldname => $files) {
  29. foreach ($files as $delta => $file) {
  30. if ($file['preview'] == $_GET['q']) {
  31. file_transfer($file['filepath'], array('Content-Type: '. mime_header_encode($file['filemime']),
  32. 'Content-Length: '. $file['filesize']));
  33. exit();
  34. }
  35. }
  36. }
  37. }
  38. // function emthumb_emfield_field_columns_extra() {
  39. // $columns = array(
  40. // 'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
  41. // 'emthumb_title' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
  42. // 'emthumb_alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
  43. // );
  44. // return $columns;
  45. // }
  46. /**
  47. * insert a file into the database.
  48. * @param $node
  49. * node object file will be associated with.
  50. * @param $file
  51. * file to be inserted, passed by reference since fid should be attached.
  52. *
  53. */
  54. function emthumb_file_insert($node, &$file, $field) {
  55. $fieldname = $field['field_name'];
  56. $filepath = file_create_path($field['widget']['emimport_image_path']) .'/'. $file['filename'];
  57. if (emthumb_check_directory($field['widget']['emimport_image_path']) && $file = file_save_upload((object)$file, $filepath)) {
  58. $file = (array)$file;
  59. $file['fid'] = db_next_id('{files}_fid');
  60. db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)
  61. VALUES (%d, %d, '%s','%s','%s',%d)",
  62. $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
  63. return (array)$file;
  64. }
  65. else {
  66. // Include file name in upload error.
  67. form_set_error(NULL, t('Thumbnail upload was unsuccessful.'));
  68. return FALSE;
  69. }
  70. }
  71. /**
  72. * update the file record if necessary
  73. * @param $node
  74. * @param $file
  75. * @param $field
  76. */
  77. function emthumb_file_update($node, &$file, $field) {
  78. $file = (array)$file;
  79. if ($file['flags']['delete'] == TRUE) {
  80. if (_emthumb_file_delete($file, $field['field_name'])) {
  81. return array();
  82. }
  83. }
  84. if ($file['fid'] == 'upload') {
  85. return emthumb_file_insert($node, $file, $field);
  86. }
  87. else {
  88. // if fid is not numeric here we should complain.
  89. // else we update the file table.
  90. }
  91. return $file;
  92. }
  93. function emthumb_emfield_field_extra($op, $node, $field, &$items, $teaser, $page) {
  94. switch ($op) {
  95. case 'submit':
  96. case 'validate':
  97. if ($field['multiple']) {
  98. foreach ($items as $delta => $item) {
  99. $items[$delta]['data'] = $list['provider'];
  100. }
  101. }
  102. else {
  103. $emthumb = array_pop($items);
  104. if (empty($items)) {
  105. array_push($items, $emthumb);
  106. }
  107. else {
  108. $items[0]['data']['emthumb'] = $emthumb['emthumb']['emthumb'];
  109. }
  110. }
  111. break;
  112. // called after content.module loads default data.
  113. case 'load'://print_r($items);
  114. $output = array();
  115. if (count($items)) {
  116. $values = array();
  117. foreach ($items as $delta => $file) {
  118. $items[$delta]['data']['emthumb'] = _emthumb_file_load($file['data']['emthumb']['fid']);
  119. }
  120. return array($field['field_name'] => $items);
  121. }
  122. break;
  123. case 'rss item':
  124. // different from load (and others) as it can be, and is, called within each $field individually
  125. $output = array();
  126. if (count($items)) {
  127. $values = array();
  128. foreach ($items as $delta => $file) {
  129. $thumbnail = _emthumb_file_load($file['data']['emthumb']['fid']);
  130. if (isset($thumbnail['filepath'])) {
  131. $thumbnail['filepath'] = url($thumbnail['filepath'], NULL, NULL, TRUE);
  132. $output[$delta]['thumbnail'] = $thumbnail;
  133. }
  134. }
  135. }
  136. return $output;
  137. // called before content.module defaults.
  138. case 'insert':
  139. foreach ($items as $delta => $item) {
  140. $items[$delta]['data']['emthumb'] = emthumb_file_insert($node, $items[$delta]['data']['emthumb'], $field);
  141. }
  142. break;
  143. // called before content.module defaults.
  144. case 'update':
  145. foreach ($items as $delta => $item) {
  146. $items[$delta]['data']['emthumb'] = emthumb_file_update($node, $items[$delta]['data']['emthumb'], $field);
  147. /* // Remove non-existant files from node_field
  148. if (empty($items[$delta])) {
  149. unset($items[$delta]);
  150. }*/
  151. }
  152. // Compact deltas.
  153. $items = array_values($items);
  154. break;
  155. case 'delete':
  156. foreach ($items as $delta => $item) {
  157. _emthumb_file_delete($item['data']['emthumb'], $field['field_name']);
  158. }
  159. break;
  160. }
  161. }
  162. /**
  163. * This provides extra widget settings to emfields.
  164. * A checkbox to allow custom thumbnails, max resolution, image path, allow custom alt tags, allow custom title tags.
  165. */
  166. function emthumb_emfield_widget_settings_extra($op, $widget) {
  167. switch ($op) {
  168. case 'form':
  169. $form = array();
  170. $form['emthumb'] = array(
  171. '#type' => 'fieldset',
  172. '#title' => t('Embedded Custom Thumbnails'),
  173. '#collapsible' => TRUE,
  174. '#collapsed' => FALSE,
  175. );
  176. $form['emthumb']['emthumb'] = array(
  177. '#type' => 'checkbox',
  178. '#title' => t('Allow custom thumbnails for this field'),
  179. '#description' => t('If checked, then editors may specify a custom thumbnail to be used, overriding any automatic thumbnails otherwise created.'),
  180. '#default_value' => isset($widget['emthumb']) ? $widget['emthumb'] : 0,
  181. );
  182. $form['emthumb']['emthumb_label'] = array(
  183. '#type' => 'textfield',
  184. '#title' => t('Custom thumbnail label'),
  185. '#default_value' => $widget['emthumb_label'] ? $widget['emthumb_label'] : t('@field custom thumbnail', array('@field' => $widget['label'])),
  186. '#description' => t('This label will be displayed when uploading a custom thumbnail.'),
  187. );
  188. $form['emthumb']['emthumb_description'] = array(
  189. '#type' => 'textfield',
  190. '#title' => t('Custom thumbnail description'),
  191. '#default_value' => $widget['emthumb_description'] ? $widget['emthumb_description'] : t('If you upload a custom thumbnail, then this will be displayed when the @field thumbnail is called for, overriding any automatic thumbnails by custom providers.', array('@field' => $widget['label'])),
  192. '#description' => t('This description will be displayed when uploading a custom thumbnail.'),
  193. '#maxlength' => 512,
  194. );
  195. $form['emthumb']['emthumb_max_resolution'] = array(
  196. '#type' => 'textfield',
  197. '#title' => t('Maximum resolution for Images'),
  198. '#default_value' => $widget['emthumb_max_resolution'] ? $widget['emthumb_max_resolution'] : 0,
  199. '#size' => 15,
  200. '#maxlength' => 10,
  201. '#description' =>
  202. t('The maximum allowed custom thumbnail size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
  203. );
  204. $form['emthumb']['emimport_image_path'] = array(
  205. '#type' => 'textfield',
  206. '#title' => t('Image path'),
  207. '#default_value' => $widget['emimport_image_path'] ? $widget['emimport_image_path'] : '',
  208. '#description' => t('Optional subdirectory within the "%dir" directory where images will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))),
  209. '#after_build' => array('emthumb_form_check_directory'),
  210. );
  211. $form['emthumb']['emthumb_custom_alt'] = array(
  212. '#type' => 'checkbox',
  213. '#title' => t('Enable custom alternate text'),
  214. '#default_value' => $widget['emthumb_custom_alt'] ? $widget['emthumb_custom_alt'] : 0,
  215. '#description' => t('Enable custom alternate text for custom thumbnails. Filename will be used if not checked.'),
  216. );
  217. $form['emthumb']['emthumb_custom_title'] = array(
  218. '#type' => 'checkbox',
  219. '#title' => t('Enable custom title text'),
  220. '#default_value' => $widget['emthumb_custom_title'] ? $widget['emthumb_custom_title'] : 0,
  221. '#description' => t('Enable custom title text for custom thumbnails. Filename will be used if not checked.'),
  222. );
  223. return $form;
  224. case 'save':
  225. return array('emthumb', 'emthumb_label', 'emthumb_description', 'emthumb_max_resolution', 'emimport_image_path', 'emthumb_custom_alt', 'emthumb_custom_title');
  226. }
  227. }
  228. /**
  229. * when editing a node with an emfield, this will add our custom thumbnail upload form if allowed.
  230. */
  231. function emthumb_emfield_widget_extra($op, $node, $field, $items) {
  232. $fieldname = $field['field_name'];
  233. $content_type = $field['type_name'];
  234. switch ($op) {
  235. case 'prepare form values':
  236. // clean up the session if we weren't posted.
  237. if (!count($_POST)) {
  238. emthumb_clear_session();
  239. }
  240. // Attach new files
  241. if ($file = file_check_upload($fieldname .'_upload')) {
  242. $file = (array)$file;
  243. if (strpos($file['filemime'], 'image') !== FALSE) {
  244. $file = _emthumb_scale_image($file, $field['widget']['emthumb_max_resolution']);
  245. // Create the filepath for the image preview
  246. $filepath = file_create_filename($file['filename'], file_create_path($field['widget']['emimport_image_path']));
  247. if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
  248. if (strpos($filepath, file_directory_path()) !== FALSE) {
  249. $filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/');
  250. }
  251. $filepath = 'system/files/'. $filepath;
  252. }
  253. $file['fid'] = 'upload';
  254. $file['preview'] = $filepath;
  255. // If a single field, mark any other images for deletion and delete files in session
  256. if (!$field['multiple']) {
  257. if (is_array($items)) {
  258. foreach ($items as $delta => $session_file) {
  259. $items[$delta]['data']['emthumb']['flags']['delete'] = TRUE;
  260. }
  261. }
  262. emthumb_clear_field_session($fieldname);
  263. }
  264. // Add the file to the session
  265. $file_id = count($items) + count($_SESSION['emthumb'][$fieldname]);
  266. $_SESSION['emthumb'][$fieldname][$file_id] = $file;
  267. }
  268. }
  269. // Load files from preview state. before committing actions.
  270. if (is_array($_SESSION['emthumb'][$fieldname]) && count($_SESSION['emthumb'][$fieldname])) {
  271. foreach ($_SESSION['emthumb'][$fieldname] as $delta => $file) {
  272. $items[$delta-1]['data']['emthumb'] = $file;
  273. // $items[$delta-1] = array_merge($items[$delta-1], $file);
  274. // $items[$delta-1] = $file;
  275. }
  276. }
  277. break;
  278. case 'form':
  279. if ($field['widget']['emthumb']) {
  280. $form = _emthumb_widget_form($node, $field, $items);
  281. return $form;
  282. }
  283. break;
  284. }
  285. }
  286. function _emthumb_widget_form($node, $field, $items) {
  287. $fieldname = $field['field_name'];
  288. drupal_add_css(drupal_get_path('module', 'emthumb') .'/emthumb.css');
  289. $form = array(
  290. '#type' => 'fieldset',
  291. '#title' => t($field['widget']['emthumb_label'] ? t('@label', array('@label' => $field['widget']['emthumb_label'])) : t('@field custom thumbnail', array('@field' => $field['widget']['label']))),
  292. '#weight' => $field['widget']['emthumb_weight'],
  293. '#collapsible' => TRUE,
  294. '#collapsed' => FALSE,
  295. '#tree' => TRUE,
  296. );
  297. // Seperate from tree becase of that silly things won't be
  298. // displayed if they are a child of '#type' = form issue
  299. $form[$fieldname .'_upload'] = array(
  300. '#type' => 'file',
  301. '#description' => $field['widget']['emthumb_description'] ? t('@description', array('@description' => $field['widget']['emthumb_description'])) : t('If you upload a custom thumbnail, then this will be displayed when the @field thumbnail is called for, overriding any automatic thumbnails by custom providers.', array('@field' => $field['widget']['label'])),
  302. '#tree' => FALSE,
  303. '#weight' => 9,
  304. );
  305. $form['upload'] = array(
  306. '#type' => 'button',
  307. '#value' => t('Upload'),
  308. '#name' => 'emthumb_'. $fieldname .'_op',
  309. '#attributes' => array('id' => $fieldname .'-attach-button'),
  310. '#tree' => FALSE,
  311. '#weight' => 10,
  312. );
  313. // Store the file data object to be carried on.
  314. $file = $items['data']['emthumb'];
  315. // drupal_set_message('file:<pre>' . print_r($file) . '</pre>');
  316. // print_r($node);
  317. if ($file['filepath'] && !$file['flags']['delete']) {
  318. $form['emthumb'] = array(
  319. '#theme' => 'emthumb_edit_image_row',
  320. );
  321. $form['emthumb']['flags']['delete'] = array(
  322. '#type' => 'checkbox',
  323. '#title' => t('Delete'),
  324. '#default_value' => 0,
  325. );
  326. $filename = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path($field['widget']['emimport_image_path'])) : $file['filepath'];
  327. // drupal_set_message('filename: ' . $filename);
  328. $form['emthumb']['preview'] = array(
  329. '#type' => 'markup',
  330. '#value' => theme('emthumb_image', $file, $file['emthumb_alt'], $file['emthumb_title'], array('width' => '150'), FALSE),
  331. );
  332. //drupal_set_message('imagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($items, true) .'</pre>');
  333. $form['emthumb']['description'] = array(
  334. '#type' => 'markup',
  335. '#value' => '<strong>'. t('Filename: ') .'</strong>'. check_plain($file['filename']),
  336. );
  337. $form['emthumb']['emthumb_alt'] = array(
  338. '#type' => 'hidden',
  339. '#value' => $file['filename'],
  340. );
  341. // overwrite with an input field if custom_alt is flagged;
  342. if ($field['widget']['emthumb_custom_alt']) {
  343. $form['emthumb']['emthumb_alt'] = array(
  344. '#type' => 'textfield',
  345. '#title' => t('Alternate text'),
  346. '#default_value' => $file['emthumb_alt'],
  347. '#description' => t('Alternate text to be displayed if the image cannot be displayed.'),
  348. '#maxlength' => 255,
  349. '#size' => 10,
  350. );
  351. }
  352. $form['emthumb']['emthumb_title'] = array(
  353. '#type' => 'hidden',
  354. '#value' => $file['filename'],
  355. );
  356. // overwrite with an input field if custom_title is flagged;
  357. if ($field['widget']['emthumb_custom_title']) {
  358. $form['emthumb']['emthumb_title'] = array(
  359. '#type' => 'textfield',
  360. '#title' => t('Title'),
  361. '#default_value' => $file['emthumb_title'],
  362. '#description' => t('Text to be displayed on mouse overs.'),
  363. '#maxlength' => 255,
  364. '#size' => 10,
  365. );
  366. }
  367. $form['emthumb']['filename'] = array('#type' => 'value', '#value' => $file['filename']);
  368. $form['emthumb']['filepath'] = array('#type' => 'value', '#value' => $file['filepath']);
  369. $form['emthumb']['filemime'] = array('#type' => 'value', '#value' => $file['filemime']);
  370. $form['emthumb']['filesize'] = array('#type' => 'value', '#value' => $file['filesize']);
  371. $form['emthumb']['fid'] = array('#type' => 'value', '#value' => $file['fid']);
  372. // Special handling for single value fields
  373. if (!$field['multiple']) {
  374. $form['emthumb']['replace'] = array(
  375. '#type' => 'markup',
  376. '#value' => t('If a new custom thumbnail is chosen, the current custom thumbnail will be replaced upon submitting the form.'),
  377. );
  378. }
  379. }
  380. elseif ($file['filepath'] && $file['flags']['delete']) {
  381. $form['emthumb']['flags']['delete'] = array(
  382. '#type' => 'hidden', // A value type will not persist here, must be hidden
  383. '#value' => $file['flags']['delete'],
  384. );
  385. }
  386. return $form;
  387. }
  388. /**
  389. * Wrapper function for emthumb_check_directory that accepts a form element
  390. * to validate - if user specified one. Won't allow form submit unless the
  391. * directory exists & is writable
  392. *
  393. * @param $form_element
  394. * The form element containing the name of the directory to check.
  395. */
  396. function emthumb_form_check_directory($form_element) {
  397. if (!empty($form_element['#value'])) {
  398. emthumb_check_directory($form_element['#value'], $form_element);
  399. }
  400. return $form_element;
  401. }
  402. /**
  403. * Create the image directory relative to the 'files' dir recursively for every
  404. * directory in the path.
  405. *
  406. * @param $directory
  407. * The directory path under files to check, such as 'photo/path/here'
  408. * @param $form_element
  409. * A form element to throw an error on if the directory is not writable
  410. */
  411. function emthumb_check_directory($directory, $form_element = array()) {
  412. foreach (explode('/', $directory) as $dir) {
  413. $dirs[] = $dir;
  414. $path = file_create_path(implode($dirs, '/'));
  415. file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);
  416. }
  417. return true;
  418. }
  419. function _emthumb_scale_image($file, $resolution = 0) {
  420. $info = image_get_info($file['filepath']);
  421. if ($info) {
  422. list($width, $height) = explode('x', $resolution);
  423. if ($width && $height) {
  424. $result = image_scale($file['filepath'], $file['filepath'], $width, $height);
  425. if ($result) {
  426. $file['filesize'] = filesize($file['filepath']);
  427. drupal_set_message(t('The thumbnail was resized to fit within the maximum allowed resolution of %resolution pixels', array('%resolution' => $resolution)));
  428. }
  429. }
  430. }
  431. return $file;
  432. }
  433. function _emthumb_file_delete($file, $fieldname) {
  434. if (is_numeric($file['fid'])) {
  435. db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']);
  436. }
  437. else {
  438. unset($_SESSION['emthumb'][$fieldname][$file['sessionid']]);
  439. }
  440. return file_delete($file['filepath']);
  441. }
  442. function _emthumb_file_load($fid = NULL) {
  443. // Don't bother if we weren't passed an fid.
  444. if (isset($fid)) {
  445. // Test to catch fid, eventuall plan to have node_load syntax
  446. // once file_attributes table is complete
  447. if (is_numeric($fid)) {
  448. $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid);
  449. $file = db_fetch_array($result);
  450. return ($file) ? $file : array();
  451. }
  452. }
  453. return array();
  454. }
  455. function emthumb_clear_session() {
  456. if (is_array($_SESSION['emthumb']) && count($_SESSION['emthumb'])) {
  457. foreach (array_keys($_SESSION['emthumb']) as $fieldname) {
  458. emthumb_clear_field_session($fieldname);
  459. }
  460. unset($_SESSION['emthumb']);
  461. }
  462. }
  463. function emthumb_clear_field_session($fieldname) {
  464. if (is_array($_SESSION['emthumb'][$fieldname]) && count($_SESSION['emthumb'][$fieldname])) {
  465. foreach ($_SESSION['emthumb'][$fieldname] as $files) {
  466. if (is_file($file['filepath'])) {
  467. file_delete($file['filepath']);
  468. }
  469. }
  470. unset($_SESSION['emthumb'][$fieldname]);
  471. }
  472. }
  473. function emthumb_thumbnail_url($item) {
  474. if ($item['data']['emthumb']['filepath']) {
  475. return file_create_url($item['data']['emthumb']['filepath']);
  476. }
  477. }
  478. function theme_emthumb_view_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  479. return theme('emthumb_image', $file, $alt, $title, $attributes , $getsize);
  480. }
  481. function theme_emthumb_edit_image_row($element) {
  482. $output = '<div class="emthumb-edit-preview">'. drupal_render($element['preview']) .'</div>';
  483. $output .= '<div class="emthumb-edit-image-detail">';
  484. $output .= '<div class="emthumb-edit-image-flags">'. drupal_render($element['flags']) .'</div>';
  485. $output .= '<div class="emthumb-edit-image-description">'. drupal_render($element['description']);
  486. $output .= '</div>';
  487. $output .= drupal_render($element['emthumb_alt']);
  488. $output .= drupal_render($element['emthumb_title']);
  489. $output .= '</div>';
  490. //$output .= '<div class="imagefield-edit-image-fid">'. $element['fid']['#value'] .'</div>';
  491. $output = '<div class="emthumb-edit-image-row clear-block">'. $output .'</div>';
  492. if (isset($element['replace'])) {
  493. $output .= '<div class="emthumb-edit-image-replace">'. drupal_render($element['replace']) .'</div>';
  494. }
  495. return $output;
  496. }
  497. function theme_emthumb_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  498. // print_r($file);
  499. $file = (array)$file;
  500. if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) {
  501. $attributes = drupal_attributes($attributes);
  502. $path = $file['fid'] == 'upload' ? $file['preview'] : $file['filepath'];
  503. $alt = empty($alt) ? $file['emthumb_alt'] : $alt;
  504. $title = empty($title) ? $file['emthumb_title'] : $title;
  505. $url = file_create_url($path);
  506. return '<img src="'. check_url($url) .'" alt="'.
  507. check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' />';
  508. }
  509. }
  510. /**
  511. * formats an array of images.
  512. * @param images
  513. * array of individually themed images
  514. * @return
  515. * html string
  516. */
  517. function theme_emthumb_multiple($images) {
  518. return implode("\n", $images);
  519. }
  520. /**
  521. * implementation of hook_filedownload
  522. * replicated from upload.module.
  523. *
  524. * conditionally included since we're just replicating the
  525. * work of upload.module for now.
  526. */
  527. if (!function_exists('upload_file_download') && !function_exists('imagecache_file_download')) {
  528. function emthumb_file_download($file) {
  529. $file = file_create_path($file);
  530. $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file);
  531. if ($file = db_fetch_object($result)) {
  532. if (user_access('view uploaded files')) {
  533. $node = node_load($file->nid);
  534. if (node_access('view', $node)) {
  535. $name = mime_header_encode($file->filename);
  536. $type = mime_header_encode($file->filemime);
  537. // Serve images and text inline for the browser to display rather than download.
  538. $disposition = ereg('^(text/|image/)', $file->filemime) ? 'inline' : 'attachment';
  539. return array(
  540. 'Content-Type: '. $type .'; name='. $name,
  541. 'Content-Length: '. $file->filesize,
  542. 'Content-Disposition: '. $disposition .'; filename='. $name,
  543. 'Cache-Control: private'
  544. );
  545. }
  546. else {
  547. return -1;
  548. }
  549. }
  550. else {
  551. return -1;
  552. }
  553. }
  554. }
  555. }