id=$id; $this->load(); } } function view() { return WWW_ROOT . "images/screenshot.php?id=".$this->id; } function load() { if (is_numeric($this->id)) { $data = query_row("SELECT * FROM " . DB_PREFIX . "images WHERE id=".$this->id); if ($data) { $this->name = $data->name; $this->type = $data->type; $this->size = $data->size; $this->height = $data->height; $this->width = $data->width; $this->location = $data->location; $this->creator = $data->creator; $this->created = $data->created; $this->setLocked($data->locked); } else { $this->id = 0; } } } function delete() { if (is_admin() || $this->creator==get_logged_in_user()->getId()) { $deleted = query("DELETE FROM " . DB_PREFIX . "images WHERE id=".$this->id); if ($deleted) { $imgs = query_rows("SELECT id FROM " . DB_PREFIX . "images WHERE name='".$this->name."' AND size=".$this->size); if (!$imgs){ unlink(SCREENSHOT_STORE . $this->name); return true; } return true; } } return false; } function rename() { preg_match("/(.*)\.(jpeg|gif|jpg|pjpeg|png)/", $this->name, $name); $count = 1; while (file_exists(SCREENSHOT_STORE . $name[1] . "_" . $count . "." . $name[2])) { $count++; } $this->name = $name[1] . "_" . $count . "." . $name[2]; $this->location = SCREENSHOT_STORE . $this->name; } function create() { if (file_exists(SCREENSHOT_STORE . $this->name)) { $this->rename(); } file_put_contents(SCREENSHOT_STORE . $this->name , $this->screenshot); $this->process(); $this->creator = get_logged_in_user()->getId(); return query_insert("INSERT INTO " . DB_PREFIX . "images (name, type, size, location, creator, width, height) values ('".mysql_real_escape_string($this->name)."', '".mysql_real_escape_string($this->type)."', ".$this->size.", '".mysql_real_escape_string($this->location)."', ".$this->creator.", ".$this->width.", ".$this->height.")"); } function update() { if ($this->id > 0) { if (file_exists(SCREENSHOT_STORE . $this->name)) { unlink(SCREENSHOT_STORE . $this->name); } file_put_contents(SCREENSHOT_STORE . $this->name , $this->screenshot); $this->process(); if (query_update("UPDATE " . DB_PREFIX . "images SET name='".mysql_real_escape_string($this->name)."', type='".mysql_real_escape_string($this->type)."', size=".$this->size.", location='".mysql_real_escape_string($this->location)."', width=".$this->width.", height=".$this->height." WHERE id=".$this->id)) return $this->id; return false; } } function process() { $this->size = filesize(SCREENSHOT_STORE . $this->name); $image_info = getimagesize(SCREENSHOT_STORE . $this->name); $this->image_type = $image_info[2]; if ($this->image_type == IMAGETYPE_JPEG) { $this->image = imagecreatefromjpeg(SCREENSHOT_STORE . $this->name); } elseif ($this->image_type == IMAGETYPE_GIF) { $this->image = imagecreatefromgif(SCREENSHOT_STORE . $this->name); } elseif ($this->image_type == IMAGETYPE_PNG) { $this->image = imagecreatefrompng(SCREENSHOT_STORE . $this->name); } $this->width = imagesx($this->image); $this->height = imagesy($this->image); } function saveScreenshot($screenshot, $filename=false) { if ($screenshot) { if ($filename) { $this->name = sanitize_file_name($filename) . '.png'; } else { $this->name = md5(uniqid()) . '.png'; } $this->type = "image/png"; $this->location = SCREENSHOT_STORE . $this->name; $this->screenshot = base64_decode(substr($screenshot, strpos($screenshot, ',') + 1)); if ($this->id > 0) { return $this->update(); } return $this->create(); } return false; } public function getLocked() { return $this->locked; } public function setLocked($locked) { if ($locked) { $this->locked = 1; } else { $this->locked = 0; } } } ?>