nxs_class_SNAP_LI

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

Defined (1)

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

/inc-cl/li.api.php  
  1. if (!class_exists("nxs_class_SNAP_LI")) { class nxs_class_SNAP_LI { 
  2.  
  3. var $ntCode = 'LI'; 
  4. var $ntLCode = 'li';  
  5.  
  6. function postShare($tkn, $msg, $title='', $url='', $imgURL='', $dsc='') { $nURL = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token='.$tkn;  
  7. $dsc = nxs_decodeEntitiesFull(strip_tags($dsc)); $msg = strip_tags(nxs_decodeEntitiesFull($msg)); $title = nxs_decodeEntitiesFull(strip_tags($title)); 
  8. $xml = '<?xml version="1.0" encoding="UTF-8"?><share><comment>'.htmlspecialchars($msg, ENT_NOQUOTES, "UTF-8").'</comment>'. 
  9. ($url!=''?'<content><title>'.htmlspecialchars($title, ENT_NOQUOTES, "UTF-8").'</title><submitted-url>'.$url.'</submitted-url>'.(!empty($imgURL)?'<submitted-image-url>'.$imgURL.'</submitted-image-url>':'').'<description>'.htmlspecialchars($dsc, ENT_NOQUOTES, "UTF-8").'</description></content>':''). 
  10. '<visibility><code>anyone</code></visibility></share>'; $hdrsArr = array(); $hdrsArr['Content-Type']='application/xml'; // prr($xml); 
  11. $wprg = array( 'method' => 'POST', 'headers' => $hdrsArr, 'httpversion' => '1.1', 'timeout' => 45, 'redirection' => 0, 'body' => $xml); $wprg['sslverify'] = false;  
  12. $response = wp_remote_post($nURL, $wprg); if (is_wp_error($response) || empty($response['body'])) return "ERROR: ".print_r($response, true);  
  13. $post = json_decode($response['body'], true); return $post;  
  14.  
  15. function postToGroup($tkn, $msg, $title, $groupID, $url='', $imgURL='', $dsc='') { $nURL = 'https://api.linkedin.com/v1/groups/'.$groupID.'/posts?oauth2_access_token='.$tkn;  
  16. $dsc = nxs_decodeEntitiesFull(strip_tags($dsc)); $msg = strip_tags(nxs_decodeEntitiesFull($msg)); $title = nxs_decodeEntitiesFull(strip_tags($title)); 
  17. $xml = '<?xml version="1.0" encoding="UTF-8"?><post><title>'.htmlspecialchars($title, ENT_NOQUOTES, "UTF-8").'</title>'."\n".'<summary>'.htmlspecialchars($msg, ENT_NOQUOTES, "UTF-8").'</summary>'."\n".' 
  18. '.($url!=''?'<content><title>'.htmlspecialchars($title, ENT_NOQUOTES, "UTF-8").'</title>'."\n".'<submitted-url>'.$url.'</submitted-url>'."\n".(!empty($imgURL)?'<submitted-image-url>'.$imgURL.'</submitted-image-url>':'')."\n".'<description>'.htmlspecialchars($dsc, ENT_NOQUOTES, "UTF-8").'</description><category>discussion</category></content><category>discussion</category>':'').'</post>'; $hdrsArr = array(); $hdrsArr['Content-Type']='application/xml';  
  19. $wprg = array( 'method' => 'POST', 'headers' => $hdrsArr, 'httpversion' => '1.1', 'timeout' => 45, 'redirection' => 0, 'body' => $xml); $wprg['sslverify'] = false;  
  20. $response = wp_remote_post($nURL, $wprg);if (is_wp_error($response) || $response['response']['code']!='201') return "ERROR: ".print_r($response, true);  
  21. return array('updateUrl'=>'https://www.linkedin.com/groups?home=&gid='.$groupID); 
  22.  
  23. function doPost($options, $message) { if (!is_array($options)) return false; $out = array(); 
  24. foreach ($options as $ii=>$ntOpts) $out[$ii] = $this->doPostToNT($ntOpts, $message); 
  25. return $out; 
  26. }  
  27. function doPostToNT($options, $message) { $badOut = array('postID'=>'', 'isPosted'=>0, 'pDate'=>date('Y-m-d H:i:s'), 'Error'=>''); $liPostID = ''; 
  28.  
  29. //## Check settings 
  30. if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; }  
  31. if ((!isset($options['ulName']) || trim($options['uPass'])=='') && (empty($options['liOAuthVerifier']))) { $badOut['Error'] = 'Not Configured'; return $badOut; }  
  32. if (empty($options['imgSize'])) $options['imgSize'] = ''; if (empty($options['liMsgFormatT'])) $options['liMsgFormatT'] = '%TITLE%';  
  33. //## Format 
  34. if (!empty($message['pText'])) $msg = $message['pText']; else $msg = nxs_doFormatMsg($options['liMsgFormat'], $message);  
  35. if (!empty($message['pTitle'])) $msgT = $message['pTitle']; else $msgT = nxs_doFormatMsg($options['liMsgFormatT'], $message);  
  36. if(empty($options['postType'])) { if ((int)$options['liAttch'] == 1 || $isNew) $options['postType'] = 'A';} 
  37. if ( $options['postType'] == 'A' || $options['postType'] == 'I') {  
  38. if (isset($message['imageURL'])) $imgURL = trim(nxs_getImgfrOpt($message['imageURL'], $options['imgSize'])); else $imgURL = ''; if (preg_match("/noImg.\.png/i", $imgURL)) $imgURL = '';  
  39. if (!empty($message['urlDescr'])) $dsc = $message['urlDescr']; else $dsc = $msg;  
  40. $dsc = strip_tags($dsc); $dsc = nxs_decodeEntitiesFull($dsc); $dsc = nxs_html_to_utf8($dsc); $dsc = nsTrnc($dsc, 300);  
  41. }  
  42. $msg = strip_tags($msg); $msg = nxs_html_to_utf8($msg); $msgT = nxs_html_to_utf8($msgT); $urlToGo = $message['url']; 
  43.  
  44. if (function_exists("doConnectToLinkedIn") && $options['ulName']!='' && $options['uPass']!='') { 
  45. $dusername = $options['ulName']; $pass = (substr($options['uPass'], 0, 5)=='n5g9a'?nsx_doDecode(substr($options['uPass'], 5)):$options['uPass']); // ??? Do we need that?????? 
  46. $auth = doConnectToLinkedIn($options['ulName'], $options['uPass'], $options['ii']); if ($auth!=false) { $badOut['Error'] .= "|Auth Error - ".$auth; return $badOut; } 
  47. $to = $options['uPage']!=''?$options['uPage']:'https://www.linkedin.com/home'; $lnk = array(); $msg = str_ireplace(' ', ' ', $msg); $msg = nsTrnc(strip_tags($msg), 700); $lnk['postTitle'] = $msgT; 
  48. if ($options['postType'] == 'A') { $lnk['title']=$message['urlTitle']; $lnk['desc'] = $message['urlDescr']; $lnk['url'] = $urlToGo; $lnk['img'] = $imgURL; $lnk['postType'] = 'A';} 
  49. if ($options['postType'] == 'I') { $lnk['title'] = ''; $lnk['desc']=''; $lnk['url'] = $imgURL; $lnk['img'] = $imgURL; $lnk['postType'] = 'I'; $lnk['postTitle'] = $msgT;}  
  50. if ($options['postType'] == 'T') { $lnk['postType'] = 'T'; } 
  51. global $nxs_gCookiesArr; $li = new nxsAPI_LI(); $li->debug = false; if (!empty($nxs_gCookiesArr)) $li->ck = $nxs_gCookiesArr; $ret = $li->post($msg, $lnk, $to);  
  52. if (is_array($ret) && !empty($ret['isPosted'])) return $ret; $liPostID = $options['uPage']; 
  53. } else { 
  54. if (!empty($options['isV2'])) { //## V2 
  55. if ($options['grpID']!='') { 
  56. try { if ($msgT == '') $msgT = ' ';  
  57. if( $options['postType'] == 'A') $ret = $this->postToGroup($options['liAccessToken'], $msg, $msgT, $options['grpID'], str_replace('&', '&', $urlToGo), $imgURL, $dsc);  
  58. else $ret = $this->postToGroup($options['liAccessToken'], $msg, $msgT, $options['grpID']);  
  59. $liPostID= 'http://www.linkedin.com/groups?gid='.$options['grpID']; 
  60. } catch (Exception $o) { $ret="ERROR: ".print_r($o, true); }  
  61. } else { //echo $msg ."|". nsTrnc($msgT, 200) ."|". $urlToGo ."|". $imgURL ."|". $dsc; 
  62. if($options['postType'] == 'A') $ret = $this->postShare($options['liAccessToken'], $msg, nsTrnc($msgT, 200), str_replace('&', '&', $urlToGo), $imgURL, $dsc);  
  63. else $ret = $this->postShare($options['liAccessToken'], $msg); 
  64. }  
  65. } else { //## V1 
  66. require_once ('apis/liOAuth.php'); $linkedin = new nsx_LinkedIn($options['liAPIKey'], $options['liAPISec']); $linkedin->oauth_verifier = $options['liOAuthVerifier']; 
  67. $linkedin->request_token = new nsx_trOAuthConsumer($options['liOAuthToken'], $options['liOAuthTokenSecret'], 1);  
  68. $linkedin->access_token = new nsx_trOAuthConsumer($options['liAccessToken'], $options['liAccessTokenSecret'], 1);  
  69. $msg = nsTrnc($msg, 700); //prr($urlToGo); $urlToGo = urlencode($urlToGo); prr($urlToGo); die(); 
  70. if ($options['grpID']!='') { 
  71. try{ if ($msgT == '') $msgT = ' ';  
  72. if($options['postType'] == 'A') $ret = $linkedin->postToGroup($msg, $msgT, $options['grpID'], str_replace('&', '&', $urlToGo), $imgURL, $dsc);  
  73. else $ret = $linkedin->postToGroup($msg, $msgT, $options['grpID']);  
  74. $liPostID= 'http://www.linkedin.com/groups?gid='.$options['grpID']; if ($ret=='201') $ret = array('updateUrl'=>$liPostID); 
  75. } catch (Exception $o) { $ret="ERROR: ".print_r($o, true); }  
  76. } else { //echo $msg ."|". nsTrnc($msgT, 200) ."|". $urlToGo ."|". $imgURL ."|". $dsc; 
  77. try{ if($options['postType'] == 'A') $ret = $linkedin->postShare($msg, nsTrnc($msgT, 200), str_replace('&', '&', $urlToGo), $imgURL, $dsc); else $ret = $linkedin->postShare($msg); } catch (Exception $o) { $ret="ERROR:".print_r($o, true); }  
  78. }  
  79. }  
  80. if ($liPostID=='') $liPostID = $options['liUserInfo'];  
  81. } // prr($ret); 
  82. if (!is_array($ret) && stripos($ret, '<update-url>')!==false) { $rurl = CutFromTo($ret, '<update-url>', '</update-url>'); $ret = array('updateUrl'=>$rurl); } 
  83. if (is_array($ret) && !empty($ret['updateUrl'])) { if (stripos($ret['updateUrl'], 'topic=')!==false) $liPostID = CutFromTo($ret['updateUrl'], 'topic=', '&'); else $liPostID = '';  
  84. return array('isPosted'=>'1', 'postID'=>$liPostID, 'postURL'=>$ret['updateUrl'], 'pDate'=>date('Y-m-d H:i:s'));  
  85. } else { $badOut['Error'] .= print_r($ret, true); } 
  86. return $badOut;  
  87. }  
  88. }}