-35)) { write_log(0, $_SESSION["logged_in"], "Create Node", "Invalid Coordinates"); $_SESSION["msg"] = "error. Your coordinates seem to be _non-victorian_"; $_SESSION["msg_type"] = 0; return false; // if supplied coords are outside the state } elseif (((int)$node_long < 130) || ((int)$node_long > 150)) { write_log(0, $_SESSION["logged_in"], "Create Node", "Invalid Coordinates"); $_SESSION["msg"] = "error. Your coordinates seem to be _non-victorian_"; $_SESSION["msg_type"] = 0; return false; // if we get past all that, then add the node } else { // select the highest ID from the DB $sql = "SELECT id FROM nodes ORDER BY id DESC LIMIT 1"; $loc_db->query( $sql ); // If the database was empty, then start with the first node if ($loc_db->num_rows() == 0) { $id = "AAA"; // otherwise, we need to create the ID ourselves } else { $loc_db->next_record(); // grab the ascii code for each of the IDs characters $letter1 = ord(substr($loc_db->f("id"),0,1)); $letter2 = ord(substr($loc_db->f("id"),1,2)); $letter3 = ord(substr($loc_db->f("id"),2,3)); // increment the last letter $letter3++; // if the last letter is past Z if ($letter3 > 90) { // take it back to A $letter3 = 65; // and increment the 2nd letter $letter2++; // but if it goes past Z as well if ($letter2 > 90) { // take _it_ back to A $letter2 = 65; // and increment the first letter $letter1++; // once we hit ZZZ, we'll start getting ids with // symbols and the like for the first char! // since we cull, the code could theoretically be changed // to start filling in the gaps // i can't imagine us ever having over 17 576 nodes! } } // change the ascii codes back into letters $node_id = chr($letter1) . chr($letter2) . chr($letter3); } // put the SQL together to insert the new node $sql = "INSERT INTO nodes(id, owner, name, suburb, latitude, longitude, url, status, area, updated) VALUES('$node_id', '" . $_SESSION["logged_in"] . "', '$node_name', '$node_suburb', $node_lat, $node_long, '$node_url', '$node_status', $area, now())"; $loc_db->query( $sql ); // update the node shp files to show the new node update_shp_nodes(); write_log(1, $_SESSION["logged_in"], "Create Node", "Node" . $node_id); $_SESSION["msg"] = "Node successfully created"; $_SESSION["msg_type"] = 1; // return the new nodes ID return $node_id; } // end validation IF } // end create_node() /* * update_node() * * takes a bunch of arguments that are the nodes general info * validates the data, then if it's all fine, updates the database * * Usage: a bunch of variables passed.... * * Returns: true or false, and sets a global variable $err_message or * $success_message to report the results of the function * */ function update_node($node_id, $node_name, $node_status, $node_suburb, $node_long, $node_lat, $area, $node_url, $notify_nearby) { // create database classes $loc_db = new locDatabase; $loc_db_2 = new locDatabase; // if required fields were missed if (($node_long == "") || ($node_lat == "")) { write_log(0, $_SESSION["logged_in"], "Update Node", "Node" . $node_id . " - Missing required fields"); $_SESSION["msg"] = "error. you missed a required field."; $_SESSION["msg_type"] = 0; return false; // if supplied coords are outside the state } elseif (((int)$node_lat < -39) || ((int)$node_lat > -35)) { write_log(0, $_SESSION["logged_in"], "Update Node", "Node" . $node_id . " - Invalid Coordinates"); $_SESSION["msg"] = "error. your coordinates appear to be _non victorian_."; $_SESSION["msg_type"] = 0; return false; // if supplied coords are outside the state } elseif (((int)$node_long < 130) || ((int)$node_long > 150)) { write_log(0, $_SESSION["logged_in"], "Update Node", "Node" . $node_id . " - Invalid Coordinates"); $_SESSION["msg"] = "error. your coordinates appear to be _non victorian_"; $_SESSION["msg_type"] = 0; return false; // if we pass all those tests, then the data is okay! } else { // select the nodes current info from the DB // so we can test for changes and perform // various jobs if it has $sql = "SELECT * FROM nodes WHERE id='" . $node_id . "'"; $loc_db->query( $sql ); $loc_db->next_record(); // assign the old details to variables $old_status = $loc_db->f("status"); $old_area = (int)$loc_db->f("area"); // if the user has changed their Area, their IP Allocations // are invalid, since allocations must come from the region // they are currently in, so we have to delete their existing // allocations if (($old_area != -1) && ((int)$area != $old_area)) { flush_assignments($node_id, true, true, false); $tmp_success_message .= "

please note.. since you changed your OSPF Area, you will have to re-request IP addresses that come from your new Area"; } // if the node status has been operational or testing, and the user // changes it to something lower, they can no longer have // services, interfaces, links or IP allocations, so they must all // be deleted if ((($old_status == "operational") || ($old_status == "testing")) && (($node_status == "building") || ($node_status == "gathering") || ($node_status == "interested"))) { flush_assignments($node_id, true, true, true); flush_interfaces($node_id); flush_services($node_id); $tmp_success_message .= "

please note.. since you changed your status to lower than testing, your services, interfaces and links have been deleted..."; } $sql = "UPDATE nodes SET name='$node_name', status='$node_status', suburb='$node_suburb', latitude=$node_lat, longitude=$node_long, area=$area, url='$node_url', notify_nearby=$notify_nearby WHERE id='$node_id'"; // update the database! $loc_db->query( $sql ); // if the user has changed their node status, rewrite the node // shp file so that the map will reflect the change if ($node_status != $old_status) { $tmp_success_message .= "

please note.. since you changed your status, the mapping data files have been rewritten"; update_shp_nodes(); } write_log(1, $_SESSION["logged_in"], "Update Node", "Node" . $node_id); // set the message to be returned to the user $_SESSION["msg"] = "general node information updated..." . $tmp_success_message; $_SESSION["msg_type"] = 1; // function was sucessful return true; } // end validation IF } // end update_node() /* * flush_links() * * takes a node ID, and deletes all the links in the database for this * node * * Usage: $node_id - the nodes ID * * Returns: true or false * */ function flush_links($node_id) { // create database classes $loc_db = new locDatabase; $loc_db_2 = new locDatabase; // select all the interfaces that belong to this node $sql = "SELECT * FROM interfaces where node='$node_id'"; $loc_db->query( $sql ); // then cycle through each interface and delete any links attached // to it while ($loc_db->next_record()) { // delete any links with an end that belongs to the node // we're flushing $sql = "DELETE FROM links WHERE ((interface_1 = " . $loc_db->f("id") . ") OR (interface_2 = " . $loc_db->f("id") . "))"; $loc_db_2->query( $sql ); } // update the links shp file to reflect the removed links update_shp_links(); // function successful return true; } // end flush_links() /* * flush_services() * * takes a node ID, and deletes all the services in the database for * this node * * Usage: $node_id - the nodes ID * * Returns: true or false * */ function flush_services($node_id) { // create Database class $loc_db = new locDatabase; // delete all services that belong to this node $sql = "DELETE FROM services WHERE (node = '$node_id')"; $loc_db->query( $sql ); // function sucessful return true; } // end flush_services() /* * flush_interfaces() * * takes a node ID, and deletes all the links and interfaces in the * database for this node * * Usage: $node_id - the nodes ID * * Returns: true or false * */ function flush_interfaces($node_id) { // delete all links first, as if we delete the interfaces // first, then the links will be 'orphaned' and we won't // be able to select them flush_links($node_id); // create database class $loc_db = new locDatabase; // delete all interfaces that belong to the node $sql = "DELETE FROM interfaces WHERE (node = '$node_id')"; $loc_db->query( $sql ); // function successfull return true; } // end flush_interfaces() /* * delete_node() * * takes a node ID, and deletes all references to it from the DB * including ip allocations, links, interfaces and services * * Usage: $node_id - the nodes ID * * Returns: true or false, and sets a Global variable, $success_message * if it suceeds * */ function delete_node($node_id) { // create database class $loc_db = new locDatabase; // delete all the extras in the database that relate to // this node flush_assignments($node_id, true, true); flush_links($node_id); flush_interfaces($node_id); flush_services($node_id); // delete the node from the main table $sql = "DELETE FROM nodes WHERE (id='$node_id')"; $loc_db->query( $sql ); if (isset($_SESSION["logged_in"])) { write_log(1, $_SESSION["logged_in"], "Delete Node", "Node" . $node_id); } else { write_log(1, "SYSTEM", "Delete Node", "Node" . $node_id); } $_SESSION["msg"] = "node" . $node_id . " has been deleted..."; $_SESSION["msg_type"] = 1; // function successful return true; } // end delete_node() /* * touch_node() * * takes a node ID, and a hash value, and if the hash is correct * it resets the nodes 'updated' field to the current day. * * Usage: $node_id - the nodes ID * $hash - a md5 value * * Returns: true or false, and sets a Global variable, $success_message * or $err_message to inform the user of the results * */ function touch_node($node_id, $hash) { // declare global variables GLOBAL $opt_server; // create the database class $loc_db = new locDatabase; // select the nodes information from the DB $sql = "SELECT users.password FROM nodes, users where node.id='" . $node_id . "'";; $loc_db->query( $sql ); $loc_db->next_record(); // if the hash is incorrect if ($hash != $loc_db->f("password")) { write_log(1, "SYSTEM", "Touch Node", "Node" . $node_id . " - Incorrect Hash"); // the function failed, and inform the user why $_SESSION["msg"] = "error. Incorrect Hash"; $_SESSION["msg_type"] = 0; return false; // otherwise, the hash is correct } else { // so tell the database that the node was updated today updated_today($node_id); write_log(1, "SYSTEM", "Touch Node", "Node" . $node_id); $_SESSION["msg"] = "Node age successfully reset"; $_SESSION["msg_type"] = 1; // function successful return true; } // end hash validation if } // end touch_node() /* * updated_today() * * takes a node ID, and resets its 'updated' value to the current day * so that its age is reset to 0 * * Usage: $node_id - the nodes ID * * Returns: true or false * */ function updated_today($node_id) { // create database class $loc_db = new locDatabase; // update the field $sql = "UPDATE nodes SET updated=now() WHERE id='" . $node_id . "'"; $loc_db->query( $sql ); // function successful return true; } // end updated_today() /* * create_link() * * takes 2 variables, the interface id's of each end of the link * and records a link between these interfaces in the database and * map data. Makes no difference which end is which * * Usage: $interface_1 - the interface ID of one end of the link * $interface_2 - the interface ID of the other end * * Returns: true or false, and sets a global variable $success_message * to let the user know how the operation went * */ function create_link($interface_1, $interface_2, $class, $bandwidth) { // create DB class $loc_db = new locDatabase; // build the SQL statement to insert the link into the DB $sql = "INSERT INTO links(interface_1, interface_2, class, bandwidth) VALUES('$interface_1', '$interface_2', '$class', $bandwidth)"; $loc_db->query( $sql ); // rewrite the link shp files to show the new link update_shp_links(); $_SESSION["msg"] = "Link successfully created"; $_SESSION["msg_type"] = 1; // function successful return true; } // end create_link() /* * delete_link() * * takes a link ID and deletes it from the database, then rewrites * the shp files * * Usage: $link_id - the ID of the link to delete * * Returns: true or false, and sets a global variable $success_message * to let the user know how the operation went * */ function delete_link($link_id) { // create the DB class $loc_db = new locDatabase; // build SQL statement $sql = "DELETE FROM links WHERE id=" . $link_id; $loc_db->query( $sql ); // rewrite the link shp files to remove the link from maps update_shp_links(); // let the user know the deletion was successful $_SESSION["msg"] = "Link succesfully deleted"; $_SESSION["msg_type"] = 1; // function successful return true; } // end delete_link() /* * create_interface() * * takes a bunch of variables, validates them and if all is hunky dory * adds an interface to the DB * * Usage: lots of variables * * Returns: true or false, and sets a Global variable, $success_message * or $err_message to inform the user of the results * */ function create_interface($node_id, $card_power, $card_receive, $card_manufacturer, $antenna_dbi, $antenna_type, $cable_loss, $mode, $class, $mac, $channel) { // create the DB class $loc_db = new locDatabase; // set all the fields left blank by the user to their default if ($mac == "") $mac = "unknown"; if ($channel == "") $channel = -1; if ($card_power == "") $card_power = -1; if ($card_receive == "") $card_receive = -1; if ($antenna_dbi == "") $antenna_dbi = 1; if ($cable_loss == "") $cable_loss = -1; // Begin validating the data // if the channel is outside valid values if ((((int)$channel > 13) || ((int)$channel < 0)) && ($channel != -1)) { $_SESSION["msg"] = "error. Invalid channel"; $_SESSION["msg_type"] = 0; return false; // if the card power is outside valid values } elseif ((((int)$card_power > 30) || ((int)$card_power < 0)) && ($card_power != -1)) { $_SESSION["msg"] = "error. Invalid card power"; $_SESSION["msg_type"] = 0; return false; // if the card recieve is positive // card recieves should almost always be close to -80 or -90 // but a value of 0 allows the user to leave blank field, // and it doesn't hurt to be lenient } elseif ((int)$card_receive > 0) { $_SESSION["msg"] = "error. invalid card receive sensitivity"; $_SESSION["msg_type"] = 0; return false; // if the antenna's gain is outside valid values // an antenna of 40dBi gain would be illegal, but again, leniency // doesn't hurt. Means less errors for the user } elseif ((((int)$antenna_dbi > 40) || ((int)$antenna_dbi < 0)) && ($antenna_dbi != -1)) { $_SESSION["msg"] = "error. invalid antenna gain"; $_SESSION["msg_type"] = 0; return false; // If the cable loss value is outside a valid range // if your losing 30dB of power in your cable, you have a problem! } elseif ((((int)$cable_loss > 30) || ((int)$cable_loss < 0)) && ($cable_loss!= -1)) { $_SESSION["msg"] = "error. invalid cable loss"; $_SESSION["msg_type"] = 0; return false; // if something has been entered into the mac field, mac sure // it is a proper mac address\ } elseif ((!ereg("^([0-9|a-f]{2}:){5}[0-9|a-f]{2}$", $mac)) && ($mac != "unknown")) { $_SESSION["msg"] = "error. invalid mac address. Must be in the form 'xx:xx:xx:xx:xx:xx'"; $_SESSION["msg_type"] = 0; return false; // otherwise, all the data is fine! } else { // build the SQL statement to insert the new interface $sql = "INSERT INTO interfaces(node, card_power, card_receive, card_manufacturer, antenna_dbi, antenna_type, cable_loss, mode, class, mac, channel) VALUES('" . strtoupper($node_id) . "', " . (int)$card_power . ", " . (int)$card_receive . ", '$card_manufacturer'," . (int)$antenna_dbi . ", '$antenna_type'," . (int)$cable_loss . ", '$mode', '$class', '$mac', " . (int)$channel . ")"; $loc_db->query( $sql ); // let the user know the operation was successful $_SESSION["msg"] = "interface successfully added..."; $_SESSION["msg_type"] = 1; // function sucessful! return true; } // end validation if } // end create_interface() /* * update_interface() * * takes a bunch of variables, validates them and if all is hunky dory * updates the specified interface in the DB * * Usage: lots of variables * * Returns: true or false, and sets a Global variables, $success_message * or $err_message to inform the user of the results * */ function update_interface($int_id, $node_id, $card_power, $card_receive, $card_manufacturer, $antenna_dbi, $antenna_type, $cable_loss, $mode, $class, $mac, $channel) { // create the DB class $loc_db = new locDatabase; // set all the fields left blank by the user to their default if ($mac == "") $mac = "unknown"; if ($card_power == "") $card_power = -1; if ($card_receive == "") $card_receive = -1; if ($channel == "") $channel = -1; if ($antenna_dbi == "") $antenna_dbi = -1; if ($cable_loss == "") $cable_loss = -1; // if the card power is outside valid values if ((((int)$channel > 13) || ((int)$channel < 0)) && ($channel != -1)) { $_SESSION["msg"] = "error. Invalid channel"; $_SESSION["msg_type"] = 0; return false; // if the card power is outside valid values } elseif ((((int)$card_power > 30) || ((int)$card_power < 0)) && ($card_power != -1)) { $_SESSION["msg"] = "error. Invalid card power"; $_SESSION["msg_type"] = 0; return false; // if the card recieve is positive // card recieves should almost always be close to -80 or -90 // but a value of 0 allows the user to leave blank field, // and it doesn't hurt to be lenient } elseif ((int)$card_receive > 0) { $_SESSION["msg"] = "error. Invalid receive sensitivity"; $_SESSION["msg_type"] = 0; return false; // if the antenna's gain is outside valid values // an antenna of 40dBi gain would be illegal, but again, leniency // doesn't hurt. Means less errors for the user } elseif ((((int)$antenna_dbi > 40) || ((int)$antenna_dbi < 0)) && ($antenaa_dbi != -1)) { $_SESSION["msg"] = "error. Invalid antenna gain"; $_SESSION["msg_type"] = 0; return false; // If the cable loss value is outside a valid range // if your losing 30dB of power in your cable, you have a problem! } elseif ((((float)$cable_loss > 30) || ((float)$cable_loss < 0)) && ($cable_loss != -1)) { $err_message = "error. invalid cable loss"; return false; // if something has been entered into the mac field, mac sure // it is a proper mac address\ } elseif ((!ereg("^([0-9|a-f]{2}:){5}[0-9|a-f]{2}$", $mac)) && ($mac != "unknown")) { $_SESSION["msg"] = "error. invalid mac address. Must be in the form 'xx:xx:xx:xx:xx:xx'"; $_SESSION["msg_type"] = 0; return false; // otherwise, all the data is fine! } else { // pull un-updated data from the DB so we can check // to see if some things have changed, and if so, // run any code that needs running $sql = "SELECT * from interfaces WHERE id=" . $int_id; $loc_db->query( $sql ); $loc_db->next_record(); // if the class of the interface has changed from p2p to // something else if (($loc_db->f("class") != $class) && ($loc_db->f("class") == "p2p")) { // select this interfaces ip address $sql = "select address from addresses where node_interface='" . $int_id . "'"; $loc_db->query( $sql ); $loc_db->next_record(); // delete any ips assigned to this interface delete_assignment($loc_db->f("address")); // delete any requests for ips to this interface delete_request($int_id, "router"); } // build the SQL statement to update the DB $sql = "UPDATE interfaces SET card_power=" . (int)$card_power . ", card_receive=" . (int)$card_receive . ", card_manufacturer='$card_manufacturer', antenna_dbi=" . (int)$antenna_dbi . ", antenna_type='$antenna_type', cable_loss=" . (int)$cable_loss . ", mode='$mode', class='$class', mac='$mac', channel=" . (int)$channel . " WHERE id=" . $int_id; $loc_db->query( $sql ); // let the user know the update was successful $_SESSION["msg"] = "interface successfully updated"; $_SESSION["msg_type"] = 1; // function successful return true; } // end validation if } // end update_interface /* * delete_interface() * * takes the ID for an interface, deletes all links that come into * it, then deletes it * * Usage: $int_id - ID for the interface you want deleted * * Returns: true or false, and sets a Global variable $success_message * to inform the user of the results * */ function delete_interface($int_id) { // create the DB class $loc_db = new locDatabase; // delete all links that begin or end with this interface $sql = "DELETE FROM links WHERE ((interface_1 =" . $int_id . ") OR (interface_2 =" . $int_id . "))"; $loc_db->query( $sql ); // delete the interface $sql = "DELETE FROM interfaces WHERE id = " . $int_id; $loc_db->query( $sql ); // let the user know the deletion was successful $_SESSION["msg"] = "interface successfully deleted"; $_SESSION["msg_type"] = 1; // function successful return true; } // end delete_interface() /* * create_service() * * takes a bunch of variables, and adds them to the DB as a service * available on a node. No point validating, as the user can enter * whatever they like... if its a dodgy entry, then noone will * be able to find what they offerto use it! * * Usage: $node_id - ID of the node the service is running on * $type - what type of service is it? * $ip - the ip address of the server * $port - what port is the service running on? * $descri..- a short, searchable description * * Returns: true or false, and sets a Global variable $success_message * to inform the user of the results * */ function create_service($node_id, $type, $ip, $port, $description) { // create DB class $loc_db = new locDatabase; // build SQL statement to insert the service into the DB $sql = "INSERT INTO services(node, type, ip, port, description) VALUES('$node_id', '$type', '$ip', '$port', '$description')"; $loc_db->query( $sql ); // inform the user that function was successful $_SESSION["msg"] = "Service successfully created"; $_SESSION["msg_type"] = 1; // function successful return true; } // end create_service() /* * delete_service() * * takes a service id and deletes that service * * Usage: $id - the ID of the service to delete * * Returns: true or false, and sets a Global variable $success_message * to inform the user of the results * */ function delete_service($service_id) { // create DB class $loc_db = new locDatabase; // build the SQL statement to delete the service $sql = "DELETE FROM services WHERE id=" . $service_id; $loc_db->query( $sql ); $_SESSION["msg"] = "Service successfully deleted"; $_SESSION["msg_type"] = 1; // function successful return true; } // end delete_service() } ?>