openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

RequestParams.class.php (5586B)


      1 <?php
      2 
      3 namespace {
      4 
      5     /* Request Parameter Names */
      6     /* @deprecated  */
      7     define('REQ_PARAM_EMBED'          ,'embed'          );
      8 
      9     // TODO: Change the following constants to class constants
     10     define('REQ_PARAM_TOKEN'          ,'token'          );
     11     define('REQ_PARAM_ACTION'         ,'action'         );
     12     define('REQ_PARAM_SUBACTION'      ,'subaction'      );
     13     define('REQ_PARAM_ID'             ,'id'             );
     14     define('REQ_PARAM_OBJECT_ID'      ,'objectid'       );
     15     define('REQ_PARAM_LANGUAGE_ID'    ,'languageid'     );
     16     define('REQ_PARAM_MODEL_ID'       ,'modelid'        );
     17     define('REQ_PARAM_PROJECT_ID'     ,'projectid'      );
     18     define('REQ_PARAM_ELEMENT_ID'     ,'elementid'      );
     19     define('REQ_PARAM_TEMPLATE_ID'    ,'templateid'     );
     20     define('REQ_PARAM_DATABASE_ID'    ,'dbid'           );
     21 
     22     /* Filter Types */
     23     define('OR_FILTER_ALPHA', 'abc');
     24     define('OR_FILTER_ALPHANUM', 'abc123');
     25     define('OR_FILTER_FILENAME', 'file');
     26     define('OR_FILTER_MAIL', 'mail');
     27     define('OR_FILTER_TEXT', 'text');
     28     define('OR_FILTER_NUMBER', '123');
     29     define('OR_FILTER_RAW', 'raw');
     30 }
     31 
     32 
     33 namespace cms\action {
     34 
     35     use Text;
     36 
     37     class RequestParams
     38     {
     39         public $action;
     40         public $method;
     41         public $id;
     42 
     43         public $isAction;
     44 
     45         /**
     46          * RequestParams constructor.
     47          */
     48         public function __construct()
     49         {
     50             $this->id         = @$_REQUEST[REQ_PARAM_ID       ];
     51             $this->action     = @$_REQUEST[REQ_PARAM_ACTION   ];
     52             $this->method     = @$_REQUEST[REQ_PARAM_SUBACTION];
     53 
     54             // Is this a POST request?
     55             $this->isAction = @$_SERVER['REQUEST_METHOD'] == 'POST';
     56         }
     57 
     58         /**
     59          * Ermittelt den Inhalt der gew�nschten Request-Variablen.
     60          * Falls nicht vorhanden, wird "" zur�ckgegeben.
     61          *
     62          * @param String $varName Schl�ssel
     63          * @return String Inhalt
     64          */
     65         public function getRequestVar($varName, $transcode = OR_FILTER_TEXT)
     66         {
     67             if($varName == REQ_PARAM_ID)
     68                 return $this->id;
     69 
     70             if($varName == REQ_PARAM_ACTION)
     71                 return $this->action;
     72 
     73             if($varName == REQ_PARAM_SUBACTION)
     74                 return $this->method;
     75 
     76             global $REQ;
     77 
     78             if (!isset($REQ[$varName]))
     79                 return '';
     80 
     81 
     82             switch ($transcode) {
     83                 case OR_FILTER_ALPHA:
     84                     $white = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
     85                     break;
     86 
     87                 case OR_FILTER_ALPHANUM:
     88                     $white = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,_-!?%&/()';
     89                     break;
     90 
     91                 case OR_FILTER_FILENAME:
     92                     // RFC 1738, Section 2.2:
     93                     // Thus, only alphanumerics, the special characters "$-_.+!*'(),", and
     94                     // reserved characters used for their reserved purposes may be used
     95                     // unencoded within a URL.
     96                     $white = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$-_.+!*(),' . "'";
     97                     break;
     98 
     99                 case OR_FILTER_MAIL:
    100                     $white = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-@';
    101                     break;
    102 
    103                 case OR_FILTER_TEXT:
    104                 	// Allow all UTF-8 characters.
    105                 	return mb_convert_encoding($REQ[$varName], 'UTF-8', 'UTF-8');
    106 
    107                 case OR_FILTER_NUMBER:
    108                     $white = '1234567890.';
    109                     break;
    110 
    111                 case OR_FILTER_RAW:
    112                     return $REQ[$varName];
    113 
    114                 default:
    115                     throw new \LogicException('Unknown request filter', 'not found: ' . $transcode);
    116             }
    117 
    118             $value = $REQ[$varName];
    119             $newValue = Text::clean($value, $white);
    120 
    121             return $newValue;
    122         }
    123 
    124 
    125         /**
    126          * Ermittelt, ob der aktuelle Request eine Variable mit dem
    127          * angegebenen Namen enth�lt.
    128          *
    129          * @param String $varName Schl�ssel
    130          * @return boolean true, falls vorhanden.
    131          */
    132         public function hasRequestVar($varName)
    133         {
    134             global $REQ;
    135 
    136             return (isset($REQ[$varName]) && (!empty($REQ[$varName]) || $REQ[$varName] == '0'));
    137         }
    138 
    139 
    140         /**
    141          * Ermittelt die aktuelle Id aus dem Request.<br>
    142          * Um welche ID es sich handelt, ist abh�ngig von der Action.
    143          *
    144          * @return Integer
    145          */
    146         public function getRequestId()
    147         {
    148             if ($this->hasRequestVar('idvar'))
    149                 return intval($this->getRequestVar($this->getRequestVar('idvar')));
    150             else
    151                 return intval($this->getRequestVar(REQ_PARAM_ID));
    152         }
    153 
    154 
    155         public function hasLanguageId()
    156         {
    157             return $this->hasRequestVar(REQ_PARAM_LANGUAGE_ID);
    158         }
    159 
    160         public function getLanguageId()
    161         {
    162             return $this->getRequestVar(REQ_PARAM_LANGUAGE_ID,OR_FILTER_NUMBER);
    163         }
    164 
    165         public function hasModelId()
    166         {
    167             return $this->hasRequestVar(REQ_PARAM_MODEL_ID);
    168         }
    169 
    170         public function getModelId()
    171         {
    172             return $this->getRequestVar(REQ_PARAM_MODEL_ID,OR_FILTER_NUMBER);
    173         }
    174         public function getProjectId()
    175         {
    176             return $this->getRequestVar(REQ_PARAM_PROJECT_ID,OR_FILTER_NUMBER);
    177         }
    178     }
    179 }