router.php 45 KB

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