public static function log($level, $message, array $context = array()) { $replace = array(); foreach ($context as $key => $val) { $replace['{' . $key . '}'] = $val; } $msg = strtr($message, $replace); switch ($level) { case 'emergency': case 'alert': echo 'ALERT! Any issue ocurred and may compromise the system, contact the website admin!'; break; case 'critical': echo 'Critical situation ocurred, Not the first time you see this message? contact the website admin!'; break; case 'error': echo 'Unknow error ocurred, please try again later or contact the website admin!'; break; case 'debug': echo 'DEBUG started: see at ' . DBNAME . '.log table this status detailed'; break; default: $_SESSION['logger'][] = $message; break; } try { $db = PDODatabase::getInstance(); $log = "INSERT INTO log SET message = ?, level = ?, request_ip = ?, request_uri = ?"; $query = $db->prepare($log); $query->bindParam(1, $msg); $query->bindParam(2, $level); $query->bindParam(3, $_SERVER['REMOTE_ADDR']); $query->bindParam(4, $_SERVER['REQUEST_URI']); $query->execute(); } catch (Exception $e) { echo 'ERROR occurred when trying write a ' . $level . ' log.\\n'; echo 'Lost log message was ´' . $msg . '´\\n'; echo 'Contact the website admin or try again later!\\n'; } }
/** * Drop from the database by the items where matchs @id * * @param array $data * @throws NoLoggedException * @return array */ public function delete($params = array(null, null, null)) { try { $command = "DELETE FROM " . constant(get_class($this) . "::TABLENAME"); $data = get_class_vars(get_class($this)); // total of values for binding $key_ = $values = ""; $command .= rtrim($values, ", ") . " WHERE "; //checks the criteria for matching if (is_null($params[0]) || strtolower($params[0]) == "id") { $criteria = "id"; } else { $criteria = "{$params[0]} "; } $operation = !is_null($params[1]) ? $params[1] : "="; // match operation // matching param if (!is_null($params[2])) { $key_ = is_numeric($params[2]) ? $params[2] : "'" . $params[2] . "'"; } else { $key_ = $this->id; } PDODatabase::getInstance()->beginTransaction(); $command .= $criteria . " " . $operation . " " . $key_; // joins all $query = PDODatabase::getInstance()->prepare($command); $i = 1; foreach ($data as $key => $val) { $query->bindValue($i, $this->{$key}, PDODatabase::PARAM_STR); $i++; } PDODatabase::getInstance(); return $query->execute(); } catch (Exception $e) { PDODatabase::getInstance()->rollback(); throw new NoLoggedException('Sorry, an error occurred. Couldn\'t save this!'); } }