<commit_before><?php
	require_once('10layer/system/TL_Api.php');
	
	/**
	 * Files class
	 * 
	 * @extends CI_Controller
	 */
	class Files extends TL_Api {
		
		/**
		 * __construct function.
		 * 
		 * @access public
		 * @return void
		 */
		public function __construct() {
			parent::__construct();
		}
		
		/**
		 * upload function.
		 * 
		 * Upload a file to the system, returns with filename
		 *
		 * @access public
		 * @return void
		 */
		public function upload() {
			$this->enforce_secure();
			$raw = file_get_contents("php://input");
			$dir = "/content/";
			$parts=date("Y")."/".date("m")."/".date("d")."/";
			
			@mkdir(".".$dir.$parts, 0755, true);
			if (!is_dir(".".$dir.$parts)) {
				$this->show_error("."."$dir$parts is not a directory or doesn't exist");
			}
			if (empty($raw)) {
				$this->show_error("File not received or empty");
			}
			if (isset($this->vars["filename"])) {
				$filename = $this->vars["filename"];
			} else {
				$filename = md5($raw);
			}
			$this->data["message"]=".".$dir.$parts.$filename;
			file_put_contents(".".$dir.$parts.$filename, $raw);
			$this->data["content"]=array("filename"=>$filename, "full_name"=>$dir.$parts.$filename);
			$this->returndata();
		}
		
		public function download() {
			$dir = "/content/";
			$uria = $this->uri->rsegment_array();
			$uria = array_slice($uria, 2);
			$filename = "./".rawurldecode(implode("/", $uria));
			if (!is_file($filename)) {
				show_404();
			}
			header("Pragma: public"); 
			header('Content-type: '.mime_content_type($filename));
			header('Content-Disposition: attachment; filename="'.basename($filename).'"');
			header('Content-Transfer-Encoding: binary'); 
			header('Content-Length: '.filesize($filename));
			ob_clean(); 
			flush(); 
			readfile($filename);
		}
	}

/* End of file files.php */
/* Location: ./system/application/controllers/api/ */<commit_msg>File browser and image renderer<commit_after><?php
	require_once('10layer/system/TL_Api.php');
	
	/**
	 * Files class
	 * 
	 * @extends CI_Controller
	 */
	class Files extends TL_Api {
		
		/**
		 * __construct function.
		 * 
		 * @access public
		 * @return void
		 */
		public function __construct() {
			parent::__construct();
		}
		
		/**
		 * upload function.
		 * 
		 * Upload a file to the system, returns with filename
		 *
		 * @access public
		 * @return void
		 */
		public function upload() {
			$this->enforce_secure();
			$raw = file_get_contents("php://input");
			$dir = "/content/";
			$parts=date("Y")."/".date("m")."/".date("d")."/";
			
			@mkdir(".".$dir.$parts, 0755, true);
			if (!is_dir(".".$dir.$parts)) {
				$this->show_error("."."$dir$parts is not a directory or doesn't exist");
			}
			if (empty($raw)) {
				$this->show_error("File not received or empty");
			}
			if (isset($this->vars["filename"])) {
				$filename = $this->vars["filename"];
			} else {
				$filename = md5($raw);
			}
			$this->data["message"]=".".$dir.$parts.$filename;
			file_put_contents(".".$dir.$parts.$filename, $raw);
			$this->data["content"]=array("filename"=>$filename, "full_name"=>$dir.$parts.$filename);
			$this->returndata();
		}
		
		public function download() {
			$dir = "/content/";
			$uria = $this->uri->rsegment_array();
			$uria = array_slice($uria, 2);
			$filename = "./".rawurldecode(implode("/", $uria));
			if (!is_file($filename)) {
				show_404();
			}
			header("Pragma: public"); 
			header('Content-type: '.mime_content_type($filename));
			header('Content-Disposition: attachment; filename="'.basename($filename).'"');
			header('Content-Transfer-Encoding: binary'); 
			header('Content-Length: '.filesize($filename));
			ob_clean(); 
			flush(); 
			readfile($filename);
		}
		
		public function browse() {
			$this->enforce_secure();
			$limit = $this->input->get_post("limit");
			if (empty($limit)) {
				$limit = 50;
			}
			$offset = $this->input->get_post("offset");
			$dir = "content";
			//$filetypes = array("jpg", "jpeg", "png", "svg", "gif", "mp4", "m4v", "doc", "docx", "xls", "xlsx", "pdf");
			$filetypes = array("jpg", "jpeg", "png", "svg", "gif");
			$files = $this->findFiles($dir, $filetypes);
			$this->data["content"]["count"] = sizeof($files);
			usort($files, array($this, "file_mtime_sort"));
			
			$files = array_slice($files, $offset, $offset + $limit);
			$this->data["content"]["files"]=$files;
			$this->data["content"]["filetypes"]=$filetypes;
			$this->returndata();
		}
		
		protected function findFiles($directory, $extensions = array()) {
			function glob_recursive($directory, &$directories = array()) {
				foreach(glob($directory, GLOB_ONLYDIR | GLOB_NOSORT) as $folder) {
					if (strpos($folder,"cache")===false) {
						$directories[] = $folder;
						glob_recursive("{$folder}/*", $directories);
					}
				}
				
			}
			glob_recursive($directory, $directories);
			$files = array ();
			foreach($directories as $directory) {
				foreach($extensions as $extension) {
					foreach(glob("{$directory}/*.{$extension}") as $file) {
						if (strpos($file,"cache")===false) {
							$files[] = $file;
						}
					}
				}
			}
			return $files;
		}
		
		protected function file_mtime_sort($f1, $f2) {
			if (filemtime($f1) == filemtime($f2)) {
				return 0;
			}
			return (filemtime($f1) < filemtime($f2)) ? 1 : -1;
		}
		
		public function image() {
			$filename = $this->input->get_post("filename");
			$width = $this->input->get_post("width");
			$height = $this->input->get_post("height");
			$render = $this->input->get_post("render");
			$dir = "content";
			$filetypes = array("jpg", "jpeg", "png", "svg", "gif", "mp4", "m4v", "doc", "docx", "xls", "xlsx", "pdf");
			$file = $dir."/".$filename;
			$parts = pathinfo($filename);

			if (strpos($filename,"..")!==false) {
				//This doesn't look good
				$this->data["error"]=true;
				$this->data["msg"]="Looks like you're trying to break out of the dir";
				$this->returndata();
				return true;
			}
			if (!file_exists($file)) {
				//This doesn't look good
				$this->data["error"]=true;
				$this->data["msg"]="File not found: $file";
				$this->returndata();
				return true;
			}
			if (!in_array($parts["extension"], $filetypes)) {
				$this->data["error"]=true;
				$this->data["msg"]="Cannot access file of that type";
				$this->returndata();
				return true;
			}
			
			$cache = "cache/content/".$parts["dirname"]."/".$parts["filename"]."-".$width."-".$height.".png";
			if (file_exists($cache)) {
				if ($render) {
					header("Content-type: image/png");
					header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($cache)).' GMT', true, 200);
			        header('Content-Length: '.filesize($cache));
		    	    readfile($cache);
		    	    return true;
				}
			}
			
			if (!is_dir("cache/content/".$parts["dirname"])) {
				$result=mkdir("cache/content/".$parts["dirname"], 0755, true);
			}
			exec("convert {$file} -background transparent -resize {$width}x{$height}^ -quality 80 -gravity center -extent {$width}x{$height} {$cache}", $result);
			if ($render) {
				header("Content-type: image/png");
				header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($cache)).' GMT', true, 200);
				header('Content-Length: '.filesize($cache));
				readfile($cache);
			    return true;
			}
		    $this->data["filename"]=$cache;
			$this->returndata();
		}
		
		
	}

/* End of file files.php */
/* Location: ./system/application/controllers/api/ */
{'fraction_non_alphanumeric': 0.2209286584540409, 'fraction_numerical': 0.00663327467172059, 'mean_word_length': 3.3793716656787196, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3376584', 'n_tokens_mistral': 3096, 'n_tokens_neox': 2495, 'n_words': 472}