bootsrap inprogress
[Anonymous-Twitter-Board.git] / class / twitter-connection.php
blobf2203c59da5da1b12c455f2f29f69c40f38cb6f8
1 <?php
3 /*
4 A NOTE ON TWITTER ERRORS:
5 ERROR 215 TYPICALLY MEANS YOU GOT THE AUTHENTICATION STRING FORAMT WRONG
6 ERROR 32 MEANS YOU GOT THE VALUES WRONG
7 */
9 class TwitterConnection{
10 private $oauth_data = array();
11 private $user_info = array();
12 private $post_properties = array();
14 function getPostProperties(){
15 return $this->post_properties;
18 private $path_prefix ="";
20 private $media_api = "https://upload.twitter.com/1.1/media/upload.json";
21 private $status_api = "https://api.twitter.com/1.1/statuses/update.json";
22 private $user_timeline_api = "https://api.twitter.com/1.1/statuses/user_timeline.json";
23 private $mention_timeline_api = "https://api.twitter.com/1.1/statuses/mentions_timeline.json";
24 private $oembed_api = "https://publish.twitter.com/oembed";
25 private $default_status_string = "https://twitter.com/:USERNAME/status/";
27 function __construct($path_prefix = ""){
28 $this->path_prefix = $path_prefix;
29 require_once("additional-functions.php");
30 require_once("board-functions.php");
31 require_once("database-connection.php");
33 $this->oauth_data = StandardFunctions::getIniFile($path_prefix . "settings/keys.ini");
34 $this->user_info = StandardFunctions::getIniFile($path_prefix . "settings/userinfo.ini");
35 $this->post_properties = StandardFunctions::getIniFile($path_prefix . "settings/postproperties.ini");
36 $this->buildStatusString();
39 function endConnection(){
40 $this->database_connection = null;
43 //base64 processing
44 function removeExtraSymbols($string){
45 $string = str_replace(".", "a", $string);
46 $string = str_replace("%", "b", $string);
47 $string = str_replace("/", "c", $string);
48 return $string;
51 function buildStatusString(){
52 $this->default_status_string = str_replace(":USERNAME", $this->user_info["User-Name"], $this->default_status_string);
55 function getEmbededTweet($post_id){
56 $query_string = "url=" . rawurlencode($this->default_status_string . "$post_id") . "&maxwidth=" . 550;
57 $curl = curl_init($this->oembed_api . "?$query_string");
58 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
59 $content = curl_exec($curl);
61 return json_decode($content,true);
64 function retrieveTimeline(){
65 $highest_post_id = -1;
66 echo $this->post_properties["TopPostNo"];
67 $timeline_arr = $this->getUserTimeline($this->post_properties["TopPostNo"]);
68 $timeline_database_arr = array();
69 echo "<pre>";
71 if($timeline_arr["errors"][0]["code"] == null && sizeof($timeline_arr) != 0){
72 foreach ($timeline_arr as $timeline_item){
73 $post_id = $timeline_item["id"];
74 $post_text = $timeline_item["text"];
75 $post_image_string = $this->grabTwitterImage($timeline_item);
76 array_push($timeline_database_arr, [$post_id, $post_text, $post_image_string]);
78 $highest_post_id = $post_id > $highest_post_id ? $post_id : $highest_post_id;
81 else echo $timeline_arr["errors"][0]["code"] . "Tim<br/>";
82 //var_dump($timeline_arr);
84 echo "<hr>"; //From the post ID try and find any replies
86 $reply_arr = $this->getTweetReplies($this->post_properties["TopPostNo"]);
87 $reply_arr_container = array();
89 if($reply_arr["errors"][0]["code"] == null && sizeof($reply_arr) != 0){
90 $reply_id = 0;
91 foreach($reply_arr as $reply_item){
92 $reply_id = $reply_item["id"];
93 $reply_text = $reply_item["text"];
94 $reply_image_string = $this->grabTwitterImage($reply_item);
95 $responding_to_id = $reply_item['in_reply_to_status_id'];
96 //echo "$reply_id -- $responding_to_id<hr/>";
97 array_push($reply_arr_container, [$reply_id, $reply_text, $reply_image_string, $responding_to_id]);
99 $highest_post_id = $reply_id > $highest_post_id ? $reply_id : $highest_post_id;
102 else echo $reply_arr["errors"][0]["code"] . "Rep<br/>";
104 if(sizeof($timeline_database_arr) + sizeof($reply_arr_container) == 0){
105 echo "No Updates<hr/>";
106 return;
108 else{
109 $postfile = fopen($this->path_prefix . "settings/postproperties.ini", "w");
110 echo "TopPostNo=$highest_post_id<br/>";
111 var_dump($postfile);
112 fwrite($postfile, "TopPostNo=$highest_post_id\n" . "Catalog-Size=" . $this->post_properties["Catalog-Size"]);
115 $combined_database_arr = array_merge ($timeline_database_arr, $reply_arr_container);
116 return $combined_database_arr;
119 function grabTwitterImage($tweet_array){
120 $first_join = false;
121 $image_url_string = null;
122 echo "<hr/>";
123 if($tweet_array["extended_entities"] != null){
124 foreach($tweet_array["extended_entities"] ["media"] as $entity){
125 $filename_url ="";
126 if(isset($entity["video_info"])){
127 $filename_url = $entity["video_info"]["variants"][0]["url"];
128 $extention = pathinfo($filename_url, PATHINFO_EXTENSION );
130 else{
131 $filename_url = $entity["media_url_https"];
132 $extention = pathinfo($filename_url , PATHINFO_EXTENSION );
135 $filename = "../images/" . (microtime(true) * 10000) . (rand(0,10000)) . ".$extention";
136 echo "$filename_url: " .$filename;
138 BoardFunctions::uploadMedia($filename, $filename_url);
139 if(!$first_join){
140 $first_join = true;
141 $image_url_string = rawurlencode($filename);
143 else $image_url_string .= "," . rawurlencode($filename);
148 return $image_url_string;
151 function getUserTimeline($since_id = 976628662446551043, $count = 100){
153 $random_value = OauthRandom::randomAlphaNumet(32);
154 $method = "HMAC-SHA1";
155 $oauth_version = "1.0";
156 $timestamp = time();
157 $reply_exclude = "false";
159 echo $this->user_info["User-ID"];
161 $get_fields = "since_id=" . $since_id . "&count=" . $count . "&include_rts=false&exclude_replies=$reply_exclude&user_id=" . $this->user_info["User-ID"];
162 //$msg_len = (strlen($this->user_timeline_api . "?$get_fields")); //GET REQUESTS HAVE NO DYNAMIC LENGTH
164 $param_array = array( "user_id" => $this->user_info["User-ID"],
165 "since_id" => "$since_id",
166 "exclude_replies"=>"$reply_exclude",
167 "count" => "$count",
168 "include_rts" => "false",
169 "oauth_version" => "$oauth_version",
170 "oauth_nonce"=>"$random_value",
171 "oauth_token"=> $this->oauth_data["oauth_token"],
172 "oauth_timestamp" => "$timestamp",
173 "oauth_consumer_key" => $this->oauth_data["oauth_consumer_key"],
174 "oauth_signature_method" => "$method"
177 $signature = rawurlencode($this->generateSignature(array(
178 "base_url" => $this->user_timeline_api,
179 "request_method" => "GET"),
180 $param_array,
181 array(
182 "consumer_secret" => $this->oauth_data["consumer_secret"],
183 "oauth_secret" => $this->oauth_data["oauth_secret"]
187 $param_array["oauth_signature"] = $signature;
188 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,
189 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", "Host: api.twitter.com",
190 $this->buildAuthorizationString($param_array));
192 //request
193 echo "<hr/>";
194 $curl = curl_init($this->user_timeline_api . "?$get_fields");
195 curl_setopt($curl, CURLOPT_HTTPGET, 1);
196 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
197 curl_setopt($curl, CURLOPT_HEADER, false);
198 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
199 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);
201 echo "<br/>-- Fin -- <hr/>";
202 $content = curl_exec($curl);
203 return json_decode(($content), true);
208 function getTweetReplies($current_post_id, $max_post_id = -1){
209 $random_value = OauthRandom::randomAlphaNumet(32);
210 $method = "HMAC-SHA1";
211 $oauth_version = "1.0";
212 $timestamp = time();
215 $get_fields = "since_id=" . $current_post_id;
216 $param_array = array(
217 "since_id" => "$current_post_id",
218 "oauth_version" => "$oauth_version",
219 "oauth_nonce"=>"$random_value",
220 "oauth_token"=> $this->oauth_data["oauth_token"],
221 "oauth_timestamp" => "$timestamp",
222 "oauth_consumer_key" => $this->oauth_data["oauth_consumer_key"],
223 "oauth_signature_method" => "$method"
225 if($max_post_id > 0){
226 $get_fields .= $max_post_id > 0 ? "&max_id=" . $max_post_id : "";
227 $param_array["max_id"] = "$max_post_id";
230 $signature = rawurlencode($this->generateSignature(array(
231 "base_url" => $this->mention_timeline_api,
232 "request_method" => "GET"),
233 $param_array,
234 array(
235 "consumer_secret" => $this->oauth_data["consumer_secret"],
236 "oauth_secret" => $this->oauth_data["oauth_secret"]
239 $param_array["oauth_signature"] = $signature;
240 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,
241 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", "Host: api.twitter.com",
242 $this->buildAuthorizationString($param_array));
244 //request
245 $curl = curl_init($this->mention_timeline_api . "?$get_fields");
246 curl_setopt($curl, CURLOPT_HTTPGET, 1);
247 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
248 curl_setopt($curl, CURLOPT_HEADER, false);
249 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
250 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER , false);
251 $content = curl_exec($curl);
252 return json_decode(($content), true);
258 function buildAuthorizationString($parameters){
259 $authorization_string = 'Authorization: OAuth ';
261 ksort($parameters);
262 $is_first_join = false;
263 foreach($parameters as $key => $value){
264 if(!$is_first_join){
265 $is_first_join = true;
266 $authorization_string .= $key . '="' . $value . '"';
268 else{
269 $authorization_string .= "," . $key . '="' . $value . '"';
272 return $authorization_string;
275 function makeTweet($comment, $file_arr){
276 $image_string = $this->addTweetMedia($file_arr);
278 //access info
279 $request_method = "POST";
281 //message info
282 $encode_tweet_msg = rawurlencode($comment);
283 $include_entities = "true";
285 //append to postfield_string the media code via media_ids=$media_id
286 $postfield_string = "include_entities=$include_entities&status=$encode_tweet_msg&media_ids=$image_string";
287 $msg_len = (strlen($postfield_string));
289 $random_value = OauthRandom::randomAlphaNumet(32);
290 $method = "HMAC-SHA1";
291 $oauth_version = "1.0";
292 $timestamp = time();
294 $param_array = array("include_entities" => "$include_entities",
295 "status" => "$encode_tweet_msg",
296 "media_ids" => "$image_string",
297 "oauth_version" => "$oauth_version",
298 "oauth_nonce"=>"$random_value",
299 "oauth_token"=> $this->oauth_data["oauth_token"],
300 "oauth_timestamp" => "$timestamp",
301 "oauth_consumer_key" => $this->oauth_data["oauth_consumer_key"],
302 "oauth_signature_method" => "$method"
305 //add media id to the signature
306 $signature = rawurlencode($this->generateSignature(array(
307 "base_url" => $this->status_api,
308 "request_method" => $request_method),
309 $param_array,
310 array(
311 "consumer_secret" => $this->oauth_data["consumer_secret"],
312 "oauth_secret" => $this->oauth_data["oauth_secret"]
316 $param_array["oauth_signature"] = $signature;
317 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,
318 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
319 "Content-Length: $msg_len", "Host: api.twitter.com",
320 $this->buildAuthorizationString($param_array));
322 //request
323 echo "<hr/>";
324 $curl = curl_init($this->status_api);
325 curl_setopt($curl, CURLOPT_POST, 1);
326 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
327 curl_setopt($curl, CURLOPT_POSTFIELDS, $postfield_string);
328 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
329 echo "<br/>-- Fin -- <hr/>";
330 $content = curl_exec($curl);
331 //var_dump (json_decode($content, true));
332 return json_decode($content, true);
335 function addTweetMedia($file_arr){
337 //image info
338 $image_string = "";//delimited by ',' commas
339 for($file = 0 ; $file < count($file_arr) ; $file++){
340 if($file_arr[$file] != ""){
341 //create data in binary/b64
342 $mime_type = pathinfo($file_arr[$file], PATHINFO_EXTENSION);
343 $file_arr[$file] = urldecode($file_arr[$file]);
344 $binary = file_get_contents($this->path_prefix . $file_arr[$file]);
346 $base64 = base64_encode($binary);
348 //upload file to twitter and get id for use in files
349 $size = filesize( $this->path_prefix . $file_arr[$file]);
350 if($file == 0)
351 $image_string = $this->getMediaID($base64, $size, 'image/' . $mime_type);
352 else
353 $image_string .= "," . $this->getMediaID($base64, $size, 'image/' . $mime_type);
356 return rawurlencode($image_string);
359 function getMediaID($base64, $size, $mime_type){
360 $random_value = OAuthRandom::randomAlphaNumet(32);
361 $timestamp = time();
363 echo "<br/><br/>";
364 /////////////MAKE INIT////////////
365 //post data
366 $media_id = $this->mediaInit($size, $mime_type, $random_value, $timestamp);
368 echo "<br/><br/>";
370 /////////////MAKE APPEND////////////
371 //post data
372 $this->mediaAppend($base64, $media_id, $random_value, $timestamp);
374 echo "<br/><br/>";
376 /////////////MAKE FINAL/
377 $this->makeFinal($media_id, $random_value, $timestamp);
378 echo "<br/><br/>";
380 return $media_id ;
383 function mediaInit($size, $mime, $random_value, $timestamp){
384 $command = "INIT";
385 $method = "HMAC-SHA1";
386 $oauth_version = "1.0";
388 $postfield_string = "command=$command&total_bytes=$size&media_type=$mime";
390 $msg_len = (strlen($postfield_string));
391 //header data
392 // BUILD SIGNATURE
393 $signature = rawurlencode($this->generateSignature(array(
394 "base_url" => $this->media_api,
395 "request_method" => "POST"),
396 array(
397 "command" => "$command",
398 "total_bytes" => "$size",
399 "media_type" => "$mime",
400 "oauth_version" => "$oauth_version",
401 "oauth_nonce"=>"$random_value",
402 "oauth_token"=> $this->oauth_data["oauth_token"],
403 "oauth_timestamp" => "$timestamp",
404 "oauth_consumer_key" => $this->oauth_data["oauth_consumer_key"],
405 "oauth_signature_method" => "$method",
407 array(
408 "consumer_secret" => $this->oauth_data["consumer_secret"],
409 "oauth_secret" => $this->oauth_data["oauth_secret"]
415 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
416 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
417 "Content-Length: $msg_len", "Host: upload.twitter.com",
418 'Authorization: OAuth oauth_consumer_key="' . $this->oauth_data["oauth_consumer_key"] .'",oauth_nonce="' . $random_value . '",oauth_signature="' .
419 $signature . '",oauth_signature_method="' .$method . '"' . ',oauth_timestamp="' . $timestamp . '",oauth_token="' . $this->oauth_data["oauth_token"] . '",oauth_version="' . $oauth_version . '"'
422 //request
423 $curl = curl_init($this->media_api);
424 curl_setopt($curl, CURLOPT_POST, 1);
425 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
426 curl_setopt($curl, CURLOPT_POSTFIELDS, $postfield_string);
427 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
428 $media_id_arr = json_decode(curl_exec($curl), true);
429 print_r ($media_id_arr);
430 return $media_id_arr["media_id_string"];
433 function mediaAppend(&$binary_file, $media_id, $random_value, $timestamp){
434 $command = "APPEND";
435 $method = "HMAC-SHA1";
436 $oauth_version = "1.0";
438 $segment_index = 0;
440 //header data
441 // BUILD SIGNATURE
442 $signature = rawurlencode($this->generateSignature(array(
443 "base_url" => $this->media_api,
444 "request_method" => "POST"),
445 array(
446 "command" => "$command",
447 "media" => "$binary_file",
448 "media_id"=>"$media_id",
449 "segment_index"=>"$segment_index",
450 "oauth_version" => "$oauth_version",
451 "oauth_nonce"=>"$random_value",
452 "oauth_token"=> $this->oauth_data["oauth_token"],
453 "oauth_timestamp" => "$timestamp",
454 "oauth_consumer_key" => $this->oauth_data["oauth_consumer_key"],
455 "oauth_signature_method" => "$method",
457 array(
458 "consumer_secret" => $this->oauth_data["consumer_secret"],
459 "oauth_secret" => $this->oauth_data["oauth_secret"]
464 $postfield_string = "media=" . rawurlencode($binary_file) . "&command=$command&media_id=$media_id&segment_index=$segment_index" ;
465 $msg_len = (strlen($postfield_string));
466 $header_data = array("Except:", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
467 "Content-Type: application/x-www-form-urlencoded",
468 "Content-Length: $msg_len", "Host: upload.twitter.com",
469 'Authorization: OAuth oauth_consumer_key="' . $this->oauth_data["oauth_consumer_key"] .'",oauth_nonce="' . $random_value . '",oauth_signature="' .
470 $signature . '",oauth_signature_method="' .$method . '"' . ',oauth_timestamp="' . $timestamp . '",oauth_token="' . $this->oauth_data["oauth_token"] . '",oauth_version="' . $oauth_version . '"'
472 //request
473 $curl = curl_init($this->media_api);
474 curl_setopt($curl, CURLOPT_POST, 1);
475 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
476 curl_setopt($curl, CURLOPT_POSTFIELDS, $postfield_string);
477 curl_setopt($curl, CURLOPT_HEADER , true);
478 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
479 $http_response = curl_exec($curl);
480 echo $http_response;
483 function makeFinal($media_id, $random_value, $timestamp){
484 $command = "FINALIZE";
485 $method = "HMAC-SHA1";
486 $oauth_version = "1.0";
488 $signature = rawurlencode($this->generateSignature(array(
489 "base_url" => $this->media_api,
490 "request_method" => "POST"),
491 array(
492 "command" => "$command",
493 "media_id"=>"$media_id",
494 "oauth_version" => "$oauth_version",
495 "oauth_nonce"=>"$random_value",
496 "oauth_token"=> $this->oauth_data["oauth_token"],
497 "oauth_timestamp" => "$timestamp",
498 "oauth_consumer_key" => $this->oauth_data["oauth_consumer_key"],
499 "oauth_signature_method" => "$method",
501 array(
502 "consumer_secret" => $this->oauth_data["consumer_secret"],
503 "oauth_secret" => $this->oauth_data["oauth_secret"]
506 $postfield_string = "command=$command&media_id=$media_id" ;
507 $msg_len = (strlen($postfield_string));
508 $header_data = array("Except:", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
509 "Content-Type: application/x-www-form-urlencoded",
510 "Content-Length: $msg_len", "Host: upload.twitter.com",
511 'Authorization: OAuth oauth_consumer_key="' . $this->oauth_data["oauth_consumer_key"] .'",oauth_nonce="' . $random_value . '",oauth_signature="' .
512 $signature . '",oauth_signature_method="' .$method . '"' . ',oauth_timestamp="' . $timestamp . '",oauth_token="' . $this->oauth_data["oauth_token"] . '",oauth_version="' . $oauth_version . '"'
514 //request
515 $curl = curl_init($this->media_api);
516 curl_setopt($curl, CURLOPT_POST, 1);
517 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
518 curl_setopt($curl, CURLOPT_POSTFIELDS, $postfield_string);
519 curl_setopt($curl, CURLOPT_HEADER , true);
520 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
521 $http_response = curl_exec($curl);
522 echo $http_response;
525 function generateSignature($request_arr, $paramater_arr, $secret_arr){
526 // BUILD SIGNATURE
527 $request_method = strtoupper($request_arr["request_method"]);
528 $base_url = rawurlencode($request_arr["base_url"]);
530 ksort($paramater_arr);
531 if(isset($paramater_arr["media"])) $paramater_arr["media"] = rawurlencode($paramater_arr["media"]);
532 $paramter_string = $this->buildOAuthParamaterString($paramater_arr);
534 $base_string = ($request_method . "&" . $base_url . "&" . $paramter_string);
535 $secret_string = $secret_arr["consumer_secret"] . "&" . $secret_arr["oauth_secret"];
536 $signature = (base64_encode(hash_hmac("SHA1",$base_string, $secret_string, true)));
538 return $signature;
541 function buildOAuthParamaterString($paramater_arr){
542 $param_string = "";
543 $join_param_by_amphersand = false;
544 foreach($paramater_arr as $key => $param){
545 if(!$join_param_by_amphersand){
546 $join_param_by_amphersand=true;
548 else{
549 $param_string .= rawurlencode("&");
551 $param_string .= rawurlencode($key . "=" . $param);
553 return $param_string;
558 //echo"run script from externals<br/>";