<?

$header=array();
$header['new']="New user";
$header['edit']="Edit user";
$header['remove']="Remove user";
$header['browse']="Browse users";
$header['ins']="Browse users";
$header['upd']="Browse users";
$header['del']="Browse users";
$header['search']="Search users";
$header=array();

if (!useringroup(array('administrator'))) {
$finish['upd']='auth_user_view($id);';
}

function getauth_userform($auth_userid,$action,$name,$params="") {
  global $conn, $SQL_SYND_DB;
  if (empty($auth_userid)) {
    $row['userid']=0;
    $row['username']='';
    $row['password']='';
  } else {
    $sql="select * from auth_users where userid=$auth_userid";
    $result=sqlexec($sql);
    $row=sqlgetrow($result);
    if (empty($row['password'])) {
      $row['username']=strtolower($row['name']);
    }
  }
  $form=new form(geturl("interface.php3?obj=auth_user&id=" . $auth_userid . "&action=" .$action.'&' . $params),"POST");
  $form->conn=$conn;  
  $form->setgroup(array('public'));  
  if (isset($row['userid'])) {
    $form->addcontrol('HIDNUM',$prepend.'userid',$row['userid']);
  }
  $form->addcontrol('editnote','','<center><h2>Username and passwords are 
case-sensitive, please remember how you typed them in. 
</h2><h4>(Special thanks to Zeno for the extended 
descriptions)</h4></center>');
  $form->addcontrol('text','username',$row['username'],'Username (Just for Logging In):','',16);
  $form->addcontrol('password','password',$row['password'],'Password:','[[:alnum:]]+',16);
  $form->addcontrol('text','name',$row['name'],'Name (used for comments and artist credit):','',64);
  $form->addcontrol('text','email',$row['email'],'Email:','',255);
  $form->addcontrol('text','homepage',$row['homepage'],'Homepage:','',255);
  $form->addcontrol('date','birthday',$row['birthday'],'Birthday (Used to wish you a happy birthday on the board):','',64);
  $form->addcontrol('select','sortpref','','Default Sorting Preferences:');
  $form->addoption('Newest Picture',0,$row['sortpref']==0?1:0);
  $form->addoption('Newest Comment',1,$row['sortpref']==1?1:0);
  $form->addcontrol('select','rpp','','Pictures per page:');
  $form->addoption('5',5,$row['rpp']==5?1:0);
  $form->addoption('10',10,$row['rpp']==10?1:0);
  $form->addoption('15',15,$row['rpp']==15?1:0);
  $form->addoption('20',20,$row['rpp']==20?1:0);
  $form->addoption('25',25,$row['rpp']==25?1:0);
  $form->addcontrol('chexbox','anim',$row['anim'],'Default to save animation?');
  $form->addcontrol('select','profileid','','Profile Picture:');
  $res=sqlexec("select * from pictures where userid=$auth_userid and stop!='0'");
  $form->addsqloptions($res,'pictureid',array('title','description'),$row['profileid']);
  $form->addcontrol('select','appletversion','string','Applet Version:');
  $applets=getapplets();
  while(list($file,$name)=each($applets)) {
    $form->addoption($name,$file,$row['appletversion']==$file?1:0);
  }
  $form->addcontrol("htmledit",'sig',$row['sig'],"Signature (Limit 255 Characters):",'',"rows=5 cols=50");
  $form->addcontrol("SUBMIT","submit",$name);
  return $form;
}

function auth_user_apdform($form,$userid) {
global $action,$filename;
global $hostsiteid;
$value=array();
if ($userid) {
  $res=sqlexec("select cf.name,ua.value from customfields cf, useranswers ua where
  ua.userid=$userid and ua.customfieldid=cf.customfieldid");
  while($row=sqlgetrow($res)) {
    echo "Got ".$row['name']." = ".$row['value']."<Br>\n";
    $value[$row['name']]=$row['value'];
  }
}
$res=sqlexec("select name,type from customfields where
siteid='$hostsiteid'order by ordernum");
while($row=sqlgetrow($res)) {
  $okname=strtolower(eregi_replace(' ','',$row['name']));
  $form->addcontrol($row['type'],$okname,$value[$row['name']],ucfirst($row['name']).':','',40);
}
return $form;
}

function auth_user_apdproc($userid) {
global $hostsiteid;
$form=getauth_userform(0,'','');
$form=auth_user_apdform($form,0);
sqlexec("delete from useranswers where userid=$userid");
$res=sqlexec("select * from customfields where
siteid='$hostsiteid'order by ordernum");
while($row=sqlgetrow($res)) {
  $okname=strtolower(eregi_replace(' ','',$row['name']));
  $cnt=$form->findcontrolnum($okname,array('public')); 
  if ($cnt>0) {
    list($val,$retype)=$form->accept_control($cnt,array('public'));
  } else {
    $val='';
  }
  sqlexec("insert into useranswers (userid,customfieldid,value) values
($userid,".$row['customfieldid'].",'$val')");
}
}

function auth_user_debug() {
	$form=getauth_userform(0,'','');
	$form->debugform();
}

function auth_user_del($id) {
	sqlexec("delete from auth_users where userid=$id");
	log_transac("auth_user","remove",$id,"");
}

function auth_user_new($params="") {
	$form=getauth_userform(0,'ins','',$params);
#	unset($form->controls[$form->control]);
        $form->control--;
#        $form=auth_user_apdform($form,0);
        $form->addcontrol("SUBMIT","submit",'Add User');
	echo $form->generate("edit",array('public'));
}

function auth_user_upd($id) {
	$form=getauth_userform($id,'ins','Update User');
	$check=$form->check_results(array('public'));
	if ($check != "") {
		echo 'You left out ' . $check;
	exit;
	}
	$form->update_table("auth_users","userid='$id'",array('public'));
#        auth_user_apdproc($id);
	log_transac("auth_user","edit",$id,"");
}

function auth_user_browse() {
  $result=sqlexec("select * from auth_users");
auth_user_display($result);
}

function auth_user_showsignups() {
$res=sqlexec("select au.* from auth_users au LEFT JOIN
auth_members am on
au.userid=am.userid where am.groupid=0");
auth_user_display($res);
}

function auth_user_display($result='') {
global $filename;
if ($result=='') {
  $result=sqlexec("select * from auth_users");
}
  echo '<A HREF="'.geturl('interface.php3?obj=auth_user&action=new'). '">Add New User</A><br>';
#  $result=sqlexec("select * from auth_users");
  echo '<table border=3><tr>';
  $form=getauth_userform(0,'','');
  echo $form->showfields(array('public'));
	$end = $start+25;
	if (empty($start)) { $start = 0; };
	for ($count = 0; $count < $start; $count++) { sqlgetrow($result); };
	for ($count = $start; (($count <= $end) && ($row=sqlgetrow($result))); $count++) {
	        if ($row['username'] == 'anonymous')  { continue; }
		$form=getauth_userform($row['userid'],'','');
		$form->addmethod($filename . '?obj=auth_user&action=edit&id=', 'user info', 'userid',2);
		$form->addmethod($filename . '?obj=auth_member&action=new&userid=', 'add group', 'userid',2);
		$form->addmethod($filename . '?obj=auth_member&action=search&userid=', 'groups', 'userid',2);		
                $form->addmethod($filename . '?obj=auth_user&action=remove&id=', 'remove record', 'userid',2);

		echo '<tr>';
		echo $form->showdata(array('public'));
		echo '</td></tr>';
	}
	echo '</table>';
	$back = $start-25;
	if ( $start == 0  && $count < $end) { 
	} elseif ($start == 0 ) {
		echo '<center><a href="interface.php3?obj=auth_user&start=' . $end . '&' . geturlparams() . '"> Next 25 </A></center>';
	} elseif ( $count < $end )  {
		echo '<center><a href="interface.php3?obj=auth_user&start=' . $back . '&'. geturlparams() . '">Prev 25</A></center>';
	} else {
	echo '<center><a href=interface.php3?obj=auth_user&start=' . $back . '&'. geturlparams() . '">Prev 25</A> | <a href="' . $filename . '?start=' . $end . '&' . geturlparams() .'">Next 25</A></center>';
	}
}

function auth_user_edit($id,$params="") {
	$form=getauth_userform($id,'upd','Update User',$params);
	$form->addcontrol('HIDDEN','id',$id);
#        $form=auth_user_apdform($form,$id);
	echo $form->generate("edit",array('public'));
}

function auth_user_ins($id) {
	$form=getauth_userform(0,'ins','Update User');
	$check=$form->check_results(array('public'));
	if ($check != "") {
		echo 'You left out ' . $check;
	exit;
	}
	$id=$form->insert_results('auth_users',array('public'));
	log_transac("auth_user","inserted",$id,"");
#        auth_user_apdproc($id);
	return $id;
}

function auth_user_view($id) {
	$form=getauth_userform($id,'','');
	echo $form->generate("view",array('public'));
}

function auth_user_remove($id) {
	$form=getauth_userform($id,'','');
	echo $form->generate("view",array('public'));
	echo '<br><br><center><b>Are you sure you would like to remove this record?<b><br><br>';
	echo '<table><tr><td>';
	echo '<FORM ACTION="'.geturl('interface.php3?obj=auth_user&action=del&id=' . $id) . '" METHOD="POST">';
	echo '<INPUT type="submit" value="Yes">';
	echo '</form></td><td>';
	echo '<FORM ACTION="'.geturl('interface.php3?obj=auth_user&action=browse').'" METHOD="POST">';
	echo '<INPUT type="submit" value="No">';
	echo '</form>';
	echo '</td></tr></table></center>';
}


function auth_user_search() {
global $rpp,$strict,$ses;
if (empty($strict)) { $strict=0; }
        $form=getauth_userform(0,'','');
$result=$form->get_search_results('auth_users',array('public'),'',$strict);
        $oldrpp=$rpp; $rpp=0;
        $sql=getsqlq();
        $ses->set_var(getauthses(),'auth_user_query',$sql);
        auth_user_display($result);
        $rpp=$oldrpp;
}

// Not Used
function auth_user_profile($id) {
global $ses,$auth_sid;
include "interfaces/picture.php3";
$res=sqlexec("select * from pictures p,auth_users a where p.name='$id' and p.userid=a.userid");
$row=sqlgetrow($res);
echo "<center>Profile for [".$row['name']."]</center><br>\n";
//echo '<h1>PROFILE</h1>';
//var_dump($row);
if ($row['profileid']) {
picture_view($row['profileid']);
}
//echo "userid[".$row['userid']."]<bR>\n";
echo '<center>';
$res=sqlexec("select count(distinct p.pictureid) 
from pictures p, histograms h 
where 
p.userid=".$row['userid']." and p.pictureid=h.pictureid");
list($pcnt)=sqlgetrow($res);
echo "Have $pcnt pictures in the analyzation database. Based on this 
pictures:<br>\n";
$res=sqlexec("select hexcolor,sum(pixels) as spix 
from pictures p, histograms h 
where p.userid=".$row['userid']." and
p.pictureid=h.pictureid
group by hexcolor order by spix DESC limit 25");
echo "<table><tr><td>";
echo $row['name']."'s top 25 favorite colors (by pixel): <br>";
while(list($hx,$spx)=sqlgetrow($res)) {
echo "<table><tr><td>";
percentagebar(100,$hx); echo "</td><td>$hx ($spx)<br>";
echo "</td></tr></table>";
}
echo '</td><td>';
$res=sqlexec("select hexcolor,count(p.pictureid) as spix 
from histograms h,pictures p
where p.userid=".$row['userid']." and
p.pictureid=h.pictureid
group by hexcolor order by spix DESC limit 25");
echo $row['name']."'s top 25 favorite colors (by picture): <br>";
while(list($hx,$spx)=sqlgetrow($res)) {
echo "<table><tr><td>";
percentagebar(100,$hx);
echo "</td><td>$hx ($spx)<br>";
echo "</td></tr></table>";
}
echo "</td></tr></table>";
echo '</center>';
$sql="select * from pictures where userid=".$row['userid']." and stop!='0' and anonymous=0";
$ses->set_var($auth_sid,'picture_query',$sql);
picture_display();
}

function auth_user_set($id) {
global $myauth,$val,$type,$table;
$uid=$myauth['id'];
$id=(int)$id;
$obj=$table;
if (empty($obj)) $obj='picture';
//echo "uid[$uid] type[$type] obj[$obj] objid[$id] val[$val]<br>\n";
  if ($val) {
    $res=sqlexec("replace into app_seen (userid,type,obj,objid) values ($uid,$type,'$obj',$id)");
  } else {
    $res=sqlexec("delete from app_seen where userid=$uid and type=$type and obj='$obj' and objid=$id");
  }
auth_user_showfavs();
}

function auth_user_showfavs() {
global $myauth;
include "interfaces/picture.php3";
$seen_data=getuserflagcache();
//print_r($seen_data[1]);
if (count($seen_data[1]['user'])) {
echo "Favorite artists:<br>\n";
echo 
'<table><tr><th>Artist</th><th>Seen</th><th>Total</th><th>Remove</th></tr>';
while(list($k,$v)=each($seen_data[1]['user'])) {
//  echo "k[$k] v[$v]<br>\n";
  $res=sqlexec("select * from auth_users where userid=$k");
  $row=sqlgetrow($res);
  if ($db[$row['name']]) continue;
    $db[$row['name']]=1;
  $res=sqlexec("select count(*) from pictures where 
name='".$row['name']."' and 
stop!='0000-00-00' and notfinished=0 and anonymous=0 and flood=0");
  list($cnt)=sqlgetrow($res);
if ($cnt) {
  $res=sqlexec("select count(*) from pictures p, app_seen s where 
s.obj='picture' and s.type=0 and s.objid=p.pictureid and 
s.userid=".$myauth['id']." and p.name='".$row['name']."' and 
p.stop!='0000-00-00' and notfinished=0 and anonymous=0 and flood=0");
  list($scnt)=sqlgetrow($res);
  echo "<tr><td><a 
href=\"".geturl('interface.php3?obj=picture&action=search&strict=1&name='.$row['name'])."\">".$row['name']."</A></td><td> 
$scnt</td><td>$cnt</td><td><A 
HREF=\"".geturl('interface.php3?obj=auth_user&action=set&table=user&type=1&val=0&id='.$k)."\" 
class=\"info\" title=\"remove favorite\">&nbsp;-&nbsp;</A></td></tr>\n";
}
}
echo '</table>';
}

if (count($seen_data[1]['picture'])) {
echo "Favorite pictures:<br>\n";
//echo '<table><tr><th>Artist</th><th>Seen</th><th>Total</th><th>Remove</th></tr>';
while(list($k,$v)=each($seen_data[1]['picture'])) {
picture_view($k);
}
//echo '</table>';
}

}

?>
