PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/s2member/includes/classes/readmes.inc.php

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