Location: PHPKode > projects > Slacker > slacker/slacker.inc/slacker/user_functions.inc.php
<?php
/*
                ------======--------
                 Slacker 	v0.1
                 ------------------
                 
                 Developed and Coded by
                 Dave Bullock - hide@address.com 2002
                 _BSD Lic._

*/


# --- Functions ---
/* 
    --------------------------------------
    FUNCTION: calendar()
    --------------------------------------
*/ 

function calendar($month="",$year=""){
$CurDate = getdate();

if ($month == NULL) {
	$YearToShow = $CurDate['year'];
	$MonthToShow = $CurDate['mon'];
	$DayToShow = $CurDate['mday'];
	$NumberOfDays = date(t,$CurDate);
} else {
	if (checkdate($month,1,$year) == false) {
		$YearToShow = $CurDate['year'];
		$MonthToShow = $CurDate['mon'];
		$DayToShow = $CurDate['mday'];
	} else {
		$YearToShow = $year;
		$MonthToShow = $month;
		if ( ($YearToShow < 1902) || ($YearToShow > 2037) ) {
			$YearToShow = $CurDate['year'];
			$MonthToShow = $CurDate['mon'];
			$DayToShow = $CurDate['mday'];
		}
		if ( ($YearToShow == $CurDate['year']) && ($MonthToShow == $CurDate['mon']) ) {
			$DayToShow = $CurDate['mday'];
		}
	}
	$NumberOfDays = date(t,mktime(0,0,0,$MonthToShow+1,0,$YearToShow,-1));
}

$MonthNames = array(1=>'January','February','March','April','May','June','July','August','September','October','November','December');
//$Years = array('1998','1999','2000','2001','2002','2003','2004','2005');
$Years = array($YearToShow-5,$YearToShow-4,$YearToShow-3,
    $YearToShow-2,$YearToShow-1,$YearToShow,$YearToShow+1,$YearToShow+2,
    $YearToShow+3,$YearToShow+4,$YearToShow+5);


$output = $output .  <<<EOT
<form action="$PHP_SELF" method="get">
<table  border="0" cellpadding="10" cellspacing="0">
<tr align="center">
<td>
<select name="month">

EOT;

while (list($key,$value) = each($MonthNames)) {
	if ($key != $MonthToShow) {
		$output = $output . '<option value="'.$key.'">'.$value."</option>\n";
	} else {
		$output = $output . '<option value="'.$key.'" selected>'.$value."</option>\n";
	}
}

$output = $output . "</select>\n</td>\n<td class=\"f\">\n<select name=\"year\">\n";
while (list($key,$value) = each($Years)) {
	if ($value != $YearToShow) {
		$output = $output . '<option value="'.$value.'">'.$value."</option>\n";
	} else {
		$output = $output . '<option value="'.$value.'" selected>'.$value."</option>\n";
	}
}

$output = $output . <<<EOT
</select>
</td>
<td class="f"><input type="submit" value="go to it!"></td>
</tr>
</table>
</form>

<br><br><br>

<table border="1" cellpadding="1" cellspacing="0">
<tr>
<td class="n" colspan="7" align="center"><h3>$MonthNames[$MonthToShow] $YearToShow</h3></td>
</tr>
<tr align="center">
<th>sunday</th>
<th>monday</th>
<th>tuesday</th>
<th>wednesday</th>
<th>thursday</th>
<th>friday</th>
<th>saturday</th>
</tr>

EOT;

$FirstDayOfWeek = date(l,mktime(0,0,0,$MonthToShow,1,$YearToShow));

switch ($FirstDayOfWeek) {

        case 'Monday':
                $offset = 1;
        break;
	case 'Tuesday':
		$offset = 2;
	break;
	case 'Wednesday':
		$offset = 3;
	break;
	case 'Thursday':
		$offset = 4;
	break;
	case 'Friday':
		$offset = 5;
	break;
	case 'Saturday':
		$offset = 6;
	break;
	default:
		$offset = 0;
}

if ($offset > 0) {
$output = $output . "<tr align=\"left\">\n";
$output = $output . str_repeat("<td width=70 height=50 valign=top class=\"n\">&nbsp;</td>\n",$offset);
}

# check for events:
$result = safe_query ("SELECT id, name, date, recurring FROM calendar WHERE (month LIKE '$MonthToShow' AND year LIKE '$YearToShow' AND active LIKE '1' ) OR (recurring NOT LIKE '' AND active LIKE '1') ORDER BY name ASC");
if ($row = mysql_fetch_array($result)) {
do {
if ($row["recurring"])
{
        $mday = $row["recurring"];
        $mid = $row["id"];
        $mname = $row["name"];
        $weeklies[$mday][$mid]=$mname;
}
else
{
	$mdate = $row["date"];
	$mid = $row["id"];
	$mname = $row["name"];
	$events[$mdate][$mid]=$mname;
}
} while($row = mysql_fetch_array($result));
}
else
{ $events = $YearToShow . " " . $MonthToShow; }


for ($i=1; $i <= $NumberOfDays; $i++) {
	$DayOfWeek = date(l,mktime(0,0,0,$MonthToShow,$i,$YearToShow));
	$myday = substr(strtolower(date(D,mktime(0,0,0,$MonthToShow,$i,$YearToShow))),0,2);

	if($DayOfWeek == 'Sunday') {
            $output = $output . "<tr align=\"left\">\n";
	}
	if ($i != $DayToShow) {
		if (is_array($events[$i])){
			foreach ( $events[$i] as $nid => $myevent ) {
                    		$event_today= $event_today . "<li><a href=/calendar.php?id=" . $nid. ">" . $myevent . "</a><br>";
                		}		
			}
                if (is_array($weeklies[$myday])){
                        foreach ( $weeklies[$myday] as $nid => $myevent ) {
                                $event_today= $event_today . "<li><a href=/calendar.php?id=" . $nid. ">" . $myevent . "</a><br>";
                                }
                        }

            $output = $output . "<td  width=70 height=50 align=left  valign=top >$i<br>".$event_today."<br><br><br>
		<a href=/calendar.php?add=$i&date=$i&month=$MonthToShow&year=$YearToShow>+</a></td>\n";
		$event_today= "";
	} else {
               if (is_array($events[$i])){
		foreach ( $events[$i] as $nid => $myevent ) {
		    $event_today= $event_today . "<li><a href=/calendar.php?id=" . $nid. ">" . $myevent . "</a><br>";
		}
	       }
		   if (is_array($weeklies[$myday])){
                        foreach ( $weeklies[$myday] as $nid => $myevent ) {
                                $event_today= $event_today . "<li><a href=/calendar.php?id=" . $nid. ">" . $myevent . "</a><br>";
                                }
                        }

            $output = $output . "<td bgcolor=#ff9900  width=70 height=50 valign=top align=left>$i<br>" 
			. $event_today. "<br><br><br><a href=/calendar.php?add=$i&date=$i&month=$MonthToShow&year=$YearToShow>+</a></td>\n";
		$event_today ="";
	}
	if($DayOfWeek == 'Saturday') {
            $output = $output . "</tr>\n";
	}
}

if ( ( ($offset == 5) && ($NumberOfDays > 30) ) || ( ($offset == 6) && ($NumberOfDays > 29) ) ) {
	if (42-$NumberOfDays-$offset > 0) {
		$output = $output . str_repeat("<td class=\"n\">&nbsp;</td>\n",42-$NumberOfDays-$offset);
	}
	$output = $output . "</tr>\n";
} elseif ( ($NumberOfDays != 28) || ($offset > 0) ) {
	if (35-$NumberOfDays-$offset > 0) {
		$output = $output . str_repeat("<td class=\"n\">&nbsp;</td>\n",35-$NumberOfDays-$offset);
	$output = $output . "</tr>\n";
	}
}

$output = $output .  <<<EOT
</table>
</center>
EOT;

return $output;

}
/* 
    --------------------------------------
    FUNCTION: safe_query()
    string safe_db_create (string query)
    --------------------------------------
*/                
function safe_query($query="")
{
   if (empty($query)) { return FLASE; }     
   $result = mysql_query($query)
            or die("hey slacker, your query failed: "
                ."<li>error=" . mysql_error()
                ."<li>query=" . $query
            );
    return $result;
}
/* 
    --------------------------------------
    FUNCTION: view_item()
    --------------------------------------
*/   

function view_item($t_name,$id)
{
    
    $t_name = sanitize($t_name);
    $id = sanitize($id);
    $m_name = $t_name . "_category";
    dbconnect();
    
    $result = safe_query ("SELECT active, blurb, link, link_only, id, title, priority, description, image, parent FROM $t_name WHERE id LIKE $id");
    if ($row = mysql_fetch_array($result)) 
    {
        do 
        {
        
            $name = stripslashes($row["title"]);
            $blurb = stripslashes($row["blurb"]);
            $description = nl2br(stripslashes($row["description"]));
            $link = $row["link"];
            
            if (($row["image"])==1)
            {
                $image = "<br><img src=/img/" . $t_name . "_image" . $id . ".jpg><br>";
            }
           
            $output =  subtitle($name) 
            . "<i>"
            . $blurb
            . "</i>\n"
            . $image
            . "<br>\n"    
            . $description
            . "<br><a href=\""
            . $link
            . "\" target=_blank>"
            . $link
            . "</a>";
              
            } 
        while($row = mysql_fetch_array($result));
    } 
    else 
    {
        $output = ("no item");
    }
    

return $output;

}

/* 
    --------------------------------------
    FUNCTION: category()
    --------------------------------------
*/   

function category($t_name,$id="")
{
    
    $t_name = sanitize($t_name);
    $id = sanitize($id);
    $m_name = $t_name . "_category";
    dbconnect();


    $result = safe_query ("SELECT image, name, description, id, image FROM $m_name WHERE visible LIKE 1 ORDER BY name");
    if ($row = mysql_fetch_array($result)) 
    {
        do 
        {
            $name = $row["name"];
            $priority = $row["priority"];
            $description = $row["description"];
            $id = $row["id"];
            $gimage = $row["image"];
            if ($gimage=='0')
            {
                $bimage="";
            }
            else
            { 
                $bimage = "<br><img src=/img/" . $m_name . "_thumb" . $id . ".jpg ><br><br>"; 
            }
            $m_name=$t_name . "_category";
            $output = $output 
            . "<h3>" . $name
            . "\n <br> " . $description   
            . $bimage . "</h3><ul>";
             
             $results = safe_query ("SELECT 
                active,link, link_only, image, title, blurb, id 
                FROM $t_name 
                WHERE parent LIKE $id
                AND active LIKE 1 
                ORDER BY title");
            if ($row = mysql_fetch_array($results)) 
            {
                do 
                {
                    $name = $row["title"];
                    $priority = $row["priority"];
                    $active = $row["active"];
                    $description = $row["blurb"];
                    $id = $row["id"];
                    $nimage = $row["image"];
		    $link = $row["link"];
                    $link_only = $row["link_only"];

                    if ($nimage=='0')
                    {
                    $vimage = "";
                    }
                    else
                    {
                        if ($link_only=="1") {$thumby = "_image";} else {$thumby = "_thumb";} 
                        $vimage = "<br><img src=/img/" . $t_name . $thumby . $id . ".jpg ><br>"; 
                    }

                    $m_name=$t_name . "_category";
      if ($link_only=="1")
                        {
                         $output = $output . "<a href=\"" . $link . "\" target=_blank>" . $name
                    . "\n : " . $description
                    . $vimage
                    . "</a><br>";

		} else { 
                   $output = $output . "<a href=/" . $t_name . ".php?view_item=1&t_name=" 
                    . $t_name . "&id=" . $id .">" . $name 
                    . "\n : " . $description   
                    . $vimage
                    . "</a><br>";
}
                } 
                while($row = mysql_fetch_array($results));
                } 
                else 
                {
                }

        $output = $output . "</ul>";  
              
                  
        } 
        while($row = mysql_fetch_array($result));
    } 
    else 
    {
    $output = "<p>" . $output . "no items in group<br></ul></p>";

    }
  
return $output;



}



/* 
    --------------------------------------
    FUNCTION: list_item()
    --------------------------------------
*/   

function list_item($t_name,$id="")
{
    
    $t_name = sanitize($t_name);
    $id = sanitize($id);
    $m_name = $t_name . "_category";
    dbconnect();


    $result = safe_query ("SELECT name, description, id, image FROM $m_name ORDER BY name");
    if ($row = mysql_fetch_array($result)) 
    {
        do 
        {
            $name = $row["name"];
            $priority = $row["priority"];
            $description = $row["description"];
            $id = $row["id"];
            $my_image = $row["image"];
            if ($my_image=='0')
            {
                $dimage = "";
            }
            else
            { 
                $dimage = "<img src=/img/" . $m_name . "_thumb" . $id . ".jpg ><br>"; 
            }
            $m_name=$t_name . "_category";
            $output = $output . "<h3>" . $name
            . "\n : " . $description   
            . "\n<a href=/admin/" . $t_name 
            . ".php?edit_category=1&t_name=" 
            . $t_name . "&id=" . $id .">" . $dimage . " [x]</a></h3><ul>";
             
             $results = safe_query ("SELECT 
                active, image, title, blurb, id 
                FROM $t_name 
                WHERE parent LIKE $id 
                ORDER BY title");
            if ($row = mysql_fetch_array($results)) 
            {
                do 
                {
                    $name = $row["title"];
                    $priority = $row["priority"];
                    $active = $row["active"];
                    $description = $row["description"];
                    $id = $row["id"];
                    $ftimage = $row["image"];
                    if ($ftimage=='0')
                    {
                        $ttimage = "";
                    }
                    else
                    { 
                        $ttimage = "<img src=/img/" . $t_name . "_thumb" . $id . ".jpg ><br>"; 
                    }

                    $m_name=$t_name . "_category";
                    $output = $output . $name 
                    . "\n : " . $description   
                    . $ttimage
                    . "<a href=/admin/" . $t_name . ".php?edit_item=1&t_name=" 
                    . $t_name . "&id=" . $id .">[x]</a><br>";
                } 
                while($row = mysql_fetch_array($results));
                } 
                else 
                {
                    $output = $output . "no items in group<br><a href=" . $t_name . "> add item</a></ul>";
                }

        $output = $output . "</ul>";  
              
                  
        } 
        while($row = mysql_fetch_array($result));
    } 
    else 
    {
    $output = "<p>" . $output . "no items in group<br></ul></p>";

    }
$output = " <a href=" . $t_name . ".php?t_name=" . $t_name . ">click to add items</a>" . $output;    
  
return $output;



}


/* 
    --------------------------------------
    FUNCTION: edit_category()
    --------------------------------------
*/   

function edit_category($t_name,$id)
{
    
    $t_name = sanitize($t_name);
    $id = sanitize($id);
    $m_name = $t_name . "_category";
    dbconnect();


    $result = safe_query ("SELECT visible, name, priority, description FROM $m_name WHERE id LIKE $id");
    if ($row = mysql_fetch_array($result)) 
    {
        do 
        {
            $name = $row["name"];
            $priority = $row["priority"];
            $active = $row["visible"];
            $description = $row["description"];
            $m_name=$t_name . "_category";
            $selector =  "<br><br>parent:<br>" . db_select_field("parent","$m_name","id","name");
            $output = subtitle($page_name) . start_form("$t_name.php") . "<hr>name:<br>" . text_field("name","$name")  	    
            . "<br>description:<br>" . text_field("description","$description")    
            . "<br>priority:<br>" . text_field("priority","$priority","2")  
            . "\n<br>" . checkbox_field("image","0","no image","2")  
            . "<br>" . checkbox_field("active","1","active","1") . $selector
            . hidden_field("update_category","1") . hidden_field("id","$id") 
            . hidden_field("t_name","$t_name") 
            . "\n<br>image:<br>" . file_field("image") . "<br>" . submit_field() . end_form();
            
        } 
        while($row = mysql_fetch_array($result));
    } 
    else 
    {
        $output = ("no item");
    }
    $output = $output . " <a href=" . $t_name . ".php?list=1&t_name=" . $t_name . ">click here for all categories</a>";    
    return $output;
  

}


/* 
    --------------------------------------
    FUNCTION: edit_item()
    --------------------------------------
*/   

function edit_item($t_name,$id)
{
    
    $t_name = sanitize($t_name);
    $id = sanitize($id);
    $m_name = $t_name . "_category";
    dbconnect();
    
    $result = safe_query ("SELECT active, blurb, link, link_only, id, title, priority, description, parent FROM $t_name WHERE id LIKE $id");
    if ($row = mysql_fetch_array($result)) 
    {
        do 
        {
        
            $name = $row["title"];
            $priority = $row["priority"];
            $active = $row["active"];
            $blurb = $row["blurb"];
            $description = $row["description"];
            $link = $row["link"];
            $id = $row["id"];
            $parent = $row["parent"];
            $link_only = $row["link_only"];
            $selector =  "<br><br>parent:<br>" . db_select_field("parent","$m_name","id","name","","$parent");

            
            $output =  subtitle($page_name) 
            . start_form("$t_name.php") . "<hr>name:<br>" . text_field("name","$name")  
            . "\n<br>blurb:<br>" . text_field("blurb","$blurb")    
            . "\n<br>description:<br>" . text_field("description","$description")    
            . "\n<br>priority:<br>" . text_field("priority","$priority","2")
            . "\n<br>link:<br>" . text_field("link","$link") . $selector
            . "\n<br>" . checkbox_field("link_only","1","link only","2")        
            . "\n<br>" . checkbox_field("image","0","no image","2")
            . "\n<br>" . checkbox_field("active","1","active","1")
            . $other_m . $other2_m . $other3_m
            . hidden_field("id","$id") 
            . hidden_field("update_item","1") 
            . hidden_field("t_name","$t_name") 
            . "\n<br>image:<br>" . file_field("image") . "<br><br>\n" . submit_field() . end_form();
    
        } 
        while($row = mysql_fetch_array($result));
    } 
    else 
    {
        $output = ("no item");
    }
    
$output = $output . " <a href=" . $t_name . ".php?list=1&t_name=" . $t_name . ">click here for all items</a>";    
return $output;

}


/* 
    --------------------------------------
    FUNCTION: udpate_item()
    --------------------------------------
*/   

function update_item($t_name,$id,$name,$blurb,$description,$priority,$active,$image="none",$link="",$link_only="",$parent="")
{

$t_name = sanitize($t_name);
$name = sanitize($name);
$id = sanitize($id);
$blurb = sanitize($blurb);
$description = sanitize($description);
$priority = sanitize($priority);
$active = sanitize($active);
$link = sanitize($link);
$parent = sanitize($parent);
$link_only = sanitize($link_only);


dbconnect();


if (!$active)
{
    $active = '0';
}


$DOCUMENT_ROOT = "/Library/Webserver/Documents/";
$doc_root = "$DOCUMENT_ROOT/img/";

if ($image=='none')
{

    safe_query ("UPDATE $t_name 
                SET title =  '$name',
                priority = '$priority', 
                link = '$link', 
                link_only = '$link_only' ,
                active = '$active', 
                blurb = '$blurb', 
                parent = '$parent', 
                description = '$description'
                WHERE id LIKE $id"
                );  	
}
elseif ($image==0)
{
    $have_image = "0";
    safe_query ("UPDATE $t_name 
                SET title =  '$name',
                priority = '$priority', 
                link = '$link', 
                link_only = '$link_only' ,
                active = '$active', 
                blurb = '$blurb', 
                parent = '$parent', 
                description = '$description', 
                image = '$have_image'
                WHERE id LIKE $id"
                );  	
}
else
{
    $have_image = "1";
    safe_query ("UPDATE $t_name 
            SET title =  '$name',
            priority = '$priority', 
            link = '$link', 
            link_only = '$link_only' ,
            active = '$active', 
            blurb = '$blurb', 
            parent = '$parent',            
            description = '$description', 
            image = '$have_image'
            WHERE id LIKE $id"
            );  


    
    $fd = fopen($image, "r");
    $image_raw = addslashes(fread($fd, filesize($image)));
    fclose($fd);
    $size=getimagesize($image); 
    system("/usr/local/bin/djpeg -pnm $image | /usr/local/bin/pnmscale -xscale 0.1 -yscale 0.1 | /usr/local/bin/cjpeg > $image.tmb");
    $fd = fopen("$image.tmb", "r");
    $image_thumb = addslashes(fread($fd, filesize("$image.tmb")));
    fclose($fd);
    unlink("$image.tmb");


            $filename1 = $doc_root . $t_name . "_image" . $id . ".jpg";
            $filename2 = $doc_root . $t_name . "_thumb" . $id . ".jpg";
            $fp = fopen ("$filename1", "w+");
            fwrite ($fp, stripslashes($image_raw));
            fclose ($fp);
            $fp = fopen ("$filename2", "w+");
            fwrite ($fp, stripslashes($image_thumb));
            fclose ($fp);
            $output = "<img border=0  src=/img/" . $t_name . "_thumb" . $id 
            . ".jpg ><a href=" . $REQUEST_URI . "?editid=" . $id . ">edit</a><br><br>";



}

    $output =  "The following item has been updated:<br><br>name: " . $name . "<br>description: " . $description . "<br>" . $output;
$output = $output . " <a href=" . $t_name . ".php?list=1&t_name=" . $t_name . ">click here for all items</a>"; 
return $output;

}


/* 
    --------------------------------------
    FUNCTION: add_item()
    --------------------------------------
*/   

function add_item($t_name,$id,$name,$blurb,$description,$priority,$active,$image="none",$link="",$link_only="",$parent="")
{

$t_name = sanitize($t_name);
$name = sanitize($name);
$id = sanitize($id);
$blurb = sanitize($blurb);
$description = sanitize($description);
$priority = sanitize($priority);
$active = sanitize($active);
$link = sanitize($link);
$link_only = sanitize($link_only);


dbconnect();

if (!$active)
{
    $active = '1';
}


$DOCUMENT_ROOT = "/Library/Webserver/Documents/";
$doc_root = "$DOCUMENT_ROOT/img/";

if ($image=='none')
{
    safe_query ("INSERT INTO $t_name (title, priority, link, link_only, active, blurb, description, image, parent)
            VALUES ('$name', '$priority', '$link', '$link_only', '$active', '$blurb', '$description', '0', '$parent')
             ");  	
    }
    else
    {
        safe_query ("INSERT INTO $t_name (title, priority, active, link, link_only, description, image, parent)
                VALUES ('$name', '$priority', '$active', '$link', '$link_only', '$description', '1', '$parent')
                ");  
    
        
        $fd = fopen($image, "r");
        $image_raw = addslashes(fread($fd, filesize($image)));
        fclose($fd);
        $size=getimagesize($image); 
        system("/usr/local/bin/djpeg -pnm $image | /usr/local/bin/pnmscale -xscale 0.1 -yscale 0.1 | /usr/local/bin/cjpeg > $image.tmb");
        $fd = fopen("$image.tmb", "r");
        $image_thumb = addslashes(fread($fd, filesize("$image.tmb")));
        fclose($fd);
        unlink("$image.tmb");
    
        
        
        $result = mysql_query ("SELECT id FROM $t_name ORDER BY id DESC LIMIT 1");
        if ($row = mysql_fetch_array($result)) 
        {
            do 
                {
                $filename1 = $doc_root  . $t_name . "_image" . $row["id"] . ".jpg";
                $filename2 = $doc_root . $t_name . "_thumb" . $row["id"] . ".jpg";
                $fp = fopen ("$filename1", "w+");
                fwrite ($fp, stripslashes($image_raw));
                fclose ($fp);
                $fp = fopen ("$filename2", "w+");
                fwrite ($fp, stripslashes($image_thumb));
                fclose ($fp);
                $output = "<img border=0  src=/img/" . $t_name . "_thumb" . $row["id"] 
                . ".jpg ><a href=" . $REQUEST_URI . "?editid=" . $row["id"] . ">edit</a><br><br>";
                } while($row = mysql_fetch_array($result));
            
        } else 
        {
            $output = ("broken");
        }
    
    
    
    }

    $output =  $output . " The following item has been added:<br><br>name: " . $name . "<br>description: " . $description . "<br>";
$output = $output . " <a href=" . $t_name . ".php?list=1&t_name=" . $t_name . ">click here for all items</a>"; 
return $output;

}
/* 
    --------------------------------------
    FUNCTION: add_category()
    --------------------------------------
*/   

function add_category($t_name,$id,$name,$blurb,$description,$priority,$active,$image="none",$parent="")
{

$t_name = sanitize($t_name);
$name = sanitize($name);
$id = sanitize($id);
$blurb = sanitize($blurb);
$parent = sanitize($parent);
$description = sanitize($description);
$priority = sanitize($priority);
$active = sanitize($active);


dbconnect();

if (!$active)
{
    $active = '1';
}

$m_name = $t_name . "_category";
$DOCUMENT_ROOT = "/Library/Webserver/Documents/";
$doc_root = "$DOCUMENT_ROOT/img/";

if ($image=='none')
{
    safe_query ("INSERT INTO $m_name (name, priority, visible, description, image, parent)
            VALUES ('$name', '$priority', '$active', '$description', '0', '$parent')
             ");  	
}
else
{
    safe_query ("INSERT INTO $m_name (name, priority, visible, description, image, parent)
            VALUES ('$name', '$priority', '$active', '$description', '1', '$parent')
             ");  

    
    $fd = fopen($image, "r");
    $image_raw = addslashes(fread($fd, filesize($image)));
    fclose($fd);
    $size=getimagesize($image); 
    system("/usr/local/bin/djpeg -pnm $image | /usr/local/bin/pnmscale -xscale 0.1 -yscale 0.1 | /usr/local/bin/cjpeg > $image.tmb");
    $fd = fopen("$image.tmb", "r");
    $image_thumb = addslashes(fread($fd, filesize("$image.tmb")));
    fclose($fd);
    unlink("$image.tmb");

    
    
    $result = mysql_query ("SELECT id FROM $m_name ORDER BY id DESC LIMIT 1");
    if ($row = mysql_fetch_array($result)) 
    {
        do 
            {
            $filename1 = $doc_root . $m_name . "_image" . $row["id"] . ".jpg";
            $filename2 = $doc_root . $m_name . "_thumb" . $row["id"] . ".jpg";
            $fp = fopen ("$filename1", "w+");
            fwrite ($fp, stripslashes($image_raw));
            fclose ($fp);
            $fp = fopen ("$filename2", "w+");
            fwrite ($fp, stripslashes($image_thumb));
            fclose ($fp);
            $output =  "<img border=0  src=/img/" . $m_name . "_thumb" . $row["id"] 
            . ".jpg ><a href=" . $REQUEST_URI . "?editid=" . $row["id"] . ">edit</a><br><br>";
            } while($row = mysql_fetch_array($result));
        
    } else 
    {
        $output = ("broken");
    }



}

    $output = $output .  "The following category has been added:<br><br>name: " . $name . "<br>description: " . $description . "<br>";
    $output = $output . " <a href=" . $t_name . ".php?list=1&t_name=" . $t_name . ">click here for all items</a>"; 
    return $output;
}

/* 
    --------------------------------------
    FUNCTION: update_category()
    --------------------------------------
*/   

function update_category($t_name,$id,$name,$blurb,$description,$priority,$active,$image="none",$parent="")
{

$t_name = sanitize($t_name);
$name = sanitize($name);
$id = sanitize($id);
$blurb = sanitize($blurb);
$parent = sanitize($parent);
$description = sanitize($description);
$priority = sanitize($priority);
$active = sanitize($active);


dbconnect();

if (!$active)
{
    $active = '0';
}

$m_name = $t_name . "_category";
$DOCUMENT_ROOT = "/Library/Webserver/Documents/";
$doc_root = "$DOCUMENT_ROOT/img/";

if ($image=='none')
    {
        safe_query ("UPDATE $m_name 
                SET name =  '$name',
                priority = '$priority', 
                visible = '$active', 
                parent = '$parent',
                description = '$description'
                WHERE id LIKE $id"
                );  
        
    }
elseif ($image=='0')
    {
        $have_image = "0";
        safe_query ("UPDATE $m_name 
                SET name =  '$name',
                priority = '$priority', 
                visible = '$active', 
                parent = '$parent',
                description = '$description', 
                image = '$have_image'
                WHERE id LIKE $id"
                );  
        
    }
    else
    {
        $have_image = "1";
        safe_query ("UPDATE $m_name 
                SET name =  '$name',
                priority = '$priority', 
                visible = '$active',
                parent = '$parent',
                description = '$description', 
                image = '$have_image'
                WHERE id LIKE $id"
                );  
    
        
        $fd = fopen($image, "r");
        $image_raw = addslashes(fread($fd, filesize($image)));
        fclose($fd);
        $size=getimagesize($image); 
        system("/usr/local/bin/djpeg -pnm $image | /usr/local/bin/pnmscale -xscale 0.1 -yscale 0.1 | /usr/local/bin/cjpeg > $image.tmb");
        $fd = fopen("$image.tmb", "r");
        $image_thumb = addslashes(fread($fd, filesize("$image.tmb")));
        fclose($fd);
        unlink("$image.tmb");
        $filename1 = $doc_root . $m_name . "_image" . $row["id"] . ".jpg";
        $filename2 = $doc_root . $m_name . "_thumb" . $row["id"] . ".jpg";
        $fp = fopen ("$filename1", "w+");
        fwrite ($fp, stripslashes($image_raw));
        fclose ($fp);
        $fp = fopen ("$filename2", "w+");
        fwrite ($fp, stripslashes($image_thumb));
        fclose ($fp);
        $output =  "<img border=0  src=/img/" . $m_name . "_thumb" . $row["id"] 
        . ".jpg ><a href=" . $REQUEST_URI . "?editid=" . $row["id"] . ">edit</a><br><br>";
      
    
    }

    $output =  "The following category has been updated:<br><br>name: " . $name . "<br>description: " . $description . "<br>" . $output;
    $output = $output . " <a href=" . $t_name . ".php?list=1&t_name=" . $t_name . ">click here for all items</a>"; 
    return $output;

}


// string get_attlist ([array attributes [,array default attributes]])

// This function will take an associative array and format as a string
// that looks like 'name1="value1" name2="value2"', as is used by HTML tags.
// Values for keys in the first argument will override values for the
// same keys in the second argument. (For example, if $atts is (color=>red)
// and $defaults is (color=>black, size=3), the resulting output will
// be 'color="red" size="3"'.)
 
function get_attlist ($atts="",$defaults="")
{
	$localatts = array();
	$attlist = "";

	if (is_array($defaults)) { $localatts = $defaults; }
	if (is_array($atts)) { $localatts = array_merge($localatts, $atts); }

	while (list($name,$value) = each($localatts))
	{
		if ($value == "") { $attlist .= "$name "; }
		else { $attlist .= "$name=\"$value\" "; }
	}
	return $attlist;
}

// string make_page_title ([string title])

// This function will clean up a string to make it suitable for use
// as the value of an HTML <TITLE> tag, removing any HTML tags and
// replacing any HTML entities with their literal character equivalents.

function make_page_title ($title="")
{
	$title = sanitize($title);
	$trans = array_flip(get_html_translation_table(HTML_ENTITIES));
	$title = strtr($title, $trans); 
	return $title;
}

// string money ([mixed value])

// This function will format the first argument as a standard US dollars
// value, rounding any decimal value two decimal places for cents 
// and prepending a dollar sign to the returned string.

function money($val=0)
{
	return "$".number_format($val,2);
}


// string start_form ([string action [, array attributes]])

// This function returns an HTML <form> tag. If the first argument
// is empty, the value of the global Apache variable SCRIPT_NAME
// is used for the 'action' attribute of the <form> tag. Other
// attributes for the form can be specified in the optional second
// argument; the default method of the form is "post".

// The behavior of this function on servers other than Apache is
// not known. It's likely that it will work, as SCRIPT_NAME is
// part of the CGI 1.1 specification.

function start_form ($action="", $atts="")
{
	global $SCRIPT_NAME;

	if (empty($action)) { $action = $SCRIPT_NAME; }

	$attlist = get_attlist($atts,array("method"=>"post"));
	$output = <<<EOQ
<form action="$action" $attlist enctype="multipart/form-data">
EOQ;
	return $output;
}

// string end_form(void)

// This function returns an HTML </form> tag.

function end_form ()
{
	$output = <<<EOQ
</form>
EOQ;
	return $output;
}

// string text_field ([string name [, string value [, int size [, int maximum length]]]])

// This function returns an HTML text input field. The default size
// of the field is 10. A value and maximum data length for the field
// may be supplied.

function text_field ($name="", $value="", $size=10, $maxlen="")
{
	$maxatt = empty($maxlen) ? "" : "maxlength=\"$maxlen\"";
	$output = <<<EOQ
<input type="text" name="$name" value="$value" size="$size" $maxatt>\n
EOQ;
	return $output;
}

// string textarea_field([string name [, string value [, int cols [, int rows [, string wrap mode]]]]])

// This function returns an HTML textarea field. The default size is
// 50 columns and 10 rows, and the default wrap mode is 'soft', which means 
// no hard newline characters will be inserted after line breaks in what
// the user types into the field. The alternative wrap mode is 'hard',
// which means that hard newlines will be inserted.

function textarea_field ($name="", $value="", $cols=50, $rows=10, $wrap="soft")
{
	$output = <<<EOQ
<textarea name="$name" cols="$cols" rows="$rows" wrap="$wrap">$value</textarea>\n
EOQ;
	return $output;
}

// string password_field ([string name [, string value [, int size [, int maximum length]]]])

// This function returns an HTML password field. This is like a text field,
// but the value of the field is obscured (only stars or bullets are visible
// for each character).  The default size of the field is 10.  A starting
// value and maximum data length may be supplied.

function password_field ($name="", $value="", $size=10, $maxlen="")
{
	$output = <<<EOQ
<input type="password" name="$name" value="$value" size="$size" maxlength="$maxlen">\n
EOQ;
	return $output;
}

// string hidden_field ([string name [, string value]])

// This function returns an HTML hidden field. A value may be supplied.

function hidden_field ($name="", $value="")
{
	$output = <<<EOQ
<input type="hidden" name="$name" value="$value">\n
EOQ;
	return $output;
}

// string file_field ([string name])

// This function returns an HTML file field. These are used to specify
// files on the user's local hard drive, typically for uploading as
// part of the form. (See http://www.zend.com/manual/features.file-upload.php
// for more information about this subject.)

function file_field ($name="")
{
	$output = <<<EOQ
<input type="file" name="$name">
EOQ;
	return $output;
}

// string submit_field ([string name [, string value]])

// This function returns an HTML submit field. The value of the field
// will be the string displayed by the button displayed by the user's
// browser. The default value is "Submit".

function submit_field ($name="", $value="")
{
	if (empty($value)) { $value = "Submit"; }

	$output = <<<EOQ
<input type="submit" name="$name" value="$value">\n
EOQ;
	return $output;
}

// string image_field ([string name [, string src [, string value]]])

// This function returns an HTML image field. An image field works
// likes a submit field, except that the image specified by the URL
// given in the second argument is displayed instead of a button.

function image_field ($name="", $src="", $value="")
{
	if (empty($value)) { $value = $name; }

	$output = <<<EOQ
<input type="image" name="$name" value="$value" src="$src">\n
EOQ;
	return $output;
}

// string reset_field ([string name [, string value]])

// This function returns an HTML reset field. A reset field returns
// the current form to its original state.

function reset_field ($name="reset", $value="Reset")
{
	$output = <<<EOQ
<input type="reset" name="$name" value="$value">
EOQ;
	return $output;
}

// string checkbox_field ([string name [, string value [, string label [, string match]]]])

// This function returns an HTML checkbox field. The optional third argument
// will be included immediately after the checkbox field, and the pair
// is included inside a HTML <nobr> tag - meaning that they will be
// displayed together on the same line.  If the value of the
// second or third argument matches that of the fourth argument,
// the checkbox will be 'checked' (i.e., flipped on).

function checkbox_field ($name="", $value="", $label="", $match="")
{
	$checked = ($value == $match || $label == $match) ? "checked" : "";
	$output = <<<EOQ
<input type="checkbox" name="$name" value="$value" $checked> $label\n
EOQ;
	return $output;
}

// string radio_field ([string name [, string value [, string label [, string match]]]])

// This function returns an HTML radio button field. The optional third 
// argument will be included immediately after the radio button, and the pair
// is included inside a HTML <nobr> tag - meaning that they will be
// displayed together on the same line.  If the value of the
// second or third argument matches that of the fourth argument,
// the radio button will be 'checked' (i.e., flipped on).

function radio_field ($name="", $value="", $label="", $match="")
{
	$checked = ($value == $match || $label == $match) ? "checked" : "";
	$output = <<<EOQ
<input type="radio" name="$name" value="$value" $checked> $label\n
EOQ;
	return $output;
}

// string select_field ([string name [, array items [, string default value]]])

// This function returns an HTML select field (a popup field).
// If the optional second argument is an array, each key in the array
// will be set to the value of an option of the select field, and
// the corresponding value from the array will be the displayed string
// for that option. If the key or the value from the array matches
// the optional third argument, that option will be designated as the default
// value of the select field.

function select_field ($name="", $array="", $value="")
{
	$output = <<<EOQ
<select name="$name">
EOQ;
	if (is_array($array))
	{
		while (list($avalue,$alabel) = each($array))
		{
			$selected = ($avalue == $value || $alabel == $value) ? 
				"selected" : ""
			;
			$output .= <<<EOQ
<option value="$avalue" $selected>$alabel</option>\n
EOQ;
		}
	}
	$output .= <<<EOQ
</select>\n
EOQ;
	return $output;
}

// string db_select_field ([string name [, string table name [, string value field [, string label field [, string sort field [, string match text [, string where clause]]]]]]])

// This function returns an HTML select field (popup field), based
// on the values in the MySQL database table specified by the second argument,
// as returned by the db_values_array() function (defined in 
// /book/functions/db.php).

function db_select_field ($name="", $table="", $value_field=""
	, $label_field="", $sort_field="", $match="", $where=""
)
{
	$values = db_values_array($table, $value_field, $label_field
		, $sort_field, $where
	);
	$output = select_field($name, $values, $match);
	return $output;
}

// string db_radio_field (string name, string table name, string value field, string label field, string sort field, [string match text], [string where clause])

// This function returns a list of HTML radio button fields, separated
// by a non-breaking space HTML entity (&nbsp;) and a newline, based
// on the values in the MySQL database table named by the second
// argument, as returned by the db_values_array() function (defined in 
// /book/functions/db.php).

function db_radio_field ($name="", $table="", $value_field=""
	, $label_field="", $sort_field="", $match="", $where=""
)
{
	$values = db_values_array($table, $value_field, $label_field
		, $sort_field, $where
	);

	$output = "";
	while (list($value, $label) = each($values))
	{
		$output .= radio_field($name, $value, $label, $match)
			."&nbsp;\n"
		;
	}
	return $output;
}

// string anchor_tag ([string href [, string text [, array attributes]]])

// This function returns an HTML anchor tag (<a>).  The first argument
// be the URL to which the tag points, and the second argument will
// be the text of the tag. Additional attributes may be supplied as
// an array in the third argument.

function anchor_tag($href="",$text="",$atts="")
{
	$attlist = get_attlist($atts,array("href"=>$href));
	$output = "<a $attlist>$text</a>";
	return $output;
}

// string image_tag ([string src [,array attributes]])

// This function returns an HTML image tag (<img>). The first argument
// gives theURL of the image to be displayed. Additional attributes
// may be supplied as an array in the third argument.

function image_tag($src="",$atts="")
{
	$attlist = get_attlist($atts,array("src"=>$src));
	$output = "<img $attlist>";
	return $output;
}

// string subtitle ([string text of subtitle])

// This function returns an HTML <h3> tag. It is used for the titles
// of secondary areas within pages in our examples. The reason to 
// display these via a function, rather than just literal <h3> tags,
// is to enable you to change the format of these subtitles in one
// place, instead of in each script.

function subtitle($what="")
{
	return "<h3>$what</h3>\n";
}

// string paragraph ([array attributes [, mixed ...]])

// This function will return a string inside HTML paragraph (<p>) tags.
// Attributes for the <p> tag may be supplied in the first argument.
// Any additional arguments will be included inside the opening and
// closing <p> tags, separated by newlines.

function paragraph ($atts="")
{
	$output = "<p";
	$i = 0;
	$attlist = get_attlist($atts);
	if ($attlist > "") 
	{ 
		$output .= " $attlist"; 
		$i++;
	}
	$output .= ">\n";
	$args = func_num_args();
	while ($i < $args)
	{
		$x = func_get_arg($i);
		$output .= $x."\n";
		$i++;
	}
	$output .= "</p>\n";
	return $output;
}

// string ul_list ([mixed values])

// This function returns an HTML unordered (bulleted) list (<ul> tags). 
// If the argument is an array, then each value from the array will be
// included as a list item (<li>) in the list. Otherwise, the
// argument will simply be included inside the <ul> tags as is.

function ul_list ($values="")
{
	$output .= "<ul>\n";
	if (is_array($values))
	{
		while (list(,$value) = each($values))
		{
			$output .= " <li>$value\n";
		}
	}
	else
	{
		$output .= $values;
	}
	$output .= "</ul>\n";
	return $output;
}

// string start_table ([array attributes])


// This function returns an opening HTML <table> tag, inside an
// opening paragraph (<p>) tag. Attributes for the table may be supplied 
// as an array.

function start_table ($atts="")
{
	$attlist = get_attlist($atts);
	$output = <<<EOQ
<p>
<table $attlist>
EOQ;
	return $output;
}

// string end_table (void)

// This function returns a closing <table> tag, followed by a closing
// paragraph (<p>) tag. (Presumably closing the paragraph opened by
// start_table().)

function end_table ()
{
	$output = <<<EOQ
</table>
</p>
EOQ;
	return $output;
}

// string table_cell ([string value [, array attributes]])

// This function returns an HTML table cell (<td>) tag. The first
// argument will be used as the value of the tag. Attributes for the
// <td> tag may be supplied as an array in the second argument.
// By default, the table cell will be aligned left horizontally,
// and to the top vertically.

function table_cell ($value="",$atts="")
{
	$attlist = get_attlist($atts,array("align"=>"left","valign"=>"top"));

	$output = <<<EOQ
  <td $attlist>$value</td>
EOQ;
	return $output;
}

// string table_row ([mixed ...])

// This function returns an HTML table row (<tr>) tag, enclosing a variable
// number of table cell (<td>) tags. If any of the arguments to the function
// is an array, it will be used as attributes for the <tr> tag. All other
// arguments will be used as values for the cells of the row. If an
// argument begins with a <td> tag, the argument is added to the row as is.
// Otherwise it is passed to the table_cell() function and the resulting
// string is added to the row.

function table_row ($cellstring="",$attlist="")
{
#	$attlist = "";
#	$cellstring = "";

	$cells = func_get_args();
	while (list(,$cell) = each($cells))
	{
		if (is_array($cell))
		{
			$attlist .= get_attlist($cell);
		}
		else
		{
			if (!eregi("<td",$cell))
			{
				$cell = table_cell($cell);
			}
			$cellstring .= "  ".trim($cell)."\n";
		}
	}
	$output = <<<EOQ
 <tr $attlist>
$cellstring
 </tr>
EOQ;
	return $output;
}

function fetch_record ($table, $key="", $value="")
{
	$query = "select * from $table ";
	if (!empty($key) && !empty($value))
	{
		if (is_array($key) && is_array($value))
		{
			$query .= " where ";
			$and = "";
			while (list($i,$v) = each($key))
			{
				$query .= "$and $v = ".$value[$i];
				$and = " and";
			}
		}
		else
		{
			$query .= " where $key = $value ";
		}
	}
	$result = safe_query($query);
	if (!empty($key) && !empty($value))
	{
		set_result_variables($result);
	}
	return $result;
}

// array db_values_array ([string table name [, string value field [, string label field [, string sort field [, string where clause]]]]])

// This function builds an associative array out of the values in
// the MySQL table specified in the first argument. The data from the column 
// named in the second argument will be set to the keys of the array.
// If the third argument is not empty, the data from the column it names
// will be the values of the array; otherwise, the values will be equal
// to the keys. If the third argument is not empty, the data will be
// ordered by the column it names; otherwise, it will be ordered by
// the key column. The optional fourth argument specifies any additional
// qualification for the query against the database table; if it is empty,
// all rows in the table will be retrieved.

// If either the first or second argument is empty, no query is run and
// an empty array is returned.

// The function presumes that whoever calls it knows what they're about -
// e.g., that the table exists, that all the column names are correct, etc.

function db_values_array ($table="", $value_field="", $label_field=""
	, $sort_field="", $where=""
)
{
	$values = array();

	if (empty($table) || empty($value_field)) { return $values; }

	if (empty($label_field)) { $label_field = $value_field; }
	if (empty($sort_field)) { $sort_field = $label_field; }
	if (empty($where)) { $where = "1=1"; }

	$query = "select $value_field 
		, $label_field 
		from $table 
		where $where
		order by $sort_field
	";
	$result = safe_query($query);
	if ($result)
	{
		while (list($value,$label) = mysql_fetch_array($result))
		{
			$values[$value] = $label;
		}
	}
	return $values;
}

/* 
    --------------------------------------
    FUNCTION: sanitize()
    string sanitize (string chunk([save]([keep]))
    --------------------------------------
*/                
function sanitize($chunk="",$save="",$keep="")
{
    if (empty($save))
    {
        $chunk = strip_tags($chunk, $keep);
    }      
    $chunk = addslashes(htmlspecialchars($chunk));              
    return $chunk;    
}





?>
Return current item: Slacker