more. Click here for instructions. Email us at post@linksalpha.com if you have any queries.
Version: 3.0.0
Author: LinksAlpha
Author URI: http://www.linksalpha.com
*/
/*
Copyright (C) 2010 LinksAlpha.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
define('NETWORKPUB_WP_PLUGIN_URL', networkpub_get_plugin_dir());
define('NETWORKPUB_WIDGET_NAME', 'Network Publisher');
define('NETWORKPUB_WIDGET_NAME_INTERNAL', 'networkpub');
define('NETWORKPUB_WIDGET_PREFIX', 'networkpub');
define('NETWORKPUB', 'Automatically publish your blog posts to 30 Social Networks including Facebook, Twitter, LinkedIn, Yahoo, Yammer, MySpace, Identi.ca');
define('NETWORKPUB_ERROR_INTERNAL', 'internal error');
define('NETWORKPUB_ERROR_INVALID_URL', 'invalid url');
define('NETWORKPUB_ERROR_INVALID_KEY', 'invalid key');
$networkpub_settings['api_key'] = array('label'=>'API Key:', 'type'=>'text', 'default'=>'');
$networkpub_settings['id'] = array('label'=>'id', 'type'=>'text', 'default'=>'');
$options = get_option(NETWORKPUB_WIDGET_NAME_INTERNAL);
function networkpub_init() {
wp_enqueue_style('thickbox');
wp_enqueue_script('jquery');
wp_enqueue_script('thickbox');
wp_register_script('networkpubjs', NETWORKPUB_WP_PLUGIN_URL .'networkpub.js');
wp_enqueue_script('networkpubjs');
wp_register_style('networkpubcss', NETWORKPUB_WP_PLUGIN_URL . 'networkpub.css');
wp_enqueue_style('networkpubcss');
add_action('admin_menu', 'networkpub_pages');
add_action('activate_{$plugin}', 'networkpub_pushpresscheck');
add_action("activated_plugin", "networkpub_pushpresscheck");
}
if ( is_admin() ) {
add_action('init', 'networkpub_init');
add_action('init', 'networkpub_remove');
register_activation_hook( __FILE__, 'networkpub_activate' );
add_action('admin_notices', 'networkpub_warning');
}
add_action('{$new_status}_{$post->post_type}', 'networkpub_ping');
add_action('publish_post', 'networkpub_ping');
add_action('future_to_publish', 'networkpub_ping');
function networkpub_activate() {
$networkpub_eget = get_bloginfo('admin_email'); $networkpub_uget = get_bloginfo('url'); $networkpub_nget = get_bloginfo('name');
$networkpub_dget = get_bloginfo('description'); $networkpub_cget = get_bloginfo('charset'); $networkpub_vget = get_bloginfo('version');
$networkpub_lget = get_bloginfo('language'); $link='http://www.linksalpha.com/a/bloginfo';
$networkpub_bloginfo = array('email'=>$networkpub_eget, 'url'=>$networkpub_uget, 'name'=>$networkpub_nget, 'desc'=>$networkpub_dget, 'charset'=>$networkpub_cget, 'version'=>$networkpub_vget, 'lang'=>$networkpub_lget, 'plugin'=>'nw');
networkpub_http_post($link, $networkpub_bloginfo);
}
function networkpub_warning() {
$options = get_option(NETWORKPUB_WIDGET_NAME_INTERNAL);
if(empty($options['api_key'])) {
if (!isset($_POST['submit'])) {
echo "
".__('Network Publisher plugin is almost ready.')." ".
sprintf(__('You must enter API key (under Plugins->Network Publisher) for automatic posting of your blog articles to 20+ Social Networks including Twitter, Facebook Profile, Facebook Pages, LinkedIn, MySpace, Yammer, Yahoo, Identi.ca, etc. to work.'),
"plugins.php?page=networkpub")."
";
}
}
}
function networkpub_ping($id) {
if(!$id) {
return FALSE;
}
$options = get_option(NETWORKPUB_WIDGET_NAME_INTERNAL);
$link = 'http://www.linksalpha.com/a/ping?id='.$options['id'];
$response_full = networkpub_http($link);
$response_code = $response_full[0];
if ($response_code === 200) {
return TRUE;
}
return FALSE;
}
function networkpub_pages() {
if ( function_exists('add_submenu_page') ) {
$page = add_submenu_page('plugins.php', NETWORKPUB_WIDGET_NAME, NETWORKPUB_WIDGET_NAME, 'manage_options', 'networkpub', 'networkpub_conf');
}
}
function networkpub_conf() {
global $networkpub_settings;
if ( isset($_POST['submit']) ) {
if ( function_exists('current_user_can') && !current_user_can('manage_options') ) {
die(__('Cheatin’ uh?'));
}
$field_name = sprintf('%s_%s', NETWORKPUB_WIDGET_PREFIX, 'api_key');
$value = strip_tags(stripslashes($_POST[$field_name]));
if($value) {
$networkadd = networkpub_add($value);
}
}
$options = get_option(NETWORKPUB_WIDGET_NAME_INTERNAL);
$html = '
Network Publisher makes it easy and painless to Publish your Blog Posts to Social Networks. All you need to do is connect to your Social Networks at
LinksAlpha.com, grab API Key for each enabled Network and enter it below. Once setup, your Blog posts content appears on the social networks as soon as you hit the Publish button.
Note: If you decide to stop using this plugin permanently, pls also remove your blog URL by logging into
http://www.linksalpha.com/publish.Otherwise, your blog posts may continue to get posted even after you remove this plugin.
';
echo $html;
}
function networkpub_add($api_key) {
if (!$api_key) {
echo 'Error occured while processing your request. Error Code 100
';
return FALSE;
}
$url = get_bloginfo('url');
$desc = get_bloginfo('description');
if (!$url) {
$errdesc = networkpub_error_msgs('invalid url');
echo $errdesc;
return FALSE;
}
$url_parsed = parse_url($url);
$url_host = $url_parsed['host'];
if( substr_count($url, 'localhost') or strpos($url_host, '192.168.') === 0 or (strpos($url_host, '172.') === 0 and (int)substr($url_host, 4, 2) > 15 and (int)substr($url_host, 4, 2) < 32 ) or strpos($url_host, '10.') === 0 ) {
$errdesc = networkpub_error_msgs('localhost url');
echo $errdesc;
return FALSE;
}
$link = 'http://www.linksalpha.com/a/networkpubadd';
$params = array('url'=>urlencode($url), 'key'=>$api_key, 'plugin'=>'nw');
$response_full = networkpub_http_post($link,$params);
$response_code = $response_full[0];
if ($response_code != 200) {
$errdesc = networkpub_error_msgs($response_full[1]);
echo $errdesc;
return FALSE;
}
$response = networkpub_json_decode($response_full[1]);
if ($response->errorCode > 0) {
$errdesc = networkpub_error_msgs($response->errorMessage);
echo $errdesc;
return FALSE;
}
$options = get_option(NETWORKPUB_WIDGET_NAME_INTERNAL);
if(empty($options['api_key'])) {
$options['api_key'] = $api_key;
} else {
$options_array = explode(',', $options['api_key']);
if(!in_array($api_key, $options_array)) {
$options['api_key'] = $options['api_key'].','.$api_key;
}
}
$options['id'] = $response->results->id;
update_option(NETWORKPUB_WIDGET_NAME_INTERNAL, $options);
echo 'API Key has been added successfully
';
return TRUE;
}
function networkpub_load() {
$options = get_option(NETWORKPUB_WIDGET_NAME_INTERNAL);
if (empty($options['api_key'])) {
$html = 'You have not added any API Key
';
return $html;
}
$link = 'http://www.linksalpha.com/a/networkpubget';
$body = array('key'=>$options['api_key'], 'version'=>2);
$response_full = networkpub_http_post($link, $body);
$response_code = $response_full[0];
if ($response_code != 200) {
$errdeschtml = networkpub_error_msgs($response_full[1]);
return $errdeschtml;
}
$response = networkpub_json_decode($response_full[1]);
if($response->errorCode > 0) {
$html = 'Error occured while trying to load the API Keys. Please try again later.
';
return $html;
}
$html = 'You are currently Publishing your Blog to '.count($response->results).' Social Networks
';
$html .= '| API Key | Network | Options | Remove |
';
if (count($response->results)) {
foreach($response->results as $row) {
$html .= '';
$html .= '| '.$row->api_key.' | '.$row->name.' | ';
$html .= 'Options | ';
$html .= 'Remove | ';
$html .= '
';
}
} else {
$html .= '| No API Keys have been added |
';
}
$html .= '
';
return $html;
}
function networkpub_remove() {
$options = get_option(NETWORKPUB_WIDGET_NAME_INTERNAL);
if (!empty($_POST['key'])) {
$key_full = $_POST['key'];
$key_only = substr($key_full, 4);
$link = 'http://www.linksalpha.com/a/networkpubremove?id='.$options['id'].'&key='.$key_only;
$response_full = networkpub_http($link);
$response_code = $response_full[0];
if ($response_code != 200) {
$errdesc = networkpubnw_error_msgs($response_full[1]);
echo $errdesc;
return;
}
$api_key = $options['api_key'];
$api_key_array = explode(',', $api_key);
$loc = array_search($key_only, $api_key_array);
if($loc !== FALSE) {
unset($api_key_array[$loc]);
}
$api_key = implode(",", $api_key_array);
$options['api_key'] = $api_key;
update_option(NETWORKPUB_WIDGET_NAME_INTERNAL, $options);
echo $key_full;
return;
}
}
function networkpub_json_decode($str) {
if (function_exists("json_decode")) {
return json_decode($str);
} else {
if (!class_exists('Services_JSON')) {
require_once("JSON.php");
}
$json = new Services_JSON();
return $json->decode($str);
}
}
function networkpub_http($link) {
if (!$link) {
return array(500, 'invalid url');
}
require_once(ABSPATH.WPINC.'/class-snoopy.php');
$snoop = new Snoopy;
$snoop->agent = NETWORKPUB_WIDGET_NAME.' - '.get_bloginfo('url');
if($snoop->fetchtext($link)){
if (strpos($snoop->response_code, '200')) {
$response = $snoop->results;
return array(200, $response);
}
}
if( !class_exists( 'WP_Http' ) ) {
include_once( ABSPATH . WPINC. '/class-http.php' );
}
if (!class_exists('WP_Http')) {
return array(500, 'internal error');
}
$request = new WP_Http;
$headers = array( 'Agent' => NETWORKPUB_WIDGET_NAME.' - '.get_bloginfo('url') );
$response_full = $request->request( $link );
$response_code = $response_full['response']['code'];
if ($response_code === 200) {
$response = $response_full['body'];
return array($response_code, $response);
}
$response_msg = $response_full['response']['message'];
return array($response_code, $response_msg);
}
function networkpub_http_post($link, $body) {
if (!$link) {
return array(500, 'invalid url');
}
require_once(ABSPATH.WPINC.'/class-snoopy.php');
$snoop = new Snoopy;
$snoop->agent = NETWORKPUB_WIDGET_NAME.' - '.get_bloginfo('url');
if($snoop->submit($link, $body)){
if (strpos($snoop->response_code, '200')) {
$response = $snoop->results;
return array(200, $response);
}
}
if( !class_exists( 'WP_Http' ) ) {
include_once( ABSPATH . WPINC. '/class-http.php' );
}
if (!class_exists('WP_Http')) {
return array(500, 'internal error');
}
$request = new WP_Http;
$headers = array( 'Agent' => NETWORKPUB_WIDGET_NAME.' - '.get_bloginfo('url') );
$response_full = $request->request( $link, array( 'method' => 'POST', 'body' => $body, 'headers'=>$headers) );
if(isset($response_full->errors)) {
return array(500, 'internal error');
}
$response_code = $response_full['response']['code'];
if ($response_code === 200) {
$response = $response_full['body'];
return array($response_code, $response);
}
$response_msg = $response_full['response']['message'];
return array($response_code, $response_msg);
}
function networkpub_error_msgs($errMsg) {
$arr_errCodes = explode(";", $errMsg);
$errCodesCount = count($arr_errCodes);
switch (trim($arr_errCodes[0])) {
case 'internal error':
$html = '
Unknown Error. There was an error. We will keep re-trying and send you an email when the request is successful.
You can also email us at
support@linksalpha.com with error description (your blog URL and the error).
';
return $html;
break;
case 'invalid url':
$html = ' Your blog URL is invalid: '.$arr_errCodes[$errCodesCount-1];
if($errCodesCount == 3) {
$html .= '. Error Code='.$arr_errCodes[$errCodesCount-2];
}
$html .= '
You can also
Click here to enter blog URL on LinksAlpha manually.
Also ensure that in
Settings->General->"Blog address (URL)" the URL is filled-in correctly.
If you still face issues then email us at
support@linksalpha.com with error description.
';
return $html;
break;
case 'localhost url':
$html = 'Website/Blog inaccessible';
$html .= '
You are trying to use the plugin on localhost or behind a firewall, which is not supported. Please install the plugin on a Wordpress blog on a live server.
';
return $html;
break;
case 'remote url error':
$html = ' Remote URL error: '.$arr_errCodes[$errCodesCount-1];
if($errCodesCount == 3) {
$html .= '. Error Code='.$arr_errCodes[$errCodesCount-2];
}
$html .= '
Description:
Your site either did not respond (it is
extremely slow) or it is
not operational or is in
maintenance mode.
.You can also
Click here to enter blog URL on LinksAlpha manually.
Also ensure that in
Settings->General->"Blog address (URL)" the URL is filled-in correctly.
If you still face issues then email us at
support@linksalpha.com with error description.
';
return $html;
break;
case 'feed parsing error':
$html = ' Feed parsing error: '.$arr_errCodes[$errCodesCount-1];
if($errCodesCount == 3) {
$html .= '. Error Code='.$arr_errCodes[$errCodesCount-2];
}
$html .= '
Description:
Your RSS feed has errors. Pls go to
href=http://beta.feedvalidator.org/
to validate your RSS feed.
If it comes out to be correct, email as at
support@linksalpha.com with your blog URL and error description.
';
return $html;
break;
case 'feed not found':
$html = '
We could not find feed URL for your blog.
Click here to enter feed URL on LinksAlpha manually.
Also ensure that in
Settings->General->"Blog address (URL)" the URL is filled-in correctly.
If you still face issues then email us at
support@linksalpha.com with error description.
';
return $html;
break;
case 'invalid key':
$html = '
Invalid Key: the key that you entered is incorrect. Please try again.
Getting Errors? See help page
here
Or,
Click here to enter your blog URL on LinksAlpha manually.
If you still face issues then email us at
support@linksalpha.com with error description.
';
return $html;
break;
case 'subscription upgrade required':
$html = '';
return $html;
break;
default:
$html = '
This is embarrassing :-(. Sorry we are undergoing maintenance at this time - this happens very rarely but is critical to ensure continued availability. We apologize for the inconvenience.
This can take upto 2 hours maximum. Please try again after sometime and it is guaranteed to work.
You can also email us at
discuss@linksalpha.com if issue persists. Thanks for your understanding.
';
return $html;
break;
}
}
function networkpub_get_plugin_dir() {
if ( version_compare($wp_version, '2.8', '<') ) {
$path = dirname(plugin_basename(__FILE__));
if ( $path == '.' )
$path = '';
$plugin_path = trailingslashit( plugins_url( $path ) );
}
else {
$plugin_path = trailingslashit( plugins_url( '', _FILE_) );
}
return $plugin_path;
}
function networkpub_deactivate() {
echo "
Please read:
If you have
permanently deactivated 'Network Publisher' plugin and you were using this plugin (i.e., added API key), pls also remove your blog URL by logging-into
http://www.linksalpha.com/publish. Otherwise, your blog posts may continue to get posted even after you remove this plugin. If you have questions, pls email us at post@linksalpha.com.
If you are just upgrading to a newer version, then pls ignore the above message.
";
}
function networkpub_pushpresscheck() {
$active_plugins = get_option('active_plugins');
$pushpress_plugin = 'pushpress/pushpress.php';
$this_plugin_key = array_search($pushpress_plugin, $active_plugins);
if ($this_plugin_key) {
$options = get_option(NETWORKPUB_WIDGET_NAME_INTERNAL);
if(array_key_exists('id', $options)) {
if($options['id']) {
$link = 'http://www.linksalpha.com/a/pushpress';
$body = array('id'=>$options['id']);
$response_full = networkpub_http_post($link, $body);
$response_code = $response_full[0];
}
}
}
}
register_deactivation_hook( __FILE__, 'networkpub_deactivate' );
?>