nxs_class_SNAP_FB

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

Defined (1)

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

/inc-cl/fb.api.php  
  1. if (!class_exists("nxs_class_SNAP_FB")) { class nxs_class_SNAP_FB { 
  2.  
  3. var $ntCode = 'FB'; 
  4. var $ntLCode = 'fb'; 
  5.  
  6. function doPost($options, $message) { if (!is_array($options)) return false; $out = array(); //return false; 
  7. foreach ($options as $ii=>$ntOpts) $out[$ii] = $this->doPostToNT($ntOpts, $message); 
  8. return $out; 
  9. function doPostToNT($options, $message) { $badOut = array('Warning'=>'', 'Error'=>''); $wprg = array('sslverify'=>false, 'timeout' => 30);  
  10. //## Check settings 
  11. if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; }  
  12. if (empty($options['fbAppAuthToken']) && empty($options['atpKey']) && empty($options['uName'])) { $badOut['Error'] = 'No Auth Token Found/Not configured'; return $badOut; } 
  13. //## Make Post 
  14.  
  15.  
  16. // $facebook = new NXS_Facebook(array( 'appId' => $options['fbAppID'], 'secret' => $options['fbAppSec'], 'cookie' => true ));  
  17. if (!empty($options['fbAppAuthToken'])) if (!isset($options['fbAppPageAuthToken']) || trim($options['fbAppPageAuthToken'])=='') $options['fbAppPageAuthToken'] = $options['fbAppAuthToken']; 
  18.  
  19. //## Some OLD Format Conversion 
  20. if (!isset($options['attachType']) && isset($options['fbAttch'])) $options['attachType'] = $options['fbAttch']; 
  21. if (!isset($options['postType']) && isset($options['fbPostType'])) $options['postType'] = $options['fbPostType']; $fbPostType = $options['postType']; //## Compatibility with v <3.2 
  22. if (!isset($options['pgID']) && isset($options['fbPgID'])) $options['pgID'] = $options['fbPgID'];  
  23.  
  24. if ($fbPostType!='I' && $fbPostType!='T') { $url = $message['url']; $flds = array('id'=>$url, 'scrape'=>'true'); sleep(2); }  
  25. //## Get URL info.  
  26. if ($fbPostType!='I' && $fbPostType!='T' && !empty($options['atchUse']) && $options['atchUse'] == 'F') {  
  27. $response = wp_remote_post('http://graph.facebook.com', array('body' => $flds, 'sslverify'=>false, 'timeout' => 30 ));  
  28. if (is_wp_error($response)) $badOut['Error'] = "Error(URL-Info): ". print_r($response, true); else { $response = json_decode($response['body'], true); // prr($response); die(); 
  29. if (!empty($response['description'])) $message['urlDescr'] = $response['description']; if (!empty($response['title'])) $message['urlTitle'] = $response['title']; 
  30. if (!empty($response['site_name'])) $message['siteName'] = $response['site_name']; elseif ($message['siteName']=='') $message['siteName'] = $message['title']; 
  31. if (!empty($response['image'][0]['url'])) $message['imageURL'] = $response['image'][0]['url']; 
  32. } // prr($message); 
  33. if (!empty($message['pText'])) $msg = $message['pText']; else $msg = nxs_doFormatMsg($options['fbMsgFormat'], $message);  
  34. $imgURL = nxs_getImgfrOpt($message['imageURL']); $fbWhere = 'feed';  
  35. $attachType = $options['attachType']; if ($attachType=='1') $attachType = 'A'; else $attachType = 'S'; 
  36. if ($options['imgUpl']!='2') $options['imgUpl'] = 'T'; else $options['imgUpl'] = 'A';  
  37. if (stripos($options['fbURL'], '/groups/')!=false) $options['destType'] == 'gr'; 
  38.  
  39. if (!empty($options['destType']) && $options['destType'] == 'pr') $page_id = $options['fbAppAuthUser']; else $page_id = $options['pgID'];  
  40. $msg = strip_tags($msg); $msg = str_ireplace('<(")', '<(")', $msg); //## FB Smiles FIX 3 
  41. if (substr($msg, 0, 1)=='@') $msg = ' '.$msg; // ERROR] couldn't open file fix 
  42.  
  43. //## Own App Post 
  44. if (!empty($options['fbAppPageAuthToken'])) { 
  45. if (empty($options['appsecret_proof'])) $options['appsecret_proof'] = hash_hmac('sha256', $options['fbAppPageAuthToken'], $options['fbAppSec']);  
  46. $mssg = array('access_token'=>$options['fbAppPageAuthToken'], 'appsecret_proof'=>$options['appsecret_proof'], 'method'=>'post', 'message'=>$msg); 
  47. if ($fbPostType=='I' && trim($imgURL)=='') $fbPostType='T'; 
  48. if ($fbPostType=='A' && !(preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:, .;]*[-a-z0-9+&@#\/%=~_|]/i", $message['url']))) {  
  49. $badOut['Warning'] = 'Unvalid URL: '.$message['url'].'| Will be posting as text message'; $fbPostType='T';  
  50. }  
  51. if ($fbPostType=='A' || $fbPostType=='') { 
  52. if (($attachType=='A' || $attachType=='S')) { $message['urlTitle'] = nsTrnc($message['urlTitle'], 250, " ", "..."); 
  53. $attArr = array('name' => $message['urlTitle'], 'caption' => $message['siteName'], 'link' =>$message['url'], 'description' => $message['urlDescr']); $mssg = array_merge($mssg, $attArr); ;  
  54. if ($attachType=='A') $mssg['actions'] = json_encode(array('name' => $message['siteName'], 'link' =>$message['url']));  
  55. if (trim($imgURL)!='') $mssg['picture'] = $imgURL; //if (trim($message['videoURL'])!='') $mssg['source'] = $message['videoURL'];  
  56. } elseif ($fbPostType=='I') { /** $facebook->setFileUploadSupport(true); */ $fbWhere = 'photos'; $mssg['url'] = $imgURL;  
  57. if ($options['imgUpl']=='T') { //## Try to Post to TImeline 
  58. $aacct = array('access_token'=>$options['fbAppPageAuthToken'], 'appsecret_proof'=>$options['appsecret_proof'], 'method'=>'get');  
  59. $res = wp_remote_get( "https://graph.facebook.com/$page_id/albums?".http_build_query($aacct, null, '&'), $wprg);  
  60. if (is_wp_error($res) || empty($res['body'])) $badOut['Error'] = ' [ERROR(Albums)] '.print_r($res, true); else { 
  61. $albums = json_decode($res['body'], true); if (empty($albums)) $badOut['Error'] .= "JSON ERROR (Albums): ".print_r($res, true); else { 
  62. if (is_array($albums) && is_array($albums["data"])) foreach ($albums["data"] as $album) { if ($album["type"] == "wall") { $chosen_album = $album; break;}} 
  63. if (isset($chosen_album) && isset($chosen_album["id"])) $page_id = $chosen_album["id"]; 
  64. }  
  65. }  
  66. //## Actual Post  
  67. $destURL = "https://graph.facebook.com/$page_id/".$fbWhere; // prr($destURL); prr($args); prr($mssg); //die(); 
  68. $response = wp_remote_post( $destURL, array( 'method' => 'POST', 'httpversion' => '1.1', 'timeout' => 30, 'sslverify'=>false, 'redirection' => 0, 'body' => $mssg));  
  69. }  
  70.  
  71. if (is_wp_error($response) || empty($response['body'])) return "ERROR: ".print_r($response, true); 
  72. $res = json_decode($response['body'], true); if (empty($res)) return "JSON ERROR: ".print_r($response, true); 
  73. if (!empty($res['error'])) if (!empty($res['error']['message'])) { $badOut['Error'] .= $res['error']['message']; //## Some Known Errors 
  74. if (stripos($res['error']['message'], 'This API call requires a valid app_id')!==false) {  
  75. if ( !is_numeric($page_id) && stripos($options['fbURL'], '/groups/')!=false) $badOut['Error'] .= ' [ERROR] Unrecognized Facebook Group ID. Please use numeric ID. Please see <a href="http://gd.is/f412">FAQ 4.12</a>';  
  76. else $badOut['Error'] .= " [ERROR] (invalid app_id) Authorization Error. <br/>\r\n<br/>\r\n Possible Reasons: <br/>\r\n 1. Your app is not authorized. Please go to the Plugin Settings - Facebook and authorize it.<br/>\r\n 2. The current authorized user have no rights to post to the specified page. Please login to Facebook as the correct user and Re-Authorize the Plugin.<br/>\r\n 3. You clicked 'Skip' or unchecked the 'Manage Pages' or 'Post on your behalf' permissions when Authorization wizard asked you. Please Re-Authorize the Plugin<br/>\r\n";  
  77. if (stripos($res['error']['message'], 'Some of the aliases you requested do not exist')!==false) $badOut['Error'] .= '| Please check what do you have in the "Facebook URL" field.'; 
  78. if (stripos($res['error']['message'], 'Unsupported post request')!==false) $badOut['Error'] .= "<br/>\r\n".'| Are you posting to a secret group? Please see: <a href="http://gd.is/fbe2">http://gd.is/fbe2</a>'; 
  79. if (stripos($res['error']['message'], 'The target user has not authorized this action')!==false) $badOut['Error'] .= '| Please Authorize the plugin from the plugin settings Page - Facebook.'; 
  80.  
  81. return $badOut;  
  82. } else return print_r($res['error'], true); 
  83. if (empty($res['id'])) return print_r($res, true); 
  84. //## All Good! 
  85. $pgID = (isset($res['post_id']) && strpos($res['post_id'], '_')!==false)?$res['post_id']:$res['id']; $pgg = explode('_', $pgID); $postID = $pgg[1]; 
  86. $pgURL = 'http://www.facebook.com/'.$options['pgID'].'/posts/'.$postID;  
  87. return array('isPosted'=>'1', 'postID'=>$pgID, 'postURL'=>$pgURL, 'pDate'=>date('Y-m-d H:i:s'), 'log'=>$badOut);  
  88. }}