function statusnet_queue_deliver(&$a, &$b) { $outq = $b['outq']; if ($outq['outq_driver'] !== 'statusnet') { return; } $ckey = get_pconfig($outq['outq_channel'], 'statusnet', 'consumerkey'); $csecret = get_pconfig($outq['outq_channel'], 'statusnet', 'consumersecret'); $otoken = get_pconfig($outq['outq_channel'], 'statusnet', 'oauthtoken'); $osecret = get_pconfig($outq['outq_channel'], 'statusnet', 'oauthsecret'); if ($ckey && $csecret && $otoken && $osecret) { $dent = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret); if ($outq['outq_msg']) { $result = $dent->post('statuses/update', array('status' => $outq['outq_msg'])); if ($result->error) { logger('Send to GNU social failed: "' . $result->error . '"'); update_queue_item($outq['outq_hash']); } else { logger('statusnet_post send, result: ' . print_r($result, true) . "\nmessage: " . $outq['outq_msg'], LOGGER_DEBUG); remove_queue_item($outq['outq_hash']); } } } $b['handled'] = true; }
function statusnet_post_hook(&$a, &$b) { /** * Post to statusnet */ if (!is_item_normal($b) || $b['item_private'] || $b['created'] !== $b['edited']) { return; } if (!perm_is_allowed($b['uid'], '', 'view_stream')) { return; } if (!strstr($b['postopts'], 'statusnet')) { return; } if ($b['parent'] != $b['id']) { return; } // if posts comes from statusnet don't send it back if ($b['app'] == "StatusNet") { return; } logger('statusnet post invoked'); load_pconfig($b['uid'], 'statusnet'); $api = get_pconfig($b['uid'], 'statusnet', 'baseapi'); $ckey = get_pconfig($b['uid'], 'statusnet', 'consumerkey'); $csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret'); $otoken = get_pconfig($b['uid'], 'statusnet', 'oauthtoken'); $osecret = get_pconfig($b['uid'], 'statusnet', 'oauthsecret'); $intelligent_shortening = get_pconfig($b['uid'], 'statusnet', 'intelligent_shortening'); // Global setting overrides this if (get_config('statusnet', 'intelligent_shortening')) { $intelligent_shortening = get_config('statusnet', 'intelligent_shortening'); } if ($ckey && $csecret && $otoken && $osecret) { require_once 'include/bbcode.php'; $dent = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret); $max_char = $dent->get_maxlength(); // max. length for a dent // we will only work with up to two times the length of the dent // we can later send to GNU social. This way we can "gain" some // information during shortening of potential links but do not // shorten all the links in a 200000 character long essay. $tempfile = ""; $intelligent_shortening = get_config('statusnet', 'intelligent_shortening'); if (!$intelligent_shortening) { if (!$b['title'] == '') { $tmp = $b['title'] . ": \n" . $b['body']; // $tmp = substr($tmp, 0, 4*$max_char); } else { $tmp = $b['body']; // substr($b['body'], 0, 3*$max_char); } // if [url=bla][img]blub.png[/img][/url] get blub.png $tmp = preg_replace('/\\[url\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\]\\[img\\](\\w+.*?)\\[\\/img\\]\\[\\/url\\]/i', '$2', $tmp); // preserve links to images, videos and audios $tmp = preg_replace('/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism', '$3', $tmp); $tmp = preg_replace('/\\[\\/?img(\\s+.*?\\]|\\])/i', '', $tmp); $tmp = preg_replace('/\\[\\/?video(\\s+.*?\\]|\\])/i', '', $tmp); $tmp = preg_replace('/\\[\\/?youtube(\\s+.*?\\]|\\])/i', '', $tmp); $tmp = preg_replace('/\\[\\/?vimeo(\\s+.*?\\]|\\])/i', '', $tmp); $tmp = preg_replace('/\\[\\/?audio(\\s+.*?\\]|\\])/i', '', $tmp); $linksenabled = get_pconfig($b['uid'], 'statusnet', 'post_taglinks'); // if a #tag is linked, don't send the [url] over to SN // that is, don't send if the option is not set in the // connector settings if ($linksenabled == '0') { // #-tags $tmp = preg_replace('/#\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', '#$2', $tmp); // @-mentions $tmp = preg_replace('/@\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', '@$2', $tmp); // recycle 1 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8'); $tmp = preg_replace('/' . $recycle . '\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', $recycle . '$2', $tmp); // recycle 2 (test) $recycle = html_entity_decode("◌ ", ENT_QUOTES, 'UTF-8'); $tmp = preg_replace('/' . $recycle . '\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', $recycle . '$2', $tmp); } // preserve links to webpages $tmp = preg_replace('/\\[url\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\](\\w+.*?)\\[\\/url\\]/i', '$2 $1', $tmp); $tmp = preg_replace('/\\[bookmark\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\](\\w+.*?)\\[\\/bookmark\\]/i', '$2 $1', $tmp); // find all http or https links in the body of the entry and // apply the shortener if the link is longer then 20 characters if (strlen($tmp) > $max_char && $max_char > 0) { preg_match_all('/(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)/i', $tmp, $allurls); foreach ($allurls as $url) { foreach ($url as $u) { if (strlen($u) > 20) { $sl = short_link($u); $tmp = str_replace($u, $sl, $tmp); } } } } // ok, all the links we want to send out are save, now strip // away the remaining bbcode //$msg = strip_tags(bbcode($tmp, false, false)); $msg = bbcode($tmp, false, false, true); $msg = str_replace(array('<br>', '<br />'), "\n", $msg); $msg = strip_tags($msg); // quotes not working - let's try this $msg = html_entity_decode($msg); if (strlen($msg) > $max_char && $max_char > 0) { $shortlink = short_link($b['plink']); // the new message will be shortened such that "... $shortlink" // will fit into the character limit $msg = nl2br(substr($msg, 0, $max_char - strlen($shortlink) - 4)); $msg = str_replace(array('<br>', '<br />'), ' ', $msg); $e = explode(' ', $msg); // remove the last word from the cut down message to // avoid sending cut words to the MicroBlog array_pop($e); $msg = implode(' ', $e); $msg .= '... ' . $shortlink; } $msg = trim($msg); $postdata = array('status' => $msg); } else { $msgarr = statusnet_shortenmsg($b, $max_char); $msg = $msgarr["msg"]; $image = $msgarr["image"]; if ($image != "") { $imagedata = file_get_contents($image); $tempfile = tempnam(get_config("system", "temppath"), "upload"); file_put_contents($tempfile, $imagedata); $postdata = array("status" => $msg, "media" => "@" . $tempfile); } else { $postdata = array("status" => $msg); } } // and now dent it :-) if (strlen($msg)) { //$result = $dent->post('statuses/update', array('status' => $msg)); $result = $dent->post('statuses/update', $postdata); logger('statusnet_post send, result: ' . print_r($result, true) . "\nmessage: " . $msg, LOGGER_DEBUG); logger("Original post: " . print_r($b, true) . "\nPost Data: " . print_r($postdata, true), LOGGER_DEBUG); if ($result->error) { logger('Send to GNU social failed: "' . $result->error . '"'); } } if ($tempfile != "") { unlink($tempfile); } } }
function statusnet_is_retweet($a, $uid, $body) { $body = trim($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; } $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]; } $ckey = get_pconfig($uid, 'statusnet', 'consumerkey'); $csecret = get_pconfig($uid, 'statusnet', 'consumersecret'); $api = get_pconfig($uid, 'statusnet', 'baseapi'); $otoken = get_pconfig($uid, 'statusnet', 'oauthtoken'); $osecret = get_pconfig($uid, 'statusnet', 'oauthsecret'); $hostname = preg_replace("=https?://([\\w\\.]*)/.*=ism", "\$1", $api); $id = preg_replace("=https?://" . $hostname . "/notice/(.*)=ism", "\$1", $link); if ($id == $link) { return false; } logger('statusnet_is_retweet: Retweeting id ' . $id . ' for user ' . $uid, LOGGER_DEBUG); $connection = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret); $result = $connection->post('statuses/retweet/' . $id); logger('statusnet_is_retweet: result ' . print_r($result, true), LOGGER_DEBUG); return isset($result->id); }
function statusnet_post_hook(&$a, &$b) { /** * Post to statusnet */ if ($b['deleted'] || $b['private'] || $b['created'] !== $b['edited']) { return; } if (!strstr($b['postopts'], 'statusnet')) { return; } load_pconfig($b['uid'], 'statusnet'); $api = get_pconfig($b['uid'], 'statusnet', 'baseapi'); $ckey = get_pconfig($b['uid'], 'statusnet', 'consumerkey'); $csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret'); $otoken = get_pconfig($b['uid'], 'statusnet', 'oauthtoken'); $osecret = get_pconfig($b['uid'], 'statusnet', 'oauthsecret'); if ($ckey && $csecret && $otoken && $osecret) { require_once 'include/bbcode.php'; $dent = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret); $max_char = $dent->get_maxlength(); // max. length for a dent // we will only work with up to two times the length of the dent // we can later send to StatusNet. This way we can "gain" some // information during shortening of potential links but do not // shorten all the links in a 200000 character long essay. if (!$b['title'] == '') { $tmp = $b['title'] . ' : ' . $b['body']; // $tmp = substr($tmp, 0, 4*$max_char); } else { $tmp = $b['body']; // substr($b['body'], 0, 3*$max_char); } // if [url=bla][img]blub.png[/img][/url] get blub.png $tmp = preg_replace('/\\[url\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\]\\[img\\](\\w+.*?)\\[\\/img\\]\\[\\/url\\]/i', '$2', $tmp); // preserve links to images, videos and audios $tmp = preg_replace('/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism', '$3', $tmp); $tmp = preg_replace('/\\[\\/?img(\\s+.*?\\]|\\])/i', '', $tmp); $tmp = preg_replace('/\\[\\/?video(\\s+.*?\\]|\\])/i', '', $tmp); $tmp = preg_replace('/\\[\\/?youtube(\\s+.*?\\]|\\])/i', '', $tmp); $tmp = preg_replace('/\\[\\/?vimeo(\\s+.*?\\]|\\])/i', '', $tmp); $tmp = preg_replace('/\\[\\/?audio(\\s+.*?\\]|\\])/i', '', $tmp); $linksenabled = get_pconfig($b['uid'], 'statusnet', 'post_taglinks'); // if a #tag is linked, don't send the [url] over to SN // that is, don't send if the option is not set in the // connector settings if ($linksenabled == '0') { // #-tags $tmp = preg_replace('/#\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', '#$2', $tmp); // @-mentions $tmp = preg_replace('/@\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', '@$2', $tmp); } // preserve links to webpages $tmp = preg_replace('/\\[url\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\](\\w+.*?)\\[\\/url\\]/i', '$2 $1', $tmp); $tmp = preg_replace('/\\[bookmark\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\](\\w+.*?)\\[\\/bookmark\\]/i', '$2 $1', $tmp); // find all http or https links in the body of the entry and // apply the shortener if the link is longer then 20 characters if (strlen($tmp) > $max_char && $max_char > 0) { preg_match_all('/(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)/i', $tmp, $allurls); foreach ($allurls as $url) { foreach ($url as $u) { if (strlen($u) > 20) { $sl = short_link($u); $tmp = str_replace($u, $sl, $tmp); } } } } // ok, all the links we want to send out are save, now strip // away the remaining bbcode $msg = strip_tags(bbcode($tmp)); // quotes not working - let's try this $msg = html_entity_decode($msg); if (strlen($msg) > $max_char && $max_char > 0) { $shortlink = short_link($b['plink']); // the new message will be shortened such that "... $shortlink" // will fit into the character limit $msg = nl2br(substr($msg, 0, $max_char - strlen($shortlink) - 4)); $msg = str_replace(array('<br>', '<br />'), ' ', $msg); $e = explode(' ', $msg); // remove the last word from the cut down message to // avoid sending cut words to the MicroBlog array_pop($e); $msg = implode(' ', $e); $msg .= '... ' . $shortlink; } // and now dent it :-) if (strlen($msg)) { $result = $dent->post('statuses/update', array('status' => $msg)); logger('statusnet_post send, result: ' . print_r($result, true), LOGGER_DEBUG); if ($result->error) { logger('Send to StatusNet failed: "' . $result->error . '"'); } } } }