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

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

  function tuveAdmin()  {
    # 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 checkNewMail($uid) {
    $query = "SELECT mid FROM mail WHERE oid = $uid AND folder = 0 AND status = 1 LIMIT 1";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
      }

    if ($result->num_rows == 1)
      return(1);
    else
      return(0);
    }

  function schedVid($data) {
    $sTime = strtotime($data['sTime']);
    $cid   = $data['cid'];
    $vid   = $data['sVid'];
    $query = "INSERT INTO schedule (cid,vid,time) VALUES($cid,$vid,$sTime)";
    $this->mysqli->query($query);
    $query = "UPDATE channels SET updated = 1 WHERE cid = $cid";
    $this->mysqli->query($query);
    }

   function requestBuddy($data) {
     $rUser = $data['rUser'];
     $rUID  = $data['rUID'];
     $username = $data['rName'];

     $query = "SELECT uid,notifications,firstname,lastname,email FROM users WHERE username LIKE '$rUser'";
     if (!$result=@$this->mysqli->query($query)) {
       $errno=$this->mysqli->errno;
       $this->mysqli->close();
       trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
       }

     if ($result->num_rows == 0) {
       $pdate = time();
       $query = "INSERT INTO mail (oid,sid,subject,message,date) VALUES($rUID,$rUID,'Friend Request Failed','Friend Request Failed<br>\nNo Such User: $rUser<br>\n\n',$pdate)";
       $this->mysqli->query($query);
       }
     else {
       $row = $result->fetch_assoc();
       $uid = $row['uid'];
       $notifications = $row['notifications'];
       $fname         = $row['firstname'];
       $lname         = $row['lastname'];
       $email         = $row['email'];

       $pdate = time();
       $message = str_replace("\r","<BR>",$data['rMessage']);
       $message .= "<BR><BR><A HREF=\"event:200-$rUID-1\">Approve</A> <A HREF=\"200-$rUID-0\">Deny</A><BR><BR>\n";
       $message = $this->mysqli->real_escape_string($message);
       $query = "INSERT INTO friends (oid,fid) VALUES($rUID,$uid)";
       $this->mysqli->query($query);
       $query = "INSERT INTO friends (fid,oid) VALUES($rUID,$uid)";
       $this->mysqli->query($query);
       $query = "INSERT INTO mail (oid,sid,subject,message,date) VALUES($uid,$rUID,'New Friend Request From $username','$message',$pdate)";
       $this->mysqli->query($query);
       /* Send Message To User */
       if ($notifications[N_FREQUEST] == 1)
         send_mail("$fname $lname <$email>","$fname,<BR>You have received a new friends.<BR><BR>Follow this link <A HREF=\"http://www.tuve.tv/launch.php\">http://www.tuve.tv/launch.php</A> to see your new friends request.<BR><BR>Sincerely,<BR>TUve Support","You have a new friends request from $username","support@tuve.tv","TUve Support");
       }
     }


  function loadWidget($wid) {
    $query = "SELECT * FROM widgets WHERE wid = $wid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
      }
    $row = $result->fetch_assoc();
    $row['ts'] = time();
    return($row);
    }

  function deleteVid($vid) {
    $query = "UPDATE videos SET status = 1 WHERE vid = $vid";
    $this->mysqli->query($query);
    }

  function findUsers($data) {
    $skey = $data['skey'];

    $query = "SELECT username,main_image,profile_image,caption,city,state_prov FROM users WHERE username LIKE '%$skey%'";

    if ($data['gender'] == 1)
      $query .= " AND gender = 1";
    else if ($data['gender'] == 2)
      $query .= " AND gender = 2";

    $syear = 2007 - (18 + $data['sAge']);
    $eyear = 2007 - (18 + $data['eAge']);

    $query .= " AND (byear <= $syear AND byear >= $eyear)";


    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
      }
    while ($row = $result->fetch_assoc()) {
      $imIndex = $row['profile_image'];
      if ($imIndex == 0)
        $row['my_image'] = $row['main_image'] . "-small.png";
      else {
        switch($imIndex) {
          case 1:
            $row['my_image'] = "http://www.ubixonline.com/images/sb/none.gif-small.png";
          case 2:
            $row['my_image'] = "http://www.ubixonline.com/images/sb/female_icon.png-small.png";
          case 3:
            $row['my_image'] = "http://www.ubixonline.com/images/sb/male_icon.png-small.png";
            }
          }
      $row['location'] = $row['city'] . ", " . $row['state_prov'];

      $return_array[] = $row;
      }
    return($return_array);
    }

  function finBroad($file) {
    $query = "UPDATE videos SET status = 5 WHERE file = '$file.flv'";
    $this->mysqli->query($query);
    }

  function addVid($data) {
    $artist = $data['username'];
    $title  = $data['title'];
    $file   = $data['file']; 
    $oid    = $data['uid'];
    $added  = time();
    $query = "INSERT INTO videos (artist,title,genre,file,rating,classification,oid,count,mType,added,status) VALUES('$artist','$title','Recorded Video','$file.flv',2,6,$oid,0,1,$added,6)";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
      }

    }

  function updateMsgFolder($data) {
    $mid = $data['mid'];
    $folder = $data['folder'];

    $query = "UPDATE mail SET folder = $folder WHERE mid = $mid";
    $this->mysqli->query($query);
    }

  function mailRead($mid) {
    $query = "UPDATE mail SET status = 2 WHERE mid = $mid";
    $this->mysqli->query($query);
    }

  function updatePass($data) {
    $uid = $data['uid'];
    $pass = $data['pass'];
    $query = "UPDATE users SET password = '$pass' WHERE uid = $uid";
    $this->mysqli->query($query);
    }

  function approveFriend($data) {
    $fid      = $data['fid'];
    $oid      = $data['oid'];
    $approved = $data['approved'];

    if ($approved == 0) {
      $query = "DELETE FROM friends WHERE fid = $fid AND oid = $oid AND approved = 0";
      $this->mysqli->query($query);
      $query = "DELETE FROM friends WHERE fid = $oid AND oid = $fid AND approved = 0";
      $this->mysqli->query($query);

      $pdate = time();
      $query = "INSERT INTO mail (oid,sid,date,subject,message,folder) VALUES($fid,$oid,$pdate,'Friend Request Denied','This Friend Request Has Been Denied',0)";
      $this->mysqli->query($query);

      $query = "SELECT firstname,lastname,email FROM users WHERE uid = $fid";
      if (!$result=@$this->mysqli->query($query)) {
        $errno=$this->mysqli->errno;
        $this->mysqli->close();
        trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
        }
      $row = $result->fetch_assoc();
      send_mail("$row[firstname] $row[lastname] <$row[email]>","Your request to make $data[username] your friend has been denied","Friend Request Denied","support@tuve.tv","TUve Support");

      }
    else {
      $query = "UPDATE friends SET approved = 1 WHERE approved = 0 AND fid = $fid AND oid = $oid";
      $this->mysqli->query($query);
      $query = "UPDATE friends SET approved = 1 WHERE approved = 0 AND fid = $oid AND oid = $fid";
      $this->mysqli->query($query);

      $pdate = time();
      $query = "INSERT INTO mail (oid,sid,date,subject,message,folder) VALUES($fid,$oid,$pdate,'Friend Request Approved','This Friend Request Has Been Approved',0)";
      $this->mysqli->query($query);
      
      $query = "SELECT firstname,lastname,email FROM users WHERE uid = $fid";
      if (!$result=@$this->mysqli->query($query)) {
        $errno=$this->mysqli->errno;
        $this->mysqli->close();
        trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
        }
      $row = $result->fetch_assoc();
      send_mail("$row[firstname] $row[lastname] <$row[email]>","Your request to make $data[username] your friend has been approved","Friend Request Approved","support@tuve.tv","TUve Support");

      }
    }

  function getMail($uid) {
    $query = "SELECT mid,subject,username,date,message,sid,folder,status FROM mail m INNER JOIN users u ON m.sid = u.uid WHERE m.oid = $uid ORDER BY mid DESC";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
      }
    while ($row = $result->fetch_assoc()) {
      $row['date'] = date("m/d/Y g:i A",$row['date']);
      $return_array[] = $row;
      }
    return($return_array);
    }


  function getProfile($user) {
    $query = "SELECT username,uid,gid,bday,bmonth,byear,about_me,caption,main_image,profile_image,gender,city,state_prov,postal_code FROM users WHERE username LIKE '$user'";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . " query: " . $query);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }


  function getUserInfo($uid) {
    $query = "SELECT username,uid,gid,bday,bmonth,byear,about_me,caption,main_image,profile_image,gender,city,state_prov,postal_code 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 . " query: " . $query);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    } 

  function updateUserInfo($data) {
    $uid = $data['uid'];

    $caption       = $this->mysqli->real_escape_string($data['caption']);
    $about_me      = $this->mysqli->real_escape_string($data['about_me']);
    $city          = $this->mysqli->real_escape_string($data['city']);
    $state_prov    = $this->mysqli->real_escape_string($data['state_prov']);
    $zip_postal    = $this->mysqli->real_escape_string($data['zip_postal']);
    $gender        = $data['gender'];
    $bday          = $data['bday'];
    $bmonth        = $data['bmonth'];
    $byear         = $data['byear'];
    $profile_image = $data['profile_image'];
    
    if ($caption == "")
      $caption = "No Caption Available";

    $query = "UPDATE users SET profile_image = $profile_image";

    if ($caption != "")
      $query .= ",caption='$caption'";
 
    if ($about_me != "")
      $query .= ",about_me='$about_me'";

    if ($city != "")
      $query .= ",city='$city'";

    if ($sate_prove != "")
      $query .= ",state_prov='$state_prov'";

    if ($zip_postal != "")
      $query .= ",zip_postal='$zip_postal'";

    if ($gender > 0)
      $query .= ",gender = $gender";

    if ($bday > 0)
      $query .= ",bday = $bday";

    if ($bmonth > 0)
      $query .= ",bmonth = $bmonth";
 
    if ($byear > 1900)
      $query .= ",byear = $byear";
    $query .= " WHERE uid = $uid";
    $this->mysqli->query($query);
    //trigger_error("AMFPHP Remoting database SELECT query error: " . $errno . "Query: $query");
    }

  /* Authenticates a user into the system */
  function userAuth($data) {
    $username = $data['username'];
    $password = $data['password'];
    $query = "SELECT username,uid,gid,profile_image,main_image FROM users WHERE username = '$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);
      }
/*
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
*/
    $row = $result->fetch_assoc();

    $imIndex = $row['profile_image'];
    if ($imIndex == 0)
      $row['my_image'] = $row['main_image'] . "-small.png";
    else {
      switch($imIndex) {
        case 1:
          $row['my_image'] = "http://www.ubixonline.com/images/sb/none.gif-small.png";
        case 2:
          $row['my_image'] = "http://www.ubixonline.com/images/sb/female_icon.png-small.png";
        case 3:
          $row['my_image'] = "http://www.ubixonline.com/images/sb/male_icon.png-small.png";
        }
      }

    return($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,length FROM videos WHERE (artist LIKE \"%$sKey%\" OR title LIKE \"%$sKey%\") AND status = 0 ORDER BY artist,title";
    else
      $query = "SELECT artist,title,vid,length FROM videos WHERE oid = $uid AND (artist LIKE \"%$sKey%\" OR title LIKE \"%$sKey%\") AND status = 0 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()) {
      $row['length'] = makeTime($row['length']);
      $return_array[] = $row;
      }
    return($return_array);
    }

  function getUserChans($data) {
    $sKey = $data['skey'];
    $uid = $data['uid'];
    if ($data['gid'] == 1)
      $query = "SELECT channel,cid FROM channels WHERE channel LIKE \"%$sKey%\" ORDER BY channel";
    else
      $query = "SELECT channel,cid FROM channels WHERE oid = $uid AND channel LIKE \"%$sKey%\" ORDER BY channel";

    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 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 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);
      }
    $row = $result->fetch_assoc();
    return($row);
    }
  function getSchedule($data) {
    $stime = strtotime($data[date]);
    $etime = $stime + 86400;

    if ($data['con'] > 1) {
      $query = "DELETE FROM schedule WHERE sid = $data[con]";
      $this->mysqli->query($query);
      $query = "UPDATE channels SET updated = 1 WHERE cid = $data[cid]";
      $this->mysqli->query($query);
      }

    $query = "SELECT sid,time,artist,title,length FROM schedule s INNER JOIN videos v ON s.vid = v.vid WHERE cid = $data[cid] AND (time >= $stime AND time < $etime) ORDER BY time";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }
    if ($data['con'] == 1) {
     $prev['sid'] = 0;
     $prev['length'] = 0;
     $prev['time'] = 0;
      while ($row = $result->fetch_assoc()) {
        $tm = $row['time'] - ($prev['time'] + $prev['length']);
        if ($tm > (0 - $row['time']) && $tm < 60) {
          $row['time'] = $prev['time'] + floor($prev['length']) + 1;
          $query = "UPDATE schedule SET time = $row[time] WHERE sid = $row[sid]";
          $this->mysqli->query($query);
          }
        $prev['sid'] = $row['sid'];
        $prev['length'] = $row['length'];
        $prev['time'] = $row['time'];
        $row['time'] = date("H:i:s",$row['time']);
        $row['length'] = makeTime($row['length']);
        $return_array[] = $row;
        }
      }
    else {
      while ($row = $result->fetch_assoc()) {
        $row['time'] = date("H:i:s",$row['time']);
        $row['length'] = makeTime($row['length']);
        $return_array[] = $row;
        }
      }
    return($return_array);
    }
 
  function getChannel($data) {
    $query = "SELECT * FROM channels WHERE channel = '$data'";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: " . $errno);
      }

    $row = $result->fetch_assoc();
    return($row);
    }

  function addArtist($data) {
    $nArtist = $data['artist'];
    $nOID    = $data['oid'];

    $query = "INSERT INTO artists (artist,photo,oid) VALUES(\"$nArtist\",'http://www.ubixonline.com/images/UbixCube.swf',$nOID)";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database INSERT query [$query] error: " . $errno);
      }

    }

  function getArtist($data) {
    $query = "SELECT * FROM artists WHERE aid = $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 updateVideo($data) {
    $nRating = $data['rating'];
    $nClass  = $data['classification'];
    $nGenre  = addslashes($data['genre']);
    $nTitle  = $data['title'];
    $nLength = $data['length'];
    $nArtist = $data['artist'];
    $nThumbFrame = $data['thumbFrame'];
    $nAID    = $data['aid'];
    $nOID    = $data['oid'];
    $nDescription = addslashes($data['description']);
   
    $vid     = $data['vid'];
    $query = "UPDATE videos SET rating = $nRating,classification = $nClass,genre = '$nGenre',title = \"$nTitle\",length = '$nLength',artist = \"$nArtist\",thumbFrame = '$nThumbFrame',aid = $nAID,description = \"$nDescription\",oid = $nOID WHERE vid = $vid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database UPDATE query [$query] error: " . $errno);
      return(0x1);
      }
    return(0x0);
    }

  function updateChannel($data) {
    $nRating = $data['rating'];
    $nClass  = $data['classification'];
    $nTopic  = $data['topic'];
    $nOID    = $data['oid'];
    $nRandom = $data['random'];
    $nQueue  = $data['queue'];
    $nExclusive = $data['exclusive'];
    $nTime      = $data['time'];
    $nDescription = addslashes($data['description']);
    $channel     = $data['channel'];

    $query = "UPDATE channels SET rating = $nRating,classification = $nClass,topic = \"$nTopic\",oid = $nOID,random = $nRandom,queue = $nQueue,exclusive = $nExclusive,time = $nTime,description = '$nDescription',updated = 1 WHERE channel = '$channel'";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database UPDATE query [$query] error: " . $errno);
      return(0x1);
      }
    return(0x0);
    }

  function updateArtist($data) {
    $nPhoto = $data['photo'];
    $nArtist = $data['artist'];
    $nBio    = addslashes($data['description']);
    $nOID    = $data['oid'];
    $aid     = $data['aid'];
    $nsBio   = substr($nBio,0,252) . "...";

   $query = "UPDATE artists SET artist = \"$nArtist\",photo = \"$nPhoto\",bio = \"$nBio\",oid = $nOID,sbio = \"$nsBio\" WHERE aid = $aid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database UPDATE query [$query] error: " . $errno);
      return(0x1);
      }
    return(0x0);
    }


  function regenThumb($data) {
    $gentime = $data['time'];
    $vid = $data['vid'];
    /*
    $file = $data['file'];
    $cmd = "/usr/local/bin/ffmpeg -i \"/usr/web/sites/ubixonline.com/docroot/streams/$file\" -t 0.001 -ss $gentime -vframes 1 -f mjpeg /tmp/1.jpg";
    #$cmd = "/usr/local/bin/ffmpeg -i '/usr/web/sites/ubixonline.com/docroot/streams/$file' -t 0.001 -ss $gentime -vframes 1 -f mjpeg -s 200x150 /tmp/1.jpg";
    system($cmd);
    $cmd = "/bin/mv /tmp/1.jpg \"/usr/web/sites/ubixonline.com/docroot/images/$file.jpg\"";
    system($cmd);
    */
    $fp = fopen("http://monkey.ubixos.com:6969/genthumb.php?data[vid]=$vid&data[gentime]=$gentime","r");
    $file = fread($fp,1024);
    $query = "UPDATE videos SET thumbFrame = '$gentime' WHERE vid = $vid";
    $this->mysqli->query($query);

    return("$file?" . time());
    }

  function chanReg($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 Manager <tuve@ubixonline.com>' . "\r\n";

    $message = "Channel: " . $data['channel'] . "<br>\n";
    $message .= "Description: " . $data['description'] . "<br>\n";
    $message .= "User: " . $data['username'] . "<br<BR>\n";

    mail("Christopher Olsen <cwolsen@domainatlantic.com>,Mark Iuzzolino <monsters@monstersoft.com>","New Chan Reg",$message,$headers);
    }



// Old Stuff Here

  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);
    }


  /* Returns Info */
  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) {
      $row = array('thumb' => 'UbixCube.swf','artist' => 'Unknown','title' => 'Unknown');
      }
    else {
      $row = $result->fetch_assoc();
      }

    return($row);
    }

  /* Get Full Video Information */
  function getFullInfo($vid) {
    $query = "SELECT thumb,a.artist,title,bio,photo,description FROM videos v INNER JOIN artists a ON v.aid = a.aid WHERE v.vid = $vid";
    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,'No Description' as description,'/images/UbixCube.swf' AS photo FROM videos WHERE vid = $vid";
      if (!$result=@$this->mysqli->query($query)) {
        $errno=$this->mysqli->errno;
        $this->mysqli->close();
        trigger_error("AMFPHP Remoting database [$query] query error: " . $errno);
        }
      }

    $row = $result->fetch_assoc();
    return($row);
    } /* End getFullInfo */


  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'];

    if ($sclass == "0")
      $query = "SELECT * FROM videos WHERE (artist like \"%$skey%\" OR title like \"%$skey%\") AND rating <= $srating ORDER BY artist";
    else
      $query = "SELECT * FROM videos WHERE (artist like \"%$skey%\" OR title like \"%$skey%\") AND rating <= $srating AND class = $sclass ORDER BY artist";
    
    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 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);
    }

  /* Send Mail For TUve */
  function sendMail($data) {
    $oid = $data['tid'];
    $sid = $data['fid'];
    $from = $data['from'];

    if ($oid == "")
      $oid = 78;
    if ($sid == "")
      $sid = 78;

    $subject = $this->mysqli->real_escape_string($data['subject']);
    $message = $this->mysqli->real_escape_string($data['message']);

    $pdate = time();

    $query = "INSERT INTO mail (oid,sid,date,subject,message,folder) VALUES($oid,$sid,$pdate,'$subject','$message',0)";
    if (!$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: [$query] " . $errno);
      }

    $query = "INSERT INTO mail (oid,sid,date,subject,message,folder) VALUES($sid,$oid,$pdate,'$subject','$message',1)";
    if (!$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: [$query] " . $errno);
      }


    $query = "SELECT firstname,lastname,email,notifications FROM users WHERE uid = $oid";
    if (!$result=@$this->mysqli->query($query)) {
      $errno=$this->mysqli->errno;
      $this->mysqli->close();
      trigger_error("AMFPHP Remoting database SELECT query error: [$query] " . $errno);
      }
    $row = $result->fetch_assoc();

    if ($row['notifications'][N_MAIL])
      send_mail("$row[firstname] $row[lastname] <$row[email]>","You have a new message waiting for you.<br><br>Message Sender: $from<br>Message Subject: $data[subject]<br><br>Please follow this link <A HREF=\"http://www.tuve.tv/\">http://www.tuve.tv/</A> to log into your account.<br><br>Sincerely,<br>TUve Staff<br>","New Message On TUve","support@tuve.tv","TUve Support");
    
    return;
    } /* End Send Mail for TUve */
  /* Get Picture */
  function getPic($data) {
    $query = "SELECT profile_image,main_image FROM users WHERE username LIKE '$data'";

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

    $row = $result->fetch_assoc();

    $imIndex = $row['profile_image'];
    if ($imIndex == 0)
      return($row['main_image'] . "-small.png");
    else {
      switch($imIndex) {
        case 1:
          return("http://www.ubixonline.com/images/sb/none.gif-small.png");
        case 2:
          return("http://www.ubixonline.com/images/sb/female_icon.png-small.png");
        case 3:
          return("http://www.ubixonline.com/images/sb/male_icon.png-small.png");
        }
      }
    }

  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' => 'TUvé');
      }
    else {
      while ($row = $result->fetch_assoc()) {
        $return_array[] = $row;
        }
      }
    return($return_array);
    }

  function getFriends($uid) {
    $query = "SELECT username,uid FROM friends f INNER JOIN users u ON f.fid = u.uid WHERE oid = $uid ORDER BY 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);
    }


  }
?>
