<?php
include("/usr/web/sites/ubixonline.com/docroot/amfphp/services/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 getMail($uid) {
    $query = "SELECT mid,subject,username,date,message,sid FROM mail m INNER JOIN users u ON m.sid = u.uid WHERE m.oid = $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()) {
      $row['date'] = date("H:I:s m/d/Y",$row['date']);
      $return_array[] = $row;
      }
    return($return_array);
    }


  function getProfile($user) {
    $query = "SELECT username,uid,gid,bday,bmonth,byear,about_me,caption,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,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'];
    
    if ($caption == "")
      $caption = "No Caption Available";

    $query = "UPDATE users SET 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 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;
      }
    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($data) {
    $sKey = $data['skey'];
    $uid = $data['uid'];
    if ($data['gid'] == 1)
      $query = "SELECT channel FROM channels WHERE channel LIKE \"%$sKey%\" ORDER BY channel";
    else
      $query = "SELECT channel 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);
      }
    while ($row = $result->fetch_assoc()) {
      $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);
      }
    while ($row = $result->fetch_assoc()) {
      $return_array[] = $row;
      }
    return($return_array);
    }

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

  function getInfo($data) {
    $query = "SELECT thumb,artist,title 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 getFullInfo($data) {
    $query = "SELECT thumb,a.artist,title,bio,photo FROM videos v INNER JOIN artists a ON v.artist = a.artist 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'];

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

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


  }
?>
