Esempio n. 1
0
 /**
  * Creates a new \Nethgui\Controller\Request object from
  * current HTTP request.
  *
  * @param array $parameters
  * @return \Nethgui\Controller\Request
  */
 private function createRequestModApache()
 {
     if (ini_get("magic_quotes_gpc")) {
         throw new \LogicException("magic_quotes_gpc directive must be disabled!", 1377176328);
     }
     $isMutation = FALSE;
     $postData = array();
     $getData = $_GET;
     $pathInfo = array();
     $locale = '';
     $localeDefault = $this->getClientLocaleDefault();
     // Split PATH_INFO
     if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '/') {
         $pathInfo = array_rest(explode('/', $_SERVER['PATH_INFO']));
         $pathHead = array_head($pathInfo);
         if (!in_array(substr($pathHead, 0, 2), $this->dc['l10n.available_languages'])) {
             throw new Exception\HttpException('Language not found', 404, 1377519247);
         }
         $locale = $pathHead;
         $pathInfo = array_rest($pathInfo);
         foreach ($pathInfo as $pathPart) {
             if ($pathPart === '.' || $pathPart === '..' || $pathPart === '') {
                 throw new Exception\HttpException('Bad Request', 400, 1322217901);
             }
         }
     }
     // Extract the requested output format (xhtml, json...)
     $format = $this->extractTargetFormat($pathInfo);
     // Transform the splitted PATH_INFO into a nested array, where each
     // PATH_INFO part is the key of a nested level:
     $pathInfoMod = array();
     $cur =& $pathInfoMod;
     foreach ($pathInfo as $pathPart) {
         $cur[$pathPart] = array();
         $cur =& $cur[$pathPart];
     }
     // FIXME:
     // Copy root level scalar GET variables into the current module for backward
     // compatibility:
     $cur = array_merge(array_filter($_GET, 'is_string'), $cur);
     if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
         $isMutation = TRUE;
         if (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] == 'application/json; charset=UTF-8') {
             // Decode RAW request
             $postData = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);
             if (is_null($postData)) {
                 throw new \Nethgui\Exception\HttpException('Bad Request', 400, 1322148404);
             }
         } else {
             // Use PHP global:
             $postData = $_POST;
         }
     }
     $dc = $this->dc;
     $R = array_replace_recursive($pathInfoMod, $getData, $postData);
     $request = new \Nethgui\Controller\Request($R);
     $request->setLog($this->dc['Log'])->setAttribute('isMutation', $isMutation)->setAttribute('format', $format)->setAttribute('locale', $locale)->setAttribute('localeDefault', $localeDefault)->setAttribute('userClosure', function () use($dc) {
         return $dc['User'];
     });
     // Append the language code to the url parts:
     $this->urlParts[] = $request->getLocale() . '/';
     return $request;
 }
Esempio n. 2
0
 /**
  * The engine that processes the equation, a step at a time
  * 
  * Recursively loop over the equation and process it one operator pair at a time, following the
  * order of operations
  * 
  * @var Operator[] $steps The list of steps reflecting the order of operations
  * @var string $equation The equation to process
  * @var int $count Unnecessary, but prevents against infinite recursion
  * @return double The calculated result of the equation
  */
 protected static function processStep(array $operators, $equation, $count = 0)
 {
     // Infinite recursion defense (for dev purposes)
     if ($count > 250) {
         die('Too much recursion');
     }
     // Pop off the current operator to work with
     $operator = array_head($operators);
     // If we've run through all the steps, convert the equation into a double and return it
     // TODO add second terminal case where $equation is just a signed number
     if ($operators === []) {
         return (double) $equation;
     } else {
         if (($pieces = $operator->getFirstChunk($equation)) === false) {
             return self::processStep(array_tail($operators), $equation, $count + 1);
         } else {
             // Calculate the return of the matched operator/operands
             $result = call_user_func_array($operator, $pieces['operands']);
             // myLog('Match', print_r($pieces, true), $result);
             // Update the equation, replacing the whole matched binary pair with the result
             $newEquation = str_replace($pieces['chunk'], $result, $equation);
             // Loop again
             return self::processStep($operators, $newEquation, $count + 1);
         }
     }
 }