PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/s2member/includes/classes/utilities.inc.php

https://gitlab.com/Gashler/dp
PHP | 208 lines | 90 code | 15 blank | 103 comment | 16 complexity | ca99f3d5f82048daa1330f07e0b03a42 MD5 | raw file
  1. <?php
  2. /**
  3. * General utilities.
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * (coded in the USA)
  8. *
  9. * Released under the terms of the GNU General Public License.
  10. * You should have received a copy of the GNU General Public License,
  11. * along with this software. In the main directory, see: /licensing/
  12. * If not, see: {@link http://www.gnu.org/licenses/}.
  13. *
  14. * @package s2Member\Utilities
  15. * @since 3.5
  16. */
  17. if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
  18. exit ("Do not access this file directly.");
  19. if (!class_exists ("c_ws_plugin__s2member_utilities"))
  20. {
  21. /**
  22. * General utilities.
  23. *
  24. * @package s2Member\Utilities
  25. * @since 3.5
  26. */
  27. class c_ws_plugin__s2member_utilities
  28. {
  29. /**
  30. * Evaluates PHP code, and "returns" output.
  31. *
  32. * @package s2Member\Utilities
  33. * @since 3.5
  34. *
  35. * @param str $code A string of data, possibly with embedded PHP code.
  36. * @return str Output after PHP evaluation.
  37. */
  38. public static function evl ($code = FALSE, $vars = array())
  39. {
  40. if(is_array($vars) && !empty($vars))
  41. extract($vars, EXTR_PREFIX_SAME, '_extract_');
  42. ob_start (); // Output buffer.
  43. eval ("?>" . trim ($code));
  44. return ob_get_clean ();
  45. }
  46. /**
  47. * Buffers (gets) function output.
  48. *
  49. * A variable length of additional arguments are possible.
  50. * Additional parameters get passed into the ``$function``.
  51. *
  52. * @package s2Member\Utilities
  53. * @since 3.5
  54. *
  55. * @param str $function Name of a function to call upon.
  56. * @return str Output after call to function.
  57. * Any output is buffered and returned.
  58. */
  59. public static function get ($function = FALSE)
  60. {
  61. $args = func_get_args ();
  62. $function = array_shift ($args);
  63. if (is_string ($function) && $function)
  64. {
  65. ob_start ();
  66. if (is_array ($args) && !empty ($args))
  67. {
  68. $return = call_user_func_array ($function, $args);
  69. }
  70. else // There are no additional arguments to pass.
  71. {
  72. $return = call_user_func ($function);
  73. }
  74. $echo = ob_get_clean ();
  75. return (!strlen ($echo) && strlen ($return)) ? $return : $echo;
  76. }
  77. else // Else return null.
  78. return;
  79. }
  80. /**
  81. * Builds a version checksum for this installation.
  82. *
  83. * @package s2Member\Utilities
  84. * @since 3.5
  85. *
  86. * @return str String with `[version]-[pro version]-[consolidated checksum]`.
  87. */
  88. public static function ver_checksum ()
  89. {
  90. $checksum = WS_PLUGIN__S2MEMBER_VERSION; // Software version string.
  91. $checksum .= (c_ws_plugin__s2member_utils_conds::pro_is_installed ()) ? "-" . WS_PLUGIN__S2MEMBER_PRO_VERSION : ""; // Pro version string?
  92. $checksum .= "-" . abs (crc32 ($GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["checksum"] . $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["options_checksum"] . $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["options_version"]));
  93. return $checksum; // (i.e. version-pro version-checksum)
  94. }
  95. /**
  96. * String with current time details.
  97. *
  98. * @package s2Member\Utilities
  99. * @since 130210
  100. *
  101. * @return str String with time representation (in UTC time).
  102. */
  103. public static function time_details ()
  104. {
  105. $time = time(); // The time at this very moment.
  106. $details = date ("D M jS, Y", $time)." @ precisely " . date ("g:i a e", $time);
  107. return $details; // Return all details.
  108. }
  109. /**
  110. * String with all version details *(for PHP, WordPress®, s2Member, and Pro)*.
  111. *
  112. * @package s2Member\Utilities
  113. * @since 3.5
  114. *
  115. * @return str String with `PHP vX.XX :: WordPress® vX.XX :: s2Member® vX.XX :: s2Member® Pro vX.XX`.
  116. */
  117. public static function ver_details ()
  118. {
  119. $details = "PHP v" . PHP_VERSION . " :: WordPress® v" . get_bloginfo ("version") . " :: s2Member® v" . WS_PLUGIN__S2MEMBER_VERSION;
  120. $details .= (c_ws_plugin__s2member_utils_conds::pro_is_installed ()) ? " :: s2Member® Pro v" . WS_PLUGIN__S2MEMBER_PRO_VERSION : "";
  121. return $details; // Return all details.
  122. }
  123. /**
  124. * Generates s2Member Security Badge.
  125. *
  126. * @package s2Member\Utilities
  127. * @since 3.5
  128. *
  129. * @param str $v A variation number to display. Defaults to `1`.
  130. * @param bool $no_cache Defaults to false. If true, the HTML markup will contain query string params that prevent caching.
  131. * @param bool $display_on_failure. Defaults to false. True if we need to display the "NOT yet verified" version inside admin panels.
  132. * @return str HTML markup for display of s2Member Security Badge.
  133. */
  134. public static function s_badge_gen ($v = "1", $no_cache = FALSE, $display_on_failure = FALSE)
  135. {
  136. if ($v && file_exists (($template = dirname (dirname (__FILE__)) . "/templates/badges/s-badge.php")))
  137. {
  138. $badge = trim (c_ws_plugin__s2member_utilities::evl (file_get_contents ($template)));
  139. $badge = preg_replace ("/%%site_url%%/i", urlencode (site_url ()), preg_replace ("/%%v%%/i", (string)$v, $badge));
  140. $badge = preg_replace ("/%%no_cache%%/i", (($no_cache) ? "&amp;no_cache=" . urlencode (mt_rand (0, PHP_INT_MAX)) : ""), $badge);
  141. $badge = preg_replace ("/%%display_on_failure%%/i", (($display_on_failure) ? "&amp;display_on_failure=1" : ""), $badge);
  142. }
  143. return (!empty ($badge)) ? $badge : ""; // Return Security Badge.
  144. }
  145. /**
  146. * Acquires information about memory usage.
  147. *
  148. * @package s2Member\Utilities
  149. * @since 110815
  150. *
  151. * @return str String with `Memory x MB :: Real Memory x MB :: Peak Memory x MB :: Real Peak Memory x MB`.
  152. */
  153. public static function mem_details ()
  154. {
  155. $memory = number_format (memory_get_usage () / 1048576, 2, ".", "");
  156. $real_memory = number_format (memory_get_usage (true) / 1048576, 2, ".", "");
  157. $peak_memory = number_format (memory_get_peak_usage () / 1048576, 2, ".", "");
  158. $real_peak_memory = number_format (memory_get_peak_usage (true) / 1048576, 2, ".", "");
  159. $details = "Memory " . $memory . " MB :: Real Memory " . $real_memory . " MB :: Peak Memory " . $peak_memory . " MB :: Real Peak Memory " . $real_peak_memory . " MB";
  160. return $details; // Return all details.
  161. }
  162. /**
  163. * Acquires s2Member options for the Main Site of a Multisite Network.
  164. *
  165. * @package s2Member\Utilities
  166. * @since 110912
  167. *
  168. * @return array Array of s2Member options for the Main Site.
  169. */
  170. public static function mms_options ()
  171. {
  172. return (is_multisite ()) ? (array)get_site_option ("ws_plugin__s2member_options") : array ();
  173. }
  174. /**
  175. * Builds an array of backtrace callers.
  176. *
  177. * @package s2Member\Utilities
  178. * @since 110912
  179. *
  180. * @param array $debug_backtrace Optional. Defaults to ``debug_backtrace()``.
  181. * @return array Array of backtrace callers (lowercase).
  182. */
  183. public static function callers ($debug_backtrace = FALSE)
  184. {
  185. $callers = array (); // Initialize array.
  186. foreach (($debug_backtrace = (is_array ($debug_backtrace)) ? $debug_backtrace : debug_backtrace ()) as $caller)
  187. if (isset ($caller["class"], $caller["function"]) || (!isset ($caller["class"]) && isset ($caller["function"])))
  188. $callers[] = (isset ($caller["class"])) ? $caller["class"] . "::" . $caller["function"] : $caller["function"];
  189. return array_map ("strtolower", array_unique ($callers));
  190. }
  191. }
  192. }
  193. ?>