2 /***********************************************************************/
4 /***********************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
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 * Generate album path padding by using album_id + album_created_date
18 function getPhotoFilePath($id, $filename, $timestamp){
19 $padding = hash('sha1', $id.$timestamp);
20 $path_parts = pathinfo($filename);
21 //return the hash if filename is empty.
22 //this is used for validation purposes.
27 $extension = strtolower($path_parts['extension']);
28 //Note: the padding might not be unique, but the path is ALWAYS unique
29 // because the id is unique.
30 return ($id.'_'.substr($padding, -5).'.'.$extension);
34 * Generate album path padding by using album_id + album_created_date
36 function getAlbumFilePath($id, $timestamp){
37 $padding = hash('sha1', $id.$timestamp);
38 //Note: the padding might not be unique, but the path is ALWAYS unique
39 // because the id is unique.
40 return ($id.'_'.substr($padding, -5));
44 * Check if the photo is supported, including extension check, file size check
45 * and library support checks.
46 * @param string location of the file.
47 * @return $_FILE[] on successful, null on failure.
49 function checkPhoto($file){
51 global $msg, $_config;
52 $msg = new AjaxMessage();
54 // check if GD is installed
55 if (!extension_loaded('gd')) {
56 $msg->printInfos('FEATURE_NOT_AVAILABLE');
60 // check if folder exists, if not, create it
61 if (!is_dir(AT_PA_CONTENT_DIR)) {
62 mkdir(AT_PA_CONTENT_DIR);
68 $supported_images = array();
69 if ($gd_info['GIF Create Support']) {
70 $supported_images[] = 'gif';
72 if ($gd_info['JPG Support'] || $gd_info['JPEG Support']) {
73 $supported_images[] = 'jpg';
75 if ($gd_info['PNG Support']) {
76 $supported_images[] = 'png';
78 if (!$supported_images) {
79 $msg->printInfos('FEATURE_NOT_AVAILABLE');
83 // check if this is a supported file type
84 $filename = $stripslashes($file['name']);
85 $path_parts = pathinfo($filename);
86 $extension = strtolower($path_parts['extension']);
87 $image_attributes = getimagesize($file['tmp_name']);
90 if ($extension == 'jpeg') {
93 if (!in_array($extension, $supported_images)) {
94 $msg->addError(array('FILE_ILLEGAL', $extension));
96 } else if ($image_attributes[2] > IMAGETYPE_PNG) {
97 $msg->addError(array('FILE_ILLEGAL', $extension));
101 // make sure under max file size
102 $allowed_usage = $_config['pa_max_memory_per_member'] * 1024 *1024; //mb
103 if (memoryUsage($_SESSION['member_id']) > $allowed_usage){
104 $msg->addError('PA_EXCEEDED_MAX_USAGE');
109 $file['name'] = str_replace(array('\'', '"', ' ', '|', '\\', '/', '<', '>', ':'), '_' , $file['name'] );
110 $file['name'] = preg_replace("/[^A-Za-z0-9._\-]/", '', $file['name'] );
116 * Return the total personal data usage (in bytes)
118 function memoryUsage($member_id){
120 $member_id = intval($member_id);
126 $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";
127 $result = mysql_query($sql, $db);
129 while ($row=mysql_fetch_assoc($result)){
130 $pa = new PhotoAlbum($row['album_id']);
131 $album_info = $pa->getAlbumInfo();
132 $photo_info = $pa->getPhotoInfo($row['id']);
133 $album_file_path = getAlbumFilePath($album_info['id'], $album_info['created_date']);
134 $photo_file_path = getPhotoFilePath($photo_info['id'], $photo_info['name'], $photo_info['created_date']);
135 $file = AT_PA_CONTENT_DIR . $album_file_path . DIRECTORY_SEPARATOR . $photo_file_path;
136 if (file_exists($file)){
137 $memory_usage += filesize($file);
141 return $memory_usage;