Merge pull request #8 from radiocontrolled/0004872
[atutor.git] / get_profile_img.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id$
13
14 define('AT_INCLUDE_PATH', 'include/');
15 @ob_end_clean();
16 header("Content-Encoding: none");
17
18 $_user_location = 'public';
19 $nophoto_flag = false;  //true if no profile photo exists
20
21 require(AT_INCLUDE_PATH . 'vitals.inc.php');
22 require(AT_INCLUDE_PATH . 'lib/mime.inc.php');
23
24 $id = intval($_GET['id']);
25 if (isset($_GET['size']) && $_GET['size'] == 'o') {
26         $size = 'originals'; //t (thumbnail) or o (original)
27 } elseif (isset($_GET['size']) && $_GET['size'] == 'p') {
28         $size = 'profile'; //p (profile) 
29 } else {
30         $size = 'thumbs';
31 }
32
33 $file = AT_CONTENT_DIR . 'profile_pictures/' . $size .'/'. $id .'.';
34 $extensions = array('gif', 'jpg', 'png');
35
36 foreach ($extensions as $extension) {
37         if (file_exists($file.$extension)) {
38                 $file .= $extension;
39         }
40 }
41
42 //if file does not exist, quit.
43 if (!file_exists($file)){
44         $file = AT_INCLUDE_PATH.'../images/nophoto.gif';
45         $nophoto_flag = true;
46
47
48 $pathinfo = pathinfo($file);
49
50 $ext = $pathinfo['extension'];
51 if ($ext == '') {
52         $ext = 'application/octet-stream';
53 } else {
54         $ext = $mime[$ext][0];
55 }
56
57 $real = realpath($file);
58
59 if ($nophoto_flag || (file_exists($real) && (substr($real, 0, strlen(AT_CONTENT_DIR)) == AT_CONTENT_DIR))) {
60         header('Content-Disposition: filename="'.$size.$id.'.'.$pathinfo['extension'].'"');     
61         /**
62          * although we can check if mod_xsendfile is installed in apache2
63          * we can't actually check if it's enabled. also, we can't check if
64          * it's enabled and installed in lighty, so instead we send the 
65          * header anyway, if it works then the line after it will not
66          * execute. if it doesn't work, then the line after it will replace
67          * it so that the full server path is not exposed.
68          *
69          * x-sendfile is supported in apache2 and lighttpd 1.5+ (previously
70          * named x-send-file in lighttpd 1.4)
71          */
72         header('x-Sendfile: '.$real);
73         header('x-Sendfile: ', TRUE); // if we get here then it didn't work
74
75         header('Content-Type: '.$ext);
76
77         @readfile($real);
78         exit;
79 } else {
80
81         header('HTTP/1.1 404 Not Found', TRUE);
82         exit;
83 }
84
85 ?>