/components/com_easyblog/controllers/media.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 117 lines · 76 code · 20 blank · 21 comment · 8 complexity · ef68ba0798012ad618b50130e71e7bb4 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: file.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla
  5. * @subpackage Content
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. // Check to ensure this file is included in Joomla!
  15. defined('_JEXEC') or die( 'Restricted access' );
  16. jimport('joomla.filesystem.file');
  17. jimport('joomla.filesystem.folder');
  18. require_once( EBLOG_ROOT . DIRECTORY_SEPARATOR . 'controller.php' );
  19. require_once( EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'mediamanager.php' );
  20. require_once( EBLOG_HELPERS . DIRECTORY_SEPARATOR . 'image.php' );
  21. require_once( EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'easysimpleimage.php' );
  22. class EasyBlogControllerMedia extends EasyBlogParentController
  23. {
  24. public function upload()
  25. {
  26. $app = JFactory::getApplication();
  27. $my = JFactory::getUser();
  28. $cfg = EasyBlogHelper::getConfig();
  29. $acl = EasyBlogACLHelper::getRuleSet();
  30. // @rule: Only allowed users are allowed to upload images.
  31. if( $my->id == 0 || empty( $acl->rules->upload_image ) )
  32. {
  33. $sessionid = JRequest::getVar('sessionid');
  34. if ($sessionid)
  35. {
  36. $session = EasyBlogHelper::getTable('Session', 'JTable');
  37. $session->load($sessionid);
  38. if (!$session->userid)
  39. {
  40. $this->output( $this->getMessageObj( EBLOG_MEDIA_SECURITY_ERROR , JText::_( 'COM_EASYBLOG_NOT_ALLOWED' ) ) );
  41. }
  42. $my = JFactory::getUser($session->userid);
  43. }
  44. else
  45. {
  46. $this->output( $this->getMessageObj( EBLOG_MEDIA_SECURITY_ERROR , JText::_( 'COM_EASYBLOG_NOT_ALLOWED' ) ) );
  47. }
  48. }
  49. // Let's get the path for the current request.
  50. $file = JRequest::getVar( 'file' , '' , 'FILES' , 'array' );
  51. $place = JRequest::getVar( 'place' );
  52. // The user might be from a subfolder?
  53. $source = urldecode(JRequest::getVar( 'path' , '/' ));
  54. // @task: Let's find the exact path first as there could be 3 possibilities here.
  55. // 1. Shared folder
  56. // 2. User folder
  57. $absolutePath = EasyBlogMediaManager::getAbsolutePath( $source , $place );
  58. $absoluteURI = EasyBlogMediaManager::getAbsoluteURI( $source , $place );
  59. // @TODO: Test if user is allowed to upload this image
  60. $message = $this->getMessageObj();
  61. $allowed = EasyImageHelper::canUploadFile( $file , $message );
  62. if( $allowed !== true )
  63. {
  64. return $this->output( $message );
  65. }
  66. $media = new EasyBlogMediaManager();
  67. $result = $media->upload( $absolutePath , $absoluteURI , $file , $source , $place );
  68. // This should be an error if the $result is not an MMIM object.
  69. if( !is_object( $result ) )
  70. {
  71. $message = $this->getMessageObj( '404' , $result );
  72. }
  73. else
  74. {
  75. $message = $this->getMessageObj( EBLOG_MEDIA_UPLOAD_SUCCESS , JText::_( 'COM_EASYBLOG_IMAGE_MANAGER_UPLOAD_SUCCESS' ) , $result );
  76. }
  77. return $this->output( $message );
  78. }
  79. private function getMessageObj( $code = '' , $message = '', $item = false )
  80. {
  81. $obj = new stdClass();
  82. $obj->code = $code;
  83. $obj->message = $message;
  84. if( $item )
  85. {
  86. $obj->item = $item;
  87. }
  88. return $obj;
  89. }
  90. private function output( $response )
  91. {
  92. include_once( EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'json.php' );
  93. $json = new Services_JSON();
  94. echo $json->encode( $response );
  95. exit;
  96. }
  97. }