PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/functions/func_attachments.php

https://github.com/lewellyn/TrellisDesk
PHP | 260 lines | 165 code | 65 blank | 30 comment | 35 complexity | c3d6bfd87f1758b3170a70c8b8388f11 MD5 | raw file
  1. <?php
  2. /**
  3. * Trellis Desk
  4. *
  5. * @copyright Copyright (C) 2009-2012 ACCORD5. All rights reserved.
  6. * @license GNU General Public License version 3 or later; see LICENSE.txt
  7. */
  8. class td_func_attachments {
  9. #=======================================
  10. # @ Get Attachments
  11. #=======================================
  12. public function get($input)
  13. {
  14. $return = array();
  15. $this->trellis->db->construct( array(
  16. 'select' => $input['select'],
  17. 'from' => 'attachments',
  18. 'where' => $input['where'],
  19. 'order' => $input['order'],
  20. 'limit' => $input['limit'],
  21. ) );
  22. $this->trellis->db->execute();
  23. if ( ! $this->trellis->db->get_num_rows() ) return false;
  24. while ( $a = $this->trellis->db->fetch_row() )
  25. {
  26. if ( $a['id'] )
  27. {
  28. $return[ $a['id'] ] = $a;
  29. }
  30. else
  31. {
  32. $return[] = $a;
  33. }
  34. }
  35. return $return;
  36. }
  37. #=======================================
  38. # @ Get Single Attachment
  39. #=======================================
  40. public function get_single($select, $where='')
  41. {
  42. $this->trellis->db->construct( array(
  43. 'select' => $select,
  44. 'from' => 'attachments',
  45. 'where' => $where,
  46. 'limit' => array( 0, 1 ),
  47. ) );
  48. $this->trellis->db->execute();
  49. if ( ! $this->trellis->db->get_num_rows() ) return false;
  50. return $this->trellis->db->fetch_row();
  51. }
  52. #=======================================
  53. # @ Get Single Attachment By ID
  54. #=======================================
  55. public function get_single_by_id($select, $id)
  56. {
  57. return $this->get_single( $select, array( 'id', '=', intval( $id ) ) );
  58. }
  59. #=======================================
  60. # @ Upload Attachment
  61. #=======================================
  62. public function upload(&$file, $data=array(), $response='')
  63. {
  64. if ( ! $file )
  65. {
  66. if ( $response == 'ajax' ) { $this->trellis->skin->ajax_output( json_encode( array( 'error' => true, 'errormsg' => 'no data received' ) ) ); } else { return false; };
  67. }
  68. if ( $this->trellis->user['g_upload_max_size'] && ( $file['size'] > $this->trellis->user['g_upload_max_size'] ) )
  69. {
  70. if ( $response == 'ajax' ) { $this->trellis->skin->ajax_output( json_encode( array( 'error' => true, 'errormsg' => $this->trellis->lang['error_upload_size'] ) ) ); } else { return false; };
  71. }
  72. $allowed_exts = array_map( create_function( '$a', 'return \'.\'. trim( $a );' ), explode( ',', $this->trellis->user['g_upload_exts'] ) );
  73. $file_ext = strtolower(strrchr( $file['name'], "." ));
  74. if ( ! in_array( $file_ext, $allowed_exts ) )
  75. {
  76. if ( $response == 'ajax' ) { $this->trellis->skin->ajax_output( json_encode( array( 'error' => true, 'errormsg' => $this->trellis->lang['error_upload_filetype'] ) ) ); } else { return false; };
  77. }
  78. $file_name = md5( $file['name'] . microtime() );
  79. $upload_location = $this->trellis->cache->data['settings']['general']['upload_dir'] . $file_name . $file_ext;
  80. if ( ! is_writeable( $this->trellis->cache->data['settings']['general']['upload_dir'] ) ) $this->trellis->skin->ajax_output( json_encode( array( 'error' => true, 'errormsg' => 'directory' ) ) );
  81. if ( ! @move_uploaded_file( $file['tmp_name'], $upload_location ) ) $this->trellis->skin->ajax_output( json_encode( array( 'error' => true, 'errormsg' => $this->trellis->lang['error_upload_move'] ) ) );
  82. # TODO: only run chmod if web user is 'nobody' (just have a setting)
  83. //@chmod( $upload_location, 0666 );
  84. $data['uid'] = $this->trellis->user['id'];
  85. $data['real_name'] = $file_name;
  86. $data['original_name'] = $this->trellis->sanitize_data( $file['name'] );
  87. $data['extension'] = $this->trellis->sanitize_data( $file_ext );
  88. if ( function_exists( 'finfo_file' ) )
  89. {
  90. $finfo = finfo_open( FILEINFO_MIME_TYPE );
  91. $data['mime'] = finfo_file( $finfo, $upload_location );
  92. finfo_close( $finfo );
  93. }
  94. $data['size'] = $file['size'];
  95. $data['date'] = time();
  96. $data['ipadd'] = $this->trellis->input['ip_address'];
  97. $fields = array(
  98. 'content_type' => 'string',
  99. 'content_id' => 'int',
  100. 'uid' => 'int',
  101. 'real_name' => 'string',
  102. 'original_name' => 'string',
  103. 'extension' => 'string',
  104. 'mime' => 'string',
  105. 'size' => 'int',
  106. 'date' => 'int',
  107. 'ipadd' => 'string',
  108. );
  109. $this->trellis->db->construct( array(
  110. 'insert' => 'attachments',
  111. 'set' => $this->trellis->process_data( $fields, $data ),
  112. ) );
  113. $this->trellis->db->execute();
  114. if ( $response == 'ajax' ) return array( 'id' => $this->trellis->db->get_insert_id(), 'name' => $data['original_name'] );
  115. return $this->trellis->db->get_insert_id();
  116. }
  117. #=======================================
  118. # @ Assign Attachments
  119. #=======================================
  120. public function assign($ids, $cid)
  121. {
  122. if ( ! $cid = intval( $cid ) ) return false;
  123. if ( ! is_array( $ids ) && intval( $ids ) )
  124. {
  125. $ids = array( $ids );
  126. }
  127. $this->trellis->db->construct( array(
  128. 'update' => 'attachments',
  129. 'set' => array( 'content_id' => $cid ),
  130. 'where' => array( 'id', 'in', $ids ),
  131. ) );
  132. $this->trellis->db->execute();
  133. return $this->trellis->db->get_affected_rows();
  134. }
  135. #=======================================
  136. # @ Delete Attachments
  137. #=======================================
  138. public function delete($ids)
  139. {
  140. if ( is_array( $ids ) )
  141. {
  142. if ( ! $files = $this->get( array( 'select' => array( 'real_name', 'extension' ), 'where' => array( 'id', 'in', $ids ) ) ) ) return false;
  143. foreach ( $files as &$f )
  144. {
  145. @unlink( $this->trellis->cache->data['settings']['general']['upload_dir'] . $f['real_name'] . $f['extension'] );
  146. }
  147. $this->trellis->db->construct( array(
  148. 'delete' => 'attachments',
  149. 'where' => array( 'id', 'in', $ids ),
  150. ) );
  151. $this->trellis->db->execute();
  152. }
  153. else
  154. {
  155. if ( ! $ids = intval( $ids ) ) return false;
  156. if ( ! $f = $this->get_single_by_id( array( 'real_name' , 'extension'), $ids ) ) return false;
  157. if ( ! @unlink( $this->trellis->cache->data['settings']['general']['upload_dir'] . $f['real_name'] . $f['extension'] ) ) return false;
  158. $this->trellis->db->construct( array(
  159. 'delete' => 'attachments',
  160. 'where' => array( 'id', '=', $ids ),
  161. 'limit' => array( 1 ),
  162. ) );
  163. $this->trellis->db->execute();
  164. }
  165. return $this->trellis->db->get_affected_rows();
  166. }
  167. #=======================================
  168. # @ Download Attachment
  169. #=======================================
  170. public function download($id)
  171. {
  172. if ( ! $id = intval( $id ) ) return false;
  173. if ( ! $f = $this->get_single_by_id( array( 'real_name', 'original_name', 'mime', 'size', 'extension' ), $id ) ) return false;
  174. if ( ! is_readable( ( $file = $this->trellis->cache->data['settings']['general']['upload_dir'] . $f['real_name'] . $f['extension'] ) ) ) return false;
  175. if ( $f['mime'] )
  176. {
  177. header( "Content-type: ". $f['mime'] );
  178. }
  179. else
  180. {
  181. #header( "Content-type: application/force-download" );
  182. }
  183. $show_types = array( 'image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/tiff' );
  184. if ( ! in_array( $f['mime'], $show_types ) )
  185. {
  186. header( "Content-Disposition: attachment; filename=". $f['original_name'] );
  187. header( "Content-length: ". filesize( $file_path ) );
  188. }
  189. header( "Expires: ". gmdate( 'D, d M Y H:i:s', time() ) ." GMT" );
  190. readfile( $file );
  191. $this->trellis->shut_down();
  192. exit();
  193. }
  194. }
  195. ?>