6f1b6094cfe6e87c6a408b0983b5747be622c13d
[atutor.git] / mods / photos / include / lib.inc.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2009                                                                              */
6 /* Adaptive Technology Resource Centre / University of Toronto  */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14
15 /**
16  * Convert all input to htmlentities output, in UTF-8.
17  * @param       string  input to be convert
18  * @param       boolean true if we wish to change all carrier returns to a <br/> tag, false otherwise.
19  * TODO: use htmlentities_utf8 in social when this become a standard module.
20  */
21 function htmlentities_utf82($str, $use_nl2br=true){
22         $return = htmlentities($str, ENT_QUOTES, 'UTF-8');
23         if ($use_nl2br){
24                 return nl2br($return);
25         } 
26         return $return;
27 }
28
29
30 /** 
31  * Generate album path padding by using album_id + album_created_date
32  */
33 function getPhotoFilePath($id, $filename, $timestamp){
34         $padding = hash('sha1', $id.$timestamp); 
35         $path_parts = pathinfo($filename);
36         //return the hash if filename is empty.
37         //this is used for validation purposes.
38         if($filename==''){
39                 return $padding;
40         }
41
42         $extension  = strtolower($path_parts['extension']);
43         //Note: the padding might not be unique, but the path is ALWAYS unique 
44         //              because the id is unique.  
45         return ($id.'_'.substr($padding, -5).'.'.$extension);
46 }
47
48 /** 
49  * Generate album path padding by using album_id + album_created_date
50  */
51 function getAlbumFilePath($id, $timestamp){
52         $padding = hash('sha1', $id.$timestamp); 
53         //Note: the padding might not be unique, but the path is ALWAYS unique 
54         //              because the id is unique.  
55         return ($id.'_'.substr($padding, -5));
56 }
57
58 /** 
59  * Check if the photo is supported, including extension check, file size check
60  * and library support checks.
61  * @param       string  location of the file.
62  * @return      $_FILE[] on successful, null on failure.
63  */
64 function checkPhoto($file){
65         global $stripslashes;
66         global $msg, $_config;
67         $msg = new AjaxMessage();
68
69         // check if GD is installed
70         if (!extension_loaded('gd')) {
71                 $msg->printInfos('FEATURE_NOT_AVAILABLE');
72                 return false;
73         }
74
75         // check if folder exists, if not, create it
76         if (!is_dir(AT_PA_CONTENT_DIR)) {
77                 mkdir(AT_PA_CONTENT_DIR);
78         }
79
80         //check GD support 
81         $gd_info = gd_info();
82
83         $supported_images = array();
84         if ($gd_info['GIF Create Support']) {
85                 $supported_images[] = 'gif';
86         }
87         if ($gd_info['JPG Support']) {
88                 $supported_images[] = 'jpg';
89         }
90         if ($gd_info['PNG Support']) {
91                 $supported_images[] = 'png';
92         }
93         if (!$supported_images) {
94                 $msg->printInfos('FEATURE_NOT_AVAILABLE');
95                 return false;
96         }
97
98         // check if this is a supported file type
99         $filename   = $stripslashes($file['name']);
100         $path_parts = pathinfo($filename);
101         $extension  = strtolower($path_parts['extension']);
102         $image_attributes = getimagesize($file['tmp_name']);
103
104         //check Extension
105         if ($extension == 'jpeg') {
106                 $extension = 'jpg';
107         }
108         if (!in_array($extension, $supported_images)) {
109                 $msg->addError(array('FILE_ILLEGAL', $extension));
110                 return false;
111         } else if ($image_attributes[2] > IMAGETYPE_PNG) {
112                 $msg->addError(array('FILE_ILLEGAL', $extension));
113                 return false;
114         }
115
116         // make sure under max file size
117         $allowed_usage = $_config['pa_max_memory_per_member'] * 1024 *1024;     //mb
118         if (memoryUsage($_SESSION['member_id']) > $allowed_usage){
119                 $msg->addError('PA_EXCEEDED_MAX_USAGE');
120                 return false;
121         }
122         
123         //check filename
124         $file['name'] = str_replace(array('\'', '"', ' ', '|', '\\', '/', '<', '>', ':'), '_' , $file['name'] );
125         $file['name'] = preg_replace("/[^A-Za-z0-9._\-]/", '', $file['name'] );
126         return $file;
127 }
128  
129
130 /**
131  * Return the total personal data usage (in bytes)
132  */
133 function memoryUsage($member_id){       
134         global $db; 
135         $member_id = intval($member_id);
136         if ($member_id < 1){
137                 return false;
138         }
139
140         $memory_usage = 0;
141         $sql = 'SELECT p.* FROM '.TABLE_PREFIX.'pa_photos p LEFT JOIN '.TABLE_PREFIX."pa_course_album ca ON p.album_id=ca.album_id WHERE member_id=$member_id AND ca.course_id IS NULL";
142         $result = mysql_query($sql, $db);
143         if ($result){
144                 while ($row=mysql_fetch_assoc($result)){
145                         $pa = new PhotoAlbum($row['album_id']);
146                         $album_info = $pa->getAlbumInfo();
147                         $photo_info = $pa->getPhotoInfo($row['id']);
148                         $album_file_path = getAlbumFilePath($album_info['id'], $album_info['created_date']);
149                         $photo_file_path = getPhotoFilePath($photo_info['id'], $photo_info['name'], $photo_info['created_date']);
150                         $file = AT_PA_CONTENT_DIR . $album_file_path . DIRECTORY_SEPARATOR . $photo_file_path;
151                         if (file_exists($file)){
152                                 $memory_usage += filesize($file);
153                         }
154                 }
155         }
156         return $memory_usage;
157 }
158 ?>