public function test_add_and_delete_item() { $location = Model::factory('kwalbum_location'); $location->name = 'Item Is Here'; $location->save(); $this->assert_not_empty($location->id); $count = $location->count; $unknown_location = Model::factory('kwalbum_location')->load(1); $unknown_count = $unknown_location->count; $item = Model::factory('kwalbum_item'); $item->type = 'jpeg'; $item->user_id = 1; $item->description = 'd escription'; $item->path = 'p ath'; $item->filename = 'f ilename'; $this->assert_empty($item->id); $item->save(); $create_date = $item->create_date; $location->reload(); $unknown_location->reload(); $this->assert_equal($location->count, $count); $this->assert_equal($unknown_location->count, $unknown_count + 1); $item->reload(); $this->assert_not_empty($item->id); $this->assert_equal($item->location, $unknown_location->name); $this->assert_equal($item->count, 0); $this->assert_equal($item->user_id, 1); $this->assert_equal($item->type, 'jpeg'); $this->assert_equal($item->user_id, 1); $this->assert_equal($item->description, 'd escription'); $this->assert_equal($item->path, Kwalbum_Model::get_config('item_path') . 'p ath'); $this->assert_equal($item->filename, 'f ilename'); $this->assert_similar($item->latitude, 0.0); $this->assert_similar($item->longitude, 0.0); $this->assert_equal($item->create_date, $create_date); $this->assert_equal($item->update_date, $create_date); $this->assert_equal($item->visible_date, $create_date); $this->assert_equal($item->sort_date, $create_date); $this->assert_similar($item->latitude, 0); $this->assert_similar($item->longitude, 0); $this->assert_false($item->has_comments); $this->assert_equal($item->hide_level, 0); sleep(1); // make sure create_date and update_date can be different $item->location = $location->name; $item->count++; $item->visible_date = '2008-09-09 09:09:09'; $item->sort_date = '2008-09-09 00:00:00'; $item->latitude = 1.234567; $item->longitude = 1.234567; $item->hide_level = 1; $item->save(); $item->reload(); $this->assert_equal($item->location, $location->name); $this->assert_equal($item->count, 1); $this->assert_equal($item->create_date, $create_date); $this->assert_not_equal($item->update_date, $create_date); $this->assert_equal($item->visible_date, '2008-09-09 09:09:09'); $this->assert_equal($item->sort_date, '2008-09-09 00:00:00'); $this->assert_similar($item->latitude, 1.234567); $this->assert_similar($item->longitude, 1.234567); $this->assert_equal($item->hide_level, 1); $location->reload(); $unknown_location->reload(); $this->assert_equal($location->count, $count + 1); $this->assert_equal($unknown_location->count, $unknown_count); $item->delete(); $location->reload(); $unknown_location->reload(); $this->assert_empty($item->id); $this->assert_similar($location->count, $count); $this->assert_equal($unknown_location->count, $unknown_count); }
/** * Create a path if one does not already exist and create * any directories in the path that do not already exist. * * Echo errors labled with css class "error" * * @return string path if successful * @throws Kohana_Exception * @version 3.0 * @since 2.0 */ private function makePath() { $path = Kwalbum_Model::get_config('item_path'); $dirs = explode('-', date('y-m')); $path = $path . $dirs[0]; if (!file_exists($path) and !mkdir($path)) { throw new Kohana_Exception('Directory :dir could not be created', array(':dir' => Debug::path($path))); } $path .= '/' . $dirs[1]; if (!file_exists($path) and !mkdir($path)) { throw new Kohana_Exception('Directory :dir could not be created', array(':dir' => Debug::path($path))); } $path .= '/'; if (!file_exists($path . 't') and !@mkdir($path . 't')) { throw new Kohana_Exception('Directory :dir could not be created', array(':dir' => Debug::path($path . 't'))); } if (!file_exists($path . 'r') and !@mkdir($path . 'r')) { throw new Kohana_Exception('Directory :dir could not be created', array(':dir' => Debug::path($path . 'r'))); } return $path; }
/** * Delete an item from the database along with any relationships to other * tables in the database. Move the original file to a trash directory * and remove the thumbnail and resized images if they exist. */ public function delete() { // make sure trash directory is writable $delete_path = Kwalbum_Model::get_config('item_path'); $delete_path .= 'deleted'; if (!file_exists($delete_path) and !mkdir($delete_path)) { throw new Kohana_Exception('Directory :dir could not be created', array(':dir' => Debug::path($delete_path))); } if (!is_dir($delete_path) or !is_writable(realpath($delete_path))) { throw new Kohana_Exception('Directory :dir must be writable', array(':dir' => Debug::path($delete_path))); } // Remove item from location count DB::query(Database::UPDATE, 'UPDATE kwalbum_locations SET count = count-1 WHERE id = :location_id AND count > 0')->param(':location_id', $this->_location_id)->execute(); // Remove item-person relations and reduce persons' item counts $this->_delete_person_relations(); // Remove item-tag relations and reduce tags' item counts $this->_delete_tag_relations(); // Delete comments $comments = $this->_load_comments(); foreach ($comments as $comment) { $comment->delete(); } // Delete the item DB::query(Database::DELETE, "DELETE FROM kwalbum_items\n WHERE id = :id")->param(':id', $this->id)->execute(); // Delete the thumbnail and resized if they exist if (file_exists($this->path . 'r/' . $this->filename)) { unlink($this->path . 'r/' . $this->filename); } if (file_exists($this->path . 't/' . $this->filename)) { unlink($this->path . 't/' . $this->filename); } // Move the main file to the trash directory and possibly overwrite // an existing "deleted" file of the same name $old_name = $this->path . $this->filename; $new_name = $delete_path . '/' . date('YmdHis') . '_' . $this->filename; if (!rename($old_name, $new_name)) { throw new Kohana_Exception('Could not move :old to :new', array(':old' => Debug::path($old_name), ':new' => Debug::path($new_name))); } $this->clear(); }
private function _send_file($filepathname, $filename_addition = '', $download = false) { $request = $this->request; if (!($filepath = realpath($filepathname))) { // Return a 404 status $request->response()->status(404); Kohana::$log->add('~item/_send_file', '404: ' . $filepathname); return; } // Use the file name as the download file name $filename = pathinfo($filepath, PATHINFO_FILENAME); // Get the file size $size = filesize($filepath); // Get the file extension $extension = strtolower(pathinfo($filepath, PATHINFO_EXTENSION)); // Guess the mime using the file extension $mimes = Kohana::$config->load('mimes'); $mime = $mimes[$extension][0]; if (!$this->user->can_see_all) { $watermark = Kwalbum_Model::get_config('watermark_filename'); if ($watermark and $size < Kwalbum_Model::get_config('watermark_filesize_limit')) { $watermark = Kwalbum_Model::get_config('item_path') . $watermark; $watermark = @imagecreatefrompng($watermark); } else { $watermark = null; } if ($watermark) { switch ($mime) { case $mimes['jpg'][0]: $picture = imagecreatefromjpeg($filepath); if ($picture) { $width_p = imagesx($picture); $height_p = imagesy($picture); $width_w = imagesx($watermark); $height_w = imagesy($watermark); $width_percent = Kwalbum_Model::get_config('watermark_width_percent'); $height_percent = Kwalbum_Model::get_config('watermark_height_percent'); if ($width_p < $height_p) { $height_r = $height_p * $height_percent; $width_r = $height_r * $width_w / $height_w; if ($width_r > $width_p * $width_percent) { $width_r = $width_p * $width_percent; $height_r = $width_r * $height_w / $width_w; } } else { $width_r = $width_p * $width_percent; $height_r = $width_r * $height_w / $width_w; if ($height_r > $height_p * $height_percent) { $height_r = $height_p * $height_percent; $width_r = $height_r * $width_w / $height_w; } } imagecopyresampled($picture, $watermark, 0, $height_p - $height_r, 0, 0, $width_r, $height_r, $width_w, $height_w); //imagecopy($picture, $watermark, 0, $height_p-$height_w, 0, 0, $width_w, $height_w); header("Content-Type: image/jpeg"); imagejpeg($picture, null, 95); exit; } break; } } } // Open the file for reading $file = fopen($filepath, 'rb'); // Set the headers for a download $response = $request->response(); $response->headers('Content-Disposition', ($download ? 'attachment; ' : null) . 'filename="' . $filename . $filename_addition . '.' . $extension . '"'); $response->headers('Content-Type', $mime); $response->headers('Content-Length', $size); // Send all headers now $response->send_headers(); while (ob_get_level()) { // Flush all output buffers ob_end_flush(); } // Manually stop execution ignore_user_abort(TRUE); // Keep the script running forever set_time_limit(0); // Send data in 16kb blocks $block = 1024 * 16; while (!feof($file)) { if (connection_aborted()) { break; } // Output a block of the file echo fread($file, $block); // Send the data now flush(); } // Close the file fclose($file); // Stop execution exit; }