forked from LocalGood/Goteo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.php
680 lines (595 loc) · 22.1 KB
/
image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
<?php
/*
* Copyright (C) 2012 Platoniq y Fundación Fuentes Abiertas (see README for details)
* This file is part of Goteo.
*
* Goteo 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.
*
* Goteo 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 Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
*
*/
namespace Goteo\Model {
use Goteo\Library\Text;
class Image extends \Goteo\Core\Model {
public
$id,
$name,
$type,
$tmp,
$error,
$size,
$dir_originals,
$dir_cache;
public static $types = array('user','project', 'post', 'glossary', 'info');
/**
* Constructor.
*
* @param type array $file Array $_FILES.
*/
public function __construct ($file) {
$this->dir_originals = GOTEO_DATA_PATH . 'images' . DIRECTORY_SEPARATOR;
$this->dir_cache = GOTEO_DATA_PATH . 'cache' . DIRECTORY_SEPARATOR;
if(is_array($file)) {
$this->name = self::check_filename($file['name'], $this->dir_originals);
$this->type = $file['type'];
$this->tmp = $file['tmp_name'];
$this->error = $file['error'];
$this->size = $file['size'];
}
elseif(is_string($file)) {
$this->name = self::check_filename(basename($file), $this->dir_originals);
$this->tmp = $file;
}
//die($this->dir_originals);
if(!is_dir($this->dir_originals)) {
mkdir($this->dir_originals);
}
if(!is_dir($this->dir_cache)) {
mkdir($this->dir_cache);
}
}
/**
* Sobrecarga de métodos 'getter'.
*
* @param type string $name
* @return type mixed
*/
public function __get ($name) {
if($name == "content") {
return $this->getContent();
}
return $this->$name;
}
/**
* (non-PHPdoc)
* @see Goteo\Core.Model::save()
*/
public function save(&$errors = array()) {
if($this->validate($errors)) {
$data[':name'] = $this->name;
if(!empty($this->type)) {
$data[':type'] = $this->type;
}
if(!empty($this->size)) {
$data[':size'] = $this->size;
}
//si es un archivo que se sube
if(is_uploaded_file($this->tmp)) {
move_uploaded_file($this->tmp,$this->dir_originals . $this->name);
chmod($this->dir_originals . $this->name, 0777);
}
else {
$errors[] = Text::get('image-upload-fail');
return false;
}
try {
// Construye SQL.
$query = "REPLACE INTO image (";
foreach($data AS $key => $row) {
$query .= substr($key, 1) . ", ";
}
$query = substr($query, 0, -2) . ") VALUES (";
foreach($data AS $key => $row) {
$query .= $key . ", ";
}
$query = substr($query, 0, -2) . ")";
// Ejecuta SQL.
$result = self::query($query, $data);
if(empty($this->id)) $this->id = self::insertId();
return true;
} catch(\PDOException $e) {
$errors[] = Text::_("No se ha guardado correctamente. ") . $e->getMessage();
return false;
}
}
return false;
}
public function saveExternalFile(&$errors = array()) {
/*
die("test");
$url = $this->avatar['profile_image_url'];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$profile_image_file = curl_exec( $ch );
curl_close();
*/
$data[':name'] = $this->name;
if(!empty($this->type)) {
$data[':type'] = $this->type;
}
if(!empty($this->size)) {
$data[':size'] = $this->size;
}
//si es un archivo que se sube
if(!empty($this->tmp)) {
$url = $this->tmp;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$profile_image_file = curl_exec( $ch );
curl_close();
file_put_contents($this->dir_originals . $this->name, $profile_image_file);
chmod($this->dir_originals . $this->name, 0777);
}
else {
$errors[] = Text::get('image-upload-fail');
return false;
}
try {
// Construye SQL.
$query = "REPLACE INTO image (";
foreach($data AS $key => $row) {
$query .= substr($key, 1) . ", ";
}
$query = substr($query, 0, -2) . ") VALUES (";
foreach($data AS $key => $row) {
$query .= $key . ", ";
}
$query = substr($query, 0, -2) . ")";
// Ejecuta SQL.
$result = self::query($query, $data);
if(empty($this->id)) $this->id = self::insertId();
return true;
} catch(\PDOException $e) {
$errors[] = Text::_("No se ha guardado correctamente. ") . $e->getMessage();
return false;
}
}
/**
* Returns a secure name to store in file system, if the generated filename exists returns a non-existing one
* @param $name original name to be changed-sanitized
* @param $dir if specified, generated name will be changed if exists in that dir
*/
public static function check_filename($name='',$dir=null){
$name = preg_replace("/[^a-z0-9_~\.-]+/","-",strtolower(self::idealiza($name, true)));
if(is_dir($dir)) {
while ( file_exists ( "$dir/$name" )) {
$name = preg_replace ( "/^(.+?)(_?)(\d*)(\.[^.]+)?$/e", "'\$1_'.(\$3+1).'\$4'", $name );
}
}
return $name;
}
/**
* (non-PHPdoc)
* @see Goteo\Core.Model::validate()
*/
public function validate(&$errors = array()) {
if(empty($this->name)) {
$errors['image'] = Text::get('error-image-name');
}
// checkeo de errores de $_FILES
if($this->error !== UPLOAD_ERR_OK) {
switch($this->error) {
case UPLOAD_ERR_INI_SIZE:
$errors['image'] = Text::get('error-image-size-too-large');
break;
case UPLOAD_ERR_FORM_SIZE:
$errors['image'] = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case UPLOAD_ERR_PARTIAL:
$errors['image'] = 'The uploaded file was only partially uploaded';
break;
case UPLOAD_ERR_NO_FILE:
if (isset($_POST['upload']))
$errors['image'] = 'No file was uploaded';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$errors['image'] = 'Missing a temporary folder';
break;
case UPLOAD_ERR_CANT_WRITE:
$errors['image'] = 'Failed to write file to disk';
break;
case UPLOAD_ERR_EXTENSION:
$errors['image'] = 'A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions';
break;
}
return false;
}
if(!empty($this->type)) {
$allowed_types = array(
'image/gif',
'image/jpeg',
'image/png',
'image/svg+xml',
);
if(!in_array($this->type, $allowed_types)) {
$errors['image'] = Text::get('error-image-type-not-allowed');
}
}
else {
$errors['image'] = Text::get('error-image-type');
}
if(empty($this->tmp) || $this->tmp == "none") {
$errors['image'] = Text::get('error-image-tmp');
}
if(empty($this->size)) {
$errors['image'] = Text::get('error-image-size');
}
return empty($errors);
}
/**
* Imagen.
*
* @param type int $id
* @return type object Image
*/
static public function get ($id) {
try {
$query = static::query("
SELECT
id,
name,
type,
size
FROM image
WHERE id = :id
", array(':id' => $id));
$image = $query->fetchObject(__CLASS__);
return $image;
} catch(\PDOException $e) {
return false;
}
}
/**
* Galeria de imágenes de un usuario / proyecto
*
* @param varchar(50) $id user id |project id
* @param string $which 'user'|'project'
* @return mixed false|array de instancias de Image
*/
public static function getAll ($id, $which) {
if (!\is_string($which) || !\in_array($which, self::$types)) {
return false;
}
$gallery = array();
$_which = "`" . GOTEO_DB_SCHEMA ."`." . $which;
$_id = $id;
try {
$sql = "SELECT image FROM {$_which}_image WHERE {$which} = :id";
$sql .= ($which == 'project') ? " ORDER BY {$_which}_image.section ASC, `order` ASC, image DESC" : " ORDER BY image ASC";
$query = self::query($sql, array(':id' => $_id));
foreach ($query->fetchAll(\PDO::FETCH_ASSOC) as $image) {
$gallery[] = self::get($image['image']);
}
return $gallery;
} catch(\PDOException $e) {
return false;
}
}
/**
* Quita una imagen de la tabla de relaciones y de la tabla de imagenes
*
* @param string $which 'user'|'project'|'post'
* @return bool true|false
*
*/
public function remove($which) {
try {
self::query("START TRANSACTION");
$sql = "DELETE FROM image WHERE id = ?";
$query = self::query($sql, array($this->id));
// para usuarios y proyectos que tienen N imagenes
// por ahora post solo tiene 1
if (\is_string($which) && \in_array($which, self::$types)) {
$sql = "DELETE FROM `" . \GOTEO_DB_SCHEMA . "`.{$which}_image WHERE image = ?";
$query = self::query($sql, array($this->id));
}
self::query("COMMIT");
return true;
} catch(\PDOException $e) {
return false;
}
}
/**
* Para montar la url de una imagen (porque las url con parametros no se cachean bien)
* - Si el thumb está creado, montamos la url de /data/cache
* - Sino, monamos la url de /image/
*
* @param type int $id
* @param type int $width
* @param type int $height
* @param type int $crop
* @return type string
*/
public function getLink ($width = 200, $height = 200, $crop = false) {
$src_url = preg_replace('/[A-Za-z0-9.]+\.localgood/','static.localgood',SRC_URL);
$tc = $crop ? 'c' : '';
$cache = $this->dir_cache . "{$width}x{$height}{$tc}" . DIRECTORY_SEPARATOR . $this->name;
if (\file_exists($cache)) {
return $src_url . "/data/cache/{$width}x{$height}{$tc}/{$this->name}";
} else {
return SRC_URL . "/image/{$this->id}/{$width}/{$height}/" . $crop;
}
}
/**
* Carga la imagen en el directorio temporal del sistema.
*
* @return type bool
*/
public function load () {
if(!empty($this->id) && !empty($this->name)) {
$tmp = tempnam(sys_get_temp_dir(), 'Goteo');
$file = fopen($tmp, "w");
fwrite($file, $this->content);
fclose($file);
if(!file_exists($tmp)) {
throw \Goteo\Core\Exception("Error al cargar la imagen temporal.");
}
else {
$this->tmp = $tmp;
return true;
}
}
}
/**
* Elimina la imagen temporal.
*
* @return type bool
*/
public function unload () {
if(!empty($this->tmp)) {
if(!file_exists($this->tmp)) {
throw \Goteo\Core\Exception("Error, la imagen temporal no ha sido encontrada.");
}
else {
unlink($this->tmp);
unset($this->tmp);
return true;
}
}
return false;
}
/**
* Muestra la imagen en pantalla.
* @param type int $width
* @param type int $height
*/
public function display ($width, $height, $crop) {
require_once PEAR . 'Image/Transform.php';
$it =& \Image_Transform::factory('GD');
if (\PEAR::isError($it)) {
die($it->getMessage() . '<br />' . $it->getDebugInfo());
}
$cache = $this->dir_cache . $width."x$height" . ($crop ? "c" : "") . DIRECTORY_SEPARATOR;
if(!is_dir($cache)) mkdir($cache);
$cache .= $this->name;
//comprova si existeix catxe
if(!is_file($cache)) {
$it->load($this->dir_originals . $this->name);
if($crop) {
if ($width > $height) {
$f = $height / $width;
$new_y = round($it->img_x * $f);
//
if($new_y < $it->img_y) {
$at = round(( $it->img_y - $new_y ) / 2);
$it->crop($it->img_x, $new_y, 0, $at);
$it->img_y = $new_y;
}
$it->resized = false;
$it->scaleByX($width);
} else {
$f = $width / $height;
$new_x = round($it->img_y * $f);
if($new_x < $it->img_x) {
$at = round(( $it->img_x - $new_x ) / 2);
$it->crop($new_x, $it->img_y, $at, 0);
$it->img_x = $new_x;
}
$it->resized = false;
$it->scaleByY($height);
}
}
else $it->fit($width,$height);
$it->save($cache);
chmod($cache, 0777);
}
header("Content-type: " . $this->type);
readfile($cache);
return true;
}
public function isGIF () {
return ($this->type == 'image/gif');
}
public function isJPG () {
return ($this->type == 'image/jpg') || ($this->type == 'image/jpeg');
}
public function isPNG () {
return ($this->type == 'image/png');
}
public function toGIF () {
$this->load();
if(!$this->isGIF()) {
list($width, $height, $type) = getimagesize($this->tmp);
switch($type) {
case 1:
$image = imagecreatefromgif($this->tmp);
break;
default:
case 2:
$image = imagecreatefromjpeg($this->tmp);
break;
case 3:
$image = imagecreatefrompng($this->tmp);
break;
case 6:
$image = imagecreatefromwbmp($this->tmp);
break;
}
$tmp = static::replace_extension($this->tmp, 'gif');
$this->unload();
$this->tmp = $tmp;
imagegif($image, $this->tmp);
imagedestroy($image);
return true;
}
return;
}
public function toJPG () {
$this->load();
if(!$this->isJPG()) {
list($width, $height, $type) = getimagesize($this->tmp);
switch($type) {
case 1:
$image = imagecreatefromgif($this->tmp);
break;
default:
case 2:
$image = imagecreatefromjpeg($this->tmp);
break;
case 3:
$image = imagecreatefrompng($this->tmp);
break;
case 6:
$image = imagecreatefromwbmp($this->tmp);
break;
}
$tmp = static::replace_extension($this->tmp, 'gif');
$this->unload();
$this->tmp = $tmp;
imagejpeg($image, $this->tmp, 100);
imagedestroy($image);
return true;
}
return;
}
public function toPNG () {
$this->load();
if(!$this->isPNG()) {
list($width, $height, $type) = getimagesize($this->tmp);
switch($type) {
case 1:
$image = imagecreatefromgif($this->tmp);
break;
default:
case 2:
$image = imagecreatefromjpeg($this->tmp);
break;
case 3:
$image = imagecreatefrompng($this->tmp);
break;
case 6:
$image = imagecreatefromwbmp($this->tmp);
break;
}
$tmp = static::replace_extension($this->tmp, 'gif');
$this->unload();
$this->tmp = $tmp;
imagepng($image, $this->tmp, 100);
imagedestroy($image);
return true;
}
return;
}
private function getContent () {
return file_get_contents($this->dir_originals . $this->name);
}
/*
* Devuelve la imagen en GIF.
*
* @return type object Image
*/
static public function gif ($id) {
$img = static::get($id);
if(!$img->isGIF())
$img->toGIF();
return $img;
}
/*
* Devuelve la imagen en JPG/JPEG.
*
* @return type object Image
*/
static public function jpg ($id) {
$img = static::get($id);
if ($img->isJPG())
$img->toJPG();
return $img;
}
/*
* Devuelve la imagen en PNG.
*
* @return type object Image
*/
static public function png ($id) {
$img = self::get($id);
if ($img->isPNG())
$img->toPNG();
return $img;
}
/**
* Reemplaza la extensión de la imagen.
*
* @param type string $src
* @param type string $new
* @return type string
*/
static private function replace_extension($src, $new) {
$pathinfo = pathinfo($src);
unset($pathinfo["basename"]);
unset($pathinfo["extension"]);
return implode(DIRECTORY_SEPARATOR, $pathinfo) . '.' . $new;
}
//
// DB接続先の横取り
//
public static function query ($query, $params = null) {
$query = self::queryFilter($query);
static $db = null;
if ($db === null) {
try {
$dsn = \GOTEO_DB_DRIVER . ':host=' . \GOTEO_DB_HOST . ';dbname=' . \COMMON_AUTH_DB_SCHEMA;
$db = new \PDO($dsn, \GOTEO_DB_USERNAME, \GOTEO_DB_PASSWORD, array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"));;
} catch (\PDOException $e) {
die ('No puede conectar la base de datos');
}
}
$params = func_num_args() === 2 && is_array($params) ? $params : array_slice(func_get_args(), 1);
// ojo que el stripslashes jode el contenido blob al grabar las imagenes
if (\get_magic_quotes_gpc ()) {
foreach ($params as $key => $value) {
if ($key != ':content') {
$params[$key] = \stripslashes(\stripslashes($value));
}
}
}
$result = $db->prepare($query);
try {
$result->execute($params);
return $result;
} catch (\PDOException $e) {
throw new Exception("Error PDO: " . \trace($e));
}
}
}
}