router.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * URL Router
  32. *
  33. * Cheap wrapper around Net_URL_Mapper
  34. *
  35. * @category URL
  36. * @package StatusNet
  37. * @author Evan Prodromou <evan@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. */
  41. class Router
  42. {
  43. var $m = null;
  44. static $inst = null;
  45. const REGEX_TAG = '[^\/]+'; // [\pL\pN_\-\.]{1,64} better if we can do unicode regexes
  46. static function get()
  47. {
  48. if (!Router::$inst) {
  49. Router::$inst = new Router();
  50. }
  51. return Router::$inst;
  52. }
  53. /**
  54. * Clear the global singleton instance for this class.
  55. * Needed to ensure reset when switching site configurations.
  56. */
  57. static function clear()
  58. {
  59. Router::$inst = null;
  60. }
  61. function __construct()
  62. {
  63. if (empty($this->m)) {
  64. $this->m = $this->initialize();
  65. }
  66. }
  67. /**
  68. * Create a unique hashkey for the router.
  69. *
  70. * The router's url map can change based on the version of the software
  71. * you're running and the plugins that are enabled. To avoid having bad routes
  72. * get stuck in the cache, the key includes a list of plugins and the software
  73. * version.
  74. *
  75. * There can still be problems with a) differences in versions of the plugins and
  76. * b) people running code between official versions, but these tend to be more
  77. * sophisticated users who can grok what's going on and clear their caches.
  78. *
  79. * @return string cache key string that should uniquely identify a router
  80. */
  81. static function cacheKey()
  82. {
  83. $parts = array('router');
  84. // Many router paths depend on this setting.
  85. if (common_config('singleuser', 'enabled')) {
  86. $parts[] = '1user';
  87. } else {
  88. $parts[] = 'multi';
  89. }
  90. return Cache::codeKey(implode(':', $parts));
  91. }
  92. function initialize()
  93. {
  94. $m = new URLMapper();
  95. if (Event::handle('StartInitializeRouter', [&$m])) {
  96. // top of the menu hierarchy, sometimes "Home"
  97. $m->connect('', ['action' => 'top']);
  98. // public endpoints
  99. $m->connect('robots.txt', ['action' => 'robotstxt']);
  100. $m->connect('opensearch/people',
  101. ['action' => 'opensearch',
  102. 'type' => 'people']);
  103. $m->connect('opensearch/notice',
  104. ['action' => 'opensearch',
  105. 'type' => 'notice']);
  106. // docs
  107. $m->connect('doc/:title', ['action' => 'doc']);
  108. $m->connect('main/otp/:user_id/:token',
  109. ['action' => 'otp'],
  110. ['user_id' => '[0-9]+',
  111. 'token' => '.+']);
  112. // these take a code; before the main part
  113. foreach (['register', 'confirmaddress', 'recoverpassword'] as $c) {
  114. $m->connect('main/'.$c.'/:code', ['action' => $c]);
  115. }
  116. // Also need a block variant accepting ID on URL for mail links
  117. $m->connect('main/block/:profileid',
  118. ['action' => 'block'],
  119. ['profileid' => '[0-9]+']);
  120. $m->connect('main/sup/:seconds',
  121. ['action' => 'sup'],
  122. ['seconds' => '[0-9]+']);
  123. // main stuff is repetitive
  124. $main = ['login', 'logout', 'register', 'subscribe',
  125. 'unsubscribe', 'cancelsubscription', 'approvesub',
  126. 'confirmaddress', 'recoverpassword',
  127. 'invite', 'sup',
  128. 'block', 'unblock', 'subedit',
  129. 'groupblock', 'groupunblock',
  130. 'sandbox', 'unsandbox',
  131. 'silence', 'unsilence',
  132. 'grantrole', 'revokerole',
  133. 'deleteuser',
  134. 'geocode',
  135. 'version',
  136. 'backupaccount',
  137. 'deleteaccount',
  138. 'restoreaccount',
  139. 'top',
  140. 'public'];
  141. foreach ($main as $a) {
  142. $m->connect('main/'.$a, ['action' => $a]);
  143. }
  144. $m->connect('main/all', ['action' => 'networkpublic']);
  145. $m->connect('main/tagprofile/:id',
  146. ['action' => 'tagprofile'],
  147. ['id' => '[0-9]+']);
  148. $m->connect('main/tagprofile', ['action' => 'tagprofile']);
  149. $m->connect('main/xrds',
  150. ['action' => 'publicxrds']);
  151. // settings
  152. foreach (['profile', 'avatar', 'password', 'im', 'oauthconnections',
  153. 'oauthapps', 'email', 'sms', 'url'] as $s) {
  154. $m->connect('settings/'.$s, ['action' => $s.'settings']);
  155. }
  156. if (common_config('oldschool', 'enabled')) {
  157. $m->connect('settings/oldschool', ['action' => 'oldschoolsettings']);
  158. }
  159. $m->connect('settings/oauthapps/show/:id',
  160. ['action' => 'showapplication'],
  161. ['id' => '[0-9]+']);
  162. $m->connect('settings/oauthapps/new',
  163. ['action' => 'newapplication']);
  164. $m->connect('settings/oauthapps/edit/:id',
  165. ['action' => 'editapplication'],
  166. ['id' => '[0-9]+']);
  167. $m->connect('settings/oauthapps/delete/:id',
  168. ['action' => 'deleteapplication'],
  169. ['id' => '[0-9]+']);
  170. // search
  171. foreach (['group', 'people', 'notice'] as $s) {
  172. $m->connect('search/'.$s.'?q=:q',
  173. ['action' => $s.'search'],
  174. ['q' => '.+']);
  175. $m->connect('search/'.$s, ['action' => $s.'search']);
  176. }
  177. // The second of these is needed to make the link work correctly
  178. // when inserted into the page. The first is needed to match the
  179. // route on the way in. Seems to be another Net_URL_Mapper bug to me.
  180. $m->connect('search/notice/rss?q=:q',
  181. ['action' => 'noticesearchrss'],
  182. ['q' => '.+']);
  183. $m->connect('search/notice/rss', ['action' => 'noticesearchrss']);
  184. foreach (['' => 'attachment',
  185. '/view' => 'attachment_view',
  186. '/download' => 'attachment_download',
  187. '/thumbnail' => 'attachment_thumbnail'] as $postfix => $action) {
  188. foreach (['filehash' => '[A-Za-z0-9._-]{64}',
  189. 'attachment' => '[0-9]+'] as $type => $match) {
  190. $m->connect("attachment/:{$type}{$postfix}",
  191. ['action' => $action],
  192. [$type => $match]);
  193. }
  194. }
  195. $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
  196. ['action' => 'newnotice'],
  197. ['replyto' => Nickname::DISPLAY_FMT,
  198. 'inreplyto' => '[0-9]+']);
  199. $m->connect('notice/new?replyto=:replyto',
  200. ['action' => 'newnotice'],
  201. ['replyto' => Nickname::DISPLAY_FMT]);
  202. $m->connect('notice/new', ['action' => 'newnotice']);
  203. $m->connect('notice/:notice',
  204. ['action' => 'shownotice'],
  205. ['notice' => '[0-9]+']);
  206. $m->connect('notice/:notice/delete',
  207. ['action' => 'deletenotice'],
  208. ['notice' => '[0-9]+']);
  209. // conversation
  210. $m->connect('conversation/:id',
  211. ['action' => 'conversation'],
  212. ['id' => '[0-9]+']);
  213. $m->connect('user/:id',
  214. ['action' => 'userbyid'],
  215. ['id' => '[0-9]+']);
  216. $m->connect('tag/:tag/rss',
  217. ['action' => 'tagrss'],
  218. ['tag' => self::REGEX_TAG]);
  219. $m->connect('tag/:tag',
  220. ['action' => 'tag'],
  221. ['tag' => self::REGEX_TAG]);
  222. // groups
  223. $m->connect('group/new', ['action' => 'newgroup']);
  224. foreach (['edit', 'join', 'leave', 'delete', 'cancel', 'approve'] as $v) {
  225. $m->connect('group/:nickname/'.$v,
  226. ['action' => $v.'group'],
  227. ['nickname' => Nickname::DISPLAY_FMT]);
  228. $m->connect('group/:id/id/'.$v,
  229. ['action' => $v.'group'],
  230. ['id' => '[0-9]+']);
  231. }
  232. foreach (['members', 'logo', 'rss'] as $n) {
  233. $m->connect('group/:nickname/'.$n,
  234. ['action' => 'group'.$n],
  235. ['nickname' => Nickname::DISPLAY_FMT]);
  236. }
  237. $m->connect('group/:nickname/foaf',
  238. ['action' => 'foafgroup'],
  239. ['nickname' => Nickname::DISPLAY_FMT]);
  240. $m->connect('group/:nickname/blocked',
  241. ['action' => 'blockedfromgroup'],
  242. ['nickname' => Nickname::DISPLAY_FMT]);
  243. $m->connect('group/:nickname/makeadmin',
  244. ['action' => 'makeadmin'],
  245. ['nickname' => Nickname::DISPLAY_FMT]);
  246. $m->connect('group/:nickname/members/pending',
  247. ['action' => 'groupqueue'],
  248. ['nickname' => Nickname::DISPLAY_FMT]);
  249. $m->connect('group/:id/id',
  250. ['action' => 'groupbyid'],
  251. ['id' => '[0-9]+']);
  252. $m->connect('group/:nickname',
  253. ['action' => 'showgroup'],
  254. ['nickname' => Nickname::DISPLAY_FMT]);
  255. $m->connect('group/:nickname/',
  256. ['action' => 'showgroup'],
  257. ['nickname' => Nickname::DISPLAY_FMT]);
  258. $m->connect('group/', ['action' => 'groups']);
  259. $m->connect('group', ['action' => 'groups']);
  260. $m->connect('groups/', ['action' => 'groups']);
  261. $m->connect('groups', ['action' => 'groups']);
  262. // Twitter-compatible API
  263. // statuses API
  264. $m->connect('api',
  265. ['action' => 'Redirect',
  266. 'nextAction' => 'doc',
  267. 'args' => ['title' => 'api']]);
  268. $m->connect('api/statuses/public_timeline.:format',
  269. ['action' => 'ApiTimelinePublic'],
  270. ['format' => '(xml|json|rss|atom|as)']);
  271. // this is not part of the Twitter API. Also may require authentication depending on server config!
  272. $m->connect('api/statuses/networkpublic_timeline.:format',
  273. ['action' => 'ApiTimelineNetworkPublic'],
  274. ['format' => '(xml|json|rss|atom|as)']);
  275. $m->connect('api/statuses/friends_timeline/:id.:format',
  276. ['action' => 'ApiTimelineFriends'],
  277. ['id' => Nickname::INPUT_FMT,
  278. 'format' => '(xml|json|rss|atom|as)']);
  279. $m->connect('api/statuses/friends_timeline.:format',
  280. ['action' => 'ApiTimelineFriends'],
  281. ['format' => '(xml|json|rss|atom|as)']);
  282. $m->connect('api/statuses/home_timeline/:id.:format',
  283. ['action' => 'ApiTimelineHome'],
  284. ['id' => Nickname::INPUT_FMT,
  285. 'format' => '(xml|json|rss|atom|as)']);
  286. $m->connect('api/statuses/home_timeline.:format',
  287. ['action' => 'ApiTimelineHome'],
  288. ['format' => '(xml|json|rss|atom|as)']);
  289. $m->connect('api/statuses/user_timeline/:id.:format',
  290. ['action' => 'ApiTimelineUser'],
  291. ['id' => Nickname::INPUT_FMT,
  292. 'format' => '(xml|json|rss|atom|as)']);
  293. $m->connect('api/statuses/user_timeline.:format',
  294. ['action' => 'ApiTimelineUser'],
  295. ['format' => '(xml|json|rss|atom|as)']);
  296. $m->connect('api/statuses/mentions/:id.:format',
  297. ['action' => 'ApiTimelineMentions'],
  298. ['id' => Nickname::INPUT_FMT,
  299. 'format' => '(xml|json|rss|atom|as)']);
  300. $m->connect('api/statuses/mentions.:format',
  301. ['action' => 'ApiTimelineMentions'],
  302. ['format' => '(xml|json|rss|atom|as)']);
  303. $m->connect('api/statuses/replies/:id.:format',
  304. ['action' => 'ApiTimelineMentions'],
  305. ['id' => Nickname::INPUT_FMT,
  306. 'format' => '(xml|json|rss|atom|as)']);
  307. $m->connect('api/statuses/replies.:format',
  308. ['action' => 'ApiTimelineMentions'],
  309. ['format' => '(xml|json|rss|atom|as)']);
  310. $m->connect('api/statuses/mentions_timeline/:id.:format',
  311. ['action' => 'ApiTimelineMentions'],
  312. ['id' => Nickname::INPUT_FMT,
  313. 'format' => '(xml|json|rss|atom|as)']);
  314. $m->connect('api/statuses/mentions_timeline.:format',
  315. ['action' => 'ApiTimelineMentions'],
  316. ['format' => '(xml|json|rss|atom|as)']);
  317. $m->connect('api/statuses/friends/:id.:format',
  318. ['action' => 'ApiUserFriends'],
  319. ['id' => Nickname::INPUT_FMT,
  320. 'format' => '(xml|json)']);
  321. $m->connect('api/statuses/friends.:format',
  322. ['action' => 'ApiUserFriends'],
  323. ['format' => '(xml|json)']);
  324. $m->connect('api/statuses/followers/:id.:format',
  325. ['action' => 'ApiUserFollowers'],
  326. ['id' => Nickname::INPUT_FMT,
  327. 'format' => '(xml|json)']);
  328. $m->connect('api/statuses/followers.:format',
  329. ['action' => 'ApiUserFollowers'],
  330. ['format' => '(xml|json)']);
  331. $m->connect('api/statuses/show/:id.:format',
  332. ['action' => 'ApiStatusesShow'],
  333. ['id' => '[0-9]+',
  334. 'format' => '(xml|json|atom)']);
  335. $m->connect('api/statuses/show.:format',
  336. ['action' => 'ApiStatusesShow'],
  337. ['format' => '(xml|json|atom)']);
  338. $m->connect('api/statuses/update.:format',
  339. ['action' => 'ApiStatusesUpdate'],
  340. ['format' => '(xml|json|atom)']);
  341. $m->connect('api/statuses/destroy/:id.:format',
  342. ['action' => 'ApiStatusesDestroy'],
  343. ['id' => '[0-9]+',
  344. 'format' => '(xml|json)']);
  345. $m->connect('api/statuses/destroy.:format',
  346. ['action' => 'ApiStatusesDestroy'],
  347. ['format' => '(xml|json)']);
  348. // START qvitter API additions
  349. $m->connect('api/attachment/:id.:format',
  350. ['action' => 'ApiAttachment'],
  351. ['id' => '[0-9]+',
  352. 'format' => '(xml|json)']);
  353. $m->connect('api/checkhub.:format',
  354. ['action' => 'ApiCheckHub'],
  355. ['format' => '(xml|json)']);
  356. $m->connect('api/externalprofile/show.:format',
  357. ['action' => 'ApiExternalProfileShow'],
  358. ['format' => '(xml|json)']);
  359. $m->connect('api/statusnet/groups/admins/:id.:format',
  360. ['action' => 'ApiGroupAdmins'],
  361. ['id' => Nickname::INPUT_FMT,
  362. 'format' => '(xml|json)']);
  363. $m->connect('api/account/update_link_color.:format',
  364. ['action' => 'ApiAccountUpdateLinkColor'],
  365. ['format' => '(xml|json)']);
  366. $m->connect('api/account/update_background_color.:format',
  367. ['action' => 'ApiAccountUpdateBackgroundColor'],
  368. ['format' => '(xml|json)']);
  369. $m->connect('api/account/register.:format',
  370. ['action' => 'ApiAccountRegister'],
  371. ['format' => '(xml|json)']);
  372. $m->connect('api/check_nickname.:format',
  373. ['action' => 'ApiCheckNickname'],
  374. ['format' => '(xml|json)']);
  375. // END qvitter API additions
  376. // users
  377. $m->connect('api/users/show/:id.:format',
  378. ['action' => 'ApiUserShow'],
  379. ['id' => Nickname::INPUT_FMT,
  380. 'format' => '(xml|json)']);
  381. $m->connect('api/users/show.:format',
  382. ['action' => 'ApiUserShow'],
  383. ['format' => '(xml|json)']);
  384. $m->connect('api/users/profile_image/:screen_name.:format',
  385. ['action' => 'ApiUserProfileImage'],
  386. ['screen_name' => Nickname::DISPLAY_FMT,
  387. 'format' => '(xml|json)']);
  388. // friendships
  389. $m->connect('api/friendships/show.:format',
  390. ['action' => 'ApiFriendshipsShow'],
  391. ['format' => '(xml|json)']);
  392. $m->connect('api/friendships/exists.:format',
  393. ['action' => 'ApiFriendshipsExists'],
  394. ['format' => '(xml|json)']);
  395. $m->connect('api/friendships/create/:id.:format',
  396. ['action' => 'ApiFriendshipsCreate'],
  397. ['id' => Nickname::INPUT_FMT,
  398. 'format' => '(xml|json)']);
  399. $m->connect('api/friendships/create.:format',
  400. ['action' => 'ApiFriendshipsCreate'],
  401. ['format' => '(xml|json)']);
  402. $m->connect('api/friendships/destroy/:id.:format',
  403. ['action' => 'ApiFriendshipsDestroy'],
  404. ['id' => Nickname::INPUT_FMT,
  405. 'format' => '(xml|json)']);
  406. $m->connect('api/friendships/destroy.:format',
  407. ['action' => 'ApiFriendshipsDestroy'],
  408. ['format' => '(xml|json)']);
  409. // Social graph
  410. $m->connect('api/friends/ids/:id.:format',
  411. ['action' => 'ApiUserFriends',
  412. 'ids_only' => true],
  413. ['id' => Nickname::INPUT_FMT,
  414. 'format' => '(xml|json)']);
  415. $m->connect('api/followers/ids/:id.:format',
  416. ['action' => 'ApiUserFollowers',
  417. 'ids_only' => true],
  418. ['id' => Nickname::INPUT_FMT,
  419. 'format' => '(xml|json)']);
  420. $m->connect('api/friends/ids.:format',
  421. ['action' => 'ApiUserFriends',
  422. 'ids_only' => true],
  423. ['format' => '(xml|json)']);
  424. $m->connect('api/followers/ids.:format',
  425. ['action' => 'ApiUserFollowers',
  426. 'ids_only' => true],
  427. ['format' => '(xml|json)']);
  428. // account
  429. $m->connect('api/account/verify_credentials.:format',
  430. ['action' => 'ApiAccountVerifyCredentials'],
  431. ['format' => '(xml|json)']);
  432. $m->connect('api/account/update_profile.:format',
  433. ['action' => 'ApiAccountUpdateProfile'],
  434. ['format' => '(xml|json)']);
  435. $m->connect('api/account/update_profile_image.:format',
  436. ['action' => 'ApiAccountUpdateProfileImage'],
  437. ['format' => '(xml|json)']);
  438. $m->connect('api/account/update_delivery_device.:format',
  439. ['action' => 'ApiAccountUpdateDeliveryDevice'],
  440. ['format' => '(xml|json)']);
  441. // special case where verify_credentials is called w/out a format
  442. $m->connect('api/account/verify_credentials',
  443. ['action' => 'ApiAccountVerifyCredentials']);
  444. $m->connect('api/account/rate_limit_status.:format',
  445. ['action' => 'ApiAccountRateLimitStatus'],
  446. ['format' => '(xml|json)']);
  447. // blocks
  448. $m->connect('api/blocks/create/:id.:format',
  449. ['action' => 'ApiBlockCreate'],
  450. ['id' => Nickname::INPUT_FMT,
  451. 'format' => '(xml|json)']);
  452. $m->connect('api/blocks/create.:format',
  453. ['action' => 'ApiBlockCreate'],
  454. ['format' => '(xml|json)']);
  455. $m->connect('api/blocks/destroy/:id.:format',
  456. ['action' => 'ApiBlockDestroy'],
  457. ['id' => Nickname::INPUT_FMT,
  458. 'format' => '(xml|json)']);
  459. $m->connect('api/blocks/destroy.:format',
  460. ['action' => 'ApiBlockDestroy'],
  461. ['format' => '(xml|json)']);
  462. // help
  463. $m->connect('api/help/test.:format',
  464. ['action' => 'ApiHelpTest'],
  465. ['format' => '(xml|json)']);
  466. // statusnet
  467. $m->connect('api/statusnet/version.:format',
  468. ['action' => 'ApiGNUsocialVersion'],
  469. ['format' => '(xml|json)']);
  470. $m->connect('api/statusnet/config.:format',
  471. ['action' => 'ApiGNUsocialConfig'],
  472. ['format' => '(xml|json)']);
  473. // For our current software name, we provide "gnusocial" base action
  474. $m->connect('api/gnusocial/version.:format',
  475. ['action' => 'ApiGNUsocialVersion'],
  476. ['format' => '(xml|json)']);
  477. $m->connect('api/gnusocial/config.:format',
  478. ['action' => 'ApiGNUsocialConfig'],
  479. ['format' => '(xml|json)']);
  480. // Groups and tags are newer than 0.8.1 so no backward-compatibility
  481. // necessary
  482. // Groups
  483. //'list' has to be handled differently, as php will not allow a method to be named 'list'
  484. $m->connect('api/statusnet/groups/timeline/:id.:format',
  485. ['action' => 'ApiTimelineGroup'],
  486. ['id' => Nickname::INPUT_FMT,
  487. 'format' => '(xml|json|rss|atom|as)']);
  488. $m->connect('api/statusnet/groups/show/:id.:format',
  489. ['action' => 'ApiGroupShow'],
  490. ['id' => Nickname::INPUT_FMT,
  491. 'format' => '(xml|json)']);
  492. $m->connect('api/statusnet/groups/show.:format',
  493. ['action' => 'ApiGroupShow'],
  494. ['format' => '(xml|json)']);
  495. $m->connect('api/statusnet/groups/join/:id.:format',
  496. ['action' => 'ApiGroupJoin'],
  497. ['id' => Nickname::INPUT_FMT,
  498. 'format' => '(xml|json)']);
  499. $m->connect('api/statusnet/groups/join.:format',
  500. ['action' => 'ApiGroupJoin'],
  501. ['format' => '(xml|json)']);
  502. $m->connect('api/statusnet/groups/leave/:id.:format',
  503. ['action' => 'ApiGroupLeave'],
  504. ['id' => Nickname::INPUT_FMT,
  505. 'format' => '(xml|json)']);
  506. $m->connect('api/statusnet/groups/leave.:format',
  507. ['action' => 'ApiGroupLeave'],
  508. ['format' => '(xml|json)']);
  509. $m->connect('api/statusnet/groups/is_member.:format',
  510. ['action' => 'ApiGroupIsMember'],
  511. ['format' => '(xml|json)']);
  512. $m->connect('api/statusnet/groups/list/:id.:format',
  513. ['action' => 'ApiGroupList'],
  514. ['id' => Nickname::INPUT_FMT,
  515. 'format' => '(xml|json|rss|atom)']);
  516. $m->connect('api/statusnet/groups/list.:format',
  517. ['action' => 'ApiGroupList'],
  518. ['format' => '(xml|json|rss|atom)']);
  519. $m->connect('api/statusnet/groups/list_all.:format',
  520. ['action' => 'ApiGroupListAll'],
  521. ['format' => '(xml|json|rss|atom)']);
  522. $m->connect('api/statusnet/groups/membership/:id.:format',
  523. ['action' => 'ApiGroupMembership'],
  524. ['id' => Nickname::INPUT_FMT,
  525. 'format' => '(xml|json)']);
  526. $m->connect('api/statusnet/groups/membership.:format',
  527. ['action' => 'ApiGroupMembership'],
  528. ['format' => '(xml|json)']);
  529. $m->connect('api/statusnet/groups/create.:format',
  530. ['action' => 'ApiGroupCreate'],
  531. ['format' => '(xml|json)']);
  532. $m->connect('api/statusnet/groups/update/:id.:format',
  533. ['action' => 'ApiGroupProfileUpdate'],
  534. ['id' => '[a-zA-Z0-9]+',
  535. 'format' => '(xml|json)']);
  536. $m->connect('api/statusnet/conversation/:id.:format',
  537. ['action' => 'apiconversation'],
  538. ['id' => '[0-9]+',
  539. 'format' => '(xml|json|rss|atom|as)']);
  540. // Lists (people tags)
  541. $m->connect('api/lists/list.:format',
  542. ['action' => 'ApiListSubscriptions'],
  543. ['format' => '(xml|json)']);
  544. $m->connect('api/lists/memberships.:format',
  545. ['action' => 'ApiListMemberships'],
  546. ['format' => '(xml|json)']);
  547. $m->connect('api/:user/lists/memberships.:format',
  548. ['action' => 'ApiListMemberships'],
  549. ['user' => '[a-zA-Z0-9]+',
  550. 'format' => '(xml|json)']);
  551. $m->connect('api/lists/subscriptions.:format',
  552. ['action' => 'ApiListSubscriptions'],
  553. ['format' => '(xml|json)']);
  554. $m->connect('api/:user/lists/subscriptions.:format',
  555. ['action' => 'ApiListSubscriptions'],
  556. ['user' => '[a-zA-Z0-9]+',
  557. 'format' => '(xml|json)']);
  558. $m->connect('api/lists.:format',
  559. ['action' => 'ApiLists'],
  560. ['format' => '(xml|json)']);
  561. $m->connect('api/:user/lists/:id.:format',
  562. ['action' => 'ApiList'],
  563. ['user' => '[a-zA-Z0-9]+',
  564. 'id' => '[a-zA-Z0-9]+',
  565. 'format' => '(xml|json)']);
  566. $m->connect('api/:user/lists.:format',
  567. ['action' => 'ApiLists'],
  568. ['user' => '[a-zA-Z0-9]+',
  569. 'format' => '(xml|json)']);
  570. $m->connect('api/:user/lists/:id/statuses.:format',
  571. ['action' => 'ApiTimelineList'],
  572. ['user' => '[a-zA-Z0-9]+',
  573. 'id' => '[a-zA-Z0-9]+',
  574. 'format' => '(xml|json|rss|atom)']);
  575. $m->connect('api/:user/:list_id/members/:id.:format',
  576. ['action' => 'ApiListMember'],
  577. ['user' => '[a-zA-Z0-9]+',
  578. 'list_id' => '[a-zA-Z0-9]+',
  579. 'id' => '[a-zA-Z0-9]+',
  580. 'format' => '(xml|json)']);
  581. $m->connect('api/:user/:list_id/members.:format',
  582. ['action' => 'ApiListMembers'],
  583. ['user' => '[a-zA-Z0-9]+',
  584. 'list_id' => '[a-zA-Z0-9]+',
  585. 'format' => '(xml|json)']);
  586. $m->connect('api/:user/:list_id/subscribers/:id.:format',
  587. ['action' => 'ApiListSubscriber'],
  588. ['user' => '[a-zA-Z0-9]+',
  589. 'list_id' => '[a-zA-Z0-9]+',
  590. 'id' => '[a-zA-Z0-9]+',
  591. 'format' => '(xml|json)']);
  592. $m->connect('api/:user/:list_id/subscribers.:format',
  593. ['action' => 'ApiListSubscribers'],
  594. ['user' => '[a-zA-Z0-9]+',
  595. 'list_id' => '[a-zA-Z0-9]+',
  596. 'format' => '(xml|json)']);
  597. // Tags
  598. $m->connect('api/statusnet/tags/timeline/:tag.:format',
  599. ['action' => 'ApiTimelineTag'],
  600. ['tag' => self::REGEX_TAG,
  601. 'format' => '(xml|json|rss|atom|as)']);
  602. // media related
  603. $m->connect('api/statusnet/media/upload',
  604. ['action' => 'ApiMediaUpload']);
  605. $m->connect('api/statuses/update_with_media.json',
  606. ['action' => 'ApiMediaUpload']);
  607. // Twitter Media upload API v1.1
  608. $m->connect('api/media/upload.:format',
  609. ['action' => 'ApiMediaUpload'],
  610. ['format' => '(xml|json)']);
  611. // search
  612. $m->connect('api/search.atom', ['action' => 'ApiSearchAtom']);
  613. $m->connect('api/search.json', ['action' => 'ApiSearchJSON']);
  614. $m->connect('api/trends.json', ['action' => 'ApiTrends']);
  615. $m->connect('api/oauth/request_token',
  616. ['action' => 'ApiOAuthRequestToken']);
  617. $m->connect('api/oauth/access_token',
  618. ['action' => 'ApiOAuthAccessToken']);
  619. $m->connect('api/oauth/authorize',
  620. ['action' => 'ApiOAuthAuthorize']);
  621. // Admin
  622. $m->connect('panel/site', ['action' => 'siteadminpanel']);
  623. $m->connect('panel/user', ['action' => 'useradminpanel']);
  624. $m->connect('panel/access', ['action' => 'accessadminpanel']);
  625. $m->connect('panel/paths', ['action' => 'pathsadminpanel']);
  626. $m->connect('panel/sessions', ['action' => 'sessionsadminpanel']);
  627. $m->connect('panel/sitenotice', ['action' => 'sitenoticeadminpanel']);
  628. $m->connect('panel/license', ['action' => 'licenseadminpanel']);
  629. $m->connect('panel/plugins', ['action' => 'pluginsadminpanel']);
  630. $m->connect('panel/plugins/enable/:plugin',
  631. ['action' => 'pluginenable'],
  632. ['plugin' => '[A-Za-z0-9_]+']);
  633. $m->connect('panel/plugins/disable/:plugin',
  634. ['action' => 'plugindisable'],
  635. ['plugin' => '[A-Za-z0-9_]+']);
  636. $m->connect('panel/plugins/delete/:plugin',
  637. ['action' => 'plugindelete'],
  638. ['plugin' => '[A-Za-z0-9_]+']);
  639. $m->connect('panel/plugins/install',
  640. ['action' => 'plugininstall']);
  641. // Common people-tag stuff
  642. $m->connect('peopletag/:tag',
  643. ['action' => 'peopletag'],
  644. ['tag' => self::REGEX_TAG]);
  645. $m->connect('selftag/:tag',
  646. ['action' => 'selftag'],
  647. ['tag' => self::REGEX_TAG]);
  648. $m->connect('main/addpeopletag', ['action' => 'addpeopletag']);
  649. $m->connect('main/removepeopletag', ['action' => 'removepeopletag']);
  650. $m->connect('main/profilecompletion', ['action' => 'profilecompletion']);
  651. $m->connect('main/peopletagautocomplete', ['action' => 'peopletagautocomplete']);
  652. // In the "root"
  653. if (common_config('singleuser', 'enabled')) {
  654. $nickname = User::singleUserNickname();
  655. foreach (['subscriptions', 'subscribers', 'all', 'foaf', 'replies'] as $a) {
  656. $m->connect($a,
  657. ['action' => $a,
  658. 'nickname' => $nickname]);
  659. }
  660. foreach (['subscriptions', 'subscribers'] as $a) {
  661. $m->connect($a.'/:tag',
  662. ['action' => $a,
  663. 'nickname' => $nickname],
  664. ['tag' => self::REGEX_TAG]);
  665. }
  666. $m->connect('subscribers/pending',
  667. ['action' => 'subqueue',
  668. 'nickname' => $nickname]);
  669. foreach (['rss', 'groups'] as $a) {
  670. $m->connect($a,
  671. ['action' => 'user'.$a,
  672. 'nickname' => $nickname]);
  673. }
  674. foreach (['all', 'replies'] as $a) {
  675. $m->connect($a.'/rss',
  676. ['action' => $a.'rss',
  677. 'nickname' => $nickname]);
  678. }
  679. $m->connect('avatar',
  680. ['action' => 'avatarbynickname',
  681. 'nickname' => $nickname]);
  682. $m->connect('avatar/:size',
  683. ['action' => 'avatarbynickname',
  684. 'nickname' => $nickname],
  685. ['size' => '(|original|\d+)']);
  686. $m->connect('tag/:tag/rss',
  687. ['action' => 'userrss',
  688. 'nickname' => $nickname],
  689. ['tag' => self::REGEX_TAG]);
  690. $m->connect('tag/:tag',
  691. ['action' => 'showstream',
  692. 'nickname' => $nickname],
  693. ['tag' => self::REGEX_TAG]);
  694. $m->connect('rsd.xml',
  695. ['action' => 'rsd',
  696. 'nickname' => $nickname]);
  697. // peopletags
  698. $m->connect('peopletags',
  699. ['action' => 'peopletagsbyuser']);
  700. $m->connect('peopletags/private',
  701. ['action' => 'peopletagsbyuser',
  702. 'private' => 1]);
  703. $m->connect('peopletags/public',
  704. ['action' => 'peopletagsbyuser',
  705. 'public' => 1]);
  706. $m->connect('othertags',
  707. ['action' => 'peopletagsforuser']);
  708. $m->connect('peopletagsubscriptions',
  709. ['action' => 'peopletagsubscriptions']);
  710. $m->connect('all/:tag/subscribers',
  711. ['action' => 'peopletagsubscribers'],
  712. ['tag' => self::REGEX_TAG]);
  713. $m->connect('all/:tag/tagged',
  714. ['action' => 'peopletagged'],
  715. ['tag' => self::REGEX_TAG]);
  716. $m->connect('all/:tag/edit',
  717. ['action' => 'editpeopletag'],
  718. ['tag' => self::REGEX_TAG]);
  719. foreach (['subscribe', 'unsubscribe'] as $v) {
  720. $m->connect('peopletag/:id/'.$v,
  721. ['action' => $v.'peopletag'],
  722. ['id' => '[0-9]{1,64}']);
  723. }
  724. $m->connect('user/:tagger_id/profiletag/:id/id',
  725. ['action' => 'profiletagbyid'],
  726. ['tagger_id' => '[0-9]+',
  727. 'id' => '[0-9]+']);
  728. $m->connect('all/:tag',
  729. ['action' => 'showprofiletag',
  730. 'tagger' => $nickname],
  731. ['tag' => self::REGEX_TAG]);
  732. foreach (['subscriptions', 'subscribers'] as $a) {
  733. $m->connect($a.'/:tag',
  734. ['action' => $a],
  735. ['tag' => self::REGEX_TAG]);
  736. }
  737. }
  738. $m->connect('rss', ['action' => 'publicrss']);
  739. $m->connect('featuredrss', ['action' => 'featuredrss']);
  740. $m->connect('featured/', ['action' => 'featured']);
  741. $m->connect('featured', ['action' => 'featured']);
  742. $m->connect('rsd.xml', ['action' => 'rsd']);
  743. foreach (['subscriptions', 'subscribers',
  744. 'nudge', 'all', 'foaf', 'replies',
  745. 'inbox', 'outbox'] as $a) {
  746. $m->connect(':nickname/'.$a,
  747. ['action' => $a],
  748. ['nickname' => Nickname::DISPLAY_FMT]);
  749. }
  750. $m->connect(':nickname/subscribers/pending',
  751. ['action' => 'subqueue'],
  752. ['nickname' => Nickname::DISPLAY_FMT]);
  753. // some targeted RSS 1.0 actions (extends TargetedRss10Action)
  754. foreach (['all', 'replies'] as $a) {
  755. $m->connect(':nickname/'.$a.'/rss',
  756. ['action' => $a.'rss'],
  757. ['nickname' => Nickname::DISPLAY_FMT]);
  758. }
  759. // people tags
  760. $m->connect(':nickname/peopletags',
  761. ['action' => 'peopletagsbyuser'],
  762. ['nickname' => Nickname::DISPLAY_FMT]);
  763. $m->connect(':nickname/peopletags/private',
  764. ['action' => 'peopletagsbyuser',
  765. 'private' => 1],
  766. ['nickname' => Nickname::DISPLAY_FMT]);
  767. $m->connect(':nickname/peopletags/public',
  768. ['action' => 'peopletagsbyuser',
  769. 'public' => 1],
  770. ['nickname' => Nickname::DISPLAY_FMT]);
  771. $m->connect(':nickname/othertags',
  772. ['action' => 'peopletagsforuser'],
  773. ['nickname' => Nickname::DISPLAY_FMT]);
  774. $m->connect(':nickname/peopletagsubscriptions',
  775. ['action' => 'peopletagsubscriptions'],
  776. ['nickname' => Nickname::DISPLAY_FMT]);
  777. $m->connect(':tagger/all/:tag/subscribers',
  778. ['action' => 'peopletagsubscribers'],
  779. ['tagger' => Nickname::DISPLAY_FMT,
  780. 'tag' => self::REGEX_TAG]);
  781. $m->connect(':tagger/all/:tag/tagged',
  782. ['action' => 'peopletagged'],
  783. ['tagger' => Nickname::DISPLAY_FMT,
  784. 'tag' => self::REGEX_TAG]);
  785. $m->connect(':tagger/all/:tag/edit',
  786. ['action' => 'editpeopletag'],
  787. ['tagger' => Nickname::DISPLAY_FMT,
  788. 'tag' => self::REGEX_TAG]);
  789. foreach (['subscribe', 'unsubscribe'] as $v) {
  790. $m->connect('peopletag/:id/'.$v,
  791. ['action' => $v.'peopletag'],
  792. ['id' => '[0-9]{1,64}']);
  793. }
  794. $m->connect('user/:tagger_id/profiletag/:id/id',
  795. ['action' => 'profiletagbyid'],
  796. ['tagger_id' => '[0-9]+',
  797. 'id' => '[0-9]+']);
  798. $m->connect(':nickname/all/:tag',
  799. ['action' => 'showprofiletag'],
  800. ['nickname' => Nickname::DISPLAY_FMT,
  801. 'tag' => self::REGEX_TAG]);
  802. foreach (['subscriptions', 'subscribers'] as $a) {
  803. $m->connect(':nickname/'.$a.'/:tag',
  804. ['action' => $a],
  805. ['tag' => self::REGEX_TAG,
  806. 'nickname' => Nickname::DISPLAY_FMT]);
  807. }
  808. foreach (['rss', 'groups'] as $a) {
  809. $m->connect(':nickname/'.$a,
  810. ['action' => 'user'.$a],
  811. ['nickname' => Nickname::DISPLAY_FMT]);
  812. }
  813. $m->connect(':nickname/avatar',
  814. ['action' => 'avatarbynickname'],
  815. ['nickname' => Nickname::DISPLAY_FMT]);
  816. $m->connect(':nickname/avatar/:size',
  817. ['action' => 'avatarbynickname'],
  818. ['size' => '(|original|\d+)',
  819. 'nickname' => Nickname::DISPLAY_FMT]);
  820. $m->connect(':nickname/tag/:tag/rss',
  821. ['action' => 'userrss'],
  822. ['nickname' => Nickname::DISPLAY_FMT,
  823. 'tag' => self::REGEX_TAG]);
  824. $m->connect(':nickname/tag/:tag',
  825. ['action' => 'showstream'],
  826. ['nickname' => Nickname::DISPLAY_FMT,
  827. 'tag' => self::REGEX_TAG]);
  828. $m->connect(':nickname/rsd.xml',
  829. ['action' => 'rsd'],
  830. ['nickname' => Nickname::DISPLAY_FMT]);
  831. $m->connect(':nickname',
  832. ['action' => 'showstream'],
  833. ['nickname' => Nickname::DISPLAY_FMT]);
  834. $m->connect(':nickname/',
  835. ['action' => 'showstream'],
  836. ['nickname' => Nickname::DISPLAY_FMT]);
  837. // AtomPub API
  838. $m->connect('api/statusnet/app/service/:id.xml',
  839. ['action' => 'ApiAtomService'],
  840. ['id' => Nickname::DISPLAY_FMT]);
  841. $m->connect('api/statusnet/app/service.xml',
  842. ['action' => 'ApiAtomService']);
  843. $m->connect('api/statusnet/app/subscriptions/:subscriber/:subscribed.atom',
  844. ['action' => 'AtomPubShowSubscription'],
  845. ['subscriber' => '[0-9]+',
  846. 'subscribed' => '[0-9]+']);
  847. $m->connect('api/statusnet/app/subscriptions/:subscriber.atom',
  848. ['action' => 'AtomPubSubscriptionFeed'],
  849. ['subscriber' => '[0-9]+']);
  850. $m->connect('api/statusnet/app/memberships/:profile/:group.atom',
  851. ['action' => 'AtomPubShowMembership'],
  852. ['profile' => '[0-9]+',
  853. 'group' => '[0-9]+']);
  854. $m->connect('api/statusnet/app/memberships/:profile.atom',
  855. ['action' => 'AtomPubMembershipFeed'],
  856. ['profile' => '[0-9]+']);
  857. // URL shortening
  858. $m->connect('url/:id',
  859. ['action' => 'redirecturl'],
  860. ['id' => '[0-9]+']);
  861. // user stuff
  862. Event::handle('RouterInitialized', [$m]);
  863. }
  864. return $m;
  865. }
  866. function map($path)
  867. {
  868. try {
  869. return $this->m->match($path);
  870. } catch (NoRouteMapException $e) {
  871. common_debug($e->getMessage());
  872. // TRANS: Client error on action trying to visit a non-existing page.
  873. throw new ClientException(_('Page not found.'), 404);
  874. }
  875. }
  876. function build($action, $args=null, $params=null, $fragment=null)
  877. {
  878. $action_arg = array('action' => $action);
  879. if ($args) {
  880. $args = array_merge($action_arg, $args);
  881. } else {
  882. $args = $action_arg;
  883. }
  884. $url = $this->m->generate($args, $params, $fragment);
  885. // Due to a bug in the Net_URL_Mapper code, the returned URL may
  886. // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
  887. // repair that here rather than modifying the upstream code...
  888. $qpos = strpos($url, '?');
  889. if ($qpos !== false) {
  890. $url = substr($url, 0, $qpos+1) .
  891. str_replace('?', '&', substr($url, $qpos+1));
  892. // @fixme this is a hacky workaround for http_build_query in the
  893. // lower-level code and bad configs that set the default separator
  894. // to &amp; instead of &. Encoded &s in parameters will not be
  895. // affected.
  896. $url = substr($url, 0, $qpos+1) .
  897. str_replace('&amp;', '&', substr($url, $qpos+1));
  898. }
  899. return $url;
  900. }
  901. }