function process_query() { $upload_dir = '../img/'; $upload_url_dir = '/img/'; $upload_file = false; try { $new_name = rand(0, PHP_INT_MAX) . '.jpg'; $upload_file = $upload_dir . basename($_FILES['image']['name']); move_uploaded_file($_FILES['image']['tmp_name'], $upload_file); if (!resize_img($upload_file, $upload_dir . $new_name)) { throw new Exception(); } else { $data = $upload_url_dir . $new_name; $label = 'url'; $status = RESPONSE_STATUS_OK; } } catch (Exception $e) { $data = 'file is not image of .jpg .gif .png formats'; $label = 'error'; $status = RESPONSE_STATUS_FAIL; } finally { if ($upload_file) { unlink($upload_file); } } api_echo_as_json($data, $label, $status); }
function process_request() { $page = isset($_GET['page']) ? intval($_GET['page']) : 1; $order = isset($_GET['order']) ? $_GET['order'] : 'id'; if (!in_array($order, ['id', 'price'])) { api_wrong_args(); return; } if ($page <= 0) { api_wrong_args(); return; } $rows = pagination_get_page($page - 1, $order); api_echo_as_json($rows, 'items'); }
function process_request() { $id = isset($_GET['item_id']) ? intval($_GET['item_id']) : false; if ($id !== false) { if ($id <= 0) { api_wrong_args(); return; } $item = pdb_get_item($id); if ($item) { api_echo_as_json($item, 'item'); } else { api_echo_as_json('item not found', 'error', RESPONSE_STATUS_FAIL); } } else { api_wrong_args(); } }
function process_request() { $item_id = isset($_GET['item_id']) ? intval($_GET['item_id']) : false; if ($item_id == false || $item_id <= 0) { api_wrong_args(); return; } $item = db_get_item($item_id); if (!$item) { api_echo_as_json("Item not found", 'msg'); return; } db_delete_item($item_id); $mc_handler = memcache_connect('localhost'); pagination_rebuild_ids($mc_handler, $item['id']); pagination_rebuild_prices($mc_handler, $item['price']); if (memcache_get($mc_handler, 'total_rows') !== false) { memcache_decrement($mc_handler, 'total_rows'); } api_echo_as_json('Item deleted', 'msg'); memcache_delete($mc_handler, "item_" . $item_id); }
function process_request() { $item_name = isset($_POST['item_name']) ? $_POST['item_name'] : null; $item_price = isset($_POST['item_price']) ? $_POST['item_price'] : null; $item_description = isset($_POST['item_description']) ? $_POST['item_description'] : null; $item_img = isset($_POST['item_img']) ? $_POST['item_img'] : null; $errors = []; if (is_null($item_name)) { $errors[] = 'Non-empty name required'; } else { $item_name = htmlspecialchars(trim($item_name)); if ($item_name === '') { $errors[] = 'Non-empty name required'; } } if (is_null($item_price) || !preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) { $errors[] = 'Incorrect price number'; } if (is_null($item_description)) { $errors[] = 'Incorrect description'; } else { $item_description = htmlspecialchars(trim($item_description)); } if (!empty($errors)) { api_echo_as_json($errors, 'errors', RESPONSE_STATUS_FAIL); return; } $item_price = str_replace(',', '.', $item_price); if (is_null($item_img)) { $item_img = "Null"; } db_insert_item($item_name, $item_description, $item_price, $item_img); $mc_handler = memcache_connect('localhost'); if (memcache_get($mc_handler, 'total_rows') !== false) { memcache_increment($mc_handler, 'total_rows'); } api_echo_as_json('Item created', 'msg'); }
function api_wrong_args() { api_echo_as_json('wrong arguments', 'error', RESPONSE_STATUS_FAIL); }
function process_request() { $item_id = isset($_POST['item_id']) ? intval($_POST['item_id']) : null; $item_name = isset($_POST['item_name']) ? $_POST['item_name'] : null; $item_price = isset($_POST['item_price']) ? $_POST['item_price'] : null; $item_description = isset($_POST['item_description']) ? $_POST['item_description'] : null; $item_img = isset($_POST['item_img']) ? $_POST['item_img'] : null; $errors = []; if (is_null($item_id) || $item_id <= 0) { $errors[] = 'Incorrect id'; } if (!is_null($item_name)) { $item_name = htmlspecialchars(trim($item_name)); if ($item_name === '') { $errors[] = 'Non-empty name required'; } } if (!is_null($item_price)) { if (!preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) { $errors[] = 'Incorrect price number'; } } if (!is_null($item_description)) { $item_description = htmlspecialchars(trim($item_description)); } if (!empty($errors)) { api_echo_as_json($errors, 'errors', RESPONSE_STATUS_FAIL); return; } $item = db_get_item($item_id); if (!$item) { api_echo_as_json("Item not found", 'msg'); return; } $values = []; if (!is_null($item_name)) { $values['name'] = $item_name; } if (!is_null($item_price)) { $item_price = str_replace(',', '.', $item_price); $values['price'] = $item_price; } if (!is_null($item_description)) { $values['description'] = $item_description; } if (!is_null($item_img)) { $values['imgurl'] = $item_img; } if (!empty($values)) { db_update_item($item_id, $values); $mc_handler = memcache_connect('localhost'); memcache_delete($mc_handler, get_page_cache_key($item_id)); pagination_rebuild_ids($mc_handler, $item_id, 1); $min_price = min($item_price, $item['price']); if ($item_price == $item['price']) { $edited_pages_amount = 1; } else { $edited_pages_amount = 0; } pagination_rebuild_prices($mc_handler, $min_price, $edited_pages_amount); } api_echo_as_json('Item successfully edited', 'msg'); }