PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wordpress/wp-content/plugins/wp-multibyte-patch/wp-multibyte-patch.php

http://ichitaso.googlecode.com/
PHP | 430 lines | 387 code | 29 blank | 14 comment | 15 complexity | 91906e8c8f59b7e799d8c05e50c1c144 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: WP Multibyte Patch
  4. Plugin URI: http://eastcoder.com/code/wp-multibyte-patch/
  5. Description: Enhances multibyte string I/O functionality of WordPress.
  6. Author: tenpura
  7. Version: 1.3
  8. Author URI: http://eastcoder.com/
  9. Text Domain: wp-multibyte-patch
  10. Domain Path: /languages
  11. */
  12. /*
  13. Copyright (C) 2010 tenpura (Email: 210pura at gmail dot com), Tinybit Inc.
  14. This program is licensed under the GNU GPL Version 2.
  15. */
  16. class multibyte_patch {
  17. var $conf = array(
  18. 'excerpt_length' => 55,
  19. 'excerpt_mblength' => 110,
  20. 'excerpt_more' => ' [...]',
  21. 'comment_excerpt_length' => 20,
  22. 'comment_excerpt_mblength' => 40,
  23. 'ascii_threshold' => 90,
  24. 'patch_wp_mail' => true,
  25. 'patch_incoming_trackback' => true,
  26. 'patch_incoming_pingback' => true,
  27. 'patch_wp_trim_excerpt' => true,
  28. 'patch_get_comment_excerpt' => true,
  29. 'patch_process_search_terms' => true,
  30. 'patch_admin_custom_css' => true,
  31. 'patch_word_count_js' => true,
  32. 'patch_sanitize_file_name' => true,
  33. 'patch_bp_create_excerpt' => false,
  34. 'bp_excerpt_mblength' => 110,
  35. 'bp_excerpt_more' => ' [...]'
  36. );
  37. var $blog_encoding;
  38. var $has_mbfunctions;
  39. var $textdomain = 'wp-multibyte-patch';
  40. var $lang_dir = 'languages';
  41. var $required_version = '3.0';
  42. var $query_based_vars = array();
  43. function guess_encoding($string, $encoding = '') {
  44. $blog_encoding = $this->blog_encoding;
  45. if(!$encoding && seems_utf8($string))
  46. return 'UTF-8';
  47. elseif(!$encoding)
  48. return $blog_encoding;
  49. else
  50. return $encoding;
  51. }
  52. function convenc($string, $to_encoding, $from_encoding = '') {
  53. $blog_encoding = $this->blog_encoding;
  54. if('' == $from_encoding)
  55. $from_encoding = $blog_encoding;
  56. if(strtoupper($to_encoding) == strtoupper($from_encoding))
  57. return $string;
  58. else
  59. return mb_convert_encoding($string, $to_encoding, $from_encoding);
  60. }
  61. function incoming_trackback($commentdata) {
  62. global $wpdb;
  63. if('trackback' != $commentdata['comment_type'])
  64. return $commentdata;
  65. if(false === $this->conf['patch_incoming_trackback'])
  66. return $commentdata;
  67. $title = isset($_POST['title']) ? stripslashes($_POST['title']) : '';
  68. $excerpt = isset($_POST['excerpt']) ? stripslashes($_POST['excerpt']) : '';
  69. $blog_name = isset($_POST['blog_name']) ? stripslashes($_POST['blog_name']) : '';
  70. $blog_encoding = $this->blog_encoding;
  71. $from_encoding = isset($_POST['charset']) ? $_POST['charset'] : '';
  72. if(!$from_encoding)
  73. $from_encoding = (preg_match("/^.*charset=([a-zA-Z0-9\-_]+).*$/i", $_SERVER['CONTENT_TYPE'], $matched)) ? $matched[1] : '';
  74. $from_encoding = str_replace(array(',', ' '), '', strtoupper(trim($from_encoding)));
  75. $from_encoding = $this->guess_encoding($excerpt . $title . $blog_name, $from_encoding);
  76. $title = $this->convenc($title, $blog_encoding, $from_encoding);
  77. $blog_name = $this->convenc($blog_name, $blog_encoding, $from_encoding);
  78. $excerpt = $this->convenc($excerpt, $blog_encoding, $from_encoding);
  79. $title = strip_tags($title);
  80. $excerpt = strip_tags($excerpt);
  81. $title = (strlen($title) > 250) ? mb_strcut($title, 0, 250, $blog_encoding) . '...' : $title;
  82. $excerpt = (strlen($excerpt) > 255) ? mb_strcut($excerpt, 0, 252, $blog_encoding) . '...' : $excerpt;
  83. $commentdata['comment_author'] = $wpdb->escape($blog_name);
  84. $commentdata['comment_content'] = $wpdb->escape("<strong>$title</strong>\n\n$excerpt");
  85. return $commentdata;
  86. }
  87. function pre_remote_source($linea, $pagelinkedto) {
  88. $this->pingback_ping_linea = $linea;
  89. $this->pingback_ping_pagelinkedto = $pagelinkedto;
  90. return $linea;
  91. }
  92. function incoming_pingback($commentdata) {
  93. if('pingback' != $commentdata['comment_type'])
  94. return $commentdata;
  95. if(false === $this->conf['patch_incoming_pingback'])
  96. return $commentdata;
  97. $pagelinkedto = $this->pingback_ping_pagelinkedto;
  98. $linea = $this->pingback_ping_linea;
  99. $linea = preg_replace("/" . preg_quote('<!DOC', '/') . "/i", '<DOC', $linea);
  100. $linea = preg_replace("/\s+/", ' ', $linea);
  101. $linea = preg_replace("/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/i", "\n\n", $linea);
  102. preg_match("/<meta[^<>]+charset=\"*([a-zA-Z0-9\-_]+)\"*[^<>]*>/i", $linea, $matches);
  103. $charset = isset($matches[1]) ? $matches[1] : '';
  104. $from_encoding = $this->guess_encoding(strip_tags($linea), $charset);
  105. $blog_encoding = $this->blog_encoding;
  106. $linea = strip_tags($linea, '<a>');
  107. $linea = $this->convenc($linea, $blog_encoding, $from_encoding);
  108. $p = explode("\n\n", $linea);
  109. foreach ($p as $para) {
  110. if(strpos($para, $pagelinkedto) !== false && preg_match("/^([^<>]*)(\<a[^<>]+[\"']" . preg_quote($pagelinkedto, '/') . "[\"'][^<>]*\>)([^<>]+)(\<\/a\>)(.*)$/i", $para, $context))
  111. break;
  112. }
  113. if(!$context)
  114. return $commentdata;
  115. $context[1] = strip_tags($context[1]);
  116. $context[5] = strip_tags($context[5]);
  117. $len_max = 250;
  118. $len_c3 = strlen($context[3]);
  119. if($len_c3 > $len_max) {
  120. $excerpt = mb_strcut($context[3], 0, 250, $blog_encoding);
  121. } else {
  122. $len_c1 = strlen($context[1]);
  123. $len_c5 = strlen($context[5]);
  124. $len_left = $len_max - $len_c3;
  125. $len_left_even = ceil($len_left / 2);
  126. if($len_left_even > $len_c1) {
  127. $context[5] = mb_strcut($context[5], 0, $len_left - $len_c1, $blog_encoding);
  128. }
  129. elseif($len_left_even > $len_c5) {
  130. $context[1] .= "\t\t\t\t\t\t";
  131. $context[1] = mb_strcut($context[1], $len_c1 - ($len_left - $len_c5), $len_c1 + 6, $blog_encoding);
  132. $context[1] = preg_replace("/\t*$/", '', $context[1]);
  133. }
  134. else {
  135. $context[1] .= "\t\t\t\t\t\t";
  136. $context[1] = mb_strcut($context[1], $len_c1 - $len_left_even, $len_c1 + 6, $blog_encoding);
  137. $context[1] = preg_replace("/\t*$/", '', $context[1]);
  138. $context[5] = mb_strcut($context[5], 0, $len_left_even, $blog_encoding);
  139. }
  140. $excerpt = $context[1] . $context[3] . $context[5];
  141. }
  142. $commentdata['comment_content'] = '[...] ' . esc_html($excerpt) . ' [...]';
  143. $commentdata['comment_content'] = addslashes($commentdata['comment_content']);
  144. $commentdata['comment_author'] = stripslashes($commentdata['comment_author']);
  145. $commentdata['comment_author'] = $this->convenc($commentdata['comment_author'], $blog_encoding, $from_encoding);
  146. $commentdata['comment_author'] = addslashes($commentdata['comment_author']);
  147. return $commentdata;
  148. }
  149. function preprocess_comment($commentdata) {
  150. if($commentdata['comment_type'] == 'trackback')
  151. return $this->incoming_trackback($commentdata);
  152. elseif($commentdata['comment_type'] == 'pingback')
  153. return $this->incoming_pingback($commentdata);
  154. else
  155. return $commentdata;
  156. }
  157. function is_almost_ascii($string, $encoding) {
  158. if(100 === $this->conf['ascii_threshold'])
  159. return false;
  160. return ($this->conf['ascii_threshold'] < round(@(mb_strlen($string, $encoding) / strlen($string)) * 100)) ? true : false;
  161. }
  162. function wp_trim_excerpt($text) {
  163. $raw_excerpt = $text;
  164. $blog_encoding = $this->blog_encoding;
  165. if('' == $text) {
  166. $text = get_the_content('');
  167. $text = strip_shortcodes( $text );
  168. $text = apply_filters('the_content', $text);
  169. $text = str_replace(']]>', ']]&gt;', $text);
  170. $text = strip_tags($text);
  171. $excerpt_length = apply_filters('excerpt_length', $this->conf['excerpt_length']);
  172. $excerpt_mblength = apply_filters('excerpt_mblength', $this->conf['excerpt_mblength']);
  173. $excerpt_more = apply_filters('excerpt_more', $this->conf['excerpt_more']);
  174. if($this->is_almost_ascii($text, $blog_encoding)) {
  175. $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
  176. if ( count($words) > $excerpt_length ) {
  177. array_pop($words);
  178. $text = implode(' ', $words);
  179. $text = $text . $excerpt_more;
  180. } else {
  181. $text = implode(' ', $words);
  182. }
  183. }
  184. else {
  185. $text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' ');
  186. if(mb_strlen($text, $blog_encoding) > $excerpt_mblength)
  187. $text = mb_substr($text, 0, $excerpt_mblength, $blog_encoding) . $excerpt_more;
  188. }
  189. }
  190. return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
  191. }
  192. function trim_multibyte_excerpt($text = '', $length = 110, $more = ' [...]', $encoding = 'UTF-8') {
  193. $text = strip_shortcodes($text);
  194. $text = str_replace(']]>', ']]&gt;', $text);
  195. $text = strip_tags($text);
  196. $text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' ');
  197. if(mb_strlen($text, $encoding) > $length)
  198. $text = mb_substr($text, 0, $length, $encoding) . $more;
  199. return $text;
  200. }
  201. function bp_create_excerpt($text = '') {
  202. if($this->is_almost_ascii($text, $this->blog_encoding))
  203. return $text;
  204. else
  205. return $this->trim_multibyte_excerpt($text, $this->conf['bp_excerpt_mblength'], $this->conf['bp_excerpt_more'], $this->blog_encoding);
  206. }
  207. function bp_get_activity_content_body($content = '') {
  208. return preg_replace("/<a [^<>]+>([^<>]+)<\/a>(" . preg_quote($this->conf['bp_excerpt_more'], '/') . "<\/p>)$/", "$1$2", $content);
  209. }
  210. // param $excerpt could already be truncated to 20 words or less by the original get_comment_excerpt() function.
  211. function get_comment_excerpt($excerpt = '') {
  212. $excerpt = preg_replace("/\.\.\.$/", '', $excerpt);
  213. $blog_encoding = $this->blog_encoding;
  214. if($this->is_almost_ascii($excerpt, $blog_encoding)) {
  215. $words = explode(' ', $excerpt, $this->conf['comment_excerpt_length'] + 1);
  216. if(count($words) > $this->conf['comment_excerpt_length']) {
  217. array_pop($words);
  218. $excerpt = implode(' ', $words) . '...';
  219. }
  220. }
  221. elseif(mb_strlen($excerpt, $blog_encoding) > $this->conf['comment_excerpt_mblength']) {
  222. $excerpt = mb_substr($excerpt, 0, $this->conf['comment_excerpt_mblength'], $blog_encoding) . '...';
  223. }
  224. return $excerpt;
  225. }
  226. function sanitize_file_name($name) {
  227. $info = pathinfo($name);
  228. $ext = !empty($info['extension']) ? '.' . $info['extension'] : '';
  229. $name = str_replace($ext, '', $name);
  230. $name_enc = rawurlencode($name);
  231. $name = ($name == $name_enc) ? $name . $ext : md5($name) . $ext;
  232. return $name;
  233. }
  234. function excerpt_mblength($length) {
  235. if(isset($this->query_based_vars['excerpt_mblength']) && (int) $this->query_based_vars['excerpt_mblength'])
  236. return $this->query_based_vars['excerpt_mblength'];
  237. else
  238. return (int) $length;
  239. }
  240. function excerpt_more($more) {
  241. if(isset($this->query_based_vars['excerpt_more']))
  242. return $this->query_based_vars['excerpt_more'];
  243. else
  244. return $more;
  245. }
  246. function query_based_settings() {
  247. $is_query_funcs = array('is_feed', 'is_404', 'is_search', 'is_tax', 'is_front_page', 'is_home', 'is_attachment', 'is_single', 'is_page', 'is_category', 'is_tag', 'is_author', 'is_date', 'is_archive', 'is_paged');
  248. foreach($is_query_funcs as $func) {
  249. if(isset($this->conf['excerpt_mblength.' . $func]) && !isset($this->query_based_vars['excerpt_mblength']) && $func())
  250. $this->query_based_vars['excerpt_mblength'] = $this->conf['excerpt_mblength.' . $func];
  251. if(isset($this->conf['excerpt_more.' . $func]) && !isset($this->query_based_vars['excerpt_more']) && $func())
  252. $this->query_based_vars['excerpt_more'] = $this->conf['excerpt_more.' . $func];
  253. }
  254. }
  255. function filters() {
  256. // remove filter
  257. if(false !== $this->conf['patch_wp_trim_excerpt'])
  258. remove_filter('get_the_excerpt', 'wp_trim_excerpt');
  259. // add filter
  260. add_filter('preprocess_comment', array(&$this, 'preprocess_comment'), 99);
  261. add_filter('excerpt_mblength', array(&$this, 'excerpt_mblength'), 9);
  262. add_filter('excerpt_more', array(&$this, 'excerpt_more'), 9);
  263. if(false !== $this->conf['patch_incoming_pingback'])
  264. add_filter('pre_remote_source', array(&$this, 'pre_remote_source'), 10, 2);
  265. if(false !== $this->conf['patch_wp_trim_excerpt'])
  266. add_filter('get_the_excerpt', array(&$this, 'wp_trim_excerpt'));
  267. if(false !== $this->conf['patch_get_comment_excerpt'])
  268. add_filter('get_comment_excerpt', array(&$this, 'get_comment_excerpt'));
  269. if(false !== $this->conf['patch_sanitize_file_name'])
  270. add_filter('sanitize_file_name', array(&$this, 'sanitize_file_name'));
  271. if(false !== $this->conf['patch_bp_create_excerpt']) {
  272. add_filter('bp_create_excerpt', array(&$this, 'bp_create_excerpt'), 99);
  273. add_filter('bp_get_activity_content_body', array(&$this, 'bp_get_activity_content_body'), 99);
  274. }
  275. // add action
  276. add_action('wp', array(&$this, 'query_based_settings'));
  277. if(method_exists($this, 'process_search_terms') && false !== $this->conf['patch_process_search_terms'])
  278. add_action('sanitize_comment_cookies', array(&$this, 'process_search_terms'));
  279. if(method_exists($this, 'wp_mail') && false !== $this->conf['patch_wp_mail'])
  280. add_action('phpmailer_init', array(&$this, 'wp_mail'));
  281. if(method_exists($this, 'admin_custom_css') && false !== $this->conf['patch_admin_custom_css'])
  282. add_action('admin_head' , array(&$this, 'admin_custom_css'), 99);
  283. if(method_exists($this, 'word_count_js') && false !== $this->conf['patch_word_count_js'])
  284. add_action('wp_default_scripts' , array(&$this, 'word_count_js'), 9);
  285. if(method_exists($this, 'word_count_js_localize') && false !== $this->conf['patch_word_count_js'])
  286. add_action('wp_default_scripts' , array(&$this, 'word_count_js_localize'), 11);
  287. }
  288. function mbfunctions_exist() {
  289. return (
  290. function_exists('mb_convert_encoding') &&
  291. function_exists('mb_convert_kana') &&
  292. function_exists('mb_detect_encoding') &&
  293. function_exists('mb_strcut') &&
  294. function_exists('mb_strlen') &&
  295. function_exists('mb_substr')
  296. ) ? true : false;
  297. }
  298. function activation_check() {
  299. global $wp_version;
  300. $required_version = $this->required_version;
  301. if(version_compare(substr($wp_version, 0, strlen($required_version)), $required_version, '<')) {
  302. deactivate_plugins(__FILE__);
  303. wp_die(sprintf(__('Sorry, WP Multibyte Patch requires WordPress %s or later.', 'wp-multibyte-patch'), $required_version));
  304. }
  305. elseif(!$this->has_mbfunctions) {
  306. deactivate_plugins(__FILE__);
  307. wp_die(__('Sorry, WP Multibyte Patch requires mbstring functions.', 'wp-multibyte-patch'));
  308. }
  309. }
  310. function multibyte_patch() {
  311. global $wpmp_conf;
  312. $this->conf = array_merge($this->conf, $wpmp_conf);
  313. $this->blog_encoding = get_option('blog_charset');
  314. $this->has_mbfunctions = $this->mbfunctions_exist();
  315. load_textdomain($this->textdomain, plugin_dir_path(__FILE__) . $this->lang_dir . '/' . $this->textdomain . '-' . get_locale() . '.mo');
  316. register_activation_hook(__FILE__, array(&$this, 'activation_check'));
  317. $this->filters();
  318. }
  319. }
  320. if(defined('WP_PLUGIN_URL')) {
  321. global $wpmp_conf, $wpmp;
  322. $wpmp_conf = array();
  323. $wpmp_conf['base_dir'] = dirname(__FILE__);
  324. if(file_exists($wpmp_conf['base_dir'] . '/wpmp-config.php'))
  325. require_once($wpmp_conf['base_dir'] . '/wpmp-config.php');
  326. if(file_exists($wpmp_conf['base_dir'] . '/ext/' . get_locale() . '/class.php')) {
  327. if(file_exists($wpmp_conf['base_dir'] . '/ext/' . get_locale() . '/config.php'))
  328. require_once($wpmp_conf['base_dir'] . '/ext/' . get_locale() . '/config.php');
  329. require_once($wpmp_conf['base_dir'] . '/ext/' . get_locale() . '/class.php');
  330. $wpmp = new multibyte_patch_ext();
  331. }
  332. elseif(file_exists($wpmp_conf['base_dir'] . '/ext/default/class.php')) {
  333. if(file_exists($wpmp_conf['base_dir'] . '/ext/default/config.php'))
  334. require_once($wpmp_conf['base_dir'] . '/ext/default/config.php');
  335. require_once($wpmp_conf['base_dir'] . '/ext/default/class.php');
  336. $wpmp = new multibyte_patch_ext();
  337. }
  338. else
  339. $wpmp = new multibyte_patch();
  340. }
  341. ?>