util.php 83 KB

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