PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/Doduo_1.1/WebKitSite/blog/wp-mail.php

https://github.com/weissms/owb-mirror
PHP | 176 lines | 133 code | 37 blank | 6 comment | 34 complexity | 14591b7ab6f6c407473149fce9cb6b9b MD5 | raw file
  1. <?php
  2. require(dirname(__FILE__) . '/wp-config.php');
  3. require_once(ABSPATH.WPINC.'/class-pop3.php');
  4. error_reporting(2037);
  5. $time_difference = get_option('gmt_offset') * 3600;
  6. $phone_delim = '::';
  7. $pop3 = new POP3();
  8. if (!$pop3->connect(get_option('mailserver_url'), get_option('mailserver_port')))
  9. wp_die(wp_specialchars($pop3->ERROR));
  10. $count = $pop3->login(get_option('mailserver_login'), get_option('mailserver_pass'));
  11. if (0 == $count) wp_die(__('There doesn&#8217;t seem to be any new mail.'));
  12. for ($i=1; $i <= $count; $i++) :
  13. $message = $pop3->get($i);
  14. $content = '';
  15. $content_type = '';
  16. $content_transfer_encoding = '';
  17. $boundary = '';
  18. $bodysignal = 0;
  19. $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  20. foreach ($message as $line) :
  21. if (strlen($line) < 3) $bodysignal = 1;
  22. if ($bodysignal) {
  23. $content .= $line;
  24. } else {
  25. if (preg_match('/Content-Type: /i', $line)) {
  26. $content_type = trim($line);
  27. $content_type = substr($content_type, 14, strlen($content_type)-14);
  28. $content_type = explode(';', $content_type);
  29. $content_type = $content_type[0];
  30. }
  31. if (preg_match('/Content-Transfer-Encoding: /i', $line)) {
  32. $content_transfer_encoding = trim($line);
  33. $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding)-14);
  34. $content_transfer_encoding = explode(';', $content_transfer_encoding);
  35. $content_transfer_encoding = $content_transfer_encoding[0];
  36. }
  37. if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == '')) {
  38. $boundary = trim($line);
  39. $boundary = explode('"', $boundary);
  40. $boundary = $boundary[1];
  41. }
  42. if (preg_match('/Subject: /i', $line)) {
  43. $subject = trim($line);
  44. $subject = substr($subject, 9, strlen($subject)-9);
  45. $subject = wp_iso_descrambler($subject);
  46. // Captures any text in the subject before $phone_delim as the subject
  47. $subject = explode($phone_delim, $subject);
  48. $subject = $subject[0];
  49. }
  50. // Set the author using the email address (From or Reply-To, the last used)
  51. // otherwise use the site admin
  52. if ( preg_match('/(From|Reply-To): /', $line) ) {
  53. if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
  54. $author = $matches[0];
  55. else
  56. $author = trim($line);
  57. $author = sanitize_email($author);
  58. if ( is_email($author) ) {
  59. echo "Author = {$author} <p>";
  60. $author = $wpdb->escape($author);
  61. $result = $wpdb->get_row("SELECT ID FROM $wpdb->users WHERE user_email='$author' LIMIT 1");
  62. if (!$result)
  63. $post_author = 1;
  64. else
  65. $post_author = $result->ID;
  66. } else
  67. $post_author = 1;
  68. }
  69. if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
  70. $ddate = trim($line);
  71. $ddate = str_replace('Date: ', '', $ddate);
  72. if (strpos($ddate, ',')) {
  73. $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
  74. }
  75. $date_arr = explode(' ', $ddate);
  76. $date_time = explode(':', $date_arr[3]);
  77. $ddate_H = $date_time[0];
  78. $ddate_i = $date_time[1];
  79. $ddate_s = $date_time[2];
  80. $ddate_m = $date_arr[1];
  81. $ddate_d = $date_arr[0];
  82. $ddate_Y = $date_arr[2];
  83. for ($j=0; $j<12; $j++) {
  84. if ($ddate_m == $dmonths[$j]) {
  85. $ddate_m = $j+1;
  86. }
  87. }
  88. $time_zn = intval($date_arr[4]) * 36;
  89. $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
  90. $ddate_U = $ddate_U - $time_zn;
  91. $post_date = gmdate('Y-m-d H:i:s', $ddate_U + $time_difference);
  92. $post_date_gmt = gmdate('Y-m-d H:i:s', $ddate_U);
  93. }
  94. }
  95. endforeach;
  96. $subject = trim($subject);
  97. if ($content_type == 'multipart/alternative') {
  98. $content = explode('--'.$boundary, $content);
  99. $content = $content[2];
  100. $content = explode('Content-Transfer-Encoding: quoted-printable', $content);
  101. $content = strip_tags($content[1], '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
  102. }
  103. $content = trim($content);
  104. if (stripos($content_transfer_encoding, "quoted-printable") !== false) {
  105. $content = quoted_printable_decode($content);
  106. }
  107. // Captures any text in the body after $phone_delim as the body
  108. $content = explode($phone_delim, $content);
  109. $content[1] ? $content = $content[1] : $content = $content[0];
  110. $content = trim($content);
  111. $post_content = apply_filters('phone_content', $content);
  112. $post_title = xmlrpc_getposttitle($content);
  113. if ($post_title == '') $post_title = $subject;
  114. if (empty($post_categories)) $post_categories[] = get_option('default_email_category');
  115. $post_category = $post_categories;
  116. // or maybe we should leave the choice to email drafts? propose a way
  117. $post_status = 'publish';
  118. $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
  119. $post_data = add_magic_quotes($post_data);
  120. $post_ID = wp_insert_post($post_data);
  121. if ( is_wp_error( $post_ID ) )
  122. echo "\n" . $post_ID->get_error_message();
  123. if (!$post_ID) {
  124. // we couldn't post, for whatever reason. better move forward to the next email
  125. continue;
  126. }
  127. do_action('publish_phone', $post_ID);
  128. echo "\n<p><b>Author:</b> " . wp_specialchars($post_author) . "</p>";
  129. echo "\n<p><b>Posted title:</b> " . wp_specialchars($post_title) . "<br />";
  130. if(!$pop3->delete($i)) {
  131. echo '<p>Oops '.wp_specialchars($pop3->ERROR).'</p></div>';
  132. $pop3->reset();
  133. exit;
  134. } else {
  135. echo "<p>Mission complete, message <strong>$i</strong> deleted.</p>";
  136. }
  137. endfor;
  138. $pop3->quit();
  139. ?>