Esempio n. 1
0
 /**
  * getTotalBlogPosts
  * @return int the total number of blog posts for the blog
  */
 function getBlogInfo($blogname = null, $useStored = false)
 {
     $info = array('count' => 0, 'description' => '', 'last_post_id' => 0, 'updated' => 0, 'last_post' => '', 'name' => '', 'uuid' => '', 'id' => '');
     if ($blogname == null) {
         $params =& JComponentHelper::getParams('com_wordbridge');
         $blogname = trim($params->get('wordbridge_blog_name'));
     }
     if (empty($blogname) || !function_exists('curl_init')) {
         return $info;
     }
     $info['name'] = $blogname;
     $stored_blog = WordbridgeHelper::getBlogByName($blogname);
     if ($stored_blog && $useStored) {
         return $stored_blog;
     }
     // Keep the uuid constant, or create a new one
     if ($stored_blog) {
         $info['uuid'] = $stored_blog['uuid'];
     } else {
         $info['uuid'] = uniqid();
     }
     // Only use the twitter API for blogs hosted at wordpress.com
     $fqdn = WordbridgeHelper::fqdnBlogName($blogname);
     if (substr($fqdn, -14) == '.wordpress.com') {
         $url = sprintf('http://twitter-api.wordpress.com/users/show.xml?screen_name=%s', urlencode($fqdn));
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $url);
         $xml = WordbridgeHelper::curl_redir_exec($curl);
         curl_close($curl);
         if (empty($xml)) {
             return $info;
         }
         $doc = new DOMDocument();
         $doc->loadXML($xml);
         $info['count'] = $doc->getElementsByTagName('statuses_count')->item(0)->textContent;
         $info['description'] = $doc->getElementsByTagName('description')->item(0)->textContent;
         $info['id'] = $doc->getElementsByTagName('id')->item(0)->textContent;
         // Get the last post information, removing the blog ID that
         // comes out when using the twitter API
         $info['last_post_id'] = $doc->getElementsByTagName('status')->item(0)->getElementsByTagName('id')->item(0)->textContent;
         $info['last_post_id'] = (int) substr($info['last_post_id'], strlen($info['id']));
         $info['last_post'] = $doc->getElementsByTagName('status')->item(0)->getElementsByTagName('text')->item(0)->textContent;
     } else {
         if (preg_match("/^https?:\\/\\/.+/", $fqdn)) {
             $url = sprintf('%s/?feed=wordbridge', $fqdn);
         } else {
             $url = sprintf('http://%s/?feed=wordbridge', $fqdn);
         }
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $url);
         $xml = WordbridgeHelper::curl_redir_exec($curl);
         curl_close($curl);
         #echo htmlspecialchars(print_r($xml, 1));
         if (empty($xml)) {
             return $info;
         }
         $doc = new DOMDocument();
         $doc->loadXML($xml);
         $info['count'] = $doc->getElementsByTagName('count')->item(0)->textContent;
         $info['description'] = $doc->getElementsByTagName('description')->item(0)->textContent;
         $info['id'] = $doc->getElementsByTagName('id')->item(0)->textContent;
         // Get the last post information
         $info['last_post_id'] = $doc->getElementsByTagName('last_post_id')->item(0)->textContent;
         $info['last_post'] = $doc->getElementsByTagName('last_post')->item(0)->textContent;
     }
     // Update the stored blog basic details if need be
     if (!empty($info['description'])) {
         if ($stored_blog) {
             if ($stored_blog['description'] != $info['description'] || $stored_blog['name'] != $blogname || $stored_blog['last_post'] != $info['last_post']) {
                 WordbridgeHelper::storeBlog($info['id'], $info['uuid'], $blogname, $info['description'], $info['last_post']);
             }
         } else {
             // Store the blog data locally
             WordbridgeHelper::storeBlog($info['id'], $info['uuid'], $blogname, $info['description'], $info['last_post']);
         }
     }
     return $info;
 }