1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ocrend Framewok 3 package. |
5
|
|
|
* |
6
|
|
|
* (c) Ocrend Software <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ocrend\Kernel\Helpers; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Helper con funciones útiles para el tratamiento de archivos. |
16
|
|
|
* |
17
|
|
|
* @author Brayan Narváez <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
class Files extends \Twig_Extension { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Devuelve un string con el contenido de un archivo |
24
|
|
|
* |
25
|
|
|
* @param string $dir: Directorio del archivo a leer |
26
|
|
|
* |
27
|
|
|
* @return string con contenido del archivo |
28
|
|
|
*/ |
29
|
|
|
public static function read_file(string $dir) : string { |
30
|
|
|
$lines = ''; |
31
|
|
|
$f = new \SplFileObject($dir); |
32
|
|
|
while (!$f->eof()) { |
33
|
|
|
$lines .= $f->fgets(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return (string) $lines; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Escribe un string completo en un archivo, si este no existe lo crea |
41
|
|
|
* |
42
|
|
|
* @param string $dir: Directorio del archivo escribir/crear |
43
|
|
|
* @param string $content: Contenido a escribir |
44
|
|
|
* |
45
|
|
|
* @return int catidad de bytes escritos en el archivo |
46
|
|
|
*/ |
47
|
|
|
public static function write_file(string $dir, string $content) : int { |
48
|
|
|
$f = new \SplFileObject($dir,'w'); |
49
|
|
|
return (int) $f->fwrite($content); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Escribe un string al final, en un archivo existente |
54
|
|
|
* |
55
|
|
|
* @param string $dir: Directorio del archivo sobre el cual se va a escribir |
56
|
|
|
* @param string $content: Contenido a escribir |
57
|
|
|
* |
58
|
|
|
* @return int : catidad de bytes escritos en el archivo |
59
|
|
|
*/ |
60
|
|
|
public static function write_in_file(string $dir, string $content) : int { |
61
|
|
|
$f = new \SplFileObject($dir,'a+'); |
62
|
|
|
return (int) $f->fwrite("\n\r" . $content); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Borra un archivo en un directorio |
67
|
|
|
* |
68
|
|
|
* @param string $route: Ruta del fichero |
69
|
|
|
* |
70
|
|
|
* @return bool true si borró el fichero, false si no (porque no existía) |
71
|
|
|
*/ |
72
|
|
|
public static function delete_file(string $route) : bool { |
73
|
|
|
if (file_exists($route)) { |
74
|
|
|
unlink($route); |
75
|
|
|
|
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Devuelve la extensión de un archivo cualquiera, da igual si es solo el nombre o la ruta con el nombre |
84
|
|
|
* |
85
|
|
|
* @param string $file_name: Nombre del archivo, da igual si es solo el nombre o la ruta con el nombre |
86
|
|
|
* |
87
|
|
|
* @return mixed string con la extensión, devuelve un string '' si no existe información alguna acerca de la extensión |
88
|
|
|
*/ |
89
|
|
|
public static function get_file_ext(string $file_name) { |
90
|
|
|
return pathinfo($file_name, PATHINFO_EXTENSION); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Dice si un elemento es una imagen o no según su extensión |
95
|
|
|
* |
96
|
|
|
* @param string $file_name: Nombre del archivo, da igual si es solo el nombre o la ruta con el nombre |
97
|
|
|
* |
98
|
|
|
* @return bool true si es una imagen, false si no lo es |
99
|
|
|
*/ |
100
|
|
|
public static function is_image(string $file_name) : bool { |
101
|
|
|
return (bool) in_array(self::get_file_ext($file_name), ['jpg', 'png', 'jpeg', 'gif', 'JPG', 'PNG', 'JPEG', 'GIF']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Devuelve el tamaño en Kbytes de un fichero |
106
|
|
|
* |
107
|
|
|
* @param string $file: path del fichero |
108
|
|
|
* |
109
|
|
|
* @return int con el tamaño del fichero |
110
|
|
|
*/ |
111
|
|
|
public static function file_size(string $file) : int { |
112
|
|
|
return (int) round(filesize($file)*0.0009765625, 1); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Devuelve la fecha y hora exacta de creación de un fichero |
117
|
|
|
* |
118
|
|
|
* @param string $file: path del fichero |
119
|
|
|
* |
120
|
|
|
* @return string con la fecha del fichero en el formato d-m-y h:i:s |
121
|
|
|
*/ |
122
|
|
|
public static function date_file(string $file) : string { |
123
|
|
|
return date('d-m-Y h:i:s', filemtime($file)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Devuelve en un arreglo numérico, la ruta de todos los ficheros en un directorio filtrado por tipos |
128
|
|
|
* |
129
|
|
|
* @param string $dir: directorio completo |
130
|
|
|
* @param string $types: tipos de archivos a buscar, por defecto '' significa todos, se puede pasar por ejemplo 'jpg' |
131
|
|
|
* |
132
|
|
|
* @return array con las rutas de todos los ficheros encontrados, un array vacío si no encontró ficheros |
133
|
|
|
*/ |
134
|
|
|
public static function get_files_in_dir(string $dir, string $types = '') : array { |
135
|
|
|
$array = array(); |
136
|
|
|
if (is_dir($dir)) { |
137
|
|
|
foreach (glob($dir . '*' . $types) as $file) { |
138
|
|
|
$array[] = $file; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
return $array; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Crea un directorio |
146
|
|
|
* |
147
|
|
|
* @param string $dir: Directorio a crear |
148
|
|
|
* @param int $permisos: Permisos del directorio a crear, por defecto es 0655 |
149
|
|
|
* |
150
|
|
|
* @return bool con true si fue creado con éxito, false si el directorio ya existía o hubo algún error |
151
|
|
|
*/ |
152
|
|
|
public static function create_dir(string $dir, int $permisos = 0655) : bool { |
153
|
|
|
if(is_dir($dir)) { |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return (bool) mkdir($dir,$permisos,true); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Elimina de forma recursiva un directorio con su contenido |
163
|
|
|
* |
164
|
|
|
* @author brandonwamboldt |
165
|
|
|
* |
166
|
|
|
* @param string $dir: Directorio a borrar |
167
|
|
|
* |
168
|
|
|
* @return bool true si todo se borró con éxito |
169
|
|
|
*/ |
170
|
|
|
public static function rm_dir(string $dir) : bool { |
171
|
|
|
|
172
|
|
|
# Evitar una desgracia |
173
|
|
|
if (in_array($dir, [ |
174
|
|
|
'api', |
175
|
|
|
'api/http', |
176
|
|
|
'app', |
177
|
|
|
'app/controllers', |
178
|
|
|
'app/models', |
179
|
|
|
'app/templates', |
180
|
|
|
'Ocrend/', |
181
|
|
|
'Ocrend/Kernel', |
182
|
|
|
'Ocrend/vendor', |
183
|
|
|
'Ocrend/KernelConfig', |
184
|
|
|
'Ocrend/Kernel/Controllers', |
185
|
|
|
'Ocrend/Kernel/Database', |
186
|
|
|
'Ocrend/Kernel/Helpers', |
187
|
|
|
'Ocrend/Kernel/Models', |
188
|
|
|
'Ocrend/Kernel/Router' |
189
|
|
|
])) { |
190
|
|
|
throw new \RuntimeException('No puede eliminar la ruta ' . $dir . ', ya que es crítica.'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
foreach (glob($dir . "/*") as $archivos_carpeta) { |
194
|
|
|
if (is_dir($archivos_carpeta)) { |
195
|
|
|
self::rm_dir($archivos_carpeta); |
196
|
|
|
} else { |
197
|
|
|
unlink($archivos_carpeta); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
rmdir($dir); |
201
|
|
|
|
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Devuelve la cantidad de imágenes contenidas dentro de un directorio |
207
|
|
|
* |
208
|
|
|
* @param string $dir: Directorio en donde están las imagenes |
209
|
|
|
* |
210
|
|
|
* @return int cantidad de imágenes |
211
|
|
|
*/ |
212
|
|
|
public static function images_in_dir(string $dir) : int { |
213
|
|
|
return sizeof(glob($dir . '{*.jpg,*.gif,*.png,*.gif,*.jpeg,*.JPG,*.GIF,*.PNG,*.JPEG}', GLOB_BRACE)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
//------------------------------------------------ |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Copia todos los ficheros de un directorio a un directorio nuevo, si el directorio nuevo no existe, es creado. |
220
|
|
|
* Si en el directorio nuevo existe un archivo con el mismo nombre de alguno en el viejo, este será sobreescrito. |
221
|
|
|
* |
222
|
|
|
* @param string $old_dir: Ruta del directorio viejo (de donde se moverán los ficheros) |
223
|
|
|
* @param string $new_dir: Ruta del directorio nuevo (hacia donde se moverán los ficheros) |
224
|
|
|
* @param bool $only_images: Pasar como TRUE, si sólo quiere pasar imagenes |
225
|
|
|
* @param bool $delete_old: Pasar como TRUE, si se quiere borrar todo el contenido del viejo directorio al pasarse |
226
|
|
|
* |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
public static function move_from_dir(string $old_dir, string $new_dir, bool $only_images = false, bool $delete_old = false) { |
230
|
|
|
self::create_dir($new_dir); |
231
|
|
|
|
232
|
|
|
foreach(glob($old_dir . ($only_images ? '{*.jpg,*.gif,*.png,*.gif,*.jpeg,*.JPG,*.GIF,*.PNG,*.JPEG}' : '*'),GLOB_BRACE) as $file) { |
233
|
|
|
if(file_exists($file)) { |
234
|
|
|
unlink($file); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$name = explode('/',$file); |
238
|
|
|
$name = end($name); |
239
|
|
|
copy($file,$new_dir . $name); |
240
|
|
|
|
241
|
|
|
if($delete_old) { |
242
|
|
|
unlink($file); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Se obtiene de Twig_Extension y sirve para que cada función esté disponible como etiqueta en twig |
249
|
|
|
* |
250
|
|
|
* @return array con todas las funciones con sus respectivos nombres de acceso en plantillas twig |
251
|
|
|
*/ |
252
|
|
|
public function getFunctions() : array { |
253
|
|
|
return array( |
254
|
|
|
new \Twig_Function('images_in_dir', array($this, 'images_in_dir')), |
255
|
|
|
new \Twig_Function('get_files_in_dir', array($this, 'get_files_in_dir')), |
256
|
|
|
new \Twig_Function('date_file', array($this, 'date_file')), |
257
|
|
|
new \Twig_Function('file_size', array($this, 'file_size')), |
258
|
|
|
new \Twig_Function('is_image', array($this, 'is_image')), |
259
|
|
|
new \Twig_Function('get_file_ext', array($this, 'get_file_ext')) |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Identificador único para la extensión de twig |
265
|
|
|
* |
266
|
|
|
* @return string con el nombre de la extensión |
267
|
|
|
*/ |
268
|
|
|
public function getName() : string { |
269
|
|
|
return 'ocrend_framework_helper_files'; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
} |