From: Stricted Date: Mon, 17 Feb 2014 00:18:38 +0000 (+0100) Subject: update some classes X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=5f6347bf417dbd90664fc7a6d912e2bf7de2deb6;p=Snippets.git update some classes --- diff --git a/APC.class.php b/APC.class.php index 27693bb..f5b7fe2 100644 --- a/APC.class.php +++ b/APC.class.php @@ -24,9 +24,11 @@ class APC { if (extension_loaded("apcu")) { $this->apcu = true; $this->version = phpversion('apcu'); - } else if (extension_loaded("apc")) { + } + else if (extension_loaded("apc")) { $this->version = phpversion('apc'); - } else { + } + else { throw new Exception('APC/APCu support is not enabled.'); } } diff --git a/Feed.class.php b/Feed.class.php index 180c968..520ddac 100644 --- a/Feed.class.php +++ b/Feed.class.php @@ -1,39 +1,41 @@ - */ +* @author Jan Altensen (Stricted) +* @copyright 2013-2014 Jan Altensen (Stricted) +* @license GNU Lesser General Public License +*/ class FeedClass { public function __construct($url) { - $file = file_get_contents($url); - $xml = simplexml_load_string($file); - $type = $this->getFeedType($xml); - $data = null; - switch($type) { - case 'rss': - $data = $this->parseRSSFeed($xml); - break; - case 'atom': - $data = $this->parseAtomFeed($xml); - break; - default: - return null; - break; - } - return $data; - } - - private function parseRSSFeed($xml) { + $file = file_get_contents($url); + $xml = simplexml_load_string($file); + $type = $this->getFeedType($xml); + $data = null; + switch($type) { + case 'rss': + $data = $this->parseRSSFeed($xml); + break; + + case 'atom': + $data = $this->parseAtomFeed($xml); + break; + + default: + return null; + break; + } + return $data; + } + + private function parseRSSFeed($xml) { foreach ($xml->channel->item as $item) { $item->pubDate = $this->getTime($item->pubDate); $item->description = html_entity_decode($item->description); $data[] = $item; } return $data; - } + } - private function parseAtomFeed($xml) { + private function parseAtomFeed($xml) { $entries = $xml->children()->entry; foreach ($entries as $entry) { $entry->children()->author = $entry->children()->author->name; @@ -42,22 +44,26 @@ class FeedClass { $data[] = $entry->children(); } return $data; - } + } - private function getTime($time) { - return date("d.m.Y H:i:s",intval(strtotime($time)));; + private function getTime($time) { + return date("d.m.Y H:i:s",intval(strtotime($time))); } - - private function getTimeStamp($time) { - return intval(strtotime($time));; + + private function getTimeStamp($time) { + return intval(strtotime($time)); } - public static function getFeedType($xml) { - $type = false; - if (isset($xml->channel->item)) $type = 'rss'; - if (isset($xml->entry)) $type = 'atom'; + public static function getFeedType($xml) { + $type = ''; + if (isset($xml->channel->item)) { + $type = 'rss'; + } + else if (isset($xml->entry)) { + $type = 'atom'; + } - return $type; - } + return $type; + } } ?> \ No newline at end of file diff --git a/LDAP.class.php b/LDAP.class.php index fbf4623..b53721a 100644 --- a/LDAP.class.php +++ b/LDAP.class.php @@ -20,10 +20,9 @@ class LDAP { * Constructs a new instance of LDAP class. */ public function __construct () { - if(!extension_loaded("ldap")) { + if (!extension_loaded("ldap")) { throw new Exception("Can not find LDAP extension."); } - } /** @@ -37,13 +36,16 @@ class LDAP { public function connect ($server, $port, $dn) { $this->ldap = ldap_connect($server, $port); $this->dn = $dn; - if($this->ldap) { + + if ($this->ldap) { ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($this->ldap, LDAP_OPT_REFERRALS, 0); return true; - } else { + } + else { throw new Exception("Cant connect to ldap server."); } + return false; } diff --git a/db.class.php b/db.class.php index ccca08e..76eab9b 100644 --- a/db.class.php +++ b/db.class.php @@ -14,30 +14,32 @@ class DB { * * @return true/false */ - public function connect($driver, $host, $user, $pass, $db, $port = Null) { + public function connect($driver, $host, $username, $password, $database, $port = "", $driver_options = array()) { if (!extension_loaded("pdo")) die("Missing PDO PHP extension."); // check if extension loaded try { switch (strtolower($driver)) { case "mysql": if (!extension_loaded("pdo_mysql")) die("Missing pdo_mysql PHP extension."); // check if extension loaded - if(empty($port)) $port=3306; - $this->conn = new PDO("mysql:host=".$host.";port=".$port.";dbname=".$db, $user, $pass); + if (empty($port)) $port=3306; + $this->conn = new PDO("mysql:host=".$host.";port=".$port.";dbname=".$v, $username, $password, $driver_options); break; case "pgsql": if (!extension_loaded("pdo_pgsql")) die("Missing pdo_pgsql PHP extension."); // check if extension loaded - if(empty($port)) $port=5432; - $this->conn = new PDO("pgsql:host=".$host.";port=".$port.";dbname=".$db, $user, $pass); + if (empty($port)) $port=5432; + $this->conn = new PDO("pgsql:host=".$host.";port=".$port.";dbname=".$database, $username, $password, $driver_options); break; case "sqlite": if (!extension_loaded("pdo_sqlite")) die("Missing pdo_sqlite PHP extension."); // check if extension loaded - if(!file_exists($db)) { - @touch($db); + if (!file_exists($v)) { + @touch($database); } - if(file_exists($db) && is_readable($db) && is_writable($db)) { - $this->conn = new PDO("sqlite:".$db, $user, $pass); - } else { + + if (file_exists($database) && is_readable($database) && is_writable($database)) { + $this->conn = new PDO("sqlite:".$database, $username, $password, $driver_options); + } + else { $this->error = "cant crate/connect the/to sqlite database"; return false; } @@ -73,10 +75,14 @@ class DB { try { $query = Null; $query = $this->conn->prepare($res); - if(is_array($bind) && !empty($bind)) + + if (is_array($bind) && !empty($bind)) { $query->execute($bind); - else + } + else { $query->execute(); + } + return $query; } catch (PDOException $e) { $this->error = $e->getMessage(); diff --git a/git.class.php b/git.class.php index 412ed71..4acb81f 100644 --- a/git.class.php +++ b/git.class.php @@ -21,7 +21,7 @@ class git { */ public function get_commits ($count = -1, $branch = "master") { $data = array(); - if(!$data) { + if (!$data) { $cmd = "git --git-dir=".$this->repo." rev-list --header --max-count=".$count." ".$branch." "; $rev_list = shell_exec($cmd); foreach(explode("\000", $rev_list) as $rev) { @@ -29,24 +29,30 @@ class git { $rev_lines = explode("\n", str_replace("\r", "", $rev)); $commit['id'] = $rev_lines[0]; foreach($rev_lines as $rev_line) { - if(substr($rev_line, 0, 4) == " ") { - if(isset($commit['text'])) { + if (substr($rev_line, 0, 4) == " ") { + if (isset($commit['text'])) { $commit['text'] .= "\n".substr($rev_line, 4); - } else + } + else { $commit['text'] = substr($rev_line, 4); - } else { + } + } + else { $opt = explode(" ", $rev_line, 2); - if($opt[0] == "tree") { + if ($opt[0] == "tree") { $commit['tree'] = $opt[1]; - } else if($opt[0] == "parent") { + } + else if ($opt[0] == "parent") { $commit['parent'][] = $opt[1]; - } else if($opt[0] == "author") { + } + else if ($opt[0] == "author") { preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches); $commit['author'] = $matches[1]; $commit['author_mail'] = $matches[2]; $commit['author_time'] = $matches[3]; $commit['author_timezone'] = $matches[4]; - } else if($opt[0] == "committer") { + } + else if ($opt[0] == "committer") { preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches); $commit['committer'] = $matches[1]; $commit['committer_mail'] = $matches[2]; @@ -111,27 +117,35 @@ class git { if ($age > 60*60*24*365*2) { $age_str = floor($age/60/60/24/365); $age_str .= " years ago"; - } else if ($age > 60*60*24*(365/12)*2) { + } + else if ($age > 60*60*24*(365/12)*2) { $age_str = floor($age/60/60/24/(365/12)); $age_str .= " months ago"; - } else if ($age > 60*60*24*7*2) { + } + else if ($age > 60*60*24*7*2) { $age_str = floor($age/60/60/24/7); $age_str .= " weeks ago"; - } else if ($age > 60*60*24*2) { + } + else if ($age > 60*60*24*2) { $age_str = floor($age/60/60/24); $age_str .= " days ago"; - } else if ($age > 60*60*2) { + } + else if ($age > 60*60*2) { $age_str = floor($age/60/60); $age_str .= " hours ago"; - } else if ($age > 60*2) { + } + else if ($age > 60*2) { $age_str = floor($age/60); $age_str .= " min ago"; - } else if ($age > 2) { + } + else if ($age > 2) { $age_str = $age; $age_str .= " sec ago"; - } else if ($age >= 0) { + } + else if ($age >= 0) { $age_str = "right now"; - } else { + } + else { $age_str = "right now"; } return $age_str; @@ -147,13 +161,16 @@ class git { $now = time(); $age = ($last_change > 0 ? ($now - $last_change) : 0); - if($age == 0) { + if ($age == 0) { $age_class = "noage"; - } else if ($age < 60*60*2) { + } + else if ($age < 60*60*2) { $age_class = "age0"; - } else if ($age < 60*60*24*2) { + } + else if ($age < 60*60*24*2) { $age_class = "age1"; - } else { + } + else { $age_class = "age2"; } return $age_class; diff --git a/hash.class.php b/hash.class.php index 7db046e..994c129 100644 --- a/hash.class.php +++ b/hash.class.php @@ -22,9 +22,11 @@ class hash { $this->raw = $raw; $this->hmac = $hmac; $this->key = $key; + if (in_array($algo, hash_algos())) { $this->algo = $algo; - } else { + } + else { die("algorithm ".$algo." not supported"); } } @@ -38,7 +40,8 @@ class hash { public function hash ($data) { if (!empty($this->hmac)) { return hash_hmac($this->algo, $data, $this->key, $this->raw); - } else { + } + else { return hash($this->algo, $data, $this->raw); } } @@ -52,7 +55,8 @@ class hash { public function hash_file ($file) { if (!empty($this->hmac)) { return hash_hmac_file($this->algo, $file, $this->key, $this->raw); - } else { + } + else { return hash_file($this->algo, $file, $this->raw); } } @@ -66,9 +70,11 @@ class hash { */ public function compare ($hash, $data) { $newhash = $this->hash($data); + if ($newhash == $hash) { return true; } + return false; } @@ -81,9 +87,11 @@ class hash { */ public function compare_file ($hash, $file) { $newhash = $this->hash_file($file); + if ($newhash == $hash) { return true; } + return false; } }