]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mailbox.php
Merge branch 'master' of git://gitorious.org/laconica/br3nda into review/master
[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         else {
141             $this->element('p', 'guide', _('You have no private messages. You can send private message to engage other users in conversation. People can send you messages for your eyes only.'));
142         }
143     }
144
145     function getMessages()
146     {
147         return null;
148     }
149
150     /**
151      * returns the profile we want to show with the message
152      *
153      * For inboxes, we show the sender; for outboxes, the recipient.
154      *
155      * @param Message $message The message to get the profile for
156      *
157      * @return Profile The profile that matches the message
158      */
159
160     function getMessageProfile($message)
161     {
162         return null;
163     }
164
165     /**
166      * show a single message in the list format
167      *
168      * XXX: This needs to be extracted out into a MessageList similar
169      * to NoticeList.
170      *
171      * @param Message $message the message to show
172      *
173      * @return void
174      */
175
176     function showMessage($message)
177     {
178         $this->elementStart('li', array('class' => 'hentry notice',
179                                          'id' => 'message-' . $message->id));
180
181         $profile = $this->getMessageProfile($message);
182
183         $this->elementStart('div', 'entry-title');
184         $this->elementStart('span', 'vcard author');
185         $this->elementStart('a', array('href' => $profile->profileurl,
186                                        'class' => 'url'));
187         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
188         $this->element('img', array('src' => ($avatar) ?
189                                     $avatar->displayUrl() :
190                                     Avatar::defaultImage(AVATAR_STREAM_SIZE),
191                                     'class' => 'photo avatar',
192                                     'width' => AVATAR_STREAM_SIZE,
193                                     'height' => AVATAR_STREAM_SIZE,
194                                     'alt' =>
195                                     ($profile->fullname) ? $profile->fullname :
196                                     $profile->nickname));
197         $this->element('span', array('class' => 'nickname fn'),
198                             $profile->nickname);
199         $this->elementEnd('a');
200         $this->elementEnd('span');
201
202         // FIXME: URL, image, video, audio
203         $this->elementStart('p', array('class' => 'entry-content'));
204         $this->raw($message->rendered);
205         $this->elementEnd('p');
206         $this->elementEnd('div');
207
208         $messageurl = common_local_url('showmessage',
209                                        array('message' => $message->id));
210
211         // XXX: we need to figure this out better. Is this right?
212         if (strcmp($message->uri, $messageurl) != 0 &&
213             preg_match('/^http/', $message->uri)) {
214             $messageurl = $message->uri;
215         }
216
217         $this->elementStart('div', 'entry-content');
218         $this->elementStart('dl', 'timestamp');
219         $this->element('dt', null, _('Published'));
220         $this->elementStart('dd', null);
221         $dt = common_date_iso8601($message->created);
222         $this->elementStart('a', array('rel' => 'bookmark',
223                                        'href' => $messageurl));
224         $this->element('abbr', array('class' => 'published',
225                                      'title' => $dt),
226                                common_date_string($message->created));
227         $this->elementEnd('a');
228         $this->elementEnd('dd');
229         $this->elementEnd('dl');
230
231         if ($message->source) {
232             $this->elementStart('dl', 'device');
233             $this->elementStart('dt');
234             $this->text(_('From'));
235             $this->elementEnd('dt');
236             $this->showSource($message->source);
237             $this->elementEnd('dl');
238         }
239         $this->elementEnd('div');
240
241         $this->elementEnd('li');
242     }
243
244     /**
245      * Show the page notice
246      *
247      * Shows instructions for the page
248      *
249      * @return void
250      */
251
252     function showPageNotice()
253     {
254         $instr  = $this->getInstructions();
255         $output = common_markup_to_html($instr);
256
257         $this->elementStart('div', 'instructions');
258         $this->raw($output);
259         $this->elementEnd('div');
260     }
261
262     /**
263      * Show the source of the message
264      *
265      * Returns either the name (and link) of the API client that posted the notice,
266      * or one of other other channels.
267      *
268      * @param string $source the source of the message 
269      *
270      * @return void
271      */
272
273     function showSource($source) 
274     {
275         $source_name = _($source);
276         switch ($source) {
277         case 'web':
278         case 'xmpp':
279         case 'mail':
280         case 'omb':
281         case 'api':
282             $this->element('dd', null, $source_name);
283             break;
284         default:
285             $ns = Notice_source::staticGet($source);
286             if ($ns) {
287                 $this->elementStart('dd', null);
288                 $this->element('a', array('href' => $ns->url,
289                                           'rel' => 'external'),
290                                $ns->name);
291                 $this->elementEnd('dd');
292             } else {
293                 $this->element('dd', null, $source_name);
294             }
295             break;
296         }
297         return;
298     }
299
300 }