PageRenderTime 69ms CodeModel.GetById 8ms RepoModel.GetById 13ms app.codeStats 0ms

/public/scripts/media_files/integration/drupal.php

https://gitlab.com/devtoannh/cafe
PHP | 115 lines | 71 code | 24 blank | 20 comment | 21 complexity | 9b1b0b90a89113834e7ac183bf075a2e MD5 | raw file
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc CMS integration code: Drupal
  5. * @package KCFinder
  6. * @version 2.51
  7. * @author Dany Alejandro Cabrera <otello2040@gmail.com>
  8. * @copyright 2010, 2011 KCFinder Project
  9. * @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
  10. * @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
  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. spl_autoload_register('__autoload');
  91. ?>