/** * Controller's default action * * @param array $params * @throws AuthenticateException */ public function index(array $params = null) { $groupId = isset($params['groupId']) && (int) $params['groupId'] ? (int) $params['groupId'] : 0; $forumService = FORUM_BOL_ForumService::getInstance(); $forumGroup = $forumService->getGroupInfo($groupId); if ($forumGroup) { $forumSection = $forumService->findSectionById($forumGroup->sectionId); $isHidden = $forumSection->isHidden; } else { $isHidden = false; } if (!OW::getUser()->isAuthenticated()) { throw new AuthenticateException(); } $userId = OW::getUser()->getId(); $this->assign('authMsg', null); if ($isHidden) { //$isModerator = OW::getUser()->isAuthorized($forumSection->entity); //$canEdit = OW::getUser()->isAuthorized($forumSection->entity, 'add_topic'); $eventParams = array('entity' => $forumSection->entity, 'entityId' => $forumGroup->entityId, 'action' => 'add_topic'); $event = new OW_Event('forum.check_permissions', $eventParams); OW::getEventManager()->trigger($event); if (!$event->getData()) { $this->setTemplate(OW::getPluginManager()->getPlugin('base')->getCtrlViewDir() . 'authorization_failed.html'); return; } $eventParams = array('pluginKey' => $forumSection->entity, 'action' => 'add_post'); $credits = OW::getEventManager()->call('usercredits.check_balance', $eventParams); if ($credits === false) { $this->assign('authMsg', OW::getEventManager()->call('usercredits.error_message', $eventParams)); } $event = new OW_Event('forum.find_forum_caption', array('entity' => $forumSection->entity, 'entityId' => $forumGroup->entityId)); OW::getEventManager()->trigger($event); $eventData = $event->getData(); $componentForumCaption = $eventData['component']; if (!empty($componentForumCaption)) { $this->assign('componentForumCaption', $componentForumCaption->render()); } else { $componentForumCaption = false; $this->assign('componentForumCaption', $componentForumCaption); } $bcItems = array(array('href' => OW::getRouter()->urlForRoute('group-default', array('groupId' => $forumGroup->getId())), 'label' => OW::getLanguage()->text($forumSection->entity, 'view_all_topics'))); $breadCrumbCmp = new BASE_CMP_Breadcrumb($bcItems); $this->addComponent('breadcrumb', $breadCrumbCmp); OW::getNavigation()->deactivateMenuItems(OW_Navigation::MAIN); OW::getNavigation()->activateMenuItem(OW_Navigation::MAIN, $forumSection->entity, $eventData['key']); $groupSelect = array(array('label' => $forumGroup->name, 'value' => $forumGroup->getId(), 'disabled' => false)); OW::getDocument()->setHeading(OW::getLanguage()->text($forumSection->entity, 'create_new_topic', array('group' => $forumGroup->name))); } else { $canEdit = OW::getUser()->isAuthorized('forum', 'edit'); if (!$userId || !$canEdit) { $this->assign('authMsg', OW::getLanguage()->text('base', 'authorization_failed_feedback')); } $eventParams = array('pluginKey' => 'forum', 'action' => 'add_post'); $credits = OW::getEventManager()->call('usercredits.check_balance', $eventParams); if ($credits === false) { $this->assign('authMsg', OW::getEventManager()->call('usercredits.error_message', $eventParams)); } if (!OW::getRequest()->isAjax()) { OW::getNavigation()->activateMenuItem(OW_Navigation::MAIN, 'forum', 'forum'); } $groupSelect = $forumService->getGroupSelectList(0, false, $userId); OW::getDocument()->setHeading(OW::getLanguage()->text('forum', 'create_new_topic')); } OW::getDocument()->setDescription(OW::getLanguage()->text('forum', 'meta_description_add_topic')); OW::getDocument()->setTitle(OW::getLanguage()->text('forum', 'meta_title_add_topic')); OW::getDocument()->setHeadingIconClass('ow_ic_write'); $this->assign('isHidden', $isHidden); $form = $this->generateForm($groupSelect, $groupId, $isHidden); OW::getDocument()->addStyleDeclaration(' .disabled_option { color: #9F9F9F; } '); $enableAttachments = OW::getConfig()->getValue('forum', 'enable_attachments'); $this->assign('enableAttachments', $enableAttachments); if (OW::getRequest()->isPost() && $form->isValid($_POST)) { $data = $form->getValues(); if ($data['group']) { $topicDto = new FORUM_BOL_Topic(); $topicDto->userId = $userId; $topicDto->groupId = $data['group']; $topicDto->title = strip_tags($data['title']); $forumService->saveOrUpdateTopic($topicDto); $postDto = new FORUM_BOL_Post(); $postDto->topicId = $topicDto->id; $postDto->userId = $userId; $postDto->text = trim($data['text']); $postDto->createStamp = time(); $forumService->saveOrUpdatePost($postDto); $topicDto->lastPostId = $postDto->getId(); $forumService->saveOrUpdateTopic($topicDto); // subscribe author to new posts if ($data['subscribe']) { $subService = FORUM_BOL_SubscriptionService::getInstance(); $subs = new FORUM_BOL_Subscription(); $subs->userId = $userId; $subs->topicId = $topicDto->id; $subService->addSubscription($subs); } $accepted = floatval(OW::getConfig()->getValue('forum', 'attachment_filesize') * 1024 * 1024); if (isset($data['attachments']) && count($data['attachments'])) { $filesArray = $data['attachments']; $filesCount = count($filesArray['name']); $attachmentService = FORUM_BOL_PostAttachmentService::getInstance(); $skipped = 0; for ($i = 0; $i < $filesCount; $i++) { if (!strlen($filesArray['tmp_name'][$i])) { continue; } // skip unsupported extensions $ext = UTIL_File::getExtension($filesArray['name'][$i]); if (!$attachmentService->fileExtensionIsAllowed($ext)) { $skipped++; continue; } // skip too big files if ($filesArray['size'][$i] > $accepted) { $skipped++; continue; } $attachmentDto = new FORUM_BOL_PostAttachment(); $attachmentDto->postId = $postDto->id; $attachmentDto->fileName = htmlspecialchars($filesArray['name'][$i]); $attachmentDto->fileNameClean = UTIL_File::sanitizeName($attachmentDto->fileName); $attachmentDto->fileSize = $filesArray['size'][$i]; $attachmentDto->hash = uniqid(); $added = $attachmentService->addAttachment($attachmentDto, $filesArray['tmp_name'][$i]); if (!$added) { $skipped++; } } if ($skipped) { OW::getFeedback()->warning(OW::getLanguage()->text('forum', 'not_all_attachments_added')); } } $topicUrl = OW::getRouter()->urlForRoute('topic-default', array('topicId' => $topicDto->id)); //Newsfeed $params = array('pluginKey' => 'forum', 'entityType' => 'forum-topic', 'entityId' => $topicDto->id, 'userId' => $topicDto->userId); $event = new OW_Event('feed.action', $params); OW::getEventManager()->trigger($event); if ($credits === true) { OW::getEventManager()->call('usercredits.track_action', $eventParams); } if ($isHidden) { $params = array('topicId' => $topicDto->id, 'entity' => $forumSection->entity, 'entityId' => $forumGroup->entityId, 'userId' => $topicDto->userId, 'topicUrl' => $topicUrl, 'topicTitle' => $topicDto->title, 'postText' => $postDto->text); $event = new OW_Event('forum.topic_add', $params); OW::getEventManager()->trigger($event); } $this->redirect($topicUrl); } else { $form->getElement('group')->addError(OW::getLanguage()->text('forum', 'select_group_error')); } } }
public function processUploadedFile($pluginKey, array $fileInfo, $bundle = null, $validFileExtensions = array(), $maxUploadSize = null, $dimensions = null) { $language = OW::getLanguage(); $error = false; if (!OW::getUser()->isAuthenticated()) { throw new InvalidArgumentException($language->text('base', 'user_is_not_authenticated')); } if (empty($fileInfo) || !is_uploaded_file($fileInfo['tmp_name'])) { throw new InvalidArgumentException($language->text('base', 'upload_file_fail')); } if ($fileInfo['error'] != UPLOAD_ERR_OK) { switch ($fileInfo['error']) { case UPLOAD_ERR_INI_SIZE: $error = $language->text('base', 'upload_file_max_upload_filesize_error'); break; case UPLOAD_ERR_PARTIAL: $error = $language->text('base', 'upload_file_file_partially_uploaded_error'); break; case UPLOAD_ERR_NO_FILE: $error = $language->text('base', 'upload_file_no_file_error'); break; case UPLOAD_ERR_NO_TMP_DIR: $error = $language->text('base', 'upload_file_no_tmp_dir_error'); break; case UPLOAD_ERR_CANT_WRITE: $error = $language->text('base', 'upload_file_cant_write_file_error'); break; case UPLOAD_ERR_EXTENSION: $error = $language->text('base', 'upload_file_invalid_extention_error'); break; default: $error = $language->text('base', 'upload_file_fail'); } throw new InvalidArgumentException($error); } if (empty($validFileExtensions)) { $validFileExtensions = json_decode(OW::getConfig()->getValue('base', 'attch_ext_list'), true); } if ($maxUploadSize === null) { $maxUploadSize = OW::getConfig()->getValue('base', 'attch_file_max_size_mb'); } if (!empty($validFileExtensions) && !in_array(UTIL_File::getExtension($fileInfo['name']), $validFileExtensions)) { throw new InvalidArgumentException($language->text('base', 'upload_file_extension_is_not_allowed')); } // get all bundle upload size $bundleSize = floor($fileInfo['size'] / 1024); if ($bundle !== null) { $list = $this->attachmentDao->findAttahcmentByBundle($pluginKey, $bundle); /* @var $item BOL_Attachment */ foreach ($list as $item) { $bundleSize += $item->getSize(); } } if ($maxUploadSize > 0 && $bundleSize > $maxUploadSize * 1024) { throw new InvalidArgumentException($language->text('base', 'upload_file_max_upload_filesize_error')); } $attachDto = new BOL_Attachment(); $attachDto->setUserId(OW::getUser()->getId()); $attachDto->setAddStamp(time()); $attachDto->setStatus(0); $attachDto->setSize(floor($fileInfo['size'] / 1024)); $attachDto->setOrigFileName(htmlspecialchars($fileInfo['name'])); $attachDto->setFileName(uniqid() . '_' . UTIL_File::sanitizeName($attachDto->getOrigFileName())); $attachDto->setPluginKey($pluginKey); if ($bundle !== null) { $attachDto->setBundle($bundle); } $this->attachmentDao->save($attachDto); $uploadPath = $this->getAttachmentsDir() . $attachDto->getFileName(); $tempPath = $this->getAttachmentsDir() . 'temp_' . $attachDto->getFileName(); if (in_array(UTIL_File::getExtension($fileInfo['name']), array('jpg', 'jpeg', 'gif', 'png'))) { try { $image = new UTIL_Image($fileInfo['tmp_name']); if (empty($dimensions)) { $dimensions = array('width' => 1000, 'height' => 1000); } $image->resizeImage($dimensions['width'], $dimensions['height'])->orientateImage()->saveImage($tempPath); $image->destroy(); @unlink($fileInfo['tmp_name']); } catch (Exception $e) { throw new InvalidArgumentException($language->text('base', 'upload_file_fail')); } } else { move_uploaded_file($fileInfo['tmp_name'], $tempPath); } OW::getStorage()->copyFile($tempPath, $uploadPath); OW::getStorage()->chmod($uploadPath, 0666); unlink($tempPath); return array('uid' => $attachDto->getBundle(), 'dto' => $attachDto, 'path' => $uploadPath, 'url' => $this->getAttachmentsUrl() . $attachDto->getFileName()); }
public function index(array $params = null) { $forumService = FORUM_BOL_ForumService::getInstance(); if (!isset($params['id']) || !($postId = (int) $params['id'])) { throw new Redirect404Exception(); } $postDto = $forumService->findPostById($postId); if (!$postDto) { throw new Redirect404Exception(); } $userId = OW::getUser()->getId(); $topicId = $postDto->topicId; $topicDto = $forumService->findTopicById($topicId); $forumGroup = $forumService->getGroupInfo($topicDto->groupId); $forumSection = $forumService->findSectionById($forumGroup->sectionId); $isHidden = $forumSection->isHidden; if ($isHidden) { $isModerator = OW::getUser()->isAuthorized($forumSection->entity); $eventParams = array('entity' => $forumSection->entity, 'entityId' => $forumGroup->entityId, 'action' => 'add_topic'); $event = new OW_Event('forum.check_permissions', $eventParams); OW::getEventManager()->trigger($event); $canPost = $event->getData(); //check permissions $canEdit = OW::getUser()->isAuthorized($forumSection->entity, 'add_topic', $postDto->userId); } else { $isModerator = OW::getUser()->isAuthorized('forum'); $canPost = true; $canEdit = $postDto->userId == OW::getUser()->getId(); } if ((!$canEdit || !$canPost) && !$isModerator) { $this->setTemplate(OW::getPluginManager()->getPlugin('base')->getCtrlViewDir() . 'authorization_failed.html'); return; } $this->assign('postId', $postId); $editPostForm = $this->generateEditPostForm($postDto); $this->addForm($editPostForm); $lang = OW::getLanguage(); $router = OW::getRouter(); if (OW::getRequest()->isPost() && $editPostForm->isValid($_POST)) { $values = $editPostForm->getValues(); $postId = (int) $values['post-id']; $text = $values['text']; $topicId = (int) $values['topic']; $topicUrl = OW::getRouter()->urlForRoute('topic-default', array('topicId' => $topicId)); $postDto = $forumService->findPostById($postId); if ($postDto === null || $postDto->userId != $userId && !$isModerator) { $this->redirect($topicUrl); } //save post $postDto->text = $text; $forumService->saveOrUpdatePost($postDto); //save post edit info $editPostDto = $forumService->findEditPost($postId); if ($editPostDto === null) { $editPostDto = new FORUM_BOL_EditPost(); } $editPostDto->postId = $postId; $editPostDto->userId = $userId; $editPostDto->editStamp = time(); $forumService->saveOrUpdateEditPost($editPostDto); if (isset($_FILES['attachments']) && count($_FILES['attachments'])) { $filesArray = $_FILES['attachments']; $filesCount = count($filesArray['name']); $attachmentService = FORUM_BOL_PostAttachmentService::getInstance(); $skipped = 0; $accepted = floatval(OW::getConfig()->getValue('forum', 'attachment_filesize') * 1024 * 1024); for ($i = 0; $i < $filesCount; $i++) { if (!strlen($filesArray['tmp_name'][$i])) { continue; } // skip unsupported extensions $ext = UTIL_File::getExtension($filesArray['name'][$i]); if (!$attachmentService->fileExtensionIsAllowed($ext)) { $skipped++; continue; } // skip too big files if ($filesArray['size'][$i] > $accepted) { $skipped++; continue; } $attachmentDto = new FORUM_BOL_PostAttachment(); $attachmentDto->postId = $postDto->id; $attachmentDto->fileName = htmlspecialchars($filesArray['name'][$i]); $attachmentDto->fileNameClean = UTIL_File::sanitizeName($attachmentDto->fileName); $attachmentDto->fileSize = $filesArray['size'][$i]; $attachmentDto->hash = uniqid(); $added = $attachmentService->addAttachment($attachmentDto, $filesArray['tmp_name'][$i]); if (!$added) { $skipped++; } } if ($skipped) { OW::getFeedback()->warning($lang->text('forum', 'not_all_attachments_added')); $this->redirect(); } } $this->redirect($forumService->getPostUrl($topicId, $postId, true)); } $enableAttachments = OW::getConfig()->getValue('forum', 'enable_attachments'); $this->assign('enableAttachments', $enableAttachments); if ($enableAttachments) { $attachments = FORUM_BOL_PostAttachmentService::getInstance()->findAttachmentsByPostIdList(array($postId)); $this->assign('attachments', $attachments); } $topicInfo = $forumService->getTopicInfo($topicId); $groupUrl = $router->urlForRoute('group-default', array('groupId' => $topicDto->groupId)); $topicUrl = $forumService->getPostUrl($topicId, $postId); $lang->addKeyForJs('forum', 'confirm_delete_attachment'); OW::getDocument()->setHeading(OW::getLanguage()->text('forum', 'edit_post_title')); OW::getDocument()->setHeadingIconClass('ow_ic_edit'); $this->assign('isHidden', $isHidden); if ($isHidden) { $event = new OW_Event('forum.find_forum_caption', array('entity' => $forumSection->entity, 'entityId' => $forumGroup->entityId)); OW::getEventManager()->trigger($event); $eventData = $event->getData(); $componentForumCaption = $eventData['component']; if (!empty($componentForumCaption)) { $this->assign('componentForumCaption', $componentForumCaption->render()); } else { $componentForumCaption = false; $this->assign('componentForumCaption', $componentForumCaption); } $bcItems = array(array('href' => OW::getRouter()->urlForRoute('topic-default', array('topicId' => $topicId)), 'label' => OW::getLanguage()->text('forum', 'back_to_topic'))); $breadCrumbCmp = new BASE_CMP_Breadcrumb($bcItems); $this->addComponent('breadcrumb', $breadCrumbCmp); OW::getNavigation()->deactivateMenuItems(OW_Navigation::MAIN); OW::getNavigation()->activateMenuItem(OW_Navigation::MAIN, $forumSection->entity, $eventData['key']); } else { $bcItems = array(array('href' => $router->urlForRoute('forum-default'), 'label' => $lang->text('forum', 'forum_index')), array('href' => $router->urlForRoute('forum-default') . '#section-' . $topicInfo['sectionId'], 'label' => $topicInfo['sectionName']), array('href' => $groupUrl, 'label' => $topicInfo['groupName']), array('href' => $topicUrl, 'label' => htmlspecialchars($topicDto->title))); $breadCrumbCmp = new BASE_CMP_Breadcrumb($bcItems, $lang->text('forum', 'post_location')); $this->addComponent('breadcrumb', $breadCrumbCmp); OW::getNavigation()->activateMenuItem(OW_Navigation::MAIN, 'forum', 'forum'); } }
/** * This action adds a post and after execution redirects to default action * * @param array $params */ public function addPost(array $params) { if (!isset($params['topicId']) || !($topicId = (int) $params['topicId'])) { throw new Redirect404Exception(); } $topicDto = $this->forumService->findTopicById($topicId); if (!$topicDto) { throw new Redirect404Exception(); } $addPostForm = $this->generateAddPostForm($topicId); if (OW::getRequest()->isPost() && $addPostForm->isValid($_POST)) { $data = $addPostForm->getValues(); if ($data['topic'] && $data['topic'] == $topicDto->id && !$topicDto->locked) { if (!OW::getUser()->getId()) { throw new AuthenticateException(); } $postDto = new FORUM_BOL_Post(); $postDto->topicId = $data['topic']; $postDto->userId = OW::getUser()->getId(); $postDto->text = trim($data['text']); $postDto->createStamp = time(); $this->forumService->saveOrUpdatePost($postDto); $topicDto->lastPostId = $postDto->getId(); $this->forumService->saveOrUpdateTopic($topicDto); $this->forumService->deleteByTopicId($topicId); if (isset($data['attachments']) && count($data['attachments'])) { $filesArray = $data['attachments']; $filesCount = count($filesArray['name']); $attachmentService = FORUM_BOL_PostAttachmentService::getInstance(); $skipped = 0; $accepted = floatval(OW::getConfig()->getValue('forum', 'attachment_filesize') * 1024 * 1024); for ($i = 0; $i < $filesCount; $i++) { if (!strlen($filesArray['tmp_name'][$i])) { continue; } // skip unsupported extensions $ext = UTIL_File::getExtension($filesArray['name'][$i]); if (!$attachmentService->fileExtensionIsAllowed($ext)) { $skipped++; continue; } // skip too big files if ($filesArray['size'][$i] > $accepted) { $skipped++; continue; } $attachmentDto = new FORUM_BOL_PostAttachment(); $attachmentDto->postId = $postDto->id; $attachmentDto->fileName = htmlspecialchars($filesArray['name'][$i]); $attachmentDto->fileNameClean = UTIL_File::sanitizeName($attachmentDto->fileName); $attachmentDto->fileSize = $filesArray['size'][$i]; $attachmentDto->hash = uniqid(); $added = $attachmentService->addAttachment($attachmentDto, $filesArray['tmp_name'][$i]); if (!$added) { $skipped++; } } if ($skipped) { OW::getFeedback()->warning(OW::getLanguage()->text('forum', 'not_all_attachments_added')); } } $postUrl = $this->forumService->getPostUrl($topicId, $postDto->id); $event = new OW_Event('forum.add_post', array('postId' => $postDto->id, 'topicId' => $topicId, 'userId' => $postDto->userId)); OW::getEventManager()->trigger($event); $forumGroup = $this->forumService->findGroupById($topicDto->groupId); if ($forumGroup) { $forumSection = $this->forumService->findSectionById($forumGroup->sectionId); if ($forumSection) { $pluginKey = $forumSection->isHidden ? $forumSection->entity : 'forum'; $eventParams = array('pluginKey' => $pluginKey, 'action' => 'add_post'); if ($credits = OW::getEventManager()->call('usercredits.check_balance', $eventParams) === true) { OW::getEventManager()->call('usercredits.track_action', $eventParams); } } } $this->redirect($postUrl); } } else { $this->redirect(OW::getRouter()->urlForRoute('topic-default', array('topicId' => $topicId))); } }