123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class Profile extends Managed_DataObject
- {
- public $__table = 'profile';
- public $id;
- public $nickname;
- public $fullname;
- public $profileurl;
- public $homepage;
- public $bio;
- public $location;
- public $lat;
- public $lon;
- public $location_id;
- public $location_ns;
- public $created;
- public $modified;
- 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'),
- )
- );
-
- if (common_config('search', 'type') == 'fulltext') {
- $def['fulltext indexes'] = array('nickname' => array('nickname', 'fullname', 'location', 'bio', 'homepage'));
- }
- return $def;
- }
- public static function getByEmail($email)
- {
-
- $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()
- {
-
- return !$this->isGroup();
- }
- public function isLocal()
- {
- try {
- $this->getUser();
- } catch (NoSuchUserException $e) {
- return false;
- }
- return true;
- }
-
-
-
- public function hasPassword()
- {
- try {
- return $this->getUser()->hasPassword();
- } catch (NoSuchUserException $e) {
- return false;
- }
- }
- public function getObjectType()
- {
-
- 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()) {
-
- 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();
-
- if (!Avatar::deleteFromProfile($this, true) || !$avatar->insert()) {
-
- @unlink(Avatar::path($filename));
- return null;
- }
- return $avatar;
- }
-
- function getBestName()
- {
- return ($this->fullname) ? $this->fullname : $this->nickname;
- }
-
- function getStreamName()
- {
- $user = common_current_user();
- if ($user instanceof User && $user->streamNicknames()) {
- return $this->nickname;
- }
- return $this->getBestName();
- }
-
- 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) {
-
- return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->getFullname(), $uri);
- } else {
- return $uri;
- }
- }
-
- function getCurrentNotice(Profile $scoped=null)
- {
- try {
- $notice = $this->getNotices(0, 1, 0, 0, $scoped);
- if ($notice->fetch()) {
- if ($notice instanceof ArrayWrapper) {
-
-
- return $notice->_items[0];
- }
- return $notice;
- }
- } catch (PrivateStreamException $e) {
-
- return null;
- }
-
- return null;
- }
- 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);
- }
- function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
- {
-
-
- $stream = new TaggedProfileNoticeStream($this, $tag, Profile::current());
- return $stream->getNotices($offset, $limit, $since_id, $max_id);
- }
- 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);
- }
- 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;
- }
- function isAdmin(User_group $group)
- {
- $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
- 'group_id' => $group->id));
- return (!empty($gm) && $gm->is_admin);
- }
- function isPendingMember($group)
- {
- $request = Group_join_queue::pkeyGet(array('profile_id' => $this->id,
- 'group_id' => $group->id));
- return !empty($request);
- }
- 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;
- }
- }
- function getGroupCount() {
- $groups = $this->getGroups(0, null);
- return $groups instanceof User_group
- ? $groups->N
- : 0;
- }
- function isTagged($peopletag)
- {
- $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
- 'tagged' => $this->id,
- 'tag' => $peopletag->tag));
- return !empty($tag);
- }
- 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;
- }
- } else if ($subs) {
- return (Subscription::exists($this, $tagged) ||
- Subscription::exists($tagged, $this));
- } else if ($remote) {
- return true;
- }
- return false;
- }
- 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);
- }
-
- function getOtherTags(Profile $scoped=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
- {
- $list = new Profile_list();
- $qry = sprintf('select profile_list.*, unix_timestamp(profile_tag.modified) as "cursor" ' .
- 'from profile_tag 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 = 0 ';
- }
- if ($since_id > 0) {
- $qry .= sprintf('AND (cursor > %d) ', $since_id);
- }
- if ($max_id > 0) {
- $qry .= sprintf('AND (cursor < %d) ', $max_id);
- }
- $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;
- }
- 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;
- }
- 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;
- }
- function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
- {
- $lists = new Profile_list();
- $subs = new Profile_tag_subscription();
- $lists->joinAdd(array('id', 'profile_tag_subscription:profile_tag_id'));
-
- $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
- $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
- if ($since_id>0) {
- $lists->whereAdd('cursor > '.$since_id);
- }
- if ($max_id>0) {
- $lists->whereAdd('cursor <= '.$max_id);
- }
- if($offset>=0 && !is_null($limit)) {
- $lists->limit($offset, $limit);
- }
- $lists->orderBy('"cursor" DESC');
- $lists->find();
- return $lists;
- }
-
- 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) {
-
- $join->notify();
- }
- return $join;
- }
-
- 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));
- }
- }
- function avatarUrl($size=AVATAR_PROFILE_SIZE)
- {
- return Avatar::urlByProfile($this, $size);
- }
- 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;
- }
- 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;
- }
- 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;
- }
- 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;
- }
-
- function getRequests($offset=0, $limit=null)
- {
-
- $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;
- }
- 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');
-
- if ($this->isLocal()) {
- $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
- }
- if (!empty($c)) {
- $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
- }
- return $cnt;
- }
- 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;
- }
-
- function isSubscribed(Profile $other)
- {
- return Subscription::exists($this, $other);
- }
- function readableBy(Profile $other=null)
- {
-
- if (!$this->isPrivateStream()) {
- return true;
- }
-
- return is_null($other) ? false : $other->isSubscribed($this);
- }
- function requiresSubscriptionApproval(Profile $other=null): bool
- {
- if (!$this->isLocal()) {
-
-
-
- return false;
- }
-
- if (!is_null($other) && $this->isSubscribed($other)) {
- return false;
- }
-
-
- return $this->isPrivateStream() || $this->getUser()->subscribe_policy === User::SUBSCRIBE_POLICY_MODERATE;
- }
-
- function hasPendingSubscription(Profile $other)
- {
- return Subscription_queue::exists($this, $other);
- }
-
- function mutuallySubscribed(Profile $other)
- {
- return $this->isSubscribed($other) &&
- $other->isSubscribed($this);
- }
- 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');
- if (!empty($c)) {
- $c->set(Cache::key('profile:notice_count:'.$this->getID()), $cnt);
- }
- return $cnt;
- }
- function blowSubscriberCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
- }
- }
- function blowSubscriptionCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $c->delete(Cache::key('profile:subscription_count:'.$this->id));
- }
- }
- function blowNoticeCount()
- {
- $c = Cache::instance();
- if (!empty($c)) {
- $c->delete(Cache::key('profile:notice_count:'.$this->id));
- }
- }
- static function maxBio()
- {
- $biolimit = common_config('profile', 'biolimit');
-
- if (is_null($biolimit)) {
- $biolimit = common_config('site', 'textlimit');
- }
- return $biolimit;
- }
- static function bioTooLong($bio)
- {
- $biolimit = self::maxBio();
- return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
- }
- 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;
-
- $local->updateWithKeys($origuser);
-
- if ($local->hasRole(Profile_role::OWNER)) {
- User::blow('user:site_owner');
- }
- } catch (NoSuchUserException $e) {
-
- }
- }
- return parent::update($dataObject);
- }
- public function getRelSelf()
- {
- return ['href' => $this->getUrl(),
- 'text' => common_config('site', 'name'),
- 'image' => Avatar::urlByProfile($this)];
- }
-
- public function getRelMes()
- {
- $relMes = array();
- try {
- $relMes[] = $this->getRelSelf();
- } catch (InvalidUrlException $e) {
-
- }
- if (common_valid_http_url($this->getHomepage())) {
- $relMes[] = ['href' => $this->getHomepage(),
- 'text' => _('Homepage'),
- 'image' => null];
- }
- Event::handle('OtherAccountProfiles', array($this, &$relMes));
- return $relMes;
- }
- function delete($useWhere=false)
- {
- $this->_deleteNotices();
- $this->_deleteSubscriptions();
- $this->_deleteTags();
- $this->_deleteBlocks();
- $this->_deleteAttentions();
- Avatar::deleteFromProfile($this, true);
-
-
- $related = array('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);
- }
- function _deleteNotices()
- {
- $notice = new Notice();
- $notice->profile_id = $this->id;
- if ($notice->find()) {
- while ($notice->fetch()) {
- $other = clone($notice);
- $other->delete();
- }
- }
- }
- 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) {
-
- common_log(LOG_INFO, 'Subscribed profile id=='.$sub->subscribed.' not found when deleting profile id=='.$this->getID().', ignoring...');
- } catch (ServerException $e) {
-
- 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) {
-
- common_log(LOG_INFO, 'Subscribed profile id=='.$sub->subscribed.' not found when deleting profile id=='.$this->getID().', ignoring...');
- } catch (ServerException $e) {
-
- common_log(LOG_INFO, 'Subscriber profile id=='.$other->getID().' could not be reached for unsubscription notice when deleting profile id=='.$this->getID().', ignoring...');
- }
- }
-
- $self = new Subscription();
- $self->subscriber = $this->getID();
- $self->subscribed = $this->getID();
- $self->delete();
- }
- function _deleteTags()
- {
- $tag = new Profile_tag();
- $tag->tagged = $this->id;
- $tag->delete();
- }
- function _deleteBlocks()
- {
- $block = new Profile_block();
- $block->blocked = $this->id;
- $block->delete();
- $block = new Group_block();
- $block->blocked = $this->id;
- $block->delete();
- }
- function _deleteAttentions()
- {
- $att = new Attention();
- $att->profile_id = $this->getID();
- if ($att->find()) {
- while ($att->fetch()) {
-
- $other = clone($att);
- $other->delete();
- }
- }
- }
-
- 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)) {
- if (!empty($this->lat) && !empty($this->lon)) {
- $location = Location::fromLatLon($this->lat, $this->lon);
- }
- }
- if (is_null($location)) {
- if (!empty($this->location)) {
- $location = Location::fromName($this->location);
- }
- }
- return $location;
- }
- public function shareLocation()
- {
- $cfg = common_config('location', 'share');
- if ($cfg == 'always') {
- return true;
- } else if ($cfg == 'never') {
- return false;
- } else {
- $share = common_config('location', 'sharedefault');
-
- $prefs = User_location_prefs::getKV('user_id', $this->id);
- if (!empty($prefs)) {
- $share = $prefs->share_location;
- $prefs->free();
- }
- return $share;
- }
- }
- 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;
- }
- 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;
- }
- function revokeRole($name)
- {
- if (Event::handle('StartRevokeRole', array($this, $name))) {
- $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
- 'role' => $name));
- if (empty($role)) {
-
-
- 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__);
-
-
- 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;
- }
- }
- function isSandboxed()
- {
- return $this->hasRole(Profile_role::SANDBOXED);
- }
- function isSilenced()
- {
- return $this->hasRole(Profile_role::SILENCED);
- }
- function sandbox()
- {
- $this->grantRole(Profile_role::SANDBOXED);
- }
- function unsandbox()
- {
- $this->revokeRole(Profile_role::SANDBOXED);
- }
- function silence()
- {
- $this->grantRole(Profile_role::SILENCED);
- if (common_config('notice', 'hidespam')) {
- $this->flushVisibility();
- }
- }
- function silenceAs(Profile $actor)
- {
- if (!$actor->hasRight(Right::SILENCEUSER)) {
- throw new AuthorizationException(_('You cannot silence users on this site.'));
- }
-
- if ($this->isPrivileged() && !$actor->hasRole(Profile_role::ADMINISTRATOR)) {
- throw new AuthorizationException(_('You cannot silence other privileged users.'));
- }
- if ($this->isSilenced()) {
-
- throw new AlreadyFulfilledException(_('User is already silenced.'));
- }
- return $this->silence();
- }
- function unsilence()
- {
- $this->revokeRole(Profile_role::SILENCED);
- if (common_config('notice', 'hidespam')) {
- $this->flushVisibility();
- }
- }
- function unsilenceAs(Profile $actor)
- {
- if (!$actor->hasRight(Right::SILENCEUSER)) {
-
- throw new AuthorizationException(_('You cannot unsilence users on this site.'));
- }
- if (!$this->isSilenced()) {
-
- throw new AlreadyFulfilledException(_('User is not silenced.'));
- }
- return $this->unsilence();
- }
- function flushVisibility()
- {
-
- $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()
- {
-
-
-
- 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;
- }
-
- 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;
- }
-
- public function hasRepeated($notice)
- {
-
- $notice = Notice::pkeyGet(array('profile_id' => $this->getID(),
- 'repeat_of' => $notice->getID(),
- 'verb' => ActivityVerb::SHARE));
- return !empty($notice);
- }
-
- 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 = Array();
- $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();
- }
-
- function profileInfo(Profile $scoped=null)
- {
- $profileInfoAttr = array('local_id' => $this->id);
- if ($scoped instanceof Profile) {
-
- $profileInfoAttr['following'] = $scoped->isSubscribed($this) ? 'true' : 'false';
-
- $profileInfoAttr['blocking'] = $scoped->hasBlocked($this) ? 'true' : 'false';
- }
- return array('statusnet:profile_info', $profileInfoAttr, null);
- }
-
- function asActivityActor()
- {
- return $this->asActivityNoun('actor');
- }
-
- 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) {
-
- }
- $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;
- }
-
- public function getUrl()
- {
- $url = null;
- if ($this->isGroup()) {
-
- 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;
- }
-
- public function getUri()
- {
- $uri = null;
-
- if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
-
- $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;
- }
-
- 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');
- }
-
- return $scheme ? $acct : mb_substr($acct, 5);
- }
- 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)
- {
-
- throw new Exception(_('Not implemented since inbox change.'));
- }
-
- static function fromUri($uri)
- {
- $profile = null;
- if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
-
- $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;
- }
- 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;
- }
- static function current()
- {
- $user = common_current_user();
- if (empty($user)) {
- $profile = null;
- } else {
- $profile = $user->getProfile();
- }
- return $profile;
- }
- static function ensureCurrent()
- {
- $profile = self::current();
- if (!$profile instanceof Profile) {
- throw new AuthorizationException('A currently scoped profile is required.');
- }
- return $profile;
- }
-
- function __sleep()
- {
- $vars = parent::__sleep();
- $skip = array('_user', '_group');
- return array_diff($vars, $skip);
- }
- public function getProfile()
- {
- return $this;
- }
-
- public function sameAs(Profile $other=null)
- {
- if (is_null($other)) {
-
- return false;
- }
- return $this->getID() === $other->getID();
- }
-
- public function shortenLinks($text, $always=false)
- {
- return $this->getUser()->shortenLinks($text, $always);
- }
- public function isPrivateStream(): bool
- {
-
- 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) {
-
- try {
- return Profile_prefs::getData($this, $namespace, $topic, $default);
- } catch (NoResultException $e) {
- return null;
- }
- }
-
- 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);
- }
- }
|