connect(); } function connect() { $this->link = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, true); mysql_set_charset("UTF8"); if (!$this->link) { die('Could not connect: ' . mysql_error()); } mysql_select_db(DB_NAME, $this->link); } function disconnect() { mysql_close(); } function query($query) { $ret = mysql_query($query, $this->link) or print(mysql_error()); return $ret; } function loadScript($scriptlocation) { if ($script = file_get_contents($scriptlocation)) { $script = preg_replace('/\-\-.*\n/', '', $script); $sql_statements = preg_split('/;[\n\r]+/', $script); foreach($sql_statements as $statement) { $statement = trim($statement); $statement = str_replace("prefix_",DB_PREFIX."_",$statement); if (!empty($statement)) { try { $result = $this->query($statement); } catch (DatabaseException $e) { die('Could not connect: ' . $e); } } } } else { die('Script not found: ' . $scriptlocation); } $this->setupAdminAccount(); } function setupAdminAccount() { $check_admin_account = mysql_fetch_array($this->query("SELECT uname FROM ".DB_PREFIX."_users WHERE uname='".ADMIN_USERNAME."'")); if(!is_array($check_admin_account) || $check_admin_account[0] != ADMIN_USERNAME) { $this->query("INSERT INTO ".DB_PREFIX."_users (uname, pwd, roles, approved) VALUES('".ADMIN_USERNAME."', '".ADMIN_PASSWORD."', '111111', True)"); $nui = mysql_insert_id(); $this->query("INSERT INTO ".DB_PREFIX."_userinfo (userid, firstname, lastname, email, language, sex, homepage) VALUES (".$nui.",'Tops', 'Admin', '', '".DEFAULT_LANGUAGE."', 'boy', '' )"); } } function getPages() { $ret = $this->query("SELECT * FROM ".DB_PREFIX."_pages"); $objs = array(); while ( $obj = mysql_fetch_array($ret) ) { array_push($objs, $obj); } return $objs; } function getTasks() { $ret = $this->query("SELECT * FROM ".DB_PREFIX."_types WHERE task=1"); $objs = array(); while ( $obj = mysql_fetch_array($ret) ) { array_push($objs, $obj); } $objs_trans = array(); foreach ($objs as $ob) { $ob['title'] = gettext($ob['title']); $objs_trans []= $ob; } $sorter = new array_sorter($objs_trans, 'title', 1); return $sorter->sortit(); } function getType($type) { $ret = $this->query("SELECT * FROM ".DB_PREFIX."_types where typeIdent='".$type."'"); return mysql_fetch_array($ret); } function getMenuElements($menu, $grp) { $sql = "SELECT * FROM ".DB_PREFIX."_menus WHERE grp='".$grp."' AND menu='".$menu."'"; $ret = $this->query($sql); $objs = array(); while ( $obj = mysql_fetch_array($ret) ) { array_push($objs, $obj); } return $objs; } function getResourceTypeByRID($rid) { if ($rid != "new") { $ret = $this->query("SELECT type FROM ".DB_PREFIX."_resources WHERE resourceID=".$rid); $row = mysql_fetch_array($ret); return $row[0]; } return false; } function getCommonData($rid) { $ret = $this->query("SELECT * FROM ".DB_PREFIX."_resources WHERE resourceID=".$rid); $row = mysql_fetch_array($ret); return $row; } function getTypeData($rid, $table) { $ret = $this->query("SELECT * FROM ".DB_PREFIX."_".$table." WHERE resourceID=".$rid); $row = mysql_fetch_array($ret); return $row; } /* * RESOURCES * */ private function getResourcesSQL($sql) { //print $sql; $ret = $this->query($sql); $objs = array(); $num_rows = mysql_num_rows($ret); $i = 0; while ( $row = mysql_fetch_array($ret) ) { $classname = $row['typeID']; _load_file($row['pluginID'], $classname); mysql_data_seek($ret, $i); $obj = mysql_fetch_object($ret, $classname, array($row['resourceID'])); array_push($objs, $obj); $i++; } return $objs; } function getResourcesByType($typ, $opts=array(), $allow_deleted=false) { $params = ""; if ($typ!="all") { $params .= " AND ".DB_PREFIX."_types.typeIdent='".$typ."'"; } foreach ($opts as $op => $val ) { $params .= " AND ".$op."=".$val; } $dele = " AND deleted=0"; if ($allow_deleted) { $dele = ""; } return $this->getResourcesSQL("SELECT * FROM ".DB_PREFIX."_resources JOIN ".DB_PREFIX."_types WHERE typeIdent=type".$dele.$params); } function getResourcesByTag($tag) { $keywords = ""; if ($tag && strcasecmp("_all_", $tag)) { $keywords = " AND keywords like '%,".$tag.",%'"; } return $this->getResourcesSQL("SELECT * FROM ".DB_PREFIX."_resources JOIN ".DB_PREFIX."_types WHERE typeIdent=type AND deleted=0 AND security='public' AND state='final'".$keywords); } function getResourcesByFTI($sword) { return $this->getResourcesSQL("SELECT * FROM ".DB_PREFIX."_resources JOIN ".DB_PREFIX."_types WHERE typeIdent=type AND deleted=0 AND security='public' AND state='final' AND resourceID IN (SELECT resourceID FROM ".DB_PREFIX."_fti WHERE MATCH(content) AGAINST ('".$sword."'))"); } function getResourcesByMD($mds) { $md_st = array(); $search = array(); foreach ($mds as $md_key => $md_val) { $md_st []= "LEFT JOIN ".$md_key." ON ".$md_key.".resourceID = ".DB_PREFIX."_resources.resourceID"; $sub_search = array(); foreach ($md_val as $val) { $sub_search []= $md_key."='".$val."'"; } if (count($sub_search)>1) { $search []= "(".join($sub_search, " OR ").")"; } else { $search [] = join($sub_search, ""); } } $search_s = join($search, " AND "); return $this->getResourcesSQL("SELECT * FROM ".DB_PREFIX."_resources left JOIN ".DB_PREFIX."_types on ".DB_PREFIX."_types.typeIdent=".DB_PREFIX."_resources.type ".join($md_st, " ")." WHERE deleted=0 AND security='public' AND state='final' AND ".$search_s); } /* * GROUPS * */ function getGroupsByUserId($uid){ $sql = "SELECT groupID FROM ".DB_PREFIX."_grouprelations WHERE userID='".$uid."'"; $ret = $this->query($sql); $groups = array(); while ( $groupI = mysql_fetch_array($ret) ) { $sql2 = "SELECT * FROM ".DB_PREFIX."_groups WHERE groupID='".$groupI['groupID']."'"; $ret2 = $this->query($sql2); $groups []= mysql_fetch_array($ret2); } return $groups; } function getGroupNameById($gid){ $sql = "SELECT groupName FROM ".DB_PREFIX."_groups WHERE groupID='".$gid."'"; $ret = $this->query($sql); while ( $res = mysql_fetch_array($ret) ) { return $res['groupName']; } return false; } function getGroupMemberListing($gid){ $sql = "SELECT userID FROM ".DB_PREFIX."_grouprelations WHERE groupID='".$gid."'"; $ret = $this->query($sql); $list = array(); while ( $res = mysql_fetch_array($ret) ) { $list []= $res[0]; } return $list; } } if ( !isset($tdb)) { $tdb = new TDB(); } ?>