remove old readme
[atutor.git] / docs / mods / _standard / photos / include / lib.inc.php
1 <?php
2 /***********************************************************************/
3 /* ATutor                                                                                                                          */
4 /***********************************************************************/
5 /* Copyright (c) 2002-2010                                             */
6 /* Inclusive Design Institute                                          */
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  * Generate album path padding by using album_id + album_created_date
17  */
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.
23         if($filename==''){
24                 return $padding;
25         }
26
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);
31 }
32
33 /** 
34  * Generate album path padding by using album_id + album_created_date
35  */
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));
41 }
42
43 /** 
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.
48  */
49 function checkPhoto($file){
50         global $stripslashes;
51         global $msg, $_config;
52         $msg = new AjaxMessage();
53
54         // check if GD is installed
55         if (!extension_loaded('gd')) {
56                 $msg->printInfos('FEATURE_NOT_AVAILABLE');
57                 return false;
58         }
59
60         // check if folder exists, if not, create it
61         if (!is_dir(AT_PA_CONTENT_DIR)) {
62                 mkdir(AT_PA_CONTENT_DIR);
63         }
64
65         //check GD support 
66         $gd_info = gd_info();
67
68         $supported_images = array();
69         if ($gd_info['GIF Create Support']) {
70                 $supported_images[] = 'gif';
71         }
72         if ($gd_info['JPG Support'] || $gd_info['JPEG Support']) {
73                 $supported_images[] = 'jpg';
74         }
75         if ($gd_info['PNG Support']) {
76                 $supported_images[] = 'png';
77         }
78         if (!$supported_images) {
79                 $msg->printInfos('FEATURE_NOT_AVAILABLE');
80                 return false;
81         }
82
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']);
88
89         //check Extension
90         if ($extension == 'jpeg') {
91                 $extension = 'jpg';
92         }
93         if (!in_array($extension, $supported_images)) {
94                 $msg->addError(array('FILE_ILLEGAL', $extension));
95                 return false;
96         } else if ($image_attributes[2] > IMAGETYPE_PNG) {
97                 $msg->addError(array('FILE_ILLEGAL', $extension));
98                 return false;
99         }
100
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');
105                 return false;
106         }
107         
108         //check filename
109         $file['name'] = str_replace(array('\'', '"', ' ', '|', '\\', '/', '<', '>', ':'), '_' , $file['name'] );
110         $file['name'] = preg_replace("/[^A-Za-z0-9._\-]/", '', $file['name'] );
111         return $file;
112 }
113  
114
115 /**
116  * Return the total personal data usage (in bytes)
117  */
118 function memoryUsage($member_id){       
119         global $db; 
120         $member_id = intval($member_id);
121         if ($member_id < 1){
122                 return false;
123         }
124
125         $memory_usage = 0;
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);
128         if ($result){
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);
138                         }
139                 }
140         }
141         return $memory_usage;
142 }
143 ?>