<?php
/**
 * Checks status for yahoo user. According to online doc, we can use "http://opi.yahoo.com/online?u=<user>" to see the status.
 * As it's a picture (a gif), and as we only want a boolean at the end, we jujst have to check length of picture content.
 * Length for "online" is 140. Else, it's offline.
 * Requiered :
 * 1- PHP must be compiled with CURL support.
 * 2- well.. I guess that's all ;)
 * @author C. Jeanneret <cjeanneret@internux.ch>
 */

/*
Copyright (C) 2007 Jeanneret Internux

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

class checkYahoo {
  /**
   * Account we want to check
   *
   * @var string
   */
  var $account;
  /**
   * Status. Should be private.
   *
   * @var bool
   */
  var $status;
  /**
   * Check URL. Should be private.
   *
   * @var string
   */
  var $url;

  /**
   * Constructor.
   *
   * @param string $account
   * @return checkYahoo
   */
  function checkYahoo($account) {
    $this->account = $account;
    $this->url = 'http://216.155.194.208/online?u='.$this->account;
  }

  /**
   * Set user status.
   *
   */
  function setStatus() {
    $ch = curl_init($this->url);
    curl_setopt($ch, CURLOPT_URL, $this->url);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    ob_start();
    curl_exec($ch);
    $content = ob_get_contents();
    curl_close($ch);
    ob_get_clean();

    if (trim(strtolower(strip_tags($content))) != 'user not specified.') {
      $this->status = (strlen($content) == 140)? true : false ;
    }
  }

  /**
   * Return user status
   *
   * @return bool true if online.
   */
  function isOnline() {
    $this->setStatus();
    return $this->status;
  }
}
?>