File modules/cms/action/Response.class.php
Last commit: Sat Oct 17 01:14:30 2026 +0200 Jan Dankert New: Using a structured class for the API-Response. The output for GET-Requests is now without the "output" layer.
1 <?php 2 3 namespace cms\action; 4 5 use cms\api\Notice; 6 use cms\base\Language as L; 7 use cms\base\Startup; 8 use logger\Logger; 9 use util\Session; 10 11 /** 12 * Response. 13 */ 14 class Response 15 { 16 protected array $header = []; 17 18 protected array $errors = []; 19 protected array $notices = []; 20 protected array $output = []; 21 protected string $status = Action::NOTICE_OK; 22 public bool $success = true; 23 24 public function addHeader( $name, $value ) { 25 $this->header[$name] = $value; 26 } 27 28 29 public function addOutput( $name, $value ) { 30 $this->output[$name] = $value; 31 } 32 public function addOutputList( $list ) { 33 $this->output += $list; 34 } 35 36 37 public function setContentSecurityPolicy( $csp ) { 38 39 // Content Security Policy 40 $this->header['Content-Security-Policy'] = implode(';', array_map( function($value,$key) { 41 return $key.' '.$value; 42 },$csp,array_keys($csp) )); 43 } 44 45 46 /** 47 * Sets the content type. 48 * @param $type string 49 * @param $charset string Default: UTF-8 50 */ 51 public function setContentType( $type, $charset = Startup::CHARSET) { 52 $this->header['Content-Type'] = $type.'; charset=' . $charset; 53 } 54 55 /** 56 * Getting the response data as an array. 57 * 58 * @return array 59 */ 60 public function getOutputData(): array 61 { 62 return $this->output; 63 } 64 65 66 public function __toString() 67 { 68 return print_r( $this->getOutputData(),true); 69 } 70 71 /** 72 * Outputs all HTTP reponse headers. 73 */ 74 public function setHTTPHeader() { 75 foreach( $this->header as $name=>$value ) { 76 header( $name.': '.$value ); 77 } 78 } 79 80 81 82 83 /** 84 * F�gt ein Meldung hinzu. 85 * 86 * @param String $type Typ des Objektes, zu dem diese Meldung geh�rt. 87 * @param $id 88 * @param String $name Name des Objektes, zu dem diese Meldung geh�rt. 89 * @param String $text Textschl�ssel der Fehlermeldung (optional) 90 * @param String $status Einer der Werte Action::NOTICE_(OK|WARN|ERROR) 91 * @param array $vars Variablen f�r den Textschl�ssel 92 * @param string|array $log Weitere Hinweistexte f�r diese Meldung. 93 */ 94 public function addNotice($type, $id, $name, $text, $status = Action::NOTICE_OK, $vars = array(), $log = array()): void 95 { 96 $this->status = ($status == Action::NOTICE_ERROR) ? Action::NOTICE_ERROR : $status; 97 $this->success = $this->success && $status != Action::NOTICE_ERROR; 98 99 if ( is_array($log) ) 100 $log = implode("\n",$log); 101 102 $vars = (array) $vars; 103 104 $this->notices[] = new Notice( 105 $type, 106 intval($id), 107 $name, 108 $text, 109 $vars, 110 L::lang($text, $vars), 111 (array) $log, 112 $status, 113 ); 114 115 } 116 117 118 119 public function addError( $errorField ) { 120 $this->errors[] = $errorField; 121 } 122 123 124 public function hasHeader($string) 125 { 126 return array_key_exists($string , $this->header ); 127 } 128 129 /** 130 * @return string 131 */ 132 public function getStatus(): string 133 { 134 return $this->status; 135 } 136 137 /** 138 * @return array 139 */ 140 public function getNotices(): array 141 { 142 return $this->notices; 143 } 144 145 /** 146 * @return array 147 */ 148 public function getErrors(): array 149 { 150 return $this->errors; 151 } 152 }
Downloadmodules/cms/action/Response.class.php
History Sat, 17 Oct 2026 01:14:30 +0200 Jan Dankert New: Using a structured class for the API-Response. The output for GET-Requests is now without the "output" layer. Sat, 19 Mar 2022 00:09:47 +0100 dankert Refactoring: Outputs are setting their content-type themself. Mon, 14 Feb 2022 00:01:10 +0100 dankert Fix: UI needs output data "_token" and "_id". Sun, 13 Feb 2022 23:35:26 +0100 dankert Refactoring: New class "Response" which stores all output information.