Example #1
0
 /**
  * @expectedException SpoonFileException
  */
 public function testDownloadFailure()
 {
     SpoonFile::download($this->nonExistingUrl, $this->destinationFile);
 }
 /**
  * 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;
 }