router.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. // blocks
  452. $m->connect('api/blocks/create/:id.:format',
  453. ['action' => 'ApiBlockCreate'],
  454. ['id' => Nickname::INPUT_FMT,
  455. 'format' => '(xml|json)']);
  456. $m->connect('api/blocks/create.:format',
  457. ['action' => 'ApiBlockCreate'],
  458. ['format' => '(xml|json)']);
  459. $m->connect('api/blocks/destroy/:id.:format',
  460. ['action' => 'ApiBlockDestroy'],
  461. ['id' => Nickname::INPUT_FMT,
  462. 'format' => '(xml|json)']);
  463. $m->connect('api/blocks/destroy.:format',
  464. ['action' => 'ApiBlockDestroy'],
  465. ['format' => '(xml|json)']);
  466. // help
  467. $m->connect('api/help/test.:format',
  468. ['action' => 'ApiHelpTest'],
  469. ['format' => '(xml|json)']);
  470. // statusnet
  471. $m->connect('api/statusnet/version.:format',
  472. ['action' => 'ApiGNUsocialVersion'],
  473. ['format' => '(xml|json)']);
  474. $m->connect('api/statusnet/config.:format',
  475. ['action' => 'ApiGNUsocialConfig'],
  476. ['format' => '(xml|json)']);
  477. // For our current software name, we provide "gnusocial" base action
  478. $m->connect('api/gnusocial/version.:format',
  479. ['action' => 'ApiGNUsocialVersion'],
  480. ['format' => '(xml|json)']);
  481. $m->connect('api/gnusocial/config.:format',
  482. ['action' => 'ApiGNUsocialConfig'],
  483. ['format' => '(xml|json)']);
  484. // Groups and tags are newer than 0.8.1 so no backward-compatibility
  485. // necessary
  486. // Groups
  487. //'list' has to be handled differently, as php will not allow a method to be named 'list'
  488. $m->connect('api/statusnet/groups/timeline/:id.:format',
  489. ['action' => 'ApiTimelineGroup'],
  490. ['id' => Nickname::INPUT_FMT,
  491. 'format' => '(xml|json|rss|atom|as)']);
  492. $m->connect('api/statusnet/groups/show/:id.:format',
  493. ['action' => 'ApiGroupShow'],
  494. ['id' => Nickname::INPUT_FMT,
  495. 'format' => '(xml|json)']);
  496. $m->connect('api/statusnet/groups/show.:format',
  497. ['action' => 'ApiGroupShow'],
  498. ['format' => '(xml|json)']);
  499. $m->connect('api/statusnet/groups/join/:id.:format',
  500. ['action' => 'ApiGroupJoin'],
  501. ['id' => Nickname::INPUT_FMT,
  502. 'format' => '(xml|json)']);
  503. $m->connect('api/statusnet/groups/join.:format',
  504. ['action' => 'ApiGroupJoin'],
  505. ['format' => '(xml|json)']);
  506. $m->connect('api/statusnet/groups/leave/:id.:format',
  507. ['action' => 'ApiGroupLeave'],
  508. ['id' => Nickname::INPUT_FMT,
  509. 'format' => '(xml|json)']);
  510. $m->connect('api/statusnet/groups/leave.:format',
  511. ['action' => 'ApiGroupLeave'],
  512. ['format' => '(xml|json)']);
  513. $m->connect('api/statusnet/groups/is_member.:format',
  514. ['action' => 'ApiGroupIsMember'],
  515. ['format' => '(xml|json)']);
  516. $m->connect('api/statusnet/groups/list/:id.:format',
  517. ['action' => 'ApiGroupList'],
  518. ['id' => Nickname::INPUT_FMT,
  519. 'format' => '(xml|json|rss|atom)']);
  520. $m->connect('api/statusnet/groups/list.:format',
  521. ['action' => 'ApiGroupList'],
  522. ['format' => '(xml|json|rss|atom)']);
  523. $m->connect('api/statusnet/groups/list_all.:format',
  524. ['action' => 'ApiGroupListAll'],
  525. ['format' => '(xml|json|rss|atom)']);
  526. $m->connect('api/statusnet/groups/membership/:id.:format',
  527. ['action' => 'ApiGroupMembership'],
  528. ['id' => Nickname::INPUT_FMT,
  529. 'format' => '(xml|json)']);
  530. $m->connect('api/statusnet/groups/membership.:format',
  531. ['action' => 'ApiGroupMembership'],
  532. ['format' => '(xml|json)']);
  533. $m->connect('api/statusnet/groups/create.:format',
  534. ['action' => 'ApiGroupCreate'],
  535. ['format' => '(xml|json)']);
  536. $m->connect('api/statusnet/groups/update/:id.:format',
  537. ['action' => 'ApiGroupProfileUpdate'],
  538. ['id' => '[a-zA-Z0-9]+',
  539. 'format' => '(xml|json)']);
  540. $m->connect('api/statusnet/conversation/:id.:format',
  541. ['action' => 'apiconversation'],
  542. ['id' => '[0-9]+',
  543. 'format' => '(xml|json|rss|atom|as)']);
  544. // Lists (people tags)
  545. $m->connect('api/lists/list.:format',
  546. ['action' => 'ApiListSubscriptions'],
  547. ['format' => '(xml|json)']);
  548. $m->connect('api/lists/memberships.:format',
  549. ['action' => 'ApiListMemberships'],
  550. ['format' => '(xml|json)']);
  551. $m->connect('api/:user/lists/memberships.:format',
  552. ['action' => 'ApiListMemberships'],
  553. ['user' => '[a-zA-Z0-9]+',
  554. 'format' => '(xml|json)']);
  555. $m->connect('api/lists/subscriptions.:format',
  556. ['action' => 'ApiListSubscriptions'],
  557. ['format' => '(xml|json)']);
  558. $m->connect('api/:user/lists/subscriptions.:format',
  559. ['action' => 'ApiListSubscriptions'],
  560. ['user' => '[a-zA-Z0-9]+',
  561. 'format' => '(xml|json)']);
  562. $m->connect('api/lists.:format',
  563. ['action' => 'ApiLists'],
  564. ['format' => '(xml|json)']);
  565. $m->connect('api/:user/lists/:id.:format',
  566. ['action' => 'ApiList'],
  567. ['user' => '[a-zA-Z0-9]+',
  568. 'id' => '[a-zA-Z0-9]+',
  569. 'format' => '(xml|json)']);
  570. $m->connect('api/:user/lists.:format',
  571. ['action' => 'ApiLists'],
  572. ['user' => '[a-zA-Z0-9]+',
  573. 'format' => '(xml|json)']);
  574. $m->connect('api/:user/lists/:id/statuses.:format',
  575. ['action' => 'ApiTimelineList'],
  576. ['user' => '[a-zA-Z0-9]+',
  577. 'id' => '[a-zA-Z0-9]+',
  578. 'format' => '(xml|json|rss|atom)']);
  579. $m->connect('api/:user/:list_id/members/:id.:format',
  580. ['action' => 'ApiListMember'],
  581. ['user' => '[a-zA-Z0-9]+',
  582. 'list_id' => '[a-zA-Z0-9]+',
  583. 'id' => '[a-zA-Z0-9]+',
  584. 'format' => '(xml|json)']);
  585. $m->connect('api/:user/:list_id/members.:format',
  586. ['action' => 'ApiListMembers'],
  587. ['user' => '[a-zA-Z0-9]+',
  588. 'list_id' => '[a-zA-Z0-9]+',
  589. 'format' => '(xml|json)']);
  590. $m->connect('api/:user/:list_id/subscribers/:id.:format',
  591. ['action' => 'ApiListSubscriber'],
  592. ['user' => '[a-zA-Z0-9]+',
  593. 'list_id' => '[a-zA-Z0-9]+',
  594. 'id' => '[a-zA-Z0-9]+',
  595. 'format' => '(xml|json)']);
  596. $m->connect('api/:user/:list_id/subscribers.:format',
  597. ['action' => 'ApiListSubscribers'],
  598. ['user' => '[a-zA-Z0-9]+',
  599. 'list_id' => '[a-zA-Z0-9]+',
  600. 'format' => '(xml|json)']);
  601. // Tags
  602. $m->connect('api/statusnet/tags/timeline/:tag.:format',
  603. ['action' => 'ApiTimelineTag'],
  604. ['tag' => self::REGEX_TAG,
  605. 'format' => '(xml|json|rss|atom|as)']);
  606. // media related
  607. $m->connect('api/statusnet/media/upload',
  608. ['action' => 'ApiMediaUpload']);
  609. $m->connect('api/statuses/update_with_media.json',
  610. ['action' => 'ApiMediaUpload']);
  611. // Twitter Media upload API v1.1
  612. $m->connect('api/media/upload.:format',
  613. ['action' => 'ApiMediaUpload'],
  614. ['format' => '(xml|json)']);
  615. // search
  616. $m->connect('api/search.atom', ['action' => 'ApiSearchAtom']);
  617. $m->connect('api/search.json', ['action' => 'ApiSearchJSON']);
  618. $m->connect('api/trends.json', ['action' => 'ApiTrends']);
  619. $m->connect('api/oauth/request_token',
  620. ['action' => 'ApiOAuthRequestToken']);
  621. $m->connect('api/oauth/access_token',
  622. ['action' => 'ApiOAuthAccessToken']);
  623. $m->connect('api/oauth/authorize',
  624. ['action' => 'ApiOAuthAuthorize']);
  625. // Admin
  626. $m->connect('panel/site', ['action' => 'siteadminpanel']);
  627. $m->connect('panel/user', ['action' => 'useradminpanel']);
  628. $m->connect('panel/access', ['action' => 'accessadminpanel']);
  629. $m->connect('panel/paths', ['action' => 'pathsadminpanel']);
  630. $m->connect('panel/sessions', ['action' => 'sessionsadminpanel']);
  631. $m->connect('panel/sitenotice', ['action' => 'sitenoticeadminpanel']);
  632. $m->connect('panel/license', ['action' => 'licenseadminpanel']);
  633. $m->connect('panel/plugins', ['action' => 'pluginsadminpanel']);
  634. $m->connect('panel/plugins/enable/:plugin',
  635. ['action' => 'pluginenable'],
  636. ['plugin' => '[A-Za-z0-9_]+']);
  637. $m->connect('panel/plugins/disable/:plugin',
  638. ['action' => 'plugindisable'],
  639. ['plugin' => '[A-Za-z0-9_]+']);
  640. $m->connect('panel/plugins/delete/:plugin',
  641. ['action' => 'plugindelete'],
  642. ['plugin' => '[A-Za-z0-9_]+']);
  643. $m->connect('panel/plugins/install',
  644. ['action' => 'plugininstall']);
  645. // Common people-tag stuff
  646. $m->connect('peopletag/:tag',
  647. ['action' => 'peopletag'],
  648. ['tag' => self::REGEX_TAG]);
  649. $m->connect('selftag/:tag',
  650. ['action' => 'selftag'],
  651. ['tag' => self::REGEX_TAG]);
  652. $m->connect('main/addpeopletag', ['action' => 'addpeopletag']);
  653. $m->connect('main/removepeopletag', ['action' => 'removepeopletag']);
  654. $m->connect('main/profilecompletion', ['action' => 'profilecompletion']);
  655. $m->connect('main/peopletagautocomplete', ['action' => 'peopletagautocomplete']);
  656. // In the "root"
  657. if (common_config('singleuser', 'enabled')) {
  658. $nickname = User::singleUserNickname();
  659. foreach (['subscriptions', 'subscribers', 'all', 'foaf', 'replies'] as $a) {
  660. $m->connect($a,
  661. ['action' => $a,
  662. 'nickname' => $nickname]);
  663. }
  664. foreach (['subscriptions', 'subscribers'] as $a) {
  665. $m->connect($a.'/:tag',
  666. ['action' => $a,
  667. 'nickname' => $nickname],
  668. ['tag' => self::REGEX_TAG]);
  669. }
  670. $m->connect('subscribers/pending',
  671. ['action' => 'subqueue',
  672. 'nickname' => $nickname]);
  673. foreach (['rss', 'groups'] as $a) {
  674. $m->connect($a,
  675. ['action' => 'user'.$a,
  676. 'nickname' => $nickname]);
  677. }
  678. foreach (['all', 'replies'] as $a) {
  679. $m->connect($a.'/rss',
  680. ['action' => $a.'rss',
  681. 'nickname' => $nickname]);
  682. }
  683. $m->connect('avatar',
  684. ['action' => 'avatarbynickname',
  685. 'nickname' => $nickname]);
  686. $m->connect('avatar/:size',
  687. ['action' => 'avatarbynickname',
  688. 'nickname' => $nickname],
  689. ['size' => '(|original|\d+)']);
  690. $m->connect('tag/:tag/rss',
  691. ['action' => 'userrss',
  692. 'nickname' => $nickname],
  693. ['tag' => self::REGEX_TAG]);
  694. $m->connect('tag/:tag',
  695. ['action' => 'showstream',
  696. 'nickname' => $nickname],
  697. ['tag' => self::REGEX_TAG]);
  698. $m->connect('rsd.xml',
  699. ['action' => 'rsd',
  700. 'nickname' => $nickname]);
  701. // peopletags
  702. $m->connect('peopletags',
  703. ['action' => 'peopletagsbyuser']);
  704. $m->connect('peopletags/private',
  705. ['action' => 'peopletagsbyuser',
  706. 'private' => 1]);
  707. $m->connect('peopletags/public',
  708. ['action' => 'peopletagsbyuser',
  709. 'public' => 1]);
  710. $m->connect('othertags',
  711. ['action' => 'peopletagsforuser']);
  712. $m->connect('peopletagsubscriptions',
  713. ['action' => 'peopletagsubscriptions']);
  714. $m->connect('all/:tag/subscribers',
  715. ['action' => 'peopletagsubscribers'],
  716. ['tag' => self::REGEX_TAG]);
  717. $m->connect('all/:tag/tagged',
  718. ['action' => 'peopletagged'],
  719. ['tag' => self::REGEX_TAG]);
  720. $m->connect('all/:tag/edit',
  721. ['action' => 'editpeopletag'],
  722. ['tag' => self::REGEX_TAG]);
  723. foreach (['subscribe', 'unsubscribe'] as $v) {
  724. $m->connect('peopletag/:id/'.$v,
  725. ['action' => $v.'peopletag'],
  726. ['id' => '[0-9]{1,64}']);
  727. }
  728. $m->connect('user/:tagger_id/profiletag/:id/id',
  729. ['action' => 'profiletagbyid'],
  730. ['tagger_id' => '[0-9]+',
  731. 'id' => '[0-9]+']);
  732. $m->connect('all/:tag',
  733. ['action' => 'showprofiletag',
  734. 'tagger' => $nickname],
  735. ['tag' => self::REGEX_TAG]);
  736. foreach (['subscriptions', 'subscribers'] as $a) {
  737. $m->connect($a.'/:tag',
  738. ['action' => $a],
  739. ['tag' => self::REGEX_TAG]);
  740. }
  741. }
  742. $m->connect('rss', ['action' => 'publicrss']);
  743. $m->connect('featuredrss', ['action' => 'featuredrss']);
  744. $m->connect('featured/', ['action' => 'featured']);
  745. $m->connect('featured', ['action' => 'featured']);
  746. $m->connect('rsd.xml', ['action' => 'rsd']);
  747. foreach (['subscriptions', 'subscribers',
  748. 'nudge', 'all', 'foaf', 'replies',
  749. 'inbox', 'outbox'] as $a) {
  750. $m->connect(':nickname/'.$a,
  751. ['action' => $a],
  752. ['nickname' => Nickname::DISPLAY_FMT]);
  753. }
  754. $m->connect(':nickname/subscribers/pending',
  755. ['action' => 'subqueue'],
  756. ['nickname' => Nickname::DISPLAY_FMT]);
  757. // some targeted RSS 1.0 actions (extends TargetedRss10Action)
  758. foreach (['all', 'replies'] as $a) {
  759. $m->connect(':nickname/'.$a.'/rss',
  760. ['action' => $a.'rss'],
  761. ['nickname' => Nickname::DISPLAY_FMT]);
  762. }
  763. // people tags
  764. $m->connect(':nickname/peopletags',
  765. ['action' => 'peopletagsbyuser'],
  766. ['nickname' => Nickname::DISPLAY_FMT]);
  767. $m->connect(':nickname/peopletags/private',
  768. ['action' => 'peopletagsbyuser',
  769. 'private' => 1],
  770. ['nickname' => Nickname::DISPLAY_FMT]);
  771. $m->connect(':nickname/peopletags/public',
  772. ['action' => 'peopletagsbyuser',
  773. 'public' => 1],
  774. ['nickname' => Nickname::DISPLAY_FMT]);
  775. $m->connect(':nickname/othertags',
  776. ['action' => 'peopletagsforuser'],
  777. ['nickname' => Nickname::DISPLAY_FMT]);
  778. $m->connect(':nickname/peopletagsubscriptions',
  779. ['action' => 'peopletagsubscriptions'],
  780. ['nickname' => Nickname::DISPLAY_FMT]);
  781. $m->connect(':tagger/all/:tag/subscribers',
  782. ['action' => 'peopletagsubscribers'],
  783. ['tagger' => Nickname::DISPLAY_FMT,
  784. 'tag' => self::REGEX_TAG]);
  785. $m->connect(':tagger/all/:tag/tagged',
  786. ['action' => 'peopletagged'],
  787. ['tagger' => Nickname::DISPLAY_FMT,
  788. 'tag' => self::REGEX_TAG]);
  789. $m->connect(':tagger/all/:tag/edit',
  790. ['action' => 'editpeopletag'],
  791. ['tagger' => Nickname::DISPLAY_FMT,
  792. 'tag' => self::REGEX_TAG]);
  793. foreach (['subscribe', 'unsubscribe'] as $v) {
  794. $m->connect('peopletag/:id/'.$v,
  795. ['action' => $v.'peopletag'],
  796. ['id' => '[0-9]{1,64}']);
  797. }
  798. $m->connect('user/:tagger_id/profiletag/:id/id',
  799. ['action' => 'profiletagbyid'],
  800. ['tagger_id' => '[0-9]+',
  801. 'id' => '[0-9]+']);
  802. $m->connect(':nickname/all/:tag',
  803. ['action' => 'showprofiletag'],
  804. ['nickname' => Nickname::DISPLAY_FMT,
  805. 'tag' => self::REGEX_TAG]);
  806. foreach (['subscriptions', 'subscribers'] as $a) {
  807. $m->connect(':nickname/'.$a.'/:tag',
  808. ['action' => $a],
  809. ['tag' => self::REGEX_TAG,
  810. 'nickname' => Nickname::DISPLAY_FMT]);
  811. }
  812. foreach (['rss', 'groups'] as $a) {
  813. $m->connect(':nickname/'.$a,
  814. ['action' => 'user'.$a],
  815. ['nickname' => Nickname::DISPLAY_FMT]);
  816. }
  817. $m->connect('avatar/:file',
  818. ['action' => 'avatar'],
  819. ['file' => '.*']);
  820. $m->connect(':nickname/avatar',
  821. ['action' => 'avatarbynickname'],
  822. ['nickname' => Nickname::DISPLAY_FMT]);
  823. $m->connect(':nickname/avatar/:size',
  824. ['action' => 'avatarbynickname'],
  825. ['size' => '(|original|\d+)',
  826. 'nickname' => Nickname::DISPLAY_FMT]);
  827. $m->connect(':nickname/tag/:tag/rss',
  828. ['action' => 'userrss'],
  829. ['nickname' => Nickname::DISPLAY_FMT,
  830. 'tag' => self::REGEX_TAG]);
  831. $m->connect(':nickname/tag/:tag',
  832. ['action' => 'showstream'],
  833. ['nickname' => Nickname::DISPLAY_FMT,
  834. 'tag' => self::REGEX_TAG]);
  835. $m->connect(':nickname/rsd.xml',
  836. ['action' => 'rsd'],
  837. ['nickname' => Nickname::DISPLAY_FMT]);
  838. $m->connect(':nickname',
  839. ['action' => 'showstream'],
  840. ['nickname' => Nickname::DISPLAY_FMT]);
  841. $m->connect(':nickname/',
  842. ['action' => 'showstream'],
  843. ['nickname' => Nickname::DISPLAY_FMT]);
  844. // AtomPub API
  845. $m->connect('api/statusnet/app/service/:id.xml',
  846. ['action' => 'ApiAtomService'],
  847. ['id' => Nickname::DISPLAY_FMT]);
  848. $m->connect('api/statusnet/app/service.xml',
  849. ['action' => 'ApiAtomService']);
  850. $m->connect('api/statusnet/app/subscriptions/:subscriber/:subscribed.atom',
  851. ['action' => 'AtomPubShowSubscription'],
  852. ['subscriber' => '[0-9]+',
  853. 'subscribed' => '[0-9]+']);
  854. $m->connect('api/statusnet/app/subscriptions/:subscriber.atom',
  855. ['action' => 'AtomPubSubscriptionFeed'],
  856. ['subscriber' => '[0-9]+']);
  857. $m->connect('api/statusnet/app/memberships/:profile/:group.atom',
  858. ['action' => 'AtomPubShowMembership'],
  859. ['profile' => '[0-9]+',
  860. 'group' => '[0-9]+']);
  861. $m->connect('api/statusnet/app/memberships/:profile.atom',
  862. ['action' => 'AtomPubMembershipFeed'],
  863. ['profile' => '[0-9]+']);
  864. // URL shortening
  865. $m->connect('url/:id',
  866. ['action' => 'redirecturl'],
  867. ['id' => '[0-9]+']);
  868. // user stuff
  869. Event::handle('RouterInitialized', [$m]);
  870. }
  871. return $m;
  872. }
  873. function map($path)
  874. {
  875. try {
  876. return $this->m->match($path);
  877. } catch (NoRouteMapException $e) {
  878. common_debug($e->getMessage());
  879. // TRANS: Client error on action trying to visit a non-existing page.
  880. throw new ClientException(_('Page not found.'), 404);
  881. }
  882. }
  883. function build($action, $args=null, $params=null, $fragment=null)
  884. {
  885. $action_arg = array('action' => $action);
  886. if ($args) {
  887. $args = array_merge($action_arg, $args);
  888. } else {
  889. $args = $action_arg;
  890. }
  891. $url = $this->m->generate($args, $params, $fragment);
  892. // Due to a bug in the Net_URL_Mapper code, the returned URL may
  893. // contain a malformed query of the form ?p1=v1?p2=v2?p3=v3. We
  894. // repair that here rather than modifying the upstream code...
  895. $qpos = strpos($url, '?');
  896. if ($qpos !== false) {
  897. $url = substr($url, 0, $qpos+1) .
  898. str_replace('?', '&', substr($url, $qpos+1));
  899. // @fixme this is a hacky workaround for http_build_query in the
  900. // lower-level code and bad configs that set the default separator
  901. // to &amp; instead of &. Encoded &s in parameters will not be
  902. // affected.
  903. $url = substr($url, 0, $qpos+1) .
  904. str_replace('&amp;', '&', substr($url, $qpos+1));
  905. }
  906. return $url;
  907. }
  908. }