drupal.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc CMS integration code: Drupal
  5. * @package KCFinder
  6. * @version 3.12
  7. * @author Dany Alejandro Cabrera <otello2040@gmail.com>
  8. * @copyright 2010-2014 KCFinder Project
  9. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  10. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  11. * @link http://kcfinder.sunhater.com
  12. */
  13. // gets a valid drupal_path
  14. function get_drupal_path() {
  15. if (!empty($_SERVER['SCRIPT_FILENAME'])) {
  16. $drupal_path = dirname(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME']))));
  17. if (!file_exists($drupal_path . '/includes/bootstrap.inc')) {
  18. $drupal_path = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])));
  19. $depth = 2;
  20. do {
  21. $drupal_path = dirname($drupal_path);
  22. $depth++;
  23. } while (!($bootstrap_file_found = file_exists($drupal_path . '/includes/bootstrap.inc')) && $depth < 10);
  24. }
  25. }
  26. if (!isset($bootstrap_file_found) || !$bootstrap_file_found) {
  27. $drupal_path = '../../../../..';
  28. if (!file_exists($drupal_path . '/includes/bootstrap.inc')) {
  29. $drupal_path = '../..';
  30. do {
  31. $drupal_path .= '/..';
  32. $depth = substr_count($drupal_path, '..');
  33. } while (!($bootstrap_file_found = file_exists($drupal_path . '/includes/bootstrap.inc')) && $depth < 10);
  34. }
  35. }
  36. return $drupal_path;
  37. }
  38. function CheckAuthentication($drupal_path) {
  39. static $authenticated;
  40. if (!isset($authenticated)) {
  41. if (!isset($bootstrap_file_found) || $bootstrap_file_found) {
  42. $current_cwd = getcwd();
  43. if (!defined('DRUPAL_ROOT')){
  44. define('DRUPAL_ROOT', $drupal_path);
  45. }
  46. // Simulate being in the drupal root folder so we can share the session
  47. chdir(DRUPAL_ROOT);
  48. global $base_url;
  49. $base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
  50. $base_url = $base_root .= '://'. preg_replace('/[^a-z0-9-:._]/i', '', $_SERVER['HTTP_HOST']);
  51. if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
  52. $base_path = "/$dir";
  53. $base_url .= $base_path;
  54. }
  55. // correct base_url so it points to Drupal root
  56. $pos = strpos($base_url, '/sites/');
  57. $base_url = substr($base_url, 0, $pos); // drupal root absolute url
  58. // If we aren't in a Drupal installation, or if Drupal path hasn't been properly found, die
  59. if(!file_exists(DRUPAL_ROOT . '/includes/bootstrap.inc')) {
  60. die("The CMS integration service for -drupal- requires KCFinder to be properly placed inside your Drupal installation.");
  61. }
  62. // bootstrap
  63. require_once(DRUPAL_ROOT . '/includes/bootstrap.inc');
  64. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  65. // if user has access permission...
  66. if (user_access('access kcfinder')) {
  67. if (!isset($_SESSION['KCFINDER'])) {
  68. $_SESSION['KCFINDER'] = array();
  69. $_SESSION['KCFINDER']['disabled'] = false;
  70. }
  71. // User has permission, so make sure KCFinder is not disabled!
  72. if(!isset($_SESSION['KCFINDER']['disabled'])) {
  73. $_SESSION['KCFINDER']['disabled'] = false;
  74. }
  75. global $user;
  76. $_SESSION['KCFINDER']['uploadURL'] = strtr(variable_get('kcfinder_upload_url', 'sites/default/files/kcfinder'), array('%u' => $user->uid, '%n' => $user->name));
  77. $_SESSION['KCFINDER']['uploadDir'] = strtr(variable_get('kcfinder_upload_dir', ''), array('%u' => $user->uid, '%n' => $user->name));
  78. $_SESSION['KCFINDER']['theme'] = variable_get('kcfinder_theme', 'oxygen');
  79. //echo '<br />uploadURL: ' . $_SESSION['KCFINDER']['uploadURL']<br />;
  80. //echo '<br />uploadDir: ' . $_SESSION['KCFINDER']['uploadDir']<br />;
  81. chdir($current_cwd);
  82. return true;
  83. }
  84. chdir($current_cwd);
  85. return false;
  86. }
  87. }
  88. }
  89. CheckAuthentication(get_drupal_path());
  90. ?>