db = $db; } function initializeDB() { $this->db->uquery($this->tableStructure); return TRUE; } function getById($id) { $req = 'SELECT * FROM `projects` WHERE `id`="'.$id.'";'; $result = $this->db->aquery($req); return $result[0]; } function add($title="",$text="",$public=false,$default=false) { if(empty($title)) return 'No title.'; if(empty($text)) return 'No text.'; $public ? $p = '1' : $p = '0'; $query = 'INSERT INTO `projects` (`id`,`title`,`text`,`public`,`def`) VALUES (null,\''.$this->db->e($title).'\', \''.$this->db->e($text).'\' ,"'.$p.'","0");'; $this->db->uquery($query); $id = $this->db->insert_id(); if($default) $this->setDefault($id); return TRUE; } function setDefault($id) { $this->db->uquery('UPDATE `projects` SET def="0" WHERE def="1";'); $this->db->uquery('UPDATE `projects` SET def="1" WHERE id="'.intval($id).'";'); return true; } function getDefault() { $res = $this->db->aquery('SELECT id FROM projects WHERE def="1";'); if(count($res) != 1) return false; return $res[0]['id']; } function exists($id) { $res = $this->db->aquery('SELECT COUNT(*) FROM projects WHERE id="'.intval($id).'";'); if(count($res) == 1) return $id; return false; } function getCurrent($userData) { if(!empty($_GET['project'])) { if(is_numeric($_GET['project'])) $id = $this->exists($_GET['project']); if(!empty($id)) return $id; } if(!empty($userData['currentproject'])) return $userData['currentproject']; return $this->getDefault(); } function delete($id) { $id = intval($id); $this->db->uquery('DELETE FROM `projects` WHERE `id`="'.$id.'";'); $this->db->uquery('DELETE FROM `bugs` WHERE `project`="'.$id.'";'); $this->db->uquery('DELETE FROM `comments` WHERE `project`="'.$id.'";'); return true; } function getList($all=false) { if(!$all) $where = 'WHERE public="1"'; else $where = ''; $out = $this->db->aquery('SELECT * FROM `projects` '.$where.' ORDER BY `def` DESC,`title` ASC'); $this->listNb = count($out); return $out; } } class Bugs { var $db = FALSE; var $listNb; var $byPage = 20; var $tableStructure = " CREATE TABLE `bugs` ( `id` INTEGER PRIMARY KEY NOT NULL, `project` INT NOT NULL DEFAULT 0, `title` VARCHAR(255) NOT NULL DEFAULT 'No title', `created` INT NOT NULL DEFAULT 0, `updated` INT NOT NULL DEFAULT 0, `comments` INT(1) NOT NULL DEFAULT 0, `ip` VARCHAR(20) NOT NULL DEFAULT '', `text` TEXT NOT NULL DEFAULT '', `status` VARCHAR(20) NOT NULL DEFAULT 'open', `type` VARCHAR(20) NOT NULL DEFAULT 'minor', `category` INT NOT NULL DEFAULT 0, `author` VARCHAR(255) NOT NULL DEFAULT 'Anonymous', `file` VARCHAR(255) NOT NULL DEFAULT '', `owner` INT NOT NULL DEFAULT 0);"; function Bugs($db=false) { if(!$db) die('Projects class needs DB.'); $this->db = $db; } function initializeDB() { $this->db->uquery($this->tableStructure); return TRUE; } function add($project,$title="",$text="",$author="",$options=false) { if(empty($title)) return "Empty title."; if(empty($text)) return "Empty text."; if(empty($author)) return "Empty author name or E-Mail."; if($options == false) { $author = strtolower($author); if(!userInput::checkMail($author)) return "Invalid E-Mail address."; $options = array( 'comments' => 1, 'category' => 0, 'file' => '', 'status' => 'open', 'type' => 'user', 'owner' => 0, ); } if(isset($options['created'])) $date = $options['created']; if(!isset($date) || empty($date)) $date = time(); if(empty($options['comments']) || $options['comments'] != '1') $options['comments'] = '0'; if(empty($options['category'])) $options['category'] = '0'; $query = 'INSERT INTO `bugs` (`id`,`project`,`title`,`created`,`text`,`updated`,`comments`,`status`,`type`,`category`,`author`,`owner`,`file`,`ip`) '. 'VALUES (null,"'.$project.'","'.$title.'","'.$date.'",\''.$this->db->e($text).'\',"0","'.$options['comments'].'","'. $options['status'].'","'.$options['type'].'","'.$options['category'].'","'. $author.'","'.$options['owner'].'","'.$options['file'].'","'.$_SERVER['REMOTE_ADDR'].'");'; if($this->db->uquery($query)) return $this->db->insert_id(); else return false; } function getList($project,$begin=0) { $out = $this->db->aquery('SELECT * FROM `bugs` WHERE project="'.intval($project).'" ORDER BY `updated` DESC LIMIT '.$begin.','.$this->byPage.';'); $this->listNb = count($out); return $out; } function getPagination($begin,$queryCount) { return false; } function getById($id) { $res = $this->db->aquery('SELECT * FROM bugs WHERE id="'.intval($id).'";'); return $res[0]; } function delete($id) { $this->db->uquery('DELETE FROM bugs WHERE id="'.intval($id).'";'); $this->db->uquery('DELETE FROM comments WHERE bug="'.intval($id).'";'); return true; } } class Comments { var $db = FALSE; var $listNb; var $tableStructure = " CREATE TABLE `comments` ( `id` INTEGER PRIMARY KEY NOT NULL, `bug` INT NOT NULL, `project` INT NOT NULL, `genre` INT(1) NOT NULL DEFAULT '0', `author` VARCHAR(255) NOT NULL DEFAULT 1, `date` INT NOT NULL DEFAULT 0, `ip` VARCHAR(20) NOT NULL DEFAULT '', `text` TEXT NOT NULL DEFAULT '');"; function Comments($db=false) { if(!$db) die('Projects class needs DB.'); $this->db = $db; } function initializeDB() { $this->db->uquery($this->tableStructure); return TRUE; } function add($bug,$genre,$nick,$text="") { if(empty($text)) xHTML::Error("Field text is empty.","You cannot leave this field empty, it is required.",TRUE); $date = time(); $query = 'INSERT INTO `comments` (`id`,`bug`,`genre`,`author`,`date`,`text`,`ip`) '. 'VALUES ("","'.$bug.'","'.$genre.'","'.$author.'","'.$date.'","'.$text.'","'.$_SERVER['REMOTE_ADDR'].'");'; $this->db->uquery($query); return TRUE; } function getList($owner) { $out = $this->db->aquery('SELECT * FROM `comments` ORDER BY `date` DESC'); $this->listNb = count($out); return $out; } } // xHTML Class // v0.1.0 class xHTML { // head(title,css file,don't show menu) function head($title="",$style="",$no_header=FALSE) { header('Content-Type: text/html; charset=utf-8'); $out = ''."\n"; $out.= ''."\n"; $out.= ''."\n"; $out.= ' '.$title.''."\n"; $out.= ' '."\n"; $out.= ' '."\n"; $out.= ' '."\n"; if(file_exists("styles/$style.css")) $out.= ' '."\n"; $out.= ''."\n\n"; $out.= ''."\n"; $out.= '

Bug Manager

'."\n"; if(!$no_header) { $out.= "\n\n"; } $out.= '

'.$title."

\n\n"; $out.= '
'; return $out; } function currentProject() { global $db; if(empty($db)) return 'No Project'; if(!empty($_SESSION['selectCache'])) return $_SESSION['selectCache']; $projects = $db->aquery('SELECT id,title,def,public FROM projects ORDER BY def DESC,public ASC,title ASC;'); $ul = ''; if(!empty($projects) && count($projects) > 1) { $ul.= ''; } if(!empty($_SESSION['userData']) && !empty($_SESSION['userData']['currentproject'])) $current = $_SESSION['userData']['currentproject']; else $current = $default; $out = ''.xHTML::Protect($pList[$current]).''.$ul; $_SESSION['selectCache'] = $out; return $out; } function foot($no_footer=FALSE) { $out = "\n
\n\n"; $out.= "\n"; $out.= "\n"; return $out; } function script($src="",$content="") { if(!empty($src)) $out = '\n"; else $out = "\n"; return $out; } function Protect($field) { $field = stripslashes($field); $field = htmlspecialchars($field); return $field; } function ProtectMail($email) { $len = strlen($email); $temp = ''; for($x = 0; $x < $len; $x++) { $ord = ord(substr($email,$x,1)); $temp.= "&#".$ord.";"; } return $temp; } function FormatCode($code, $lng) { $code = strip_tags($code); return $code; } function Format($field) { $field = xHTML::Protect($field); $field = nl2br($field); $field = eregi_replace( "(^|[ \t\r\n>])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|peercast|ed2k):". "[A-Za-z0-9/](([A-Za-z0-9$|.+!*(),;/?:@&~=_#-])|%[A-Fa-f0-9]{2})+)", "\\1\\2", $field); $field = eregi_replace("(^|[ \t\r\n>])([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)", "\\0", $field); while(eregi("([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)", $field, $pat)) { $field = str_replace($pat[0], xHTML::ProtectMail($pat[0]), $field); } while(eregi('\[code(:([^\]]+))?\]', $field, $pat)) { $begin = strpos($field, $pat[0]); $block = substr($field, $begin, strpos($field, '[/code]') - $begin + 7); $code = substr($block, strlen($pat[0]), -7); $lang = (empty($pat[1]) ? 'generic' : $pat[1]); $code = xHTML::FormatCode($code, $lang); $code = '
'.$code.'
'; $field = str_replace($block, $code, $field); } return $field; } function noCache() { header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); } function Error($message,$debug="",$die=FALSE) { if($die) { echo xHTML::head("Erreur","admin"); echo '

'.htmlspecialchars(stripslashes($message))."

\n"; if(!empty($debug)) echo '

Informations: '.htmlspecialchars(stripslashes($debug))."

\n"; echo xHTML::foot(); exit; } } function Antispam($email) { $len = strlen($email); for($x = 0; $x < $len; $x++) { $ord = ord(substr($email,$x,1)); $temp.= "&#".$ord.";"; } return $temp; } function dateForm($fieldName="date",$timestamp="",$hours=TRUE) { if(empty($timestamp)) $timestamp = time(); $out = ' '; $currentYear = date("Y",$timestamp); /*';*/ $out.= ' '; if($hours) $out.= ' - : '; return $out; } function getMonthName($i) { $months = array(1=>"Janvier",2=>"Février",3=>"Mars",4=>"Avril",5=>"Mai",6=>"Juin",7=>"Juillet",8=>"Août", 9=>"Septembre",10=>"Octobre",11=>"Novembre",12=>"Décembre"); return $months[$i]; } function getDayName($i) { $days = array(1=>"Lundi",2=>"Mardi",3=>"Mercredi",4=>"Jeudi",5=>"Vendredi",6=>"Samedi",7=>"Dimanche"); return $days[$i]; } function dateShort($i) { $j = explode("-",$i); if($j[1] < 10) $j[1] = substr($j[1],1); $out = $j[2]." ".$this->getMonthName($j[1])." ".$j[0]; return $out; } function date2timestamp($i) { $j = explode("-",$i); return mktime(0,0,0,$j[1],$j[2],$j[0]); } } // Class Users // For managing users // v0.1.0 class Users { var $db = FALSE; var $tableStructure = "CREATE TABLE users ( `id` INTEGER PRIMARY KEY, `login` VARCHAR(30) NOT NULL, `currentproject` INT NOT NULL DEFAULT 0, `password` VARCHAR(32) NOT NULL, `email` VARCHAR(255) NOT NULL DEFAULT '', `access` INT NOT NULL DEFAULT 0, `lastcheck` INT NOT NULL DEFAULT 0, `lasthost` VARCHAR(255) NOT NULL DEFAULT '');"; var $firstUser = "INSERT INTO `users` (`id`,`login`,`password`,`access`) VALUES ('0','admin', 'e2fc714c4727ee9395f324cd2e7f331f','100');"; function Users($db=false) { if(!$db) die('Class Users needs DB.'); $this->db = $db; } function initializeDB() { $this->db->uquery($this->tableStructure); $this->db->uquery($this->firstUser); return TRUE; } function Login($login,$password,$sessionTime="0") { if(userInput::checkLogin($login) !== TRUE) return "Nom d'utilisateur invalide."; if(userInput::checkLogin($password) !== TRUE) return "Mot de passe invalide."; $result = $this->db->array_query("SELECT * FROM users WHERE login='".$this->db->e($login)."'"); if(!$result || (count($result) != 1)) return "Impossible de trouver ce nom d'utilisateur."; $result = $result[0]; if($result['password'] != md5($password)) { return "Mot de passe invalide! Réessayez..."; } $time = time(); $host = gethostbyaddr($_SERVER['REMOTE_ADDR']); $this->db->uquery("UPDATE users SET lastcheck='".$time."', lasthost='".$host."' WHERE id='".$result['id']."';"); $_SESSION['logged'] = true; $_SESSION['userData'] = $result; return TRUE; } function isLoggedReturnDatas() { if(!empty($_SESSION['logged'])) return $_SESSION['userData']; return false; } function isLogged() { if(!empty($_SESSION['logged'])) return true; return false; } function Logout($sessionID="") { $_SESSION = array(); session_unset(); session_destroy(); return TRUE; } function haveAccess($accessLevel,$userLevel="") { if(empty($userLevel)) $userLevel = $_SESSION['userData']['access']; if($userLevel >= $accessLevel) return TRUE; return FALSE; } function isAdmin() { if(empty($_SESSION['userData'])) return false; if(!empty($_SESSION['userData']['access']) && ($_SESSION['userData']['access'] >= 10)) return true; return false; } function modify($id,$datas) { extract($datas); if(!empty($password1) && !empty($password2)) { if(($error = userInput::checkPassword($password1,$password2)) !== TRUE) xHTML::Error($error,'',TRUE); $datas['password'] = md5($password1); } if(($error = userInput::checkLogin($login)) !== TRUE) xHTML::Error($error,'',TRUE); unset($datas['password1'],$datas['password2']); $items = array(); foreach($datas as $key=>$value) { $items[] = "$key=\"$value\""; } $query = "UPDATE users SET ".implode(", ",$items)." WHERE id='".$id."'"; $this->db->uquery($query); return TRUE; } function add($datas) { extract($datas); if(($error = userInput::checkPassword($password1,$password2)) !== TRUE) xHTML::Error($error,'',TRUE); $datas['password'] = md5($password1); if(($error = userInput::checkLogin($login)) !== TRUE) xHTML::Error($error,'',TRUE); unset($datas['password1'],$datas['password2']); $items = array(); $values = array(); foreach($datas as $key=>$value) { $items[] = "`$key`"; $values[] = "\"$value\""; } $query = "INSERT INTO users (".implode(",",$items).") VALUES (".implode(",",$values).")"; $this->db->uquery($query); return TRUE; } function getList($condition="") { if(!empty($condition)) $condition = "WHERE ".$condition; $result = $this->db->aquery("SELECT * FROM users ".$condition." ORDER BY login"); $out = array(); foreach($result as $rec) $out[$rec['id']] = $rec; return $out; } function get($id) { $result = $this->db->aquery("SELECT * FROM users WHERE id='".$id."'"); return $result[0]; } function getByLogin($login) { $result = $this->db->aquery("SELECT * FROM users WHERE login='".$login."'"); return $result[0]; } function genPass() { if(func_num_args() == 1) $nb = func_get_arg(0); else $nb = 8; // on utilise certains chiffres : 1 = i, 5 = S, 6=b, 3=E, 9=G, 0=O $lettre = array(); $lettre[0] = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'D', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '9', '0', '6', '5', '1', '3'); $lettre[1] = array('a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y' , '1', '3', '0' ); $lettre[-1] = array('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Z', '5', '6', '9'); $retour = ""; $prec = 1; $precprec = -1; srand((double)microtime()*20001107); // pour genere la suite de lettre, on dit : si les deux lettres sonts // des consonnes (resp. des voyelles) on affiche des voyelles (resp, des consonnes). // si les lettres sont de type differents, on affiche une lettre de l'alphabet while(strlen($retour) < $nb) { $type = ($precprec + $prec)/2; $r = $lettre[$type][array_rand($lettre[$type], 1)]; $retour .= $r; $precprec = $prec; $prec = in_array($r, $lettre[-1]) - in_array($r, $lettre[1]); } return $retour; } function delete($id) { $this->db->uquery("DELETE FROM users WHERE id=\"".$id."\""); return TRUE; } function setCurrent($project) { $user = $_SESSION['userData']['id']; $this->db->uquery('UPDATE users SET currentproject="'.intval($project).'" WHERE id="'.intval($user).'";'); $_SESSION['userData']['currentproject'] = intval($project); return true; } } // User Input // v0.1.0 class userInput { function checkPassword($pass1,$pass2) { if($pass1 != $pass2) return "Les deux mots de passe entrés sont différents. Vous devez entrer deux fois le même mot de passe."; if(strlen($pass1) < 4) return "Le mot de passe est trop court. Merci d'entrer un mot de passe de minimum 4 caractères."; if(!ereg("^[a-zA-Z0-9]+$",$pass1)) return "Le mot de passe contient des caractères invalides. Caractères autorisés: A-Z, a-z, 0-9, _ et -."; return TRUE; } function checkLogin($login) { if(strlen($login) < 4) return "Nom d'utilisateur trop court. Merci d'utiliser un nom d'utilisateur de min. 4 caractères."; if(strlen($login) > 30) return "Nom d'utilisateur trop long. Merci d'entrer un nom d'utilisateur de max. 30 caractères."; if(!ereg("^[a-zA-Z0-9]+$",$login)) return "Le nom d'utilisateur utilise des caractères invalides. Caractères autorisés: A-Z, a-z, 0-9."; return TRUE; } function checkUnixname($name) { if(strlen($name) < 4) return "TOO_SHORT"; if(strlen($name) > 32) return "TOO_LONG"; if(!ereg("^[a-z0-9]+$",$name)) return "INVALID"; return TRUE; } function checkMail($mail) { if(!ereg('^([a-z0-9._-]+)@([a-z0-9]+[-.]?)+([a-z]{2,6})$',$mail)) return false; $dom = ereg_replace('^[^@]+@','',$mail); return true; } } function zerofill($var,$length) { while(strlen($var) < $length) $var = "0".$var; return $var; } ?>