<?php
    include_once('includes/db_connect.php');
    include_once('includes/global_functions.php');
    $radioid = $_GET['radioid'];
    $radiomac = $_GET['radiomac'];
    
 if($radioid == "" && $radiomac == ""){
        print(json_encode([error => "Not a valid radioid"]));
        die();
    }else{
        if($radiomac != ""){
            $radiomac = str_replace(':', '', $radiomac);
            $radioid = substr($radiomac, 6 , 11);
        }
        $currentStream = null;
        if($stmt = $mysqli -> prepare("SELECT FollowerID FROM Twitter LEFT JOIN users ON Twitter.uid = users.uid WHERE users.radiotoken = ?")){
            $stmt -> bind_param("s", $radioid);
         	$stmt -> execute();
         	$stmt -> bind_result($FollowerID);
        	$stmt -> fetch();
         	$stmt -> close();
         	
              //specificeer token, key variable
            $oauth_access_token = "254139292-uhnFJBvMdO2ymdTWnzRGuct5aME4dT3Ll9Ag03J6";
            $oauth_access_token_secret = "L1D96ulaUSb8ArC7NV0UwoGResYLcpPvnoPRsZwvJTVpz";
            $consumer_key = "OKcwZL7ZeCElUU3GevJW2flcb";
            $consumer_secret = "6M8Gwco5PP15baRYrSxI3HKCXW3mIKO1nzlr7dQJioZAdr3lTV";
            
            $twitter_timeline = "user_timeline";
            
             $request = array('count' => '2',
                            'screen_name' => $FollowerID
                            );
            
            // authentticatie protocol   
            $oauth = array(
                'oauth_consumer_key' => $consumer_key,
                'oauth_nonce' => time(),
                'oauth_signature_method' => 'HMAC-SHA1',
                'oauth_token' => $oauth_access_token,
                'oauth_timestamp' => time(),
                'oauth_version' => '1.0'
            );
                           
            $oauth = array_merge($oauth,$request);
            
            $baseURI="https://api.twitter.com/1.1/statuses/$twitter_timeline.json";
            $method="GET";
            $params=$oauth;
            
            $r = array();
            ksort($params);
            foreach($params as $key=>$value){
                $r[] = "$key=" . rawurlencode($value);
            }
            $base_info = $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
            $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
             
            // get oauth signature
            $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
            $oauth['oauth_signature'] = $oauth_signature;
            
            
            
            // authentication request
            
            $r = 'Authorization: OAuth ';
            
            $values = array();
            foreach($oauth as $key=>$value){
                $values[] = "$key=\"" . rawurlencode($value) . "\"";
            }
            $r .= implode(', ', $values);
            
            $header = array($r, 'Expect:');
            
            $options = array(
                CURLOPT_HTTPHEADER => $header,
                CURLOPT_HEADER => false,
                CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_SSL_VERIFYPEER => true
            );
            // ontvangen van twitter feeds met user informaties
            $feed = curl_init();
            curl_setopt_array($feed, $options);
            $json = curl_exec($feed);
            curl_close($feed);
            $tweets=json_decode($json, true);
            if($tweets[0]["text"] != null)
            {
            //array('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@','/[^A-Za-z0-9 ]/')
              $regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@";
              $regex2 = "/[^a-zA-Z0-9 ]+/";
              
              $message = preg_replace($regex, "", $tweets[0]['text']);
              echo preg_replace($regex2, "", $message);
            }
        }
    }
?> 
 