public function processPost($items, $params)
 {
     $dbo = JFactory::getDBO();
     $update_time = JFactory::getDate()->toSql();
     date_default_timezone_set($params->get('timezone'));
     $global = $params->get('schedule');
     $date = date('Y-m-d');
     $now = date('H:i', strtotime('-24 hours', time()));
     $now = explode(":", $now);
     foreach ($items as $item) {
         if (!empty($item->params)) {
             $available = json_decode($item->params);
             $query = 'SELECT count(' . $available->tb_id . ') FROM ' . $available->table . ' WHERE ' . $available->published . ' AND ' . $available->tb_id . '=' . $available->id;
             $dbo->setQuery($query);
             $iSpublish = $dbo->loadResult();
         } else {
             $iSpublish = 1;
         }
         if ($iSpublish == 1) {
             $cron = (object) unserialize($item->scron);
             $schedule = (object) unserialize($item->schedule);
             $hour = preg_replace('/( *)/', '', $schedule->hour);
             $sche_date = preg_replace('/( *)/', '', $schedule->date);
             if ($hour == '' && $global != '') {
                 $hour = $global;
             }
             $hour = explode(":", $hour);
             if ($schedule->date == '' || $sche_date == $date) {
                 if ($hour[0] == '' || (int) $hour[0] < (int) $now[0] || (int) $hour[0] == (int) $now[0] && (int) $hour[1] <= (int) $now[1]) {
                     $name = $cron->name;
                     if (self::checkClass($cron->alias)) {
                         $publishes = new BTPublishes($cron->alias, $dbo);
                         $result = $name::postMessage($publishes, $cron->attachment);
                         if (!$dbo->connected()) {
                             $dbo = BTJFactory::createDbo();
                         }
                         if ($result['checked'] == 1) {
                             $query = 'UPDATE  #__bt_messages SET published =\'' . $result['publish'] . '\',log =\'' . $dbo->escape($result['log']) . '\',sent_time= \'' . $update_time . '\' WHERE id=' . $item->id;
                             $dbo->setQuery($query);
                             $dbo->query();
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 public function save($data)
 {
     $dispatcher = JDispatcher::getInstance();
     $table = $this->getTable();
     $user = JFactory::getUser();
     $schedule = '';
     $pk = !empty($data['id']) ? $data['id'] : (int) $this->getState('message.id');
     $isNew = true;
     $db = $this->getDbo();
     if (isset($data['schedule'])) {
         $schedule = serialize($data['schedule']);
     }
     $Idmessage = self::MessageIds();
     $IdOld = self::findMessageOld($data['id'], $Idmessage);
     if ($IdOld >= 0) {
         if ($data['published'] == 1) {
             $items = self::getItemsObject($data['id']);
             //Check is publish content
             if (!empty($items->params)) {
                 $available = json_decode($items->params);
                 $query = 'SELECT count(' . $available->tb_id . ') FROM ' . $available->table . ' WHERE ' . $available->published . ' AND ' . $available->tb_id . '=' . $available->id;
                 $db->setQuery($query);
                 $iSpublish = $db->loadResult();
             } else {
                 $iSpublish = 1;
             }
             //end
             $type = $items->message_type;
             $item = (object) unserialize($items->scron);
             $attachment = $item->attachment;
             switch ($type) {
                 case 'profile':
                     require_once JPATH_SITE . '/components/com_bt_socialconnect/helpers/html/factory.php';
                     if ($iSpublish == 1) {
                         switch ($item->alias) {
                             case 'facebook':
                                 $facebook_id = $attachment['item']->social_id;
                                 $url = "https://graph.facebook.com/{$facebook_id}/feed";
                                 $post = JHtmlBt_Socialpost::socialPost($item->alias, $attachment['attachment'], $url, $item->name, $facebook_id);
                                 break;
                             case 'twitter':
                                 if (!class_exists('TwitterOAuth')) {
                                     require_once JPATH_SITE . '/components/com_bt_socialconnect/helpers/html/twitter/twitteroauth.php';
                                 }
                                 $twitter = unserialize($attachment['item']->access_token);
                                 $connection = new TwitterOAuth(trim($params->get('ttappId', '')), trim($params->get('ttsecret', '')), $twitter['oauth_token'], $twitter['oauth_token_secret']);
                                 $message = $attachment['attachment'];
                                 //Share link in twitter
                                 $parameters = array('status' => $message);
                                 $status = $connection->post('statuses/update', $parameters);
                                 if (isset($status->errors)) {
                                     $post['log'] = $status->errors[0]->message;
                                     $post['publish'] = 0;
                                 } else {
                                     $post['log'] = JText::_('COM_BT_SOCIALCONNECT_SUBMIITED_SUCCESSFULL');
                                     $post['publish'] = 1;
                                 }
                                 break;
                             case 'linkedin':
                                 $response = JHtmlBt_Socialpost::fetch($attachment['item']->access_token, $attachment['attachment']);
                                 $post = JHtmlBt_Socialpost::socialPost($item->alias, $response['body'], $response['url'], $item->name, $attachment['item']->social_id);
                                 break;
                         }
                     } else {
                         $post['log'] = JText::_('COM_BT_SOCIALCONNECT_ITEM_POST_UNPUBLISH');
                         $post['publish'] = 0;
                     }
                     break;
                 case 'system':
                     require_once JPATH_ADMINISTRATOR . '/components/com_bt_socialconnect/publishes/btpublishes.php';
                     if ($iSpublish == 1) {
                         $name = $item->name;
                         if (self::checkClass($item->alias)) {
                             $publishes = new BTPublishes($item->alias, $db);
                             $post = $name::postMessage($publishes, $item->attachment);
                         } else {
                             $post['log'] = JText::_('COM_BT_SOCIALCONNECT_CHANNEL_UNPUBLISH');
                             $post['publish'] = 0;
                         }
                     } else {
                         $post['log'] = JText::_('COM_BT_SOCIALCONNECT_ITEM_POST_UNPUBLISH');
                         $post['publish'] = 0;
                     }
                     break;
             }
             if (isset($post)) {
                 if (!$db->connected()) {
                     $db = BTJFactory::createDbo();
                 }
                 $update_time = JFactory::getDate()->toSql();
                 $query = 'UPDATE  #__bt_messages  SET	published= \'' . $post['publish'] . '\',schedule = \'' . $db->escape($schedule) . '\',sent_time= \'' . $update_time . '\',log = \'' . $post['log'] . '\' WHERE id =' . $data['id'];
                 $db->setQuery($query);
                 $db->query();
                 return $post;
             }
         } else {
             $query = 'UPDATE  #__bt_messages  SET	published= \'' . $db->escape($data['published']) . '\' ,schedule = \'' . $db->escape($schedule) . '\' WHERE id =' . (int) $data['id'];
             $db->setQuery($query);
             $db->query();
         }
         $this->setState('message.id', $data['id']);
     } else {
         $params_scoialpublish = $data['channel'];
         //Prepare data
         $params = JComponentHelper::getParams('com_bt_socialconnect');
         $trigger = 'Custom Content';
         $title = $data['title'];
         $message = $data['message'];
         $link = JHtmlBt_Socialpost::getcaseshortlink($data['url']);
         $description = $data['fulltext'];
         $message_type = 'system';
         if (!empty($data['image'])) {
             $picture = JURI::root() . $data['image'];
         } else {
             $picture = '';
         }
         if ($params->get('cropthumb')) {
             $saveDir = JPATH_SITE . '/images/bt_socialconnect/';
             JHtmlBt_Socialpost::createFolder($saveDir);
             $images_path = $saveDir . 'thumb/';
             JHtmlBt_Socialpost::createFolder($images_path);
             if ($picture) {
                 $cropimage = JHtmlBt_Socialpost::cropimage($picture, $images_path, $params);
                 $picture = JURI::root() . 'images/bt_socialconnect/thumb/' . $cropimage;
             }
         }
         $created_time = JFactory::getDate()->toSql();
         //NNONO
         require_once JPATH_SITE . '/components/com_bt_socialconnect/helpers/html/factory.php';
         require_once JPATH_ADMINISTRATOR . '/components/com_bt_socialconnect/publishes/btpublishes.php';
         //END
         $result = JHtmlBt_Socialpost::getpublish($params_scoialpublish);
         foreach ($result as $key => $item) {
             $publishes = new BTPublishes($item->alias, $db);
             $name = $publishes->type;
             $access_token = $publishes->params->get('access_token');
             //Check facebookname post message
             if ($publishes->params->get('uname')) {
                 $uname = $publishes->params->get('uname');
             } else {
                 $uname = $user->name;
             }
             //Check facebook id
             $uid = $publishes->params->get('uid');
             $getuser = JHtmlBt_Socialpost::getUser($uid);
             if (empty($getuser)) {
                 $userpost = $user->name;
             } else {
                 $userpost = $getuser;
             }
             //Set content to post
             $token = $access_token;
             //end
             //Set attachment default
             $attachment = self::setDataSocial('', $message, $title, $link, $description, $picture);
             $urlprocess = self::processAttachment($userpost, $item->type, $title, $data['url'], $attachment, $trigger, $created_time, $params, $data['title'], $message, $link, $item->id, $message_type, $db);
             $attachment = self::setDataSocial($token, $urlprocess['message'], $title, $urlprocess['link'], $description, $picture);
             //Set Cronjob for all site to renew post
             $seralizedCron = self::setDataOfline('system', $item->alias, $name, $attachment);
             if ($data['published'] == 1) {
                 $result = $name::postMessage($publishes, $attachment);
                 if (!$db->connected()) {
                     $db = BTJFactory::createDbo();
                 }
                 if ($result['checked'] == 1) {
                     $update_time = JFactory::getDate()->toSql();
                     $query = 'UPDATE  #__bt_messages  SET	published= \'' . $db->escape($result['publish']) . '\',schedule = \'' . $db->escape($schedule) . '\',log = \'' . $db->escape($result['log']) . '\',scron= \'' . $seralizedCron . '\',sent_time= \'' . $update_time . '\' WHERE id =' . (int) $urlprocess['id'];
                     $db->setQuery($query);
                     $db->query();
                 }
             } else {
                 $query = 'UPDATE  #__bt_messages  SET	published= \'' . $db->escape($data['published']) . '\',schedule = \'' . $db->escape($schedule) . '\',scron= \'' . $seralizedCron . '\' WHERE id =' . (int) $urlprocess['id'];
                 $db->setQuery($query);
                 $db->query();
             }
             $this->setState('message.id', $urlprocess['id']);
         }
     }
     return true;
 }
Exemplo n.º 3
0
 public static function postArticle($article, $cronjob, $config)
 {
     $app = JApplication::getInstance('site');
     $router = $app->getRouter();
     $params = JComponentHelper::getParams('com_bt_socialconnect');
     $uri = JURI::getInstance();
     $user = JFactory::getUser();
     $db = JFactory::getDbo();
     // Get link article
     $link_article = $article->link;
     $self_url = $router->build($link_article);
     $link_article = $self_url->toString();
     $link_article = str_replace('/administrator/', '/', $link_article);
     $link_article = $uri->getScheme() . "://" . $uri->getHost() . $link_article;
     $trigger = $article->trigger;
     $template = $config->def('template', '');
     $dc_template = $config->def('des_template', '');
     $image = '';
     $default_image = $config->def('default_img', '');
     if ($default_image != '') {
         $image = $default_image;
     } else {
         $image = $article->image;
     }
     if ($image != '') {
         if (strpos($image, "http") !== false) {
             $urlimage = $image;
         } else {
             $urlimage = JURI::root() . $image;
         }
     } else {
         $urlimage = '';
     }
     // Crop image neu config trong admin
     if ($params->get('cropthumb')) {
         $saveDir = JPATH_SITE . '/images/bt_socialconnect/';
         JHtmlBt_Socialpost::createFolder($saveDir);
         $images_path = $saveDir . 'thumb/';
         JHtmlBt_Socialpost::createFolder($images_path);
         if ($urlimage) {
             $cropimage = JHtmlBt_Socialpost::cropimage($urlimage, $images_path, $params);
             $urlimage = JURI::root() . 'images/bt_socialconnect/thumb/' . $cropimage;
         }
     }
     //Prepare content----------------------------------------------------- post
     $created_time = JFactory::getDate()->toSql();
     $message = self::setmessage($template, $user->name, $article, $link_article);
     $description = self::setmessage($dc_template, $user->name, $article, $link_article);
     $title = $article->title;
     $link = JHtmlBt_Socialpost::getcaseshortlink($link_article);
     $picture = $urlimage;
     $default_content = self::setDataSocial('', $message, $title, $link, $description, $picture);
     $publish = 2;
     $log = 'Waiting to process in queue';
     // Post len social network neu tai khoan ket noi nhieu mang xa hoi
     $items = JHtmlBt_Socialpost::getSocialpostuser($user->id);
     foreach ($items as $key => $item) {
         $token = $item->access_token;
         switch ($item->social_type) {
             case 'facebook':
                 $facebook_id = $item->social_id;
                 $url = "https://graph.facebook.com/{$facebook_id}/feed";
                 $sociatype = 'facebookprofile';
                 $message_type = 'profile';
                 //Check count the link and get id Re set attachmetn when save attachment default
                 $urlprocess = self::processAttachment($user->name, $sociatype, $article, $link_article, $default_content, $trigger, $created_time, $params, $template, $message, $link, $user->id, $message_type, $db);
                 $attachment = self::setAttachment($token, $urlprocess['message'], $title, $urlprocess['link'], $description, $picture);
                 $attachment = $attachment['facebook'];
                 if (!$cronjob) {
                     //Post message user connect and update
                     $result = JHtmlBt_Socialpost::socialPost($item->social_type, $attachment, $url, $user->name, $facebook_id);
                     $log = $result['log'];
                     $publish = $result['publish'];
                 }
                 break;
             case 'twitter':
                 $sociatype = 'twitterprofile';
                 $message_type = 'profile';
                 $twitter = unserialize($item->access_token);
                 if (!class_exists('TwitterOAuth')) {
                     require_once JPATH_SITE . '/components/com_bt_socialconnect/helpers/html/twitter/twitteroauth.php';
                 }
                 $connection = new TwitterOAuth(trim($params->get('ttappId', '')), trim($params->get('ttsecret', '')), $twitter['oauth_token'], $twitter['oauth_token_secret']);
                 $urlprocess = self::processAttachment($user->name, $sociatype, $article, $link_article, $default_content, $trigger, $created_time, $params, $template, $message, $link, $user->id, $message_type, $db);
                 $attachment = $urlprocess['message'];
                 //Share link in twitter
                 if (!$cronjob) {
                     $parameters = array('status' => $urlprocess['message']);
                     $status = $connection->post('statuses/update', $parameters);
                     if (isset($status->errors)) {
                         $log = $status->errors[0]->message;
                         $publish = 0;
                     } else {
                         $log = JTEXT::_('Message has sent ' . '<a href="https://twitter.com/' . $user->username . '" style="text-decoration: underline;" target="_blank">' . $user->name . '</a>' . ' successfully');
                         $publish = 1;
                     }
                 }
                 break;
             case 'linkedin':
                 $sociatype = 'linkedinprofile';
                 $message_type = 'profile';
                 $urlprocess = self::processAttachment($user->name, $sociatype, $article, $link_article, $default_content, $trigger, $created_time, $params, $template, $message, $link, $user->id, $message_type, $db);
                 //Set re attachment
                 $attachment = self::setAttachment($token, $urlprocess['message'], $title, $urlprocess['link'], $description, $picture);
                 $attachment = $attachment['linkedin'];
                 if (!$cronjob) {
                     $response = JHtmlBt_Socialpost::fetch($item->access_token, $attachment);
                     $return = JHtmlBt_Socialpost::socialPost($item->social_type, $response['body'], $response['url'], $user->name, $item->social_id);
                     $log = $return['log'];
                     $publish = $return['publish'];
                 }
                 break;
         }
         //Set Cronjob for all site to renew post
         $data = array();
         $data['item'] = $item;
         $data['attachment'] = $attachment;
         //Set data to renew post
         $seralizedCron = self::setDataOfline($item->social_type, $user->name, $data);
         $update_time = JFactory::getDate()->toSql();
         $query = 'UPDATE  #__bt_messages  SET	`published`= \'' . $publish . '\',`log` = \'' . $log . '\',`scron`= \'' . $db->escape($seralizedCron) . '\',`sent_time`= \'' . $update_time . '\' WHERE `id` =' . (int) $urlprocess['id'];
         $db->setQuery($query);
         $db->query();
     }
     //Post message group system
     $params_scoialpublish = $config->get('channel', 0);
     $result = JHtmlBt_Socialpost::getpublish($params_scoialpublish);
     if (!empty($result)) {
         foreach ($result as $key => $item) {
             $publishes = new BTPublishes($item->alias, $db);
             $name = $publishes->type;
             $access_token = $publishes->params->get('access_token');
             //Check facebookname post message
             if ($publishes->params->get('uname')) {
                 $uname = $publishes->params->get('uname');
             } else {
                 $uname = $user->name;
             }
             //Check facebook id
             $uid = $publishes->params->get('uid');
             $getuser = JHtmlBt_Socialpost::getUser($uid);
             if (empty($getuser)) {
                 $userpost = $user->name;
             } else {
                 $userpost = $getuser;
             }
             //Set and assign content to post
             $token = $access_token;
             $message = self::setmessage($template, $uname, $article, $link_article);
             $title = $article->title;
             $message_type = 'system';
             //end
             //Set attachment default
             $urlprocess = self::processAttachment($userpost, $item->type, $article, $link_article, $default_content, $trigger, $created_time, $params, $template, $message, $link, $item->id, $message_type, $db);
             $attachment = self::setDataSocial($token, $urlprocess['message'], $title, $urlprocess['link'], $description, $picture);
             $seralizedCron = self::setDataOfline($item->alias, $name, $attachment);
             //Kiem tra co de dang cronjob ko
             if ($cronjob) {
                 $time = array();
                 $time['hour'] = $params->get('schedule', '');
                 $time['date'] = '';
                 $schedule = serialize($time);
                 $query = 'UPDATE  #__bt_messages  SET	 `scron`= \'' . $db->escape($seralizedCron) . '\',`schedule` = \'' . $db->escape($schedule) . '\' WHERE id =' . (int) $urlprocess['id'];
                 $db->setQuery($query);
                 $db->query();
             } else {
                 if (!empty($name)) {
                     $result = $name::postMessage($publishes, $attachment);
                     if (!$db->connected()) {
                         $db = BTJFactory::createDbo();
                     }
                     if ($result['checked'] == 1) {
                         $update_time = JFactory::getDate()->toSql();
                         $query = 'UPDATE  #__bt_messages  SET	`published`= \'' . $result['publish'] . '\',`log` = \'' . $db->escape($result['log']) . '\',`scron`= \'' . $db->escape($seralizedCron) . '\',`sent_time`= \'' . $update_time . '\' WHERE `id` =' . (int) $urlprocess['id'];
                         $db->setQuery($query);
                         $db->query();
                     }
                 }
             }
         }
     }
     //End
 }