') ) { // Get the title, original URL, and Facebook URL for each note $notenumber = $notenumber + 1; //James 0.21 removed space from note title $line = preg_replace("/.*?
/", '($1)', $line); $line = preg_replace("/<\/a><\/div>.*/", "", $line); $tmp = preg_split("/[()]/", $line); $url[$notenumber] = $tmp[1]; $line = preg_replace("/\(.*?\)/", "", $line); $title[$notenumber] = $line; $tmp = explode("?", $url[$notenumber]); $url2 = $tmp[1]; } elseif ( strstr($line, "note_redirect.php?") && strstr($line, "View original post") && $wpURL[$notenumber] == "" ) { $line = preg_replace("/.*?note_redirect.php?([^\"]+)\".*/", "$1", $line); $line = urldecode($line); $wpURL[$notenumber] = preg_replace("/.*?url=([^\"]+)\&h.*/", "$1", $line); if ($verbose) { print "Checking for comments on $wpURL[$notenumber]...
\n"; } } elseif ( preg_match("/#comments\">[0-9]+ comment/", $line) ) { // This entry has comments. Perhaps we should follow its link! $getcomments[$notenumber] = "true"; $l2 = facebook_get_url("http://facebook.com/".$url[$notenumber], $challenge); //James: Facebook now seems to not have line feeds, so split by sometime else //$l2 = explode("\n", $l2); $l2 = explode('', $l2); foreach ($l2 as $line2) { //James: Facebook now seems to have more than one space after the profile link //if ( preg_match ("/class=\"profile_link\" >.*? wrote/", $line2) ) { if ( preg_match ("/class=\"profile_link\" *>.*? wrote/", $line2) ) { $lines2 = explode("/td>.*? wrote/", $l3) ) { if ( preg_match ("/class=\"profile_link\" *>.*? wrote/", $l3) ) { $l3 = str_replace("\n", "", $l3); //James: changed & added because I couldn't be bothered to hack around with the regex anymore $pos = strrpos($l3, ""); if ($pos >= 0) { $l3 = substr($l3, 0, $pos); } //$comment_author_url = preg_replace("/.*.*/", '$1', $l3); $comment_author_url = preg_replace("/.*.*/", '$1', $l3); // Thomas Albright edited this line: // Original line: //$comment_author = preg_replace("/.*(.*?)<\/a> wrote.*/", '$1', $l3); // Edited line: //$comment_author = preg_replace("/.*(.*?)<\/a>[^<]+wrote.*/", '$1', $l3); //James: changed further to $comment_author = preg_replace("/.*(.*?)<\/a>[^<]+wrote.*/", '$1', $l3); $when = preg_replace("/.* wrote
(.*?)<\/small>.*/", '$1', $l3); $comment_content = preg_replace("/.*
(.*?)<\/div>.*/", '$1', $l3); $comment_author = $comment_author . " " . get_option('facebook_add_to_author'); $when = str_replace("at ", "", $when); $when = str_replace("on ", "", $when); $epochtime = strtotime($when); // Check if this comment exists already $addcomment = 1; global $wpdb; $thisquery = "SELECT A.ID FROM wp_posts A WHERE A.guid LIKE '$wpURL[$notenumber]'"; $comment_post_ID = $wpdb->get_var($thisquery); if ($verbose) { print "    Comment found for post $comment_post_ID... "; } $doescommentexist = $wpdb->get_var("SELECT count(A.comment_post_ID) FROM wp_comments A WHERE A.comment_post_ID = '$comment_post_ID' AND A.comment_approved = '1' AND A.comment_author LIKE '$comment_author'"); if ($doescommentexist) { // All that the above SQL tells us is that this person already has a comment for this entry in the database. However, we don't know whether they commented more than once on this particular note/entry. So, we've got a bit more work to do. //James 0.21 this was broken in the original, get_row only returns one row, so it wouldn't work if someone comments more than once. //$thesecomments = $wpdb->get_row("SELECT A.comment_content FROM wp_comments A WHERE A.comment_post_ID = '$comment_post_ID' AND A.comment_approved = '1' AND A.comment_author LIKE '$comment_author'", ARRAY_N); $thesecomments = $wpdb->get_col("SELECT A.comment_content FROM wp_comments A WHERE A.comment_post_ID = '$comment_post_ID' AND A.comment_approved = '1' AND A.comment_author LIKE '$comment_author'",0); foreach ($thesecomments as $thiscomment) { // We want to strip down the comments, both from the database and from Facebook, to the bare minimum and then compare them. So no HTML, no whitespace, no anything that isn't A-Z, 0-9, or underscore. $tc = urldecode($thiscomment); $tc = preg_replace("/<[^>]*>/", "", $tc); $tc = preg_replace("/\W/", "", $tc); $fc = urldecode($comment_content); $fc = preg_replace("/<[^>]*>/", "", $fc); $fc = preg_replace("/\W/", "", $fc); if (strcmp($tc, $fc) == 0) { // If these are the same comment, don't add it again. if ($verbose) { print "already exists in Wordpress database."; } $addcomment = 0; } } } if ($addcomment == 1) { if ($verbose) { print "not in Wordpress database. Adding to Wordpress..."; } // Insert the comment if (get_option('facebook_fake_author_email')) { $comment_author_email = get_option('facebook_fake_author_email'); } else { $comment_author_email = $email; } $comment_date = date("Y-m-d H:i:s", $epochtime); $comment_date_gmt = gmdate("Y-m-d H:i:s", $epochtime); $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_date'); $comment_ID = facebook_new_comment ($commentdata); if ($verbose) { print "OK!"; } } // Done adding the comment now. if ($verbose) { print "
\n"; } } // end "if line contains profile_link" } // next table cell } // end "if line contains profile_link" } // next line of file } // Done with this note } // Next line of file // Turn off buffering ob_end_flush(); return; } function facebook_activate() { // Add options add_option('facebook_add_to_author', " (via Facebook)", "Text to append to the comment author's name"); add_option('facebook_auto_freq', "86400", "How often to automatically check Facebook to import comments"); // All other options are empty by default and so we'll add them when they have data, later. // Schedule if (!wp_next_scheduled('import_facebook_comments_hook')) { $freq = get_option('facebook_auto_freq'); if ($freq == 86400) { // Daily wp_schedule_event( time(), 'daily', 'import_facebook_comments_hook' ); } elseif ($freq == 3600) { // Hourly wp_schedule_event( time(), 'hourly', 'import_facebook_comments_hook' ); } else { // Turn off automatic checking. } } } function get_facebook_auth() { add_submenu_page("options-general.php", "Facebook", "Facebook", 5, "facebooknotes.php", facebook_options_subpanel); } function facebook_options_subpanel() { if (! get_option('facebook_add_to_author')) { } if (isset($_POST['manual_run'])) { update_option('facebook_plugin_verbose', '1'); print '

'; print "Manually importing comments...

\n"; import_facebook_comments(); print '

'; update_option('facebook_plugin_verbose', ''); } if (isset($_POST['info_update'])) {?>