| @@ -1,0 +1,496 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | + | |
| 5 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 6 | +} | |
| 7 | + | |
| 8 | +if ( ! class_exists( 'SuextParseReadme' ) ) { | |
| 9 | + | |
| 10 | + class SuextParseReadme { | |
| 11 | + | |
| 12 | + private static $instance = null; // SuextParseReadme class object. | |
| 13 | + | |
| 14 | + function __construct() {} | |
| 15 | + | |
| 16 | + public static function &get_instance() { | |
| 17 | + | |
| 18 | + if ( null === self::$instance ) { | |
| 19 | + | |
| 20 | + self::$instance = new self; | |
| 21 | + } | |
| 22 | + | |
| 23 | + return self::$instance; | |
| 24 | + } | |
| 25 | + | |
| 26 | + function parse_file( $file_path ) { | |
| 27 | + | |
| 28 | + $file_content = @implode( '', @file( $file_path ) ); | |
| 29 | + | |
| 30 | + return $this->parse_content( $file_content ); | |
| 31 | + } | |
| 32 | + | |
| 33 | + function parse_content( $file_content ) { | |
| 34 | + | |
| 35 | + $file_content = str_replace( array( "\r\n", "\r" ), "\n", $file_content ); | |
| 36 | + $file_content = trim( $file_content ); | |
| 37 | + | |
| 38 | + if ( 0 === strpos( $file_content, "\xEF\xBB\xBF" ) ) { | |
| 39 | + | |
| 40 | + $file_content = substr( $file_content, 3 ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + if ( ! preg_match('|^===(.*)===|', $file_content, $_title ) ) { | |
| 44 | + | |
| 45 | + return array(); // Require a title. | |
| 46 | + } | |
| 47 | + | |
| 48 | + $title = trim( $_title[ 1 ], '=' ); | |
| 49 | + $title = $this->sanitize_text( $title ); | |
| 50 | + | |
| 51 | + $file_content = $this->chop_string( $file_content, $_title[ 0 ] ); | |
| 52 | + | |
| 53 | + if ( preg_match( '|Plugin Name: *(.*)|i', $file_content, $_plugin_name ) ) { | |
| 54 | + | |
| 55 | + $plugin_name = $this->sanitize_text( $_plugin_name[ 1 ] ); | |
| 56 | + | |
| 57 | + } else $plugin_name = null; | |
| 58 | + | |
| 59 | + if ( preg_match( '|Plugin Slug: *(.*)|i', $file_content, $_plugin_slug ) ) { | |
| 60 | + | |
| 61 | + $plugin_slug = $this->sanitize_text( $_plugin_slug[ 1 ] ); | |
| 62 | + | |
| 63 | + } else $plugin_slug = null; | |
| 64 | + | |
| 65 | + if ( preg_match( '|License: *(.*)|i', $file_content, $_license ) ) { | |
| 66 | + | |
| 67 | + $license = $this->sanitize_text( $_license[ 1 ] ); | |
| 68 | + | |
| 69 | + } else $license = null; | |
| 70 | + | |
| 71 | + if ( preg_match( '|License URI: *(.*)|i', $file_content, $_license_uri ) ) { | |
| 72 | + | |
| 73 | + $license_uri = esc_url_raw( $_license_uri[ 1 ] ); | |
| 74 | + | |
| 75 | + } else $license_uri = null; | |
| 76 | + | |
| 77 | + if ( preg_match( '|Donate Link: *(.*)|i', $file_content, $_donate_link ) ) { | |
| 78 | + | |
| 79 | + $donate_link = esc_url_raw( $_donate_link[ 1 ] ); | |
| 80 | + | |
| 81 | + } else $donate_link = null; | |
| 82 | + | |
| 83 | + if ( preg_match( '|Assets URI: *(.*)|i', $file_content, $_assets_uri ) ) { | |
| 84 | + | |
| 85 | + $assets_uri = esc_url_raw( $_assets_uri[ 1 ] ); | |
| 86 | + | |
| 87 | + } else $assets_uri = null; | |
| 88 | + | |
| 89 | + if ( preg_match( '|Tags: *(.*)|i', $file_content, $_tags ) ) { | |
| 90 | + | |
| 91 | + $tags = preg_split( '|,[\s]*?|', trim( $_tags[ 1 ] ) ); | |
| 92 | + | |
| 93 | + foreach ( array_keys($tags) as $t ) { | |
| 94 | + | |
| 95 | + $tags[ $t ] = $this->sanitize_text( $tags[ $t ] ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + } else $tags = array(); | |
| 99 | + | |
| 100 | + $contributors = array(); | |
| 101 | + | |
| 102 | + if ( preg_match( '|Contributors: *(.*)|i', $file_content, $_contributors ) ) { | |
| 103 | + | |
| 104 | + $all_contributors = preg_split( '|,[\s]*|', trim( $_contributors[ 1 ] ) ); | |
| 105 | + | |
| 106 | + foreach ( array_keys( $all_contributors ) as $c ) { | |
| 107 | + | |
| 108 | + $c_sanitized = trim( $this->user_sanitize( $all_contributors[ $c ] ) ); | |
| 109 | + | |
| 110 | + if ( strlen( $c_sanitized ) > 0 ) { | |
| 111 | + | |
| 112 | + $contributors[ $c ] = $c_sanitized; | |
| 113 | + } | |
| 114 | + | |
| 115 | + unset( $c_sanitized ); | |
| 116 | + } | |
| 117 | + } | |
| 118 | + | |
| 119 | + if ( preg_match( '|Requires PHP: *(.*)|i', $file_content, $_requires_php ) ) { | |
| 120 | + | |
| 121 | + $requires_php = $this->sanitize_text( $_requires_php[ 1 ] ); | |
| 122 | + | |
| 123 | + } else $requires_php = null; | |
| 124 | + | |
| 125 | + if ( preg_match( '|Requires At Least: *(.*)|i', $file_content, $_requires_at_least ) ) { | |
| 126 | + | |
| 127 | + $requires_at_least = $this->sanitize_text( $_requires_at_least[ 1 ] ); | |
| 128 | + | |
| 129 | + } else $requires_at_least = null; | |
| 130 | + | |
| 131 | + if ( preg_match( '|Tested Up To: *(.*)|i', $file_content, $_tested_up_to ) ) { | |
| 132 | + | |
| 133 | + $tested_up_to = $this->sanitize_text( $_tested_up_to[ 1 ] ); | |
| 134 | + | |
| 135 | + } else $tested_up_to = null; | |
| 136 | + | |
| 137 | + if ( preg_match( '|WC Tested Up To: *(.*)|i', $file_content, $_wc_tested_up_to ) ) { | |
| 138 | + | |
| 139 | + $wc_tested_up_to = $this->sanitize_text( $_wc_tested_up_to[ 1 ] ); | |
| 140 | + | |
| 141 | + } else $wc_tested_up_to = null; | |
| 142 | + | |
| 143 | + if ( preg_match( '|Stable Tag: *(.*)|i', $file_content, $_stable_tag ) ) { | |
| 144 | + | |
| 145 | + $stable_tag = $this->sanitize_text( $_stable_tag[ 1 ] ); | |
| 146 | + | |
| 147 | + } else $stable_tag = null; | |
| 148 | + | |
| 149 | + foreach ( array( | |
| 150 | + 'plugin_name', | |
| 151 | + 'plugin_slug', | |
| 152 | + 'license', | |
| 153 | + 'license_uri', | |
| 154 | + 'donate_link', | |
| 155 | + 'assets_uri', | |
| 156 | + 'tags', | |
| 157 | + 'contributors', | |
| 158 | + 'requires_php', | |
| 159 | + 'requires_at_least', | |
| 160 | + 'tested_up_to', | |
| 161 | + 'wc_tested_up_to', | |
| 162 | + 'stable_tag', | |
| 163 | + ) as $chop ) { | |
| 164 | + | |
| 165 | + if ( $$chop ) { | |
| 166 | + | |
| 167 | + $_chop = '_' . $chop; | |
| 168 | + | |
| 169 | + $file_content = $this->chop_string( $file_content, $$_chop[ 0 ] ); | |
| 170 | + } | |
| 171 | + } | |
| 172 | + | |
| 173 | + $file_content = trim( $file_content ); | |
| 174 | + | |
| 175 | + if ( ! preg_match( '/(^(.*?))^[\s]*=+?[\s]*.+?[\s]*=+?/ms', $file_content, $_short_description ) ) { | |
| 176 | + | |
| 177 | + $_short_description = array( 1 => &$file_content, 2 => &$file_content ); | |
| 178 | + } | |
| 179 | + | |
| 180 | + $short_desc_filtered = $this->sanitize_text( $_short_description[ 2 ] ); | |
| 181 | + $short_desc_length = strlen( $short_desc_filtered ); | |
| 182 | + $short_description = substr( $short_desc_filtered, 0, 150 ); | |
| 183 | + | |
| 184 | + if ( $short_desc_length > strlen( $short_description ) ) { | |
| 185 | + | |
| 186 | + $truncated = true; | |
| 187 | + | |
| 188 | + } else $truncated = false; | |
| 189 | + | |
| 190 | + if ( $_short_description[ 1 ] ) { | |
| 191 | + | |
| 192 | + $file_content = $this->chop_string( $file_content, $_short_description[ 1 ] ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + $_sections = preg_split( '/^[\s]*==[\s]*(.+?)[\s]*==/m', $file_content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); | |
| 196 | + | |
| 197 | + $sections = array(); | |
| 198 | + | |
| 199 | + for ( $i = 1; $i <= count( $_sections ); $i += 2 ) { | |
| 200 | + | |
| 201 | + if ( isset( $_sections[ $i ] ) ) { | |
| 202 | + | |
| 203 | + $_sections[ $i ] = preg_replace( '/^[\s]*=[\s]+(.+?)[\s]+=/m', '<h4>$1</h4>', $_sections[ $i ] ); | |
| 204 | + $_sections[ $i ] = $this->filter_text( $_sections[ $i ], true ); | |
| 205 | + $_sections[ $i ] = preg_replace( '/\[youtube https:\/\/www\.youtube\.com\/watch\?v=([^\]]+)\]/', '<div class="video"><object width="532" height="325"><param name="movie" value="http://www.youtube.com/v/$1?fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="never"></param><embed src="http://www.youtube.com/v/$1?fs=1" type="application/x-shockwave-flash" allowscriptaccess="never" allowfullscreen="true" width="532" height="325"></embed></object></div>', $_sections[ $i ] ); | |
| 206 | + | |
| 207 | + $section_title = $this->sanitize_text( $_sections[ $i - 1 ] ); | |
| 208 | + | |
| 209 | + $sections[ str_replace( ' ', '_', strtolower( $section_title ) ) ] = array( | |
| 210 | + 'section_title' => $section_title, | |
| 211 | + 'section_content' => $_sections[ $i ], | |
| 212 | + ); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + $final_sections = array(); | |
| 217 | + | |
| 218 | + foreach ( array( | |
| 219 | + 'description' => 'description', | |
| 220 | + 'installation' => 'installation', | |
| 221 | + 'frequently_asked_questions' => 'faq', | |
| 222 | + 'screenshots' => 'screenshots', | |
| 223 | + 'changelog' => 'changelog', | |
| 224 | + 'change_log' => 'changelog', | |
| 225 | + 'upgrade_notice' => 'upgrade_notice', | |
| 226 | + ) as $section_key => $final_key ) { | |
| 227 | + | |
| 228 | + if ( isset( $sections[ $section_key ] ) ) { | |
| 229 | + | |
| 230 | + if ( empty( $final_sections[ $final_key ] ) ) { | |
| 231 | + | |
| 232 | + $final_sections[ $final_key ] = $sections[ $section_key ][ 'section_content' ]; | |
| 233 | + } | |
| 234 | + | |
| 235 | + unset( $sections[ $section_key ] ); | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + $final_screenshots = array(); | |
| 240 | + | |
| 241 | + if ( isset( $final_sections[ 'screenshots' ] ) ) { | |
| 242 | + | |
| 243 | + preg_match_all( '|<li>(.*?)</li>|s', $final_sections[ 'screenshots' ], $screenshots, PREG_SET_ORDER ); | |
| 244 | + | |
| 245 | + if ( $screenshots ) { | |
| 246 | + | |
| 247 | + foreach ( $screenshots as $ss ) { | |
| 248 | + | |
| 249 | + $final_screenshots[] = $ss[ 1 ]; | |
| 250 | + } | |
| 251 | + } | |
| 252 | + } | |
| 253 | + | |
| 254 | + if ( isset( $final_sections[ 'upgrade_notice' ] ) ) { | |
| 255 | + | |
| 256 | + $upgrade_notice = array(); | |
| 257 | + | |
| 258 | + $split = preg_split( '#<h4>(.*?)</h4>#', $final_sections[ 'upgrade_notice' ], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); | |
| 259 | + | |
| 260 | + for ( $i = 0; $i < count( $split ); $i += 2 ) { | |
| 261 | + | |
| 262 | + if ( isset( $split[ $i + 1 ] ) ) { | |
| 263 | + | |
| 264 | + $upgrade_notice[ $this->sanitize_text( $split[ $i ] ) ] = substr( $this->sanitize_text( $split[ $i + 1 ] ), 0, 300 ); | |
| 265 | + } | |
| 266 | + } | |
| 267 | + | |
| 268 | + unset( $final_sections[ 'upgrade_notice' ] ); | |
| 269 | + | |
| 270 | + } else $upgrade_notice = ''; | |
| 271 | + | |
| 272 | + $excerpt = false; | |
| 273 | + | |
| 274 | + if ( ! isset ( $final_sections[ 'description' ] ) ) { | |
| 275 | + | |
| 276 | + $final_sections = array_merge( array( 'description' => $this->filter_text( $_short_description[ 2 ], true ) ), $final_sections ); | |
| 277 | + | |
| 278 | + $excerpt = true; | |
| 279 | + } | |
| 280 | + | |
| 281 | + $remaining_content = ''; | |
| 282 | + | |
| 283 | + foreach ( $sections as $s_name => $s_data ) { | |
| 284 | + | |
| 285 | + $remaining_content .= $s_data[ 'section_content' ] . "\n"; | |
| 286 | + } | |
| 287 | + | |
| 288 | + $remaining_content = trim( $remaining_content ); | |
| 289 | + | |
| 290 | + $r = array( | |
| 291 | + 'title' => $title, | |
| 292 | + 'plugin_name' => $plugin_name, | |
| 293 | + 'plugin_slug' => $plugin_slug, | |
| 294 | + 'license' => $license, | |
| 295 | + 'license_uri' => $license_uri, | |
| 296 | + 'donate_link' => $donate_link, | |
| 297 | + 'assets_uri' => $assets_uri, | |
| 298 | + 'tags' => $tags, | |
| 299 | + 'contributors' => $contributors, | |
| 300 | + 'requires_php' => $requires_php, | |
| 301 | + 'requires_at_least' => $requires_at_least, | |
| 302 | + 'tested_up_to' => $tested_up_to, | |
| 303 | + 'wc_tested_up_to' => $wc_tested_up_to, | |
| 304 | + 'stable_tag' => $stable_tag, | |
| 305 | + 'short_description' => $short_description, | |
| 306 | + 'screenshots' => $final_screenshots, | |
| 307 | + 'sections' => $final_sections, | |
| 308 | + 'remaining_content' => $remaining_content, | |
| 309 | + 'upgrade_notice' => $upgrade_notice, | |
| 310 | + 'is_excerpt' => $excerpt, | |
| 311 | + 'is_truncated' => $truncated, | |
| 312 | + ); | |
| 313 | + | |
| 314 | + return $r; | |
| 315 | + } | |
| 316 | + | |
| 317 | + function chop_string( $string, $chop ) { | |
| 318 | + | |
| 319 | + if ( $_string = strstr( $string, $chop ) ) { | |
| 320 | + | |
| 321 | + $_string = substr( $_string, strlen( $chop ) ); | |
| 322 | + | |
| 323 | + return trim( $_string ); | |
| 324 | + } | |
| 325 | + | |
| 326 | + return trim( $string ); | |
| 327 | + } | |
| 328 | + | |
| 329 | + function user_sanitize( $text, $strict = false ) { | |
| 330 | + | |
| 331 | + if ( function_exists('user_sanitize') ) { // bbPress native | |
| 332 | + | |
| 333 | + return user_sanitize( $text, $strict ); | |
| 334 | + } | |
| 335 | + | |
| 336 | + if ( $strict ) { | |
| 337 | + | |
| 338 | + $text = preg_replace('/[^a-z0-9-]/i', '', $text); | |
| 339 | + $text = preg_replace('|-+|', '-', $text); | |
| 340 | + | |
| 341 | + } else $text = preg_replace('/[^a-z0-9_-]/i', '', $text); | |
| 342 | + | |
| 343 | + return $text; | |
| 344 | + } | |
| 345 | + | |
| 346 | + function sanitize_text( $text ) { // Not fancy. | |
| 347 | + | |
| 348 | + $text = strip_tags( $text ); | |
| 349 | + $text = esc_html( $text ); | |
| 350 | + $text = trim( $text ); | |
| 351 | + | |
| 352 | + return $text; | |
| 353 | + } | |
| 354 | + | |
| 355 | + function filter_text( $text, $markdown = false ) { | |
| 356 | + | |
| 357 | + $text = trim( $text ); | |
| 358 | + $text = self::code_trick( $text, $markdown ); | |
| 359 | + | |
| 360 | + if ( $markdown ) { | |
| 361 | + | |
| 362 | + if ( ! class_exists( 'SuextMarkdown' ) ) { | |
| 363 | + | |
| 364 | + require_once dirname( __FILE__ ) . '/markdown.php'; | |
| 365 | + } | |
| 366 | + | |
| 367 | + $text = SuextMarkdown::transform( $text ); | |
| 368 | + } | |
| 369 | + | |
| 370 | + $allowed = array( | |
| 371 | + 'a' => array( | |
| 372 | + 'name' => array(), | |
| 373 | + 'href' => array(), | |
| 374 | + 'title' => array(), | |
| 375 | + 'rel' => array(), | |
| 376 | + ), | |
| 377 | + 'blockquote' => array( | |
| 378 | + 'cite' => array(), | |
| 379 | + ), | |
| 380 | + 'br' => array(), | |
| 381 | + 'code' => array(), | |
| 382 | + 'div' => array(), | |
| 383 | + 'em' => array(), | |
| 384 | + 'h3' => array(), | |
| 385 | + 'h4' => array(), | |
| 386 | + 'font' => array( | |
| 387 | + 'color' => array(), | |
| 388 | + ), | |
| 389 | + 'img' => array( | |
| 390 | + 'src' => array(), | |
| 391 | + 'alt' => array(), | |
| 392 | + 'width' => array(), | |
| 393 | + 'height' => array(), | |
| 394 | + 'style' => array(), | |
| 395 | + ), | |
| 396 | + 'li' => array(), | |
| 397 | + 'ol' => array(), | |
| 398 | + 'p' => array(), | |
| 399 | + 'pre' => array(), | |
| 400 | + 'small' => array(), | |
| 401 | + 'strong' => array(), | |
| 402 | + 'table' => array(), | |
| 403 | + 'tr' => array(), | |
| 404 | + 'th' => array(), | |
| 405 | + 'td' => array( | |
| 406 | + 'valign' => array(), | |
| 407 | + ), | |
| 408 | + 'ul' => array(), | |
| 409 | + ); | |
| 410 | + | |
| 411 | + $text = balanceTags( $text ); | |
| 412 | + | |
| 413 | + return $text; | |
| 414 | + } | |
| 415 | + | |
| 416 | + function code_trick( $text, $markdown ) { | |
| 417 | + | |
| 418 | + $text = str_replace( array( "\r\n", "\r" ), "\n", $text ); | |
| 419 | + | |
| 420 | + if ( ! $markdown ) { | |
| 421 | + | |
| 422 | + /* | |
| 423 | + * This gets the "inline" code blocks, but can't be used with markdown. | |
| 424 | + */ | |
| 425 | + $text = preg_replace_callback( "!(`)(.*?)`!", array( __CLASS__, 'encodeit' ), $text ); | |
| 426 | + | |
| 427 | + /* | |
| 428 | + * This gets the "block level" code blocks and converts them to pre code. | |
| 429 | + */ | |
| 430 | + $text = preg_replace_callback( "!(^|\n)`(.*?)`!s", array( __CLASS__, 'encodeit'), $text ); | |
| 431 | + | |
| 432 | + } else { | |
| 433 | + | |
| 434 | + /* | |
| 435 | + * Markdown can do inline code, we convert bbPress style block level code to markdown style. | |
| 436 | + */ | |
| 437 | + $text = preg_replace_callback( "!(^|\n)([ \t]*?)`(.*?)`!s", array( __CLASS__, 'indent' ), $text ); | |
| 438 | + } | |
| 439 | + | |
| 440 | + return $text; | |
| 441 | + } | |
| 442 | + | |
| 443 | + function indent( $matches ) { | |
| 444 | + | |
| 445 | + $text = $matches[ 3 ]; | |
| 446 | + $text = preg_replace( '|^|m', $matches[ 2 ] . "\t", $text ); | |
| 447 | + | |
| 448 | + return $matches[ 1 ] . "\n`" . $text . "`\n"; | |
| 449 | + } | |
| 450 | + | |
| 451 | + function encodeit( $matches ) { | |
| 452 | + | |
| 453 | + if ( function_exists( 'encodeit' ) ) { // bbPress native | |
| 454 | + | |
| 455 | + return encodeit( $matches ); | |
| 456 | + } | |
| 457 | + | |
| 458 | + $text = trim( $matches[ 2 ] ); | |
| 459 | + $text = htmlspecialchars( $text, ENT_QUOTES ); | |
| 460 | + $text = str_replace( array( "\r\n", "\r" ), "\n", $text ); | |
| 461 | + $text = preg_replace( "|\n\n\n+|", "\n\n", $text ); | |
| 462 | + $text = str_replace( '&lt;', '<', $text ); | |
| 463 | + $text = str_replace( '&gt;', '>', $text ); | |
| 464 | + $text = '<code>' . $text . '</code>'; | |
| 465 | + | |
| 466 | + if ( '`' != $matches[ 1 ] ) { | |
| 467 | + | |
| 468 | + $text = '<pre>' . $text . '</pre>'; | |
| 469 | + } | |
| 470 | + | |
| 471 | + return $text; | |
| 472 | + } | |
| 473 | + | |
| 474 | + function decodeit( $matches ) { | |
| 475 | + | |
| 476 | + if ( function_exists('decodeit') ) { // bbPress native | |
| 477 | + | |
| 478 | + return decodeit( $matches ); | |
| 479 | + } | |
| 480 | + | |
| 481 | + $text = $matches[ 2 ]; | |
| 482 | + $trans_table = array_flip( get_html_translation_table( HTML_ENTITIES ) ); | |
| 483 | + $text = strtr( $text, $trans_table ); | |
| 484 | + $text = str_replace( '<br />', '', $text ); | |
| 485 | + $text = str_replace( '&', '&', $text ); | |
| 486 | + $text = str_replace( ''', "'", $text ); | |
| 487 | + | |
| 488 | + if ( '<pre><code>' == $matches[ 1 ] ) { | |
| 489 | + | |
| 490 | + $text = "\n$text\n"; | |
| 491 | + } | |
| 492 | + | |
| 493 | + return "`$text`"; | |
| 494 | + } | |
| 495 | + } | |
| 496 | +} | |