action.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Base class for all actions (~views)
  18. *
  19. * @category Action
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Sarven Capadisli <csarven@status.net>
  23. * @copyright 2008 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Base class for all actions
  29. *
  30. * This is the base class for all actions in the package. An action is
  31. * more or less a "view" in an MVC framework.
  32. *
  33. * Actions are responsible for extracting and validating parameters; using
  34. * model classes to read and write to the database; and doing ouput.
  35. *
  36. * @category Output
  37. * @copyright 2008 StatusNet, Inc.
  38. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  39. *
  40. * @see HTMLOutputter
  41. */
  42. class Action extends HTMLOutputter // lawsuit
  43. {
  44. // This should be protected/private in the future
  45. public $args = [];
  46. // Action properties, set per-class
  47. protected $action = false;
  48. protected $ajax = false;
  49. protected $menus = true;
  50. protected $needLogin = false;
  51. protected $redirectAfterLogin = false;
  52. protected $needPost = false; // implies canPost if true
  53. protected $canPost = false; // can this action handle POST method?
  54. // The currently scoped profile (normally Profile::current; from $this->auth_user for API)
  55. protected $scoped = null;
  56. // Related to front-end user representation
  57. protected $format = null;
  58. protected $error = null;
  59. protected $msg = null;
  60. /**
  61. * Constructor
  62. *
  63. * Just wraps the HTMLOutputter constructor.
  64. *
  65. * @param string $output URI to output to, default = stdout
  66. * @param boolean $indent Whether to indent output, default true
  67. *
  68. * @see XMLOutputter::__construct
  69. * @see HTMLOutputter::__construct
  70. */
  71. public function __construct($output = 'php://output', $indent = null)
  72. {
  73. parent::__construct($output, $indent);
  74. }
  75. public static function run(array $args = [], $output = 'php://output', $indent = null)
  76. {
  77. $class = get_called_class();
  78. $action = new $class($output, $indent);
  79. set_exception_handler(array($action, 'handleError'));
  80. $action->execute($args);
  81. return $action;
  82. }
  83. public function getInfo()
  84. {
  85. return $this->msg;
  86. }
  87. public function handleError($e)
  88. {
  89. if ($e instanceof ClientException) {
  90. $this->clientError($e->getMessage(), $e->getCode());
  91. } elseif ($e instanceof ServerException) {
  92. $this->serverError($e->getMessage(), $e->getCode());
  93. } else {
  94. // If it wasn't specified more closely which kind of exception it was
  95. $this->serverError($e->getMessage(), 500);
  96. }
  97. }
  98. /**
  99. * Client error
  100. *
  101. * @param string $msg error message to display
  102. * @param integer $code http error code, 400 by default
  103. * @param string $format error format (json, xml, text) for ApiAction
  104. *
  105. * @return void
  106. * @throws ClientException always
  107. */
  108. public function clientError($msg, $code = 400, $format = null)
  109. {
  110. // $format is currently only relevant for an ApiAction anyway
  111. if ($format === null) {
  112. $format = $this->format;
  113. }
  114. common_debug("User error '{$code}' on '{$this->action}': {$msg}", __FILE__);
  115. if (!array_key_exists($code, ClientErrorAction::$status)) {
  116. $code = 400;
  117. }
  118. $status_string = ClientErrorAction::$status[$code];
  119. switch ($format) {
  120. case 'xml':
  121. header("HTTP/1.1 {$code} {$status_string}");
  122. $this->initDocument('xml');
  123. $this->elementStart('hash');
  124. $this->element('error', null, $msg);
  125. $this->element('request', null, $_SERVER['REQUEST_URI']);
  126. $this->elementEnd('hash');
  127. $this->endDocument('xml');
  128. break;
  129. case 'json':
  130. if (!isset($this->callback)) {
  131. header("HTTP/1.1 {$code} {$status_string}");
  132. }
  133. $this->initDocument('json');
  134. $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
  135. print(json_encode($error_array));
  136. $this->endDocument('json');
  137. break;
  138. case 'text':
  139. header("HTTP/1.1 {$code} {$status_string}");
  140. header('Content-Type: text/plain; charset=utf-8');
  141. echo $msg;
  142. break;
  143. default:
  144. common_log(LOG_ERR, 'Handled clientError (' . _ve($code) . ') but cannot output into desired format (' . _ve($this->format) . '): ' . _ve($msg));
  145. $action = new ClientErrorAction($msg, $code);
  146. $action->execute();
  147. }
  148. exit((int)$code);
  149. }
  150. public function execute(array $args = [])
  151. {
  152. // checkMirror stuff
  153. if (common_config('db', 'mirror') && $this->isReadOnly($args)) {
  154. if (is_array(common_config('db', 'mirror'))) {
  155. // "load balancing", ha ha
  156. $arr = common_config('db', 'mirror');
  157. $k = array_rand($arr);
  158. $mirror = $arr[$k];
  159. } else {
  160. $mirror = common_config('db', 'mirror');
  161. }
  162. // everyone else uses the mirror
  163. common_config_set('db', 'database', $mirror);
  164. }
  165. if (Event::handle('StartActionExecute', array($this, &$args))) {
  166. $prepared = $this->prepare($args);
  167. if ($prepared) {
  168. $this->handle();
  169. } else {
  170. common_debug('Prepare failed for Action.');
  171. }
  172. $this->flush();
  173. Event::handle('EndActionExecute', array($this));
  174. }
  175. }
  176. /**
  177. * Return true if read only.
  178. *
  179. * MAY override
  180. *
  181. * @param array $args other arguments
  182. *
  183. * @return boolean is read only action?
  184. */
  185. public function isReadOnly($args)
  186. {
  187. return false;
  188. }
  189. /**
  190. * For initializing members of the class.
  191. *
  192. * @param array $args misc. arguments
  193. *
  194. * @return boolean true
  195. * @throws ClientException
  196. */
  197. protected function prepare(array $args = [])
  198. {
  199. if ($this->needPost && !$this->isPost()) {
  200. // TRANS: Client error. POST is a HTTP command. It should not be translated.
  201. $this->clientError(_('This method requires a POST.'), 405);
  202. }
  203. // needPost, of course, overrides canPost if true
  204. if (!$this->canPost) {
  205. $this->canPost = $this->needPost;
  206. }
  207. $this->args = common_copy_args($args);
  208. // This could be set with get_called_action and then
  209. // chop off 'Action' from the class name. In lower case.
  210. $this->action = strtolower($this->trimmed('action'));
  211. if ($this->ajax || $this->boolean('ajax')) {
  212. // check with GNUsocial::isAjax()
  213. GNUsocial::setAjax(true);
  214. }
  215. if ($this->needLogin) {
  216. $this->checkLogin(); // if not logged in, this redirs/excepts
  217. }
  218. if ($this->redirectAfterLogin) {
  219. common_set_returnto($this->selfUrl());
  220. }
  221. $this->updateScopedProfile();
  222. return true;
  223. }
  224. /**
  225. * Check if the current request is a POST
  226. *
  227. * @return boolean true if POST; otherwise false.
  228. */
  229. public function isPost()
  230. {
  231. return ($_SERVER['REQUEST_METHOD'] == 'POST');
  232. }
  233. // Must be run _after_ prepare
  234. /**
  235. * Returns trimmed query argument or default value if not found
  236. *
  237. * @param string $key requested argument
  238. * @param string $def default value to return if $key is not provided
  239. *
  240. * @return boolean is read only action?
  241. */
  242. public function trimmed($key, $def = null)
  243. {
  244. $arg = $this->arg($key, $def);
  245. return is_string($arg) ? trim($arg) : $arg;
  246. }
  247. /**
  248. * Returns query argument or default value if not found
  249. *
  250. * @param string $key requested argument
  251. * @param string $def default value to return if $key is not provided
  252. *
  253. * @return boolean is read only action?
  254. */
  255. public function arg($key, $def = null)
  256. {
  257. if (array_key_exists($key, $this->args)) {
  258. return $this->args[$key];
  259. } else {
  260. return $def;
  261. }
  262. }
  263. /**
  264. * Boolean understands english (yes, no, true, false)
  265. *
  266. * @param string $key query key we're interested in
  267. * @param string $def default value
  268. *
  269. * @return boolean interprets yes/no strings as boolean
  270. */
  271. public function boolean($key, $def = false)
  272. {
  273. $arg = strtolower($this->trimmed($key));
  274. if (is_null($arg)) {
  275. return $def;
  276. } elseif (in_array($arg, array('true', 'yes', '1', 'on'))) {
  277. return true;
  278. } elseif (in_array($arg, array('false', 'no', '0'))) {
  279. return false;
  280. } else {
  281. return $def;
  282. }
  283. }
  284. /**
  285. * If not logged in, take appropriate action (redir or exception)
  286. *
  287. * @param boolean $redir Redirect to login if not logged in
  288. *
  289. * @return boolean true if logged in (never returns if not)
  290. * @throws ClientException
  291. */
  292. public function checkLogin($redir = true)
  293. {
  294. if (common_logged_in()) {
  295. return true;
  296. }
  297. if ($redir == true) {
  298. common_set_returnto($_SERVER['REQUEST_URI']);
  299. common_redirect(common_local_url('login'));
  300. }
  301. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  302. $this->clientError(_('Not logged in.'), 403);
  303. }
  304. public function updateScopedProfile()
  305. {
  306. $this->scoped = Profile::current();
  307. return $this->scoped;
  308. }
  309. /**
  310. * Handler method
  311. */
  312. protected function handle()
  313. {
  314. header('Vary: Accept-Encoding,Cookie');
  315. $lm = $this->lastModified();
  316. $etag = $this->etag();
  317. if ($etag) {
  318. header('ETag: ' . $etag);
  319. }
  320. if ($lm) {
  321. header('Last-Modified: ' . date(DATE_RFC1123, $lm));
  322. if ($this->isCacheable()) {
  323. header('Expires: ' . gmdate('D, d M Y H:i:s', 0) . ' GMT');
  324. header("Cache-Control: private, must-revalidate, max-age=0");
  325. header("Pragma:");
  326. }
  327. }
  328. $checked = false;
  329. if ($etag) {
  330. $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
  331. $_SERVER['HTTP_IF_NONE_MATCH'] : null;
  332. if ($if_none_match) {
  333. // If this check fails, ignore the if-modified-since below.
  334. $checked = true;
  335. if ($this->_hasEtag($etag, $if_none_match)) {
  336. header('HTTP/1.1 304 Not Modified');
  337. // Better way to do this?
  338. exit(0);
  339. }
  340. }
  341. }
  342. if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
  343. $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
  344. $ims = strtotime($if_modified_since);
  345. if ($lm <= $ims) {
  346. header('HTTP/1.1 304 Not Modified');
  347. // Better way to do this?
  348. exit(0);
  349. }
  350. }
  351. }
  352. /**
  353. * Return last modified, if applicable.
  354. *
  355. * MAY override
  356. *
  357. * @return string last modified http header
  358. */
  359. public function lastModified()
  360. {
  361. // For comparison with If-Last-Modified
  362. // If not applicable, return null
  363. return null;
  364. }
  365. /**
  366. * Return etag, if applicable.
  367. *
  368. * MAY override
  369. *
  370. * @return string etag http header
  371. */
  372. public function etag()
  373. {
  374. return null;
  375. }
  376. /**
  377. * Is this action cacheable?
  378. *
  379. * If the action returns a last-modified
  380. *
  381. * @return boolean is read only action?
  382. */
  383. public function isCacheable()
  384. {
  385. return true;
  386. }
  387. /**
  388. * Has etag? (private)
  389. *
  390. * @param string $etag etag http header
  391. * @param string $if_none_match ifNoneMatch http header
  392. *
  393. * @return boolean
  394. */
  395. public function _hasEtag($etag, $if_none_match)
  396. {
  397. $etags = explode(',', $if_none_match);
  398. return in_array($etag, $etags) || in_array('*', $etags);
  399. }
  400. /**
  401. * Server error
  402. *
  403. * @param string $msg error message to display
  404. * @param integer $code http error code, 500 by default
  405. *
  406. * @param string $format
  407. * @return void
  408. */
  409. public function serverError($msg, $code = 500, $format = null)
  410. {
  411. if ($format === null) {
  412. $format = $this->format;
  413. }
  414. common_debug("Server error '{$code}' on '{$this->action}': {$msg}", __FILE__);
  415. if (!array_key_exists($code, ServerErrorAction::$status)) {
  416. $code = 500;
  417. }
  418. $status_string = ServerErrorAction::$status[$code];
  419. switch ($format) {
  420. case 'xml':
  421. header("HTTP/1.1 {$code} {$status_string}");
  422. $this->initDocument('xml');
  423. $this->elementStart('hash');
  424. $this->element('error', null, $msg);
  425. $this->element('request', null, $_SERVER['REQUEST_URI']);
  426. $this->elementEnd('hash');
  427. $this->endDocument('xml');
  428. break;
  429. case 'json':
  430. if (!isset($this->callback)) {
  431. header("HTTP/1.1 {$code} {$status_string}");
  432. }
  433. $this->initDocument('json');
  434. $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
  435. print(json_encode($error_array));
  436. $this->endDocument('json');
  437. break;
  438. default:
  439. common_log(LOG_ERR, 'Handled serverError (' . _ve($code) . ') but cannot output into desired format (' . _ve($this->format) . '): ' . _ve($msg));
  440. $action = new ServerErrorAction($msg, $code);
  441. $action->execute();
  442. }
  443. exit((int)$code);
  444. }
  445. public function getScoped()
  446. {
  447. return ($this->scoped instanceof Profile) ? $this->scoped : null;
  448. }
  449. public function isAction(array $names)
  450. {
  451. foreach ($names as $class) {
  452. // PHP is case insensitive, and we have stuff like ApiUpperCaseAction,
  453. // but we at least make a point out of wanting to do stuff case-sensitive.
  454. $class = ucfirst($class) . 'Action';
  455. if ($this instanceof $class) {
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. /**
  462. * Show page, a template method.
  463. *
  464. * @return void
  465. * @throws ClientException
  466. * @throws ReflectionException
  467. * @throws ServerException
  468. */
  469. public function showPage()
  470. {
  471. if (GNUsocial::isAjax()) {
  472. self::showAjax();
  473. return;
  474. }
  475. if (Event::handle('StartShowHTML', array($this))) {
  476. $this->startHTML();
  477. $this->flush();
  478. Event::handle('EndShowHTML', array($this));
  479. }
  480. if (Event::handle('StartShowHead', array($this))) {
  481. $this->showHead();
  482. $this->flush();
  483. Event::handle('EndShowHead', array($this));
  484. }
  485. if (Event::handle('StartShowBody', array($this))) {
  486. $this->showBody();
  487. Event::handle('EndShowBody', array($this));
  488. }
  489. if (Event::handle('StartEndHTML', array($this))) {
  490. $this->endHTML();
  491. Event::handle('EndEndHTML', array($this));
  492. }
  493. }
  494. public function showAjax()
  495. {
  496. $this->startHTML('text/xml;charset=utf-8');
  497. $this->elementStart('head');
  498. // TRANS: Title for conversation page.
  499. $this->element('title', null, $this->title());
  500. $this->elementEnd('head');
  501. $this->elementStart('body');
  502. if ($this->getError()) {
  503. $this->element('p', array('id' => 'error'), $this->getError());
  504. } else {
  505. $this->showContent();
  506. }
  507. $this->elementEnd('body');
  508. $this->endHTML();
  509. }
  510. /**
  511. * Returns the page title
  512. *
  513. * SHOULD overload
  514. *
  515. * @return string page title
  516. */
  517. public function title()
  518. {
  519. // TRANS: Page title for a page without a title set.
  520. return _('Untitled page');
  521. }
  522. public function getError()
  523. {
  524. return $this->error;
  525. }
  526. /**
  527. * Show content.
  528. *
  529. * MUST overload (unless there's not a notice)
  530. *
  531. * @return void
  532. */
  533. protected function showContent()
  534. {
  535. }
  536. public function endHTML()
  537. {
  538. global $_startTime;
  539. if (isset($_startTime)) {
  540. $endTime = microtime(true);
  541. $diff = round(($endTime - $_startTime) * 1000);
  542. $this->raw("<!-- ${diff}ms -->");
  543. }
  544. parent::endHTML();
  545. }
  546. /**
  547. * Show head, a template method.
  548. *
  549. * @return void
  550. */
  551. public function showHead()
  552. {
  553. // XXX: attributes (profile?)
  554. $this->elementStart('head');
  555. if (Event::handle('StartShowHeadElements', array($this))) {
  556. if (Event::handle('StartShowHeadTitle', array($this))) {
  557. $this->showTitle();
  558. Event::handle('EndShowHeadTitle', array($this));
  559. }
  560. $this->showShortcutIcon();
  561. $this->showStylesheets();
  562. $this->showOpenSearch();
  563. $this->showFeeds();
  564. $this->showDescription();
  565. $this->extraHead();
  566. Event::handle('EndShowHeadElements', array($this));
  567. }
  568. $this->elementEnd('head');
  569. }
  570. /**
  571. * Show title, a template method.
  572. *
  573. * @return void
  574. */
  575. public function showTitle()
  576. {
  577. $this->element(
  578. 'title',
  579. null,
  580. // TRANS: Page title. %1$s is the title, %2$s is the site name.
  581. sprintf(
  582. _('%1$s - %2$s'),
  583. $this->title(),
  584. common_config('site', 'name')
  585. )
  586. );
  587. }
  588. /**
  589. * Show themed shortcut icon
  590. *
  591. * @return void
  592. */
  593. public function showShortcutIcon()
  594. {
  595. if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
  596. $this->element('link', array('rel' => 'shortcut icon',
  597. 'href' => Theme::path('favicon.ico')));
  598. } else {
  599. // favicon.ico should be HTTPS if the rest of the page is
  600. $this->element('link', array('rel' => 'shortcut icon',
  601. 'href' => common_path('favicon.ico', GNUsocial::isHTTPS())));
  602. }
  603. if (common_config('site', 'mobile')) {
  604. if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
  605. $this->element('link', array('rel' => 'apple-touch-icon',
  606. 'href' => Theme::path('apple-touch-icon.png')));
  607. } else {
  608. $this->element('link', array('rel' => 'apple-touch-icon',
  609. 'href' => common_path('apple-touch-icon.png')));
  610. }
  611. }
  612. }
  613. /**
  614. * Show stylesheets
  615. *
  616. * @return void
  617. */
  618. public function showStylesheets()
  619. {
  620. if (Event::handle('StartShowStyles', array($this))) {
  621. // Use old name for StatusNet for compatibility on events
  622. if (Event::handle('StartShowStylesheets', array($this))) {
  623. $this->primaryCssLink(null, 'screen, projection, tv, print');
  624. Event::handle('EndShowStylesheets', array($this));
  625. }
  626. $this->cssLink('js/extlib/jquery-ui/css/smoothness/jquery-ui.css');
  627. if (Event::handle('StartShowUAStyles', array($this))) {
  628. Event::handle('EndShowUAStyles', array($this));
  629. }
  630. Event::handle('EndShowStyles', array($this));
  631. if (common_config('custom_css', 'enabled')) {
  632. $css = common_config('custom_css', 'css');
  633. if (Event::handle('StartShowCustomCss', array($this, &$css))) {
  634. if (trim($css) != '') {
  635. $this->style($css);
  636. }
  637. Event::handle('EndShowCustomCss', array($this));
  638. }
  639. }
  640. }
  641. }
  642. public function primaryCssLink($mainTheme = null, $media = null)
  643. {
  644. $theme = new Theme($mainTheme);
  645. // Some themes may have external stylesheets
  646. foreach ($theme->getExternals() as $url) {
  647. $this->cssLink($url, $mainTheme, $media);
  648. }
  649. // If the currently-selected theme has dependencies on other themes,
  650. // we'll need to load their display.css files as well in order.
  651. $baseThemes = $theme->getDeps();
  652. foreach ($baseThemes as $baseTheme) {
  653. $this->cssLink('css/display.css', $baseTheme, $media);
  654. }
  655. $this->cssLink('css/display.css', $mainTheme, $media);
  656. // Additional styles for RTL languages
  657. if (is_rtl(common_language())) {
  658. if (file_exists(Theme::file('css/rtl.css'))) {
  659. $this->cssLink('css/rtl.css', $mainTheme, $media);
  660. }
  661. }
  662. }
  663. /**
  664. * Show OpenSearch headers
  665. *
  666. * @return void
  667. */
  668. public function showOpenSearch()
  669. {
  670. $this->element('link', array('rel' => 'search',
  671. 'type' => 'application/opensearchdescription+xml',
  672. 'href' => common_local_url('opensearch', array('type' => 'people')),
  673. 'title' => common_config('site', 'name') . ' People Search'));
  674. $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
  675. 'href' => common_local_url('opensearch', array('type' => 'notice')),
  676. 'title' => common_config('site', 'name') . ' Notice Search'));
  677. }
  678. /**
  679. * Show feed headers
  680. *
  681. * MAY overload
  682. *
  683. * @return void
  684. */
  685. public function showFeeds()
  686. {
  687. foreach ($this->getFeeds() as $feed) {
  688. $this->element('link', array('rel' => $feed->rel(),
  689. 'href' => $feed->url,
  690. 'type' => $feed->mimeType(),
  691. 'title' => $feed->title));
  692. }
  693. }
  694. /**
  695. * An array of feeds for this action.
  696. *
  697. * Returns an array of potential feeds for this action.
  698. *
  699. * @return array Feed object to show in head and links
  700. */
  701. public function getFeeds()
  702. {
  703. return [];
  704. }
  705. /**
  706. * Show description.
  707. *
  708. * SHOULD overload
  709. *
  710. * @return void
  711. */
  712. public function showDescription()
  713. {
  714. // does nothing by default
  715. }
  716. /**
  717. * Show extra stuff in <head>.
  718. *
  719. * MAY overload
  720. *
  721. * @return void
  722. */
  723. public function extraHead()
  724. {
  725. // does nothing by default
  726. }
  727. /**
  728. * Show body.
  729. *
  730. * Calls template methods
  731. *
  732. * @return void
  733. * @throws ServerException
  734. * @throws ReflectionException
  735. */
  736. public function showBody()
  737. {
  738. $params = array('id' => $this->getActionName());
  739. if ($this->scoped instanceof Profile) {
  740. $params['class'] = 'user_in';
  741. }
  742. $this->elementStart('body', $params);
  743. $this->elementStart('div', array('id' => 'wrap'));
  744. if (Event::handle('StartShowHeader', array($this))) {
  745. $this->showHeader();
  746. $this->flush();
  747. Event::handle('EndShowHeader', array($this));
  748. }
  749. $this->showCore();
  750. $this->flush();
  751. if (Event::handle('StartShowFooter', array($this))) {
  752. $this->showFooter();
  753. $this->flush();
  754. Event::handle('EndShowFooter', array($this));
  755. }
  756. $this->elementEnd('div');
  757. $this->showScripts();
  758. $this->elementEnd('body');
  759. }
  760. public function getActionName()
  761. {
  762. return $this->action;
  763. }
  764. /**
  765. * Show header of the page.
  766. *
  767. * Calls template methods
  768. *
  769. * @return void
  770. * @throws ServerException
  771. */
  772. public function showHeader()
  773. {
  774. $this->elementStart('div', array('id' => 'header'));
  775. $this->showLogo();
  776. $this->showPrimaryNav();
  777. if (Event::handle('StartShowSiteNotice', array($this))) {
  778. $this->showSiteNotice();
  779. Event::handle('EndShowSiteNotice', array($this));
  780. }
  781. $this->elementEnd('div');
  782. }
  783. /**
  784. * Show configured logo.
  785. *
  786. * @return void
  787. * @throws ServerException
  788. */
  789. public function showLogo()
  790. {
  791. $this->elementStart('address', array('id' => 'site_contact', 'class' => 'h-card'));
  792. if (Event::handle('StartAddressData', array($this))) {
  793. if (common_config('singleuser', 'enabled')) {
  794. $user = User::singleUser();
  795. $url = common_local_url(
  796. 'showstream',
  797. array('nickname' => $user->nickname)
  798. );
  799. } elseif (common_logged_in()) {
  800. $cur = common_current_user();
  801. $url = common_local_url('all', array('nickname' => $cur->nickname));
  802. } else {
  803. $url = common_local_url('public');
  804. }
  805. $this->elementStart('a', array('class' => 'home bookmark',
  806. 'href' => $url));
  807. if (GNUsocial::isHTTPS()) {
  808. $logoUrl = common_config('site', 'ssllogo');
  809. if (empty($logoUrl)) {
  810. // if logo is an uploaded file, try to fall back to HTTPS file URL
  811. $httpUrl = common_config('site', 'logo');
  812. if (!empty($httpUrl)) {
  813. try {
  814. $f = File::getByUrl($httpUrl);
  815. if (!empty($f->filename)) {
  816. // this will handle the HTTPS case
  817. $logoUrl = File::url($f->filename);
  818. }
  819. } catch (NoResultException $e) {
  820. // no match
  821. }
  822. }
  823. }
  824. } else {
  825. $logoUrl = common_config('site', 'logo');
  826. }
  827. if (empty($logoUrl) && file_exists(Theme::file('logo.png'))) {
  828. // This should handle the HTTPS case internally
  829. $logoUrl = Theme::path('logo.png');
  830. }
  831. if (!empty($logoUrl)) {
  832. $this->element('img', array('class' => 'logo u-photo p-name',
  833. 'src' => $logoUrl,
  834. 'alt' => common_config('site', 'name')));
  835. }
  836. $this->elementEnd('a');
  837. Event::handle('EndAddressData', array($this));
  838. }
  839. $this->elementEnd('address');
  840. }
  841. /**
  842. * Show primary navigation.
  843. *
  844. * @return void
  845. */
  846. public function showPrimaryNav()
  847. {
  848. $this->elementStart('div', array('id' => 'site_nav_global_primary'));
  849. $user = common_current_user();
  850. if (!empty($user) || !common_config('site', 'private')) {
  851. $form = new SearchForm($this);
  852. $form->show();
  853. }
  854. $pn = new PrimaryNav($this);
  855. $pn->show();
  856. $this->elementEnd('div');
  857. }
  858. /**
  859. * Show site notice.
  860. *
  861. * @return void
  862. */
  863. public function showSiteNotice()
  864. {
  865. // Revist. Should probably do an hAtom pattern here
  866. $text = common_config('site', 'notice');
  867. if ($text) {
  868. $this->elementStart('div', array('id' => 'site_notice',
  869. 'class' => 'system_notice'));
  870. $this->raw($text);
  871. $this->elementEnd('div');
  872. }
  873. }
  874. /**
  875. * Show core.
  876. *
  877. * Shows local navigation, content block and aside.
  878. *
  879. * @return void
  880. * @throws ReflectionException
  881. */
  882. public function showCore()
  883. {
  884. $this->elementStart('div', array('id' => 'core'));
  885. $this->elementStart('div', array('id' => 'aside_primary_wrapper'));
  886. $this->elementStart('div', array('id' => 'content_wrapper'));
  887. $this->elementStart('div', array('id' => 'site_nav_local_views_wrapper'));
  888. if (Event::handle('StartShowLocalNavBlock', array($this))) {
  889. $this->showLocalNavBlock();
  890. $this->flush();
  891. Event::handle('EndShowLocalNavBlock', array($this));
  892. }
  893. if (Event::handle('StartShowContentBlock', array($this))) {
  894. $this->showContentBlock();
  895. $this->flush();
  896. Event::handle('EndShowContentBlock', array($this));
  897. }
  898. if (Event::handle('StartShowAside', array($this))) {
  899. $this->showAside();
  900. $this->flush();
  901. Event::handle('EndShowAside', array($this));
  902. }
  903. $this->elementEnd('div');
  904. $this->elementEnd('div');
  905. $this->elementEnd('div');
  906. $this->elementEnd('div');
  907. }
  908. /**
  909. * Show local navigation block.
  910. *
  911. * @return void
  912. */
  913. public function showLocalNavBlock()
  914. {
  915. // Need to have this ID for CSS; I'm too lazy to add it to
  916. // all menus
  917. $this->elementStart('div', array('id' => 'site_nav_local_views'));
  918. // Cheat cheat cheat!
  919. $this->showLocalNav();
  920. $this->elementEnd('div');
  921. }
  922. /**
  923. * Show local navigation.
  924. *
  925. * SHOULD overload
  926. *
  927. * @return void
  928. */
  929. public function showLocalNav()
  930. {
  931. $nav = new DefaultLocalNav($this);
  932. $nav->show();
  933. }
  934. /**
  935. * Show content block.
  936. *
  937. * @return void
  938. * @throws ReflectionException
  939. */
  940. public function showContentBlock()
  941. {
  942. $this->elementStart('div', array('id' => 'content'));
  943. if (common_logged_in()) {
  944. if (Event::handle('StartShowNoticeForm', array($this))) {
  945. $this->showNoticeForm();
  946. Event::handle('EndShowNoticeForm', array($this));
  947. }
  948. }
  949. if (Event::handle('StartShowPageTitle', array($this))) {
  950. $this->showPageTitle();
  951. Event::handle('EndShowPageTitle', array($this));
  952. }
  953. $this->showPageNoticeBlock();
  954. $this->elementStart('div', array('id' => 'content_inner'));
  955. // show the actual content (forms, lists, whatever)
  956. $this->showContent();
  957. $this->elementEnd('div');
  958. $this->elementEnd('div');
  959. }
  960. /**
  961. * Show notice form.
  962. *
  963. * MAY overload if no notice form needed... or direct message box????
  964. *
  965. * @return void
  966. */
  967. public function showNoticeForm()
  968. {
  969. // TRANS: Tab on the notice form.
  970. $tabs = array('status' => array('title' => _m('TAB', 'Status'),
  971. 'href' => common_local_url('newnotice')));
  972. $this->elementStart('div', 'input_forms');
  973. $this->element('label', array('for' => 'input_form_nav'), _m('TAB', 'Share your:'));
  974. if (Event::handle('StartShowEntryForms', array(&$tabs))) {
  975. $this->elementStart('ul', array('class' => 'nav',
  976. 'id' => 'input_form_nav'));
  977. foreach ($tabs as $tag => $data) {
  978. $tag = htmlspecialchars($tag);
  979. $attrs = array('id' => 'input_form_nav_' . $tag,
  980. 'class' => 'input_form_nav_tab');
  981. if ($tag == 'status') {
  982. $attrs['class'] .= ' current';
  983. }
  984. $this->elementStart('li', $attrs);
  985. $this->element(
  986. 'a',
  987. array('onclick' => 'return SN.U.switchInputFormTab("' . $tag . '");',
  988. 'href' => $data['href']),
  989. $data['title']
  990. );
  991. $this->elementEnd('li');
  992. }
  993. $this->elementEnd('ul');
  994. foreach ($tabs as $tag => $data) {
  995. $attrs = array('class' => 'input_form',
  996. 'id' => 'input_form_' . $tag);
  997. if ($tag == 'status') {
  998. $attrs['class'] .= ' current';
  999. }
  1000. $this->elementStart('div', $attrs);
  1001. $form = null;
  1002. if (Event::handle('StartMakeEntryForm', array($tag, $this, &$form))) {
  1003. if ($tag == 'status') {
  1004. $options = $this->noticeFormOptions();
  1005. $form = new NoticeForm($this, $options);
  1006. }
  1007. Event::handle('EndMakeEntryForm', array($tag, $this, $form));
  1008. }
  1009. if (!empty($form)) {
  1010. $form->show();
  1011. }
  1012. $this->elementEnd('div');
  1013. }
  1014. }
  1015. $this->elementEnd('div');
  1016. }
  1017. public function noticeFormOptions()
  1018. {
  1019. return [];
  1020. }
  1021. /**
  1022. * Show page title.
  1023. *
  1024. * @return void
  1025. */
  1026. public function showPageTitle()
  1027. {
  1028. $this->element('h1', null, $this->title());
  1029. }
  1030. /**
  1031. * Show page notice block.
  1032. *
  1033. * Only show the block if a subclassed action has overrided
  1034. * Action::showPageNotice(), or an event handler is registered for
  1035. * the StartShowPageNotice event, in which case we assume the
  1036. * 'page_notice' definition list is desired. This is to prevent
  1037. * empty 'page_notice' definition lists from being output everywhere.
  1038. *
  1039. * @return void
  1040. * @throws ReflectionException
  1041. */
  1042. public function showPageNoticeBlock()
  1043. {
  1044. $rmethod = new ReflectionMethod($this, 'showPageNotice');
  1045. $dclass = $rmethod->getDeclaringClass()->getName();
  1046. if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {
  1047. $this->elementStart('div', array('id' => 'page_notice',
  1048. 'class' => 'system_notice'));
  1049. if (Event::handle('StartShowPageNotice', array($this))) {
  1050. $this->showPageNotice();
  1051. Event::handle('EndShowPageNotice', array($this));
  1052. }
  1053. $this->elementEnd('div');
  1054. }
  1055. }
  1056. /**
  1057. * Show page notice.
  1058. *
  1059. * SHOULD overload (unless there's not a notice)
  1060. *
  1061. * @return void
  1062. */
  1063. public function showPageNotice()
  1064. {
  1065. }
  1066. /**
  1067. * Show Aside.
  1068. *
  1069. * @return void
  1070. * @throws ReflectionException
  1071. */
  1072. public function showAside()
  1073. {
  1074. $this->elementStart('div', array('id' => 'aside_primary',
  1075. 'class' => 'aside'));
  1076. $this->showProfileBlock();
  1077. if (Event::handle('StartShowObjectNavBlock', array($this))) {
  1078. $this->showObjectNavBlock();
  1079. Event::handle('EndShowObjectNavBlock', array($this));
  1080. }
  1081. if (Event::handle('StartShowSections', array($this))) {
  1082. $this->showSections();
  1083. Event::handle('EndShowSections', array($this));
  1084. }
  1085. if (Event::handle('StartShowExportData', array($this))) {
  1086. $this->showExportData();
  1087. Event::handle('EndShowExportData', array($this));
  1088. }
  1089. $this->elementEnd('div');
  1090. }
  1091. /**
  1092. * If there's a logged-in user, show a bit of login context
  1093. *
  1094. * @return void
  1095. * @throws Exception
  1096. */
  1097. public function showProfileBlock()
  1098. {
  1099. if (common_logged_in()) {
  1100. $block = new DefaultProfileBlock($this);
  1101. $block->show();
  1102. }
  1103. }
  1104. /**
  1105. * Show menu for an object (group, profile)
  1106. *
  1107. * This block will only show if a subclass has overridden
  1108. * the showObjectNav() method.
  1109. *
  1110. * @return void
  1111. * @throws ReflectionException
  1112. */
  1113. public function showObjectNavBlock()
  1114. {
  1115. $rmethod = new ReflectionMethod($this, 'showObjectNav');
  1116. $dclass = $rmethod->getDeclaringClass()->getName();
  1117. if ($dclass != 'Action') {
  1118. // Need to have this ID for CSS; I'm too lazy to add it to
  1119. // all menus
  1120. $this->elementStart('div', array('id' => 'site_nav_object',
  1121. 'class' => 'section'));
  1122. $this->showObjectNav();
  1123. $this->elementEnd('div');
  1124. }
  1125. }
  1126. /**
  1127. * Show object navigation.
  1128. *
  1129. * If there are things to do with this object, show it here.
  1130. *
  1131. * @return void
  1132. */
  1133. public function showObjectNav()
  1134. {
  1135. /* Nothing here. */
  1136. }
  1137. /**
  1138. * Show sections.
  1139. *
  1140. * SHOULD overload
  1141. *
  1142. * @return void
  1143. */
  1144. public function showSections()
  1145. {
  1146. // for each section, show it
  1147. }
  1148. /**
  1149. * Show export data feeds.
  1150. *
  1151. * @return void
  1152. */
  1153. public function showExportData()
  1154. {
  1155. $feeds = $this->getFeeds();
  1156. if (!empty($feeds)) {
  1157. $fl = new FeedList($this, $feeds);
  1158. $fl->show();
  1159. }
  1160. }
  1161. /**
  1162. * Show footer.
  1163. *
  1164. * @return void
  1165. */
  1166. public function showFooter()
  1167. {
  1168. $this->elementStart('div', array('id' => 'footer'));
  1169. if (Event::handle('StartShowInsideFooter', array($this))) {
  1170. $this->showSecondaryNav();
  1171. $this->showLicenses();
  1172. Event::handle('EndShowInsideFooter', array($this));
  1173. }
  1174. $this->elementEnd('div');
  1175. }
  1176. /**
  1177. * Show secondary navigation.
  1178. *
  1179. * @return void
  1180. */
  1181. public function showSecondaryNav()
  1182. {
  1183. $sn = new SecondaryNav($this);
  1184. $sn->show();
  1185. }
  1186. /**
  1187. * Show licenses.
  1188. *
  1189. * @return void
  1190. */
  1191. public function showLicenses()
  1192. {
  1193. $this->showGNUsocialLicense();
  1194. $this->showContentLicense();
  1195. }
  1196. /**
  1197. * Show GNU social license.
  1198. *
  1199. * @return void
  1200. */
  1201. public function showGNUsocialLicense()
  1202. {
  1203. if (common_config('site', 'broughtby')) {
  1204. // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is set.
  1205. // TRANS: Text between [] is a link description, text between () is the link itself.
  1206. // TRANS: Make sure there is no whitespace between "]" and "(".
  1207. // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
  1208. $instr = _('**%%site.name%%** is a social network, courtesy of [%%site.broughtby%%](%%site.broughtbyurl%%).');
  1209. } else {
  1210. // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is not set.
  1211. $instr = _('**%%site.name%%** is a social network.');
  1212. }
  1213. $instr .= ' ';
  1214. // TRANS: Second sentence of the GNU social site license. Mentions the GNU social source code license.
  1215. // TRANS: Make sure there is no whitespace between "]" and "(".
  1216. // TRANS: [%1$s](%2$s) is a link description followed by the link itself
  1217. // TRANS: %3$s is the version of GNU social that is being used.
  1218. $instr .= sprintf(_('It runs on [%1$s](%2$s), version %3$s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), GNUSOCIAL_ENGINE, GNUSOCIAL_ENGINE_URL, GNUSOCIAL_VERSION);
  1219. $output = common_markup_to_html($instr);
  1220. $this->raw($output);
  1221. // do it
  1222. }
  1223. /**
  1224. * Show content license.
  1225. *
  1226. * @return void
  1227. */
  1228. public function showContentLicense()
  1229. {
  1230. if (Event::handle('StartShowContentLicense', array($this))) {
  1231. switch (common_config('license', 'type')) {
  1232. case 'private':
  1233. // TRANS: Content license displayed when license is set to 'private'.
  1234. // TRANS: %1$s is the site name.
  1235. $this->element('p', null, sprintf(
  1236. _('Content and data of %1$s are private and confidential.'),
  1237. common_config('site', 'name')
  1238. ));
  1239. // fall through
  1240. // no break
  1241. case 'allrightsreserved':
  1242. if (common_config('license', 'owner')) {
  1243. // TRANS: Content license displayed when license is set to 'allrightsreserved'.
  1244. // TRANS: %1$s is the copyright owner.
  1245. $this->element('p', null, sprintf(
  1246. _('Content and data copyright by %1$s. All rights reserved.'),
  1247. common_config('license', 'owner')
  1248. ));
  1249. } else {
  1250. // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
  1251. $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
  1252. }
  1253. break;
  1254. case 'cc': // fall through
  1255. default:
  1256. $this->elementStart('p');
  1257. $image = common_config('license', 'image');
  1258. $sslimage = common_config('license', 'sslimage');
  1259. if (GNUsocial::isHTTPS()) {
  1260. if (!empty($sslimage)) {
  1261. $url = $sslimage;
  1262. } elseif (preg_match('#^http://i.creativecommons.org/#', $image)) {
  1263. // CC support HTTPS on their images
  1264. $url = preg_replace('/^http/', 'https', $image, 1);
  1265. } else {
  1266. // Better to show mixed content than no content
  1267. $url = $image;
  1268. }
  1269. } else {
  1270. $url = $image;
  1271. }
  1272. $this->element('img', array('id' => 'license_cc',
  1273. 'src' => $url,
  1274. 'alt' => common_config('license', 'title'),
  1275. 'width' => '80',
  1276. 'height' => '15'));
  1277. $this->text(' ');
  1278. // TRANS: license message in footer.
  1279. // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
  1280. $notice = _('All %1$s content and data are available under the %2$s license.');
  1281. $link = sprintf(
  1282. '<a class="license" rel="external license" href="%1$s">%2$s</a>',
  1283. htmlspecialchars(common_config('license', 'url')),
  1284. htmlspecialchars(common_config('license', 'title'))
  1285. );
  1286. $this->raw(@sprintf(
  1287. htmlspecialchars($notice),
  1288. htmlspecialchars(common_config('site', 'name')),
  1289. $link
  1290. ));
  1291. $this->elementEnd('p');
  1292. break;
  1293. }
  1294. Event::handle('EndShowContentLicense', array($this));
  1295. }
  1296. }
  1297. /**
  1298. * Show javascript headers
  1299. *
  1300. * @return void
  1301. */
  1302. public function showScripts()
  1303. {
  1304. if (Event::handle('StartShowScripts', array($this))) {
  1305. if (Event::handle('StartShowJQueryScripts', array($this))) {
  1306. $this->script('extlib/jquery.js');
  1307. $this->script('extlib/jquery.form.js');
  1308. $this->script('extlib/jquery-ui/jquery-ui.js');
  1309. $this->script('extlib/jquery.cookie.js');
  1310. Event::handle('EndShowJQueryScripts', array($this));
  1311. }
  1312. if (Event::handle('StartShowStatusNetScripts', array($this))) {
  1313. $this->script('util.js');
  1314. $this->script('xbImportNode.js');
  1315. // This route isn't available in single-user mode.
  1316. // Not sure why, but it causes errors here.
  1317. $this->inlineScript('var _peopletagAC = "' .
  1318. common_local_url('peopletagautocomplete') . '";');
  1319. $this->showScriptMessages();
  1320. $this->showScriptVariables();
  1321. // Anti-framing code to avoid clickjacking attacks in older browsers.
  1322. // This will show a blank page if the page is being framed, which is
  1323. // consistent with the behavior of the 'X-Frame-Options: SAMEORIGIN'
  1324. // header, which prevents framing in newer browser.
  1325. if (common_config('javascript', 'bustframes')) {
  1326. $this->inlineScript('if (window.top !== window.self) { document.write = ""; window.top.location = window.self.location; setTimeout(function () { document.body.innerHTML = ""; }, 1); window.self.onload = function () { document.body.innerHTML = ""; }; }');
  1327. }
  1328. Event::handle('EndShowStatusNetScripts', array($this));
  1329. }
  1330. Event::handle('EndShowScripts', array($this));
  1331. }
  1332. }
  1333. /**
  1334. * Exports a map of localized text strings to JavaScript code.
  1335. *
  1336. * Plugins can add to what's exported by hooking the StartScriptMessages or EndScriptMessages
  1337. * events and appending to the array. Try to avoid adding strings that won't be used, as
  1338. * they'll be added to HTML output.
  1339. */
  1340. public function showScriptMessages()
  1341. {
  1342. $messages = [];
  1343. if (Event::handle('StartScriptMessages', array($this, &$messages))) {
  1344. // Common messages needed for timeline views etc...
  1345. // TRANS: Localized tooltip for '...' expansion button on overlong remote messages.
  1346. $messages['showmore_tooltip'] = _m('TOOLTIP', 'Show more');
  1347. $messages['popup_close_button'] = _m('TOOLTIP', 'Close popup');
  1348. $messages = array_merge($messages, $this->getScriptMessages());
  1349. Event::handle('EndScriptMessages', array($this, &$messages));
  1350. }
  1351. if (!empty($messages)) {
  1352. $this->inlineScript('SN.messages=' . json_encode($messages));
  1353. }
  1354. return $messages;
  1355. }
  1356. /**
  1357. * If the action will need localizable text strings, export them here like so:
  1358. *
  1359. * return array('pool_deepend' => _('Deep end'),
  1360. * 'pool_shallow' => _('Shallow end'));
  1361. *
  1362. * The exported map will be available via SN.msg() to JS code:
  1363. *
  1364. * $('#pool').html('<div class="deepend"></div><div class="shallow"></div>');
  1365. * $('#pool .deepend').text(SN.msg('pool_deepend'));
  1366. * $('#pool .shallow').text(SN.msg('pool_shallow'));
  1367. *
  1368. * Exports a map of localized text strings to JavaScript code.
  1369. *
  1370. * Plugins can add to what's exported on any action by hooking the StartScriptMessages or
  1371. * EndScriptMessages events and appending to the array. Try to avoid adding strings that won't
  1372. * be used, as they'll be added to HTML output.
  1373. */
  1374. public function getScriptMessages()
  1375. {
  1376. return [];
  1377. }
  1378. protected function showScriptVariables()
  1379. {
  1380. $vars = [];
  1381. if (Event::handle('StartScriptVariables', array($this, &$vars))) {
  1382. $vars['urlNewNotice'] = common_local_url('newnotice');
  1383. $vars['xhrTimeout'] = ini_get('max_execution_time') * 1000; // milliseconds
  1384. Event::handle('EndScriptVariables', array($this, &$vars));
  1385. }
  1386. $this->inlineScript('SN.V = ' . json_encode($vars) . ';');
  1387. return $vars;
  1388. }
  1389. /**
  1390. * Show anonymous message.
  1391. *
  1392. * SHOULD overload
  1393. *
  1394. * @return void
  1395. */
  1396. public function showAnonymousMessage()
  1397. {
  1398. // needs to be defined by the class
  1399. }
  1400. /**
  1401. * Integer value of an argument
  1402. *
  1403. * @param string $key query key we're interested in
  1404. * @param string $defValue optional default value (default null)
  1405. * @param string $maxValue optional max value (default null)
  1406. * @param string $minValue optional min value (default null)
  1407. *
  1408. * @return integer integer value
  1409. */
  1410. public function int($key, $defValue = null, $maxValue = null, $minValue = null)
  1411. {
  1412. $arg = intval($this->arg($key));
  1413. if (!is_numeric($this->arg($key)) || $arg != $this->arg($key)) {
  1414. return $defValue;
  1415. }
  1416. if (!is_null($maxValue)) {
  1417. $arg = min($arg, $maxValue);
  1418. }
  1419. if (!is_null($minValue)) {
  1420. $arg = max($arg, $minValue);
  1421. }
  1422. return $arg;
  1423. }
  1424. /**
  1425. * Returns the current URL
  1426. *
  1427. * @return string current URL
  1428. */
  1429. public function selfUrl()
  1430. {
  1431. list($action, $args) = $this->returnToArgs();
  1432. return common_local_url($action, $args);
  1433. }
  1434. /**
  1435. * Generate pagination links
  1436. *
  1437. * @param boolean $have_before is there something before?
  1438. * @param boolean $have_after is there something after?
  1439. * @param integer $page current page
  1440. * @param string $action current action
  1441. * @param array $args rest of query arguments
  1442. *
  1443. * @return void
  1444. */
  1445. // XXX: The messages in this pagination method only tailor to navigating
  1446. // notices. In other lists, "Previous"/"Next" type navigation is
  1447. // desirable, but not available.
  1448. /**
  1449. * Returns arguments sufficient for re-constructing URL
  1450. *
  1451. * @return array two elements: action, other args
  1452. */
  1453. public function returnToArgs()
  1454. {
  1455. $action = $this->getActionName();
  1456. $args = $this->args;
  1457. unset($args['action']);
  1458. if (common_config('site', 'fancy')) {
  1459. unset($args['p']);
  1460. }
  1461. if (array_key_exists('submit', $args)) {
  1462. unset($args['submit']);
  1463. }
  1464. foreach (array_keys($_COOKIE) as $cookie) {
  1465. unset($args[$cookie]);
  1466. }
  1467. return array($action, $args);
  1468. }
  1469. /**
  1470. * Generate a menu item
  1471. *
  1472. * @param string $url menu URL
  1473. * @param string $text menu name
  1474. * @param string $title title attribute, null by default
  1475. * @param boolean $is_selected current menu item, false by default
  1476. * @param string $id element id, null by default
  1477. *
  1478. * @return void
  1479. */
  1480. public function menuItem($url, $text, $title = null, $is_selected = false, $id = null, $class = null)
  1481. {
  1482. // Added @id to li for some control.
  1483. // XXX: We might want to move this to htmloutputter.php
  1484. $lattrs = [];
  1485. $classes = [];
  1486. if ($class !== null) {
  1487. $classes[] = trim($class);
  1488. }
  1489. if ($is_selected) {
  1490. $classes[] = 'current';
  1491. }
  1492. if (!empty($classes)) {
  1493. $lattrs['class'] = implode(' ', $classes);
  1494. }
  1495. if (!is_null($id)) {
  1496. $lattrs['id'] = $id;
  1497. }
  1498. $this->elementStart('li', $lattrs);
  1499. $attrs['href'] = $url;
  1500. if ($title) {
  1501. $attrs['title'] = $title;
  1502. }
  1503. $this->element('a', $attrs, $text);
  1504. $this->elementEnd('li');
  1505. }
  1506. /**
  1507. * Check the session token.
  1508. *
  1509. * Checks that the current form has the correct session token,
  1510. * and throw an exception if it does not.
  1511. *
  1512. * @return void
  1513. */
  1514. // XXX: Finding this type of check with the same message about 50 times.
  1515. // Possible to refactor?
  1516. public function pagination($have_before, $have_after, $page, $action, $args = null)
  1517. {
  1518. // Does a little before-after block for next/prev page
  1519. if ($have_before || $have_after) {
  1520. $this->elementStart('ul', array('class' => 'nav',
  1521. 'id' => 'pagination'));
  1522. }
  1523. if ($have_before) {
  1524. $pargs = array('page' => $page - 1);
  1525. $this->elementStart('li', array('class' => 'nav_prev'));
  1526. $this->element(
  1527. 'a',
  1528. array('href' => common_local_url($action, $args, $pargs),
  1529. 'rel' => 'prev'),
  1530. // TRANS: Pagination message to go to a page displaying information more in the
  1531. // TRANS: present than the currently displayed information.
  1532. _('After')
  1533. );
  1534. $this->elementEnd('li');
  1535. }
  1536. if ($have_after) {
  1537. $pargs = array('page' => $page + 1);
  1538. $this->elementStart('li', array('class' => 'nav_next'));
  1539. $this->element(
  1540. 'a',
  1541. array('href' => common_local_url($action, $args, $pargs),
  1542. 'rel' => 'next'),
  1543. // TRANS: Pagination message to go to a page displaying information more in the
  1544. // TRANS: past than the currently displayed information.
  1545. _('Before')
  1546. );
  1547. $this->elementEnd('li');
  1548. }
  1549. if ($have_before || $have_after) {
  1550. $this->elementEnd('ul');
  1551. }
  1552. }
  1553. public function checkSessionToken()
  1554. {
  1555. // CSRF protection
  1556. $token = $this->trimmed('token');
  1557. if (empty($token) || $token != common_session_token()) {
  1558. // TRANS: Client error text when there is a problem with the session token.
  1559. $this->clientError(_('There was a problem with your session token.'));
  1560. }
  1561. }
  1562. }