function doPostToNT($options, $message) { $badOut = array('pgID' => '', 'isPosted' => 0, 'pDate' => date('Y-m-d H:i:s'), 'Error' => ''); global $nxs_vkCkArray; //## Check settings if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; } if (empty($options['imgSize'])) { $options['imgSize'] = ''; } if ((!isset($options['uName']) || trim($options['uPass']) == '') && (!isset($options['vkAppAuthToken']) || trim($options['vkAppAuthToken']) == '')) { $badOut['Error'] = 'Not Configured'; return $badOut; } $pass = substr($options['uPass'], 0, 5) == 'n5g9a' ? nsx_doDecode(substr($options['uPass'], 5)) : $options['uPass']; //## Format if (!empty($message['pText'])) { $msg = $message['pText']; } else { $msg = nxs_doFormatMsg($options['msgFrmt'], $message); } $urlToGo = !empty($message['url']) ? $message['url'] : ''; $postType = $options['postType']; //$link = urlencode($link); $desc = urlencode(substr($msg, 0, 500)); if (isset($message['imageURL'])) { $imgURL = trim(nxs_getImgfrOpt($message['imageURL'], $options['imgSize'])); } else { $imgURL = ''; } $msgOpts = array(); $msgOpts['uid'] = $options['vkPgID']; // if ($link!='') $msgOpts['link'] = $link; if (!empty($message['videoURL']) && $postType == "I") { $postType = 'A'; $urlToGo = $message['videoURL']; $msgOpts['vID'] = $vids[0]; } if ($postType == 'I' && trim($imgURL) == '') { $postType = 'T'; } $msgOpts['type'] = $postType; if (function_exists('nxs_doPostToVK') && $postType == 'A' && $urlToGo != '') { //## Login if (isset($options['vkSvC'])) { $nxs_vkCkArray = maybe_unserialize($options['vkSvC']); } $loginError = true; if (is_array($nxs_vkCkArray)) { $loginError = nxs_doCheckVK(); } if ($loginError != false) { if (!empty($options['vkPh'])) { $replArr = explode(' ... ', $options['vkPhReq']); $ph = $options['vkPh']; $ln = strlen($replArr[0]); if (substr($ph, 0, $ln) == $replArr[0]) { $ph = substr($ph, $ln); } $ln = strlen($replArr[1]); $mln = -$ln; if (substr($ph, $mln) == $replArr[1]) { $ph = substr($ph, $ln, $mln); } } else { $ph = ''; } $loginError = nxs_doConnectToVK($options['uName'], $pass, $ph); } // prr($loginError); if ($loginError !== false) { if (stripos($loginError, 'Phone verification required:') !== false) { return $loginError; } else { return "ERROR - BAD USER/PASS - " . print_r($loginError, true); } } //## Post $msgOpts['url'] = $urlToGo; $msgOpts['urlTitle'] = $message['urlTitle']; $msgOpts['urlDesc'] = $message['urlDescr']; $msgOpts['imgURL'] = $imgURL; $ret = nxs_doPostToVK($msg, $options['url'], $msgOpts); if (is_array($ret) && !empty($ret['code']) && $ret['code'] == 'OK') { return array('postID' => $ret['post_id'], 'isPosted' => 1, 'postURL' => 'http://vk.com/wall' . $ret['post_id'], 'pDate' => date('Y-m-d H:i:s')); } else { $badOut .= 'ERROR: ' . print_r($ret, true); } } //prr($postType); if ($postType == 'I') { $imgUpld = $this->nxs_uplImgtoVK($imgURL, $options); if (is_object($imgUpld)) { $imgID = $imgUpld->id; $atts[] = $imgID; } else { $badOut['Error'] .= '-=ERROR=- ' . print_r($imgUpld, true); } } if ($postType != 'A') { if ($options['addBackLink'] == '1') { $atts[] = $urlToGo; } if (is_array($atts)) { $atts = implode(',', $atts); } $postUrl = 'https://api.vkontakte.ru/method/wall.post'; $postArr = array('owner_id' => $options['pgIntID'], 'access_token' => $options['vkAppAuthToken'], 'from_group' => '1', 'message' => $msg, 'attachment' => $atts); $response = wp_remote_post($postUrl, array('body' => $postArr)); if (is_wp_error($response) || is_object($response) && isset($response->errors) || is_array($response) && stripos($response['body'], '"error":') !== false) { $badOut['Error'] .= 'Error: ' . print_r($response['body'], true); } else { $respJ = json_decode($response['body'], true); $ret = $options['pgIntID'] . '_' . $respJ['response']['post_id']; } } if (isset($ret) && $ret != '') { return array('postID' => $ret, 'isPosted' => 1, 'postURL' => 'http://vk.com/wall' . $ret, 'pDate' => date('Y-m-d H:i:s')); } return $badOut; }
function nxs_doPublishToVK($postID, $options) { global $ShownAds, $nxs_vkCkArray; $ntCd = 'VK'; $ntCdL = 'vk'; $ntNm = 'vKontakte(VK)'; //if (isset($options['timeToRun'])) wp_unschedule_event( $options['timeToRun'], 'nxs_doPublishToVK', array($postID, $options)); $ii = $options['ii']; if (!isset($options['pType'])) { $options['pType'] = 'im'; } if ($options['pType'] == 'sh') { sleep(rand(1, 10)); } $logNT = '<span style="color:#000080">vKontakte</span> - ' . $options['nName']; $snap_ap = get_post_meta($postID, 'snap' . $ntCd, true); $snap_ap = maybe_unserialize($snap_ap); if ($options['pType'] != 'aj' && is_array($snap_ap) && (nxs_chArrVar($snap_ap[$ii], 'isPosted', '1') || nxs_chArrVar($snap_ap[$ii], 'isPrePosted', '1'))) { $snap_isAutoPosted = get_post_meta($postID, 'snap_isAutoPosted', true); if ($snap_isAutoPosted != '2') { nxs_addToLogN('W', 'Notice', $logNT, '-=Duplicate=- Post ID:' . $postID, 'Already posted. No reason for posting duplicate' . ' |' . $options['pType']); return; } } $email = $options['uName']; $pass = substr($options['uPass'], 0, 5) == 'n5g9a' ? nsx_doDecode(substr($options['uPass'], 5)) : $options['uPass']; if ($postID == '0') { echo "Testing ... <br/><br/>"; $link = home_url(); $msg = 'Test Link from ' . $link; } else { $post = get_post($postID); if (!$post) { return; } $msgFormat = $options['msgFrmt']; $msg = strip_tags(nsFormatMessage($msgFormat, $postID)); $link = get_permalink($postID); nxs_metaMarkAsPosted($postID, $ntCd, $options['ii'], array('isPrePosted' => '1')); } $dusername = $options['uName']; $postType = $options['postType']; //$link = urlencode($link); $desc = urlencode(substr($msg, 0, 500)); $extInfo = ' | PostID: ' . $postID . " - " . $post->post_title . ' |' . $options['pType']; $imgURL = nxs_getPostImage($postID); $vids = nsFindVidsInPost($post); if (count($vids) > 0) { if (strlen($vids[0]) == 11) { $vidURL = 'http://www.youtube.com/watch?v=' . $vids[0]; $imgURL = 'http://img.youtube.com/vi/' . $vids[0] . '/maxresdefault.jpg'; } if (strlen($vids[0]) == 8) { $vidURL = 'https://secure.vimeo.com/moogaloop.swf?clip_id=' . $vids[0] . '&autoplay=1'; //$mssg['source'] = 'http://player.vimeo.com/video/'.$vids[0]; $apiURL = "http://vimeo.com/api/v2/video/" . $vids[0] . ".json?callback=showThumb"; $json = wp_remote_get($apiURL); if (!is_wp_error($json)) { $json = $json['body']; $json = str_replace('showThumb(', '', $json); $json = str_replace('])', ']', $json); $json = json_decode($json, true); $imgURL = $json[0]['thumbnail_large']; } } } $msgOpts = array(); $msgOpts['uid'] = $options['vkPgID']; // if ($link!='') $msgOpts['link'] = $link; if ($vidURL != '' && $postType == "I") { $postType = 'A'; $link = $vidURL; $msgOpts['vID'] = $vids[0]; } if ($postType == 'I' && trim($imgURL) == '') { $postType = 'T'; } $msgOpts['type'] = $postType; if ($postType == 'A' && $link != '') { //## Login if (isset($options['vkSvC'])) { $nxs_vkCkArray = maybe_unserialize($options['vkSvC']); } $loginError = true; if (is_array($nxs_vkCkArray)) { $loginError = nxs_doCheckVK(); } if ($loginError != false) { $loginError = nxs_doConnectToVK($email, $pass); } if (serialize($nxs_vkCkArray) != $options['vkSvC']) { global $plgn_NS_SNAutoPoster; $gOptions = $plgn_NS_SNAutoPoster->nxs_options; if (isset($options['ii']) && $options['ii'] !== '') { $gOptions['vk'][$options['ii']]['vkSvC'] = serialize($nxs_vkCkArray); update_option('NS_SNAutoPoster', $gOptions); } else { foreach ($gOptions['vk'] as $ii => $gpn) { $result = array_diff($options, $gpn); if (!is_array($result) || count($result) < 1) { $gOptions['vk'][$ii]['vkSvC'] = serialize($nxs_vkCkArray); update_option('NS_SNAutoPoster', $gOptions); break; } } } } if ($loginError !== false) { if ($postID == '0') { prr($loginError); } nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- ' . print_r($loginError, true) . " - BAD USER/PASS", $extInfo); return " -= BAD USER/PASS =- "; } //## Post if (trim($fbMsgAFormat) != '') { $dsc = nsFormatMessage($fbMsgAFormat, $postID); } else { if (function_exists('aioseop_mrt_fix_meta') && $dsc == '') { $dsc = trim(get_post_meta($postID, '_aioseop_description', true)); } if (function_exists('wpseo_admin_init') && $dsc == '') { $dsc = trim(get_post_meta($postID, '_yoast_wpseo_opengraph-description', true)); } if (function_exists('wpseo_admin_init') && $dsc == '') { $dsc = trim(get_post_meta($postID, '_yoast_wpseo_metadesc', true)); } if ($dsc == '') { $dsc = trim(apply_filters('the_content', nxs_doQTrans($post->post_excerpt, $lng))); } if ($dsc == '') { $dsc = trim(nxs_doQTrans($post->post_excerpt, $lng)); } if ($dsc == '') { $dsc = trim(apply_filters('the_content', nxs_doQTrans($post->post_content, $lng))); } if ($dsc == '') { $dsc = trim(nxs_doQTrans($post->post_content, $lng)); } if ($dsc == '') { $dsc = get_bloginfo('description'); } } $dsc = strip_tags($dsc); $dsc = nxs_decodeEntitiesFull($dsc); $dsc = nsTrnc($dsc, 900, ' '); $msgOpts['url'] = $link; $msgOpts['urlTitle'] = nxs_doQTrans($post->post_title, $lng); $msgOpts['urlDesc'] = $dsc; $msgOpts['imgURL'] = $imgURL; $ret = nxs_doPostToVK($msg, $options['url'], $msgOpts); // prr($ret); } //prr($postType); if ($postType == 'I') { $imgUpld = nxs_uplImgtoVK($imgURL, $options); if (is_object($imgUpld)) { $imgID = $imgUpld->id; $atts[] = $imgID; } } if ($postType != 'A') { if ($options['addBackLink'] == '1') { $atts[] = $link; } if (is_array($atts)) { $atts = implode(',', $atts); } $postUrl = 'https://api.vkontakte.ru/method/wall.post?owner_id=' . $options['pgIntID'] . '&access_token=' . $options['vkAppAuthToken'] . '&from_group=1&message=' . urlencode($msg) . '&attachment=' . urlencode($atts); $response = wp_remote_get($postUrl); // prr($atts); prr($postUrl); if (is_wp_error($response) || is_object($response) && isset($response->errors) || is_array($response) && stripos($response['body'], '"error":') !== false) { $ret = $response['body']; } else { $respJ = json_decode($response['body'], true); $ret = array("code" => "OK", "post_id" => $options['pgIntID'] . '_' . $respJ['response']['post_id']); } } if (is_array($ret) && $ret['code'] == 'OK') { if ($postID == '0') { nxs_addToLogN('S', 'Test', $logNT, 'OK - TEST Message Posted '); echo ' OK - Message Posted, please see your VK Page '; } else { nxs_metaMarkAsPosted($postID, 'VK', $options['ii'], array('isPosted' => '1', 'pgID' => $ret['post_id'], 'pDate' => date('Y-m-d H:i:s'))); nxs_addToLogN('S', 'Posted', $logNT, 'OK - Message Posted ', $extInfo); return 200; } } else { if ($postID == '0') { prr($ret); } nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- ' . print_r($ret, true), $extInfo); } }