function Save($context, $article, $isNew) { if (is_null($context)) { //joomla 1.5 if ($_REQUEST['state'] != '1') { return; } if ($_REQUEST['details']['access'] != '0') { return; } } else { //joomla 2.5 if ($_REQUEST['jform']['state'] != '1') { return; } if ($_REQUEST['jform']['access'] != '1') { return; } if ($context == 'com_media.file') { return; } //enabled "com_content.article" (backend) and "com_content.form" (frontend) } $app = JFactory::getApplication(); $fb_enable_autopublish = $this->params->get('fb_enable_autopublish'); $twitter_enable_autopublish = $this->params->get('twitter_enable_autopublish'); //Enable autopublish only on the articles or categories where are rendered the share buttons $category_tobe_excluded = $this->params->get('category_tobe_excluded_buttons', ''); $content_tobe_excluded = $this->params->get('content_tobe_excluded_buttons', ''); $excludedContentList = @explode(",", $content_tobe_excluded); if (isset($article->id)) { if (in_array($article->id, $excludedContentList)) { return true; } if (is_array($category_tobe_excluded)) { if (in_array($article->catid, $category_tobe_excluded)) { return true; } } else { $excludedCatList = @explode(",", $category_tobe_excluded); if (in_array($article->catid, $excludedCatList)) { return true; } } } else { if (isset($app->input)) { $id = $app->input->get('id'); } else { $id = JRequest::getCmd('id'); } if (is_array($category_tobe_excluded)) { if (in_array($id, $category_tobe_excluded)) { return true; } } else { $excludedCatList = @explode(",", $category_tobe_excluded); if (in_array($id, $excludedCatList)) { return true; } } } //Enable autopublish only on "apply" action if ($_REQUEST['task'] != 'apply') { return true; } if (($fb_enable_autopublish || $twitter_enable_autopublish) && !extension_loaded('curl')) { $this->show('Facebook or Twitter Autopublish is not possible because CURL extension is not loaded.', 'error'); return true; } //Facebook autopublish if ($fb_enable_autopublish) { if (!class_exists('Facebook', false)) { require_once 'facebook' . DS . 'facebook.php'; } $fb_app_id = $this->params->get('fb_app_id'); $fb_secret_key = $this->params->get('fb_secret_key'); if (method_exists($this->params, 'exists') && $this->params->exists('fb_extra_params')) { $fb_extra_params = $this->params->get('fb_extra_params'); $fb_ids = $fb_extra_params->fb_ids; $token = $fb_extra_params->fb_token; } else { $token = $this->params->get('fb_token'); $fb_ids = $this->params->get('fb_ids'); if ($fb_ids == '') { $fb_ids = array(); } if (!is_array($fb_ids)) { $fb_ids = array($fb_ids); } } if ($fb_app_id != '' && $fb_secret_key != '' && count($fb_ids) > 0 && $token != '') { $title = $this->getTitle($article); $caption = ''; $url = JUri::root() . ContentHelperRoute::getArticleRoute($article->id . ':' . $article->alias, $article->catid); $router = JSite::getInstance('site')->getRouter('site'); $url = $router->build($url)->toString(); $url = str_replace('administrator/', '', $url); $description = $this->getDescription($article, 'article'); if ($this->params->get('fb_autopublish_image', '1') == '1') { //first image $images = $this->getPicture($article, 'article'); if (count($images) > 0) { $pic = $images[0]; } else { $pic = ''; } } else { $pic = ''; } if ($isNew) { $msg = $this->params->get('fb_text_new', ''); } else { $msg = $this->params->get('fb_text_old', 'Update'); } $facebook = new Facebook(array('appId' => $fb_app_id, 'secret' => $fb_secret_key, 'cookie' => true)); $ok = true; try { $me = $facebook->api('/me/', array('access_token' => $token)); $info_accounts = $facebook->api('/me/accounts', array('access_token' => $token)); $info_groups = $facebook->api('/me/groups', array('access_token' => $token)); } catch (FacebookApiException $e) { JError::raiseWarning('1', 'Facebook error: ' . $e->getMessage()); $ok = false; } if ($ok) { if (in_array($me['id'], $fb_ids)) { $ok = true; try { $facebook->api('/' . $me['id'] . '/feed', 'post', array('access_token' => $token, 'message' => $msg, 'link' => $url, 'picture' => $pic, 'name' => $title, 'caption' => $caption, 'description' => $description)); } catch (FacebookApiException $e) { JError::raiseWarning('1', 'Facebook error: ' . $e->getMessage()); $ok = false; } if ($ok) { $this->show('Content published on Facebook account: ' . "<a href='" . $me['link'] . "'>" . $me['name'] . "</a>", 'message'); } } $accounts = $info_accounts['data']; foreach ($accounts as $account) { if (in_array($account['id'], $fb_ids)) { $ok = true; try { $pagetoken = $account['access_token']; $facebook->api('/' . $account['id'] . '/feed', 'post', array('access_token' => $pagetoken, 'message' => $msg, 'link' => $url, 'picture' => $pic, 'name' => $title, 'caption' => $caption, 'description' => $description)); } catch (FacebookApiException $e) { JError::raiseWarning('1', 'Facebook error: ' . $e->getMessage()); $ok = false; } if ($ok) { $info = $facebook->api('/' . $account['id'] . '/', array('access_token' => $token)); $this->show("Content published on Facebook page: <a href='" . $info['link'] . "'>" . $info['name'] . "</a>", 'message'); } } } $accounts = $info_groups['data']; foreach ($accounts as $account) { if (in_array($account['id'], $fb_ids)) { try { $result = $facebook->api('/' . $account['id'] . '/feed', 'post', array('access_token' => $token, 'message' => $msg, 'link' => $url, 'picture' => $pic, 'name' => $title, 'caption' => $caption, 'description' => $description)); } catch (FacebookApiException $e) { JError::raiseWarning('1', 'Facebook error: ' . $e->getMessage()); } if (isset($result['id'])) { $this->show("Content published on Facebook group <a href='//www.facebook.com/" . $account['id'] . "/'>" . $account['name'] . "</a>", 'message'); } } } } } else { if ($fb_app_id == '') { $this->show('App ID is missing', 'error'); } if ($fb_secret_key == '') { $this->show('App secret key is missing', 'error'); } if (count($fb_ids) == 0) { $this->show('Must be specified on at least one Facebook account ID where to publish the article', 'error'); } if ($token == '') { $this->show('Valid access token missing', 'error'); } } } //Twitter autopublish if ($twitter_enable_autopublish) { if (!class_exists('TwitterOAuth', false)) { require_once 'twitteroauth' . DS . 'twitteroauth.php'; } $consumer_key = $this->params->get('twitter_consumer_key', ''); $consumer_secret = $this->params->get('twitter_consumer_secret', ''); $oauth_token = $this->params->get('twitter_oauth_token', ''); $oauth_token_secret = $this->params->get('twitter_oauth_token_secret', ''); if ($consumer_key != '' && $consumer_secret != '' && $oauth_token != '' && $oauth_token_secret != '') { $conn = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); if (!$conn) { $this->show('Connection error occurred', 'error'); die; } $title = $this->getTitle($article); $url = JUri::root() . ContentHelperRoute::getArticleRoute($article->id . ':' . $article->alias, $article->catid); $router = JSite::getInstance('site')->getRouter('site'); $url = $router->build($url)->toString(); $url = str_replace('administrator/', '', $url); if ($isNew) { $msg = substr($title, 0, 100) . " " . $url; } else { $msg = "Update:" . substr($title, 0, 100) . " " . $url; } $status = $conn->post('statuses/update', array('status' => $msg)); if (!isset($status->error)) { $this->show('Content published on Twitter', 'message'); } else { $this->show('Content published on Twitter: ' . $status->error, 'error'); } } else { if ($consumer_key == '') { $this->show('Consumer key is missing', 'error'); } if ($consumer_secret == '') { $this->show('Consumer secret key is missing', 'error'); } if ($oauth_token == '') { $this->show('Oauth token is missing', 'error'); } if ($oauth_token_secret == '') { $this->show('Oauth token secret key is missing', 'error'); } } } return true; }