htmloutputter.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. require_once INSTALLDIR.'/lib/xmloutputter.php';
  34. // Can include XHTML options but these are too fragile in practice.
  35. define('PAGE_TYPE_PREFS', 'text/html');
  36. /**
  37. * Low-level generator for HTML
  38. *
  39. * Abstracts some of the code necessary for HTML generation. Especially
  40. * has methods for generating HTML form elements. Note that these have
  41. * been created kind of haphazardly, not with an eye to making a general
  42. * HTML-creation class.
  43. *
  44. * @category Output
  45. * @package StatusNet
  46. * @author Evan Prodromou <evan@status.net>
  47. * @author Sarven Capadisli <csarven@status.net>
  48. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  49. * @link http://status.net/
  50. *
  51. * @see Action
  52. * @see XMLOutputter
  53. */
  54. class HTMLOutputter extends XMLOutputter
  55. {
  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->xw->writeDTD('html',
  109. '-//W3C//DTD XHTML 1.0 Strict//EN',
  110. 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
  111. $language = $this->getLanguage();
  112. $attrs = array(
  113. 'xmlns' => 'http://www.w3.org/1999/xhtml',
  114. 'xml:lang' => $language,
  115. 'lang' => $language
  116. );
  117. if (Event::handle('StartHtmlElement', array($this, &$attrs))) {
  118. $this->elementStart('html', $attrs);
  119. Event::handle('EndHtmlElement', array($this, &$attrs));
  120. }
  121. }
  122. function getLanguage()
  123. {
  124. // FIXME: correct language for interface
  125. return common_language();
  126. }
  127. /**
  128. * Ends an HTML document
  129. *
  130. * @return void
  131. */
  132. function endHTML()
  133. {
  134. $this->elementEnd('html');
  135. $this->endXML();
  136. }
  137. /**
  138. * To specify additional HTTP headers for the action
  139. *
  140. * @return void
  141. */
  142. function extraHeaders()
  143. {
  144. // Needs to be overloaded
  145. }
  146. /**
  147. * Output an HTML text input element
  148. *
  149. * Despite the name, it is specifically for outputting a
  150. * text input element, not other <input> elements. It outputs
  151. * a cluster of elements, including a <label> and an associated
  152. * instructions span.
  153. *
  154. * If $attrs['type'] does not exist it will be set to 'text'.
  155. *
  156. * @param string $id element ID, must be unique on page
  157. * @param string $label text of label for the element
  158. * @param string $value value of the element, default null
  159. * @param string $instructions instructions for valid input
  160. * @param string $name name of the element; if null, the id will
  161. * be used
  162. * @param bool $required HTML5 required attribute (exclude when false)
  163. * @param array $attrs Initial attributes manually set in an array (overwritten by previous options)
  164. *
  165. * @todo add a $maxLength parameter
  166. * @todo add a $size parameter
  167. *
  168. * @return void
  169. */
  170. function input($id, $label, $value=null, $instructions=null, $name=null, $required=false, array $attrs=array())
  171. {
  172. $this->element('label', array('for' => $id), $label);
  173. if (!array_key_exists('type', $attrs)) {
  174. $attrs['type'] = 'text';
  175. }
  176. $attrs['id'] = $id;
  177. $attrs['name'] = is_null($name) ? $id : $name;
  178. if (!is_null($value)) { // value can be 0 or ''
  179. $attrs['value'] = $value;
  180. }
  181. if (!empty($required)) {
  182. $attrs['required'] = 'required';
  183. }
  184. $this->element('input', $attrs);
  185. if ($instructions) {
  186. $this->element('p', 'form_guide', $instructions);
  187. }
  188. }
  189. /**
  190. * output an HTML checkbox and associated elements
  191. *
  192. * Note that the value is default 'true' (the string), which can
  193. * be used by Action::boolean()
  194. *
  195. * @param string $id element ID, must be unique on page
  196. * @param string $label text of label for the element
  197. * @param string $checked if the box is checked, default false
  198. * @param string $instructions instructions for valid input
  199. * @param string $value value of the checkbox, default 'true'
  200. * @param string $disabled show the checkbox disabled, default false
  201. *
  202. * @return void
  203. *
  204. * @todo add a $name parameter
  205. */
  206. function checkbox($id, $label, $checked=false, $instructions=null,
  207. $value='true', $disabled=false)
  208. {
  209. $attrs = array('name' => $id,
  210. 'type' => 'checkbox',
  211. 'class' => 'checkbox',
  212. 'id' => $id);
  213. if ($value) {
  214. $attrs['value'] = $value;
  215. }
  216. if ($checked) {
  217. $attrs['checked'] = 'checked';
  218. }
  219. if ($disabled) {
  220. $attrs['disabled'] = 'true';
  221. }
  222. $this->element('input', $attrs);
  223. $this->text(' ');
  224. $this->element('label', array('class' => 'checkbox',
  225. 'for' => $id),
  226. $label);
  227. $this->text(' ');
  228. if ($instructions) {
  229. $this->element('p', 'form_guide', $instructions);
  230. }
  231. }
  232. /**
  233. * output an HTML combobox/select and associated elements
  234. *
  235. * $content is an array of key-value pairs for the dropdown, where
  236. * the key is the option value attribute and the value is the option
  237. * text. (Careful on the overuse of 'value' here.)
  238. *
  239. * @param string $id element ID, must be unique on page
  240. * @param string $label text of label for the element
  241. * @param array $content options array, value => text
  242. * @param string $instructions instructions for valid input
  243. * @param string $blank_select whether to have a blank entry, default false
  244. * @param string $selected selected value, default null
  245. *
  246. * @return void
  247. *
  248. * @todo add a $name parameter
  249. */
  250. function dropdown($id, $label, $content, $instructions=null,
  251. $blank_select=false, $selected=null)
  252. {
  253. $this->element('label', array('for' => $id), $label);
  254. $this->elementStart('select', array('id' => $id, 'name' => $id));
  255. if ($blank_select) {
  256. $this->element('option', array('value' => ''));
  257. }
  258. foreach ($content as $value => $option) {
  259. if ($value == $selected) {
  260. $this->element('option', array('value' => $value,
  261. 'selected' => 'selected'),
  262. $option);
  263. } else {
  264. $this->element('option', array('value' => $value), $option);
  265. }
  266. }
  267. $this->elementEnd('select');
  268. if ($instructions) {
  269. $this->element('p', 'form_guide', $instructions);
  270. }
  271. }
  272. /**
  273. * output an HTML hidden element
  274. *
  275. * $id is re-used as name
  276. *
  277. * @param string $id element ID, must be unique on page
  278. * @param string $value hidden element value, default null
  279. * @param string $name name, if different than ID
  280. *
  281. * @return void
  282. */
  283. function hidden($id, $value, $name=null)
  284. {
  285. $this->element('input', array('name' => $name ?: $id,
  286. 'type' => 'hidden',
  287. 'id' => $id,
  288. 'value' => $value));
  289. }
  290. /**
  291. * output an HTML password input and associated elements
  292. *
  293. * @param string $id element ID, must be unique on page
  294. * @param string $label text of label for the element
  295. * @param string $instructions instructions for valid input
  296. *
  297. * @return void
  298. *
  299. * @todo add a $name parameter
  300. */
  301. function password($id, $label, $instructions=null)
  302. {
  303. $this->element('label', array('for' => $id), $label);
  304. $attrs = array('name' => $id,
  305. 'type' => 'password',
  306. 'class' => 'password',
  307. 'id' => $id);
  308. $this->element('input', $attrs);
  309. if ($instructions) {
  310. $this->element('p', 'form_guide', $instructions);
  311. }
  312. }
  313. /**
  314. * output an HTML submit input and associated elements
  315. *
  316. * @param string $id element ID, must be unique on page
  317. * @param string $label text of the button
  318. * @param string $cls class of the button, default 'submit'
  319. * @param string $name name, if different than ID
  320. * @param string $title title text for the submit button
  321. *
  322. * @return void
  323. *
  324. * @todo add a $name parameter
  325. */
  326. function submit($id, $label, $cls='submit', $name=null, $title=null)
  327. {
  328. $this->element('input', array('type' => 'submit',
  329. 'id' => $id,
  330. 'name' => $name ?: $id,
  331. 'class' => $cls,
  332. 'value' => $label,
  333. 'title' => $title));
  334. }
  335. /**
  336. * output a script (almost always javascript) tag
  337. *
  338. * @param string $src relative or absolute script path
  339. * @param string $type 'type' attribute value of the tag
  340. *
  341. * @return void
  342. */
  343. function script($src, $type='text/javascript')
  344. {
  345. if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
  346. $url = parse_url($src);
  347. if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
  348. // XXX: this seems like a big assumption
  349. if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
  350. $src = common_path($src, StatusNet::isHTTPS()) . '?version=' . GNUSOCIAL_VERSION;
  351. } else {
  352. if (StatusNet::isHTTPS()) {
  353. $sslserver = common_config('javascript', 'sslserver');
  354. if (empty($sslserver)) {
  355. if (is_string(common_config('site', 'sslserver')) &&
  356. mb_strlen(common_config('site', 'sslserver')) > 0) {
  357. $server = common_config('site', 'sslserver');
  358. } else if (common_config('site', 'server')) {
  359. $server = common_config('site', 'server');
  360. }
  361. $path = common_config('site', 'path') . '/js/';
  362. } else {
  363. $server = $sslserver;
  364. $path = common_config('javascript', 'sslpath');
  365. if (empty($path)) {
  366. $path = common_config('javascript', 'path');
  367. }
  368. }
  369. $protocol = 'https';
  370. } else {
  371. $path = common_config('javascript', 'path');
  372. if (empty($path)) {
  373. $path = common_config('site', 'path') . '/js/';
  374. }
  375. $server = common_config('javascript', 'server');
  376. if (empty($server)) {
  377. $server = common_config('site', 'server');
  378. }
  379. $protocol = 'http';
  380. }
  381. if ($path[strlen($path)-1] != '/') {
  382. $path .= '/';
  383. }
  384. if ($path[0] != '/') {
  385. $path = '/'.$path;
  386. }
  387. $src = $protocol.'://'.$server.$path.$src . '?version=' . GNUSOCIAL_VERSION;
  388. }
  389. }
  390. $this->element('script', array('type' => $type,
  391. 'src' => $src),
  392. ' ');
  393. Event::handle('EndScriptElement', array($this,$src,$type));
  394. }
  395. }
  396. /**
  397. * output a script (almost always javascript) tag with inline
  398. * code.
  399. *
  400. * @param string $code code to put in the script tag
  401. * @param string $type 'type' attribute value of the tag
  402. *
  403. * @return void
  404. */
  405. function inlineScript($code, $type='text/javascript')
  406. {
  407. if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
  408. $this->elementStart('script', array('type' => $type));
  409. if($type == 'text/javascript') {
  410. $this->raw('/*<![CDATA[*/ '); // XHTML compat
  411. }
  412. $this->raw($code);
  413. if($type == 'text/javascript') {
  414. $this->raw(' /*]]>*/'); // XHTML compat
  415. }
  416. $this->elementEnd('script');
  417. Event::handle('EndInlineScriptElement', array($this,$code,$type));
  418. }
  419. }
  420. /**
  421. * output a css link
  422. *
  423. * @param string $src relative path within the theme directory, or an absolute path
  424. * @param string $theme 'theme' that contains the stylesheet
  425. * @param string media 'media' attribute of the tag
  426. *
  427. * @return void
  428. */
  429. function cssLink($src,$theme=null,$media=null)
  430. {
  431. if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
  432. $url = parse_url($src);
  433. if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
  434. {
  435. if(file_exists(Theme::file($src,$theme))){
  436. $src = Theme::path($src, $theme);
  437. }else{
  438. $src = common_path($src, StatusNet::isHTTPS());
  439. }
  440. $src.= '?version=' . GNUSOCIAL_VERSION;
  441. }
  442. $this->element('link', array('rel' => 'stylesheet',
  443. 'type' => 'text/css',
  444. 'href' => $src,
  445. 'media' => $media));
  446. Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
  447. }
  448. }
  449. /**
  450. * output a style (almost always css) tag with inline
  451. * code.
  452. *
  453. * @param string $code code to put in the style tag
  454. * @param string $type 'type' attribute value of the tag
  455. * @param string $media 'media' attribute value of the tag
  456. *
  457. * @return void
  458. */
  459. function style($code, $type = 'text/css', $media = null)
  460. {
  461. if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
  462. $this->elementStart('style', array('type' => $type, 'media' => $media));
  463. $this->raw($code);
  464. $this->elementEnd('style');
  465. Event::handle('EndStyleElement', array($this,$code,$type,$media));
  466. }
  467. }
  468. /**
  469. * output an HTML textarea and associated elements
  470. *
  471. * @param string $id element ID, must be unique on page
  472. * @param string $label text of label for the element
  473. * @param string $content content of the textarea, default none
  474. * @param string $instructions instructions for valid input
  475. * @param string $name name of textarea; if null, $id will be used
  476. * @param int $cols number of columns
  477. * @param int $rows number of rows
  478. * @param bool $required HTML5 required attribute (exclude when false)
  479. *
  480. * @return void
  481. */
  482. function textarea(
  483. $id,
  484. $label,
  485. $content = null,
  486. $instructions = null,
  487. $name = null,
  488. $cols = null,
  489. $rows = null,
  490. $required = false
  491. ) {
  492. $this->element('label', array('for' => $id), $label);
  493. $attrs = array(
  494. 'rows' => 3,
  495. 'cols' => 40,
  496. 'id' => $id
  497. );
  498. $attrs['name'] = is_null($name) ? $id : $name;
  499. if ($cols != null) {
  500. $attrs['cols'] = $cols;
  501. }
  502. if ($rows != null) {
  503. $attrs['rows'] = $rows;
  504. }
  505. $this->element(
  506. 'textarea',
  507. $attrs,
  508. is_null($content) ? '' : $content
  509. );
  510. if ($instructions) {
  511. $this->element('p', 'form_guide', $instructions);
  512. }
  513. }
  514. /**
  515. * Internal script to autofocus the given element on page onload.
  516. *
  517. * @param string $id element ID, must refer to an existing element
  518. *
  519. * @return void
  520. *
  521. */
  522. function autofocus($id)
  523. {
  524. $this->inlineScript(
  525. ' $(document).ready(function() {'.
  526. ' var el = $("#' . $id . '");'.
  527. ' if (el.length) { el.focus(); }'.
  528. ' });');
  529. }
  530. }