public function indexAction()
 {
     // To do before anything else
     $this->initPage();
     // Action parameters
     $page = $this->getRequest()->getParam("page");
     $tab = $this->getRequest()->getParam("tab");
     // A bit of filtering
     $page = $page >= 0 ? $page : 0;
     $count = 50;
     // Get all the items; if we are an admin, we also get the hidden one
     $mentions = new Mentions();
     $items = $mentions->getLastMentions($count, $page * $count, $this->_admin);
     $this->view->items = $items;
     // Prepare the common elements
     $this->common();
     // Add js controler
     $this->view->headScript()->appendFile('js/controllers/timeline.js');
     // Add description
     if ($description = $this->_page_properties->getProperty('description')) {
         $this->view->description = $description;
     }
     // Add paging
     $this->view->count = $count;
     $this->view->page = $page;
     $this->view->hasprevious = $page > 0 ? true : false;
     $this->view->hasnext = count($items) >= $count ? true : false;
     $this->view->nextlink = "home?tab={$tab}&page=" . ($page + 1);
     $this->view->previouslink = "home?tab={$tab}&page=" . ($page - 1);
 }
 public function indexAction()
 {
     // Get, check and setup the parameters
     $source_id = $this->getRequest()->getParam("source");
     $item_id = $this->getRequest()->getParam("item");
     //Verify if the requested item exist
     $data = new Data();
     if (!($item = $data->getItem($source_id, $item_id))) {
         return;
     }
     // Get the source
     $sources = new Sources();
     $source = $sources->getSource($source_id);
     // Are we the owner of these comments ?
     $owner = $this->_application->user && $source['user_id'] == $this->_application->user->id ? true : false;
     // Reset to the system timezone; just in case
     $config = Zend_Registry::get("configuration");
     $server_timezone = $config->web->timezone;
     date_default_timezone_set($server_timezone);
     // Get the mentions
     $m = new Mentions();
     $mentions = $m->getMentions($source_id, $item_id);
     foreach ($mentions as &$mention) {
         $mention['when'] = $mention['timestamp'];
         $mention['delete'] = $owner;
         $mention['type'] = "mention";
     }
     // Get the comments
     $c = new Comments();
     $comments = $c->getComments($source_id, $item_id);
     foreach ($comments as &$comment) {
         $comment['when'] = strtotime($comment['timestamp']);
         $comment['comment'] = str_replace("\n", " <br />", $comment['comment']);
         $comment['type'] = "comment";
         $comment['delete'] = $owner;
     }
     // Set the timezone to the user timezone
     $timezone = $this->_properties->getProperty('timezone');
     date_default_timezone_set($timezone);
     // Merged items
     $all = array_merge($mentions, $comments);
     usort($all, 'cmp');
     // Prepare the view
     $this->view->mentions = $mentions;
     $this->view->comments = $comments;
     $this->view->all = $all;
 }
 public function deleteAction()
 {
     // Get, check and setup the parameters
     $mention_id = $this->getRequest()->getParam("id");
     // Get the mention and source tables
     $mentions = new Mentions();
     // Check if the mention exist
     if (!($mention = $mentions->getMention($mention_id))) {
         return $this->_helper->json->sendJson(true);
     }
     // Check if we are the owner of the mention
     if (!($mention['user_id'] == $this->_application->user->id)) {
         return $this->_helper->json->sendJson(true);
     }
     // All checks ok, we can delete !
     $mentions->deleteMention($mention_id);
     return $this->_helper->json->sendJson(false);
 }
 private function process($source, $target)
 {
     $this->_logger->log("Processing pingback from {$source} for {$target}", Zend_Log::INFO);
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $source);
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_USERAGENT, 'Storytlr/1.0');
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
     $response = curl_exec($curl);
     $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     curl_close($curl);
     if ($http_code != 200) {
         $this->_logger->log("Failed to get content for {$source}", Zend_Log::DEBUG);
         return;
     }
     // Parse the source page for microformats content
     $parser = new Parser($response);
     $output = $parser->parse();
     $this->_logger->log("Parsed output: " . var_export($output, true), Zend_Log::DEBUG);
     // Extract relevant data
     $hcards = array();
     $hentries = array();
     $this->processItems($output["items"], $hcards, $hentries);
     $this->_logger->log("Extracted hcards: " . var_export($hcards, true), Zend_Log::DEBUG);
     $this->_logger->log("Extracted hentries: " . var_export($hentries, true), Zend_Log::DEBUG);
     // Lookup if existing entry
     preg_match('/(?P<source>\\d+)\\-(?P<item>\\d+)\\.html$/', $target, $matches);
     $this->_logger->log("Matches: " . var_export($matches, true), Zend_Log::DEBUG);
     $source_id = $matches["source"];
     $item_id = $matches["item"];
     // Get the source and the user owning it
     $data = new Data();
     $sources = new Sources();
     $users = new Users();
     // Does it relate to an item ?
     if ($source_id && $item_id) {
         $s = $sources->getSource($source_id);
         $i = $data->getItem($source_id, $item_id);
         if ($s && $i) {
             $user = $users->getUser($s['user_id']);
         }
     }
     // Otherwise, can we relate to a user ?
     if (!$user) {
         $user = $this->lookupUser($target);
     }
     // No user ? We have to giveup
     if (!$user) {
         throw new Exception('Failed to find corresponding storytlr user.');
     }
     // Is this a h-entry ?
     if (count($hentries) > 0) {
         $hentry = $hentries[0];
         if (count($hentry["author"]) > 0) {
             $hcard = $hentry["author"][0];
         }
     }
     // If no hcard yet (maybe there was no h-entry, we grab the top-level one
     if (!$hcard && count($hcards) > 0) {
         $hcard = $hcards[0];
     }
     // Find the published date
     if ($hentry && $hentry["published"]) {
         $timestamp = strtotime($hentry["published"]);
     }
     // If no timestamp, we fallback to now
     if (!$timestamp) {
         $timestamp = time();
     }
     // Add the mention to the database
     try {
         $mentions = new Mentions();
         $mentions->addMention($source_id, $item_id, $user->id, $source, $hentry["title"], $hcard["name"], $hcard["url"], "", $hcard["avatar"], $timestamp);
     } catch (Exception $e) {
         $this->_logger->log("Exception when storing mention: " . $e->getMessage(), Zend_Log::ERR);
         return;
     }
     // Send an email alert to owner
     try {
         $on_comment = $this->_properties->getProperty('on_comment');
         if ($on_comment) {
             Stuffpress_Emails::sendMentionEmail($user->email, $user->username, $hcard["name"], $hcard["url"], $hentry["title"], $source, $target);
         }
     } catch (Exception $e) {
         $this->_logger->log("Sending mention notification exception: " . $e->getMessage(), Zend_Log::ERR);
     }
 }