function DeleteLink($link_id, $update_count = TRUE, $link = null) { global $DB; if ($update_count) { $ids = GetCategoryIds($link_id); } if ($link == null) { $link = $DB->Row('SELECT * FROM lx_links WHERE link_id=?', array($link_id)); } // Clear cache ClearLinkDetailsCache($link_id); $DB->Update('DELETE FROM lx_links WHERE link_id=?', array($link_id)); $DB->Update('DELETE FROM lx_link_cats WHERE link_id=?', array($link_id)); $DB->Update('DELETE FROM lx_link_fields WHERE link_id=?', array($link_id)); $DB->Update('DELETE FROM lx_link_comments WHERE link_id=?', array($link_id)); // If link was associated with an account, update account info if ($link['username']) { $DB->Update('UPDATE lx_users SET num_links=num_links-1 WHERE username=?', array($link['username'])); } // Update category link count if ($update_count) { foreach ($ids as $id) { if ($link['status'] == 'active') { $DB->Update('UPDATE lx_categories SET links=links-1 WHERE category_id=?', array($id)); } } } // TODO: Remove screenshot }
function lxEditLink() { global $DB, $C; VerifyPrivileges(P_LINK_MODIFY); // See if URL already exists $url_exists = $DB->Count('SELECT COUNT(*) FROM lx_links WHERE site_url=? AND link_id!=?', array($_REQUEST['site_url'], $_REQUEST['link_id'])); $validator = new Validator(); $validator->Register($_REQUEST['email'], V_EMAIL, 'The email address is not properly formatted'); $validator->Register($_REQUEST['site_url'], V_URL, 'The site URL is not properly formatted'); $validator->Register($_REQUEST['category_id'], V_EMPTY, 'Please select at least one category for this link'); $validator->Register($url_exists, V_ZERO, 'This URL is already in the database'); $validator->Register($_REQUEST['date_added'], V_DATETIME, 'The Date Added field is not properly formatted'); // Handle improperly formatted expire dates if (!empty($_REQUEST['expires'])) { $validator->Register($_REQUEST['expires'], V_DATETIME, 'The expiration date is not properly formatted'); } if (!empty($_REQUEST['date_modified'])) { $validator->Register($_REQUEST['date_modified'], V_DATETIME, 'The Date Modified field is not properly formatted'); } // Make sure account exists if ($_REQUEST['username']) { $account = $DB->Row('SELECT * FROM lx_users WHERE username=?', array($_REQUEST['username'])); $validator->Register($account, V_NOT_FALSE, "No user account exists with the username '{$_REQUEST['username']}'"); } if (!$validator->Validate()) { $GLOBALS['errstr'] = join('<br />', $validator->GetErrors()); lxShEditLink(); return; } $link = $DB->Row('SELECT * FROM lx_links WHERE link_id=?', array($_REQUEST['link_id'])); // Calculate average rating $rating_avg = null; if ($_REQUEST['ratings'] > 0) { $rating_avg = $_REQUEST['rating_total'] / $_REQUEST['ratings']; } // Encrypt the password if (!empty($_REQUEST['password'])) { $_REQUEST['password'] = sha1($_REQUEST['password']); } else { $_REQUEST['password'] = $link['password']; } if (empty($_REQUEST['expires'])) { $_REQUEST['expires'] = DEF_EXPIRES; } // Scan the link to see if it has a recip $scan_result = ScanLink($_REQUEST); $has_recip = $scan_result['has_recip']; NullIfEmpty($_REQUEST['date_modified']); // Update regular fields $DB->Update('UPDATE lx_links SET ' . 'site_url=?, ' . 'recip_url=?, ' . 'title=?, ' . 'description=?, ' . 'status=?, ' . 'type=?, ' . 'expires=?, ' . 'name=?, ' . 'email=?, ' . 'submit_ip=?, ' . 'keywords=?, ' . 'clicks=?, ' . 'screenshot=?, ' . 'ratings=?, ' . 'rating_total=?, ' . 'rating_avg=?, ' . 'weight=?, ' . 'date_added=?, ' . 'date_modified=?, ' . 'recip_required=?, ' . 'allow_redirect=?, ' . 'icons=?, ' . 'admin_comments=?, ' . 'username=?, ' . 'password=?, ' . 'has_recip=? ' . 'WHERE link_id=?', array($_REQUEST['site_url'], $_REQUEST['recip_url'], $_REQUEST['title'], $_REQUEST['description'], $_REQUEST['status'], $_REQUEST['type'], $_REQUEST['expires'], $_REQUEST['name'], $_REQUEST['email'], $_REQUEST['submit_ip'], $_REQUEST['keywords'], $_REQUEST['clicks'], '', $_REQUEST['ratings'], $_REQUEST['rating_total'], $rating_avg, $_REQUEST['weight'], $_REQUEST['date_added'], $_REQUEST['date_modified'], intval($_REQUEST['recip_required']), intval($_REQUEST['allow_redirect']), $_REQUEST['icons'], $_REQUEST['admin_comments'], $_REQUEST['username'], $_REQUEST['password'], $has_recip, $_REQUEST['link_id'])); // Get current categories this link is located in so the link count can be updated $old_categories = array(); $result = $DB->Query('SELECT * FROM lx_link_cats WHERE link_id=?', array($_REQUEST['link_id'])); while ($old_category = $DB->NextRow($result)) { $old_categories[] = $old_category['category_id']; } $DB->Free($result); // Update category data $DB->Update('DELETE FROM lx_link_cats WHERE link_id=?', array($_REQUEST['link_id'])); foreach (explode(',', $_REQUEST['category_id']) as $category_id) { $sorter = $DB->Count('SELECT MAX(sorter) FROM lx_link_cats WHERE category_id=?', array($category_id)); $DB->Update('INSERT INTO lx_link_cats VALUES (?,?,?)', array($_REQUEST['link_id'], $category_id, $sorter)); UpdateLinkCount($category_id); } // Update the link count for the old categories this link was located in foreach ($old_categories as $old_category) { UpdateLinkCount($old_category); } // Update user defined fields UserDefinedUpdate('lx_link_fields', 'lx_link_field_defs', 'link_id', $_REQUEST['link_id'], $_REQUEST); // If username was supplied, update link count if ($_REQUEST['username'] != $link['username']) { if (!empty($link['username'])) { UpdateAccountLinkCount($link['username']); } if (!empty($_REQUEST['username'])) { UpdateAccountLinkCount($_REQUEST['username']); } } // Clear cache ClearLinkDetailsCache($_REQUEST['link_id']); $GLOBALS['message'] = 'Link has been successfully updated'; $GLOBALS['added'] = true; lxShEditLink(); }