Advertisement
Guest User

UploadController

a guest
Jun 4th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Uploadpin;
  4.  
  5. class UploadController extends \Core\Base\Action {
  6.  
  7. public function init() {
  8. $this->noLayout(true);
  9. $this->_ = new \Translate\Locale('Front\\'.__NAMESPACE__, $this->getModule('Language')->getLanguageId());
  10. }
  11.  
  12. public function indexAction() {
  13. $return = array();
  14. $user = \User\User::getUserData();
  15. if($user->id) {
  16. if(!file_exists(BASE_PATH . '/cache/tmp/') || !is_dir(BASE_PATH . '/cache/tmp/')) {
  17. @mkdir(BASE_PATH . '/cache/tmp/', 0777, true);
  18. @chmod(BASE_PATH . '/cache/tmp/', 0777);
  19. }
  20.  
  21. //delete old files
  22. $files_delete = glob(BASE_PATH . '/cache/tmp/*.*');
  23. if($files_delete) {
  24. for($i=0; $i<min(count($files_delete),250); $i++) {
  25. if((filemtime($files_delete[$i]) + 3600) < time()) {
  26. @unlink($files_delete[$i]);
  27. }
  28. }
  29. }
  30.  
  31. $valid_extensions = $this->getAllowedTypes();
  32. $upload = new \Core\File\Upload('file');
  33. $ext = $upload->getExtension();
  34. $upload->newFileName = md5(mt_rand(0, 99999999) . time()) . '.' . $ext;
  35. $result = $upload->handleUpload(BASE_PATH . '/cache/tmp/', $valid_extensions);
  36. if($result) {
  37. if( is_array( $image_info = @getimagesize(BASE_PATH . '/cache/tmp/' . $upload->newFileName) ) ) {
  38. $request = $this->getRequest();
  39. $data = array();
  40. $data['media'] = 'cache/tmp/' . $upload->newFileName;
  41. $pinTable = new \Pin\Pin();
  42. $this->x_form_cmd = $pinTable->getXFormCmd();
  43.  
  44. $data['isXmlHttpRequest'] = $request->isXmlHttpRequest();
  45.  
  46. //get user boards
  47. $boardTable = new \Board\Board(array(
  48. \Board\Board::ORDER => 'sort_order ASC'
  49. ));
  50. $data['boards'] = $boardTable->getWithShared($user->id);
  51.  
  52. $return = array('success' => true, 'html' => $this->render('index', $data, null, true));
  53. } else {
  54. $return = array('success' => false, 'msg' => $this->_('Unable to get image information'));
  55. }
  56. } else {
  57. $return = array('success' => false, 'msg' => $this->_($upload->getErrorMsg()));
  58. }
  59. } else {
  60. $return = array('success' => false, 'msg' => $this->_('You must be logged in to upload files.'));
  61. }
  62. $this->responseJsonCallback($return);
  63. }
  64.  
  65. private function getAllowedTypes() {
  66. $allowedUploadMimes = \Core\Registry::get('allowedmimeimages');
  67. if(is_array($allowedUploadMimes)) {
  68. return array_keys($allowedUploadMimes);
  69. }
  70. return array();
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement