]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mailbox.php
automatically handle non-laconica-named databases
[quix0rs-gnu-social.git] / lib / mailbox.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * common superclass for direct messages inbox and outbox
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Message
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/personal.php';
35
36 define('MESSAGES_PER_PAGE', 20);
37
38 /**
39  * common superclass for direct messages inbox and outbox
40  *
41  * @category Message
42  * @package  Laconica
43  * @author   Evan Prodromou <evan@controlyourself.ca>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://laconi.ca/
46  * @see      InboxAction
47  * @see      OutboxAction
48  */
49
50 class MailboxAction extends PersonalAction
51 {
52     var $page = null;
53
54     function prepare($args) 
55     {
56         parent::prepare($args);
57
58         $nickname   = common_canonical_nickname($this->arg('nickname'));
59         $this->user = User::staticGet('nickname', $nickname);
60         $this->page = $this->trimmed('page');
61
62         if (!$this->page) {
63             $this->page = 1;
64         }
65
66         common_set_returnto($this->selfUrl());
67
68         return true;
69     }
70
71     /**
72      * output page based on arguments
73      *
74      * @param array $args HTTP arguments (from $_REQUEST)
75      *
76      * @return void
77      */
78
79     function handle($args)
80     {
81         parent::handle($args);
82
83         if (!$this->user) {
84             $this->clientError(_('No such user.'), 404);
85             return;
86         }
87
88         $cur = common_current_user();
89
90         if (!$cur || $cur->id != $this->user->id) {
91             $this->clientError(_('Only the user can read their own mailboxes.'),
92                 403);
93             return;
94         }
95
96         $this->showPage();
97     }
98
99     function showLocalNav()
100     {
101         $nav = new PersonalGroupNav($this);
102         $nav->show();
103     }
104
105     function showNoticeForm()
106     {
107         $message_form = new MessageForm($this);
108         $message_form->show();
109     }
110
111     function showContent()
112     {
113         $message = $this->getMessages();
114
115         if ($message) {
116             $cnt = 0;
117             $this->elementStart('div', array('id' =>'notices_primary'));
118             $this->element('h2', null, _('Notices'));
119             $this->elementStart('ul', 'notices');
120
121             while ($message->fetch() && $cnt <= MESSAGES_PER_PAGE) {
122                 $cnt++;
123
124                 if ($cnt > MESSAGES_PER_PAGE) {
125                     break;
126                 }
127
128                 $this->showMessage($message);
129             }
130
131             $this->elementEnd('ul');
132
133             $this->pagination($this->page > 1, $cnt > MESSAGES_PER_PAGE,
134                               $this->page, $this->trimmed('action'),
135                               array('nickname' => $this->user->nickname));
136             $this->elementEnd('div');
137             $message->free();
138             unset($message);
139         }
140     }
141
142     function getMessages()
143     {
144         return null;
145     }
146
147     /**
148      * returns the profile we want to show with the message
149      *
150      * For inboxes, we show the sender; for outboxes, the recipient.
151      *
152      * @param Message $message The message to get the profile for
153      *
154      * @return Profile The profile that matches the message
155      */
156
157     function getMessageProfile($message)
158     {
159         return null;
160     }
161
162     /**
163      * show a single message in the list format
164      *
165      * XXX: This needs to be extracted out into a MessageList similar
166      * to NoticeList.
167      *
168      * @param Message $message the message to show
169      *
170      * @return void
171      */
172
173     function showMessage($message)
174     {
175         $this->elementStart('li', array('class' => 'hentry notice',
176                                          'id' => 'message-' . $message->id));
177
178         $profile = $this->getMessageProfile($message);
179
180         $this->elementStart('div', 'entry-title');
181         $this->elementStart('span', 'vcard author');
182         $this->elementStart('a', array('href' => $profile->profileurl,
183                                        'class' => 'url'));
184         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
185         $this->element('img', array('src' => ($avatar) ?
186                                     $avatar->displayUrl() :
187                                     Avatar::defaultImage(AVATAR_STREAM_SIZE),
188                                     'class' => 'photo avatar',
189                                     'width' => AVATAR_STREAM_SIZE,
190                                     'height' => AVATAR_STREAM_SIZE,
191                                     'alt' =>
192                                     ($profile->fullname) ? $profile->fullname :
193                                     $profile->nickname));
194         $this->element('span', array('class' => 'nickname fn'),
195                             $profile->nickname);
196         $this->elementEnd('a');
197         $this->elementEnd('span');
198
199         // FIXME: URL, image, video, audio
200         $this->elementStart('p', array('class' => 'entry-content'));
201         $this->raw($message->rendered);
202         $this->elementEnd('p');
203         $this->elementEnd('div');
204
205         $messageurl = common_local_url('showmessage',
206                                        array('message' => $message->id));
207
208         // XXX: we need to figure this out better. Is this right?
209         if (strcmp($message->uri, $messageurl) != 0 &&
210             preg_match('/^http/', $message->uri)) {
211             $messageurl = $message->uri;
212         }
213
214         $this->elementStart('div', 'entry-content');
215         $this->elementStart('dl', 'timestamp');
216         $this->element('dt', null, _('Published'));
217         $this->elementStart('dd', null);
218         $dt = common_date_iso8601($message->created);
219         $this->elementStart('a', array('rel' => 'bookmark',
220                                        'href' => $messageurl));
221         $this->element('abbr', array('class' => 'published',
222                                      'title' => $dt),
223                                common_date_string($message->created));
224         $this->elementEnd('a');
225         $this->elementEnd('dd');
226         $this->elementEnd('dl');
227
228         if ($message->source) {
229             $this->elementStart('dl', 'device');
230             $this->elementStart('dt');
231             $this->text(_('From'));
232             $this->elementEnd('dt');
233             $this->showSource($message->source);
234             $this->elementEnd('dl');
235         }
236         $this->elementEnd('div');
237
238         $this->elementEnd('li');
239     }
240
241     /**
242      * Show the page notice
243      *
244      * Shows instructions for the page
245      *
246      * @return void
247      */
248
249     function showPageNotice()
250     {
251         $instr  = $this->getInstructions();
252         $output = common_markup_to_html($instr);
253
254         $this->elementStart('div', 'instructions');
255         $this->raw($output);
256         $this->elementEnd('div');
257     }
258
259     /**
260      * Show the source of the message
261      *
262      * Returns either the name (and link) of the API client that posted the notice,
263      * or one of other other channels.
264      *
265      * @param string $source the source of the message 
266      *
267      * @return void
268      */
269
270     function showSource($source) 
271     {
272         $source_name = _($source);
273         switch ($source) {
274         case 'web':
275         case 'xmpp':
276         case 'mail':
277         case 'omb':
278         case 'api':
279             $this->element('dd', null, $source_name);
280             break;
281         default:
282             $ns = Notice_source::staticGet($source);
283             if ($ns) {
284                 $this->elementStart('dd', null);
285                 $this->element('a', array('href' => $ns->url,
286                                           'rel' => 'external'),
287                                $ns->name);
288                 $this->elementEnd('dd');
289             } else {
290                 $this->element('dd', null, $source_name);
291             }
292             break;
293         }
294         return;
295     }
296
297 }