Beispiel #1
0
 /**
  * Formats a Tweet into a message suitable for output
  *
  * @param object $tweet      JSON-decoded tweet object from Twitter
  * @param bool   $includeUrl whether or not to include the URL in the 
  *  formatted output
  *
  * @return string
  */
 protected function formatTweet(StdClass $tweet, $includeUrl = true)
 {
     $body = Phergie_Plugin_Helper_String::cleanString($tweet->text);
     $ts = new Phergie_Plugin_Helper_Time($tweet->created_at);
     $out = '<@' . $tweet->user->screen_name . '> ' . $body . ' - ' . $ts->getCountDown() . ' ago';
     if ($includeUrl) {
         $out .= ' (' . $this->twitter->getUrlOutputStatus($tweet) . ')';
     }
     return $out;
 }
Beispiel #2
0
 /**
  * Returns the title of the given page
  *
  * @param string $url url to the page
  * @return string title
  */
 public function getTitle($url)
 {
     $opts = array('http' => array('timeout' => 3.5, 'method' => 'GET', 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12'));
     $context = stream_context_create($opts);
     if ($page = fopen($url, 'r', false, $context)) {
         stream_set_timeout($page, 3.5);
         $data = stream_get_meta_data($page);
         foreach ($data['wrapper_data'] as $header) {
             if (preg_match('/^Content-Type: ([^;]+)/', $header, $match) && !preg_match('#^(text/x?html|application/xhtml+xml)$#', $match[1])) {
                 $title = $match[1];
             }
         }
         if (!isset($title)) {
             $content = '';
             $tstamp = time() + 5;
             while ($chunk = fread($page, 64)) {
                 $data = stream_get_meta_data($page);
                 if ($data['timed_out']) {
                     $this->debug('Url Timed Out: ' . $url);
                     $this->errorStatus = true;
                     break;
                 }
                 $content .= $chunk;
                 // Check for timeout
                 if (time() > $tstamp) {
                     break;
                 }
                 // Try to read title
                 if (preg_match('#<title[^>]*>(.*)#is', $content, $m)) {
                     // Start another loop to grab some more data in order to be sure we have the complete title
                     $content = $m[1];
                     $loop = 2;
                     while (($chunk = fread($page, 64)) && $loop-- && !strstr($content, '<')) {
                         $content .= $chunk;
                         // Check for timeout
                         if (time() > $tstamp) {
                             break;
                         }
                     }
                     preg_match('#^([^<]*)#is', $content, $m);
                     $title = preg_replace('#\\s+#', ' ', $m[1]);
                     $title = trim($this->decode($title, $this->titleLength));
                     break;
                 }
                 // Title won't appear beyond that point so stop parsing
                 if (preg_match('#</head>|<body#i', $content)) {
                     break;
                 }
             }
         }
         fclose($page);
     } else {
         if (!$this->errorStatus) {
             $this->debug('Couldn\\t Open Url: ' . $url);
         }
     }
     if (empty($title)) {
         if ($this->errorStatus) {
             if (!$this->showErrors || empty($this->errorMessage)) {
                 return;
             }
             $title = $this->errorMessage;
             $this->errorStatus = false;
             $this->errorMessage = null;
         } else {
             $title = 'No Title';
         }
     }
     // Sanitise the title.
     $title = Phergie_Plugin_Helper_String::cleanString($title);
     return $title;
 }