nxs_class_SNAP_VK

The NextScripts: Social Networks Auto-Poster nxs class SNAP VK class.

Defined (1)

The class is defined in the following location(s).

/inc-cl/vk.api.php  
  1. if (!class_exists("nxs_class_SNAP_VK")) { class nxs_class_SNAP_VK { 
  2.  
  3. var $ntCode = 'VK'; 
  4. var $ntLCode = 'vk';  
  5.  
  6. function doPost($options, $message) { if (!is_array($options)) return false; $out = array(); 
  7. foreach ($options as $ii=>$ntOpts) $out[$ii] = $this->doPostToNT($ntOpts, $message); 
  8. return $out; 
  9. }  
  10. function nxs_uplImgtoVK($imgURL, $options) { 
  11. $postUrl = 'https://api.vk.com/method/photos.getWallUploadServer?gid='.(str_replace('-', '', $options['pgIntID'])).'&access_token='.$options['vkAppAuthToken']; 
  12. $response = wp_remote_get($postUrl); $thumbUploadUrl = $response['body'];  
  13. if (!empty($thumbUploadUrl)) { $thumbUploadUrlObj = json_decode($thumbUploadUrl); $VKuploadUrl = $thumbUploadUrlObj->response->upload_url; } // prr($thumbUploadUrlObj); echo "UURL=====-----"; 
  14. if (!empty($VKuploadUrl)) {  
  15. // if (stripos($VKuploadUrl, '//pu.vkontakte.ru/c')!==false) { $c = 'c'.CutFromTo($VKuploadUrl, '.ru/c', '/'); $VKuploadUrl = str_ireplace('/pu.', '/'.$c.'.', str_ireplace($c.'/', '', $VKuploadUrl)); } 
  16. $remImgURL = urldecode($imgURL); $urlParced = pathinfo($remImgURL); $remImgURLFilename = $urlParced['basename']; $imgData = wp_remote_get($remImgURL);  
  17. if(is_wp_error($imgData) || empty($imgData['body']) || (!empty($imgData['headers']['content-length']) && (int)$imgData['headers']['content-length']<200) ||  
  18. $imgData['headers']['content-type'] == 'text/html' || $imgData['response']['code'] == '403' ) { $options['attchImg'] = 0;  
  19. nxs_addToLogN('E', 'Error', $logNT, 'Could not get image ( '.$remImgURL.' ), will post without it - ', print_r($imgData, true)); return 'Image Upload Error, please see log'; 
  20. } $imgData = $imgData['body'];  
  21.  
  22. $tmp=array_search('uri', @array_flip(stream_get_meta_data($GLOBALS[mt_rand()]=tmpfile())));  
  23. if (!is_writable($tmp)) return "Your temporary folder or file (file - ".$tmp.") is not writable. Can't upload image to VK"; 
  24. rename($tmp, $tmp.='.png'); register_shutdown_function(create_function('', "unlink('{$tmp}');"));  
  25. file_put_contents($tmp, $imgData);  
  26.  
  27. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $VKuploadUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  28. global $nxs_skipSSLCheck; if ($nxs_skipSSLCheck===true) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  29.  
  30. if (function_exists('curl_file_create')) { $file = curl_file_create($tmp); curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => $file)); }  
  31. else curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => '@' . $tmp)); 
  32.  
  33. $response = curl_exec($ch); $errmsg = curl_error($ch); curl_close($ch); //prr($response); 
  34.  
  35. $uploadResultObj = json_decode($response); // prr($response); //prr($uploadResultObj); 
  36.  
  37. if (!empty($uploadResultObj->server) && !empty($uploadResultObj->photo) && !empty($uploadResultObj->hash)) { 
  38. $postUrl = 'https://api.vk.com/method/photos.saveWallPhoto?server='.$uploadResultObj->server.'&photo='.$uploadResultObj->photo.'&hash='.$uploadResultObj->hash.'&gid='.(str_replace('-', '', $options['pgIntID'])).'&access_token='.$options['vkAppAuthToken']; 
  39. $response = wp_remote_get($postUrl);  
  40. $resultObject = json_decode($response['body']); //prr($resultObject); 
  41. if (isset($resultObject) && isset($resultObject->response[0]->id)) { return $resultObject->response[0]; } else { return 'Image Upload Error'; } 
  42. function doPostToNT($options, $message) { $badOut = array('pgID'=>'', 'isPosted'=>0, 'pDate'=>date('Y-m-d H:i:s'), 'Error'=>''); global $nxs_vkCkArray; //prr($message); die(); 
  43. //## Check settings 
  44. if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; }  
  45. if (empty($options['imgSize'])) $options['imgSize'] = ''; 
  46. if ((!isset($options['uName']) || trim($options['uPass'])=='') && (!isset($options['vkAppAuthToken']) || trim($options['vkAppAuthToken'])=='')) { $badOut['Error'] = 'Not Configured'; return $badOut; }  
  47. $pass = (substr($options['uPass'], 0, 5)=='n5g9a'?nsx_doDecode(substr($options['uPass'], 5)):$options['uPass']);  
  48. //## Format 
  49. if (!empty($message['pText'])) $msg = $message['pText']; else $msg = nxs_doFormatMsg($options['msgFrmt'], $message); $urlToGo = (!empty($message['url']))?$message['url']:'';  
  50. $postType = $options['postType']; //$link = urlencode($link); $desc = urlencode(substr($msg, 0, 500));  
  51. if (isset($message['imageURL'])) $imgURL = trim(nxs_getImgfrOpt($message['imageURL'], $options['imgSize'])); else $imgURL = ''; 
  52. $msgOpts = array(); $msgOpts['uid'] = $options['vkPgID']; // if ($link!='') $msgOpts['link'] = $link;  
  53. if (!empty($message['videoURL']) && $postType=="I") { $postType='A'; $urlToGo=$message['videoURL']; $msgOpts['vID'] = $vids[0]; }  
  54. if ($postType=='I' && trim($imgURL)=='') $postType='T'; $msgOpts['type'] = $postType;  
  55. if (function_exists('nxs_doPostToVK') && !empty($options['uName']) && !empty($pass) && $postType=='A' && $urlToGo!='') { $postType = 'T';  
  56. //## Login 
  57. if (isset($options['vkSvC'])) $nxs_vkCkArray = maybe_unserialize( $options['vkSvC']); $loginError = true;  
  58. if (is_array($nxs_vkCkArray)) $loginError = nxs_doCheckVK(); if ($loginError!=false) {  
  59. if (!empty($options['vkPh'])) { $replArr = explode(' ... ', $options['vkPhReq']); $ph = $options['vkPh'];  
  60. $ln = strlen($replArr[0]); if (substr($ph, 0, $ln)==$replArr[0]) $ph = substr($ph, $ln);  
  61. $ln = strlen($replArr[1]); $mln = -$ln; if (substr($ph, $mln)==$replArr[1]) $ph = substr($ph, $ln, $mln); 
  62. } else $ph = ''; $loginError = nxs_doConnectToVK($options['uName'], $pass, $ph);  
  63. } // prr($loginError); 
  64. if ($loginError!==false) { if (stripos($loginError, 'Phone verification required:')!==false) return $loginError; else return "ERROR - BAD USER/PASS - ".print_r($loginError, true); } 
  65. //## Post  
  66. $msgOpts['url'] = $urlToGo; $msgOpts['urlTitle'] = $message['urlTitle']; $msgOpts['urlDesc'] = $message['urlDescr']; $msgOpts['imgURL'] = $imgURL; //prr($msgOpts); 
  67. $ret = nxs_doPostToVK($msg, $options['url'], $msgOpts);  
  68. 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'));  
  69. else { $badOut['Error'] .= 'ERROR: '.print_r($ret, true); $ret=''; } 
  70. } //prr($postType); 
  71. 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); } 
  72.  
  73. if ($postType!='A') { if( $options['addBackLink']=='1') $atts[] = $urlToGo;  
  74. if (is_array($atts)) $atts = implode(', ', $atts); $postUrl = 'https://api.vk.com/method/wall.post'; 
  75. $postArr = array('owner_id'=>$options['pgIntID'], 'access_token'=>$options['vkAppAuthToken'], 'from_group'=>'1', 'message'=>$msg, 'attachment'=>$atts); 
  76. $response = wp_remote_post($postUrl, array('body' => $postArr)); // prr($postArr); prr($response); 
  77. if ( is_wp_error($response) || (is_object($response) && (isset($response->errors))) || (is_array($response) && stripos($response['body'], '"error":')!==false )) {  
  78. $badOut['Error'] .= 'Error: '. print_r($response, true); 
  79. } else { $respJ = json_decode($response['body'], true); $ret = $options['pgIntID'].'_'.$respJ['response']['post_id']; } 
  80.  
  81. }  
  82. if (isset($ret) && $ret!='') return array('postID'=>$ret, 'isPosted'=>1, 'postURL'=>'http://vk.com/wall'.$ret, 'pDate'=>date('Y-m-d H:i:s'), 'err'=>$badOut['Error']);  
  83. return $badOut;  
  84. }  
  85. }}