htmloutputter.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Low-level generator for HTML
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Output
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @copyright 2008 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. // Can include XHTML options but these are too fragile in practice.
  32. define('PAGE_TYPE_PREFS', 'text/html');
  33. /**
  34. * Low-level generator for HTML
  35. *
  36. * Abstracts some of the code necessary for HTML generation. Especially
  37. * has methods for generating HTML form elements. Note that these have
  38. * been created kind of haphazardly, not with an eye to making a general
  39. * HTML-creation class.
  40. *
  41. * @category Output
  42. * @package StatusNet
  43. * @author Evan Prodromou <evan@status.net>
  44. * @author Sarven Capadisli <csarven@status.net>
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. *
  48. * @see Action
  49. * @see XMLOutputter
  50. */
  51. class HTMLOutputter extends XMLOutputter
  52. {
  53. protected $DTD = array('doctype' => 'html',
  54. 'spec' => '-//W3C//DTD XHTML 1.0 Strict//EN',
  55. 'uri' => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
  56. /**
  57. * Constructor
  58. *
  59. * Just wraps the XMLOutputter constructor.
  60. *
  61. * @param string $output URI to output to, default = stdout
  62. * @param boolean $indent Whether to indent output, default true
  63. */
  64. function __construct($output='php://output', $indent=null)
  65. {
  66. parent::__construct($output, $indent);
  67. }
  68. /**
  69. * Start an HTML document
  70. *
  71. * If $type isn't specified, will attempt to do content negotiation.
  72. *
  73. * Attempts to do content negotiation for language, also.
  74. *
  75. * @param string $type MIME type to use; default is to do negotation.
  76. *
  77. * @todo extract content negotiation code to an HTTP module or class.
  78. *
  79. * @return void
  80. */
  81. function startHTML($type=null)
  82. {
  83. if (!$type) {
  84. $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
  85. $_SERVER['HTTP_ACCEPT'] : null;
  86. // XXX: allow content negotiation for RDF, RSS, or XRDS
  87. $cp = common_accept_to_prefs($httpaccept);
  88. $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
  89. $type = common_negotiate_type($cp, $sp);
  90. if (!$type) {
  91. // TRANS: Client exception 406
  92. throw new ClientException(_('This page is not available in a '.
  93. 'media type you accept'), 406);
  94. }
  95. }
  96. header('Content-Type: '.$type);
  97. // Output anti-framing headers to prevent clickjacking (respected by newer
  98. // browsers).
  99. if (common_config('javascript', 'bustframes')) {
  100. header('X-XSS-Protection: 1; mode=block'); // detect XSS Reflection attacks
  101. header('X-Frame-Options: SAMEORIGIN'); // no rendering if origin mismatch
  102. }
  103. $this->extraHeaders();
  104. if (preg_match("/.*\/.*xml/", $type)) {
  105. // Required for XML documents
  106. $this->startXML();
  107. }
  108. $this->writeDTD();
  109. $language = $this->getLanguage();
  110. $attrs = array(
  111. 'xmlns' => 'http://www.w3.org/1999/xhtml',
  112. 'xml:lang' => $language,
  113. 'lang' => $language
  114. );
  115. if (Event::handle('StartHtmlElement', array($this, &$attrs))) {
  116. $this->elementStart('html', $attrs);
  117. Event::handle('EndHtmlElement', array($this, &$attrs));
  118. }
  119. }
  120. public function setDTD($doctype, $spec, $uri)
  121. {
  122. $this->DTD = array('doctype' => $doctype, 'spec' => $spec, 'uri' => $uri);
  123. }
  124. protected function writeDTD()
  125. {
  126. $this->xw->writeDTD($this->DTD['doctype'],
  127. $this->DTD['spec'],
  128. $this->DTD['uri']);
  129. }
  130. function getLanguage()
  131. {
  132. // FIXME: correct language for interface
  133. return common_language();
  134. }
  135. /**
  136. * Ends an HTML document
  137. *
  138. * @return void
  139. */
  140. function endHTML()
  141. {
  142. $this->elementEnd('html');
  143. $this->endXML();
  144. }
  145. /**
  146. * To specify additional HTTP headers for the action
  147. *
  148. * @return void
  149. */
  150. function extraHeaders()
  151. {
  152. // Needs to be overloaded
  153. }
  154. /**
  155. * Output an HTML text input element
  156. *
  157. * Despite the name, it is specifically for outputting a
  158. * text input element, not other <input> elements. It outputs
  159. * a cluster of elements, including a <label> and an associated
  160. * instructions span.
  161. *
  162. * If $attrs['type'] does not exist it will be set to 'text'.
  163. *
  164. * @param string $id element ID, must be unique on page
  165. * @param string $label text of label for the element
  166. * @param string $value value of the element, default null
  167. * @param string $instructions instructions for valid input
  168. * @param string $name name of the element; if null, the id will
  169. * be used
  170. * @param bool $required HTML5 required attribute (exclude when false)
  171. * @param array $attrs Initial attributes manually set in an array (overwritten by previous options)
  172. *
  173. * @todo add a $maxLength parameter
  174. * @todo add a $size parameter
  175. *
  176. * @return void
  177. */
  178. function input($id, $label, $value=null, $instructions=null, $name=null, $required=false, array $attrs=array())
  179. {
  180. $this->element('label', array('for' => $id), $label);
  181. if (!array_key_exists('type', $attrs)) {
  182. $attrs['type'] = 'text';
  183. }
  184. $attrs['id'] = $id;
  185. $attrs['name'] = is_null($name) ? $id : $name;
  186. if (array_key_exists('placeholder', $attrs) && (is_null($attrs['placeholder']) || $attrs['placeholder'] === '')) {
  187. // If placeholder is type-aware equal to '' or null, unset it as we apparently don't want a placeholder value
  188. unset($attrs['placeholder']);
  189. } else {
  190. // If the placeholder is set use it, or use the label as fallback.
  191. $attrs['placeholder'] = isset($attrs['placeholder']) ? $attrs['placeholder'] : $label;
  192. }
  193. if (!is_null($value)) { // value can be 0 or ''
  194. $attrs['value'] = $value;
  195. }
  196. if (!empty($required)) {
  197. $attrs['required'] = 'required';
  198. }
  199. $this->element('input', $attrs);
  200. if ($instructions) {
  201. $this->element('p', 'form_guide', $instructions);
  202. }
  203. }
  204. /**
  205. * output an HTML checkbox and associated elements
  206. *
  207. * Note that the value is default 'true' (the string), which can
  208. * be used by Action::boolean()
  209. *
  210. * @param string $id element ID, must be unique on page
  211. * @param string $label text of label for the element
  212. * @param string $checked if the box is checked, default false
  213. * @param string $instructions instructions for valid input
  214. * @param string $value value of the checkbox, default 'true'
  215. * @param string $disabled show the checkbox disabled, default false
  216. *
  217. * @return void
  218. *
  219. * @todo add a $name parameter
  220. */
  221. function checkbox($id, $label, $checked=false, $instructions=null,
  222. $value='true', $disabled=false)
  223. {
  224. $attrs = array('name' => $id,
  225. 'type' => 'checkbox',
  226. 'class' => 'checkbox',
  227. 'id' => $id);
  228. if ($value) {
  229. $attrs['value'] = $value;
  230. }
  231. if ($checked) {
  232. $attrs['checked'] = 'checked';
  233. }
  234. if ($disabled) {
  235. $attrs['disabled'] = 'true';
  236. }
  237. $this->element('input', $attrs);
  238. $this->text(' ');
  239. $this->element('label', array('class' => 'checkbox',
  240. 'for' => $id),
  241. $label);
  242. $this->text(' ');
  243. if ($instructions) {
  244. $this->element('p', 'form_guide', $instructions);
  245. }
  246. }
  247. /**
  248. * output an HTML combobox/select and associated elements
  249. *
  250. * $content is an array of key-value pairs for the dropdown, where
  251. * the key is the option value attribute and the value is the option
  252. * text. (Careful on the overuse of 'value' here.)
  253. *
  254. * @param string $id element ID, must be unique on page
  255. * @param string $label text of label for the element
  256. * @param array $content options array, value => text
  257. * @param string $instructions instructions for valid input
  258. * @param string $blank_select whether to have a blank entry, default false
  259. * @param string $selected selected value, default null
  260. *
  261. * @return void
  262. *
  263. * @todo add a $name parameter
  264. */
  265. function dropdown($id, $label, $content, $instructions=null,
  266. $blank_select=false, $selected=null)
  267. {
  268. $this->element('label', array('for' => $id), $label);
  269. $this->elementStart('select', array('id' => $id, 'name' => $id));
  270. if ($blank_select) {
  271. $this->element('option', array('value' => ''));
  272. }
  273. foreach ($content as $value => $option) {
  274. if ($value == $selected) {
  275. $this->element('option', array('value' => $value,
  276. 'selected' => 'selected'),
  277. $option);
  278. } else {
  279. $this->element('option', array('value' => $value), $option);
  280. }
  281. }
  282. $this->elementEnd('select');
  283. if ($instructions) {
  284. $this->element('p', 'form_guide', $instructions);
  285. }
  286. }
  287. /**
  288. * output an HTML hidden element
  289. *
  290. * $id is re-used as name
  291. *
  292. * @param string $id element ID, must be unique on page
  293. * @param string $value hidden element value, default null
  294. * @param string $name name, if different than ID
  295. *
  296. * @return void
  297. */
  298. function hidden($id, $value, $name=null)
  299. {
  300. $this->element('input', array('name' => $name ?: $id,
  301. 'type' => 'hidden',
  302. 'id' => $id,
  303. 'value' => $value));
  304. }
  305. /**
  306. * output an HTML password input and associated elements
  307. *
  308. * @param string $id element ID, must be unique on page
  309. * @param string $label text of label for the element
  310. * @param string $instructions instructions for valid input
  311. *
  312. * @return void
  313. *
  314. * @todo add a $name parameter
  315. */
  316. function password($id, $label, $instructions=null)
  317. {
  318. $this->element('label', array('for' => $id), $label);
  319. $attrs = array('name' => $id,
  320. 'type' => 'password',
  321. 'class' => 'password',
  322. 'id' => $id);
  323. $this->element('input', $attrs);
  324. if ($instructions) {
  325. $this->element('p', 'form_guide', $instructions);
  326. }
  327. }
  328. /**
  329. * output an HTML submit input and associated elements
  330. *
  331. * @param string $id element ID, must be unique on page
  332. * @param string $label text of the button
  333. * @param string $cls class of the button, default 'submit'
  334. * @param string $name name, if different than ID
  335. * @param string $title title text for the submit button
  336. *
  337. * @return void
  338. *
  339. * @todo add a $name parameter
  340. */
  341. function submit($id, $label, $cls='submit', $name=null, $title=null)
  342. {
  343. $this->element('input', array('type' => 'submit',
  344. 'id' => $id,
  345. 'name' => $name ?: $id,
  346. 'class' => $cls,
  347. 'value' => $label,
  348. 'title' => $title));
  349. }
  350. /**
  351. * output a script (almost always javascript) tag
  352. *
  353. * @param string $src relative or absolute script path
  354. * @param string $type 'type' attribute value of the tag
  355. *
  356. * @return void
  357. */
  358. function script($src, $type='text/javascript')
  359. {
  360. if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
  361. $url = parse_url($src);
  362. if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
  363. // XXX: this seems like a big assumption
  364. if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
  365. $src = common_path($src, GNUsocial::isHTTPS()) . '?version=' . GNUSOCIAL_VERSION;
  366. } else {
  367. if (GNUsocial::isHTTPS()) {
  368. $sslserver = common_config('javascript', 'sslserver');
  369. if (empty($sslserver)) {
  370. if (is_string(common_config('site', 'sslserver')) &&
  371. mb_strlen(common_config('site', 'sslserver')) > 0) {
  372. $server = common_config('site', 'sslserver');
  373. } else if (common_config('site', 'server')) {
  374. $server = common_config('site', 'server');
  375. }
  376. $path = common_config('site', 'path') . '/js/';
  377. } else {
  378. $server = $sslserver;
  379. $path = common_config('javascript', 'sslpath');
  380. if (empty($path)) {
  381. $path = common_config('javascript', 'path');
  382. }
  383. }
  384. $protocol = 'https';
  385. } else {
  386. $path = common_config('javascript', 'path');
  387. if (empty($path)) {
  388. $path = common_config('site', 'path') . '/js/';
  389. }
  390. $server = common_config('javascript', 'server');
  391. if (empty($server)) {
  392. $server = common_config('site', 'server');
  393. }
  394. $protocol = 'http';
  395. }
  396. if ($path[strlen($path)-1] != '/') {
  397. $path .= '/';
  398. }
  399. if ($path[0] != '/') {
  400. $path = '/'.$path;
  401. }
  402. $src = $protocol.'://'.$server.$path.$src . '?version=' . GNUSOCIAL_VERSION;
  403. }
  404. }
  405. $this->element('script', array('type' => $type,
  406. 'src' => $src),
  407. ' ');
  408. Event::handle('EndScriptElement', array($this,$src,$type));
  409. }
  410. }
  411. /**
  412. * output a script (almost always javascript) tag with inline
  413. * code.
  414. *
  415. * @param string $code code to put in the script tag
  416. * @param string $type 'type' attribute value of the tag
  417. *
  418. * @return void
  419. */
  420. function inlineScript($code, $type='text/javascript')
  421. {
  422. if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
  423. $this->elementStart('script', array('type' => $type));
  424. if($type == 'text/javascript') {
  425. $this->raw('/*<![CDATA[*/ '); // XHTML compat
  426. }
  427. $this->raw($code);
  428. if($type == 'text/javascript') {
  429. $this->raw(' /*]]>*/'); // XHTML compat
  430. }
  431. $this->elementEnd('script');
  432. Event::handle('EndInlineScriptElement', array($this,$code,$type));
  433. }
  434. }
  435. /**
  436. * output a css link
  437. *
  438. * @param string $src relative path within the theme directory, or an absolute path
  439. * @param string $theme 'theme' that contains the stylesheet
  440. * @param string media 'media' attribute of the tag
  441. *
  442. * @return void
  443. */
  444. function cssLink($src,$theme=null,$media=null)
  445. {
  446. if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
  447. $url = parse_url($src);
  448. if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
  449. {
  450. if(file_exists(Theme::file($src,$theme))){
  451. $src = Theme::path($src, $theme);
  452. }else{
  453. $src = common_path($src, GNUsocial::isHTTPS());
  454. }
  455. $src.= '?version=' . GNUSOCIAL_VERSION;
  456. }
  457. $this->element('link', array('rel' => 'stylesheet',
  458. 'type' => 'text/css',
  459. 'href' => $src,
  460. 'media' => $media));
  461. Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
  462. }
  463. }
  464. /**
  465. * output a style (almost always css) tag with inline
  466. * code.
  467. *
  468. * @param string $code code to put in the style tag
  469. * @param string $type 'type' attribute value of the tag
  470. * @param string $media 'media' attribute value of the tag
  471. *
  472. * @return void
  473. */
  474. function style($code, $type = 'text/css', $media = null)
  475. {
  476. if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
  477. $this->elementStart('style', array('type' => $type, 'media' => $media));
  478. $this->raw($code);
  479. $this->elementEnd('style');
  480. Event::handle('EndStyleElement', array($this,$code,$type,$media));
  481. }
  482. }
  483. /**
  484. * output an HTML textarea and associated elements
  485. *
  486. * @param string $id element ID, must be unique on page
  487. * @param string $label text of label for the element
  488. * @param string $content content of the textarea, default none
  489. * @param string $instructions instructions for valid input
  490. * @param string $name name of textarea; if null, $id will be used
  491. * @param int $cols number of columns
  492. * @param int $rows number of rows
  493. * @param bool $required HTML5 required attribute (exclude when false)
  494. *
  495. * @return void
  496. */
  497. function textarea(
  498. $id,
  499. $label,
  500. $content = null,
  501. $instructions = null,
  502. $name = null,
  503. $cols = null,
  504. $rows = null,
  505. $required = false
  506. ) {
  507. $this->element('label', array('for' => $id), $label);
  508. $attrs = array(
  509. 'rows' => 3,
  510. 'cols' => 40,
  511. 'id' => $id
  512. );
  513. $attrs['name'] = is_null($name) ? $id : $name;
  514. if ($cols != null) {
  515. $attrs['cols'] = $cols;
  516. }
  517. if ($rows != null) {
  518. $attrs['rows'] = $rows;
  519. }
  520. $this->element(
  521. 'textarea',
  522. $attrs,
  523. is_null($content) ? '' : $content
  524. );
  525. if ($instructions) {
  526. $this->element('p', 'form_guide', $instructions);
  527. }
  528. }
  529. /**
  530. * Internal script to autofocus the given element on page onload.
  531. *
  532. * @param string $id element ID, must refer to an existing element
  533. *
  534. * @return void
  535. *
  536. */
  537. function autofocus($id)
  538. {
  539. $this->inlineScript(
  540. ' $(document).ready(function() {'.
  541. ' var el = $("#' . $id . '");'.
  542. ' if (el.length) { el.focus(); }'.
  543. ' });');
  544. }
  545. }