PluploadController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. class PluploadController extends Zend_Controller_Action
  3. {
  4. public function init()
  5. {
  6. $ajaxContext = $this->_helper->getHelper('AjaxContext');
  7. $ajaxContext->addActionContext('upload', 'json')
  8. ->addActionContext('copyfile', 'json')
  9. ->initContext();
  10. }
  11. public function indexAction()
  12. {
  13. $CC_CONFIG = Config::getConfig();
  14. $baseUrl = Application_Common_OsPath::getBaseDir();
  15. $locale = Application_Model_Preference::GetLocale();
  16. $this->view->headScript()->appendFile($baseUrl.'js/plupload/plupload.full.min.js?'.$CC_CONFIG['airtime_version'],'text/javascript');
  17. $this->view->headScript()->appendFile($baseUrl.'js/plupload/jquery.plupload.queue.min.js?'.$CC_CONFIG['airtime_version'],'text/javascript');
  18. $this->view->headScript()->appendFile($baseUrl.'js/airtime/library/plupload.js?'.$CC_CONFIG['airtime_version'],'text/javascript');
  19. $this->view->headScript()->appendFile($baseUrl.'js/plupload/i18n/'.$locale.'.js?'.$CC_CONFIG['airtime_version'],'text/javascript');
  20. $this->view->headLink()->appendStylesheet($baseUrl.'css/plupload.queue.css?'.$CC_CONFIG['airtime_version']);
  21. $csrf_namespace = new Zend_Session_Namespace('csrf_namespace');
  22. $csrf_namespace->setExpirationSeconds(5*60*60);
  23. $csrf_namespace->authtoken = sha1(uniqid(rand(),1));
  24. $csrf_element = new Zend_Form_Element_Hidden('csrf');
  25. $csrf_element->setValue($csrf_namespace->authtoken)->setRequired('true')->removeDecorator('HtmlTag')->removeDecorator('Label');
  26. $csrf_form = new Zend_Form();
  27. $csrf_form->addElement($csrf_element);
  28. $this->view->form = $csrf_form;
  29. }
  30. public function uploadAction()
  31. {
  32. $current_namespace = new Zend_Session_Namespace('csrf_namespace');
  33. $observed_csrf_token = $this->_getParam('csrf_token');
  34. $expected_csrf_token = $current_namespace->authtoken;
  35. if($observed_csrf_token == $expected_csrf_token){
  36. $upload_dir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  37. $tempFilePath = Application_Model_StoredFile::uploadFile($upload_dir);
  38. $tempFileName = basename($tempFilePath);
  39. $this->_helper->json->sendJson(array("jsonrpc" => "2.0", "tempfilepath" => $tempFileName));
  40. }else{
  41. $this->_helper->json->sendJson(array("jsonrpc" => "2.0", "valid" => false, "error" => "CSRF token did not match."));
  42. }
  43. }
  44. public function copyfileAction()
  45. {
  46. $upload_dir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  47. $filename = $this->_getParam('name');
  48. $tempname = $this->_getParam('tempname');
  49. $result = Application_Model_StoredFile::copyFileToStor($upload_dir,
  50. $filename, $tempname);
  51. if (!is_null($result))
  52. $this->_helper->json->sendJson(array("jsonrpc" => "2.0", "error" => $result));
  53. $this->_helper->json->sendJson(array("jsonrpc" => "2.0"));
  54. }
  55. }