PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/public/cui/plugins/tinymce/plugins/imagemanager/plugins/ExternalAuthenticator/ExternalAuthenticator.php

http://github.com/centurion-project/Centurion
PHP | 152 lines | 84 code | 29 blank | 39 comment | 37 complexity | 56c07532d3166b3159a95c544578f572 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DrupalAuthenticatorImpl.php
  4. *
  5. * @package MCImageManager.authenicators
  6. * @author Moxiecode
  7. * @copyright Copyright © 2005, Moxiecode Systems AB, All rights reserved.
  8. */
  9. /**
  10. * This class is a External authenticator implementation.
  11. *
  12. * @package MCImageManager.Authenticators
  13. */
  14. class Moxiecode_ExternalAuthenticator extends Moxiecode_ManagerPlugin {
  15. /**#@+
  16. * @access public
  17. */
  18. /**
  19. * Main constructor.
  20. */
  21. function Moxiecode_ExternalAuthenticator() {
  22. }
  23. function onAuthenticate(&$man) {
  24. $config =& $man->getConfig();
  25. session_start();
  26. $authURL = $config['ExternalAuthenticator.external_auth_url'];
  27. $secretKey = $config['ExternalAuthenticator.secret_key'];
  28. $prefix = isset($config['ExternalAuthenticator.session_prefix']) ? $config['ExternalAuthenticator.session_prefix'] : "mcmanager_";
  29. $useCookie = isset($config['ExternalAuthenticator.use_cookie']) ? $config['ExternalAuthenticator.use_cookie'] == true : true;
  30. $dir = basename(dirname($_SERVER["PHP_SELF"]));
  31. // Always allow language packs to be loaded
  32. if ($dir == "language") {
  33. // Override language key
  34. if (isset($_SESSION[$prefix . "ExternalAuthenticator_general__language"]))
  35. $config["general.language"] = $_SESSION[$prefix . "ExternalAuthenticator_general__language"];
  36. return true;
  37. }
  38. // Check local session if authenticated
  39. if ($dir == "rpc" || $dir == "stream") {
  40. if (isset($_SESSION[$prefix . 'ExternalAuthenticator']) && $_SESSION[$prefix . 'ExternalAuthenticator'] == true) {
  41. if (!$useCookie || isset($_COOKIE[$prefix . 'enabled']) && $_COOKIE[$prefix . 'enabled'] == md5($secretKey . $_SERVER['REMOTE_ADDR'])) {
  42. foreach ($_SESSION as $key => $value) {
  43. if (strpos($key, $prefix . "ExternalAuthenticator_") === 0) {
  44. $key = str_replace("__", ".", $key);
  45. $key = substr($key, strlen($prefix . "ExternalAuthenticator_"));
  46. $config[$key] = $value;
  47. }
  48. }
  49. // Try create rootpath
  50. $rootPath = $man->toAbsPath($config['filesystem.rootpath']);
  51. $rootPathItems = explode(';', $rootPath);
  52. $rootPathItems = explode('=', $rootPathItems[0]);
  53. if (count($rootPathItems) > 1)
  54. $rootPath = $rootPathItems[1];
  55. else
  56. $rootPath = $rootPathItems[0];
  57. if (!file_exists($rootPath))
  58. @mkdir($rootPath);
  59. // Use rootpath as path
  60. if (!$config['filesystem.path'] || !$man->isChildPath($rootPath, $config['filesystem.path']))
  61. $config['filesystem.path'] = $rootPath;
  62. return true;
  63. }
  64. }
  65. }
  66. if (isset($_POST['key'])) {
  67. // Generate data chunk
  68. $data = "";
  69. $ignored = array("key");
  70. foreach ($_POST as $key => $value) {
  71. if (!in_array($key, $ignored))
  72. $data .= $value;
  73. }
  74. // Check input
  75. if ($_POST['key'] == md5($data . $secretKey)) {
  76. // Set authenticated session and cookie
  77. $_SESSION[$prefix . 'ExternalAuthenticator'] = true;
  78. if ($useCookie)
  79. setcookie($prefix . 'enabled', md5($secretKey . $_SERVER['REMOTE_ADDR']), 0, '/');
  80. // Set config parameters
  81. foreach ($_POST as $key => $value) {
  82. if (!in_array($key, $ignored)) {
  83. $_SESSION[$prefix . 'ExternalAuthenticator_' . $key] = $value;
  84. $config[$key] = $value;
  85. }
  86. }
  87. return true;
  88. } else {
  89. sleep(1); // Sleep for bots
  90. die("Invalid input make sure that the secret keys match.");
  91. }
  92. }
  93. // Force absolute
  94. if (strpos($authURL, "http") !== 0 && strpos($authURL, "/") !== 0)
  95. $authURL = "plugins/ExternalAuthenticator/" . $authURL;
  96. // Setup return URL
  97. $prot = "http";
  98. //$port = "";
  99. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on")
  100. $prot = "https";
  101. // Non default port
  102. //if ($_SERVER['SERVER_PORT'] != "80" && $_SERVER['SERVER_PORT'] != "443")
  103. // $port = ":" . $_SERVER['SERVER_PORT'];
  104. // If RPC or stream then return it using config
  105. if ($dir == "rpc" || $dir == "stream") {
  106. // This part doesn't work yet but isn't really needed.
  107. // Make it absolute
  108. if (strpos($authURL, "/") === 0)
  109. $authURL = $prot . "://" . $_SERVER['HTTP_HOST'] . $authURL;
  110. $returnURL = $prot . "://" . $_SERVER['HTTP_HOST'] . dirname(dirname($_SERVER['PHP_SELF'])) . "/index.php?type=" . $man->getType();
  111. $config['authenticator.login_page'] = $authURL . "?return_url=" . urlencode($returnURL);
  112. return false;
  113. }
  114. // Not logged redirect to External backend
  115. $returnURL = $prot . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?type=" . $man->getType();
  116. header('location: ' . $authURL . "?return_url=" . urlencode($returnURL));
  117. die();
  118. }
  119. /**#@-*/
  120. }
  121. // Add plugin to MCManager
  122. $man->registerPlugin("ExternalAuthenticator", new Moxiecode_ExternalAuthenticator());
  123. ?>