/* Plugin Name: WP -> GF (PRO) Plugin URI: http://gossamer-threads.com Description: Publish your posts on GF. Version: 1.0 Author: Gossamer Threads Author URI: http://gossamer-threads.com */ // Constants if ( !defined('WP2GF_DBSERVER') ) define('WP2GF_DBSERVER', 'localhost'); if ( !defined('WP2GF_DBNAME') ) define('WP2GF_DBNAME', 'mydb_gforum'); if ( !defined('WP2GF_DBUSERNAME') ) define('WP2GF_DBUSERNAME', 'gforum'); if ( !defined('WP2GF_DBPASSWORD') ) define('WP2GF_DBPASSWORD', 'gforum'); if ( !defined('WP2GF_DEFAULT_PUBLISHER_USERID') ) define('WP2GF_DEFAULT_PUBLISHER_USERID', 2); if ( !defined('WP2GF_DEFAULT_PUBLISHER_USERNAME') ) define('WP2GF_DEFAULT_PUBLISHER_USERNAME', 'Publisher'); if ( !defined('WP2GF_INFOWARNING_USERINFOEMPTY') ) define('WP2GF_INFOWARNING_USERINFOEMPTY', 'User Info can not be empty, using default instead'); if ( !defined('WP2GF_DBWARNING_FORUMLIST') ) define('WP2GF_DBWARNING_FORUMLIST', 'Can not fetch the forum list'); if ( !defined('WP2GF_DBERROR_HANDLE') ) define('WP2GF_DBERROR_HANDLE', 'Can not connect to DB'); if ( !defined('WP2GF_DBERROR_UNIQUEID') ) define('WP2GF_DBERROR_UNIQUEID', 'Can not create unique id for the new post'); if ( !defined('WP2GF_DBERROR_INSERTPOST') ) define('WP2GF_DBERROR_INSERTPOST', 'Can not insert new post into the DB'); if ( !defined('WP2GF_DBERROR_INSERTEDID') ) define('WP2GF_DBERROR_INSERTEDID', 'Can not determine the ID of the new inserted post'); if ( !defined('WP2GF_DBERROR_UPDATEFORUM') ) define('WP2GF_DBERROR_UPDATEFORUM', 'Can not update the forum post counter'); if ( !defined('WP2GF_DBERROR_UPDATEFORUM') ) define('WP2GF_DBERROR_UPDATEUSERPOSTS', 'Can not update the user post counter'); if ( !defined('WP2GF_DBERROR_INTEGRITY') ) define('WP2GF_DBERROR_INTEGRITY', 'Can not delete the new post after failing to update the post counter'); /* * FUNCTIONS */ function wp2gf_get_settings() { $settings = unserialize(get_option('wp2gf_settings')); if( !$settings ) { $settings['publish'] = false; $settings['forumid'] = 1; $settings['userid'] = WP2GF_DEFAULT_PUBLISHER_USERID; $settings['username'] = WP2GF_DEFAULT_PUBLISHER_USERNAME; add_option('wp2gf_settings', serialize($settings)); } return $settings; } function wp2gf_save_post_settings( $settings ) { $settings['publish'] = $_POST['wp2gf_publish']?true:false; $settings['forumid'] = $_POST['wp2gf_forumid']; $settings['userid'] = ltrim($_POST['wp2gf_userid'])!=''?$_POST['wp2gf_userid']:WP2GF_DEFAULT_PUBLISHER_USERID; $settings['username'] = ltrim($_POST['wp2gf_username'])!=''?$_POST['wp2gf_username']:WP2GF_DEFAULT_PUBLISHER_USERNAME; if( ltrim($_POST['wp2gf_userid'])=='' || ltrim($_POST['wp2gf_username'])=='' ) $settings['warning'] = WP2GF_INFOWARNING_USERINFOEMPTY; update_option('wp2gf_settings', serialize($settings)); } // Adds a custom section to the "advanced" Post and Page edit screens function wp2gf_add_custom_box() { if( function_exists( 'add_meta_box' )) { add_meta_box( 'wp2gf_sectionid', 'Publish on GForum', 'wp2gf_inner_custom_box', 'post', 'advanced' ); add_meta_box( 'wp2gf_sectionid', 'Publish on GForum', 'wp2gf_inner_custom_box', 'page', 'advanced' ); } else { add_action( 'dbx_post_advanced', 'wp2gf_old_custom_box' ); add_action( 'dbx_page_advanced', 'wp2gf_old_custom_box' ); } } // Prints the inner fields for the custom post/page section function wp2gf_inner_custom_box() { global $wp2gf_settings, $wp2gf_forumlist; // Use nonce for verification echo ''; // The actual fields for data entry echo '

'; $checked = $wp2gf_settings['publish']?' checked="checked"':''; echo ''; echo '

'; echo '

Publisher Info on GForum:

'; echo '

UserName UserID

'; echo 'Please note: It is your responsibility to provide the correct userid and username.'; } // Prints the edit form for pre-WordPress 2.5 post/page function wp2gf_old_custom_box() { echo '
' . "\n"; echo '
' . "\n"; echo '

' . __( 'My Post Section Title', 'wp2gf_textdomain' ) . "

"; echo '
'; // output editing form wp2fg_inner_custom_box(); // end wrapper echo "
\n"; } function wp2gf_add_notices() { global $wp2gf_settings; if( $wp2gf_settings['error'] ) { echo '
The following error occured while trying to connect to GForum: '.$wp2gf_settings['error'].'.
'; unset($wp2gf_settings['error']); } if( $wp2gf_settings['warning'] ) { echo '
The following warning occured while trying to connect to GForum: '.$wp2gf_settings['warning'].'.
'; unset($wp2gf_settings['warning']); } if( $wp2gf_settings['success'] ) { echo '
Post published successfully on GForum.
'; unset($wp2gf_settings['success']); } update_option('wp2gf_settings', serialize($wp2gf_settings)); } function wp2gf_do_publish() { global $wp2gf_db_handle, $wp2gf_settings; $time = mktime($_POST['hh'],$_POST['mn'],$_POST['ss'],$_POST['mm'],$_POST['jj'],$_POST['aa']); $hash = md5($time . rand(1, 16000)); $sql = "SELECT post_unique_id FROM Post WHERE post_unique_id = '$hash'"; $resultset = mysql_query($sql, $wp2gf_db_handle); if( !$resultset ) { $wp2gf_settings['error'] = WP2GF_DBERROR_UNIQUEID; } else { while( $row = mysql_fetch_row($resultset)) { $hash = md5($time . rand(1, 16000)); $sql = "SELECT post_unique_id FROM Post WHERE post_unique_id = '$hash'"; $resultset = mysql_query($sql, $wp2gf_db_handle); if( !$resultset ) { $wp2gf_settings['error'] = WP2GF_DBERROR_UNIQUEID; } } } $post_latest_reply = 2000000000 - $time; if( $resultset ) { $sql = "INSERT INTO Post " . "(user_id_fk, post_unique_id, post_username, forum_id_fk, post_time, post_latest_reply, post_latest_poster, post_subject, post_message, post_style, post_ip, post_locked)" . " VALUES " . "(".$wp2gf_settings['userid'].", '$hash', '".$wp2gf_settings['username']."', $_POST[wp2gf_forumid], $time, $post_latest_reply, '".$wp2gf_settings['username']."', '$_POST[post_title]', '$_POST[content]', 3, '$_SERVER[REMOTE_ADDR]', 0)"; if ( !mysql_query($sql, $wp2gf_db_handle) ) { $wp2gf_settings['error'] = WP2GF_DBERROR_INSERTPOST; } else { $last_inserted_id = mysql_insert_id($wp2gf_db_handle); if( $last_inserted_id ) { $sql = "UPDATE Forum SET forum_total = forum_total + 1, forum_total_threads = forum_total_threads + 1, " . "forum_last = $time, forum_last_poster = '".$wp2gf_settings['username']."', forum_last_id = $last_inserted_id " . "WHERE forum_id = $_POST[wp2gf_forumid]"; if ( !mysql_query($sql, $wp2gf_db_handle) ) { $wp2gf_settings['error'] = WP2GF_DBERROR_UPDATEFORUM; $sql = "DELETE FROM Post WHERE post_unique_id = '$hash'"; if ( !mysql_query($sql, $wp2gf_db_handle) ) { $wp2gf_settings['error'] = WP2GF_DBERROR_INTEGRITY; } } $sql = "UPDATE User SET user_posts = user_posts + 1 WHERE user_id = ".$wp2gf_settings['userid']; if ( !mysql_query($sql, $wp2gf_db_handle) ) { $wp2gf_settings['error'] = WP2GF_DBERROR_UPDATEUSERPOSTS; $sql = "DELETE FROM Post WHERE post_unique_id = '$hash'"; if ( !mysql_query($sql, $wp2gf_db_handle) ) { $wp2gf_settings['error'] = WP2GF_DBERROR_INTEGRITY; } } } else { $wp2gf_settings['error'] = WP2GF_DBERROR_INSERTEDID; } } } if( !$wp2gf_settings['error'] ) $wp2gf_settings['success'] = true; update_option('wp2gf_settings', serialize($wp2gf_settings)); } function wp2gf_get_db_handle( $settings ) { $handle = mysql_connect(WP2GF_DBSERVER, WP2GF_DBUSERNAME, WP2GF_DBPASSWORD, true); if ( !mysql_select_db(WP2GF_DBNAME, $handle) ) { $settings['error'] = WP2GF_DBERROR_HANDLE; $handle = null; } return $handle; } function wp2gf_get_forumlist( $handle, $settings ) { $sql = "SELECT forum_id, forum_name from Forum ORDER BY forum_id asc"; $resultset = mysql_query($sql, $handle); if( !$resultset ) { $settings['warning'] = WP2GF_DBWARNING_FORUMLIST; $results = null; } else { while( $row = mysql_fetch_array($resultset, MYSQL_ASSOC) ) $results[] = $row; } return $results; } /* * MAIN */ $wp2gf_settings = wp2gf_get_settings(); if( $_POST['wp2gf_forumid'] ) { wp2gf_save_post_settings( & $wp2gf_settings ); } $wp2gf_db_handle = wp2gf_get_db_handle( & $wp2gf_settings ); if( $wp2gf_db_handle ) { $wp2gf_forumlist = wp2gf_get_forumlist( $wp2gf_db_handle, & $wp2gf_settings ); if( is_array($wp2gf_forumlist) ) { add_action('admin_menu', 'wp2gf_add_custom_box'); } if( $_POST['wp2gf_publish'] && $_POST['wp2gf_forumid'] && $wp2gf_db_handle ) { // Hook to the draft_to_publish trigger add_action('draft_to_publish', 'wp2gf_do_publish'); // Hook to the new_to_publish(quickpress) trigger add_action('new_to_publish', 'wp2gf_do_publish'); } } if( $wp2gf_settings['error'] || $wp2gf_settings['warning'] || $wp2gf_settings['success'] ) { add_action('admin_notices', 'wp2gf_add_notices'); } ?>