Exemplo n.º 1
0
 public function upload()
 {
     // Check for request forgeries
     JRequest::checkToken() or jexit(JText::_('COM_COMMUNITY_INVALID_TOKEN'));
     if ($this->blockUnregister()) {
         return;
     }
     $this->checkVideoAccess();
     $document = JFactory::getDocument();
     $viewType = $document->getType();
     $viewName = JRequest::getCmd('view', $this->getName());
     $view = $this->getView($viewName, '', $viewType);
     $mainframe = JFactory::getApplication();
     $jinput = $mainframe->input;
     $my = CFactory::getUser();
     $creatorType = $jinput->get('creatortype', VIDEO_USER_TYPE, 'NONE');
     //JRequest::getVar('creatortype', VIDEO_USER_TYPE);
     $groupid = $jinput->get('groupid', 0, 'INT');
     $eventid = $jinput->get('eventid', 0, 'INT');
     $config = CFactory::getConfig();
     $redirect = CVideosHelper::getVideoReturnUrlFromRequest();
     // @rule: Do not allow users to add more videos than they are allowed to
     //CFactory::load( 'libraries' , 'limits' );
     if (CLimitsLibrary::exceedDaily('videos')) {
         $mainframe->redirect($redirect, JText::_('COM_COMMUNITY_VIDEOS_LIMIT_REACHED'), 'error');
     }
     // Process according to video creator type
     list($creatorType, $videoLimit) = $this->_manipulateParameter($groupid, $eventid, $config);
     $group = JTable::getInstance('Group', 'CTable');
     $group->load($groupid);
     if ($group->approvals) {
         $permission = 40;
     }
     $permission = JRequest::getInt('permissions', 0, 'post');
     // Check is video upload is permitted
     if (CLimitsHelper::exceededVideoUpload($my->id, $creatorType)) {
         $message = JText::sprintf('COM_COMMUNITY_VIDEOS_CREATION_LIMIT_ERROR', $videoLimit);
         $mainframe->redirect($redirect, $message);
         exit;
     }
     if (!$config->get('enablevideos')) {
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_VIDEOS_VIDEO_DISABLED', 'notice'));
         $mainframe->redirect(CRoute::_('index.php?option=com_community&view=frontpage', false));
         exit;
     }
     if (!$config->get('enablevideosupload')) {
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_VIDEOS_UPLOAD_DISABLED', 'notice'));
         $mainframe->redirect(CRoute::_('index.php?option=com_community&view=videos', false));
         exit;
     }
     // Check if the video file is valid
     $files = JRequest::get('files');
     $videoFile = !empty($files['videoFile']) ? $files['videoFile'] : array();
     if (empty($files) || empty($videoFile['name']) && $videoFile['size'] < 1) {
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_VIDEOS_UPLOAD_ERROR', 'error'));
         $mainframe->redirect($redirect, false);
         exit;
     }
     // Check file type.
     $fileType = CVideosHelper::getMIMEType($videoFile);
     $allowable = CVideosHelper::getValidMIMEType();
     if (!in_array(strtolower($fileType), $allowable)) {
         $mainframe->redirect($redirect, JText::sprintf('COM_COMMUNITY_VIDEOS_FILETYPE_ERROR', $fileType));
         exit;
     }
     $fileExtension = pathinfo($videoFile['name']);
     $allowextension = CVideosHelper::getValidExtensionType();
     if (!in_array(strtolower($fileExtension['extension']), $allowextension)) {
         $mainframe->redirect($redirect, JText::sprintf('COM_COMMUNITY_VIDEOS_FILETYPE_ERROR', $fileExtension['extension']));
         exit;
     }
     // Check if the video file exceeds file size limit
     $uploadLimit = $config->get('maxvideouploadsize');
     /* MB unit */
     $videoFileSize = sprintf("%u", filesize($videoFile['tmp_name']));
     //convert to MB
     $videoFileSize = number_format($videoFileSize / 1048576, 2);
     if ($uploadLimit > 0 && $videoFileSize > $uploadLimit) {
         $mainframe->redirect($redirect, JText::sprintf('COM_COMMUNITY_VIDEOS_FILE_SIZE_EXCEEDED', $uploadLimit));
     }
     // Passed all checking, attempt to save the video file
     $folderPath = CVideoLibrary::getPath($my->id, 'original');
     $randomFileName = CFileHelper::getRandomFilename($folderPath, $videoFile['name'], '');
     $destination = JPATH::clean($folderPath . '/' . $randomFileName);
     if (!CFileHelper::upload($videoFile, $destination)) {
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_VIDEOS_UPLOAD_ERROR', 'error'));
         $mainframe->redirect($redirect, false);
         exit;
     }
     $config = CFactory::getConfig();
     $videofolder = $config->get('videofolder');
     //CFactory::load( 'models' , 'videos' );
     $video = JTable::getInstance('Video', 'CTable');
     $video->set('path', $videofolder . '/originalvideos/' . $my->id . '/' . $randomFileName);
     $video->set('title', $jinput->get('title', '', 'STRING'));
     $video->set('description', $jinput->get('description', '', 'STRING'));
     $video->set('category_id', JRequest::getInt('category_id', 0, 'post'));
     $video->set('permissions', $permission);
     $video->set('creator', $my->id);
     $video->set('creator_type', $creatorType);
     $video->set('groupid', $groupid);
     $video->set('eventid', $eventid);
     $video->set('location', $jinput->get('location', '', 'STRING'));
     $video->set('filesize', $videoFileSize);
     if (!$video->store()) {
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_VIDEOS_SAVE_ERROR', 'error'));
         $mainframe->redirect($redirect, false);
         exit;
     }
     //add notification: New group album is added
     if ($video->groupid != 0) {
         $group = JTable::getInstance('Group', 'CTable');
         $group->load($video->groupid);
         $modelGroup = $this->getModel('groups');
         $groupMembers = array();
         $groupMembers = $modelGroup->getMembersId($video->groupid, true);
         $params = new CParameter('');
         $params->set('title', $video->title);
         $params->set('group', $group->name);
         $params->set('group_url', 'index.php?option=com_community&view=groups&task=viewgroup&groupid=' . $group->id);
         $params->set('url', 'index.php?option=com_community&view=videos&task=video&videoid=' . $video->id);
         CNotificationLibrary::add('groups_create_video', $my->id, $groupMembers, JText::sprintf('COM_COMMUNITY_GROUP_NEW_VIDEO_NOTIFICATION', '{actor}', '{group}'), '', 'groups.video', $params);
     }
     // Trigger for onVideoCreate
     $this->_triggerEvent('onVideoCreate', $video);
     $this->cacheClean(array(COMMUNITY_CACHE_TAG_VIDEOS, COMMUNITY_CACHE_TAG_FRONTPAGE, COMMUNITY_CACHE_TAG_FEATURED, COMMUNITY_CACHE_TAG_VIDEOS_CAT, COMMUNITY_CACHE_TAG_ACTIVITIES, COMMUNITY_CACHE_TAG_GROUPS_DETAIL));
     // Video saved, redirect
     $redirect = CVideosHelper::getVideoReturnUrlFromRequest('pending');
     $mainframe->redirect($redirect, JText::sprintf('COM_COMMUNITY_VIDEOS_UPLOAD_SUCCESS', $video->title));
 }