Ejemplo n.º 1
0
 public function handle(Request $request)
 {
     $this->logger->message("Creating query");
     $query = new Query();
     $this->logger->message("Creating form");
     try {
         $form = (new QueryFormBuilder())->buildForm($query);
     } catch (\Exception $e) {
         throw new \Exception("Cannot build form", 0, $e);
     }
     $this->logger->message("Submitting request to form");
     try {
         $form->submit($request->getQuery()->all());
     } catch (\Exception $e) {
         throw new \Exception("Cannot submit request to form", 0, $e);
     }
     if ($form->isValid()) {
         $this->logger->message("The form is valid");
         $this->logger->message("Creating query handler");
         try {
             $queryHandler = new QueryHandler($this->pdoBuilder->getPDO());
         } catch (\Exception $e) {
             throw new \Exception("Cannot create query handler", 0, $e);
         }
         $queryHandler->setLogger($this->logger);
         $this->logger->message("Executing query");
         $data = $queryHandler->execute($query);
         try {
             return new JsonResponse($data, 200);
         } catch (\Exception $e) {
             throw new \Exception("Cannot create response", 0, $e);
         }
     }
     $this->logger->message("The form is not valid");
     try {
         return new JsonResponse($form->getErrors(), 400);
     } catch (\Exception $e) {
         throw new \Exception("Cannot create response", 0, $e);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param Query $query
  *
  * @return \PDOStatement
  */
 private function prepareSQL(Query $query)
 {
     $subQueries = array();
     foreach ($query->getTables() as $table) {
         $subQueries[] = '(' . $this->buildSubQuery($table, $query) . ')';
     }
     $sql = sprintf('SELECT %s, %s, %s, %s FROM (', self::$_tableCol, self::$_sensorIdCol, self::$_timestampCol, self::$_valuesCol);
     $sql .= implode(' UNION ALL ', $subQueries) . ') t';
     $where = array();
     $timestampColumn = $this->sanitize($query->getTimestampColumn());
     if ($query->getStart() !== null) {
         $where[] = self::$_timestampCol . ' > :start';
     }
     if ($query->getEnd() !== null) {
         $where[] = self::$_timestampCol . ' < :end';
     }
     if (count($where) != 0) {
         $sql .= ' WHERE ' . implode(' AND ', $where);
     }
     $sql .= sprintf(' ORDER BY
         t.%1$s ASC,
         t.%2$s ASC,
         t.%3$s ASC', self::$_tableCol, self::$_sensorIdCol, self::$_timestampCol);
     $sql = preg_replace('/\\r\\n|\\r|\\n/', ' ', $sql);
     $sql = preg_replace('/\\s+/', ' ', $sql);
     $sth = $this->pdo->prepare($sql);
     if ($this->logger != null) {
         $this->logger->message($sql);
     }
     foreach ($query->getTables() as $table) {
         foreach ($query->getSensorsByTable($table) as $i => $sensor) {
             $sth->bindValue(":{$table}{$i}", $sensor);
         }
     }
     if ($query->getStart() !== null) {
         $sth->bindValue(':start', $query->getStart());
     }
     if ($query->getEnd() !== null) {
         $sth->bindValue(':end', $query->getEnd());
     }
     return $sth;
 }
Ejemplo n.º 3
0
<?php

if (isset($_SERVER['HTTP_CLIENT_IP']) || isset($_SERVER['HTTP_X_FORWARDED_FOR']) || !(in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', 'fe80::1', '::1']) || php_sapi_name() === 'cli-server')) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check ' . basename(__FILE__) . ' for more information.');
}
require_once __DIR__ . '/../vendor/autoload.php';
use DS3\Framework\HTTP\Request;
use DS3\Framework\PDO\FilePDOBuilder;
use DS3\Framework\HTTP\JsonResponse;
use DS3\Framework\Logger\Logger;
use DS3\Framework\Filesystem\File;
use DS3\Framework\HTTP\JsonHandler;
$logger = new Logger(new File(__DIR__ . '/../app/dev.log'));
try {
    $logger->message("-------------------------------------------------------\n");
    $logger->message(sprintf('[%s] : Started dbCharts', date(DATE_ATOM)));
    // --- Request
    $logger->message("Creating request...", true);
    $request = Request::fromGlobals();
    $logger->done();
    $logger->message('Handling request ' . "`{$request->getMethod()} {$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}`");
    // --- PDO configuration
    $pdo_cfg_path = '/../app/pdo.cfg';
    $logger->message("Loading PDO configuration from file '" . $pdo_cfg_path . "'...", true);
    $pdo_config = new FilePDOBuilder(__DIR__ . $pdo_cfg_path);
    $logger->done();
    // --- Controller
    $logger->message("Creating controller...", true);
    $controller = new \DS3\Application\Controller($pdo_config, $logger);
    $logger->done();
Ejemplo n.º 4
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use DS3\Framework\HTTP\Request;
use DS3\Framework\PDO\FilePDOBuilder;
use DS3\Framework\HTTP\JsonResponse;
use DS3\Framework\Logger\Logger;
use DS3\Framework\Filesystem\File;
use DS3\Framework\HTTP\JsonHandler;
$logger = new Logger(new File(__DIR__ . '/../app/prod.log'));
$logger->message(sprintf('[%s] : Started dbCharts', date(DATE_ATOM)), true);
try {
    $request = Request::fromGlobals();
    $pdo_config = new FilePDOBuilder(__DIR__ . '/../app/pdo.cfg');
    $controller = new \DS3\Application\Controller($pdo_config, $logger);
    $response = $controller->handle($request);
    $response->send();
    $logger->done();
} catch (\Exception $e) {
    $logger->message(sprintf('Exception occured : %s', JsonHandler::encode($e)));
    $response = new JsonResponse(array('code' => 500, 'message' => 'Internal Server Error'), 500);
    $response->send();
    $logger->done();
}