+
+ function api_fr_photos_list(&$a,$type) {
+ if (api_user()===false) return false;
+ $r = q("select distinct `resource-id` from photo where uid = %d and album != 'Contact Photos' ",
+ intval(local_user())
+ );
+ if($r) {
+ $ret = array();
+ foreach($r as $rr)
+ $ret[] = $rr['resource-id'];
+ header("Content-type: application/json");
+ echo json_encode($ret);
+ }
+ killme();
+ }
+
+ function api_fr_photo_detail(&$a,$type) {
+ if (api_user()===false) return false;
+ if(! $_REQUEST['photo_id']) return false;
+ $scale = ((array_key_exists('scale',$_REQUEST)) ? intval($_REQUEST['scale']) : 0);
+ $r = q("select * from photo where uid = %d and `resource-id` = '%s' and scale = %d limit 1",
+ intval(local_user()),
+ dbesc($_REQUEST['photo_id']),
+ intval($scale)
+ );
+ if($r) {
+ header("Content-type: application/json");
+ $r[0]['data'] = base64_encode($r[0]['data']);
+ echo json_encode($r[0]);
+ }
+
+ killme();
+ }
+
+ api_register_func('api/friendica/photos/list', 'api_fr_photos_list', true);
+ api_register_func('api/friendica/photo', 'api_fr_photo_detail', true);
+
+
+
+ /**
+ * similar as /mod/redir.php
+ * redirect to 'url' after dfrn auth
+ *
+ * why this when there is mod/redir.php already?
+ * This use api_user() and api_login()
+ *
+ * params
+ * c_url: url of remote contact to auth to
+ * url: string, url to redirect after auth
+ */
+ function api_friendica_remoteauth(&$a) {
+ $url = ((x($_GET,'url')) ? $_GET['url'] : '');
+ $c_url = ((x($_GET,'c_url')) ? $_GET['c_url'] : '');
+
+ if ($url === '' || $c_url === '')
+ die((api_error($a, 'json', "Wrong parameters")));
+
+ $c_url = normalise_link($c_url);
+
+ // traditional DFRN
+
+ $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `nurl` = '%s' LIMIT 1",
+ dbesc($c_url),
+ intval(api_user())
+ );
+
+ if ((! count($r)) || ($r[0]['network'] !== NETWORK_DFRN))
+ die((api_error($a, 'json', "Unknown contact")));
+
+ $cid = $r[0]['id'];
+
+ $dfrn_id = $orig_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']);
+
+ if($r[0]['duplex'] && $r[0]['issued-id']) {
+ $orig_id = $r[0]['issued-id'];
+ $dfrn_id = '1:' . $orig_id;
+ }
+ if($r[0]['duplex'] && $r[0]['dfrn-id']) {
+ $orig_id = $r[0]['dfrn-id'];
+ $dfrn_id = '0:' . $orig_id;
+ }
+
+ $sec = random_string();
+
+ q("INSERT INTO `profile_check` ( `uid`, `cid`, `dfrn_id`, `sec`, `expire`)
+ VALUES( %d, %s, '%s', '%s', %d )",
+ intval(api_user()),
+ intval($cid),
+ dbesc($dfrn_id),
+ dbesc($sec),
+ intval(time() + 45)
+ );
+
+ logger($r[0]['name'] . ' ' . $sec, LOGGER_DEBUG);
+ $dest = (($url) ? '&destination_url=' . $url : '');
+ goaway ($r[0]['poll'] . '?dfrn_id=' . $dfrn_id
+ . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
+ . '&type=profile&sec=' . $sec . $dest . $quiet );
+ }
+ api_register_func('api/friendica/remoteauth', 'api_friendica_remoteauth', true);
+
+
+
+function api_share_as_retweet(&$item) {
+ $body = trim($item["body"]);
+
+ // Skip if it isn't a pure repeated messages
+ // Does it start with a share?
+ if (strpos($body, "[share") > 0)
+ return(false);
+
+ // Does it end with a share?
+ if (strlen($body) > (strrpos($body, "[/share]") + 8))
+ return(false);
+
+ $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
+ // Skip if there is no shared message in there
+ if ($body == $attributes)
+ return(false);
+
+ $author = "";
+ preg_match("/author='(.*?)'/ism", $attributes, $matches);
+ if ($matches[1] != "")
+ $author = html_entity_decode($matches[1],ENT_QUOTES,'UTF-8');
+
+ preg_match('/author="(.*?)"/ism', $attributes, $matches);
+ if ($matches[1] != "")
+ $author = $matches[1];
+
+ $profile = "";
+ preg_match("/profile='(.*?)'/ism", $attributes, $matches);
+ if ($matches[1] != "")
+ $profile = $matches[1];
+
+ preg_match('/profile="(.*?)"/ism', $attributes, $matches);
+ if ($matches[1] != "")
+ $profile = $matches[1];
+
+ $avatar = "";
+ preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
+ if ($matches[1] != "")
+ $avatar = $matches[1];
+
+ preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
+ if ($matches[1] != "")
+ $avatar = $matches[1];
+
+ $link = "";
+ preg_match("/link='(.*?)'/ism", $attributes, $matches);
+ if ($matches[1] != "")
+ $link = $matches[1];
+
+ preg_match('/link="(.*?)"/ism', $attributes, $matches);
+ if ($matches[1] != "")
+ $link = $matches[1];
+
+ $shared_body = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$2",$body);
+
+ if (($shared_body == "") OR ($profile == "") OR ($author == "") OR ($avatar == ""))
+ return(false);
+
+ $item["body"] = $shared_body;
+ $item["author-name"] = $author;
+ $item["author-link"] = $profile;
+ $item["author-avatar"] = $avatar;
+ $item["plink"] = $link;
+
+ return(true);
+
+}
+
+function api_get_nick($profile) {
+/* To-Do:
+ - remove trailing jung from profile url
+ - pump.io check has to check the website
+*/
+
+ $nick = "";
+
+ $friendica = preg_replace("=https?://(.*)/profile/(.*)=ism", "$2", $profile);
+ if ($friendica != $profile)
+ $nick = $friendica;
+
+ if (!$nick == "") {
+ $diaspora = preg_replace("=https?://(.*)/u/(.*)=ism", "$2", $profile);
+ if ($diaspora != $profile)
+ $nick = $diaspora;
+ }
+
+ if (!$nick == "") {
+ $twitter = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $profile);
+ if ($twitter != $profile)
+ $nick = $twitter;
+ }
+
+
+ if (!$nick == "") {
+ $StatusnetHost = preg_replace("=https?://(.*)/user/(.*)=ism", "$1", $profile);
+ if ($StatusnetHost != $profile) {
+ $StatusnetUser = preg_replace("=https?://(.*)/user/(.*)=ism", "$2", $profile);
+ if ($StatusnetUser != $profile) {
+ $UserData = fetch_url("http://".$StatusnetHost."/api/users/show.json?user_id=".$StatusnetUser);
+ $user = json_decode($UserData);
+ if ($user)
+ $nick = $user->screen_name;
+ }
+ }
+ }
+
+ // To-Do: look at the page if its really a pumpio site
+ //if (!$nick == "") {
+ // $pumpio = preg_replace("=https?://(.*)/(.*)/=ism", "$2", $profile."/");
+ // if ($pumpio != $profile)
+ // $nick = $pumpio;
+ // <div class="media" id="profile-block" data-profile-id="acct:kabniel@microca.st">
+
+ //}
+
+ if ($nick != "") {
+ q("UPDATE unique_contacts SET nick = '%s' WHERE url = '%s'",
+ dbesc($nick), dbesc(normalise_link($profile)));
+ return($nick);
+ }
+
+ return(false);
+}
+
+function api_clean_plain_items($Text) {
+ $include_entities = strtolower(x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:"false");
+
+ $Text = bb_CleanPictureLinks($Text);
+
+ $URLSearchString = "^\[\]";
+
+ $Text = preg_replace("/([!#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",'$1$3',$Text);
+
+ if ($include_entities == "true") {
+ $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",'[url=$1]$1[/url]',$Text);
+ }
+
+ $Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism","api_cleanup_share",$Text);
+ return($Text);
+}
+
+function api_cleanup_share($shared) {
+ if ($shared[2] != "type-link")
+ return($shared[0]);
+
+ if (!preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$shared[3], $bookmark))
+ return($shared[0]);
+
+ $title = "";
+ $link = "";
+
+ if (isset($bookmark[2][0]))
+ $title = $bookmark[2][0];
+
+ if (isset($bookmark[1][0]))
+ $link = $bookmark[1][0];
+
+ if (strpos($shared[1],$title) !== false)
+ $title = "";
+
+ if (strpos($shared[1],$link) !== false)
+ $link = "";
+
+ $text = trim($shared[1]);
+
+ //if (strlen($text) < strlen($title))
+ if (($text == "") AND ($title != ""))
+ $text .= "\n\n".trim($title);
+
+ if ($link != "")
+ $text .= "\n".trim($link);
+
+ return(trim($text));
+}
+
+function api_best_nickname(&$contacts) {
+ $best_contact = array();
+
+ if (count($contact) == 0)
+ return;
+
+ foreach ($contacts AS $contact)
+ if ($contact["network"] == "") {
+ $contact["network"] = "dfrn";
+ $best_contact = array($contact);
+ }
+
+ if (sizeof($best_contact) == 0)
+ foreach ($contacts AS $contact)
+ if ($contact["network"] == "dfrn")
+ $best_contact = array($contact);
+
+ if (sizeof($best_contact) == 0)
+ foreach ($contacts AS $contact)
+ if ($contact["network"] == "dspr")
+ $best_contact = array($contact);
+
+ if (sizeof($best_contact) == 0)
+ foreach ($contacts AS $contact)
+ if ($contact["network"] == "stat")
+ $best_contact = array($contact);
+
+ if (sizeof($best_contact) == 0)
+ foreach ($contacts AS $contact)
+ if ($contact["network"] == "pump")
+ $best_contact = array($contact);
+
+ if (sizeof($best_contact) == 0)
+ foreach ($contacts AS $contact)
+ if ($contact["network"] == "twit")
+ $best_contact = array($contact);
+
+ if (sizeof($best_contact) == 1)
+ $contacts = $best_contact;
+ else
+ $contacts = array($contacts[0]);
+}
+
+