PageRenderTime 37ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/attachments.inc.php

https://gitlab.com/cyberfighter/Mods-for-HESK
PHP | 165 lines | 87 code | 32 blank | 46 comment | 14 complexity | acde20240cca054caf29e959123459ba MD5 | raw file
  1. <?php
  2. /*******************************************************************************
  3. * Title: Help Desk Software HESK
  4. * Version: 2.6.7 from 18th April 2016
  5. * Author: Klemen Stirn
  6. * Website: http://www.hesk.com
  7. ********************************************************************************
  8. * COPYRIGHT AND TRADEMARK NOTICE
  9. * Copyright 2005-2015 Klemen Stirn. All Rights Reserved.
  10. * HESK is a registered trademark of Klemen Stirn.
  11. * The HESK may be used and modified free of charge by anyone
  12. * AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
  13. * By using this code you agree to indemnify Klemen Stirn from any
  14. * liability that might arise from it's use.
  15. * Selling the code for this program, in part or full, without prior
  16. * written consent is expressly forbidden.
  17. * Using this code, in part or full, to create derivate work,
  18. * new scripts or products is expressly forbidden. Obtain permission
  19. * before redistributing this software over the Internet or in
  20. * any other medium. In all cases copyright and header must remain intact.
  21. * This Copyright is in full effect in any country that has International
  22. * Trade Agreements with the United States of America or
  23. * with the European Union.
  24. * Removing any of the copyright notices without purchasing a license
  25. * is expressly forbidden. To remove HESK copyright notice you must purchase
  26. * a license for this script. For more information on how to obtain
  27. * a license please visit the page below:
  28. * https://www.hesk.com/buy.php
  29. *******************************************************************************/
  30. /* Check if this is a valid include */
  31. if (!defined('IN_SCRIPT')) {
  32. die('Invalid attempt');
  33. }
  34. /***************************
  35. * Function hesk_uploadFiles()
  36. ***************************/
  37. function hesk_uploadFile($i, $isTicket = true)
  38. {
  39. global $hesk_settings, $hesklang, $trackingID, $hesk_error_buffer, $modsForHesk_settings;
  40. /* Return if name is empty */
  41. $name = $i == -1
  42. ? $_FILES['attachment']['name']
  43. : $_FILES['attachment']['name'][$i];
  44. if (empty($name)) {
  45. return '';
  46. }
  47. /* Parse the name */
  48. $file_realname = hesk_cleanFileName($name);
  49. /* Check file extension */
  50. $ext = strtolower(strrchr($file_realname, "."));
  51. if (!in_array($ext, $hesk_settings['attachments']['allowed_types'])) {
  52. return hesk_fileError(sprintf($hesklang['type_not_allowed'], $ext, $file_realname));
  53. }
  54. /* Check file size */
  55. $size = $i == -1
  56. ? $_FILES['attachment']['size']
  57. : $_FILES['attachment']['size'][$i];
  58. if ($size > $hesk_settings['attachments']['max_size']) {
  59. return hesk_fileError(sprintf($hesklang['file_too_large'], $file_realname));
  60. } else {
  61. $file_size = $size;
  62. }
  63. /* Generate a random file name */
  64. $useChars = 'AEUYBDGHJLMNPQRSTVWXZ123456789';
  65. $tmp = uniqid();
  66. for ($j = 1; $j < 10; $j++) {
  67. $tmp .= $useChars{mt_rand(0, 29)};
  68. }
  69. $file_name = substr(md5($tmp . $file_realname), 0, 200) . $ext;
  70. // Does the temporary file exist? If not, probably server-side configuration limits have been reached
  71. // Uncomment this for debugging purposes
  72. /*
  73. if ( ! file_exists($_FILES['attachment']['tmp_name'][$i]) )
  74. {
  75. return hesk_fileError($hesklang['fnuscphp']);
  76. }
  77. */
  78. /* If upload was successful let's create the headers */
  79. $directory = $hesk_settings['attach_dir'];
  80. if (!$isTicket) {
  81. $directory = $modsForHesk_settings['kb_attach_dir'];
  82. }
  83. $file_to_move = $i == -1
  84. ? $_FILES['attachment']['tmp_name']
  85. : $_FILES['attachment']['tmp_name'][$i];
  86. if (!move_uploaded_file($file_to_move, dirname(dirname(__FILE__)) . '/' . $directory . '/' . $file_name)) {
  87. return hesk_fileError($hesklang['cannot_move_tmp']);
  88. }
  89. $info = array(
  90. 'saved_name' => $file_name,
  91. 'real_name' => $file_realname,
  92. 'size' => $file_size
  93. );
  94. return $info;
  95. } // End hesk_uploadFile()
  96. function hesk_fileError($error)
  97. {
  98. global $hesk_settings, $hesklang, $trackingID;
  99. global $hesk_error_buffer;
  100. $hesk_error_buffer['attachments'] = $error;
  101. return false;
  102. } // End hesk_fileError()
  103. function hesk_removeAttachments($attachments, $isTicket)
  104. {
  105. global $hesk_settings, $hesklang, $modsForHesk_settings;
  106. $directory = $hesk_settings['attach_dir'];
  107. if (!$isTicket) {
  108. $directory = $modsForHesk_settings['kb_attach_dir'];
  109. }
  110. $hesk_settings['server_path'] = dirname(dirname(__FILE__)) . '/' . $directory . '/';
  111. foreach ($attachments as $myatt) {
  112. hesk_unlink($hesk_settings['server_path'] . $myatt['saved_name']);
  113. }
  114. return true;
  115. } // End hesk_removeAttachments()
  116. function mfh_getTemporaryAttachment($id) {
  117. global $hesk_settings;
  118. $rs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "temp_attachment` WHERE `id` = " . intval($id));
  119. if (hesk_dbNumRows($rs) == 0) {
  120. return NULL;
  121. }
  122. $row = hesk_dbFetchAssoc($rs);
  123. $info = array(
  124. 'saved_name' => $row['saved_name'],
  125. 'real_name' => $row['file_name'],
  126. 'size' => $row['size']
  127. );
  128. return $info;
  129. }
  130. function mfh_deleteTemporaryAttachment($id) {
  131. global $hesk_settings;
  132. hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "temp_attachment` WHERE `id` = ".intval($id));
  133. }