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

readmes.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 110710, at includes/classes/readmes.inc.php

236 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Readme file parsing.
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\Readmes
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_readmes"))
21 {
22 /**
23 * Readme file parsing.
24 *
25 * @package s2Member\Readmes
26 * @since 3.5
27 */
28 class c_ws_plugin__s2member_readmes
29 {
30 /**
31 * Handles readme parsing.
32 *
33 * @package s2Member\Readmes
34 * @since 3.5
35 *
36 * @param str $specific_path Optional. Path to a specific readme file to parse. Defaults to that of the software itself.
37 * When/if a readme-dev.txt file is available, that will be used instead of the default readme.txt.
38 * @param str $specific_section Optional. The title of a specific section to parse, instead of the entire file.
39 * @param bool $_blank_targets Optional. Defaults to true. If false, no target attribute is used.
40 * @param bool $process_wp_syntax Optional. Defaults to false.
41 * If true, and WP Syntax is installed; it will be used to parse code samples.
42 * @return str Parsed readme file, or a parsed readme file section; based on parameter configuration.
43 */
44 public static function parse_readme ($specific_path = FALSE, $specific_section = FALSE, $_blank_targets = TRUE, $process_wp_syntax = FALSE)
45 {
46 if (!($path = $specific_path)) /* Was a specific path passed in? */
47 {
48 $path = dirname (dirname (dirname (__FILE__))) . "/readme.txt";
49 $dev_path = dirname (dirname (dirname (__FILE__))) . "/readme-dev.txt";
50 $path = (file_exists ($dev_path)) ? $dev_path : $path;
51 }
52 /**/
53 eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
54 do_action ("ws_plugin__s2member_before_parse_readme", get_defined_vars ());
55 unset ($__refs, $__v); /* Unset defined __refs, __v. */
56 /**/
57 if (file_exists ($path)) /* Give hooks a chance. */
58 {
59 $o_pcre = @ini_get ("pcre.backtrack_limit");
60 @ini_set ("pcre.backtrack_limit", 10000000);
61 /**/
62 if (!function_exists ("NC_Markdown"))
63 include_once dirname (dirname (__FILE__)) . "/_xtnls/markdown/nc-markdown.inc.php";
64 /**/
65 $rm = file_get_contents ($path); /* Get readme.txt file contents. */
66 $mb = function_exists ("mb_convert_encoding") ? @mb_convert_encoding ($rm, "UTF-8",@mb_detect_encoding ($rm, "WINDOWS-1252, UTF-8")) : $rm;
67 $rm = ($mb) ? $mb : $rm; /* Double check this, just in case conversion fails on an unpredicted charset. */
68 /**/
69 if ($specific_section) /* If we are ONLY parsing a specific section. This is a very useful way of pulling details out. */
70 {
71 preg_match ("/(\=\= )(" . preg_quote ($specific_section, "/") . ")( \=\=)(.+?)([\r\n]+\=\= |$)/si", $rm, $m);
72 /**/
73 if ($rm = trim ($m[4])) /* Looking for a specific section, indicated by `$specific_section`. */
74 {
75 $rm = preg_replace ("/(\=\=\=)( )(.+?)( )(\=\=\=)/", "<h4 id=\"rm-specs\">Specifications</h4>", $rm);
76 $rm = preg_replace ("/(\=\=)( )(Installation)( )(\=\=)/", "<h4 id=\"rm-installation\">$3</h4>", $rm);
77 $rm = preg_replace ("/(\=\=)( )(Description)( )(\=\=)/", "<h4 id=\"rm-description\">$3</h4>", $rm);
78 $rm = preg_replace ("/(\=\=)( )(Screenshots)( )(\=\=)/", "<h4 id=\"rm-screenshots\">$3</h4>", $rm);
79 $rm = preg_replace ("/(\=\=)( )(Frequently Asked Questions)( )(\=\=)/", "<h4 id=\"rm-faqs\">$3</h4>", $rm);
80 $rm = preg_replace ("/(\=\=)( )(Changelog)( )(\=\=)/", "<h4 id=\"rm-changelog\">$3</h4>", $rm);
81 $rm = preg_replace ("/(\=\=)( )(.+?)( )(\=\=)/", "<h4>$3</h4>", $rm);
82 $rm = preg_replace ("/(\=)( )(.+?)( )(\=)/", "<h6>$3</h6>", $rm);
83 /**/
84 $y1 = "/\[youtube http\:\/\/www\.youtube\.com\/view_play_list\?p\=(.+?)[\s\/]*?\]/i";
85 $y2 = "/\[youtube http\:\/\/www\.youtube\.com\/watch\?v\=(.+?)[\s\/]*?\]/i";
86 /**/
87 $rm = preg_replace ($y1, '<embed type="application/x-shockwave-flash" src="//www.youtube.com/p/$1?version=3&hd=1&fs=1&rel=0" style="width:320px; height:210px; float:right; margin:0 0 15px 15px;" allowscriptaccess="always" allowfullscreen="true"></embed>', $rm);
88 $rm = preg_replace ($y2, '<embed type="application/x-shockwave-flash" src="//www.youtube.com/v/$1?version=3&hd=1&fs=1&rel=0" style="width:320px; height:210px; float:right; margin:0 0 15px 15px;" allowscriptaccess="always" allowfullscreen="true"></embed>', $rm);
89 /**/
90 $rm = NC_Markdown ($rm); /* Parse out the Markdown syntax. */
91 /**/
92 $r1 = "/(\<a)( href)/i"; /* Modify all links. Assume nofollow. */
93 /**/
94 if ($_blank_targets) /* Modify all links. Always nofollow. ( with _blank targets ? ). */
95 $rm = preg_replace ($r1, "$1" . ' target="_blank" rel="nofollow external"' . "$2", $rm);
96 else /* Otherwise, we don't need to set _blank targets. So external is removed also. */
97 $rm = preg_replace ($r1, "$1" . ' rel="nofollow"' . "$2", $rm);
98 /**/
99 if ($process_wp_syntax) /* If we're processing <pre><code> tags for WP-Syntax. */
100 if (function_exists ("wp_syntax_before_filter") && function_exists ("wp_syntax_before_filter"))
101 {
102 $rm = preg_replace ("/\<pre\>\<code\>/i", '<pre lang="php" escaped="true">', $rm);
103 $rm = preg_replace ("/\<\/code\>\<\/pre\>/i", '</pre>', $rm);
104 $rm = wp_syntax_after_filter (wp_syntax_before_filter ($rm));
105 }
106 }
107 /**/
108 @ini_set ("pcre.backtrack_limit", $o_pcre);
109 /**/
110 $readme = '<div class="readme">' . "\n";
111 $readme .= $rm . "\n"; /* Content. */
112 $readme .= '</div>' . "\n";
113 /**/
114 return apply_filters ("ws_plugin__s2member_parse_readme", $readme, get_defined_vars ());
115 }
116 else /* Otherwise, we're going for the entire readme file. Here we have lots of work to do. */
117 {
118 $rm = preg_replace ("/(\=\=\=)( )(.+?)( )(\=\=\=)/", "<h2 id=\"rm-specs\">Specifications</h2>", $rm);
119 $rm = preg_replace ("/(\=\=)( )(Installation)( )(\=\=)/", "<h2 id=\"rm-installation\">$3</h2>", $rm);
120 $rm = preg_replace ("/(\=\=)( )(Description)( )(\=\=)/", "<h2 id=\"rm-description\">$3</h2>", $rm);
121 $rm = preg_replace ("/(\=\=)( )(Screenshots)( )(\=\=)/", "<h2 id=\"rm-screenshots\">$3</h2>", $rm);
122 $rm = preg_replace ("/(\=\=)( )(Frequently Asked Questions)( )(\=\=)/", "<h2 id=\"rm-faqs\">$3</h2>", $rm);
123 $rm = preg_replace ("/(\=\=)( )(Changelog)( )(\=\=)/", "<h2 id=\"rm-changelog\">$3</h2>", $rm);
124 $rm = preg_replace ("/(\=\=)( )(.+?)( )(\=\=)/", "<h2>$3</h2>", $rm);
125 $rm = preg_replace ("/(\=)( )(.+?)( )(\=)/", "<h3>$3</h3>", $rm);
126 /**/
127 $y1 = "/\[youtube http\:\/\/www\.youtube\.com\/view_play_list\?p\=(.+?)[\s\/]*?\]/i";
128 $y2 = "/\[youtube http\:\/\/www\.youtube\.com\/watch\?v\=(.+?)[\s\/]*?\]/i";
129 /**/
130 $rm = preg_replace ($y1, '<embed type="application/x-shockwave-flash" src="//www.youtube.com/p/$1?version=3&hd=1&fs=1&rel=0" style="width:320px; height:210px; float:right; margin:0 0 15px 15px;" allowscriptaccess="always" allowfullscreen="true"></embed>', $rm);
131 $rm = preg_replace ($y2, '<embed type="application/x-shockwave-flash" src="//www.youtube.com/v/$1?version=3&hd=1&fs=1&rel=0" style="width:320px; height:210px; float:right; margin:0 0 15px 15px;" allowscriptaccess="always" allowfullscreen="true"></embed>', $rm);
132 /**/
133 $rm = NC_Markdown ($rm); /* Parse out the Markdown syntax. */
134 /**/
135 $r1 = "/(\<h2(.*?)\>)(.+?)(\<\/h2\>)(.+?)(\<h2(.*?)\>|$)/si";
136 $r2 = "/(\<\/div\>)(\<h2(.*?)\>)(.+?)(\<\/h2\>)(.+?)(\<div class\=\"section\"\>\<h2(.*?)\>|$)/si";
137 $r3 = "/(\<div class\=\"section\"\>)(\<h2 id\=\"rm-specs\"\>)(Specifications)(\<\/h2\>)(\<div class\=\"content\"\>)(.+?)(\<\/div\>\<\/div\>)/sei";
138 $r4 = "/(\<div class\=\"section\"\>)(\<h2 id\=\"rm-screenshots\"\>)(Screenshots)(\<\/h2\>)(\<div class\=\"content\"\>)(.+?)(\<\/div\>\<\/div\>)/sei";
139 $r5 = "/(\<a)( href)/i"; /* Modify all links. Assume a nofollow relationship since destinations are unknown. */
140 /**/
141 $rm = preg_replace ($r1, '<div class="section">' . "$1$3$4" . '<div class="content">' . "$5" . '</div></div>' . "$6", $rm);
142 $rm = preg_replace ($r2, "$1" . '<div class="section">' . "$2$4$5" . '<div class="content">' . "$6" . '</div></div>' . "$7", $rm);
143 $rm = stripslashes (preg_replace ($r3, "'$1$2$3$4$5'.c_ws_plugin__s2member_readmes::_parse_readme_specs('$6').'$7'", $rm, 1));
144 $rm = preg_replace ($r4, "", $rm, 1); /* Here we just remove the screenshots completely. */
145 /**/
146 if ($_blank_targets) /* Modify all links. Always nofollow. ( with _blank targets ? ). */
147 $rm = preg_replace ($r5, "$1" . ' target="_blank" rel="nofollow external"' . "$2", $rm);
148 else /* Otherwise, we don't need to set _blank targets. So external is removed also. */
149 $rm = preg_replace ($r5, "$1" . ' rel="nofollow"' . "$2", $rm);
150 /**/
151 if ($process_wp_syntax) /* If we're processing <pre><code> tags for WP-Syntax. */
152 if (function_exists ("wp_syntax_before_filter") && function_exists ("wp_syntax_before_filter"))
153 {
154 $rm = preg_replace ("/\<pre\>\<code\>/i", '<pre lang="php" escaped="true">', $rm);
155 $rm = preg_replace ("/\<\/code\>\<\/pre\>/i", '</pre>', $rm);
156 $rm = wp_syntax_after_filter (wp_syntax_before_filter ($rm));
157 }
158 /**/
159 @ini_set ("pcre.backtrack_limit", $o_pcre);
160 /**/
161 $readme = '<div class="readme">' . "\n";
162 $readme .= $rm . "\n"; /* Content. */
163 $readme .= '</div>' . "\n";
164 /**/
165 return apply_filters ("ws_plugin__s2member_parse_readme", $readme, get_defined_vars ());
166 }
167 }
168 else /* Just in case readme.txt was deleted by the site owner. */
169 {
170 return "Unable to parse /readme.txt.";
171 }
172 }
173 /**
174 * Callback parses specs in a readme file.
175 *
176 * @package s2Member\Readmes
177 * @since 3.5
178 *
179 * @param str $str A string *( i.e. the specs section )*.
180 * @return str Parsed specs. With HTML markup for list item display.
181 */
182 public static function _parse_readme_specs ($str = FALSE)
183 {
184 do_action ("_ws_plugin__s2member_before_parse_readme_specs", get_defined_vars ());
185 /**/
186 $str = preg_replace ("/(\<p\>|^)(.+?)(\:)( )(.+?)($|\<\/p\>)/mi", "$1" . '<li><strong>' . "$2" . '</strong>' . "$3" . '&nbsp;&nbsp;&nbsp;&nbsp;<code>' . "$5" . '</code></li>' . "$6", $str);
187 $str = preg_replace ("/\<p\>\<li\>/i", '<ul><li>', $str); /* Open the list items. */
188 $str = preg_replace ("/\<\/li\>\<\/p\>/i", '</li></ul><br />', $str);
189 /**/
190 return apply_filters ("_ws_plugin__s2member_parse_readme_specs", $str, get_defined_vars ());
191 }
192 /**
193 * Parses readme specification keys.
194 *
195 * @package s2Member\Readmes
196 * @since 3.5
197 *
198 * @param str $key A key *( within the specs section )*.
199 * @param str $specific_path Optional. Path to a specific readme file to parse. Defaults to that of the software itself.
200 * When/if a readme-dev.txt file is available, that will be used instead of the default readme.txt.
201 * @return str|bool The value of the key, else false if not found.
202 */
203 public static function parse_readme_value ($key = FALSE, $specific_path = FALSE)
204 {
205 static $readme = array (); /* For repeated lookups. */
206 /**/
207 if (!($path = $specific_path)) /* Was a specific path passed in? */
208 {
209 $path = dirname (dirname (dirname (__FILE__))) . "/readme.txt";
210 $dev_path = dirname (dirname (dirname (__FILE__))) . "/readme-dev.txt";
211 $path = (file_exists ($dev_path)) ? $dev_path : $path;
212 }
213 /**/
214 eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
215 do_action ("ws_plugin__s2member_before_parse_readme_value", get_defined_vars ());
216 unset ($__refs, $__v); /* Unset defined __refs, __v. */
217 /**/
218 if (!empty ($readme[$path]) || file_exists ($path))
219 {
220 if (empty ($readme[$path])) /* If not already opened. */
221 {
222 $readme[$path] = file_get_contents ($path); /* Get readme.txt file contents. */
223 $mb = function_exists ("mb_convert_encoding") ? @mb_convert_encoding ($readme[$path], "UTF-8",@mb_detect_encoding ($readme[$path], "WINDOWS-1252, UTF-8")) : $readme[$path];
224 $readme[$path] = ($mb) ? $mb : $readme[$path]; /* Double check this, just in case conversion fails on an unpredicted charset. */
225 }
226 /**/
227 preg_match ("/(^)(" . preg_quote ($key, "/") . ")(\:)( )(.+?)($)/m", $readme[$path], $m);
228 /**/
229 return apply_filters ("ws_plugin__s2member_parse_readme_value", ((isset ($m[5]) && strlen ($m[5] = trim ($m[5]))) ? $m[5] : false), get_defined_vars ());
230 }
231 else /* Nope. */
232 return false;
233 }
234 }
235 }
236 ?>