public function link() { if (empty($_GET['lid'])) { $c = '<h1>Create new CoolUri</h1>'; $new = true; } else { $c = '<h1>Update this CoolUri</h1>'; $new = false; $id = (int) $_GET['lid']; } if (!$new) { $q = $this->db->query('SELECT * FROM ' . $this->table . 'cache WHERE id=' . $id); $data = $this->db->fetch($q); $data['params'] = str_replace('&', '&', $this->serializedArrayToQueryString($data['params'])); } if (!empty($_POST)) { $data = $_POST; $data = array_map('trim', $data); if (empty($data['url']) || empty($data['params'])) { $c .= '<div class="error"><p>You must fill all inputs.</p></div>'; } else { $params = \Bednarik\Cooluri\Core\Functions::convertQuerystringToArray($data['params']); $cp = \Bednarik\Cooluri\Core\Functions::prepareParamsForCache($params); $ok = true; $olq = $this->db->query('SELECT COUNT(*) FROM ' . $this->table . 'cache WHERE params=' . $cp . ($new ? '' : ' AND id<>' . $id)); $num = $this->db->fetch_row($olq); if ($num[0] > 0) { $c .= '<div class="error"><p>A different link with such parameters exists already.</p></div>'; $ok = false; } $temp = preg_replace('~/$~', '', $data['url']); if ($temp == $data['url']) { $temp .= '/'; } $olq = $this->db->query('SELECT COUNT(*) FROM ' . $this->table . 'cache WHERE (url=' . $this->db->escape($temp) . ' OR url=' . $this->db->escape($data['url']) . ')' . ($new ? '' : ' AND id<>' . $id)); $num = $this->db->fetch_row($olq); if ($num[0] > 0) { $c .= '<div class="error"><p>A different link with such URI exists already.</p></div>'; $ok = false; } if ($new && $ok) { $q = $this->db->query('INSERT INTO ' . $this->table . 'cache(url,params,sticky,crdatetime) VALUES(' . $this->db->escape($data['url']) . ', ' . $cp . ', ' . (!empty($data['sticky']) && $data['sticky'] == 1 ? 1 : 0) . ', NOW())'); $this->db->query('DELETE FROM ' . $this->table . 'oldlinks WHERE url=' . $this->db->escape($data['url'])); if ($q) { $c .= '<div class="succes"><p>The new link was saved successfully.</p></div>'; $c .= '<p class="center"><a href="' . $this->file . 'mod=cache&l=' . htmlspecialchars($data['url']) . '">Show >></a></p>'; $data = array(); } else { $c .= '<div class="error"><p>Could not save the link.</p></div>'; } } elseif (!empty($id) && $ok) { $oldq = $this->db->query('SELECT * FROM ' . $this->table . 'cache WHERE id=' . $id); $old = $this->db->fetch($oldq); if ($data['url'] != $old['url']) { $q = $this->db->query('INSERT INTO ' . $this->table . 'oldlinks(link_id,url) VALUES(' . $id . ', \'' . $old['url'] . '\')'); } $qq = $this->db->query('UPDATE ' . $this->table . 'cache SET url=' . $this->db->escape($data['url']) . ', params=' . $cp . ', sticky=' . (!empty($data['sticky']) && $data['sticky'] == 1 ? 1 : 0) . ' WHERE id=' . $id . ' LIMIT 1 '); $this->db->query('DELETE FROM ' . $this->table . 'oldlinks WHERE url=' . $this->db->escape($data['url'])); if ($qq) { $c .= '<div class="succes"><p>The link was updated successfully.</p></div>'; $c .= '<p class="center"><a href="' . $this->file . 'mod=cache&l=' . htmlspecialchars($data['url']) . '">Show >></a></p>'; } else { $c .= '<div class="error"><p>Could not update the link.</p></div>'; } } } } $c .= '<form method="post" action="' . $this->file . 'mod=link' . ($new ? '' : '&lid=' . $id) . '"> <fieldset> <legend>URI details</legend> <label for="url">URI:</label><br /> <input type="text" name="url" id="url" value="' . (empty($data['url']) ? '' : htmlspecialchars($data['url'])) . '" /><br /> <label for="params">Parameters (query string: id=1&type=2):</label><br /> <input type="text" name="params" id="params" value="' . (empty($data['params']) ? '' : htmlspecialchars($data['params'])) . '" /><br /> <label for="sticky">Sticky (won\'t be updated):</label><br /> <input type="checkbox" class="check" name="sticky" id="sticky" value="1" ' . (empty($data['sticky']) ? '' : ' checked="checked"') . ' /> </fieldset> <input type="submit" value=" ' . ($new ? 'Save new URI' : 'Update this URI') . ' " class="submit" /> </form> '; return $c; }