Ejemplo n.º 1
0
function atom_save($document)
{
    global $config, $javascript_msg;
    // Split up the post to get the title and content separately
    $div = strpos($document, "\n");
    $title = trim(substr($document, 0, $div));
    $content = substr($document, $div + 1);
    // Prep these elements for use in the Atom objects later
    $title = array('title' => htmlspecialchars($title), 'mode' => 'escaped', 'type' => 'text/html');
    $content = array('content' => htmlspecialchars($content), 'mode' => 'escaped', 'type' => 'text/html');
    // Create Auth Object
    if ($config['plugins'][$_SESSION['plugin']]['type'] == 'blogger') {
        require_once dirname(__FILE__) . '/class.basicauth.php';
        $auth = new BasicAuth($config['plugins'][$_SESSION['plugin']]['username'], $config['plugins'][$_SESSION['plugin']]['password']);
    } else {
        require_once dirname(__FILE__) . '/class.wsse.php';
        $auth = new WSSE($config['plugins'][$_SESSION['plugin']]['username'], $config['plugins'][$_SESSION['plugin']]['password']);
    }
    // Break down the identifier details into its parts
    if (preg_match('/^\\[(https?)?.*@https?.*\\]$/Ui', $_SESSION['plugin_identifier'])) {
        $at = strpos($_SESSION['plugin_identifier'], '@');
        $entry_uri = substr($_SESSION['plugin_identifier'], 1, $at - 1);
        if (trim($entry_uri) == '') {
            $entry_uri = false;
        }
        $feed_uri = substr($_SESSION['plugin_identifier'], $at + 1, -1);
    } else {
        // Couldn't figure out where to save to
        $javascript_msg = '@Couldn\'t locate the blog to save this post to.';
        return $_SESSION['filename'];
    }
    // If we're updating an existing one, we need some details
    if ($entry_uri !== false) {
        // Create the new entry and get it as XML
        $ae = new AtomEntry($title, $content);
        $ae = $ae->to_xml('PUT');
        $ar = new AtomRequest('PUT', urldecode($entry_uri), $auth, $ae);
        $response = $ar->exec();
        if ($response == 200) {
            $javascript_msg = 'Post saved successfully.';
            return;
        } else {
            $javascript_msg = '@Saving your post failed, please try again. (' . $ar->get_response() . ')';
            return;
        }
    } else {
        // Make the entry, and get it in XML (for POSTing)
        $ae = new AtomEntry($title, $content);
        $ae = $ae->to_xml('POST');
        $ar = new AtomRequest('POST', urldecode($feed_uri), $auth, $ae);
        $response = $ar->exec();
        if ($response == 200 || $response == 201) {
            // Need to get the EditURI for this new post now
            $ae = new AtomEntry();
            $ae->from_xml($ar->get_response());
            $link = $ae->get_links('rel', 'service.edit');
            $javascript_msg = 'Post saved successfully.';
            $_SESSION['plugin_identifier'] = '[' . urlencode($link[0]['href']) . '@' . $feed_uri . ']';
            return;
        } else {
            $javascript_msg = '@Saving your post failed, please try again. (' . $ar->get_response() . ')';
            return;
        }
    }
}
Ejemplo n.º 2
0
 /**
  * @return void
  * @param String $xml
  * @desc Populates the variables of the AtomFeed, based on a complete XML representation of it.
  */
 function from_xml($xml)
 {
     $orig_xml = $xml;
     // Strip down the XML to just the part we want to work with
     if (preg_match('/(<feed.*)<entry/sUi', $xml, $feed_xml)) {
         $xml = $feed_xml[1];
     }
     // ATOM FEED VERSION
     if (preg_match('/<feed[^>]+version="([^"]*)"/is', $xml, $ver)) {
         $this->set_version($ver[1]);
     }
     // TITLE
     if (preg_match_all('/<title([^>]*)>(.*)<\\/title>/sUi', $xml, $title)) {
         $title_attribs = $this->extract_attribs($title[1][0]);
         $title = array('title' => $title[2][0]);
         $title = array_merge_recursive($title, $title_attribs);
         $this->set_title($title);
     }
     // TAGLINE
     if (preg_match_all('/<tagline([^>]*)>(.*)<\\/tagline>/sUi', $xml, $tagline)) {
         $tagline_attribs = $this->extract_attribs($tagline[1][0]);
         $tagline = array('tagline' => $tagline[2][0]);
         $tagline = array_merge_recursive($tagline, $tagline_attribs);
         $this->set_tagline($tagline);
     }
     // ID
     if (preg_match('/<id>([^<]*)<\\/id>/is', $xml, $id)) {
         $this->set_id($id[1]);
     }
     // INFO
     if (preg_match('/<info([^>]+)>(.*)<\\/info>/is', $xml, $info)) {
         $info_attribs = $this->extract_attribs($info[1]);
         $info = array('info' => $info[2]);
         $info = array_merge_recursive($info, $info_attribs);
         $this->set_info($info);
     }
     // MODIFIED
     if (preg_match('/<modified>([^<]*)<\\/modified>/is', $xml, $modified)) {
         $this->set_modified($modified[1]);
     }
     // GENERATOR
     if (preg_match_all('/<generator([^>]*)>(.*)<\\/generator>/sUi', $xml, $generator)) {
         $generator_attribs = $this->extract_attribs($generator[1][0]);
         $generator = array('generator' => $generator[2][0]);
         $generator = array_merge_recursive($generator, $generator_attribs);
         $this->set_generator($generator);
     }
     // LINKS
     if (preg_match_all('/<link([^>]+)>/Ui', $xml, $links)) {
         foreach ($links[1] as $link) {
             $link = $this->extract_attribs($link);
             $this->add_link($link['href'], $link['rel'], $link['title'], $link['type']);
         }
     }
     // Handle all of the entries, creating AtomEntry objects and linking them
     preg_match_all('/(<entry[^>]*>.*<\\/entry>)/sUi', $orig_xml, $entries_raw);
     if (strlen($entries_raw[0][0])) {
         foreach ($entries_raw[1] as $e => $entry) {
             $ae = new AtomEntry();
             $ae->from_xml($entry);
             if ($ae) {
                 $this->add_entry($ae);
             }
         }
     }
 }