/** * Éxécute PDOStatement::bindValue sur une instance de PDOStatement passé en paramètre * * @param PDOStatement $pdoStatement * @param $data * * @return PDOStatement */ protected static function bind(PDOStatement $pdoStatement, array $data = []) { // On sécurise les informations avants l'insertion. $data = Security::sanitaze($data, true); foreach ($data as $key => $value) { if (is_null($value) || strtolower($value) === 'null') { $pdoStatement->bindValue(':' . $key, $value, PDO::PARAM_NULL); continue; } $param = PDO::PARAM_INT; if (preg_match('/[a-zA-Z_-]+/', $value)) { /** * SÉCURIATION DES DONNÉS * - Injection SQL * - XSS */ $param = PDO::PARAM_STR; } else { /** * On force la valeur en entier ou en réél. */ if (is_int($value)) { $value = (int) $value; } else { if (is_float($value)) { $value = (double) $value; } else { $value = (double) $value; } } } /** * Exécution de bindValue */ if (is_string($key)) { $pdoStatement->bindValue(':' . $key, $value, $param); } else { $pdoStatement->bindValue($key, $value, $param); } } }
/** * secure, échape les anti-slashes, les balises html * eg. secure('j'ai') => j\'ai * * @param mixed $data * @return mixed */ function secure($data) { if (is_numeric($data)) { return $data; } else { return Security::sanitaze($data, true); } }
/** * templateLoader, charge le moteur template à utiliser. * * @throws ErrorException * @return \Twig_Environment|\Mustache_Engine|\Jade\Jade */ private function templateLoader() { if ($this->config->getTemplateEngine() !== null) { if (!in_array($this->config->getTemplateEngine(), ['twig', 'mustache', 'jade'], true)) { throw new ErrorException('Le moteur de template n\'est pas implementé.', E_USER_ERROR); } } else { throw new ResponseException('Le moteur de template non défini.', E_USER_ERROR); } $tpl = null; if ($this->config->getTemplateEngine() == 'twig') { $loader = new \Twig_Loader_Filesystem($this->config->getViewpath()); $tpl = new \Twig_Environment($loader, ['cache' => $this->config->getCachepath(), 'auto_reload' => $this->config->getCacheAutoReload(), 'debug' => $this->config->getLoggerMode() == 'develepment' ? true : false]); /** * - Ajout de variable globale * dans le cadre de l'utilisation de Twig */ $tpl->addGlobal('public', $this->config->getPublicPath()); $tpl->addGlobal('root', $this->config->getApproot()); /** * - Ajout de fonction global * dans le cadre de l'utilisation de Twig */ $tpl->addFunction(new \Twig_SimpleFunction('secure', function ($data) { return Security::sanitaze($data, true); })); $tpl->addFunction(new \Twig_SimpleFunction('sanitaze', function ($data) { return Security::sanitaze($data); })); $tpl->addFunction(new \Twig_SimpleFunction('csrf_field', function () { return Security::getCsrfToken()->field; })); $tpl->addFunction(new \Twig_SimpleFunction('csrf_token', function () { return Security::getCsrfToken()->token; })); $tpl->addFunction(new \Twig_SimpleFunction('slugify', [Str::class, 'slugify'])); return $tpl; } if ($this->config->getTemplateEngine() == 'mustache') { return new \Mustache_Engine(['cache' => $this->config->getCachepath(), 'loader' => new \Mustache_Loader_FilesystemLoader($this->config->getViewpath()), 'helpers' => ['secure' => function ($data) { return Security::sanitaze($data, true); }, 'sanitaze' => function ($data) { return Security::sanitaze($data); }, 'slugify' => function ($data) { return Str::slugify($data); }, 'csrf_token' => function () { return Security::getCsrfToken()->token; }, 'csrf_field' => function () { return Security::getCsrfToken()->field; }, 'public', $this->config->getPublicPath(), 'root', $this->config->getApproot()]]); } return new Jade(['cache' => $this->config->getCachepath(), 'prettyprint' => true, 'extension' => $this->config->getTemplateExtension()]); }
/** * Action update * * @param array $data Les données à mettre à jour * @param callable $cb La fonction de rappel. Dans ou elle est définie * elle récupère en paramètre une instance de DatabaseErrorHanlder * et les données récupérés par la réquête. * * @return int */ public function update(array $data = [], callable $cb = null) { $sql = 'update `' . $this->tableName . '` set '; $sql .= parent::rangeField(parent::add2points(array_keys($data))); if (!is_null($this->where)) { $sql .= ' where ' . $this->where; $this->where = null; $data = array_merge($data, $this->whereDataBind); $this->whereDataBind = []; } $stmt = $this->connection->prepare($sql); $data = Security::sanitaze($data, true); static::bind($stmt, $data); // execution de la requête $stmt->execute(); // récupération de la dernière erreur. self::$errorInfo = $stmt->errorInfo(); $r = $stmt->rowCount(); if (is_callable($cb)) { return call_user_func_array($cb, [$this->getResponseOfQuery($r), $r]); } return (int) $r; }
/** * Insertion des données dans la DB avec la method query * ====================== USAGE ======================== * $options = [ * "query" => [ * "table" => "nomdelatable", * "type" => INSERT|SELECT|DELETE|UPDATE, * "data" => [ les informations a mettre ici dépendent de la requête que l'utilisateur veux faire. ] * ], * "data" => [ "les données a insérer." ] * ]; * * @param array $options * @param bool|false $return * @param bool|false $lastInsertId * * @throws \ErrorException * * @return array|static|\StdClass */ public static function query(array $options, $return = false, $lastInsertId = false) { static::verifyConnection(); $sqlStatement = static::makeQuery($options["query"]); $pdoStatement = static::$db->prepare($sqlStatement); static::bind($pdoStatement, isset($options["data"]) ? $options["data"] : []); $pdoStatement->execute(); static::$errorInfo = $pdoStatement->errorInfo(); $data = $pdoStatement->fetchAll(); if ($return == true) { if ($lastInsertId == false) { $data = empty($data) ? null : Security::sanitaze($data); } else { $data = static::$db->lastInsertId(); } } return $data; }