/** * save posting * * @return int */ function savePosting() { //global $mainframe; $app = JFactory::getApplication(); $params = JComponentHelper::getParams('com_discussions'); $_dateformat = $params->get('dateformat', 'd.m.Y'); $_timeformat = $params->get('timeformat', 'H:i'); $user =& JFactory::getUser(); $logUser = new CofiUser($user->id); $CofiHelper = new CofiHelper(); $this->_headline = ""; $this->_dbmode = JRequest::getString('dbmode', ''); $_postSubject = JRequest::getString('postSubject', '', 'POST', JREQUEST_ALLOWRAW); $_postSubject = strip_tags($_postSubject); $_postText = JRequest::getString('postText', '', 'POST', JREQUEST_ALLOWRAW); $_postText = strip_tags($_postText); $_image1_description = JRequest::getString('image1_description', ''); $_image1_description = strip_tags($_image1_description); $_image2_description = JRequest::getString('image2_description', ''); $_image2_description = strip_tags($_image2_description); $_image3_description = JRequest::getString('image3_description', ''); $_image3_description = strip_tags($_image3_description); $_image4_description = JRequest::getString('image4_description', ''); $_image4_description = strip_tags($_image4_description); $_image5_description = JRequest::getString('image5_description', ''); $_image5_description = strip_tags($_image5_description); $_postCatId = JRequest::getInt('catid', '0'); $_postThread = JRequest::getInt('thread', '0'); $_postParent = JRequest::getInt('parent', '0'); $_postId = JRequest::getInt('id', '0'); // get user IP address $_postIpAddress = $_SERVER['REMOTE_ADDR']; // redirect link $redirectLink = JRoute::_("index.php?option=com_discussions&view=category&catid=" . $this->getCategorySlug()); // check if user is logged in - maybe session has timed out if ($user->guest) { // if user is not logged in, kick him back into category $app->redirect($redirectLink, JText::_('COFI_POST_NOT_SAVED'), "message"); } // 1. check if subject >= 5 chars // todo make minimum subject length configurable if (strlen($_postSubject) < 5) { $isSubjectTooShort = true; } else { $isSubjectTooShort = false; } // 2. check if text >= 5 chars // todo make minimum text length configurable if (strlen($_postText) < 5) { $isTextTooShort = true; } else { $isTextTooShort = false; } // check if insert or update // update if ($this->_dbmode == "update") { if (!$isSubjectTooShort && !$isTextTooShort) { // check if subject and text have minimum length $db =& $this->getDBO(); // insert last edit time stamp $_unixtime = time(); // todo change date, time calculation // get rid of the percentage symbol % $_dateformat = str_replace("%", "", $_dateformat); $_timeformat = str_replace("%", "", $_timeformat); //$_timeformat = "g:i A"; $_date = date($_dateformat, $_unixtime); $_time = date($_timeformat, $_unixtime); $_timestamp = "\n\n" . JText::_('COFI_EDITED_BY') . " " . $user->username . " - " . $_date . " " . $_time; $_postText .= $_timestamp; if ($logUser->isModerator()) { // moderators are allowed to edit all posts $sql = "UPDATE " . $db->nameQuote('#__discussions_messages') . " SET" . " message = " . $db->Quote($_postText) . ", " . " image1_description = " . $db->Quote($_image1_description) . ", " . " image2_description = " . $db->Quote($_image2_description) . ", " . " image3_description = " . $db->Quote($_image3_description) . ", " . " image4_description = " . $db->Quote($_image4_description) . ", " . " image5_description = " . $db->Quote($_image5_description) . " WHERE id = '" . $_postId . "'"; } else { // no mod? then user must be owner $sql = "UPDATE " . $db->nameQuote('#__discussions_messages') . " SET" . " message = " . $db->Quote($_postText) . ", " . " image1_description = " . $db->Quote($_image1_description) . ", " . " image2_description = " . $db->Quote($_image2_description) . ", " . " image3_description = " . $db->Quote($_image3_description) . ", " . " image4_description = " . $db->Quote($_image4_description) . ", " . " image5_description = " . $db->Quote($_image5_description) . " WHERE id = '" . $_postId . "' AND user_id = '" . $user->id . "'"; } $db->setQuery($sql); $result = $db->query(); // check if there are images to delete // get folder name $rootDir = JPATH_ROOT; $cb_image1 = JRequest::getString('cb_image1', '', 'POST'); $cb_image2 = JRequest::getString('cb_image2', '', 'POST'); $cb_image3 = JRequest::getString('cb_image3', '', 'POST'); $cb_image4 = JRequest::getString('cb_image4', '', 'POST'); $cb_image5 = JRequest::getString('cb_image5', '', 'POST'); if ($cb_image1 == "delete") { $this->del_image($_postThread, $_postId, "image1", $rootDir, $db, 1); } if ($cb_image2 == "delete") { $this->del_image($_postThread, $_postId, "image2", $rootDir, $db, 2); } if ($cb_image3 == "delete") { $this->del_image($_postThread, $_postId, "image3", $rootDir, $db, 3); } if ($cb_image4 == "delete") { $this->del_image($_postThread, $_postId, "image4", $rootDir, $db, 4); } if ($cb_image5 == "delete") { $this->del_image($_postThread, $_postId, "image5", $rootDir, $db, 5); } if ($result) { // update went fine // upload images to id folder if (isset($_FILES['image1']) and !$_FILES['image1']['error']) { $this->add_image($_postThread, $_postId, "image1", $rootDir, $db, 1); } if (isset($_FILES['image2']) and !$_FILES['image2']['error']) { $this->add_image($_postThread, $_postId, "image2", $rootDir, $db, 2); } if (isset($_FILES['image3']) and !$_FILES['image3']['error']) { $this->add_image($_postThread, $_postId, "image3", $rootDir, $db, 3); } if (isset($_FILES['image4']) and !$_FILES['image4']['error']) { $this->add_image($_postThread, $_postId, "image4", $rootDir, $db, 4); } if (isset($_FILES['image5']) and !$_FILES['image5']['error']) { $this->add_image($_postThread, $_postId, "image5", $rootDir, $db, 5); } } } } else { if (!$isSubjectTooShort && !$isTextTooShort) { // check if subject and text have minimum length $db =& $this->getDBO(); // preset is published and not moderated (normal state) $published = 1; $wfm = 0; // wfm = waiting for moderation if ($logUser->isModerator() == 0) { // bypass these checks if user is moderator // 1. check for rookie mode // get Rookie Mode setting from com_discussions parameters $rookie = $params->get('rookie', '0'); if ($rookie > 0) { // we are in rookie mode if ($logUser->isRookie() == 1) { // user is a rookie $wfm = 1; // wfm = waiting for moderation $published = 0; } } // 2. check if this is a moderated user if ($logUser->isModerated() == 1) { // user is moderated $wfm = 1; // wfm = waiting for moderation $published = 0; } // 3. check if this is a moderated category if ($CofiHelper->isCategoryModerated($_postCatId)) { // category is moderated $wfm = 1; // wfm = waiting for moderation $published = 0; } } // create alias for SEF URL jimport('joomla.filter.output'); $alias = $_postSubject; $alias = JFilterOutput::stringURLSafe($alias); $insert_sql = "INSERT INTO " . $db->nameQuote('#__discussions_messages') . " ( parent_id, cat_id, thread, user_id, account, name, email, ip, subject, alias, message, image1_description, image2_description, image3_description, image4_description, image5_description, published, wfm) " . " VALUES ( " . $_postParent . ", " . $_postCatId . ", " . $_postThread . ", '" . $user->id . "', '" . $user->username . "', '" . $user->name . "', '" . $user->email . "', '" . $_postIpAddress . "', " . $db->Quote($_postSubject) . ", " . $db->Quote($alias) . ", " . $db->Quote($_postText) . ", " . $db->Quote($_image1_description) . ", " . $db->Quote($_image2_description) . ", " . $db->Quote($_image3_description) . ", " . $db->Quote($_image4_description) . ", " . $db->Quote($_image5_description) . ", " . $published . ", " . $wfm . " )"; $db->setQuery($insert_sql); $insert_result = $db->query(); // $_postId = last_insert_id(); $db->setQuery("SELECT LAST_INSERT_ID() FROM " . $db->nameQuote('#__discussions_messages')); $_postId = $db->loadResult(); // get parent and set thread to id if 0 if ($_postThread == 0) { // no thread id, so it is like id $_postThread = $_postId; $sql = "UPDATE " . $db->nameQuote('#__discussions_messages') . " SET thread = '" . $_postThread . "' WHERE id = '" . $_postId . "'"; $db->setQuery($sql); $result = $db->query(); } else { // thread is set if ($_postParent == 0) { // no parent id, so it is like thread id $sql = "UPDATE " . $db->nameQuote('#__discussions_messages') . " SET parent_id = '" . $_postThread . "' WHERE id = '" . $_postId . "'"; $db->setQuery($sql); $result = $db->query(); } } if ($insert_result) { // if insert was successful update statistics if ($published == 1) { // thread goes live, so we can update stats // set user post counter ++ $result = $CofiHelper->increaseUserPostCounter($user->id); // update thread stats $result = $CofiHelper->updateThreadStats($_postThread); // update category stats $result = $CofiHelper->updateCategoryStats($_postCatId); } // if published } if ($insert_result) { // insert went fine // upload image attachments todo // get folder name $rootDir = JPATH_ROOT; if (isset($_FILES['image1']) and !$_FILES['image1']['error']) { $this->add_image($_postThread, $_postId, "image1", $rootDir, $db, 1); } if (isset($_FILES['image2']) and !$_FILES['image2']['error']) { $this->add_image($_postThread, $_postId, "image2", $rootDir, $db, 2); } if (isset($_FILES['image3']) and !$_FILES['image3']['error']) { $this->add_image($_postThread, $_postId, "image3", $rootDir, $db, 3); } if (isset($_FILES['image4']) and !$_FILES['image4']['error']) { $this->add_image($_postThread, $_postId, "image4", $rootDir, $db, 4); } if (isset($_FILES['image5']) and !$_FILES['image5']['error']) { $this->add_image($_postThread, $_postId, "image5", $rootDir, $db, 5); } if ($wfm == 1) { // this post needs moderator approval $CofiHelper->sendEmailToModeratorsPostWFM(); $app->redirect($redirectLink, JText::_('COFI_POST_SAVED_NEEDS_APPROVAL'), "notice"); } else { // redirect link to last post $redirectLinkToLastPost = $this->getLinkToLastPostByThreadId($_postThread); $app->redirect($redirectLinkToLastPost, JText::_('COFI_POST_SAVED'), "notice"); } } else { $app->redirect($redirectLink, JText::_('COFI_POST_NOT_SAVED_INSERT_ERROR'), "message"); } } } // end insert if ($isSubjectTooShort) { $app->redirect($redirectLink, JText::_('COFI_POST_NOT_SAVED_SUBJECT_TOO_SHORT'), "message"); } if ($isTextTooShort) { $app->redirect($redirectLink, JText::_('COFI_POST_NOT_SAVED_TEXT_TOO_SHORT'), "message"); } // redirect link to last post $redirectLinkToLastPost = $this->getLinkToLastPostByThreadId($_postThread); $app->redirect($redirectLinkToLastPost, JText::_('COFI_POST_SAVED'), "notice"); return 0; // save OK }