<?
/*
NOTE TO Vincent
the module config is ..
LoadModule =../modules/mod_defraga.php
mp3_root = /<path>/mp3_content
mp3_host = http://phpslave.com:8080/mp3_content/


*/
/*

DeFraga mp3 module
==========================

Copyright (C) 2002 3mediaweb.com mike defraga <mike@3mediaweb.com>

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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/
 class mod_defraga {

        function  mod_defraga() {

                global $conf;

                $this->modtype="url";
                $this->modname="mp3 server";
                $this->urls=array("/mp3");

        }
#<-=-=-=-=-=-=-=-=-[FUNCTIONS]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=->#
 function url(&$rq_err, &$out_contenttype, &$out_add_headers)
                 {
                  global $conf,$query_string;

				  $rq_err=200;
#<-=-=-=-=-=-=-=-=-[FUNCTIONS]-=-=-=-=-=->#
     function split_name($name)
                 {
                 if($name)
                    {
                    $t=split(".",$name);
                    if(is_array($t)){return urlencode($t[0]);}
                    }
                 }
#<-=-=-=-=-=-=-=-=-[FUNCTIONS]-=-=-=-=-=->#
   function directory_listing()
         {
         global $conf;
         DEFINE("WORK_FOLDER",$conf["global"]["mp3_root"][0]);
         $handle = opendir(WORK_FOLDER);
         while (false !== ($file = readdir($handle))){if($file != "." && $file != ".."){if(ereg("mp3|MP3",$file))
               {$list[]=$file;}}}
               closedir($handle);
               if(is_array($list)){return $list;}
         }
   function stream_type($type,$file)
            {
            global $conf;
            if($type=="rand")
               {
               $list = directory_listing();
               srand ((float) microtime() * 10000000);
               $items = array_rand ($list, "2");
               $s.="#EXTM3U\r\n";
               $x=1;
               foreach($items as $var=>$val)
                                       {
                                       if($x=="1")
                                          {
                              $s.="#EXTINF:$list[$val],".split_name($list["$val"])."\n" ;
                              $s.=$conf["global"]["mp3_host"][0].urlencode($list["$val"])."\n";
                                          $x++;
                                          }
                                       }
               }
               return($s);
            }
#<-=-=-=-=-=-=-=-=-[FUNCTIONS]-=-=-=-=-=->#
             function sort_query_string()
                      {
                      global $query_string;
                      $good_strings=array("file","type");
                     if(ereg("&",$query_string))
                         {
                         $t=split("&",$query_string);
                         if(is_array($t))
                            {
                            foreach($t as $var=>$val)
                                    {if($val){
                                       $b=split("=",$val);
                                       if(is_array($b)){if(in_array("$b[0]",$good_strings))
                                             {
                                             $d=$b[0];
                                             $return[$d]=stripslashes(trim(escapeshellcmd(strip_tags($b[1]))));
                                             }
                                          }
                                       }
                                    }
                            return ($return);
                            }
                         }
                         else
                            {
                            if(ereg("=",$query_string))
                               {
                                $b=split("=",$query_string);
                                       if(is_array($b)){if(in_array("$b[0]",$good_strings))
                                             {
                                             $d=$b[0];
                                             $return[$d]=stripslashes(trim(escapeshellcmd(strip_tags($b[1]))));
                                             }
                                          }
                               }
                            return($return);
                            }
                      }
#<-=-=-=-=-=-=-=-=-[FUNCTIONS]-=-=-=-=-=->#
function list_main()
         {
         global $conf;
          $files=directory_listing();
          $s.="#EXTM3U\r\n";
          foreach($files as $var=>$val)
                            {
          $s.="#EXTINF:$var,".split_name($val)."\n" ;
          $s.=$conf["global"]["mp3_host"][0].urlencode($val)."\n";
                            }
          return($s);
         }
#<-=-=-=-=-=-=-=-=-[FUNCTIONS]-=-=-=-=-=->#
                        if($query_string)
                           {
                           #$out_contenttype="text/html ";
                           $sx = sort_query_string();
                           foreach($sx as $var=>$val){$$var = $val;}
                           $out_contenttype="audio/x-mpegurl\r\nContent-Disposition: attachment; filename=defraga_mod-".substr(time(),-4).".m3u";
                           return stream_type($type,$file);
                           }
                           else
                               {
                               $out_contenttype="audio/x-mpegurl\r\nContent-Disposition: attachment; filename=defraga_mod-".substr(time(),-4).".m3u";
                               $s=list_main();
                               return($s);
                                }



           }


 }
?>