1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/phpseclib');
- class OStatusPlugin extends Plugin
- {
-
- public function onRouterInitialized(URLMapper $m)
- {
-
- $m->connect('main/ostatustag',
- array('action' => 'ostatustag'));
- $m->connect('main/ostatustag?nickname=:nickname',
- array('action' => 'ostatustag'), array('nickname' => '[A-Za-z0-9_-]+'));
- $m->connect('main/ostatus/nickname/:nickname',
- array('action' => 'ostatusinit'), array('nickname' => '[A-Za-z0-9_-]+'));
- $m->connect('main/ostatus/group/:group',
- array('action' => 'ostatusinit'), array('group' => '[A-Za-z0-9_-]+'));
- $m->connect('main/ostatus/peopletag/:peopletag/tagger/:tagger',
- array('action' => 'ostatusinit'), array('tagger' => '[A-Za-z0-9_-]+',
- 'peopletag' => '[A-Za-z0-9_-]+'));
- $m->connect('main/ostatus',
- array('action' => 'ostatusinit'));
-
- $m->connect('main/ostatussub',
- array('action' => 'ostatussub'));
- $m->connect('main/ostatusgroup',
- array('action' => 'ostatusgroup'));
- $m->connect('main/ostatuspeopletag',
- array('action' => 'ostatuspeopletag'));
-
- $m->connect('main/push/hub', array('action' => 'pushhub'));
- $m->connect('main/push/callback/:feed',
- array('action' => 'pushcallback'),
- array('feed' => '[0-9]+'));
-
- $m->connect('main/salmon/user/:id',
- array('action' => 'usersalmon'),
- array('id' => '[0-9]+'));
- $m->connect('main/salmon/group/:id',
- array('action' => 'groupsalmon'),
- array('id' => '[0-9]+'));
- $m->connect('main/salmon/peopletag/:id',
- array('action' => 'peopletagsalmon'),
- array('id' => '[0-9]+'));
- return true;
- }
- public function onAutoload($cls)
- {
- switch ($cls) {
- case 'Crypt_AES':
- case 'Crypt_RSA':
-
-
- require_once str_replace('_', '/', $cls) . '.php';
- return false;
- }
- return parent::onAutoload($cls);
- }
-
- function onEndInitializeQueueManager(QueueManager $qm)
- {
-
- $qm->connect('ostatus', 'OStatusQueueHandler');
-
- $qm->connect('hubconf', 'HubConfQueueHandler');
- $qm->connect('hubprep', 'HubPrepQueueHandler');
- $qm->connect('hubout', 'HubOutQueueHandler');
-
- $qm->connect('salmon', 'SalmonQueueHandler');
-
- $qm->connect('pushin', 'PushInQueueHandler');
-
- $qm->connect('pushrenew', 'PushRenewQueueHandler');
- return true;
- }
-
- function onStartEnqueueNotice($notice, &$transports)
- {
- if ($notice->inScope(null)) {
-
- array_unshift($transports, 'ostatus');
- $this->log(LOG_INFO, "Notice {$notice->id} queued for OStatus processing");
- } else {
-
-
- $this->log(LOG_NOTICE, "Not queueing notice {$notice->id} for OStatus because of privacy; scope = {$notice->scope}");
- }
- return true;
- }
-
- function onStartApiAtom($feed)
- {
- $id = null;
- if ($feed instanceof AtomUserNoticeFeed) {
- $salmonAction = 'usersalmon';
- $user = $feed->getUser();
- $id = $user->id;
- $profile = $user->getProfile();
- } else if ($feed instanceof AtomGroupNoticeFeed) {
- $salmonAction = 'groupsalmon';
- $group = $feed->getGroup();
- $id = $group->id;
- } else if ($feed instanceof AtomListNoticeFeed) {
- $salmonAction = 'peopletagsalmon';
- $peopletag = $feed->getList();
- $id = $peopletag->id;
- } else {
- return true;
- }
- if (!empty($id)) {
- $hub = common_config('ostatus', 'hub');
- if (empty($hub)) {
-
- $hub = common_local_url('pushhub');
- }
- $feed->addLink($hub, array('rel' => 'hub'));
-
- $salmon = common_local_url($salmonAction, array('id' => $id));
- $feed->addLink($salmon, array('rel' => Salmon::REL_SALMON));
-
- $feed->addLink($salmon, array('rel' => Salmon::NS_REPLIES));
- $feed->addLink($salmon, array('rel' => Salmon::NS_MENTIONS));
- }
- return true;
- }
-
- function onStartProfileRemoteSubscribe($output, $profile)
- {
- $this->onStartProfileListItemActionElements($output, $profile);
- return false;
- }
- function onStartGroupSubscribe($widget, $group)
- {
- $cur = common_current_user();
- if (empty($cur)) {
- $widget->out->elementStart('li', 'entity_subscribe');
- $url = common_local_url('ostatusinit',
- array('group' => $group->nickname));
- $widget->out->element('a', array('href' => $url,
- 'class' => 'entity_remote_subscribe'),
-
- _m('Subscribe'));
- $widget->out->elementEnd('li');
- return false;
- }
- return true;
- }
- function onStartSubscribePeopletagForm($output, $peopletag)
- {
- $cur = common_current_user();
- if (empty($cur)) {
- $output->elementStart('li', 'entity_subscribe');
- $profile = $peopletag->getTagger();
- $url = common_local_url('ostatusinit',
- array('tagger' => $profile->nickname, 'peopletag' => $peopletag->tag));
- $output->element('a', array('href' => $url,
- 'class' => 'entity_remote_subscribe'),
-
- _m('Subscribe'));
- $output->elementEnd('li');
- return false;
- }
- return true;
- }
-
- function onStartProfileCompletionSearch($action, $profile, $search_engine) {
- if ($action->field == 'uri') {
- $profile->joinAdd(array('id', 'user:id'));
- $profile->whereAdd('uri LIKE "%' . $profile->escape($q) . '%"');
- $profile->query();
- $validate = new Validate();
- if ($profile->N == 0) {
- try {
- if ($validate->email($q)) {
- $oprofile = Ostatus_profile::ensureWebfinger($q);
- } else if ($validate->uri($q)) {
- $oprofile = Ostatus_profile::ensureProfileURL($q);
- } else {
-
- throw new Exception(_m('Invalid URI.'));
- }
- return $this->filter(array($oprofile->localProfile()));
- } catch (Exception $e) {
-
-
- $this->msg = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname.");
- return array();
- }
- }
- return false;
- }
- return true;
- }
-
- function onEndFindMentions(Profile $sender, $text, &$mentions)
- {
- $matches = array();
- $wmatches = array();
-
- if (preg_match_all('!(?:^|\s+)@((?:\w+[\w\-\_\.]?)*(?:[\w\-\_\.]*\w+)@(?:\w+\-?\w+\.)*\w+(?:\w+\-\w+)*\.\w+)!',
- $text,
- $wmatches,
- PREG_OFFSET_CAPTURE)) {
- foreach ($wmatches[1] as $wmatch) {
- list($target, $pos) = $wmatch;
- $this->log(LOG_INFO, "Checking webfinger '$target'");
- $profile = null;
- try {
- $oprofile = Ostatus_profile::ensureWebfinger($target);
- if (!$oprofile instanceof Ostatus_profile || !$oprofile->isPerson()) {
- continue;
- }
- $profile = $oprofile->localProfile();
- } catch (OStatusShadowException $e) {
-
- $profile = $e->profile;
- } catch (Exception $e) {
- $this->log(LOG_ERR, "Webfinger check failed: " . $e->getMessage());
- continue;
- }
- assert($profile instanceof Profile);
- $text = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target)
- ? $profile->getNickname()
- : $target;
- $url = $profile->getUri();
- if (!common_valid_http_url($url)) {
- $url = $profile->getUrl();
- }
- $matches[$pos] = array('mentioned' => array($profile),
- 'type' => 'mention',
- 'text' => $text,
- 'position' => $pos,
- 'length' => mb_strlen($target),
- 'url' => $url);
- }
- }
-
- if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)*)!',
- $text,
- $wmatches,
- PREG_OFFSET_CAPTURE)) {
- foreach ($wmatches[1] as $wmatch) {
- list($target, $pos) = $wmatch;
- $schemes = array('http', 'https');
- foreach ($schemes as $scheme) {
- $url = "$scheme://$target";
- $this->log(LOG_INFO, "Checking profile address '$url'");
- try {
- $oprofile = Ostatus_profile::ensureProfileURL($url);
- if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
- $profile = $oprofile->localProfile();
- $text = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target) ?
- $profile->nickname : $target;
- $matches[$pos] = array('mentioned' => array($profile),
- 'type' => 'mention',
- 'text' => $text,
- 'position' => $pos,
- 'length' => mb_strlen($target),
- 'url' => $profile->getUrl());
- break;
- }
- } catch (Exception $e) {
- $this->log(LOG_ERR, "Profile check failed: " . $e->getMessage());
- }
- }
- }
- }
- foreach ($mentions as $i => $other) {
-
- $pos = $other['position'];
- if (isset($matches[$pos])) {
- $mentions[$i] = $matches[$pos];
- unset($matches[$pos]);
- }
- }
- foreach ($matches as $mention) {
- $mentions[] = $mention;
- }
- return true;
- }
-
- function onStartCommandGetProfile($command, $arg, &$profile)
- {
- $oprofile = $this->pullRemoteProfile($arg);
- if ($oprofile instanceof Ostatus_profile && !$oprofile->isGroup()) {
- try {
- $profile = $oprofile->localProfile();
- } catch (NoProfileException $e) {
-
- return true;
- }
- return false;
- } else {
- return true;
- }
- }
-
- function onStartCommandGetGroup($command, $arg, &$group)
- {
- $oprofile = $this->pullRemoteProfile($arg);
- if ($oprofile instanceof Ostatus_profile && $oprofile->isGroup()) {
- $group = $oprofile->localGroup();
- return false;
- } else {
- return true;
- }
- }
- protected function pullRemoteProfile($arg)
- {
- $oprofile = null;
- if (preg_match('!^((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)$!', $arg)) {
-
- try {
- return Ostatus_profile::ensureWebfinger($arg);
- } catch (Exception $e) {
- common_log(LOG_ERR, 'Webfinger lookup failed for ' .
- $arg . ': ' . $e->getMessage());
- }
- }
-
- $urls = array();
- if (preg_match('!^https?://((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
- $urls[] = $arg;
- }
- if (preg_match('!^((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)+)$!', $arg)) {
- $schemes = array('http', 'https');
- foreach ($schemes as $scheme) {
- $urls[] = "$scheme://$arg";
- }
- }
- foreach ($urls as $url) {
- try {
- return Ostatus_profile::ensureProfileURL($url);
- } catch (Exception $e) {
- common_log(LOG_ERR, 'Profile lookup failed for ' .
- $arg . ': ' . $e->getMessage());
- }
- }
- return null;
- }
- function onEndProfileSettingsActions($out) {
- $siteName = common_config('site', 'name');
- $js = 'navigator.registerContentHandler("application/vnd.mozilla.maybe.feed", "'.addslashes(common_local_url('ostatussub', null, array('profile' => '%s'))).'", "'.addslashes($siteName).'")';
- $out->elementStart('li');
- $out->element('a',
- array('href' => 'javascript:'.$js),
-
- _('Add to Firefox as feedreader'));
- $out->elementEnd('li');
- }
-
- function onCheckSchema() {
- $schema = Schema::get();
- $schema->ensureTable('ostatus_profile', Ostatus_profile::schemaDef());
- $schema->ensureTable('ostatus_source', Ostatus_source::schemaDef());
- $schema->ensureTable('feedsub', FeedSub::schemaDef());
- $schema->ensureTable('hubsub', HubSub::schemaDef());
- $schema->ensureTable('magicsig', Magicsig::schemaDef());
- return true;
- }
- public function onEndShowStylesheets(Action $action) {
- $action->cssLink($this->path('theme/base/css/ostatus.css'));
- return true;
- }
- function onEndShowStatusNetScripts($action) {
- $action->script($this->path('js/ostatus.js'));
- return true;
- }
-
- function onStartNoticeSourceLink($notice, &$name, &$url, &$title)
- {
-
- if (!in_array($notice->source, array('ostatus', 'share'))) {
- return true;
- }
- try {
- $url = $notice->getUrl();
-
-
- $bits = parse_url($url);
- $domain = $bits['host'];
- if (substr($domain, 0, 4) == 'www.') {
- $name = substr($domain, 4);
- } else {
- $name = $domain;
- }
-
- $title = sprintf(_m('Sent from %s via OStatus'), $domain);
-
- return false;
- } catch (InvalidUrlException $e) {
-
- return true;
- }
- }
-
- function onStartFeedSubReceive($feedsub, $feed)
- {
- $oprofile = Ostatus_profile::getKV('feeduri', $feedsub->uri);
- if ($oprofile instanceof Ostatus_profile) {
- $oprofile->processFeed($feed, 'push');
- } else {
- common_log(LOG_DEBUG, "No ostatus profile for incoming feed $feedsub->uri");
- }
- }
-
- function onFeedSubSubscriberCount($feedsub, &$count)
- {
- $oprofile = Ostatus_profile::getKV('feeduri', $feedsub->uri);
- if ($oprofile instanceof Ostatus_profile) {
- $count += $oprofile->subscriberCount();
- }
- return true;
- }
-
- function onStartSubscribe(Profile $profile, Profile $other)
- {
- if (!$profile->isLocal()) {
- return true;
- }
- $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $oprofile->subscribe();
- }
-
- function onEndSubscribe(Profile $profile, Profile $other)
- {
- if (!$profile->isLocal()) {
- return true;
- }
- $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $sub = Subscription::pkeyGet(array('subscriber' => $profile->id,
- 'subscribed' => $other->id));
- $act = $sub->asActivity();
- $oprofile->notifyActivity($act, $profile);
- return true;
- }
-
- function onEndUnsubscribe(Profile $profile, Profile $other)
- {
- if (!$profile->isLocal()) {
- return true;
- }
- $oprofile = Ostatus_profile::getKV('profile_id', $other->id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
-
- $oprofile->garbageCollect();
- $act = new Activity();
- $act->verb = ActivityVerb::UNFOLLOW;
- $act->id = TagURI::mint('unfollow:%d:%d:%s',
- $profile->id,
- $other->id,
- common_date_iso8601(time()));
- $act->time = time();
-
- $act->title = _m('TITLE','Unfollow');
-
-
- $act->content = sprintf(_m('%1$s stopped following %2$s.'),
- $profile->getBestName(),
- $other->getBestName());
- $act->actor = $profile->asActivityObject();
- $act->objects[] = $other->asActivityObject();
- $oprofile->notifyActivity($act, $profile);
- return true;
- }
-
- function onStartJoinGroup($group, $profile)
- {
- $oprofile = Ostatus_profile::getKV('group_id', $group->id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $oprofile->subscribe();
-
-
- $act = new Activity();
- $act->id = TagURI::mint('join:%d:%d:%s',
- $profile->id,
- $group->id,
- common_date_iso8601(time()));
- $act->actor = $profile->asActivityObject();
- $act->verb = ActivityVerb::JOIN;
- $act->objects[] = $oprofile->asActivityObject();
- $act->time = time();
-
- $act->title = _m('TITLE','Join');
-
-
- $act->content = sprintf(_m('%1$s has joined group %2$s.'),
- $profile->getBestName(),
- $oprofile->getBestName());
- if ($oprofile->notifyActivity($act, $profile)) {
- return true;
- } else {
- $oprofile->garbageCollect();
-
- throw new Exception(_m('Failed joining remote group.'));
- }
- }
-
- function onEndLeaveGroup($group, $profile)
- {
- $oprofile = Ostatus_profile::getKV('group_id', $group->id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
-
- $oprofile->garbageCollect();
- $member = $profile;
- $act = new Activity();
- $act->id = TagURI::mint('leave:%d:%d:%s',
- $member->id,
- $group->id,
- common_date_iso8601(time()));
- $act->actor = $member->asActivityObject();
- $act->verb = ActivityVerb::LEAVE;
- $act->objects[] = $oprofile->asActivityObject();
- $act->time = time();
-
- $act->title = _m('TITLE','Leave');
-
-
- $act->content = sprintf(_m('%1$s has left group %2$s.'),
- $member->getBestName(),
- $oprofile->getBestName());
- $oprofile->notifyActivity($act, $member);
- }
-
- function onStartSubscribePeopletag($peopletag, $user)
- {
- $oprofile = Ostatus_profile::getKV('peopletag_id', $peopletag->id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $oprofile->subscribe();
- $sub = $user->getProfile();
- $tagger = Profile::getKV($peopletag->tagger);
- $act = new Activity();
- $act->id = TagURI::mint('subscribe_peopletag:%d:%d:%s',
- $sub->id,
- $peopletag->id,
- common_date_iso8601(time()));
- $act->actor = $sub->asActivityObject();
- $act->verb = ActivityVerb::FOLLOW;
- $act->objects[] = $oprofile->asActivityObject();
- $act->time = time();
-
- $act->title = _m('TITLE','Follow list');
-
-
- $act->content = sprintf(_m('%1$s is now following people listed in %2$s by %3$s.'),
- $sub->getBestName(),
- $oprofile->getBestName(),
- $tagger->getBestName());
- if ($oprofile->notifyActivity($act, $sub)) {
- return true;
- } else {
- $oprofile->garbageCollect();
-
- throw new Exception(_m('Failed subscribing to remote list.'));
- }
- }
-
- function onEndUnsubscribePeopletag($peopletag, $user)
- {
- $oprofile = Ostatus_profile::getKV('peopletag_id', $peopletag->id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
-
- $oprofile->garbageCollect();
- $sub = Profile::getKV($user->id);
- $tagger = Profile::getKV($peopletag->tagger);
- $act = new Activity();
- $act->id = TagURI::mint('unsubscribe_peopletag:%d:%d:%s',
- $sub->id,
- $peopletag->id,
- common_date_iso8601(time()));
- $act->actor = $member->asActivityObject();
- $act->verb = ActivityVerb::UNFOLLOW;
- $act->objects[] = $oprofile->asActivityObject();
- $act->time = time();
-
- $act->title = _m('Unfollow list');
-
-
- $act->content = sprintf(_m('%1$s stopped following the list %2$s by %3$s.'),
- $sub->getBestName(),
- $oprofile->getBestName(),
- $tagger->getBestName());
- $oprofile->notifyActivity($act, $user);
- }
-
- function onEndFavorNotice(Profile $profile, Notice $notice)
- {
-
-
- if (!$profile->isLocal()) {
- return true;
- }
- $oprofile = Ostatus_profile::getKV('profile_id', $notice->profile_id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $fav = Fave::pkeyGet(array('user_id' => $profile->id,
- 'notice_id' => $notice->id));
- if (!$fav instanceof Fave) {
-
-
- return true;
- }
- $act = $fav->asActivity();
- $oprofile->notifyActivity($act, $profile);
- return true;
- }
-
- function onEndTagProfile($ptag)
- {
- $oprofile = Ostatus_profile::getKV('profile_id', $ptag->tagged);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $plist = $ptag->getMeta();
- if ($plist->private) {
- return true;
- }
- $act = new Activity();
- $tagger = $plist->getTagger();
- $tagged = Profile::getKV('id', $ptag->tagged);
- $act->verb = ActivityVerb::TAG;
- $act->id = TagURI::mint('tag_profile:%d:%d:%s',
- $plist->tagger, $plist->id,
- common_date_iso8601(time()));
- $act->time = time();
-
- $act->title = _m('TITLE','List');
-
-
- $act->content = sprintf(_m('%1$s listed %2$s in the list %3$s.'),
- $tagger->getBestName(),
- $tagged->getBestName(),
- $plist->getBestName());
- $act->actor = $tagger->asActivityObject();
- $act->objects = array($tagged->asActivityObject());
- $act->target = ActivityObject::fromPeopletag($plist);
- $oprofile->notifyDeferred($act, $tagger);
-
- $oprofile->subscribe();
- return true;
- }
-
- function onEndUntagProfile($ptag)
- {
- $oprofile = Ostatus_profile::getKV('profile_id', $ptag->tagged);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $plist = $ptag->getMeta();
- if ($plist->private) {
- return true;
- }
- $act = new Activity();
- $tagger = $plist->getTagger();
- $tagged = Profile::getKV('id', $ptag->tagged);
- $act->verb = ActivityVerb::UNTAG;
- $act->id = TagURI::mint('untag_profile:%d:%d:%s',
- $plist->tagger, $plist->id,
- common_date_iso8601(time()));
- $act->time = time();
-
- $act->title = _m('TITLE','Unlist');
-
-
- $act->content = sprintf(_m('%1$s removed %2$s from the list %3$s.'),
- $tagger->getBestName(),
- $tagged->getBestName(),
- $plist->getBestName());
- $act->actor = $tagger->asActivityObject();
- $act->objects = array($tagged->asActivityObject());
- $act->target = ActivityObject::fromPeopletag($plist);
- $oprofile->notifyDeferred($act, $tagger);
-
- $oprofile->garbageCollect();
- return true;
- }
-
- function onEndDisfavorNotice(Profile $profile, Notice $notice)
- {
-
-
- if (!$profile->isLocal()) {
- return true;
- }
- $oprofile = Ostatus_profile::getKV('profile_id', $notice->profile_id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $act = new Activity();
- $act->verb = ActivityVerb::UNFAVORITE;
- $act->id = TagURI::mint('disfavor:%d:%d:%s',
- $profile->id,
- $notice->id,
- common_date_iso8601(time()));
- $act->time = time();
-
- $act->title = _m('Unlike');
-
-
- $act->content = sprintf(_m('%1$s no longer likes %2$s.'),
- $profile->getBestName(),
- $notice->getUrl());
- $act->actor = $profile->asActivityObject();
- $act->objects[] = $notice->asActivityObject();
- $oprofile->notifyActivity($act, $profile);
- return true;
- }
- function onStartGetProfileUri($profile, &$uri)
- {
- $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
- if ($oprofile instanceof Ostatus_profile) {
- $uri = $oprofile->uri;
- return false;
- }
- return true;
- }
- function onStartUserGroupHomeUrl($group, &$url)
- {
- return $this->onStartUserGroupPermalink($group, $url);
- }
- function onStartUserGroupPermalink($group, &$url)
- {
- $oprofile = Ostatus_profile::getKV('group_id', $group->id);
- if ($oprofile instanceof Ostatus_profile) {
-
-
- $url = $oprofile->uri;
- return false;
- }
- }
- function onStartShowSubscriptionsContent($action)
- {
- $this->showEntityRemoteSubscribe($action);
- return true;
- }
- function onStartShowUserGroupsContent($action)
- {
- $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
- return true;
- }
- function onEndShowSubscriptionsMiniList($action)
- {
- $this->showEntityRemoteSubscribe($action);
- return true;
- }
- function onEndShowGroupsMiniList($action)
- {
- $this->showEntityRemoteSubscribe($action, 'ostatusgroup');
- return true;
- }
- function showEntityRemoteSubscribe($action, $target='ostatussub')
- {
- if (!$action->getScoped() instanceof Profile) {
-
- return true;
- }
- if ($action->getScoped()->sameAs($action->getTarget())) {
- $action->elementStart('div', 'entity_actions');
- $action->elementStart('p', array('id' => 'entity_remote_subscribe',
- 'class' => 'entity_subscribe'));
- $action->element('a', array('href' => common_local_url($target),
- 'class' => 'entity_remote_subscribe'),
-
- _m('Remote'));
- $action->elementEnd('p');
- $action->elementEnd('div');
- }
- }
-
- function onEndBroadcastProfile(Profile $profile)
- {
- $user = User::getKV('id', $profile->id);
-
-
-
-
-
- $sql = "SELECT * FROM ostatus_profile " .
- "WHERE profile_id IN " .
- "(SELECT subscribed FROM subscription WHERE subscriber=%d) " .
- "OR group_id IN " .
- "(SELECT group_id FROM group_member WHERE profile_id=%d)";
- $oprofile = new Ostatus_profile();
- $oprofile->query(sprintf($sql, $profile->id, $profile->id));
- if ($oprofile->N == 0) {
- common_log(LOG_DEBUG, "No OStatus remote subscribees for $profile->nickname");
- return true;
- }
- $act = new Activity();
- $act->verb = ActivityVerb::UPDATE_PROFILE;
- $act->id = TagURI::mint('update-profile:%d:%s',
- $profile->id,
- common_date_iso8601(time()));
- $act->time = time();
-
- $act->title = _m('Profile update');
-
-
- $act->content = sprintf(_m('%s has updated their profile page.'),
- $profile->getBestName());
- $act->actor = $profile->asActivityObject();
- $act->objects[] = $act->actor;
- while ($oprofile->fetch()) {
- $oprofile->notifyDeferred($act, $profile);
- }
- return true;
- }
-
- function onStartProfileListItemActionElements($item)
- {
- if (common_logged_in()) {
-
- return true;
- } elseif (!$item->getTarget()->isLocal()) {
-
- return true;
- }
- if ($item instanceof ProfileAction) {
- $output = $item;
- } elseif ($item instanceof Widget) {
- $output = $item->out;
- } else {
-
- throw new ServerException('Bad item type for onStartProfileListItemActionElements');
- }
-
- $output->elementStart('li', 'entity_subscribe');
- $url = common_local_url('ostatusinit',
- array('nickname' => $item->getTarget()->getNickname()));
- $output->element('a', array('href' => $url,
- 'class' => 'entity_remote_subscribe'),
-
- _m('Subscribe'));
- $output->elementEnd('li');
- $output->elementStart('li', 'entity_tag');
- $url = common_local_url('ostatustag',
- array('nickname' => $item->getTarget()->getNickname()));
- $output->element('a', array('href' => $url,
- 'class' => 'entity_remote_tag'),
-
- _m('List'));
- $output->elementEnd('li');
- return true;
- }
- function onPluginVersion(array &$versions)
- {
- $versions[] = array('name' => 'OStatus',
- 'version' => GNUSOCIAL_VERSION,
- 'author' => 'Evan Prodromou, James Walker, Brion Vibber, Zach Copley',
- 'homepage' => 'http://status.net/wiki/Plugin:OStatus',
-
- 'rawdescription' => _m('Follow people across social networks that implement '.
- '<a href="http://ostatus.org/">OStatus</a>.'));
- return true;
- }
-
- public static function localGroupFromUrl($url)
- {
- $group = User_group::getKV('uri', $url);
- if ($group instanceof User_group) {
- if ($group->isLocal()) {
- return $group->id;
- }
- } else {
-
-
-
- $template = common_local_url('groupbyid', array('id' => '31337'));
- $template = preg_quote($template, '/');
- $template = str_replace('31337', '(\d+)', $template);
- if (preg_match("/$template/", $url, $matches)) {
- return intval($matches[1]);
- }
- }
- return false;
- }
- public function onStartProfileGetAtomFeed($profile, &$feed)
- {
- $oprofile = Ostatus_profile::getKV('profile_id', $profile->id);
- if (!$oprofile instanceof Ostatus_profile) {
- return true;
- }
- $feed = $oprofile->feeduri;
- return false;
- }
- function onStartGetProfileFromURI($uri, &$profile)
- {
-
-
-
-
- $user = User::getKV('uri', $uri);
- if ($user instanceof User) {
- $profile = $user->getProfile();
- return false;
- } else {
- $group = User_group::getKV('uri', $uri);
- if ($group instanceof User_group) {
- $profile = $group->getProfile();
- return false;
- }
- }
-
- try {
- $oprofile = Ostatus_profile::ensureProfileURI($uri);
- $profile = $oprofile->localProfile();
- return !($profile instanceof Profile);
- } catch (Exception $e) {
- return true;
- }
- }
- function onEndWebFingerNoticeLinks(XML_XRD $xrd, Notice $target)
- {
- $author = $target->getProfile();
- $profiletype = $this->profileTypeString($author);
- $salmon_url = common_local_url("{$profiletype}salmon", array('id' => $author->id));
- $xrd->links[] = new XML_XRD_Element_Link(Salmon::REL_SALMON, $salmon_url);
- return true;
- }
- function onEndWebFingerProfileLinks(XML_XRD $xrd, Profile $target)
- {
- if ($target->getObjectType() === ActivityObject::PERSON) {
- $this->addWebFingerPersonLinks($xrd, $target);
- }
-
- $profiletype = $this->profileTypeString($target);
- $salmon_url = common_local_url("{$profiletype}salmon", array('id' => $target->id));
- $xrd->links[] = new XML_XRD_Element_Link(Salmon::REL_SALMON, $salmon_url);
-
- $xrd->links[] = new XML_XRD_Element_Link(Salmon::NS_REPLIES, $salmon_url);
- $xrd->links[] = new XML_XRD_Element_Link(Salmon::NS_MENTIONS, $salmon_url);
-
- $xrd->links[] = new XML_XRD_Element_Link('http://ostatus.org/schema/1.0/subscribe',
- common_local_url('ostatussub') . '?profile={uri}',
- null,
- true);
- return true;
- }
- protected function profileTypeString(Profile $target)
- {
-
- switch ($target->getObjectType()) {
- case ActivityObject::PERSON:
- return 'user';
- case ActivityObject::GROUP:
- return 'group';
- default:
- throw new ServerException('Unknown profile type for WebFinger profile links');
- }
- }
- protected function addWebFingerPersonLinks(XML_XRD $xrd, Profile $target)
- {
- $xrd->links[] = new XML_XRD_Element_Link(Discovery::UPDATESFROM,
- common_local_url('ApiTimelineUser',
- array('id' => $target->id, 'format' => 'atom')),
- 'application/atom+xml');
-
- $magicsig = Magicsig::getKV('user_id', $target->id);
- if (!$magicsig instanceof Magicsig && $target->isLocal()) {
- $magicsig = Magicsig::generate($target->getUser());
- }
- if (!$magicsig instanceof Magicsig) {
- return false;
- }
- if (Event::handle('StartAttachPubkeyToUserXRD', array($magicsig, $xrd, $target))) {
- $xrd->links[] = new XML_XRD_Element_Link(Magicsig::PUBLICKEYREL,
- 'data:application/magic-public-key,'. $magicsig->toString());
-
- Event::handle('EndAttachPubkeyToUserXRD', array($magicsig, $xrd, $target));
- }
- }
- public function onGetLocalAttentions(Profile $actor, array $attention_uris, array &$mentions, array &$groups)
- {
- list($groups, $mentions) = Ostatus_profile::filterAttention($actor, $attention_uris);
- }
-
- static public function onCheckActivityAuthorship(Activity $activity, Profile &$profile)
- {
- try {
- $oprofile = Ostatus_profile::ensureProfileURL($profile->getUrl());
- $profile = $oprofile->checkAuthorship($activity);
- } catch (Exception $e) {
- common_log(LOG_ERR, 'Could not get a profile or check authorship ('.get_class($e).': "'.$e->getMessage().'") for activity ID: '.$activity->id);
- $profile = null;
- return false;
- }
- return true;
- }
- public function onProfileDeleteRelated($profile, &$related)
- {
-
- $related[] = 'Ostatus_profile';
-
- $magicsig = Magicsig::getKV('user_id', $profile->id);
- if ($magicsig instanceof Magicsig) {
- $magicsig->delete();
- }
- return true;
- }
- public function onSalmonSlap($endpoint_uri, MagicEnvelope $magic_env, Profile $target=null)
- {
- $envxml = $magic_env->toXML($target);
- $headers = array('Content-Type: application/magic-envelope+xml');
- try {
- $client = new HTTPClient();
- $client->setBody($envxml);
- $response = $client->post($endpoint_uri, $headers);
- } catch (Exception $e) {
- common_log(LOG_ERR, "Salmon post to $endpoint_uri failed: " . $e->getMessage());
- return false;
- }
- if ($response->getStatus() === 422) {
- common_debug(sprintf('Salmon (from profile %d) endpoint %s returned status %s. We assume it is a Diaspora seed; will adapt and try again if that plugin is enabled!', $magic_env->getActor()->getID(), $endpoint_uri, $response->getStatus()));
- return true;
- }
-
-
- if (!in_array($response->getStatus(), array(200, 202))) {
- common_log(LOG_ERR, sprintf('Salmon (from profile %d) endpoint %s returned status %s: %s',
- $magic_env->getActor()->getID(), $endpoint_uri, $response->getStatus(), $response->getBody()));
- return true;
- }
-
- return false;
- }
- public function onCronDaily()
- {
- try {
- $sub = FeedSub::renewalCheck();
- } catch (NoResultException $e) {
- common_log(LOG_INFO, "There were no expiring feeds.");
- return;
- }
- $qm = QueueManager::get();
- while ($sub->fetch()) {
- $item = array('feedsub_id' => $sub->id);
- $qm->enqueue($item, 'pushrenew');
- }
- }
- }
|