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