Forum

Welcome to ProjeQtOr new Forum. We migrated old forum to the new website.
You will find all your posts here, with your usual account.
Just one point : you’ll have to reinitialize your password. Use “Lost password” feature.

Request handling
 
Notifications
Clear all

Request handling

9 Posts
2 Users
0 Reactions
8,054 Views
(@antonio-grr)
Posts: 84
Active Member
Topic starter
 
[#4131]

Hi.
In order to get a cleaner code and to reduce NetBeans warning I introduced the singleton in attachment.
Using it I changed code (e.g.)
$mode="";
$testCaseRunId=null;
if ( array_key_exists('testCaseRunId',$_REQUEST) ) {
$testCaseRunId=$_REQUEST['testCaseRunId'];
Security::checkValidId($testCaseRunId);
$mode='edit';
} else {
$mode='add';
}
$testSessionId=0;
if ( array_key_exists('testSessionId',$_REQUEST)) {
$testSessionId=$_REQUEST['testSessionId'];
}

Security::checkValidId($testSessionId);

in
$request = RequestHdl::istanza();
$testCaseRunId = $request->get_validated_id('testCaseRunId', null);
if ($testCaseRunId) {
$mode = 'edit';
} else {
$mode = 'add';
}
$testSessionId = $request->get_validated_id('testSessionId', 0);


 
Posted : 22 Feb 2017 13H33
(@babynus)
Posts: 14952
Member Admin
 

Very Good idea.
Ticket #2582 recorded

(notice that community version will be soewhat different from yours, for instance because we use camelCase for naming conventions)


 
Posted : 27 Feb 2017 22H16
(@babynus)
Posts: 14952
Member Admin
 

For information, we started implementing Request Handler, but not as singleton : it is an abstract class will all static functions.
We also use camelCase naming.
We start using is for new needs and will try and find time to replace all direct call to $_REQUEST with call to functions of this class.
Thnaks for the idea.

Here is first version :

abstract class RequestHandler {

  public static function getValue($code,$required=false,$default=null) {
    if (isset($_REQUEST[$code])) {
      return $_REQUEST[$code];
    } else {
      if ($required) {
        throwError("parameter '$code' not found in Request");
        exit;
      } else {
        return $default;
      }  
    }
  }
  
  public static function getClass($code,$required=false,$default=null) {
    $val=self::getValue($code,$required,$default);
    if ($val==$default) return $val;
    return Security::checkValidClass($val);
  }
  
  public static function getId($code,$required=false,$default=null) {
    $val=self::getValue($code,$required,$default);
    if ($val==$default) return $val;
    return Security::checkValidId($val);
  }
  
  public static function getNumeric($code,$required=false,$default=null) {
    $val=self::getValue($code,$required,$default);
    if ($val==$default) return $val;
    return Security::checkValidNumeric($val);
  }
  
  public static function getAlphanumeric($code,$required=false,$default=null) {
    $val=self::getValue($code,$required,$default);
    if ($val==$default) return $val;
    return Security::checkValidAlphanumeric($val);
  }
  
  public static function getDatetime($code,$required=false,$default=null) {
    $val=self::getValue($code,$required,$default);
    if ($val==$default) return $val;
    return Security::checkValidDateTime($val);
  }
  
  public static function getYear($code,$required=false,$default=null) {
    $val=self::getValue($code,$required,$default);
    if ($val==$default) return $val;
    return Security::checkValidYear($val);
  }
  
  public static function getMonth($code,$required=false,$default=null) {
    $val=self::getValue($code,$required,$default);
    if ($val==$default) return $val;
    return Security::checkValidMonth($val);
  }
  public static function getExpected($code,$expectedList) {
    $val=self::getValue($code,true,null);
    if (in_array($val, $expectedList)) {
      return $val;
    } else {
      throwError("parameter $code='$val' has an unexpected value");
      exit;
    }
  }
  
}

 
Posted : 18 Mar 2017 22H26
(@antonio-grr)
Posts: 84
Active Member
Topic starter
 

Ok. I prefer singleton, but the result is quite the same.
At the moment I have used RequestHandler in a large amount of modules; if you believe that this can help you, I can send my code to you.
Best regards.


 
Posted : 20 Mar 2017 10H23
(@babynus)
Posts: 14952
Member Admin
 

Hi,
Thanks for your proposale.
Yes, please snd your code, I'll have a look at least and check if I can use it (or part of it).


 
Posted : 22 Mar 2017 10H39
(@antonio-grr)
Posts: 84
Active Member
Topic starter
 

Ok,
in attachment a version of getSingleData.php based on RequestHandler class.
I had to add a method to the class
public static function isRequestSet($code) {
return isset($_REQUEST[$code]);
}

Let me know if this kind of cooperation can be useful. If so, I'll send new modules time by time.
Best regards


 
Posted : 22 Mar 2017 13H19
(@babynus)
Posts: 14952
Member Admin
 

I took your work into account.
Thanks !


 
Posted : 25 Mar 2017 18H30
(@antonio-grr)
Posts: 84
Active Member
Topic starter
 

I notice now that you used a different signature in RequestHandler in order to distinguish between optional and mandatory parameters in Request.
I used $default = 'none' so missing argument can be easily detected to assume that parameter is mandatory.
If you really prefer your approach, you should use $required=true as it refers to the condition of accessing $_REQUEST element with no previous isset test.
This is, for example, what happened in original getSingleElement.php code; using my version and $required=false, exception will be avoided, but I'm not sure that you really want this.
Let me know before I proceed supplying you other code.
Best Regards


 
Posted : 27 Mar 2017 11H06
(@babynus)
Posts: 14952
Member Admin
 

I used $default = 'none' so missing argument can be easily detected to assume that parameter is mandatory.

I prefer the explicit $required parameter, because 'none' is a string and could be a possible default value.

If you really prefer your approach, you should use $required=true as it refers to the condition of accessing $_REQUEST element with no previous isset test.

You're right. I prefered the default to false because it would not raise "Hack Detected" when not necessary. But in the other hand it will not when nedded...
I think best is to set signature as
getValue($code,$required,$default=null)
with no default value for $required.
This way, we'll have to think about need for a required Request attribute or not.
I'll keep existing signature until sure all calls have been upgraded...


 
Posted : 27 Mar 2017 16H49
Share:

Scroll to Top