PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 260805
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v260805
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / src / includes / classes / utils-s2o.inc.php

utils-s2o.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 260805, at src/includes/classes/utils-s2o.inc.php

145 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreFile
3 /**
4 * s2Member-only utilities.
5 *
6 * Copyright: © 2009-2011
7 * {@link http://websharks-inc.com/ WebSharks, Inc.}
8 * (coded in the USA)
9 *
10 * Released under the terms of the GNU General Public License.
11 * You should have received a copy of the GNU General Public License,
12 * along with this software. In the main directory, see: /licensing/
13 * If not, see: {@link http://www.gnu.org/licenses/}.
14 *
15 * @package s2Member\Utilities
16 * @since 110912
17 */
18 if(!class_exists('c_ws_plugin__s2member_utils_s2o'))
19 {
20 /**
21 * s2Member-only utilities.
22 *
23 * @package s2Member\Utilities
24 * @since 110912
25 */
26 class c_ws_plugin__s2member_utils_s2o
27 {
28 /**
29 * WordPress directory.
30 *
31 * @package s2Member\Utilities
32 * @since 110912
33 *
34 * @param string $starting_dir A directory to start searching from.
35 * @param string $alt_starting_dir An alternate directory to search from.
36 *
37 * @return string|null WordPress directory, else exits script execution on failure.
38 */
39 public static function wp_dir($starting_dir = '', $alt_starting_dir = '')
40 {
41 if(!empty($_SERVER['WP_DIR']))
42 return (string)$_SERVER['WP_DIR'];
43
44 foreach(array($starting_dir, $alt_starting_dir, $_SERVER['DOCUMENT_ROOT']) as $_directory)
45 if($_directory && is_string($_directory) && is_dir($_directory))
46 for($_i = 0, $_dir = $_directory; $_i <= 20; $_i++, $_dir = dirname($_dir))
47 if(file_exists($_dir.'/wp-settings.php'))
48 return ($wp_dir = $_dir);
49
50 header('HTTP/1.0 500 Error');
51 header('Content-Type: text/plain; charset=UTF-8');
52
53 while(@ob_end_clean()) ; // Clean any existing output buffers.
54 exit ('ERROR: s2Member unable to locate WordPress directory.');
55 }
56
57 /**
58 * WordPress settings, after ``SHORTINIT`` section.
59 *
60 * @package s2Member\Utilities
61 * @since 110912
62 *
63 * @param string $wp_dir WordPress directory path.
64 * @param string $o_file Location of calling `*-o.php` file.
65 *
66 * @return string|bool WordPress settings, else false on failure.
67 */
68 public static function wp_settings_as($wp_dir = '', $o_file = '')
69 {
70 if($wp_dir && is_dir($wp_dir) && is_readable($wp_settings = $wp_dir.'/wp-settings.php') && $o_file && is_file($o_file) && ($_wp_settings = trim(file_get_contents($wp_settings))))
71 {
72 $wp_shortinit_section = '/if\s*\(\s*SHORTINIT\s*\)['."\r\n\t".'\s]*\{?['."\r\n\t".'\s]*return\s+false;['."\r\n\t".'\s]*\}?['."\r\n\t".'\s]*/'; // Run ``preg_match()`` to confirm existence.
73 if(preg_match($wp_shortinit_section, $_wp_settings) && ($_wp_settings_parts = preg_split($wp_shortinit_section, $_wp_settings, 2)) && ($_wp_settings = trim($_wp_settings_parts[1])) && ($_wp_settings = '<?php'."\n".$_wp_settings))
74 {
75 if(($_wp_settings = str_replace('__FILE__', "'".str_replace("'", "'", $wp_settings)."'", $_wp_settings))) // Eval compatible. Hard-code the ``__FILE__`` location here.
76 {
77 $mu_plugins_section = '/['."\r\n\t".'\s]+foreach\s*\(\s*wp_get_mu_plugins\s*\(\s*\)\s*as\s*\$mu_plugin\s*\)['."\r\n\t".'\s]*\{?['."\r\n\t".'\s]*include_once\s*\(\s*\$mu_plugin\s*\)\s*;['."\r\n\t".'\s]*\}?['."\r\n\t".'\s]*unset\s*\(\s*\$mu_plugin\s*\)\s*;/';
78 $mu_plugins_replace = "\n\n".c_ws_plugin__s2member_utils_s2o::esc_ds(trim(c_ws_plugin__s2member_utils_s2o::evl(file_get_contents(dirname(dirname(__FILE__)).'/templates/cfg-files/s2o-mu-plugins.php'))))."\n";
79 if(($_wp_settings = preg_replace($mu_plugins_section, $mu_plugins_replace, $_wp_settings, 1, $mu_plugins_replaced)) && $mu_plugins_replaced)
80 {
81 $nw_plugins_section = '/['."\r\n\t".'\s]+foreach\s*\(\s*wp_get_active_network_plugins\s*\(\s*\)\s*as\s*\$network_plugin\s*\)['."\r\n\t".'\s]*\{?['."\r\n\t".'\s]*wp_register_plugin_realpath\s*\(\s*\$network_plugin\s*\)\s*;['."\r\n\t".'\s]*include_once\s*\(\s*\$network_plugin\s*\)\s*;['."\r\n\t".'\s]*\}?['."\r\n\t".'\s]*unset\s*\(\s*\$network_plugin\s*\)\s*;/';
82 $nw_plugins_replace = "\n\n".c_ws_plugin__s2member_utils_s2o::esc_ds(trim(c_ws_plugin__s2member_utils_s2o::evl(file_get_contents(dirname(dirname(__FILE__)).'/templates/cfg-files/s2o-nw-plugins.php'))))."\n";
83 if(($_wp_settings = preg_replace($nw_plugins_section, $nw_plugins_replace, $_wp_settings, 1, $nw_plugins_replaced)) && $nw_plugins_replaced)
84 {
85 $st_plugins_section = '/['."\r\n\t".'\s]+foreach\s*\(\s*wp_get_active_and_valid_plugins\s*\(\s*\)\s*as\s*\$plugin\s*\)['."\r\n\t".'\s]*\{?['."\r\n\t".'\s]*wp_register_plugin_realpath\s*\(\s*\$plugin\s*\)\s*;['."\r\n\t".'\s]*include_once\s*\(\s*\$plugin\s*\)\s*;['."\r\n\t".'\s]*\}?['."\r\n\t".'\s ]*unset\s*\(\s*\$plugin\s*\)\s*;/';
86 $st_plugins_replace = "\n\n".c_ws_plugin__s2member_utils_s2o::esc_ds(trim(c_ws_plugin__s2member_utils_s2o::evl(file_get_contents(dirname(dirname(__FILE__)).'/templates/cfg-files/s2o-st-plugins.php'))))."\n";
87 if(($_wp_settings = preg_replace($st_plugins_section, $st_plugins_replace, $_wp_settings, 1, $st_plugins_replaced)) && $st_plugins_replaced)
88 {
89 $th_funcs_section = '/['."\r\n\t".'\s]+if\s*\(\s*\!\s*defined\s*\(\s*[\'"]WP_INSTALLING[\'"]\s*\)\s*\|\|\s*[\'"]wp\-activate\.php[\'"]\s*\=\=\=\s*\$pagenow\s*\)['."\r\n\t".'\s]*\{['."\r\n\t".'\s]*if\s*\(\s*TEMPLATEPATH\s*\!\=\=\s*STYLESHEETPATH\s*&&\s*file_exists\s*\(\s*STYLESHEETPATH\s*\.\s*[\'"]\/functions\.php[\'"]\s*\)\s*\)['."\r\n\t".'\s]*\{?['."\r\n\t".'\s]*include\s*\(\s*STYLESHEETPATH\s*\.\s*[\'"]\/functions\.php[\'"]\s*\);['."\r\n\t".'\s]*\}?['."\r\n\t".'\s]*if\s*\(\s*file_exists\s*\(\s*TEMPLATEPATH\s*\.\s*[\'"]\/functions\.php[\'"]\s*\)\s*\)['."\r\n\t".'\s]*\{?['."\r\n\t".'\s]*include\s*\(\s*TEMPLATEPATH\s*\.\s*[\'"]\/functions\.php[\'"]\s*\);['."\r\n\t".'\s]*\}?['."\r\n\t".'\s]*\}/';
90 $th_funcs_replace = "\n\n".c_ws_plugin__s2member_utils_s2o::esc_ds(trim(c_ws_plugin__s2member_utils_s2o::evl(file_get_contents(dirname(dirname(__FILE__)).'/templates/cfg-files/s2o-th-funcs.php'))))."\n";
91 if(($_wp_settings = preg_replace($th_funcs_section, $th_funcs_replace, $_wp_settings, 1, $th_funcs_replaced)) && $th_funcs_replaced)
92 {
93 if(($_wp_settings = str_replace('__FILE__', '"'.str_replace('"', '\"', $o_file).'"', $_wp_settings))) // Eval compatible.
94 {
95 if(($_wp_settings = trim($_wp_settings))) // WordPress, with s2Member only.
96 return ($wp_settings_as = $_wp_settings); // After ``SHORTINIT``.
97 }
98 }
99 }
100 }
101 }
102 }
103 }
104 }
105 return FALSE;
106 }
107
108 /**
109 * Escapes dollars signs (for regex patterns).
110 *
111 * @package s2Member\Utilities
112 * @since 110917
113 *
114 * @param string $string Input string.
115 * @param int $times Mumber of escapes. Defaults to 1.
116 *
117 * @return string Output string after dollar signs are escaped.
118 */
119 public static function esc_ds($string = '', $times = 0)
120 {
121 $times = is_numeric($times) && $times >= 0 ? (int)$times : 1;
122 return str_replace('$', str_repeat('\\', $times).'$', (string)$string);
123 }
124
125 /**
126 * Evaluates PHP code, and 'returns' output.
127 *
128 * @package s2Member\Utilities
129 * @since 110917
130 *
131 * @param string $code A string of data, possibly with embedded PHP code.
132 *
133 * @return string Output after PHP evaluation.
134 */
135 public static function evl($code = '')
136 {
137 ob_start(); // Output buffer.
138
139 eval ('?>'.trim($code));
140
141 return ob_get_clean();
142 }
143 }
144 }
145