]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/router.php
add StartInitializeRouter event
[quix0rs-gnu-social.git] / lib / router.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * URL routing utilities
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  URL
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once 'Net/URL/Mapper.php';
35
36 /**
37  * URL Router
38  *
39  * Cheap wrapper around Net_URL_Mapper
40  *
41  * @category URL
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class Router
49 {
50     var $m = null;
51     static $inst = null;
52     static $bare = array('requesttoken', 'accesstoken', 'userauthorization',
53                          'postnotice', 'updateprofile', 'finishremotesubscribe');
54
55     static function get()
56     {
57         if (!Router::$inst) {
58             Router::$inst = new Router();
59         }
60         return Router::$inst;
61     }
62
63     function __construct()
64     {
65         if (!$this->m) {
66             $this->m = $this->initialize();
67         }
68     }
69
70     function initialize()
71     {
72         $m = Net_URL_Mapper::getInstance();
73
74         if (Event::handle('StartInitializeRouter', array(&$m))) {
75
76             // In the "root"
77
78             $m->connect('', array('action' => 'public'));
79             $m->connect('rss', array('action' => 'publicrss'));
80             $m->connect('featuredrss', array('action' => 'featuredrss'));
81             $m->connect('favoritedrss', array('action' => 'favoritedrss'));
82             $m->connect('opensearch/people', array('action' => 'opensearch',
83                                                    'type' => 'people'));
84             $m->connect('opensearch/notice', array('action' => 'opensearch',
85                                                    'type' => 'notice'));
86
87             // docs
88
89             $m->connect('doc/:title', array('action' => 'doc'));
90
91             // main stuff is repetitive
92
93             $main = array('login', 'logout', 'register', 'subscribe',
94                           'unsubscribe', 'confirmaddress', 'recoverpassword',
95                           'invite', 'favor', 'disfavor', 'sup',
96                           'block', 'unblock', 'subedit',
97                           'groupblock', 'groupunblock');
98
99             foreach ($main as $a) {
100                 $m->connect('main/'.$a, array('action' => $a));
101             }
102
103             $m->connect('main/sup/:seconds', array('action' => 'sup'),
104                         array('seconds' => '[0-9]+'));
105
106             $m->connect('main/tagother/:id', array('action' => 'tagother'));
107
108             $m->connect('main/oembed',
109                         array('action' => 'oembed'));
110
111             // these take a code
112
113             foreach (array('register', 'confirmaddress', 'recoverpassword') as $c) {
114                 $m->connect('main/'.$c.'/:code', array('action' => $c));
115             }
116
117             // exceptional
118
119             $m->connect('main/remote', array('action' => 'remotesubscribe'));
120             $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+'));
121
122             foreach (Router::$bare as $action) {
123                 $m->connect('index.php?action=' . $action, array('action' => $action));
124             }
125
126             // settings
127
128             foreach (array('profile', 'avatar', 'password', 'im',
129                            'email', 'sms', 'userdesign', 'other') as $s) {
130                 $m->connect('settings/'.$s, array('action' => $s.'settings'));
131             }
132
133             // search
134
135             foreach (array('group', 'people', 'notice') as $s) {
136                 $m->connect('search/'.$s, array('action' => $s.'search'));
137                 $m->connect('search/'.$s.'?q=:q',
138                             array('action' => $s.'search'),
139                             array('q' => '.+'));
140             }
141
142             // The second of these is needed to make the link work correctly
143             // when inserted into the page. The first is needed to match the
144             // route on the way in. Seems to be another Net_URL_Mapper bug to me.
145             $m->connect('search/notice/rss', array('action' => 'noticesearchrss'));
146             $m->connect('search/notice/rss?q=:q', array('action' => 'noticesearchrss'),
147                         array('q' => '.+'));
148
149             $m->connect('attachment/:attachment',
150                         array('action' => 'attachment'),
151                         array('attachment' => '[0-9]+'));
152
153             $m->connect('attachment/:attachment/ajax',
154                         array('action' => 'attachment_ajax'),
155                         array('attachment' => '[0-9]+'));
156
157             $m->connect('attachment/:attachment/thumbnail',
158                         array('action' => 'attachment_thumbnail'),
159                         array('attachment' => '[0-9]+'));
160
161             $m->connect('notice/new', array('action' => 'newnotice'));
162             $m->connect('notice/new?replyto=:replyto',
163                         array('action' => 'newnotice'),
164                         array('replyto' => '[A-Za-z0-9_-]+'));
165             $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
166                         array('action' => 'newnotice'),
167                         array('replyto' => '[A-Za-z0-9_-]+'),
168                         array('inreplyto' => '[0-9]+'));
169
170             $m->connect('notice/:notice/file',
171                         array('action' => 'file'),
172                         array('notice' => '[0-9]+'));
173
174             $m->connect('notice/:notice',
175                         array('action' => 'shownotice'),
176                         array('notice' => '[0-9]+'));
177             $m->connect('notice/delete', array('action' => 'deletenotice'));
178             $m->connect('notice/delete/:notice',
179                         array('action' => 'deletenotice'),
180                         array('notice' => '[0-9]+'));
181
182             // conversation
183
184             $m->connect('conversation/:id',
185                         array('action' => 'conversation'),
186                         array('id' => '[0-9]+'));
187
188             $m->connect('message/new', array('action' => 'newmessage'));
189             $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+'));
190             $m->connect('message/:message',
191                         array('action' => 'showmessage'),
192                         array('message' => '[0-9]+'));
193
194             $m->connect('user/:id',
195                         array('action' => 'userbyid'),
196                         array('id' => '[0-9]+'));
197
198             $m->connect('tags/', array('action' => 'publictagcloud'));
199             $m->connect('tag/', array('action' => 'publictagcloud'));
200             $m->connect('tags', array('action' => 'publictagcloud'));
201             $m->connect('tag', array('action' => 'publictagcloud'));
202             $m->connect('tag/:tag/rss',
203                         array('action' => 'tagrss'),
204                         array('tag' => '[a-zA-Z0-9]+'));
205             $m->connect('tag/:tag',
206                         array('action' => 'tag'),
207                         array('tag' => '[\pL\pN_\-\.]{1,64}'));
208
209             $m->connect('peopletag/:tag',
210                         array('action' => 'peopletag'),
211                         array('tag' => '[a-zA-Z0-9]+'));
212
213             $m->connect('featured/', array('action' => 'featured'));
214             $m->connect('featured', array('action' => 'featured'));
215             $m->connect('favorited/', array('action' => 'favorited'));
216             $m->connect('favorited', array('action' => 'favorited'));
217
218             // groups
219
220             $m->connect('group/new', array('action' => 'newgroup'));
221
222             foreach (array('edit', 'join', 'leave') as $v) {
223                 $m->connect('group/:nickname/'.$v,
224                             array('action' => $v.'group'),
225                             array('nickname' => '[a-zA-Z0-9]+'));
226             }
227
228             foreach (array('members', 'logo', 'rss', 'designsettings') as $n) {
229                 $m->connect('group/:nickname/'.$n,
230                             array('action' => 'group'.$n),
231                             array('nickname' => '[a-zA-Z0-9]+'));
232             }
233
234             $m->connect('group/:nickname/foaf',
235                         array('action' => 'foafgroup'),
236                         array('nickname' => '[a-zA-Z0-9]+'));
237
238             $m->connect('group/:nickname/blocked',
239                         array('action' => 'blockedfromgroup'),
240                         array('nickname' => '[a-zA-Z0-9]+'));
241
242             $m->connect('group/:nickname/makeadmin',
243                         array('action' => 'makeadmin'),
244                         array('nickname' => '[a-zA-Z0-9]+'));
245
246             $m->connect('group/:id/id',
247                         array('action' => 'groupbyid'),
248                         array('id' => '[0-9]+'));
249
250             $m->connect('group/:nickname',
251                         array('action' => 'showgroup'),
252                         array('nickname' => '[a-zA-Z0-9]+'));
253
254             $m->connect('group/', array('action' => 'groups'));
255             $m->connect('group', array('action' => 'groups'));
256             $m->connect('groups/', array('action' => 'groups'));
257             $m->connect('groups', array('action' => 'groups'));
258
259             // Twitter-compatible API
260
261             // statuses API
262
263             $m->connect('api/statuses/public_timeline.:format',
264                         array('action' => 'ApiTimelinePublic',
265                               'format' => '(xml|json|rss|atom)'));
266
267             $m->connect('api/statuses/friends_timeline.:format',
268                         array('action' => 'ApiTimelineFriends',
269                               'format' => '(xml|json|rss|atom)'));
270
271             $m->connect('api/statuses/friends_timeline/:id.:format',
272                         array('action' => 'ApiTimelineFriends',
273                               'id' => '[a-zA-Z0-9]+',
274                               'format' => '(xml|json|rss|atom)'));
275             $m->connect('api/statuses/home_timeline.:format',
276                         array('action' => 'ApiTimelineFriends',
277                               'format' => '(xml|json|rss|atom)'));
278
279             $m->connect('api/statuses/home_timeline/:id.:format',
280                         array('action' => 'ApiTimelineFriends',
281                               'id' => '[a-zA-Z0-9]+',
282                               'format' => '(xml|json|rss|atom)'));
283
284             $m->connect('api/statuses/user_timeline.:format',
285                         array('action' => 'ApiTimelineUser',
286                               'format' => '(xml|json|rss|atom)'));
287
288             $m->connect('api/statuses/user_timeline/:id.:format',
289                         array('action' => 'ApiTimelineUser',
290                               'id' => '[a-zA-Z0-9]+',
291                               'format' => '(xml|json|rss|atom)'));
292
293             $m->connect('api/statuses/mentions.:format',
294                         array('action' => 'ApiTimelineMentions',
295                               'format' => '(xml|json|rss|atom)'));
296
297             $m->connect('api/statuses/mentions/:id.:format',
298                         array('action' => 'ApiTimelineMentions',
299                               'id' => '[a-zA-Z0-9]+',
300                               'format' => '(xml|json|rss|atom)'));
301
302             $m->connect('api/statuses/replies.:format',
303                         array('action' => 'ApiTimelineMentions',
304                               'format' => '(xml|json|rss|atom)'));
305
306             $m->connect('api/statuses/replies/:id.:format',
307                         array('action' => 'ApiTimelineMentions',
308                               'id' => '[a-zA-Z0-9]+',
309                               'format' => '(xml|json|rss|atom)'));
310
311             $m->connect('api/statuses/friends.:format',
312                         array('action' => 'ApiUserFriends',
313                               'format' => '(xml|json)'));
314
315             $m->connect('api/statuses/friends/:id.:format',
316                         array('action' => 'ApiUserFriends',
317                               'id' => '[a-zA-Z0-9]+',
318                               'format' => '(xml|json)'));
319
320             $m->connect('api/statuses/followers.:format',
321                         array('action' => 'ApiUserFollowers',
322                               'format' => '(xml|json)'));
323
324             $m->connect('api/statuses/followers/:id.:format',
325                         array('action' => 'ApiUserFollowers',
326                               'id' => '[a-zA-Z0-9]+',
327                               'format' => '(xml|json)'));
328
329             $m->connect('api/statuses/show.:format',
330                         array('action' => 'ApiStatusesShow',
331                               'format' => '(xml|json)'));
332
333             $m->connect('api/statuses/show/:id.:format',
334                         array('action' => 'ApiStatusesShow',
335                               'id' => '[0-9]+',
336                               'format' => '(xml|json)'));
337
338             $m->connect('api/statuses/update.:format',
339                         array('action' => 'ApiStatusesUpdate',
340                               'format' => '(xml|json)'));
341
342             $m->connect('api/statuses/destroy.:format',
343                         array('action' => 'ApiStatusesDestroy',
344                               'format' => '(xml|json)'));
345
346             $m->connect('api/statuses/destroy/:id.:format',
347                         array('action' => 'ApiStatusesDestroy',
348                               'id' => '[0-9]+',
349                               'format' => '(xml|json)'));
350
351             // users
352
353             $m->connect('api/users/show/:id.:format',
354                         array('action' => 'ApiUserShow',
355                               'id' => '[a-zA-Z0-9]+',
356                               'format' => '(xml|json)'));
357
358             $m->connect('api/users/:method',
359                         array('action' => 'api',
360                               'apiaction' => 'users'),
361                         array('method' => 'show(\.(xml|json))?'));
362
363             // direct messages
364
365             $m->connect('api/direct_messages.:format',
366                         array('action' => 'ApiDirectMessage',
367                               'format' => '(xml|json|rss|atom)'));
368
369             $m->connect('api/direct_messages/sent.:format',
370                         array('action' => 'ApiDirectMessage',
371                               'format' => '(xml|json|rss|atom)',
372                               'sent' => true));
373
374             $m->connect('api/direct_messages/new.:format',
375                         array('action' => 'ApiDirectMessageNew',
376                               'format' => '(xml|json)'));
377
378             // friendships
379
380             $m->connect('api/friendships/show.:format',
381                         array('action' => 'ApiFriendshipsShow',
382                               'format' => '(xml|json)'));
383
384             $m->connect('api/friendships/exists.:format',
385                         array('action' => 'ApiFriendshipsExists',
386                               'format' => '(xml|json)'));
387
388             $m->connect('api/friendships/create.:format',
389                         array('action' => 'ApiFriendshipsCreate',
390                               'format' => '(xml|json)'));
391
392             $m->connect('api/friendships/destroy.:format',
393                         array('action' => 'ApiFriendshipsDestroy',
394                               'format' => '(xml|json)'));
395
396             $m->connect('api/friendships/create/:id.:format',
397                         array('action' => 'ApiFriendshipsCreate',
398                               'id' => '[a-zA-Z0-9]+',
399                               'format' => '(xml|json)'));
400
401             $m->connect('api/friendships/destroy/:id.:format',
402                         array('action' => 'ApiFriendshipsDestroy',
403                               'id' => '[a-zA-Z0-9]+',
404                               'format' => '(xml|json)'));
405
406             // Social graph
407
408             $m->connect('api/friends/ids/:id.:format',
409                         array('action' => 'apiFriends',
410                               'ids_only' => true));
411
412             $m->connect('api/followers/ids/:id.:format',
413                         array('action' => 'apiFollowers',
414                               'ids_only' => true));
415
416             $m->connect('api/friends/ids.:format',
417                         array('action' => 'apiFriends',
418                               'ids_only' => true));
419
420             $m->connect('api/followers/ids.:format',
421                         array('action' => 'apiFollowers',
422                               'ids_only' => true));
423
424             // account
425
426             $m->connect('api/account/verify_credentials.:format',
427                         array('action' => 'ApiAccountVerifyCredentials'));
428
429             // special case where verify_credentials is called w/out a format
430
431             $m->connect('api/account/verify_credentials',
432                         array('action' => 'ApiAccountVerifyCredentials'));
433
434             $m->connect('api/account/rate_limit_status.:format',
435                         array('action' => 'ApiAccountRateLimitStatus'));
436
437             // favorites
438
439             $m->connect('api/favorites.:format',
440                         array('action' => 'ApiTimelineFavorites',
441                               'format' => '(xml|json|rss|atom)'));
442
443             $m->connect('api/favorites/:id.:format',
444                         array('action' => 'ApiTimelineFavorites',
445                               'id' => '[a-zA-Z0-9]+',
446                               'format' => '(xmljson|rss|atom)'));
447
448             $m->connect('api/favorites/create/:id.:format',
449                         array('action' => 'ApiFavoriteCreate',
450                               'id' => '[a-zA-Z0-9]+',
451                               'format' => '(xml|json)'));
452
453             $m->connect('api/favorites/destroy/:id.:format',
454                         array('action' => 'ApiFavoriteDestroy',
455                               'id' => '[a-zA-Z0-9]+',
456                               'format' => '(xml|json)'));
457
458             // notifications
459
460             $m->connect('api/notifications/:method/:argument',
461                         array('action' => 'api',
462                               'apiaction' => 'favorites'));
463
464             // blocks
465
466             $m->connect('api/blocks/create/:id.:format',
467                         array('action' => 'ApiBlockCreate',
468                               'id' => '[a-zA-Z0-9]+',
469                               'format' => '(xml|json)'));
470
471             $m->connect('api/blocks/destroy/:id.:format',
472                         array('action' => 'ApiBlockDestroy',
473                               'id' => '[a-zA-Z0-9]+',
474                               'format' => '(xml|json)'));
475             // help
476
477             $m->connect('api/help/test.:format',
478                         array('action' => 'ApiHelpTest',
479                               'format' => '(xml|json)'));
480
481             // statusnet
482
483             $m->connect('api/statusnet/version.:format',
484                         array('action' => 'ApiStatusnetVersion',
485                               'format' => '(xml|json)'));
486
487             $m->connect('api/statusnet/config.:format',
488                         array('action' => 'ApiStatusnetConfig',
489                               'format' => '(xml|json)'));
490
491             // For older methods, we provide "laconica" base action
492
493             $m->connect('api/laconica/version.:format',
494                         array('action' => 'ApiStatusnetVersion',
495                               'format' => '(xml|json)'));
496
497             $m->connect('api/laconica/config.:format',
498                         array('action' => 'ApiStatusnetConfig',
499                               'format' => '(xml|json)'));
500
501             // Groups and tags are newer than 0.8.1 so no backward-compatibility
502             // necessary
503
504             // Groups
505             //'list' has to be handled differently, as php will not allow a method to be named 'list'
506
507             $m->connect('api/statusnet/groups/timeline/:id.:format',
508                         array('action' => 'ApiTimelineGroup',
509                               'id' => '[a-zA-Z0-9]+',
510                               'format' => '(xmljson|rss|atom)'));
511
512             $m->connect('api/statusnet/groups/show.:format',
513                         array('action' => 'ApiGroupShow',
514                               'format' => '(xml|json)'));
515
516             $m->connect('api/statusnet/groups/show/:id.:format',
517                         array('action' => 'ApiGroupShow',
518                               'id' => '[a-zA-Z0-9]+',
519                               'format' => '(xml|json)'));
520
521             $m->connect('api/statusnet/groups/join.:format',
522                         array('action' => 'ApiGroupJoin',
523                               'id' => '[a-zA-Z0-9]+',
524                               'format' => '(xml|json)'));
525
526             $m->connect('api/statusnet/groups/join/:id.:format',
527                         array('action' => 'ApiGroupJoin',
528                               'format' => '(xml|json)'));
529
530             $m->connect('api/statusnet/groups/leave.:format',
531                         array('action' => 'ApiGroupLeave',
532                               'id' => '[a-zA-Z0-9]+',
533                               'format' => '(xml|json)'));
534
535             $m->connect('api/statusnet/groups/leave/:id.:format',
536                         array('action' => 'ApiGroupLeave',
537                               'format' => '(xml|json)'));
538
539             $m->connect('api/statusnet/groups/is_member.:format',
540                         array('action' => 'ApiGroupIsMember',
541                               'format' => '(xml|json)'));
542
543             $m->connect('api/statusnet/groups/list.:format',
544                         array('action' => 'ApiGroupList',
545                               'format' => '(xml|json|rss|atom)'));
546
547             $m->connect('api/statusnet/groups/list/:id.:format',
548                         array('action' => 'ApiGroupList',
549                               'id' => '[a-zA-Z0-9]+',
550                               'format' => '(xml|json|rss|atom)'));
551
552             $m->connect('api/statusnet/groups/list_all.:format',
553                         array('action' => 'ApiGroupListAll',
554                               'format' => '(xml|json|rss|atom)'));
555
556             $m->connect('api/statusnet/groups/membership.:format',
557                         array('action' => 'ApiGroupMembership',
558                               'format' => '(xml|json)'));
559
560             $m->connect('api/statusnet/groups/membership/:id.:format',
561                         array('action' => 'ApiGroupMembership',
562                               'id' => '[a-zA-Z0-9]+',
563                               'format' => '(xml|json)'));
564
565             $m->connect('api/statusnet/groups/create.:format',
566                         array('action' => 'ApiGroupCreate',
567                               'format' => '(xml|json)'));
568             // Tags
569             $m->connect('api/statusnet/tags/timeline/:tag.:format',
570                         array('action' => 'ApiTimelineTag',
571                               'format' => '(xmljson|rss|atom)'));
572
573             // search
574             $m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
575             $m->connect('api/search.json', array('action' => 'twitapisearchjson'));
576             $m->connect('api/trends.json', array('action' => 'twitapitrends'));
577
578             // user stuff
579
580             foreach (array('subscriptions', 'subscribers',
581                            'nudge', 'all', 'foaf', 'xrds',
582                            'replies', 'inbox', 'outbox', 'microsummary') as $a) {
583                 $m->connect(':nickname/'.$a,
584                             array('action' => $a),
585                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
586             }
587
588             foreach (array('subscriptions', 'subscribers') as $a) {
589                 $m->connect(':nickname/'.$a.'/:tag',
590                             array('action' => $a),
591                             array('tag' => '[a-zA-Z0-9]+',
592                                   'nickname' => '[a-zA-Z0-9]{1,64}'));
593             }
594
595             foreach (array('rss', 'groups') as $a) {
596                 $m->connect(':nickname/'.$a,
597                             array('action' => 'user'.$a),
598                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
599             }
600
601             foreach (array('all', 'replies', 'favorites') as $a) {
602                 $m->connect(':nickname/'.$a.'/rss',
603                             array('action' => $a.'rss'),
604                             array('nickname' => '[a-zA-Z0-9]{1,64}'));
605             }
606
607             $m->connect(':nickname/favorites',
608                         array('action' => 'showfavorites'),
609                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
610
611             $m->connect(':nickname/avatar/:size',
612                         array('action' => 'avatarbynickname'),
613                         array('size' => '(original|96|48|24)',
614                               'nickname' => '[a-zA-Z0-9]{1,64}'));
615
616             $m->connect(':nickname/tag/:tag/rss',
617                         array('action' => 'userrss'),
618                         array('nickname' => '[a-zA-Z0-9]{1,64}'),
619                         array('tag' => '[a-zA-Z0-9]+'));
620
621             $m->connect(':nickname/tag/:tag',
622                         array('action' => 'showstream'),
623                         array('nickname' => '[a-zA-Z0-9]{1,64}'),
624                         array('tag' => '[a-zA-Z0-9]+'));
625
626             $m->connect(':nickname',
627                         array('action' => 'showstream'),
628                         array('nickname' => '[a-zA-Z0-9]{1,64}'));
629
630             Event::handle('RouterInitialized', array($m));
631         }
632
633         return $m;
634     }
635
636     function map($path)
637     {
638         try {
639             $match = $this->m->match($path);
640         } catch (Net_URL_Mapper_InvalidException $e) {
641             common_log(LOG_ERR, "Problem getting route for $path - " .
642                        $e->getMessage());
643             $cac = new ClientErrorAction("Page not found.", 404);
644             $cac->showPage();
645         }
646
647         return $match;
648     }
649
650     function build($action, $args=null, $params=null, $fragment=null)
651     {
652         $action_arg = array('action' => $action);
653
654         if ($args) {
655             $args = array_merge($action_arg, $args);
656         } else {
657             $args = $action_arg;
658         }
659
660         $url = $this->m->generate($args, $params, $fragment);
661
662         // Due to a bug in the Net_URL_Mapper code, the returned URL may
663         // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
664         // repair that here rather than modifying the upstream code...
665
666         $qpos = strpos($url, '?');
667         if ($qpos !== false) {
668             $url = substr($url, 0, $qpos+1) .
669               str_replace('?', '&', substr($url, $qpos+1));
670         }
671         return $url;
672     }
673 }