nxs_class_SNAP_SU

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

Defined (1)

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

/inc-cl/su.api.php  
  1. if (!class_exists("nxs_class_SNAP_SU")) { class nxs_class_SNAP_SU { 
  2.  
  3. var $ntCode = 'SU'; 
  4. var $ntLCode = 'su';  
  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_getSUHeaders($ref, $post=false, $xhr=true) { $hdrsArr = array();  
  11. if ($xhr) $hdrsArr['X-Requested-With']='XMLHttpRequest';  
  12. $hdrsArr['Connection']='keep-alive'; $hdrsArr['Referer']=$ref; 
  13. $hdrsArr['User-Agent']='Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)'; 
  14. if($post) $hdrsArr['Content-Type']='application/x-www-form-urlencoded';  
  15. if ($xhr) $hdrsArr['Accept']='application/json, text/javascript, */*; q=0.01'; else $hdrsArr['Accept']='text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8'; 
  16. $hdrsArr['Origin']='http://www.stumbleupon.com'; 
  17. if (function_exists('gzdeflate')) $hdrsArr['Accept-Encoding']='gzip, deflate, sdch'; $hdrsArr['Accept-Language']='en-US, en;q=0.8'; $hdrsArr['Accept-Charset']='ISO-8859-1, utf-8;q=0.7, *;q=0.3'; return $hdrsArr; 
  18. function nxs_doCheckSU() { global $nxs_suCkArray; $hdrsArr = $this->nxs_getSUHeaders('https://www.stumbleupon.com/submit'); $ckArr = $nxs_suCkArray;  
  19. $response = wp_remote_get('http://www.stumbleupon.com/submit', array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'cookies' => $ckArr));  
  20. if (is_wp_error($response)) { nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- '.print_r($response, true), ''); return "Connection ERROR. Please see log";} 
  21. $response['body'] = htmlentities($response['body'], ENT_COMPAT, "UTF-8"); // $response['body'] = htmlentities($response['body']); prr($response); die(); 
  22. if (isset($response['headers']['location']) && $response['headers']['location']=='/submit/visitor') return 'Bad Saved Login';  
  23. if ( $response['response']['code']=='200' && stripos($response['body'], 'Add a New Page')!==false) {  
  24. /**echo "You are IN"; */ return false;  
  25. } else return 'No Saved Login'; 
  26. return false;  
  27.  
  28. function nxs_doConnectToSU($u, $p) { global $nxs_suCkArray; $hdrsArr = $this->nxs_getSUHeaders('https://www.stumbleupon.com/', false, false); // echo "LOGGIN"; 
  29. $response = wp_remote_get('http://www.stumbleupon.com', array('headers' => $hdrsArr)); $p = substr($p, 0, 16);  
  30. if (is_wp_error($response)) { nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- '.print_r($response, true), ''); return "Connection ERROR. Please see log";} 
  31. $contents = $response['body']; $ckArr = $response['cookies']; //$response['body'] = htmlentities($response['body']); prr($response); die();  
  32. $flds = array(); $flds['username'] = $u; $flds['password'] = $p; 
  33. $hdrsArr = $this->nxs_getSUHeaders('https://www.stumbleupon.com', true, true); 
  34. $r2 = wp_remote_post( 'https://www.stumbleupon.com/api/v2_0/auth/login', array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'body' => $flds, 'cookies' => $ckArr)); 
  35. //prr($flds); prr($ckArr); prr($r2); prr($ckArr);  
  36. if (is_wp_error($r2)) { nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- '.print_r($r2, true), ''); return "Connection ERROR (2). Please see log";} 
  37. if (stripos($r2['body'], ', "_error":"Invalid username') !==false ) { nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- '.print_r($r2['body'], true), ''); return "Invalid username or password";}  
  38. if (stripos($r2['body'], '"_success":true') !==false) { $ckArr[] = new WP_Http_Cookie( array( 'name' => 'SU_REMEMBER', 'value' => urlencode(CutFromTo($r2['body'], '"}, "su_remember":"', '"')) ) ); 
  39. $ckArr[] = new WP_Http_Cookie( array( 'name' => 'seulepage', 'value' => 'oui') ); $nxs_suCkArray=$ckArr; return false; 
  40. } else { nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- '.print_r($r2, true), ''); return "Connection ERROR (3). Please see log"; } 
  41.  
  42. function nxs_doPostToSU($msg, $lnk, $cat, $tags, $nsfw=false) { global $nxs_suCkArray; $r2 = wp_remote_get($lnk);  
  43. $hdrsArr = $this->nxs_getSUHeaders('https://www.stumbleupon.com/submit', false, false); $ckArr = $nxs_suCkArray;  
  44. $response = wp_remote_get('http://www.stumbleupon.com/submit?_nospa=true&_notoolbar=true&_notoolbar=true&_nospa=true', array( 'method' => 'GET', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'cookies' => $ckArr));  
  45. if (is_wp_error($response)) return "Connection ERROR. ".print_r($response, true); $ckArr2 = nxsMergeArraysOV($ckArr, $response['cookies']); //$nxs_suCkArray = $ckArr;  
  46. $contents = $response['body']; //$response['body'] = htmlentities($response['body']);  
  47. //$ckArr = nxsMergeArraysOV($ckArr, $response['cookies']);  
  48. $hdrsArr = $this->nxs_getSUHeaders('https://www.stumbleupon.com/submit', true); 
  49. $frmTxt = CutFromTo($contents, '<form method="post" id="submit-form"', '</form>'); $md = array(); $flds = array(); $mids = ''; // prr($frmTxt); 
  50. while (stripos($frmTxt, '<input')!==false) { $inpField = trim(CutFromTo($frmTxt, '<input', '>')); $name = trim(CutFromTo($inpField, 'name="', '"')); 
  51. if ( stripos($inpField, '"hidden"')!==false && $name!='' && !in_array($name, $md)) { $md[] = $name; $val = trim(CutFromTo($inpField, 'value="', '"')); $flds[$name]= $val; $mids .= "&".$name."=".$val;} 
  52. $frmTxt = substr($frmTxt, stripos($frmTxt, '<input')+8); 
  53. } $flds['url'] = $lnk; $flds['review'] = $msg; $flds['tags'] = $cat; $flds['nsfw'] = $nsfw?'true':'false'; $flds['user-tags'] = $tags; $flds['_output'] = 'Json'; $flds['_method'] = 'create'; $flds['language'] = 'EN';  
  54.  
  55. $r2 = wp_remote_post('https://www.stumbleupon.com/submit', array('method' => 'POST', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'body' => $flds, 'cookies' => $ckArr));  
  56. if (is_wp_error($r2)) return "Connection ERROR. ".print_r($r2, true); $resp = json_decode($r2['body'], true);  
  57.  
  58. if ( isset($resp['_reason']) && is_array($resp['_reason']) && count($resp['_reason'])>0 && stripos($resp['_reason'][0]['message'], 'Failed to add URL')!==false) { sleep(5); 
  59. $r2 = wp_remote_post('https://www.stumbleupon.com/submit', array('method' => 'POST', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'body' => $flds, 'cookies' => $ckArr));  
  60. if (is_wp_error($r2)) return "Connection ERROR. ".print_r($r2, true);$resp = json_decode($r2['body'], true); 
  61.  
  62. if (stripos($resp['_error'], 'Invalid token')!==false) { // In case we got the Wrong Cookies 
  63. $r2 = wp_remote_post('https://www.stumbleupon.com/submit', array('method' => 'POST', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'body' => $flds, 'cookies' => $ckArr2));  
  64. if (is_wp_error($r2)) return "Connection ERROR. ".print_r($r2, true); $resp = json_decode($r2['body'], true); 
  65.  
  66. if (stripos($resp['_reason'][0]['message'], 'Failed to add URL')!==false) { sleep(5); 
  67. $r2 = wp_remote_post('https://www.stumbleupon.com/submit', array('method' => 'POST', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'body' => $flds, 'cookies' => $ckArr2));  
  68. if (is_wp_error($r2)) return "Connection ERROR. ".print_r($r2, true); $resp = json_decode($r2['body'], true); // prr($flds); prr($resp); //nxs_addToLogN('SU', 'E', '-=DBG=- '.print_r($resp, true)." - #####", $extInfo); 
  69. }  
  70. }  
  71.  
  72. if (isset($resp['discovery']['publicid'])) $pageID = $resp['discovery']['publicid']; elseif (isset($resp['discovery']['url']['publicid']))$pageID = $resp['discovery']['url']['publicid'];  
  73. if ($resp['_success']=='1') { $ckArr = nxsMergeArraysOV($ckArr, $r2['cookies']); $nxs_suCkArray = $ckArr; return array("code"=>"OK", "post_id"=>$pageID); }  
  74. elseif (isset($resp['_reason'])) { $resp['_reason']['NXS_FIELDS'] = $flds; $resp['_reason']['NXS_RESP'] = $resp; return $resp['_reason']; } else return "ERROR".print_r($resp, true);  
  75.  
  76. function doPostToNT($options, $message) { global $nxs_suCkArray; $badOut = array('pgID'=>'', 'isPosted'=>0, 'pDate'=>date('Y-m-d H:i:s'), 'Error'=>''); 
  77. //## Check settings 
  78. if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; }  
  79. if (!isset($options['suUName']) || trim($options['suPass'])=='') { $badOut['Error'] = 'Not Configured'; return $badOut; }  
  80. $pass = (substr($options['suPass'], 0, 5)=='n5g9a'?nsx_doDecode(substr($options['suPass'], 5)):$options['suPass']);  
  81. //## Format 
  82. if (!empty($message['pText'])) $msg = $message['pText']; else $msg = nxs_doFormatMsg($options['suMsgFormat'], $message); $urlToGo = (!empty($message['url']))?$message['url']:''; $tags = $message['tags']; 
  83.  
  84. if (isset($options['suSvC'])) $nxs_suCkArray = maybe_unserialize( $options['suSvC']); $loginError = true; 
  85. if (is_array($nxs_suCkArray)) $loginError = $this->nxs_doCheckSU(); if ($loginError!=false) $loginError = $this->nxs_doConnectToSU($options['suUName'], $pass);  
  86. if ($loginError!==false) { $badOut['Error'] = print_r($loginError, true)." - BAD USER/PASS"; return $badOut; }  
  87.  
  88. $ret = $this->nxs_doPostToSU($msg, $urlToGo, $options['suCat'], $tags, $options['nsfw']=='1'); // $extInfo .= "++".$msg."|".$link."|".$options['suCat']."|".$tags."|".$options['nsfw'];  
  89.  
  90. if ($ret=='OK') $ret = array("code"=>"OK", "post_id"=>''); 
  91. if ( (!is_array($ret)) && $ret!='OK') { $badOut['Error'] .= 'Something went wrong - '.print_r($ret, true); }  
  92. elseif (isset($ret['code']) && $ret['code']=='OK') return array('isPosted'=>'1', 'postID'=>$ret['post_id'], 'postURL'=>'http://www.stumbleupon.com/content/'.$ret['post_id'].'/comments', 'pDate'=>date('Y-m-d H:i:s')); else $badOut['Error'] .= 'Error - '.print_r($ret, true); 
  93. return $badOut;  
  94. }  
  95. }}