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