PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / trunk
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions vtrunk
260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 120219 120301 All 187 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 trunk, at src/includes/classes/utils-s2o.inc.php

149 lines 7.1 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 //260903.2216 Match stable loader boundaries instead of exact loop bodies; WordPress 5.1+ adds per-plugin hooks and later releases add more statements inside these loops.
78 $mu_plugins_section = '/\s+foreach\s*\(\s*wp_get_mu_plugins\s*\(\s*\)\s*as\s*\$mu_plugin\s*\)\s*\{.*?\}\s*unset\s*\(\s*\$mu_plugin(?:\s*,[^)]*)?\s*\)\s*;/s';
79 $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";
80 if(($_wp_settings = preg_replace($mu_plugins_section, $mu_plugins_replace, $_wp_settings, 1, $mu_plugins_replaced)) && $mu_plugins_replaced)
81 {
82 //260903.2216 Keep the network-plugin replacement tolerant of WordPress statements added between the loader's stable foreach/unset boundaries.
83 $nw_plugins_section = '/\s+foreach\s*\(\s*wp_get_active_network_plugins\s*\(\s*\)\s*as\s*\$network_plugin\s*\)\s*\{.*?\}\s*unset\s*\(\s*\$network_plugin(?:\s*,[^)]*)?\s*\)\s*;/s';
84 $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";
85 if(($_wp_settings = preg_replace($nw_plugins_section, $nw_plugins_replace, $_wp_settings, 1, $nw_plugins_replaced)) && $nw_plugins_replaced)
86 {
87 //260903.2216 Keep the active-plugin replacement tolerant of metadata, translation, and plugin-loaded bookkeeping added by newer WordPress releases.
88 $st_plugins_section = '/\s+foreach\s*\(\s*wp_get_active_and_valid_plugins\s*\(\s*\)\s*as\s*\$plugin\s*\)\s*\{.*?\}\s*unset\s*\(\s*\$plugin(?:\s*,[^)]*)?\s*\)\s*;/s';
89 $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";
90 if(($_wp_settings = preg_replace($st_plugins_section, $st_plugins_replace, $_wp_settings, 1, $st_plugins_replaced)) && $st_plugins_replaced)
91 {
92 //260903.2302 Support all declared WordPress 4.2+ theme-loader forms: WP_INSTALLING through 4.3, wp_installing() in 4.4–5.0, and wp_get_active_and_valid_themes() from 5.1 onward.
93 $th_funcs_section = '/(?:\s+if\s*\(\s*!\s*(?:defined\s*\(\s*[\'"]WP_INSTALLING[\'"]\s*\)|wp_installing\s*\(\s*\))\s*\|\|\s*[\'"]wp\-activate\.php[\'"]\s*\=\=\=\s*\$pagenow\s*\)\s*\{.*?\}\s*(?=\/\*\*)|\s+foreach\s*\(\s*wp_get_active_and_valid_themes\s*\(\s*\)\s*as\s*\$theme\s*\)\s*\{.*?\}\s*unset\s*\(\s*\$theme(?:\s*,[^)]*)?\s*\)\s*;)/s';
94 $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";
95 if(($_wp_settings = preg_replace($th_funcs_section, $th_funcs_replace, $_wp_settings, 1, $th_funcs_replaced)) && $th_funcs_replaced)
96 {
97 if(($_wp_settings = str_replace('__FILE__', '"'.str_replace('"', '\"', $o_file).'"', $_wp_settings))) // Eval compatible.
98 {
99 if(($_wp_settings = trim($_wp_settings))) // WordPress, with s2Member only.
100 return ($wp_settings_as = $_wp_settings); // After ``SHORTINIT``.
101 }
102 }
103 }
104 }
105 }
106 }
107 }
108 }
109 return FALSE;
110 }
111
112 /**
113 * Escapes dollars signs (for regex patterns).
114 *
115 * @package s2Member\Utilities
116 * @since 110917
117 *
118 * @param string $string Input string.
119 * @param int $times Mumber of escapes. Defaults to 1.
120 *
121 * @return string Output string after dollar signs are escaped.
122 */
123 public static function esc_ds($string = '', $times = 0)
124 {
125 $times = is_numeric($times) && $times >= 0 ? (int)$times : 1;
126 return str_replace('$', str_repeat('\\', $times).'$', (string)$string);
127 }
128
129 /**
130 * Evaluates PHP code, and 'returns' output.
131 *
132 * @package s2Member\Utilities
133 * @since 110917
134 *
135 * @param string $code A string of data, possibly with embedded PHP code.
136 *
137 * @return string Output after PHP evaluation.
138 */
139 public static function evl($code = '')
140 {
141 ob_start(); // Output buffer.
142
143 eval ('?>'.trim($code));
144
145 return ob_get_clean();
146 }
147 }
148 }
149