Source Code Documentation


s2member\includes\classes\utils-logs.inc.php


  1. <?php
  2. /**
  3. * Log 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_utils_logs"))
  21. {
  22. /**
  23. * Log utilities.
  24. *
  25. * @package s2Member\Utilities
  26. * @since 3.5
  27. */
  28. class c_ws_plugin__s2member_utils_logs
  29. {
  30. /**
  31. * Array of log file descriptions.
  32. *
  33. * @package s2Member\Utilities
  34. * @since 120214
  35. *
  36. * @var array Array of log file descriptions.
  37. */
  38. public static $log_file_descriptions = array // Array keys are regex patterns matching their associated log file names.
  39. (
  40. '/paypal-api/' => array('short' => 'PayPal® API communication.', 'long' => 'This log file records all communication between s2Member® and PayPal® APIs. Such as PayPal® Button Encryption and PayPal® Pro API calls that process transactions. See also: paypal-ipn.log (s2Member\'s core processor).'),
  41. '/paypal-payflow-api/' => array('short' => 'PayPal® (PayFlow Edition) API communication.', 'long' => 'This log file records all communication between s2Member® and the PayPal® (PayFlow Edition) APIs. Only applicable if you operate a PayPal® Payments Pro (PayFlow Edition) account. See also: paypal-ipn.log (s2Member\'s core processor).'),
  42. '/paypal-ipn/' => array('short' => 'Core PayPal® IPN and post-processing handler.', 'long' => 'This log file records all communication between s2Member® and the PayPal® IPN service. Also logs all post-processing routines from other Payment Gateway integrations, where s2Member® translates its communication with other Payment Gateways into a format it\'s core PayPal® processing routines can understand. All transactions pass through s2Member\'s core PayPal® processor and they will be logged in this file. Including transactions processed via s2Member® Pro Forms; for all Payment Gateway integrations.'),
  43. '/paypal-rtn/' => array('short' => 'Core PayPal® PDT/Auto-Return communication.', 'long' => 'This log file records all communication between s2Member® and the PayPal® PDT Auto-Return system (i.e. routines that help s2Member® process Thank-You pages). Also logs all Auto-Return routines from other Payment Gateway integrations (those implemented via Payment Buttons), where s2Member® translates its communication with other Payment Gateways into a format it\'s core PayPal® processing routines can understand. Not used in s2Member® Pro Form integrations however.'),
  44.  
  45. '/authnet-api/' => array('short' => 'Authorize.Net API communication.', 'long' => 'This log file records all communication between s2Member® and Authorize.Net APIs (for both AIM and ARB integrations).'),
  46. '/authnet-arb/' => array('short' => 'Authorize.Net ARB Subscription status checks.', 'long' => 'This log file records s2Member\'s Authorize.Net ARB Subscription status checks. s2Member® polls the ARB service periodically to check the status of existing Members (e.g. to see if billing is still active or not).'),
  47. '/authnet-ipn/' => array('short' => 'Authorize.Net Silent Post/IPN communication.', 'long' => 'This log file records the Silent Post/IPN data Authorize.Net sends to s2Member® with details regarding new transactions.'),
  48.  
  49. '/alipay-ipn/' => array('short' => 'AliPay® IPN communication.', 'long' => 'This log file records the IPN data AliPay® sends to s2Member® with details regarding new transactions. See also: paypal-ipn.log (s2Member\'s core processor).'),
  50. '/alipay-rtn/' => array('short' => 'AliPay® Auto-Return communication.', 'long' => 'This log file records the Auto-Return data AliPay® sends to s2Member® with details regarding new transactions (i.e. logs routines that help s2Member® process Thank-You pages). See also: paypal-rtn.log (s2Member\'s core processor).'),
  51.  
  52. '/clickbank-ipn/' => array('short' => 'ClickBank® IPN communication.', 'long' => 'This log file records the IPN data ClickBank® sends to s2Member® with details regarding new transactions, cancellations, expirations, etc. See also: paypal-ipn.log (s2Member\'s core processor).'),
  53. '/clickbank-rtn/' => array('short' => 'ClickBank® Auto-Return communication.', 'long' => 'This log file records the Auto-Return data ClickBank® sends to s2Member® with details regarding new transactions (i.e. logs routines that help s2Member® process Thank-You pages). See also: paypal-rtn.log (s2Member\'s core processor).'),
  54.  
  55. '/google-ipn/' => array('short' => 'Google® Callback/IPN communication.', 'long' => 'This log file records the Callback/IPN data Google® sends to s2Member® with details regarding new transactions, cancellations, expirations, etc. See also: paypal-ipn.log (s2Member\'s core processor).'),
  56.  
  57. '/ccbill-ipn/' => array('short' => 'ccBill® Bg Post/IPN communication.', 'long' => 'This log file records the Bg Post/IPN data ccBill® sends to s2Member® with details regarding new transactions. See also: paypal-ipn.log (s2Member\'s core processor).'),
  58. '/ccbill-rtn/' => array('short' => 'ccBill® Auto-Return communication.', 'long' => 'This log file records the Auto-Return data ccBill® sends to s2Member® with details regarding new transactions (i.e. logs routines that help s2Member® process Thank-You pages). See also: paypal-rtn.log (s2Member\'s core processor).'),
  59. '/ccbill-dl-ipn/' => array('short' => 'ccBill® Datalink Subscription status checks.', 'long' => 'This log file records s2Member\'s ccBill® Datalink Subscription status checks that may result in actions taken by s2Member®. s2Member® polls the ccBill® Datalink service periodically to check the status of existing Members (e.g. to see if billing is still active or not).'),
  60. '/ccbill-dl/' => array('short' => 'ccBill® Datalink collections.', 'long' => 'This log file records s2Member\'s ccBill® Datalink connections. s2Member® polls the ccBill® Datalink service periodically to obtain information about existing Users/Members.'),
  61.  
  62. '/mailchimp-api/' => array('short' => 'MailChimp® API communication.', 'long' => 'This log file records all of s2Member\'s communication with MailChimp® APIs.'),
  63. '/aweber-api/' => array('short' => 'AWeber® API communication.', 'long' => 'This log file records all of s2Member\'s communication with AWeber® APIs.'),
  64.  
  65. '/reg-handler/' => array('short' => 'User registrations processed by s2Member®.', 'long' => 'This log file records all User/Member registrations processed by s2Member® (either directly or indirectly). This includes both free and paid registrations. It also logs registrations that occur as a result of new Users/Members being created from the Dashboard by a site owner. It also includes registrations that occur through the s2Member® Pro Remote Operations API.'),
  66.  
  67. '/s2-http-api-debug/' => array('short' => 'All outgoing HTTP connections related to s2Member®.', 'long' => 'This log file records all outgoing WP_Http connections that are specifically related to s2Member®. This log file can be extremely helpful. It includes technical details about remote HTTP connections that are not available in other log files.'),
  68. '/wp-http-api-debug/' => array('short' => 'All outgoing WordPress® HTTP connections.', 'long' => 'This log file records all outgoing HTTP connections processed by the WP_Http class. This includes everything processed by WordPress®; even things unrelated to s2Member®. This log file can be extremely helpful. It includes technical details about remote HTTP connections that are not available in other log files.'),
  69. );
  70.  
  71. /**
  72. * Logs HTTP communication (if enabled).
  73. *
  74. * @package s2Member\Utilities
  75. * @since 120212
  76. *
  77. * @return null Nothing.
  78. */
  79. public static function http_api_debug ($response = NULL, $state = NULL, $class = NULL, $args = NULL, $url = NULL)
  80. {
  81. if (!$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs"])
  82. return; // Logging is NOT enabled in this case.
  83.  
  84. $is_s2member = (!empty($args["s2member"]) || strpos($url, "s2member") !== false) ? true : false;
  85.  
  86. if (!$GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs_extensive"] && !$is_s2member)
  87. return; // Extensive logging is NOT enabled in this case.
  88.  
  89. global /* For Multisite support. */ $current_site, $current_blog;
  90.  
  91. $http_api_debug = array(
  92. "state" => $state,
  93. "transport_class" => $class,
  94. "args" => $args,
  95. "url" => $url,
  96. "response" => $response
  97. );
  98.  
  99. $logt = c_ws_plugin__s2member_utilities::time_details();
  100. $logv = c_ws_plugin__s2member_utilities::ver_details();
  101. $logm = c_ws_plugin__s2member_utilities::mem_details();
  102.  
  103. $log4 = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . "\nUser-Agent: " . $_SERVER["HTTP_USER_AGENT"];
  104. $log4 = (is_multisite () && !is_main_site ()) ? ($_log4 = $current_blog->domain . $current_blog->path) . "\n" . $log4 : $log4;
  105. $log2 = (is_multisite () && !is_main_site ()) ? "http-api-debug-4-" . trim (preg_replace ("/[^a-z0-9]/i", "-", $_log4), "-") . ".log" : "http-api-debug.log";
  106.  
  107. $http_api_debug_conceal_private_info = c_ws_plugin__s2member_utils_logs::conceal_private_info(var_export ($http_api_debug, true));
  108.  
  109. if (is_dir ($logs_dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]))
  110. if (is_writable ($logs_dir) && c_ws_plugin__s2member_utils_logs::archive_oversize_log_files ())
  111. {
  112. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["gateway_debug_logs_extensive"])
  113. file_put_contents ($logs_dir . "/wp-" . $log2,
  114. "LOG ENTRY: ".$logt . "\n" . $logv . "\n" . $logm . "\n" . $log4 . "\n" .
  115. $http_api_debug_conceal_private_info . "\n\n",
  116. FILE_APPEND);
  117.  
  118. if($is_s2member) // Log s2Member® HTTP connections separately.
  119. file_put_contents ($logs_dir . "/s2-" . $log2,
  120. "LOG ENTRY: ".$logt . "\n" . $logv . "\n" . $logm . "\n" . $log4 . "\n" .
  121. $http_api_debug_conceal_private_info . "\n\n",
  122. FILE_APPEND);
  123. }
  124. }
  125. /**
  126. * Archives logs to prevent HUGE files from building up over time.
  127. *
  128. * This routine is staggered to conserve resources.
  129. * This is called by all logging routines for s2Member.
  130. *
  131. * @package s2Member\Utilities
  132. * @since 3.5
  133. *
  134. * @param bool $stagger Optional. Defaults to true. If false, the routine will run, regardless.
  135. * @return bool Always returns true.
  136. */
  137. public static function archive_oversize_log_files ($stagger = TRUE)
  138. {
  139. if (!$stagger || is_float ($stagger = time () / 2)) // Stagger this routine?
  140. {
  141. if (is_dir ($dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]) && is_writable ($dir))
  142. {
  143. $max = apply_filters ("ws_plugin__s2member_oversize_log_file_bytes", 2097152, get_defined_vars ());
  144.  
  145. $log_files = scandir ($dir); shuffle($log_files); $counter = 1;
  146.  
  147. foreach ($log_files as $file) // Go through each log file. Up to 25 files at a time.
  148. {
  149. if (preg_match ("/\.log$/i", $file) && !preg_match ("/-ARCHIVED-/i", $file) && is_file ($dir_file = $dir . "/" . $file))
  150. {
  151. if (filesize ($dir_file) > $max && is_writable ($dir_file)) // The file must be writable.
  152. if ($log = preg_replace ("/\.log$/i", "", $dir_file)) // Strip .log before renaming.
  153. rename ($dir_file, $log . "-ARCHIVED-" . date ("m-d-Y") . "-" . time () . ".log");
  154. }
  155.  
  156. if (($counter = $counter + 1) > 25) // Up to 25 files at a time.
  157. break; // Stop for now.
  158. }
  159. }
  160. }
  161. return true;
  162. }
  163. /**
  164. * Removes expired Transients inserted into the database by s2Member.
  165. *
  166. * This routine is staggered to conserve resources.
  167. * Only 5 Transients are deleted each time.
  168. *
  169. * This is called by s2Member's Auto-EOT System, every 10 minutes.
  170. *
  171. * @package s2Member\Utilities
  172. * @since 3.5
  173. *
  174. * @param bool $stagger Optional. Defaults to true. If false, the routine will run, regardless.
  175. * @return bool Always returns true.
  176. */
  177. public static function cleanup_expired_s2m_transients ($stagger = TRUE)
  178. {
  179. global $wpdb; // Will need this for database cleaning.
  180.  
  181. if (!$stagger || is_float ($stagger = time () / 2)) // Stagger this routine?
  182. {
  183. if (is_array ($expired_s2m_transients = $wpdb->get_results ("SELECT * FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '" . esc_sql (like_escape ("_transient_timeout_s2m_")) . "%' AND `option_value` < '" . esc_sql (time ()) . "' LIMIT 5")) && !empty ($expired_s2m_transients))
  184. {
  185. foreach ($expired_s2m_transients as $expired_s2m_transient) // Delete the _timeout, and also the Transient entry name itself.
  186. if (($id = $expired_s2m_transient->option_id) && ($name = preg_replace ("/_transient_timeout_/i", "_transient_", $expired_s2m_transient->option_name, 1)))
  187. $wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_id` = '" . esc_sql ($id) . "' OR `option_name` = '" . esc_sql ($name) . "'");
  188. }
  189. }
  190. return true;
  191. }
  192.  
  193. /**
  194. * Attempts to conceal private details in log entries.
  195. *
  196. * @package s2Member\Utilities
  197. * @since 130315
  198. *
  199. * @param string $log_entry The log entry we need to conceal private details in.
  200. * @return string Filtered string with some data X'd out :-)
  201. */
  202. public static function conceal_private_info($log_entry)
  203. {
  204. $log_entry = preg_replace('/\b([3456][0-9]{10,11})([0-9]{4})\b/', 'xxxxxxxxxxxx'.'$2', (string)$log_entry);
  205.  
  206. $log_entry = preg_replace('/(\'.*pass_?(?:word)?(?:[0-9]+)?\'\s*\=\>\s*\')([^\']+)(\')/', '$1'.'xxxxxxxx/pass'.'$3', $log_entry);
  207. $log_entry = preg_replace('/([&?][^&]*pass_?(?:word)?(?:[0-9]+)?\=)([^&]+)/', '$1'.'xxxxxxxx/pass', $log_entry);
  208.  
  209. $log_entry = preg_replace('/(\'api_?(?:key|secret)\'\s*\=\>\s*\')([^\']+)(\')/', '$1'.'xxxxxxxx/api/key/sec'.'$3', $log_entry);
  210. $log_entry = preg_replace('/([&?][^&]api_?(?:key|secret)\=)([^&]+)/', '$1'.'xxxxxxxx/api/key/sec', $log_entry);
  211.  
  212. $log_entry = preg_replace('/(\'(?:PWD|SIGNATURE)\'\s*\=\>\s*\')([^\']+)(\')/', '$1'.'xxxxxxxx/PWD/SIG'.'$3', $log_entry);
  213. $log_entry = preg_replace('/([&?][^&](?:PWD|SIGNATURE)\=)([^&]+)/', '$1'.'xxxxxxxx/PWD/SIG', $log_entry);
  214.  
  215. $log_entry = preg_replace('/(\'(?:x_login|x_tran_key)\'\s*\=\>\s*\')([^\']+)(\')/', '$1'.'xxxxxxxx/key/tran'.'$3', $log_entry);
  216. $log_entry = preg_replace('/([&?][^&](?:x_login|x_tran_key)\=)([^&]+)/', '$1'.'xxxxxxxx/key/tran', $log_entry);
  217.  
  218. return $log_entry;
  219. }
  220. }
  221. }
  222. ?>

s2member\includes\classes\utils-logs.inc.php

Source Code Documentation