imce.page.inc

  1. cis7 sites/all/modules/ulmus/imce/inc/imce.page.inc
  2. cle7 sites/all/modules/ulmus/imce/inc/imce.page.inc
  3. ecd7 sites/all/modules/ulmus/imce/inc/imce.page.inc
  4. elmsmedia7 sites/all/modules/ulmus/imce/inc/imce.page.inc
  5. harmony7 sites/all/modules/ulmus/imce/inc/imce.page.inc
  6. icor7 sites/all/modules/ulmus/imce/inc/imce.page.inc
  7. meedjum_blog7 sites/all/modules/ulmus/imce/inc/imce.page.inc
  8. mooc7 sites/all/modules/ulmus/imce/inc/imce.page.inc

Implements the file browser.

Functions

Namesort descending Description
imce q = imce.
imce_add_file Add a new file to the file list.
imce_check_directory Create a writable directory(any level) under file system directory.
imce_content Returns the content of the file browser.
imce_create_thumbnail Create a thumbnail.
imce_create_thumbnails Create all selected thumbnails.
imce_create_tree Create directory tree.
imce_delete_file Deletes a file in the file list.
imce_delete_filepath Deletes a file by uri.
imce_delete_form Delete form.
imce_delete_submit Submit delete form.
imce_directory_info Return the permissions for a directory that is accessed directly or indirectly. A child of a predefined directory in the directory list takes its parent's properties. If it has multiple parents, it gets the properties of the latter in the list.
imce_dir_uri Return the uri of the active directory
imce_fileop_form File operations form.
imce_fileop_form_validate Validate file operations form.
imce_file_register Registers the file as an IMCE file.
imce_inaccessible_directory Generate and log a directory access error.
imce_initiate_profile Initiate and return configuration profile for the $user.
imce_js Ajax operations. q=imce&jsop={op}
imce_page Returns the imce page for the specified user and the file scheme.
imce_perm_exists Check if a permission is given to at least one directory in the list.
imce_process_directories Translate tokens and evaluate php in directory names. Convert directories into an associative array (dirname => info)
imce_process_files Do batch operations on files. Used by delete, resize, create thumbnail submissions.
imce_process_profile Get files and folders of the actve directory. Do custom processing.
imce_remove_file Remove a file from the file list.
imce_resize_form Resizing form.
imce_resize_image Resize an image in the file list. Also used for thumbnail creation.
imce_resize_submit Submit resize form.
imce_scan_directory Scan directory and return file list, subdirectories, and total size.
imce_subdirectories_accessible Detect if the subdirectories are accessible through any directory(not just the current one) in the list.
imce_thumbnail_options Return thumbnails as options to be used in upload form.
imce_thumb_form Thumbnail form.
imce_thumb_submit Submit thumbnail form.
imce_tree_html Return tree html. This is not themable because it is complex and needs to be in a proper format for js processing.
imce_upload_form Upload form.
imce_upload_submit Submit upload form.
imce_user_page q = user/x/imce.
imce_validate_all Validate uploaded file.
imce_validate_filesize Validate filesize for maximum allowed file size.
imce_validate_quota Validate filesize for directory quota.
imce_validate_quotas Validate both directory and total user quota. Returns true/false not errors.
imce_validate_tuquota Validate filesize for total user quota.
imce_working_directory Return an avaliable directory for the profile.
theme_imce_root_text Returns the text for the root directory in a directory tree.
theme_imce_user_page Returns the html for user's file browser tab.

File

sites/all/modules/ulmus/imce/inc/imce.page.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Implements the file browser.
  5. */
  6. /**
  7. * q = imce.
  8. */
  9. function imce($scheme = NULL) {
  10. module_invoke('admin_menu', 'suppress');//suppress admin_menu
  11. $jsop = isset($_GET['jsop']) ? $_GET['jsop'] : NULL;
  12. drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
  13. print imce_page($GLOBALS['user'], $scheme, $jsop);
  14. exit();
  15. }
  16. /**
  17. * q = user/x/imce.
  18. */
  19. function imce_user_page($account) {
  20. return theme('imce_user_page', array('account' => $account));
  21. }
  22. /**
  23. * Returns the imce page for the specified user and the file scheme.
  24. */
  25. function imce_page($user, $scheme = NULL, $jsop = NULL) {
  26. return theme('imce_page', array('content' => imce_content($user, $scheme, $jsop)));
  27. }
  28. /**
  29. * Returns the content of the file browser.
  30. */
  31. function imce_content($user, $scheme = NULL, $jsop = NULL) {
  32. //execute ajax calls.
  33. if ($jsop) {
  34. return imce_js($user, $scheme, $jsop);
  35. }
  36. //initiate configuration profile
  37. if (!$imce = imce_initiate_profile($user, $scheme)) {
  38. return '';
  39. }
  40. imce_process_profile($imce);//get active directory content
  41. //Before creating the content let's add main files required for imce to function properly.
  42. $path = drupal_get_path('module', 'imce');
  43. drupal_add_js($path . '/js/jquery.form.js');
  44. drupal_add_js($path . '/js/imce.js');
  45. drupal_add_js($path . '/js/imce_extras.js');
  46. drupal_add_css($path . '/css/imce-content.css');
  47. //process forms.
  48. $imce_ref = array('imce' => &$imce);
  49. $forms = array();
  50. if (!$imce['error']) {
  51. //process file upload.
  52. if (imce_perm_exists($imce, 'upload')) {
  53. $forms[] = drupal_get_form('imce_upload_form', $imce_ref);
  54. }
  55. //process file operations.
  56. $forms[] = drupal_get_form('imce_fileop_form', $imce_ref);
  57. }
  58. $forms = drupal_render($forms);
  59. //run custom content functions. possible to insert extra forms. content is invisible when js is enabled.
  60. foreach (variable_get('imce_custom_content', array()) as $func => $state) {
  61. if ($state && function_exists($func) && $output = $func($imce)) {
  62. $forms .= $output;
  63. }
  64. }
  65. $content = theme('imce_content', array(
  66. 'tree' => imce_create_tree($imce),
  67. 'forms' => $forms,
  68. 'imce_ref' => $imce_ref,
  69. ));
  70. //make necessary changes for js conversion
  71. $imce['dir'] = str_replace('%2F', '/', rawurlencode($imce['dir']));
  72. unset($imce['files'], $imce['name'], $imce['directories'], $imce['subdirectories'], $imce['filesize'], $imce['quota'], $imce['tuquota'], $imce['thumbnails'], $imce['uid'], $imce['usertab']);
  73. drupal_add_js($imce_ref, 'setting');
  74. return $content;
  75. }
  76. /**
  77. * Ajax operations. q=imce&jsop={op}
  78. */
  79. function imce_js($user, $scheme, $jsop = '') {
  80. $response = array();
  81. //data
  82. if ($imce = imce_initiate_profile($user, $scheme)) {
  83. imce_process_profile($imce);
  84. if (!$imce['error']) {
  85. module_load_include('inc', 'imce', 'inc/imce.js');
  86. if (function_exists($func = 'imce_js_' . $jsop)) {
  87. $response['data'] = $func($imce);
  88. }
  89. // Allow alteration of response.
  90. foreach (variable_get('imce_custom_response', array()) as $func => $state) {
  91. if ($state && function_exists($func)) {
  92. $func($jsop, $response, $imce, $user);
  93. }
  94. }
  95. }
  96. }
  97. //messages
  98. $response['messages'] = drupal_get_messages();
  99. //disable devel log.
  100. $GLOBALS['devel_shutdown'] = FALSE;
  101. //for upload we must return plain text header.
  102. drupal_add_http_header('Content-Type', 'text/' . ($jsop == 'upload' ? 'html' : 'javascript') . '; charset=utf-8');
  103. print drupal_json_encode($response);
  104. exit();
  105. }
  106. /**
  107. * Upload form.
  108. */
  109. function imce_upload_form($form, &$form_state, $ref) {
  110. $imce =& $ref['imce'];
  111. $form['imce'] = array(
  112. '#type' => 'file',
  113. '#title' => t('File'),
  114. '#size' => 30,
  115. );
  116. if (!empty($imce['thumbnails'])) {
  117. $form['thumbnails'] = array(
  118. '#type' => 'checkboxes',
  119. '#title' => t('Create thumbnails'),
  120. '#options' => imce_thumbnail_options($imce['thumbnails']),
  121. );
  122. }
  123. $form['upload'] = array(
  124. '#type' => 'submit',
  125. '#value' => t('Upload'),
  126. '#submit' => $imce['perm']['upload'] ? array('imce_upload_submit') : NULL,
  127. );
  128. $form = array('fset_upload' => array('#type' => 'fieldset', '#title' => t('Upload file')) + $form);
  129. $form['#attributes']['enctype'] = 'multipart/form-data';
  130. $form['#action'] = $imce['url'];
  131. return $form;
  132. }
  133. /**
  134. * File operations form.
  135. */
  136. function imce_fileop_form($form, &$form_state, $ref) {
  137. $imce =& $ref['imce'];
  138. $form['filenames'] = array(
  139. '#type' => 'textfield',
  140. '#title' => t('Selected files'),
  141. '#maxlength' => $imce['filenum'] ? $imce['filenum']*255 : NULL,
  142. );
  143. //thumbnail
  144. if (!empty($imce['thumbnails']) && imce_perm_exists($imce, 'thumb')) {
  145. $form['fset_thumb'] = array(
  146. '#type' => 'fieldset',
  147. '#title' => t('Thumbnails'),
  148. ) + imce_thumb_form($imce);
  149. }
  150. //delete
  151. if (imce_perm_exists($imce, 'delete')) {
  152. $form['fset_delete'] = array(
  153. '#type' => 'fieldset',
  154. '#title' => t('Delete'),
  155. ) + imce_delete_form($imce);
  156. }
  157. //resize
  158. if (imce_perm_exists($imce, 'resize')) {
  159. $form['fset_resize'] = array(
  160. '#type' => 'fieldset',
  161. '#title' => t('Resize'),
  162. ) + imce_resize_form($imce);
  163. }
  164. $form['#action'] = $imce['url'];
  165. return $form;
  166. }
  167. /**
  168. * Thumbnail form.
  169. */
  170. function imce_thumb_form(&$imce) {
  171. $form['thumbnails'] = array(
  172. '#type' => 'checkboxes',
  173. '#title' => t('Thumbnails'),
  174. '#options' => imce_thumbnail_options($imce['thumbnails']),
  175. );
  176. $form['thumb'] = array(
  177. '#type' => 'submit',
  178. '#value' => t('Create thumbnails'),
  179. '#submit' => $imce['perm']['thumb'] ? array('imce_thumb_submit') : NULL,
  180. );
  181. return $form;
  182. }
  183. /**
  184. * Delete form.
  185. */
  186. function imce_delete_form(&$imce) {
  187. $form['delete'] = array(
  188. '#type' => 'submit',
  189. '#value' => t('Delete'),
  190. '#submit' => $imce['perm']['delete'] ? array('imce_delete_submit') : NULL,
  191. );
  192. return $form;
  193. }
  194. /**
  195. * Resizing form.
  196. */
  197. function imce_resize_form(&$imce) {
  198. $form['width'] = array(
  199. '#type' => 'textfield',
  200. '#title' => t('Width x Height'),
  201. '#size' => 5,
  202. '#maxlength' => 4,
  203. '#prefix' => '<div class="container-inline">',
  204. );
  205. $form['height'] = array(
  206. '#type' => 'textfield',
  207. '#size' => 5,
  208. '#maxlength' => 4,
  209. '#prefix' => 'x',
  210. );
  211. $form['resize'] = array(
  212. '#type' => 'submit',
  213. '#value' => t('Resize'),
  214. '#submit' => $imce['perm']['resize'] ? array('imce_resize_submit') : NULL,//permission for submission
  215. '#suffix' => '</div>',
  216. );
  217. $form['copy'] = array(
  218. '#type' => 'checkbox',
  219. '#title' => t('Create a new image'),
  220. '#default_value' => 1,
  221. );
  222. return $form;
  223. }
  224. /**
  225. * Validate file operations form.
  226. */
  227. function imce_fileop_form_validate($form, &$form_state) {
  228. $imce =& $form_state['build_info']['args'][0]['imce'];
  229. //check if the filenames is empty
  230. if ($form_state['values']['filenames'] == '') {
  231. return form_error($form['filenames'], t('Please select a file.'));
  232. }
  233. //filenames come separated by colon
  234. $filenames = explode(':', $form_state['values']['filenames']);
  235. $cnt = count($filenames);
  236. //check the number of files.
  237. if ($imce['filenum'] && $cnt > $imce['filenum']) {
  238. return form_error($form['filenames'], t('You are not allowed to operate on more than %num files.', array('%num' => $imce['filenum'])));
  239. }
  240. //check if there is any illegal choice
  241. for ($i = 0; $i < $cnt; $i++) {
  242. $filenames[$i] = $filename = rawurldecode($filenames[$i]);
  243. if (!isset($imce['files'][$filename])) {
  244. watchdog('imce', 'Illegal choice %choice in !name element.', array('%choice' => $filename, '!name' => t('directory (%dir)', array('%dir' => imce_dir_uri($imce)))), WATCHDOG_ERROR);
  245. return form_error($form['filenames'], t('An illegal choice has been detected. Please contact the site administrator.'));
  246. }
  247. }
  248. $form_state['values']['filenames'] = $filenames;
  249. }
  250. /**
  251. * Submit upload form.
  252. */
  253. function imce_upload_submit($form, &$form_state) {
  254. $form_state['redirect'] = FALSE;
  255. $imce =& $form_state['build_info']['args'][0]['imce'];
  256. // Need to provide extension validatior, otherwise file_save_upload uses the default.
  257. $validators['file_validate_extensions'] = array($imce['extensions'] === '*' ? NULL : $imce['extensions']);
  258. $validators['imce_validate_all'] = array(&$imce);
  259. $diruri = imce_dir_uri($imce);
  260. //save uploaded file.
  261. $replace = variable_get('imce_settings_replace', FILE_EXISTS_RENAME);
  262. if ($file = file_save_upload('imce', $validators, $diruri, $replace)) {
  263. //core bug #54223.
  264. if ($replace == FILE_EXISTS_RENAME) {
  265. $name = basename($file->uri);
  266. if ($name != $file->filename) {
  267. $file->filename = $name;
  268. drupal_set_message(t('The file has been renamed to %filename.', array('%filename' => $file->filename)));
  269. }
  270. }
  271. $file->uid = $imce['uid'];//global user may not be the owner.
  272. $file->status = FILE_STATUS_PERMANENT;
  273. file_save($file);
  274. imce_file_register($file);
  275. drupal_set_message(t('%filename has been uploaded.', array('%filename' => $file->filename)));
  276. //update file list
  277. $img = imce_image_info($file->uri);
  278. $file->width = $img ? $img['width'] : 0;
  279. $file->height = $img ? $img['height'] : 0;
  280. imce_add_file($file, $imce);
  281. //create thumbnails
  282. if (isset($form_state['values']['thumbnails']) && $img) {
  283. imce_create_thumbnails($file->filename, $imce, $form_state['values']['thumbnails']);
  284. }
  285. }
  286. else {
  287. drupal_set_message(t('Upload failed.'), 'error');
  288. }
  289. }
  290. /**
  291. * Submit thumbnail form.
  292. */
  293. function imce_thumb_submit($form, &$form_state) {
  294. $form_state['redirect'] = FALSE;
  295. $imce =& $form_state['build_info']['args'][0]['imce'];
  296. //create thumbnails
  297. imce_process_files($form_state['values']['filenames'], $imce, 'imce_create_thumbnails', array($form_state['values']['thumbnails']));
  298. }
  299. /**
  300. * Submit delete form.
  301. */
  302. function imce_delete_submit($form, &$form_state) {
  303. $form_state['redirect'] = FALSE;
  304. $imce =& $form_state['build_info']['args'][0]['imce'];
  305. $deleted = imce_process_files($form_state['values']['filenames'], $imce, 'imce_delete_file');
  306. if (!empty($deleted)) {
  307. drupal_set_message(t('File deletion successful: %files.', array('%files' => utf8_encode(implode(', ', $deleted)))));
  308. }
  309. }
  310. /**
  311. * Submit resize form.
  312. */
  313. function imce_resize_submit($form, &$form_state) {
  314. $form_state['redirect'] = FALSE;
  315. $imce =& $form_state['build_info']['args'][0]['imce'];
  316. //check dimensions
  317. $width = (int) $form_state['values']['width'];
  318. $height = (int) $form_state['values']['height'];
  319. list($maxw, $maxh) = $imce['dimensions'] ? explode('x', $imce['dimensions']) : array(0, 0);
  320. if ($width < 1 || $height < 1 || ($maxw && ($width > $maxw || $height > $maxh))) {
  321. drupal_set_message(t('Please specify dimensions within the allowed range that is from 1x1 to @dimensions.', array('@dimensions' => $imce['dimensions'] ? $imce['dimensions'] : t('unlimited'))), 'error');
  322. return;
  323. }
  324. $resized = imce_process_files($form_state['values']['filenames'], $imce, 'imce_resize_image', array($width, $height, $form_state['values']['copy']));
  325. if (!empty($resized)) {
  326. drupal_set_message(t('File resizing successful: %files.', array('%files' => utf8_encode(implode(', ', $resized)))));
  327. }
  328. }
  329. /**
  330. * Do batch operations on files.
  331. * Used by delete, resize, create thumbnail submissions.
  332. */
  333. function imce_process_files($filenames, &$imce, $function, $args = array()) {
  334. $args = array_merge(array('', &$imce), $args);
  335. $processed = array();
  336. foreach ($filenames as $filename) {
  337. $args[0] = $filename;
  338. if (call_user_func_array($function, $args)) {
  339. $processed[] = $filename;
  340. }
  341. }
  342. return $processed;
  343. }
  344. /**
  345. * Deletes a file in the file list.
  346. */
  347. function imce_delete_file($filename, &$imce) {
  348. $uri = imce_dir_uri($imce) . $filename;
  349. if (!imce_delete_filepath($uri)) {
  350. return FALSE;
  351. }
  352. imce_remove_file($filename, $imce);
  353. return TRUE;
  354. }
  355. /**
  356. * Deletes a file by uri.
  357. */
  358. function imce_delete_filepath($uri) {
  359. $file = file_load_multiple(array(), array('uri' => $uri));
  360. $file = reset($file);
  361. // File exists in database
  362. if ($file) {
  363. $usage = file_usage_list($file);
  364. $is_imce = isset($usage['imce']);
  365. unset($usage['imce']);
  366. // File is in use by an other module.
  367. if (!empty($usage)) {
  368. drupal_set_message(t('%filename is in use by another application.', array('%filename' => $file->filename)), 'error');
  369. return FALSE;
  370. }
  371. // Force delete file. Prevent running file_usage_list() second time.
  372. if (!file_delete($file, TRUE)) {
  373. return FALSE;
  374. }
  375. }
  376. // Not in db. Probably loaded via ftp.
  377. elseif (!file_unmanaged_delete($uri)) {
  378. return FALSE;
  379. }
  380. return TRUE;
  381. }
  382. /**
  383. * Create all selected thumbnails.
  384. */
  385. function imce_create_thumbnails($filename, &$imce, $values) {
  386. $created = array();
  387. foreach ($imce['thumbnails'] as $thumbnail) {
  388. if ($values[$thumbnail['name']] && imce_create_thumbnail($filename, $imce, $thumbnail)) {
  389. $created[] = $thumbnail['name'];
  390. }
  391. }
  392. if (!empty($created)) {
  393. drupal_set_message(t('Thumbnail creation (%thumbnames) successful for %filename.', array('%thumbnames' => implode(', ', $created), '%filename' => utf8_encode($filename))));
  394. }
  395. return $created;
  396. }
  397. /**
  398. * Create a thumbnail.
  399. */
  400. function imce_create_thumbnail($filename, &$imce, $thumbnail) {
  401. //generate thumbnail name
  402. $name = $thumbnail['prefix'];
  403. if ($thumbnail['suffix'] != '' && $dot = strrpos($filename, '.')) {
  404. $name .= substr($filename, 0, $dot);
  405. $name .= $thumbnail['suffix'];
  406. $name .= substr($filename, $dot);
  407. }
  408. else {
  409. $name .= $filename;
  410. }
  411. //scale the image
  412. list($width, $height) = explode('x', $thumbnail['dimensions']);
  413. return imce_resize_image($filename, $imce, $width, $height, TRUE, $name, variable_get('imce_settings_thumb_method', 'scale_and_crop'));
  414. }
  415. /**
  416. * Resize an image in the file list. Also used for thumbnail creation.
  417. */
  418. function imce_resize_image($filename, &$imce, $width, $height, $copy = TRUE, $destname = FALSE, $op = 'resize') {
  419. $destdir = imce_dir_uri($imce);
  420. $imguri = $destdir . $filename;
  421. //check if the file is an image
  422. if (!$imce['files'][$filename]['width'] || !$img = imce_image_info($imguri)) {
  423. drupal_set_message(t('%filename is not an image.', array('%filename' => utf8_encode($filename))), 'error', FALSE);
  424. return FALSE;
  425. }
  426. if (substr($op, 0, 5) == 'scale' && !($width < $img['width'] || $height < $img['height'])) {
  427. drupal_set_message(t('Scaling up is not allowed.'), 'error', FALSE);
  428. return FALSE;
  429. }
  430. //create file object
  431. $file = new stdClass();
  432. $file->uri = $destdir . $destname;
  433. if (!$destname || $destname == $filename) {
  434. $file->uri = $copy ? file_create_filename($filename, $destdir) : $imguri;
  435. }
  436. $file->filename = basename($file->uri);
  437. //check if a file having the same properties exists already.
  438. if (isset($imce['files'][$file->filename])) {
  439. if (($f = $imce['files'][$file->filename]) && $f['width'] == $width && $f['height'] == $height) {
  440. drupal_set_message(t('%filename(%dimensions) already exists.', array('%filename' => utf8_encode($file->filename), '%dimensions' => $width . 'x' . $height)), 'error');
  441. return FALSE;
  442. }
  443. }
  444. //validate file name
  445. $errors = file_validate_name_length($file);
  446. if (!empty($errors)) {
  447. drupal_set_message($errors[0], 'error');
  448. return FALSE;
  449. }
  450. //resize image
  451. $image = image_load($imguri);
  452. $function = 'image_' . $op;
  453. if (!$image || !function_exists($function) || !$function($image, $width, $height)) {
  454. drupal_set_message(t('%filename cannot be resized to %dimensions', array('%filename' => utf8_encode($filename), '%dimensions' => $width . 'x' . $height)), 'error', FALSE);
  455. return FALSE;
  456. }
  457. //copy to a temp file
  458. if (!$tempuri = drupal_tempnam('temporary://', 'imce')) {
  459. return FALSE;
  460. }
  461. register_shutdown_function('file_unmanaged_delete', $tempuri);
  462. if (!image_save($image, $tempuri) || !$image->info) {
  463. return FALSE;
  464. }
  465. //validate quota
  466. $file->filesize = $image->info['file_size'];
  467. $quotadiff = $file->filename == $filename ? -$imce['files'][$filename]['size'] : 0;
  468. if (!imce_validate_quotas($file, $imce, $quotadiff)) {
  469. return FALSE;
  470. }
  471. //build the rest of the file object
  472. $file->uid = $imce['uid'];
  473. $file->filemime = $img['mime'];
  474. $file->status = FILE_STATUS_PERMANENT;
  475. $file->timestamp = REQUEST_TIME;
  476. //copy from temp to file uri
  477. $destination = $file->uri;
  478. $file->uri = $tempuri;
  479. if (!$file = file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
  480. return FALSE;
  481. }
  482. imce_file_register($file);
  483. //update file list
  484. //if the file was scaled get the new dimensions
  485. if ($op == 'scale') {
  486. $img = imce_image_info($file->uri);
  487. $width = $img['width'];
  488. $height = $img['height'];
  489. }
  490. $file->width = $width;
  491. $file->height = $height;
  492. imce_add_file($file, $imce);
  493. return $file;
  494. }
  495. /**
  496. * Add a new file to the file list.
  497. */
  498. function imce_add_file($file, &$imce) {
  499. $imce['dirsize'] += $file->filesize;
  500. if (isset($imce['files'][$file->filename])) {
  501. $imce['dirsize'] -= $imce['files'][$file->filename]['size'];
  502. }
  503. $imce['files'][$file->filename] = array(
  504. 'name' => $file->filename,
  505. 'size' => $file->filesize,
  506. 'width' => $file->width,
  507. 'height' => $file->height,
  508. 'date' => $file->timestamp
  509. );
  510. if (isset($_GET['jsop'])) {
  511. $add = $imce['files'][$file->filename];
  512. $add['name'] = rawurlencode($file->filename);
  513. $add['fsize'] = format_size($file->filesize);
  514. $add['fdate'] = format_date($file->timestamp, 'short');
  515. $add['id'] = $file->fid;
  516. $imce['added'][] = $add;
  517. }
  518. }
  519. /**
  520. * Remove a file from the file list.
  521. */
  522. function imce_remove_file($filename, &$imce) {
  523. if (isset($imce['files'][$filename])) {
  524. $imce['dirsize'] -= $imce['files'][$filename]['size'];
  525. unset($imce['files'][$filename]);
  526. if (isset($_GET['jsop'])) {
  527. $imce['removed'][] = rawurlencode($filename);
  528. }
  529. }
  530. }
  531. /**
  532. * Validate uploaded file.
  533. */
  534. function imce_validate_all($file, $imce) {
  535. //validate image resolution only if filesize validation passes.
  536. //because user might have uploaded a very big image
  537. //and scaling it may exploit system memory.
  538. $errors = imce_validate_filesize($file, $imce['filesize']);
  539. //image resolution validation
  540. if (empty($errors) && preg_match('/\.(png|gif|jpe?g)$/i', $file->filename)) {
  541. $errors = array_merge($errors, file_validate_image_resolution($file, $imce['dimensions']));
  542. }
  543. //directory quota validation
  544. if ($imce['quota']) {
  545. $errors = array_merge($errors, imce_validate_quota($file, $imce['quota'], $imce['dirsize']));
  546. }
  547. //user quota validation. check it if no errors were thrown.
  548. if (empty($errors) && $imce['tuquota']) {
  549. $errors = imce_validate_tuquota($file, $imce['tuquota'], file_space_used($imce['uid']));
  550. }
  551. return $errors;
  552. }
  553. /**
  554. * Validate filesize for maximum allowed file size.
  555. */
  556. function imce_validate_filesize($file, $maxsize = 0) {
  557. $errors = array();
  558. if ($maxsize && $file->filesize > $maxsize) {
  559. $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file->filesize), '%maxsize' => format_size($maxsize)));
  560. }
  561. return $errors;
  562. }
  563. /**
  564. * Validate filesize for directory quota.
  565. */
  566. function imce_validate_quota($file, $quota = 0, $currentsize = 0) {
  567. $errors = array();
  568. if ($quota && ($currentsize + $file->filesize) > $quota) {
  569. $errors[] = t('%filename is %filesize which would exceed your directory quota. You are currently using %size of %total_quota.', array('%size' => format_size($currentsize), '%total_quota' => format_size($quota), '%filesize' => format_size($file->filesize), '%filename' => utf8_encode($file->filename)));
  570. }
  571. return $errors;
  572. }
  573. /**
  574. * Validate filesize for total user quota.
  575. */
  576. function imce_validate_tuquota($file, $quota = 0, $currentsize = 0) {
  577. $errors = array();
  578. if ($quota && ($currentsize + $file->filesize) > $quota) {
  579. $errors[] = t('%filename is %filesize which would exceed your total user quota. You are currently using %size of %total_quota.', array('%size' => format_size($currentsize), '%total_quota' => format_size($quota), '%filesize' => format_size($file->filesize), '%filename' => utf8_encode($file->filename)));
  580. }
  581. return $errors;
  582. }
  583. /**
  584. * Validate both directory and total user quota. Returns true/false not errors.
  585. */
  586. function imce_validate_quotas($file, &$imce, $add = 0) {
  587. $errors = imce_validate_quota($file, $imce['quota'], $imce['dirsize'] + $add);
  588. if (empty($errors) && $imce['tuquota']) {
  589. $errors = imce_validate_tuquota($file, $imce['tuquota'], file_space_used($imce['uid']) + $add);
  590. }
  591. if (!empty($errors)) {
  592. drupal_set_message($errors[0], 'error');
  593. return FALSE;
  594. }
  595. return TRUE;
  596. }
  597. /**
  598. * Checks if the file is an image and returns info.
  599. * There are two switchable versions that use image_get_info() and getimagesize()
  600. */
  601. if (variable_get('imce_image_get_info', 0)) {
  602. function imce_image_info($file) {
  603. $mimes = array('image/jpeg' => IMAGETYPE_JPEG, 'image/gif' => IMAGETYPE_GIF, 'image/png' => IMAGETYPE_PNG);
  604. if (is_file($file) && ($dot = strrpos($file, '.')) && in_array(strtolower(substr($file, $dot+1)), array('jpg', 'jpeg', 'gif', 'png')) && ($info = @image_get_info($file)) && isset($mimes[$info['mime_type']]) ) {
  605. return array('width' => $info['width'], 'height' => $info['height'], 'type' => $mimes[$info['mime_type']], 'mime' => $info['mime_type']);
  606. }
  607. return FALSE;
  608. }
  609. }
  610. else {
  611. function imce_image_info($file) {
  612. if (is_file($file) && ($dot = strrpos($file, '.')) && in_array(strtolower(substr($file, $dot+1)), array('jpg', 'jpeg', 'gif', 'png')) && ($info = @getimagesize($file)) && in_array($info[2], array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG)) ) {
  613. return array('width' => $info[0], 'height' => $info[1], 'type' => $info[2], 'mime' => $info['mime']);
  614. }
  615. return FALSE;
  616. }
  617. }
  618. /**
  619. * Return thumbnails as options to be used in upload form.
  620. */
  621. function imce_thumbnail_options($thumbs = array()) {
  622. $options = array();
  623. foreach ($thumbs as $thumb) {
  624. $options[$thumb['name']] = $thumb['name'] . ' (' . $thumb['dimensions'] . ')';
  625. }
  626. return $options;
  627. }
  628. /**
  629. * Initiate and return configuration profile for the $user.
  630. */
  631. function imce_initiate_profile($user, $scheme = NULL) {
  632. //check user profile and translate tokens in directory paths and evaluate php paths.
  633. if ($imce = imce_user_profile($user, $scheme)) {
  634. // Allow alteration of raw profile
  635. foreach (variable_get('imce_custom_init', array()) as $func => $state) {
  636. if ($state && function_exists($func)) {
  637. $func($imce, $user);
  638. }
  639. }
  640. imce_process_directories($imce, $user);
  641. if (!empty($imce['directories'])) {
  642. $imce['uid'] = (int) $user->uid;
  643. $imce['url'] = url($_GET['q']);
  644. $imce['clean'] = variable_get('clean_url', 0) == 1;
  645. $imce['absurls'] = variable_get('imce_settings_absurls', 0) == 1;
  646. $imce['furl'] = file_create_url($imce['scheme'] . '://');
  647. $imce['filesize'] *= 1048576;//convert from Mb to byte
  648. $imce['quota'] *= 1048576;
  649. $imce['tuquota'] *= 1048576;
  650. $imce['filenum'] = (int) $imce['filenum'];
  651. //check and set the active directory
  652. if ($info = imce_working_directory($imce)) {
  653. $imce['direct'] = isset($imce['directories'][$info['name']]);
  654. $imce['directories'][$info['name']] = $info;
  655. $imce['dir'] = $info['name'];
  656. $imce['perm'] = $info;//copy permissions of the active directory.
  657. unset($imce['perm']['name']);
  658. }
  659. else {
  660. drupal_set_message(t('Unable to get a working directory for the file browser!'), 'error');
  661. $imce['dir'] = FALSE;
  662. $imce['error'] = TRUE;
  663. }
  664. return $imce;
  665. }
  666. drupal_set_message(t('There is no valid directory specified for the file browser!'), 'error');
  667. }
  668. else {
  669. drupal_set_message(t('You do not have access to any configuration profile to use the file browser!'), 'error');
  670. }
  671. return FALSE;
  672. }
  673. /**
  674. * Get files and folders of the actve directory. Do custom processing.
  675. */
  676. function imce_process_profile(&$imce) {
  677. //get directory content. do a custom scan if it is set
  678. $scan = ($scan = variable_get('imce_custom_scan', '')) && function_exists($scan) ? $scan : 'imce_scan_directory';
  679. $imce += $scan($imce['dir'], $imce);
  680. //run custom process functions
  681. foreach (variable_get('imce_custom_process', array()) as $func => $state) {
  682. if ($state && function_exists($func)) {
  683. $func($imce);
  684. }
  685. }
  686. //set subdirectories
  687. if (!$imce['error'] && !imce_subdirectories_accessible($imce)) {
  688. $imce['subdirectories'] = array();
  689. }
  690. }
  691. /**
  692. * Translate tokens and evaluate php in directory names.
  693. * Convert directories into an associative array (dirname => info)
  694. */
  695. function imce_process_directories(&$imce, $user) {
  696. $directories = $imce['directories'];
  697. $paths = array();
  698. $translate = array('%uid' => $user->uid);
  699. foreach ($directories as $directory) {
  700. if (substr($directory['name'], 0, 4) == 'php:') {
  701. $directory['name'] = eval(substr($directory['name'], 4));
  702. //php may return an array of directories
  703. if (is_array($directory['name'])) {
  704. foreach ($directory['name'] as $name) {
  705. $paths[$name] = array('name' => $name) + $directory;
  706. }
  707. continue;
  708. }
  709. }
  710. else {
  711. $directory['name'] = strtr($directory['name'], $translate);
  712. }
  713. if ($directory['name']) {
  714. $paths[$directory['name']] = $directory;
  715. }
  716. }
  717. $imce['directories'] = $paths;
  718. }
  719. /**
  720. * Return an avaliable directory for the profile.
  721. */
  722. function imce_working_directory(&$imce) {
  723. //Do not use session if there is only one directory assigned.
  724. $sess = TRUE;
  725. if (count($imce['directories']) < 2) {
  726. $perms = reset($imce['directories']);
  727. if (!isset($perms['subnav']) || !$perms['subnav']) {
  728. $sess = FALSE;
  729. }
  730. }
  731. //check GET.
  732. if (isset($_GET['dir'])) {
  733. if ($info = imce_directory_info($_GET['dir'], $imce)) {
  734. if (imce_check_directory($_GET['dir'], $imce)) {
  735. if ($sess) {
  736. $_SESSION['imce_directory'] = rawurlencode($info['name']);
  737. }
  738. }
  739. else {
  740. $info = FALSE;
  741. }
  742. }
  743. else {
  744. imce_inaccessible_directory($_GET['dir'], $imce);
  745. }
  746. return $info;
  747. }
  748. //check session
  749. if ($sess && isset($_SESSION['imce_directory'])) {
  750. $dirname = rawurldecode($_SESSION['imce_directory']);
  751. if ($info = imce_directory_info($dirname, $imce)) {
  752. if (imce_check_directory($dirname, $imce)) {
  753. return $info;
  754. }
  755. }
  756. }
  757. //or the whole list.
  758. foreach ($imce['directories'] as $dirname => $info) {
  759. $dirname = (string) $dirname;
  760. if (imce_check_directory($dirname, $imce)) {
  761. if ($sess) {
  762. $_SESSION['imce_directory'] = rawurlencode($dirname);
  763. }
  764. return $info;
  765. }
  766. }
  767. return FALSE;
  768. }
  769. /**
  770. * Create a writable directory(any level) under file system directory.
  771. */
  772. function imce_check_directory($dirname, $imce) {
  773. $diruri = imce_dir_uri($imce, $dirname);
  774. if (!imce_reg_dir($dirname) || !file_prepare_directory($diruri, FILE_CREATE_DIRECTORY)) {
  775. return imce_inaccessible_directory($dirname, $imce);
  776. }
  777. return TRUE;
  778. }
  779. /**
  780. * Generate and log a directory access error.
  781. */
  782. function imce_inaccessible_directory($dirname, $imce) {
  783. if (is_string($dirname)) {
  784. $dirname = utf8_encode($dirname);
  785. $diruri = imce_dir_uri($imce, $dirname);
  786. drupal_set_message(t('Directory %dirname is not accessible.', array('%dirname' => $dirname)), 'error');
  787. watchdog('imce', 'Access to %directory was denied.', array('%directory' => $diruri), WATCHDOG_ERROR);
  788. }
  789. return FALSE;
  790. }
  791. /**
  792. * Return the permissions for a directory that is accessed directly or indirectly.
  793. * A child of a predefined directory in the directory list takes its parent's properties.
  794. * If it has multiple parents, it gets the properties of the latter in the list.
  795. */
  796. function imce_directory_info($dirname, $imce) {
  797. if (isset($imce['directories'][$dirname])) {
  798. return $imce['directories'][$dirname];
  799. }
  800. $info = FALSE;
  801. if (imce_reg_dir($dirname)) {
  802. $diruri = imce_dir_uri($imce, $dirname);
  803. if (file_prepare_directory($diruri)) {
  804. foreach ($imce['directories'] as $name => $prop) {
  805. if ($prop['subnav'] && ($name === '.' || strpos($dirname . '/', $name . '/') === 0)) {
  806. $info = $prop;
  807. $info['name'] = $dirname;
  808. }
  809. }
  810. }
  811. }
  812. return $info;
  813. }
  814. /**
  815. * Detect if the subdirectories are accessible through any directory(not just the current one) in the list.
  816. */
  817. function imce_subdirectories_accessible(&$imce) {
  818. if (!empty($imce['subdirectories'])) {
  819. if (!empty($imce['perm']['subnav'])) {
  820. return TRUE;
  821. }
  822. //checking only the first one is sufficient.
  823. $dirname = ($imce['dir'] == '.' ? '' : $imce['dir'] . '/') . $imce['subdirectories'][0];
  824. //check if any setting is applicable for this subdirectory through any directory in the list.
  825. foreach ($imce['directories'] as $name => $info) {
  826. $name = (string) $name;
  827. if ($info['subnav'] && $dirname != $name && ($name == '.' || strpos($dirname . '/', $name . '/') === 0)) {
  828. return TRUE;
  829. }
  830. }
  831. }
  832. return FALSE;
  833. }
  834. /**
  835. * Check if a permission is given to at least one directory in the list.
  836. */
  837. function imce_perm_exists(&$imce, $perm) {
  838. static $perms = array();
  839. if (isset($perms[$perm])) {
  840. return $perms[$perm];
  841. }
  842. if (isset($imce['perm'][$perm]) && $imce['perm'][$perm]) {
  843. return $perms[$perm] = TRUE;
  844. }
  845. foreach ($imce['directories'] as $name => $info) {
  846. if (isset($info[$perm]) && $info[$perm]) {
  847. return $perms[$perm] = TRUE;
  848. }
  849. }
  850. return $perms[$perm] = FALSE;
  851. }
  852. /**
  853. * Scan directory and return file list, subdirectories, and total size.
  854. */
  855. function imce_scan_directory($dirname, $imce) {
  856. $directory = array('dirsize' => 0, 'files' => array(), 'subdirectories' => array(), 'error' => FALSE);
  857. $diruri = imce_dir_uri($imce, $dirname);
  858. if (!is_string($dirname) || $dirname == '' || !$handle = opendir($diruri)) {
  859. imce_inaccessible_directory($dirname, $imce);
  860. $directory['error'] = TRUE;
  861. return $directory;
  862. }
  863. while (($file = readdir($handle)) !== FALSE) {
  864. // Do not include dot files and folders
  865. if (substr($file, 0, 1) === '.') {
  866. continue;
  867. }
  868. $path = $diruri . $file;
  869. if (is_dir($path)) {
  870. $directory['subdirectories'][] = $file;
  871. continue;
  872. }
  873. $width = $height = 0;
  874. if ($img = imce_image_info($path)) {
  875. $width = $img['width'];
  876. $height = $img['height'];
  877. }
  878. $size = filesize($path);
  879. $date = filemtime($path);
  880. $directory['files'][$file] = array(
  881. 'name' => $file,
  882. 'size' => $size,
  883. 'width' => $width,
  884. 'height' => $height,
  885. 'date' => $date
  886. );
  887. $directory['dirsize'] += $size;
  888. }
  889. closedir($handle);
  890. sort($directory['subdirectories']);
  891. return $directory;
  892. }
  893. /**
  894. * Create directory tree.
  895. */
  896. function imce_create_tree(&$imce) {
  897. $paths = array();
  898. //rearrange paths as arg0=>arg1=>...
  899. foreach ($imce['directories'] as $path => $arr) {
  900. $tmp =& $paths;
  901. if ($path != '.') {
  902. $args = explode('/', $path);
  903. foreach ($args as $arg) {
  904. if (!isset($tmp[$arg])) {
  905. $tmp[$arg] = array();
  906. }
  907. $tmp =& $tmp[$arg];
  908. }
  909. $tmp[':access:'] = TRUE;
  910. }
  911. if ("$path" == $imce['dir']) {
  912. $tmp[':active:'] = TRUE;
  913. foreach ($imce['subdirectories'] as $arg) {
  914. $tmp[$arg][':access:'] = TRUE;
  915. }
  916. }
  917. }
  918. //set root branch
  919. $root = theme('imce_root_text', array('imce_ref' => array('imce' => &$imce)));
  920. $q = $imce['clean'] ? '?' : '&';
  921. if (isset($imce['directories']['.'])) {
  922. $root = '<a href="' . $imce['url'] . $q . 'dir=." title="." class="folder' . ($imce['dir'] == '.' ? ' active' : '') . '">' . $root . '</a>';
  923. }
  924. else {
  925. $root = '<a title="." class="folder disabled">' . $root . '</a>';
  926. }
  927. return $root . imce_tree_html($imce, $paths, $q);
  928. }
  929. /**
  930. * Return tree html.
  931. * This is not themable because it is complex and needs to be in a proper format for js processing.
  932. */
  933. function imce_tree_html(&$imce, $paths, $q = '?', $prefix = '', $eprefix = '') {
  934. unset($paths[':access:'], $paths[':active:']);
  935. $html = '';
  936. foreach ($paths as $arg => $children) {
  937. $path = $prefix . $arg;
  938. $earg = rawurlencode($arg);
  939. $epath = $eprefix . $earg;
  940. if (isset($children[':access:']) || imce_directory_info($path, $imce)) {
  941. $a = '<a href="' . $imce['url'] . $q . 'dir=' . $epath . '" title="' . $epath . '" class="folder' . (isset($children[':active:']) ? ' active' : '') . '">' . $earg . '</a>';
  942. }
  943. else {
  944. $a = '<a title="' . $epath . '" class="folder disabled">' . $earg . '</a>';
  945. }
  946. $ul = imce_tree_html($imce, $children, $q, $path . '/', $epath . '/');
  947. $class = $ul ? ' class="expanded"' : (isset($children[':active:']) ? ' class="leaf"' : '');
  948. $html .= '<li' . $class . '>' . $a . $ul . '</li>';
  949. }
  950. if ($html) {
  951. $html = '<ul>' . $html . '</ul>';
  952. }
  953. return $html;
  954. }
  955. /**
  956. * Return the uri of the active directory
  957. */
  958. function imce_dir_uri($imce, $dir = NULL) {
  959. if (!isset($dir)) {
  960. $dir = $imce['dir'];
  961. }
  962. return $imce['scheme'] . '://' . ($dir == '.' ? '' : $dir . '/');
  963. }
  964. /**
  965. * Returns the text for the root directory in a directory tree.
  966. */
  967. function theme_imce_root_text($variables) {
  968. //$imce = &$variables['imce_ref']['imce'];
  969. return '&lt;' . t('root') . '&gt;';
  970. }
  971. /**
  972. * Returns the html for user's file browser tab.
  973. */
  974. function theme_imce_user_page($variables) {
  975. global $user;
  976. $account = $variables['account'];
  977. $options = array();
  978. //switch to account's active folder
  979. if ($user->uid == 1 && $account->uid != 1) {
  980. $imce = imce_initiate_profile($account);
  981. $options['query'] = array('dir' => $imce['dir']);
  982. }
  983. return '<iframe src="' . url('imce', $options) . '" frameborder="0" style="border: 1px solid #eee; width: 99%; height: 520px" class="imce-frame"></iframe>';
  984. }
  985. /**
  986. * Registers the file as an IMCE file.
  987. */
  988. function imce_file_register($file) {
  989. if (!db_query("SELECT 1 FROM {file_usage} WHERE module = 'imce' AND fid = :fid", array(':fid' => $file->fid))->fetchField()) {
  990. file_usage_add($file, 'imce', 'file', $file->fid);
  991. }
  992. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.