コード例 #1
0
ファイル: FrontController.php プロジェクト: wilaheng/wila
 public function argv(array $parameters = null, $methodName = null)
 {
     if ($parameters == null) {
         if ($this->conf == null) {
             return;
         }
         $parameters = $this->conf;
     }
     $_map = array();
     $_req = req();
     $_inf = inf();
     $_msg = null;
     try {
         foreach ($parameters as $k => $r) {
             if ($r == null) {
                 continue;
             }
             $_msg = isset($r["msg"]) ? $r["msg"] : null;
             $v = trim($_req->getParameter(isset($r["name"]) ? $r["name"] : $k));
             if ($v == STR_EMPTY) {
                 if (isset($r["req"])) {
                     if (isset($r["constant"])) {
                         $_map[$k] = DB::constant($r["constant"]);
                     }
                     if (isset($r["session"]) && $r["session"] === true) {
                         if (isset($_SESSION[$k])) {
                             $_map[$k] = $_SESSION[$k];
                         }
                     }
                     if ($methodName != null && isset($r["req"][$methodName])) {
                         if ($r["req"][$methodName] && !isset($_map[$k])) {
                             throw new Exception($_inf->requiredException($k));
                         }
                     }
                 }
             } else {
                 if (isset($r["type"])) {
                     switch ($r["type"]) {
                         case "email":
                             if (!ctype_email($v)) {
                                 quit($_inf->requiredException($k));
                             }
                             break;
                         case "digit":
                         case "int":
                             $x = preg_replace("/([^0-9.\\-])/", STR_EMPTY, $v);
                             if (!is_numeric($x)) {
                                 throw new Exception($_inf->digit($k, $v));
                             }
                             $v = $r["type"] == "int" ? intval($x) : $x;
                             break;
                         case "alnum":
                             if (!ctype_alnum($v)) {
                                 throw new Exception($_inf->alnum($k, $v));
                             }
                             break;
                         case "alpha":
                             if (!ctype_alpha($v)) {
                                 throw new Exception($_inf->alpha($k, $v));
                             }
                             break;
                         case "regex":
                             if (!preg_match($r["regex"], $v)) {
                                 throw new Exception($_inf->regexException($k, $r["regex"], $v));
                             }
                             break;
                         case "bool":
                             if ($v !== "true" || $v !== "false") {
                                 throw new Exception($_inf->boolean($k, $v));
                             }
                             $v = $v === "true" ? true : false;
                             break;
                         case "date":
                             if (strlen($v) === 8) {
                                 $v = sprintf("%s-%s-%s", substr($v, 0, 4), substr($v, 4, 2), substr($v, 6));
                             }
                             if (isset($r["format"])) {
                                 $c = array_combine(preg_split("/([\\/\\.-])/", $r["format"], -1), preg_split("/([\\/\\.-])/", $v, -1));
                                 $d = intval($c["d"]);
                                 $m = intval($c["m"]);
                                 $Y = intval($c["Y"]);
                                 if (!checkdate($m, $d, $Y)) {
                                     throw new Exception($_inf->date($k, $v));
                                 }
                                 $v = sprintf("%s-%s-%s", $c["Y"], $c["m"], $c["d"]);
                             }
                             break;
                         case "periode":
                             $j = strlen($v);
                             if ($j > 7) {
                                 throw new Exception($_inf->length($k, $v, $j));
                             }
                             if ($j === 6) {
                                 $v = sprintf("%s-%s", substr($v, 0, 4), substr($v, 4));
                             }
                             break;
                     }
                 }
                 if (isset($r["uppercase"])) {
                     if ($r["uppercase"] === true) {
                         $v = strtoupper($v);
                     }
                 }
                 if (isset($r["lowercase"])) {
                     if ($r["lowercase"] === true) {
                         $v = strtolower($v);
                     }
                 }
                 $x = isset($r["minl"]) ? true : false;
                 $y = isset($r["maxl"]) ? true : false;
                 if ($x || $y) {
                     $j = strlen($v);
                     if ($x) {
                         if ($j < $r["minl"]) {
                             throw new Exception($_inf->length($k, $v, $j));
                         }
                     }
                     if ($y) {
                         if ($j > $r["maxl"]) {
                             throw new Exception($_inf->length($k, $v, $j));
                         }
                     }
                 }
                 $x = isset($r["minv"]) ? true : false;
                 $y = isset($r["maxv"]) ? true : false;
                 if ($x || $y) {
                     if ($x) {
                         if ($v < $r["minv"]) {
                             throw new Exception($_inf->value($k, $v, "<", $r["minv"]));
                         }
                     }
                     if ($y) {
                         if ($v > $r["maxv"]) {
                             throw new Exception($_inf->value($k, $v, ">", $r["maxv"]));
                         }
                     }
                 }
                 $_map[$k] = $v;
             }
         }
     } catch (Exception $e) {
         throw new Exception($_msg == null ? $e->getMessage() : $_msg);
     }
     if (DEBUG_MSG) {
         api_DBG::setMap($_map, $parameters);
     }
     #comment for production use to avoid useless eval
     return $_map;
 }
コード例 #2
0
ファイル: CGI.php プロジェクト: wilaheng/wila
 if ($inc) {
     //
     // require footer (classic approach)
     //
     $output = ob_get_flush();
 }
 if (!$silent && $output !== null) {
     if (is_scalar($output)) {
         if ($chk) {
             $output = $req->confirm() ? confirm_message($output) : alert($output);
         }
     } else {
         $obj = $output instanceof envWrapper ? true : false;
         #ifdef: comment for production use to avoid useless eval
         if (DEBUG_MSG && !isset($info["ignore"])) {
             $map = api_DBG::getMap();
             if ($obj) {
                 $output->debug($map);
             } else {
                 $output["__DBG__"] = $map;
             }
         }
         #endif
         $output = $obj ? $output->toString() : json_encode($output);
         if ($chk) {
             $output = sprintf("<html><head><script>window.onload=function(){window.parent.rs(eval('(%s)'))}</script></head></html>", $output);
         } else {
             header("Content-Type: application/json");
         }
     }
     #ifdef: without buffer hack (to reduce 4kb buffer size) avoid useless task