kb-manage.php3 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2005 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");

#
# No Standard Testbed Header; going to spit out a redirect later.
#

# Some constants.
define("TBDB_KB_SECTIONLEN",	80);
define("TBDB_KB_TITLELEN",	140);
define("TBDB_KB_XREFTAGLEN",	30);
define("TBDB_KB_BODYLEN",	10000);

#
# Only known and logged in users.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

# Summary data for admins only.
if (!ISADMIN()) {
    USERERROR("You are not allowed to view this page!", 1);
}

#
# Spit the form out using the array of data. 
# 
function SPITFORM($formfields, $errors)
{
    global	$idx, $action;
    
    PAGEHEADER("Add/Modify a Knowledge Base Entry");

    if (isset($idx)) {
	if ($action == "edit") {
	    echo "<center><h3>Modify Entry</h3>\n";
	}
	elseif ($action == "delete") {
	    echo "<center><h3>Delete Entry</h3>\n";
	}
	else {
	    echo "<center><h3>Add Entry</h3>\n";
	}
    }
    else {
	echo "<center><h3>Add Entry</h3>\n";
    }
    echo "(<a href=kb-search.php3>Search For Entries</a>)</center>\n";
    
    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }

    echo "<br>
          <table align=center border=1> 
          <tr>
             <td align=center colspan=2>
                 <em>(Fields marked with * are required)</em>
             </td>
          </tr>
          <form action='kb-manage.php3' method=post>\n";

    $sections_result = 
	DBQueryFatal("select distinct section from knowledge_base_entries");

    #
    # If the form is being edited, then throw up some extra detail.
    #
    if (isset($idx)) {
	echo "<input type='hidden' name='idx' value='$idx'>\n";
	echo "<input type='hidden' name='action' value='$action'>\n";

	echo "<tr>
                  <td>Posted by:</td>
                  <td>$formfields[creator_uid]</td>
              </tr>\n";
	echo "<tr>
                  <td>Posted on:</td>
                  <td>$formfields[date_created]</td>
              </tr>\n";
	if (isset($formfields[date_modified])) {
	    echo "<tr>
                     <td>Last Modified by:</td>
                     <td>$formfields[modifier_uid]</td>
                  </tr>\n";
	    echo "<tr>
                      <td>Modified on:</td>
                      <td>$formfields[date_modified]</td>
                  </tr>\n";
	}
	echo "<tr></tr>\n";
    }

    #
    # Select Section. 
    #
    echo "<tr>
              <td>*Section Name:</td>
	      <td><select name=\"formfields[section]\">
		          <option value=none>Please Select </option>\n";

    while ($section_row = mysql_fetch_array($sections_result)) {
	$section  = $section_row['section'];
	$selected = "";

	if (isset($formfields[section]) &&
	    strcmp($formfields[section], $section) == 0)
	    $selected = "selected";

	echo "<option $selected value='$section'>$section &nbsp </option>\n";
    }
    echo "       </select>\n";

    # And allow for a new section name to be created
    echo "    or add: ";
    echo "    <input type=text
                     name=\"formfields[new_section]\"
                     value=\"" . $formfields[new_section] . "\"
   	             size=50>\n";
    echo "   </td>
	  </tr>\n";

    #
    # Title
    #
    echo "<tr>
              <td>*Title:<br>
                  (a short pithy sentence)</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[title]\"
                         value=\"" . $formfields[title] . "\"
	                 size=70>
              </td>
          </tr>\n";


    #
    # Make it a FAQ entry?
    #
    echo "<tr>
  	      <td>Faq Entry?</td>
               <td class=left>
                   <input type=checkbox
                          name=\"formfields[faq_entry]\"
                          value=1";

    if (isset($formfields[faq_entry]) && $formfields[faq_entry] == "1")
	echo "           checked";
	
    echo "                       > Yes
              </td>
          </tr>\n";
    
    #
    # Cross Reference Tag
    #
    echo "<tr>
              <td>Cross Reference Tag</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[xref_tag]\"
                         value=\"" . $formfields[xref_tag] . "\"
	                 size=20>
              </td>
          </tr>\n";

    #
    # Body
    #
    echo "<tr>
              <td colspan=2>
               *Please enter (HTML) text of entry
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[body]\"
                    rows=30 cols=80>$formfields[body]</textarea>
              </td>
          </tr>\n";

    echo "<tr>
              <td align=center colspan=2>
                  <b><input type=submit name=submit ";
    if (isset($idx)) {
	if ($action == "edit") {
	    echo "value=Modify></b>";
	}
	elseif ($action == "delete") {
	    echo "value=Delete></b>";
	}
	else {
	    echo "value=Submit></b>";
	}
    }
    else {
	echo "value=Submit></b>";
    }
    echo "   </td>
          </tr>\n";

    echo "</form>
          </table>\n";

    echo "<br>
          <blockquote><blockquote>

          </blockquote></blockquote>\n";
}

#
# For delete/modify, check record.
# 
if (isset($idx)) {
    if (!isset($action) || !($action == "edit" || $action == "delete")) {
	PAGEARGERROR("Invalid action");
    }
    if (! TBvalid_integer($idx)) {
	PAGEARGERROR("Invalid characters in $idx");
    }
    $query_result =
	DBQueryFatal("select * from knowledge_base_entries ".
		     "where idx='$idx'");
    if (! mysql_num_rows($query_result)) {
	USERERROR("No such knowledge_base entry: $idx", 1);
    }
    $defaults = mysql_fetch_array($query_result);
}

#
# On first load, display a virgin form and exit.
#
if (! $submit) {
    #
    # If doing an edit of an existing entry, then need to get that from
    # the DB. 
    #
    if (!isset($idx)) {
	$defaults = array();

        #
        # Allow formfields that are already set to override defaults
        #
	if (isset($formfields)) {
	    while (list ($field, $value) = each ($formfields)) {
		$defaults[$field] = $formfields[$field];
	    }
	}
    }
    
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
}

#
# Deal with deletion.
#
if (isset($submit) && $submit == "delete" && isset($idx)) {
    DBQueryFatal("delete from knowledge_base_entries where idx='$idx'");
    PAGEFOOTER();
    return;
}

#
# Otherwise, must validate and redisplay if errors
#
$errors = array();

#
# Section Name.
#
if (isset($formfields[new_section]) && $formfields[new_section] != "") {
    if (! TBvalid_userdata($formfields[new_section])) {
	$errors["Section"] = "Invalid characters";
    }
    elseif (strlen($formfields[new_section]) > TBDB_KB_SECTIONLEN) {
	$errors["Section"] =
	    "Too long! ".
	    "Must be less than or equal to " . TBDB_KB_SECTIONLEN;
    }
    $section = addslashes($formfields[new_section]);
}
elseif (isset($formfields[section]) &&
	$formfields[section] != "" && $formfields[section] != "none") {
    
    if (! TBvalid_userdata($formfields[section])) {
	$errors["Section"] = "Invalid characters";
    }
    elseif (strlen($formfields[section]) > TBDB_KB_SECTIONLEN) {
	$errors["Section"] =
	    "Too long! ".
	    "Must be less than or equal to " . TBDB_KB_SECTIONLEN;
    }
    $section = addslashes($formfields[section]);
}
else {
    $errors["Section"] = "Missing Field";
}

#
# Title
#
if (isset($formfields[title]) && $formfields[title] != "") {
    if (! TBvalid_userdata($formfields[title])) {
	$errors["Title"] = "Invalid characters";
    }
    elseif (strlen($formfields[title]) > TBDB_KB_TITLELEN) {
	$errors["Title"] =
	    "Too long! ".
	    "Must be less than or equal to " . TBDB_KB_TITLELEN;
    }
    $title = addslashes($formfields[title]);
}
else {
    $errors["Title"] = "Missing Field";
}

#
# Faq Entry?
#
if (isset($formfields[faq_entry]) && $formfields[faq_entry] == "1") {
    $faq_entry = 1;
}
else {
    $faq_entry = 0;
}

#
# Cross Reference Tag
#
if (isset($formfields[xref_tag]) && $formfields[xref_tag] != "") {
    if (! preg_match("/^[-\w]+$/", $formfields[xref_tag])) {
	$errors["Cross Reference Tag"] = "Invalid characters";
    }
    elseif (strlen($formfields[xref_tag]) > TBDB_KB_XREFTAGLEN) {
	$errors["Cross Reference Tag"] =
	    "Too long! ".
	    "Must be less than or equal to " . TBDB_KB_XREFTAGLEN;
    }
    $xref_tag = addslashes($formfields[xref_tag]);
    $xref_tag = "'$xref_tag'";
    
    #
    # Make sure the xref_tag is not already in use (by another entry).
    #
    $xref_result =
	DBQueryFatal("select idx from knowledge_base_entries ".
		     "where xref_tag=$xref_tag ".
		     (isset($idx) ? "and idx!='$idx'" : ""));
    
    if (mysql_num_rows($xref_result)) {
	$errors["Cross Reference Tag"] = "Already in use";
    }
}
else {
    $xref_tag = "NULL";
}

#
# Body
#
if (isset($formfields[body]) && $formfields[body] != "") {
    if (! TBvalid_fulltext($formfields[body])) {
	$errors["Body"] = "Invalid characters";
    }
    elseif (strlen($formfields[body]) > TBDB_KB_BODYLEN) {
	$errors["Body"] =
	    "Too long! ".
	    "Must be less than or equal to " . TBDB_KB_BODYLEN;
    }
    $body = addslashes($formfields[body]);
}
else {
    $errors["Body"] = "Missing Field";
}

#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
# 
if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}

#
# And insert the entry.
#
if (isset($idx)) {
    if ($action == "delete") {
	DBQueryFatal("delete from knowledge_base_entries ".
		     "where idx='$idx'");
    }
    else {
	DBQueryFatal("update knowledge_base_entries set ".
		     "  modifier_uid='$uid', ".
		     "  date_modified=now(), ".
		     "  section='$section', ".
		     "  title='$title', ".
		     "  body='$body', ".
		     "  faq_entry=$faq_entry, ".
		     "  xref_tag=$xref_tag ".
		     "where idx='$idx'");
    }
}
else {
    DBQueryFatal("insert into knowledge_base_entries set ".
		 "  idx=NULL, ".
		 "  creator_uid='$uid', ".
		 "  date_created=now(), ".
		 "  section='$section', ".
		 "  title='$title', ".
		 "  body='$body', ".
		 "  faq_entry=$faq_entry, ".
		 "  xref_tag=$xref_tag");

    $query_result = DBQueryFatal("select last_insert_id()");
    $row = mysql_fetch_array($query_result);
    $idx = $row[0];
}

if (isset($idx) && $action != "delete") {
    #
    # Okay, redirect the user over to the show page so they can see it.
    #
    header("Location: kb-show.php3?idx=$idx");
}
else {
    # Redirect to the search page.
    header("Location: kb-search.php3");
}
?>