From 51609351f2c4b5082b7e6f0744cd3811c325303f Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 11 Oct 2016 14:01:29 +0200 Subject: * initial template --- include/Convert.class.php | 15 ++ include/Env.class.php | 113 +++++++++++++++ include/Menu.class.php | 30 ++++ include/Minify.class.php | 78 ++++++++++ include/MyCache.class.php | 113 +++++++++++++++ include/MyLocale.class.php | 89 ++++++++++++ include/MySession.class.php | 61 ++++++++ include/MySmarty.class.php | 18 +++ include/PgDB.class.php | 339 ++++++++++++++++++++++++++++++++++++++++++++ include/setup.php | 74 ++++++++++ 10 files changed, 930 insertions(+) create mode 100644 include/Convert.class.php create mode 100644 include/Env.class.php create mode 100644 include/Menu.class.php create mode 100644 include/Minify.class.php create mode 100644 include/MyCache.class.php create mode 100644 include/MyLocale.class.php create mode 100644 include/MySession.class.php create mode 100644 include/MySmarty.class.php create mode 100644 include/PgDB.class.php create mode 100644 include/setup.php (limited to 'include') diff --git a/include/Convert.class.php b/include/Convert.class.php new file mode 100644 index 0000000..90cc59c --- /dev/null +++ b/include/Convert.class.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/include/Env.class.php b/include/Env.class.php new file mode 100644 index 0000000..2f7ed44 --- /dev/null +++ b/include/Env.class.php @@ -0,0 +1,113 @@ + $link) { + if (is_array($link)) { + foreach ($link as $subitem => $sublink) { + if (trim($sublink, '/') === $pageID) { + $activeContent = $link['_']; + break; + } + } + } else { + if (trim($link, '/') === $pageID) { + $activeContent = $link; + } + } + } + return $activeContent; + } + +} + +?> diff --git a/include/Minify.class.php b/include/Minify.class.php new file mode 100644 index 0000000..ad66f32 --- /dev/null +++ b/include/Minify.class.php @@ -0,0 +1,78 @@ + diff --git a/include/MyCache.class.php b/include/MyCache.class.php new file mode 100644 index 0000000..e074a3b --- /dev/null +++ b/include/MyCache.class.php @@ -0,0 +1,113 @@ + diff --git a/include/MySession.class.php b/include/MySession.class.php new file mode 100644 index 0000000..b57f7c8 --- /dev/null +++ b/include/MySession.class.php @@ -0,0 +1,61 @@ + diff --git a/include/MySmarty.class.php b/include/MySmarty.class.php new file mode 100644 index 0000000..31aa5b4 --- /dev/null +++ b/include/MySmarty.class.php @@ -0,0 +1,18 @@ +caching = Smarty::CACHING_OFF; + $this->template_dir = BASEPATH.'/template'; + $this->compile_dir = BASEPATH.'/caches/smarty/compile'; + $this->cache_dir = BASEPATH.'/caches/smarty'; + $this->addPluginsDir(BASEPATH.'/lib/smarty-plugins'); + } + +} + +?> \ No newline at end of file diff --git a/include/PgDB.class.php b/include/PgDB.class.php new file mode 100644 index 0000000..b79b2ec --- /dev/null +++ b/include/PgDB.class.php @@ -0,0 +1,339 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + self::$_db = &self::$_master_db; + } + + /** returns value from first column of first row of query result. + * accepts two types of variable substitution: '?' or ':var', + * '?' expects variable parameter list, ':var' expects second argument + * to be array working as dictionary + * \param $query PgSQL query with optional parameter substitution + * \param $params (optional) array or first parameter for '?' type query + * \param $... - (optional) following parameters for '?' type query + */ + public static function fetchValue($query, $params = []) { + self::_needsMasterDB(); + $sth = self::$_db->prepare($query.';'); + if (!is_array($params)) { + $params = array_slice(func_get_args(),1); + } + $sth->execute($params); + $result = $sth->fetch(PDO::FETCH_NUM); + return isset($result[0]) ? $result[0] : null; + } + + /** returns first first row of query result as assiociative array. + * accepts two types of variable substitution: '?' or ':var', + * '?' expects variable parameter list, ':var' expects second argument + * to be array working as dictionary + * \param $query PgSQL query with optional parameter substitution + * \param $params (optional) array or first parameter for '?' type query + * \param $... - (optional) following parameters for '?' type query + */ + public static function fetchRow($query, $params = []) { + self::_needsMasterDB(); + $sth = self::$_db->prepare($query.';'); + if (!is_array($params)) { + $params = array_slice(func_get_args(),1); + } + + $sth->execute($params); + return $sth->fetch(PDO::FETCH_ASSOC); + } + + /** returns whole query result as numbered array of assiociative arrays. + * accepts two types of variable substitution: '?' or ':var', + * '?' expects variable parameter list, ':var' expects second argument + * to be array working as dictionary + * \param $query PgSQL query with optional parameter substitution + * \param $params (optional) array or first parameter for '?' type query + * \param $... - (optional) following parameters for '?' type query + */ + static public function fetchAll($query, $params = []) { + self::_needsMasterDB(); + $sth = self::$_db->prepare($query.';'); + if (!is_array($params)) { + $params = array_slice(func_get_args(),1); + } + + $sth->execute($params); + return $sth->fetchAll(PDO::FETCH_ASSOC); + } + + + static public function execute($query, $params = []) { + self::_needsMasterDB(); + $sth = self::$_db->prepare($query.';'); + if (!is_array($params)) { + $params = array_slice(func_get_args(),1); + } + $sth->execute($params); + return $sth->rowCount(); + + } + + static public function insert($table, $values, $functions = []) { + self::_needsMasterDB(); + list($query,$args) = self::_insert_prepare($table,$values,$functions); + $sth = self::$_db->prepare($query.';'); + $sth->execute($args); + return $sth->rowCount(); + } + + + static public function update($table,$values,$whereclause,$functions="") { + self::_needsMasterDB(); + $query = 'UPDATE "'. $table .'" SET '; + $cnt=0; + $where = ""; + $args = []; + + foreach ($values as $key => $val) { + if ($cnt) { + $query .=', '; + } + $query .= '"'. $key .'"=?'; + if (is_bool($val)) { + $args[] = ($val) ? 'TRUE' : 'FALSE'; + } + else { + $args[] = $val; + } + $cnt++; + } + + if ($functions!="") { + foreach ($functions as $key => $val) { + if ($cnt) { + $query .=', '; + } + $query .= '"'. $key .'"'. $val; + $cnt++; + } + } + + if (empty($values)) { + $query = 'SELECT * FROM '.$table; + } + + if (is_array($whereclause)) { + $cnt=0; + foreach ($whereclause as $key => $val) { + if ($cnt) { + $where .=' AND '; + } + $where .= '("'. $key .'"=?)'; + $args[] = $val; + $cnt++; + } + $query .= " WHERE ".$where .";"; + } + else { + $query .= " WHERE ". $whereclause .";"; + } + + $sth = self::$_db->prepare($query.';'); + $sth->execute($args); + return $sth->rowCount(); + } + + + public static function set($table,$values,$wherelist,$functions = []) { + $rowsAffected = self::update($table,$values,$wherelist,$functions); + if ($rowsAffected == 0) { + return self::insert($table,array_merge($values,$wherelist),$functions); + } + return $rowsAffcted; + } + + + public static function delete($table, $values, $extrarules="") { + $query = 'DELETE FROM "'. $table .'" WHERE '; + $cnt=0; + $args = []; + foreach ($values as $key => $val) { + if ($cnt) { + $query .= 'AND'; + } + $query .= ' ("'. $key .'" = ?)'; + $args[] = $val; + $cnt++; + } + + if ($extrarules !== "") { + $query .= ' AND '.$extrarules; + } + return self::execute($query,$args); + } + + static private function _insert_prepare($table,$values,$functions = []) { + $query = 'INSERT INTO "'. $table .'"('; + $cnt=0; + $valbuf = ""; + $args = []; + foreach ($values as $key => $val) { + if ($cnt) { + $query .=','; + $valbuf.=','; + } + $query .= '"'. $key .'"'; + $valbuf .= '?'; + + if (is_bool($val)) { + $args[] = ($val) ? 'TRUE' : 'FALSE'; + } + elseif ($val===null) { + $args[] = NULL; + } + else { + $args[] = $val; + } + $cnt++; + } + + foreach ($functions as $key => $val) { + if ($cnt) { + $query .=','; + $valbuf .=','; + } + $query .= '"'. $key .'"'; + $valbuf .= $val; + + $cnt++; + } + + $query .= ") VALUES (". $valbuf .");"; + + return [$query, $args]; + } + + + public static function begin() { + self::_needsMasterDB(); + if (self::$_transaction_depth == 0) { + self::$_db->beginTransaction(); + self::$_transaction_depth++; + } + elseif (self::$_transaction_failed) { + throw new Exception("Trying to begin transaction inside of already stopped transaction!"); + } + else { + self::$_transaction_depth++; + } + } + + public static function commit() { + if (self::$_transaction_depth>0) { + if (self::$_transaction_failed) { + // I'm not convinced that it's good idea, + // but we need to be sure transaction won't go to database. + self::rollback(); + throw new Exception("Transaction already stopped, couldn't be commited"); + } + self::$_transaction_depth--; + if (self::$_transaction_depth==0) { + return self::$_db->commit(); + } + } + else { + throw new Exception("To commit transaction it needs to be started first"); + } + } + + public static function rollback() { + self::$_transaction_failed = true; + self::$_transaction_depth--; + if (self::$_transaction_depth == 0) { + self::$_db->rollback(); + self::$_transaction_failed = false; + } + } + + public static function inTransaction() { + return (self::$_transaction_depth>0); + } + + public static function transactionStateFailed() { + return self::$_transaction_failed; + } +} + +?> diff --git a/include/setup.php b/include/setup.php new file mode 100644 index 0000000..f5e58f4 --- /dev/null +++ b/include/setup.php @@ -0,0 +1,74 @@ + 'ERROR', + E_WARNING => 'WARNING', + E_PARSE => 'PARSING ERROR', + E_NOTICE => 'NOTICE', + E_CORE_ERROR => 'CORE ERROR', + E_CORE_WARNING => 'CORE WARNING', + E_COMPILE_ERROR => 'COMPILE ERROR', + E_COMPILE_WARNING => 'COMPILE WARNING', + E_USER_ERROR => 'USER ERROR', + E_USER_WARNING => 'USER WARNING', + E_USER_NOTICE => 'USER NOTICE', + E_STRICT => 'STRICT NOTICE', + E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR', + E_DEPRECATED => 'DEPRECATED', + E_USER_DEPRECATED => 'USER DEPRECATED' + ]; + return $severities[$code]; +} + +Env::get(); +MySession::init(); +//MyLocale::init(); + +?> -- cgit v1.2.3