*
 * 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.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * This file creates a static array from the Essentials/items.csv file.
 * The items.csv file contains a dictionary of possible item names and their 
 * actual item ID and type ID. This is used for the /search command and the 
 * sanitization function in core_include.php. It needs to be run whenever the 
 * Essential plugin gets updated.
 */
require_once '/home/minecraft/server/bin/core_include.php';
// this here creates a new items array file
$search_arr = umc_item_data_get_namelist();
if (($handle = fopen("/home/minecraft/server/bukkit/plugins/Essentials/items.csv", "r")) !== FALSE) {
    while (($items = fgetcsv($handle, 10000, ",")) !== FALSE) {
        $firstletter = substr($items[0], 0, 1);
        if (count($items) == 3 && $firstletter !== '#' && !isset($search_arr[$items[0]])) {
            $item = umc_goods_get_text($items[1], $items[2]);
            $search_arr[$items[0]] = array('item_name' => $item['item_name'], 'type' => $item['type']);
        }
    }
    umc_array2file($search_arr, 'ITEM_SEARCH', '/home/minecraft/server/bin/includes/item_search.inc.php');
} else {
    die("Could not read items file!");
}
function umc_sanitize_input(&$value, $type)
{
    XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    $MAX_UNCS = 10000;
    $MIN_UNCS = 1.0E-5;
    if ($type == "price") {
        # Check that this is a number
        # Check that it is greater than zero
        # Check bounds
        if (!is_numeric($value)) {
            umc_error("{red}Invalid amount of uncs ({yellow}{$value}{red}), must be a number.");
        } elseif ($value < $MIN_UNCS) {
            umc_error("{red}Invalid amount of uncs ({yellow}{$value}{red}), must be at least {yellow}{$MIN_UNCS}{red}.");
        } elseif ($value > $MAX_UNCS) {
            umc_error("{red}Invalid amount of uncs ({yellow}{$value}{red}), cannot be more than {yellow}{$MAX_UNCS}{red}.");
        } else {
            return $value;
        }
    }
    if ($type == "amount") {
        if ($value == NULL) {
            // buying all available
            return NULL;
        }
        if (!is_numeric($value)) {
            umc_error("{red}Invalid amount ({yellow}{$value}{red}), must be an integer.");
        } elseif (intval($value) < 1) {
            umc_error("{red}Invalid amount ({yellow}{$value}{red}), must be at least 1.");
        } else {
            return intval(abs($value));
        }
    }
    if ($type == "player") {
        $player = umc_check_user($value);
        if (!$player) {
            umc_error("{red}Invalid player name ({yellow}{$value}{red}), no such player.");
        } else {
            return $player;
        }
    }
    if ($type == "item") {
        // get a list of all possible item names. REquires exact match of the searched item
        $all_names = umc_item_data_get_namelist();
        if (isset($all_names[$value])) {
            return $all_names[$value];
        } else {
            // we searched only for the EXACT item above. We should be looking for possible matches in the
            // search database too.
            global $ITEM_SEARCH;
            if (isset($ITEM_SEARCH[$value])) {
                return $ITEM_SEARCH[$value];
            }
            return false;
        }
    }
    if ($type == "table") {
        if (isset($value[2]) && ($value[2] == 'request' || $value[2] == 'req' || $value[2] == 'r')) {
            return 'request';
        } elseif (isset($value[2]) && ($value[2] == 'offer' || $value[2] == 'off' || $value[2] == 'o')) {
            return 'stock';
        } else {
            array_splice($value, 2, 0, 'offer');
            umc_echo("{yellow}[!]{gray} Didn't specify {yellow}request{gray} or {yellow}offer{gray}, assuming {yellow}offer", true);
            return 'stock';
        }
    }
    if ($type == "lot") {
        $check = !preg_match('/[^A-Za-z0-9_.#\\-$]/', $value);
        if (!$check) {
            umc_error('You need to enter a valid lot name such as "emp_a1"');
        } else {
            return $value;
        }
    }
    if ($type == "meta") {
        $meta_name = umc_parse_meta_input($value);
        if (is_null($meta_name)) {
            umc_error("Unknown Metavalue name: {white}{$value}");
        } else {
            return $meta_nam;
        }
    }
}