Profile.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008-2011, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  20. /**
  21. * Table Definition for profile
  22. */
  23. class Profile extends Managed_DataObject
  24. {
  25. ###START_AUTOCODE
  26. /* the code below is auto generated do not remove the above tag */
  27. public $__table = 'profile'; // table name
  28. public $id; // int(4) primary_key not_null
  29. public $nickname; // varchar(64) multiple_key not_null
  30. public $fullname; // varchar(191) multiple_key not 255 because utf8mb4 takes more space
  31. public $profileurl; // varchar(191) not 255 because utf8mb4 takes more space
  32. public $homepage; // varchar(191) multiple_key not 255 because utf8mb4 takes more space
  33. public $bio; // text() multiple_key
  34. public $location; // varchar(191) multiple_key not 255 because utf8mb4 takes more space
  35. public $lat; // decimal(10,7)
  36. public $lon; // decimal(10,7)
  37. public $location_id; // int(4)
  38. public $location_ns; // int(4)
  39. public $created; // datetime() not_null
  40. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  41. public static function schemaDef()
  42. {
  43. $def = array(
  44. 'description' => 'local and remote users have profiles',
  45. 'fields' => array(
  46. 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
  47. 'nickname' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'nickname or username', 'collate' => 'utf8_general_ci'),
  48. 'fullname' => array('type' => 'varchar', 'length' => 191, 'description' => 'display name', 'collate' => 'utf8_general_ci'),
  49. 'profileurl' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL, cached so we dont regenerate'),
  50. 'homepage' => array('type' => 'varchar', 'length' => 191, 'description' => 'identifying URL', 'collate' => 'utf8_general_ci'),
  51. 'bio' => array('type' => 'text', 'description' => 'descriptive biography', 'collate' => 'utf8_general_ci'),
  52. 'location' => array('type' => 'varchar', 'length' => 191, 'description' => 'physical location', 'collate' => 'utf8_general_ci'),
  53. 'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'),
  54. 'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'),
  55. 'location_id' => array('type' => 'int', 'description' => 'location id if possible'),
  56. 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'),
  57. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  58. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  59. ),
  60. 'primary key' => array('id'),
  61. 'indexes' => array(
  62. 'profile_nickname_idx' => array('nickname'),
  63. )
  64. );
  65. // Add a fulltext index
  66. if (common_config('search', 'type') == 'fulltext') {
  67. $def['fulltext indexes'] = array('nickname' => array('nickname', 'fullname', 'location', 'bio', 'homepage'));
  68. }
  69. return $def;
  70. }
  71. /* the code above is auto generated do not remove the tag below */
  72. ###END_AUTOCODE
  73. public static function getByEmail($email)
  74. {
  75. // in the future, profiles should have emails stored...
  76. $user = User::getKV('email', $email);
  77. if (!($user instanceof User)) {
  78. throw new NoSuchUserException(array('email'=>$email));
  79. }
  80. return $user->getProfile();
  81. }
  82. protected $_user = array();
  83. public function getUser()
  84. {
  85. if (!isset($this->_user[$this->id])) {
  86. $user = User::getKV('id', $this->id);
  87. if (!$user instanceof User) {
  88. throw new NoSuchUserException(array('id'=>$this->id));
  89. }
  90. $this->_user[$this->id] = $user;
  91. }
  92. return $this->_user[$this->id];
  93. }
  94. protected $_group = array();
  95. public function getGroup()
  96. {
  97. if (!isset($this->_group[$this->id])) {
  98. $group = User_group::getKV('profile_id', $this->id);
  99. if (!$group instanceof User_group) {
  100. throw new NoSuchGroupException(array('profile_id'=>$this->id));
  101. }
  102. $this->_group[$this->id] = $group;
  103. }
  104. return $this->_group[$this->id];
  105. }
  106. public function isGroup()
  107. {
  108. try {
  109. $this->getGroup();
  110. return true;
  111. } catch (NoSuchGroupException $e) {
  112. return false;
  113. }
  114. }
  115. public function isLocal()
  116. {
  117. try {
  118. $this->getUser();
  119. } catch (NoSuchUserException $e) {
  120. return false;
  121. }
  122. return true;
  123. }
  124. public function getObjectType()
  125. {
  126. // FIXME: More types... like peopletags and whatever
  127. if ($this->isGroup()) {
  128. return ActivityObject::GROUP;
  129. } else {
  130. return ActivityObject::PERSON;
  131. }
  132. }
  133. public function getAvatar($width, $height=null)
  134. {
  135. return Avatar::byProfile($this, $width, $height);
  136. }
  137. public function setOriginal($filename)
  138. {
  139. if ($this->isGroup()) {
  140. // Until Group avatars are handled just like profile avatars.
  141. return $this->getGroup()->setOriginal($filename);
  142. }
  143. $imagefile = new ImageFile(null, Avatar::path($filename));
  144. $avatar = new Avatar();
  145. $avatar->profile_id = $this->id;
  146. $avatar->width = $imagefile->width;
  147. $avatar->height = $imagefile->height;
  148. $avatar->mediatype = image_type_to_mime_type($imagefile->type);
  149. $avatar->filename = $filename;
  150. $avatar->original = true;
  151. $avatar->url = Avatar::url($filename);
  152. $avatar->created = common_sql_now();
  153. // XXX: start a transaction here
  154. if (!Avatar::deleteFromProfile($this, true) || !$avatar->insert()) {
  155. // If we can't delete the old avatars, let's abort right here.
  156. @unlink(Avatar::path($filename));
  157. return null;
  158. }
  159. return $avatar;
  160. }
  161. /**
  162. * Gets either the full name (if filled) or the nickname.
  163. *
  164. * @return string
  165. */
  166. function getBestName()
  167. {
  168. return ($this->fullname) ? $this->fullname : $this->nickname;
  169. }
  170. /**
  171. * Takes the currently scoped profile into account to give a name
  172. * to list in notice streams. Preferences may differ between profiles.
  173. */
  174. function getStreamName()
  175. {
  176. $user = common_current_user();
  177. if ($user instanceof User && $user->streamNicknames()) {
  178. return $this->nickname;
  179. }
  180. return $this->getBestName();
  181. }
  182. /**
  183. * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
  184. * if no fullname is provided.
  185. *
  186. * @return string
  187. */
  188. function getFancyName()
  189. {
  190. if ($this->fullname) {
  191. // TRANS: Full name of a profile or group (%1$s) followed by nickname (%2$s) in parentheses.
  192. return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
  193. } else {
  194. return $this->nickname;
  195. }
  196. }
  197. /**
  198. * Get the most recent notice posted by this user, if any.
  199. *
  200. * @return mixed Notice or null
  201. */
  202. function getCurrentNotice()
  203. {
  204. $notice = $this->getNotices(0, 1);
  205. if ($notice->fetch()) {
  206. if ($notice instanceof ArrayWrapper) {
  207. // hack for things trying to work with single notices
  208. return $notice->_items[0];
  209. }
  210. return $notice;
  211. }
  212. return null;
  213. }
  214. function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
  215. {
  216. $stream = new TaggedProfileNoticeStream($this, $tag);
  217. return $stream->getNotices($offset, $limit, $since_id, $max_id);
  218. }
  219. function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, Profile $scoped=null)
  220. {
  221. $stream = new ProfileNoticeStream($this, $scoped);
  222. return $stream->getNotices($offset, $limit, $since_id, $max_id);
  223. }
  224. function isMember(User_group $group)
  225. {
  226. $groups = $this->getGroups(0, null);
  227. while ($groups instanceof User_group && $groups->fetch()) {
  228. if ($groups->id == $group->id) {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. function isAdmin(User_group $group)
  235. {
  236. $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
  237. 'group_id' => $group->id));
  238. return (!empty($gm) && $gm->is_admin);
  239. }
  240. function isPendingMember($group)
  241. {
  242. $request = Group_join_queue::pkeyGet(array('profile_id' => $this->id,
  243. 'group_id' => $group->id));
  244. return !empty($request);
  245. }
  246. function getGroups($offset=0, $limit=PROFILES_PER_PAGE)
  247. {
  248. $ids = array();
  249. $keypart = sprintf('profile:groups:%d', $this->id);
  250. $idstring = self::cacheGet($keypart);
  251. if ($idstring !== false) {
  252. $ids = explode(',', $idstring);
  253. } else {
  254. $gm = new Group_member();
  255. $gm->profile_id = $this->id;
  256. if ($gm->find()) {
  257. while ($gm->fetch()) {
  258. $ids[] = $gm->group_id;
  259. }
  260. }
  261. self::cacheSet($keypart, implode(',', $ids));
  262. }
  263. if (!is_null($offset) && !is_null($limit)) {
  264. $ids = array_slice($ids, $offset, $limit);
  265. }
  266. try {
  267. return User_group::multiGet('id', $ids);
  268. } catch (NoResultException $e) {
  269. return null; // throw exception when we handle it everywhere
  270. }
  271. }
  272. function getGroupCount() {
  273. $groups = $this->getGroups(0, null);
  274. return $groups instanceof User_group
  275. ? $groups->N
  276. : 0;
  277. }
  278. function isTagged($peopletag)
  279. {
  280. $tag = Profile_tag::pkeyGet(array('tagger' => $peopletag->tagger,
  281. 'tagged' => $this->id,
  282. 'tag' => $peopletag->tag));
  283. return !empty($tag);
  284. }
  285. function canTag($tagged)
  286. {
  287. if (empty($tagged)) {
  288. return false;
  289. }
  290. if ($tagged->id == $this->id) {
  291. return true;
  292. }
  293. $all = common_config('peopletag', 'allow_tagging', 'all');
  294. $local = common_config('peopletag', 'allow_tagging', 'local');
  295. $remote = common_config('peopletag', 'allow_tagging', 'remote');
  296. $subs = common_config('peopletag', 'allow_tagging', 'subs');
  297. if ($all) {
  298. return true;
  299. }
  300. $tagged_user = $tagged->getUser();
  301. if (!empty($tagged_user)) {
  302. if ($local) {
  303. return true;
  304. }
  305. } else if ($subs) {
  306. return (Subscription::exists($this, $tagged) ||
  307. Subscription::exists($tagged, $this));
  308. } else if ($remote) {
  309. return true;
  310. }
  311. return false;
  312. }
  313. function getLists($auth_user, $offset=0, $limit=null, $since_id=0, $max_id=0)
  314. {
  315. $ids = array();
  316. $keypart = sprintf('profile:lists:%d', $this->id);
  317. $idstr = self::cacheGet($keypart);
  318. if ($idstr !== false) {
  319. $ids = explode(',', $idstr);
  320. } else {
  321. $list = new Profile_list();
  322. $list->selectAdd();
  323. $list->selectAdd('id');
  324. $list->tagger = $this->id;
  325. $list->selectAdd('id as "cursor"');
  326. if ($since_id>0) {
  327. $list->whereAdd('id > '.$since_id);
  328. }
  329. if ($max_id>0) {
  330. $list->whereAdd('id <= '.$max_id);
  331. }
  332. if($offset>=0 && !is_null($limit)) {
  333. $list->limit($offset, $limit);
  334. }
  335. $list->orderBy('id DESC');
  336. if ($list->find()) {
  337. while ($list->fetch()) {
  338. $ids[] = $list->id;
  339. }
  340. }
  341. self::cacheSet($keypart, implode(',', $ids));
  342. }
  343. $showPrivate = (($auth_user instanceof User ||
  344. $auth_user instanceof Profile) &&
  345. $auth_user->id === $this->id);
  346. $lists = array();
  347. foreach ($ids as $id) {
  348. $list = Profile_list::getKV('id', $id);
  349. if (!empty($list) &&
  350. ($showPrivate || !$list->private)) {
  351. if (!isset($list->cursor)) {
  352. $list->cursor = $list->id;
  353. }
  354. $lists[] = $list;
  355. }
  356. }
  357. return new ArrayWrapper($lists);
  358. }
  359. /**
  360. * Get tags that other people put on this profile, in reverse-chron order
  361. *
  362. * @param (Profile|User) $auth_user Authorized user (used for privacy)
  363. * @param int $offset Offset from latest
  364. * @param int $limit Max number to get
  365. * @param datetime $since_id max date
  366. * @param datetime $max_id min date
  367. *
  368. * @return Profile_list resulting lists
  369. */
  370. function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
  371. {
  372. $list = new Profile_list();
  373. $qry = sprintf('select profile_list.*, unix_timestamp(profile_tag.modified) as "cursor" ' .
  374. 'from profile_tag join profile_list '.
  375. 'on (profile_tag.tagger = profile_list.tagger ' .
  376. ' and profile_tag.tag = profile_list.tag) ' .
  377. 'where profile_tag.tagged = %d ',
  378. $this->id);
  379. if ($auth_user instanceof User || $auth_user instanceof Profile) {
  380. $qry .= sprintf('AND ( ( profile_list.private = false ) ' .
  381. 'OR ( profile_list.tagger = %d AND ' .
  382. 'profile_list.private = true ) )',
  383. $auth_user->id);
  384. } else {
  385. $qry .= 'AND profile_list.private = 0 ';
  386. }
  387. if ($since_id > 0) {
  388. $qry .= sprintf('AND (cursor > %d) ', $since_id);
  389. }
  390. if ($max_id > 0) {
  391. $qry .= sprintf('AND (cursor < %d) ', $max_id);
  392. }
  393. $qry .= 'ORDER BY profile_tag.modified DESC ';
  394. if ($offset >= 0 && !is_null($limit)) {
  395. $qry .= sprintf('LIMIT %d OFFSET %d ', $limit, $offset);
  396. }
  397. $list->query($qry);
  398. return $list;
  399. }
  400. function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
  401. {
  402. $tags = new Profile_list();
  403. $tags->private = true;
  404. $tags->tagger = $this->id;
  405. if ($since_id>0) {
  406. $tags->whereAdd('id > '.$since_id);
  407. }
  408. if ($max_id>0) {
  409. $tags->whereAdd('id <= '.$max_id);
  410. }
  411. if($offset>=0 && !is_null($limit)) {
  412. $tags->limit($offset, $limit);
  413. }
  414. $tags->orderBy('id DESC');
  415. $tags->find();
  416. return $tags;
  417. }
  418. function hasLocalTags()
  419. {
  420. $tags = new Profile_tag();
  421. $tags->joinAdd(array('tagger', 'user:id'));
  422. $tags->whereAdd('tagged = '.$this->id);
  423. $tags->whereAdd('tagger != '.$this->id);
  424. $tags->limit(0, 1);
  425. $tags->fetch();
  426. return ($tags->N == 0) ? false : true;
  427. }
  428. function getTagSubscriptions($offset=0, $limit=null, $since_id=0, $max_id=0)
  429. {
  430. $lists = new Profile_list();
  431. $subs = new Profile_tag_subscription();
  432. $lists->joinAdd(array('id', 'profile_tag_subscription:profile_tag_id'));
  433. #@fixme: postgres (round(date_part('epoch', my_date)))
  434. $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
  435. $lists->whereAdd('profile_tag_subscription.profile_id = '.$this->id);
  436. if ($since_id>0) {
  437. $lists->whereAdd('cursor > '.$since_id);
  438. }
  439. if ($max_id>0) {
  440. $lists->whereAdd('cursor <= '.$max_id);
  441. }
  442. if($offset>=0 && !is_null($limit)) {
  443. $lists->limit($offset, $limit);
  444. }
  445. $lists->orderBy('"cursor" DESC');
  446. $lists->find();
  447. return $lists;
  448. }
  449. /**
  450. * Request to join the given group.
  451. * May throw exceptions on failure.
  452. *
  453. * @param User_group $group
  454. * @return mixed: Group_member on success, Group_join_queue if pending approval, null on some cancels?
  455. */
  456. function joinGroup(User_group $group)
  457. {
  458. $join = null;
  459. if ($group->join_policy == User_group::JOIN_POLICY_MODERATE) {
  460. $join = Group_join_queue::saveNew($this, $group);
  461. } else {
  462. if (Event::handle('StartJoinGroup', array($group, $this))) {
  463. $join = Group_member::join($group->id, $this->id);
  464. self::blow('profile:groups:%d', $this->id);
  465. self::blow('group:member_ids:%d', $group->id);
  466. self::blow('group:member_count:%d', $group->id);
  467. Event::handle('EndJoinGroup', array($group, $this));
  468. }
  469. }
  470. if ($join) {
  471. // Send any applicable notifications...
  472. $join->notify();
  473. }
  474. return $join;
  475. }
  476. /**
  477. * Leave a group that this profile is a member of.
  478. *
  479. * @param User_group $group
  480. */
  481. function leaveGroup(User_group $group)
  482. {
  483. if (Event::handle('StartLeaveGroup', array($group, $this))) {
  484. Group_member::leave($group->id, $this->id);
  485. self::blow('profile:groups:%d', $this->id);
  486. self::blow('group:member_ids:%d', $group->id);
  487. self::blow('group:member_count:%d', $group->id);
  488. Event::handle('EndLeaveGroup', array($group, $this));
  489. }
  490. }
  491. function avatarUrl($size=AVATAR_PROFILE_SIZE)
  492. {
  493. return Avatar::urlByProfile($this, $size);
  494. }
  495. function getSubscribed($offset=0, $limit=null)
  496. {
  497. $subs = Subscription::getSubscribedIDs($this->id, $offset, $limit);
  498. try {
  499. $profiles = Profile::multiGet('id', $subs);
  500. } catch (NoResultException $e) {
  501. return $e->obj;
  502. }
  503. return $profiles;
  504. }
  505. function getSubscribers($offset=0, $limit=null)
  506. {
  507. $subs = Subscription::getSubscriberIDs($this->id, $offset, $limit);
  508. try {
  509. $profiles = Profile::multiGet('id', $subs);
  510. } catch (NoResultException $e) {
  511. return $e->obj;
  512. }
  513. return $profiles;
  514. }
  515. function getTaggedSubscribers($tag, $offset=0, $limit=null)
  516. {
  517. $qry =
  518. 'SELECT profile.* ' .
  519. 'FROM profile JOIN subscription ' .
  520. 'ON profile.id = subscription.subscriber ' .
  521. 'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
  522. 'AND profile_tag.tagger = subscription.subscribed) ' .
  523. 'WHERE subscription.subscribed = %d ' .
  524. "AND profile_tag.tag = '%s' " .
  525. 'AND subscription.subscribed != subscription.subscriber ' .
  526. 'ORDER BY subscription.created DESC ';
  527. if ($offset) {
  528. $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
  529. }
  530. $profile = new Profile();
  531. $cnt = $profile->query(sprintf($qry, $this->id, $profile->escape($tag)));
  532. return $profile;
  533. }
  534. function getTaggedSubscriptions($tag, $offset=0, $limit=null)
  535. {
  536. $qry =
  537. 'SELECT profile.* ' .
  538. 'FROM profile JOIN subscription ' .
  539. 'ON profile.id = subscription.subscribed ' .
  540. 'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
  541. 'AND profile_tag.tagger = subscription.subscriber) ' .
  542. 'WHERE subscription.subscriber = %d ' .
  543. "AND profile_tag.tag = '%s' " .
  544. 'AND subscription.subscribed != subscription.subscriber ' .
  545. 'ORDER BY subscription.created DESC ';
  546. $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
  547. $profile = new Profile();
  548. $profile->query(sprintf($qry, $this->id, $profile->escape($tag)));
  549. return $profile;
  550. }
  551. /**
  552. * Get pending subscribers, who have not yet been approved.
  553. *
  554. * @param int $offset
  555. * @param int $limit
  556. * @return Profile
  557. */
  558. function getRequests($offset=0, $limit=null)
  559. {
  560. $qry =
  561. 'SELECT profile.* ' .
  562. 'FROM profile JOIN subscription_queue '.
  563. 'ON profile.id = subscription_queue.subscriber ' .
  564. 'WHERE subscription_queue.subscribed = %d ' .
  565. 'ORDER BY subscription_queue.created DESC ';
  566. if ($limit != null) {
  567. if (common_config('db','type') == 'pgsql') {
  568. $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
  569. } else {
  570. $qry .= ' LIMIT ' . $offset . ', ' . $limit;
  571. }
  572. }
  573. $members = new Profile();
  574. $members->query(sprintf($qry, $this->id));
  575. return $members;
  576. }
  577. function subscriptionCount()
  578. {
  579. $c = Cache::instance();
  580. if (!empty($c)) {
  581. $cnt = $c->get(Cache::key('profile:subscription_count:'.$this->id));
  582. if (is_integer($cnt)) {
  583. return (int) $cnt;
  584. }
  585. }
  586. $sub = new Subscription();
  587. $sub->subscriber = $this->id;
  588. $cnt = (int) $sub->count('distinct subscribed');
  589. $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
  590. if (!empty($c)) {
  591. $c->set(Cache::key('profile:subscription_count:'.$this->id), $cnt);
  592. }
  593. return $cnt;
  594. }
  595. function subscriberCount()
  596. {
  597. $c = Cache::instance();
  598. if (!empty($c)) {
  599. $cnt = $c->get(Cache::key('profile:subscriber_count:'.$this->id));
  600. if (is_integer($cnt)) {
  601. return (int) $cnt;
  602. }
  603. }
  604. $sub = new Subscription();
  605. $sub->subscribed = $this->id;
  606. $sub->whereAdd('subscriber != subscribed');
  607. $cnt = (int) $sub->count('distinct subscriber');
  608. if (!empty($c)) {
  609. $c->set(Cache::key('profile:subscriber_count:'.$this->id), $cnt);
  610. }
  611. return $cnt;
  612. }
  613. /**
  614. * Is this profile subscribed to another profile?
  615. *
  616. * @param Profile $other
  617. * @return boolean
  618. */
  619. function isSubscribed(Profile $other)
  620. {
  621. return Subscription::exists($this, $other);
  622. }
  623. /**
  624. * Check if a pending subscription request is outstanding for this...
  625. *
  626. * @param Profile $other
  627. * @return boolean
  628. */
  629. function hasPendingSubscription(Profile $other)
  630. {
  631. return Subscription_queue::exists($this, $other);
  632. }
  633. /**
  634. * Are these two profiles subscribed to each other?
  635. *
  636. * @param Profile $other
  637. * @return boolean
  638. */
  639. function mutuallySubscribed(Profile $other)
  640. {
  641. return $this->isSubscribed($other) &&
  642. $other->isSubscribed($this);
  643. }
  644. function noticeCount()
  645. {
  646. $c = Cache::instance();
  647. if (!empty($c)) {
  648. $cnt = $c->get(Cache::key('profile:notice_count:'.$this->id));
  649. if (is_integer($cnt)) {
  650. return (int) $cnt;
  651. }
  652. }
  653. $notices = new Notice();
  654. $notices->profile_id = $this->id;
  655. $cnt = (int) $notices->count('distinct id');
  656. if (!empty($c)) {
  657. $c->set(Cache::key('profile:notice_count:'.$this->id), $cnt);
  658. }
  659. return $cnt;
  660. }
  661. function blowSubscriberCount()
  662. {
  663. $c = Cache::instance();
  664. if (!empty($c)) {
  665. $c->delete(Cache::key('profile:subscriber_count:'.$this->id));
  666. }
  667. }
  668. function blowSubscriptionCount()
  669. {
  670. $c = Cache::instance();
  671. if (!empty($c)) {
  672. $c->delete(Cache::key('profile:subscription_count:'.$this->id));
  673. }
  674. }
  675. function blowNoticeCount()
  676. {
  677. $c = Cache::instance();
  678. if (!empty($c)) {
  679. $c->delete(Cache::key('profile:notice_count:'.$this->id));
  680. }
  681. }
  682. static function maxBio()
  683. {
  684. $biolimit = common_config('profile', 'biolimit');
  685. // null => use global limit (distinct from 0!)
  686. if (is_null($biolimit)) {
  687. $biolimit = common_config('site', 'textlimit');
  688. }
  689. return $biolimit;
  690. }
  691. static function bioTooLong($bio)
  692. {
  693. $biolimit = self::maxBio();
  694. return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
  695. }
  696. function update($dataObject=false)
  697. {
  698. if (is_object($dataObject) && $this->nickname != $dataObject->nickname) {
  699. try {
  700. $local = $this->getUser();
  701. common_debug("Updating User ({$this->id}) nickname from {$dataObject->nickname} to {$this->nickname}");
  702. $origuser = clone($local);
  703. $local->nickname = $this->nickname;
  704. // updateWithKeys throws exception on failure.
  705. $local->updateWithKeys($origuser);
  706. // Clear the site owner, in case nickname changed
  707. if ($local->hasRole(Profile_role::OWNER)) {
  708. User::blow('user:site_owner');
  709. }
  710. } catch (NoSuchUserException $e) {
  711. // Nevermind...
  712. }
  713. }
  714. return parent::update($dataObject);
  715. }
  716. function delete($useWhere=false)
  717. {
  718. $this->_deleteNotices();
  719. $this->_deleteSubscriptions();
  720. $this->_deleteTags();
  721. $this->_deleteBlocks();
  722. $this->_deleteAttentions();
  723. Avatar::deleteFromProfile($this, true);
  724. // Warning: delete() will run on the batch objects,
  725. // not on individual objects.
  726. $related = array('Reply',
  727. 'Group_member',
  728. );
  729. Event::handle('ProfileDeleteRelated', array($this, &$related));
  730. foreach ($related as $cls) {
  731. $inst = new $cls();
  732. $inst->profile_id = $this->id;
  733. $inst->delete();
  734. }
  735. return parent::delete($useWhere);
  736. }
  737. function _deleteNotices()
  738. {
  739. $notice = new Notice();
  740. $notice->profile_id = $this->id;
  741. if ($notice->find()) {
  742. while ($notice->fetch()) {
  743. $other = clone($notice);
  744. $other->delete();
  745. }
  746. }
  747. }
  748. function _deleteSubscriptions()
  749. {
  750. $sub = new Subscription();
  751. $sub->subscriber = $this->id;
  752. $sub->find();
  753. while ($sub->fetch()) {
  754. $other = Profile::getKV('id', $sub->subscribed);
  755. if (empty($other)) {
  756. continue;
  757. }
  758. if ($other->id == $this->id) {
  759. continue;
  760. }
  761. Subscription::cancel($this, $other);
  762. }
  763. $subd = new Subscription();
  764. $subd->subscribed = $this->id;
  765. $subd->find();
  766. while ($subd->fetch()) {
  767. $other = Profile::getKV('id', $subd->subscriber);
  768. if (empty($other)) {
  769. continue;
  770. }
  771. if ($other->id == $this->id) {
  772. continue;
  773. }
  774. Subscription::cancel($other, $this);
  775. }
  776. $self = new Subscription();
  777. $self->subscriber = $this->id;
  778. $self->subscribed = $this->id;
  779. $self->delete();
  780. }
  781. function _deleteTags()
  782. {
  783. $tag = new Profile_tag();
  784. $tag->tagged = $this->id;
  785. $tag->delete();
  786. }
  787. function _deleteBlocks()
  788. {
  789. $block = new Profile_block();
  790. $block->blocked = $this->id;
  791. $block->delete();
  792. $block = new Group_block();
  793. $block->blocked = $this->id;
  794. $block->delete();
  795. }
  796. function _deleteAttentions()
  797. {
  798. $att = new Attention();
  799. $att->profile_id = $this->getID();
  800. if ($att->find()) {
  801. while ($att->fetch()) {
  802. // Can't do delete() on the object directly since it won't remove all of it
  803. $other = clone($att);
  804. $other->delete();
  805. }
  806. }
  807. }
  808. // XXX: identical to Notice::getLocation.
  809. public function getLocation()
  810. {
  811. $location = null;
  812. if (!empty($this->location_id) && !empty($this->location_ns)) {
  813. $location = Location::fromId($this->location_id, $this->location_ns);
  814. }
  815. if (is_null($location)) { // no ID, or Location::fromId() failed
  816. if (!empty($this->lat) && !empty($this->lon)) {
  817. $location = Location::fromLatLon($this->lat, $this->lon);
  818. }
  819. }
  820. if (is_null($location)) { // still haven't found it!
  821. if (!empty($this->location)) {
  822. $location = Location::fromName($this->location);
  823. }
  824. }
  825. return $location;
  826. }
  827. public function shareLocation()
  828. {
  829. $cfg = common_config('location', 'share');
  830. if ($cfg == 'always') {
  831. return true;
  832. } else if ($cfg == 'never') {
  833. return false;
  834. } else { // user
  835. $share = common_config('location', 'sharedefault');
  836. // Check if user has a personal setting for this
  837. $prefs = User_location_prefs::getKV('user_id', $this->id);
  838. if (!empty($prefs)) {
  839. $share = $prefs->share_location;
  840. $prefs->free();
  841. }
  842. return $share;
  843. }
  844. }
  845. function hasRole($name)
  846. {
  847. $has_role = false;
  848. if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
  849. $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
  850. 'role' => $name));
  851. $has_role = !empty($role);
  852. Event::handle('EndHasRole', array($this, $name, $has_role));
  853. }
  854. return $has_role;
  855. }
  856. function grantRole($name)
  857. {
  858. if (Event::handle('StartGrantRole', array($this, $name))) {
  859. $role = new Profile_role();
  860. $role->profile_id = $this->id;
  861. $role->role = $name;
  862. $role->created = common_sql_now();
  863. $result = $role->insert();
  864. if (!$result) {
  865. throw new Exception("Can't save role '$name' for profile '{$this->id}'");
  866. }
  867. if ($name == 'owner') {
  868. User::blow('user:site_owner');
  869. }
  870. Event::handle('EndGrantRole', array($this, $name));
  871. }
  872. return $result;
  873. }
  874. function revokeRole($name)
  875. {
  876. if (Event::handle('StartRevokeRole', array($this, $name))) {
  877. $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
  878. 'role' => $name));
  879. if (empty($role)) {
  880. // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
  881. // TRANS: %1$s is the role name, %2$s is the user ID (number).
  882. throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
  883. }
  884. $result = $role->delete();
  885. if (!$result) {
  886. common_log_db_error($role, 'DELETE', __FILE__);
  887. // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
  888. // TRANS: %1$s is the role name, %2$s is the user ID (number).
  889. throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; database error.'),$name, $this->id));
  890. }
  891. if ($name == 'owner') {
  892. User::blow('user:site_owner');
  893. }
  894. Event::handle('EndRevokeRole', array($this, $name));
  895. return true;
  896. }
  897. }
  898. function isSandboxed()
  899. {
  900. return $this->hasRole(Profile_role::SANDBOXED);
  901. }
  902. function isSilenced()
  903. {
  904. return $this->hasRole(Profile_role::SILENCED);
  905. }
  906. function sandbox()
  907. {
  908. $this->grantRole(Profile_role::SANDBOXED);
  909. }
  910. function unsandbox()
  911. {
  912. $this->revokeRole(Profile_role::SANDBOXED);
  913. }
  914. function silence()
  915. {
  916. $this->grantRole(Profile_role::SILENCED);
  917. if (common_config('notice', 'hidespam')) {
  918. $this->flushVisibility();
  919. }
  920. }
  921. function unsilence()
  922. {
  923. $this->revokeRole(Profile_role::SILENCED);
  924. if (common_config('notice', 'hidespam')) {
  925. $this->flushVisibility();
  926. }
  927. }
  928. function flushVisibility()
  929. {
  930. // Get all notices
  931. $stream = new ProfileNoticeStream($this, $this);
  932. $ids = $stream->getNoticeIds(0, CachingNoticeStream::CACHE_WINDOW);
  933. foreach ($ids as $id) {
  934. self::blow('notice:in-scope-for:%d:null', $id);
  935. }
  936. }
  937. /**
  938. * Does this user have the right to do X?
  939. *
  940. * With our role-based authorization, this is merely a lookup for whether the user
  941. * has a particular role. The implementation currently uses a switch statement
  942. * to determine if the user has the pre-defined role to exercise the right. Future
  943. * implementations may allow per-site roles, and different mappings of roles to rights.
  944. *
  945. * @param $right string Name of the right, usually a constant in class Right
  946. * @return boolean whether the user has the right in question
  947. */
  948. public function hasRight($right)
  949. {
  950. $result = false;
  951. if ($this->hasRole(Profile_role::DELETED)) {
  952. return false;
  953. }
  954. if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
  955. switch ($right)
  956. {
  957. case Right::DELETEOTHERSNOTICE:
  958. case Right::MAKEGROUPADMIN:
  959. case Right::SANDBOXUSER:
  960. case Right::SILENCEUSER:
  961. case Right::DELETEUSER:
  962. case Right::DELETEGROUP:
  963. case Right::TRAINSPAM:
  964. case Right::REVIEWSPAM:
  965. $result = $this->hasRole(Profile_role::MODERATOR);
  966. break;
  967. case Right::CONFIGURESITE:
  968. $result = $this->hasRole(Profile_role::ADMINISTRATOR);
  969. break;
  970. case Right::GRANTROLE:
  971. case Right::REVOKEROLE:
  972. $result = $this->hasRole(Profile_role::OWNER);
  973. break;
  974. case Right::NEWNOTICE:
  975. case Right::NEWMESSAGE:
  976. case Right::SUBSCRIBE:
  977. case Right::CREATEGROUP:
  978. $result = !$this->isSilenced();
  979. break;
  980. case Right::PUBLICNOTICE:
  981. case Right::EMAILONREPLY:
  982. case Right::EMAILONSUBSCRIBE:
  983. case Right::EMAILONFAVE:
  984. $result = !$this->isSandboxed();
  985. break;
  986. case Right::WEBLOGIN:
  987. $result = !$this->isSilenced();
  988. break;
  989. case Right::API:
  990. $result = !$this->isSilenced();
  991. break;
  992. case Right::BACKUPACCOUNT:
  993. $result = common_config('profile', 'backup');
  994. break;
  995. case Right::RESTOREACCOUNT:
  996. $result = common_config('profile', 'restore');
  997. break;
  998. case Right::DELETEACCOUNT:
  999. $result = common_config('profile', 'delete');
  1000. break;
  1001. case Right::MOVEACCOUNT:
  1002. $result = common_config('profile', 'move');
  1003. break;
  1004. default:
  1005. $result = false;
  1006. break;
  1007. }
  1008. }
  1009. return $result;
  1010. }
  1011. // FIXME: Can't put Notice typing here due to ArrayWrapper
  1012. public function hasRepeated($notice)
  1013. {
  1014. // XXX: not really a pkey, but should work
  1015. $notice = Notice::pkeyGet(array('profile_id' => $this->id,
  1016. 'repeat_of' => $notice->id));
  1017. return !empty($notice);
  1018. }
  1019. /**
  1020. * Returns an XML string fragment with limited profile information
  1021. * as an Atom <author> element.
  1022. *
  1023. * Assumes that Atom has been previously set up as the base namespace.
  1024. *
  1025. * @param Profile $cur the current authenticated user
  1026. *
  1027. * @return string
  1028. */
  1029. function asAtomAuthor($cur = null)
  1030. {
  1031. $xs = new XMLStringer(true);
  1032. $xs->elementStart('author');
  1033. $xs->element('name', null, $this->nickname);
  1034. $xs->element('uri', null, $this->getUri());
  1035. if ($cur != null) {
  1036. $attrs = Array();
  1037. $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
  1038. $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
  1039. $xs->element('statusnet:profile_info', $attrs, null);
  1040. }
  1041. $xs->elementEnd('author');
  1042. return $xs->getString();
  1043. }
  1044. /**
  1045. * Extra profile info for atom entries
  1046. *
  1047. * Clients use some extra profile info in the atom stream.
  1048. * This gives it to them.
  1049. *
  1050. * @param Profile $scoped The currently logged in/scoped profile
  1051. *
  1052. * @return array representation of <statusnet:profile_info> element or null
  1053. */
  1054. function profileInfo(Profile $scoped=null)
  1055. {
  1056. $profileInfoAttr = array('local_id' => $this->id);
  1057. if ($scoped instanceof Profile) {
  1058. // Whether the current user is a subscribed to this profile
  1059. $profileInfoAttr['following'] = $scoped->isSubscribed($this) ? 'true' : 'false';
  1060. // Whether the current user is has blocked this profile
  1061. $profileInfoAttr['blocking'] = $scoped->hasBlocked($this) ? 'true' : 'false';
  1062. }
  1063. return array('statusnet:profile_info', $profileInfoAttr, null);
  1064. }
  1065. /**
  1066. * Returns an XML string fragment with profile information as an
  1067. * Activity Streams <activity:actor> element.
  1068. *
  1069. * Assumes that 'activity' namespace has been previously defined.
  1070. *
  1071. * @return string
  1072. */
  1073. function asActivityActor()
  1074. {
  1075. return $this->asActivityNoun('actor');
  1076. }
  1077. /**
  1078. * Returns an XML string fragment with profile information as an
  1079. * Activity Streams noun object with the given element type.
  1080. *
  1081. * Assumes that 'activity', 'georss', and 'poco' namespace has been
  1082. * previously defined.
  1083. *
  1084. * @param string $element one of 'actor', 'subject', 'object', 'target'
  1085. *
  1086. * @return string
  1087. */
  1088. function asActivityNoun($element)
  1089. {
  1090. $noun = $this->asActivityObject();
  1091. return $noun->asString('activity:' . $element);
  1092. }
  1093. public function asActivityObject()
  1094. {
  1095. $object = new ActivityObject();
  1096. if (Event::handle('StartActivityObjectFromProfile', array($this, &$object))) {
  1097. $object->type = $this->getObjectType();
  1098. $object->id = $this->getUri();
  1099. $object->title = $this->getBestName();
  1100. $object->link = $this->getUrl();
  1101. $object->summary = $this->getDescription();
  1102. try {
  1103. $avatar = Avatar::getUploaded($this);
  1104. $object->avatarLinks[] = AvatarLink::fromAvatar($avatar);
  1105. } catch (NoAvatarException $e) {
  1106. // Could not find an original avatar to link
  1107. }
  1108. $sizes = array(
  1109. AVATAR_PROFILE_SIZE,
  1110. AVATAR_STREAM_SIZE,
  1111. AVATAR_MINI_SIZE
  1112. );
  1113. foreach ($sizes as $size) {
  1114. $alink = null;
  1115. try {
  1116. $avatar = Avatar::byProfile($this, $size);
  1117. $alink = AvatarLink::fromAvatar($avatar);
  1118. } catch (NoAvatarException $e) {
  1119. $alink = new AvatarLink();
  1120. $alink->type = 'image/png';
  1121. $alink->height = $size;
  1122. $alink->width = $size;
  1123. $alink->url = Avatar::defaultImage($size);
  1124. }
  1125. $object->avatarLinks[] = $alink;
  1126. }
  1127. if (isset($this->lat) && isset($this->lon)) {
  1128. $object->geopoint = (float)$this->lat
  1129. . ' ' . (float)$this->lon;
  1130. }
  1131. $object->poco = PoCo::fromProfile($this);
  1132. if ($this->isLocal()) {
  1133. $object->extra[] = array('followers', array('url' => common_local_url('subscribers', array('nickname' => $this->getNickname()))));
  1134. }
  1135. Event::handle('EndActivityObjectFromProfile', array($this, &$object));
  1136. }
  1137. return $object;
  1138. }
  1139. /**
  1140. * Returns the profile's canonical url, not necessarily a uri/unique id
  1141. *
  1142. * @return string $profileurl
  1143. */
  1144. public function getUrl()
  1145. {
  1146. if (empty($this->profileurl) ||
  1147. !filter_var($this->profileurl, FILTER_VALIDATE_URL)) {
  1148. throw new InvalidUrlException($this->profileurl);
  1149. }
  1150. return $this->profileurl;
  1151. }
  1152. public function getNickname()
  1153. {
  1154. return $this->nickname;
  1155. }
  1156. public function getFullname()
  1157. {
  1158. return $this->fullname;
  1159. }
  1160. public function getDescription()
  1161. {
  1162. return $this->bio;
  1163. }
  1164. /**
  1165. * Returns the best URI for a profile. Plugins may override.
  1166. *
  1167. * @return string $uri
  1168. */
  1169. public function getUri()
  1170. {
  1171. $uri = null;
  1172. // give plugins a chance to set the URI
  1173. if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
  1174. // check for a local user first
  1175. $user = User::getKV('id', $this->id);
  1176. if ($user instanceof User) {
  1177. $uri = $user->getUri();
  1178. }
  1179. Event::handle('EndGetProfileUri', array($this, &$uri));
  1180. }
  1181. return $uri;
  1182. }
  1183. /**
  1184. * Returns an assumed acct: URI for a profile. Plugins are required.
  1185. *
  1186. * @return string $uri
  1187. */
  1188. public function getAcctUri()
  1189. {
  1190. $acct = null;
  1191. if (Event::handle('StartGetProfileAcctUri', array($this, &$acct))) {
  1192. Event::handle('EndGetProfileAcctUri', array($this, &$acct));
  1193. }
  1194. if ($acct === null) {
  1195. throw new ProfileNoAcctUriException($this);
  1196. }
  1197. return $acct;
  1198. }
  1199. function hasBlocked($other)
  1200. {
  1201. $block = Profile_block::exists($this, $other);
  1202. return !empty($block);
  1203. }
  1204. function getAtomFeed()
  1205. {
  1206. $feed = null;
  1207. if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
  1208. $user = User::getKV('id', $this->id);
  1209. if (!empty($user)) {
  1210. $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
  1211. 'format' => 'atom'));
  1212. }
  1213. Event::handle('EndProfileGetAtomFeed', array($this, $feed));
  1214. }
  1215. return $feed;
  1216. }
  1217. public function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
  1218. {
  1219. // TRANS: Exception thrown when trying view "repeated to me".
  1220. throw new Exception(_('Not implemented since inbox change.'));
  1221. }
  1222. /*
  1223. * Get a Profile object by URI. Will call external plugins for help
  1224. * using the event StartGetProfileFromURI.
  1225. *
  1226. * @param string $uri A unique identifier for a resource (profile/group/whatever)
  1227. */
  1228. static function fromUri($uri)
  1229. {
  1230. $profile = null;
  1231. if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
  1232. // Get a local user when plugin lookup (like OStatus) fails
  1233. $user = User::getKV('uri', $uri);
  1234. if ($user instanceof User) {
  1235. $profile = $user->getProfile();
  1236. }
  1237. Event::handle('EndGetProfileFromURI', array($uri, $profile));
  1238. }
  1239. if (!$profile instanceof Profile) {
  1240. throw new UnknownUriException($uri);
  1241. }
  1242. return $profile;
  1243. }
  1244. function canRead(Notice $notice)
  1245. {
  1246. if ($notice->scope & Notice::SITE_SCOPE) {
  1247. $user = $this->getUser();
  1248. if (empty($user)) {
  1249. return false;
  1250. }
  1251. }
  1252. if ($notice->scope & Notice::ADDRESSEE_SCOPE) {
  1253. $replies = $notice->getReplies();
  1254. if (!in_array($this->id, $replies)) {
  1255. $groups = $notice->getGroups();
  1256. $foundOne = false;
  1257. foreach ($groups as $group) {
  1258. if ($this->isMember($group)) {
  1259. $foundOne = true;
  1260. break;
  1261. }
  1262. }
  1263. if (!$foundOne) {
  1264. return false;
  1265. }
  1266. }
  1267. }
  1268. if ($notice->scope & Notice::FOLLOWER_SCOPE) {
  1269. $author = $notice->getProfile();
  1270. if (!Subscription::exists($this, $author)) {
  1271. return false;
  1272. }
  1273. }
  1274. return true;
  1275. }
  1276. static function current()
  1277. {
  1278. $user = common_current_user();
  1279. if (empty($user)) {
  1280. $profile = null;
  1281. } else {
  1282. $profile = $user->getProfile();
  1283. }
  1284. return $profile;
  1285. }
  1286. /**
  1287. * Magic function called at serialize() time.
  1288. *
  1289. * We use this to drop a couple process-specific references
  1290. * from DB_DataObject which can cause trouble in future
  1291. * processes.
  1292. *
  1293. * @return array of variable names to include in serialization.
  1294. */
  1295. function __sleep()
  1296. {
  1297. $vars = parent::__sleep();
  1298. $skip = array('_user', '_group');
  1299. return array_diff($vars, $skip);
  1300. }
  1301. public function getProfile()
  1302. {
  1303. return $this;
  1304. }
  1305. /**
  1306. * This will perform shortenLinks with the connected User object.
  1307. *
  1308. * Won't work on remote profiles or groups, so expect a
  1309. * NoSuchUserException if you don't know it's a local User.
  1310. *
  1311. * @param string $text String to shorten
  1312. * @param boolean $always Disrespect minimum length etc.
  1313. *
  1314. * @return string link-shortened $text
  1315. */
  1316. public function shortenLinks($text, $always=false)
  1317. {
  1318. return $this->getUser()->shortenLinks($text, $always);
  1319. }
  1320. public function isPrivateStream()
  1321. {
  1322. // We only know of public remote users as of yet...
  1323. if (!$this->isLocal()) {
  1324. return false;
  1325. }
  1326. return $this->getUser()->private_stream ? true : false;
  1327. }
  1328. public function delPref($namespace, $topic) {
  1329. return Profile_prefs::setData($this, $namespace, $topic, null);
  1330. }
  1331. public function getPref($namespace, $topic, $default=null) {
  1332. // If you want an exception to be thrown, call Profile_prefs::getData directly
  1333. try {
  1334. return Profile_prefs::getData($this, $namespace, $topic, $default);
  1335. } catch (NoResultException $e) {
  1336. return null;
  1337. }
  1338. }
  1339. // The same as getPref but will fall back to common_config value for the same namespace/topic
  1340. public function getConfigPref($namespace, $topic)
  1341. {
  1342. return Profile_prefs::getConfigData($this, $namespace, $topic);
  1343. }
  1344. public function setPref($namespace, $topic, $data) {
  1345. return Profile_prefs::setData($this, $namespace, $topic, $data);
  1346. }
  1347. }