View difference between Paste ID: WLVjDu6c and 4uukHL86
SHOW: | | - or go back to the newest paste.
1
public function add(){
2
        if(Session::get("tipo") == "admin"){
3
            Session::set('mod', isset($_POST['txtmod']) ? $_POST['txtmod'] : Session::get('mod'));
4
            $modelos = (Session::get('mod')!="") ? $this->mod_v->obtenerXDataList(Session::get('mod')) : array();
5
            if (isset($_POST['btnaceptar'])) {
6
                if($this->checkDates()) {
7
                    if(isset($_FILES['foto'])){
8
                        $ruta=$this->checkImage($_FILES['foto']);
9
                        if($ruta!= null){
10
                            $load= array($_POST['txtmat'],$_POST['txtcant'],$_POST['txtdes'],$ruta,$_POST['txtmod'],$_POST['cboxtipo']);
11
                            $this->mod_v->guardame($load);
12
                            Session::set('vehiculos', array());
13
                            Session::set("msg","Vehículo Creado");
14
                            $this->redirect(array('index.php'));
15
                            exit();
16
                        }
17
                    }
18
                }
19
            }
20
            Session::set('modelos', $modelos);
21
            Session::set('tiposveh', $this->mod_tv->obtenerTodos());
22
            $this->redirect(array('add.php'));
23
        }
24
        else {
25
            Session::set("msg","Debe ser administrador para acceder.");
26
            $this->redirect(array('Main','index.php'));
27
        }
28
    }
29
    public function edit(){
30
        if(Session::get("tipo") == "admin"){
31
            Session::set("id",$_GET['p']);
32
            Session::set('mod', isset($_POST['txtmod']) ? $_POST['txtmod'] : Session::get('mod'));
33
            $modelos = (Session::get('mod')!="") ? $this->mod_v->obtenerXDataList(Session::get('mod')) : array();
34
            if (Session::get('id')!=null && isset($_POST['btnaceptar'])){
35
                if($this->checkDates()) {
36
                    $load= array($_POST['hid'],$_POST['txtmat'],$_POST['txtcant'],$_POST['txtdes'],$_POST['txtmod'],$_POST['cboxtipo']);
37
                    $this->mod_v->modificame($load);
38
                    Session::set('vehiculos', array());
39
                    Session::set("msg","Vehículo Editado");
40
                    $this->redirect(array('index.php'));
41
                    exit();                                                     
42
                }
43
            }
44
            Session::set('vehiculo', $this->mod_v->obtenerXId(Session::get('id'))); 
45
            Session::set('modelos', $modelos);
46
            Session::set('tiposveh', $this->mod_tv->obtenerTodos());
47
            $this->redirect(array('edit.php'));
48
        }
49
        else {
50
            Session::set("msg","Debe ser administrador para acceder.");
51
            $this->redirect(array('Main','index.php'));
52
        }
53
    }
54
    public function foto(){
55
        if(Session::get("tipo") == "admin"){
56
            if (isset($_POST['btnaceptar'])) {
57
                if(isset($_FILES['foto'])){
58
                    $ruta= $this->checkImage($_FILES['foto']);
59
                    if($ruta!= null){
60
                        $load=array(Session::get('vehiculo')['vehId'],$ruta);
61
                        $this->mod_v->mod_foto($load);
62
                        echo 'anda';
63
                        header("Location:index.php?c=vehiculos&a=edit&p=$load[0]");
64
                        exit();                    
65
                    }
66
                }                                             
67
            }
68
            $this->redirect(array('foto.php'));
69
        }
70
        else {
71
            Session::set("msg","Debe ser administrador para acceder.");
72
            $this->redirect(array('Main','index.php'));
73
        }
74
    }
75
76
private function checkImage($file){
77
        if ($file["error"] > 0){
78
            Session::set('msg', "ha ocurrido un error");
79
            return null;
80
        } 
81
        else {
82
            $permitidos = array("image/jpg", "image/jpeg", "image/gif", "image/png");
83
            if($this->checkExtension($file)){
84-
                $prefijo = substr(md5(uniqid(rand())),0,6);            
84+
                // Es mejor calcular el prefijo y ruta luego de la validacion siguiente por q si no cumple habrias hecho ese trabajo en vano	
85-
                $ruta = "View/Layout/upload/".$prefijo."_".$file['name'];		
85+
86
                if (in_array($file['type'], $permitidos) && $file['size'] <= $limite_kb * 1024){
87
			// Es mejor su tu mismo agregas la extension al archivo en vez de confiar en lo que manda el usuario
88
			// Existe muchos bypass a validaciones de extension usando null bytes u otras tecnicas
89-
                    return ($resultado) ? $ruta : null; 		
89+
			// Por ejemplo imagen.jpg%00.php
90-
                } 
90+
			$extensiones = array('jpg', 'jpeg', 'gif', 'png');
91
			// Generamos el prefijo
92
			$prefijo = uniqueId(6);
93
			// Obtenemos la extension correcta para el archivo
94-
                }            
94+
			$extension = $extensiones[array_search($file['type'], $extensiones)];
95
			// Limpiamos el nombre del archivo de cualquier cosa rara
96
			$nombre = stringURLSafe($file['name']);
97
			// Y finalmente armamos la ruta final
98
			$ruta = "View/Layout/upload/$prefijo-$nombre.$extension";
99
                    $resultado = move_uploaded_file($file["tmp_name"], $ruta);
100
                    return ($resultado) ? $ruta : null;                
101
                }
102
                else {
103
                    Session::set('msg', "tipo de archivo no permitido o excede a los $limite_kb kb");
104
                    return null;
105
                }           
106-
            if($type == $file['type']){
106+
107
            else {
108
                Session::set('msg', "error en la extensión");
109
                return null;
110
            }
111-
    }
111+
112
    }
113
    private function checkExtension($file){
114
        $file['type'] = pathinfo($file['name'], PATHINFO_EXTENSION);
115
        $types = array('jpg', 'jpeg','png', 'gif');
116
	// Aca le agrego strtolower por si envian algo como .JPG q suele ser comun
117
        foreach($types as $type){
118
            if($type == strtolower($file['type'])){
119
                return true;
120
            }
121
        }
122
        return false;
123
    }
124
// Esta funcion nos limpiara el nombre del archivo eliminando cualquier caracter raro
125
function stringURLSafe($string){
126
    $str = str_replace('-', ' ', $string);
127
    $str = str_replace('_', ' ', $string);
128
    $str = str_replace('.', ' ', $string);
129
    $str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);
130
    $str = trim(strtolower($str));
131
    return $str;
132
}
133
// Mi version cuando quiero generar un id unico de longitud fija :)
134
function uniqueId($l = 8) {
135
    return substr(md5(uniqid(mt_rand(), true)), 0, $l);
136
}