define('MYSQL_HOST', 'localhost:3306');
define('MYSQL_USER', 'SCIEnce');
define('MYSQL_PASS', 'm@th;open');
define('MYSQL_DB', 'SCIEnce');
define('REQIP', $_SERVER["REMOTE_ADDR"] );
define('IS_ADMIN', preg_match('/^131\.155\.70\.8$/', REQIP) );
define('PAGE_OMDEFS', 'omdefs.php');
define('OMDB_DEBUG', false);
$IMGS_SUP = array(
'enc_perc' => 'blue.png',
'enc_yes' => 'blue_plus.png',
'enc_no' => 'blue_minus.png',
'dec_perc' => 'red.png',
'dec_yes' => 'red_plus.png',
'dec_no' => 'red_minus.png'
);
define('IMG_HEIGHT', '10');
define('IMG_MAX_WIDTH', '50');
function shorten_url($url, $maxlen = 40, $target='_blank') {
if (strlen($url) > $maxlen) {
$n1 = floor(($maxlen-3)*(3/4));
$n2 = floor(($maxlen-3)*(1/4));
$x = substr($url, 0, $n1).'...'.substr($url, -1, $n2);
} else {
$x = $url;
}
$r = '';
return $r;
}
class OMDB {
var $dbconn;
var $notes;
var $notecount;
var $notesrmap;
function OMDB() {
}
function Query($sql) {
if (!isset($this->dbconn)) {
$this->dbconn = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
mysql_select_db(MYSQL_DB, $this->dbconn);
}
if (OMDB_DEBUG) echo "SQL: $sql
";
return mysql_query($sql, $this->dbconn);
}
/*
* Straightforward (foot)notes
*/
function ResetNotes() {
$this->notes = '';
$this->notecount = 0;
$this->notesrmap = array();
}
function AddNote($txt) {
if ($txt == '') return false;
if (!($n = @$this->notesrmap[$txt])) {
$n = ++$this->notecount;
$this->notesrmap[$txt] = $n;
$this->notes .= $n.'. '.$txt.'
';
}
return $n;
}
function AddNoteURL($url) {
if ($url == '') return false;
if (!($n = @$this->notesrmap[$url])) {
$n = ++$this->notecount;
$this->notesrmap[$url] = $n;
$this->notes .= $n.'. '.shorten_url($url).'
';
}
return $n;
}
/*
* Getting data
*/
function GetQueryIntoArray($qry) {
$res = $this->Query($qry);
$r = array();
while ($row = mysql_fetch_array($res)) array_push($r, $row);
return $r;
}
function GetCDs() {
return $this->GetQueryIntoArray('SELECT * FROM om_cds ORDER BY LOWER(cd_name);');
}
function GetCD($cd_id) {
$t = $this->GetQueryIntoArray('SELECT * FROM om_cds WHERE cd_id='.((int) $cd_id));
if (sizeof($t) == 0) return false;
return $t[0];
}
function GetDefs($cd_id) {
return $this->GetQueryIntoArray('SELECT * FROM om_defs WHERE cd_id='.((int) $cd_id).' ORDER BY LOWER(def_name)');
}
function GetCass($cd_id = -1) {
if ($cd_id == -1) {
return $this->GetQueryIntoArray('SELECT * FROM om_cass ORDER BY LOWER(cas_name)');
} else {
return $this->GetQueryIntoArray('SELECT c.*, cc.refurl, cc.demourl FROM om_cass c INNER JOIN om_cd_cas cc ON c.cas_id = cc.cas_id WHERE cd_id = '.((int) $cd_id).' ORDER BY LOWER(cas_name)');
}
}
function CreateCasEmptyAndFirstRow(&$empty_row, &$first_row, &$cass_col_index, $cd_id = -1) {
$cass = $this->GetCass($cd_id);
$empty_row = array('');
$first_row = array('');
$cass_col_index = array();
foreach($cass as $cas) {
array_push($first_row, array('cas_name' => $cas['cas_name'], 'cas_id' => $cas['cas_id'], 'refurl' => stripslashes(@$cas['refurl']), 'demourl' => stripslashes(@$cas['demourl']) ));
array_push($empty_row, '');
$cass_col_index[$cas['cas_id']] = sizeof($empty_row) - 1;
}
}
function GetCDsWithSupport() {
//Since MySQL has no crosstab capabilities, we DIO (Do It Ourselves).
$this->CreateCasEmptyAndFirstRow($empty_row, $first_row, $cass_col_index);
$res = $this->Query('
select c.cd_name, c.cd_id, cc.cas_id, cc.cnt_tot, cc.cnt_sup_enc, cc.cnt_sup_dec
from om_cds c left join om_cd_cas_with_count cc on c.cd_id = cc.cd_id
ORDER BY LOWER(cd_name), cd_id;
');
$rettable = array($first_row);
$rw = $empty_row;
$cd_id = -1;
while ($row = mysql_fetch_array($res)) {
if ($cd_id == -1 || $cd_id != $row['cd_id']) {
if ($cd_id != -1) array_push($rettable, $rw);
$rw = $empty_row;
$rw[0] = array('cd_name' => $row['cd_name'], 'cd_id' => $row['cd_id'] );
$cd_id = $row['cd_id'];
}
if ($row['cnt_tot'] != 0) {
$rw[$cass_col_index[$row['cas_id']]] = $row;
}
}
array_push($rettable, $rw);
return $rettable;
}
function GetDefsWithSupport($cd_id) {
//Again, we do the crosstab ourselves.
$this->CreateCasEmptyAndFirstRow($empty_row, $first_row, $cass_col_index, $cd_id);
$res = $this->Query('
select d.def_id, d.def_name, cc.cas_id, dc.support_enc, dc.support_dec, dc.note
from om_defs d
left join om_cd_cas cc on d.cd_id = cc.cd_id
left join om_def_cas dc on d.def_id = dc.def_id and cc.cas_id = dc.cas_id
where d.cd_id='.((int) $cd_id).'
order by lower(def_name);
');
$rettable = array($first_row);
$rw = $empty_row;
$def_id = -1;
while ($row = mysql_fetch_array($res)) {
if ($def_id == -1 || $def_id != $row['def_id']) {
if ($def_id != -1) array_push($rettable, $rw);
$rw = $empty_row;
$rw[0] = array( 'def_name' => $row['def_name'], 'def_id' => $row['def_id'] );
$def_id = $row['def_id'];
}
if ($row['cas_id'] == '') continue; //This is the no-support-at-all case
$rw[$cass_col_index[$row['cas_id']]] = array('enc' => $row['support_enc'], 'dec' => $row['support_dec'], 'note' => @$row['note']);
}
array_push($rettable, $rw);
return $rettable;
}
/*
* Getting formatting data
*/
function PrintSupportImgs($tot, $enc, $dec) {
global $IMGS_SUP;
$r = '';
$tot = (int) $tot;
if ($tot == 0) return '';
$fracs = array('enc' => round(100*($enc / $tot)), 'dec' => round(100*($dec / $tot)));
foreach($fracs as $m => $v) {
$r .= '';
$r .= $v.'%';
$r .= '
';
}
return $r;
}
function PrintCDsWithSupport($show_admin = IS_ADMIN) {
$data = $this->GetCdsWithSupport();
$r = '
'.$c['cd_name'].' | '; } else if ($i == 0) { //First row, print names of CASs $r .= "".(is_array($c) ? $c['cas_name'] : $c)." | "; } else if ($c == '') { $r .= ''; } else if ($c != '') { //Other columns, is support fraction -> draw some sort of graph $r .= ' | '; $r .= $this->PrintSupportImgs($c['cnt_tot'], $c['cnt_sup_enc'], $c['cnt_sup_dec']); $r .= ' | '; } } if ($show_admin && $i != 0) $r .= 'Add CAS... | '; $r .= '