<?php
//===========================================================================
//* --    ~~                Crafty Syntax Live Help                ~~    -- *
//===========================================================================
//           URL:   http://www.craftysyntax.com/    EMAIL: eric@craftysyntax.com
//         Copyright (C) 2003-2005 Eric Gerdes   (http://www.craftysyntax.com )
// --------------------------------------------------------------------------
// $              CVS will be released with version 3.1.0                   $
// $    Please check http://www.craftysyntax.com/ or REGISTER your program for updates  $
// --------------------------------------------------------------------------
// NOTICE: Do NOT remove the copyright and/or license information any files. 
//         doing so will automatically terminate your rights to use program.
//         If you change the program you MUST clause your changes and note
//         that the original program is Crafty Syntax Live help or you will 
//         also be terminating your rights to use program and any segment 
//         of it.        
// --------------------------------------------------------------------------
// LICENSE:
//     This program is free software; you can redistribute it and/or
//     modify it under the terms of the GNU General Public License
//     as published by the Free Software Foundation; 
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
//
//     You should have received a copy of the GNU General Public License
//     along with this program in a file named LICENSE.txt .
// --------------------------------------------------------------------------  
// BIG NOTE:
//     At the time of the release of this version of CSLH, Version 3.1.0 
//     which is a more modular, extendable , “skinable” version of CSLH
//     was being developed.. please visit http://www.craftysyntax.com to see if it was released! 
//===========================================================================
require_once("admin_common.php");
validate_session($identity);

// get the info of this user.. 
$query = "SELECT * FROM livehelp_users WHERE sessionid='".$identity['SESSIONID']."'";	
$people = $mydatabase->query($query);
$people = $people->fetchRow(DB_FETCHMODE_ASSOC);
$myid = $people['user_id'];
$channel = $people['onchannel'];
$isadminsetting = $people['isadmin'];

$lastaction = date("Ymdhis");
$startdate =  date("Ymd");

$bgcolor = "";
if(!(isset($UNTRUSTED['action']))){ $UNTRUSTED['action'] = ""; }
if(!(isset($UNTRUSTED['addmenu']))){ $UNTRUSTED['addmenu'] = ""; }
if(!(isset($UNTRUSTED['editit']))){ $UNTRUSTED['editit'] = ""; }
if(!(isset($UNTRUSTED['remove']))){ $UNTRUSTED['remove'] = ""; }
if(!(isset($UNTRUSTED['isadmin']))){ $UNTRUSTED['isadmin'] = "N"; }

 
if($UNTRUSTED['action'] == "addnew"){

 // just a little error checking..
 $query = "SELECT * FROM livehelp_users WHERE username='".filter_sql($UNTRUSTED['newuser']) . "'";
 $data = $mydatabase->query($query);
 if($data->numrows() != 0){
   print "<font color=990000>" . $lang['error2'];
   exit;
 }
 if(empty($UNTRUSTED['newpass'])){
   print "<font color=990000>" . $lang['error3'];
   exit; 
 }
 
 $md5string = strtolower(MD5(date("MdHis")));
 $query = "INSERT INTO livehelp_users (username,password,isoperator,isadmin,isnamed,email,user_alert,show_arrival,identity,greeting,showtype) 
           VALUES ('".filter_sql($UNTRUSTED['newuser'])."','".filter_sql($UNTRUSTED['newpass'])."','Y','".filter_sql($UNTRUSTED['isadmin'])."','Y','".filter_sql($UNTRUSTED['email'])."','".filter_sql($UNTRUSTED['user_alert'])."','".filter_sql($UNTRUSTED['show_arrival'])."','$md5string','".filter_sql($UNTRUSTED['greeting'])."','".filter_sql($UNTRUSTED['showtyping'])."')";	
 $user_id = $mydatabase->insert($query);

 $query = "UPDATE livehelp_users set onchannel=user_id where isadmin='Y'";	
 $mydatabase->query($query);
  
 $query = "SELECT * FROM livehelp_users ORDER BY user_id DESC LIMIT 1";
 $channel_a = $mydatabase->query($query);
 $channel_a = $channel_a->fetchRow(DB_FETCHMODE_ASSOC);
 $user_id = $channel_a['user_id'];
 $found = 0;
    	
  $query = "SELECT * FROM livehelp_departments";
  $data = $mydatabase->query($query);
  while($row = $data->fetchRow(DB_FETCHMODE_ASSOC)){
    $varname = "mydepartment_" . $row['recno']; 
    if(isset($UNTRUSTED[$varname])) {  
    	$found = 1;
      $query = "INSERT INTO livehelp_operator_departments (user_id,department) VALUES (".intval($user_id)."," . intval($row['recno']) . ")";
      $mydatabase->query($query);
    }	
  }	
  if($found == 0){
   print "<font color=990000>" . $lang['txt101'] . "</font>";	
  }
}
if(!(empty($UNTRUSTED['remove']))){
  $query = "DELETE FROM livehelp_users WHERE user_id=". intval($UNTRUSTED['remove']);	
  $mydatabase->query($query);
  $query = "DELETE FROM livehelp_operator_departments WHERE user_id=". intval($UNTRUSTED['remove']);	
  $mydatabase->query($query);
}

if($UNTRUSTED['action'] == "updateuser"){
    $query = "UPDATE livehelp_users 
                SET showtype='".filter_sql($UNTRUSTED['showtyping'])."',
                  photo='".filter_sql($UNTRUSTED['photo'])."',
                  greeting='".filter_sql($UNTRUSTED['greeting'])."',
                  user_alert='".filter_sql($UNTRUSTED['user_alert'])."',
                  show_arrival='".filter_sql($UNTRUSTED['show_arrival'])."',
                  username='".filter_sql($UNTRUSTED['newuser'])."',
                  password='".filter_sql($UNTRUSTED['newpass'])."',
                  isadmin='".filter_sql($UNTRUSTED['isadmin'])."',
                  email='".filter_sql($UNTRUSTED['email'])."' 
                WHERE user_id=". intval($UNTRUSTED['who']);
    $mydatabase->query($query);
    $query = "DELETE FROM livehelp_operator_departments WHERE user_id=". intval($UNTRUSTED['who']);
    $mydatabase->query($query);

  $query = "SELECT * FROM livehelp_departments";
  $data = $mydatabase->query($query);
  while ($row = $data->fetchRow(DB_FETCHMODE_ASSOC)){
    $varname = "mydepartment_" . $row['recno']; 
    if(!(empty($UNTRUSTED[$varname]))) {  
        $query = "INSERT INTO livehelp_operator_departments (user_id,department) 
                  VALUES (".intval($UNTRUSTED['who'])."," . intval($row['recno']) . ")";	
        $mydatabase->query($query);
    }	
  }
    $mydatabase->close_connect();
    sleep(1);
    Header("Location: operators.php");
    exit;  
}
?>
<link title="new" rel="stylesheet" href="style.css" type="text/css">
  <meta http-equiv="Content-Type" content="text/html; charset=<?php echo $lang['charset']; ?>" >
<body bgcolor=<?php echo $color_background;?>><center>
<?php if ( ($UNTRUSTED['addmenu'] != "Y" ) && ($UNTRUSTED['editit'] == "") ){ ?>
<table border=0>
<tr bgcolor=FFFFFF><td><b><?php echo $lang['txt184']; ?></b></td><td><b><?php echo $lang['dept']; ?></b></td><td><b>Admin</b></td><td><b>e-mail</b></td><td><b><?php echo $lang['options']; ?></b></td></tr>
<?php 
$query = "SELECT * FROM livehelp_users WHERE isoperator='Y' ";
$data = $mydatabase->query($query);
while($row = $data->fetchRow(DB_FETCHMODE_ASSOC)){
   if($bgcolor=="$color_alt2"){ $bgcolor="$color_alt1"; } else { $bgcolor="$color_alt2"; }
 print "<tr bgcolor=$bgcolor><td>" . $row['username'] . "</td><td>";
 $query = "SELECT * 
           FROM livehelp_users,livehelp_operator_departments 
           WHERE livehelp_users.user_id=livehelp_operator_departments.user_id 
             AND livehelp_users.user_id=" . intval($row['user_id']);
 $data2 = $mydatabase->query($query);
 while($dept = $data2->fetchRow(DB_FETCHMODE_ASSOC)){	
  $query = "SELECT * 
            FROM livehelp_departments 
            WHERE recno=" . intval($dept['department']);
  $data3 = $mydatabase->query($query);
  $data3 =  $data3->fetchRow(DB_FETCHMODE_ASSOC); 
  print $data3['nameof'] . "<br>";
 }
 print "</td><td>" . $row['isadmin'] . "</td><td>" . $row['email'] . "</td><td>";
 if( ($isadminsetting == "Y") || ($myid == $row['user_id']) ) { print "<a href=operators.php?editit=" . $row['user_id'] . ">".$lang['EDIT']."</a> "; }
 if( ($isadminsetting == "Y") && ($row['user_id'] != $myid) ){ print "<a href=operators.php?remove=" . $row['user_id'] . " onClick=\"return confirm('".str_replace("'","\\'",$lang['areyousure'])."')\"><font color=990000>".$lang['REMOVE']."</font> "; }
 print "</a>  ";
 if( ($isadminsetting == "Y") || ($myid == $row['user_id']) ) { print "  <a href=operators_history.php?user_id=" . $row['user_id'] . ">".$lang['HISTORY']."</a> "; }

 print "</td></tr>\n"; 	
}
?>
</table>
<?php 
if($isadminsetting == "Y"){	
  print "<a href=operators.php?addmenu=Y>" . $lang['CREATE'] . "  " . $lang['operators'] . "</a>";
 }
}
if( ($isadminsetting == "Y") && ( ($UNTRUSTED['addmenu'] == "Y" ) || (!(empty($UNTRUSTED['editit']))) ) ){ 
?>
<br><table width=600><tr><td>
<form action=operators.php name=operatorform method=post>
<?php 
if(!(empty($UNTRUSTED['editit']))){
 $query = "SELECT * FROM livehelp_users where user_id=". intval($UNTRUSTED['editit']);
 $data = $mydatabase->query($query);
 $userinfo = $data->fetchRow(DB_FETCHMODE_ASSOC);	
?>
<input type=hidden name=action value=updateuser>
<input type=hidden name=who value=<?php echo $UNTRUSTED['editit']; ?> >
<table bgcolor=<?php echo $color_alt1;?>>
<tr><td bgcolor=FFFFFF colspan=2><?php echo $lang['UPDATE']; ?> <?php echo $lang['operators']; ?>  </td></tr>
<?php } else { ?>
<input type=hidden name=action value=addnew>
<table bgcolor=<?php echo $color_alt1;?>>
<tr><td bgcolor=FFFFFF colspan=2><?php echo $lang['CREATE']; ?> <?php echo $lang['operators']; ?></td></tr>
<?php 
$userinfo['show_arrival'] = "Y";
$userinfo['user_alert'] = "Y";
$userinfo['isadmin'] = "N";
$userinfo['username'] = "";
$userinfo['password'] = "";
$userinfo['email'] = "";
$userinfo['user_id'] = 0;
$userinfo['greeting'] = "How May I help You?";
$userinfo['photo'] = "";
$userinfo['showtype'] = "";


} ?>
<tr><td>username:</td><td><input type=text name=newuser size=30 value="<?php echo $userinfo['username']; ?>"></td></tr>
<tr><td>password:</td><td><input type=text name=newpass size=30 value="<?php echo $userinfo['password']; ?>"></td></tr>
<tr><td>email:</td><td><input type=text name=email size=30 value="<?php echo $userinfo['email']; ?>"></td></tr>
<?php
if ($userinfo['show_arrival'] == "N"){ $show_arrival_n = " CHECKED "; $show_arrival_y=""; } else { $show_arrival_n=""; $show_arrival_y = " CHECKED "; }
if ($userinfo['showtype'] == "N"){ $showtyping_n = " CHECKED "; $showtyping_y=""; } else { $showtyping_n=""; $showtyping_y = " CHECKED "; }
if ($userinfo['user_alert'] == "Y"){ $user_alert_y = " CHECKED "; $user_alert_n ="";  }
if ($userinfo['user_alert'] == "N"){ $user_alert_n = " CHECKED "; $user_alert_y =""; }
if ($userinfo['user_alert'] == "E"){ $user_alert_e = " CHECKED "; $user_alert_e = ""; }
  $isadmin_y = "";
if ($userinfo['isadmin'] == "Y"){ $isadmin_y = " CHECKED "; }
?>
<tr><td><?php echo $lang['txt135']?>:</td><td><input type=radio name=show_arrival value=Y <?php echo $show_arrival_y; ?> ><?php echo $lang['YES'];?> <input type=radio name=show_arrival value=N <?php echo $show_arrival_n; ?>><?php echo $lang['NO'];?></td></tr>
<tr><td><?php echo $lang['txt136']?>:</td><td><input type=radio name=user_alert value=Y <?php echo $user_alert_y; ?> > JS alert <input type=radio name=user_alert value=N <?php echo $user_alert_n; ?> > EMBED wav sound</td></tr>
<tr><td><?php echo $lang['txt156']?>:</td><td><input type=radio name=showtyping value=Y <?php echo $showtyping_y; ?> > <?php echo $lang['txt158']?><br>
                                              <input type=radio name=showtyping value=N <?php echo $showtyping_n; ?> > <?php echo $lang['txt157']?></td></tr>
<tr><td>Admin:</td><td><input type=checkbox name=isadmin value=Y <?php echo $isadmin_y; ?> ></td></tr>

<tr><td colspan=2><?php echo $lang['dept']; ?><ul>
<?php
$query = "SELECT * FROM livehelp_departments";
$data = $mydatabase->query($query);
while($dept = $data->fetchRow(DB_FETCHMODE_ASSOC)){
  $query = "SELECT * 
            FROM livehelp_operator_departments 
            WHERE user_id=" . intval($userinfo['user_id']) . " AND department=" . intval($dept['recno']);
  $check = $mydatabase->query($query);
  if( $check->numrows() == 0){ $checked =""; } else { $checked= " CHECKED "; }
  print "<input type=checkbox name=mydepartment_" . $dept['recno'] . " value=" . $dept['recno'] . " $checked><b> " . $dept['nameof'] . " </b><br>";
}


?>
<table><tr><td>
<b>Photo:</b><br>
<?php 
  if (empty($userinfo['photo'])) 
    print "No Photo<br>";
  else
    print "<img src=". $userinfo['photo'] .">";
?>
</td><td>
<b>Greeting:</B><br>
<textarea cols=25 rows=6 name=greeting><?php echo $userinfo['greeting']; ?></textarea><br>
<input type=checkbox name=cleargreeting onclick="javascript:document.operatorform.greeting.value=''">Clear Greeting (No Greeting message)<br>
</td></tr>
<tr><td colspan=2>
<br>Image URL:<input type=text size=50 name=photo value=<?php echo $userinfo['photo']; ?> ><br>
</td></tr>
</table>

<input type=submit value="<?php echo $lang['SAVE']; ?>"><br>

</td></tr>
</table> 

</td></tr></table><br>
</form>
<?php
}

$mydatabase->close_connect();
?>
<pre>


</pre>
<font size=-2>
<!-- Note if you remove this line you will be violating the license even if you have modified the program -->
<a href=http://www.craftysyntax.com/ target=_blank>Crafty Syntax Live Help</a> © 2003 - 2005 by <a href=http://www.craftysyntax.com/EricGerdes/ target=_blank>Eric Gerdes</a>  
<br>
CSLH is  Software released 
under the <a href=http://www.gnu.org/copyleft/gpl.html target=_blank>GNU/GPL license</a>
</font>