]*>)$i';
if (preg_match_all($pattern, $input, $matches)) {
list($dummy, $links) = ($matches);
return $links;
}
return false;
}
/* this is the same as above but is used on other places, like user bios and places where the
shortcodes and the emoji url is defined in the same place */
function emojify($string, $emojis, $size = 40) {
foreach ($emojis as $emoji) {
$string = str_replace(":" . $emoji['shortcode'] . ":", "", $string);
}
return $string;
}
/* This function displays the emoji list of an instance based on a search
string given on $val */
function emoji_list($val){
$emojilist = api_get("/custom_emojis");
$c = 0;
$return = "";
foreach ($emojilist as $emoji){
if (starts_with($emoji['shortcode'],$val) && $c < 50){
$return .= "";
$c++;
}
}
if ($c < 50){
foreach ($emojilist as $emoji){
if ((contains($emoji['shortcode'],$val) && !starts_with($emoji['shortcode'],$val)) && $c < 50){
$return .= "";
$c++;
}
}
}
return $return;
}
function contact_search($val){
global $user_settings;
$return = "";
$list = api_get("/accounts/search?q=".$val);
foreach ($list as $contact){
$return .= "
";
}
return $return;
}
/* general function to check if one strings starts with a given search */
function starts_with($string,$search){
if (substr(strtolower($string),0,strlen($search)) == strtolower($search)){
return true;
}
return false;
}
/* general function to check if one strings contains with a given search */
function contains($string,$search){
if (is_numeric(strpos(strtolower($string),strtolower($search)))){
return true;
}
return false;
}
/* this function fetches the context (the previous posts and replies) of a specified post
$post = ID of the post.
*/
function context($post) {
global $srv;
global $token;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://$srv/api/v1/statuses/$post/context");
if (!is_null($token)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token
));
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/* this function just reduces an image to a 1x1 pixel image to get the overall color.
*/
function averageColor($url) {
@$image = imagecreatefromstring(file_get_contents($url));
if (!$image) {
$mainColor = "CCCCCC";
}
else {
$thumb = imagecreatetruecolor(1, 1);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, 1, 1, imagesx($image) , imagesy($image));
$mainColor = strtoupper(dechex(imagecolorat($thumb, 0, 0)));
}
return $mainColor;
}
/* this function fetches all the data from a profile (bio, username, relationships, etc) */
function user_info($user) {
global $user_settings;
$info = api_get("accounts/" . $user);
$rel = api_get("accounts/relationships?id=" . $user);
return array(
$info,
$rel
);
}
/* a function to make an authenticated general GET api call to the logged-in instance */
function api_get($url) {
global $srv;
global $token;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://$srv/api/v1/" . $url);
if (!is_null($token)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token
));
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result, true);
}
/* same as above but used in some newer api endpoinds (v2) */
function api_getv2($url) {
global $srv;
global $token;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://$srv/api/v2/" . $url);
if (!is_null($token)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token
));
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result, true);
}
/* a function to make an authenticated general POST api call to the logged-in instance */
function api_post($url, $array) {
global $srv;
global $token;
$cSession = curl_init();
curl_setopt($cSession, CURLOPT_HEADER, false);
curl_setopt($cSession, CURLOPT_POST, 1);
curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/" . $url);
if (!is_null($token)) {
curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token
));
}
curl_setopt($cSession, CURLOPT_POSTFIELDS, http_build_query($array));
curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($cSession);
curl_close($cSession);
return json_decode($result, true);
}
/* a function to make general DELETE api calls to the logged-in instance*/
function api_delete($url, $array) {
global $srv;
global $token;
$cSession = curl_init();
curl_setopt($cSession, CURLOPT_HEADER, false);
curl_setopt($cSession, CURLOPT_POST, 1);
curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/" . $url);
curl_setopt($cSession, CURLOPT_CUSTOMREQUEST, "DELETE");
if (!is_null($token)) {
curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token
));
}
curl_setopt($cSession, CURLOPT_POSTFIELDS, http_build_query($array));
curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($cSession);
curl_close($cSession);
return json_decode($result, true);
}
/* a function to make general PATCH api calls to the logged-in instance*/
function api_patch($url, $array) {
global $srv;
global $token;
$cSession = curl_init();
curl_setopt($cSession, CURLOPT_HEADER, false);
curl_setopt($cSession, CURLOPT_POST, 1);
curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/" . $url);
curl_setopt($cSession, CURLOPT_CUSTOMREQUEST, "PATCH");
if (!is_null($token)) {
curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token
));
}
curl_setopt($cSession, CURLOPT_POSTFIELDS, http_build_query($array));
curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($cSession);
curl_close($cSession);
return json_decode($result, true);
}
/* Function to upload files to the profile of the authenticated user
$type can be 'avatar' or 'header'*/
function upload_profile($file,$type){
global $srv;
global $token;
$mime = get_mime($file);
$info = pathinfo($file);
$name = $info['basename'];
$output = new CURLFile($file, $mime, $name);
$cSession = curl_init();
curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/accounts/update_credentials");
curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cSession, CURLOPT_POST, 1);
curl_setopt($cSession, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $token
));
curl_setopt($cSession, CURLOPT_POSTFIELDS, array(
$type => $output
));
$result = curl_exec($cSession);
curl_close($cSession);
return $result;
}
/* this function is used to generate the html code of a poll */
function renderPoll($elem) {
global $logedin;
$output = "";
$output .= " ";
$votes = $elem['poll']['votes_count'];
if ($elem['poll']['voted'] || $elem['poll']['expired']) {
$output.= "Votes: $votes ";
foreach ($elem['poll']['options'] as $option){
$percentage = ($option['votes_count'] / $votes ) * 100;
$output .= "
";
}
$output .= ($logedin ? "" : "");
}
return $output;
}
/* this function is used to generate the html code of a reply */
function render_reply($item) {
global $user_settings;
global $logedin;
global $srv;
$reply['mode'] = "";
if (isset($item['type']) && $item['type'] == 'ancestor') {
$reply['mode'] = "ancestor";
}
$reply['id'] = $item['id'];
$reply['uid'] = $item['account']['id'];
$reply['name'] = emojify($item['account']['display_name'], $item['account']['emojis'], 15);
$reply['acct'] = $item['account']['acct'];
$reply['avatar'] = $item['account']['avatar'];
$reply['menu'] = "