PageRenderTime 23ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mods/_standard/photos/include/lib.inc.php

https://github.com/harriswong/ATutor
PHP | 143 lines | 94 code | 12 blank | 37 comment | 14 complexity | 2fe0bf10d028c3569705c590475c1a3e MD5 | raw file
  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. * Generate album path padding by using album_id + album_created_date
  16. */
  17. function getPhotoFilePath($id, $filename, $timestamp){
  18. $padding = hash('sha1', $id.$timestamp);
  19. $path_parts = pathinfo($filename);
  20. //return the hash if filename is empty.
  21. //this is used for validation purposes.
  22. if($filename==''){
  23. return $padding;
  24. }
  25. $extension = strtolower($path_parts['extension']);
  26. //Note: the padding might not be unique, but the path is ALWAYS unique
  27. // because the id is unique.
  28. return ($id.'_'.substr($padding, -5).'.'.$extension);
  29. }
  30. /**
  31. * Generate album path padding by using album_id + album_created_date
  32. */
  33. function getAlbumFilePath($id, $timestamp){
  34. $padding = hash('sha1', $id.$timestamp);
  35. //Note: the padding might not be unique, but the path is ALWAYS unique
  36. // because the id is unique.
  37. return ($id.'_'.substr($padding, -5));
  38. }
  39. /**
  40. * Check if the photo is supported, including extension check, file size check
  41. * and library support checks.
  42. * @param string location of the file.
  43. * @return $_FILE[] on successful, null on failure.
  44. */
  45. function checkPhoto($file){
  46. global $stripslashes;
  47. global $msg, $_config;
  48. $msg = new AjaxMessage();
  49. // check if GD is installed
  50. if (!extension_loaded('gd')) {
  51. $msg->printInfos('FEATURE_NOT_AVAILABLE');
  52. return false;
  53. }
  54. // check if folder exists, if not, create it
  55. if (!is_dir(AT_PA_CONTENT_DIR)) {
  56. mkdir(AT_PA_CONTENT_DIR);
  57. }
  58. //check GD support
  59. $gd_info = gd_info();
  60. $supported_images = array();
  61. if ($gd_info['GIF Create Support']) {
  62. $supported_images[] = 'gif';
  63. }
  64. if ($gd_info['JPG Support'] || $gd_info['JPEG Support']) {
  65. $supported_images[] = 'jpg';
  66. }
  67. if ($gd_info['PNG Support']) {
  68. $supported_images[] = 'png';
  69. }
  70. if (!$supported_images) {
  71. $msg->printInfos('FEATURE_NOT_AVAILABLE');
  72. return false;
  73. }
  74. // check if this is a supported file type
  75. $filename = $stripslashes($file['name']);
  76. $path_parts = pathinfo($filename);
  77. $extension = strtolower($path_parts['extension']);
  78. $image_attributes = getimagesize($file['tmp_name']);
  79. //check Extension
  80. if ($extension == 'jpeg') {
  81. $extension = 'jpg';
  82. }
  83. if (!in_array($extension, $supported_images)) {
  84. $msg->addError(array('FILE_ILLEGAL', $extension));
  85. return false;
  86. } else if ($image_attributes[2] > IMAGETYPE_PNG) {
  87. $msg->addError(array('FILE_ILLEGAL', $extension));
  88. return false;
  89. }
  90. // make sure under max file size
  91. $allowed_usage = $_config['pa_max_memory_per_member'] * 1024 *1024; //mb
  92. if (memoryUsage($_SESSION['member_id']) > $allowed_usage){
  93. $msg->addError('PA_EXCEEDED_MAX_USAGE');
  94. return false;
  95. }
  96. //check filename
  97. $file['name'] = str_replace(array('\'', '"', ' ', '|', '\\', '/', '<', '>', ':'), '_' , $file['name'] );
  98. $file['name'] = preg_replace("/[^A-Za-z0-9._\-]/", '', $file['name'] );
  99. return $file;
  100. }
  101. /**
  102. * Return the total personal data usage (in bytes)
  103. */
  104. function memoryUsage($member_id){
  105. global $db;
  106. $member_id = intval($member_id);
  107. if ($member_id < 1){
  108. return false;
  109. }
  110. $memory_usage = 0;
  111. $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";
  112. $result = mysql_query($sql, $db);
  113. if ($result){
  114. while ($row=mysql_fetch_assoc($result)){
  115. $pa = new PhotoAlbum($row['album_id']);
  116. $album_info = $pa->getAlbumInfo();
  117. $photo_info = $pa->getPhotoInfo($row['id']);
  118. $album_file_path = getAlbumFilePath($album_info['id'], $album_info['created_date']);
  119. $photo_file_path = getPhotoFilePath($photo_info['id'], $photo_info['name'], $photo_info['created_date']);
  120. $file = AT_PA_CONTENT_DIR . $album_file_path . DIRECTORY_SEPARATOR . $photo_file_path;
  121. if (file_exists($file)){
  122. $memory_usage += filesize($file);
  123. }
  124. }
  125. }
  126. return $memory_usage;
  127. }
  128. ?>