]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / classes / User_group.php
1 <?php
2 /**
3  * Table Definition for user_group
4  */
5
6 class User_group extends Managed_DataObject
7 {
8     const JOIN_POLICY_OPEN = 0;
9     const JOIN_POLICY_MODERATE = 1;
10     const CACHE_WINDOW = 201;
11
12     ###START_AUTOCODE
13     /* the code below is auto generated do not remove the above tag */
14
15     public $__table = 'user_group';                      // table name
16     public $id;                              // int(4)  primary_key not_null
17     public $nickname;                        // varchar(64)
18     public $fullname;                        // varchar(191)   not 255 because utf8mb4 takes more space
19     public $homepage;                        // varchar(191)   not 255 because utf8mb4 takes more space
20     public $description;                     // text
21     public $location;                        // varchar(191)   not 255 because utf8mb4 takes more space
22     public $original_logo;                   // varchar(191)   not 255 because utf8mb4 takes more space
23     public $homepage_logo;                   // varchar(191)   not 255 because utf8mb4 takes more space
24     public $stream_logo;                     // varchar(191)   not 255 because utf8mb4 takes more space
25     public $mini_logo;                       // varchar(191)   not 255 because utf8mb4 takes more space
26     public $created;                         // datetime   not_null default_0000-00-00%2000%3A00%3A00
27     public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
28     public $uri;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
29     public $mainpage;                        // varchar(191)   not 255 because utf8mb4 takes more space
30     public $join_policy;                     // tinyint
31     public $force_scope;                     // tinyint
32
33     /* the code above is auto generated do not remove the tag below */
34     ###END_AUTOCODE
35
36     public function getObjectType()
37     {
38         return ActivityObject::GROUP;
39     }
40
41
42     public static function schemaDef()
43     {
44         return array(
45             'fields' => array(
46                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
47                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
48
49                 'nickname' => array('type' => 'varchar', 'length' => 64, 'description' => 'nickname for addressing'),
50                 'fullname' => array('type' => 'varchar', 'length' => 191, 'description' => 'display name'),
51                 'homepage' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL, cached so we dont regenerate'),
52                 'description' => array('type' => 'text', 'description' => 'group description'),
53                 'location' => array('type' => 'varchar', 'length' => 191, 'description' => 'related physical location, if any'),
54
55                 'original_logo' => array('type' => 'varchar', 'length' => 191, 'description' => 'original size logo'),
56                 'homepage_logo' => array('type' => 'varchar', 'length' => 191, 'description' => 'homepage (profile) size logo'),
57                 'stream_logo' => array('type' => 'varchar', 'length' => 191, 'description' => 'stream-sized logo'),
58                 'mini_logo' => array('type' => 'varchar', 'length' => 191, 'description' => 'mini logo'),
59
60                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
61                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
62
63                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'),
64                 'mainpage' => array('type' => 'varchar', 'length' => 191, 'description' => 'page for group info to link to'),
65                 'join_policy' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=open; 1=requires admin approval'),      
66                 'force_scope' => array('type' => 'int', 'size' => 'tiny', 'description' => '0=never,1=sometimes,-1=always'),
67             ),
68             'primary key' => array('id'),
69             'unique keys' => array(
70                 'user_group_uri_key' => array('uri'),
71 // when it's safe and everyone's run upgrade.php                'user_profile_id_key' => array('profile_id'),
72             ),
73             'foreign keys' => array(
74                 'user_group_id_fkey' => array('profile', array('profile_id' => 'id')),
75             ),
76             'indexes' => array(
77                 'user_group_nickname_idx' => array('nickname'),
78                 'user_group_profile_id_idx' => array('profile_id'), //make this unique in future
79             ),
80         );
81     }
82
83     protected $_profile = array();
84
85     /**
86      * @return Profile
87      *
88      * @throws GroupNoProfileException if user has no profile
89      */
90     public function getProfile()
91     {
92         if (!isset($this->_profile[$this->profile_id])) {
93             $profile = Profile::getKV('id', $this->profile_id);
94             if (!$profile instanceof Profile) {
95                 throw new GroupNoProfileException($this);
96             }
97             $this->_profile[$this->profile_id] = $profile;
98         }
99         return $this->_profile[$this->profile_id];
100     }
101
102     public function getNickname()
103     {
104         return $this->getProfile()->getNickname();
105     }
106
107     public static function defaultLogo($size)
108     {
109         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
110                                   AVATAR_STREAM_SIZE => 'stream',
111                                   AVATAR_MINI_SIZE => 'mini');
112         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
113     }
114
115     function homeUrl()
116     {
117         $url = null;
118         if (Event::handle('StartUserGroupHomeUrl', array($this, &$url))) {
119             // normally stored in mainpage, but older ones may be null
120             if (!empty($this->mainpage)) {
121                 $url = $this->mainpage;
122             } elseif ($this->isLocal()) {
123                 $url = common_local_url('showgroup',
124                                         array('nickname' => $this->nickname));
125             }
126         }
127         Event::handle('EndUserGroupHomeUrl', array($this, &$url));
128         return $url;
129     }
130
131     function getUri()
132     {
133         $uri = null;
134         if (Event::handle('StartUserGroupGetUri', array($this, &$uri))) {
135             if (!empty($this->uri)) {
136                 $uri = $this->uri;
137             } elseif ($this->isLocal()) {
138                 $uri = common_local_url('groupbyid',
139                                         array('id' => $this->id));
140             }
141         }
142         Event::handle('EndUserGroupGetUri', array($this, &$uri));
143         return $uri;
144     }
145
146     function permalink()
147     {
148         $url = null;
149         if (Event::handle('StartUserGroupPermalink', array($this, &$url))) {
150             if ($this->isLocal()) {
151                 $url = common_local_url('groupbyid',
152                                         array('id' => $this->id));
153             }
154         }
155         Event::handle('EndUserGroupPermalink', array($this, &$url));
156         return $url;
157     }
158
159     function getNotices($offset, $limit, $since_id=null, $max_id=null)
160     {
161         $stream = new GroupNoticeStream($this);
162
163         return $stream->getNotices($offset, $limit, $since_id, $max_id);
164     }
165
166     function getMembers($offset=0, $limit=null) {
167         $ids = null;
168         if (is_null($limit) || $offset + $limit > User_group::CACHE_WINDOW) {
169             $ids = $this->getMemberIDs($offset,
170                                        $limit);
171         } else {
172             $key = sprintf('group:member_ids:%d', $this->id);
173             $window = self::cacheGet($key);
174             if ($window === false) {
175                 $window = $this->getMemberIDs(0,
176                                               User_group::CACHE_WINDOW);
177                 self::cacheSet($key, $window);
178             }
179
180             $ids = array_slice($window,
181                                $offset,
182                                $limit);
183         }
184
185         return Profile::multiGet('id', $ids);
186     }
187
188     function getMemberIDs($offset=0, $limit=null)
189     {
190         $gm = new Group_member();
191
192         $gm->selectAdd();
193         $gm->selectAdd('profile_id');
194
195         $gm->group_id = $this->id;
196
197         $gm->orderBy('created DESC');
198
199         if (!is_null($limit)) {
200             $gm->limit($offset, $limit);
201         }
202
203         $ids = array();
204
205         if ($gm->find()) {
206             while ($gm->fetch()) {
207                 $ids[] = $gm->profile_id;
208             }
209         }
210
211         return $ids;
212     }
213
214     /**
215      * Get pending members, who have not yet been approved.
216      *
217      * @param int $offset
218      * @param int $limit
219      * @return Profile
220      */
221     function getRequests($offset=0, $limit=null)
222     {
223         $rq = new Group_join_queue();
224         $rq->group_id = $this->id;
225
226         $members = new Profile();
227
228         $members->joinAdd(['id', $rq, 'profile_id']);
229
230         if ($limit != null) {
231             $members->limit($offset, $limit);
232         }
233
234         $members->find();
235
236         return $members;
237     }
238
239     public function getAdminCount()
240     {
241         $block = new Group_member();
242         $block->group_id = $this->id;
243         $block->is_admin = 1;
244
245         return $block->count();
246     }
247
248     public function getMemberCount()
249     {
250         $key = sprintf("group:member_count:%d", $this->id);
251
252         $cnt = self::cacheGet($key);
253
254         if (is_integer($cnt)) {
255             return (int) $cnt;
256         }
257
258         $mem = new Group_member();
259         $mem->group_id = $this->id;
260
261         // XXX: why 'distinct'?
262
263         $cnt = (int) $mem->count('distinct profile_id');
264
265         self::cacheSet($key, $cnt);
266
267         return $cnt;
268     }
269
270     function getBlockedCount()
271     {
272         // XXX: WORM cache this
273
274         $block = new Group_block();
275         $block->group_id = $this->id;
276
277         return $block->count();
278     }
279
280     function getQueueCount()
281     {
282         // XXX: WORM cache this
283
284         $queue = new Group_join_queue();
285         $queue->group_id = $this->id;
286
287         return $queue->count();
288     }
289
290     function getAdmins($offset=null, $limit=null)   // offset is null because DataObject wants it, 0 would mean no results
291     {
292         $admins = new Profile();
293         $admins->joinAdd(array('id', 'group_member:profile_id'));
294         $admins->whereAdd(sprintf('group_member.group_id = %u AND group_member.is_admin = 1', $this->id));
295         $admins->orderBy('group_member.modified ASC');
296         $admins->limit($offset, $limit);
297         $admins->find();
298
299         return $admins;
300     }
301
302     function getBlocked($offset=null, $limit=null)   // offset is null because DataObject wants it, 0 would mean no results
303     {
304         $blocked = new Profile();
305         $blocked->joinAdd(array('id', 'group_block:blocked'));
306         $blocked->whereAdd(sprintf('group_block.group_id = %u', $this->id));
307         $blocked->orderBy('group_block.modified DESC');
308         $blocked->limit($offset, $limit);
309         $blocked->find();
310
311         return $blocked;
312     }
313
314     function setOriginal($filename)
315     {
316         // This should be handled by the Profile->setOriginal function so user and group avatars are handled the same
317         $imagefile = new ImageFile(null, Avatar::path($filename));
318
319         $sizes = array('homepage_logo' => AVATAR_PROFILE_SIZE,
320                        'stream_logo' => AVATAR_STREAM_SIZE,
321                        'mini_logo' => AVATAR_MINI_SIZE);
322
323         $orig = clone($this);
324         $this->original_logo = Avatar::url($filename);
325         foreach ($sizes as $name=>$size) {
326             $filename = Avatar::filename($this->profile_id, image_type_to_extension($imagefile->preferredType()),
327                                          $size, common_timestamp());
328             $imagefile->resizeTo(Avatar::path($filename), array('width'=>$size, 'height'=>$size));
329             $this->$name = Avatar::url($filename);
330         }
331         common_debug(common_log_objstring($this));
332         return $this->update($orig);
333     }
334
335     function getBestName()
336     {
337         return ($this->fullname) ? $this->fullname : $this->nickname;
338     }
339
340     /**
341      * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone
342      * if no fullname is provided.
343      *
344      * @return string
345      */
346     function getFancyName()
347     {
348         if ($this->fullname) {
349             // TRANS: Full name of a profile or group followed by nickname in parens
350             return sprintf(_m('FANCYNAME','%1$s (%2$s)'), $this->fullname, $this->nickname);
351         } else {
352             return $this->nickname;
353         }
354     }
355
356     function getAliases()
357     {
358         $aliases = array();
359
360         // XXX: cache this
361
362         $alias = new Group_alias();
363
364         $alias->group_id = $this->id;
365
366         if ($alias->find()) {
367             while ($alias->fetch()) {
368                 $aliases[] = $alias->alias;
369             }
370         }
371
372         $alias->free();
373
374         return $aliases;
375     }
376
377     function setAliases($newaliases) {
378
379         $newaliases = array_unique($newaliases);
380
381         $oldaliases = $this->getAliases();
382
383         // Delete stuff that's old that not in new
384
385         $to_delete = array_diff($oldaliases, $newaliases);
386
387         // Insert stuff that's in new and not in old
388
389         $to_insert = array_diff($newaliases, $oldaliases);
390
391         $alias = new Group_alias();
392
393         $alias->group_id = $this->id;
394
395         foreach ($to_delete as $delalias) {
396             $alias->alias = $delalias;
397             $result = $alias->delete();
398             if (!$result) {
399                 common_log_db_error($alias, 'DELETE', __FILE__);
400                 return false;
401             }
402         }
403
404         foreach ($to_insert as $insalias) {
405             if ($insalias === $this->nickname) {
406                 continue;
407             }
408             $alias->alias = Nickname::normalize($insalias, true);
409             $result = $alias->insert();
410             if (!$result) {
411                 common_log_db_error($alias, 'INSERT', __FILE__);
412                 return false;
413             }
414         }
415
416         return true;
417     }
418
419     static function getForNickname($nickname, Profile $profile=null)
420     {
421         $nickname = Nickname::normalize($nickname);
422
423         // Are there any matching remote groups this profile's in?
424         if ($profile instanceof Profile) {
425             $group = $profile->getGroups(0, null);
426             while ($group instanceof User_group && $group->fetch()) {
427                 if ($group->nickname == $nickname) {
428                     // @fixme is this the best way?
429                     return clone($group);
430                 }
431             }
432         }
433
434         // If not, check local groups.
435         $group = Local_group::getKV('nickname', $nickname);
436         if ($group instanceof Local_group) {
437             return User_group::getKV('id', $group->group_id);
438         }
439         $alias = Group_alias::getKV('alias', $nickname);
440         if ($alias instanceof Group_alias) {
441             return User_group::getKV('id', $alias->group_id);
442         }
443         return null;
444     }
445
446     function getUserMembers()
447     {
448         // XXX: cache this
449
450         $user = new User();
451         if(common_config('db','quote_identifiers'))
452             $user_table = '"user"';
453         else $user_table = 'user';
454
455         $qry =
456           'SELECT id ' .
457           'FROM '. $user_table .' JOIN group_member '.
458           'ON '. $user_table .'.id = group_member.profile_id ' .
459           'WHERE group_member.group_id = %d ';
460
461         $user->query(sprintf($qry, $this->id));
462
463         $ids = array();
464
465         while ($user->fetch()) {
466             $ids[] = $user->id;
467         }
468
469         $user->free();
470
471         return $ids;
472     }
473
474     static function maxDescription()
475     {
476         $desclimit = common_config('group', 'desclimit');
477         // null => use global limit (distinct from 0!)
478         if (is_null($desclimit)) {
479             $desclimit = common_config('site', 'textlimit');
480         }
481         return $desclimit;
482     }
483
484     static function descriptionTooLong($desc)
485     {
486         $desclimit = self::maxDescription();
487         return ($desclimit > 0 && !empty($desc) && (mb_strlen($desc) > $desclimit));
488     }
489
490     function asAtomEntry($namespace=false, $source=false)
491     {
492         $xs = new XMLStringer(true);
493
494         if ($namespace) {
495             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
496                            'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
497         } else {
498             $attrs = array();
499         }
500
501         $xs->elementStart('entry', $attrs);
502
503         if ($source) {
504             $xs->elementStart('source');
505             $xs->element('id', null, $this->permalink());
506             $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
507             $xs->element('link', array('href' => $this->permalink()));
508             $xs->element('updated', null, $this->modified);
509             $xs->elementEnd('source');
510         }
511
512         $xs->element('title', null, $this->nickname);
513         $xs->element('summary', null, common_xml_safe_str($this->description));
514
515         $xs->element('link', array('rel' => 'alternate',
516                                    'href' => $this->permalink()));
517
518         $xs->element('id', null, $this->permalink());
519
520         $xs->element('published', null, common_date_w3dtf($this->created));
521         $xs->element('updated', null, common_date_w3dtf($this->modified));
522
523         $xs->element(
524             'content',
525             array('type' => 'html'),
526             common_xml_safe_str($this->description)
527         );
528
529         $xs->elementEnd('entry');
530
531         return $xs->getString();
532     }
533
534     function asAtomAuthor()
535     {
536         $xs = new XMLStringer(true);
537
538         $xs->elementStart('author');
539         $xs->element('name', null, $this->nickname);
540         $xs->element('uri', null, $this->permalink());
541         $xs->elementEnd('author');
542
543         return $xs->getString();
544     }
545
546     /**
547      * Returns an XML string fragment with group information as an
548      * Activity Streams noun object with the given element type.
549      *
550      * Assumes that 'activity', 'georss', and 'poco' namespace has been
551      * previously defined.
552      *
553      * @param string $element one of 'actor', 'subject', 'object', 'target'
554      *
555      * @return string
556      */
557     function asActivityNoun($element)
558     {
559         $noun = ActivityObject::fromGroup($this);
560         return $noun->asString('activity:' . $element);
561     }
562
563     function getAvatar()
564     {
565         return empty($this->homepage_logo)
566             ? User_group::defaultLogo(AVATAR_PROFILE_SIZE)
567             : $this->homepage_logo;
568     }
569
570     static function register($fields) {
571         if (!empty($fields['userid'])) {
572             $profile = Profile::getKV('id', $fields['userid']);
573             if ($profile && !$profile->hasRight(Right::CREATEGROUP)) {
574                 common_log(LOG_WARNING, "Attempted group creation from banned user: " . $profile->nickname);
575
576                 // TRANS: Client exception thrown when a user tries to create a group while banned.
577                 throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
578             }
579         }
580
581         $fields['nickname'] = Nickname::normalize($fields['nickname']);
582
583         // MAGICALLY put fields into current scope
584         // @fixme kill extract(); it makes debugging absurdly hard
585
586                 $defaults = array('nickname' => null,
587                                                   'fullname' => null,
588                                                   'homepage' => null,
589                                                   'description' => null,
590                                                   'location' => null,
591                                                   'uri' => null,
592                                                   'mainpage' => null,
593                                                   'aliases' => array(),
594                                                   'userid' => null);
595                 
596                 $fields = array_merge($defaults, $fields);
597                 
598         extract($fields);
599
600         $group = new User_group();
601
602         if (empty($uri)) {
603             // fill in later...
604             $uri = null;
605         }
606         if (empty($mainpage)) {
607             $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
608         }
609
610         // We must create a new, incrementally assigned profile_id
611         $profile = new Profile();
612         $profile->nickname   = $nickname;
613         $profile->fullname   = $fullname;
614         $profile->profileurl = $mainpage;
615         $profile->homepage   = $homepage;
616         $profile->bio        = $description;
617         $profile->location   = $location;
618         $profile->created    = common_sql_now();
619
620         $group->nickname    = $profile->nickname;
621         $group->fullname    = $profile->fullname;
622         $group->homepage    = $profile->homepage;
623         $group->description = $profile->bio;
624         $group->location    = $profile->location;
625         $group->mainpage    = $profile->profileurl;
626         $group->created     = $profile->created;
627
628         $profile->query('BEGIN');
629         $id = $profile->insert();
630         if ($id === false) {
631             $profile->query('ROLLBACK');
632             throw new ServerException(_('Profile insertion failed'));
633         }
634
635         $group->profile_id = $id;
636         $group->uri        = $uri;
637
638         if (isset($fields['join_policy'])) {
639             $group->join_policy = intval($fields['join_policy']);
640         } else {
641             $group->join_policy = 0;
642         }
643
644         if (isset($fields['force_scope'])) {
645             $group->force_scope = intval($fields['force_scope']);
646         } else {
647             $group->force_scope = 0;
648         }
649
650         if (Event::handle('StartGroupSave', array(&$group))) {
651
652             $result = $group->insert();
653
654             if ($result === false) {
655                 common_log_db_error($group, 'INSERT', __FILE__);
656                 // TRANS: Server exception thrown when creating a group failed.
657                 throw new ServerException(_('Could not create group.'));
658             }
659
660             if (!isset($uri) || empty($uri)) {
661                 $orig = clone($group);
662                 $group->uri = common_local_url('groupbyid', array('id' => $group->id));
663                 $result = $group->update($orig);
664                 if (!$result) {
665                     common_log_db_error($group, 'UPDATE', __FILE__);
666                     // TRANS: Server exception thrown when updating a group URI failed.
667                     throw new ServerException(_('Could not set group URI.'));
668                 }
669             }
670
671             $result = $group->setAliases($aliases);
672
673             if (!$result) {
674                 // TRANS: Server exception thrown when creating group aliases failed.
675                 throw new ServerException(_('Could not create aliases.'));
676             }
677
678             $member = new Group_member();
679
680             $member->group_id   = $group->id;
681             $member->profile_id = $userid;
682             $member->is_admin   = 1;
683             $member->created    = $group->created;
684
685             $result = $member->insert();
686
687             if (!$result) {
688                 common_log_db_error($member, 'INSERT', __FILE__);
689                 // TRANS: Server exception thrown when setting group membership failed.
690                 throw new ServerException(_('Could not set group membership.'));
691             }
692
693             self::blow('profile:groups:%d', $userid);
694             
695             if ($local) {
696                 $local_group = new Local_group();
697
698                 $local_group->group_id = $group->id;
699                 $local_group->nickname = $nickname;
700                 $local_group->created  = common_sql_now();
701
702                 $result = $local_group->insert();
703
704                 if (!$result) {
705                     common_log_db_error($local_group, 'INSERT', __FILE__);
706                     // TRANS: Server exception thrown when saving local group information failed.
707                     throw new ServerException(_('Could not save local group info.'));
708                 }
709             }
710
711             Event::handle('EndGroupSave', array($group));
712         }
713
714         $profile->query('COMMIT');
715
716         return $group;
717     }
718
719     /**
720      * Handle cascading deletion, on the model of notice and profile.
721      *
722      * This should handle freeing up cached entries for the group's
723      * id, nickname, URI, and aliases. There may be other areas that
724      * are not de-cached in the UI, including the sidebar lists on
725      * GroupsAction
726      */
727     function delete($useWhere=false)
728     {
729         if (empty($this->id)) {
730             common_log(LOG_WARNING, "Ambiguous User_group->delete(); skipping related tables.");
731             return parent::delete($useWhere);
732         }
733
734         try {
735             $profile = $this->getProfile();
736             $profile->delete();
737         } catch (GroupNoProfileException $unp) {
738             common_log(LOG_INFO, "Group {$this->nickname} has no profile; continuing deletion.");
739         }
740
741         // Safe to delete in bulk for now
742
743         $related = array('Group_inbox',
744                          'Group_block',
745                          'Group_member',
746                          'Related_group');
747
748         Event::handle('UserGroupDeleteRelated', array($this, &$related));
749
750         foreach ($related as $cls) {
751             $inst = new $cls();
752             $inst->group_id = $this->id;
753
754             if ($inst->find()) {
755                 while ($inst->fetch()) {
756                     $dup = clone($inst);
757                     $dup->delete();
758                 }
759             }
760         }
761
762         // And related groups in the other direction...
763         $inst = new Related_group();
764         $inst->related_group_id = $this->id;
765         $inst->delete();
766
767         // Aliases and the local_group entry need to be cleared explicitly
768         // or we'll miss clearing some cache keys; that can make it hard
769         // to create a new group with one of those names or aliases.
770         $this->setAliases(array());
771
772         // $this->isLocal() but we're using the resulting object
773         $local = Local_group::getKV('group_id', $this->id);
774         if ($local instanceof Local_group) {
775             $local->delete();
776         }
777
778         // blow the cached ids
779         self::blow('user_group:notice_ids:%d', $this->id);
780
781         return parent::delete($useWhere);
782     }
783
784     public function update($dataObject=false)
785     {
786         // Whenever the User_group is updated, find the Local_group
787         // and update its nickname too.
788         if ($this->nickname != $dataObject->nickname) {
789             $local = Local_group::getKV('group_id', $this->id);
790             if ($local instanceof Local_group) {
791                 common_debug("Updating Local_group ({$this->id}) nickname from {$dataObject->nickname} to {$this->nickname}");
792                 $local->setNickname($this->nickname);
793             }
794         }
795
796         // Also make sure the Profile table is up to date!
797         $fields = array(/*group field => profile field*/
798                     'nickname'      => 'nickname',
799                     'fullname'      => 'fullname',
800                     'mainpage'      => 'profileurl',
801                     'homepage'      => 'homepage',
802                     'description'   => 'bio',
803                     'location'      => 'location',
804                     'created'       => 'created',
805                     'modified'      => 'modified',
806                     );
807         $profile = $this->getProfile();
808         $origpro = clone($profile);
809         foreach ($fields as $gf=>$pf) {
810             $profile->$pf = $this->$gf;
811         }
812         if ($profile->update($origpro) === false) {
813             throw new ServerException(_('Unable to update profile'));
814         }
815
816         return parent::update($dataObject);
817     }
818
819     function isPrivate()
820     {
821         return ($this->join_policy == self::JOIN_POLICY_MODERATE &&
822                 intval($this->force_scope) === 1);
823     }
824
825     public function isLocal()
826     {
827         $local = Local_group::getKV('group_id', $this->id);
828         return ($local instanceof Local_group);
829     }
830
831     static function groupsFromText($text, Profile $profile)
832     {
833         $groups = array();
834
835         /* extract all !group */
836         $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
837                                 strtolower($text),
838                                 $match);
839
840         if (!$count) {
841             return $groups;
842         }
843
844         foreach (array_unique($match[1]) as $nickname) {
845             $group = self::getForNickname($nickname, $profile);
846             if ($group instanceof User_group && $profile->isMember($group)) {
847                 $groups[] = clone($group);
848             }
849         }
850
851         return $groups;
852     }
853
854     static function idsFromText($text, Profile $profile)
855     {
856         $ids = array();
857         $groups = self::groupsFromText($text, $profile);
858         foreach ($groups as $group) {
859             $ids[$group->id] = true;
860         }
861         return array_keys($ids);
862     }
863 }