article_image.php 3.24 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
6
// 
7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9
// $Id$
10

11 12
// application to display an image from the database with 
// option to resize the image dynamically creating a thumbnail on the fly.
13

14
// This handles 4 types of images, depending on the image_type parameter: 
15 16
// "article": Images for articles
// "submission": Images for article submissions
17
// "topic": Images for topics associated to articles
18
// "preview": Special case used for article previews. A recently uploaded custom article image. Not cached nor generated here. See tiki-edit_article.php
19
// Any other value is invalid
20
// If image_type has no value, we default to "article" to preserve previous behaviour
21

22
require_once ('tiki-setup.php');
lphuberdeau's avatar
lphuberdeau committed
23
$artlib = TikiLib::lib('art');
24

25
$access->check_feature('feature_articles');
26 27

// Now check permissions to access this page
28
$access->check_permission_either(array('tiki_p_read_article','tiki_p_articles_read_heading'));
29

30 31
if (!isset($_REQUEST["id"])) {
	die;
32
}
33
$useCache = isset($_REQUEST['cache']) && $_REQUEST['cache'] == 'y'; // cache only the image in list mode
34

35
// If image_type has no value, we default to "article" to preserve previous behaviour
36
if (!isset($_REQUEST["image_type"])) {
37 38 39
	$_REQUEST["image_type"]="article";
}

40 41
switch ($_REQUEST["image_type"]) {
	case "article":
42
		$imagePrefix="article";
43
    	break;
44
	case "submission":
45
		$imagePrefix="article_submission";
46
    	break;
47
	case "topic":
48
		$imagePrefix="article_topic";
49
    	break;
50
	case "preview":
51
		$imagePrefix="article_preview";
52
    	break;
53 54 55
	default:
		die;
}
56
$temporaryFile = $prefs['tmpDir'];
57
if ($tikidomain) { 
58
	$temporaryFile .= "/$tikidomain";
59
}
60
$temporaryFile .= "/$imagePrefix." . $_REQUEST["id"];
61

62 63 64 65 66 67

if ($imagePrefix == "article_preview" && !is_file($temporaryFile)) {
	header("HTTP/1.1 404 Not Found");
} elseif ( $imagePrefix != "article_preview" && (isset($_REQUEST["reload"]) || !$useCache) || !is_file($temporaryFile) ) {
	// If "reload" parameter is set, recreate the cached image file from database values.
	
68 69
	switch ($_REQUEST["image_type"]) {
		case "article":
70
			$storedData = $artlib->get_article_image($_REQUEST["id"]);
71
    		break;
72
		case "submission":
73
			$storedData = $artlib->get_submission($_REQUEST["id"]);
74
    		break;
75
		case "topic":
76
			$storedData = $artlib->get_topic_image($_REQUEST["id"]);
77
    		break;
78 79 80 81
		default:
			// Invalid value
			die;
	}
82 83
	// if blank then die, otherwise we offer to download strangeness
	// this also catches invalid id's
84
	if (!$storedData) {
85 86
		die;
	}
87
	$type = $storedData["image_type"];
88
	$data =& $storedData["image_data"];
89
	header("Content-type: ".$type);
90
	if (!empty($_REQUEST['width'])) {
91
		require_once('lib/images/images.php');
92
		$image = new Image($data);
93 94 95 96
		if ($image->get_width() > $_REQUEST['width']) {
			$image->resize($_REQUEST['width'], 0);
			$data = $image->display();
		}
97
		if (empty($data)) die;
98
	}
99
	if ($useCache && $data) {
100
		$fp = fopen($temporaryFile, "wb");
101
		fputs($fp, $data);
102 103 104
		fclose($fp);
	}
	echo $data;
105 106 107 108
} else {
	$size = getimagesize($temporaryFile);
	header("Content-type: ".$size['mime']);
	readfile($temporaryFile);
109
}