12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865 |
- <?php
- // This file is part of GNU social - https://www.gnu.org/software/social
- //
- // GNU social is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // GNU social is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
- /**
- * @copyright 2008-2011 StatusNet, Inc.
- * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
- */
- defined('GNUSOCIAL') || die();
- /**
- * Table Definition for profile
- */
- class Profile extends Managed_DataObject
- {
- public $__table = 'profile'; // table name
- public $id; // int(4) primary_key not_null
- public $nickname; // varchar(64) multiple_key not_null
- public $fullname; // text()
- public $profileurl; // text()
- public $homepage; // text()
- public $bio; // text() multiple_key
- public $location; // text()
- public $lat; // decimal(10,7)
- public $lon; // decimal(10,7)
- public $location_id; // int(4)
- public $location_ns; // int(4)
- public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
- public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
- public static function schemaDef()
- {
- $def = array(
- 'description' => 'local and remote users have profiles',
- 'fields' => array(
- 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
- 'nickname' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'nickname or username', 'collate' => 'utf8mb4_general_ci'),
- 'fullname' => array('type' => 'text', 'description' => 'display name', 'collate' => 'utf8mb4_general_ci'),
- 'profileurl' => array('type' => 'text', 'description' => 'URL, cached so we dont regenerate'),
- 'homepage' => array('type' => 'text', 'description' => 'identifying URL', 'collate' => 'utf8mb4_general_ci'),
- 'bio' => array('type' => 'text', 'description' => 'descriptive biography', 'collate' => 'utf8mb4_general_ci'),
- 'location' => array('type' => 'text', 'description' => 'physical location', 'collate' => 'utf8mb4_general_ci'),
- 'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'),
- 'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'),
- 'location_id' => array('type' => 'int', 'description' => 'location id if possible'),
- 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'),
- 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
- 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
- ),
- 'primary key' => array('id'),
- 'indexes' => array(
- 'profile_nickname_idx' => array('nickname'),
- )
- );
- // Add a fulltext index
- if (common_config('search', 'type') == 'fulltext') {
- $def['fulltext indexes'] = array('nickname' => array('nickname', 'fullname', 'location', 'bio', 'homepage'));
- }
- return $def;
- }
- public static function getByEmail($email)
- {
- // in the future, profiles should have emails stored...
- $user = User::getKV('email', $email);
- if (!($user instanceof User)) {
- throw new NoSuchUserException(array('email'=>$email));
- }
- return $user->getProfile();
- }
- protected $_user = array();
- public function getUser()
- {
- if (!isset($this->_user[$this->id])) {
- $cur_user = common_current_user();
- if (($cur_user instanceof User) && $cur_user->sameAs($this)) {
- $user = $cur_user;
- } else {
- $user = User::getKV('id', $this->id);
- if (!$user instanceof User) {
- throw new NoSuchUserException(array('id'=>$this->id));
- }
- }
- $this->_user[$this->id] = $user;
- }
- return $this->_user[$this->id];
- }
- protected $_group = array();
- public function getGroup()
- {
- if (!isset($this->_group[$this->id])) {
- $group = User_group::getKV('profile_id', $this->id);
- if (!$group instanceof User_group) {
- throw new NoSuchGroupException(array('profile_id'=>$this->id));
- }
- $this->_group[$this->id] = $group;
- }
- return $this->_group[$this->id];
- }
- public function isGroup()
- {
- try {
- $this->getGroup();
- return true;
- } catch (NoSuchGroupException $e) {
- return false;
- }
- }
- public function isPerson()
- {
- // Maybe other things than PERSON and GROUP can have Profiles in the future?
- return !$this->isGroup();
- }
- public function isLocal()
- {
- try {
- $this->getUser();
- } catch (NoSuchUserException $e) {
- return false;
- }
- return true;
- }
- // Returns false if the user has no password (which will always
- // be the case for remote users). This can be the case for OpenID
- // logins or other mechanisms which don't store a password hash.
- public function hasPassword()
- {
- try {
- return $this->getUser()->hasPassword();
- } catch (NoSuchUserException $e) {
- return false;
- }
- }
- public function getObjectType()
- {
- // FIXME: More types... like peopletags and whatever
- if ($this->isGroup()) {
- return ActivityObject::GROUP;
- } else {
- return ActivityObject::PERSON;
- }
- }
- public function getAvatar($width, $height=null)
- {
- return Avatar::byProfile($this, $width, $height);
- }
- public function setOriginal($filename)
- {
- if ($this->isGroup()) {
- // Until Group avatars are handled just like profile avatars.
- return $this->getGroup()->setOriginal($filename);
- }
- $imagefile = new ImageFile(null, Avatar::path($filename));
- $avatar = new Avatar();
- $avatar->profile_id = $this->id;
- $avatar->width = $imagefile->width;
- $avatar->height = $imagefile->height;
- $avatar->mediatype = image_type_to_mime_type($imagefile->type);
- $avatar->filename = $filename;
- $avatar->original = true;
- $avatar->created = common_sql_now();
- // XXX: start a transaction here
- if (!Avatar::deleteFromProfile($this, true) || !$avatar->insert()) {
- // If we can't delete the old avatars, let's abort right here.
- @unlink(Avatar::path($filename));
- return null;
- }
- return $avatar;
- }
- /**
- * Gets either the full name (if filled) or the nickname.
- *
- * @return string
- */
- public function getBestName()
- {
- return ($this->fullname) ? $this->fullname : $this->nickname;
- }
- /**
- * Takes the currently scoped profile into account to give a name
- * to list in notice streams. Preferences may differ between profiles.
- */
- public function getStreamName()
- {
- $user = common_current_user();
- if ($user instanceof User && $user->streamNicknames()) {
- return $this->nickname;
- }
- return $this->getBestName();
- }
- /**
- * Gets the full name (if filled) with acct URI, URL, or URI as a
- * parenthetical (in that order, for each not found). If no full
- * name is found only the second part is returned, without ()s.
- *
- * @return string
- */
- public function getFancyName()
- {
- $uri = null;
- try {
- $uri = $this->getAcctUri(false);
- } catch (ProfileNoAcctUriException $e) {
- try {
- $uri = $this->getUrl();
- } catch (InvalidUrlException $e) {
- $uri = $this->getUri();
- }
- }
- if (mb_strlen($this->getFullname()) > 0) {
- // TRANS: The "fancy name": Full name of a profile or group (%1$s) followed by some URI (%2$s) in parentheses.
- return sprintf(_m('FANCYNAME', '%1$s (%2$s)'), $this->getFullname(), $uri);
- } else {
- return $uri;
- }
- }
- /**
- * Get the most recent notice posted by this user, if any.
- *
- * @return mixed Notice or null
- */
- public function getCurrentNotice(Profile $scoped = null)
- {
- try {
- $notice = $this->getNotices(0, 1, 0, 0, $scoped);
- if ($notice->fetch()) {
- if ($notice instanceof ArrayWrapper) {
- // hack for things trying to work with single notices
- // ...but this shouldn't happen anymore I think. Keeping it for safety...
- return $notice->_items[0];
- }
- return $notice;
- }
- } catch (PrivateStreamException $e) {
- // Maybe we should let this through if it's handled well upstream
- return null;
- }
- return null;
- }
- public function getReplies($offset = 0, $limit = NOTICES_PER_PAGE, $since_id = 0, $before_id = 0)
- {
- return Reply::stream($this->getID(), $offset, $limit, $since_id, $before_id);
- }
- public function getTaggedNotices($tag, $offset = 0, $limit = NOTICES_PER_PAGE, $since_id = 0, $max_id = 0)
- {
- //FIXME: Get Profile::current() some other way to avoid possible
- // confusion between current session profile and background processing.
- $stream = new TaggedProfileNoticeStream($this, $tag, Profile::current());
- return $stream->getNotices($offset, $limit, $since_id, $max_id);
- }
- public function getNotices($offset = 0, $limit = NOTICES_PER_PAGE, $since_id = 0, $max_id = 0, Profile $scoped = null)
- {
- $stream = new ProfileNoticeStream($this, $scoped);
- return $stream->getNotices($offset, $limit, $since_id, $max_id);
- }
- public function isMember(User_group $group)
- {
- $groups = $this->getGroups(0, null);
- while ($groups instanceof User_group && $groups->fetch()) {
- if ($groups->id == $group->id) {
- return true;
- }
- }
- return false;
- }
- public function isAdmin(User_group $group)
- {
- $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
- 'group_id' => $group->id));
- return (!empty($gm) && $gm->is_admin);
- }
- public function isPendingMember($group)
- {
- $request = Group_join_queue::pkeyGet(array('profile_id' => $this->id,
- 'group_id' => $group->id));
- return !empty($request);
- }
- public function getGroups($offset = 0, $limit = PROFILES_PER_PAGE)
- {
- $ids = array();
- $keypart = sprintf('profile:groups:%d', $this->id);
- $idstring = self::cacheGet($keypart);
- if ($idstring !== false) {
- $ids = explode(',', $idstring);
- } else {
- $gm = new Group_member();
- $gm->profile_id = $this->id;
- if ($gm->find()) {
- while ($gm->fetch()) {
- $ids[] = $gm->group_id;
- }
- }
- self::cacheSet($keypart, implode(',', $ids));
- }
- if (!is_null($offset) && !is_null($limit)) {
- $ids = array_slice($ids, $offset, $limit);
- }
- try {
- return User_group::multiGet('id', $ids);
- } catch (NoResultException $e) {
- return null; // throw exception when we handle it everywhere
- }
- }
- public function getGroupCount()
- {
- $groups = $this->getGroups(0, null);
- return $groups instanceof User_group
- ? $groups->N
- : 0;
- }
- public function isTagged($peopletag)
- {
- $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
- 'tagged' => $this->id,
- 'tag' => $peopletag->tag));
- return !empty($tag);
- }
- public function canTag($tagged)
- {
- if (empty($tagged)) {
- return false;
- }
- if ($tagged->id == $this->id) {
- return true;
- }
- $all = common_config('peopletag', 'allow_tagging', 'all');
- $local = common_config('peopletag', 'allow_tagging', 'local');
- $remote = common_config('peopletag', 'allow_tagging', 'remote');
- $subs = common_config('peopletag', 'allow_tagging', 'subs');
- if ($all) {
- return true;
- }
- $tagged_user = $tagged->getUser();
- if (!empty($tagged_user)) {
- if ($local) {
- return true;
- }
- } elseif ($subs) {
- return (Subscription::exists($this, $tagged) ||
- Subscription::exists($tagged, $this));
- } elseif ($remote) {
- return true;
- }
- return false;
- }
- public function getLists(Profile $scoped = null, $offset = 0, $limit = null, $since_id = 0, $max_id = 0)
- {
- $ids = array();
- $keypart = sprintf('profile:lists:%d', $this->id);
- $idstr = self::cacheGet($keypart);
- if ($idstr !== false) {
- $ids = explode(',', $idstr);
- } else {
- $list = new Profile_list();
- $list->selectAdd();
- $list->selectAdd('id');
- $list->tagger = $this->id;
- $list->selectAdd('id as "cursor"');
- if ($since_id > 0) {
- $list->whereAdd('id > ' . $since_id);
- }
- if ($max_id > 0) {
- $list->whereAdd('id <= ' . $max_id);
- }
- if ($offset >= 0 && !is_null($limit)) {
- $list->limit($offset, $limit);
- }
- $list->orderBy('id DESC');
- if ($list->find()) {
- while ($list->fetch()) {
- $ids[] = $list->id;
- }
- }
- self::cacheSet($keypart, implode(',', $ids));
- }
- $showPrivate = $this->sameAs($scoped);
- $lists = array();
- foreach ($ids as $id) {
- $list = Profile_list::getKV('id', $id);
- if (!empty($list) &&
- ($showPrivate || !$list->private)) {
- if (!isset($list->cursor)) {
- $list->cursor = $list->id;
- }
- $lists[] = $list;
- }
- }
- return new ArrayWrapper($lists);
- }
- /**
- * Get tags that other people put on this profile, in reverse-chron order
- *
- * @param Profile $scoped User we are requesting as
- * @param int $offset Offset from latest
- * @param int $limit Max number to get
- * @param datetime $since_id max date
- * @param datetime $max_id min date
- *
- * @return Profile_list resulting lists
- */
- public function getOtherTags(Profile $scoped = null, int $offset = 0, ?int $limit = null, int $since = 0, int $upto = 0)
- {
- $list = new Profile_list();
- if (common_config('db', 'type') !== 'mysql') {
- $cursor = sprintf(
- '((EXTRACT(DAY %1$s) * 24 + EXTRACT(HOUR %1$s)) * 60 + ' .
- 'EXTRACT(MINUTE %1$s)) * 60 + FLOOR(EXTRACT(SECOND %1$s)) AS "cursor"',
- "FROM (profile_tag.modified - TIMESTAMP '1970-01-01 00:00:00')"
- );
- } else {
- // The SQL/Foundation conforming implementation above doesn't work on MariaDB/MySQL
- $cursor = "timestampdiff(SECOND, '1970-01-01', profile_tag.modified) AS `cursor`";
- }
- $qry = sprintf(
- 'SELECT profile_list.*, ' . $cursor . ' ' .
- 'FROM profile_tag INNER JOIN profile_list ' .
- 'ON (profile_tag.tagger = profile_list.tagger ' .
- ' AND profile_tag.tag = profile_list.tag) ' .
- 'WHERE profile_tag.tagged = %d ',
- $this->id
- );
- if (!is_null($scoped)) {
- $qry .= sprintf(
- 'AND ( profile_list.private = false ' .
- 'OR ( profile_list.tagger = %d AND ' .
- 'profile_list.private = TRUE ) )',
- $scoped->getID()
- );
- } else {
- $qry .= 'AND profile_list.private = FALSE ';
- }
- if ($since > 0) {
- $qry .= 'AND cursor > ' . $since . ' ';
- }
- if ($upto > 0) {
- $qry .= 'AND cursor < ' . $upto . ' ';
- }
- $qry .= 'ORDER BY profile_tag.modified DESC ';
- if ($offset >= 0 && !is_null($limit)) {
- $qry .= sprintf('LIMIT %d OFFSET %d ', $limit, $offset);
- }
- $list->query($qry);
- return $list;
- }
- public function getPrivateTags($offset = 0, $limit = null, $since_id = 0, $max_id = 0)
- {
- $tags = new Profile_list();
- $tags->private = true;
- $tags->tagger = $this->id;
- if ($since_id > 0) {
- $tags->whereAdd('id > ' . $since_id);
- }
- if ($max_id > 0) {
- $tags->whereAdd('id <= ' . $max_id);
- }
- if ($offset >= 0 && !is_null($limit)) {
- $tags->limit($offset, $limit);
- }
- $tags->orderBy('id DESC');
- $tags->find();
- return $tags;
- }
- public function hasLocalTags()
- {
- $tags = new Profile_tag();
- $tags->joinAdd(array('tagger', 'user:id'));
- $tags->whereAdd('tagged = ' . $this->id);
- $tags->whereAdd('tagger <> ' . $this->id);
- $tags->limit(0, 1);
- $tags->fetch();
- return ($tags->N == 0) ? false : true;
- }
- public function getTagSubscriptions(int $offset = 0, ?int $limit = null, int $since = 0, int $upto = 0)
- {
- $lists = new Profile_list();
- $subs = new Profile_tag_subscription();
- $lists->joinAdd(['id', 'profile_tag_subscription:profile_tag_id']);
- if (common_config('db', 'type') !== 'mysql') {
- $lists->selectAdd(sprintf(
- '((EXTRACT(DAY %1$s) * 24 + EXTRACT(HOUR %1$s)) * 60 + ' .
- 'EXTRACT(MINUTE %1$s)) * 60 + FLOOR(EXTRACT(SECOND %1$s)) AS "cursor"',
- "FROM (profile_tag_subscription.created - TIMESTAMP '1970-01-01 00:00:00')"
- ));
- } else {
- $lists->selectAdd("timestampdiff(SECOND, '1970-01-01', profile_tag_subscription.created) AS `cursor`");
- }
- $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
- if ($since > 0) {
- $lists->whereAdd('cursor > ' . $since);
- }
- if ($upto > 0) {
- $lists->whereAdd('cursor <= ' . $upto);
- }
- if ($offset >= 0 && !is_null($limit)) {
- $lists->limit($offset, $limit);
- }
- $lists->orderBy('profile_tag_subscription.created DESC');
- $lists->find();
- return $lists;
- }
- /**
- * Request to join the given group.
- * May throw exceptions on failure.
- *
- * @param User_group $group
- * @return mixed: Group_member on success, Group_join_queue if pending approval, null on some cancels?
- */
- public function joinGroup(User_group $group)
- {
- $join = null;
- if ($group->join_policy == User_group::JOIN_POLICY_MODERATE) {
- $join = Group_join_queue::saveNew($this, $group);
- } else {
- if (Event::handle('StartJoinGroup', array($group, $this))) {
- $join = Group_member::join($group->id, $this->id);
- self::blow('profile:groups:%d', $this->id);
- self::blow('group:member_ids:%d', $group->id);
- self::blow('group:member_count:%d', $group->id);
- Event::handle('EndJoinGroup', array($group, $this));
- }
- }
- if ($join) {
- // Send any applicable notifications...
- $join->notify();
- }
- return $join;
- }
- /**
- * Leave a group that this profile is a member of.
- *
- * @param User_group $group
- */
- public function leaveGroup(User_group $group)
- {
- if (Event::handle('StartLeaveGroup', array($group, $this))) {
- Group_member::leave($group->id, $this->id);
- self::blow('profile:groups:%d', $this->id);
- self::blow('group:member_ids:%d', $group->id);
- self::blow('group:member_count:%d', $group->id);
- Event::handle('EndLeaveGroup', array($group, $this));
- }
- }
- public function avatarUrl($size = AVATAR_PROFILE_SIZE)
- {
- return Avatar::urlByProfile($this, $size);
- }
- public function getSubscribed($offset = 0, $limit = null)
- {
- $subs = Subscription::getSubscribedIDs($this->id, $offset, $limit);
- try {
- $profiles = Profile::multiGet('id', $subs);
- } catch (NoResultException $e) {
- return $e->obj;
- }
- return $profiles;
- }
- public function getSubscribers($offset = 0, $limit = null)
- {
- $subs = Subscription::getSubscriberIDs($this->id, $offset, $limit);
- try {
- $profiles = Profile::multiGet('id', $subs);
- } catch (NoResultException $e) {
- return $e->obj;
- }
- return $profiles;
- }
- public function getTaggedSubscribers($tag, $offset = 0, $limit = null)
- {
- $qry =
- 'SELECT profile.* ' .
- 'FROM profile JOIN subscription ' .
- 'ON profile.id = subscription.subscriber ' .
- 'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
- 'AND profile_tag.tagger = subscription.subscribed) ' .
- 'WHERE subscription.subscribed = %d ' .
- "AND profile_tag.tag = '%s' " .
- 'AND subscription.subscribed <> subscription.subscriber ' .
- 'ORDER BY subscription.created DESC ';
- if ($offset) {
- $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
- }
- $profile = new Profile();
- $cnt = $profile->query(sprintf($qry, $this->id, $profile->escape($tag)));
- return $profile;
- }
- public function getTaggedSubscriptions($tag, $offset = 0, $limit = null)
- {
- $qry =
- 'SELECT profile.* ' .
- 'FROM profile JOIN subscription ' .
- 'ON profile.id = subscription.subscribed ' .
- 'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
- 'AND profile_tag.tagger = subscription.subscriber) ' .
- 'WHERE subscription.subscriber = %d ' .
- "AND profile_tag.tag = '%s' " .
- 'AND subscription.subscribed <> subscription.subscriber ' .
- 'ORDER BY subscription.created DESC ';
- $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
- $profile = new Profile();
- $profile->query(sprintf($qry, $this->id, $profile->escape($tag)));
- return $profile;
- }
- /**
- * Get pending subscribers, who have not yet been approved.
- *
- * @param int $offset
- * @param int $limit
- * @return Profile
- */
- public function getRequests($offset = 0, $limit = null)
- {
- // FIXME: mysql only
- $subqueue = new Profile();
- $subqueue->joinAdd(array('id', 'subscription_queue:subscriber'));
- $subqueue->whereAdd(sprintf('subscription_queue.subscribed = %d', $this->getID()));
- $subqueue->limit($offset, $limit);
- $subqueue->orderBy('subscription_queue.created', 'DESC');
- if (!$subqueue->find()) {
- throw new NoResultException($subqueue);
- }
- return $subqueue;
- }
- public function subscriptionCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
- if (is_integer($cnt)) {
- return (int) $cnt;
- }
- }
- $sub = new Subscription();
- $sub->subscriber = $this->id;
- $cnt = (int) $sub->count('distinct subscribed');
- // Local users are subscribed to themselves
- if ($this->isLocal()) {
- $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
- }
- if (!empty($c)) {
- $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
- }
- return $cnt;
- }
- public function subscriberCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
- if (is_integer($cnt)) {
- return (int) $cnt;
- }
- }
- $sub = new Subscription();
- $sub->subscribed = $this->id;
- $sub->whereAdd('subscriber <> subscribed');
- $cnt = (int) $sub->count('DISTINCT subscriber');
- if (!empty($c)) {
- $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
- }
- return $cnt;
- }
- /**
- * Is this profile subscribed to another profile?
- *
- * @param Profile $other
- * @return boolean
- */
- public function isSubscribed(Profile $other)
- {
- return Subscription::exists($this, $other);
- }
- public function readableBy(Profile $other = null)
- {
- // If it's not a private stream, it's readable by anyone
- if (!$this->isPrivateStream()) {
- return true;
- }
- // If it's a private stream, $other must be a subscriber to $this
- return is_null($other) ? false : $other->isSubscribed($this);
- }
- public function requiresSubscriptionApproval(Profile $other = null): bool
- {
- if (!$this->isLocal()) {
- // We don't know for remote users, and we'll always be able to send
- // the request. Whether it'll work immediately or require moderation
- // can be determined in another function.
- return false;
- }
- // Assume that profiles _we_ subscribe to are permitted. Could be made configurable.
- if (!is_null($other) && $this->isSubscribed($other)) {
- return false;
- }
- // If the local user either has a private stream (implies the following)
- // or user has a moderation policy for new subscriptions, return true.
- return $this->isPrivateStream() || $this->getUser()->subscribe_policy === User::SUBSCRIBE_POLICY_MODERATE;
- }
- /**
- * Check if a pending subscription request is outstanding for this...
- *
- * @param Profile $other
- * @return boolean
- */
- public function hasPendingSubscription(Profile $other)
- {
- return Subscription_queue::exists($this, $other);
- }
- /**
- * Are these two profiles subscribed to each other?
- *
- * @param Profile $other
- * @return boolean
- */
- public function mutuallySubscribed(Profile $other)
- {
- return $this->isSubscribed($other) &&
- $other->isSubscribed($this);
- }
- public function noticeCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $cnt = $c->get(Cache::key('profile:notice_count:'.$this->getID()));
- if (is_integer($cnt)) {
- return (int) $cnt;
- }
- }
- $notices = new Notice();
- $notices->profile_id = $this->getID();
- $notices->verb = ActivityVerb::POST;
- $cnt = (int) $notices->count('id'); // Not sure if I imagine this, but 'id' was faster than the defaulting 'uri'?
- if (!empty($c)) {
- $c->set(Cache::key('profile:notice_count:'.$this->getID()), $cnt);
- }
- return $cnt;
- }
- public function blowSubscriberCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
- }
- }
- public function blowSubscriptionCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $c->delete(Cache::key('profile:subscription_count:'.$this->id));
- }
- }
- public function blowNoticeCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $c->delete(Cache::key('profile:notice_count:'.$this->id));
- }
- }
- public static function maxBio()
- {
- $biolimit = common_config('profile', 'biolimit');
- // null => use global limit (distinct from 0!)
- if (is_null($biolimit)) {
- $biolimit = common_config('site', 'textlimit');
- }
- return $biolimit;
- }
- public static function bioTooLong($bio)
- {
- $biolimit = self::maxBio();
- return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
- }
- public function update($dataObject = false)
- {
- if (is_object($dataObject) && $this->nickname != $dataObject->nickname) {
- try {
- $local = $this->getUser();
- common_debug("Updating User ({$this->id}) nickname from {$dataObject->nickname} to {$this->nickname}");
- $origuser = clone($local);
- $local->nickname = $this->nickname;
- // updateWithKeys throws exception on failure.
- $local->updateWithKeys($origuser);
- // Clear the site owner, in case nickname changed
- if ($local->hasRole(Profile_role::OWNER)) {
- User::blow('user:site_owner');
- }
- } catch (NoSuchUserException $e) {
- // Nevermind...
- }
- }
- return parent::update($dataObject);
- }
- public function getRelSelf()
- {
- return ['href' => $this->getUrl(),
- 'text' => common_config('site', 'name'),
- 'image' => Avatar::urlByProfile($this)];
- }
- // All the known rel="me", used for the IndieWeb audience
- public function getRelMes()
- {
- $relMes = array();
- try {
- $relMes[] = $this->getRelSelf();
- } catch (InvalidUrlException $e) {
- // no valid profile URL available
- }
- if (common_valid_http_url($this->getHomepage())) {
- $relMes[] = ['href' => $this->getHomepage(),
- 'text' => _('Homepage'),
- 'image' => null];
- }
- Event::handle('OtherAccountProfiles', array($this, &$relMes));
- return $relMes;
- }
- public function delete($useWhere = false)
- {
- $this->_deleteNotices();
- $this->_deleteSubscriptions();
- $this->_deleteTags();
- $this->_deleteBlocks();
- $this->_deleteAttentions();
- Avatar::deleteFromProfile($this, true);
- // Warning: delete() will run on the batch objects,
- // not on individual objects.
- $related = [
- 'Reply',
- 'Group_member',
- 'Profile_role',
- ];
- Event::handle('ProfileDeleteRelated', array($this, &$related));
- foreach ($related as $cls) {
- $inst = new $cls();
- $inst->profile_id = $this->id;
- $inst->delete();
- }
- $this->grantRole(Profile_role::DELETED);
- $localuser = User::getKV('id', $this->id);
- if ($localuser instanceof User) {
- $localuser->delete();
- }
- return parent::delete($useWhere);
- }
- public function _deleteNotices()
- {
- $notice = new Notice();
- $notice->profile_id = $this->id;
- if ($notice->find()) {
- while ($notice->fetch()) {
- $other = clone($notice);
- $other->delete();
- }
- }
- }
- public function _deleteSubscriptions()
- {
- $sub = new Subscription();
- $sub->subscriber = $this->getID();
- $sub->find();
- while ($sub->fetch()) {
- try {
- $other = $sub->getSubscribed();
- if (!$other->sameAs($this)) {
- Subscription::cancel($this, $other);
- }
- } catch (NoResultException $e) {
- // Profile not found
- common_log(LOG_INFO, 'Subscribed profile id=='.$sub->subscribed.' not found when deleting profile id=='.$this->getID().', ignoring...');
- } catch (ServerException $e) {
- // Subscription cancel failed
- common_log(LOG_INFO, 'Subscribed profile id=='.$other->getID().' could not be reached for unsubscription notice when deleting profile id=='.$this->getID().', ignoring...');
- }
- }
- $sub = new Subscription();
- $sub->subscribed = $this->getID();
- $sub->find();
- while ($sub->fetch()) {
- try {
- $other = $sub->getSubscriber();
- common_log(LOG_INFO, 'Subscriber profile id=='.$sub->subscribed.' not found when deleting profile id=='.$this->getID().', ignoring...');
- if (!$other->sameAs($this)) {
- Subscription::cancel($other, $this);
- }
- } catch (NoResultException $e) {
- // Profile not found
- common_log(LOG_INFO, 'Subscribed profile id=='.$sub->subscribed.' not found when deleting profile id=='.$this->getID().', ignoring...');
- } catch (ServerException $e) {
- // Subscription cancel failed
- common_log(LOG_INFO, 'Subscriber profile id=='.$other->getID().' could not be reached for unsubscription notice when deleting profile id=='.$this->getID().', ignoring...');
- }
- }
- // Finally delete self-subscription
- $self = new Subscription();
- $self->subscriber = $this->getID();
- $self->subscribed = $this->getID();
- $self->delete();
- }
- public function _deleteTags()
- {
- $tag = new Profile_tag();
- $tag->tagged = $this->id;
- $tag->delete();
- }
- public function _deleteBlocks()
- {
- $block = new Profile_block();
- $block->blocked = $this->id;
- $block->delete();
- $block = new Group_block();
- $block->blocked = $this->id;
- $block->delete();
- }
- public function _deleteAttentions()
- {
- $att = new Attention();
- $att->profile_id = $this->getID();
- if ($att->find()) {
- while ($att->fetch()) {
- // Can't do delete() on the object directly since it won't remove all of it
- $other = clone($att);
- $other->delete();
- }
- }
- }
- // XXX: identical to Notice::getLocation.
- public function getLocation()
- {
- $location = null;
- if (!empty($this->location_id) && !empty($this->location_ns)) {
- $location = Location::fromId($this->location_id, $this->location_ns);
- }
- if (is_null($location)) { // no ID, or Location::fromId() failed
- if (!empty($this->lat) && !empty($this->lon)) {
- $location = Location::fromLatLon($this->lat, $this->lon);
- }
- }
- if (is_null($location)) { // still haven't found it!
- if (!empty($this->location)) {
- $location = Location::fromName($this->location);
- }
- }
- return $location;
- }
- public function shareLocation()
- {
- $cfg = common_config('location', 'share');
- if ($cfg == 'always') {
- return true;
- } elseif ($cfg == 'never') {
- return false;
- } else { // user
- $share = common_config('location', 'sharedefault');
- // Check if user has a personal setting for this
- $prefs = User_location_prefs::getKV('user_id', $this->id);
- if (!empty($prefs)) {
- $share = $prefs->share_location;
- $prefs->free();
- }
- return $share;
- }
- }
- public function hasRole($name)
- {
- $has_role = false;
- if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
- $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
- 'role' => $name));
- $has_role = !empty($role);
- Event::handle('EndHasRole', array($this, $name, $has_role));
- }
- return $has_role;
- }
- public function grantRole($name)
- {
- if (Event::handle('StartGrantRole', array($this, $name))) {
- $role = new Profile_role();
- $role->profile_id = $this->id;
- $role->role = $name;
- $role->created = common_sql_now();
- $result = $role->insert();
- if (!$result) {
- throw new Exception("Can't save role '$name' for profile '{$this->id}'");
- }
- if ($name == 'owner') {
- User::blow('user:site_owner');
- }
- Event::handle('EndGrantRole', array($this, $name));
- }
- return $result;
- }
- public function revokeRole($name)
- {
- if (Event::handle('StartRevokeRole', array($this, $name))) {
- $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
- 'role' => $name));
- if (empty($role)) {
- // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
- // TRANS: %1$s is the role name, %2$s is the user ID (number).
- throw new Exception(sprintf(
- _('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),
- $name,
- $this->id
- ));
- }
- $result = $role->delete();
- if (!$result) {
- common_log_db_error($role, 'DELETE', __FILE__);
- // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
- // TRANS: %1$s is the role name, %2$s is the user ID (number).
- throw new Exception(sprintf(
- _('Cannot revoke role "%1$s" for user #%2$d; database error.'),
- $name,
- $this->id
- ));
- }
- if ($name == 'owner') {
- User::blow('user:site_owner');
- }
- Event::handle('EndRevokeRole', array($this, $name));
- return true;
- }
- }
- public function isSandboxed()
- {
- return $this->hasRole(Profile_role::SANDBOXED);
- }
- public function isSilenced()
- {
- return $this->hasRole(Profile_role::SILENCED);
- }
- public function sandbox()
- {
- $this->grantRole(Profile_role::SANDBOXED);
- }
- public function unsandbox()
- {
- $this->revokeRole(Profile_role::SANDBOXED);
- }
- public function silence()
- {
- $this->grantRole(Profile_role::SILENCED);
- if (common_config('notice', 'hidespam')) {
- $this->flushVisibility();
- }
- }
- public function silenceAs(Profile $actor)
- {
- if (!$actor->hasRight(Right::SILENCEUSER)) {
- throw new AuthorizationException(_('You cannot silence users on this site.'));
- }
- // Only administrators can silence other privileged users (such as others who have the right to silence).
- if ($this->isPrivileged() && !$actor->hasRole(Profile_role::ADMINISTRATOR)) {
- throw new AuthorizationException(_('You cannot silence other privileged users.'));
- }
- if ($this->isSilenced()) {
- // TRANS: Client error displayed trying to silence an already silenced user.
- throw new AlreadyFulfilledException(_('User is already silenced.'));
- }
- return $this->silence();
- }
- public function unsilence()
- {
- $this->revokeRole(Profile_role::SILENCED);
- if (common_config('notice', 'hidespam')) {
- $this->flushVisibility();
- }
- }
- public function unsilenceAs(Profile $actor)
- {
- if (!$actor->hasRight(Right::SILENCEUSER)) {
- // TRANS: Client error displayed trying to unsilence a user when the user does not have the right.
- throw new AuthorizationException(_('You cannot unsilence users on this site.'));
- }
- if (!$this->isSilenced()) {
- // TRANS: Client error displayed trying to unsilence a user when the target user has not been silenced.
- throw new AlreadyFulfilledException(_('User is not silenced.'));
- }
- return $this->unsilence();
- }
- public function flushVisibility()
- {
- // Get all notices
- $stream = new ProfileNoticeStream($this, $this);
- $ids = $stream->getNoticeIds(0, CachingNoticeStream::CACHE_WINDOW);
- foreach ($ids as $id) {
- self::blow('notice:in-scope-for:%d:null', $id);
- }
- }
- public function isPrivileged()
- {
- // TODO: An Event::handle so plugins can report if users are privileged.
- // The ModHelper is the only one I care about when coding this, and that
- // can be tested with Right::SILENCEUSER which I do below:
- switch (true) {
- case $this->hasRight(Right::SILENCEUSER):
- case $this->hasRole(Profile_role::MODERATOR):
- case $this->hasRole(Profile_role::ADMINISTRATOR):
- case $this->hasRole(Profile_role::OWNER):
- return true;
- }
- return false;
- }
- /**
- * Does this user have the right to do X?
- *
- * With our role-based authorization, this is merely a lookup for whether the user
- * has a particular role. The implementation currently uses a switch statement
- * to determine if the user has the pre-defined role to exercise the right. Future
- * implementations may allow per-site roles, and different mappings of roles to rights.
- *
- * @param $right string Name of the right, usually a constant in class Right
- * @return boolean whether the user has the right in question
- */
- public function hasRight($right)
- {
- $result = false;
- if ($this->hasRole(Profile_role::DELETED)) {
- return false;
- }
- if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
- switch ($right) {
- case Right::DELETEOTHERSNOTICE:
- case Right::MAKEGROUPADMIN:
- case Right::SANDBOXUSER:
- case Right::SILENCEUSER:
- case Right::DELETEUSER:
- case Right::DELETEGROUP:
- case Right::TRAINSPAM:
- case Right::REVIEWSPAM:
- $result = $this->hasRole(Profile_role::MODERATOR);
- break;
- case Right::CONFIGURESITE:
- $result = $this->hasRole(Profile_role::ADMINISTRATOR);
- break;
- case Right::GRANTROLE:
- case Right::REVOKEROLE:
- $result = $this->hasRole(Profile_role::OWNER);
- break;
- case Right::NEWNOTICE:
- case Right::NEWMESSAGE:
- case Right::SUBSCRIBE:
- case Right::CREATEGROUP:
- $result = !$this->isSilenced();
- break;
- case Right::PUBLICNOTICE:
- case Right::EMAILONREPLY:
- case Right::EMAILONSUBSCRIBE:
- case Right::EMAILONFAVE:
- $result = !$this->isSandboxed() && !$this->isSilenced();
- break;
- case Right::WEBLOGIN:
- $result = !$this->isSilenced();
- break;
- case Right::API:
- $result = !$this->isSilenced();
- break;
- case Right::BACKUPACCOUNT:
- $result = common_config('profile', 'backup');
- break;
- case Right::RESTOREACCOUNT:
- $result = common_config('profile', 'restore');
- break;
- case Right::DELETEACCOUNT:
- $result = common_config('profile', 'delete');
- break;
- case Right::MOVEACCOUNT:
- $result = common_config('profile', 'move');
- break;
- default:
- $result = false;
- break;
- }
- }
- return $result;
- }
- // FIXME: Can't put Notice typing here due to ArrayWrapper
- public function hasRepeated($notice)
- {
- // XXX: not really a pkey, but should work
- $notice = Notice::pkeyGet(array('profile_id' => $this->getID(),
- 'repeat_of' => $notice->getID(),
- 'verb' => ActivityVerb::SHARE));
- return !empty($notice);
- }
- /**
- * Returns an XML string fragment with limited profile information
- * as an Atom <author> element.
- *
- * Assumes that Atom has been previously set up as the base namespace.
- *
- * @param Profile $cur the current authenticated user
- *
- * @return string
- */
- public function asAtomAuthor($cur = null)
- {
- $xs = new XMLStringer(true);
- $xs->elementStart('author');
- $xs->element('name', null, $this->nickname);
- $xs->element('uri', null, $this->getUri());
- if ($cur != null) {
- $attrs = [];
- $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
- $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
- $xs->element('statusnet:profile_info', $attrs, null);
- }
- $xs->elementEnd('author');
- return $xs->getString();
- }
- /**
- * Extra profile info for atom entries
- *
- * Clients use some extra profile info in the atom stream.
- * This gives it to them.
- *
- * @param Profile $scoped The currently logged in/scoped profile
- *
- * @return array representation of <statusnet:profile_info> element or null
- */
- public function profileInfo(Profile $scoped = null)
- {
- $profileInfoAttr = array('local_id' => $this->id);
- if ($scoped instanceof Profile) {
- // Whether the current user is a subscribed to this profile
- $profileInfoAttr['following'] = $scoped->isSubscribed($this) ? 'true' : 'false';
- // Whether the current user is has blocked this profile
- $profileInfoAttr['blocking'] = $scoped->hasBlocked($this) ? 'true' : 'false';
- }
- return array('statusnet:profile_info', $profileInfoAttr, null);
- }
- /**
- * Returns an XML string fragment with profile information as an
- * Activity Streams <activity:actor> element.
- *
- * Assumes that 'activity' namespace has been previously defined.
- *
- * @return string
- */
- public function asActivityActor()
- {
- return $this->asActivityNoun('actor');
- }
- /**
- * Returns an XML string fragment with profile information as an
- * Activity Streams noun object with the given element type.
- *
- * Assumes that 'activity', 'georss', and 'poco' namespace has been
- * previously defined.
- *
- * @param string $element one of 'actor', 'subject', 'object', 'target'
- *
- * @return string
- */
- public function asActivityNoun($element)
- {
- $noun = $this->asActivityObject();
- return $noun->asString('activity:' . $element);
- }
- public function asActivityObject()
- {
- $object = new ActivityObject();
- if (Event::handle('StartActivityObjectFromProfile', array($this, &$object))) {
- $object->type = $this->getObjectType();
- $object->id = $this->getUri();
- $object->title = $this->getBestName();
- $object->link = $this->getUrl();
- $object->summary = $this->getDescription();
- try {
- $avatar = Avatar::getUploaded($this);
- $object->avatarLinks[] = AvatarLink::fromAvatar($avatar);
- } catch (NoAvatarException $e) {
- // Could not find an original avatar to link
- }
- $sizes = array(
- AVATAR_PROFILE_SIZE,
- AVATAR_STREAM_SIZE,
- AVATAR_MINI_SIZE
- );
- foreach ($sizes as $size) {
- $alink = null;
- try {
- $avatar = Avatar::byProfile($this, $size);
- $alink = AvatarLink::fromAvatar($avatar);
- } catch (NoAvatarException $e) {
- $alink = new AvatarLink();
- $alink->type = 'image/png';
- $alink->height = $size;
- $alink->width = $size;
- $alink->url = Avatar::defaultImage($size);
- }
- $object->avatarLinks[] = $alink;
- }
- if (isset($this->lat) && isset($this->lon)) {
- $object->geopoint = (float)$this->lat
- . ' ' . (float)$this->lon;
- }
- $object->poco = PoCo::fromProfile($this);
- if ($this->isLocal()) {
- $object->extra[] = array('followers', array('url' => common_local_url('subscribers', array('nickname' => $this->getNickname()))));
- }
- Event::handle('EndActivityObjectFromProfile', array($this, &$object));
- }
- return $object;
- }
- /**
- * Returns the profile's canonical url, not necessarily a uri/unique id
- *
- * @return string $profileurl
- */
- public function getUrl()
- {
- $url = null;
- if ($this->isGroup()) {
- // FIXME: Get rid of this event, it fills no real purpose, data should be in Profile->profileurl (replaces User_group->mainpage)
- if (Event::handle('StartUserGroupHomeUrl', array($this->getGroup(), &$url))) {
- $url = $this->getGroup()->isLocal()
- ? common_local_url('showgroup', array('nickname' => $this->getNickname()))
- : $this->profileurl;
- }
- Event::handle('EndUserGroupHomeUrl', array($this->getGroup(), $url));
- } elseif ($this->isLocal()) {
- $url = common_local_url('showstream', array('nickname' => $this->getNickname()));
- } else {
- $url = $this->profileurl;
- }
- if (empty($url) ||
- !filter_var($url, FILTER_VALIDATE_URL)) {
- throw new InvalidUrlException($url);
- }
- return $url;
- }
- public function getHtmlTitle()
- {
- try {
- return $this->getAcctUri(false);
- } catch (ProfileNoAcctUriException $e) {
- return $this->getNickname();
- }
- }
- public function getNickname()
- {
- return $this->nickname;
- }
- public function getFullname()
- {
- return $this->fullname;
- }
- public function getHomepage()
- {
- return $this->homepage;
- }
- public function getDescription()
- {
- return $this->bio;
- }
- /**
- * Returns the best URI for a profile. Plugins may override.
- *
- * @return string $uri
- */
- public function getUri()
- {
- $uri = null;
- // give plugins a chance to set the URI
- if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
- // check for a local user first
- $user = User::getKV('id', $this->id);
- if ($user instanceof User) {
- $uri = $user->getUri();
- } else {
- $group = User_group::getKV('profile_id', $this->id);
- if ($group instanceof User_group) {
- $uri = $group->getUri();
- }
- }
- Event::handle('EndGetProfileUri', array($this, &$uri));
- }
- return $uri;
- }
- /**
- * Returns an assumed acct: URI for a profile. Plugins are required.
- *
- * @return string $uri
- */
- public function getAcctUri($scheme=true)
- {
- $acct = null;
- if (Event::handle('StartGetProfileAcctUri', array($this, &$acct))) {
- Event::handle('EndGetProfileAcctUri', array($this, &$acct));
- }
- if ($acct === null) {
- throw new ProfileNoAcctUriException($this);
- }
- if (parse_url($acct, PHP_URL_SCHEME) !== 'acct') {
- throw new ServerException('Acct URI does not have acct: scheme');
- }
- // if we don't return the scheme, just remove the 'acct:' in the beginning
- return $scheme ? $acct : mb_substr($acct, 5);
- }
- public function hasBlocked(Profile $other)
- {
- $block = Profile_block::exists($this, $other);
- return !empty($block);
- }
- public function getAtomFeed()
- {
- $feed = null;
- if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
- if ($this->isLocal()) {
- $feed = common_local_url('ApiTimelineUser', array('id' => $this->getID(),
- 'format' => 'atom'));
- }
- Event::handle('EndProfileGetAtomFeed', array($this, $feed));
- }
- return $feed;
- }
- public function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
- {
- // TRANS: Exception thrown when trying view "repeated to me".
- throw new Exception(_('Not implemented since inbox change.'));
- }
- /*
- * Get a Profile object by URI. Will call external plugins for help
- * using the event StartGetProfileFromURI.
- *
- * @param string $uri A unique identifier for a resource (profile/group/whatever)
- */
- public static function fromUri($uri)
- {
- $profile = null;
- if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
- // Get a local user when plugin lookup (like OStatus) fails
- $user = User::getKV('uri', $uri);
- if ($user instanceof User) {
- $profile = $user->getProfile();
- } else {
- $group = User_group::getKV('uri', $uri);
- if ($group instanceof User_group) {
- $profile = $group->getProfile();
- }
- }
- Event::handle('EndGetProfileFromURI', array($uri, $profile));
- }
- if (!$profile instanceof Profile) {
- throw new UnknownUriException($uri);
- }
- return $profile;
- }
- public function canRead(Notice $notice)
- {
- if ($notice->scope & Notice::SITE_SCOPE) {
- $user = $this->getUser();
- if (empty($user)) {
- return false;
- }
- }
- if ($notice->scope & Notice::ADDRESSEE_SCOPE) {
- $replies = $notice->getReplies();
- if (!in_array($this->id, $replies)) {
- $groups = $notice->getGroups();
- $foundOne = false;
- foreach ($groups as $group) {
- if ($this->isMember($group)) {
- $foundOne = true;
- break;
- }
- }
- if (!$foundOne) {
- return false;
- }
- }
- }
- if ($notice->scope & Notice::FOLLOWER_SCOPE) {
- $author = $notice->getProfile();
- if (!Subscription::exists($this, $author)) {
- return false;
- }
- }
- return true;
- }
- public static function current()
- {
- $user = common_current_user();
- if (empty($user)) {
- $profile = null;
- } else {
- $profile = $user->getProfile();
- }
- return $profile;
- }
- public static function ensureCurrent()
- {
- $profile = self::current();
- if (!$profile instanceof Profile) {
- throw new AuthorizationException('A currently scoped profile is required.');
- }
- return $profile;
- }
- /**
- * Magic function called at serialize() time.
- *
- * We use this to drop a couple process-specific references
- * from DB_DataObject which can cause trouble in future
- * processes.
- *
- * @return array of variable names to include in serialization.
- */
- public function __sleep()
- {
- $vars = parent::__sleep();
- $skip = array('_user', '_group');
- return array_diff($vars, $skip);
- }
- public function getProfile()
- {
- return $this;
- }
- /**
- * Test whether the given profile is the same as the current class,
- * for testing identities.
- *
- * @param Profile $other The other profile, usually from Action's $this->scoped
- *
- * @return boolean
- */
- public function sameAs(Profile $other=null)
- {
- if (is_null($other)) {
- // In case $this->scoped is null or something, i.e. not a current/legitimate profile.
- return false;
- }
- return $this->getID() === $other->getID();
- }
- /**
- * This will perform shortenLinks with the connected User object.
- *
- * Won't work on remote profiles or groups, so expect a
- * NoSuchUserException if you don't know it's a local User.
- *
- * @param string $text String to shorten
- * @param boolean $always Disrespect minimum length etc.
- *
- * @return string link-shortened $text
- */
- public function shortenLinks($text, $always=false)
- {
- return $this->getUser()->shortenLinks($text, $always);
- }
- public function isPrivateStream(): bool
- {
- // We only know of public remote users as of yet...
- if (!$this->isLocal()) {
- return false;
- }
- $private_stream = $this->getUser()->private_stream;
- return !is_null($private_stream) && $private_stream;
- }
- public function delPref($namespace, $topic)
- {
- return Profile_prefs::setData($this, $namespace, $topic, null);
- }
- public function getPref($namespace, $topic, $default = null)
- {
- // If you want an exception to be thrown, call Profile_prefs::getData directly
- try {
- return Profile_prefs::getData($this, $namespace, $topic, $default);
- } catch (NoResultException $e) {
- return null;
- }
- }
- // The same as getPref but will fall back to common_config value for the same namespace/topic
- public function getConfigPref($namespace, $topic)
- {
- return Profile_prefs::getConfigData($this, $namespace, $topic);
- }
- public function setPref($namespace, $topic, $data)
- {
- return Profile_prefs::setData($this, $namespace, $topic, $data);
- }
- public function getConnectedApps($offset=0, $limit=null)
- {
- return $this->getUser()->getConnectedApps($offset, $limit);
- }
- }
|