<?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);
 

  // open up layer directory and get list of layer invites:
  $dir = "layer_invites/";
  $handle=opendir($dir);
  $i = 0;
  $count =101;
	while($file=readdir($handle)){
		if ($file!="." && $file!=".." && eregi("layer-",$file) ){
			$imageurl = $file;
			if ( (is_file("$dir/$file")) && (!(eregi(".txt",$file))) ){
				 $parts = explode(".",$file);
				 $file = $parts[0];			 				 				 
				 $file = str_replace("layer-","",$file);	
				 // see if we have this invite in the database if not 
				 // add it .
				 $querysql = "SELECT layerid from livehelp_layerinvites WHERE  imagename='$imageurl'";
         $rs = $mydatabase->query($querysql);
				 if($rs->numrows() == 0){
				 	  $layerid = find_puka();
				 	  $fullfile = "layer_invites/layer-". $file . ".txt";
				 	  $imagemapof = file_get_contents($fullfile);
				 	  $imagemap = addslashes($imagemapof);
				 	  $sqlquery = "INSERT INTO livehelp_layerinvites (layerid,imagename,imagemap) VALUES ($layerid,'$imageurl','$imagemap')";
				 	  $mydatabase->query($sqlquery);
				  }			 
				  $count++;
			}
		}
	}
  
$timeof = date("YmdHis");

if(!(isset($UNTRUSTED['whattodo']))){  $UNTRUSTED['whattodo'] = ""; }


if(empty($myid)){
  // get the if 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'];
}

if($UNTRUSTED['whattodo'] == $lang['SAVE']){
 if(empty($UNTRUSTED['editidnum'])){
  $query = "INSERT INTO livehelp_autoinvite (isactive,department,message,page,visits,referer,typeof) VALUES ('Y',".intval($UNTRUSTED['department']).",'".filter_sql($UNTRUSTED['comment'])."','".filter_sql($UNTRUSTED['page'])."',". intval($UNTRUSTED['visits']).",'".filter_sql($UNTRUSTED['referer'])."','".filter_sql($UNTRUSTED['typeof'])."')";
  $mydatabase->query($query);  	
 } else {
  $query = "UPDATE livehelp_autoinvite SET typeof='".filter_sql($UNTRUSTED['typeof'])."',department=".intval($UNTRUSTED['department']).",message='".filter_sql($UNTRUSTED['comment'])."',page='".filter_sql($UNTRUSTED['page'])."',visits=".intval($UNTRUSTED['visits']).",referer='".filter_sql($UNTRUSTED['referer'])."' WHERE idnum=". intval($UNTRUSTED['editidnum']); 
  $mydatabase->query($query);   	
 }
}

if($UNTRUSTED['whattodo'] == $lang['UPDATE']){  
  $query = "SELECT * FROM livehelp_autoinvite";
  $data = $mydatabase->query($query);
  while($row = $data->fetchRow(DB_FETCHMODE_ASSOC)){
    $varname = "isactive__" . $row['idnum']; 
    if(!(empty($UNTRUSTED[$varname]))) {  
        $query = "UPDATE livehelp_autoinvite set isactive='Y' WHERE idnum='".$row['idnum']."' ";
        $mydatabase->query($query);
    } else {
        $query = "UPDATE livehelp_autoinvite set isactive='N' WHERE idnum='".$row['idnum']."' ";
        $mydatabase->query($query);    	
    }
  }  
}

if($UNTRUSTED['whattodo'] == $lang['REMOVE']){
        $query = "DELETE FROM livehelp_autoinvite WHERE idnum=".intval($UNTRUSTED['which']);
        $mydatabase->query($query);  
}

?>
<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=E0E8F0 onload=window.focus()>

<table width=100% bgcolor=<?php echo $color_alt1;?>><tr><td>
<b><?php echo $lang['txt5']; ?></b></td></tr></table>
<?php echo $lang['txt6']; ?>

<br>
<?php
if ( (empty($UNTRUSTED['createnew'])) && ($UNTRUSTED['whattodo'] != "EDIT") ){ 
?>
<b><?php echo $lang['txt7']; ?></b>
<form action=autoinvite.php method=post>
<table width=555>
<tr bgcolor=D4DCF2><td><b><?php echo $lang['active']; ?></b></td><td><b><?php echo $lang['invite']; ?></b></td><td><b><?php echo $lang['options']; ?></b></td></tr>
<?php
$query = "SELECT * FROM livehelp_autoinvite ";
$data = $mydatabase->query($query);
if($data->numrows() == 0){
  print "<tr><td colspan=3 bgcolor=FFFFFF align=center><font color=990000>" . $lang['txt8'] ." </font></td></tr>";  	
} else {
 $bgcolor="";
 while($row = $data->fetchRow(DB_FETCHMODE_ASSOC)){
 
  if($bgcolor=="FEFEFE"){ $bgcolor="CDCDCD";  } else { $bgcolor="FEFEFE"; }
  if($row['isactive'] =="Y"){ $isactive = " CHECKED "; } else { $isactive = " "; }
  print "<tr bgcolor=$bgcolor><td><input type=checkbox name=isactive__".$row['idnum']." value=Y $isactive></td><td>";
  print "<table width=100% bgcolor=$color_background><tr><td> ";
  if($row['department']!= 0){ print "<b>" . $lang['txt9'] ."</b><font color=000077>" . whatdep($row['department']) . "</font><br>"; }
  if($row['page']!= ""){ print "<b>" . $lang['txt10'] ."</b><font color=000077>".$row['page']."</font><br>"; }
  if($row['referer']!= ""){ print "<b>" . $lang['txt11'] ."</b><font color=000077>".$row['referer']."</font><br>"; }
  if($row['visits'] > 1){ print "<b>" . $lang['txt12'] ."</b><font color=000077>".$row['visits']."</font><br>";  }
  print "</td></tr></table>";
  if($row['typeof'] == "layer"){

  // $row['message'] should be the id of the layerinvite:
  $layerid = intval($row['message']);
  $sqlquery = "SELECT layerid,imagename,imagemap FROM livehelp_layerinvites WHERE layerid='$layerid'";
  $layers = $mydatabase->query($sqlquery);    
  $invite = $layers->fetchRow(DB_FETCHMODE_ORDERED);
  print "<img src=layer_invites/$invite[1]>"; 
  } else
    print $row['message'];
  print  "</td><td><a href=autoinvite.php?whattodo=EDIT&which=" . $row['idnum'] . ">" . $lang['edit'] ."</a> <br><Br><br> <a href=autoinvite.php?whattodo=REMOVE&which=" . $row['idnum'] . "><font color=990000>" . $lang['delete'] ."</font></td></tr>";	
 }

}
?>
</table>
<table><tr><td><input type=submit name=whattodo value="<?php echo $lang['UPDATE']; ?>"></td><td><input type=submit name=createnew value="<?php echo $lang['CREATE']; ?>"></td></tr></table>
<br>

<?php } else { ?>
<form action=autoinvite.php name=chatter method=post>
<?php
if ($UNTRUSTED['whattodo'] == "EDIT"){ 
 $query = "SELECT * FROM livehelp_autoinvite WHERE idnum=".intval($UNTRUSTED['which']);
 $row = $mydatabase->query($query);
 $item = $row->fetchRow(DB_FETCHMODE_ASSOC);
 print "<input type=hidden name=editidnum value=".intval($UNTRUSTED['which']).">\n";
} else {
 	 $item['page'] = "";
	 $item['department'] = "";
   $item['referer'] = "";
   $item['visits'] = "";
   $item['message'] = "";
   $item['typeof'] = "";
}

?>
<h2><?php echo $lang['txt13']; ?></h2>
<b><?php echo $lang['txt14']; ?></b>
<?php
 if(empty($UNTRUSTED['createnew']))  $UNTRUSTED['createnew'] = "";
 if(empty($UNTRUSTED['whattodo']))  $UNTRUSTED['whattodo'] = "";    
 if(empty($UNTRUSTED['which']))  $UNTRUSTED['which'] = "";  
 if(empty($UNTRUSTED['force']))  $UNTRUSTED['force'] = "";  
?>
<SCRIPT>
function refreshthis(what){
  if (what == "popup")
       	window.location.replace("autoinvite.php?createnew=<?php echo $UNTRUSTED['createnew']; ?>&whattodo=<?php echo $UNTRUSTED['whattodo']; ?>&which=<?php echo $UNTRUSTED['which']; ?>&force=popup");  
  else
       	window.location.replace("autoinvite.php?createnew=<?php echo $UNTRUSTED['createnew']; ?>&whattodo=<?php echo $UNTRUSTED['whattodo']; ?>&which=<?php echo $UNTRUSTED['which']; ?>&force=layer");  
 
 
}
</SCRIPT>
<table>
<tr><td><b>
<?php echo $lang['txt15']; ?></b></td><td>
<select name=typeof onchange="refreshthis(this.value)">
<?php
    $layerselected = " ";
    $popupselected = "  ";    
  if( ($item['typeof'] == "layer") || ($UNTRUSTED['force'] =="layer")){
    $layerselected = " SELECTED ";
    $popupselected = "  ";    	
  } 
  if( ($item['typeof'] == "popup") || ($UNTRUSTED['force'] =="popup")){  
    $layerselected = "  ";
    $popupselected = " SELECTED ";   	
  }
?>
<option value=layer <?php echo $layerselected; ?>><?php echo $lang['txt16']; ?></option>
<option value=popup <?php echo $popupselected; ?>><?php echo $lang['txt17']; ?></option>
</select>
</td></tr><tr>
<td>
<b><?php echo $lang['txt18']; ?></b></td><td>
<select name=department>
<option value=0><?php echo $lang['any']; ?></option>
<?php
 $query = "SELECT * FROM livehelp_departments ";
 $data = $mydatabase->query($query);
 while($row = $data->fetchRow(DB_FETCHMODE_ASSOC)){   
   print "<option value=" . $row['recno'];
   if($item['department'] == $row['recno']){ print " SELECTED "; }
   print ">" . $row['nameof'] . "</option>\n";
 }
?>
</select>
</td></tr>
</table>
<b><?php echo $lang['txt19']; ?></b><br>
<?php echo $lang['txt20']; ?>
<br>
<input type=text size=45 name=page value="<?php echo $item['page']; ?>" ><br>
<b><?php echo $lang['txt21']; ?></b><br>
<?php echo $lang['txt22']; ?>
<br>
<input type=text size=45 name=referer  value="<?php echo $item['referer']; ?>" ><br>
<table>
<tr><td><b><?php echo $lang['txt23']; ?></b></td><td>
<select name=visits>
<option value="<?php echo $item['visits']; ?>"><?php echo $item['visits']; ?></option>
<?php
for($i=1; $i< 30; $i++){
print "<option value=$i>$i</option>\n";
}
?>

</select> <?php echo $lang['txt24']; ?>
</td></tr>
</table>
<br>

<br>

<input type=hidden name=typing value="no"> 
<b><?php echo $lang['message']; ?></b>
<input type=hidden name=user_id value="<?php echo $myid; ?>">
<input type=hidden name=alt_what value="">
<SCRIPT>
function autofill(num){
   if(num == 0){ document.chatter.comment.value=''; }
<?php 
  $query = "SELECT * FROM livehelp_quick Where typeof!='URL' AND typeof!='IMAGE' ORDER by name ";
  $result = $mydatabase->query($query);
  $j=-1;
  while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
  	if(note_access($row['visiblity'],$row['department'],$row['user'])){
  	$j++;
   $in= $j + 1;
   print "if(num == $in){ document.chatter.comment.value='" . addslashes(stripslashes(stripslashes($row['message']))) . "'; document.chatter.editid.value='" . $row['id'] . "';  }\n";	
   }
 } ?>
}
function autofill_url(num){
   if(num == 0){ document.chatter.comment.value=''; }
<?php 
  $query = "SELECT * FROM livehelp_quick Where typeof='URL' ORDER by name ";
  $result = $mydatabase->query($query);
  $j = -1;
  $j=-1;
  while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
  	if(note_access($row['visiblity'],$row['department'],$row['user'])){
  	$j++;
   $in= $j + 1;
   print "if(num == $in){ document.chatter.comment.value='[SCRIPT]openwindow(\'". $row['message'] . "\',\'window".$row['id']."\');[/SCRIPT]';  }\n";	
  }
 } ?>
}

function openwindow(url){ 
 window.open(url, 'chat540', 'width=572,height=320,menubar=no,scrollbars=0,resizable=1');
}

function autofill_image(num){
   if(num == 0){ document.chatter.comment.value=''; }
<?php 
  $query = "SELECT * FROM livehelp_quick Where typeof='IMAGE' ORDER by name ";
  $result = $mydatabase->query($query);
  $j=-1;
  while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
   if(note_access($row['visiblity'],$row['department'],$row['user'])){ 	
  	$j++;
   $in= $j + 1;
   print "if(num == $in){ document.chatter.comment.value='<img src=".$row['message'].">';  }\n";	
   }
 } ?>
}

function updatedhtml(){

<?php
  $sqlquery = "SELECT layerid,imagename,imagemap FROM livehelp_layerinvites ";
  $layers = $mydatabase->query($sqlquery);    
  while($invite = $layers->fetchRow(DB_FETCHMODE_ORDERED)){ 
				   print "if (document.chatter.comment.value==\"".$invite[0]."\")\n";
				   print "   document.chatter.dhtmlimage.src = 'layer_invites/$invite[1]'\n";
				   $count++;
	}  
?>		

}
</SCRIPT><br>
<input type=hidden name=timeof value=<?php echo $timeof; ?> >
<input type=hidden name=editid value="">
<?php if( (($UNTRUSTED['force'] !="popup") && ($item['typeof'] == "layer")) || ($UNTRUSTED['force'] =="layer") || (empty($item['typeof']) && (empty($UNTRUSTED['force']))) ){

if(!(empty($item['message']))){
 
  // $item['message'] should be the id of the layerinvite:
  $layerid = intval($item['message']);
  $sqlquery = "SELECT layerid,imagename,imagemap FROM livehelp_layerinvites WHERE layerid='$layerid'";
  $layers = $mydatabase->query($sqlquery);    
  $invite = $layers->fetchRow(DB_FETCHMODE_ORDERED);
  print "<img src=layer_invites/$invite[1]  name=dhtmlimage>"; 
 
} else {
  print "<img src=images/blank.gif name=dhtmlimage border=0>";
}
?>
<br><br>
<?php echo $lang['txt27'] ?> <select name=comment onchange=updatedhtml()>
<option value="<?php echo $item['message']; ?>"></option>
<?php
  $sqlquery = "SELECT layerid,imagename,imagemap FROM livehelp_layerinvites ";
  $layers = $mydatabase->query($sqlquery);    
  while($invite = $layers->fetchRow(DB_FETCHMODE_ORDERED)){ 
    print "<option value=$invite[0]> $invite[1]</option>\n";  
  }
?>			 
</select><br><br>
<input type=submit name=whattodo value="<?php echo $lang['SAVE']; ?>">
</FORM>
<br>
 <br><br>
<?php
 } else { 
?>
<textarea cols=40 rows=3 name=comment ><?php echo $item['message']; ?>
</textarea>

 
<br>
<img src=images/blank.gif width=300 height=1><br>
</td></tr>
<tr><td colspan=3><b><?php echo $lang['txt31']; ?></b><br>
<select name=quicknote onchange=autofill(this.selectedIndex)>
<option value=-1 ><?php echo $lang['txt27']; ?></option>
<?php 
  $query = "SELECT * FROM livehelp_quick Where typeof!='URL' AND typeof!='IMAGE' ORDER by name ";
  $result = $mydatabase->query($query);
  $j=-1;
  while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
    if(note_access($row['visiblity'],$row['department'],$row['user'])){	
  	$j++;
   $in= $j + 1;
   print "<option value=".$row['id'].">".$row['name']."</option>\n";	
  }
} ?>
</select> <a href=javascript:openwindow('edit_quick.php')><?php echo $lang['txt32']; ?></a><br>
</td>
</tr></table>

<input type=submit name=whattodo value="<?php echo $lang['SAVE']; ?>">
</FORM>
<br><br>
<br><br>
<?php } ?>
</body>
<?php
$mydatabase->close_connect();
}
?>