]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DirectMessage/classes/Message.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / DirectMessage / classes / Message.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) { exit(1); }
4
5 /**
6  * Table Definition for message
7  */
8
9 class Message extends Managed_DataObject
10 {
11     ###START_AUTOCODE
12     /* the code below is auto generated do not remove the above tag */
13
14     public $__table = 'message';                         // table name
15     public $id;                              // int(4)  primary_key not_null
16     public $uri;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
17     public $from_profile;                    // int(4)   not_null
18     public $to_profile;                      // int(4)   not_null
19     public $content;                         // text()
20     public $rendered;                        // text()
21     public $url;                             // varchar(191)   not 255 because utf8mb4 takes more space
22     public $created;                         // datetime()   not_null
23     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
24     public $source;                          // varchar(32)
25
26     /* the code above is auto generated do not remove the tag below */
27     ###END_AUTOCODE
28
29     public static function schemaDef()
30     {
31         return array(
32             'fields' => array(
33                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
34                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier'),
35                 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is from'),
36                 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is to'),
37                 'content' => array('type' => 'text', 'description' => 'message content'),
38                 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
39                 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
40                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
41                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
42                 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
43             ),
44             'primary key' => array('id'),
45             'unique keys' => array(
46                 'message_uri_key' => array('uri'),
47             ),
48             'foreign keys' => array(
49                 'message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
50                 'message_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
51             ),
52             'indexes' => array(
53                 // @fixme these are really terrible indexes, since you can only sort on one of them at a time.
54                 // looks like we really need a (to_profile, created) for inbox and a (from_profile, created) for outbox
55                 'message_from_idx' => array('from_profile'),
56                 'message_to_idx' => array('to_profile'),
57                 'message_created_idx' => array('created'),
58             ),
59         );
60     }
61
62     function getFrom()
63     {
64         return Profile::getKV('id', $this->from_profile);
65     }
66
67     function getTo()
68     {
69         return Profile::getKV('id', $this->to_profile);
70     }
71
72     static function saveNew($from, $to, $content, $source) {
73         $sender = Profile::getKV('id', $from);
74
75         if (!$sender->hasRight(Right::NEWMESSAGE)) {
76             // TRANS: Client exception thrown when a user tries to send a direct message while being banned from sending them.
77             throw new ClientException(_('You are banned from sending direct messages.'));
78         }
79
80         $user = User::getKV('id', $sender->id);
81
82         $msg = new Message();
83
84         $msg->from_profile = $from;
85         $msg->to_profile = $to;
86         if ($user) {
87             // Use the sender's URL shortening options.
88             $msg->content = $user->shortenLinks($content);
89         } else {
90             $msg->content = common_shorten_links($content);
91         }
92         $msg->rendered = common_render_text($msg->content);
93         $msg->created = common_sql_now();
94         $msg->source = $source;
95
96         $result = $msg->insert();
97
98         if (!$result) {
99             common_log_db_error($msg, 'INSERT', __FILE__);
100             // TRANS: Message given when a message could not be stored on the server.
101             throw new ServerException(_('Could not insert message.'));
102         }
103
104         $orig = clone($msg);
105         $msg->uri = common_local_url('showmessage', array('message' => $msg->id));
106
107         $result = $msg->update($orig);
108
109         if (!$result) {
110             common_log_db_error($msg, 'UPDATE', __FILE__);
111             // TRANS: Message given when a message could not be updated on the server.
112             throw new ServerException(_('Could not update message with new URI.'));
113         }
114
115         return $msg;
116     }
117
118     static function maxContent()
119     {
120         $desclimit = common_config('message', 'contentlimit');
121         // null => use global limit (distinct from 0!)
122         if (is_null($desclimit)) {
123             $desclimit = common_config('site', 'textlimit');
124         }
125         return $desclimit;
126     }
127
128     static function contentTooLong($content)
129     {
130         $contentlimit = self::maxContent();
131         return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
132     }
133
134     function notify()
135     {
136         $from = User::getKV('id', $this->from_profile);
137         $to   = User::getKV('id', $this->to_profile);
138
139         mail_notify_message($this, $from, $to);
140     }
141
142     function getSource()
143     {
144         if (empty($this->source)) {
145             return false;
146         }
147
148         $ns = new Notice_source();
149         switch ($this->source) {
150         case 'web':
151         case 'xmpp':
152         case 'mail':
153         case 'omb':
154         case 'system':
155         case 'api':
156             $ns->code = $this->source;
157             break;
158         default:
159             $ns = Notice_source::getKV($this->source);
160             if (!$ns instanceof Notice_source) {
161                 $ns = new Notice_source();
162                 $ns->code = $this->source;
163                 $app = Oauth_application::getKV('name', $this->source);
164                 if ($app) {
165                     $ns->name = $app->name;
166                     $ns->url  = $app->source_url;
167                 }
168             }
169             break;
170         }
171         return $ns;
172     }
173
174     function asActivity()
175     {
176         $act = new Activity();
177
178         if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
179
180             $act->id      = TagURI::mint(sprintf('activity:message:%d', $this->id));
181             $act->time    = strtotime($this->created);
182             $act->link    = $this->url;
183
184             $profile = Profile::getKV('id', $this->from_profile);
185
186             if (empty($profile)) {
187                 throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
188             }
189             
190             $act->actor            = $profile->asActivityObject();
191             $act->actor->extra[]   = $profile->profileInfo();
192
193             $act->verb = ActivityVerb::POST;
194
195             $act->objects[] = ActivityObject::fromMessage($this);
196
197             $ctx = new ActivityContext();
198
199             $rprofile = Profile::getKV('id', $this->to_profile);
200
201             if (empty($rprofile)) {
202                 throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
203             }
204
205             $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
206
207             $act->context = $ctx;
208
209             $source = $this->getSource();
210
211             if ($source instanceof Notice_source) {
212                 $act->generator = ActivityObject::fromNoticeSource($source);
213             }
214
215             Event::handle('EndMessageAsActivity', array($this, &$act));
216         }
217
218         return $act;
219     }
220 }