Source Code Documentation


s2member\includes\classes\utilities.inc.php


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

s2member\includes\classes\utilities.inc.php

Source Code Documentation