/src/Includes/Utils/Operator.php

https://github.com/Koc/core · PHP · 222 lines · 72 code · 26 blank · 124 comment · 9 complexity · 00e833a01fdd0fa4d97cde03d2efdbb7 MD5 · raw file

  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * @category LiteCommerce
  17. * @package XLite
  18. * @subpackage Includes_Utils
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. * @see ____file_see____
  24. * @since 1.0.0
  25. */
  26. namespace Includes\Utils;
  27. /**
  28. * Operator
  29. *
  30. * @package XLite
  31. * @see ____class_see____
  32. * @since 1.0.0
  33. */
  34. abstract class Operator extends \Includes\Utils\AUtils
  35. {
  36. /**
  37. * Return length of the "dummy" buffer for flush
  38. *
  39. * @return int
  40. * @access protected
  41. * @see ____func_see____
  42. * @since 1.0.0
  43. */
  44. protected static function getDummyBufferLength()
  45. {
  46. return 4096;
  47. }
  48. /**
  49. * Perform the "flush" itself
  50. *
  51. * @return void
  52. * @access protected
  53. * @see ____func_see____
  54. * @since 1.0.0
  55. */
  56. protected static function flushBuffers()
  57. {
  58. @ob_flush();
  59. flush();
  60. }
  61. /**
  62. * Wrap message into some HTML tags (to fast output)
  63. *
  64. * @param string $message Message to prepare
  65. * @param string $jsOutput JS output
  66. *
  67. * @return string
  68. * @see ____func_see____
  69. * @since 1.0.0
  70. */
  71. protected static function getJSMessage($message, $jsOutput)
  72. {
  73. return '<noscript>' . $message . '</noscript>'
  74. . '<script type="text/javascript">' . $jsOutput . '</script>';
  75. }
  76. /**
  77. * Redirect
  78. *
  79. * @param string $location URL
  80. * @param int $code operation code
  81. *
  82. * @return void
  83. * @access public
  84. * @see ____func_see____
  85. * @since 1.0.0
  86. */
  87. public static function redirect($location, $code = 302)
  88. {
  89. $location = \Includes\Utils\Converter::removeCRLF($location);
  90. if ('cli' !== PHP_SAPI) {
  91. if (headers_sent()) {
  92. $message = '<a href="' . $location . '">Click here to redirect</a>';
  93. $jsOutput = 'self.location = \'' . $location . '\';';
  94. static::flush($message, true, $jsOutput);
  95. } else {
  96. header('Location: ' . $location, true, $code);
  97. }
  98. }
  99. exit (0);
  100. }
  101. /**
  102. * Refresh current page
  103. *
  104. * @return void
  105. * @access public
  106. * @see ____func_see____
  107. * @since 1.0.0
  108. */
  109. public static function refresh()
  110. {
  111. static::redirect(\Includes\Utils\URLManager::getSelfURI());
  112. }
  113. /**
  114. * Echo message and flush output
  115. *
  116. * @param string $message Text to display
  117. * @param boolean $dummyFlush Output extra spaces or not OPTIONAL
  118. * @param string $jsOutput Flag to quick output OPTIONAL
  119. *
  120. * @return void
  121. * @see ____func_see____
  122. * @since 1.0.0
  123. */
  124. public static function flush($message, $dummyFlush = false, $jsOutput = null)
  125. {
  126. if ('cli' !== PHP_SAPI) {
  127. // Send extra whitespace before flushing
  128. if ($dummyFlush) {
  129. echo (str_repeat(' ', static::getDummyBufferLength()));
  130. }
  131. // Wrap message into the "<script>" tag
  132. if (isset($jsOutput)) {
  133. $message = static::getJSMessage($message, $jsOutput);
  134. }
  135. }
  136. // Print message
  137. echo ($message);
  138. static::flushBuffers();
  139. }
  140. /**
  141. * Wrapper to message quick display
  142. *
  143. * @param string $message Message text
  144. * @param boolean $addNewline Flag OPTIONAL
  145. *
  146. * @return void
  147. * @see ____func_see____
  148. * @since 1.0.0
  149. */
  150. public static function showMessage($message, $addNewline = true)
  151. {
  152. static::flush($message, true, 'document.write(\'' . $message . '\');');
  153. if ($addNewline) {
  154. static::flush(LC_EOL);
  155. }
  156. }
  157. /**
  158. * Set custom value for the "max_execution_time" INI setting, and execute some function
  159. *
  160. * @param int $time time (in seconds) to set
  161. * @param mixed $callback function to execute
  162. * @param array $args call arguments
  163. *
  164. * @return mixed
  165. * @access public
  166. * @see ____func_see____
  167. * @since 1.0.0
  168. */
  169. public static function executeWithCustomMaxExecTime($time, $callback, array $args = array())
  170. {
  171. $savedValue = @ini_get('max_execution_time');
  172. @set_time_limit($time);
  173. $result = call_user_func_array($callback, $args);
  174. if (!empty($savedValue)) {
  175. @set_time_limit($savedValue);
  176. }
  177. return $result;
  178. }
  179. /**
  180. * Check if class is already declared.
  181. *
  182. * :NOTE: this function does not use autoloader
  183. *
  184. * @param string $name Class name
  185. *
  186. * @return boolean
  187. * @access public
  188. * @see ____func_see____
  189. * @since 1.0.0
  190. */
  191. public static function checkIfClassExists($name)
  192. {
  193. $file = \Includes\Autoloader::getLCAutoloadDir() . \Includes\Utils\Converter::getClassFile($name);
  194. return class_exists($name, false) || \Includes\Utils\FileManager::isFileReadable($file);
  195. }
  196. }