nxs_class_SNAP_BG

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

Defined (1)

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

/inc-cl/bg.api.php  
  1. if (!class_exists("nxs_class_SNAP_BG")) { class nxs_class_SNAP_BG { 
  2.  
  3. var $ntCode = 'BG'; 
  4. var $ntLCode = 'bg';  
  5.  
  6. function doPost($options, $message) { if (!is_array($options)) return false;  
  7. foreach ($options as $ii=>$ntOpts) $out[$ii] = $this->doPostToNT($ntOpts, $message); 
  8. return $out; 
  9. }  
  10.  
  11. function doPostToNT($options, $message) { $badOut = array('pgID'=>'', 'isPosted'=>0, 'pDate'=>date('Y-m-d H:i:s'), 'Error'=>''); // prr($message); prr($options); 
  12. //## Check API Lib 
  13. //if (!function_exists('doConnectToBlogger')) if (file_exists('apis/postToGooglePlus.php')) require_once ('apis/postToGooglePlus.php'); elseif (file_exists('/home/_shared/deSrc.php')) require_once ('/home/_shared/deSrc.php');  
  14. //## Check settings 
  15. if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; }  
  16. if ( (!isset($options['bgUName']) || empty($options['bgPass'])) && empty($options['AccessToken'])) { $badOut['Error'] = 'Not Configured'; return $badOut; }  
  17. //## Format 
  18. if (!empty($message['pText'])) $msg = $message['pText']; else $msg = nxs_doFormatMsg($options['bgMsgFormat'], $message);  
  19. if (!empty($message['pTitle'])) $msgT = $message['pTitle']; else $msgT = nxs_doFormatMsg($options['bgMsgTFormat'], $message);  
  20. if ($options['bgInclTags']=='1') $tags = nsTrnc($message['tags'], 195, ', ', ''); else $tags = '';  
  21. //## Check/Fix HTML  
  22. if (class_exists('DOMDocument')) {$doc = new DOMDocument(); @$doc->loadHTML('<?xml encoding="UTF-8">' .$msg); $doc->encoding = 'UTF-8'; $msg = $doc->saveHTML(); $msg = CutFromTo($msg, '<body>', '</body>');  
  23. $msg = preg_replace('/<br(.*?)\/?>/', '<br$1/>', $msg); $msg = preg_replace('/<img(.*?)\/?>/', '<img$1/>', $msg); 
  24. require ('apis/htmlNumTable.php'); if (is_array($HTML401NamedToNumeric)) { $msg = strtr($msg, $HTML401NamedToNumeric); $msgT = strtr($msgT, $HTML401NamedToNumeric); } 
  25. }  
  26. $msg = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $msg); $msg = preg_replace('/<!--(.*)-->/Uis', "", $msg); $nxshf = new NXS_HtmlFixer(); $nxshf->debug = false; $msg = $nxshf->getFixedHtml($msg);  
  27. $msg = str_replace("\r\n", "\n", $msg); $msg = str_replace("\n\r", "\n", $msg); $msg = str_replace("\r", "\n", $msg); $msg = str_replace("\n", "<br/>", $msg);  
  28. //## Make Post 
  29. $email = $options['bgUName']; $pass = substr($options['bgPass'], 0, 5)=='b4d7s'?nsx_doDecode(substr($options['bgPass'], 5)):$options['bgPass']; $blogID = $options['bgBlogID']; // prr($msgT); prr($msg); die(); 
  30. if (class_exists('nxsAPI_GP') && !empty($options['bgUName']) && empty($options['APIKey'])) {  
  31. $nt = new nxsAPI_GP(); if(!empty($options['ck'])) $nt->ck = $options['ck']; $nt->debug = false; $loginError = $nt->connect($email, $pass, 'BG');  
  32. if (!$loginError) {  
  33. $result = $nt -> postBG($blogID, $msgT, $msg, $tags);// prr($result);  
  34. } else { $badOut['Error'] = "Login/Connection Error: ". print_r($loginError, true); return $badOut; }  
  35. if (is_array($result) && $result['isPosted']=='1') nxs_save_glbNtwrks('bg', $options['ii'], $nt->ck, 'ck'); 
  36. return $result;  
  37. } else {  
  38. //## Refresh token 
  39. if (function_exists('get_option')) $currTime = time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); else $currTime = time(); 
  40. if ($options['AccessTokenExp']<$currTime) { 
  41. $tknURL = 'https://www.googleapis.com/oauth2/v3/token?refresh_token='.$options['RefreshToken'].'&client_id='.$options['APIKey'].'&client_secret='.$options['APISec'].'&grant_type=refresh_token'; 
  42. $response = wp_remote_post($tknURL); $resp = json_decode($response['body'], true); $options['AccessToken'] = $resp['access_token']; $options['AccessTokenExp'] = $currTime + $resp['expires_in']; 
  43. nxs_save_glbNtwrks('bg', $options['ii'], $resp['access_token'], 'AccessTokenExp'); nxs_save_glbNtwrks('bg', $options['ii'], $options['AccessTokenExp'], 'AccessTokenExp');  
  44. //nxs_addToLogN('S', 'Test', $logNT, 'Token Refreshed '.date('Y-m-d H:i:s', $options['AccessTokenExp'])."|".$tknURL.$options['AccessToken'].print_r($response, true)); 
  45. }  
  46. //## Post 
  47. $post = array("kind"=>"blogger#post", "blog"=>array("id"=>$blogID), "title"=> $msgT, "content" => $msg ); $post = json_encode($post); // prr($post);  
  48. $hdrsArr = array('Content-Type'=>'application/json'); $advSet = array('headers' => $hdrsArr, 'httpversion' => '1.1', 'timeout' => 45, 'redirection' => 0, 'body' => $post);  
  49. $tknURL = 'https://www.googleapis.com/blogger/v3/blogs/'.$blogID.'/posts?access_token='.$options['AccessToken'].''; $ret = ''; $response = wp_remote_post($tknURL, $advSet); //prr($tknURL); prr($response);  
  50. if ((is_object($response) && isset($response->errors))) $badOut['Error'] = print_r($response, true); else $ret = json_decode($response['body'], true); //prr($ret); 
  51. if (is_array($ret) && !empty($ret['id'])) return array('postID'=>$ret['id'], 'isPosted'=>1, 'postURL'=>$ret['url'], 'pDate'=>date('Y-m-d H:i:s'));  
  52. else { $badOut['Error'].= "Error: ".print_r($ret, true); return $badOut;}  
  53. }  
  54. //## Return  
  55. if (is_array($ret) && $ret['post_id']!='') { 
  56. return array('postID'=>$ret['post_id'], 'isPosted'=>1, 'postURL'=>$ret['post_id'], 'pDate'=>date('Y-m-d H:i:s'));  
  57. } else { $badOut['Error'] .= print_r($ret, true);  
  58. return $badOut; 
  59. }}