<?
/*
=====================================================
 Volkov NET!! - by SaNcHeS Group 
-----------------------------------------------------
 http://AllForCS.RU/
-----------------------------------------------------
 Copyright (c) 2009 SaNcHeS Group
=====================================================
 Данный код защищен авторскими правами
=====================================================
 Файл: generator.php
-----------------------------------------------------
 Назначение: Генератор страниц
=====================================================
*/

@set_time_limit(999999990);

function generate_pop_mp3(){
global $bytes,$WWW;
$sql = @mysql_query("SELECT * FROM `m3_mp3`  WHERE `active` = '1' ORDER BY `count` DESC LIMIT 5");
if ($sql && @mysql_num_rows($sql)>0){
$i=0;$to_file="";
while ($row = @mysql_fetch_array($sql, MYSQL_ASSOC)){
$i++;
$number=$row['id'];
if ($i != 5){$to_file.="&nbsp;&raquo; <a href=\"".$WWW."pages/".get_dir($number)."/".$number.".shtml\">".unsafe_str($row['artist'])." - ".unsafe_str($row['track'])."</a><br>";}else
{$to_file.="&nbsp;&raquo; <a href=\"".$WWW."pages/".get_dir($number)."/".$number.".shtml\">".unsafe_str($row['artist'])." - ".unsafe_str($row['track'])."</a>";}
}}
$fp = fopen ("./../html/top.html", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}


function generate_new_mp3(){
global $bytes,$WWW;
$sql = @mysql_query("SELECT * FROM `m3_mp3` WHERE `active` = '1' ORDER BY `id`  DESC LIMIT 5");
if ($sql && @mysql_num_rows($sql)>0){
$i=0;$to_file="";
while ($row = @mysql_fetch_array($sql, MYSQL_ASSOC)){
$i++;
$number=$row['id'];
if ($i != 5){$to_file.="&nbsp;&raquo; <a href=\"".$WWW."pages/".get_dir($number)."/".$number.".shtml\">".unsafe_str($row['artist'])." - ".unsafe_str($row['track'])."</a><br>";}else
{$to_file.="&nbsp;&raquo; <a href=\"".$WWW."pages/".get_dir($number)."/".$number.".shtml\">".unsafe_str($row['artist'])." - ".unsafe_str($row['track'])."</a>";}
}}
$fp = fopen ("./../html/new.html", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}

function generate_viewers($s_start,$s_stop){
global $bytes;
if (($_start == 0) and ($s_stop == "end"))
{
for ($i=0; $i<1000; $i++){
if (is_dir("./../pages/".$i)){
remove_all("./../pages/".$i);}
}}
if ($s_stop == "end"){$sql_v = @mysql_query("SELECT * FROM `m3_mp3` WHERE `active` = '1' AND `id` >= '".$s_start."'");}else{$sql_v = @mysql_query("SELECT * FROM `m3_mp3` WHERE `id` >= '".$s_start."' AND `id` <= '".$s_stop."' AND `active` = '1'");}
if ($sql_v && @mysql_num_rows($sql_v)>0){
$i=0;$to_file="";

// Читаем шаблон
$fp = fopen ("./../template/viewer.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);
$to_file="";
while ($row = @mysql_fetch_array($sql_v, MYSQL_ASSOC)){
$i++;
$number=$row['id'];
get2tag($number);
$to_file=tag2html($buffer,"/");
@mkdir("./../pages/".get_dir($number));
$fp = fopen ("./../pages/".get_dir($number)."/".$number.".shtml", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}}}


function generate_lists($bukva){
global $bytes,$WWW, $LIST_DATA;
// Читаем шаблон
$fp = fopen ("./../template/list.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);
if ($bukva != ""){$r=2;}else{$r=56;}
for ($i=1; $i<$r; $i++){
$LIST_DATA="";
$view=$i;
switch ($view) {
  case 1: $view="1"; break;
  case 2: $view="a"; break;
  case 3: $view="b"; break;
  case 4: $view="c"; break;
  case 5: $view="d"; break;
  case 6: $view="e"; break;
  case 7: $view="f"; break;
  case 8: $view="g"; break;
  case 9: $view="h"; break;
  case 10: $view="i"; break;
  case 11: $view="j"; break;
  case 12: $view="k"; break;
  case 13: $view="l"; break;
  case 14: $view="m"; break;
  case 15: $view="n"; break;
  case 16: $view="o"; break;
  case 17: $view="p"; break;
  case 18: $view="q"; break;
  case 19: $view="r"; break;
  case 20: $view="s"; break;
  case 21: $view="t"; break;
  case 22: $view="u"; break;
  case 23: $view="v"; break;
  case 24: $view="w"; break;
  case 25: $view="x"; break;
  case 26: $view="y"; break;
  case 27: $view="z"; break;
  case 28: $view="а"; break;
  case 29: $view="б"; break;
  case 30: $view="в"; break;
  case 31: $view="г"; break;
  case 32: $view="д"; break;
  case 33: $view="е"; break;
  case 34: $view="ж"; break;
  case 35: $view="з"; break;
  case 36: $view="и"; break;
  case 37: $view="к"; break;
  case 38: $view="л"; break;
  case 39: $view="м"; break;
  case 40: $view="н"; break;
  case 41: $view="о"; break;
  case 42: $view="п"; break;
  case 43: $view="р"; break;
  case 44: $view="с"; break;
  case 45: $view="т"; break;
  case 46: $view="у"; break;
  case 47: $view="ф"; break;
  case 48: $view="х"; break;
  case 49: $view="ц"; break;
  case 50: $view="ш"; break;
  case 51: $view="щ"; break;
  case 52: $view="э"; break;
  case 53: $view="ю"; break;
  case 54: $view="я"; break;
  case 55: $view="ч"; break;
}
if ($bukva != ""){$view=strtolower($bukva);}

// SELECT * FROM m3_mp3 WHERE artist LIKE 'a%';

if ($view == "1"){
$leet = mysql_query("SELECT * FROM `m3_mp3` WHERE (SUBSTRING(LCASE(`artist`),1,1) = '1' OR SUBSTRING(LCASE(`artist`),1,1) = '2' OR SUBSTRING(LCASE(`artist`),1,1) = '3' OR SUBSTRING(LCASE(`artist`),1,1) = '4' OR SUBSTRING(LCASE(`artist`),1,1) = '5' OR SUBSTRING(LCASE(`artist`),1,1) = '6' OR SUBSTRING(LCASE(`artist`),1,1) = '7' OR SUBSTRING(LCASE(`artist`),1,1) = '8' OR SUBSTRING(LCASE(`artist`),1,1) = '9' OR SUBSTRING(LCASE(`artist`),1,1) = '0') AND `active` = '1' ORDER BY `artist` ASC;");
}else{
$leet = mysql_query("SELECT * FROM `m3_mp3` WHERE (SUBSTRING(LCASE(`artist`),1,1) = '".$view."') AND `active` = '1' ORDER BY `artist` ASC;");
}
$zh=0;
while ($row = mysql_fetch_array($leet, MYSQL_ASSOC))
{

$zh=$zh+1;
$w_id=$row['id'];
$w_artist=unsafe_str($row['artist']);
$w_track=unsafe_str($row['track']);
$w_size=$row['size'];
$w_uploader=$row['uploader'];
$w_date=$row['date'];
$v_link=$WWW."pages/".get_dir($w_id)."/".$w_id.".shtml";

$LIST_DATA.="&nbsp;&raquo; <a href=\"".$v_link."\">".$w_artist." - ".$w_track."</a><br>";

}

$to_file=tag2html($buffer,"/");
$fp = fopen ("./../m3_lists/".$i.".shtml", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
mysql_free_result($leet);
}

}


function generate_guest(){
global $bytes,$WWW,$WWW_TITLE,$THIS_GUEST,$THIS_ABOUT;
// Читаем шаблон
$fp = fopen ("./../template/blank.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);

$THIS_GUEST=2;$THIS_ABOUT=0;
$WWW_TITLE="Гостевая книга";
$to_file=tag2html($buffer,"search");

$fp = fopen ("./../guest.php", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
@chmod("./../guest.php",0755);

}


function all_null(){
@mysql_query("UPDATE `m3_mp3` SET `count` = '0'");
}

function generate_about(){
global $bytes,$WWW,$WWW_TITLE,$THIS_GUEST,$THIS_ABOUT;
// Читаем шаблон
$fp = fopen ("./../template/blank.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);

$THIS_GUEST=0;$THIS_ABOUT=1;
$WWW_TITLE="О проекте";
$to_file=tag2html($buffer,"/");

$fp = fopen ("./../about.shtml", "w+");
fwrite ($fp, $to_file);
fclose ($fp);

}


function generate_search(){
global $bytes,$WWW,$SERV,$WWW_TITLE,$THIS_GUEST,$THIS_ABOUT;
// Читаем шаблон
$fp = fopen ("./../template/blank.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);

$THIS_GUEST=0;$THIS_ABOUT=0;
$WWW_TITLE="Поиск";
$to_file=tag2html($buffer,"search");

$fp = fopen ("./../search.php", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}

function generate_404(){
global $bytes,$WWW,$SERV,$WWW_TITLE,$THIS_GUEST,$THIS_ABOUT,$GUEST_DATA,$I404;
// Читаем шаблон
$fp = fopen ("./../template/blank.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);

$THIS_GUEST=1;$THIS_ABOUT=0;
$WWW_TITLE="404: Страница не найдена";
$GUEST_DATA="<center><br><br>$I404<br><br><br></center>";
$to_file=tag2html($buffer,"/");

$fp = fopen ("./../404.shtml", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}


function generate_403(){
global $bytes,$WWW,$SERV,$WWW_TITLE,$THIS_GUEST,$THIS_ABOUT,$GUEST_DATA,$I403;
// Читаем шаблон
$fp = fopen ("./../template/blank.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);

$THIS_GUEST=1;$THIS_ABOUT=0;
$WWW_TITLE="403: Доступ запрещен";
$GUEST_DATA="<center><br><br>$I403<br><br><br></center>";
$to_file=tag2html($buffer,"/");

$fp = fopen ("./../403.shtml", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}


function generate_register(){
global $bytes,$WWW,$SERV,$WWW_TITLE,$THIS_GUEST,$THIS_ABOUT,$GUEST_DATA;
// Читаем шаблон
$fp = fopen ("./../template/blank.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);

$REG_FORM=<<<form_reg
<form name='reg_ok' enctype='multipart/form-data' action='inc/reg.php?reg=me' method=post>
<table width="350" cellpadding="0" cellspacing="0">

<tr height='25'><td width="80">
<p>Логин:<font color="red">*</font></p></td><td width="10"><p></p></td><td>
<p><input type="text" name="login" maxlength="15"></p>
</td></tr>

<tr><td>
<p>Пароль:<font color="red">*</font></p></td><td width="10"><p></p></td><td>
<p><input type="password" name="pass" maxlength="15"></p>
</td></tr>


<tr height='25'><td>
<p>e-mail:<font color="red">*</font></p></td><td width="10"><p></p></td><td>
<p><input type="text" name="mail" maxlength="40"></p>
</td></tr>

<tr><td>
<p>Реальное имя:<font color="red">*</font></p></td><td width="10"><p></p></td><td>
<p><input type="text" name="rname" maxlength="20"></p>
</td></tr>

<tr height='25'><td>
<p>Возраст:<font color="red">*</font></p></td><td width="10"><p></p></td><td>
<p><input type="text" name="age" maxlength="2" size="5"></p>
</td></tr>

<tr><td>
<p>Город:</p></td><td width="10"><p></p></td><td>
<p><input type="text" name="city" maxlength="30"></p>
</td></tr>

<tr height='25'><td>
<p>ICQ:</p></td><td width="10"><p></p></td><td>
<p><input type="text" name="icq" maxlength="9" size="11"></p>
</td></tr>

<tr><td>
<p>Сайт:</p></td><td width="10"><p></p></td><td>
<p><input type="text" name="www" maxlength="80"></p>
</td></tr>

<tr height='25'><td>
<p align="right"><input type="reset" name="rezet"></p></td><td width="10"><p></p></td><td height='30'>
<p><input type="submit" name="ok" value="Зарегистрироваться"></p>
</td></tr>

</table>
</form>

form_reg;

$THIS_GUEST=1;$THIS_ABOUT=0;
$WWW_TITLE="Регистрация";
$GUEST_DATA="<center>$REG_FORM</center>";
$to_file=tag2html($buffer,"/");

$fp = fopen ("./../register.shtml", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}

function generate_new(){
global $bytes,$WWW,$SERV,$WWW_TITLE,$THIS_GUEST,$THIS_ABOUT,$GUEST_DATA;
// Читаем шаблон
$fp = fopen ("./../template/blank.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);

$THIS_GUEST=1;$THIS_ABOUT=0;

$GUEST_DATA="";
$sql = @mysql_query("SELECT * FROM `m3_mp3` WHERE `active` = '1' ORDER BY `id` DESC LIMIT 100");
if ($sql && @mysql_num_rows($sql)>0){
$i=0;
while ($row = @mysql_fetch_array($sql, MYSQL_ASSOC)){
$i++;
$number=$row['id'];
$GUEST_DATA.="&nbsp;&raquo; <a href=\"".$WWW."pages/".get_dir($number)."/".$number.".shtml\">".unsafe_str($row['artist'])." - ".unsafe_str($row['track'])."</a><BR>";
}
}



$WWW_TITLE="Свежие mp3";
$to_file=tag2html($buffer,"/")
;

$fp = fopen ("./../new.shtml", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}


function generate_top(){
global $bytes,$WWW,$SERV,$WWW_TITLE,$THIS_GUEST,$THIS_ABOUT,$GUEST_DATA;
// Читаем шаблон
$fp = fopen ("./../template/blank.html", "r");
$buffer = fread($fp, $bytes);
fclose ($fp);

$THIS_GUEST=1;$THIS_ABOUT=0;
$GUEST_DATA="<!--#include virtual=\"/inc/top.php\" -->";
$WWW_TITLE="Популярные mp3";
$to_file=tag2html($buffer,"/");

$fp = fopen ("./../top.shtml", "w+");
fwrite ($fp, $to_file);
fclose ($fp);
}


function generate_filesize($str_size){
if ($str_size != ""){
$str_size=(int)$str_size;
$sql = @mysql_query("SELECT * FROM `m3_mp3` WHERE `id` = '$str_size';");
}else{
$sql = @mysql_query("SELECT * FROM `m3_mp3` WHERE `active` = '1';");}
if ($sql && @mysql_num_rows($sql)>0){
while ($row = @mysql_fetch_array($sql, MYSQL_ASSOC)){
$mpd="./../m3_files/".get_dir($row['id'])."/".$row['id'].".mp3";
@$blya=(int)(@filesize($mpd)/1024);
@$blya=$blya/1024;
list($mb,$kb) = @explode(".",$blya);
@$kb = substr($kb, 0, 2);
if ($kb==""){$kb="00";}
@$blyxa=$mb.".".$kb." Mb";
@mysql_query("UPDATE `m3_mp3` SET `size` = '".$blyxa."' WHERE `id` = '".$row['id']."';");
}}
}

function generate_voite(){
global $bytes,$WWW;

$sql=mysql_query("SELECT * FROM `m3_question`;") or die(mysql_error());
if ($sql && @mysql_num_rows($sql)>0){
$m3_voite_data="";
while ($row = @mysql_fetch_array($sql, MYSQL_ASSOC)){
$m3_voite_data.="<p align=\"left\"><b>Q: ".$row['question']."</b>
<form name=\"vote\" action=\"javascript:na_open_window('voite', '".$WWW."inc/voite.php', 150, 200, 200, 300, 0, 0, 0, 0, 0)\" method=\"POST\">";
}mysql_free_result($sql);}

$sql=mysql_query("SELECT * FROM `m3_answer`;") or die(mysql_error());
if ($sql && @mysql_num_rows($sql)>0){
while ($row = @mysql_fetch_array($sql, MYSQL_ASSOC)){

$m3_voite_data.="<input type=\"radio\" onclick=\"javascript:na_open_window('voite', '".$WWW."inc/voite.php?v3=".$row['id']."', 150, 200, 200, 300, 0, 0, 0, 0, 0)\" name=\"vote\" value=\"".$row['id']."\">&nbsp;$row[answer]<br>\r\n";

}@mysql_free_result($sql);}

$m3_voite_data.="</form></p><br>";

$fp = fopen ("./../html/voite.html", "w+");
fwrite ($fp, $m3_voite_data);
fclose ($fp);

}

unset($buffer);

################
# SAMPLES
################

//generate_new_mp3();                 // Генерим 5 новых песен
//generate_pop_mp3();                 // Генерим 5 популярных песен
//generate_viewers(0, "end");         // Генерим вьюверы для всей таблицы
//generate_guest();                   // Генерим гостевую книгу
//generate_about();                   // Генерим страницу "О проекте"
//generate_register();				  // Генерим страницу "Регистрация"
//generate_search();                  // Генерим странцу поиска
//generate_filesize(123); 			  // if null then generate all items
//generate_lists('А');                // Генерим странцы листинга песен по буквам
//generate_voite();                   // Генерим голосование
//generate_new();                     // Генерим 100 новых песен
//generate_top();                     // Генерим 100 популярных песен
//generate_404();                     // Генерим страницу 404
//generate_403();                     // Генерим страницу 403


?>