Action.class.php (12808B)
1 <?php 2 3 namespace { 4 5 class ObjectNotFoundException extends Exception 6 { 7 } 8 9 10 define('OR_NOTICE_OK', 'ok'); 11 define('OR_NOTICE_INFO', 'info'); 12 define('OR_NOTICE_WARN', 'warning'); 13 define('OR_NOTICE_ERROR', 'error'); 14 15 } 16 17 18 namespace cms\action { 19 20 use cms\model\User; 21 use \Html; 22 use \Session; 23 use \Logger; 24 use \Http; 25 use \Text; 26 27 28 /** 29 * Eltern-Klasse fuer alle Actions. 30 * 31 * Diese Klasse stellt grundlegende action-uebergreifende Methoden 32 * bereit. 33 * Dient als Ueberklasse fuer alle abgeleiteten Action-Klassen in 34 * diesem Package bzw. Verzeichnis. 35 * 36 * @author Jan Dankert 37 * @package openrat.actions 38 * @abstract 39 */ 40 class Action 41 { 42 const SECURITY_GUEST = 1; // Jeder (auch nicht angemeldete) dürfen diese Aktion ausführen 43 const SECURITY_USER = 2; // Angemeldete Benutzer dürfen diese Aktion ausführen 44 const SECURITY_ADMIN = 3; // Nur Administratoren dürfen diese Aktion ausführen 45 46 public $security = self::SECURITY_USER; // Default. 47 48 protected $templateVars = array( 'output'=>array() ); 49 50 /** 51 * Aktuell angemeldeter Benutzer.<br> 52 * Wird im Konstruktor gesetzt. 53 * 54 * @var Object Benutzer 55 */ 56 var $currentUser; 57 58 /** 59 * @var RequestParams 60 */ 61 public $request; 62 63 64 /** 65 * Will be called by the Dispatcher right after the contruction of this class instance. 66 */ 67 public function init() 68 { 69 70 } 71 72 73 protected function setStyle($style) 74 { 75 $this->setControlVar("new_style", $style); 76 } 77 78 79 public function __construct() 80 { 81 //$this->request = new RequestParams(); 82 83 $this->currentUser = Session::getUser(); 84 85 $this->templateVars['errors'] = array(); 86 $this->templateVars['notices'] = array(); 87 $this->templateVars['control'] = array(); 88 $this->templateVars['output'] = array(); 89 } 90 91 /** 92 * Liest eine Session-Variable 93 * 94 * @param String $varName Schl�ssel 95 * @return mixed 96 */ 97 protected function getSessionVar($varName) 98 { 99 global $SESS; 100 101 if (!isset($SESS[$varName])) 102 return ''; 103 else return $SESS[$varName]; 104 } 105 106 107 /** 108 * Setzt eine Session-Variable 109 * 110 * @param string $varName Schluessel 111 * @param mixed $value Inhalt 112 * @return mixed 113 */ 114 protected function setSessionVar($varName, $value) 115 { 116 global $SESS; 117 118 $SESS[$varName] = $value; 119 } 120 121 122 /** 123 * Ermittelt den Inhalt der gew�nschten Request-Variablen. 124 * Falls nicht vorhanden, wird "" zur�ckgegeben. 125 * 126 * @param String $varName Schl�ssel 127 * @return String Inhalt 128 */ 129 protected function getRequestVar($varName, $transcode = OR_FILTER_TEXT) 130 { 131 return $this->request->getRequestVar($varName,$transcode); 132 } 133 134 135 /** 136 * Ermittelt, ob der aktuelle Request eine Variable mit dem 137 * angegebenen Namen enth�lt. 138 * 139 * @param String $varName Schl�ssel 140 * @return boolean true, falls vorhanden. 141 */ 142 protected function hasRequestVar($varName) 143 { 144 return $this->request->hasRequestVar($varName); 145 } 146 147 148 /** 149 * Ermittelt die aktuelle Id aus dem Request.<br> 150 * Um welche ID es sich handelt, ist abh�ngig von der Action. 151 * 152 * @return Integer 153 */ 154 protected function getRequestId( $name = null ) 155 { 156 if ( is_null($name) ) 157 return $this->request->getRequestId(); 158 else 159 return intval($this->request->getRequestVar($name,OR_FILTER_NUMBER)); 160 } 161 162 163 /** 164 * Setzt eine Variable f�r die Oberfl�che. 165 * 166 * @param String $varName Schl�ssel 167 * @param Mixed $value 168 */ 169 protected function setTemplateVar($varName, $value) 170 { 171 $this->templateVars['output'][$varName] = $value; 172 } 173 174 175 /** 176 * Setzt eine Variable f�r die Oberfl�che. 177 * 178 * @param String $varName Schl�ssel 179 * @param Mixed $value 180 * @deprecated Diese Schicht soll keine Dialog-Logik enthalten. 181 */ 182 protected function setControlVar($varName, $value) 183 { 184 $this->templateVars['control'][$varName] = $value; 185 } 186 187 188 /** 189 * Setzt eine Liste von Variablen f�r die Oberfl�che. 190 * 191 * @param array $varList Assoziatives Array 192 */ 193 protected function setTemplateVars($varList) 194 { 195 foreach ($varList as $name => $value) { 196 $this->setTemplateVar($name, $value); 197 } 198 } 199 200 201 /** 202 * F�gt einen Validierungsfehler hinzu. 203 * 204 * @param String $name Name des validierten Eingabefeldes 205 * @param String Textschl�ssel der Fehlermeldung (optional) 206 */ 207 public function addValidationError($name, $message = "COMMON_VALIDATION_ERROR", $vars = array(), $log = array()) 208 { 209 if (!empty($message)) 210 $this->addNotice('', '', $message, OR_NOTICE_ERROR, $vars, $log); 211 212 $this->templateVars['errors'][] = $name; 213 } 214 215 216 public function handleResult($result) 217 { 218 // TODO - 219 } 220 221 /** 222 * F�gt ein Meldung hinzu. 223 * 224 * @param String $type Typ des Objektes, zu dem diese Meldung geh�rt. 225 * @param String $name Name des Objektes, zu dem diese Meldung geh�rt. 226 * @param String $text Textschl�ssel der Fehlermeldung (optional) 227 * @param String $status Einer der Werte OR_NOTICE_(OK|WARN|ERROR) 228 * @param array $vars Variablen f�r den Textschl�ssel 229 * @param array $log Weitere Hinweistexte f�r diese Meldung. 230 */ 231 protected function addNotice($type, $name, $text, $status = OR_NOTICE_OK, $vars = array(), $log = array()) 232 { 233 if ($status === true) 234 $status = OR_NOTICE_OK; 235 elseif ($status === false) 236 $status = OR_NOTICE_ERROR; 237 238 $this->templateVars['notice_status'] = $status; 239 $this->templateVars['status'] = $status; 240 $this->templateVars['success'] = ($status == OR_NOTICE_ERROR ? 'false' : 'true'); 241 242 if ($status == OR_NOTICE_OK && isset($_COOKIE['or_ignore_ok_notices'])) 243 return; 244 245 if (!is_array($log)) 246 $log = array($log); 247 248 if (!is_array($vars)) 249 $vars = array($vars); 250 251 $this->templateVars['notices'][] = array('type' => $type, 252 'name' => $name, 253 'key' => 'NOTICE_' . $text, 254 'vars' => $vars, 255 'text' => lang('NOTICE_' . $text, $vars), 256 'log' => $log, 257 'status' => $status); 258 } 259 260 261 public function getOutputData() 262 { 263 return $this->templateVars; 264 } 265 266 /** 267 * Ruft eine weitere Subaction auf. 268 * 269 * @param String $subActionName Name der n�chsten Subaction. Es muss eine Methode mit diesem Namen geben. 270 * @deprecated 271 */ 272 protected function callSubAction($subActionName) 273 { 274 return; 275 } 276 277 278 /** 279 * Calling another action method. 280 * 281 * @param String $method Name of next method to call. 282 */ 283 protected function nextSubAction($method) 284 { 285 Logger::trace("next subaction is '$method'"); 286 287 $this->request->method = $method; 288 289 $methodName = $method . ($_SERVER['REQUEST_METHOD'] == 'POST' ? 'Post' : 'View'); 290 $this->$methodName(); 291 } 292 293 294 /** 295 * Ermitteln, ob Benutzer Administratorrechte besitzt 296 * @return Boolean TRUE, falls der Benutzer ein Administrator ist. 297 */ 298 protected function userIsAdmin() 299 { 300 $user = $this->getUserFromSession(); 301 return is_object($user) && $user->isAdmin; 302 } 303 304 305 /** 306 * Ermitteln des Benutzerobjektes aus der Session 307 * @return User 308 */ 309 protected function getUserFromSession() 310 { 311 return Session::getUser(); 312 } 313 314 315 /** 316 * Benutzen eines sog. "Conditional GET". 317 * 318 * Diese Funktion setzt einen "Last-Modified"-HTTP-Header. 319 * Ist der Inhalt der Seite nicht neuer, so wird der Inhalt 320 * der Seite nicht ausgegeben, sondern nur HTTP-Status 304 321 * ("304 not modified") gesetzt. 322 * Der Rest der Seite muss dann nicht mehr erzeugt werden, 323 * wodurch die Performance stark erhoeht werden kann. 324 * 325 * Credits: Danke an Charles Miller 326 * @see http://fishbowl.pastiche.org/2002/10/21/http_conditional_get_for_rss_hackers 327 * 328 * Gefunden auf: 329 * @see http://simon.incutio.com/archive/2003/04/23/conditionalGet 330 * 331 * @param $time int Letztes Aenderungsdatum des Objektes 332 * @param $expirationDuration int Gültigkeitsdauer 333 */ 334 protected function lastModified($time, $expirationDuration = 0) 335 { 336 if ( DEVELOPMENT ) 337 return; 338 339 // Conditional-Get eingeschaltet? 340 if (!config('cache', 'conditional_get')) 341 return; 342 343 $expires = substr(date('r', time() + $expirationDuration - date('Z')), 0, -5) . 'GMT'; 344 $lastModified = substr(date('r', $time - date('Z')), 0, -5) . 'GMT'; 345 $etag = '"' . base_convert($time, 10, 36) . '"'; 346 347 // Header senden 348 header('Expires: ' . $expires); 349 header('Last-Modified: ' . $lastModified); 350 header('ETag: ' . $etag); 351 352 // Die vom Interpreter sonst automatisch gesetzten 353 // Header uebersteuern 354 header('Cache-Control: must-revalidate'); 355 header('Pragma:'); 356 357 // See if the client has provided the required headers 358 $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false; 359 $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false; 360 361 // Bug in Apache 2.2, mod_deflat adds '-gzip' to E-Tag 362 if (substr($if_none_match, -6) == '-gzip"') 363 $if_none_match = substr($if_none_match, 0, -6) . '"'; 364 365 // At least one of the headers is there - check them 366 if ($if_none_match && $if_none_match != $etag) 367 return; // etag is there but doesn't match 368 369 if ($if_modified_since && $if_modified_since != $lastModified) 370 return; // if-modified-since is there but doesn't match 371 372 if (!$if_modified_since && !$if_none_match) 373 return; 374 375 // Der entfernte Browser bzw. Proxy holt die Seite nun aus seinem Cache 376 header('HTTP/1.0 304 Not Modified'); 377 exit; // Sofortiges Skript-Ende 378 } 379 380 381 /** 382 * @param $max int max Anzahl der Sekunden, die die Seite im Browsercache bleiben darf 383 */ 384 protected function maxAge($max = 3600) 385 { 386 // Die Header "Last-Modified" und "ETag" wurden bereits in der 387 // Methode "lastModified()" gesetzt. 388 389 header('Expires: ' . substr(date('r', time() - date('Z') + $max), 0, -5) . 'GMT'); 390 header('Pragma: '); // 'Pragma' ist Bullshit und 391 // wird von den meisten Browsern ignoriert. 392 header('Cache-Control: public, max-age=' . $max . ", s-maxage=" . $max); 393 } 394 395 396 /** 397 * Erzeugt einen Redirect auf einen bestimmte URL. 398 */ 399 protected function redirect($url) 400 { 401 $this->setControlVar('redirect', $url); 402 } 403 404 405 protected function setCookie($name,$value='' ) { 406 407 if (empty($value)) 408 $expire = time(); // Cookie wird gelöscht. 409 else 410 $expire = time() + 60 * 60 * 24 * config('security', 'cookie', 'expire'); 411 412 $secure = config('security', 'cookie', 'secure'); 413 $httponly = config('security', 'cookie', 'httponly'); 414 setcookie($name , $value, $expire,COOKIE_PATH, '', $secure, $httponly); 415 } 416 } 417 418 }