public function authControl()
 {
     $this->disableCaching();
     // we don't want to cache the rss link with api key as it can get updated
     Utils::defineConstants();
     $this->setContentType('text/html; charset=UTF-8');
     $this->setPageTitle("ThinkUp Crawler");
     $this->setViewTemplate('crawler.updatenow.tpl');
     $whichphp = @exec('which php');
     $php_path = !empty($whichphp) ? $whichphp : 'php';
     $email = $this->getLoggedInUser();
     $owner = parent::getOwner($email);
     $rss_url = THINKUP_BASE_URL . sprintf('rss.php?un=%s&as=%s', urlencode($email), $owner->api_key);
     $config = Config::getInstance();
     $site_root_path = $config->getValue('site_root_path');
     $this->addInfoMessage('<b>Hint</b><br />You can automate ThinkUp crawls by subscribing to ' . '<strong><a href="' . $rss_url . '" target="_blank">this secret RSS feed</a></strong> ' . 'in your favorite newsreader. Accidentally share the feed URL? ' . '<a href="' . $site_root_path . 'account/index.php?m=manage#instances">Reset it.</a>' . '<br /><br />Alternately, use the command below to set up a cron job that ' . 'runs hourly to update your posts. (Be sure to change yourpassword to your real password!)<br /><br />' . '<code style="font-family:Courier">cd ' . THINKUP_WEBAPP_PATH . 'crawler/;export THINKUP_PASSWORD=yourpassword; ' . $php_path . ' crawl.php ' . $this->getLoggedInUser() . '</code><br /><br /><a href="http://thinkupapp.com/docs/userguide/datacapture.html">Learn more about ' . 'how to update your ThinkUp data</a>.');
     if (isset($_GET['log']) && $_GET['log'] == 'full') {
         $this->addToView('log', 'full');
     }
     return $this->generateView();
 }
Example #2
0
 /**
  * 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()
 {
     $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
     }
     $base_url = Utils::getApplicationURL();
     $crawler_plugin_registrar_launched = false;
     $instance_dao = DAOFactory::getDAO('InstanceDAO');
     $email = $this->getLoggedInUser();
     $owner = parent::getOwner($email);
     $freshest_instance = $instance_dao->getFreshestByOwnerId($owner->id);
     if ($freshest_instance) {
         $crawler_plugin_registrar_last_run = strtotime($freshest_instance->crawler_last_run);
     }
     if ($freshest_instance && $crawler_plugin_registrar_last_run < time() - $rss_crawler_refresh_rate * 60) {
         $crawler_plugin_registrar_run_url = $base_url . 'crawler/run.php?' . sprintf('un=%s&as=%s', $email, $owner->api_key);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $crawler_plugin_registrar_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_plugin_registrar_launched = false;
             } else {
                 if (isset($json->result) && $json->result == 'success') {
                     $crawler_plugin_registrar_launched = true;
                 }
             }
         } else {
             if (strpos($body, 'Error starting crawler') !== FALSE) {
                 $crawler_plugin_registrar_launched = false;
             } else {
                 $crawler_plugin_registrar_launched = true;
             }
         }
     }
     $items = array();
     $logger = Logger::getInstance();
     // Don't return an item if there is a crawler log defined;
     // it would just duplicate the information available in that file.
     if ($crawler_plugin_registrar_launched && !isset($logger->log)) {
         $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();
 }
Example #3
0
 /**
  * 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()
 {
     $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
     }
     $base_url = Utils::getApplicationURL();
     $crawler_plugin_registrar_launched = false;
     $instance_dao = DAOFactory::getDAO('InstanceDAO');
     $email = $this->getLoggedInUser();
     $owner = parent::getOwner($email);
     $freshest_instance = $instance_dao->getFreshestByOwnerId($owner->id);
     if ($freshest_instance) {
         $crawler_plugin_registrar_last_run = strtotime($freshest_instance->crawler_last_run);
     }
     if ($freshest_instance && $crawler_plugin_registrar_last_run < time() - $rss_crawler_refresh_rate * 60) {
         $crawler_plugin_registrar_run_url = $base_url . 'crawler/run.php?' . sprintf('un=%s&as=%s', $email, $owner->api_key);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $crawler_plugin_registrar_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_plugin_registrar_launched = false;
             } else {
                 if (isset($json->result) && $json->result == 'success') {
                     $crawler_plugin_registrar_launched = true;
                 }
             }
         } else {
             if (strpos($body, 'Error starting crawler') !== FALSE) {
                 $crawler_plugin_registrar_launched = false;
             } else {
                 $crawler_plugin_registrar_launched = true;
             }
         }
     }
     $items = array();
     $logger = Logger::getInstance();
     // Don't return an item if there is a crawler log defined;
     // it would just duplicate the information available in that file.
     if ($crawler_plugin_registrar_launched && !isset($logger->log)) {
         $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));
     //Add insights to RSS feed
     $insight_dao = DAOFactory::getDAO('InsightDAO');
     if ($this->isAdmin()) {
         ///show all insights for all service users
         $insights = $insight_dao->getAllInstanceInsights($page_count = InsightStreamController::PAGE_INSIGHTS_COUNT + 1, 1);
     } else {
         //show only service users owner owns
         $owner_dao = DAOFactory::getDAO('OwnerDAO');
         $owner = $owner_dao->getByEmail($this->getLoggedInUser());
         $insights = $insight_dao->getAllOwnerInstanceInsights($owner->id, $page_count = InsightStreamController::PAGE_INSIGHTS_COUNT + 1, 1);
     }
     if (sizeof($insights) == 0) {
         $title = 'No insights exist on ' . date('Y-m-d H:i:s');
         $link = $base_url . 'rss.php?d=' . urlencode(date('Y-m-d H:i:s'));
         $description = "ThinkUp doesn't have any insights to show you. Check your crawler log to make sure " . "ThinkUp is capturing data.";
         $items[] = self::createRSSItem($title, $link, $description);
     } else {
         foreach ($insights as $insight) {
             $username_in_title = ($insight->instance->network == 'twitter' ? '@' : '') . $insight->instance->network_username;
             $title = str_replace(':', '', $insight->headline) . " (" . $username_in_title . ")";
             $link = $base_url . '?u=' . $insight->instance->network_username . '&n=' . urlencode($insight->instance->network) . '&d=' . urlencode(date('Y-m-d', strtotime($insight->date))) . '&s=' . urlencode($insight->slug);
             $description = $insight->headline . " " . $insight->text . '<br><a href="' . $link . '">Link</a>';
             $time = strtotime($insight->date);
             $items[] = self::createRSSItem($title, $link, $description, $time);
         }
     }
     $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();
 }