MY_grouptable.'` ORDER BY `group` ASC';//alpha order } function listgroups(){ $q='SELECT `group` FROM `'.$this->MY_grouptable.'` ORDER BY `group` ASC'; $this->db->query($q); //display or none //none $gl=array(); if($this->db->num_rows()<1){ $gl[0]='No Groups'; }else{ //build rows $i=0; while($this->db->next_record()){ $gl[$i]=$this->db->Record['group']; $i++; } } return $gl; } function listactivegroups(){ $q='SELECT `group` FROM `'.$this->MY_grouptable.'` WHERE `active`=1 ORDER BY `group` ASC'; $this->db->query($q); //display or none //none $gl=array(); if($this->db->num_rows()>0){ //build rows $i=0; while($this->db->next_record()){ $gl[$i]=$this->db->Record['group']; $i++; } } return $gl; } function listpurgegroups(){ //all active groups that havn't been purged in a day $q='SELECT `group` FROM `'.$this->MY_grouptable.'` WHERE `active`=1 AND `purgeage`>0 AND `lastpurge`<(NOW()-INTERVAL 1 DAY) ORDER BY `group` ASC'; $this->db->query($q); //display or none //none $gl=array(); if($this->db->num_rows()>0){ //build rows $i=0; while($this->db->next_record()){ $gl[$i]=$this->db->Record['group']; $i++; } } return $gl; } function listpurgegroupsID(){ //all active groups that havn't been purged in a day $q='SELECT `group`,`key` FROM `'.$this->MY_grouptable.'` WHERE `active`=1 AND `purgeage`>0 AND `lastpurge`<(NOW()-INTERVAL 1 DAY) ORDER BY `group` ASC'; $this->db->query($q); //display or none //none $gl=array(); if($this->db->num_rows()>0){ //build rows $i=0; while($this->db->next_record()){ $gl[$i]['group']=$this->db->Record['group']; $gl[$i]['groupid']=$this->db->Record['key']; $i++; } } return $gl; } function ID2GROUP($groupid){ $q='SELECT `group` FROM `'.$this->MY_grouptable.'` WHERE `key`=\''.mysql_real_escape_string($groupid).'\' LIMIT 1'; $this->db->query($q); $this->db->next_record(); return $this->db->Record['group']; } function GROUP2ID($group){ $q='SELECT `key` FROM `'.$this->MY_grouptable.'` WHERE `group`=\''.mysql_real_escape_string($group).'\' LIMIT 1'; $this->db->query($q); $this->db->next_record(); return $this->db->Record['key']; } } class sqladmin extends sqlbase{ function listcrongroups(){ $q='SELECT `group`,`key`,`lastheaderid`,`lastscan` FROM `'.$this->MY_grouptable.'` WHERE `active`=1 ORDER BY `group` ASC'; $this->db->query($q); //display or none //none $gl=array(); if($this->db->num_rows()>0){ //build rows $i=0; while($this->db->next_record()){ //this could just be : //$gl[]=$this->db->Record; $gl[$i]['group']=$this->db->Record['group']; $gl[$i]['groupid']=$this->db->Record['key']; $gl[$i]['lastheaderid']=$this->db->Record['lastheaderid']; $gl[$i]['lastscan']=$this->db->Record['lastscan']; $i++; } } return $gl; } function setgroupactive($groupid,$active){ $q='UPDATE `'.$this->MY_grouptable."` SET `active`='$active' WHERE `key`='$groupid' LIMIT 1"; $this->db->query($q); } function cronlog($start,$stop,$log){ $q="INSERT INTO `".$this->MY_crontable."` (`start`,`stop`,`log`) VALUES ('$start','$stop','$log')"; $this->db->query($q); } function flushgroup($groupid){ $q="DELETE headers, collections, files FROM files INNER JOIN collections ON files.collid = collections.`key` INNER JOIN groups ON (collections.`group` = groups.`key`) INNER JOIN headers ON headers.fileid = files.`key` WHERE groups.`key` = '".mysql_real_escape_string($groupid)."'"; $this->db->query($q); $q='UPDATE `'.$this->MY_grouptable."` SET `lastheaderid`='0' WHERE `key`='$groupid' LIMIT 1"; $this->db->query($q); } function purgegroup($group){ //bibi-pov made this very nice purge SQL. //give it the group NAME (not ID) $q="DELETE headers, collections, files FROM files INNER JOIN collections ON files.collid = collections.`key` INNER JOIN groups ON (collections.`group` = groups.`key`and collections.`date` <(NOW() - INTERVAL groups.purgeage DAY)) INNER JOIN headers ON headers.fileid = files.`key` WHERE groups.`group` = '".mysql_real_escape_string($group)."'"; $this->db->query($q); } function purgegroupid($groupid){ //bibi-pov made this very nice purge SQL. //give it the group NAME (not ID) $q="DELETE headers, collections, files FROM files INNER JOIN collections ON files.collid = collections.`key` INNER JOIN groups ON (collections.`group` = groups.`key`and collections.`date` <(NOW() - INTERVAL groups.purgeage DAY)) INNER JOIN headers ON headers.fileid = files.`key` WHERE groups.`key` = '".mysql_real_escape_string($groupid)."'"; $this->db->query($q); } //a function to clean up orphan files and headers without a collection... function cleanuptables(){ $q="DELETE files.* FROM files LEFT JOIN collections ON files.`collid` = collections.`key` WHERE collections.`key` IS NULL"; $this->db->query($q); $q="DELETE headers.* FROM headers LEFT JOIN files ON headers.`fileid` = files.`key` WHERE files.`key` IS NULL"; $this->db->query($q); } function deletegroup($groupid){ $q='DELETE FROM `'.$this->MY_grouptable."` WHERE `key`='$groupid' LIMIT 1"; $this->db->query($q); } //GROUP STATISTICS function UPDATEAFTERPURGE($groupid){ $this->UPDATEGROUPSTATS($groupid); $q='UPDATE `'.$this->MY_grouptable."` SET `lastpurge`=now() WHERE `key`='$groupid' LIMIT 1"; $this->db->query($q); } function UPDATEGROUPSTATS($groupid){ //update last scan time, files, headers, etc. //the header count took far too many resources... /* $q="SELECT COUNT(`".$this->MY_hdrtable."`.`messageid`) AS count FROM `".$this->MY_filetable."` INNER JOIN `".$this->MY_hdrtable. "` ON `".$this->MY_filetable."`.`key` = `".$this->MY_hdrtable."`.fileid WHERE `group`='$groupid'"; $this->db->query($q); $this->db->next_record();*/ //$tot_headers = 0;//$this->db->Record['count']; $ft=$this->MY_filetable; $q="SELECT COUNT(`key`) as `count` FROM `$ft` WHERE `group`='$groupid'"; $this->db->query($q); $this->db->next_record(); $tot_files=$this->db->Record['count']; $q="SELECT SUM(`size`) as `size` FROM `".$this->MY_collectiontable."` WHERE `group`='$groupid'"; $this->db->query($q); $this->db->next_record(); $tot_size=round($this->db->Record['size']/1073741824); $q="SELECT MIN($ft.date) AS MIN_date, MAX($ft.date) AS MAX_date FROM `$ft` WHERE `group`='$groupid' GROUP BY `group`"; $this->db->query($q); $this->db->next_record(); $oldest_file=$this->db->Record['MIN_date']; $newest_file=$this->db->Record['MAX_date']; //lastscan newestfile oldestfile totalfiles totalheaders $q="UPDATE `".$this->MY_grouptable."` SET `lastscan`=now(), `newestfile`='$newest_file', `oldestfile`='$oldest_file', `totalfiles`='$tot_files' , `totalsize`='$tot_size' WHERE `key`='$groupid' LIMIT 1"; $this->db->query($q); } //SITE STATISTICS function checksitestatstable(){ $q='SELECT `key` FROM `'.$this->MY_sitestats.'` WHERE `key`=1'; $this->db->query($q); if($this->db->num_rows()==0){ $q='INSERT INTO `'.$this->MY_sitestats.'` (`key`)VALUES(\'1\')'; $this->db->query($q); } } function updatesitestats(){ $q='SHOW table STATUS'; $this->db->query($q); while($this->db->next_record()){ if($this->db->Record['Name']=='files'){ $fi_avelen=$this->db->Record['Avg_row_length']; $fi_total=$this->db->Record['Rows']; $fi_size=round(($this->db->Record['Data_length']+$this->db->Record['Index_length'])/1048576); } elseif($this->db->Record['Name']=='headers'){ $he_avelen=$this->db->Record['Avg_row_length']; $he_total=$this->db->Record['Rows']; $he_size=round(($this->db->Record['Data_length']+$this->db->Record['Index_length'])/1048576); } elseif($this->db->Record['Name']=='collections'){ $co_total=$this->db->Record['Rows']; $co_size=round(($this->db->Record['Data_length']+$this->db->Record['Index_length'])/1048576); } $totalsize+=$this->db->Record['Data_length']+$this->db->Record['Index_length']; } $totalsize=round($totalsize/1048576); $q="UPDATE `".$this->MY_sitestats."` SET `totalsize`='$totalsize', `fi_avelen`='$fi_avelen', `fi_total`='$fi_total', `fi_size`='$fi_size', `he_avelen`='$he_avelen', `he_total`='$he_total', `he_size`='$he_size', `co_total`='$co_total', `co_size`='$co_size' WHERE `key`=1"; $this->db->query($q); } function updateNFOstats($totalfiles,$totalsize){ $q="UPDATE `".$this->MY_sitestats."` SET `nfo_total`='$totalfiles', `nfo_size`='$totalsize' WHERE `key`=1"; $this->db->query($q); } function updateNFOdirinfo($dir){ $dir=$dir.'/'; $totalfiles=0; $totalsize=0; $handle = @opendir($dir); while ($file = @readdir ($handle)){ if (eregi("^\.{1,2}$",$file)) continue; $totalfiles++; $totalsize+=filesize($dir.$file); } @closedir($handle); $this->updateNFOstats($totalfiles,$totalsize); } function purgeNFOdir($dir){ $dir=$dir.'/'; $totalfiles=0; $totalsize=0; //get the oldest retention age $days=$GLOBALS['CONF_nfo']['retention']; $handle = @opendir($dir); while ($file = @readdir ($handle)){ if (!is_file($dir.$file)) continue; $moddays=((time()-filemtime($dir.$file))/(24*60*60)); if($days && $moddays>$days){//if false, still update the stats!!! unlink($dir.$file); }else{ $totalfiles++; $totalsize+=filesize($dir.$file); } } @closedir($handle); $this->updateNFOstats($totalfiles,$totalsize); } function getOldestRetention(){ $q="SELECT `purgeage` FROM `".$this->MY_grouptable."` WHERE `active`=1 AND `purgeage`>0 ORDER BY `purgeage` DESC LIMIT 1"; $this->db->query($q); $this->db->next_record(); //send false if no rows, else send the age. if($this->db->num_rows()==0){return false;} $days=$this->db->Record['purgeage']; } function setpurge($days,$groupid){ $q="UPDATE `".$this->MY_grouptable."` SET `purgeage`='".mysql_real_escape_string($days)."' WHERE `key`='".mysql_real_escape_string($groupid)."' LIMIT 1"; $this->db->query($q); } function shortgroup($group){ $shortgroup=str_replace('alt','a',$group); $shortgroup=str_replace('binaries','b',$shortgroup); return $shortgroup; } function addgroup($group){ $sg=$this->shortgroup($group); $q="INSERT INTO `".$this->MY_grouptable."` (`group`,`shortgroup`) VALUES ('".mysql_real_escape_string($group)."', '".mysql_real_escape_string($sg)."')"; $this->db->query($q); } function addgrouparray($gac){ $q="INSERT INTO `".$this->MY_grouptable."` (`group`,`shortgroup`,`purgeage`) VALUES "; foreach($gac as $key=>$g){ if($key>0){$q.=',';$i=$key;} $q.="('".mysql_real_escape_string($g['group'])."', '".mysql_real_escape_string($this->shortgroup($g['group']))."', '".mysql_real_escape_string($g['purgeage'])."')"; } $this->db->query($q) or die('

If you add more than 127 groups, be sure that you increase the data size of `key` (groups table) and `group` (collections and files table) to smallint or mediumint!!

'); } function toggleupdatelock($set,$groupid){ if($set){ $set=1; //toggle set $q="SELECT `updatelock` FROM `".$this->MY_grouptable."` WHERE `key`='$groupid' LIMIT 1"; $this->db->query($q); $this->db->next_record(); if($this->db->Record['updatelock']==1){ return false; } }else{ $set=0; } $q="UPDATE `".$this->MY_grouptable."` SET `updatelock`=$set, `lastscan`=now() WHERE `key`='$groupid' LIMIT 1"; $this->db->query($q); return true; } function savelastheaderid($groupid,$lastheaderid){ //added check to be sure that the ID is greater //this will cause problems if your news host resets the database message counter. $q="UPDATE `".$this->MY_grouptable."` SET `lastheaderid`='".mysql_real_escape_string($lastheaderid)."' WHERE `key`='".mysql_real_escape_string($groupid)."' AND `lastheaderid`<='".mysql_real_escape_string($lastheaderid)."' LIMIT 1"; $this->db->query($q); if($this->db->affected_rows()==0){return false;}else{return true;} } function lastheaderid($groupid){ $q='SELECT `lastheaderid` FROM `'.$this->MY_grouptable."` WHERE `key`='".mysql_real_escape_string($groupid)."' LIMIT 1"; $this->db->query($q); $this->db->next_record(); return $this->db->Record['lastheaderid']; } function logreject($subject){ $q="INSERT INTO `".$this->MY_rejectstable."` (`subject`) VALUES ('".mysql_real_escape_string($subject)."')"; $this->db->query($q); } //Header, file,collection cache SQL function updatefilefromcache($fua){ foreach($fua as $k=>$v){ $q='UPDATE `'.$this->MY_filetable.'` SET `parts`=`parts`+'.$v['parts'].', `size`=`size`+'.$v['bytes'].' WHERE `key`=\''.$k.'\' LIMIT 1'; $this->db->query($q); } } function updatecollfromcache($cua){ foreach($cua as $k=>$v){ $q='UPDATE `'.$this->MY_collectiontable.'` SET `parts`=`parts`+'.$v['parts'].', `size`=`size`+'.$v['bytes'].' WHERE `key`=\''.$k.'\' LIMIT 1'; $this->db->query($q); } } function addheaderfromcache(&$hc){ //reduced footprint header table $q="INSERT INTO `".$this->MY_hdrtable."` (`messageid` , `bytes`,`fileid`,`part` )VALUES "; $arr_count=count($hc); for($i=0;$i<$arr_count;$i++){ if($i>0){$q.=',';} $q.="('".mysql_real_escape_string($hc[$i]['messageid'])."', '".mysql_real_escape_string($hc[$i]['bytes'])."', '".mysql_real_escape_string($hc[$i]['fileid'])."', '".mysql_real_escape_string($hc[$i]['part'])."')"; } $this->db->query($q); } function addnfofromcache($nc){ $q="INSERT INTO `".$this->MY_nfotable."` (`messageid`,`collectionid`) VALUES "; $i=0; foreach($nc as $key=>$v){ if($i>0){$q.=',';} $q.="('".mysql_real_escape_string($v)."','$key')"; $i++; } $this->db->query($q); } function updateNFO($collectionid,$url,$gotbody){ $del_processed_NFO=true; if($gotbody){$nfo='1';}else{$nfo='0';} $q="UPDATE `".$this->MY_collectiontable."` SET `nfo_available`=$nfo,`details_url`='".mysql_real_escape_string($url)."' WHERE `key`='$collectionid' LIMIT 1"; $this->db->query($q); if($del_processed_NFO){ $q="DELETE FROM `".$this->MY_nfotable."` WHERE `collectionid`='$collectionid'"; }else{ $q="UPDATE `".$this->MY_nfotable."` SET `done`=1 WHERE `collectionid`='$collectionid'"; } $this->db->query($q); } function fetchNFOcache(){ $q="SELECT * FROM `".$this->MY_nfotable."` WHERE `done`=0 GROUP BY `collectionid`"; $this->db->query($q); if($this->db->num_rows()>0){ $nc=array(); while($this->db->next_record()){ $nc[$this->db->Record['collectionid']]=$this->db->Record['messageid']; } return $nc; } return false; } function collupdatenewfilefromcache(&$a){ foreach($a as $key=>$value){ $q="UPDATE `".$this->MY_collectiontable."` SET `parts`=`parts`+".$value['parts'].", `size`=`size`+".$value['size'].", `totalparts`=`totalparts`+".$value['totalparts']; foreach($value['filetypes']as $key2=>$value2){ $q.=",`".$key2."`=`".$key2."`+".$value2; } $q.=" WHERE `key`='".$key."' LIMIT 1"; $this->db->query($q); } } function matchtofile(&$groupid,&$xo){ //check for file name, sender, etc in file table $q='SELECT `key`,`collid` FROM `'.$this->MY_filetable.'` WHERE `group`=\''.mysql_real_escape_string($groupid).'\' AND `subject`=\''.mysql_real_escape_string($xo['fileinfo']['subjectstem']).'\' AND `sender`=\''.mysql_real_escape_string($xo['sender']).'\' AND `date` > (\''.$xo['date'].'\' - INTERVAL '.$GLOBALS['CONF_fileaddwindow'].' DAY)'; $this->db->query($q); if($this->db->nf()==0){ return false; }else{ /*$fileinfo=array( 'key'=>'', 'collid' => '', );*/ $this->db->next_record(); $fileinfo['key']=$this->db->Record['key']; $fileinfo['collid']=$this->db->Record['collid']; return $fileinfo; } } function addfile(&$groupid,&$xo){ //if none exist, create one, get insert ID $q="INSERT INTO `".$this->MY_filetable."` (`group`,`subject`, `sender`,`date`,`parts`,`totalparts`,`size`,`collid`) VALUES('". mysql_real_escape_string($groupid). "','".mysql_real_escape_string($xo['fileinfo']['subjectstem']). "','".mysql_real_escape_string($xo['sender']). "','".$xo['date']. "','1','".$xo['fileinfo']['totalparts']. "','".$xo['bytes']. "','".$xo['collectioninfo']['id']."')" ;//update the record with the file entry ID $this->db->query($q); $q="SELECT LAST_INSERT_ID() as `fileid`"; $this->db->query($q); $this->db->next_record(); return $this->db->Record['fileid']; } function matchtocollection(&$groupid,&$xo){ //check for file name, sender, etc in file table $q="SELECT `key`,`nfo_available` FROM `".$this->MY_collectiontable."` WHERE `group`='".mysql_real_escape_string($groupid)."' AND `stem`='".mysql_real_escape_string($xo['collectioninfo']['stem'])."' AND `poster`='".mysql_real_escape_string($xo['sender'])."' AND `date` > ('".$xo['date']."' - INTERVAL ".$GLOBALS['CONF_coladdwindow']." DAY)"; $this->db->query($q); if($this->db->nf()==0){ return false; }else{ $this->db->next_record(); $a['cid']=$this->db->Record['key']; $a['nfo']=$this->db->Record['nfo_available']; return $a; } } function getXREFid($xref){ $q="SELECT `key` FROM `xrefgroups` WHERE `group`='".mysql_real_escape_string($xref)."'"; $this->db->query($q); if($this->db->num_rows()>0){ $this->db->next_record(); return $this->db->Record['key']; }else{ $q="INSERT INTO `".$this->MY_XREFtable."` (`group`,`shortgroup`) VALUES ('".mysql_real_escape_string($xref). "','".mysql_real_escape_string($this->shortgroup($xref)). "')"; $this->db->query($q); $q="SELECT LAST_INSERT_ID() as `id`"; $this->db->query($q); $this->db->next_record(); return $this->db->Record['id']; } } function addcollection(&$groupid,&$xo){ //$group=$this->ID2GROUP($xo['groupid']); //get the alternate group IDs if($xo['ag1']){ $ag1=$this->getXREFid($xo['ag1']); //only need to to ag2 if we have an ag1 //but there is something wrong in parsexover prehaps? if($xo['ag2']){ $ag2=$this->getXREFid($xo['ag2']); } }/*else if($xo['ag2']){ $ag2=$this->getXREFid($xo['ag2']); }*/ $q="INSERT INTO `".$this->MY_collectiontable."` (`group`,`altgroup1`,`altgroup2`,`subject`,`date`, `stem`,`poster`,`parts`,`totalparts`, `size`,`".$xo['collectioninfo']['filetype']."`) VALUES('". mysql_real_escape_string($groupid). "','".$ag1. "','".$ag2. "','".mysql_real_escape_string($xo['subject']). "','".$xo['date']. "','".mysql_real_escape_string($xo['collectioninfo']['stem']). "','".mysql_real_escape_string($xo['sender']). "','1','".mysql_real_escape_string($xo['fileinfo']['totalparts']). "','".mysql_real_escape_string($xo['bytes']). "','1')"; $this->db->query($q); $q="SELECT LAST_INSERT_ID() as `collid`"; $this->db->query($q); $this->db->next_record(); return $this->db->Record['collid']; } }//class class sqluser extends sqlbase{ /* Now included in the NZB.php file function nzbfileheaders($value){ return 'SELECT * FROM `'.$this->MY_hdrtable."` WHERE `fileid`='$value'";// ORDER BY `part` ASC"; } */ function advancedsearch($query,$start,$results,$days,$sort,$order,$group,$minsize,$maxsize,$nfo,$collection){ $sq=mysql_real_escape_string($query); $q="SELECT c.*, g.`shortgroup`, MATCH(c.`subject`) AGAINST ('$sq' IN BOOLEAN MODE) AS score FROM ".$this->MY_collectiontable." AS c,". $this->MY_grouptable." as g WHERE MATCH(c.`subject`) AGAINST ('$sq' IN BOOLEAN MODE)"; if($group){$q.="AND g.`group`='".mysql_real_escape_string($group)."' ";} if($minsize){$q.="AND c.`size`>".mysql_real_escape_string($minsize).' ';} if($maxsize){$q.="AND c.`size`<".mysql_real_escape_string($maxsize).' ';} if($nfo){$q.="AND c.`nfo`>0 ";} if(is_numeric($days)){$q.='AND c.`date` > (NOW() - INTERVAL '.mysql_real_escape_string($days).' DAY)';} $q.="AND c.`group`=g.`key` ORDER BY c.`$sort` $order LIMIT ".mysql_real_escape_string($start).','.mysql_real_escape_string($results); return $q; } function search($query,$start,$results,$days,$sort,$order){ $sq=mysql_real_escape_string($query); //define the c fields needed $q="SELECT c.*, g.`shortgroup`, MATCH(c.`subject`) AGAINST ('$sq' IN BOOLEAN MODE) AS score FROM ".$this->MY_collectiontable." AS c,". $this->MY_grouptable." as g WHERE MATCH(c.`subject`) AGAINST ('$sq' IN BOOLEAN MODE) "; if(is_numeric($days)){$q.='AND c.`date` > (NOW() - INTERVAL '.mysql_real_escape_string($days).' DAY)';} $q.=" AND c.`group`=g.`key` ORDER BY c.`".mysql_real_escape_string($sort).'` '.mysql_real_escape_string($order).' LIMIT '.mysql_real_escape_string($start).','.mysql_real_escape_string($results); return $q; } function browsegroup($group,$s,$r){ /* We use a sliding record number indicator to save our place This is helpful because the user's place won't go all funny as new articles are added for the group! */ if(!(is_numeric($r)&&($r<101))){$r=100;} if(is_numeric($s)&&($s>0)){ return 'SELECT * FROM `'.$this->MY_collectiontable.'` WHERE `group`=\''.mysql_real_escape_string($group)."' AND `key`<$s ORDER BY `key` DESC LIMIT $r"; }else{ return 'SELECT * FROM `'.$this->MY_collectiontable.'` WHERE `group`=\''.mysql_real_escape_string($group)."' ORDER BY `key` DESC LIMIT $r"; } } function updatesitestats(){ $dir = "./nfo"; $size_in_bytes = disk_total_space($dir); return $size_in_bytes; } }//class /* case "normal": $sql = "SELECT mytable_id, mytable_title, mytable_caption, mytable_dts, MATCH(mytable_title, mytable_caption, mytable_full_body) AGAINST ('$searchstring') AS score FROM mytable WHERE MATCH(mytable_title, mytable_caption, mytable_full_body) AGAINST ('$searchstring') ORDER BY score DESC"; */ ?>