<?php
include("./tuve.inc");


// Create new service for AMFPHP Remoting as Class
class tuve {
  var $mysqli;

  function tuve()  {
    # Connect to MySQL database....
    $this->mysqli = new mysqli(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);         
    # Check MySQL connection
    if (mysqli_connect_errno()) {
      # Dont use die (Fatal Error), return useful info to the client
      trigger_error("AMFPHP Remoting 'sample' class could not connect: " . mysqli_connect_error()); 
      }
    }

  /* Function For TUplayer */
  function pVid($vid) {
    $query = "SELECT file,count FROM videos WHERE vid = $vid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    $nCount = $return_array[0]['count'] + 1;
    $query = "UPDATE videos SET count = $nCount WHERE vid = $vid";
    $this->mysqli->query($query);
    return($return_array);

    }
  function sendFB($data) {
    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: TUve Feedback <tuve@ubixonline.com>' . "\r\n";

    mail("Christopher Olsen <cwolsen@domainatlantic.com>,Mark Iuzzolino <monsters@monstersoft.com>",$data['subject'] . " - " . $data['username'],$data['message'],$headers);
    }

  function getVideo($data) {
    $query = "SELECT * FROM videos WHERE vid = $data";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }


  function getArtists($data) {
    $sKey = $data['skey'];
    $uid = $data['uid'];

    if ($data['gid'] == 1)
      $query = "SELECT artist,aid FROM artists WHERE artist LIKE \"%$sKey%\" ORDER BY artist";
    else
      $query = "SELECT artist,aid FROM artists WHERE artist LIKE \"%$sKey%\" AND oid = $uid ORDER BY artist";

    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }


  function getUsers($data) {
    if ($data['gid'] == 1)
      $query = "SELECT username,uid FROM users ORDER BY username";
    else
      $query = "SELECT username,uid FROM users WHERE uid = " . $data['uid'];
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);

    }

  function getVideos($data) {
    $sKey = $data['skey'];
    $uid = $data['uid'];
    if ($data['gid'] == 1)
      $query = "SELECT artist,title,vid FROM videos WHERE artist LIKE \"%$sKey%\" OR title LIKE \"%$sKey%\" ORDER BY artist,title";
    else
      $query = "SELECT artist,title,vid FROM videos WHERE oid = $uid AND (artist LIKE \"%$sKey%\" OR title LIKE \"%$sKey%\") ORDER BY artist,title";

    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query [%query] error: " . $errno);
      }

    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }


  function getChans() {
    $query = "SELECT channel FROM channels";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database [$query] query error: " . $errno);
      }

    if ($result->num_rows == 0) {
      $return_array[] = array('channel' => '#General');
      }
    else {
      while ($row = $result->fetch_assoc()) {
        $return_array[] = $row;
        }
      }

    return($return_array);
    }


  function getInfo($data) {
    $query = "SELECT thumb,v.artist,title,sbio FROM videos v INNER JOIN artists a ON v.aid = a.aid WHERE vid = $data";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database [$query] query error: " . $errno);
      }

    if ($result->num_rows == 0) {
      $return_array[] = array('thumb' => 'UbixCube.swf','artist' => 'Unknown','title' => 'Unknown'); 
      }
    else {
      while ($row = $result->fetch_assoc()) {
        $return_array[] = $row;
        }
      }

    return($return_array);
    }

  function getFullInfo($data) {
    $query = "SELECT thumb,v.artist,title,bio,photo,description FROM videos v INNER JOIN artists a ON v.aid = a.aid WHERE v.vid = $data";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database [$query] query error: " . $errno);
      }
    if ($result->num_rows == 0) {

      $query = "SELECT thumb,artist,title,'NA' as bio,'/images/UbixCube.swf' AS photo FROM videos WHERE vid = $data";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database [$query] query error: " . $errno);
      }


      }

    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }


  function getNextMovie() {
    $query = "SELECT file,artist,title FROM videos";
    if (!$result = @$this->mysqli->query($query)) {
      $errno = $this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }

    $rn = rand(0,$result->num_rows);
    for ($i=0;$i < $rn;$i++)
      $row = $result->fetch_assoc();
  
    return($row);
    }

  function findSongs($data) {
    $skey = $data['skey'];
    $srating = $data['srating'];
    $sclass = $data['sclass'];
    $exclusive = $data['exclusive'];
    if ($data['inca'] == 2)
      $mType = " AND mType >= 1";
    else
      $mType = " AND mType = 1";

      $query = "SELECT vid,artist,title,length,thumb,genre FROM videos WHERE (artist like \"%$skey%\" OR title like \"%$skey%\") AND rating <= $srating$mType"; 
    if ($sclass != 0)
      $query .= " AND classification = $sclass";
    if ($exclusive != 0)
      $query .= " AND oid = $exclusive";

    $query .= " ORDER BY artist,title";
    
    if (!$result = @$this->mysqli->query($query)) {
      $errno = $this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database [$query] query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
       if ($row['length'] > 3600)
         $row['length'] = date("H:i:s",$row['length']);
       else
         $row['length'] = date("i:s",$row['length']);
       $return_array[] = $row;
       }
    return($return_array);
    }

  function findSongsVOD($data) {
    $skey = $data['skey'];
    $srating = $data['srating'];
    $sclass = $data['sclass'];
    $exclusive = $data['exclusive'];
    if ($data['inca'] == 2)
      $mType = " AND mType >= 1";
    else
      $mType = " AND mType = 1";

      $query = "SELECT vid,artist,title,length,thumb,genre,file FROM videos WHERE (artist like \"%$skey%\" OR title like \"%$skey%\") AND rating <= $srating$mType"; 
    if ($sclass != 0)
      $query .= " AND classification = $sclass";
    if ($exclusive != 0)
      $query .= " AND oid = $exclusive";

    $query .= " ORDER BY artist,title";
    
    if (!$result = @$this->mysqli->query($query)) {
      $errno = $this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database [$query] query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
       if ($row['length'] > 3600)
         $row['length'] = date("H:i:s",$row['length']);
       else
         $row['length'] = date("i:s",$row['length']);
       $return_array[] = $row;
       }
    return($return_array);
    }

  function userAuth($data) {
    $username = $data['username'];
    $password = $data['password'];
    $query = "SELECT uid,username,password,gid,active FROM users WHERE username LIKE '$username' AND password = '$password'";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }

    if ($result->num_rows == 0) {
      $return_array[] = array('active' => '0');
      }
    else {
      while ($row = $result->fetch_assoc()) {
        $return_array[] = $row;
        }
      $query = "UPDATE users SET laston = " . time() . " WHERE uid = " . $return_array[0]['uid'];
      $this->mysqli->query($query);
      }


    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }

  function getView($uid) {
    $query = "SELECT viewXML FROM users WHERE uid = $uid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }

  function getFriends($uid) {
    $query = "SELECT friendsXML FROM users WHERE uid = $uid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }


  function updateView($data) {
    $uid = $data['uid'];
    $viewXML = $data['viewXML'];
    $query = "UPDATE users SET viewXML = '$viewXML' WHERE uid = $uid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    return(null);
    }

  function getPublicView($username) {
    $query = "SELECT viewXML FROM users WHERE username = '$username'";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }
  function getPortal($pid) {
    $query = "SELECT portalXML,portalName FROM tmpP WHERE pid = $pid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }

  function cmpMail($data) {
    $query = "SELECT * FROM users WHERE uid = $data[mailTo]";
    $result = @$this->mysqli->query($query);
    $userInfo = $result->fetch_array(MYSQLI_ASSOC);

    $name = $userInfo['firstName'] . " " . $userInfo['lastName'];

    switch($data['pid']) {
      case -1:
        $podName = "Mega Pod";
        break;
      case -3:
        $podName = "Friends Pod";
        break;
      default:
        $query = "SELECT * FROM podz WHERE pid = $data[pid]";
        $result = @$this->mysqli->query($query);
        $podInfo = $result->fetch_array(MYSQLI_ASSOC);
        $podName = $podInfo['podName'];
        break;
      }

    return(array(array('mailTo' => $name,'toUID' => $data['mailTo'],'podName' => $podName,'pid' => $data['pid'])));
    }

  function getPod($pid) {
    $query = "SELECT * FROM podz WHERE pid = $pid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: [$query] " . $errno);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }

  function sendMail($data) {
    $query = "SELECT username FROM users WHERE uid = $data[toUID]";
    $result = $this->mysqli->query($query);
    $userInfo = $result->fetch_array(MYSQLI_ASSOC);

    $email = $userInfo['username'] . "@podz.ubixos.com";
    $from = "From: " . $data['fromName'] . " <" . $data['fromEmail'] . "@cupid.ubixos.com>";
    mail($email,$data['subject'],$data['message'],$from);
    
    return;
    }


  }
?>
