2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2009 */
6 /* Adaptive Technology Resource Centre / University of Toronto */
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 /****************************************************************/
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.
21 function htmlentities_utf82($str, $use_nl2br=true){
22 $return = htmlentities($str, ENT_QUOTES, 'UTF-8');
24 return nl2br($return);
31 * Generate album path padding by using album_id + album_created_date
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.
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);
49 * Generate album path padding by using album_id + album_created_date
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));
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.
64 function checkPhoto($file){
66 global $msg, $_config;
67 $msg = new AjaxMessage();
69 // check if GD is installed
70 if (!extension_loaded('gd')) {
71 $msg->printInfos('FEATURE_NOT_AVAILABLE');
75 // check if folder exists, if not, create it
76 if (!is_dir(AT_PA_CONTENT_DIR)) {
77 mkdir(AT_PA_CONTENT_DIR);
83 $supported_images = array();
84 if ($gd_info['GIF Create Support']) {
85 $supported_images[] = 'gif';
87 if ($gd_info['JPG Support']) {
88 $supported_images[] = 'jpg';
90 if ($gd_info['PNG Support']) {
91 $supported_images[] = 'png';
93 if (!$supported_images) {
94 $msg->printInfos('FEATURE_NOT_AVAILABLE');
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']);
105 if ($extension == 'jpeg') {
108 if (!in_array($extension, $supported_images)) {
109 $msg->addError(array('FILE_ILLEGAL', $extension));
111 } else if ($image_attributes[2] > IMAGETYPE_PNG) {
112 $msg->addError(array('FILE_ILLEGAL', $extension));
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');
124 $file['name'] = str_replace(array('\'', '"', ' ', '|', '\\', '/', '<', '>', ':'), '_' , $file['name'] );
125 $file['name'] = preg_replace("/[^A-Za-z0-9._\-]/", '', $file['name'] );
131 * Return the total personal data usage (in bytes)
133 function memoryUsage($member_id){
135 $member_id = intval($member_id);
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);
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);
156 return $memory_usage;