util.php 87 KB

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