util.php 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008-2011, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* XXX: break up into separate modules (HTTP, user, files) */
  20. /**
  21. * Show a server error.
  22. */
  23. function common_server_error($msg, $code=500)
  24. {
  25. $err = new ServerErrorAction($msg, $code);
  26. $err->showPage();
  27. }
  28. /**
  29. * Show a user error.
  30. */
  31. function common_user_error($msg, $code=400)
  32. {
  33. $err = new ClientErrorAction($msg, $code);
  34. $err->showPage();
  35. }
  36. /**
  37. * This should only be used at setup; processes switching languages
  38. * to send text to other users should use common_switch_locale().
  39. *
  40. * @param string $language Locale language code (optional; empty uses
  41. * current user's preference or site default)
  42. * @return mixed success
  43. */
  44. function common_init_locale($language=null)
  45. {
  46. if(!$language) {
  47. $language = common_language();
  48. }
  49. putenv('LANGUAGE='.$language);
  50. putenv('LANG='.$language);
  51. $ok = setlocale(LC_ALL, $language . ".utf8",
  52. $language . ".UTF8",
  53. $language . ".utf-8",
  54. $language . ".UTF-8",
  55. $language);
  56. return $ok;
  57. }
  58. /**
  59. * Initialize locale and charset settings and gettext with our message catalog,
  60. * using the current user's language preference or the site default.
  61. *
  62. * This should generally only be run at framework initialization; code switching
  63. * languages at runtime should call common_switch_language().
  64. *
  65. * @access private
  66. */
  67. function common_init_language()
  68. {
  69. mb_internal_encoding('UTF-8');
  70. // Note that this setlocale() call may "fail" but this is harmless;
  71. // gettext will still select the right language.
  72. $language = common_language();
  73. $locale_set = common_init_locale($language);
  74. if (!$locale_set) {
  75. // The requested locale doesn't exist on the system.
  76. //
  77. // gettext seems very picky... We first need to setlocale()
  78. // to a locale which _does_ exist on the system, and _then_
  79. // we can set in another locale that may not be set up
  80. // (say, ga_ES for Galego/Galician) it seems to take it.
  81. //
  82. // For some reason C and POSIX which are guaranteed to work
  83. // don't do the job. en_US.UTF-8 should be there most of the
  84. // time, but not guaranteed.
  85. $ok = common_init_locale("en_US");
  86. if (!$ok && strtolower(substr(PHP_OS, 0, 3)) != 'win') {
  87. // Try to find a complete, working locale on Unix/Linux...
  88. // @fixme shelling out feels awfully inefficient
  89. // but I don't think there's a more standard way.
  90. $all = `locale -a`;
  91. foreach (explode("\n", $all) as $locale) {
  92. if (preg_match('/\.utf[-_]?8$/i', $locale)) {
  93. $ok = setlocale(LC_ALL, $locale);
  94. if ($ok) {
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. if (!$ok) {
  101. common_log(LOG_ERR, "Unable to find a UTF-8 locale on this system; UI translations may not work.");
  102. }
  103. $locale_set = common_init_locale($language);
  104. }
  105. common_init_gettext();
  106. }
  107. /**
  108. * @access private
  109. */
  110. function common_init_gettext()
  111. {
  112. setlocale(LC_CTYPE, 'C');
  113. // So we do not have to make people install the gettext locales
  114. $path = common_config('site','locale_path');
  115. bindtextdomain("statusnet", $path);
  116. bind_textdomain_codeset("statusnet", "UTF-8");
  117. textdomain("statusnet");
  118. }
  119. /**
  120. * Switch locale during runtime, and poke gettext until it cries uncle.
  121. * Otherwise, sometimes it doesn't actually switch away from the old language.
  122. *
  123. * @param string $language code for locale ('en', 'fr', 'pt_BR' etc)
  124. */
  125. function common_switch_locale($language=null)
  126. {
  127. common_init_locale($language);
  128. setlocale(LC_CTYPE, 'C');
  129. // So we do not have to make people install the gettext locales
  130. $path = common_config('site','locale_path');
  131. bindtextdomain("statusnet", $path);
  132. bind_textdomain_codeset("statusnet", "UTF-8");
  133. textdomain("statusnet");
  134. }
  135. function common_timezone()
  136. {
  137. if (common_logged_in()) {
  138. $user = common_current_user();
  139. if ($user->timezone) {
  140. return $user->timezone;
  141. }
  142. }
  143. return common_config('site', 'timezone');
  144. }
  145. function common_valid_language($lang)
  146. {
  147. if ($lang) {
  148. // Validate -- we don't want to end up with a bogus code
  149. // left over from some old junk.
  150. foreach (common_config('site', 'languages') as $code => $info) {
  151. if ($info['lang'] == $lang) {
  152. return true;
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. function common_language()
  159. {
  160. // Allow ?uselang=xx override, very useful for debugging
  161. // and helping translators check usage and context.
  162. if (isset($_GET['uselang'])) {
  163. $uselang = strval($_GET['uselang']);
  164. if (common_valid_language($uselang)) {
  165. return $uselang;
  166. }
  167. }
  168. // If there is a user logged in and they've set a language preference
  169. // then return that one...
  170. if (_have_config() && common_logged_in()) {
  171. $user = common_current_user();
  172. if (common_valid_language($user->language)) {
  173. return $user->language;
  174. }
  175. }
  176. // Otherwise, find the best match for the languages requested by the
  177. // user's browser...
  178. if (common_config('site', 'langdetect')) {
  179. $httplang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null;
  180. if (!empty($httplang)) {
  181. $language = client_preferred_language($httplang);
  182. if ($language)
  183. return $language;
  184. }
  185. }
  186. // Finally, if none of the above worked, use the site's default...
  187. return common_config('site', 'language');
  188. }
  189. /**
  190. * Salted, hashed passwords are stored in the DB.
  191. */
  192. function common_munge_password($password, Profile $profile=null)
  193. {
  194. $hashed = null;
  195. if (Event::handle('StartHashPassword', array(&$hashed, $password, $profile))) {
  196. Event::handle('EndHashPassword', array(&$hashed, $password, $profile));
  197. }
  198. if (empty($hashed)) {
  199. throw new PasswordHashException();
  200. }
  201. return $hashed;
  202. }
  203. /**
  204. * Check if a username exists and has matching password.
  205. */
  206. function common_check_user($nickname, $password)
  207. {
  208. // empty nickname always unacceptable
  209. if (empty($nickname)) {
  210. return false;
  211. }
  212. $authenticatedUser = false;
  213. if (Event::handle('StartCheckPassword', array($nickname, $password, &$authenticatedUser))) {
  214. if (common_is_email($nickname)) {
  215. $user = User::getKV('email', common_canonical_email($nickname));
  216. } else {
  217. $user = User::getKV('nickname', Nickname::normalize($nickname));
  218. }
  219. if ($user instanceof User && !empty($password)) {
  220. if (0 == strcmp(common_munge_password($password, $user->getProfile()), $user->password)) {
  221. //internal checking passed
  222. $authenticatedUser = $user;
  223. }
  224. }
  225. }
  226. Event::handle('EndCheckPassword', array($nickname, $password, $authenticatedUser));
  227. return $authenticatedUser;
  228. }
  229. /**
  230. * Is the current user logged in?
  231. */
  232. function common_logged_in()
  233. {
  234. return (!is_null(common_current_user()));
  235. }
  236. function common_local_referer()
  237. {
  238. return isset($_SERVER['HTTP_REFERER'])
  239. && parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) === common_config('site', 'server');
  240. }
  241. function common_have_session()
  242. {
  243. return (0 != strcmp(session_id(), ''));
  244. }
  245. function common_ensure_session()
  246. {
  247. $c = null;
  248. if (array_key_exists(session_name(), $_COOKIE)) {
  249. $c = $_COOKIE[session_name()];
  250. }
  251. if (!common_have_session()) {
  252. if (common_config('sessions', 'handle')) {
  253. Session::setSaveHandler();
  254. }
  255. if (array_key_exists(session_name(), $_GET)) {
  256. $id = $_GET[session_name()];
  257. } else if (array_key_exists(session_name(), $_COOKIE)) {
  258. $id = $_COOKIE[session_name()];
  259. }
  260. if (isset($id)) {
  261. session_id($id);
  262. }
  263. @session_start();
  264. if (!isset($_SESSION['started'])) {
  265. $_SESSION['started'] = time();
  266. if (!empty($id)) {
  267. common_log(LOG_WARNING, 'Session cookie "' . $_COOKIE[session_name()] . '" ' .
  268. ' is set but started value is null');
  269. }
  270. }
  271. }
  272. }
  273. // Three kinds of arguments:
  274. // 1) a user object
  275. // 2) a nickname
  276. // 3) null to clear
  277. // Initialize to false; set to null if none found
  278. $_cur = false;
  279. function common_set_user($user)
  280. {
  281. global $_cur;
  282. if (is_null($user) && common_have_session()) {
  283. $_cur = null;
  284. unset($_SESSION['userid']);
  285. return true;
  286. } else if (is_string($user)) {
  287. $nickname = $user;
  288. $user = User::getKV('nickname', $nickname);
  289. } else if (!$user instanceof User) {
  290. return false;
  291. }
  292. if ($user) {
  293. if (Event::handle('StartSetUser', array(&$user))) {
  294. if (!empty($user)) {
  295. if (!$user->hasRight(Right::WEBLOGIN)) {
  296. // TRANS: Authorisation exception thrown when a user a not allowed to login.
  297. throw new AuthorizationException(_('Not allowed to log in.'));
  298. }
  299. common_ensure_session();
  300. $_SESSION['userid'] = $user->id;
  301. $_cur = $user;
  302. Event::handle('EndSetUser', array($user));
  303. return $_cur;
  304. }
  305. }
  306. }
  307. return false;
  308. }
  309. function common_set_cookie($key, $value, $expiration=0)
  310. {
  311. $path = common_config('site', 'path');
  312. $server = common_config('site', 'server');
  313. if ($path && ($path != '/')) {
  314. $cookiepath = '/' . $path . '/';
  315. } else {
  316. $cookiepath = '/';
  317. }
  318. return setcookie($key,
  319. $value,
  320. $expiration,
  321. $cookiepath,
  322. $server,
  323. GNUsocial::useHTTPS());
  324. }
  325. define('REMEMBERME', 'rememberme');
  326. define('REMEMBERME_EXPIRY', 30 * 24 * 60 * 60); // 30 days
  327. function common_rememberme($user=null)
  328. {
  329. if (!$user) {
  330. $user = common_current_user();
  331. if (!$user) {
  332. return false;
  333. }
  334. }
  335. $rm = new Remember_me();
  336. $rm->code = common_random_hexstr(16);
  337. $rm->user_id = $user->id;
  338. // Wrap the insert in some good ol' fashioned transaction code
  339. $rm->query('BEGIN');
  340. $result = $rm->insert();
  341. if (!$result) {
  342. common_log_db_error($rm, 'INSERT', __FILE__);
  343. $rm->query('ROLLBACK');
  344. return false;
  345. }
  346. $rm->query('COMMIT');
  347. $cookieval = $rm->user_id . ':' . $rm->code;
  348. common_log(LOG_INFO, 'adding rememberme cookie "' . $cookieval . '" for ' . $user->nickname);
  349. common_set_cookie(REMEMBERME, $cookieval, time() + REMEMBERME_EXPIRY);
  350. return true;
  351. }
  352. function common_remembered_user()
  353. {
  354. $user = null;
  355. $packed = isset($_COOKIE[REMEMBERME]) ? $_COOKIE[REMEMBERME] : null;
  356. if (!$packed) {
  357. return null;
  358. }
  359. list($id, $code) = explode(':', $packed);
  360. if (!$id || !$code) {
  361. common_log(LOG_WARNING, 'Malformed rememberme cookie: ' . $packed);
  362. common_forgetme();
  363. return null;
  364. }
  365. $rm = Remember_me::getKV('code', $code);
  366. if (!$rm) {
  367. common_log(LOG_WARNING, 'No such remember code: ' . $code);
  368. common_forgetme();
  369. return null;
  370. }
  371. if ($rm->user_id != $id) {
  372. common_log(LOG_WARNING, 'Rememberme code for wrong user: ' . $rm->user_id . ' != ' . $id);
  373. common_forgetme();
  374. return null;
  375. }
  376. $user = User::getKV('id', $rm->user_id);
  377. if (!$user instanceof User) {
  378. common_log(LOG_WARNING, 'No such user for rememberme: ' . $rm->user_id);
  379. common_forgetme();
  380. return null;
  381. }
  382. // successful!
  383. $result = $rm->delete();
  384. if (!$result) {
  385. common_log_db_error($rm, 'DELETE', __FILE__);
  386. common_log(LOG_WARNING, 'Could not delete rememberme: ' . $code);
  387. common_forgetme();
  388. return null;
  389. }
  390. common_log(LOG_INFO, 'logging in ' . $user->nickname . ' using rememberme code ' . $rm->code);
  391. common_set_user($user);
  392. common_real_login(false);
  393. // We issue a new cookie, so they can log in
  394. // automatically again after this session
  395. common_rememberme($user);
  396. return $user;
  397. }
  398. /**
  399. * must be called with a valid user!
  400. */
  401. function common_forgetme()
  402. {
  403. common_set_cookie(REMEMBERME, '', 0);
  404. }
  405. /**
  406. * Who is the current user?
  407. */
  408. function common_current_user()
  409. {
  410. global $_cur;
  411. if (!_have_config()) {
  412. return null;
  413. }
  414. if ($_cur === false) {
  415. if (isset($_COOKIE[session_name()]) || isset($_GET[session_name()])
  416. || (isset($_SESSION['userid']) && $_SESSION['userid'])) {
  417. common_ensure_session();
  418. $id = isset($_SESSION['userid']) ? $_SESSION['userid'] : false;
  419. if ($id) {
  420. $user = User::getKV('id', $id);
  421. if ($user instanceof User) {
  422. $_cur = $user;
  423. return $_cur;
  424. }
  425. }
  426. }
  427. // that didn't work; try to remember; will init $_cur to null on failure
  428. $_cur = common_remembered_user();
  429. if ($_cur) {
  430. // XXX: Is this necessary?
  431. $_SESSION['userid'] = $_cur->id;
  432. }
  433. }
  434. return $_cur;
  435. }
  436. /**
  437. * Logins that are 'remembered' aren't 'real' -- they're subject to
  438. * cookie-stealing. So, we don't let them do certain things. New reg,
  439. * OpenID, and password logins _are_ real.
  440. */
  441. function common_real_login($real=true)
  442. {
  443. common_ensure_session();
  444. $_SESSION['real_login'] = $real;
  445. }
  446. function common_is_real_login()
  447. {
  448. return common_logged_in() && $_SESSION['real_login'];
  449. }
  450. /**
  451. * Get a hash portion for HTTP caching Etags and such including
  452. * info on the current user's session. If login/logout state changes,
  453. * or we've changed accounts, or we've renamed the current user,
  454. * we'll get a new hash value.
  455. *
  456. * This should not be considered secure information.
  457. *
  458. * @param User $user (optional; uses common_current_user() if left out)
  459. * @return string
  460. */
  461. function common_user_cache_hash($user=false)
  462. {
  463. if ($user === false) {
  464. $user = common_current_user();
  465. }
  466. if ($user) {
  467. return crc32($user->id . ':' . $user->nickname);
  468. } else {
  469. return '0';
  470. }
  471. }
  472. /**
  473. * get canonical version of nickname for comparison
  474. *
  475. * @param string $nickname
  476. * @return string
  477. *
  478. * @throws NicknameException on invalid input
  479. * @deprecated call Nickname::normalize() directly.
  480. */
  481. function common_canonical_nickname($nickname)
  482. {
  483. return Nickname::normalize($nickname);
  484. }
  485. /**
  486. * get canonical version of email for comparison
  487. *
  488. * @fixme actually normalize
  489. * @fixme reject invalid input
  490. *
  491. * @param string $email
  492. * @return string
  493. */
  494. function common_canonical_email($email)
  495. {
  496. // XXX: canonicalize UTF-8
  497. // XXX: lcase the domain part
  498. return $email;
  499. }
  500. function common_to_alphanumeric($str)
  501. {
  502. $filtered = preg_replace('/[^A-Za-z0-9]\s*/', '', $str);
  503. if (strlen($filtered) < 1) {
  504. throw new Exception('Filtered string was zero-length.');
  505. }
  506. return $filtered;
  507. }
  508. function common_purify($html, array $args=array())
  509. {
  510. require_once INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php';
  511. $cfg = HTMLPurifier_Config::createDefault();
  512. /**
  513. * rel values that should be avoided since they can be used to infer
  514. * information about the _current_ page, not the h-entry:
  515. *
  516. * directory, home, license, payment
  517. *
  518. * Source: http://microformats.org/wiki/rel
  519. */
  520. $cfg->set('Attr.AllowedRel', ['bookmark', 'enclosure', 'nofollow', 'tag', 'noreferrer']);
  521. $cfg->set('HTML.ForbiddenAttributes', array('style')); // id, on* etc. are already filtered by default
  522. $cfg->set('URI.AllowedSchemes', array_fill_keys(common_url_schemes(), true));
  523. if (isset($args['URI.Base'])) {
  524. $cfg->set('URI.Base', $args['URI.Base']); // if null this is like unsetting it I presume
  525. $cfg->set('URI.MakeAbsolute', !is_null($args['URI.Base'])); // if we have a URI base, convert relative URLs to absolute ones.
  526. }
  527. if (common_config('cache', 'dir')) {
  528. $cfg->set('Cache.SerializerPath', common_config('cache', 'dir'));
  529. }
  530. // if you don't want to use the default cache dir for htmlpurifier, set it specifically as $config['htmlpurifier']['Cache.SerializerPath'] = '/tmp'; or something.
  531. foreach (common_config('htmlpurifier') as $key=>$val) {
  532. $cfg->set($key, $val);
  533. }
  534. // Remove more elements than what the default filter removes, default in GNU social are remotely
  535. // linked resources such as img, video, audio
  536. $forbiddenElements = array();
  537. foreach (common_config('htmlfilter') as $tag=>$filter) {
  538. if ($filter === true) {
  539. $forbiddenElements[] = $tag;
  540. }
  541. }
  542. $cfg->set('HTML.ForbiddenElements', $forbiddenElements);
  543. $html = common_remove_unicode_formatting($html);
  544. $purifier = new HTMLPurifier($cfg);
  545. $purified = $purifier->purify($html);
  546. Event::handle('EndCommonPurify', array(&$purified, $html));
  547. return $purified;
  548. }
  549. function common_remove_unicode_formatting($text)
  550. {
  551. // Strip Unicode text formatting/direction codes
  552. // this is pretty dangerous for visualisation of text and can be used for mischief
  553. return preg_replace('/[\\x{200b}-\\x{200f}\\x{202a}-\\x{202e}]/u', '', $text);
  554. }
  555. /**
  556. * Partial notice markup rendering step: build links to !group references.
  557. *
  558. * @param string $text partially rendered HTML
  559. * @param Profile $author the Profile that is composing the current notice
  560. * @param Notice $parent the Notice this is sent in reply to, if any
  561. * @return string partially rendered HTML
  562. */
  563. function common_render_content($text, Profile $author, Notice $parent=null)
  564. {
  565. $text = common_render_text($text);
  566. $text = common_linkify_mentions($text, $author, $parent);
  567. return $text;
  568. }
  569. /**
  570. * Finds @-mentions within the partially-rendered text section and
  571. * turns them into live links.
  572. *
  573. * Should generally not be called except from common_render_content().
  574. *
  575. * @param string $text partially-rendered HTML
  576. * @param Profile $author the Profile that is composing the current notice
  577. * @param Notice $parent the Notice this is sent in reply to, if any
  578. * @return string partially-rendered HTML
  579. */
  580. function common_linkify_mentions($text, Profile $author, Notice $parent=null)
  581. {
  582. $mentions = common_find_mentions($text, $author, $parent);
  583. // We need to go through in reverse order by position,
  584. // so our positions stay valid despite our fudging with the
  585. // string!
  586. $points = array();
  587. foreach ($mentions as $mention)
  588. {
  589. $points[$mention['position']] = $mention;
  590. }
  591. krsort($points);
  592. foreach ($points as $position => $mention) {
  593. $linkText = common_linkify_mention($mention);
  594. $text = substr_replace($text, $linkText, $position, $mention['length']);
  595. }
  596. return $text;
  597. }
  598. function common_linkify_mention(array $mention)
  599. {
  600. $output = null;
  601. if (Event::handle('StartLinkifyMention', array($mention, &$output))) {
  602. $xs = new XMLStringer(false);
  603. $attrs = array('href' => $mention['url'],
  604. 'class' => 'h-card u-url p-nickname '.$mention['type']);
  605. if (!empty($mention['title'])) {
  606. $attrs['title'] = $mention['title'];
  607. }
  608. $xs->element('a', $attrs, $mention['text']);
  609. $output = $xs->getString();
  610. Event::handle('EndLinkifyMention', array($mention, &$output));
  611. }
  612. return $output;
  613. }
  614. function common_get_attentions($text, Profile $sender, Notice $parent=null)
  615. {
  616. $mentions = common_find_mentions($text, $sender, $parent);
  617. $atts = array();
  618. foreach ($mentions as $mention) {
  619. foreach ($mention['mentioned'] as $mentioned) {
  620. $atts[$mentioned->getUri()] = $mentioned->getObjectType();
  621. }
  622. }
  623. if ($parent instanceof Notice) {
  624. $parentAuthor = $parent->getProfile();
  625. // afaik groups can't be authors
  626. $atts[$parentAuthor->getUri()] = ActivityObject::PERSON;
  627. }
  628. return $atts;
  629. }
  630. /**
  631. * Find @-mentions in the given text, using the given notice object as context.
  632. * References will be resolved with common_relative_profile() against the user
  633. * who posted the notice.
  634. *
  635. * Note the return data format is internal, to be used for building links and
  636. * such. Should not be used directly; rather, call common_linkify_mentions().
  637. *
  638. * @param string $text
  639. * @param Profile $sender the Profile that is sending the current text
  640. * @param Notice $parent the Notice this text is in reply to, if any
  641. *
  642. * @return array
  643. *
  644. * @access private
  645. */
  646. function common_find_mentions($text, Profile $sender, Notice $parent=null)
  647. {
  648. $mentions = array();
  649. if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) {
  650. // Get the context of the original notice, if any
  651. $origMentions = array();
  652. // Does it have a parent notice for context?
  653. if ($parent instanceof Notice) {
  654. foreach ($parent->getAttentionProfiles() as $repliedTo) {
  655. if (!$repliedTo->isPerson()) {
  656. continue;
  657. }
  658. $origMentions[$repliedTo->id] = $repliedTo;
  659. }
  660. }
  661. $matches = common_find_mentions_raw($text, '@');
  662. foreach ($matches as $match) {
  663. try {
  664. $nickname = Nickname::normalize($match[0]);
  665. } catch (NicknameException $e) {
  666. // Bogus match? Drop it.
  667. continue;
  668. }
  669. // primarily mention the profiles mentioned in the parent
  670. $mention_found_in_origMentions = false;
  671. foreach($origMentions as $origMentionsId=>$origMention) {
  672. if($origMention->getNickname() == $nickname) {
  673. $mention_found_in_origMentions = $origMention;
  674. // don't mention same twice! the parent might have mentioned
  675. // two users with same nickname on different instances
  676. unset($origMentions[$origMentionsId]);
  677. break;
  678. }
  679. }
  680. // Try to get a profile for this nickname.
  681. // Start with parents mentions, then go to parents sender context
  682. if ($mention_found_in_origMentions) {
  683. $mentioned = $mention_found_in_origMentions;
  684. } else if ($parent instanceof Notice && $parent->getProfile()->getNickname() === $nickname) {
  685. $mentioned = $parent->getProfile();
  686. } else {
  687. // sets to null if no match
  688. $mentioned = common_relative_profile($sender, $nickname);
  689. }
  690. if ($mentioned instanceof Profile) {
  691. try {
  692. $url = $mentioned->getUri(); // prefer the URI as URL, if it is one.
  693. if (!common_valid_http_url($url)) {
  694. $url = $mentioned->getUrl();
  695. }
  696. } catch (InvalidUrlException $e) {
  697. $url = common_local_url('userbyid', array('id' => $mentioned->getID()));
  698. }
  699. $mention = array('mentioned' => array($mentioned),
  700. 'type' => 'mention',
  701. 'text' => $match[0],
  702. 'position' => $match[1],
  703. 'length' => mb_strlen($match[0]),
  704. 'title' => $mentioned->getFullname(),
  705. 'url' => $url);
  706. $mentions[] = $mention;
  707. }
  708. }
  709. // @#tag => mention of all subscriptions tagged 'tag'
  710. preg_match_all('/'.Nickname::BEFORE_MENTIONS.'@#([\pL\pN_\-\.]{1,64})/',
  711. $text, $hmatches, PREG_OFFSET_CAPTURE);
  712. foreach ($hmatches[1] as $hmatch) {
  713. $tag = common_canonical_tag($hmatch[0]);
  714. $plist = Profile_list::getByTaggerAndTag($sender->getID(), $tag);
  715. if (!$plist instanceof Profile_list || $plist->private) {
  716. continue;
  717. }
  718. $tagged = $sender->getTaggedSubscribers($tag);
  719. $url = common_local_url('showprofiletag',
  720. array('nickname' => $sender->getNickname(),
  721. 'tag' => $tag));
  722. $mentions[] = array('mentioned' => $tagged,
  723. 'type' => 'list',
  724. 'text' => $hmatch[0],
  725. 'position' => $hmatch[1],
  726. 'length' => mb_strlen($hmatch[0]),
  727. 'url' => $url);
  728. }
  729. $hmatches = common_find_mentions_raw($text, '!');
  730. foreach ($hmatches as $hmatch) {
  731. $nickname = Nickname::normalize($hmatch[0]);
  732. $group = User_group::getForNickname($nickname, $sender);
  733. if (!$group instanceof User_group || !$sender->isMember($group)) {
  734. continue;
  735. }
  736. $profile = $group->getProfile();
  737. $mentions[] = array('mentioned' => array($profile),
  738. 'type' => 'group',
  739. 'text' => $hmatch[0],
  740. 'position' => $hmatch[1],
  741. 'length' => mb_strlen($hmatch[0]),
  742. 'url' => $group->permalink(),
  743. 'title' => $group->getFancyName());
  744. }
  745. Event::handle('EndFindMentions', array($sender, $text, &$mentions));
  746. }
  747. return $mentions;
  748. }
  749. /**
  750. * Does the actual regex pulls to find @-mentions in text.
  751. * Should generally not be called directly; for use in common_find_mentions.
  752. *
  753. * @param string $text
  754. * @param string $preMention Character(s) that signals a mention ('@', '!'...)
  755. * @return array of PCRE match arrays
  756. */
  757. function common_find_mentions_raw($text, $preMention='@')
  758. {
  759. $tmatches = array();
  760. preg_match_all('/^T (' . Nickname::DISPLAY_FMT . ') /',
  761. $text,
  762. $tmatches,
  763. PREG_OFFSET_CAPTURE);
  764. $atmatches = array();
  765. // the regexp's "(?!\@)" makes sure it doesn't matches the single "@remote" in "@remote@server.com"
  766. preg_match_all('/'.Nickname::BEFORE_MENTIONS.preg_quote($preMention, '/').'(' . Nickname::DISPLAY_FMT . ')\b(?!\@)/',
  767. $text,
  768. $atmatches,
  769. PREG_OFFSET_CAPTURE);
  770. $matches = array_merge($tmatches[1], $atmatches[1]);
  771. return $matches;
  772. }
  773. function common_render_text($text)
  774. {
  775. $text = common_remove_unicode_formatting($text);
  776. $text = nl2br(htmlspecialchars($text));
  777. $text = preg_replace('/[\x{0}-\x{8}\x{b}-\x{c}\x{e}-\x{19}]/', '', $text);
  778. $text = common_replace_urls_callback($text, 'common_linkify');
  779. $text = preg_replace_callback('/(^|\&quot\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/u',
  780. function ($m) { return "{$m[1]}#".common_tag_link($m[2]); }, $text);
  781. // XXX: machine tags
  782. return $text;
  783. }
  784. define('_URL_SCHEME_COLON_DOUBLE_SLASH', 1);
  785. define('_URL_SCHEME_SINGLE_COLON', 2);
  786. define('_URL_SCHEME_NO_DOMAIN', 4);
  787. define('_URL_SCHEME_COLON_COORDINATES', 8);
  788. function common_url_schemes($filter=null)
  789. {
  790. // TODO: move these to $config
  791. $schemes = [
  792. 'http' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  793. 'https' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  794. 'ftp' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  795. 'ftps' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  796. 'mms' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  797. 'rtsp' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  798. 'gopher' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  799. 'news' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  800. 'nntp' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  801. 'telnet' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  802. 'wais' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  803. 'file' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  804. 'prospero' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  805. 'webcal' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  806. 'irc' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  807. 'ircs' => _URL_SCHEME_COLON_DOUBLE_SLASH,
  808. 'aim' => _URL_SCHEME_SINGLE_COLON,
  809. 'bitcoin' => _URL_SCHEME_SINGLE_COLON,
  810. 'fax' => _URL_SCHEME_SINGLE_COLON,
  811. 'jabber' => _URL_SCHEME_SINGLE_COLON,
  812. 'mailto' => _URL_SCHEME_SINGLE_COLON,
  813. 'tel' => _URL_SCHEME_SINGLE_COLON,
  814. 'xmpp' => _URL_SCHEME_SINGLE_COLON,
  815. 'magnet' => _URL_SCHEME_NO_DOMAIN,
  816. 'geo' => _URL_SCHEME_COLON_COORDINATES,
  817. ];
  818. return array_keys(
  819. array_filter($schemes,
  820. function ($scheme) use ($filter) {
  821. return is_null($filter) || ($scheme & $filter);
  822. })
  823. );
  824. }
  825. /**
  826. * Find links in the given text and pass them to the given callback function.
  827. *
  828. * @param string $text
  829. * @param function($text, $arg) $callback: return replacement text
  830. * @param mixed $arg: optional argument will be passed on to the callback
  831. */
  832. function common_replace_urls_callback($text, $callback, $arg = null) {
  833. $geouri_labeltext_regex = '\pN\pL\-';
  834. $geouri_mark_regex = '\-\_\.\!\~\*\\\'\(\)'; // the \\\' is really pretty
  835. $geouri_unreserved_regex = '\pN\pL' . $geouri_mark_regex;
  836. $geouri_punreserved_regex = '\[\]\:\&\+\$';
  837. $geouri_pctencoded_regex = '(?:\%[0-9a-fA-F][0-9a-fA-F])';
  838. $geouri_paramchar_regex = $geouri_unreserved_regex . $geouri_punreserved_regex; //FIXME: add $geouri_pctencoded_regex here so it works
  839. // Start off with a regex
  840. $regex = '#'.
  841. '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'.
  842. '('.
  843. '(?:'.
  844. '(?:'. //Known protocols
  845. '(?:'.
  846. '(?:(?:' . implode('|', common_url_schemes(_URL_SCHEME_COLON_DOUBLE_SLASH)) . ')://)'.
  847. '|'.
  848. '(?:(?:' . implode('|', common_url_schemes(_URL_SCHEME_SINGLE_COLON)) . '):)'.
  849. ')'.
  850. '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@
  851. '(?:'.
  852. '(?:'.
  853. '\[[\pN\pL\-\_\:\.]+(?<![\.\:])\]'. //[dns]
  854. ')|(?:'.
  855. '[\pN\pL\-\_\:\.]+(?<![\.\:])'. //dns
  856. ')'.
  857. ')'.
  858. ')'.
  859. '|(?:'.
  860. '(?:' . implode('|', common_url_schemes(_URL_SCHEME_COLON_COORDINATES)) . '):'.
  861. // There's an order that must be followed here too, if ;crs= is used, it must precede ;u=
  862. // Also 'crsp' (;crs=$crsp) must match $geouri_labeltext_regex
  863. // Also 'uval' (;u=$uval) must be a pnum: \-?[0-9]+
  864. '(?:'.
  865. '(?:[0-9]+(?:\.[0-9]+)?(?:\,[0-9]+(?:\.[0-9]+)?){1,2})'. // 1(.23)?(,4(.56)){1,2}
  866. '(?:\;(?:['.$geouri_labeltext_regex.']+)(?:\=['.$geouri_paramchar_regex.']+)*)*'.
  867. ')'.
  868. ')'.
  869. // URLs without domain name, like magnet:?xt=...
  870. '|(?:(?:' . implode('|', common_url_schemes(_URL_SCHEME_NO_DOMAIN)) . '):(?=\?))'. // zero-length lookahead requires ? after :
  871. (common_config('linkify', 'bare_ipv4') // Convert IPv4 addresses to hyperlinks
  872. ? '|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
  873. : '').
  874. (common_config('linkify', 'bare_ipv6') // Convert IPv6 addresses to hyperlinks
  875. ? '|(?:'. //IPv6
  876. '\[?(?:(?:(?:[0-9A-Fa-f]{1,4}:){7}(?:(?:[0-9A-Fa-f]{1,4})|:))|(?:(?:[0-9A-Fa-f]{1,4}:){6}(?::|(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(?::[0-9A-Fa-f]{1,4})))|(?:(?:[0-9A-Fa-f]{1,4}:){5}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){4}(?::[0-9A-Fa-f]{1,4}){0,1}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){3}(?::[0-9A-Fa-f]{1,4}){0,2}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:){2}(?::[0-9A-Fa-f]{1,4}){0,3}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:[0-9A-Fa-f]{1,4}:)(?::[0-9A-Fa-f]{1,4}){0,4}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?::(?::[0-9A-Fa-f]{1,4}){0,5}(?:(?::(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|(?:(?::[0-9A-Fa-f]{1,4}){1,2})))|(?:(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))\]?(?<!:)'.
  877. ')'
  878. : '').
  879. (common_config('linkify', 'bare_domains')
  880. ? '|(?:'. //DNS
  881. '(?:[\pN\pL\-\_\+\%\~]+(?:\:[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@
  882. '[\pN\pL\-\_]+(?:\.[\pN\pL\-\_]+)*\.'.
  883. //tld list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt, also added local, loc, and onion
  884. '(?:AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|ASIA|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|BZ|CA|CAT|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EDU|EE|EG|ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GOV|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|ID|IE|IL|IM|IN|INFO|INT|IO|IQ|IR|IS|IT|JE|JM|JO|JOBS|JP|KE|KG|KH|KI|KM|KN|KP|KR|KW|KY|KZ|LA|LB|LC|LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|ME|MG|MH|MIL|MK|ML|MM|MN|MO|MOBI|MP|MQ|MR|MS|MT|MU|MUSEUM|MV|MW|MX|MY|MZ|NA|NAME|NC|NE|NET|NF|NG|NI|NL|NO|NP|NR|NU|NZ|OM|ORG|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PRO|PS|PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|ST|SU|SV|SY|SZ|TC|TD|TEL|TF|TG|TH|TJ|TK|TL|TM|TN|TO|TP|TR|TRAVEL|TT|TV|TW|TZ|UA|UG|UK|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|XN--0ZWM56D|测试|XN--11B5BS3A9AJ6G|परीक्षा|XN--80AKHBYKNJ4F|испытание|XN--9T4B11YI5A|테스트|XN--DEBA0AD|טעסט|XN--G6W251D|測試|XN--HGBK6AJ7F53BBA|آزمایشی|XN--HLCJ6AYA9ESC7A|பரிட்சை|XN--JXALPDLP|δοκιμή|XN--KGBECHTV|إختبار|XN--ZCKZAH|テスト|YE|YT|YU|ZA|ZM|ZONE|ZW|local|loc|onion)'.
  885. ')(?![\pN\pL\-\_])'
  886. : '') . // if common_config('linkify', 'bare_domains') is false, don't add anything here
  887. ')'.
  888. '(?:'.
  889. '(?:\:\d+)?'. //:port
  890. '(?:/[' . URL_REGEX_VALID_PATH_CHARS . ']*)?'. // path
  891. '(?:\?[' . URL_REGEX_VALID_QSTRING_CHARS . ']*)?'. // ?query string
  892. '(?:\#[' . URL_REGEX_VALID_FRAGMENT_CHARS . ']*)?'. // #fragment
  893. ')(?<!['. URL_REGEX_EXCLUDED_END_CHARS .'])'.
  894. ')'.
  895. '#ixu';
  896. //preg_match_all($regex,$text,$matches);
  897. //print_r($matches);
  898. return preg_replace_callback($regex, curry('callback_helper',$callback,$arg) ,$text);
  899. }
  900. /**
  901. * Intermediate callback for common_replace_links(), helps resolve some
  902. * ambiguous link forms before passing on to the final callback.
  903. *
  904. * @param array $matches
  905. * @param callable $callback
  906. * @param mixed $arg optional argument to pass on as second param to callback
  907. * @return string
  908. *
  909. * @access private
  910. */
  911. function callback_helper($matches, $callback, $arg=null) {
  912. $url=$matches[1];
  913. $left = strpos($matches[0],$url);
  914. $right = $left+strlen($url);
  915. $groupSymbolSets=array(
  916. array(
  917. 'left'=>'(',
  918. 'right'=>')'
  919. ),
  920. array(
  921. 'left'=>'[',
  922. 'right'=>']'
  923. ),
  924. array(
  925. 'left'=>'{',
  926. 'right'=>'}'
  927. ),
  928. array(
  929. 'left'=>'<',
  930. 'right'=>'>'
  931. )
  932. );
  933. $cannotEndWith=array('.','?',',','#');
  934. $original_url=$url;
  935. do{
  936. $original_url=$url;
  937. foreach($groupSymbolSets as $groupSymbolSet){
  938. if(substr($url,-1)==$groupSymbolSet['right']){
  939. $group_left_count = substr_count($url,$groupSymbolSet['left']);
  940. $group_right_count = substr_count($url,$groupSymbolSet['right']);
  941. if($group_left_count<$group_right_count){
  942. $right-=1;
  943. $url=substr($url,0,-1);
  944. }
  945. }
  946. }
  947. if(in_array(substr($url,-1),$cannotEndWith)){
  948. $right-=1;
  949. $url=substr($url,0,-1);
  950. }
  951. }while($original_url!=$url);
  952. $result = call_user_func_array($callback, array($url, $arg));
  953. return substr($matches[0],0,$left) . $result . substr($matches[0],$right);
  954. }
  955. require_once INSTALLDIR . "/lib/curry.php";
  956. function common_linkify($url) {
  957. // It comes in special'd, so we unspecial it before passing to the stringifying
  958. // functions
  959. $url = htmlspecialchars_decode($url);
  960. if (strpos($url, '@') !== false && strpos($url, ':') === false && Validate::email($url)) {
  961. //url is an email address without the mailto: protocol
  962. $canon = "mailto:$url";
  963. $longurl = "mailto:$url";
  964. } else {
  965. $canon = File_redirection::_canonUrl($url);
  966. $longurl_data = File_redirection::where($canon, common_config('attachments', 'process_links'));
  967. if(isset($longurl_data->redir_url)) {
  968. $longurl = $longurl_data->redir_url;
  969. } else {
  970. // e.g. local files
  971. $longurl = $longurl_data->url;
  972. }
  973. }
  974. $attrs = array('href' => $longurl, 'title' => $longurl);
  975. $is_attachment = false;
  976. $attachment_id = null;
  977. $has_thumb = false;
  978. // Check to see whether this is a known "attachment" URL.
  979. try {
  980. $f = File::getByUrl($longurl);
  981. } catch (NoResultException $e) {
  982. if (common_config('attachments', 'process_links')) {
  983. // XXX: this writes to the database. :<
  984. try {
  985. $f = File::processNew($longurl);
  986. } catch (ServerException $e) {
  987. $f = null;
  988. }
  989. }
  990. }
  991. if ($f instanceof File) {
  992. try {
  993. $enclosure = $f->getEnclosure();
  994. $is_attachment = true;
  995. $attachment_id = $f->id;
  996. $thumb = File_thumbnail::getKV('file_id', $f->id);
  997. $has_thumb = ($thumb instanceof File_thumbnail);
  998. } catch (ServerException $e) {
  999. // There was not enough metadata available
  1000. }
  1001. }
  1002. // Whether to nofollow
  1003. $nf = common_config('nofollow', 'external');
  1004. if ($nf == 'never') {
  1005. $attrs['rel'] = 'external';
  1006. } else {
  1007. $attrs['rel'] = 'nofollow external';
  1008. }
  1009. // Add clippy
  1010. if ($is_attachment) {
  1011. $attrs['class'] = 'attachment';
  1012. if ($has_thumb) {
  1013. $attrs['class'] = 'attachment thumbnail';
  1014. }
  1015. $attrs['id'] = "attachment-{$attachment_id}";
  1016. $attrs['rel'] .= ' noreferrer';
  1017. }
  1018. return XMLStringer::estring('a', $attrs, $url);
  1019. }
  1020. /**
  1021. * Find and shorten links in a given chunk of text if it's longer than the
  1022. * configured notice content limit (or unconditionally).
  1023. *
  1024. * Side effects: may save file and file_redirection records for referenced URLs.
  1025. *
  1026. * Pass the $user option or call $user->shortenLinks($text) to ensure the proper
  1027. * user's options are used; otherwise the current web session user's setitngs
  1028. * will be used or ur1.ca if there is no active web login.
  1029. *
  1030. * @param string $text
  1031. * @param boolean $always (optional)
  1032. * @param User $user (optional)
  1033. *
  1034. * @return string
  1035. */
  1036. function common_shorten_links($text, $always = false, User $user=null)
  1037. {
  1038. if ($user === null) {
  1039. $user = common_current_user();
  1040. }
  1041. $maxLength = User_urlshortener_prefs::maxNoticeLength($user);
  1042. if ($always || ($maxLength != -1 && mb_strlen($text) > $maxLength)) {
  1043. return common_replace_urls_callback($text, array('File_redirection', 'forceShort'), $user);
  1044. } else {
  1045. return common_replace_urls_callback($text, array('File_redirection', 'makeShort'), $user);
  1046. }
  1047. }
  1048. /**
  1049. * Very basic stripping of invalid UTF-8 input text.
  1050. *
  1051. * @param string $str
  1052. * @return mixed string or null if invalid input
  1053. *
  1054. * @todo ideally we should drop bad chars, and maybe do some of the checks
  1055. * from common_xml_safe_str. But we can't strip newlines, etc.
  1056. * @todo Unicode normalization might also be useful, but not needed now.
  1057. */
  1058. function common_validate_utf8($str)
  1059. {
  1060. // preg_replace will return NULL on invalid UTF-8 input.
  1061. //
  1062. // Note: empty regex //u also caused NULL return on some
  1063. // production machines, but none of our test machines.
  1064. //
  1065. // This should be replaced with a more reliable check.
  1066. return preg_replace('/\x00/u', '', $str);
  1067. }
  1068. /**
  1069. * Make sure an arbitrary string is safe for output in XML as a single line.
  1070. *
  1071. * @param string $str
  1072. * @return string
  1073. */
  1074. function common_xml_safe_str($str)
  1075. {
  1076. // Replace common eol and extra whitespace input chars
  1077. $unWelcome = array(
  1078. "\t", // tab
  1079. "\n", // newline
  1080. "\r", // cr
  1081. "\0", // null byte eos
  1082. "\x0B" // vertical tab
  1083. );
  1084. $replacement = array(
  1085. ' ', // single space
  1086. ' ',
  1087. '', // nothing
  1088. '',
  1089. ' '
  1090. );
  1091. $str = str_replace($unWelcome, $replacement, $str);
  1092. // Neutralize any additional control codes and UTF-16 surrogates
  1093. // (Twitter uses '*')
  1094. return preg_replace('/[\p{Cc}\p{Cs}]/u', '*', $str);
  1095. }
  1096. function common_slugify($str)
  1097. {
  1098. // php5-intl is highly recommended...
  1099. if (!function_exists('transliterator_transliterate')) {
  1100. $str = preg_replace('/[^\pL\pN]/u', '', $str);
  1101. $str = mb_convert_case($str, MB_CASE_LOWER, 'UTF-8');
  1102. $str = substr($str, 0, 64);
  1103. return $str;
  1104. }
  1105. $str = transliterator_transliterate(
  1106. 'Any-Latin;' . // any charset to latin compatible
  1107. 'NFD;' . // decompose
  1108. '[:Nonspacing Mark:] Remove;' . // remove nonspacing marks (accents etc.)
  1109. 'NFC;' . // composite again
  1110. '[:Punctuation:] Remove;' . // remove punctuation (.,¿? etc.)
  1111. 'Lower();' . // turn into lowercase
  1112. 'Latin-ASCII;', // get ASCII equivalents (ð to d for example)
  1113. $str);
  1114. return preg_replace('/[^\pL\pN]/', '', $str);
  1115. }
  1116. function common_tag_link($tag)
  1117. {
  1118. $canonical = common_canonical_tag($tag);
  1119. if (common_config('singleuser', 'enabled')) {
  1120. // regular TagAction isn't set up in 1user mode
  1121. $nickname = User::singleUserNickname();
  1122. $url = common_local_url('showstream',
  1123. array('nickname' => $nickname,
  1124. 'tag' => $canonical));
  1125. } else {
  1126. $url = common_local_url('tag', array('tag' => $canonical));
  1127. }
  1128. $xs = new XMLStringer();
  1129. $xs->elementStart('span', 'tag');
  1130. $xs->element('a', array('href' => $url,
  1131. 'rel' => 'tag'),
  1132. $tag);
  1133. $xs->elementEnd('span');
  1134. return $xs->getString();
  1135. }
  1136. function common_canonical_tag($tag)
  1137. {
  1138. $tag = common_slugify($tag);
  1139. $tag = substr($tag, 0, 64);
  1140. return $tag;
  1141. }
  1142. function common_valid_profile_tag($str)
  1143. {
  1144. return preg_match('/^[A-Za-z0-9_\-\.]{1,64}$/', $str);
  1145. }
  1146. /**
  1147. * Resolve an ambiguous profile nickname reference, checking in following order:
  1148. * - profiles that $sender subscribes to
  1149. * - profiles that subscribe to $sender
  1150. * - local user profiles
  1151. *
  1152. * WARNING: does not validate or normalize $nickname -- MUST BE PRE-VALIDATED
  1153. * OR THERE MAY BE A RISK OF SQL INJECTION ATTACKS. THIS FUNCTION DOES NOT
  1154. * ESCAPE SQL.
  1155. *
  1156. * @fixme validate input
  1157. * @fixme escape SQL
  1158. * @fixme fix or remove mystery third parameter
  1159. * @fixme is $sender a User or Profile?
  1160. *
  1161. * @param <type> $sender the user or profile in whose context we're looking
  1162. * @param string $nickname validated nickname of
  1163. * @param <type> $dt unused mystery parameter; in Notice reply-to handling a timestamp is passed.
  1164. *
  1165. * @return Profile or null
  1166. */
  1167. function common_relative_profile($sender, $nickname, $dt=null)
  1168. {
  1169. // Will throw exception on invalid input.
  1170. $nickname = Nickname::normalize($nickname);
  1171. // Try to find profiles this profile is subscribed to that have this nickname
  1172. $recipient = new Profile();
  1173. // XXX: use a join instead of a subquery
  1174. $recipient->whereAdd('EXISTS (SELECT subscribed from subscription where subscriber = '.intval($sender->id).' and subscribed = id)', 'AND');
  1175. $recipient->whereAdd("nickname = '" . $recipient->escape($nickname) . "'", 'AND');
  1176. if ($recipient->find(true)) {
  1177. // XXX: should probably differentiate between profiles with
  1178. // the same name by date of most recent update
  1179. return $recipient;
  1180. }
  1181. // Try to find profiles that listen to this profile and that have this nickname
  1182. $recipient = new Profile();
  1183. // XXX: use a join instead of a subquery
  1184. $recipient->whereAdd('EXISTS (SELECT subscriber from subscription where subscribed = '.intval($sender->id).' and subscriber = id)', 'AND');
  1185. $recipient->whereAdd("nickname = '" . $recipient->escape($nickname) . "'", 'AND');
  1186. if ($recipient->find(true)) {
  1187. // XXX: should probably differentiate between profiles with
  1188. // the same name by date of most recent update
  1189. return $recipient;
  1190. }
  1191. // If this is a local user, try to find a local user with that nickname.
  1192. $sender = User::getKV('id', $sender->id);
  1193. if ($sender instanceof User) {
  1194. $recipient_user = User::getKV('nickname', $nickname);
  1195. if ($recipient_user instanceof User) {
  1196. return $recipient_user->getProfile();
  1197. }
  1198. }
  1199. // Otherwise, no links. @messages from local users to remote users,
  1200. // or from remote users to other remote users, are just
  1201. // outside our ability to make intelligent guesses about
  1202. return null;
  1203. }
  1204. function common_local_url($action, $args=null, $params=null, $fragment=null, $addSession=true)
  1205. {
  1206. if (Event::handle('StartLocalURL', array(&$action, &$params, &$fragment, &$addSession, &$url))) {
  1207. $r = Router::get();
  1208. $path = $r->build($action, $args, $params, $fragment);
  1209. $ssl = GNUsocial::useHTTPS();
  1210. if (common_config('site','fancy')) {
  1211. $url = common_path($path, $ssl, $addSession);
  1212. } else {
  1213. if (mb_strpos($path, '/index.php') === 0) {
  1214. $url = common_path($path, $ssl, $addSession);
  1215. } else {
  1216. $url = common_path('index.php/'.$path, $ssl, $addSession);
  1217. }
  1218. }
  1219. Event::handle('EndLocalURL', array(&$action, &$params, &$fragment, &$addSession, &$url));
  1220. }
  1221. return $url;
  1222. }
  1223. function common_path($relative, $ssl=false, $addSession=true)
  1224. {
  1225. $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
  1226. if ($ssl && GNUsocial::useHTTPS()) {
  1227. $proto = 'https';
  1228. if (is_string(common_config('site', 'sslserver')) &&
  1229. mb_strlen(common_config('site', 'sslserver')) > 0) {
  1230. $serverpart = common_config('site', 'sslserver');
  1231. } else if (common_config('site', 'server')) {
  1232. $serverpart = common_config('site', 'server');
  1233. } else {
  1234. common_log(LOG_ERR, 'Site server not configured, unable to determine site name.');
  1235. }
  1236. } else {
  1237. $proto = 'http';
  1238. if (common_config('site', 'server')) {
  1239. $serverpart = common_config('site', 'server');
  1240. } else {
  1241. common_log(LOG_ERR, 'Site server not configured, unable to determine site name.');
  1242. }
  1243. }
  1244. if ($addSession) {
  1245. $relative = common_inject_session($relative, $serverpart);
  1246. }
  1247. return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
  1248. }
  1249. // FIXME: Maybe this should also be able to handle non-fancy URLs with index.php?p=...
  1250. function common_fake_local_fancy_url($url)
  1251. {
  1252. /**
  1253. * This is a hacky fix to make URIs generated with "index.php/" match against
  1254. * locally stored URIs without that. So for example if the remote site is looking
  1255. * up the webfinger for some user and for some reason knows about https://some.example/user/1
  1256. * but we locally store and report only https://some.example/index.php/user/1 then they would
  1257. * dismiss the profile for not having an identified alias.
  1258. *
  1259. * There are various live instances where these issues occur, for various reasons.
  1260. * Most of them being users fiddling with configuration while already having
  1261. * started federating (distributing the URI to other servers) or maybe manually
  1262. * editing the local database.
  1263. */
  1264. if (!preg_match(
  1265. // [1] protocol part, we can only rewrite http/https anyway.
  1266. '/^(https?:\/\/)' .
  1267. // [2] site name.
  1268. // FIXME: Dunno how this acts if we're aliasing ourselves with a .onion domain etc.
  1269. '('.preg_quote(common_config('site', 'server'), '/').')' .
  1270. // [3] site path, or if that is empty just '/' (to retain the /)
  1271. '('.preg_quote(common_config('site', 'path') ?: '/', '/').')' .
  1272. // [4] + [5] extract index.php (+ possible leading double /) and the rest of the URL separately.
  1273. '(\/?index\.php\/)(.*)$/', $url, $matches)) {
  1274. // if preg_match failed to match
  1275. throw new Exception('No known change could be made to the URL.');
  1276. }
  1277. // now reconstruct the URL with everything except the "index.php/" part
  1278. $fancy_url = '';
  1279. foreach ([1,2,3,5] as $idx) {
  1280. $fancy_url .= $matches[$idx];
  1281. }
  1282. return $fancy_url;
  1283. }
  1284. // FIXME: Maybe this should also be able to handle non-fancy URLs with index.php?p=...
  1285. function common_fake_local_nonfancy_url($url)
  1286. {
  1287. /**
  1288. * This is a hacky fix to make URIs NOT generated with "index.php/" match against
  1289. * locally stored URIs WITH that. The reverse from the above.
  1290. *
  1291. * It will also "repair" index.php URLs with multiple / prepended. Like https://some.example///index.php/user/1
  1292. */
  1293. if (!preg_match(
  1294. // [1] protocol part, we can only rewrite http/https anyway.
  1295. '/^(https?:\/\/)' .
  1296. // [2] site name.
  1297. // FIXME: Dunno how this acts if we're aliasing ourselves with a .onion domain etc.
  1298. '('.preg_quote(common_config('site', 'server'), '/').')' .
  1299. // [3] site path, or if that is empty just '/' (to retain the /)
  1300. '('.preg_quote(common_config('site', 'path') ?: '/', '/').')' .
  1301. // [4] should be empty (might contain one or more / and then maybe also index.php). Will be overwritten.
  1302. // [5] will have the extracted actual URL part (besides site path)
  1303. '((?!index.php\/)\/*(?:index.php\/)?)(.*)$/', $url, $matches)) {
  1304. // if preg_match failed to match
  1305. throw new Exception('No known change could be made to the URL.');
  1306. }
  1307. $matches[4] = 'index.php/'; // inject the index.php/ rewritethingy
  1308. // remove the first element, which is the full matching string
  1309. array_shift($matches);
  1310. return implode($matches);
  1311. }
  1312. function common_inject_session($url, $serverpart = null)
  1313. {
  1314. if (!common_have_session()) {
  1315. return $url;
  1316. }
  1317. if (empty($serverpart)) {
  1318. $serverpart = parse_url($url, PHP_URL_HOST);
  1319. }
  1320. $currentServer = (array_key_exists('HTTP_HOST', $_SERVER)) ? $_SERVER['HTTP_HOST'] : null;
  1321. // Are we pointing to another server (like an SSL server?)
  1322. if (!empty($currentServer) && 0 != strcasecmp($currentServer, $serverpart)) {
  1323. // Pass the session ID as a GET parameter
  1324. $sesspart = session_name() . '=' . session_id();
  1325. $i = strpos($url, '?');
  1326. if ($i === false) { // no GET params, just append
  1327. $url .= '?' . $sesspart;
  1328. } else {
  1329. $url = substr($url, 0, $i + 1).$sesspart.'&'.substr($url, $i + 1);
  1330. }
  1331. }
  1332. return $url;
  1333. }
  1334. function common_date_string($dt)
  1335. {
  1336. // XXX: do some sexy date formatting
  1337. // return date(DATE_RFC822, $dt);
  1338. $t = strtotime($dt);
  1339. $now = time();
  1340. $diff = $now - $t;
  1341. if ($now < $t) { // that shouldn't happen!
  1342. return common_exact_date($dt);
  1343. } else if ($diff < 60) {
  1344. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1345. return _('a few seconds ago');
  1346. } else if ($diff < 92) {
  1347. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1348. return _('about a minute ago');
  1349. } else if ($diff < 3300) {
  1350. $minutes = round($diff/60);
  1351. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1352. return sprintf( _m('about one minute ago', 'about %d minutes ago', $minutes), $minutes);
  1353. } else if ($diff < 5400) {
  1354. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1355. return _('about an hour ago');
  1356. } else if ($diff < 22 * 3600) {
  1357. $hours = round($diff/3600);
  1358. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1359. return sprintf( _m('about one hour ago', 'about %d hours ago', $hours), $hours);
  1360. } else if ($diff < 37 * 3600) {
  1361. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1362. return _('about a day ago');
  1363. } else if ($diff < 24 * 24 * 3600) {
  1364. $days = round($diff/(24*3600));
  1365. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1366. return sprintf( _m('about one day ago', 'about %d days ago', $days), $days);
  1367. } else if ($diff < 46 * 24 * 3600) {
  1368. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1369. return _('about a month ago');
  1370. } else if ($diff < 330 * 24 * 3600) {
  1371. $months = round($diff/(30*24*3600));
  1372. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1373. return sprintf( _m('about one month ago', 'about %d months ago',$months), $months);
  1374. } else if ($diff < 480 * 24 * 3600) {
  1375. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1376. return _('about a year ago');
  1377. } else {
  1378. return common_exact_date($dt);
  1379. }
  1380. }
  1381. function common_exact_date($dt)
  1382. {
  1383. static $_utc;
  1384. static $_siteTz;
  1385. if (!$_utc) {
  1386. $_utc = new DateTimeZone('UTC');
  1387. $_siteTz = new DateTimeZone(common_timezone());
  1388. }
  1389. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1390. $d = new DateTime($dateStr, $_utc);
  1391. $d->setTimezone($_siteTz);
  1392. // TRANS: Human-readable full date-time specification (formatting on http://php.net/date)
  1393. return $d->format(_('l, d-M-Y H:i:s T'));
  1394. }
  1395. function common_date_w3dtf($dt)
  1396. {
  1397. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1398. $d = new DateTime($dateStr, new DateTimeZone('UTC'));
  1399. $d->setTimezone(new DateTimeZone(common_timezone()));
  1400. return $d->format(DATE_W3C);
  1401. }
  1402. function common_date_rfc2822($dt)
  1403. {
  1404. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1405. $d = new DateTime($dateStr, new DateTimeZone('UTC'));
  1406. $d->setTimezone(new DateTimeZone(common_timezone()));
  1407. return $d->format('r');
  1408. }
  1409. function common_date_iso8601($dt)
  1410. {
  1411. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1412. $d = new DateTime($dateStr, new DateTimeZone('UTC'));
  1413. $d->setTimezone(new DateTimeZone(common_timezone()));
  1414. return $d->format('c');
  1415. }
  1416. function common_sql_now()
  1417. {
  1418. return common_sql_date(time());
  1419. }
  1420. function common_sql_date($datetime)
  1421. {
  1422. return strftime('%Y-%m-%d %H:%M:%S', $datetime);
  1423. }
  1424. /**
  1425. * Return an SQL fragment to calculate an age-based weight from a given
  1426. * timestamp or datetime column.
  1427. *
  1428. * @param string $column name of field we're comparing against current time
  1429. * @param integer $dropoff divisor for age in seconds before exponentiation
  1430. * @return string SQL fragment
  1431. */
  1432. function common_sql_weight($column, $dropoff)
  1433. {
  1434. if (common_config('db', 'type') == 'pgsql') {
  1435. // PostgreSQL doesn't support timestampdiff function.
  1436. // @fixme will this use the right time zone?
  1437. // @fixme does this handle cross-year subtraction correctly?
  1438. return "sum(exp(-extract(epoch from (now() - $column)) / $dropoff))";
  1439. } else {
  1440. return "sum(exp(timestampdiff(second, utc_timestamp(), $column) / $dropoff))";
  1441. }
  1442. }
  1443. function common_redirect($url, $code=307)
  1444. {
  1445. static $status = array(301 => "Moved Permanently",
  1446. 302 => "Found",
  1447. 303 => "See Other",
  1448. 307 => "Temporary Redirect");
  1449. header('HTTP/1.1 '.$code.' '.$status[$code]);
  1450. header("Location: $url");
  1451. header("Connection: close");
  1452. $xo = new XMLOutputter();
  1453. $xo->startXML('a',
  1454. '-//W3C//DTD XHTML 1.0 Strict//EN',
  1455. 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
  1456. $xo->element('a', array('href' => $url), $url);
  1457. $xo->endXML();
  1458. exit;
  1459. }
  1460. // Stick the notice on the queue
  1461. function common_enqueue_notice($notice)
  1462. {
  1463. static $localTransports = array('ping');
  1464. $transports = array();
  1465. if (common_config('sms', 'enabled')) {
  1466. $transports[] = 'sms';
  1467. }
  1468. if (Event::hasHandler('HandleQueuedNotice')) {
  1469. $transports[] = 'plugin';
  1470. }
  1471. // We can skip these for gatewayed notices.
  1472. if ($notice->isLocal()) {
  1473. $transports = array_merge($transports, $localTransports);
  1474. }
  1475. if (Event::handle('StartEnqueueNotice', array($notice, &$transports))) {
  1476. $qm = QueueManager::get();
  1477. foreach ($transports as $transport)
  1478. {
  1479. $qm->enqueue($notice, $transport);
  1480. }
  1481. Event::handle('EndEnqueueNotice', array($notice, $transports));
  1482. }
  1483. return true;
  1484. }
  1485. function common_profile_url($nickname)
  1486. {
  1487. return common_local_url('showstream', array('nickname' => $nickname),
  1488. null, null, false);
  1489. }
  1490. /**
  1491. * Should make up a reasonable root URL
  1492. *
  1493. * @param bool $tls true or false to force TLS scheme, null to use server configuration
  1494. */
  1495. function common_root_url($tls=null)
  1496. {
  1497. if (is_null($tls)) {
  1498. $tls = GNUsocial::useHTTPS();
  1499. }
  1500. $url = common_path('', $tls, false);
  1501. $i = strpos($url, '?');
  1502. if ($i !== false) {
  1503. $url = substr($url, 0, $i);
  1504. }
  1505. return $url;
  1506. }
  1507. /**
  1508. * returns $bytes bytes of raw random data
  1509. */
  1510. function common_random_rawstr($bytes)
  1511. {
  1512. $rawstr = @file_exists('/dev/urandom')
  1513. ? common_urandom($bytes)
  1514. : common_mtrand($bytes);
  1515. return $rawstr;
  1516. }
  1517. /**
  1518. * returns $bytes bytes of random data as a hexadecimal string
  1519. */
  1520. function common_random_hexstr($bytes)
  1521. {
  1522. $str = common_random_rawstr($bytes);
  1523. $hexstr = '';
  1524. for ($i = 0; $i < $bytes; $i++) {
  1525. $hexstr .= sprintf("%02x", ord($str[$i]));
  1526. }
  1527. return $hexstr;
  1528. }
  1529. function common_urandom($bytes)
  1530. {
  1531. $h = fopen('/dev/urandom', 'rb');
  1532. // should not block
  1533. $src = fread($h, $bytes);
  1534. fclose($h);
  1535. return $src;
  1536. }
  1537. function common_mtrand($bytes)
  1538. {
  1539. $str = '';
  1540. for ($i = 0; $i < $bytes; $i++) {
  1541. $str .= chr(mt_rand(0, 255));
  1542. }
  1543. return $str;
  1544. }
  1545. /**
  1546. * Record the given URL as the return destination for a future
  1547. * form submission, to be read by common_get_returnto().
  1548. *
  1549. * @param string $url
  1550. *
  1551. * @fixme as a session-global setting, this can allow multiple forms
  1552. * to conflict and overwrite each others' returnto destinations if
  1553. * the user has multiple tabs or windows open.
  1554. *
  1555. * Should refactor to index with a token or otherwise only pass the
  1556. * data along its intended path.
  1557. */
  1558. function common_set_returnto($url)
  1559. {
  1560. common_ensure_session();
  1561. $_SESSION['returnto'] = $url;
  1562. }
  1563. /**
  1564. * Fetch a return-destination URL previously recorded by
  1565. * common_set_returnto().
  1566. *
  1567. * @return mixed URL string or null
  1568. *
  1569. * @fixme as a session-global setting, this can allow multiple forms
  1570. * to conflict and overwrite each others' returnto destinations if
  1571. * the user has multiple tabs or windows open.
  1572. *
  1573. * Should refactor to index with a token or otherwise only pass the
  1574. * data along its intended path.
  1575. */
  1576. function common_get_returnto()
  1577. {
  1578. common_ensure_session();
  1579. return (array_key_exists('returnto', $_SESSION)) ? $_SESSION['returnto'] : null;
  1580. }
  1581. function common_timestamp()
  1582. {
  1583. return date('YmdHis');
  1584. }
  1585. function common_ensure_syslog()
  1586. {
  1587. static $initialized = false;
  1588. if (!$initialized) {
  1589. openlog(common_config('syslog', 'appname'), 0,
  1590. common_config('syslog', 'facility'));
  1591. $initialized = true;
  1592. }
  1593. }
  1594. function common_log_line($priority, $msg)
  1595. {
  1596. static $syslog_priorities = array('LOG_EMERG', 'LOG_ALERT', 'LOG_CRIT', 'LOG_ERR',
  1597. 'LOG_WARNING', 'LOG_NOTICE', 'LOG_INFO', 'LOG_DEBUG');
  1598. return date('Y-m-d H:i:s') . ' ' . $syslog_priorities[$priority] . ': ' . $msg . PHP_EOL;
  1599. }
  1600. function common_request_id()
  1601. {
  1602. $pid = getmypid();
  1603. $server = common_config('site', 'server');
  1604. if (php_sapi_name() == 'cli') {
  1605. $script = basename($_SERVER['PHP_SELF']);
  1606. return "$server:$script:$pid";
  1607. } else {
  1608. static $req_id = null;
  1609. if (!isset($req_id)) {
  1610. $req_id = substr(md5(mt_rand()), 0, 8);
  1611. }
  1612. if (isset($_SERVER['REQUEST_URI'])) {
  1613. $url = $_SERVER['REQUEST_URI'];
  1614. }
  1615. $method = $_SERVER['REQUEST_METHOD'];
  1616. return "$server:$pid.$req_id $method $url";
  1617. }
  1618. }
  1619. function common_log($priority, $msg, $filename=null)
  1620. {
  1621. if(Event::handle('StartLog', array(&$priority, &$msg, &$filename))){
  1622. $msg = (empty($filename)) ? $msg : basename($filename) . ' - ' . $msg;
  1623. $msg = '[' . common_request_id() . '] ' . $msg;
  1624. $logfile = common_config('site', 'logfile');
  1625. if ($logfile) {
  1626. $log = fopen($logfile, "a");
  1627. if ($log) {
  1628. $output = common_log_line($priority, $msg);
  1629. fwrite($log, $output);
  1630. fclose($log);
  1631. }
  1632. } else {
  1633. common_ensure_syslog();
  1634. syslog($priority, $msg);
  1635. }
  1636. Event::handle('EndLog', array($priority, $msg, $filename));
  1637. }
  1638. }
  1639. function common_debug($msg, $filename=null)
  1640. {
  1641. if ($filename) {
  1642. common_log(LOG_DEBUG, basename($filename).' - '.$msg);
  1643. } else {
  1644. common_log(LOG_DEBUG, $msg);
  1645. }
  1646. }
  1647. function common_log_db_error(&$object, $verb, $filename=null)
  1648. {
  1649. global $_PEAR;
  1650. $objstr = common_log_objstring($object);
  1651. $last_error = &$_PEAR->getStaticProperty('DB_DataObject','lastError');
  1652. if (is_object($last_error)) {
  1653. $msg = $last_error->message;
  1654. } else {
  1655. $msg = 'Unknown error (' . var_export($last_error, true) . ')';
  1656. }
  1657. common_log(LOG_ERR, $msg . '(' . $verb . ' on ' . $objstr . ')', $filename);
  1658. }
  1659. function common_log_objstring(&$object)
  1660. {
  1661. if (is_null($object)) {
  1662. return "null";
  1663. }
  1664. if (!($object instanceof DB_DataObject)) {
  1665. return "(unknown)";
  1666. }
  1667. $arr = $object->toArray();
  1668. $fields = array();
  1669. foreach ($arr as $k => $v) {
  1670. if (is_object($v)) {
  1671. $fields[] = "$k='".get_class($v)."'";
  1672. } else {
  1673. $fields[] = "$k='$v'";
  1674. }
  1675. }
  1676. $objstring = $object->tableName() . '[' . implode(',', $fields) . ']';
  1677. return $objstring;
  1678. }
  1679. function common_valid_http_url($url, $secure=false)
  1680. {
  1681. if (empty($url)) {
  1682. return false;
  1683. }
  1684. // If $secure is true, only allow https URLs to pass
  1685. // (if false, we use '?' in 'https?' to say the 's' is optional)
  1686. $regex = $secure ? '/^https$/' : '/^https?$/';
  1687. return filter_var($url, FILTER_VALIDATE_URL)
  1688. && preg_match($regex, parse_url($url, PHP_URL_SCHEME));
  1689. }
  1690. function common_valid_tag($tag)
  1691. {
  1692. if (preg_match('/^tag:(.*?),(\d{4}(-\d{2}(-\d{2})?)?):(.*)$/', $tag, $matches)) {
  1693. return (Validate::email($matches[1]) ||
  1694. preg_match('/^([\w-\.]+)$/', $matches[1]));
  1695. }
  1696. return false;
  1697. }
  1698. /**
  1699. * Determine if given domain or address literal is valid
  1700. * eg for use in JIDs and URLs. Does not check if the domain
  1701. * exists!
  1702. *
  1703. * @param string $domain
  1704. * @return boolean valid or not
  1705. */
  1706. function common_valid_domain($domain)
  1707. {
  1708. $octet = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])";
  1709. $ipv4 = "(?:$octet(?:\.$octet){3})";
  1710. if (preg_match("/^$ipv4$/u", $domain)) return true;
  1711. $group = "(?:[0-9a-f]{1,4})";
  1712. $ipv6 = "(?:\[($group(?::$group){0,7})?(::)?($group(?::$group){0,7})?\])"; // http://tools.ietf.org/html/rfc3513#section-2.2
  1713. if (preg_match("/^$ipv6$/ui", $domain, $matches)) {
  1714. $before = explode(":", $matches[1]);
  1715. $zeroes = $matches[2];
  1716. $after = explode(":", $matches[3]);
  1717. if ($zeroes) {
  1718. $min = 0;
  1719. $max = 7;
  1720. } else {
  1721. $min = 1;
  1722. $max = 8;
  1723. }
  1724. $explicit = count($before) + count($after);
  1725. if ($explicit < $min || $explicit > $max) {
  1726. return false;
  1727. }
  1728. return true;
  1729. }
  1730. try {
  1731. require_once "Net/IDNA.php";
  1732. $idn = Net_IDNA::getInstance();
  1733. $domain = $idn->encode($domain);
  1734. } catch (Exception $e) {
  1735. return false;
  1736. }
  1737. $subdomain = "(?:[a-z0-9][a-z0-9-]*)"; // @fixme
  1738. $fqdn = "(?:$subdomain(?:\.$subdomain)*\.?)";
  1739. return preg_match("/^$fqdn$/ui", $domain);
  1740. }
  1741. /* Following functions are copied from MediaWiki GlobalFunctions.php
  1742. * and written by Evan Prodromou. */
  1743. function common_accept_to_prefs($accept, $def = '*/*')
  1744. {
  1745. // No arg means accept anything (per HTTP spec)
  1746. if(!$accept) {
  1747. return array($def => 1);
  1748. }
  1749. $prefs = array();
  1750. $parts = explode(',', $accept);
  1751. foreach($parts as $part) {
  1752. // FIXME: doesn't deal with params like 'text/html; level=1'
  1753. @list($value, $qpart) = explode(';', trim($part));
  1754. $match = array();
  1755. if(!isset($qpart)) {
  1756. $prefs[$value] = 1;
  1757. } elseif(preg_match('/q\s*=\s*(\d*\.\d+)/', $qpart, $match)) {
  1758. $prefs[$value] = $match[1];
  1759. }
  1760. }
  1761. return $prefs;
  1762. }
  1763. // Match by our supported file extensions
  1764. function common_supported_filename_to_mime($filename)
  1765. {
  1766. // Accept a filename and take out the extension
  1767. if (strpos($filename, '.') === false) {
  1768. throw new ServerException(sprintf('No extension on filename: %1$s', _ve($filename)));
  1769. }
  1770. $fileext = substr(strrchr($filename, '.'), 1);
  1771. return common_supported_ext_to_mime($fileext);
  1772. }
  1773. function common_supported_ext_to_mime($fileext)
  1774. {
  1775. $supported = common_config('attachments', 'supported');
  1776. if ($supported === true) {
  1777. // FIXME: Should we just accept the extension straight off when supported === true?
  1778. throw new UnknownExtensionMimeException($fileext);
  1779. }
  1780. foreach($supported as $type => $ext) {
  1781. if ($ext === $fileext) {
  1782. return $type;
  1783. }
  1784. }
  1785. throw new ServerException('Unsupported file extension');
  1786. }
  1787. // Match by our supported mime types
  1788. function common_supported_mime_to_ext($mimetype)
  1789. {
  1790. $supported = common_config('attachments', 'supported');
  1791. if (is_array($supported)) {
  1792. foreach($supported as $type => $ext) {
  1793. if ($mimetype === $type) {
  1794. return $ext;
  1795. }
  1796. }
  1797. }
  1798. throw new UnknownMimeExtensionException($mimetype);
  1799. }
  1800. // The MIME "media" is the part before the slash (video in video/webm)
  1801. function common_get_mime_media($type)
  1802. {
  1803. $tmp = explode('/', $type);
  1804. return strtolower($tmp[0]);
  1805. }
  1806. // Get only the mimetype and not additional info (separated from bare mime with semi-colon)
  1807. function common_bare_mime($mimetype)
  1808. {
  1809. $mimetype = mb_strtolower($mimetype);
  1810. if ($semicolon = mb_strpos($mimetype, ';')) {
  1811. $mimetype = mb_substr($mimetype, 0, $semicolon);
  1812. }
  1813. return trim($mimetype);
  1814. }
  1815. function common_mime_type_match($type, $avail)
  1816. {
  1817. if(array_key_exists($type, $avail)) {
  1818. return $type;
  1819. } else {
  1820. $parts = explode('/', $type);
  1821. if(array_key_exists($parts[0] . '/*', $avail)) {
  1822. return $parts[0] . '/*';
  1823. } elseif(array_key_exists('*/*', $avail)) {
  1824. return '*/*';
  1825. } else {
  1826. return null;
  1827. }
  1828. }
  1829. }
  1830. function common_negotiate_type($cprefs, $sprefs)
  1831. {
  1832. $combine = array();
  1833. foreach(array_keys($sprefs) as $type) {
  1834. $parts = explode('/', $type);
  1835. if($parts[1] != '*') {
  1836. $ckey = common_mime_type_match($type, $cprefs);
  1837. if($ckey) {
  1838. $combine[$type] = $sprefs[$type] * $cprefs[$ckey];
  1839. }
  1840. }
  1841. }
  1842. foreach(array_keys($cprefs) as $type) {
  1843. $parts = explode('/', $type);
  1844. if($parts[1] != '*' && !array_key_exists($type, $sprefs)) {
  1845. $skey = common_mime_type_match($type, $sprefs);
  1846. if($skey) {
  1847. $combine[$type] = $sprefs[$skey] * $cprefs[$type];
  1848. }
  1849. }
  1850. }
  1851. $bestq = 0;
  1852. $besttype = 'text/html';
  1853. foreach(array_keys($combine) as $type) {
  1854. if($combine[$type] > $bestq) {
  1855. $besttype = $type;
  1856. $bestq = $combine[$type];
  1857. }
  1858. }
  1859. if ('text/html' === $besttype) {
  1860. return "text/html; charset=utf-8";
  1861. }
  1862. return $besttype;
  1863. }
  1864. function common_config($main, $sub=null)
  1865. {
  1866. global $config;
  1867. if (is_null($sub)) {
  1868. // Return the config category array
  1869. return array_key_exists($main, $config) ? $config[$main] : array();
  1870. }
  1871. // Return the config value
  1872. return (array_key_exists($main, $config) &&
  1873. array_key_exists($sub, $config[$main])) ? $config[$main][$sub] : false;
  1874. }
  1875. function common_config_set($main, $sub, $value)
  1876. {
  1877. global $config;
  1878. if (!array_key_exists($main, $config)) {
  1879. $config[$main] = array();
  1880. }
  1881. $config[$main][$sub] = $value;
  1882. }
  1883. function common_config_append($main, $sub, $value)
  1884. {
  1885. global $config;
  1886. if (!array_key_exists($main, $config)) {
  1887. $config[$main] = array();
  1888. }
  1889. if (!array_key_exists($sub, $config[$main])) {
  1890. $config[$main][$sub] = array();
  1891. }
  1892. if (!is_array($config[$main][$sub])) {
  1893. $config[$main][$sub] = array($config[$main][$sub]);
  1894. }
  1895. array_push($config[$main][$sub], $value);
  1896. }
  1897. /**
  1898. * Pull arguments from a GET/POST/REQUEST array with first-level input checks:
  1899. * strips "magic quotes" slashes if necessary, and kills invalid UTF-8 strings.
  1900. *
  1901. * @param array $from
  1902. * @return array
  1903. */
  1904. function common_copy_args($from)
  1905. {
  1906. $to = array();
  1907. $strip = get_magic_quotes_gpc();
  1908. foreach ($from as $k => $v) {
  1909. if(is_array($v)) {
  1910. $to[$k] = common_copy_args($v);
  1911. } else {
  1912. if ($strip) {
  1913. $v = stripslashes($v);
  1914. }
  1915. $to[$k] = strval(common_validate_utf8($v));
  1916. }
  1917. }
  1918. return $to;
  1919. }
  1920. /**
  1921. * Neutralise the evil effects of magic_quotes_gpc in the current request.
  1922. * This is used before handing a request off to OAuthRequest::from_request.
  1923. * @fixme Doesn't consider vars other than _POST and _GET?
  1924. * @fixme Can't be undone and could corrupt data if run twice.
  1925. */
  1926. function common_remove_magic_from_request()
  1927. {
  1928. if(get_magic_quotes_gpc()) {
  1929. $_POST=array_map('stripslashes',$_POST);
  1930. $_GET=array_map('stripslashes',$_GET);
  1931. }
  1932. }
  1933. function common_user_uri(&$user)
  1934. {
  1935. return common_local_url('userbyid', array('id' => $user->id),
  1936. null, null, false);
  1937. }
  1938. // 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits
  1939. function common_confirmation_code($bits)
  1940. {
  1941. // 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits
  1942. static $codechars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
  1943. $chars = ceil($bits/5);
  1944. $code = '';
  1945. for ($i = 0; $i < $chars; $i++) {
  1946. // XXX: convert to string and back
  1947. $num = hexdec(common_random_hexstr(1));
  1948. // XXX: randomness is too precious to throw away almost
  1949. // 40% of the bits we get!
  1950. $code .= $codechars[$num%32];
  1951. }
  1952. return $code;
  1953. }
  1954. // convert markup to HTML
  1955. function common_markup_to_html($c, $args=null)
  1956. {
  1957. if ($c === null) {
  1958. return '';
  1959. }
  1960. if (is_null($args)) {
  1961. $args = array();
  1962. }
  1963. // XXX: not very efficient
  1964. foreach ($args as $name => $value) {
  1965. $c = preg_replace('/%%arg.'.$name.'%%/', $value, $c);
  1966. }
  1967. $c = preg_replace_callback('/%%user.(\w+)%%/', function ($m) { return common_user_property($m[1]); }, $c);
  1968. $c = preg_replace_callback('/%%action.(\w+)%%/', function ($m) { return common_local_url($m[1]); }, $c);
  1969. $c = preg_replace_callback('/%%doc.(\w+)%%/', function ($m) { return common_local_url('doc', array('title'=>$m[1])); }, $c);
  1970. $c = preg_replace_callback('/%%(\w+).(\w+)%%/', function ($m) { return common_config($m[1], $m[2]); }, $c);
  1971. return \Michelf\Markdown::defaultTransform($c);
  1972. }
  1973. function common_user_property($property)
  1974. {
  1975. $profile = Profile::current();
  1976. if (empty($profile)) {
  1977. return null;
  1978. }
  1979. switch ($property) {
  1980. case 'profileurl':
  1981. case 'nickname':
  1982. case 'fullname':
  1983. case 'location':
  1984. case 'bio':
  1985. return $profile->$property;
  1986. break;
  1987. case 'avatar':
  1988. try {
  1989. return $profile->getAvatar(AVATAR_STREAM_SIZE);
  1990. } catch (Exception $e) {
  1991. return null;
  1992. }
  1993. break;
  1994. case 'bestname':
  1995. return $profile->getBestName();
  1996. break;
  1997. default:
  1998. return null;
  1999. }
  2000. }
  2001. function common_profile_uri($profile)
  2002. {
  2003. $uri = null;
  2004. if (!empty($profile)) {
  2005. if (Event::handle('StartCommonProfileURI', array($profile, &$uri))) {
  2006. $user = User::getKV('id', $profile->id);
  2007. if ($user instanceof User) {
  2008. $uri = $user->getUri();
  2009. }
  2010. Event::handle('EndCommonProfileURI', array($profile, &$uri));
  2011. }
  2012. }
  2013. // XXX: this is a very bad profile!
  2014. return $uri;
  2015. }
  2016. function common_canonical_sms($sms)
  2017. {
  2018. // strip non-digits
  2019. preg_replace('/\D/', '', $sms);
  2020. return $sms;
  2021. }
  2022. function common_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  2023. {
  2024. switch ($errno) {
  2025. case E_ERROR:
  2026. case E_COMPILE_ERROR:
  2027. case E_CORE_ERROR:
  2028. case E_USER_ERROR:
  2029. case E_PARSE:
  2030. case E_RECOVERABLE_ERROR:
  2031. common_log(LOG_ERR, "[$errno] $errstr ($errfile:$errline) [ABORT]");
  2032. die();
  2033. break;
  2034. case E_WARNING:
  2035. case E_COMPILE_WARNING:
  2036. case E_CORE_WARNING:
  2037. case E_USER_WARNING:
  2038. common_log(LOG_WARNING, "[$errno] $errstr ($errfile:$errline)");
  2039. break;
  2040. case E_NOTICE:
  2041. case E_USER_NOTICE:
  2042. common_log(LOG_NOTICE, "[$errno] $errstr ($errfile:$errline)");
  2043. break;
  2044. case E_STRICT:
  2045. case E_DEPRECATED:
  2046. case E_USER_DEPRECATED:
  2047. // XXX: config variable to log this stuff, too
  2048. break;
  2049. default:
  2050. common_log(LOG_ERR, "[$errno] $errstr ($errfile:$errline) [UNKNOWN LEVEL, die()'ing]");
  2051. die();
  2052. break;
  2053. }
  2054. // FIXME: show error page if we're on the Web
  2055. /* Don't execute PHP internal error handler */
  2056. return true;
  2057. }
  2058. function common_session_token()
  2059. {
  2060. common_ensure_session();
  2061. if (!array_key_exists('token', $_SESSION)) {
  2062. $_SESSION['token'] = common_random_hexstr(64);
  2063. }
  2064. return $_SESSION['token'];
  2065. }
  2066. function common_license_terms($uri)
  2067. {
  2068. if(preg_match('/creativecommons.org\/licenses\/([^\/]+)/', $uri, $matches)) {
  2069. return explode('-',$matches[1]);
  2070. }
  2071. return array($uri);
  2072. }
  2073. function common_compatible_license($from, $to)
  2074. {
  2075. $from_terms = common_license_terms($from);
  2076. // public domain and cc-by are compatible with everything
  2077. if(count($from_terms) == 1 && ($from_terms[0] == 'publicdomain' || $from_terms[0] == 'by')) {
  2078. return true;
  2079. }
  2080. $to_terms = common_license_terms($to);
  2081. // sa is compatible across versions. IANAL
  2082. if(in_array('sa',$from_terms) || in_array('sa',$to_terms)) {
  2083. return count(array_diff($from_terms, $to_terms)) == 0;
  2084. }
  2085. // XXX: better compatibility check needed here!
  2086. // Should at least normalise URIs
  2087. return ($from == $to);
  2088. }
  2089. /**
  2090. * returns a quoted table name, if required according to config
  2091. */
  2092. function common_database_tablename($tablename)
  2093. {
  2094. if(common_config('db','quote_identifiers')) {
  2095. $tablename = '"'. $tablename .'"';
  2096. }
  2097. //table prefixes could be added here later
  2098. return $tablename;
  2099. }
  2100. /**
  2101. * Shorten a URL with the current user's configured shortening service,
  2102. * or ur1.ca if configured, or not at all if no shortening is set up.
  2103. *
  2104. * @param string $long_url original URL
  2105. * @param User $user to specify a particular user's options
  2106. * @param boolean $force Force shortening (used when notice is too long)
  2107. * @return string may return the original URL if shortening failed
  2108. *
  2109. * @fixme provide a way to specify a particular shortener
  2110. */
  2111. function common_shorten_url($long_url, User $user=null, $force = false)
  2112. {
  2113. $long_url = trim($long_url);
  2114. $user = common_current_user();
  2115. $maxUrlLength = User_urlshortener_prefs::maxUrlLength($user);
  2116. // $force forces shortening even if it's not strictly needed
  2117. // I doubt URL shortening is ever 'strictly' needed. - ESP
  2118. if (($maxUrlLength == -1 || mb_strlen($long_url) < $maxUrlLength) && !$force) {
  2119. return $long_url;
  2120. }
  2121. $shortenerName = User_urlshortener_prefs::urlShorteningService($user);
  2122. if (Event::handle('StartShortenUrl',
  2123. array($long_url, $shortenerName, &$shortenedUrl))) {
  2124. if ($shortenerName == 'internal') {
  2125. try {
  2126. $f = File::processNew($long_url);
  2127. $shortenedUrl = common_local_url('redirecturl', array('id' => $f->id));
  2128. if ((mb_strlen($shortenedUrl) < mb_strlen($long_url)) || $force) {
  2129. return $shortenedUrl;
  2130. } else {
  2131. return $long_url;
  2132. }
  2133. } catch (ServerException $e) {
  2134. return $long_url;
  2135. }
  2136. } else {
  2137. return $long_url;
  2138. }
  2139. } else {
  2140. //URL was shortened, so return the result
  2141. return trim($shortenedUrl);
  2142. }
  2143. }
  2144. /**
  2145. * @return mixed array($proxy, $ip) for web requests; proxy may be null
  2146. * null if not a web request
  2147. *
  2148. * @fixme X-Forwarded-For can be chained by multiple proxies;
  2149. we should parse the list and provide a cleaner array
  2150. * @fixme X-Forwarded-For can be forged by clients; only use them if trusted
  2151. * @fixme X_Forwarded_For headers will override X-Forwarded-For read through $_SERVER;
  2152. * use function to get exact request headers from Apache if possible.
  2153. */
  2154. function common_client_ip()
  2155. {
  2156. if (!isset($_SERVER) || !array_key_exists('REQUEST_METHOD', $_SERVER)) {
  2157. return null;
  2158. }
  2159. if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
  2160. if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
  2161. $proxy = $_SERVER['HTTP_CLIENT_IP'];
  2162. } else {
  2163. $proxy = $_SERVER['REMOTE_ADDR'];
  2164. }
  2165. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  2166. } else {
  2167. $proxy = null;
  2168. if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
  2169. $ip = $_SERVER['HTTP_CLIENT_IP'];
  2170. } else {
  2171. $ip = $_SERVER['REMOTE_ADDR'];
  2172. }
  2173. }
  2174. return array($proxy, $ip);
  2175. }
  2176. function common_url_to_nickname($url)
  2177. {
  2178. static $bad = array('query', 'user', 'password', 'port', 'fragment');
  2179. $parts = parse_url($url);
  2180. // If any of these parts exist, this won't work
  2181. foreach ($bad as $badpart) {
  2182. if (array_key_exists($badpart, $parts)) {
  2183. return null;
  2184. }
  2185. }
  2186. // We just have host and/or path
  2187. // If it's just a host...
  2188. if (array_key_exists('host', $parts) &&
  2189. (!array_key_exists('path', $parts) || strcmp($parts['path'], '/') == 0))
  2190. {
  2191. $hostparts = explode('.', $parts['host']);
  2192. // Try to catch common idiom of nickname.service.tld
  2193. if ((count($hostparts) > 2) &&
  2194. (strlen($hostparts[count($hostparts) - 2]) > 3) && # try to skip .co.uk, .com.au
  2195. (strcmp($hostparts[0], 'www') != 0))
  2196. {
  2197. return common_nicknamize($hostparts[0]);
  2198. } else {
  2199. // Do the whole hostname
  2200. return common_nicknamize($parts['host']);
  2201. }
  2202. } else {
  2203. if (array_key_exists('path', $parts)) {
  2204. // Strip starting, ending slashes
  2205. $path = preg_replace('@/$@', '', $parts['path']);
  2206. $path = preg_replace('@^/@', '', $path);
  2207. $path = basename($path);
  2208. // Hack for MediaWiki user pages, in the form:
  2209. // http://example.com/wiki/User:Myname
  2210. // ('User' may be localized.)
  2211. if (strpos($path, ':')) {
  2212. $parts = array_filter(explode(':', $path));
  2213. $path = $parts[count($parts) - 1];
  2214. }
  2215. if ($path) {
  2216. return common_nicknamize($path);
  2217. }
  2218. }
  2219. }
  2220. return null;
  2221. }
  2222. function common_nicknamize($str)
  2223. {
  2224. try {
  2225. return Nickname::normalize($str);
  2226. } catch (NicknameException $e) {
  2227. return null;
  2228. }
  2229. }
  2230. function common_perf_counter($key, $val=null)
  2231. {
  2232. global $_perfCounters;
  2233. if (isset($_perfCounters)) {
  2234. if (common_config('site', 'logperf')) {
  2235. if (array_key_exists($key, $_perfCounters)) {
  2236. $_perfCounters[$key][] = $val;
  2237. } else {
  2238. $_perfCounters[$key] = array($val);
  2239. }
  2240. if (common_config('site', 'logperf_detail')) {
  2241. common_log(LOG_DEBUG, "PERF COUNTER HIT: $key $val");
  2242. }
  2243. }
  2244. }
  2245. }
  2246. function common_log_perf_counters()
  2247. {
  2248. if (common_config('site', 'logperf')) {
  2249. global $_startTime, $_perfCounters;
  2250. if (isset($_startTime)) {
  2251. $endTime = microtime(true);
  2252. $diff = round(($endTime - $_startTime) * 1000);
  2253. common_log(LOG_DEBUG, "PERF runtime: ${diff}ms");
  2254. }
  2255. $counters = $_perfCounters;
  2256. ksort($counters);
  2257. foreach ($counters as $key => $values) {
  2258. $count = count($values);
  2259. $unique = count(array_unique($values));
  2260. common_log(LOG_DEBUG, "PERF COUNTER: $key $count ($unique unique)");
  2261. }
  2262. }
  2263. }
  2264. function common_is_email($str)
  2265. {
  2266. return (strpos($str, '@') !== false);
  2267. }
  2268. function common_init_stats()
  2269. {
  2270. global $_mem, $_ts;
  2271. $_mem = memory_get_usage(true);
  2272. $_ts = microtime(true);
  2273. }
  2274. function common_log_delta($comment=null)
  2275. {
  2276. global $_mem, $_ts;
  2277. $mold = $_mem;
  2278. $told = $_ts;
  2279. $_mem = memory_get_usage(true);
  2280. $_ts = microtime(true);
  2281. $mtotal = $_mem - $mold;
  2282. $ttotal = $_ts - $told;
  2283. if (empty($comment)) {
  2284. $comment = 'Delta';
  2285. }
  2286. common_debug(sprintf("%s: %d %d", $comment, $mtotal, round($ttotal * 1000000)));
  2287. }
  2288. function common_strip_html($html, $trim=true, $save_whitespace=false)
  2289. {
  2290. // first replace <br /> with \n
  2291. $html = preg_replace('/\<(\s*)?br(\s*)?\/?(\s*)?\>/i', "\n", $html);
  2292. // then, unless explicitly avoided, remove excessive whitespace
  2293. if (!$save_whitespace) {
  2294. $html = preg_replace('/\s+/', ' ', $html);
  2295. }
  2296. $text = html_entity_decode(strip_tags($html), ENT_QUOTES, 'UTF-8');
  2297. return $trim ? trim($text) : $text;
  2298. }
  2299. function html_sprintf()
  2300. {
  2301. $args = func_get_args();
  2302. for ($i=1; $i<count($args); $i++) {
  2303. $args[$i] = htmlspecialchars($args[$i]);
  2304. }
  2305. return call_user_func_array('sprintf', $args);
  2306. }
  2307. function _ve($var)
  2308. {
  2309. return var_export($var, true);
  2310. }