router.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. // Attachment page for file
  185. $m->connect("attachment/:attachment",
  186. ['action' => 'attachment'],
  187. ['attachment' => '[0-9]+']);
  188. // Retrieve thumbnail
  189. $m->connect("thumbnail/:attachment",
  190. ['action' => 'attachment_thumbnail'],
  191. ['attachment' => '[0-9]+']);
  192. // Retrieve local file
  193. foreach (['/view' => 'attachment_view',
  194. '/download' => 'attachment_download'] as $postfix => $action) {
  195. $m->connect("attachment/:filehash{$postfix}",
  196. ['action' => $action],
  197. ['filehash' => '[A-Za-z0-9._-]{64}']);
  198. }
  199. $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
  200. ['action' => 'newnotice'],
  201. ['replyto' => Nickname::DISPLAY_FMT,
  202. 'inreplyto' => '[0-9]+']);
  203. $m->connect('notice/new?replyto=:replyto',
  204. ['action' => 'newnotice'],
  205. ['replyto' => Nickname::DISPLAY_FMT]);
  206. $m->connect('notice/new', ['action' => 'newnotice']);
  207. $m->connect('notice/:notice',
  208. ['action' => 'shownotice'],
  209. ['notice' => '[0-9]+']);
  210. $m->connect('notice/:notice/delete',
  211. ['action' => 'deletenotice'],
  212. ['notice' => '[0-9]+']);
  213. // conversation
  214. $m->connect('conversation/:id',
  215. ['action' => 'conversation'],
  216. ['id' => '[0-9]+']);
  217. $m->connect('user/:id',
  218. ['action' => 'userbyid'],
  219. ['id' => '[0-9]+']);
  220. $m->connect('tag/:tag/rss',
  221. ['action' => 'tagrss'],
  222. ['tag' => self::REGEX_TAG]);
  223. $m->connect('tag/:tag',
  224. ['action' => 'tag'],
  225. ['tag' => self::REGEX_TAG]);
  226. // groups
  227. $m->connect('group/new', ['action' => 'newgroup']);
  228. foreach (['edit', 'join', 'leave', 'delete', 'cancel', 'approve'] as $v) {
  229. $m->connect('group/:nickname/'.$v,
  230. ['action' => $v.'group'],
  231. ['nickname' => Nickname::DISPLAY_FMT]);
  232. $m->connect('group/:id/id/'.$v,
  233. ['action' => $v.'group'],
  234. ['id' => '[0-9]+']);
  235. }
  236. foreach (['members', 'logo', 'rss'] as $n) {
  237. $m->connect('group/:nickname/'.$n,
  238. ['action' => 'group'.$n],
  239. ['nickname' => Nickname::DISPLAY_FMT]);
  240. }
  241. $m->connect('group/:nickname/foaf',
  242. ['action' => 'foafgroup'],
  243. ['nickname' => Nickname::DISPLAY_FMT]);
  244. $m->connect('group/:nickname/blocked',
  245. ['action' => 'blockedfromgroup'],
  246. ['nickname' => Nickname::DISPLAY_FMT]);
  247. $m->connect('group/:nickname/makeadmin',
  248. ['action' => 'makeadmin'],
  249. ['nickname' => Nickname::DISPLAY_FMT]);
  250. $m->connect('group/:nickname/members/pending',
  251. ['action' => 'groupqueue'],
  252. ['nickname' => Nickname::DISPLAY_FMT]);
  253. $m->connect('group/:id/id',
  254. ['action' => 'groupbyid'],
  255. ['id' => '[0-9]+']);
  256. $m->connect('group/:nickname',
  257. ['action' => 'showgroup'],
  258. ['nickname' => Nickname::DISPLAY_FMT]);
  259. $m->connect('group/:nickname/',
  260. ['action' => 'showgroup'],
  261. ['nickname' => Nickname::DISPLAY_FMT]);
  262. $m->connect('group/', ['action' => 'groups']);
  263. $m->connect('group', ['action' => 'groups']);
  264. $m->connect('groups/', ['action' => 'groups']);
  265. $m->connect('groups', ['action' => 'groups']);
  266. // Twitter-compatible API
  267. // statuses API
  268. $m->connect('api',
  269. ['action' => 'Redirect',
  270. 'nextAction' => 'doc',
  271. 'args' => ['title' => 'api']]);
  272. $m->connect('api/statuses/public_timeline.:format',
  273. ['action' => 'ApiTimelinePublic'],
  274. ['format' => '(xml|json|rss|atom|as)']);
  275. // this is not part of the Twitter API. Also may require authentication depending on server config!
  276. $m->connect('api/statuses/networkpublic_timeline.:format',
  277. ['action' => 'ApiTimelineNetworkPublic'],
  278. ['format' => '(xml|json|rss|atom|as)']);
  279. $m->connect('api/statuses/friends_timeline/:id.:format',
  280. ['action' => 'ApiTimelineFriends'],
  281. ['id' => Nickname::INPUT_FMT,
  282. 'format' => '(xml|json|rss|atom|as)']);
  283. $m->connect('api/statuses/friends_timeline.:format',
  284. ['action' => 'ApiTimelineFriends'],
  285. ['format' => '(xml|json|rss|atom|as)']);
  286. $m->connect('api/statuses/home_timeline/:id.:format',
  287. ['action' => 'ApiTimelineHome'],
  288. ['id' => Nickname::INPUT_FMT,
  289. 'format' => '(xml|json|rss|atom|as)']);
  290. $m->connect('api/statuses/home_timeline.:format',
  291. ['action' => 'ApiTimelineHome'],
  292. ['format' => '(xml|json|rss|atom|as)']);
  293. $m->connect('api/statuses/user_timeline/:id.:format',
  294. ['action' => 'ApiTimelineUser'],
  295. ['id' => Nickname::INPUT_FMT,
  296. 'format' => '(xml|json|rss|atom|as)']);
  297. $m->connect('api/statuses/user_timeline.:format',
  298. ['action' => 'ApiTimelineUser'],
  299. ['format' => '(xml|json|rss|atom|as)']);
  300. $m->connect('api/statuses/mentions/:id.:format',
  301. ['action' => 'ApiTimelineMentions'],
  302. ['id' => Nickname::INPUT_FMT,
  303. 'format' => '(xml|json|rss|atom|as)']);
  304. $m->connect('api/statuses/mentions.:format',
  305. ['action' => 'ApiTimelineMentions'],
  306. ['format' => '(xml|json|rss|atom|as)']);
  307. $m->connect('api/statuses/replies/:id.:format',
  308. ['action' => 'ApiTimelineMentions'],
  309. ['id' => Nickname::INPUT_FMT,
  310. 'format' => '(xml|json|rss|atom|as)']);
  311. $m->connect('api/statuses/replies.:format',
  312. ['action' => 'ApiTimelineMentions'],
  313. ['format' => '(xml|json|rss|atom|as)']);
  314. $m->connect('api/statuses/mentions_timeline/:id.:format',
  315. ['action' => 'ApiTimelineMentions'],
  316. ['id' => Nickname::INPUT_FMT,
  317. 'format' => '(xml|json|rss|atom|as)']);
  318. $m->connect('api/statuses/mentions_timeline.:format',
  319. ['action' => 'ApiTimelineMentions'],
  320. ['format' => '(xml|json|rss|atom|as)']);
  321. $m->connect('api/statuses/friends/:id.:format',
  322. ['action' => 'ApiUserFriends'],
  323. ['id' => Nickname::INPUT_FMT,
  324. 'format' => '(xml|json)']);
  325. $m->connect('api/statuses/friends.:format',
  326. ['action' => 'ApiUserFriends'],
  327. ['format' => '(xml|json)']);
  328. $m->connect('api/statuses/followers/:id.:format',
  329. ['action' => 'ApiUserFollowers'],
  330. ['id' => Nickname::INPUT_FMT,
  331. 'format' => '(xml|json)']);
  332. $m->connect('api/statuses/followers.:format',
  333. ['action' => 'ApiUserFollowers'],
  334. ['format' => '(xml|json)']);
  335. $m->connect('api/statuses/show/:id.:format',
  336. ['action' => 'ApiStatusesShow'],
  337. ['id' => '[0-9]+',
  338. 'format' => '(xml|json|atom)']);
  339. $m->connect('api/statuses/show.:format',
  340. ['action' => 'ApiStatusesShow'],
  341. ['format' => '(xml|json|atom)']);
  342. $m->connect('api/statuses/update.:format',
  343. ['action' => 'ApiStatusesUpdate'],
  344. ['format' => '(xml|json|atom)']);
  345. $m->connect('api/statuses/destroy/:id.:format',
  346. ['action' => 'ApiStatusesDestroy'],
  347. ['id' => '[0-9]+',
  348. 'format' => '(xml|json)']);
  349. $m->connect('api/statuses/destroy.:format',
  350. ['action' => 'ApiStatusesDestroy'],
  351. ['format' => '(xml|json)']);
  352. // START qvitter API additions
  353. $m->connect('api/attachment/:id.:format',
  354. ['action' => 'ApiAttachment'],
  355. ['id' => '[0-9]+',
  356. 'format' => '(xml|json)']);
  357. $m->connect('api/checkhub.:format',
  358. ['action' => 'ApiCheckHub'],
  359. ['format' => '(xml|json)']);
  360. $m->connect('api/externalprofile/show.:format',
  361. ['action' => 'ApiExternalProfileShow'],
  362. ['format' => '(xml|json)']);
  363. $m->connect('api/statusnet/groups/admins/:id.:format',
  364. ['action' => 'ApiGroupAdmins'],
  365. ['id' => Nickname::INPUT_FMT,
  366. 'format' => '(xml|json)']);
  367. $m->connect('api/account/update_link_color.:format',
  368. ['action' => 'ApiAccountUpdateLinkColor'],
  369. ['format' => '(xml|json)']);
  370. $m->connect('api/account/update_background_color.:format',
  371. ['action' => 'ApiAccountUpdateBackgroundColor'],
  372. ['format' => '(xml|json)']);
  373. $m->connect('api/account/register.:format',
  374. ['action' => 'ApiAccountRegister'],
  375. ['format' => '(xml|json)']);
  376. $m->connect('api/check_nickname.:format',
  377. ['action' => 'ApiCheckNickname'],
  378. ['format' => '(xml|json)']);
  379. // END qvitter API additions
  380. // users
  381. $m->connect('api/users/show/:id.:format',
  382. ['action' => 'ApiUserShow'],
  383. ['id' => Nickname::INPUT_FMT,
  384. 'format' => '(xml|json)']);
  385. $m->connect('api/users/show.:format',
  386. ['action' => 'ApiUserShow'],
  387. ['format' => '(xml|json)']);
  388. $m->connect('api/users/profile_image/:screen_name.:format',
  389. ['action' => 'ApiUserProfileImage'],
  390. ['screen_name' => Nickname::DISPLAY_FMT,
  391. 'format' => '(xml|json)']);
  392. // friendships
  393. $m->connect('api/friendships/show.:format',
  394. ['action' => 'ApiFriendshipsShow'],
  395. ['format' => '(xml|json)']);
  396. $m->connect('api/friendships/exists.:format',
  397. ['action' => 'ApiFriendshipsExists'],
  398. ['format' => '(xml|json)']);
  399. $m->connect('api/friendships/create/:id.:format',
  400. ['action' => 'ApiFriendshipsCreate'],
  401. ['id' => Nickname::INPUT_FMT,
  402. 'format' => '(xml|json)']);
  403. $m->connect('api/friendships/create.:format',
  404. ['action' => 'ApiFriendshipsCreate'],
  405. ['format' => '(xml|json)']);
  406. $m->connect('api/friendships/destroy/:id.:format',
  407. ['action' => 'ApiFriendshipsDestroy'],
  408. ['id' => Nickname::INPUT_FMT,
  409. 'format' => '(xml|json)']);
  410. $m->connect('api/friendships/destroy.:format',
  411. ['action' => 'ApiFriendshipsDestroy'],
  412. ['format' => '(xml|json)']);
  413. // Social graph
  414. $m->connect('api/friends/ids/:id.:format',
  415. ['action' => 'ApiUserFriends',
  416. 'ids_only' => true],
  417. ['id' => Nickname::INPUT_FMT,
  418. 'format' => '(xml|json)']);
  419. $m->connect('api/followers/ids/:id.:format',
  420. ['action' => 'ApiUserFollowers',
  421. 'ids_only' => true],
  422. ['id' => Nickname::INPUT_FMT,
  423. 'format' => '(xml|json)']);
  424. $m->connect('api/friends/ids.:format',
  425. ['action' => 'ApiUserFriends',
  426. 'ids_only' => true],
  427. ['format' => '(xml|json)']);
  428. $m->connect('api/followers/ids.:format',
  429. ['action' => 'ApiUserFollowers',
  430. 'ids_only' => true],
  431. ['format' => '(xml|json)']);
  432. // account
  433. $m->connect('api/account/verify_credentials.:format',
  434. ['action' => 'ApiAccountVerifyCredentials'],
  435. ['format' => '(xml|json)']);
  436. $m->connect('api/account/update_profile.:format',
  437. ['action' => 'ApiAccountUpdateProfile'],
  438. ['format' => '(xml|json)']);
  439. $m->connect('api/account/update_profile_image.:format',
  440. ['action' => 'ApiAccountUpdateProfileImage'],
  441. ['format' => '(xml|json)']);
  442. $m->connect('api/account/update_delivery_device.:format',
  443. ['action' => 'ApiAccountUpdateDeliveryDevice'],
  444. ['format' => '(xml|json)']);
  445. // special case where verify_credentials is called w/out a format
  446. $m->connect('api/account/verify_credentials',
  447. ['action' => 'ApiAccountVerifyCredentials']);
  448. $m->connect('api/account/rate_limit_status.:format',
  449. ['action' => 'ApiAccountRateLimitStatus'],
  450. ['format' => '(xml|json)']);
  451. $m->connect('api/account/delete/:id.:format',
  452. ['action' => 'ApiAccountDelete'],
  453. ['id' => Nickname::INPUT_FMT,
  454. 'format' => '(xml|json)']);
  455. $m->connect('api/account/delete.:format',
  456. ['action' => 'ApiAccountDelete'],
  457. ['format' => '(xml|json)']);
  458. // blocks
  459. $m->connect('api/blocks/create/:id.:format',
  460. ['action' => 'ApiBlockCreate'],
  461. ['id' => Nickname::INPUT_FMT,
  462. 'format' => '(xml|json)']);
  463. $m->connect('api/blocks/create.:format',
  464. ['action' => 'ApiBlockCreate'],
  465. ['format' => '(xml|json)']);
  466. $m->connect('api/blocks/destroy/:id.:format',
  467. ['action' => 'ApiBlockDestroy'],
  468. ['id' => Nickname::INPUT_FMT,
  469. 'format' => '(xml|json)']);
  470. $m->connect('api/blocks/destroy.:format',
  471. ['action' => 'ApiBlockDestroy'],
  472. ['format' => '(xml|json)']);
  473. // help
  474. $m->connect('api/help/test.:format',
  475. ['action' => 'ApiHelpTest'],
  476. ['format' => '(xml|json)']);
  477. // statusnet
  478. $m->connect('api/statusnet/version.:format',
  479. ['action' => 'ApiGNUsocialVersion'],
  480. ['format' => '(xml|json)']);
  481. $m->connect('api/statusnet/config.:format',
  482. ['action' => 'ApiGNUsocialConfig'],
  483. ['format' => '(xml|json)']);
  484. // For our current software name, we provide "gnusocial" base action
  485. $m->connect('api/gnusocial/version.:format',
  486. ['action' => 'ApiGNUsocialVersion'],
  487. ['format' => '(xml|json)']);
  488. $m->connect('api/gnusocial/config.:format',
  489. ['action' => 'ApiGNUsocialConfig'],
  490. ['format' => '(xml|json)']);
  491. // Groups and tags are newer than 0.8.1 so no backward-compatibility
  492. // necessary
  493. // Groups
  494. //'list' has to be handled differently, as php will not allow a method to be named 'list'
  495. $m->connect('api/statusnet/groups/timeline/:id.:format',
  496. ['action' => 'ApiTimelineGroup'],
  497. ['id' => Nickname::INPUT_FMT,
  498. 'format' => '(xml|json|rss|atom|as)']);
  499. $m->connect('api/statusnet/groups/show/:id.:format',
  500. ['action' => 'ApiGroupShow'],
  501. ['id' => Nickname::INPUT_FMT,
  502. 'format' => '(xml|json)']);
  503. $m->connect('api/statusnet/groups/show.:format',
  504. ['action' => 'ApiGroupShow'],
  505. ['format' => '(xml|json)']);
  506. $m->connect('api/statusnet/groups/join/:id.:format',
  507. ['action' => 'ApiGroupJoin'],
  508. ['id' => Nickname::INPUT_FMT,
  509. 'format' => '(xml|json)']);
  510. $m->connect('api/statusnet/groups/join.:format',
  511. ['action' => 'ApiGroupJoin'],
  512. ['format' => '(xml|json)']);
  513. $m->connect('api/statusnet/groups/leave/:id.:format',
  514. ['action' => 'ApiGroupLeave'],
  515. ['id' => Nickname::INPUT_FMT,
  516. 'format' => '(xml|json)']);
  517. $m->connect('api/statusnet/groups/leave.:format',
  518. ['action' => 'ApiGroupLeave'],
  519. ['format' => '(xml|json)']);
  520. $m->connect('api/statusnet/groups/is_member.:format',
  521. ['action' => 'ApiGroupIsMember'],
  522. ['format' => '(xml|json)']);
  523. $m->connect('api/statusnet/groups/list/:id.:format',
  524. ['action' => 'ApiGroupList'],
  525. ['id' => Nickname::INPUT_FMT,
  526. 'format' => '(xml|json|rss|atom)']);
  527. $m->connect('api/statusnet/groups/list.:format',
  528. ['action' => 'ApiGroupList'],
  529. ['format' => '(xml|json|rss|atom)']);
  530. $m->connect('api/statusnet/groups/list_all.:format',
  531. ['action' => 'ApiGroupListAll'],
  532. ['format' => '(xml|json|rss|atom)']);
  533. $m->connect('api/statusnet/groups/membership/:id.:format',
  534. ['action' => 'ApiGroupMembership'],
  535. ['id' => Nickname::INPUT_FMT,
  536. 'format' => '(xml|json)']);
  537. $m->connect('api/statusnet/groups/membership.:format',
  538. ['action' => 'ApiGroupMembership'],
  539. ['format' => '(xml|json)']);
  540. $m->connect('api/statusnet/groups/create.:format',
  541. ['action' => 'ApiGroupCreate'],
  542. ['format' => '(xml|json)']);
  543. $m->connect('api/statusnet/groups/update/:id.:format',
  544. ['action' => 'ApiGroupProfileUpdate'],
  545. ['id' => '[a-zA-Z0-9]+',
  546. 'format' => '(xml|json)']);
  547. $m->connect('api/statusnet/conversation/:id.:format',
  548. ['action' => 'apiconversation'],
  549. ['id' => '[0-9]+',
  550. 'format' => '(xml|json|rss|atom|as)']);
  551. // Lists (people tags)
  552. $m->connect('api/lists/list.:format',
  553. ['action' => 'ApiListSubscriptions'],
  554. ['format' => '(xml|json)']);
  555. $m->connect('api/lists/memberships.:format',
  556. ['action' => 'ApiListMemberships'],
  557. ['format' => '(xml|json)']);
  558. $m->connect('api/:user/lists/memberships.:format',
  559. ['action' => 'ApiListMemberships'],
  560. ['user' => '[a-zA-Z0-9]+',
  561. 'format' => '(xml|json)']);
  562. $m->connect('api/lists/subscriptions.:format',
  563. ['action' => 'ApiListSubscriptions'],
  564. ['format' => '(xml|json)']);
  565. $m->connect('api/:user/lists/subscriptions.:format',
  566. ['action' => 'ApiListSubscriptions'],
  567. ['user' => '[a-zA-Z0-9]+',
  568. 'format' => '(xml|json)']);
  569. $m->connect('api/lists.:format',
  570. ['action' => 'ApiLists'],
  571. ['format' => '(xml|json)']);
  572. $m->connect('api/:user/lists/:id.:format',
  573. ['action' => 'ApiList'],
  574. ['user' => '[a-zA-Z0-9]+',
  575. 'id' => '[a-zA-Z0-9]+',
  576. 'format' => '(xml|json)']);
  577. $m->connect('api/:user/lists.:format',
  578. ['action' => 'ApiLists'],
  579. ['user' => '[a-zA-Z0-9]+',
  580. 'format' => '(xml|json)']);
  581. $m->connect('api/:user/lists/:id/statuses.:format',
  582. ['action' => 'ApiTimelineList'],
  583. ['user' => '[a-zA-Z0-9]+',
  584. 'id' => '[a-zA-Z0-9]+',
  585. 'format' => '(xml|json|rss|atom)']);
  586. $m->connect('api/:user/:list_id/members/:id.:format',
  587. ['action' => 'ApiListMember'],
  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/members.:format',
  593. ['action' => 'ApiListMembers'],
  594. ['user' => '[a-zA-Z0-9]+',
  595. 'list_id' => '[a-zA-Z0-9]+',
  596. 'format' => '(xml|json)']);
  597. $m->connect('api/:user/:list_id/subscribers/:id.:format',
  598. ['action' => 'ApiListSubscriber'],
  599. ['user' => '[a-zA-Z0-9]+',
  600. 'list_id' => '[a-zA-Z0-9]+',
  601. 'id' => '[a-zA-Z0-9]+',
  602. 'format' => '(xml|json)']);
  603. $m->connect('api/:user/:list_id/subscribers.:format',
  604. ['action' => 'ApiListSubscribers'],
  605. ['user' => '[a-zA-Z0-9]+',
  606. 'list_id' => '[a-zA-Z0-9]+',
  607. 'format' => '(xml|json)']);
  608. // Tags
  609. $m->connect('api/statusnet/tags/timeline/:tag.:format',
  610. ['action' => 'ApiTimelineTag'],
  611. ['tag' => self::REGEX_TAG,
  612. 'format' => '(xml|json|rss|atom|as)']);
  613. // media related
  614. $m->connect('api/statusnet/media/upload',
  615. ['action' => 'ApiMediaUpload']);
  616. $m->connect('api/statuses/update_with_media.json',
  617. ['action' => 'ApiMediaUpload']);
  618. // Twitter Media upload API v1.1
  619. $m->connect('api/media/upload.:format',
  620. ['action' => 'ApiMediaUpload'],
  621. ['format' => '(xml|json)']);
  622. // search
  623. $m->connect('api/search.atom', ['action' => 'ApiSearchAtom']);
  624. $m->connect('api/search.json', ['action' => 'ApiSearchJSON']);
  625. $m->connect('api/trends.json', ['action' => 'ApiTrends']);
  626. $m->connect('api/oauth/request_token',
  627. ['action' => 'ApiOAuthRequestToken']);
  628. $m->connect('api/oauth/access_token',
  629. ['action' => 'ApiOAuthAccessToken']);
  630. $m->connect('api/oauth/authorize',
  631. ['action' => 'ApiOAuthAuthorize']);
  632. // Admin
  633. $m->connect('panel/site', ['action' => 'siteadminpanel']);
  634. $m->connect('panel/user', ['action' => 'useradminpanel']);
  635. $m->connect('panel/access', ['action' => 'accessadminpanel']);
  636. $m->connect('panel/paths', ['action' => 'pathsadminpanel']);
  637. $m->connect('panel/sessions', ['action' => 'sessionsadminpanel']);
  638. $m->connect('panel/sitenotice', ['action' => 'sitenoticeadminpanel']);
  639. $m->connect('panel/license', ['action' => 'licenseadminpanel']);
  640. $m->connect('panel/plugins', ['action' => 'pluginsadminpanel']);
  641. $m->connect('panel/plugins/enable/:plugin',
  642. ['action' => 'pluginenable'],
  643. ['plugin' => '[A-Za-z0-9_]+']);
  644. $m->connect('panel/plugins/disable/:plugin',
  645. ['action' => 'plugindisable'],
  646. ['plugin' => '[A-Za-z0-9_]+']);
  647. $m->connect('panel/plugins/delete/:plugin',
  648. ['action' => 'plugindelete'],
  649. ['plugin' => '[A-Za-z0-9_]+']);
  650. $m->connect('panel/plugins/install',
  651. ['action' => 'plugininstall']);
  652. // Common people-tag stuff
  653. $m->connect('peopletag/:tag',
  654. ['action' => 'peopletag'],
  655. ['tag' => self::REGEX_TAG]);
  656. $m->connect('selftag/:tag',
  657. ['action' => 'selftag'],
  658. ['tag' => self::REGEX_TAG]);
  659. $m->connect('main/addpeopletag', ['action' => 'addpeopletag']);
  660. $m->connect('main/removepeopletag', ['action' => 'removepeopletag']);
  661. $m->connect('main/profilecompletion', ['action' => 'profilecompletion']);
  662. $m->connect('main/peopletagautocomplete', ['action' => 'peopletagautocomplete']);
  663. // In the "root"
  664. if (common_config('singleuser', 'enabled')) {
  665. $nickname = User::singleUserNickname();
  666. foreach (['subscriptions', 'subscribers', 'all', 'foaf', 'replies'] as $a) {
  667. $m->connect($a,
  668. ['action' => $a,
  669. 'nickname' => $nickname]);
  670. }
  671. foreach (['subscriptions', 'subscribers'] as $a) {
  672. $m->connect($a.'/:tag',
  673. ['action' => $a,
  674. 'nickname' => $nickname],
  675. ['tag' => self::REGEX_TAG]);
  676. }
  677. $m->connect('subscribers/pending',
  678. ['action' => 'subqueue',
  679. 'nickname' => $nickname]);
  680. foreach (['rss', 'groups'] as $a) {
  681. $m->connect($a,
  682. ['action' => 'user'.$a,
  683. 'nickname' => $nickname]);
  684. }
  685. foreach (['all', 'replies'] as $a) {
  686. $m->connect($a.'/rss',
  687. ['action' => $a.'rss',
  688. 'nickname' => $nickname]);
  689. }
  690. $m->connect('avatar',
  691. ['action' => 'avatarbynickname',
  692. 'nickname' => $nickname]);
  693. $m->connect('avatar/:size',
  694. ['action' => 'avatarbynickname',
  695. 'nickname' => $nickname],
  696. ['size' => '(|original|\d+)']);
  697. $m->connect('tag/:tag/rss',
  698. ['action' => 'userrss',
  699. 'nickname' => $nickname],
  700. ['tag' => self::REGEX_TAG]);
  701. $m->connect('tag/:tag',
  702. ['action' => 'showstream',
  703. 'nickname' => $nickname],
  704. ['tag' => self::REGEX_TAG]);
  705. $m->connect('rsd.xml',
  706. ['action' => 'rsd',
  707. 'nickname' => $nickname]);
  708. // peopletags
  709. $m->connect('peopletags',
  710. ['action' => 'peopletagsbyuser']);
  711. $m->connect('peopletags/private',
  712. ['action' => 'peopletagsbyuser',
  713. 'private' => 1]);
  714. $m->connect('peopletags/public',
  715. ['action' => 'peopletagsbyuser',
  716. 'public' => 1]);
  717. $m->connect('othertags',
  718. ['action' => 'peopletagsforuser']);
  719. $m->connect('peopletagsubscriptions',
  720. ['action' => 'peopletagsubscriptions']);
  721. $m->connect('all/:tag/subscribers',
  722. ['action' => 'peopletagsubscribers'],
  723. ['tag' => self::REGEX_TAG]);
  724. $m->connect('all/:tag/tagged',
  725. ['action' => 'peopletagged'],
  726. ['tag' => self::REGEX_TAG]);
  727. $m->connect('all/:tag/edit',
  728. ['action' => 'editpeopletag'],
  729. ['tag' => self::REGEX_TAG]);
  730. foreach (['subscribe', 'unsubscribe'] as $v) {
  731. $m->connect('peopletag/:id/'.$v,
  732. ['action' => $v.'peopletag'],
  733. ['id' => '[0-9]{1,64}']);
  734. }
  735. $m->connect('user/:tagger_id/profiletag/:id/id',
  736. ['action' => 'profiletagbyid'],
  737. ['tagger_id' => '[0-9]+',
  738. 'id' => '[0-9]+']);
  739. $m->connect('all/:tag',
  740. ['action' => 'showprofiletag',
  741. 'tagger' => $nickname],
  742. ['tag' => self::REGEX_TAG]);
  743. foreach (['subscriptions', 'subscribers'] as $a) {
  744. $m->connect($a.'/:tag',
  745. ['action' => $a],
  746. ['tag' => self::REGEX_TAG]);
  747. }
  748. }
  749. $m->connect('rss', ['action' => 'publicrss']);
  750. $m->connect('featuredrss', ['action' => 'featuredrss']);
  751. $m->connect('featured/', ['action' => 'featured']);
  752. $m->connect('featured', ['action' => 'featured']);
  753. $m->connect('rsd.xml', ['action' => 'rsd']);
  754. foreach (['subscriptions', 'subscribers',
  755. 'nudge', 'all', 'foaf', 'replies',
  756. 'inbox', 'outbox'] as $a) {
  757. $m->connect(':nickname/'.$a,
  758. ['action' => $a],
  759. ['nickname' => Nickname::DISPLAY_FMT]);
  760. }
  761. $m->connect(':nickname/subscribers/pending',
  762. ['action' => 'subqueue'],
  763. ['nickname' => Nickname::DISPLAY_FMT]);
  764. // some targeted RSS 1.0 actions (extends TargetedRss10Action)
  765. foreach (['all', 'replies'] as $a) {
  766. $m->connect(':nickname/'.$a.'/rss',
  767. ['action' => $a.'rss'],
  768. ['nickname' => Nickname::DISPLAY_FMT]);
  769. }
  770. // people tags
  771. $m->connect(':nickname/peopletags',
  772. ['action' => 'peopletagsbyuser'],
  773. ['nickname' => Nickname::DISPLAY_FMT]);
  774. $m->connect(':nickname/peopletags/private',
  775. ['action' => 'peopletagsbyuser',
  776. 'private' => 1],
  777. ['nickname' => Nickname::DISPLAY_FMT]);
  778. $m->connect(':nickname/peopletags/public',
  779. ['action' => 'peopletagsbyuser',
  780. 'public' => 1],
  781. ['nickname' => Nickname::DISPLAY_FMT]);
  782. $m->connect(':nickname/othertags',
  783. ['action' => 'peopletagsforuser'],
  784. ['nickname' => Nickname::DISPLAY_FMT]);
  785. $m->connect(':nickname/peopletagsubscriptions',
  786. ['action' => 'peopletagsubscriptions'],
  787. ['nickname' => Nickname::DISPLAY_FMT]);
  788. $m->connect(':tagger/all/:tag/subscribers',
  789. ['action' => 'peopletagsubscribers'],
  790. ['tagger' => Nickname::DISPLAY_FMT,
  791. 'tag' => self::REGEX_TAG]);
  792. $m->connect(':tagger/all/:tag/tagged',
  793. ['action' => 'peopletagged'],
  794. ['tagger' => Nickname::DISPLAY_FMT,
  795. 'tag' => self::REGEX_TAG]);
  796. $m->connect(':tagger/all/:tag/edit',
  797. ['action' => 'editpeopletag'],
  798. ['tagger' => Nickname::DISPLAY_FMT,
  799. 'tag' => self::REGEX_TAG]);
  800. foreach (['subscribe', 'unsubscribe'] as $v) {
  801. $m->connect('peopletag/:id/'.$v,
  802. ['action' => $v.'peopletag'],
  803. ['id' => '[0-9]{1,64}']);
  804. }
  805. $m->connect('user/:tagger_id/profiletag/:id/id',
  806. ['action' => 'profiletagbyid'],
  807. ['tagger_id' => '[0-9]+',
  808. 'id' => '[0-9]+']);
  809. $m->connect(':nickname/all/:tag',
  810. ['action' => 'showprofiletag'],
  811. ['nickname' => Nickname::DISPLAY_FMT,
  812. 'tag' => self::REGEX_TAG]);
  813. foreach (['subscriptions', 'subscribers'] as $a) {
  814. $m->connect(':nickname/'.$a.'/:tag',
  815. ['action' => $a],
  816. ['tag' => self::REGEX_TAG,
  817. 'nickname' => Nickname::DISPLAY_FMT]);
  818. }
  819. foreach (['rss', 'groups'] as $a) {
  820. $m->connect(':nickname/'.$a,
  821. ['action' => 'user'.$a],
  822. ['nickname' => Nickname::DISPLAY_FMT]);
  823. }
  824. $m->connect('avatar/:file',
  825. ['action' => 'avatar'],
  826. ['file' => '.*']);
  827. $m->connect(':nickname/avatar',
  828. ['action' => 'avatarbynickname'],
  829. ['nickname' => Nickname::DISPLAY_FMT]);
  830. $m->connect(':nickname/avatar/:size',
  831. ['action' => 'avatarbynickname'],
  832. ['size' => '(|original|\d+)',
  833. 'nickname' => Nickname::DISPLAY_FMT]);
  834. $m->connect(':nickname/tag/:tag/rss',
  835. ['action' => 'userrss'],
  836. ['nickname' => Nickname::DISPLAY_FMT,
  837. 'tag' => self::REGEX_TAG]);
  838. $m->connect(':nickname/tag/:tag',
  839. ['action' => 'showstream'],
  840. ['nickname' => Nickname::DISPLAY_FMT,
  841. 'tag' => self::REGEX_TAG]);
  842. $m->connect(':nickname/rsd.xml',
  843. ['action' => 'rsd'],
  844. ['nickname' => Nickname::DISPLAY_FMT]);
  845. $m->connect(':nickname',
  846. ['action' => 'showstream'],
  847. ['nickname' => Nickname::DISPLAY_FMT]);
  848. $m->connect(':nickname/',
  849. ['action' => 'showstream'],
  850. ['nickname' => Nickname::DISPLAY_FMT]);
  851. // AtomPub API
  852. $m->connect('api/statusnet/app/service/:id.xml',
  853. ['action' => 'ApiAtomService'],
  854. ['id' => Nickname::DISPLAY_FMT]);
  855. $m->connect('api/statusnet/app/service.xml',
  856. ['action' => 'ApiAtomService']);
  857. $m->connect('api/statusnet/app/subscriptions/:subscriber/:subscribed.atom',
  858. ['action' => 'AtomPubShowSubscription'],
  859. ['subscriber' => '[0-9]+',
  860. 'subscribed' => '[0-9]+']);
  861. $m->connect('api/statusnet/app/subscriptions/:subscriber.atom',
  862. ['action' => 'AtomPubSubscriptionFeed'],
  863. ['subscriber' => '[0-9]+']);
  864. $m->connect('api/statusnet/app/memberships/:profile/:group.atom',
  865. ['action' => 'AtomPubShowMembership'],
  866. ['profile' => '[0-9]+',
  867. 'group' => '[0-9]+']);
  868. $m->connect('api/statusnet/app/memberships/:profile.atom',
  869. ['action' => 'AtomPubMembershipFeed'],
  870. ['profile' => '[0-9]+']);
  871. // URL shortening
  872. $m->connect('url/:id',
  873. ['action' => 'redirecturl'],
  874. ['id' => '[0-9]+']);
  875. // user stuff
  876. Event::handle('RouterInitialized', [$m]);
  877. }
  878. return $m;
  879. }
  880. function map($path)
  881. {
  882. try {
  883. return $this->m->match($path);
  884. } catch (NoRouteMapException $e) {
  885. common_debug($e->getMessage());
  886. // TRANS: Client error on action trying to visit a non-existing page.
  887. throw new ClientException(_('Page not found.'), 404);
  888. }
  889. }
  890. function build($action, $args=null, $params=null, $fragment=null)
  891. {
  892. $action_arg = array('action' => $action);
  893. if ($args) {
  894. $args = array_merge($action_arg, $args);
  895. } else {
  896. $args = $action_arg;
  897. }
  898. $url = $this->m->generate($args, $params, $fragment);
  899. // Due to a bug in the Net_URL_Mapper code, the returned URL may
  900. // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
  901. // repair that here rather than modifying the upstream code...
  902. $qpos = strpos($url, '?');
  903. if ($qpos !== false) {
  904. $url = substr($url, 0, $qpos+1) .
  905. str_replace('?', '&', substr($url, $qpos+1));
  906. // @fixme this is a hacky workaround for http_build_query in the
  907. // lower-level code and bad configs that set the default separator
  908. // to &amp; instead of &. Encoded &s in parameters will not be
  909. // affected.
  910. $url = substr($url, 0, $qpos+1) .
  911. str_replace('&amp;', '&', substr($url, $qpos+1));
  912. }
  913. return $url;
  914. }
  915. }