/features/cerberusweb.core/api/uri/files.php

https://github.com/sluther/cerb5 · PHP · 109 lines · 40 code · 14 blank · 55 comment · 11 complexity · 3ab3695fd8537aedfff5a6d8eb285f57 MD5 · raw file

  1. <?php
  2. /***********************************************************************
  3. | Cerberus Helpdesk(tm) developed by WebGroup Media, LLC.
  4. |-----------------------------------------------------------------------
  5. | All source code & content (c) Copyright 2011, WebGroup Media LLC
  6. | unless specifically noted otherwise.
  7. |
  8. | This source code is released under the Devblocks Public License.
  9. | The latest version of this license can be found here:
  10. | http://www.cerberusweb.com/license.php
  11. |
  12. | By using this software, you acknowledge having read this license
  13. | and agree to be bound thereby.
  14. | ______________________________________________________________________
  15. | http://www.cerberusweb.com http://www.webgroupmedia.com/
  16. ***********************************************************************/
  17. /*
  18. * IMPORTANT LICENSING NOTE from your friends on the Cerberus Helpdesk Team
  19. *
  20. * Sure, it would be so easy to just cheat and edit this file to use the
  21. * software without paying for it. But we trust you anyway. In fact, we're
  22. * writing this software for you!
  23. *
  24. * Quality software backed by a dedicated team takes money to develop. We
  25. * don't want to be out of the office bagging groceries when you call up
  26. * needing a helping hand. We'd rather spend our free time coding your
  27. * feature requests than mowing the neighbors' lawns for rent money.
  28. *
  29. * We've never believed in hiding our source code out of paranoia over not
  30. * getting paid. We want you to have the full source code and be able to
  31. * make the tweaks your organization requires to get more done -- despite
  32. * having less of everything than you might need (time, people, money,
  33. * energy). We shouldn't be your bottleneck.
  34. *
  35. * We've been building our expertise with this project since January 2002. We
  36. * promise spending a couple bucks [Euro, Yuan, Rupees, Galactic Credits] to
  37. * let us take over your shared e-mail headache is a worthwhile investment.
  38. * It will give you a sense of control over your inbox that you probably
  39. * haven't had since spammers found you in a game of 'E-mail Battleship'.
  40. * Miss. Miss. You sunk my inbox!
  41. *
  42. * A legitimate license entitles you to support from the developers,
  43. * and the warm fuzzy feeling of feeding a couple of obsessed developers
  44. * who want to help you get more done.
  45. *
  46. * - Jeff Standen, Darren Sugita, Dan Hildebrandt, Scott Luther,
  47. * and Jerry Kanoholani.
  48. * WEBGROUP MEDIA LLC. - Developers of Cerberus Helpdesk
  49. */
  50. class ChFilesController extends DevblocksControllerExtension {
  51. function isVisible() {
  52. // The current session must be a logged-in worker to use this page.
  53. if(null == ($worker = CerberusApplication::getActiveWorker()))
  54. return false;
  55. return true;
  56. }
  57. /*
  58. * Request Overload
  59. */
  60. function handleRequest(DevblocksHttpRequest $request) {
  61. $translate = DevblocksPlatform::getTranslationService();
  62. $stack = $request->path; // URLS like: /files/10000/plaintext.txt
  63. array_shift($stack); // files
  64. $file_guid = array_shift($stack); // GUID
  65. $file_name = array_shift($stack); // plaintext.txt
  66. // Security
  67. if(null == ($active_worker = CerberusApplication::getActiveWorker()))
  68. die($translate->_('common.access_denied'));
  69. if(empty($file_guid) || empty($file_name))
  70. die($translate->_('files.not_found'));
  71. $link = DAO_AttachmentLink::getByGUID($file_guid);
  72. if(null == ($context = $link->getContext()))
  73. die($translate->_('common.access_denied'));
  74. // Security
  75. if(!$context->authorize($link->context_id, $active_worker))
  76. die($translate->_('common.access_denied'));
  77. $file = $link->getAttachment();
  78. if(false === ($fp = DevblocksPlatform::getTempFile()))
  79. die("Could not open a temporary file.");
  80. if(false === $file->getFileContents($fp))
  81. die("Error reading resource.");
  82. $file_stats = fstat($fp);
  83. // Set headers
  84. header("Expires: Mon, 26 Nov 1962 00:00:00 GMT\n");
  85. header("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT\n");
  86. header("Cache-control: private\n");
  87. header("Pragma: no-cache\n");
  88. header("Content-Type: " . $file->mime_type . "\n");
  89. header("Content-Transfer-Encoding: binary\n");
  90. header("Content-Length: " . $file_stats['size'] . "\n");
  91. fpassthru($fp);
  92. fclose($fp);
  93. exit;
  94. }
  95. };