]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mailbox.php
9af0dbd2f23a1635b6e2c92004a7ab0073795053
[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         return true;
67     }
68
69     /**
70      * output page based on arguments
71      *
72      * @param array $args HTTP arguments (from $_REQUEST)
73      *
74      * @return void
75      */
76
77     function handle($args)
78     {
79         parent::handle($args);
80
81         if (!$this->user) {
82             $this->clientError(_('No such user.'), 404);
83             return;
84         }
85
86         $cur = common_current_user();
87
88         if (!$cur || $cur->id != $this->user->id) {
89             $this->clientError(_('Only the user can read their own mailboxes.'),
90                 403);
91             return;
92         }
93
94         $this->showPage();
95     }
96
97     function showLocalNav()
98     {
99         $nav = new PersonalGroupNav($this);
100         $nav->show();
101     }
102
103     function showNoticeForm()
104     {
105         $message_form = new MessageForm($this);
106         $message_form->show();
107     }
108
109     function showContent()
110     {
111         $message = $this->getMessages();
112
113         if ($message) {
114
115             $cnt = 0;
116             $this->elementStart('ul', array('id' => 'messages'));
117
118             while ($message->fetch() && $cnt <= MESSAGES_PER_PAGE) {
119                 $cnt++;
120
121                 if ($cnt > MESSAGES_PER_PAGE) {
122                     break;
123                 }
124
125                 $this->showMessage($message);
126             }
127
128             $this->elementEnd('ul');
129
130             $this->pagination($this->page > 1, $cnt > MESSAGES_PER_PAGE,
131                               $this->page, $this->trimmed('action'),
132                               array('nickname' => $this->user->nickname));
133
134             $message->free();
135             unset($message);
136         }
137     }
138
139     function getMessages()
140     {
141         return null;
142     }
143
144     /**
145      * returns the profile we want to show with the message
146      *
147      * For inboxes, we show the sender; for outboxes, the recipient.
148      *
149      * @param Message $message The message to get the profile for
150      *
151      * @return Profile The profile that matches the message
152      */
153
154     function getMessageProfile($message)
155     {
156         return null;
157     }
158
159     /**
160      * show a single message in the list format
161      *
162      * XXX: This needs to be extracted out into a MessageList similar
163      * to NoticeList.
164      *
165      * @param Message $message the message to show
166      *
167      * @return void
168      */
169
170     function showMessage($message)
171     {
172         $this->elementStart('li', array('class' => 'message_single',
173                                          'id' => 'message-' . $message->id));
174
175         $profile = $this->getMessageProfile($message);
176
177         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
178         $this->elementStart('a', array('href' => $profile->profileurl));
179         $this->element('img', array('src' => ($avatar) ?
180                                     common_avatar_display_url($avatar) :
181                                     common_default_avatar(AVATAR_STREAM_SIZE),
182                                     'class' => 'avatar stream',
183                                     'width' => AVATAR_STREAM_SIZE,
184                                     'height' => AVATAR_STREAM_SIZE,
185                                     'alt' =>
186                                     ($profile->fullname) ? $profile->fullname :
187                                     $profile->nickname));
188         $this->elementEnd('a');
189         $this->element('a', array('href' => $profile->profileurl,
190                                   'class' => 'nickname'),
191                        $profile->nickname);
192         // FIXME: URL, image, video, audio
193         $this->elementStart('p', array('class' => 'content'));
194         $this->raw($message->rendered);
195         $this->elementEnd('p');
196
197         $messageurl = common_local_url('showmessage',
198                                        array('message' => $message->id));
199
200         // XXX: we need to figure this out better. Is this right?
201         if (strcmp($message->uri, $messageurl) != 0 &&
202             preg_match('/^http/', $message->uri)) {
203             $messageurl = $message->uri;
204         }
205         $this->elementStart('p', 'time');
206         $this->element('a', array('class' => 'permalink',
207                                   'href' => $messageurl,
208                                   'title' => common_exact_date($message->created)),
209                        common_date_string($message->created));
210         if ($message->source) {
211             $this->text(_(' from '));
212             $this->showSource($message->source);
213         }
214
215         $this->elementEnd('p');
216
217         $this->elementEnd('li');
218     }
219
220     /**
221      * Show the page notice
222      *
223      * Shows instructions for the page
224      *
225      * @return void
226      */
227
228     function showPageNotice()
229     {
230         $instr  = $this->getInstructions();
231         $output = common_markup_to_html($instr);
232
233         $this->elementStart('div', 'instructions');
234         $this->raw($output);
235         $this->elementEnd('div');
236     }
237
238     /**
239      * Show the source of the message
240      *
241      * Returns either the name (and link) of the API client that posted the notice,
242      * or one of other other channels.
243      *
244      * @param string $source the source of the message 
245      *
246      * @return void
247      */
248
249     function showSource($source) 
250     {
251         $source_name = _($source);
252         switch ($source) {
253         case 'web':
254         case 'xmpp':
255         case 'mail':
256         case 'omb':
257         case 'api':
258             $this->element('span', 'noticesource', $source_name);
259             break;
260         default:
261             $ns = Notice_source::staticGet($source);
262             if ($ns) {
263                 $this->element('a', array('href' => $ns->url),
264                                $ns->name);
265             } else {
266                 $this->element('span', 'noticesource', $source_name);
267             }
268             break;
269         }
270         return;
271     }
272
273 }