| 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 |
* Archives logs to prevent HUGE files from building up over time. |
| 32 |
* |
| 33 |
* This routine is staggered to conserve resources. |
| 34 |
* This is called by all logging routines for s2Member. |
| 35 |
* |
| 36 |
* @package s2Member\Utilities |
| 37 |
* @since 3.5 |
| 38 |
* |
| 39 |
* @param bool $stagger Optional. Defaults to true. If false, the routine will run, regardless. |
| 40 |
* @return bool Always returns true. |
| 41 |
*/ |
| 42 |
public static function archive_oversize_log_files ($stagger = TRUE) |
| 43 |
{ |
| 44 |
if (!$stagger || is_float ($stagger = time () / 2)) /* Stagger this routine? */ |
| 45 |
{ |
| 46 |
if (is_dir ($dir = $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["logs_dir"]) && is_writable ($dir)) |
| 47 |
{ |
| 48 |
$max = apply_filters ("ws_plugin__s2member_oversize_log_file_bytes", 2097152, get_defined_vars ()); |
| 49 |
/**/ |
| 50 |
eval ('$log_files = scandir ($dir); shuffle($log_files); $counter = 1;'); |
| 51 |
/**/ |
| 52 |
foreach ($log_files as $file) /* Go through each log file. Up to 25 files at a time. */ |
| 53 |
{ |
| 54 |
if (preg_match ("/\.log$/i", $file) && !preg_match ("/-ARCHIVED-/i", $file) && is_file ($dir_file = $dir . "/" . $file)) |
| 55 |
{ |
| 56 |
if (filesize ($dir_file) > $max && is_writable ($dir_file)) /* The file must be writable. */ |
| 57 |
if ($log = preg_replace ("/\.log$/i", "", $dir_file)) /* Strip .log before renaming. */ |
| 58 |
rename ($dir_file, $log . "-ARCHIVED-" . date ("m-d-Y") . "-" . time () . ".log"); |
| 59 |
} |
| 60 |
/**/ |
| 61 |
if (($counter = $counter + 1) > 25) /* Up to 25 files at a time. */ |
| 62 |
break; /* Stop for now. */ |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
/**/ |
| 67 |
return true; |
| 68 |
} |
| 69 |
/** |
| 70 |
* Removes expired Transients inserted into the database by s2Member. |
| 71 |
* |
| 72 |
* This routine is staggered to conserve resources. |
| 73 |
* Only 5 Transients are deleted each time. |
| 74 |
* |
| 75 |
* This is called by s2Member's Auto-EOT System, every 10 minutes. |
| 76 |
* |
| 77 |
* @package s2Member\Utilities |
| 78 |
* @since 3.5 |
| 79 |
* |
| 80 |
* @param bool $stagger Optional. Defaults to true. If false, the routine will run, regardless. |
| 81 |
* @return bool Always returns true. |
| 82 |
*/ |
| 83 |
public static function cleanup_expired_s2m_transients ($stagger = TRUE) |
| 84 |
{ |
| 85 |
global $wpdb; /* Will need this for database cleaning. */ |
| 86 |
/**/ |
| 87 |
if (!$stagger || is_float ($stagger = time () / 2)) /* Stagger this routine? */ |
| 88 |
{ |
| 89 |
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)) |
| 90 |
{ |
| 91 |
foreach ($expired_s2m_transients as $expired_s2m_transient) /* Delete the _timeout, and also the Transient entry name itself. */ |
| 92 |
if (($id = $expired_s2m_transient->option_id) && ($name = preg_replace ("/_transient_timeout_/i", "_transient_", $expired_s2m_transient->option_name, 1))) |
| 93 |
$wpdb->query ("DELETE FROM `" . $wpdb->options . "` WHERE `option_id` = '" . esc_sql ($id) . "' OR `option_name` = '" . esc_sql ($name) . "'"); |
| 94 |
} |
| 95 |
} |
| 96 |
/**/ |
| 97 |
return true; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
?> |