function mysticalshop_uninstall()
{
    require_once './modules/mysticalshop/run/editor_what/delete.php';
    $items = db_query('SELECT id FROM ' . db_prefix('magicitems'));
    while ($item = db_fetch_assoc($items)) {
        mysticalshop_delete_item($item['id']);
    }
    $sql = 'DROP TABLE IF EXISTS ' . db_prefix('magicitems');
    db_query($sql);
    if (getsetting('usedatacache', false)) {
        require_once './modules/mysticalshop/libcoredup.php';
        mysticalshop_massinvalidate('modules-mysticalshop-');
    }
}
    db_query($sql);
    output('`6The item "`^%s`6" has been successfully edited.`n`n', $displayname);
    if (getsetting('usedatacache', false) && $cat != $postcat) {
        $cat = (int) $cat;
        require_once './modules/mysticalshop/libcoredup.php';
        invalidatedatacache('modules-mysticalshop-view-' . $cat);
        mysticalshop_massinvalidate('modules-mysticalshop-viewgoods-' . $cat);
    }
} else {
    $sql = 'LOCK TABLES ' . db_prefix('magicitems') . ' WRITE;';
    db_query($sql);
    $sql = "INSERT INTO " . db_prefix("magicitems") . "\r\n\t(category,name,description,gold,gems,dk,attack,defense,charm,hitpoints,turns,favor,bigdesc,rare,rarenum)\r\n\tVALUES ({$postcat},'{$name}','{$describe}',{$gold},{$gems},{$dk},{$attack},{$defense},{$charm},{$hitpoints},{$turns},{$favor},'{$bigdesc}',{$rare},{$rarenum})";
    db_query($sql);
    $itemid = db_insert_id();
    $sql = 'UNLOCK TABLES;';
    db_query($sql);
    output('`6The item "`^%s`6" has been saved to the database.`n`n', $displayname);
}
output('Would you like to <a href="' . htmlentities($fromeditor . 'preview&id=' . $itemid . '&cat=') . $postcat . '">[ Review ]</a> or <a href="' . htmlentities($fromeditor . 'edit&id=' . $itemid . '&cat=') . $postcat . '">[ Edit ]</a> this item?`0', true);
addnav('', $fromeditor . 'edit&id=' . $itemid . '&cat=' . $postcat);
addnav('', $fromeditor . 'preview&id=' . $itemid . '&cat=' . $postcat);
if (getsetting('usedatacache', false)) {
    invalidatedatacache('modules-mysticalshop-editorcats');
    invalidatedatacache('modules-mysticalshop-view-' . $postcat);
    invalidatedatacache('modules-mysticalshop-enter');
    require_once './modules/mysticalshop/libcoredup.php';
    mysticalshop_massinvalidate('modules-mysticalshop-viewgoods-' . $postcat);
}
httpset('id', $itemid);
httpset('cat', $postcat);
$cat = $postcat;
function mysticalshop_delete_item($id)
{
    $magic_table = db_prefix('magicitems');
    $item_stats_sql = 'SELECT name,category,gold,gems,attack,defense,charm,hitpoints,turns,favor
			FROM ' . $magic_table . ' WHERE id=' . $id . ' LIMIT 1';
    $del_sql = 'DELETE FROM ' . $magic_table . ' WHERE id=' . $id;
    $item_categories = array('ring', 'amulet', 'weapon', 'armor', 'cloak', 'helm', 'glove', 'boot', 'misc');
    $item_stats_array = db_fetch_assoc(db_query($item_stats_sql));
    $item_name = $item_stats_array['name'];
    $item_catint = $item_stats_array['category'];
    $item_cat = strtolower($item_categories[$item_catint]);
    db_query($del_sql);
    output('`^Item %s`^ deleted.`n', $item_name);
    $prefs_table = db_prefix('module_userprefs');
    $accts_sql = 'SELECT userid FROM ' . $prefs_table . ' WHERE modulename=\'mysticalshop\' AND setting=\'' . $item_cat . 'id\' AND `value`=\'' . $id . '\' AND userid IN(SELECT userid FROM ' . $prefs_table . ' WHERE modulename=\'mysticalshop\' AND setting=\'' . $item_cat . ($item_cat == 'boot' ? 's' : '') . '\' AND value=\'1\')';
    $accts_result = db_query($accts_sql);
    $accts = '';
    while ($acct_array = db_fetch_assoc($accts_result)) {
        $accts .= $acct_array['userid'] . ',';
    }
    $accts = rtrim($accts, ',');
    if ($accts != '') {
        if ($item_cat == 'weapon') {
            $default_weapon = trim(get_module_setting('def_weapon'));
            if ($default_weapon === '') {
                $default_weapon = db_fetch_assoc(db_query('DESC ' . db_prefix('accounts') . ' weapon'));
                $default_weapon = $default_weapon['Default'];
            }
            $extra_sql = ',weaponvalue=0,weapondmg=0,weapon=\'' . $default_weapon . '\'';
        } elseif ($item_cat == 'armor') {
            $default_armor = trim(get_module_setting('def_armor'));
            if ($default_armor === '') {
                $default_armor = db_fetch_assoc(db_query('DESC ' . db_prefix('accounts') . ' armor'));
                $default_armor = $default_armor['Default'];
            }
            $extra_sql = ',armorvalue=0,armordef=0,armor=\'' . $default_armor . '\'';
        } else {
            $extra_sql = '';
        }
        $cleanup_sql = 'UPDATE ' . db_prefix('accounts') . ' SET gold=gold+(' . (int) $item_stats_array['gold'] . '),gems=gems+(' . (int) $item_stats_array['gems'] . '),attack=attack-(' . (int) $item_stats_array['attack'] . '),defense=defense-(' . (int) $item_stats_array['defense'] . '),charm=charm-(' . (int) $item_stats_array['charm'] . '),maxhitpoints=maxhitpoints-(' . (int) $item_stats_array['hitpoints'] . '),turns=turns-(' . (int) $item_stats_array['turns'] . '),deathpower=deathpower-(' . (int) $item_stats_array['favor'] . ')' . $extra_sql . ' WHERE acctid IN(' . $accts . ')';
        db_query($cleanup_sql);
        $affected = db_affected_rows();
        if ($affected > 0) {
            output('Player accounts refunded: %s. ', $affected);
        }
        if ($item_cat) {
            $cleanuser_sql = 'UPDATE ' . $prefs_table . ' SET `value`=\'0\'' . ' WHERE userid IN (' . $accts . ') AND modulename=\'mysticalshop\'
					AND (setting=\'' . $item_cat . ($item_cat == 'boot' ? 's' : '') . '\'
						OR setting=\'' . $item_cat . 'id\')';
            $clearname_sql = 'UPDATE ' . $prefs_table . ' SET `value`=\'None\'' . ' WHERE userid IN (' . $accts . ') AND modulename=\'mysticalshop\'
					AND setting=\'' . $item_cat . 'name\'';
            $clearturns_sql = 'UPDATE ' . $prefs_table . ' SET `value`=`value`-(' . (int) $item_stats_array['turns'] . ') WHERE userid IN (' . $accts . ') AND modulename=\'mysticalshop\'
					AND setting=\'turnadd\'';
            $clearfavor_sql = 'UPDATE ' . $prefs_table . ' SET `value`=`value`-(' . (int) $item_stats_array['favor'] . ') WHERE userid IN (' . $accts . ') AND modulename=\'mysticalshop\'
					AND setting=\'favoradd\'';
            db_query($cleanuser_sql);
            $affected_prefs = db_affected_rows();
            db_query($clearname_sql);
            $affected_prefs += db_affected_rows();
            db_query($clearturns_sql);
            $affected_prefs += db_affected_rows();
            db_query($clearfavor_sql);
            $affected_prefs += db_affected_rows();
            if ($affected_prefs > 0) {
                output('Player preferences cleared: %s.', $affected_prefs);
            }
        }
    }
    output_notl('`0`n`n');
    if (getsetting('usedatacache', false)) {
        invalidatedatacache('modules-mysticalshop-editorcats');
        invalidatedatacache('modules-mysticalshop-view-' . $item_catint);
        invalidatedatacache('modules-mysticalshop-enter');
        require_once './modules/mysticalshop/libcoredup.php';
        mysticalshop_massinvalidate('modules-mysticalshop-viewgoods-' . $item_catint);
    }
}
<?php

$magic_items = array('id' => array('name' => 'id', 'type' => 'int(11) unsigned', 'extra' => 'auto_increment'), 'category' => array('name' => 'category', 'type' => 'int(10) unsigned', 'default' => '0'), 'name' => array('name' => 'name', 'type' => 'varchar(50)', 'default' => 'None'), 'description' => array('name' => 'description', 'type' => 'text'), 'gold' => array('name' => 'gold', 'default' => '0', 'type' => 'int(11) unsigned'), 'gems' => array('name' => 'gems', 'default' => '0', 'type' => 'int(11) unsigned'), 'dk' => array('name' => 'dk', 'type' => 'int(11)', 'default' => '0'), 'attack' => array('name' => 'attack', 'type' => 'varchar(11)', 'default' => '0'), 'defense' => array('name' => 'defense', 'type' => 'varchar(11)', 'default' => '0'), 'charm' => array('name' => 'charm', 'type' => 'varchar(11)', 'default' => '0'), 'hitpoints' => array('name' => 'hitpoints', 'type' => 'varchar(11)', 'default' => '0'), 'turns' => array('name' => 'turns', 'type' => 'varchar(11)', 'default' => '0'), 'favor' => array('name' => 'favor', 'type' => 'varchar(11)', 'default' => '0'), 'bigdesc' => array('name' => 'bigdesc', 'type' => 'text'), 'align' => array('name' => 'align', 'type' => 'varchar(11)', 'default' => '0'), 'odor' => array('name' => 'odor', 'type' => 'varchar(11)', 'default' => '0'), 'hunger' => array('name' => 'hunger', 'type' => 'varchar(11)', 'default' => '0'), 'rare' => array('name' => 'rare', 'type' => 'tinyint(3)', 'default' => '0'), 'rarenum' => array('name' => 'rarenum', 'type' => 'int(11)', 'default' => '0'), 'key-PRIMARY' => array('name' => 'PRIMARY', 'type' => 'primary key', 'unique' => '1', 'columns' => 'id'), 'index-category' => array('name' => 'category', 'type' => 'index', 'columns' => 'category'));
require_once "lib/tabledescriptor.php";
synctable(db_prefix('magicitems'), $magic_items, true);
if (getsetting('usedatacache', false)) {
    invalidatedatacache('modules-mysticalshop-enter');
    invalidatedatacache('modules-mysticalshop-viewgoods');
    invalidatedatacache('module-mysticalshop-selectall-ordercat');
    require_once './modules/mysticalshop/libcoredup.php';
    mysticalshop_massinvalidate('modules-mysticalshop-');
}
module_addhook("superuser");
module_addhook("village");
module_addhook("dragonkill");
module_addhook("newday");
module_addhook("lodge");
module_addhook("pointsdesc");
module_addhook("charstats");
module_addhook("changesetting");
module_addhook("training-victory");
module_addhook("bioinfo");
module_addhook("validateprefs");
<?php

if ($args['setting'] == "villagename") {
    if ($args['old'] == get_module_setting("shoploc")) {
        set_module_setting("shoploc", $args['new']);
    }
} elseif ($args['setting'] == 'shownum') {
    if (getsetting('usedatacache', false)) {
        require_once './modules/mysticalshop/libcoredup.php';
        mysticalshop_massinvalidate('modules-mysticalshop-viewgoods-');
    }
}
function mysticalshop_additem($id, $cat, $seller_present = true)
{
    global $session;
    if ($seller_present) {
        $shopkeep = get_module_setting('shopkeepname');
    }
    $sql = 'SELECT * FROM ' . db_prefix('magicitems') . ' WHERE id=' . $id . ' LIMIT 1';
    $result = db_query($sql);
    $row = db_fetch_assoc($result);
    $name = $row['name'];
    $turns = $row['turns'];
    $charm = $row['charm'];
    if ($turns + $session['user']['turns'] < 0) {
        if ($seller_present) {
            output('`^%s`^ sees that you are too tired to carry %s`^ right now and informs you that you should come back later.`n`n', $shopkeep, $name);
        }
        $row['gold'] = 0;
        $row['gems'] = 0;
        $name = false;
    } elseif ($charm + $session['user']['charm'] < 0) {
        if ($seller_present) {
            output('`^%s`^ refuses to give you %s`^ on the grounds of your current looks.`n`n', $shopkeep, $name);
        }
        $row['gold'] = 0;
        $row['gems'] = 0;
        $name = false;
    } elseif ($row['favor'] + $session['user']['deathpower'] < 0) {
        if ($seller_present) {
            output('`^%s`^ refuses to give you %s`^ on the grounds of your current status with a certain deity.`n`n', $shopkeep, $name);
        }
        $row['gold'] = 0;
        $row['gems'] = 0;
        $name = false;
    } else {
        $attack = $row['attack'];
        $defense = $row['defense'];
        $health = $row['hitpoints'];
        $rare = $row['rare'];
        $subtract = $row['rarenum'] - 1;
        //if this is a limited item, let's subtract from the total available
        if ($rare == 1) {
            $sql = "UPDATE " . db_prefix("magicitems") . " SET rarenum={$subtract} WHERE id={$id}";
            db_query($sql);
            if (getsetting('usedatacache', false)) {
                invalidatedatacache('modules-mysticalshop-enter');
                require_once './modules/mysticalshop/libcoredup.php';
                mysticalshop_massinvalidate('modules-mysticalshop-viewgoods-' . $cat);
            }
        }
        if ($cat == 0) {
            set_module_pref("ringid", $id);
            set_module_pref("ring", 1);
            set_module_pref("ringname", $name);
        } else {
            if ($cat == 1) {
                set_module_pref("amuletid", $id);
                set_module_pref("amulet", 1);
                set_module_pref("amuletname", $name);
            } else {
                if ($cat == 2) {
                    $value = $row['gold'];
                    //alright, let's first subtract the previous weapon damage
                    //credit to seretogis for catching this bug
                    if ($session['user']['weapondmg'] != 0) {
                        output('`2Your `^%s`2 disintegrates as soon as you take hold of `^%s`2.`n`n', $session['user']['weapon'], $name);
                    }
                    $session['user']['attack'] -= $session['user']['weapondmg'];
                    //these are magical blades, they adjust as you level
                    //i.e. each level, their attack goes up by one
                    $session['user']['weapon'] = $name;
                    $session['user']['weaponvalue'] = $value;
                    $weapon_base_atk = get_module_setting('weapon_atk');
                    if ($weapon_base_atk == 0) {
                        // adaptive
                        $session['user']['weapondmg'] = $session['user']['level'];
                        $session['user']['attack'] += $session['user']['weapondmg'];
                    } elseif ($weapon_base_atk == 1) {
                        // synchronous
                        $session['user']['weapondmg'] = $attack;
                    } else {
                        // static
                        $session['user']['weapondmg'] = (int) get_module_setting('weapon_atk_power');
                    }
                    //
                    set_module_pref("weaponid", $id);
                    set_module_pref("weapon", 1);
                    set_module_pref("weaponname", $name);
                    if ($seller_present) {
                        output("`^\"I'm also sorry to say,\" %s`^ notes, \"that MightyE's Weapon Shop is closed to you until you sell your weapon back. Business rivarly, you know.\"`n`n", $shopkeep);
                    }
                } else {
                    if ($cat == 3) {
                        $value = $row['gold'];
                        set_module_pref("armorid", $id);
                        set_module_pref("armor", 1);
                        set_module_pref("armorname", $name);
                        //take away original armor value, the sister of the bug stated above
                        if ($session['user']['armordef'] != 0) {
                            output("`2You touch `^%s`2 and watch your `^%s`2 fall apart.`n`n", $name, $session['user']['armor']);
                        }
                        $session['user']['defense'] -= $session['user']['armordef'];
                        //magical armor, adjusts as you level
                        $session['user']['armor'] = $name;
                        $session['user']['armorvalue'] = $value;
                        $armor_base_def = get_module_setting('armor_def');
                        if ($armor_base_def == 0) {
                            // adaptive
                            $session['user']['armordef'] = $session['user']['level'];
                            $session['user']['defense'] += $session['user']['armordef'];
                        } elseif ($armor_base_def == 1) {
                            // synchronous
                            $session['user']['armordef'] = $defense;
                        } else {
                            // static
                            $session['user']['armordef'] = (int) get_module_setting('armor_def_power');
                        }
                        //To defeat the double armor bug once and for all, I've blocked the armor shop from showing up. Let's tell the players this.
                        if ($seller_present) {
                            output("`^\"I'm afraid to say Pegasus doesn't care too much for the competition,\" %s`^ notes. \"Her doors are closed to you until you sell your armor back to the shop.\"`n`n", $shopkeep);
                        }
                    } else {
                        if ($cat == 4) {
                            set_module_pref("cloakid", $id);
                            set_module_pref("cloak", 1);
                            set_module_pref("cloakname", $name);
                        } else {
                            if ($cat == 5) {
                                set_module_pref("helmid", $id);
                                set_module_pref("helm", 1);
                                set_module_pref("helmname", $name);
                            } else {
                                if ($cat == 6) {
                                    set_module_pref("gloveid", $id);
                                    set_module_pref("glove", 1);
                                    set_module_pref("glovename", $name);
                                } else {
                                    if ($cat == 7) {
                                        set_module_pref("bootid", $id);
                                        set_module_pref("boots", 1);
                                        set_module_pref("bootname", $name);
                                    } else {
                                        if ($cat == 8) {
                                            set_module_pref("miscid", $id);
                                            set_module_pref("misc", 1);
                                            set_module_pref("miscname", $name);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        //end
        $point = translate_inline('point');
        $points = translate_inline('points');
        //alter stats if needed
        if ($attack != 0) {
            $session['user']['attack'] += $attack;
            output("`&This item has altered your attack by `^%s `&%s.`n", $attack, abs($attack) != 1 ? $points : $point);
        }
        if ($defense != 0) {
            $session['user']['defense'] += $defense;
            output("`&This item has altered your defense by `^%s `&%s.`n", $defense, abs($defense) != 1 ? $points : $point);
        }
        if ($charm != 0) {
            $session['user']['charm'] = $session['user']['charm'] + $charm;
            output("`&This item has altered your charm by `^%s `&%s.`n", $charm, abs($charm) != 1 ? $points : $point);
        }
        if ($health != 0) {
            $session['user']['maxhitpoints'] += $health;
            //adjust to fit
            $session['user']['hitpoints'] = $session['user']['maxhitpoints'];
            output("`&This item has altered your maximum hit points by `^%s`&.`n", $health);
        }
        //this needs to be adjusted at newday as well.
        if ($turns != 0) {
            $session['user']['turns'] += $turns;
            $stamina = number_format($turns * 25000);
            output("`&This item has increased your Stamina by `^%s`& points.`n", $stamina);
            set_module_pref("turnadd", get_module_pref("turnadd") + $turns);
        }
        //items that grant favor are a little trickier, since they have to be restored upon each resurrection
        //So, extra additions are needed. See newday above to see how this is done.
        if ($row['favor'] != 0) {
            $favor = $row['favor'];
            //grant one-time automatic favor
            $session['user']['deathpower'] += $favor;
            //store favor granted to be restored upon resurrection
            set_module_pref("res", $session['user']['resurrections']);
            set_module_pref("favor", 1);
            set_module_pref("favoradd", get_module_pref("favoradd") + $favor);
            output('`&This item has altered your favor with %s`& by `^%s `&%s.`n', getsetting('deathoverlord', '`$Ramius'), $favor, abs($favor) != 1 ? $points : $point);
        }
    }
    return array('name' => $name, 'gold' => $row['gold'], 'gems' => $row['gems']);
}