util.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437
  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_prefered_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, $id, 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->id),
  221. $user->password)) {
  222. //internal checking passed
  223. $authenticatedUser = $user;
  224. }
  225. }
  226. }
  227. Event::handle('EndCheckPassword', array($nickname, $password, $authenticatedUser));
  228. return $authenticatedUser;
  229. }
  230. /**
  231. * Is the current user logged in?
  232. */
  233. function common_logged_in()
  234. {
  235. return (!is_null(common_current_user()));
  236. }
  237. function common_have_session()
  238. {
  239. return (0 != strcmp(session_id(), ''));
  240. }
  241. function common_ensure_session()
  242. {
  243. $c = null;
  244. if (array_key_exists(session_name(), $_COOKIE)) {
  245. $c = $_COOKIE[session_name()];
  246. }
  247. if (!common_have_session()) {
  248. if (common_config('sessions', 'handle')) {
  249. Session::setSaveHandler();
  250. }
  251. if (array_key_exists(session_name(), $_GET)) {
  252. $id = $_GET[session_name()];
  253. } else if (array_key_exists(session_name(), $_COOKIE)) {
  254. $id = $_COOKIE[session_name()];
  255. }
  256. if (isset($id)) {
  257. session_id($id);
  258. }
  259. @session_start();
  260. if (!isset($_SESSION['started'])) {
  261. $_SESSION['started'] = time();
  262. if (!empty($id)) {
  263. common_log(LOG_WARNING, 'Session cookie "' . $_COOKIE[session_name()] . '" ' .
  264. ' is set but started value is null');
  265. }
  266. }
  267. }
  268. }
  269. // Three kinds of arguments:
  270. // 1) a user object
  271. // 2) a nickname
  272. // 3) null to clear
  273. // Initialize to false; set to null if none found
  274. $_cur = false;
  275. function common_set_user($user)
  276. {
  277. global $_cur;
  278. if (is_null($user) && common_have_session()) {
  279. $_cur = null;
  280. unset($_SESSION['userid']);
  281. return true;
  282. } else if (is_string($user)) {
  283. $nickname = $user;
  284. $user = User::getKV('nickname', $nickname);
  285. } else if (!$user instanceof User) {
  286. return false;
  287. }
  288. if ($user) {
  289. if (Event::handle('StartSetUser', array(&$user))) {
  290. if (!empty($user)) {
  291. if (!$user->hasRight(Right::WEBLOGIN)) {
  292. // TRANS: Authorisation exception thrown when a user a not allowed to login.
  293. throw new AuthorizationException(_('Not allowed to log in.'));
  294. }
  295. common_ensure_session();
  296. $_SESSION['userid'] = $user->id;
  297. $_cur = $user;
  298. Event::handle('EndSetUser', array($user));
  299. return $_cur;
  300. }
  301. }
  302. }
  303. return false;
  304. }
  305. function common_set_cookie($key, $value, $expiration=0)
  306. {
  307. $path = common_config('site', 'path');
  308. $server = common_config('site', 'server');
  309. if ($path && ($path != '/')) {
  310. $cookiepath = '/' . $path . '/';
  311. } else {
  312. $cookiepath = '/';
  313. }
  314. return setcookie($key,
  315. $value,
  316. $expiration,
  317. $cookiepath,
  318. $server,
  319. common_config('site', 'ssl')=='always');
  320. }
  321. define('REMEMBERME', 'rememberme');
  322. define('REMEMBERME_EXPIRY', 30 * 24 * 60 * 60); // 30 days
  323. function common_rememberme($user=null)
  324. {
  325. if (!$user) {
  326. $user = common_current_user();
  327. if (!$user) {
  328. return false;
  329. }
  330. }
  331. $rm = new Remember_me();
  332. $rm->code = common_random_hexstr(16);
  333. $rm->user_id = $user->id;
  334. // Wrap the insert in some good ol' fashioned transaction code
  335. $rm->query('BEGIN');
  336. $result = $rm->insert();
  337. if (!$result) {
  338. common_log_db_error($rm, 'INSERT', __FILE__);
  339. $rm->query('ROLLBACK');
  340. return false;
  341. }
  342. $rm->query('COMMIT');
  343. $cookieval = $rm->user_id . ':' . $rm->code;
  344. common_log(LOG_INFO, 'adding rememberme cookie "' . $cookieval . '" for ' . $user->nickname);
  345. common_set_cookie(REMEMBERME, $cookieval, time() + REMEMBERME_EXPIRY);
  346. return true;
  347. }
  348. function common_remembered_user()
  349. {
  350. $user = null;
  351. $packed = isset($_COOKIE[REMEMBERME]) ? $_COOKIE[REMEMBERME] : null;
  352. if (!$packed) {
  353. return null;
  354. }
  355. list($id, $code) = explode(':', $packed);
  356. if (!$id || !$code) {
  357. common_log(LOG_WARNING, 'Malformed rememberme cookie: ' . $packed);
  358. common_forgetme();
  359. return null;
  360. }
  361. $rm = Remember_me::getKV('code', $code);
  362. if (!$rm) {
  363. common_log(LOG_WARNING, 'No such remember code: ' . $code);
  364. common_forgetme();
  365. return null;
  366. }
  367. if ($rm->user_id != $id) {
  368. common_log(LOG_WARNING, 'Rememberme code for wrong user: ' . $rm->user_id . ' != ' . $id);
  369. common_forgetme();
  370. return null;
  371. }
  372. $user = User::getKV('id', $rm->user_id);
  373. if (!$user instanceof User) {
  374. common_log(LOG_WARNING, 'No such user for rememberme: ' . $rm->user_id);
  375. common_forgetme();
  376. return null;
  377. }
  378. // successful!
  379. $result = $rm->delete();
  380. if (!$result) {
  381. common_log_db_error($rm, 'DELETE', __FILE__);
  382. common_log(LOG_WARNING, 'Could not delete rememberme: ' . $code);
  383. common_forgetme();
  384. return null;
  385. }
  386. common_log(LOG_INFO, 'logging in ' . $user->nickname . ' using rememberme code ' . $rm->code);
  387. common_set_user($user);
  388. common_real_login(false);
  389. // We issue a new cookie, so they can log in
  390. // automatically again after this session
  391. common_rememberme($user);
  392. return $user;
  393. }
  394. /**
  395. * must be called with a valid user!
  396. */
  397. function common_forgetme()
  398. {
  399. common_set_cookie(REMEMBERME, '', 0);
  400. }
  401. /**
  402. * Who is the current user?
  403. */
  404. function common_current_user()
  405. {
  406. global $_cur;
  407. if (!_have_config()) {
  408. return null;
  409. }
  410. if ($_cur === false) {
  411. if (isset($_COOKIE[session_name()]) || isset($_GET[session_name()])
  412. || (isset($_SESSION['userid']) && $_SESSION['userid'])) {
  413. common_ensure_session();
  414. $id = isset($_SESSION['userid']) ? $_SESSION['userid'] : false;
  415. if ($id) {
  416. $user = User::getKV('id', $id);
  417. if ($user instanceof User) {
  418. $_cur = $user;
  419. return $_cur;
  420. }
  421. }
  422. }
  423. // that didn't work; try to remember; will init $_cur to null on failure
  424. $_cur = common_remembered_user();
  425. if ($_cur) {
  426. // XXX: Is this necessary?
  427. $_SESSION['userid'] = $_cur->id;
  428. }
  429. }
  430. return $_cur;
  431. }
  432. /**
  433. * Logins that are 'remembered' aren't 'real' -- they're subject to
  434. * cookie-stealing. So, we don't let them do certain things. New reg,
  435. * OpenID, and password logins _are_ real.
  436. */
  437. function common_real_login($real=true)
  438. {
  439. common_ensure_session();
  440. $_SESSION['real_login'] = $real;
  441. }
  442. function common_is_real_login()
  443. {
  444. return common_logged_in() && $_SESSION['real_login'];
  445. }
  446. /**
  447. * Get a hash portion for HTTP caching Etags and such including
  448. * info on the current user's session. If login/logout state changes,
  449. * or we've changed accounts, or we've renamed the current user,
  450. * we'll get a new hash value.
  451. *
  452. * This should not be considered secure information.
  453. *
  454. * @param User $user (optional; uses common_current_user() if left out)
  455. * @return string
  456. */
  457. function common_user_cache_hash($user=false)
  458. {
  459. if ($user === false) {
  460. $user = common_current_user();
  461. }
  462. if ($user) {
  463. return crc32($user->id . ':' . $user->nickname);
  464. } else {
  465. return '0';
  466. }
  467. }
  468. /**
  469. * get canonical version of nickname for comparison
  470. *
  471. * @param string $nickname
  472. * @return string
  473. *
  474. * @throws NicknameException on invalid input
  475. * @deprecated call Nickname::normalize() directly.
  476. */
  477. function common_canonical_nickname($nickname)
  478. {
  479. return Nickname::normalize($nickname);
  480. }
  481. /**
  482. * get canonical version of email for comparison
  483. *
  484. * @fixme actually normalize
  485. * @fixme reject invalid input
  486. *
  487. * @param string $email
  488. * @return string
  489. */
  490. function common_canonical_email($email)
  491. {
  492. // XXX: canonicalize UTF-8
  493. // XXX: lcase the domain part
  494. return $email;
  495. }
  496. function common_purify($html)
  497. {
  498. require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
  499. $config = array('safe' => 1, // means that elements=* means elements=*-applet-embed-iframe-object-script or so
  500. 'elements' => '*',
  501. 'deny_attribute' => 'id,style,on*');
  502. // Remove more elements than what the 'safe' filter gives (elements must be '*' before this)
  503. // http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/htmLawed_README.htm#s3.6
  504. foreach (common_config('htmlfilter') as $tag=>$filter) {
  505. if ($filter === true) {
  506. $config['elements'] .= "-{$tag}";
  507. }
  508. }
  509. $html = common_remove_unicode_formatting($html);
  510. return htmLawed($html, $config);
  511. }
  512. function common_remove_unicode_formatting($text)
  513. {
  514. // Strip Unicode text formatting/direction codes
  515. // this is pretty dangerous for visualisation of text and can be used for mischief
  516. return preg_replace('/[\\x{200b}-\\x{200f}\\x{202a}-\\x{202e}]/u', '', $text);
  517. }
  518. /**
  519. * Partial notice markup rendering step: build links to !group references.
  520. *
  521. * @param string $text partially rendered HTML
  522. * @param Notice $notice in whose context we're working
  523. * @return string partially rendered HTML
  524. */
  525. function common_render_content($text, Notice $notice)
  526. {
  527. $text = common_render_text($text);
  528. $text = common_linkify_mentions($text, $notice);
  529. return $text;
  530. }
  531. /**
  532. * Finds @-mentions within the partially-rendered text section and
  533. * turns them into live links.
  534. *
  535. * Should generally not be called except from common_render_content().
  536. *
  537. * @param string $text partially-rendered HTML
  538. * @param Notice $notice in-progress or complete Notice object for context
  539. * @return string partially-rendered HTML
  540. */
  541. function common_linkify_mentions($text, Notice $notice)
  542. {
  543. $mentions = common_find_mentions($text, $notice);
  544. // We need to go through in reverse order by position,
  545. // so our positions stay valid despite our fudging with the
  546. // string!
  547. $points = array();
  548. foreach ($mentions as $mention)
  549. {
  550. $points[$mention['position']] = $mention;
  551. }
  552. krsort($points);
  553. foreach ($points as $position => $mention) {
  554. $linkText = common_linkify_mention($mention);
  555. $text = substr_replace($text, $linkText, $position, mb_strlen($mention['text']));
  556. }
  557. return $text;
  558. }
  559. function common_linkify_mention(array $mention)
  560. {
  561. $output = null;
  562. if (Event::handle('StartLinkifyMention', array($mention, &$output))) {
  563. $xs = new XMLStringer(false);
  564. $attrs = array('href' => $mention['url'],
  565. 'class' => 'h-card '.$mention['type']);
  566. if (!empty($mention['title'])) {
  567. $attrs['title'] = $mention['title'];
  568. }
  569. $xs->element('a', $attrs, $mention['text']);
  570. $output = $xs->getString();
  571. Event::handle('EndLinkifyMention', array($mention, &$output));
  572. }
  573. return $output;
  574. }
  575. /**
  576. * Find @-mentions in the given text, using the given notice object as context.
  577. * References will be resolved with common_relative_profile() against the user
  578. * who posted the notice.
  579. *
  580. * Note the return data format is internal, to be used for building links and
  581. * such. Should not be used directly; rather, call common_linkify_mentions().
  582. *
  583. * @param string $text
  584. * @param Notice $notice notice in whose context we're building links
  585. *
  586. * @return array
  587. *
  588. * @access private
  589. */
  590. function common_find_mentions($text, Notice $notice)
  591. {
  592. // The getProfile call throws NoProfileException on failure
  593. $sender = $notice->getProfile();
  594. $mentions = array();
  595. if (Event::handle('StartFindMentions', array($sender, $text, &$mentions))) {
  596. // Get the context of the original notice, if any
  597. $origAuthor = null;
  598. $origNotice = null;
  599. $origMentions = array();
  600. // Is it a reply?
  601. if ($notice instanceof Notice) {
  602. try {
  603. $origNotice = $notice->getParent();
  604. $origAuthor = $origNotice->getProfile();
  605. $ids = $origNotice->getReplies();
  606. foreach ($ids as $id) {
  607. $repliedTo = Profile::getKV('id', $id);
  608. if ($repliedTo instanceof Profile) {
  609. $origMentions[$repliedTo->nickname] = $repliedTo;
  610. }
  611. }
  612. } catch (NoProfileException $e) {
  613. common_log(LOG_WARNING, sprintf('Notice %d author profile id %d does not exist', $origNotice->id, $origNotice->profile_id));
  614. } catch (NoParentNoticeException $e) {
  615. // This notice is not in reply to anything
  616. } catch (Exception $e) {
  617. common_log(LOG_WARNING, __METHOD__ . ' got exception ' . get_class($e) . ' : ' . $e->getMessage());
  618. }
  619. }
  620. $matches = common_find_mentions_raw($text);
  621. foreach ($matches as $match) {
  622. try {
  623. $nickname = Nickname::normalize($match[0]);
  624. } catch (NicknameException $e) {
  625. // Bogus match? Drop it.
  626. continue;
  627. }
  628. // Try to get a profile for this nickname.
  629. // Start with conversation context, then go to
  630. // sender context.
  631. if ($origAuthor instanceof Profile && $origAuthor->nickname == $nickname) {
  632. $mentioned = $origAuthor;
  633. } else if (!empty($origMentions) &&
  634. array_key_exists($nickname, $origMentions)) {
  635. $mentioned = $origMentions[$nickname];
  636. } else {
  637. $mentioned = common_relative_profile($sender, $nickname);
  638. }
  639. if ($mentioned instanceof Profile) {
  640. $user = User::getKV('id', $mentioned->id);
  641. if ($user instanceof User) {
  642. $url = common_local_url('userbyid', array('id' => $user->id));
  643. } else {
  644. $url = $mentioned->profileurl;
  645. }
  646. $mention = array('mentioned' => array($mentioned),
  647. 'type' => 'mention',
  648. 'text' => $match[0],
  649. 'position' => $match[1],
  650. 'url' => $url);
  651. if (!empty($mentioned->fullname)) {
  652. $mention['title'] = $mentioned->fullname;
  653. }
  654. $mentions[] = $mention;
  655. }
  656. }
  657. // @#tag => mention of all subscriptions tagged 'tag'
  658. preg_match_all('/(?:^|[\s\.\,\:\;]+)@#([\pL\pN_\-\.]{1,64})/',
  659. $text, $hmatches, PREG_OFFSET_CAPTURE);
  660. foreach ($hmatches[1] as $hmatch) {
  661. $tag = common_canonical_tag($hmatch[0]);
  662. $plist = Profile_list::getByTaggerAndTag($sender->id, $tag);
  663. if (!$plist instanceof Profile_list || $plist->private) {
  664. continue;
  665. }
  666. $tagged = $sender->getTaggedSubscribers($tag);
  667. $url = common_local_url('showprofiletag',
  668. array('tagger' => $sender->nickname,
  669. 'tag' => $tag));
  670. $mentions[] = array('mentioned' => $tagged,
  671. 'type' => 'list',
  672. 'text' => $hmatch[0],
  673. 'position' => $hmatch[1],
  674. 'url' => $url);
  675. }
  676. preg_match_all('/(?:^|[\s\.\,\:\;]+)!(' . Nickname::DISPLAY_FMT . ')/',
  677. $text, $hmatches, PREG_OFFSET_CAPTURE);
  678. foreach ($hmatches[1] as $hmatch) {
  679. $nickname = Nickname::normalize($hmatch[0]);
  680. $group = User_group::getForNickname($nickname, $sender);
  681. if (!$group instanceof User_group || !$sender->isMember($group)) {
  682. continue;
  683. }
  684. $profile = $group->getProfile();
  685. $mentions[] = array('mentioned' => array($profile),
  686. 'type' => 'group',
  687. 'text' => $hmatch[0],
  688. 'position' => $hmatch[1],
  689. 'url' => $group->permalink(),
  690. 'title' => $group->getFancyName());
  691. }
  692. Event::handle('EndFindMentions', array($sender, $text, &$mentions));
  693. }
  694. return $mentions;
  695. }
  696. /**
  697. * Does the actual regex pulls to find @-mentions in text.
  698. * Should generally not be called directly; for use in common_find_mentions.
  699. *
  700. * @param string $text
  701. * @return array of PCRE match arrays
  702. */
  703. function common_find_mentions_raw($text)
  704. {
  705. $tmatches = array();
  706. preg_match_all('/^T (' . Nickname::DISPLAY_FMT . ') /',
  707. $text,
  708. $tmatches,
  709. PREG_OFFSET_CAPTURE);
  710. $atmatches = array();
  711. preg_match_all('/(?:^|\s+)@(' . Nickname::DISPLAY_FMT . ')\b/',
  712. $text,
  713. $atmatches,
  714. PREG_OFFSET_CAPTURE);
  715. $matches = array_merge($tmatches[1], $atmatches[1]);
  716. return $matches;
  717. }
  718. function common_render_text($text)
  719. {
  720. $text = common_remove_unicode_formatting($text);
  721. $text = nl2br(htmlspecialchars($text));
  722. $text = preg_replace('/[\x{0}-\x{8}\x{b}-\x{c}\x{e}-\x{19}]/', '', $text);
  723. $text = common_replace_urls_callback($text, 'common_linkify');
  724. $text = preg_replace_callback('/(^|\&quot\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/u',
  725. function ($m) { return "{$m[1]}#".common_tag_link($m[2]); }, $text);
  726. // XXX: machine tags
  727. return $text;
  728. }
  729. /**
  730. * Find links in the given text and pass them to the given callback function.
  731. *
  732. * @param string $text
  733. * @param function($text, $arg) $callback: return replacement text
  734. * @param mixed $arg: optional argument will be passed on to the callback
  735. */
  736. function common_replace_urls_callback($text, $callback, $arg = null) {
  737. // Start off with a regex
  738. $regex = '#'.
  739. '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'.
  740. '('.
  741. '(?:'.
  742. '(?:'. //Known protocols
  743. '(?:'.
  744. '(?:(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://)'.
  745. '|'.
  746. '(?:(?:mailto|aim|tel|xmpp):)'.
  747. ')'.
  748. '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@
  749. '(?:'.
  750. '(?:'.
  751. '\[[\pN\pL\-\_\:\.]+(?<![\.\:])\]'. //[dns]
  752. ')|(?:'.
  753. '[\pN\pL\-\_\:\.]+(?<![\.\:])'. //dns
  754. ')'.
  755. ')'.
  756. ')'.
  757. '|(?:(?:magnet):)'. // URLs without domain name
  758. '|(?:(?: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]?)'. //IPv4
  759. '|(?:'. //IPv6
  760. '\[?(?:(?:(?:[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})))\]?(?<!:)'.
  761. ')'.
  762. (common_config('linkify', 'bare_domains')
  763. ? '|(?:'. //DNS
  764. '(?:[\pN\pL\-\_\+\%\~]+(?:\:[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@
  765. '[\pN\pL\-\_]+(?:\.[\pN\pL\-\_]+)*\.'.
  766. //tld list from http://data.iana.org/TLD/tlds-alpha-by-domain.txt, also added local, loc, and onion
  767. '(?: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)'.
  768. ')(?![\pN\pL\-\_])'
  769. : '') . // if common_config('linkify', 'bare_domains') is false, don't add anything here
  770. ')'.
  771. '(?:'.
  772. '(?:\:\d+)?'. //:port
  773. '(?:/[\pN\pL$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'@]*)?'. // /path
  774. '(?:\?[\pN\pL\$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'@\/]*)?'. // ?query string
  775. '(?:\#[\pN\pL$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'\@/\?\#]*)?'. // #fragment
  776. ')(?<![\?\.\,\#\,])'.
  777. ')'.
  778. '#ixu';
  779. //preg_match_all($regex,$text,$matches);
  780. //print_r($matches);
  781. return preg_replace_callback($regex, curry('callback_helper',$callback,$arg) ,$text);
  782. }
  783. /**
  784. * Intermediate callback for common_replace_links(), helps resolve some
  785. * ambiguous link forms before passing on to the final callback.
  786. *
  787. * @param array $matches
  788. * @param callable $callback
  789. * @param mixed $arg optional argument to pass on as second param to callback
  790. * @return string
  791. *
  792. * @access private
  793. */
  794. function callback_helper($matches, $callback, $arg=null) {
  795. $url=$matches[1];
  796. $left = strpos($matches[0],$url);
  797. $right = $left+strlen($url);
  798. $groupSymbolSets=array(
  799. array(
  800. 'left'=>'(',
  801. 'right'=>')'
  802. ),
  803. array(
  804. 'left'=>'[',
  805. 'right'=>']'
  806. ),
  807. array(
  808. 'left'=>'{',
  809. 'right'=>'}'
  810. ),
  811. array(
  812. 'left'=>'<',
  813. 'right'=>'>'
  814. )
  815. );
  816. $cannotEndWith=array('.','?',',','#');
  817. $original_url=$url;
  818. do{
  819. $original_url=$url;
  820. foreach($groupSymbolSets as $groupSymbolSet){
  821. if(substr($url,-1)==$groupSymbolSet['right']){
  822. $group_left_count = substr_count($url,$groupSymbolSet['left']);
  823. $group_right_count = substr_count($url,$groupSymbolSet['right']);
  824. if($group_left_count<$group_right_count){
  825. $right-=1;
  826. $url=substr($url,0,-1);
  827. }
  828. }
  829. }
  830. if(in_array(substr($url,-1),$cannotEndWith)){
  831. $right-=1;
  832. $url=substr($url,0,-1);
  833. }
  834. }while($original_url!=$url);
  835. $result = call_user_func_array($callback, array($url, $arg));
  836. return substr($matches[0],0,$left) . $result . substr($matches[0],$right);
  837. }
  838. require_once INSTALLDIR . "/lib/curry.php";
  839. function common_linkify($url) {
  840. // It comes in special'd, so we unspecial it before passing to the stringifying
  841. // functions
  842. $url = htmlspecialchars_decode($url);
  843. if (strpos($url, '@') !== false && strpos($url, ':') === false && Validate::email($url)) {
  844. //url is an email address without the mailto: protocol
  845. $canon = "mailto:$url";
  846. $longurl = "mailto:$url";
  847. } else {
  848. $canon = File_redirection::_canonUrl($url);
  849. $longurl_data = File_redirection::where($canon, common_config('attachments', 'process_links'));
  850. if (is_array($longurl_data)) {
  851. $longurl = $longurl_data['url'];
  852. } elseif (is_string($longurl_data)) {
  853. $longurl = $longurl_data;
  854. } else {
  855. // Unable to reach the server to verify contents, etc
  856. // Just pass the link on through for now.
  857. common_log(LOG_ERR, "Can't linkify url '$url'");
  858. $longurl = $url;
  859. }
  860. }
  861. $attrs = array('href' => $canon, 'title' => $longurl);
  862. $is_attachment = false;
  863. $attachment_id = null;
  864. $has_thumb = false;
  865. // Check to see whether this is a known "attachment" URL.
  866. $f = File::getKV('url', $longurl);
  867. if (!$f instanceof File) {
  868. if (common_config('attachments', 'process_links')) {
  869. // XXX: this writes to the database. :<
  870. try {
  871. $f = File::processNew($longurl);
  872. } catch (ServerException $e) {
  873. $f = null;
  874. }
  875. }
  876. }
  877. if ($f instanceof File) {
  878. try {
  879. $enclosure = $f->getEnclosure();
  880. $is_attachment = true;
  881. $attachment_id = $f->id;
  882. $thumb = File_thumbnail::getKV('file_id', $f->id);
  883. $has_thumb = ($thumb instanceof File_thumbnail);
  884. } catch (ServerException $e) {
  885. // There was not enough metadata available
  886. }
  887. }
  888. // Add clippy
  889. if ($is_attachment) {
  890. $attrs['class'] = 'attachment';
  891. if ($has_thumb) {
  892. $attrs['class'] = 'attachment thumbnail';
  893. }
  894. $attrs['id'] = "attachment-{$attachment_id}";
  895. }
  896. // Whether to nofollow
  897. $nf = common_config('nofollow', 'external');
  898. if ($nf == 'never') {
  899. $attrs['rel'] = 'external';
  900. } else {
  901. $attrs['rel'] = 'nofollow external';
  902. }
  903. return XMLStringer::estring('a', $attrs, $url);
  904. }
  905. /**
  906. * Find and shorten links in a given chunk of text if it's longer than the
  907. * configured notice content limit (or unconditionally).
  908. *
  909. * Side effects: may save file and file_redirection records for referenced URLs.
  910. *
  911. * Pass the $user option or call $user->shortenLinks($text) to ensure the proper
  912. * user's options are used; otherwise the current web session user's setitngs
  913. * will be used or ur1.ca if there is no active web login.
  914. *
  915. * @param string $text
  916. * @param boolean $always (optional)
  917. * @param User $user (optional)
  918. *
  919. * @return string
  920. */
  921. function common_shorten_links($text, $always = false, User $user=null)
  922. {
  923. if ($user === null) {
  924. $user = common_current_user();
  925. }
  926. $maxLength = User_urlshortener_prefs::maxNoticeLength($user);
  927. if ($always || ($maxLength != -1 && mb_strlen($text) > $maxLength)) {
  928. return common_replace_urls_callback($text, array('File_redirection', 'forceShort'), $user);
  929. } else {
  930. return common_replace_urls_callback($text, array('File_redirection', 'makeShort'), $user);
  931. }
  932. }
  933. /**
  934. * Very basic stripping of invalid UTF-8 input text.
  935. *
  936. * @param string $str
  937. * @return mixed string or null if invalid input
  938. *
  939. * @todo ideally we should drop bad chars, and maybe do some of the checks
  940. * from common_xml_safe_str. But we can't strip newlines, etc.
  941. * @todo Unicode normalization might also be useful, but not needed now.
  942. */
  943. function common_validate_utf8($str)
  944. {
  945. // preg_replace will return NULL on invalid UTF-8 input.
  946. //
  947. // Note: empty regex //u also caused NULL return on some
  948. // production machines, but none of our test machines.
  949. //
  950. // This should be replaced with a more reliable check.
  951. return preg_replace('/\x00/u', '', $str);
  952. }
  953. /**
  954. * Make sure an arbitrary string is safe for output in XML as a single line.
  955. *
  956. * @param string $str
  957. * @return string
  958. */
  959. function common_xml_safe_str($str)
  960. {
  961. // Replace common eol and extra whitespace input chars
  962. $unWelcome = array(
  963. "\t", // tab
  964. "\n", // newline
  965. "\r", // cr
  966. "\0", // null byte eos
  967. "\x0B" // vertical tab
  968. );
  969. $replacement = array(
  970. ' ', // single space
  971. ' ',
  972. '', // nothing
  973. '',
  974. ' '
  975. );
  976. $str = str_replace($unWelcome, $replacement, $str);
  977. // Neutralize any additional control codes and UTF-16 surrogates
  978. // (Twitter uses '*')
  979. return preg_replace('/[\p{Cc}\p{Cs}]/u', '*', $str);
  980. }
  981. function common_slugify($str)
  982. {
  983. // php5-intl is highly recommended...
  984. if (!function_exists('transliterator_transliterate')) {
  985. $str = preg_replace('/[^\pL\pN]/u', '', $str);
  986. $str = mb_convert_case($str, MB_CASE_LOWER, 'UTF-8');
  987. $str = substr($str, 0, 64);
  988. return $str;
  989. }
  990. $str = transliterator_transliterate(
  991. 'Any-Latin;' . // any charset to latin compatible
  992. 'NFD;' . // decompose
  993. '[:Nonspacing Mark:] Remove;' . // remove nonspacing marks (accents etc.)
  994. 'NFC;' . // composite again
  995. '[:Punctuation:] Remove;' . // remove punctuation (.,¿? etc.)
  996. 'Lower();' . // turn into lowercase
  997. 'Latin-ASCII;', // get ASCII equivalents (ð to d for example)
  998. $str);
  999. return preg_replace('/[^\pL\pN]/', '', $str);
  1000. }
  1001. function common_tag_link($tag)
  1002. {
  1003. $canonical = common_canonical_tag($tag);
  1004. if (common_config('singleuser', 'enabled')) {
  1005. // regular TagAction isn't set up in 1user mode
  1006. $nickname = User::singleUserNickname();
  1007. $url = common_local_url('showstream',
  1008. array('nickname' => $nickname,
  1009. 'tag' => $canonical));
  1010. } else {
  1011. $url = common_local_url('tag', array('tag' => $canonical));
  1012. }
  1013. $xs = new XMLStringer();
  1014. $xs->elementStart('span', 'tag');
  1015. $xs->element('a', array('href' => $url,
  1016. 'rel' => 'tag'),
  1017. $tag);
  1018. $xs->elementEnd('span');
  1019. return $xs->getString();
  1020. }
  1021. function common_canonical_tag($tag)
  1022. {
  1023. $tag = common_slugify($tag);
  1024. $tag = substr($tag, 0, 64);
  1025. return $tag;
  1026. }
  1027. function common_valid_profile_tag($str)
  1028. {
  1029. return preg_match('/^[A-Za-z0-9_\-\.]{1,64}$/', $str);
  1030. }
  1031. /**
  1032. * Resolve an ambiguous profile nickname reference, checking in following order:
  1033. * - profiles that $sender subscribes to
  1034. * - profiles that subscribe to $sender
  1035. * - local user profiles
  1036. *
  1037. * WARNING: does not validate or normalize $nickname -- MUST BE PRE-VALIDATED
  1038. * OR THERE MAY BE A RISK OF SQL INJECTION ATTACKS. THIS FUNCTION DOES NOT
  1039. * ESCAPE SQL.
  1040. *
  1041. * @fixme validate input
  1042. * @fixme escape SQL
  1043. * @fixme fix or remove mystery third parameter
  1044. * @fixme is $sender a User or Profile?
  1045. *
  1046. * @param <type> $sender the user or profile in whose context we're looking
  1047. * @param string $nickname validated nickname of
  1048. * @param <type> $dt unused mystery parameter; in Notice reply-to handling a timestamp is passed.
  1049. *
  1050. * @return Profile or null
  1051. */
  1052. function common_relative_profile($sender, $nickname, $dt=null)
  1053. {
  1054. // Will throw exception on invalid input.
  1055. $nickname = Nickname::normalize($nickname);
  1056. // Try to find profiles this profile is subscribed to that have this nickname
  1057. $recipient = new Profile();
  1058. // XXX: use a join instead of a subquery
  1059. $recipient->whereAdd('EXISTS (SELECT subscribed from subscription where subscriber = '.intval($sender->id).' and subscribed = id)', 'AND');
  1060. $recipient->whereAdd("nickname = '" . $recipient->escape($nickname) . "'", 'AND');
  1061. if ($recipient->find(true)) {
  1062. // XXX: should probably differentiate between profiles with
  1063. // the same name by date of most recent update
  1064. return $recipient;
  1065. }
  1066. // Try to find profiles that listen to this profile and that have this nickname
  1067. $recipient = new Profile();
  1068. // XXX: use a join instead of a subquery
  1069. $recipient->whereAdd('EXISTS (SELECT subscriber from subscription where subscribed = '.intval($sender->id).' and subscriber = id)', 'AND');
  1070. $recipient->whereAdd("nickname = '" . $recipient->escape($nickname) . "'", 'AND');
  1071. if ($recipient->find(true)) {
  1072. // XXX: should probably differentiate between profiles with
  1073. // the same name by date of most recent update
  1074. return $recipient;
  1075. }
  1076. // If this is a local user, try to find a local user with that nickname.
  1077. $sender = User::getKV('id', $sender->id);
  1078. if ($sender instanceof User) {
  1079. $recipient_user = User::getKV('nickname', $nickname);
  1080. if ($recipient_user instanceof User) {
  1081. return $recipient_user->getProfile();
  1082. }
  1083. }
  1084. // Otherwise, no links. @messages from local users to remote users,
  1085. // or from remote users to other remote users, are just
  1086. // outside our ability to make intelligent guesses about
  1087. return null;
  1088. }
  1089. function common_local_url($action, $args=null, $params=null, $fragment=null, $addSession=true)
  1090. {
  1091. if (Event::handle('StartLocalURL', array(&$action, &$params, &$fragment, &$addSession, &$url))) {
  1092. $r = Router::get();
  1093. $path = $r->build($action, $args, $params, $fragment);
  1094. $ssl = common_config('site', 'ssl') === 'always'
  1095. || GNUsocial::isHTTPS()
  1096. || common_is_sensitive($action);
  1097. if (common_config('site','fancy')) {
  1098. $url = common_path($path, $ssl, $addSession);
  1099. } else {
  1100. if (mb_strpos($path, '/index.php') === 0) {
  1101. $url = common_path($path, $ssl, $addSession);
  1102. } else {
  1103. $url = common_path('index.php/'.$path, $ssl, $addSession);
  1104. }
  1105. }
  1106. Event::handle('EndLocalURL', array(&$action, &$params, &$fragment, &$addSession, &$url));
  1107. }
  1108. return $url;
  1109. }
  1110. function common_is_sensitive($action)
  1111. {
  1112. static $sensitive = array(
  1113. 'login',
  1114. 'register',
  1115. 'passwordsettings',
  1116. 'api',
  1117. 'ApiOAuthRequestToken',
  1118. 'ApiOAuthAccessToken',
  1119. 'ApiOAuthAuthorize',
  1120. 'ApiOAuthPin',
  1121. 'showapplication'
  1122. );
  1123. $ssl = null;
  1124. if (Event::handle('SensitiveAction', array($action, &$ssl))) {
  1125. $ssl = in_array($action, $sensitive);
  1126. }
  1127. return $ssl;
  1128. }
  1129. function common_path($relative, $ssl=false, $addSession=true)
  1130. {
  1131. $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
  1132. if (($ssl && (common_config('site', 'ssl') === 'sometimes'))
  1133. || GNUsocial::isHTTPS()
  1134. || common_config('site', 'ssl') === 'always') {
  1135. $proto = 'https';
  1136. if (is_string(common_config('site', 'sslserver')) &&
  1137. mb_strlen(common_config('site', 'sslserver')) > 0) {
  1138. $serverpart = common_config('site', 'sslserver');
  1139. } else if (common_config('site', 'server')) {
  1140. $serverpart = common_config('site', 'server');
  1141. } else {
  1142. common_log(LOG_ERR, 'Site server not configured, unable to determine site name.');
  1143. }
  1144. } else {
  1145. $proto = 'http';
  1146. if (common_config('site', 'server')) {
  1147. $serverpart = common_config('site', 'server');
  1148. } else {
  1149. common_log(LOG_ERR, 'Site server not configured, unable to determine site name.');
  1150. }
  1151. }
  1152. if ($addSession) {
  1153. $relative = common_inject_session($relative, $serverpart);
  1154. }
  1155. return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
  1156. }
  1157. function common_inject_session($url, $serverpart = null)
  1158. {
  1159. if (!common_have_session()) {
  1160. return $url;
  1161. }
  1162. if (empty($serverpart)) {
  1163. $serverpart = parse_url($url, PHP_URL_HOST);
  1164. }
  1165. $currentServer = (array_key_exists('HTTP_HOST', $_SERVER)) ? $_SERVER['HTTP_HOST'] : null;
  1166. // Are we pointing to another server (like an SSL server?)
  1167. if (!empty($currentServer) && 0 != strcasecmp($currentServer, $serverpart)) {
  1168. // Pass the session ID as a GET parameter
  1169. $sesspart = session_name() . '=' . session_id();
  1170. $i = strpos($url, '?');
  1171. if ($i === false) { // no GET params, just append
  1172. $url .= '?' . $sesspart;
  1173. } else {
  1174. $url = substr($url, 0, $i + 1).$sesspart.'&'.substr($url, $i + 1);
  1175. }
  1176. }
  1177. return $url;
  1178. }
  1179. function common_date_string($dt)
  1180. {
  1181. // XXX: do some sexy date formatting
  1182. // return date(DATE_RFC822, $dt);
  1183. $t = strtotime($dt);
  1184. $now = time();
  1185. $diff = $now - $t;
  1186. if ($now < $t) { // that shouldn't happen!
  1187. return common_exact_date($dt);
  1188. } else if ($diff < 60) {
  1189. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1190. return _('a few seconds ago');
  1191. } else if ($diff < 92) {
  1192. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1193. return _('about a minute ago');
  1194. } else if ($diff < 3300) {
  1195. $minutes = round($diff/60);
  1196. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1197. return sprintf( _m('about one minute ago', 'about %d minutes ago', $minutes), $minutes);
  1198. } else if ($diff < 5400) {
  1199. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1200. return _('about an hour ago');
  1201. } else if ($diff < 22 * 3600) {
  1202. $hours = round($diff/3600);
  1203. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1204. return sprintf( _m('about one hour ago', 'about %d hours ago', $hours), $hours);
  1205. } else if ($diff < 37 * 3600) {
  1206. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1207. return _('about a day ago');
  1208. } else if ($diff < 24 * 24 * 3600) {
  1209. $days = round($diff/(24*3600));
  1210. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1211. return sprintf( _m('about one day ago', 'about %d days ago', $days), $days);
  1212. } else if ($diff < 46 * 24 * 3600) {
  1213. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1214. return _('about a month ago');
  1215. } else if ($diff < 330 * 24 * 3600) {
  1216. $months = round($diff/(30*24*3600));
  1217. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1218. return sprintf( _m('about one month ago', 'about %d months ago',$months), $months);
  1219. } else if ($diff < 480 * 24 * 3600) {
  1220. // TRANS: Used in notices to indicate when the notice was made compared to now.
  1221. return _('about a year ago');
  1222. } else {
  1223. return common_exact_date($dt);
  1224. }
  1225. }
  1226. function common_exact_date($dt)
  1227. {
  1228. static $_utc;
  1229. static $_siteTz;
  1230. if (!$_utc) {
  1231. $_utc = new DateTimeZone('UTC');
  1232. $_siteTz = new DateTimeZone(common_timezone());
  1233. }
  1234. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1235. $d = new DateTime($dateStr, $_utc);
  1236. $d->setTimezone($_siteTz);
  1237. // TRANS: Human-readable full date-time specification (formatting on http://php.net/date)
  1238. return $d->format(_('l, d-M-Y H:i:s T'));
  1239. }
  1240. function common_date_w3dtf($dt)
  1241. {
  1242. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1243. $d = new DateTime($dateStr, new DateTimeZone('UTC'));
  1244. $d->setTimezone(new DateTimeZone(common_timezone()));
  1245. return $d->format(DATE_W3C);
  1246. }
  1247. function common_date_rfc2822($dt)
  1248. {
  1249. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1250. $d = new DateTime($dateStr, new DateTimeZone('UTC'));
  1251. $d->setTimezone(new DateTimeZone(common_timezone()));
  1252. return $d->format('r');
  1253. }
  1254. function common_date_iso8601($dt)
  1255. {
  1256. $dateStr = date('d F Y H:i:s', strtotime($dt));
  1257. $d = new DateTime($dateStr, new DateTimeZone('UTC'));
  1258. $d->setTimezone(new DateTimeZone(common_timezone()));
  1259. return $d->format('c');
  1260. }
  1261. function common_sql_now()
  1262. {
  1263. return common_sql_date(time());
  1264. }
  1265. function common_sql_date($datetime)
  1266. {
  1267. return strftime('%Y-%m-%d %H:%M:%S', $datetime);
  1268. }
  1269. /**
  1270. * Return an SQL fragment to calculate an age-based weight from a given
  1271. * timestamp or datetime column.
  1272. *
  1273. * @param string $column name of field we're comparing against current time
  1274. * @param integer $dropoff divisor for age in seconds before exponentiation
  1275. * @return string SQL fragment
  1276. */
  1277. function common_sql_weight($column, $dropoff)
  1278. {
  1279. if (common_config('db', 'type') == 'pgsql') {
  1280. // PostgreSQL doesn't support timestampdiff function.
  1281. // @fixme will this use the right time zone?
  1282. // @fixme does this handle cross-year subtraction correctly?
  1283. return "sum(exp(-extract(epoch from (now() - $column)) / $dropoff))";
  1284. } else {
  1285. return "sum(exp(timestampdiff(second, utc_timestamp(), $column) / $dropoff))";
  1286. }
  1287. }
  1288. function common_redirect($url, $code=307)
  1289. {
  1290. static $status = array(301 => "Moved Permanently",
  1291. 302 => "Found",
  1292. 303 => "See Other",
  1293. 307 => "Temporary Redirect");
  1294. header('HTTP/1.1 '.$code.' '.$status[$code]);
  1295. header("Location: $url");
  1296. header("Connection: close");
  1297. $xo = new XMLOutputter();
  1298. $xo->startXML('a',
  1299. '-//W3C//DTD XHTML 1.0 Strict//EN',
  1300. 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
  1301. $xo->element('a', array('href' => $url), $url);
  1302. $xo->endXML();
  1303. exit;
  1304. }
  1305. // Stick the notice on the queue
  1306. function common_enqueue_notice($notice)
  1307. {
  1308. static $localTransports = array('ping');
  1309. $transports = array();
  1310. if (common_config('sms', 'enabled')) {
  1311. $transports[] = 'sms';
  1312. }
  1313. if (Event::hasHandler('HandleQueuedNotice')) {
  1314. $transports[] = 'plugin';
  1315. }
  1316. // We can skip these for gatewayed notices.
  1317. if ($notice->isLocal()) {
  1318. $transports = array_merge($transports, $localTransports);
  1319. }
  1320. if (Event::handle('StartEnqueueNotice', array($notice, &$transports))) {
  1321. $qm = QueueManager::get();
  1322. foreach ($transports as $transport)
  1323. {
  1324. $qm->enqueue($notice, $transport);
  1325. }
  1326. Event::handle('EndEnqueueNotice', array($notice, $transports));
  1327. }
  1328. return true;
  1329. }
  1330. function common_profile_url($nickname)
  1331. {
  1332. return common_local_url('showstream', array('nickname' => $nickname),
  1333. null, null, false);
  1334. }
  1335. /**
  1336. * Should make up a reasonable root URL
  1337. */
  1338. function common_root_url($ssl=false)
  1339. {
  1340. $url = common_path('', $ssl, false);
  1341. $i = strpos($url, '?');
  1342. if ($i !== false) {
  1343. $url = substr($url, 0, $i);
  1344. }
  1345. return $url;
  1346. }
  1347. /**
  1348. * returns $bytes bytes of random data as a hexadecimal string
  1349. */
  1350. function common_random_hexstr($bytes)
  1351. {
  1352. $str = @file_exists('/dev/urandom')
  1353. ? common_urandom($bytes)
  1354. : common_mtrand($bytes);
  1355. $hexstr = '';
  1356. for ($i = 0; $i < $bytes; $i++) {
  1357. $hexstr .= sprintf("%02x", ord($str[$i]));
  1358. }
  1359. return $hexstr;
  1360. }
  1361. function common_urandom($bytes)
  1362. {
  1363. $h = fopen('/dev/urandom', 'rb');
  1364. // should not block
  1365. $src = fread($h, $bytes);
  1366. fclose($h);
  1367. return $src;
  1368. }
  1369. function common_mtrand($bytes)
  1370. {
  1371. $str = '';
  1372. for ($i = 0; $i < $bytes; $i++) {
  1373. $str .= chr(mt_rand(0, 255));
  1374. }
  1375. return $str;
  1376. }
  1377. /**
  1378. * Record the given URL as the return destination for a future
  1379. * form submission, to be read by common_get_returnto().
  1380. *
  1381. * @param string $url
  1382. *
  1383. * @fixme as a session-global setting, this can allow multiple forms
  1384. * to conflict and overwrite each others' returnto destinations if
  1385. * the user has multiple tabs or windows open.
  1386. *
  1387. * Should refactor to index with a token or otherwise only pass the
  1388. * data along its intended path.
  1389. */
  1390. function common_set_returnto($url)
  1391. {
  1392. common_ensure_session();
  1393. $_SESSION['returnto'] = $url;
  1394. }
  1395. /**
  1396. * Fetch a return-destination URL previously recorded by
  1397. * common_set_returnto().
  1398. *
  1399. * @return mixed URL string or null
  1400. *
  1401. * @fixme as a session-global setting, this can allow multiple forms
  1402. * to conflict and overwrite each others' returnto destinations if
  1403. * the user has multiple tabs or windows open.
  1404. *
  1405. * Should refactor to index with a token or otherwise only pass the
  1406. * data along its intended path.
  1407. */
  1408. function common_get_returnto()
  1409. {
  1410. common_ensure_session();
  1411. return (array_key_exists('returnto', $_SESSION)) ? $_SESSION['returnto'] : null;
  1412. }
  1413. function common_timestamp()
  1414. {
  1415. return date('YmdHis');
  1416. }
  1417. function common_ensure_syslog()
  1418. {
  1419. static $initialized = false;
  1420. if (!$initialized) {
  1421. openlog(common_config('syslog', 'appname'), 0,
  1422. common_config('syslog', 'facility'));
  1423. $initialized = true;
  1424. }
  1425. }
  1426. function common_log_line($priority, $msg)
  1427. {
  1428. static $syslog_priorities = array('LOG_EMERG', 'LOG_ALERT', 'LOG_CRIT', 'LOG_ERR',
  1429. 'LOG_WARNING', 'LOG_NOTICE', 'LOG_INFO', 'LOG_DEBUG');
  1430. return date('Y-m-d H:i:s') . ' ' . $syslog_priorities[$priority] . ': ' . $msg . PHP_EOL;
  1431. }
  1432. function common_request_id()
  1433. {
  1434. $pid = getmypid();
  1435. $server = common_config('site', 'server');
  1436. if (php_sapi_name() == 'cli') {
  1437. $script = basename($_SERVER['PHP_SELF']);
  1438. return "$server:$script:$pid";
  1439. } else {
  1440. static $req_id = null;
  1441. if (!isset($req_id)) {
  1442. $req_id = substr(md5(mt_rand()), 0, 8);
  1443. }
  1444. if (isset($_SERVER['REQUEST_URI'])) {
  1445. $url = $_SERVER['REQUEST_URI'];
  1446. }
  1447. $method = $_SERVER['REQUEST_METHOD'];
  1448. return "$server:$pid.$req_id $method $url";
  1449. }
  1450. }
  1451. function common_log($priority, $msg, $filename=null)
  1452. {
  1453. if(Event::handle('StartLog', array(&$priority, &$msg, &$filename))){
  1454. $msg = (empty($filename)) ? $msg : basename($filename) . ' - ' . $msg;
  1455. $msg = '[' . common_request_id() . '] ' . $msg;
  1456. $logfile = common_config('site', 'logfile');
  1457. if ($logfile) {
  1458. $log = fopen($logfile, "a");
  1459. if ($log) {
  1460. $output = common_log_line($priority, $msg);
  1461. fwrite($log, $output);
  1462. fclose($log);
  1463. }
  1464. } else {
  1465. common_ensure_syslog();
  1466. syslog($priority, $msg);
  1467. }
  1468. Event::handle('EndLog', array($priority, $msg, $filename));
  1469. }
  1470. }
  1471. function common_debug($msg, $filename=null)
  1472. {
  1473. if ($filename) {
  1474. common_log(LOG_DEBUG, basename($filename).' - '.$msg);
  1475. } else {
  1476. common_log(LOG_DEBUG, $msg);
  1477. }
  1478. }
  1479. function common_log_db_error(&$object, $verb, $filename=null)
  1480. {
  1481. global $_PEAR;
  1482. $objstr = common_log_objstring($object);
  1483. $last_error = &$_PEAR->getStaticProperty('DB_DataObject','lastError');
  1484. if (is_object($last_error)) {
  1485. $msg = $last_error->message;
  1486. } else {
  1487. $msg = 'Unknown error (' . var_export($last_error, true) . ')';
  1488. }
  1489. common_log(LOG_ERR, $msg . '(' . $verb . ' on ' . $objstr . ')', $filename);
  1490. }
  1491. function common_log_objstring(&$object)
  1492. {
  1493. if (is_null($object)) {
  1494. return "null";
  1495. }
  1496. if (!($object instanceof DB_DataObject)) {
  1497. return "(unknown)";
  1498. }
  1499. $arr = $object->toArray();
  1500. $fields = array();
  1501. foreach ($arr as $k => $v) {
  1502. if (is_object($v)) {
  1503. $fields[] = "$k='".get_class($v)."'";
  1504. } else {
  1505. $fields[] = "$k='$v'";
  1506. }
  1507. }
  1508. $objstring = $object->tableName() . '[' . implode(',', $fields) . ']';
  1509. return $objstring;
  1510. }
  1511. function common_valid_http_url($url, $secure=false)
  1512. {
  1513. // If $secure is true, only allow https URLs to pass
  1514. // (if false, we use '?' in 'https?' to say the 's' is optional)
  1515. $regex = $secure ? '/^https$/' : '/^https?$/';
  1516. return filter_var($url, FILTER_VALIDATE_URL)
  1517. && preg_match($regex, parse_url($url, PHP_URL_SCHEME));
  1518. }
  1519. function common_valid_tag($tag)
  1520. {
  1521. if (preg_match('/^tag:(.*?),(\d{4}(-\d{2}(-\d{2})?)?):(.*)$/', $tag, $matches)) {
  1522. return (Validate::email($matches[1]) ||
  1523. preg_match('/^([\w-\.]+)$/', $matches[1]));
  1524. }
  1525. return false;
  1526. }
  1527. /**
  1528. * Determine if given domain or address literal is valid
  1529. * eg for use in JIDs and URLs. Does not check if the domain
  1530. * exists!
  1531. *
  1532. * @param string $domain
  1533. * @return boolean valid or not
  1534. */
  1535. function common_valid_domain($domain)
  1536. {
  1537. $octet = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])";
  1538. $ipv4 = "(?:$octet(?:\.$octet){3})";
  1539. if (preg_match("/^$ipv4$/u", $domain)) return true;
  1540. $group = "(?:[0-9a-f]{1,4})";
  1541. $ipv6 = "(?:\[($group(?::$group){0,7})?(::)?($group(?::$group){0,7})?\])"; // http://tools.ietf.org/html/rfc3513#section-2.2
  1542. if (preg_match("/^$ipv6$/ui", $domain, $matches)) {
  1543. $before = explode(":", $matches[1]);
  1544. $zeroes = $matches[2];
  1545. $after = explode(":", $matches[3]);
  1546. if ($zeroes) {
  1547. $min = 0;
  1548. $max = 7;
  1549. } else {
  1550. $min = 1;
  1551. $max = 8;
  1552. }
  1553. $explicit = count($before) + count($after);
  1554. if ($explicit < $min || $explicit > $max) {
  1555. return false;
  1556. }
  1557. return true;
  1558. }
  1559. try {
  1560. require_once "Net/IDNA.php";
  1561. $idn = Net_IDNA::getInstance();
  1562. $domain = $idn->encode($domain);
  1563. } catch (Exception $e) {
  1564. return false;
  1565. }
  1566. $subdomain = "(?:[a-z0-9][a-z0-9-]*)"; // @fixme
  1567. $fqdn = "(?:$subdomain(?:\.$subdomain)*\.?)";
  1568. return preg_match("/^$fqdn$/ui", $domain);
  1569. }
  1570. /* Following functions are copied from MediaWiki GlobalFunctions.php
  1571. * and written by Evan Prodromou. */
  1572. function common_accept_to_prefs($accept, $def = '*/*')
  1573. {
  1574. // No arg means accept anything (per HTTP spec)
  1575. if(!$accept) {
  1576. return array($def => 1);
  1577. }
  1578. $prefs = array();
  1579. $parts = explode(',', $accept);
  1580. foreach($parts as $part) {
  1581. // FIXME: doesn't deal with params like 'text/html; level=1'
  1582. @list($value, $qpart) = explode(';', trim($part));
  1583. $match = array();
  1584. if(!isset($qpart)) {
  1585. $prefs[$value] = 1;
  1586. } elseif(preg_match('/q\s*=\s*(\d*\.\d+)/', $qpart, $match)) {
  1587. $prefs[$value] = $match[1];
  1588. }
  1589. }
  1590. return $prefs;
  1591. }
  1592. // Match by our supported file extensions
  1593. function common_supported_ext_to_mime($fileext)
  1594. {
  1595. // Accept a filename and take out the extension
  1596. if (strpos($fileext, '.') !== false) {
  1597. $fileext = substr(strrchr($fileext, '.'), 1);
  1598. }
  1599. $supported = common_config('attachments', 'supported');
  1600. foreach($supported as $type => $ext) {
  1601. if ($ext === $fileext) {
  1602. return $type;
  1603. }
  1604. }
  1605. throw new ServerException('Unsupported file extension');
  1606. }
  1607. // Match by our supported mime types
  1608. function common_supported_mime_to_ext($mimetype)
  1609. {
  1610. $supported = common_config('attachments', 'supported');
  1611. foreach($supported as $type => $ext) {
  1612. if ($mimetype === $type) {
  1613. return $ext;
  1614. }
  1615. }
  1616. throw new ServerException('Unsupported MIME type');
  1617. }
  1618. // The MIME "media" is the part before the slash (video in video/webm)
  1619. function common_get_mime_media($type)
  1620. {
  1621. $tmp = explode('/', $type);
  1622. return strtolower($tmp[0]);
  1623. }
  1624. function common_bare_mime($mimetype)
  1625. {
  1626. $mimetype = mb_strtolower($mimetype);
  1627. if ($semicolon = mb_strpos($mimetype, ';')) {
  1628. $mimetype = mb_substr($mimetype, 0, $semicolon);
  1629. }
  1630. return $mimetype;
  1631. }
  1632. function common_mime_type_match($type, $avail)
  1633. {
  1634. if(array_key_exists($type, $avail)) {
  1635. return $type;
  1636. } else {
  1637. $parts = explode('/', $type);
  1638. if(array_key_exists($parts[0] . '/*', $avail)) {
  1639. return $parts[0] . '/*';
  1640. } elseif(array_key_exists('*/*', $avail)) {
  1641. return '*/*';
  1642. } else {
  1643. return null;
  1644. }
  1645. }
  1646. }
  1647. function common_negotiate_type($cprefs, $sprefs)
  1648. {
  1649. $combine = array();
  1650. foreach(array_keys($sprefs) as $type) {
  1651. $parts = explode('/', $type);
  1652. if($parts[1] != '*') {
  1653. $ckey = common_mime_type_match($type, $cprefs);
  1654. if($ckey) {
  1655. $combine[$type] = $sprefs[$type] * $cprefs[$ckey];
  1656. }
  1657. }
  1658. }
  1659. foreach(array_keys($cprefs) as $type) {
  1660. $parts = explode('/', $type);
  1661. if($parts[1] != '*' && !array_key_exists($type, $sprefs)) {
  1662. $skey = common_mime_type_match($type, $sprefs);
  1663. if($skey) {
  1664. $combine[$type] = $sprefs[$skey] * $cprefs[$type];
  1665. }
  1666. }
  1667. }
  1668. $bestq = 0;
  1669. $besttype = 'text/html';
  1670. foreach(array_keys($combine) as $type) {
  1671. if($combine[$type] > $bestq) {
  1672. $besttype = $type;
  1673. $bestq = $combine[$type];
  1674. }
  1675. }
  1676. if ('text/html' === $besttype) {
  1677. return "text/html; charset=utf-8";
  1678. }
  1679. return $besttype;
  1680. }
  1681. function common_config($main, $sub=null)
  1682. {
  1683. global $config;
  1684. if (is_null($sub)) {
  1685. // Return the config category array
  1686. return array_key_exists($main, $config) ? $config[$main] : array();
  1687. }
  1688. // Return the config value
  1689. return (array_key_exists($main, $config) &&
  1690. array_key_exists($sub, $config[$main])) ? $config[$main][$sub] : false;
  1691. }
  1692. function common_config_set($main, $sub, $value)
  1693. {
  1694. global $config;
  1695. if (!array_key_exists($main, $config)) {
  1696. $config[$main] = array();
  1697. }
  1698. $config[$main][$sub] = $value;
  1699. }
  1700. function common_config_append($main, $sub, $value)
  1701. {
  1702. global $config;
  1703. if (!array_key_exists($main, $config)) {
  1704. $config[$main] = array();
  1705. }
  1706. if (!array_key_exists($sub, $config[$main])) {
  1707. $config[$main][$sub] = array();
  1708. }
  1709. if (!is_array($config[$main][$sub])) {
  1710. $config[$main][$sub] = array($config[$main][$sub]);
  1711. }
  1712. array_push($config[$main][$sub], $value);
  1713. }
  1714. /**
  1715. * Pull arguments from a GET/POST/REQUEST array with first-level input checks:
  1716. * strips "magic quotes" slashes if necessary, and kills invalid UTF-8 strings.
  1717. *
  1718. * @param array $from
  1719. * @return array
  1720. */
  1721. function common_copy_args($from)
  1722. {
  1723. $to = array();
  1724. $strip = get_magic_quotes_gpc();
  1725. foreach ($from as $k => $v) {
  1726. if(is_array($v)) {
  1727. $to[$k] = common_copy_args($v);
  1728. } else {
  1729. if ($strip) {
  1730. $v = stripslashes($v);
  1731. }
  1732. $to[$k] = strval(common_validate_utf8($v));
  1733. }
  1734. }
  1735. return $to;
  1736. }
  1737. /**
  1738. * Neutralise the evil effects of magic_quotes_gpc in the current request.
  1739. * This is used before handing a request off to OAuthRequest::from_request.
  1740. * @fixme Doesn't consider vars other than _POST and _GET?
  1741. * @fixme Can't be undone and could corrupt data if run twice.
  1742. */
  1743. function common_remove_magic_from_request()
  1744. {
  1745. if(get_magic_quotes_gpc()) {
  1746. $_POST=array_map('stripslashes',$_POST);
  1747. $_GET=array_map('stripslashes',$_GET);
  1748. }
  1749. }
  1750. function common_user_uri(&$user)
  1751. {
  1752. return common_local_url('userbyid', array('id' => $user->id),
  1753. null, null, false);
  1754. }
  1755. // 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits
  1756. function common_confirmation_code($bits)
  1757. {
  1758. // 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits
  1759. static $codechars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
  1760. $chars = ceil($bits/5);
  1761. $code = '';
  1762. for ($i = 0; $i < $chars; $i++) {
  1763. // XXX: convert to string and back
  1764. $num = hexdec(common_random_hexstr(1));
  1765. // XXX: randomness is too precious to throw away almost
  1766. // 40% of the bits we get!
  1767. $code .= $codechars[$num%32];
  1768. }
  1769. return $code;
  1770. }
  1771. // convert markup to HTML
  1772. function common_markup_to_html($c, $args=null)
  1773. {
  1774. if ($c === null) {
  1775. return '';
  1776. }
  1777. if (is_null($args)) {
  1778. $args = array();
  1779. }
  1780. // XXX: not very efficient
  1781. foreach ($args as $name => $value) {
  1782. $c = preg_replace('/%%arg.'.$name.'%%/', $value, $c);
  1783. }
  1784. $c = preg_replace_callback('/%%user.(\w+)%%/', function ($m) { return common_user_property($m[1]); }, $c);
  1785. $c = preg_replace_callback('/%%action.(\w+)%%/', function ($m) { return common_local_url($m[1]); }, $c);
  1786. $c = preg_replace_callback('/%%doc.(\w+)%%/', function ($m) { return common_local_url('doc', array('title'=>$m[1])); }, $c);
  1787. $c = preg_replace_callback('/%%(\w+).(\w+)%%/', function ($m) { return common_config($m[1], $m[2]); }, $c);
  1788. return \Michelf\Markdown::defaultTransform($c);
  1789. }
  1790. function common_user_property($property)
  1791. {
  1792. $profile = Profile::current();
  1793. if (empty($profile)) {
  1794. return null;
  1795. }
  1796. switch ($property) {
  1797. case 'profileurl':
  1798. case 'nickname':
  1799. case 'fullname':
  1800. case 'location':
  1801. case 'bio':
  1802. return $profile->$property;
  1803. break;
  1804. case 'avatar':
  1805. try {
  1806. return $profile->getAvatar(AVATAR_STREAM_SIZE);
  1807. } catch (Exception $e) {
  1808. return null;
  1809. }
  1810. break;
  1811. case 'bestname':
  1812. return $profile->getBestName();
  1813. break;
  1814. default:
  1815. return null;
  1816. }
  1817. }
  1818. function common_profile_uri($profile)
  1819. {
  1820. $uri = null;
  1821. if (!empty($profile)) {
  1822. if (Event::handle('StartCommonProfileURI', array($profile, &$uri))) {
  1823. $user = User::getKV('id', $profile->id);
  1824. if ($user instanceof User) {
  1825. $uri = $user->getUri();
  1826. }
  1827. Event::handle('EndCommonProfileURI', array($profile, &$uri));
  1828. }
  1829. }
  1830. // XXX: this is a very bad profile!
  1831. return $uri;
  1832. }
  1833. function common_canonical_sms($sms)
  1834. {
  1835. // strip non-digits
  1836. preg_replace('/\D/', '', $sms);
  1837. return $sms;
  1838. }
  1839. function common_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  1840. {
  1841. switch ($errno) {
  1842. case E_ERROR:
  1843. case E_COMPILE_ERROR:
  1844. case E_CORE_ERROR:
  1845. case E_USER_ERROR:
  1846. case E_PARSE:
  1847. case E_RECOVERABLE_ERROR:
  1848. common_log(LOG_ERR, "[$errno] $errstr ($errfile:$errline) [ABORT]");
  1849. die();
  1850. break;
  1851. case E_WARNING:
  1852. case E_COMPILE_WARNING:
  1853. case E_CORE_WARNING:
  1854. case E_USER_WARNING:
  1855. common_log(LOG_WARNING, "[$errno] $errstr ($errfile:$errline)");
  1856. break;
  1857. case E_NOTICE:
  1858. case E_USER_NOTICE:
  1859. common_log(LOG_NOTICE, "[$errno] $errstr ($errfile:$errline)");
  1860. break;
  1861. case E_STRICT:
  1862. case E_DEPRECATED:
  1863. case E_USER_DEPRECATED:
  1864. // XXX: config variable to log this stuff, too
  1865. break;
  1866. default:
  1867. common_log(LOG_ERR, "[$errno] $errstr ($errfile:$errline) [UNKNOWN LEVEL, die()'ing]");
  1868. die();
  1869. break;
  1870. }
  1871. // FIXME: show error page if we're on the Web
  1872. /* Don't execute PHP internal error handler */
  1873. return true;
  1874. }
  1875. function common_session_token()
  1876. {
  1877. common_ensure_session();
  1878. if (!array_key_exists('token', $_SESSION)) {
  1879. $_SESSION['token'] = common_random_hexstr(64);
  1880. }
  1881. return $_SESSION['token'];
  1882. }
  1883. function common_license_terms($uri)
  1884. {
  1885. if(preg_match('/creativecommons.org\/licenses\/([^\/]+)/', $uri, $matches)) {
  1886. return explode('-',$matches[1]);
  1887. }
  1888. return array($uri);
  1889. }
  1890. function common_compatible_license($from, $to)
  1891. {
  1892. $from_terms = common_license_terms($from);
  1893. // public domain and cc-by are compatible with everything
  1894. if(count($from_terms) == 1 && ($from_terms[0] == 'publicdomain' || $from_terms[0] == 'by')) {
  1895. return true;
  1896. }
  1897. $to_terms = common_license_terms($to);
  1898. // sa is compatible across versions. IANAL
  1899. if(in_array('sa',$from_terms) || in_array('sa',$to_terms)) {
  1900. return count(array_diff($from_terms, $to_terms)) == 0;
  1901. }
  1902. // XXX: better compatibility check needed here!
  1903. // Should at least normalise URIs
  1904. return ($from == $to);
  1905. }
  1906. /**
  1907. * returns a quoted table name, if required according to config
  1908. */
  1909. function common_database_tablename($tablename)
  1910. {
  1911. if(common_config('db','quote_identifiers')) {
  1912. $tablename = '"'. $tablename .'"';
  1913. }
  1914. //table prefixes could be added here later
  1915. return $tablename;
  1916. }
  1917. /**
  1918. * Shorten a URL with the current user's configured shortening service,
  1919. * or ur1.ca if configured, or not at all if no shortening is set up.
  1920. *
  1921. * @param string $long_url original URL
  1922. * @param User $user to specify a particular user's options
  1923. * @param boolean $force Force shortening (used when notice is too long)
  1924. * @return string may return the original URL if shortening failed
  1925. *
  1926. * @fixme provide a way to specify a particular shortener
  1927. */
  1928. function common_shorten_url($long_url, User $user=null, $force = false)
  1929. {
  1930. $long_url = trim($long_url);
  1931. $user = common_current_user();
  1932. $maxUrlLength = User_urlshortener_prefs::maxUrlLength($user);
  1933. // $force forces shortening even if it's not strictly needed
  1934. // I doubt URL shortening is ever 'strictly' needed. - ESP
  1935. if (($maxUrlLength == -1 || mb_strlen($long_url) < $maxUrlLength) && !$force) {
  1936. return $long_url;
  1937. }
  1938. $shortenerName = User_urlshortener_prefs::urlShorteningService($user);
  1939. if (Event::handle('StartShortenUrl',
  1940. array($long_url, $shortenerName, &$shortenedUrl))) {
  1941. if ($shortenerName == 'internal') {
  1942. try {
  1943. $f = File::processNew($long_url);
  1944. $shortenedUrl = common_local_url('redirecturl', array('id' => $f->id));
  1945. if ((mb_strlen($shortenedUrl) < mb_strlen($long_url)) || $force) {
  1946. return $shortenedUrl;
  1947. } else {
  1948. return $long_url;
  1949. }
  1950. } catch (ServerException $e) {
  1951. return $long_url;
  1952. }
  1953. } else {
  1954. return $long_url;
  1955. }
  1956. } else {
  1957. //URL was shortened, so return the result
  1958. return trim($shortenedUrl);
  1959. }
  1960. }
  1961. /**
  1962. * @return mixed array($proxy, $ip) for web requests; proxy may be null
  1963. * null if not a web request
  1964. *
  1965. * @fixme X-Forwarded-For can be chained by multiple proxies;
  1966. we should parse the list and provide a cleaner array
  1967. * @fixme X-Forwarded-For can be forged by clients; only use them if trusted
  1968. * @fixme X_Forwarded_For headers will override X-Forwarded-For read through $_SERVER;
  1969. * use function to get exact request headers from Apache if possible.
  1970. */
  1971. function common_client_ip()
  1972. {
  1973. if (!isset($_SERVER) || !array_key_exists('REQUEST_METHOD', $_SERVER)) {
  1974. return null;
  1975. }
  1976. if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
  1977. if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
  1978. $proxy = $_SERVER['HTTP_CLIENT_IP'];
  1979. } else {
  1980. $proxy = $_SERVER['REMOTE_ADDR'];
  1981. }
  1982. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  1983. } else {
  1984. $proxy = null;
  1985. if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
  1986. $ip = $_SERVER['HTTP_CLIENT_IP'];
  1987. } else {
  1988. $ip = $_SERVER['REMOTE_ADDR'];
  1989. }
  1990. }
  1991. return array($proxy, $ip);
  1992. }
  1993. function common_url_to_nickname($url)
  1994. {
  1995. static $bad = array('query', 'user', 'password', 'port', 'fragment');
  1996. $parts = parse_url($url);
  1997. // If any of these parts exist, this won't work
  1998. foreach ($bad as $badpart) {
  1999. if (array_key_exists($badpart, $parts)) {
  2000. return null;
  2001. }
  2002. }
  2003. // We just have host and/or path
  2004. // If it's just a host...
  2005. if (array_key_exists('host', $parts) &&
  2006. (!array_key_exists('path', $parts) || strcmp($parts['path'], '/') == 0))
  2007. {
  2008. $hostparts = explode('.', $parts['host']);
  2009. // Try to catch common idiom of nickname.service.tld
  2010. if ((count($hostparts) > 2) &&
  2011. (strlen($hostparts[count($hostparts) - 2]) > 3) && # try to skip .co.uk, .com.au
  2012. (strcmp($hostparts[0], 'www') != 0))
  2013. {
  2014. return common_nicknamize($hostparts[0]);
  2015. } else {
  2016. // Do the whole hostname
  2017. return common_nicknamize($parts['host']);
  2018. }
  2019. } else {
  2020. if (array_key_exists('path', $parts)) {
  2021. // Strip starting, ending slashes
  2022. $path = preg_replace('@/$@', '', $parts['path']);
  2023. $path = preg_replace('@^/@', '', $path);
  2024. $path = basename($path);
  2025. // Hack for MediaWiki user pages, in the form:
  2026. // http://example.com/wiki/User:Myname
  2027. // ('User' may be localized.)
  2028. if (strpos($path, ':')) {
  2029. $parts = array_filter(explode(':', $path));
  2030. $path = $parts[count($parts) - 1];
  2031. }
  2032. if ($path) {
  2033. return common_nicknamize($path);
  2034. }
  2035. }
  2036. }
  2037. return null;
  2038. }
  2039. function common_nicknamize($str)
  2040. {
  2041. try {
  2042. return Nickname::normalize($str);
  2043. } catch (NicknameException $e) {
  2044. return null;
  2045. }
  2046. }
  2047. function common_perf_counter($key, $val=null)
  2048. {
  2049. global $_perfCounters;
  2050. if (isset($_perfCounters)) {
  2051. if (common_config('site', 'logperf')) {
  2052. if (array_key_exists($key, $_perfCounters)) {
  2053. $_perfCounters[$key][] = $val;
  2054. } else {
  2055. $_perfCounters[$key] = array($val);
  2056. }
  2057. if (common_config('site', 'logperf_detail')) {
  2058. common_log(LOG_DEBUG, "PERF COUNTER HIT: $key $val");
  2059. }
  2060. }
  2061. }
  2062. }
  2063. function common_log_perf_counters()
  2064. {
  2065. if (common_config('site', 'logperf')) {
  2066. global $_startTime, $_perfCounters;
  2067. if (isset($_startTime)) {
  2068. $endTime = microtime(true);
  2069. $diff = round(($endTime - $_startTime) * 1000);
  2070. common_log(LOG_DEBUG, "PERF runtime: ${diff}ms");
  2071. }
  2072. $counters = $_perfCounters;
  2073. ksort($counters);
  2074. foreach ($counters as $key => $values) {
  2075. $count = count($values);
  2076. $unique = count(array_unique($values));
  2077. common_log(LOG_DEBUG, "PERF COUNTER: $key $count ($unique unique)");
  2078. }
  2079. }
  2080. }
  2081. function common_is_email($str)
  2082. {
  2083. return (strpos($str, '@') !== false);
  2084. }
  2085. function common_init_stats()
  2086. {
  2087. global $_mem, $_ts;
  2088. $_mem = memory_get_usage(true);
  2089. $_ts = microtime(true);
  2090. }
  2091. function common_log_delta($comment=null)
  2092. {
  2093. global $_mem, $_ts;
  2094. $mold = $_mem;
  2095. $told = $_ts;
  2096. $_mem = memory_get_usage(true);
  2097. $_ts = microtime(true);
  2098. $mtotal = $_mem - $mold;
  2099. $ttotal = $_ts - $told;
  2100. if (empty($comment)) {
  2101. $comment = 'Delta';
  2102. }
  2103. common_debug(sprintf("%s: %d %d", $comment, $mtotal, round($ttotal * 1000000)));
  2104. }
  2105. function common_strip_html($html, $trim=true, $save_whitespace=false)
  2106. {
  2107. if (!$save_whitespace) {
  2108. $html = preg_replace('/\s+/', ' ', $html);
  2109. }
  2110. $text = html_entity_decode(strip_tags($html), ENT_QUOTES, 'UTF-8');
  2111. return $trim ? trim($text) : $text;
  2112. }