PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Controller/Response/Abstract.php

https://bitbucket.org/ksekar/campus
PHP | 796 lines | 381 code | 88 blank | 327 comment | 72 complexity | e1bbb25a70daa884afdabeb0066b50ec MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Abstract.php 24594 2012-01-05 21:27:01Z matthew $
  20. */
  21. /**
  22. * Zend_Controller_Response_Abstract
  23. *
  24. * Base class for Zend_Controller responses
  25. *
  26. * @package Zend_Controller
  27. * @subpackage Response
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Controller_Response_Abstract
  32. {
  33. /**
  34. * Body content
  35. * @var array
  36. */
  37. protected $_body = array();
  38. /**
  39. * Exception stack
  40. * @var Exception
  41. */
  42. protected $_exceptions = array();
  43. /**
  44. * Array of headers. Each header is an array with keys 'name' and 'value'
  45. * @var array
  46. */
  47. protected $_headers = array();
  48. /**
  49. * Array of raw headers. Each header is a single string, the entire header to emit
  50. * @var array
  51. */
  52. protected $_headersRaw = array();
  53. /**
  54. * HTTP response code to use in headers
  55. * @var int
  56. */
  57. protected $_httpResponseCode = 200;
  58. /**
  59. * Flag; is this response a redirect?
  60. * @var boolean
  61. */
  62. protected $_isRedirect = false;
  63. /**
  64. * Whether or not to render exceptions; off by default
  65. * @var boolean
  66. */
  67. protected $_renderExceptions = false;
  68. /**
  69. * Flag; if true, when header operations are called after headers have been
  70. * sent, an exception will be raised; otherwise, processing will continue
  71. * as normal. Defaults to true.
  72. *
  73. * @see canSendHeaders()
  74. * @var boolean
  75. */
  76. public $headersSentThrowsException = true;
  77. /**
  78. * Normalize a header name
  79. *
  80. * Normalizes a header name to X-Capitalized-Names
  81. *
  82. * @param string $name
  83. * @return string
  84. */
  85. protected function _normalizeHeader($name)
  86. {
  87. $filtered = str_replace(array('-', '_'), ' ', (string) $name);
  88. $filtered = ucwords(strtolower($filtered));
  89. $filtered = str_replace(' ', '-', $filtered);
  90. return $filtered;
  91. }
  92. /**
  93. * Set a header
  94. *
  95. * If $replace is true, replaces any headers already defined with that
  96. * $name.
  97. *
  98. * @param string $name
  99. * @param string $value
  100. * @param boolean $replace
  101. * @return Zend_Controller_Response_Abstract
  102. */
  103. public function setHeader($name, $value, $replace = false)
  104. {
  105. $this->canSendHeaders(true);
  106. $name = $this->_normalizeHeader($name);
  107. $value = (string) $value;
  108. if ($replace) {
  109. foreach ($this->_headers as $key => $header) {
  110. if ($name == $header['name']) {
  111. unset($this->_headers[$key]);
  112. }
  113. }
  114. }
  115. $this->_headers[] = array(
  116. 'name' => $name,
  117. 'value' => $value,
  118. 'replace' => $replace
  119. );
  120. return $this;
  121. }
  122. /**
  123. * Set redirect URL
  124. *
  125. * Sets Location header and response code. Forces replacement of any prior
  126. * redirects.
  127. *
  128. * @param string $url
  129. * @param int $code
  130. * @return Zend_Controller_Response_Abstract
  131. */
  132. public function setRedirect($url, $code = 302)
  133. {
  134. $this->canSendHeaders(true);
  135. $this->setHeader('Location', $url, true)
  136. ->setHttpResponseCode($code);
  137. return $this;
  138. }
  139. /**
  140. * Is this a redirect?
  141. *
  142. * @return boolean
  143. */
  144. public function isRedirect()
  145. {
  146. return $this->_isRedirect;
  147. }
  148. /**
  149. * Return array of headers; see {@link $_headers} for format
  150. *
  151. * @return array
  152. */
  153. public function getHeaders()
  154. {
  155. return $this->_headers;
  156. }
  157. /**
  158. * Clear headers
  159. *
  160. * @return Zend_Controller_Response_Abstract
  161. */
  162. public function clearHeaders()
  163. {
  164. $this->_headers = array();
  165. return $this;
  166. }
  167. /**
  168. * Clears the specified HTTP header
  169. *
  170. * @param string $name
  171. * @return Zend_Controller_Response_Abstract
  172. */
  173. public function clearHeader($name)
  174. {
  175. if (! count($this->_headers)) {
  176. return $this;
  177. }
  178. foreach ($this->_headers as $index => $header) {
  179. if ($name == $header['name']) {
  180. unset($this->_headers[$index]);
  181. }
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Set raw HTTP header
  187. *
  188. * Allows setting non key => value headers, such as status codes
  189. *
  190. * @param string $value
  191. * @return Zend_Controller_Response_Abstract
  192. */
  193. public function setRawHeader($value)
  194. {
  195. $this->canSendHeaders(true);
  196. if ('Location' == substr($value, 0, 8)) {
  197. $this->_isRedirect = true;
  198. }
  199. $this->_headersRaw[] = (string) $value;
  200. return $this;
  201. }
  202. /**
  203. * Retrieve all {@link setRawHeader() raw HTTP headers}
  204. *
  205. * @return array
  206. */
  207. public function getRawHeaders()
  208. {
  209. return $this->_headersRaw;
  210. }
  211. /**
  212. * Clear all {@link setRawHeader() raw HTTP headers}
  213. *
  214. * @return Zend_Controller_Response_Abstract
  215. */
  216. public function clearRawHeaders()
  217. {
  218. $this->_headersRaw = array();
  219. return $this;
  220. }
  221. /**
  222. * Clears the specified raw HTTP header
  223. *
  224. * @param string $headerRaw
  225. * @return Zend_Controller_Response_Abstract
  226. */
  227. public function clearRawHeader($headerRaw)
  228. {
  229. if (! count($this->_headersRaw)) {
  230. return $this;
  231. }
  232. $key = array_search($headerRaw, $this->_headersRaw);
  233. if ($key !== false) {
  234. unset($this->_headersRaw[$key]);
  235. }
  236. return $this;
  237. }
  238. /**
  239. * Clear all headers, normal and raw
  240. *
  241. * @return Zend_Controller_Response_Abstract
  242. */
  243. public function clearAllHeaders()
  244. {
  245. return $this->clearHeaders()
  246. ->clearRawHeaders();
  247. }
  248. /**
  249. * Set HTTP response code to use with headers
  250. *
  251. * @param int $code
  252. * @return Zend_Controller_Response_Abstract
  253. */
  254. public function setHttpResponseCode($code)
  255. {
  256. if (!is_int($code) || (100 > $code) || (599 < $code)) {
  257. require_once 'Zend/Controller/Response/Exception.php';
  258. throw new Zend_Controller_Response_Exception('Invalid HTTP response code');
  259. }
  260. if ((300 <= $code) && (307 >= $code)) {
  261. $this->_isRedirect = true;
  262. } else {
  263. $this->_isRedirect = false;
  264. }
  265. $this->_httpResponseCode = $code;
  266. return $this;
  267. }
  268. /**
  269. * Retrieve HTTP response code
  270. *
  271. * @return int
  272. */
  273. public function getHttpResponseCode()
  274. {
  275. return $this->_httpResponseCode;
  276. }
  277. /**
  278. * Can we send headers?
  279. *
  280. * @param boolean $throw Whether or not to throw an exception if headers have been sent; defaults to false
  281. * @return boolean
  282. * @throws Zend_Controller_Response_Exception
  283. */
  284. public function canSendHeaders($throw = false)
  285. {
  286. $ok = headers_sent($file, $line);
  287. if ($ok && $throw && $this->headersSentThrowsException) {
  288. require_once 'Zend/Controller/Response/Exception.php';
  289. throw new Zend_Controller_Response_Exception('Cannot send headers; headers already sent in ' . $file . ', line ' . $line);
  290. }
  291. return !$ok;
  292. }
  293. /**
  294. * Send all headers
  295. *
  296. * Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code}
  297. * has been specified, it is sent with the first header.
  298. *
  299. * @return Zend_Controller_Response_Abstract
  300. */
  301. public function sendHeaders()
  302. {
  303. // Only check if we can send headers if we have headers to send
  304. if (count($this->_headersRaw) || count($this->_headers) || (200 != $this->_httpResponseCode)) {
  305. $this->canSendHeaders(true);
  306. } elseif (200 == $this->_httpResponseCode) {
  307. // Haven't changed the response code, and we have no headers
  308. return $this;
  309. }
  310. $httpCodeSent = false;
  311. foreach ($this->_headersRaw as $header) {
  312. if (!$httpCodeSent && $this->_httpResponseCode) {
  313. header($header, true, $this->_httpResponseCode);
  314. $httpCodeSent = true;
  315. } else {
  316. header($header);
  317. }
  318. }
  319. foreach ($this->_headers as $header) {
  320. if (!$httpCodeSent && $this->_httpResponseCode) {
  321. header($header['name'] . ': ' . $header['value'], $header['replace'], $this->_httpResponseCode);
  322. $httpCodeSent = true;
  323. } else {
  324. header($header['name'] . ': ' . $header['value'], $header['replace']);
  325. }
  326. }
  327. if (!$httpCodeSent) {
  328. header('HTTP/1.1 ' . $this->_httpResponseCode);
  329. $httpCodeSent = true;
  330. }
  331. return $this;
  332. }
  333. /**
  334. * Set body content
  335. *
  336. * If $name is not passed, or is not a string, resets the entire body and
  337. * sets the 'default' key to $content.
  338. *
  339. * If $name is a string, sets the named segment in the body array to
  340. * $content.
  341. *
  342. * @param string $content
  343. * @param null|string $name
  344. * @return Zend_Controller_Response_Abstract
  345. */
  346. public function setBody($content, $name = null)
  347. {
  348. if ((null === $name) || !is_string($name)) {
  349. $this->_body = array('default' => (string) $content);
  350. } else {
  351. $this->_body[$name] = (string) $content;
  352. }
  353. return $this;
  354. }
  355. /**
  356. * Append content to the body content
  357. *
  358. * @param string $content
  359. * @param null|string $name
  360. * @return Zend_Controller_Response_Abstract
  361. */
  362. public function appendBody($content, $name = null)
  363. {
  364. if ((null === $name) || !is_string($name)) {
  365. if (isset($this->_body['default'])) {
  366. $this->_body['default'] .= (string) $content;
  367. } else {
  368. return $this->append('default', $content);
  369. }
  370. } elseif (isset($this->_body[$name])) {
  371. $this->_body[$name] .= (string) $content;
  372. } else {
  373. return $this->append($name, $content);
  374. }
  375. return $this;
  376. }
  377. /**
  378. * Clear body array
  379. *
  380. * With no arguments, clears the entire body array. Given a $name, clears
  381. * just that named segment; if no segment matching $name exists, returns
  382. * false to indicate an error.
  383. *
  384. * @param string $name Named segment to clear
  385. * @return boolean
  386. */
  387. public function clearBody($name = null)
  388. {
  389. if (null !== $name) {
  390. $name = (string) $name;
  391. if (isset($this->_body[$name])) {
  392. unset($this->_body[$name]);
  393. return true;
  394. }
  395. return false;
  396. }
  397. $this->_body = array();
  398. return true;
  399. }
  400. /**
  401. * Return the body content
  402. *
  403. * If $spec is false, returns the concatenated values of the body content
  404. * array. If $spec is boolean true, returns the body content array. If
  405. * $spec is a string and matches a named segment, returns the contents of
  406. * that segment; otherwise, returns null.
  407. *
  408. * @param boolean $spec
  409. * @return string|array|null
  410. */
  411. public function getBody($spec = false)
  412. {
  413. if (false === $spec) {
  414. ob_start();
  415. $this->outputBody();
  416. return ob_get_clean();
  417. } elseif (true === $spec) {
  418. return $this->_body;
  419. } elseif (is_string($spec) && isset($this->_body[$spec])) {
  420. return $this->_body[$spec];
  421. }
  422. return null;
  423. }
  424. /**
  425. * Append a named body segment to the body content array
  426. *
  427. * If segment already exists, replaces with $content and places at end of
  428. * array.
  429. *
  430. * @param string $name
  431. * @param string $content
  432. * @return Zend_Controller_Response_Abstract
  433. */
  434. public function append($name, $content)
  435. {
  436. if (!is_string($name)) {
  437. require_once 'Zend/Controller/Response/Exception.php';
  438. throw new Zend_Controller_Response_Exception('Invalid body segment key ("' . gettype($name) . '")');
  439. }
  440. if (isset($this->_body[$name])) {
  441. unset($this->_body[$name]);
  442. }
  443. $this->_body[$name] = (string) $content;
  444. return $this;
  445. }
  446. /**
  447. * Prepend a named body segment to the body content array
  448. *
  449. * If segment already exists, replaces with $content and places at top of
  450. * array.
  451. *
  452. * @param string $name
  453. * @param string $content
  454. * @return void
  455. */
  456. public function prepend($name, $content)
  457. {
  458. if (!is_string($name)) {
  459. require_once 'Zend/Controller/Response/Exception.php';
  460. throw new Zend_Controller_Response_Exception('Invalid body segment key ("' . gettype($name) . '")');
  461. }
  462. if (isset($this->_body[$name])) {
  463. unset($this->_body[$name]);
  464. }
  465. $new = array($name => (string) $content);
  466. $this->_body = $new + $this->_body;
  467. return $this;
  468. }
  469. /**
  470. * Insert a named segment into the body content array
  471. *
  472. * @param string $name
  473. * @param string $content
  474. * @param string $parent
  475. * @param boolean $before Whether to insert the new segment before or
  476. * after the parent. Defaults to false (after)
  477. * @return Zend_Controller_Response_Abstract
  478. */
  479. public function insert($name, $content, $parent = null, $before = false)
  480. {
  481. if (!is_string($name)) {
  482. require_once 'Zend/Controller/Response/Exception.php';
  483. throw new Zend_Controller_Response_Exception('Invalid body segment key ("' . gettype($name) . '")');
  484. }
  485. if ((null !== $parent) && !is_string($parent)) {
  486. require_once 'Zend/Controller/Response/Exception.php';
  487. throw new Zend_Controller_Response_Exception('Invalid body segment parent key ("' . gettype($parent) . '")');
  488. }
  489. if (isset($this->_body[$name])) {
  490. unset($this->_body[$name]);
  491. }
  492. if ((null === $parent) || !isset($this->_body[$parent])) {
  493. return $this->append($name, $content);
  494. }
  495. $ins = array($name => (string) $content);
  496. $keys = array_keys($this->_body);
  497. $loc = array_search($parent, $keys);
  498. if (!$before) {
  499. // Increment location if not inserting before
  500. ++$loc;
  501. }
  502. if (0 === $loc) {
  503. // If location of key is 0, we're prepending
  504. $this->_body = $ins + $this->_body;
  505. } elseif ($loc >= (count($this->_body))) {
  506. // If location of key is maximal, we're appending
  507. $this->_body = $this->_body + $ins;
  508. } else {
  509. // Otherwise, insert at location specified
  510. $pre = array_slice($this->_body, 0, $loc);
  511. $post = array_slice($this->_body, $loc);
  512. $this->_body = $pre + $ins + $post;
  513. }
  514. return $this;
  515. }
  516. /**
  517. * Echo the body segments
  518. *
  519. * @return void
  520. */
  521. public function outputBody()
  522. {
  523. $body = implode('', $this->_body);
  524. echo $body;
  525. }
  526. /**
  527. * Register an exception with the response
  528. *
  529. * @param Exception $e
  530. * @return Zend_Controller_Response_Abstract
  531. */
  532. public function setException(Exception $e)
  533. {
  534. $this->_exceptions[] = $e;
  535. return $this;
  536. }
  537. /**
  538. * Retrieve the exception stack
  539. *
  540. * @return array
  541. */
  542. public function getException()
  543. {
  544. return $this->_exceptions;
  545. }
  546. /**
  547. * Has an exception been registered with the response?
  548. *
  549. * @return boolean
  550. */
  551. public function isException()
  552. {
  553. return !empty($this->_exceptions);
  554. }
  555. /**
  556. * Does the response object contain an exception of a given type?
  557. *
  558. * @param string $type
  559. * @return boolean
  560. */
  561. public function hasExceptionOfType($type)
  562. {
  563. foreach ($this->_exceptions as $e) {
  564. if ($e instanceof $type) {
  565. return true;
  566. }
  567. }
  568. return false;
  569. }
  570. /**
  571. * Does the response object contain an exception with a given message?
  572. *
  573. * @param string $message
  574. * @return boolean
  575. */
  576. public function hasExceptionOfMessage($message)
  577. {
  578. foreach ($this->_exceptions as $e) {
  579. if ($message == $e->getMessage()) {
  580. return true;
  581. }
  582. }
  583. return false;
  584. }
  585. /**
  586. * Does the response object contain an exception with a given code?
  587. *
  588. * @param int $code
  589. * @return boolean
  590. */
  591. public function hasExceptionOfCode($code)
  592. {
  593. $code = (int) $code;
  594. foreach ($this->_exceptions as $e) {
  595. if ($code == $e->getCode()) {
  596. return true;
  597. }
  598. }
  599. return false;
  600. }
  601. /**
  602. * Retrieve all exceptions of a given type
  603. *
  604. * @param string $type
  605. * @return false|array
  606. */
  607. public function getExceptionByType($type)
  608. {
  609. $exceptions = array();
  610. foreach ($this->_exceptions as $e) {
  611. if ($e instanceof $type) {
  612. $exceptions[] = $e;
  613. }
  614. }
  615. if (empty($exceptions)) {
  616. $exceptions = false;
  617. }
  618. return $exceptions;
  619. }
  620. /**
  621. * Retrieve all exceptions of a given message
  622. *
  623. * @param string $message
  624. * @return false|array
  625. */
  626. public function getExceptionByMessage($message)
  627. {
  628. $exceptions = array();
  629. foreach ($this->_exceptions as $e) {
  630. if ($message == $e->getMessage()) {
  631. $exceptions[] = $e;
  632. }
  633. }
  634. if (empty($exceptions)) {
  635. $exceptions = false;
  636. }
  637. return $exceptions;
  638. }
  639. /**
  640. * Retrieve all exceptions of a given code
  641. *
  642. * @param mixed $code
  643. * @return void
  644. */
  645. public function getExceptionByCode($code)
  646. {
  647. $code = (int) $code;
  648. $exceptions = array();
  649. foreach ($this->_exceptions as $e) {
  650. if ($code == $e->getCode()) {
  651. $exceptions[] = $e;
  652. }
  653. }
  654. if (empty($exceptions)) {
  655. $exceptions = false;
  656. }
  657. return $exceptions;
  658. }
  659. /**
  660. * Whether or not to render exceptions (off by default)
  661. *
  662. * If called with no arguments or a null argument, returns the value of the
  663. * flag; otherwise, sets it and returns the current value.
  664. *
  665. * @param boolean $flag Optional
  666. * @return boolean
  667. */
  668. public function renderExceptions($flag = null)
  669. {
  670. if (null !== $flag) {
  671. $this->_renderExceptions = $flag ? true : false;
  672. }
  673. return $this->_renderExceptions;
  674. }
  675. /**
  676. * Send the response, including all headers, rendering exceptions if so
  677. * requested.
  678. *
  679. * @return void
  680. */
  681. public function sendResponse()
  682. {
  683. $this->sendHeaders();
  684. if ($this->isException() && $this->renderExceptions()) {
  685. $exceptions = '';
  686. foreach ($this->getException() as $e) {
  687. $exceptions .= $e->__toString() . "\n";
  688. }
  689. echo $exceptions;
  690. return;
  691. }
  692. $this->outputBody();
  693. }
  694. /**
  695. * Magic __toString functionality
  696. *
  697. * Proxies to {@link sendResponse()} and returns response value as string
  698. * using output buffering.
  699. *
  700. * @return string
  701. */
  702. public function __toString()
  703. {
  704. ob_start();
  705. $this->sendResponse();
  706. return ob_get_clean();
  707. }
  708. }