Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function add(){
- if(Session::get("tipo") == "admin"){
- Session::set('mod', isset($_POST['txtmod']) ? $_POST['txtmod'] : Session::get('mod'));
- $modelos = (Session::get('mod')!="") ? $this->mod_v->obtenerXDataList(Session::get('mod')) : array();
- if (isset($_POST['btnaceptar'])) {
- if($this->checkDates()) {
- if(isset($_FILES['foto'])){
- $ruta=$this->checkImage($_FILES['foto']);
- if($ruta!= null){
- $load= array($_POST['txtmat'],$_POST['txtcant'],$_POST['txtdes'],$ruta,$_POST['txtmod'],$_POST['cboxtipo']);
- $this->mod_v->guardame($load);
- Session::set('vehiculos', array());
- Session::set("msg","Vehículo Creado");
- $this->redirect(array('index.php'));
- exit();
- }
- }
- }
- }
- Session::set('modelos', $modelos);
- Session::set('tiposveh', $this->mod_tv->obtenerTodos());
- $this->redirect(array('add.php'));
- }
- else {
- Session::set("msg","Debe ser administrador para acceder.");
- $this->redirect(array('Main','index.php'));
- }
- }
- public function edit(){
- if(Session::get("tipo") == "admin"){
- Session::set("id",$_GET['p']);
- Session::set('mod', isset($_POST['txtmod']) ? $_POST['txtmod'] : Session::get('mod'));
- $modelos = (Session::get('mod')!="") ? $this->mod_v->obtenerXDataList(Session::get('mod')) : array();
- if (Session::get('id')!=null && isset($_POST['btnaceptar'])){
- if($this->checkDates()) {
- $load= array($_POST['hid'],$_POST['txtmat'],$_POST['txtcant'],$_POST['txtdes'],$_POST['txtmod'],$_POST['cboxtipo']);
- $this->mod_v->modificame($load);
- Session::set('vehiculos', array());
- Session::set("msg","Vehículo Editado");
- $this->redirect(array('index.php'));
- exit();
- }
- }
- Session::set('vehiculo', $this->mod_v->obtenerXId(Session::get('id')));
- Session::set('modelos', $modelos);
- Session::set('tiposveh', $this->mod_tv->obtenerTodos());
- $this->redirect(array('edit.php'));
- }
- else {
- Session::set("msg","Debe ser administrador para acceder.");
- $this->redirect(array('Main','index.php'));
- }
- }
- public function foto(){
- if(Session::get("tipo") == "admin"){
- if (isset($_POST['btnaceptar'])) {
- if(isset($_FILES['foto'])){
- $ruta= $this->checkImage($_FILES['foto']);
- if($ruta!= null){
- $load=array(Session::get('vehiculo')['vehId'],$ruta);
- $this->mod_v->mod_foto($load);
- echo 'anda';
- header("Location:index.php?c=vehiculos&a=edit&p=$load[0]");
- exit();
- }
- }
- }
- $this->redirect(array('foto.php'));
- }
- else {
- Session::set("msg","Debe ser administrador para acceder.");
- $this->redirect(array('Main','index.php'));
- }
- }
- private function checkImage($file){
- if ($file["error"] > 0){
- Session::set('msg', "ha ocurrido un error");
- return null;
- }
- else {
- $permitidos = array("image/jpg", "image/jpeg", "image/gif", "image/png");
- if($this->checkExtension($file)){
- // Es mejor calcular el prefijo y ruta luego de la validacion siguiente por q si no cumple habrias hecho ese trabajo en vano
- $limite_kb = 100;
- if (in_array($file['type'], $permitidos) && $file['size'] <= $limite_kb * 1024){
- // Es mejor su tu mismo agregas la extension al archivo en vez de confiar en lo que manda el usuario
- // Existe muchos bypass a validaciones de extension usando null bytes u otras tecnicas
- // Por ejemplo imagen.jpg%00.php
- $extensiones = array('jpg', 'jpeg', 'gif', 'png');
- // Generamos el prefijo
- $prefijo = uniqueId(6);
- // Obtenemos la extension correcta para el archivo
- $extension = $extensiones[array_search($file['type'], $extensiones)];
- // Limpiamos el nombre del archivo de cualquier cosa rara
- $nombre = stringURLSafe($file['name']);
- // Y finalmente armamos la ruta final
- $ruta = "View/Layout/upload/$prefijo-$nombre.$extension";
- $resultado = move_uploaded_file($file["tmp_name"], $ruta);
- return ($resultado) ? $ruta : null;
- }
- else {
- Session::set('msg', "tipo de archivo no permitido o excede a los $limite_kb kb");
- return null;
- }
- }
- else {
- Session::set('msg', "error en la extensión");
- return null;
- }
- }
- }
- private function checkExtension($file){
- $file['type'] = pathinfo($file['name'], PATHINFO_EXTENSION);
- $types = array('jpg', 'jpeg','png', 'gif');
- // Aca le agrego strtolower por si envian algo como .JPG q suele ser comun
- foreach($types as $type){
- if($type == strtolower($file['type'])){
- return true;
- }
- }
- return false;
- }
- // Esta funcion nos limpiara el nombre del archivo eliminando cualquier caracter raro
- function stringURLSafe($string){
- $str = str_replace('-', ' ', $string);
- $str = str_replace('_', ' ', $string);
- $str = str_replace('.', ' ', $string);
- $str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);
- $str = trim(strtolower($str));
- return $str;
- }
- // Mi version cuando quiero generar un id unico de longitud fija :)
- function uniqueId($l = 8) {
- return substr(md5(uniqid(mt_rand(), true)), 0, $l);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement