router.php 46 KB

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