示例#1
0
 public function multiUpload()
 {
     $mainframe = JFactory::getApplication();
     $jinput = $mainframe->input;
     $my = CFactory::getUser();
     $config = CFactory::getConfig();
     $type = $jinput->get('type', NULL, 'NONE');
     $id = $jinput->get('id', '0', 'Int');
     if ($my->id == 0) {
         $tokenId = $jinput->request->get('token', '', 'STRING');
         $userId = $jinput->request->get('uploaderid', '', 'INT');
         $my = CFactory::getUserFromTokenId($tokenId, $userId);
         $session = JFactory::getSession();
         $session->set('user', $my);
     }
     $parentTable = JTable::getInstance(ucfirst($type), 'CTable');
     $parentTable->load($id);
     $table = JTable::getInstance('File', 'CTable');
     $_file = $jinput->files->get('file');
     $fileLib = new CFilesLibrary();
     if (CLimitsLibrary::exceedDaily('files', $my->id)) {
         $json = array('msg' => JText::_('COM_COMMUNITY_FILES_LIMIT_REACHED'));
         echo json_encode($json);
         exit;
     }
     if ($type == 'discussion' && !CLimitsHelper::exceededGroupFileUpload($parentTable->groupid)) {
         $json = array('msg' => JText::_('COM_COMMUNITY_FILES_LIMIT_REACHED'));
         echo json_encode($json);
         exit;
     }
     $now = new JDate();
     $ext = pathinfo($_file['name']);
     $file = new stdClass();
     $file->creator = $my->id;
     $file->filesize = sprintf("%u", $_file['size']);
     $file->name = JString::substr($_file['name'], 0, JString::strlen($_file['name']) - (JString::strlen($ext['extension']) + 1));
     $file->created = $now->toSql();
     $file->type = CFileHelper::getExtensionIcon(CFileHelper::getFileExtension($_file['name']));
     $fileName = JApplication::getHash($_file['name'] . time()) . JString::substr($_file['name'], JString::strlen($_file['name']) - (JString::strlen($ext['extension']) + 1));
     if ($_file['error'] > 0 && $_file['error'] !== 'UPLOAD_ERR_OK') {
         $json = array('msg' => JText::sprintf('COM_COMMUNITY_PHOTOS_UPLOAD_ERROR', $_file['error']));
         echo json_encode($json);
         exit;
     }
     if (!$fileLib->checkType($_file['name'])) {
         $json = array('msg' => JText::_('COM_COMMUNITY_IMAGE_FILE_NOT_SUPPORTED'));
         echo json_encode($json);
         exit;
     }
     switch ($type) {
         case 'discussion':
             $file->discussionid = $parentTable->id;
             $file->groupid = $parentTable->groupid;
             $file->filepath = 'images/files' . '/' . $type . '/' . $file->discussionid . '/' . $fileName;
             break;
         case 'bulletin':
             $file->bulletinid = $parentTable->id;
             $file->groupid = $parentTable->groupid;
             $file->filepath = 'images/files' . '/' . $type . '/' . $file->bulletinid . '/' . $fileName;
             break;
         case 'message':
             $file->messageid = -1;
             // set as -1 just in case this is not used and cron can clear it later
             if ($id) {
                 $file->filepath = 'images/files/' . $type . '/' . $id . '/' . $fileName;
                 if (!JFolder::exists(JPATH_ROOT . '/images/files/' . $type . '/' . $id)) {
                     JFolder::create(JPATH_ROOT . '/images/files/' . $type . '/' . $id, (int) octdec($config->get('folderpermissionsphoto')));
                     JFile::copy(JPATH_ROOT . '/components/com_community/index.html', JPATH_ROOT . '/images/files/' . $type . '/' . $id . '/index.html');
                 }
                 JFile::copy($_file['tmp_name'], JPATH_ROOT . '/' . $file->filepath);
             } else {
                 //this could be from new message, and there is no id given
                 $file->filepath = 'images/files/' . $type . '/temp/' . $fileName;
                 //create the folder here as the logic for bulletin and discussion is not the same
                 if (!JFolder::exists(JPATH_ROOT . '/' . $type . '/temp')) {
                     JFolder::create(JPATH_ROOT . '/images/files' . '/' . $type . '/temp', (int) octdec($config->get('folderpermissionsphoto')));
                     JFile::copy(JPATH_ROOT . '/components/com_community/index.html', JPATH_ROOT . '/files' . '/' . $type . '/temp/index.html');
                 }
                 JFile::copy($_file['tmp_name'], JPATH_ROOT . '/images/files' . '/' . $type . '/temp/' . $fileName);
             }
             break;
     }
     if ($type != 'message') {
         if (!JFolder::exists(JPATH_ROOT . '/' . $type . '/' . $parentTable->id)) {
             JFolder::create(JPATH_ROOT . '/images/files' . '/' . $type . '/' . $parentTable->id, (int) octdec($config->get('folderpermissionsphoto')));
             JFile::copy(JPATH_ROOT . '/components/com_community/index.html', JPATH_ROOT . '/files' . '/' . $type . '/' . $parentTable->id . '/index.html');
         }
         JFile::copy($_file['tmp_name'], JPATH_ROOT . '/images/files' . '/' . $type . '/' . $parentTable->id . '/' . $fileName);
     }
     $table->bind($file);
     $table->store();
     $params = new CParameter('');
     switch ($type) {
         case 'discussion':
             // Get repliers for this discussion and notify the discussion creator too
             $discussionModel = CFactory::getModel('Discussions');
             $discussion = JTable::getInstance('Discussion', 'CTable');
             $discussion->load($parentTable->id);
             $users = $discussionModel->getRepliers($discussion->id, $discussion->groupid);
             $users[] = $discussion->creator;
             // The person who post this, should not be getting notification email
             $key = array_search($my->id, $users);
             if ($key !== false && isset($users[$key])) {
                 unset($users[$key]);
             }
             $params->set('url', 'index.php?option=com_community&view=groups&task=viewdiscussion&groupid=' . $discussion->groupid . '&topicid=' . $discussion->id);
             $params->set('filename', $_file['name']);
             $params->set('discussion', $discussion->title);
             $params->set('discussion_url', 'index.php?option=com_community&view=groups&task=viewdiscussion&groupid=' . $discussion->groupid . '&topicid=' . $discussion->id);
             CNotificationLibrary::add('groups_discussion_newfile', $my->id, $users, JText::sprintf('COM_COMMUNITY_GROUP_DISCUSSION_NEW_FILE_SUBJECT'), '', 'groups.discussion.newfile', $params);
             break;
         case 'bulletin':
             break;
     }
     $json = array('id' => $table->id);
     die(json_encode($json));
 }