public function testOutput()
 {
     //not logged in
     $controller = new TwitterPluginHashtagConfigurationController(null, 'twitter', 'ginatrapani');
     $output = $controller->go();
     $v_mgr = $controller->getViewManager();
     $config = Config::getInstance();
     $this->assertEqual('You must <a href="' . $config->getValue('site_root_path') . 'session/login.php">log in</a> to do this.', $v_mgr->getTemplateDataItem('error_msg'));
     //logged in, no user set up
     $this->simulateLogin('*****@*****.**');
     $owner_dao = DAOFactory::getDAO('OwnerDAO');
     $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     $controller = new TwitterPluginHashtagConfigurationController(null, 'twitter', 'ginatrapani');
     $output = $controller->go();
     $v_mgr = $controller->getViewManager();
     $this->assertIsA($v_mgr->getTemplateDataItem('user'), 'string');
     $this->assertEqual('Twitter user @ginatrapani does not exist.', $v_mgr->getTemplateDataItem('error_msg'));
     //logged in, user set up
     $builders = array();
     $builders[] = FixtureBuilder::build('instances', array('network_username' => 'ginatrapani', 'network' => 'twitter'));
     $controller = new TwitterPluginHashtagConfigurationController(null, 'twitter', 'ginatrapani');
     $output = $controller->go();
     $v_mgr = $controller->getViewManager();
     $this->assertIsA($v_mgr->getTemplateDataItem('user'), 'string');
     $this->assertIsA($v_mgr->getTemplateDataItem('hashtags'), 'array');
 }
Exemple #2
0
 public function generateInsight(Instance $instance, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     if (self::shouldGenerateInsight('favorited_links', $instance, $insight_date = 'today', $regenerate_existing_insight = true)) {
         $fpost_dao = DAOFactory::getDAO('FavoritePostDAO');
         $favorited_posts = $fpost_dao->getAllFavoritePosts($instance->network_user_id, $instance->network, 40);
         $todays_favorited_posts_with_links = array();
         foreach ($favorited_posts as $post) {
             if (date('Y-m-d', strtotime($post->pub_date)) == date('Y-m-d')) {
                 $post_text = $post->post_text;
                 $text_parser = new Twitter_Extractor($post_text);
                 $elements = $text_parser->extract();
                 if (count($elements['urls'])) {
                     $todays_favorited_posts_with_links[] = $post;
                 }
             }
         }
         $favorited_links_count = count($todays_favorited_posts_with_links);
         if ($favorited_links_count) {
             $verb = '';
             $post_type = '';
             if ($favorited_links_count == 1) {
                 $insight_text = $this->username . " " . $this->terms->getVerb('liked') . " <strong>1 " . $this->terms->getNoun('post') . "</strong> with a link in it.";
             } else {
                 $insight_text = $this->username . " " . $this->terms->getVerb('liked') . " <strong>" . $favorited_links_count . " " . $this->terms->getNoun('post', InsightTerms::PLURAL) . "</strong> with links in them:";
             }
             $this->insight_dao->insertInsightDeprecated("favorited_links", $instance->id, $this->insight_date, "Links you liked:", $insight_text, basename(__FILE__, ".php"), Insight::EMPHASIS_LOW, serialize($todays_favorited_posts_with_links));
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
 public function control()
 {
     $this->redirectToSternIndiaEndpoint('forgot.php');
     $config = Config::getInstance();
     //$this->addToView('is_registration_open', $config->getValue('is_registration_open'));
     // if (isset($_POST['email']) && $_POST['Submit'] == 'Send Reset') {
     // /$_POST['email'] = '*****@*****.**';
     if (isset($_POST['email'])) {
         $this->disableCaching();
         $dao = DAOFactory::getDAO('UserDAO');
         $user = $dao->getByEmail($_POST['email']);
         if (isset($user)) {
             $token = $user->setPasswordRecoveryToken();
             $es = new ViewManager();
             $es->caching = false;
             //$es->assign('apptitle', $config->getValue('app_title_prefix')."ThinkUp" );
             $es->assign('first_name', $user->first_name);
             $es->assign('recovery_url', "session/reset.php?token={$token}");
             $es->assign('application_url', Utils::getApplicationURL(false));
             $es->assign('site_root_path', $config->getValue('site_root_path'));
             $message = $es->fetch('_email.forgotpassword.tpl');
             $subject = $config->getValue('app_title_prefix') . "Stern India Password Recovery";
             //Will put the things in queue to mail the things.
             Resque::enqueue('user_mail', 'Mailer', array($_POST['email'], $subject, $message));
             $this->addToView('link_sent', true);
         } else {
             $this->addErrorMessage('Error: account does not exist.');
         }
     }
     $this->setViewTemplate('Session/forgot.tpl');
     return $this->generateView();
 }
 public function testGetScoredLinks()
 {
     $year = date('Y');
     $builders = self::setUpPublicInsight($this->instance);
     // set up posts with links (not photos)
     $builders[] = FixtureBuilder::build('posts', array('id' => 10, 'post_id' => 10, 'author_username' => $this->instance->network_username, 'author_user_id' => $this->instance->network_user_id, 'author_fullname' => 'Twitter User', 'author_avatar' => 'avatar.jpg', 'network' => $this->instance->network, 'post_text' => 'This is a post http://t.co/B5LAotKMWY with a link.', 'source' => 'web', 'pub_date' => "{$year}-01-01", 'reply_count_cache' => 10, 'is_protected' => false));
     $builders[] = FixtureBuilder::Build('links', array('post_key' => 10, 'url' => 'http://t.co/B5LAotKMWY', 'image_src' => '', 'expanded_url' => 'http://pic.twitter.com/vx4YL7Yz'));
     $builders[] = FixtureBuilder::build('posts', array('id' => 12, 'post_id' => 12, 'author_username' => $this->instance->network_username, 'author_user_id' => $this->instance->network_user_id, 'author_fullname' => 'Twitter User', 'author_avatar' => 'avatar.jpg', 'network' => $this->instance->network, 'post_text' => 'This is a post http://t.co/B5LAotKMWY with a link.', 'source' => 'web', 'pub_date' => "{$year}-01-01", 'reply_count_cache' => 12, 'is_protected' => false));
     $builders[] = FixtureBuilder::Build('links', array('post_key' => 12, 'url' => 'http://t.co/B5LAotKMWY', 'image_src' => '', 'expanded_url' => 'http://pic.twitter.com/vx4YL7Yz'));
     // set up posts with photos
     $builders[] = FixtureBuilder::build('posts', array('id' => 13, 'post_id' => 13, 'author_username' => $this->instance->network_username, 'author_user_id' => $this->instance->network_user_id, 'author_fullname' => 'Twitter User', 'author_avatar' => 'avatar.jpg', 'network' => $this->instance->network, 'post_text' => 'This is a post http://t.co/B5LAotKMWY with a link.', 'source' => 'web', 'pub_date' => "{$year}-01-01", 'reply_count_cache' => 12, 'is_protected' => false));
     $builders[] = FixtureBuilder::Build('links', array('post_key' => 13, 'url' => 'http://t.co/B5LAotKMWY', 'image_src' => 'http://pic.twitter.com.foo.jpg', 'expanded_url' => 'http://pic.twitter.com/vx4YL7Yz'));
     // set up one post with no link
     $builders[] = FixtureBuilder::build('posts', array('id' => 11, 'post_id' => 11, 'author_username' => $this->instance->network_username, 'author_user_id' => $this->instance->network_user_id, 'author_fullname' => 'Twitter User', 'author_avatar' => 'avatar.jpg', 'network' => $this->instance->network, 'post_text' => 'This post has no photo', 'source' => 'web', 'pub_date' => "{$year}-01-01", 'reply_count_cache' => 1, 'is_protected' => false));
     $insight_plugin = new EOYPopularLinkInsight();
     $post_dao = DAOFactory::getDAO('PostDAO');
     $last_year_of_posts = $post_dao->getThisYearOfPostsWithLinksIterator($author_id = $this->instance->network_user_id, $network = $this->instance->network);
     $scored_pics = $insight_plugin->getScoredLinks($last_year_of_posts);
     $sorted_pics = array(0 => array(12 => 60), 1 => array(10 => 50));
     $i = 0;
     foreach ($scored_pics as $post_id => $score) {
         $this->assertEqual($sorted_pics[$i][$post_id], $score);
         $i++;
     }
     $this->assertEqual($i, 2);
 }
Exemple #5
0
 /**
  * For a given post, extract URLs and store them, including image_src if that's from a known source like Twitpic,
  * Twitgoo, Yfrog, Instagr.am.
  * @param str $post_text
  * @param int $post_id
  * @param str $network
  * @param Logger $logger
  * @param arr $urls Array of URLs, optionally set, defaults to null
  */
 public static function processPostURLs($post_text, $post_id, $network, $logger, $urls = null)
 {
     if (!$urls) {
         $urls = Post::extractURLs($post_text);
     }
     if ($urls) {
         $link_dao = DAOFactory::getDAO('LinkDAO');
         $post_dao = DAOFactory::getDAO('PostDAO');
         $post = $post_dao->getPost($post_id, $network);
         if (isset($post->id)) {
             foreach ($urls as $url) {
                 $logger->logInfo("Processing URL {$url}", __METHOD__ . ',' . __LINE__);
                 $image_src = self::getImageSource($url);
                 //if we have an image_src, the URL is a known image source not in need of expansion
                 $expanded_url = $image_src !== '' ? $url : '';
                 $link_array = array('url' => $url, 'expanded_url' => $expanded_url, "image_src" => $image_src, 'post_key' => $post->id);
                 $link = new Link($link_array);
                 try {
                     $link_dao->insert($link);
                     $logger->logSuccess("Inserted " . $url . " " . ($image_src == '' ? '' : "(thumbnail " . $image_src . ") ") . "into links table", __METHOD__ . ',' . __LINE__);
                 } catch (DuplicateLinkException $e) {
                     $logger->logInfo($url . " " . ($image_src == '' ? '' : "(thumbnail " . $image_src . ") ") . " already exists in links table", __METHOD__ . ',' . __LINE__);
                 } catch (DataExceedsColumnWidthException $e) {
                     $logger->logInfo($url . " " . ($image_src == '' ? '' : "(thumbnail " . $image_src . ") ") . " data exceeds table column width", __METHOD__ . ',' . __LINE__);
                 }
             }
         }
     }
 }
 public function control()
 {
     if (isset($_POST['Submit']) && $_POST['Submit'] == 'Send Reset') {
         $this->disableCaching();
         $dao = DAOFactory::getDAO('OwnerDAO');
         $user = $dao->getByEmail($_POST['email']);
         if (isset($user)) {
             $token = $user->setPasswordRecoveryToken();
             $es = new SmartyThinkUp();
             $es->caching = false;
             $config = Config::getInstance();
             $es->assign('apptitle', $config->getValue('app_title'));
             $es->assign('recovery_url', "session/reset.php?token={$token}");
             $es->assign('server', isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost');
             $es->assign('site_root_path', $config->getValue('site_root_path'));
             $message = $es->fetch('_email.forgotpassword.tpl');
             Mailer::mail($_POST['email'], $config->getValue('app_title') . " Password Recovery", $message);
             $this->addSuccessMessage('Password recovery information has been sent to your email address.');
         } else {
             $this->addErrorMessage('Error: account does not exist.');
         }
     }
     $this->setViewTemplate('session.forgot.tpl');
     return $this->generateView();
 }
 public function adminControl()
 {
     if (!$this->is_missing_param) {
         // verify CSRF token
         $this->validateCSRFToken();
         $is_active = $_GET["a"] != 1 ? false : true;
         $plugin_dao = DAOFactory::getDAO('PluginDAO');
         $result = $plugin_dao->setActive($_GET["pid"], $is_active);
         if ($result > 0) {
             $plugin_folder = $plugin_dao->getPluginFolder($_GET["pid"]);
             $webapp = Webapp::getInstance();
             try {
                 $plugin_class_name = $webapp->getPluginObject($plugin_folder);
                 $p = new $plugin_class_name();
                 if ($is_active) {
                     $p->activate();
                 } else {
                     $p->deactivate();
                 }
             } catch (Exception $e) {
                 //plugin object isn't registered, do nothing
                 //echo $e->getMessage();
             }
         }
         $this->addToView('result', $result);
         $this->view_mgr->clear_all_cache();
     }
     return $this->generateView();
 }
 public function control()
 {
     $this->view_name = isset($_GET['v']) ? $_GET['v'] : 'default';
     $post_dao = DAOFactory::getDAO('PostDAO');
     $this->setPageTitle('Post Details');
     $this->setViewTemplate('post.index.tpl');
     $network = isset($_GET['n']) ? $_GET['n'] : 'twitter';
     if ($this->shouldRefreshCache()) {
         if (isset($_GET['t']) && is_numeric($_GET['t'])) {
             $post_id = $_GET['t'];
             $post = $post_dao->getPost($post_id, $network);
             if (isset($post)) {
                 $config = Config::getInstance();
                 $this->addToView('disable_embed_code', $config->getValue('is_embed_disabled') || $post->is_protected);
                 if (isset($_GET['search'])) {
                     $this->addToView('search_on', true);
                 }
                 if (!$post->is_protected || $this->isLoggedIn()) {
                     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
                     $options = $plugin_option_dao->getOptionsHash('geoencoder', true);
                     if (isset($options['distance_unit']->option_value)) {
                         $distance_unit = $options['distance_unit']->option_value;
                     } else {
                         $distance_unit = 'km';
                     }
                     $this->addToView('post', $post);
                     $this->addToView('unit', $distance_unit);
                     $replies = $post_dao->getRepliesToPost($post_id, $network, 'default', $distance_unit);
                     $public_replies = array();
                     foreach ($replies as $reply) {
                         if (!$reply->author->is_protected) {
                             $public_replies[] = $reply;
                         }
                     }
                     $public_replies_count = count($public_replies);
                     $this->addToView('public_reply_count', $public_replies_count);
                     if ($this->isLoggedIn()) {
                         $this->addToView('replies', $replies);
                     } else {
                         $this->addToView('replies', $public_replies);
                     }
                     $all_replies_count = count($replies);
                     $private_reply_count = $all_replies_count - $public_replies_count;
                     $this->addToView('private_reply_count', $private_reply_count);
                     $webapp = Webapp::getInstance();
                     $sidebar_menu = $webapp->getPostDetailMenu($post);
                     $this->addToView('sidebar_menu', $sidebar_menu);
                     $this->loadView($post);
                 } else {
                     $this->addErrorMessage('Insufficient privileges');
                 }
             } else {
                 $this->addErrorMessage('Post not found');
             }
         } else {
             $this->addErrorMessage('Post not specified');
         }
     }
     return $this->generateView();
 }
    public function  testFlickrCrawl() {
        $builders = $this->buildData();

        $crawler = Crawler::getInstance();
        $config = Config::getInstance();

        //use fake Flickr API key
        $plugin_builder = FixtureBuilder::build('plugins', array('id'=>'2', 'folder_name'=>'flickrthumbnails'));
        $option_builder = FixtureBuilder::build('options', array(
            'namespace' => OptionDAO::PLUGIN_OPTIONS . '-2',
            'option_name' => 'flickr_api_key',
            'option_value' => 'dummykey') );
        //$config->setValue('flickr_api_key', 'dummykey');

        $this->simulateLogin('*****@*****.**', true);
        $crawler->crawl();

        $ldao = DAOFactory::getDAO('LinkDAO');

        $link = $ldao->getLinkById(43);
        $this->assertEqual($link->expanded_url, 'http://farm3.static.flickr.com/2755/4488149974_04d9558212_m.jpg');
        $this->assertEqual($link->error, '');

        $link = $ldao->getLinkById(42);
        $this->assertEqual($link->expanded_url, '');
        $this->assertEqual($link->error, 'No response from Flickr API');

        $link = $ldao->getLinkById(41);
        $this->assertEqual($link->expanded_url, '');
        $this->assertEqual($link->error, 'No response from Flickr API');
    }
 /**
  * Returns URL-encoded parameters needed to make an API call.
  * @param str $username
  * @return str Parameters to use in a URL to make an API call
  */
 public static function getAuthParameters($username)
 {
     $owner_dao = DAOFactory::getDAO('OwnerDAO');
     $pwd_from_db = $owner_dao->getPass($username);
     $api_secret = Session::getAPISecretFromPassword($pwd_from_db);
     return 'un=' . urlencode($username) . '&as=' . urlencode($api_secret);
 }
Exemple #11
0
 public function generateInsight(Instance $instance, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     if (self::shouldGenerateInsight('link_prompt', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = null, $count_last_week_of_posts = null, $excluded_networks = array('foursquare', 'youtube'), $alternate_day = (int) date('j') % 2)) {
         $post_dao = DAOFactory::getDAO('PostDAO');
         $link_dao = DAOFactory::getDAO('LinkDAO');
         // Check from midnight two days ago until an hour from now
         // (to avoid clock-sync issues)
         $recent_posts = $post_dao->getPostsByUserInRange($instance->network_user_id, $instance->network, date('Y-m-d H:i:s', strtotime('-2 days midnight')), date('Y-m-d H:i:s', strtotime('+1 hour')));
         $posts_with_links = array();
         foreach ($recent_posts as $post) {
             $post_text = $post->post_text;
             $text_parser = new Twitter_Extractor($post_text);
             $elements = $text_parser->extract();
             if (count($elements['urls'])) {
                 $posts_with_links[] = $post;
             }
         }
         $num_posts = $post_dao->countAllPostsByUserSinceDaysAgo($instance->network_user_id, $instance->network, 30);
         $num_links = $link_dao->countLinksPostedByUserSinceDaysAgo($instance->network_user_id, $instance->network, 30);
         if ($num_posts && $num_links / $num_posts > 0.2 && count($recent_posts) && !count($posts_with_links)) {
             $insight_text = $this->username . " hasn't " . $this->terms->getVerb('posted') . " a link in the last 2 days. It may be time to share an interesting link with " . $this->terms->getNoun('friend', InsightTerms::PLURAL) . ".";
             $this->insight_dao->insertInsightDeprecated('link_prompt', $instance->id, $this->insight_date, "Nudge:", $insight_text, basename(__FILE__, ".php"), Insight::EMPHASIS_LOW);
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
Exemple #12
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $video_dao = DAOFactory::getDAO('VideoDAO');
     $filename = basename(__FILE__, ".php");
     foreach ($last_week_of_posts as $post) {
         if ($post->network == 'youtube') {
             $video = $video_dao->getVideoByID($post->post_id, 'youtube');
             $simplified_post_date = date('Y-m-d', strtotime($video->pub_date));
         } else {
             break;
         }
         // Get the average baselines
         $total_likes_and_dislikes = $video->likes + $video->dislikes;
         $percent_likes = round($video->likes / $total_likes_and_dislikes * 100, 2);
         $percent_dislikes = round($video->dislikes / $total_likes_and_dislikes * 100, 2);
         // If the video splits opinion in the range of 60/40 likes / dislikes or 40/60 likes / dislikes
         if ($percent_likes >= 40 && $percent_likes <= 60) {
             $headline = $video->post_text . " really touched a nerve!";
             $insight_text = "{$percent_likes}" . "% of people liked ";
             $insight_text .= "<a href=\"http://plus.google.com/{$instance->network_user_id}/about\">" . $instance->network_username;
             $insight_text .= '</a>\'s video ';
             $insight_text .= "<a href=\"http://www.youtube.com/watch?v={$post->post_id}\">" . $video->post_text . "</a> ";
             $insight_text .= "and " . $percent_dislikes . "% disliked it.";
             $this->insight_dao->insertInsightDeprecated("split_opinions" . $video->id, $instance->id, $simplified_post_date, $headline, $insight_text, $filename, 1, serialize($video));
         }
     }
 }
 public function testShouldGenerateInsight()
 {
     $instance = new Instance();
     $instance->id = 10;
     $instance->network_username = '******';
     $instance->network = 'twitter';
     $time_now = date("Y-m-d H:i:s");
     $today = date('Y-m-d', strtotime('today'));
     $yesterday = date('Y-m-d', strtotime('-1 day'));
     $builders = array();
     $builders[] = FixtureBuilder::build('insights', array('id' => 76, 'instance_id' => 10, 'slug' => 'some_slug', 'date' => $today, 'time_generated' => $time_now));
     $builders[] = FixtureBuilder::build('insights', array('id' => 77, 'instance_id' => 10, 'slug' => 'some_other_slug', 'date' => $yesterday, 'time_generated' => $time_now));
     $insight_plugin_parent = new InsightPluginParent();
     $insight_plugin_parent->insight_dao = DAOFactory::getDAO('InsightDAO');
     // Test default values
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('some_slug', $instance));
     // Test regeneration on a given date
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = $today));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('some_other_slug', $instance, $insight_date = $yesterday));
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('some_other_slug', $instance, $insight_date = $yesterday, $regenerate_existing_insight = true));
     // Test for day of week
     $dow1 = date('w');
     $dow2 = date('w', strtotime('-1 day'));
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = $dow1));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = $dow2));
     // Test with last week of posts
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = null, $count_last_week_of_posts = 13));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = null, $count_last_week_of_posts = 0));
     // Test excluded networks
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = null, $count_last_week_of_posts = null, $excluded_networks = array('facebook')));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = null, $count_last_week_of_posts = null, $excluded_networks = array('twitter', 'facebook')));
 }
Exemple #14
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     if ($instance->network == 'twitter' || $instance->network == 'facebook') {
         if ($instance->network == 'facebook' && self::shouldGenerateWeeklyInsight($this->slug, $instance, 'today', false, 1)) {
             $run = true;
         } else {
             if ($instance->network == 'twitter' && self::shouldGenerateWeeklyInsight($this->slug, $instance, 'today', false, 5)) {
                 $run = true;
             }
         }
         if ($run) {
             $photo_this_week = $this->findPostWithPhoto($last_week_of_posts);
             if (!$photo_this_week) {
                 $post_dao = DAOFactory::getDAO('PostDAO');
                 $posts = $post_dao->getAllPostsByUsernameOrderedBy($instance->network_username, $instance->network, $count = 0, $order_by = "pub_date", $in_last_x_days = 14, $iterator = false, $is_public = false);
                 $photo_at_all = $this->findPostWithPhoto($posts);
                 if ($photo_at_all) {
                     $insight = new Insight();
                     $insight->slug = $this->slug;
                     $insight->instance_id = $instance->id;
                     $insight->date = $this->insight_date;
                     $days = floor((time() - strtotime($photo_at_all->pub_date)) / (60 * 60 * 24));
                     $insight->headline = $this->getVariableCopy(array("A great time for a pic", "%total days without a pic", "A thousand words unsaid"), array('total' => $days));
                     $insight->text = $this->username . " hasn't posted a photo in {$days} day" . ($days == 1 ? "" : "s") . ". " . "It might be worth finding something to share.";
                     $insight->filename = basename(__FILE__, ".php");
                     $insight->emphasis = Insight::EMPHASIS_LOW;
                     $this->insight_dao->insertInsight($insight);
                 }
             }
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
Exemple #15
0
 public function generateInsight(Instance $instance, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $existing_insight = $this->insight_dao->getInsight("posts_on_this_day_popular_flashback", $instance->id, $this->insight_date);
     if (!isset($existing_insight)) {
         //Generate flashback post list
         $post_dao = DAOFactory::getDAO('PostDAO');
         $flashback_posts = $post_dao->getOnThisDayFlashbackPosts($instance->network_user_id, $instance->network, $this->insight_date);
         $most_popular_post = null;
         $most_responses = 0;
         if (isset($flashback_posts) && sizeof($flashback_posts) > 0) {
             foreach ($flashback_posts as $post) {
                 $total_responses = $post->reply_count_cache + $post->all_retweets + $post->favlike_count_cache;
                 if ($total_responses > 0 && $total_responses > $most_responses) {
                     $most_popular_post = $post;
                     $most_responses = $total_responses;
                 }
             }
             if (isset($most_popular_post)) {
                 $post_year = date(date('Y', strtotime($most_popular_post->pub_date)));
                 $current_year = date('Y');
                 $number_of_years_ago = $current_year - $post_year;
                 $plural = $number_of_years_ago > 1 ? 's' : '';
                 $insight_text = "{$this->username}'s most popular post <strong>{$number_of_years_ago} year{$plural} ago" . "</strong> today was:";
                 $this->insight_dao->insertInsight("posts_on_this_day_popular_flashback", $instance->id, $this->insight_date, "Time machine:", $insight_text, basename(__FILE__, ".php"), Insight::EMPHASIS_LOW, serialize($most_popular_post));
             }
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
Exemple #16
0
 public function generateInsight(Instance $instance, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $archived_posts_in_hundreds = intval($instance->total_posts_in_system / 100);
     if ($archived_posts_in_hundreds > 0) {
         $insight_baseline_slug = "archived_posts_" . $archived_posts_in_hundreds;
         $insight_baseline_dao = DAOFactory::getDAO('InsightBaselineDAO');
         if (!$insight_baseline_dao->doesInsightBaselineExist($insight_baseline_slug, $instance->id)) {
             $insight_baseline_dao->insertInsightBaseline($insight_baseline_slug, $instance->id, $archived_posts_in_hundreds);
             $config = Config::getInstance();
             switch ($instance->network) {
                 case "twitter":
                     $posts_term = "tweets";
                     break;
                 case "foursquare":
                     $posts_term = "checkins";
                     break;
                 default:
                     $posts_term = "posts";
             }
             $text = "ThinkUp has captured over <strong>" . number_format($archived_posts_in_hundreds * 100) . ' ' . $posts_term . '</strong> by ' . $this->username . '.';
             $this->insight_dao->insertInsight("archived_posts", $instance->id, $this->insight_date, "Archived:", $text, basename(__FILE__, ".php"), Insight::EMPHASIS_MED);
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
 /**
  * Launch the crawler, if the latest crawler_last_run date is older than X minutes, then return a valid RSS feed.
  * @return string rendered view markup
  */
 public function authControl()
 {
     Utils::defineConstants();
     $this->setContentType('application/rss+xml; charset=UTF-8');
     $this->setViewTemplate('rss.tpl');
     $config = Config::getInstance();
     $rss_crawler_refresh_rate = $config->getValue('rss_crawler_refresh_rate');
     if (empty($rss_crawler_refresh_rate)) {
         $rss_crawler_refresh_rate = 20;
         // minutes
     }
     $protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
     $base_url = "{$protocol}://" . $_SERVER['HTTP_HOST'] . THINKUP_BASE_URL;
     $crawler_launched = false;
     $instance_dao = DAOFactory::getDAO('InstanceDAO');
     $freshest_instance = $instance_dao->getInstanceFreshestOne();
     $crawler_last_run = strtotime($freshest_instance->crawler_last_run);
     if ($crawler_last_run < time() - $rss_crawler_refresh_rate * 60) {
         $crawler_run_url = $base_url . 'run.php?' . ThinkUpAuthAPIController::getAuthParameters($this->getLoggedInUser());
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $crawler_run_url);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
         // seconds
         curl_setopt($ch, CURLOPT_TIMEOUT, 5);
         // seconds
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_HEADER, true);
         $result = curl_exec($ch);
         curl_close($ch);
         $body = substr($result, strpos($result, "\r\n\r\n") + 4);
         if (strpos($result, 'Content-Type: application/json') && function_exists('json_decode')) {
             $json = json_decode($body);
             if (isset($json->error)) {
                 $crawler_launched = false;
             } else {
                 if (isset($json->result) && $json->result == 'success') {
                     $crawler_launched = true;
                 }
             }
         } else {
             if (strpos($body, 'Error starting crawler') !== FALSE) {
                 $crawler_launched = false;
             } else {
                 $crawler_launched = true;
             }
         }
     }
     $items = array();
     if ($crawler_launched) {
         $title = 'ThinkUp crawl started on ' . date('Y-m-d H:i:s');
         $link = $base_url . 'rss.php?d=' . urlencode(date('Y-m-d H:i:s'));
         $description = "Last ThinkUp crawl ended on {$freshest_instance->crawler_last_run}<br />A new crawl " . "was started just now, since it's been more than {$rss_crawler_refresh_rate} minutes since the last run.";
         $items[] = self::createRSSItem($title, $link, $description);
     }
     $items = array_merge($items, $this->getAdditionalItems($base_url));
     $this->addToView('items', $items);
     $this->addToView('logged_in_user', htmlspecialchars($this->getLoggedInUser()));
     $this->addToView('rss_crawler_refresh_rate', htmlspecialchars($rss_crawler_refresh_rate));
     return $this->generateView();
 }
 public function crawl()
 {
     $logger = Logger::getInstance();
     $config = Config::getInstance();
     $instance_dao = DAOFactory::getDAO('InstanceDAO');
     $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
     $owner_dao = DAOFactory::getDAO('OwnerDAO');
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $options = $plugin_option_dao->getOptionsHash('googleplus', true);
     //get cached
     $current_owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     //crawl Google+ users
     $instances = $instance_dao->getActiveInstancesStalestFirstForOwnerByNetworkNoAuthError($current_owner, 'google+');
     if (isset($options['google_plus_client_id']->option_value) && isset($options['google_plus_client_secret']->option_value)) {
         foreach ($instances as $instance) {
             $logger->setUsername(ucwords($instance->network) . ' | ' . $instance->network_username);
             $logger->logUserSuccess("Starting to collect data for " . $instance->network_username . "'s " . ucwords($instance->network), __METHOD__ . ',' . __LINE__);
             $tokens = $owner_instance_dao->getOAuthTokens($instance->id);
             $access_token = $tokens['oauth_access_token'];
             $refresh_token = $tokens['oauth_access_token_secret'];
             $instance_dao->updateLastRun($instance->id);
             $google_plus_crawler = new GooglePlusCrawler($instance, $access_token);
             $dashboard_module_cacher = new DashboardModuleCacher($instance);
             try {
                 $google_plus_crawler->initializeInstanceUser($options['google_plus_client_id']->option_value, $options['google_plus_client_secret']->option_value, $access_token, $refresh_token, $current_owner->id);
                 $google_plus_crawler->fetchInstanceUserPosts();
             } catch (Exception $e) {
                 $logger->logUserError('EXCEPTION: ' . $e->getMessage(), __METHOD__ . ',' . __LINE__);
             }
             $dashboard_module_cacher->cacheDashboardModules();
             $instance_dao->save($google_plus_crawler->instance, 0, $logger);
             $logger->logUserSuccess("Finished collecting data for " . $instance->network_username . "'s " . ucwords($instance->network), __METHOD__ . ',' . __LINE__);
         }
     }
 }
 public function testValidResults()
 {
     // one result
     $builders = $this->buildOptions(1);
     $post_dao = DAOFactory::getDAO('PostDAO');
     $post_it = $post_dao->getAllPostsByUsernameIterator('mojojojo', 'twitter');
     $posts = $post_dao->getAllPostsByUsername('mojojojo', 'twitter');
     $cnt = 0;
     foreach ($post_it as $key => $value) {
         $this->assertEqual($value->post_text, $posts[$cnt]->post_text);
         $this->assertIsA($value, 'Post');
         $cnt++;
     }
     $this->assertEqual(1, $cnt);
     // 10 results
     $builders = null;
     $builders = $this->buildOptions(10);
     $post_it = $post_dao->getAllPostsByUsernameIterator('mojojojo', 'twitter');
     $posts = $post_dao->getAllPostsByUsername('mojojojo', 'twitter');
     $cnt = 0;
     foreach ($post_it as $key => $value) {
         $this->assertEqual($value->post_text, $posts[$cnt]->post_text);
         $this->assertIsA($value, 'Post');
         $cnt++;
     }
     $this->assertEqual(10, $cnt);
 }
 public function control()
 {
     $session = new Session();
     $dao = DAOFactory::getDAO('OwnerDAO');
     $this->setViewTemplate('session.resetpassword.tpl');
     $this->disableCaching();
     if (!isset($_GET['token']) || !preg_match('/^[\\da-f]{32}$/', $_GET['token']) || !($user = $dao->getByPasswordToken($_GET['token']))) {
         // token is nonexistant or bad
         $this->addErrorMessage('You have reached this page in error.');
         return $this->generateView();
     }
     if (!$user->validateRecoveryToken($_GET['token'])) {
         $this->addErrorMessage('Your token is expired.');
         return $this->generateView();
     }
     if (isset($_POST['password'])) {
         if ($_POST['password'] == $_POST['password_confirm']) {
             if ($dao->updatePassword($user->email, $session->pwdcrypt($_POST['password'])) < 1) {
                 echo "not updated";
             }
             $login_controller = new LoginController(true);
             $login_controller->addSuccessMessage('You have changed your password.');
             return $login_controller->go();
         } else {
             $this->addErrorMessage("Passwords didn't match.");
         }
     } else {
         if (isset($_POST['Submit'])) {
             $this->addErrorMessage('Please enter a new password.');
         }
     }
     return $this->generateView();
 }
Exemple #21
0
 public function generateInsight(Instance $instance, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     //Set up DAOs
     $instance_hashtag_dao = DAOFactory::getDAO('InstanceHashtagDAO');
     $hashtag_post_dao = DAOFactory::getDAO('HashtagPostDAO');
     $hashtag_dao = DAOFactory::getDAO('HashtagDAO');
     // Get all the hashtags for the instance
     $instance_hashtags = $instance_hashtag_dao->getByInstance($instance->id);
     // foreach hashtag, get the count of new posts
     foreach ($instance_hashtags as $instance_hashtag) {
         $total_new_posts = $hashtag_post_dao->getTotalPostsByHashtagAndDate($instance_hashtag->hashtag_id);
         //Only insert insight if there are new results
         if ($total_new_posts > 0) {
             //Assemble insight text
             $post_term = $instance->network == 'twitter' ? 'tweets' : 'posts';
             $hashtag = $hashtag_dao->getHashtagByID($instance_hashtag->hashtag_id);
             $link = 'search.php?u=' . $instance->network_username . '&n=' . $instance->network . '&c=searches&k=' . urlencode($hashtag->hashtag) . '&q=' . urlencode($hashtag->hashtag);
             $text = number_format($total_new_posts) . " new " . $post_term . " contain <b><a href=\"" . $link . "\">" . $hashtag->hashtag . "</a></b>.";
             // Insert insight
             $this->insight_dao->insertInsightDeprecated("saved_search_results_" . $instance_hashtag->hashtag_id, $instance->id, $this->insight_date, "New search results:", $text, basename(__FILE__, ".php"), Insight::EMPHASIS_MED);
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
Exemple #22
0
 /**
  * Gets called when crawler runs.
  *
  * About crawler exclusivity (mutex usage):
  * When launched by an admin, no other user, admin or not, will be able to launch a crawl until this one is done.
  * When launched by a non-admin, we first check that no admin run is under way, and if that's the case,
  * we launch a crawl for the current user only.
  * No user will be able to launch two crawls in parallel, but different non-admin users crawls can run in parallel.
  */
 public function crawl()
 {
     if (!Session::isLoggedIn()) {
         throw new UnauthorizedUserException('You need a valid session to launch the crawler.');
     }
     $mutex_dao = DAOFactory::getDAO('MutexDAO');
     $owner_dao = DAOFactory::getDAO('OwnerDAO');
     $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
     if (empty($owner)) {
         throw new UnauthorizedUserException('You need a valid session to launch the crawler.');
     }
     $global_mutex_name = 'crawler';
     // Everyone needs to check the global mutex
     $lock_successful = $mutex_dao->getMutex($global_mutex_name);
     if ($lock_successful) {
         // Global mutex was free, which means no admin crawls are under way
         if ($owner->is_admin) {
             // Nothing more needs to be done, since admins use the global mutex
             $mutex_name = $global_mutex_name;
         } else {
             // User is a non-admin; let's use a user mutex.
             $mutex_name = 'crawler-' . $owner->id;
             $lock_successful = $mutex_dao->getMutex($mutex_name);
             $mutex_dao->releaseMutex($global_mutex_name);
         }
     }
     if ($lock_successful) {
         $this->emitObjectMethod('crawl');
         $mutex_dao->releaseMutex($mutex_name);
     } else {
         throw new CrawlerLockedException("Error starting crawler; another crawl is already in progress.");
     }
 }
 public function control()
 {
     $config = Config::getInstance();
     $this->addToView('is_registration_open', $config->getValue('is_registration_open'));
     if (isset($_POST['Submit']) && $_POST['Submit'] == 'Send Reset') {
         $this->disableCaching();
         $dao = DAOFactory::getDAO('OwnerDAO');
         $user = $dao->getByEmail($_POST['email']);
         if (isset($user)) {
             $token = $user->setPasswordRecoveryToken();
             $es = new ViewManager();
             $es->caching = false;
             $es->assign('apptitle', $config->getValue('app_title_prefix') . "ThinkUp");
             $es->assign('recovery_url', "session/reset.php?token={$token}");
             $es->assign('application_url', Utils::getApplicationURL($false));
             $es->assign('site_root_path', $config->getValue('site_root_path'));
             $message = $es->fetch('_email.forgotpassword.tpl');
             Mailer::mail($_POST['email'], $config->getValue('app_title_prefix') . "ThinkUp Password Recovery", $message);
             $this->addSuccessMessage('Password recovery information has been sent to your email address.');
         } else {
             $this->addErrorMessage('Error: account does not exist.');
         }
     }
     $this->view_mgr->addHelp('forgot', 'userguide/accounts/index');
     $this->setViewTemplate('session.forgot.tpl');
     return $this->generateView();
 }
Exemple #24
0
 public function generateInsight(Instance $instance, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $post_dao = DAOFactory::getDAO('PostDAO');
     $user_dao = DAOFactory::getDAO('UserDAO');
     $service_user = $user_dao->getDetails($instance->network_user_id, $instance->network);
     $share_verb = $instance->network == 'twitter' ? 'retweeted' : 'reshared';
     foreach ($last_week_of_posts as $post) {
         $big_reshares = $post_dao->getRetweetsByAuthorsOverFollowerCount($post->post_id, $instance->network, $service_user->follower_count);
         if (isset($big_reshares) && sizeof($big_reshares) > 0) {
             if (!isset($config)) {
                 $config = Config::getInstance();
             }
             $post_link = '<a href="' . $config->getValue('site_root_path') . 'post/?t=' . $post->post_id . '&n=' . $post->network . '&v=fwds">';
             if (sizeof($big_reshares) > 1) {
                 $notification_text = "People with lots of followers {$share_verb} " . $post_link . "{$this->username}'s post</a>.";
             } else {
                 $follower_count_multiple = intval($big_reshares[0]->follower_count / $service_user->follower_count);
                 if ($follower_count_multiple > 1) {
                     $notification_text = "Someone with <strong>" . $follower_count_multiple . "x</strong> more followers than {$this->username} {$share_verb} " . $post_link . "this post</a>.";
                 } else {
                     $notification_text = "Someone with lots of followers {$share_verb} " . $post_link . "{$this->username}'s post</a>.";
                 }
             }
             //Replace each big resharer's bio line with the text of the post
             foreach ($big_reshares as $sharer) {
                 $sharer->description = '"' . $post->post_text . '"';
             }
             $simplified_post_date = date('Y-m-d', strtotime($post->pub_date));
             $this->insight_dao->insertInsight("big_reshare_" . $post->id, $instance->id, $simplified_post_date, "Big reshare!", $notification_text, basename(__FILE__, ".php"), Insight::EMPHASIS_HIGH, serialize($big_reshares));
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
 public function control()
 {
     $output = "";
     $authorized = false;
     if (isset($this->argc) && $this->argc > 1) {
         // check for CLI credentials
         $session = new Session();
         $username = $this->argv[1];
         if ($this->argc > 2) {
             $pw = $this->argv[2];
         } else {
             $pw = getenv('THINKUP_PASSWORD');
         }
         $owner_dao = DAOFactory::getDAO('OwnerDAO');
         $owner = $owner_dao->getByEmail($username);
         if ($owner_dao->isOwnerAuthorized($username, $pw)) {
             $authorized = true;
             Session::completeLogin($owner);
         } else {
             $output = "ERROR: Incorrect username and password.";
         }
     } else {
         // check user is logged in on the web
         if ($this->isLoggedIn()) {
             $authorized = true;
         } else {
             $output = "ERROR: Invalid or missing username and password.";
         }
     }
     if ($authorized) {
         $crawler = Crawler::getInstance();
         $crawler->crawl();
     }
     return $output;
 }
 protected function exportReplies()
 {
     $post_dao = DAOFactory::getDAO('PostDAO');
     $replies_it = $post_dao->getRepliesToPostIterator($_GET['post_id'], $_GET['n']);
     $column_labels = array_keys(get_class_vars('Post'));
     self::outputCSV($replies_it, $column_labels, 'replies-' . $_GET['post_id']);
 }
 public function testCreateNewStreamDataDAO()
 {
     $dao = DAOFactory::getDAO('StreamDataDAO');
     $this->assertTrue(isset($dao));
     $dao = new StreamDataMySQLDAO();
     $this->assertTrue(isset($dao));
 }
 public function control()
 {
     $controller = new LoginController(true);
     if ($this->is_missing_param) {
         $controller->addErrorMessage('Invalid account activation credentials.');
     } else {
         $owner_dao = DAOFactory::getDAO('OwnerDAO');
         $acode = $owner_dao->getActivationCode($_GET['usr']);
         if ($_GET['code'] == $acode['activation_code']) {
             $owner = $owner_dao->getByEmail($_GET['usr']);
             if (isset($owner) && isset($owner->is_activated)) {
                 if ($owner->is_activated == 1) {
                     $controller->addSuccessMessage("You have already activated your account. Please log in.");
                 } else {
                     $owner_dao->activateOwner($_GET['usr']);
                     $controller->addSuccessMessage("Success! Your account has been activated. Please log in.");
                 }
             } else {
                 $controller->addErrorMessage('Houston, we have a problem: Account activation failed.');
             }
         } else {
             $controller->addErrorMessage('Houston, we have a problem: Account activation failed.');
         }
     }
     return $controller->go();
 }
Exemple #29
0
 /**
  * Generates a new password recovery token and returns it.
  *
  * The internal format of the token is a Unix timestamp of when it was set (for checking if it's stale), an
  * underscore, and then the token itself.
  *
  * @return string A new password token for embedding in a link and emailing a user.
  */
 public function setPasswordRecoveryToken()
 {
     $token = md5(uniqid(rand()));
     $dao = DAOFactory::getDAO('UserDAO');
     $dao->updatePasswordToken($this->email, $token . '_' . time());
     return $token;
 }
 public function crawl()
 {
     $config = Config::getInstance();
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $options = $plugin_option_dao->getOptionsHash('flickrthumbnails', true);
     $api_key = $options['flickr_api_key']->option_value;
     if (isset($api_key) && $api_key != '') {
         $logger = Logger::getInstance();
         $fa = new FlickrAPIAccessor($api_key);
         $ldao = DAOFactory::getDAO('LinkDAO');
         $flickrlinkstoexpand = $ldao->getLinksToExpandByURL('http://flic.kr/');
         if (count($flickrlinkstoexpand > 0)) {
             $logger->logStatus(count($flickrlinkstoexpand) . " Flickr links to expand", "Flickr Plugin");
         } else {
             $logger->logStatus("No Flickr links to expand", "Flickr Plugin");
         }
         foreach ($flickrlinkstoexpand as $fl) {
             $eurl = $fa->getFlickrPhotoSource($fl);
             if ($eurl["expanded_url"] != '') {
                 $ldao->saveExpandedUrl($fl, $eurl["expanded_url"], '', 1);
             } elseif ($eurl["error"] != '') {
                 $ldao->saveExpansionError($fl, $eurl["error"]);
             }
         }
         $logger->close();
         # Close logging
     }
 }