nxs_class_SNAP_AP

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

Defined (1)

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

/inc-cl/ap.api.php  
  1. if (!class_exists("nxs_class_SNAP_AP")) { class nxs_class_SNAP_AP { 
  2.  
  3. var $ntCode = 'AP'; 
  4. var $ntLCode = 'ap'; 
  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 createFile($imgURL, $auth) { $data = array();  
  10. $remImgURL = urldecode($imgURL); $urlParced = pathinfo($remImgURL); $remImgURLFilename = $urlParced['basename'];  
  11. $imgData = wp_remote_get($remImgURL); if (is_wp_error($imgData)) { $badOut['Error'] = print_r($imgData, true)." - ERROR"; return $badOut; }  
  12. $imgData = $imgData['body']; 
  13. $tmp=array_search('uri', @array_flip(stream_get_meta_data($GLOBALS[mt_rand()]=tmpfile())));  
  14. if (!is_writable($tmp)) return "Your temporary folder or file (file - ".$tmp.") is not witable. Can't upload image to App.Net"; 
  15. rename($tmp, $tmp.='.png'); register_shutdown_function(create_function('', "unlink('{$tmp}');"));  
  16. file_put_contents($tmp, $imgData); if (!$tmp) return 'You must specify a path to a file'; if (!file_exists($tmp)) return 'File path specified does not exist'; 
  17. if (!is_readable($tmp)) return 'File path specified is not readable'; 
  18. if (!array_key_exists('name', $data)) $data['name'] = basename($tmp); 
  19. if (array_key_exists('mime-type', $data)) { $mimeType = $data['mime-type']; unset($data['mime-type']);} else $mimeType = null; 
  20. if (!array_key_exists('kind', $data)) { $test = @getimagesize($tmp);  
  21. if ($test && array_key_exists('mime', $test)) { $data['kind'] = 'image'; if (!$mimeType) $mimeType = $test['mime']; } else $data['kind'] = 'other'; 
  22. if (!$mimeType && function_exists('finfo_open') ) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimeType = finfo_file($finfo, $tmp); finfo_close($finfo); } 
  23. if (!$mimeType) return 'Unable to determine mime type of file, try specifying it explicitly'; $data['type'] = "com.nextscripts.photos"; 
  24. $data['content'] = "@$tmp;type=$mimeType"; 
  25. $url = "https://alpha-api.app.net/stream/0/files?access_token=".$auth;  
  26.  
  27. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); 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. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $response = curl_exec($ch); $errmsg = curl_error($ch); curl_close($ch); //prr($response); 
  30. if ($errmsg!='') return $errmsg; else $response = json_decode($response, true); 
  31. if (!is_array($response) || !isset($response['meta']) || $response['meta']['code']!='200' || $response['data']['file_token']=='') return print_r($response, true); 
  32. return array('id'=>$response['data']['id'], 'file_token'=>$response['data']['file_token'], 'url'=>$response['data']['url']); 
  33.  
  34. function doPostToNT($options, $message) { global $nxs_urlLen; $badOut = array('pgID'=>'', 'isPosted'=>0, 'pDate'=>date('Y-m-d H:i:s'), 'Error'=>''); 
  35. //## Check settings 
  36. if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; }  
  37. if (!isset($options['apAppAuthToken']) || trim($options['apAppAuthToken'])=='') { $badOut['Error'] = 'Not Authorized'; return $badOut; }  
  38. if (empty($options['imgSize'])) $options['imgSize'] = ''; 
  39. //## Format Post 
  40. if (!empty($message['pText'])) $text = $message['pText']; else $text = nxs_doFormatMsg($options['apTextFormat'], $message); $text = nsTrnc($text, 256);  
  41. //## Make Post  
  42. if (isset($message['imageURL'])) $img = trim(nxs_getImgfrOpt($message['imageURL'], $options['imgSize'])); else $img = '';  
  43. if ($options['attchImg']!=false && $img!='') $remoteImg = $this->createFile($img, $options['apAppAuthToken']); $ann = array();  
  44. if (is_array($remoteImg)) { 
  45. $ann[] = array("type"=>"net.app.core.oembed", "value"=> array("+net.app.core.file"=>array("file_id" => $remoteImg['id'], "file_token" => $remoteImg['file_token'], "format"=> "oembed")));  
  46. $url = "https://alpha-api.app.net/stream/0/posts?include_post_annotations=1&access_token=".$options['apAppAuthToken'];  
  47. $flds = array('text' => $text, 'annotations' => $ann); $flds = json_encode($flds); $hdrsArr = array('Content-Type' => 'application/json'); 
  48. $response = wp_remote_post( $url, array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 0, 'headers' => $hdrsArr, 'body' => $flds));  
  49. if (is_wp_error($response)) { $badOut['Error'] = print_r($response, true)." - ERROR"; return $badOut; }  
  50. $response = json_decode($response['body'], true); //prr($response); die();  
  51. //## Check Result 
  52. if (!is_array($response) || !isset($response['meta']) || $response['meta']['code']!='200' || $response['data']['canonical_url']=='') { $badOut['Error'] = print_r($response, true)." - ERROR"; return $badOut; }  
  53. $apNewPostURL = $response['data']['canonical_url']; $apNewPostID = $response['data']['id'];  
  54. if ($apNewPostID!='') {  
  55. return array('postID'=>$apNewPostID, 'isPosted'=>1, 'postURL'=>$apNewPostURL, 'pDate'=>date('Y-m-d H:i:s'));  
  56. } else { $badOut['Error'] .= print_r($tmhOAuth->response['response'], true)." MSG:".print_r($msg, true);  
  57. return $badOut; 
  58. return $badOut; 
  59. }  
  60.  
  61. }}