util.php 85 KB

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