function get_youtube_access_token($refresh = false)
{
    global $baseurl, $userref, $youtube_publish_client_id, $youtube_publish_client_secret, $youtube_publish_callback_url, $code;
    $url = 'https://accounts.google.com/o/oauth2/token';
    if ($refresh) {
        $refresh_token = sql_value("select youtube_refresh_token as value from user where ref='{$userref}'", "");
        if ($refresh_token == "") {
            get_youtube_authorization_code();
            exit;
        }
        $params = array("client_id" => $youtube_publish_client_id, "client_secret" => $youtube_publish_client_secret, "refresh_token" => $refresh_token, "grant_type" => "refresh_token");
    } else {
        $params = array("code" => $code, "client_id" => $youtube_publish_client_id, "client_secret" => $youtube_publish_client_secret, "redirect_uri" => $baseurl . $youtube_publish_callback_url, "grant_type" => "authorization_code");
    }
    $curl = curl_init("https://accounts.google.com/o/oauth2/token");
    curl_setopt($curl, CURLOPT_HEADER, "Content-Type:application/x-www-form-urlencoded");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
    $response = json_decode(curl_exec($curl), true);
    curl_close($curl);
    //exit (print_r($response));
    if (isset($response["error"])) {
        sql_query("update user set youtube_access_token='' where ref='{$userref}'");
        //exit("ERROR: bad response" . print_r($response));
        get_youtube_authorization_code();
        exit;
    }
    if (isset($response["access_token"])) {
        $access_token = escape_check($response["access_token"]);
        sql_query("update user set youtube_access_token='{$access_token}' where ref='{$userref}'");
        if (isset($response["refresh_token"])) {
            $refresh_token = escape_check($response["refresh_token"]);
            sql_query("update user set youtube_refresh_token='{$refresh_token}' where ref='{$userref}'");
        }
        debug("YouTube plugin: Access token: " . $access_token);
        debug("YouTube plugin: Refresh token: " . $refresh_token);
    }
    # Get user account details and store these so we can tell which account they will be uploading to
    $headers = array("Authorization: Bearer " . $access_token, "GData-Version: 2");
    $curl = curl_init("https://gdata.youtube.com/feeds/api/users/default");
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_HTTPGET, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
    #$response = json_decode( curl_exec( $curl ), true );
    $response = curl_exec($curl);
    $userdataxml = new SimpleXmlElement($response, LIBXML_NOCDATA);
    //exit(print_r($userdataxml));
    $youtube_username = escape_check($userdataxml->title);
    sql_query("update user set youtube_username='******' where ref='{$userref}'");
    return $access_token;
}
            ?>
");
			</script>
			<?php 
        }
    }
}
# Check we have an access token. If not, get one
$youtube_access_token = sql_value("select youtube_access_token as value from user where ref='{$userref}'", "");
//$youtube_refresh_token = sql_value("select youtube_refresh_token as value from user where ref='$userref'","");
if ($youtube_access_token == "") {
    # We don't have a token, do we have a code?
    $code = getvalescaped('code', "");
    if ($code == "") {
        # no? Then get a temp code, we will be returned to this page afterwards with it in the querystring
        get_youtube_authorization_code();
        exit;
    } else {
        // Use the code to get an access token
        $access_token_response = get_youtube_access_token();
        if (!$access_token_response) {
            $youtube_error = $lang["youtube_access_failed"];
        }
    }
}
$youtube_username = sql_value("select youtube_username as value from user where ref='{$userref}'", "");
if (isset($_POST['video_title']) && isset($_POST['video_description'])) {
    $video_title = getvalescaped("video_title", "");
    $video_description = getval("video_description", "");
    $video_keywords = getvalescaped("video_keywords", "");
    $filename = get_data_by_field($ref, $filename_field);