PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/184.168.182.1/admin/old/ckfinder/core/connector/php/php4/Core/Hooks.php

https://gitlab.com/endomorphosis/falkenstein
PHP | 173 lines | 110 code | 13 blank | 50 comment | 36 complexity | 6eadaad6e5908f2c6116157c51e7e7b7 MD5 | raw file
  1. <?php
  2. /*
  3. * CKFinder
  4. * ========
  5. * http://ckfinder.com
  6. * Copyright (C) 2007-2012, CKSource - Frederico Knabben. All rights reserved.
  7. *
  8. * The software, this file and its contents are subject to the CKFinder
  9. * License. Please read the license.txt file before using, installing, copying,
  10. * modifying or distribute this file or part of its contents. The contents of
  11. * this file is part of the Source Code of CKFinder.
  12. */
  13. if (!defined('IN_CKFINDER')) exit;
  14. /**
  15. * @package CKFinder
  16. * @subpackage Core
  17. * @copyright CKSource - Frederico Knabben
  18. */
  19. class CKFinder_Connector_Core_Hooks
  20. {
  21. /**
  22. * Run user defined hooks
  23. *
  24. * @param string $event
  25. * @param object $errorHandler
  26. * @param array $args
  27. * @return boolean (true to continue processing, false otherwise)
  28. */
  29. function run($event, $args = array())
  30. {
  31. $config = $GLOBALS['config'];
  32. if (!isset($config['Hooks'])) {
  33. return true;
  34. }
  35. $hooks =& $config['Hooks'];
  36. if (!is_array($hooks) || !array_key_exists($event, $hooks) || !is_array($hooks[$event])) {
  37. return true;
  38. }
  39. $errorHandler = $GLOBALS['connector']->getErrorHandler();
  40. foreach ($hooks[$event] as $i => $hook) {
  41. $object = NULL;
  42. $method = NULL;
  43. $function = NULL;
  44. $data = NULL;
  45. $passData = false;
  46. /* $hook can be: a function, an object, an array of $functiontion and $data,
  47. * an array of just a function, an array of object and method, or an
  48. * array of object, method, and data.
  49. */
  50. //function
  51. if (is_string($hook)) {
  52. $function = $hook;
  53. }
  54. //object
  55. else if (is_object($hook)) {
  56. $object = $hooks[$event][$i];
  57. $method = "on" . $event;
  58. }
  59. //array of...
  60. else if (is_array($hook)) {
  61. $count = count($hook);
  62. if ($count) {
  63. //...object
  64. if (is_object($hook[0])) {
  65. $object = $hooks[$event][$i][0];
  66. if ($count < 2) {
  67. $method = "on" . $event;
  68. } else {
  69. //...object and method
  70. $method = $hook[1];
  71. if (count($hook) > 2) {
  72. //...object, method and data
  73. $passData = true;
  74. $data = $hook[2];
  75. }
  76. }
  77. }
  78. //...function
  79. else if (is_string($hook[0])) {
  80. $function = $hook[0];
  81. if ($count > 1) {
  82. //...function with data
  83. $passData = true;
  84. $data = $hook[1];
  85. }
  86. }
  87. }
  88. }
  89. /* If defined, add data to the arguments array */
  90. if ($passData) {
  91. $args = array_merge(array($data), $args);
  92. }
  93. if (isset($object)) {
  94. $callback = array($object, $method);
  95. }
  96. else if (false !== ($pos = strpos($function, '::'))) {
  97. $callback = array(substr($function, 0, $pos), substr($function, $pos + 2));
  98. }
  99. else {
  100. $callback = $function;
  101. }
  102. if (is_callable($callback)) {
  103. $ret = call_user_func_array($callback, $args);
  104. }
  105. else {
  106. $functionName = CKFinder_Connector_Core_Hooks::_printCallback($callback);
  107. $errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_CUSTOM_ERROR,
  108. "CKFinder failed to call a hook: " . $functionName);
  109. return false;
  110. }
  111. //String return is a custom error
  112. if (is_string($ret)) {
  113. $errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_CUSTOM_ERROR, $ret);
  114. return false;
  115. }
  116. //hook returned an error code, user error codes start from 50000
  117. //error codes are important because this way it is possible to create multilanguage extensions
  118. //TODO: two custom extensions may be popular and return the same error codes
  119. //recomendation: create a function that calculates the error codes starting number
  120. //for an extension, a pool of 100 error codes for each extension should be safe enough
  121. else if (is_int($ret)) {
  122. $errorHandler->throwError($ret);
  123. return false;
  124. }
  125. //no value returned
  126. else if( $ret === null ) {
  127. $functionName = CKFinder_Connector_Core_Hooks::_printCallback($callback);
  128. $errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_CUSTOM_ERROR,
  129. "CKFinder extension returned an invalid value (null)." .
  130. "Hook " . $functionName . " should return a value.");
  131. return false;
  132. }
  133. else if (!$ret) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. /**
  140. * Print user friendly name of a callback
  141. *
  142. * @param mixed $callback
  143. * @return string
  144. */
  145. function _printCallback($callback)
  146. {
  147. if (is_array($callback)) {
  148. if (is_object($callback[0])) {
  149. $className = get_class($callback[0]);
  150. } else {
  151. $className = strval($callback[0]);
  152. }
  153. $functionName = $className . '::' . strval($callback[1]);
  154. }
  155. else {
  156. $functionName = strval($callback);
  157. }
  158. return $functionName;
  159. }
  160. }