/* Plugin Name: Gateway Relay Plugin URI: http://premium.wpmudev.org/project/classifieds Description: Forwards IPN from Paypal and Silent Posts from Authorizenet to multiple new urls based on a prefix in certain fields. Version: 1.0 Author: Arnold Bailey (Incsub) Author URI: http://premium.wpmudev.org License: GNU General Public License (Version 2 - GPLv2) Network: true */ /** * You must include a prefix in one of the custom fields to forward it on correctly. * Multiple url destinations per prefix can be registered. * * For Paypal 'PROFILEREFERENCE' (rp_invoice_id), 'custom','INVNUM' (invoice), 'item_number' (for subscriptions) will be checked in the IPN response. * * For Authorize Net 'x_invoice_num', 'x_po_num', 'x_cust_id' will be checked for the prefix. * * @register_gateway_relay($prefix, $url, $password) registers a prefix, the url to send to and a password you can use to authenticate. Duplicate combinations of the prefix and url are forbidden. * * @unregister_gateway_relay($prefix, $url, $password) removes a prefix, an url and password combination. */ if( ! defined('GATEWAY_RELAY_SETTINGS_NAME') ): define('GATEWAY_RELAY_SETTINGS_NAME', 'gr_settings' ); define('GATEWAY_RELAY_QUEUE_NAME', 'gr_queue' ); class Gateway_Relay{ const PAYPAL_LIVE = "https://www.paypal.com/cgi-bin/webscr"; const PAYPAL_SANDBOX = "https://www.sandbox.paypal.com/cgi-bin/webscr"; public $settings = null; public $settings_name = GATEWAY_RELAY_SETTINGS_NAME; public $queue = null; public $queue_name = GATEWAY_RELAY_QUEUE_NAME; public $postdata = ''; public $source = ''; function __construct(){ add_action( 'wp_ajax_nopriv_gateway_relay', array( &$this, 'process' ) ); add_action( 'wp_ajax_gateway_relay', array( &$this, 'process' ) ); } function process(){ //identify the source if (isset($_POST['txn_id']) ) $this->source = 'paypal'; elseif (isset($_POST['x_trans_id']) ) $this->source = 'authnet'; else return; $this->postdata = http_build_query($_POST); switch ($this->source){ //PAYPAL case'paypal': { //Check if from sandbox Class C IPs $url = ( strpos($_SERVER['REMOTE_ADDR'], '173.0.82.') === 0 ) ? self::PAYPAL_SANDBOX : self::PAYPAL_LIVE; $args = array('timeout' => 90, 'sslverify' => false ); $response = wp_remote_get( $url . "?" . $this->postdata, $args ); if(is_wp_error($response) ){ $this->write_to_log(print_r($response, true)); } else { $response = $response['body']; } if ( $response != "VERIFIED" ) { $this->write_to_log(print_r($response, true) ); die( 'not VERIFIED' ); } //Verified add to queue $this->add_to_queue($this->source, $this->postdata); break; } //AUTHORIZE NET case 'authnet': { $this->add_to_queue($this->source, $this->postdata); break; } //UNKNOWN default: { break; } } $this->write_to_log( $this->postdata, $this->source); for($i=0;$i <= count($this->queue); $i++){ $this->process_queue(); } exit; } function add_to_queue($source, $postdata, $retries = 5 ){ $this->settings = get_site_option($this->settings_name); parse_str($postdata, $params); $apps = array(); //Get the apps that match foreach($this->settings['apps'] as $key => $app){ $found = false; if($source == 'paypal'){ $found = (strpos($params['rp_invoice_id'], $app['prefix']) === 0 || strpos($params['custom'], $app['prefix']) === 0 || strpos($params['item_number'], $app['prefix']) === 0 || strpos($params['invoice'], $app['prefix']) === 0); } elseif($source == 'authnet'){ $found = (strpos($params['x_invoice_num'], $app['prefix']) === 0 || strpos($params['x_po_num'], $app['prefix']) === 0 || strpos($params['x_cust_id'], $app['prefix']) === 0); } if($found) $apps[] = $app; } $this->queue = get_site_option($this->queue_name); $item = array('source' => $source, 'postdata' => $postdata, 'retries' => $retries, 'apps' => $apps ); $this->queue[] = $item; update_site_option($this->queue_name, $this->queue); $this->queue = get_site_option($this->queue_name); } function process_queue(){ $this->settings = get_site_option($this->settings_name); $this->queue = get_site_option($this->queue_name); if(empty($this->queue) ) return; $q = array_shift($this->queue); update_site_option($this->queue_name, $this->queue); foreach($q['apps'] as $key => $app){ $args = array('timeout' => 90, 'sslverify' => false, 'body' => $q['postdata'] . "&relay_pass={$app['password']}" ); $response = wp_remote_post( $app['url'], $args ); if(is_wp_error($response) ){ $this->write_to_log(print_r($response, true)); } else { if($response['response']['code'] == 200) unset($q['apps'][$key]); } } $q['retries']--; if($q['retries'] > 0 && ! empty($q['apps']) ) { $this->queue = get_site_option($this->queue_name); $this->queue[] = $q; update_site_option($this->queue_name, $this->queue); } } function write_to_log($error, $log = 'error') { //create filename for each month $filename = __DIR__ . "/logs/{$log}_" . date('Y_m') . '.log'; //add timestamp to error $message = gmdate('[Y-m-d H:i:s] ') . $error; //write to file file_put_contents($filename, $message . "\n", FILE_APPEND); } } function register_gateway_relay($prefix = '', $url='', $password=''){ $settings = get_site_option(GATEWAY_RELAY_SETTINGS_NAME); $item = array('prefix' => $prefix, 'url' => $url, 'password' => $password ); foreach($settings['apps'] as $key => $value) { if($value['prefix'] == $item['prefix'] && $value['url'] == $item['url']) return false; } $settings['apps'][] = $item; update_site_option(GATEWAY_RELAY_SETTINGS_NAME, $settings); return true; } function unregister_gateway_relay($prefix = '', $url='', $password=''){ $settings = get_site_option(GATEWAY_RELAY_SETTINGS_NAME); $item = array('prefix' => $prefix, 'url' => $url); foreach($settings['apps'] as $key => $value) { if($value == $item) { unset($settings['apps'][$key]); update_site_option(GATEWAY_RELAY_SETTINGS_NAME, $settings); return true; } } return false; } new Gateway_Relay; endif; // Start profiling @include_once( WP_PLUGIN_DIR . '/p3-profiler/start-profile.php' ); ?> /* Plugin Name: WPMU Assist Plugin URI: http://premium.wpmudev.org/project/classifieds Description: Allows single blog registration on Multisite. On registering from a blog other than the main site it saves the blog_id and sends the emails as the originating blog and on activation adds the user to the originating blog as well as the main blog. Version: 1.0.2 Author: Arnold Bailey (Incsub) Author URI: http://premium.wpmudev.org License: GNU General Public License (Version 2 - GPLv2) Network: true */ if(! class_exists('WPMU_Assist') ): class WPMU_Assist{ public $plugin_dir; public $debug = false; function __construct(){ if(is_multisite()){ //Intercept wpmu_signup so user can be added to the add_filter('wpmu_signup_user_notification', array(&$this, 'on_signup_user'), 1, 4); add_filter('wpmu_signup_blog_notification', array(&$this, 'on_signup_blog'), 1, 7); add_filter('wpmu_activate_user', array(&$this, 'on_activate_user'), 10, 3); add_filter('wpmu_activate_blog', array(&$this, 'on_activate_blog'), 10, 5); add_filter('wp_loaded', array(&$this, 'on_template_redirect') ); $this->plugin_dir = plugin_dir_path(__FILE__); } if(defined('WPMU_ASSIST_DEBUG') && WPMU_ASSIST_DEBUG ) $this->debug = true; } /** * Traps the blog id that a wp-login.php registration is coming from so the user can be added to the blog. * For a wp-signup registration you must set the transient in the calling form. */ function on_template_redirect(){ global $blog_id; if ($this->debug) $this->write_to_log('REQUEST_URI:' . $_SERVER['REQUEST_URI'] ); //Save the incoming $blog_id for wp-signup.php if ($this->debug) $this->write_to_log('SCRIPT_NAME:' . $_SERVER['SCRIPT_NAME'] ); if($_SERVER['SCRIPT_NAME'] == '/wp-signup.php' || $_SERVER['SCRIPT_NAME'] == '/wp-login.php' || $_SERVER['REQUEST_URI'] == '/register/' ) { if($blog_id > 1) { if ($this->debug) $this->write_to_log('Transient: register_blog_id_'.$_SERVER['REMOTE_ADDR'] . $blog_id ); set_site_transient('register_blog_id_'.$_SERVER['REMOTE_ADDR'], $blog_id, 60 * 60 ); } } if ($this->debug) $this->write_to_log('Blog:' . $blog_id ); } function on_signup_user($user, $user_email, $key, $meta){ global $wpdb; // Get the saved $blog_id $blogid = get_site_transient('register_blog_id_'.$_SERVER['REMOTE_ADDR']); $blogid = empty($blogid) ? 1 : $blogid; delete_site_transient('register_blog_id_'.$_SERVER['REMOTE_ADDR']); $meta = maybe_unserialize($meta); if($blogid > 1){ $meta = serialize(array_merge( $meta, array('register_blog_id' => $blogid) ) ); // Save the originating blog id $wpdb->update( $wpdb->signups, array('meta' => $meta ), array('activation_key' => $key) ); // update the db switch_to_blog($blogid); //So it will send mail under the blogs domain } if ($this->debug) $this->write_to_log('Signup:' . print_r($meta, true) ); return ( ! file_exists('bp_core_activation_signup_user_notification') ); } function on_signup_blog( $domain, $path, $title, $user, $user_email, $key, $meta){ return $this->on_signup_user($user, $user_email, $key, $meta); } function on_activate_user($user_id, $password, $meta){ $default_role = get_option('default_role'); $blogid = empty($meta['register_blog_id']) ? 1 : intval($meta['register_blog_id']); add_user_to_blog( 1, $user_id, $default_role); add_user_to_blog($blogid, $user_id, $default_role); if ($this->debug) $this->write_to_log('Activate:' . print_r($meta, true) ); } function on_activate_blog($blog_id, $user_id, $password, $title, $meta){ $this->on_activate_user($user_id, $password, $meta); } function write_to_log($error, $log = 'wpmu-assist') { //create filename for each month $filename = $this->plugin_dir . "{$log}_" . date('Y_m') . '.log'; //add timestamp to error $message = gmdate('[Y-m-d H:i:s] ') . "\n" . $error; //write to file file_put_contents($filename, $message . "\n", FILE_APPEND); } } new WPMU_Assist; endif;