public function indexAction() { // Get, check and setup the parameters if (!($widget_id = $this->getRequest()->getParam("id"))) { throw new Stuffpress_Exception("No widget id provided to the widget controller"); } // Verify if the requested widget exist and get its data $widgets = new Widgets(); if (!($widget = $widgets->getWidget($widget_id))) { throw new Stuffpress_Exception("Invalid widget id"); } // Get the last comments $comments = new Comments(array(Stuffpress_Db_Table::USER => $widget['user_id'])); $mycomments = $comments->getLastComments(); $data = new Data(); // Prepare the comments for output foreach ($mycomments as &$comment) { $time = strtotime($comment['timestamp']); $item = $data->getItem($comment['source_id'], $comment['item_id']); $comment['item'] = $item; $comment['when'] = Stuffpress_Date::ago($time, "j M y"); $comment['comment'] = str_replace("\n", " ", $comment['comment']); if (strlen($comment['comment']) > 50) { $comment['comment'] = mb_substr($comment['comment'], 0, 47) . "..."; } } // Get the widget properties $properties = new WidgetsProperties(array(Properties::KEY => $widget_id)); $title = $properties->getProperty('title'); $this->view->title = $title ? $title : "Latest comments"; // Prepare the view for rendering $this->view->comments = $mycomments; }
protected function updateTwitter($items) { // Get the user $users = new Users(); $shortUrl = new ShortUrl(); $user = $users->getUser($this->getUserID()); // Get twitter consumer tokens and user secrets $config = Zend_Registry::get("configuration"); $consumer_key = $config->twitter->consumer_key; $consumer_secret = $config->twitter->consumer_secret; // Get twitter credentials $properties = new Properties(array(Properties::KEY => $user->id)); $auth = $properties->getProperty('twitter_auth'); $services = $properties->getProperty('twitter_services'); $oauth_token = $properties->getProperty('twitter_oauth_token'); $oauth_token_secret = $properties->getProperty('twitter_oauth_token_secret'); if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) { return; } $has_preamble = $properties->getProperty('preamble', true); // Return if not all conditions are met if (!$auth || !in_array($this->getID(), unserialize($services))) { return; } // Get an item $count = count($items); $data = new Data(); $source_id = $this->_source['id']; if ($count <= 3) { foreach($items as $id) { $item = $data->getItem($source_id, $id); $title = strip_tags($item->getTitle()); $service = $this->getServiceName(); if (($item->getType() == SourceItem::STATUS_TYPE ) && strlen($title) < 140) { $tweet = $title; } else { $preamble = $has_preamble ? $item->getPreamble() : ""; $tweet = $preamble . $title; $url = $users->getUrl($user->id, "s/" . $shortUrl->shorten($item->getSlug())); if (strlen($tweet) + strlen($url) > 140) { $tweet = substr($tweet, 0, 140 - strlen($url) - 5) . "... $url"; } else { $tweet = "$tweet $url"; } } try { $connection = new TwitterOAuth_Client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); $response = $connection->post('statuses/update', array('status' => $tweet)); } catch (Exception $e) {} } } else { $url = $users->getUrl($user->id); $tweet = sprintf($this->_update_tweet, $count, $url); try { $twitter = new Stuffpress_Services_Twitter($username, $password); $twitter->sendTweet($tweet); } catch (Exception $e) {} } }
protected function updateTwitter($items) { // Get the user $users = new Users(); $user = $users->getUser($this->getUserID()); // Get twitter credentials $properties = new Properties(array(Properties::KEY => $user->id)); $auth = $properties->getProperty('twitter_auth'); $services = $properties->getProperty('twitter_services'); $username = $properties->getProperty('twitter_username'); $password = $properties->getProperty('twitter_password'); $has_preamble = $properties->getProperty('preamble', true); // Return if not all conditions are met if (!$auth || !in_array($this->getID(), unserialize($services))) { return; } // Get an item $count = count($items); $data = new Data(); $source_id = $this->_source['id']; if ($count <= 3) { foreach ($items as $id) { $item = $data->getItem($source_id, $id); $title = strip_tags($item->getTitle()); $service = $this->getServiceName(); if ($item->getType() == SourceItem::STATUS_TYPE && strlen($title) < 140) { $tweet = $title; } else { $preamble = $has_preamble ? $item->getPreamble() : ""; $tweet = $preamble . $title; $url = $users->getUrl($user->id, "/entry/" . $item->getSlug()); if (strlen($tweet) + strlen($url) > 140) { $tweet = substr($tweet, 0, 140 - strlen($url) - 5) . "... {$url}"; } else { $tweet = "{$tweet} {$url}"; } } try { $twitter = new Stuffpress_Services_Twitter($username, $password); $twitter->sendTweet($tweet); } catch (Exception $e) { } } } else { $url = $users->getUrl($user->id); $tweet = sprintf($this->_update_tweet, $count, $url); try { $twitter = new Stuffpress_Services_Twitter($username, $password); $twitter->sendTweet($tweet); } catch (Exception $e) { } } }
private function editItem($source_id, $item_id, $values) { //Verify if the requested item exist $data = new Data(); if (!($item = $data->getItem($source_id, $item_id))) { throw new Stuffpress_NotFoundException("This item does not exist."); } // Get the user $users = new Users(); $attributes = $item->getAttributes(); $user = $users->getUser($attributes['user_id']); // Check if we are the owner if ($this->_application->user->id != $user->id) { throw new Stuffpress_NotFoundException("Not the owner"); } // Process the date if available if (@$values['date_type'] == 'other') { $timestamp = Stuffpress_Date::strToTimezone($values['date'], $this->_properties->getProperty('timezone')); } else { $timestamp = time(); } // Process the tags if available $tag_input = @$values['tags']; $tags = explode(',',$tag_input); $data->setTags($source_id, $item_id, $tags); // Process the location if available $latitude = @$values['latitude']; $longitude = @$values['longitude']; if ($latitude && $longitude) { $data->setLocation($source_id, $item_id, $latitude, $longitude, 0); } // Update the item data $data->setTimestamp($source_id, $item_id, $timestamp); switch ($item->getType()) { case SourceItem::STATUS_TYPE: $status = html_entity_decode(@$values['title']); $item->setStatus($status); break; case SourceItem::BLOG_TYPE: $title = @$values['title']; $text = @$values['text']; $item->setTitle($title); $item->setContent($text); break; case SourceItem::LINK_TYPE: $title = @$values['title']; $link = @$values['link']; $desc = @$values['text']; $item->setTitle($title); $item->setLink($link); $item->setDescription($desc); break; case SourceItem::IMAGE_TYPE: $title = @$values['title']; $desc = @$values['text']; $item->setTitle($title); $item->setDescription($desc); break; case SourceItem::AUDIO_TYPE: $title = @$values['title']; $desc = @$values['text']; $item->setTitle($title); $item->setDescription($desc); break; case SourceItem::VIDEO_TYPE: $title = @$values['title']; $desc = @$values['text']; $item->setTitle($title); $item->setDescription($desc); break; } // Send notification if twitter post is enabled if ($this->_properties->getProperty('twitter_auth') && $values['twitter_notify']) { $this->notifyTwitter($item); } // Redirect to the timeline $host = $this->getHostname(); $username = $this->_application->user->username; $url = $this->getUrl($username, "/entry/" . $item->getSlug()); return $this->_redirect($url); }
private function notifyTwitter($item_id, $source_id) { // Get twitter credentials $username = $this->_properties->getProperty('twitter_username'); $password = $this->_properties->getProperty('twitter_password'); $preamble = $this->_properties->getProperty('preamble', true); // Get item $data = new Data(); $item = $data->getItem($source_id, $item_id); $pream = $preamble ? $item->getPreamble() : ""; $title = $pream . $item->getTitle(); // Assemble tweet depending on type if ($item->getType() == SourceItem::STATUS_TYPE && strlen($title) < 140) { $tweet = $title; } else { if (strlen($title) > 121) { $title = substr($title, 0, 117) . "[..]"; } $db_ShortUrls = new ShortUrls(); $hash = $db_ShortUrls->addUrlForItem($this->_user->id, $source_id, $item_id); $tweet = "{$title} http://st.tl/{$hash}"; } try { $twitter = new Stuffpress_Services_Twitter($username, $password); $twitter->sendTweet($tweet); } catch (Exception $e) { throw new Stuffpress_Exception("Twitter notification generated exception {$e}"); } }
public function setcoverAction() { // Get, check and setup the parameters $story_id = $this->getRequest()->getParam("story"); $source_id = $this->getRequest()->getParam("source"); $item_id = $this->getRequest()->getParam("item"); //Verify if the requested story exist $stories = new Stories(); if (!($story = $stories->getStory($story_id))) { return $this->_helper->json->sendJson(false); } // Check if we are the owner if ($this->_application->user->id != $story->user_id) { return $this->_helper->json->sendJson(false); } //Verify if the requested source exist $sources = new Sources(); if (!($source = $sources->getSource($source_id))) { return $this->_helper->json->sendJson(false); } // Check if we are the owner if ($this->_application->user->id != $source['user_id']) { return $this->_helper->json->sendJson(false); } //Verify if the requested item exist $data = new Data(); if (!($item = $data->getItem($source_id, $item_id))) { return $this->_helper->json->sendJson(false); } // Do we have a url ? if (!($url = $item->getImageUrl(ImageItem::SIZE_MEDIUM))) { return $this->_helper->json->sendJson(false); } // Get the old image and delete it $files = new Files(); if ($file = $files->getFileFromKey($story->thumbnail)) { $files->deleteFile($file->key); } // Ok, we can set the image $this->setKeyImage($story_id, $url); return $this->_helper->json->sendJson(true); }
public function settitleAction() { // Get, check and setup the parameters $source_id = $this->getRequest()->getParam("source"); $item_id = $this->getRequest()->getParam("item"); $title = $this->getRequest()->getParam("value"); //Verify if the requested item exist $data = new Data(); if (!($item = $data->getItem($source_id, $item_id))) { return $this->_helper->json->sendJson(false); } // Get the user $users = new Users(); $user = $users->getUser($item->getUserid()); // Check if we are the owner if ($this->_application->user->id != $user->id) { return $this->_helper->json->sendJson(false); } // Ok, we can change the title $item->setTitle($title); // Return the new title if (!$title) { $title = '[Click to add text]'; } die($title); }
public function formAction() { // 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; } // Are we logged in and the owner of the item ? $owner = $this->_application->user && $item->getUserid() == $this->_application->user->id ? true : false; // Get the form $this->view->source_id = $source_id; $this->view->item_id = $item_id; $this->view->form = $this->getForm($source_id, $item_id, $owner); }
/** * @param array $data * @return StuffpressItem */ protected function _updateItem($data, $source_id, $item_id) { // Update the item $source = StuffpressModel::forUser($this->_application->user->id); $data_table = new Data(); $source->updateItem($item_id, $data, $data['published']); // Fetch the newly updated item $item = $data_table->getItem($source_id, $item_id); return $item; }
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); } }