<commit_before><?php
/**
 * @package img.pew.cc
 * @author <NAME> <<EMAIL>>
 * @version $Id: upload.php 100 2011-01-06 23:17:53Z daniel $
 * @license http://opensource.org/licenses/agpl-v3.html
 */

/**
 * img.pew.cc Image Hosting
 * Copyright (C) 2009-2010  <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once(__DIR__ . '/class.sqlite.php');
require_once(__DIR__ . '/functions.php');

class UploadException extends Exception {};

class upload {
	private $time;
	private $img = '';
	private $mime;
	private $name = '';
	private $dir;
	private $db = NULL;
	private $tags = '';
	private $preview_width = 150;
	private $preview_height = 150;

	public function __construct()
	{
		$this->time = time();
		$this->mime = array();
		$this->dir = __DIR__ . '../';
	}

	public function __destruct() {
		$this->db = NULL;
	}

	public function image($img = NULL)
	{
		$return = $this->img;
		if ($img !== NULL) $this->img = $img;
		return $return;
	}

	public function mimeTypes($mime = NULL)
	{
		$return = $this->mime;
		if ($mime !== NULL) $this->mime = $mime;
		return $return;
	}

	public function name($name = NULL)
	{
		$return = $this->name;
		if ($name !== NULL) $this->name = $name;
		return $return;
	}

	public function dir($dir = NULL)
	{
		$return = $this->dir;
		if ($dir !== NULL) $this->dir = $dir;
		return $return;
	}

	public function db($db = NULL)
	{
		$return = $this->db;
		if ($db !== NULL) $this->db = $db;
		return $return;
	}

	public function tags($tags = NULL)
	{
		$return = $this->tags;
		if ($tags !== NULL) $this->tags = $tags;
		return $return;
	}

	public function time($time = NULL)
	{
		$return = $this->time;
		if ($time !== NULL) $this->time = $time;
		return $return;
	}

	public function preview_width($preview_width = NULL)
	{
		$return = $this->preview_width;
		if ($preview_width !== NULL) $this->preview_width = $preview_width;
		return $return;
	}

	public function preview_height($preview_height = NULL)
	{
		$return = $this->preview_height;
		if ($preview_height !== NULL) $this->preview_height = $preview_height;
		return $return;
	}

	public function save()
	{
		/*
		 * [0]			- width
		 * [1]			- geight
		 * [2]			- IMAGETYPE_XXX
		 + [3]			- Text string with width and height
		 * ["bits"]
		 * ["channels"]
		 * ["mime"]		- Mime type
		 */
		$info = getimagesize($this->img);

		// Check if this type of image is allowed
		if (!isset($this->mime[$info['mime']])) {
			unlink_safe($this->img);
			return false;
			//errorMsg('Imagetype not allowed.');
		}

		$md5 = md5_file($this->img);

		// Assign the correct extension for this image
		$name = str_replace('\'', '', $this->name);
		$name = explode('.', $name);

		if(count($name) < 2) {
			$name = $name[0] . '.' . $this->mime[$info['mime']];
		} else {
			$name[count($name) - 1] = $this->mime[$info['mime']];
			$name = implode('.', $name);
		}

		// Choose the location for the file
		$name = trim(str_replace('//', '/', checkExists($this->dir . '/' . $name)));

		// Generate a URL save string to send to the browser
		$location = explode('/', $name);
		for ($i = 0; $i < count($location); $i++) {
			$location[$i] = rawurlencode($location[$i]);
		}
		$location = implode('/', $location);

		// Move the file to it's new location
		if (!isCLI()) {
			if (!move_uploaded_file_save($this->img, $name)) {
				unlink_safe($this->img);
				throw new UploadException('Can\'t move uploaded file.');
			}
		} else {<commit_msg>- Fixed: class.upload.php generating wrong image path<commit_after><?php
/**
 * @package img.pew.cc
 * @author <NAME> <<EMAIL>>
 * @version $Id: upload.php 100 2011-01-06 23:17:53Z daniel $
 * @license http://opensource.org/licenses/agpl-v3.html
 */

/**
 * img.pew.cc Image Hosting
 * Copyright (C) 2009-2010  <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once(__DIR__ . '/class.sqlite.php');
require_once(__DIR__ . '/functions.php');

class UploadException extends Exception {};

class upload {
	private $time;
	private $img = '';
	private $mime;
	private $name = '';
	private $dir;
	private $db = NULL;
	private $tags = '';
	private $preview_width = 150;
	private $preview_height = 150;

	public function __construct()
	{
		$this->time = time();
		$this->mime = array();
		$this->dir = __DIR__ . '../';
	}

	public function __destruct() {
		$this->db = NULL;
	}

	public function image($img = NULL)
	{
		$return = $this->img;
		if ($img !== NULL) $this->img = $img;
		return $return;
	}

	public function mimeTypes($mime = NULL)
	{
		$return = $this->mime;
		if ($mime !== NULL) $this->mime = $mime;
		return $return;
	}

	public function name($name = NULL)
	{
		$return = $this->name;
		if ($name !== NULL) $this->name = $name;
		return $return;
	}

	public function dir($dir = NULL)
	{
		$return = $this->dir;
		if ($dir !== NULL) $this->dir = $dir;
		return $return;
	}

	public function db($db = NULL)
	{
		$return = $this->db;
		if ($db !== NULL) $this->db = $db;
		return $return;
	}

	public function tags($tags = NULL)
	{
		$return = $this->tags;
		if ($tags !== NULL) $this->tags = $tags;
		return $return;
	}

	public function time($time = NULL)
	{
		$return = $this->time;
		if ($time !== NULL) $this->time = $time;
		return $return;
	}

	public function preview_width($preview_width = NULL)
	{
		$return = $this->preview_width;
		if ($preview_width !== NULL) $this->preview_width = $preview_width;
		return $return;
	}

	public function preview_height($preview_height = NULL)
	{
		$return = $this->preview_height;
		if ($preview_height !== NULL) $this->preview_height = $preview_height;
		return $return;
	}

	public function save()
	{
		/*
		 * [0]			- width
		 * [1]			- geight
		 * [2]			- IMAGETYPE_XXX
		 + [3]			- Text string with width and height
		 * ["bits"]
		 * ["channels"]
		 * ["mime"]		- Mime type
		 */
		$info = getimagesize($this->img);

		// Check if this type of image is allowed
		if (!isset($this->mime[$info['mime']])) {
			unlink_safe($this->img);
			return false;
			//errorMsg('Imagetype not allowed.');
		}

		$md5 = md5_file($this->img);

		// Assign the correct extension for this image
		$name = str_replace('\'', '', $this->name);
		$name = explode('.', $name);

		if(count($name) < 2) {
			$name = $name[0] . '.' . $this->mime[$info['mime']];
		} else {
			$name[count($name) - 1] = $this->mime[$info['mime']];
			$name = implode('.', $name);
		}

		// Generate a URL save string to send to the browser
		$location = explode('/', trim(str_replace('//', '', $this->dir . '/' . $name)));
		for ($i = 0; $i < count($location); $i++) {
			$location[$i] = rawurlencode($location[$i]);
		}
		$location = implode('/', $location);
		
		// Choose the location for the file
		$name = trim(str_replace('//', '/', checkExists(__DIR__ . '/' . $this->dir . '/' . $name)));

		// Move the file to it's new location
		if (!isCLI()) {
			if (!move_uploaded_file_save($this->img, $name)) {
				unlink_safe($this->img);
				throw new UploadException('Can\'t move uploaded file.');
			}
		} else {
{'fraction_non_alphanumeric': 0.17612121212121212, 'fraction_numerical': 0.010424242424242424, 'mean_word_length': 3.3266911379129525, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2807687', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2788, 'n_words': 823}