/** * Process the XML and treat it as a blogpost * * @param SimpleXMLElement $xml The XML to process. * @return bool */ private function processXMLAsPost(SimpleXMLElement $xml) { // init var $postID = substr((string) $xml->id, mb_strpos((string) $xml->id, 'post-') + 5); // validate if ($postID == '') { return false; } if ((string) $xml->title == '') { return false; } // build item $item['id'] = (int) BackendBlogModel::getMaximumId() + 1; $item['user_id'] = BackendAuthentication::getUser()->getUserId(); $item['hidden'] = 'N'; $item['allow_comments'] = 'Y'; $item['num_comments'] = 0; $item['status'] = 'active'; $item['language'] = BL::getWorkingLanguage(); $item['publish_on'] = BackendModel::getUTCDate(null, strtotime((string) $xml->published)); $item['created_on'] = BackendModel::getUTCDate(null, strtotime((string) $xml->published)); $item['edited_on'] = BackendModel::getUTCDate(null, strtotime((string) $xml->updated)); $item['category_id'] = 1; $item['title'] = (string) $xml->title; $item['text'] = (string) $xml->content; // set drafts hidden if (strtotime((string) $xml->published) > time()) { $item['hidden'] = 'Y'; $item['status'] = 'draft'; } // build meta $meta = array(); $meta['keywords'] = $item['title']; $meta['keywords_overwrite'] = 'N'; $meta['description'] = $item['title']; $meta['description_overwrite'] = 'N'; $meta['title'] = $item['title']; $meta['title_overwrite'] = 'N'; $meta['url'] = BackendBlogModel::getURL($item['title']); $meta['url_overwrite'] = 'N'; // replace f****d up links $item['text'] = preg_replace('|<a(.*)onblur="(.*)"(.*)>|Ui', '<a$1$3>', $item['text']); // fix images $item['text'] = preg_replace('|<img(.*)border="(.*)"(.*)>|Ui', '<img$1$3>', $item['text']); // remove inline styles $item['text'] = preg_replace('|<(.*)style="(.*)"(.*)>|Ui', '<$1$3>', $item['text']); // whitespace $item['text'] = preg_replace('|\\s{2,}|', ' ', $item['text']); // cleanup $search = array('<br /><br />', '<div><br /></div>', '<div>', '</div>', '<i>', '</i>', '<b>', '</b>', '<p><object', '</object></p>', '<p><p>', '</p></p>', '...'); $replace = array('</p><p>', '</p><p>', '', '', '<em>', '</em>', '<strong>', '</strong>', '<object', '</object>', '<p>', '</p>', '…'); // cleanup $item['text'] = '<p>' . str_replace($search, $replace, SpoonFilter::htmlentitiesDecode($item['text'])) . '</p>'; // get images $matches = array(); preg_match_all('/<img.*src="(.*)".*\\/>/Ui', $item['text'], $matches); // any images? if (isset($matches[1]) && !empty($matches[1])) { // init var $imagesPath = FRONTEND_FILES_PATH . '/userfiles/images/blog'; $imagesURL = FRONTEND_FILES_URL . '/userfiles/images/blog'; // create dir if needed if (!SpoonDirectory::exists($imagesPath)) { SpoonDirectory::create($imagesPath); } // loop matches foreach ($matches[1] as $key => $file) { // get file info $fileInfo = SpoonFile::getInfo($file); // init var $destinationFile = $item['id'] . '_' . $fileInfo['basename']; try { // download SpoonFile::download($file, $imagesPath . '/' . $destinationFile); // replace the old URL with the new one $item['text'] = str_replace($file, $imagesURL . '/' . $destinationFile, $item['text']); } catch (Exception $e) { // ignore } } } // get links $matches = array(); preg_match_all('/<a.*href="(.*)".*\\/>/Ui', $item['text'], $matches); // any images? if (isset($matches[1]) && !empty($matches[1])) { // loop matches foreach ($matches[1] as $key => $file) { // get new link $replaceWith = self::download($file, $item['id']); // should we replace? if ($replaceWith !== false) { // replace the old URL with the new one $item['text'] = str_replace($file, $replaceWith, $item['text']); } } } // insert meta $item['meta_id'] = BackendModel::getDB(true)->insert('meta', $meta); // insert BackendBlogModel::insert($item); // store the post $this->newIds[$postID] = $item['id']; // get tags $tags = array(); // loop categories foreach ($xml->category as $category) { // is this a tag? if so add it if ((string) $category['scheme'] == 'http://www.blogger.com/atom/ns#') { $tags[] = (string) $category['term']; } } // any tags? if (!empty($tags)) { BackendTagsModel::saveTags($item['id'], implode(',', $tags), $this->getModule()); } // return return true; }
/** * Validate the form */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // get the status $status = SpoonFilter::getPostValue('status', array('active', 'draft'), 'active'); // cleanup the submitted fields, ignore fields that were added by hackers $this->frm->cleanupFields(); // validate fields $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired')); $this->frm->getField('text')->isFilled(BL::err('FieldIsRequired')); $this->frm->getField('publish_on_date')->isValid(BL::err('DateIsInvalid')); $this->frm->getField('publish_on_time')->isValid(BL::err('TimeIsInvalid')); $this->frm->getField('category_id')->isFilled(BL::err('FieldIsRequired')); if ($this->frm->getField('category_id')->getValue() == 'new_category') { $this->frm->getField('category_id')->addError(BL::err('FieldIsRequired')); } if ($this->imageIsAllowed) { // validate the image if ($this->frm->getField('image')->isFilled()) { // image extension and mime type $this->frm->getField('image')->isAllowedExtension(array('jpg', 'png', 'gif', 'jpeg'), BL::err('JPGGIFAndPNGOnly')); $this->frm->getField('image')->isAllowedMimeType(array('image/jpg', 'image/png', 'image/gif', 'image/jpeg'), BL::err('JPGGIFAndPNGOnly')); } } // validate meta $this->meta->validate(); if ($this->frm->isCorrect()) { // build item $item['id'] = (int) BackendBlogModel::getMaximumId() + 1; $item['meta_id'] = $this->meta->save(); $item['category_id'] = (int) $this->frm->getField('category_id')->getValue(); $item['user_id'] = $this->frm->getField('user_id')->getValue(); $item['language'] = BL::getWorkingLanguage(); $item['title'] = $this->frm->getField('title')->getValue(); $item['introduction'] = $this->frm->getField('introduction')->getValue(); $item['text'] = $this->frm->getField('text')->getValue(); $item['publish_on'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($this->frm->getField('publish_on_date'), $this->frm->getField('publish_on_time'))); $item['created_on'] = BackendModel::getUTCDate(); $item['edited_on'] = $item['created_on']; $item['hidden'] = $this->frm->getField('hidden')->getValue(); $item['allow_comments'] = $this->frm->getField('allow_comments')->getChecked() ? 'Y' : 'N'; $item['num_comments'] = 0; $item['status'] = $status; if ($this->imageIsAllowed) { // the image path $imagePath = FRONTEND_FILES_PATH . '/blog/images'; // validate the image if ($this->frm->getField('image')->isFilled()) { // build the image name $item['image'] = $this->meta->getURL() . '.' . $this->frm->getField('image')->getExtension(); // upload the image $this->frm->getField('image')->moveFile($imagePath . '/source/' . $item['image']); } } // insert the item $item['revision_id'] = BackendBlogModel::insert($item); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_add', array('item' => $item)); // save the tags BackendTagsModel::saveTags($item['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule()); // active if ($item['status'] == 'active') { // add search index BackendSearchModel::saveIndex($this->getModule(), $item['id'], array('title' => $item['title'], 'text' => $item['text'])); // ping if (BackendModel::getModuleSetting($this->getModule(), 'ping_services', false)) { BackendModel::ping(SITE_URL . BackendModel::getURLForBlock('blog', 'detail') . '/' . $this->meta->getURL()); } // everything is saved, so redirect to the overview $this->redirect(BackendModel::createURLForAction('index') . '&report=added&var=' . urlencode($item['title']) . '&highlight=row-' . $item['revision_id']); } elseif ($item['status'] == 'draft') { // everything is saved, so redirect to the edit action $this->redirect(BackendModel::createURLForAction('edit') . '&report=saved-as-draft&var=' . urlencode($item['title']) . '&id=' . $item['id'] . '&draft=' . $item['revision_id'] . '&highlight=row-' . $item['revision_id']); } } } }