class-fl-builder-importer.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * The WordPress importer plugin has a few issues that break
  4. * serialized data in certain cases. This class is our own
  5. * patched version that fixes these issues.
  6. *
  7. * @since 1.8
  8. */
  9. class FLBuilderImporter extends WP_Import {
  10. /**
  11. * @since 1.8
  12. * @return array
  13. */
  14. function parse( $file ) {
  15. $parser = new FLBuilderImportParserRegex();
  16. return $parser->parse( $file );
  17. }
  18. }
  19. /**
  20. * The Regex parser is the only parser we have found that
  21. * doesn't break serialized data. It does have two bugs
  22. * that can break serialized data. Those are calling rtrim
  23. * on each $importline and adding a newline to each $importline.
  24. * This class fixes those bugs.
  25. *
  26. * @since 1.8
  27. */
  28. class FLBuilderImportParserRegex extends WXR_Parser_Regex {
  29. /**
  30. * @since 1.8
  31. * @return array
  32. */
  33. function parse( $file ) {
  34. // @codingStandardsIgnoreLine
  35. $wxr_version = $in_post = false;
  36. $fp = $this->fopen( $file, 'r' );
  37. if ( $fp ) {
  38. while ( ! $this->feof( $fp ) ) {
  39. $importline = $this->fgets( $fp );
  40. if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) ) {
  41. $wxr_version = $version[1];
  42. }
  43. if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) {
  44. preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url );
  45. $this->base_url = $url[1];
  46. continue;
  47. }
  48. if ( false !== strpos( $importline, '<wp:category>' ) ) {
  49. preg_match( '|<wp:category>(.*?)</wp:category>|is', $importline, $category );
  50. if ( isset( $category[1] ) ) {
  51. $this->categories[] = $this->process_category( $category[1] );
  52. }
  53. continue;
  54. }
  55. if ( false !== strpos( $importline, '<wp:tag>' ) ) {
  56. preg_match( '|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag );
  57. if ( isset( $tag[1] ) ) {
  58. $this->tags[] = $this->process_tag( $tag[1] );
  59. }
  60. continue;
  61. }
  62. if ( false !== strpos( $importline, '<wp:term>' ) ) {
  63. preg_match( '|<wp:term>(.*?)</wp:term>|is', $importline, $term );
  64. if ( isset( $term[1] ) ) {
  65. $this->terms[] = $this->process_term( $term[1] );
  66. }
  67. continue;
  68. }
  69. if ( false !== strpos( $importline, '<wp:author>' ) ) {
  70. preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author );
  71. if ( isset( $author[1] ) ) {
  72. $a = $this->process_author( $author[1] );
  73. }
  74. $this->authors[ $a['author_login'] ] = $a;
  75. continue;
  76. }
  77. if ( false !== strpos( $importline, '<item>' ) ) {
  78. $post = '';
  79. $in_post = true;
  80. continue;
  81. }
  82. if ( false !== strpos( $importline, '</item>' ) ) {
  83. $in_post = false;
  84. $this->set_pcre_limit( apply_filters( 'fl_builder_importer_pcre', '23001337' ) );
  85. $this->posts[] = $this->process_post( $post );
  86. $this->set_pcre_limit( 'default' );
  87. continue;
  88. }
  89. if ( $in_post ) {
  90. $post .= $importline;
  91. }
  92. }
  93. $this->fclose( $fp );
  94. // Try to fix any broken builder data.
  95. foreach ( $this->posts as $post_index => $post ) {
  96. if ( ! isset( $post['postmeta'] ) || ! is_array( $post['postmeta'] ) ) {
  97. continue;
  98. }
  99. foreach ( $post['postmeta'] as $postmeta_index => $postmeta ) {
  100. if ( stristr( $postmeta['key'], '_fl_builder_' ) ) {
  101. $this->posts[ $post_index ]['postmeta'][ $postmeta_index ]['value'] = FLBuilderImporterDataFix::run( $postmeta['value'] );
  102. }
  103. }
  104. }
  105. }
  106. if ( ! $wxr_version ) {
  107. return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'fl-builder' ) );
  108. }
  109. return array(
  110. 'authors' => $this->authors,
  111. 'posts' => $this->posts,
  112. 'categories' => $this->categories,
  113. 'tags' => $this->tags,
  114. 'terms' => $this->terms,
  115. 'base_url' => $this->base_url,
  116. 'version' => $wxr_version,
  117. );
  118. }
  119. /**
  120. * Try increasing PCRE limit to avoid failing of importing huge postmeta data.
  121. *
  122. * @since 1.10.9
  123. * @param string $value
  124. */
  125. function set_pcre_limit( $value ) {
  126. if ( ! isset( $this->default_backtrack_limit ) ) {
  127. $this->default_backtrack_limit = @ini_get( 'pcre.backtrack_limit' ); // @codingStandardsIgnoreLine
  128. $this->default_recursion_limit = @ini_get( 'pcre.recursion_limit' ); // @codingStandardsIgnoreLine
  129. }
  130. if ( 'default' != $value ) {
  131. @ini_set( 'pcre.backtrack_limit', $value ); // @codingStandardsIgnoreLine
  132. @ini_set( 'pcre.recursion_limit', $value ); // @codingStandardsIgnoreLine
  133. } else {
  134. // Reset limit back to default.
  135. if ( is_numeric( $this->default_backtrack_limit ) ) {
  136. @ini_set( 'pcre.backtrack_limit', $this->default_backtrack_limit ); // @codingStandardsIgnoreLine
  137. }
  138. if ( is_numeric( $this->default_recursion_limit ) ) {
  139. @ini_set( 'pcre.recursion_limit', $this->default_recursion_limit ); // @codingStandardsIgnoreLine
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * Portions borrowed from https://github.com/Blogestudio/Fix-Serialization/blob/master/fix-serialization.php
  146. *
  147. * Attempts to fix broken serialized data.
  148. *
  149. * @since 1.8
  150. */
  151. final class FLBuilderImporterDataFix {
  152. /**
  153. * @since 1.8
  154. * @return string
  155. */
  156. static public function run( $data ) {
  157. // return if empty
  158. if ( empty( $data ) ) {
  159. return $data;
  160. }
  161. $data = maybe_unserialize( $data );
  162. // return if maybe_unserialize() returns an object or array, this is good.
  163. if ( is_object( $data ) || is_array( $data ) ) {
  164. return $data;
  165. }
  166. return preg_replace_callback( '!s:(\d+):([\\\\]?"[\\\\]?"|[\\\\]?"((.*?)[^\\\\])[\\\\]?");!', 'FLBuilderImporterDataFix::regex_callback', $data );
  167. }
  168. /**
  169. * @since 1.8
  170. * @return string
  171. */
  172. static public function regex_callback( $matches ) {
  173. if ( ! isset( $matches[3] ) ) {
  174. return $matches[0];
  175. }
  176. return 's:' . strlen( self::unescape_mysql( $matches[3] ) ) . ':"' . self::unescape_quotes( $matches[3] ) . '";';
  177. }
  178. /**
  179. * Unescape to avoid dump-text issues.
  180. *
  181. * @since 1.8
  182. * @access private
  183. * @return string
  184. */
  185. static private function unescape_mysql( $value ) {
  186. return str_replace( array( '\\\\', "\\0", "\\n", "\\r", '\Z', "\'", '\"' ),
  187. array( '\\', "\0", "\n", "\r", "\x1a", "'", '"' ),
  188. $value );
  189. }
  190. /**
  191. * Fix strange behaviour if you have escaped quotes in your replacement.
  192. *
  193. * @since 1.8
  194. * @access private
  195. * @return string
  196. */
  197. static private function unescape_quotes( $value ) {
  198. return str_replace( '\"', '"', $value );
  199. }
  200. }