Example #1
0
 function run($dbi, $argstr, &$request, $basepage)
 {
     extract($this->getArgs($argstr, $request));
     $parser = new AtomParser();
     assert(!empty($url));
     $parser->parse_url($url);
     $html = '';
     $items = HTML::dl();
     foreach ($parser->feed as $feed) {
         $title = HTML::h3(HTML::a(array('href' => $feed["links"]["0"]["href"]), $feed["title"]));
         $counter = 1;
         foreach ($parser->entries as $entry) {
             $item = HTML::dt(HTML::a(array('href' => $entry["links"]["0"]["href"]), $entry["title"]));
             $items->pushContent($item);
             if (!$titleonly) {
                 $description = HTML::dd(HTML::raw(html_entity_decode($entry["content"])));
             } else {
                 $description = HTML::dd();
             }
             $items->pushContent($description);
             if ($maxitem > 0 && $counter >= $maxitem) {
                 break;
             }
             $counter++;
         }
         $html = HTML::div(array('class' => 'rss'), $title);
         $html->pushContent($items);
     }
     return $html;
 }
Example #2
0
 /**
  * Update attachment.
  *
  * @since 2.2.0
  *
  * @param int $postID Post ID.
  */
 function put_attachment($postID)
 {
     // checked for valid content-types (atom+xml)
     // quick check and exit
     $this->get_accepted_content_type($this->atom_content_types);
     $parser = new AtomParser();
     if (!$parser->parse()) {
         $this->bad_request();
     }
     $parsed = array_pop($parser->feed->entries);
     // check for not found
     global $entry;
     $this->set_current_entry($postID);
     if (!current_user_can('edit_post', $entry['ID'])) {
         $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
     }
     extract($entry);
     $post_title = $parsed->title[1];
     $post_content = $parsed->summary[1];
     $pubtimes = $this->get_publish_time($parsed->updated);
     $post_modified = $pubtimes[0];
     $post_modified_gmt = $pubtimes[1];
     $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt');
     $this->escape($postdata);
     $result = wp_update_post($postdata);
     if (!$result) {
         $this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
     }
     log_app('function', "put_attachment({$postID})");
     $this->ok();
 }
Example #3
0
 function import_blog($blogID)
 {
     global $importing_blog;
     $importing_blog = $blogID;
     if (isset($_GET['authors'])) {
         return print $this->get_author_form();
     }
     header('Content-Type: text/plain');
     if (isset($_GET['status'])) {
         die($this->get_js_status());
     }
     if (isset($_GET['saveauthors'])) {
         die($this->save_authors());
     }
     $blog = $this->blogs[$blogID];
     $total_results = $this->get_total_results('posts', $blog['host']);
     $this->blogs[$importing_blog]['total_posts'] = $total_results;
     $start_index = $total_results - MAX_RESULTS + 1;
     if (isset($this->blogs[$importing_blog]['posts_start_index'])) {
         $start_index = (int) $this->blogs[$importing_blog]['posts_start_index'];
     } elseif ($total_results > MAX_RESULTS) {
         $start_index = $total_results - MAX_RESULTS + 1;
     } else {
         $start_index = 1;
     }
     // This will be positive until we have finished importing posts
     if ($start_index > 0) {
         // Grab all the posts
         $this->blogs[$importing_blog]['mode'] = 'posts';
         $query = "start-index={$start_index}&max-results=" . MAX_RESULTS;
         do {
             $index = $struct = $entries = array();
             $headers = array("GET /feeds/posts/default?{$query} HTTP/1.0", "Host: {$blog['host']}", "Authorization: AuthSub token=\"{$this->token}\"");
             $request = join("\r\n", $headers) . "\r\n\r\n";
             $sock = $this->_get_blogger_sock($blog['host']);
             if (!$sock) {
                 return;
             }
             // TODO: Error handling
             $response = $this->_txrx($sock, $request);
             $response = $this->parse_response($response);
             // Extract the entries and send for insertion
             preg_match_all('/<entry[^>]*>.*?<\\/entry>/s', $response['body'], $matches);
             if (count($matches[0])) {
                 $entries = array_reverse($matches[0]);
                 foreach ($entries as $entry) {
                     $entry = "<feed>{$entry}</feed>";
                     $AtomParser = new AtomParser();
                     $AtomParser->parse($entry);
                     $result = $this->import_post($AtomParser->entry);
                     if (is_wp_error($result)) {
                         return $result;
                     }
                     unset($AtomParser);
                 }
             } else {
                 break;
             }
             // Get the 'previous' query string which we'll use on the next iteration
             $query = '';
             $links = preg_match_all('/<link([^>]*)>/', $response['body'], $matches);
             if (count($matches[1])) {
                 foreach ($matches[1] as $match) {
                     if (preg_match('/rel=.previous./', $match)) {
                         $query = html_entity_decode(preg_replace('/^.*href=[\'"].*\\?(.+)[\'"].*$/', '$1', $match));
                     }
                 }
             }
             if ($query) {
                 parse_str($query, $q);
                 $this->blogs[$importing_blog]['posts_start_index'] = (int) $q['start-index'];
             } else {
                 $this->blogs[$importing_blog]['posts_start_index'] = 0;
             }
             $this->save_vars();
         } while (!empty($query) && $this->have_time());
     }
     $total_results = $this->get_total_results('comments', $blog['host']);
     $this->blogs[$importing_blog]['total_comments'] = $total_results;
     if (isset($this->blogs[$importing_blog]['comments_start_index'])) {
         $start_index = (int) $this->blogs[$importing_blog]['comments_start_index'];
     } elseif ($total_results > MAX_RESULTS) {
         $start_index = $total_results - MAX_RESULTS + 1;
     } else {
         $start_index = 1;
     }
     if ($start_index > 0) {
         // Grab all the comments
         $this->blogs[$importing_blog]['mode'] = 'comments';
         $query = "start-index={$start_index}&max-results=" . MAX_RESULTS;
         do {
             $index = $struct = $entries = array();
             $headers = array("GET /feeds/comments/default?{$query} HTTP/1.0", "Host: {$blog['host']}", "Authorization: AuthSub token=\"{$this->token}\"");
             $request = join("\r\n", $headers) . "\r\n\r\n";
             $sock = $this->_get_blogger_sock($blog['host']);
             if (!$sock) {
                 return;
             }
             // TODO: Error handling
             $response = $this->_txrx($sock, $request);
             $response = $this->parse_response($response);
             // Extract the comments and send for insertion
             preg_match_all('/<entry[^>]*>.*?<\\/entry>/s', $response['body'], $matches);
             if (count($matches[0])) {
                 $entries = array_reverse($matches[0]);
                 foreach ($entries as $entry) {
                     $entry = "<feed>{$entry}</feed>";
                     $AtomParser = new AtomParser();
                     $AtomParser->parse($entry);
                     $this->import_comment($AtomParser->entry);
                     unset($AtomParser);
                 }
             }
             // Get the 'previous' query string which we'll use on the next iteration
             $query = '';
             $links = preg_match_all('/<link([^>]*)>/', $response['body'], $matches);
             if (count($matches[1])) {
                 foreach ($matches[1] as $match) {
                     if (preg_match('/rel=.previous./', $match)) {
                         $query = html_entity_decode(preg_replace('/^.*href=[\'"].*\\?(.+)[\'"].*$/', '$1', $match));
                     }
                 }
             }
             parse_str($query, $q);
             $this->blogs[$importing_blog]['comments_start_index'] = (int) $q['start-index'];
             $this->save_vars();
         } while (!empty($query) && $this->have_time());
     }
     $this->blogs[$importing_blog]['mode'] = 'authors';
     $this->save_vars();
     if (!$this->blogs[$importing_blog]['posts_done'] && !$this->blogs[$importing_blog]['comments_done']) {
         die('nothing');
     }
     do_action('import_done', 'blogger');
     die('done');
 }
Example #4
0
    function testExtensiveAtomExampleFromRFC4287()
    {
        $fake_atom_file = <<<ATOM
<?xml version="1.0" encoding="utf-8"?>

<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text">dive into mark</title>
  <subtitle type="html">
    A &lt;em&gt;lot&lt;/em&gt; of effort
    went into making this effortless
  </subtitle>

  <updated>2005-07-31T12:29:29Z</updated>
  <id>tag:example.org,2003:3</id>
  <link rel="alternate" type="text/html" 
   hreflang="en" href="http://example.org/"/>
  <link rel="self" type="application/atom+xml" 
   href="http://example.org/feed.atom"/>
  <rights>Copyright (c) 2003, Mark Pilgrim</rights>

  <generator uri="http://www.example.com/" version="1.0">
    Example Toolkit
  </generator>
  <entry>
    <title>Atom draft-07 snapshot</title>
    <link rel="alternate" type="text/html" 
     href="http://example.org/2005/04/02/atom"/>

    <link rel="enclosure" type="audio/mpeg" length="1337"
     href="http://example.org/audio/ph34r_my_podcast.mp3"/>
    <id>tag:example.org,2003:3.2397</id>
    <updated>2005-07-31T12:29:29Z</updated>
    <published>2003-12-13T08:29:29-04:00</published>

    <author>
      <name>Mark Pilgrim</name>
      <uri>http://example.org/</uri>
      <email>f8dy@example.com</email>

    </author>
    <contributor>
      <name>Sam Ruby</name>
    </contributor>
    <contributor>

      <name>Joe Gregorio</name>
    </contributor>
    <content type="xhtml" xml:lang="en" 
     xml:base="http://diveintomark.org/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p><i>[Update: The Atom draft is finished.]</i></p>

      </div>
    </content>
  </entry>
</feed>
ATOM;
        $parser = new AtomParser();
        $parser->parse($fake_atom_file);
        $this->assertFalse(count($parser->feed) == 0);
        $current_feed = $parser->feed[0];
        $this->assertEquals("dive into mark", $current_feed["title"]);
        $this->assertEquals("Copyright (c) 2003, Mark Pilgrim", $current_feed["rights"]);
        $this->assertEquals("A <em>lot</em> of effort\n    went into making this effortless", $current_feed["subtitle"]);
        $this->assertEquals("2005-07-31T12:29:29Z", $current_feed["updated"]);
        $this->assertEquals("tag:example.org,2003:3", $current_feed["id"]);
        $this->assertEquals("Example Toolkit", $current_feed["generator"]);
        $this->assertTrue(count($current_feed["authors"]) == 0);
        $this->assertTrue(count($current_feed["contributors"]) == 0);
        $this->assertFalse(count($parser->entries) == 0);
        $current_entry = $parser->entries[0];
        $this->assertEquals("Atom draft-07 snapshot", $current_entry["title"]);
        $this->assertEquals("2005-07-31T12:29:29Z", $current_entry["updated"]);
        $this->assertEquals("2003-12-13T08:29:29-04:00", $current_entry["published"]);
        $this->assertEquals("tag:example.org,2003:3.2397", $current_entry["id"]);
        $this->assertEquals(2, count($current_entry["links"]));
        $this->assertTrue(count($current_entry["authors"]) == 1);
        $this->assertTrue(count($current_entry["contributors"]) == 2);
        $current_author = $current_entry["authors"][0];
        $this->assertEquals("Mark Pilgrim", $current_author["name"]);
        $this->assertEquals("*****@*****.**", $current_author["email"]);
        $first_contributor = $current_entry["contributors"][0];
        $second_contributor = $current_entry["contributors"][1];
        $this->assertEquals("Sam Ruby", $first_contributor["name"]);
        $this->assertEquals("Joe Gregorio", $second_contributor["name"]);
        $first_link = $current_entry["links"][0];
        $this->assertEquals("alternate", $first_link["rel"]);
        $this->assertEquals("text/html", $first_link["type"]);
        $this->assertEquals("http://example.org/2005/04/02/atom", $first_link["href"]);
        $second_link = $current_entry["links"][1];
        $this->assertEquals("enclosure", $second_link["rel"]);
        $this->assertEquals("audio/mpeg", $second_link["type"]);
        $this->assertEquals("1337", $second_link["length"]);
        $this->assertEquals("http://example.org/audio/ph34r_my_podcast.mp3", $second_link["href"]);
        $payload = <<<CONTENT
<div xmlns="http://www.w3.org/1999/xhtml">
        <p><i>[Update: The Atom draft is finished.]</i></p>

      </div>
CONTENT;
        $this->assertEquals($payload, $current_entry["content"]);
    }
	function put_attachment($postID) {

		// checked for valid content-types (atom+xml)
		// quick check and exit
		$this->get_accepted_content_type($this->atom_content_types);

		$parser = new AtomParser();
		if(!$parser->parse()) {
			$this->bad_request();
		}

		$parsed = $parser->entry;

		// check for not found
		global $entry;
		$this->set_current_entry($postID);
		$this->escape($entry);

		if(!current_user_can('edit_post', $entry['ID']))
			$this->auth_required(__('Sorry, you do not have the right to edit this post.'));

		$publish = (isset($parsed->draft) && trim($parsed->draft) == 'yes') ? false : true;

		extract($entry);

		$post_title = $parsed->title;
		$post_content = $parsed->content;

		$postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');

		$result = wp_update_post($postdata);

		if (!$result) {
			$this->internal_error(__('For some strange yet very annoying reason, this post could not be edited.'));
		}

		log_app('function',"put_attachment($postID)");
		$this->ok();
	}
 /**
  * The error returned by OData service will be in Atom format in this case
  * AtomParser will be used to retrive exact error from the atom, if some
  * other error happens for example, if credentials are wrong, in this case
  * IIS will retruns error in plain string format.
  *
  * @param string $errorStr Error String as plain string or Atom format
  * @param string $content_type
  */
 public function ODataServiceException($errorStr, $content_type = '', $headers = array(), $statusCode = '')
 {
     $this->_headers = $headers;
     $this->_statusCode = $statusCode;
     AtomParser::GetErrorDetails($errorStr, $this->_error, $this->_detailedError, $content_type);
 }
Example #7
0
 /**
  * To handle the HttpResponse object of a HttpRequest POST request in
  * non-batching mode. This function will invoke ObjectContext::AttachLocation
  * which uses the value of HttpHeader with key 'Location' to set the Identity
  * and EditLink of current entry (ResourceBox) under process. Also populate the
  * entity instance with OData service returned values.
  *
  * @param HttpResponse $httpResponse
  */
 protected function HandleOperationResponse($httpResponse)
 {
     $resourceBox = $this->_changedEntries[$this->_entryIndex];
     if ($resourceBox->IsResource()) {
         $headers = $httpResponse->getHeaders();
         //Handle the POST Operation Response.
         //SDK will fire POST operation in three cases
         //1. AddLink [we will skip this case]
         //2. AddObject [$resourceBox->State == EntityStates::Added]
         //3. SetSaveStream on an entity which is just added using AddObject
         //   In this case the CheckAndProcessMediaEntryPost will set the state
         //   of the object to Modified.
         //   [($resourceBox->State == EntityStates::Modified) &&
         //      $this->_processingMediaLinkEntry) &&
         //     !$this->_processingMediaLinkEntryPut)]
         if ($resourceBox->State == EntityStates::Added || $resourceBox->State == EntityStates::Modified && $this->_processingMediaLinkEntry && !$this->_processingMediaLinkEntryPut) {
             $resourceBox->EntityETag = AtomParser::GetEntityEtag($httpResponse->getBody());
             $location = isset($headers[HttpRequestHeader::Location]) ? $headers[HttpRequestHeader::Location] : null;
             if ($httpResponse->isSuccessful()) {
                 if ($location == null) {
                     throw new ODataServiceException(Resource::NoLocationHeader, '', array(), null);
                 }
                 $this->_context->AttachLocation($resourceBox->getResource(), $location);
                 if ($resourceBox->State == EntityStates::Added) {
                     $atomEntry = null;
                     AtomParser::PopulateObject($httpResponse->getBody(), $resourceBox->getResource(), $uri, $atomEntry);
                 } else {
                     //After the POST operation for a media, state of corrosponding entity will be
                     //updated to Modified [earlier it will be Added] in CheckAndProcessMediaEntryPost
                     //function. So next will be a MERGE request, while generating body for this
                     //MERGE operation, the function CreateChangeSetBodyForResource will throw error
                     //if any of the Key field is null. So update the Key fields.
                     AtomParser::PopulateMediaEntryKeyFields($httpResponse->getBody(), $resourceBox->getResource());
                 }
             }
         }
         if ($this->_processingMediaLinkEntry && !$httpResponse->isSuccessful()) {
             $this->_processingMediaLinkEntry = false;
             if ($this->_processingMediaLinkEntryPut) {
                 $resourceBox->State = EntityStates::Added;
                 $this->_processingMediaLinkEntryPut = false;
             }
         }
     }
 }
Example #8
0
 /**
  * To update the $resourceBox::Source object by parsing the atom XML in $str.
  * [Note: Do not call this function from your application, it is used internally]
  *
  * @param string $str
  * @param ResourceBox $resourceBox
  * @param string $content_type
  */
 public function LoadResourceBox($str, $resourceBox, $content_type)
 {
     $resource = $resourceBox->GetResource();
     $uri = null;
     $atomEntry = null;
     AtomParser::PopulateObject($str, $resource, $uri, $atomEntry);
     if (isset($uri)) {
         $index = Utility::reverseFind($uri, '/');
         $editLink = substr($uri, $index + 1, strlen($uri) - $index);
         $resourceBox->Identity = $uri;
         $resourceBox->EditLink = $editLink;
     }
     //If $str represents content of entry of type Media then
     //popluate values specific to media entry
     $resourceBox->EditMediaLink = $atomEntry->EditMediaLink;
     $resourceBox->MediaLinkEntry = $atomEntry->MediaLinkEntry;
     $resourceBox->StreamETag = $atomEntry->StreamETag;
     $resourceBox->EntityETag = $atomEntry->EntityETag;
     $resourceBox->StreamLink = $atomEntry->MediaContentUri;
 }