Просмотр файла funciones.php

Размер файла: 17.56Kb
  1. <?php
  2. function CheckCanGzip(){
  3. global $HTTP_ACCEPT_ENCODING;
  4. if (headers_sent() || connection_aborted()){
  5. return 0;
  6. }
  7. if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false) return "x-gzip";
  8. if (strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false) return "gzip";
  9. return 0;
  10. }
  11. function GzDocOut($level,$stats){
  12. $ENCODING = CheckCanGzip();
  13. if ($ENCODING){
  14. print "\n<!-- Use compress $ENCODING -->\n";
  15. $Contents = ob_get_contents();
  16. ob_end_clean();
  17. if ($stats){
  18. $s = '<center><font size="1">'.$ENCODING;
  19. $s .="($level):";
  20. $s .= "&nbsp".number_format(strlen($Contents)/1000,1,'','')."KB...";
  21. $s .= "(".number_format(100-(100*strlen(gzcompress($Contents,$level))/strlen($Contents)),0)."%)";
  22. $s .= "...".number_format(strlen(gzcompress($Contents,$level))/1000,1,'','').'KB</font></center>';
  23. $Contents .= $s;
  24. }
  25. header("Content-Encoding: $ENCODING");
  26. print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
  27. $Size = strlen($Contents);
  28. $Crc = crc32($Contents);
  29. $Contents = gzcompress($Contents,$level);
  30. $Contents = substr($Contents, 0, strlen($Contents) - 4);
  31. print $Contents;
  32. print pack('V',$Crc);
  33. print pack('V',$Size);
  34. exit;
  35. }else{
  36. ob_end_flush();
  37. exit;
  38. }
  39. }
  40.  
  41. function getmicrotime() {
  42. list($usec, $sec) = explode(" ",microtime());
  43. return ((float)$usec + (float)$sec);
  44. }
  45.  
  46. function TOPoTags($modo,$para1,$para2) { //rev002
  47. $templates=file("html/topotags.dat");
  48. if($modo=="select") {
  49. foreach($templates as $key => $raw) {
  50. $aux=explode("||",$raw);
  51. $output.='<OPTION value="'.$key.'"';
  52. if($key==$para1) $output.=' selected';
  53. $output.='>'.$aux[0].'</OPTION>';
  54. }
  55. return $output;
  56. }
  57. if($modo=="read") {
  58. if(!isset($templates[$para1])) $para1=0;
  59. $aux=explode("||",$templates[$para1]);
  60. return $aux;
  61. }
  62. if($modo=="save" OR $modo=="saveas") {
  63. if($modo=="save") {
  64. $templates[$para1]=$para2;
  65. } else {
  66. $templates[]=$para2;
  67. }
  68. foreach($templates as $value) $data.=$value;
  69. $fp=fopen("html/topotags.dat","w"); flock($fp,2);
  70. $ok=fwrite($fp,$data); fclose($fp);
  71. if(!$ok) return -1;
  72. return count($templates);
  73. }
  74. if($modo=="rename") {
  75. $aux=explode("||",$templates[$para1]);
  76. $NEWdata=$para2."||".$aux[1]."||".$aux[2]."||".$aux[3]."||".$aux[4]."||".$aux[5]."||".$aux[6]."||".$aux[7]."||".$aux[8]."||\n";
  77. $ok=TOPoTags("save",$para1,$NEWdata);
  78. if(!$ok) return -1;
  79. return count($templates);
  80. }
  81. if($modo=="delete") {
  82. global $template;
  83. $templates[$para1]="";
  84. foreach($templates as $value) $data.=$value;
  85. $fp=fopen("html/topotags.dat","w"); flock($fp,2);
  86. $ok=fwrite($fp,$data); fclose($fp);
  87. if($para1<=$template) {
  88. $NEWtemplate=$template-$para1;
  89. config("parcial","\$template=".$template.";","\$template=".$NEWtemplate.";");
  90. }
  91. if(!$ok) return -1;
  92. return 1;
  93. }
  94. }
  95.  
  96. function AutoBanner($para) { //rev002
  97. if(file_exists("data/index.dat")) {
  98. $linea=file("data/index.dat");
  99. $num=count($linea);
  100. if($num<$para) $para=$num;
  101. srand((float) microtime() * 10000000);
  102. for($i=0;$i<$para;$i++) {
  103. do {
  104. $key=array_rand($linea);
  105. $aux=explode("||",$linea[$key]);
  106. include("data/".$aux[0]."info.php");
  107. } while(strlen($bannerURL)<10 OR $aux[2]==0);
  108. $codigo[$i]='<CENTER><A href="'.$webURL.'" target="_blank"><img src="'.$bannerURL.'" border="0" width="'.$bannerAncho.'" height="'.$bannerAlto.'"></A></CENTER>';
  109. }
  110. } else {
  111. for($i=0;$i<$para;$i++) $codigo[$i]="";
  112. }
  113. return $codigo;
  114. }
  115.  
  116. function string($modo,$para1,$para2) {
  117. if($para1=="") return $para2;
  118. if($modo=='raw') {
  119. $str=str_replace("\n",'',$para1);
  120. $str=str_replace("\r",'',$str);
  121. $str=str_replace('\\','',$str);
  122. return $str;
  123. }
  124. if($modo=='addslash') {
  125. if(substr($para1,-1)!='/') $para1.='/';
  126. return $para1;
  127. }
  128. if($modo=='int') {
  129. if(!is_int(intval($para1))) $para1=$para2;
  130. if($para1>$para2) $para1=$para2;
  131. return $para1;
  132. }
  133. if($modo=='int2') {
  134. if(!is_int(intval($para1))) $para1=$para2;
  135. return $para1;
  136. }
  137. }
  138.  
  139. function media($modo,$data1,$data2,$data3) {
  140. if($modo=="dias") {
  141. $seg=abs($data1-$data2);
  142. $dias=$seg/86400;
  143. return number_format($dias,0,'.','');
  144. }
  145. if($modo=="minutos") {
  146. $seg=abs($data1-$data2);
  147. $minutos=$seg/60;
  148. return number_format($minutos,0,'.','');
  149. }
  150. if($modo=="pordia") {
  151. $dias=abs($data1-$data2)/86400;
  152. if($dias<1) {
  153. $media=$data3;
  154. } else {
  155. $media=$data3/$dias;
  156. }
  157. return number_format($media,1,'.','');
  158. }
  159. }
  160.  
  161. function barra($modo,$para1,$para2,$para3) {
  162. if($modo=="simple") {
  163. if($para3==0) {
  164. $cociente=0;
  165. } else {
  166. $cociente=number_format(($para1*$para2)/$para3,0,'.','');
  167. }
  168. $HTML='<img src="gif/bar_blue.jpg" valign="middle" border="0" height="10" width="'.$cociente.'">';
  169. return $HTML;
  170. }
  171.  
  172. if($modo=="ratio") {
  173. if($para2+$para3==0) {
  174. $cociente1=0;
  175. $cociente2=0;
  176. } else {
  177. $cociente1=number_format($para1*$para2/($para2+$para3),0,'.','');
  178. $cociente2=number_format($para1*$para3/($para2+$para3),0,'.','');
  179. }
  180. if($para2<=$para3) {
  181. if($para2) {
  182. $para3=number_format($para3/$para2,0,'.','');
  183. $para2=1;
  184. } else {
  185. $para2='';
  186. }
  187. } else {
  188. if($para3) {
  189. $para2=number_format($para2/$para3,0,'.','');
  190. $para3=1;
  191. } else {
  192. $para3='';
  193. }
  194. }
  195. $HTML='<table class="textobar" border="0" cellspacing="0" cellpadding="0"><tr>';
  196. $HTML.='<td class="bar1" width="'.$cociente1.'" align="center">'.$para2.'</td>';
  197. $HTML.='<td class="bar2" width="'.$cociente2.'" align="center">'.$para3.'</td>';
  198. $HTML.='</tr></table>';
  199. return $HTML;
  200. }
  201. }
  202.  
  203.  
  204. function datos($modo,$ID,$data) {
  205. $archivo='data/'.$ID.'datos.dat';
  206. if($modo=="reset") { //Se llama a ella misma en $modo="read" y $modo="write";
  207. if(file_exists($archivo)) {
  208. $datos=datos("read",$ID,0);
  209. $datos[0]=$datos[1]=0;
  210. } else {
  211. $datos[0]=$datos[1]=$datos[2]=$datos[3]=$datos[4]=$datos[5]=$datos[6]=$datos[8]=0;
  212. $datos[7]=time();
  213. }
  214. $ok=datos("write",$ID,$datos);
  215. return $ok;
  216. }
  217. if($modo=="read") {
  218. $fp=fopen($archivo,"r");
  219. $raw=fread($fp,filesize($archivo));
  220. fclose($fp);
  221. $datos=explode("||",$raw);
  222. return $datos;
  223. }
  224. if($modo=="write") {
  225. $fp=fopen($archivo,"w");
  226. flock($fp,2);
  227. $raw=$data[0]."||".$data[1]."||".$data[2]."||".$data[3]."||".$data[4]."||".$data[5]."||".$data[6]."||".$data[7]."||".$data[8]."||";
  228. $ok=fwrite($fp,$raw);
  229. fclose($fp);
  230. @chmod($archivo,0666);
  231. return $ok;
  232. }
  233. }
  234.  
  235. function index($modo,$ID,$data1,$data2) {
  236. if($modo=="write") {
  237. if(file_exists("data/index.dat")) {
  238. $fp=fopen("data/index.dat","a");
  239. } else {
  240. $fp=fopen("data/index.dat","w");
  241. }
  242. flock($fp,2);
  243. $ok=fwrite($fp,$ID."||".$data1."||".$data2."||\n");
  244. fclose($fp);
  245. @chmod($archivo,0666);
  246. return $ok;
  247. }
  248. if($modo=="delete") { //Llamada recursiva.
  249. $datos=index("search",$ID,0,0);
  250. $fp=fopen("data/index.dat","r");
  251. $raw=fread($fp,filesize("data/index.dat"));
  252. fclose($fp);
  253. $raw=str_replace($ID."||".$datos[0]."||".$datos[1]."||\n","",$raw);
  254. $fp=fopen("data/index.dat","w");
  255. flock($fp,2);
  256. $ok=fwrite($fp,$raw);
  257. fclose($fp);
  258. //Borramos todos los archivos asociados a ID
  259. unlink("data/".$ID."comentarios.dat");
  260. unlink("data/".$ID."datos.dat");
  261. unlink("data/".$ID."info.php");
  262. unlink("data/".$ID."ip.dat");
  263. unlink("data/".$ID."notas.dat");
  264. return $ok;
  265. }
  266. if($modo=="change") { //Llamada recursiva.
  267. $datos=index("search",$ID,0,0);
  268. $fp=fopen("data/index.dat","r");
  269. $raw=fread($fp,filesize("data/index.dat"));
  270. fclose($fp);
  271. $raw=str_replace($ID."||".$datos[0]."||".$datos[1]."||\n",$ID."||".$data1."||".$data2."||\n",$raw);
  272. $fp=fopen("data/index.dat","w");
  273. flock($fp,2);
  274. $ok=fwrite($fp,$raw);
  275. fclose($fp);
  276. return $ok;
  277. }
  278. if($modo=="search") {
  279. if(!file_exists("data/index.dat")) return FALSE;
  280. $linea=file("data/index.dat");
  281. foreach($linea as $raw) {
  282. $aux=explode("||",$raw);
  283. $index[$aux[0]]=$aux[1]."||".$aux[2]."||";
  284. }
  285. //if(array_key_exists($ID,$index)) {
  286. if(isset($index[$ID])) {
  287. $output=explode("||",$index[$ID]);
  288. return $output;
  289. } else {
  290. return FALSE;
  291. }
  292. }
  293. if($modo=="select") {
  294. $output="";
  295. $linea=file("data/index.dat");
  296. foreach($linea as $raw) {
  297. $aux=explode("||",$raw);
  298. $output.='<OPTION value="'.$aux[0].'">'.$aux[1].'</OPTION>';
  299. }
  300. return $output;
  301. }
  302. if($modo=="select+") {
  303. $output="";
  304. $linea=file("data/index.dat");
  305. foreach($linea as $raw) {
  306. $aux=explode("||",$raw);
  307. if($aux[2]==$data1) $output.='<OPTION value="'.$aux[0].'">'.$aux[1].'</OPTION>';
  308. }
  309. return $output;
  310. }
  311. }
  312.  
  313.  
  314. function ip($modo,$ID,$data) {
  315. $archivo='data/'.$ID.'ip.dat';
  316. if($modo=="reset") {
  317. $fp=fopen($archivo,"w");
  318. flock($fp,2);
  319. $ok=fwrite($fp,"");
  320. fclose($fp);
  321. @chmod($archivo,0666);
  322. return $ok;
  323. }
  324. if($modo=="write") {
  325. $fp=fopen($archivo,"a");
  326. flock($fp,2);
  327. $ok=fwrite($fp,$data."||".time()."||\n");
  328. fclose($fp);
  329. @chmod($archivo,0666);
  330. return $ok;
  331. }
  332. if($modo=="update") {
  333. if(filesize($archivo)<10) return 1;
  334. $datos="";
  335. $linea=file($archivo);
  336. foreach($linea as $raw) {
  337. $aux=explode("||",$raw);
  338. if($aux[1]>$data) $datos.=$aux[0]."||".$aux[1]."||\n";
  339. }
  340. $fp=fopen($archivo,"w");
  341. flock($fp,2);
  342. $ok=fwrite($fp,$datos);
  343. fclose($fp);
  344. return $ok;
  345. }
  346. if($modo=="search") {
  347. if(filesize($archivo)<10) return 0;
  348. $linea=file($archivo);
  349. foreach($linea as $raw) {
  350. $aux=explode("||",$raw);
  351. $ip[$aux[0]]=$aux[1];
  352. }
  353. //if(array_key_exists($data,$ip)) {
  354. if(isset($ip[$data])) {
  355. return $ip[$data];
  356. } else {
  357. return 0;
  358. }
  359. }
  360. }
  361.  
  362.  
  363. function notas($modo,$ID,$data1,$data2) {
  364. $archivo='data/'.$ID.'notas.dat';
  365. if($modo=="reset") {
  366. $fp=fopen($archivo,"w");
  367. flock($fp,2);
  368. $ok=fwrite($fp,"");
  369. fclose($fp);
  370. @chmod($archivo,0666);
  371. return $ok;
  372. }
  373. if($modo=="write") {
  374. $fp=fopen($archivo,"a");
  375. flock($fp,2);
  376. $ok=fwrite($fp,$data1."||".time()."||".$data2."||\n");
  377. fclose($fp);
  378. @chmod($archivo,0666);
  379. return $ok;
  380. }
  381. if($modo=="update") {
  382. if(filesize($archivo)<10) return -1;
  383. $linea=file($archivo);
  384. foreach($linea as $raw) {
  385. $aux=explode("||",$raw);
  386. $notas[$aux[0]]=$aux[1]."||".$aux[2];
  387. }
  388. $notas[$data1]=time()."||".$data2;
  389. $datos="";
  390. foreach($notas as $key => $value) {
  391. if($value!="") $datos.=$key."||".$value."||\n";
  392. }
  393. $fp=fopen($archivo,"w");
  394. flock($fp,2);
  395. $ok=fwrite($fp,$datos);
  396. fclose($fp);
  397. return $ok;
  398. }
  399. if($modo=="search") {
  400. if(filesize($archivo)<10) return -1;
  401. $linea=file($archivo);
  402. foreach($linea as $raw) {
  403. $aux=explode("||",$raw);
  404. $notas[$aux[0]]=$aux[1]."||".$aux[2]."||";
  405. }
  406. //if(array_key_exists($data1,$notas)) {
  407. if(isset($notas[$data1])) {
  408. $output=explode("||",$notas[$data1]);
  409. return $output;
  410. } else {
  411. return -1;
  412. }
  413. }
  414. if($modo=="stats") {
  415. if(filesize($archivo)<5) return 0;
  416. $linea=file($archivo);
  417. foreach($linea as $raw) {
  418. $aux=explode("||",$raw);
  419. $notas[$i++]=$aux[2];
  420. }
  421. foreach($notas as $value) {
  422. $output[$value]++;
  423. $sumatotal+=$value;
  424. $output[11]++;
  425. }
  426. $max=$output[14]=0;
  427. for($i=0;$i<=10;$i++) {
  428. if($output[$i]>$max) {
  429. $max=$output[$i];
  430. $output[14]=$i;
  431. }
  432. }
  433. $output[13]=number_format($sumatotal/$output[11],1,'.','');
  434. return $output;
  435. }
  436. }
  437.  
  438. function comentarios($modo,$ID,$data1,$data2) { //rev001
  439. $archivo='data/'.$ID.'comentarios.dat';
  440. if($modo=="reset") {
  441. $fp=fopen($archivo,"w");
  442. flock($fp,2);
  443. $ok=fwrite($fp,"");
  444. fclose($fp);
  445. @chmod($archivo,0666);
  446. return $ok;
  447. }
  448.  
  449. if($modo=="read") {
  450. if(filesize($archivo)<10) {
  451. return 0;
  452. } else {
  453. $linea=file($archivo);
  454. return $linea;
  455. }
  456. }
  457.  
  458. if($modo=="write") {
  459. $fp=fopen($archivo,"a");
  460. flock($fp,2);
  461. $ok=fwrite($fp,$data1[0]."||".$data1[1]."||".$data1[2]."||".$data1[3]."||".$data1[4]."||".$data1[5]."||\n");
  462. fclose($fp);
  463. @chmod($archivo,0666);
  464. return $ok;
  465. }
  466. if($modo=="delete") { //Llamada recursiva.
  467. $linea=comentarios("read",$ID,0,0);
  468. foreach($linea as $raw) {
  469. $aux=explode("||",$raw);
  470. if($aux[1]==$data1) break;
  471. }
  472. $fp=fopen($archivo,"r");
  473. $raw=fread($fp,filesize($archivo));
  474. fclose($fp);
  475. $raw=str_replace($aux[0]."||".$aux[1]."||".$aux[2]."||".$aux[3]."||".$aux[4]."||".$aux[5]."||\n","",$raw);
  476. $fp=fopen($archivo,"w");
  477. flock($fp,2);
  478. $ok=fwrite($fp,$raw);
  479. fclose($fp);
  480. return $ok;
  481. }
  482. if($modo=="update") {
  483. $aux=$data1[0]."||".$data1[1]."||".$data1[2]."||".$data1[3]."||".$data1[4]."||".$data1[5]."||\n";
  484. $fp=fopen($archivo,"r");
  485. $raw=fread($fp,filesize($archivo));
  486. fclose($fp);
  487. $raw=str_replace($data2,$aux,$raw);
  488. $fp=fopen($archivo,"w");
  489. flock($fp,2);
  490. $ok=fwrite($fp,$raw);
  491. fclose($fp);
  492. return $ok;
  493. }
  494. if($modo=="search") {
  495. if(filesize($archivo)<10) return 0;
  496. $linea=file($archivo);
  497. foreach($linea as $raw) {
  498. $aux=explode("||",$raw);
  499. $comentarios[$aux[0]]=$aux[0]."||".$aux[1]."||".$aux[2]."||".$aux[3]."||".$aux[4]."||".$aux[5]."||\n";
  500. }
  501. //if(array_key_exists($data1,$comentarios)) {
  502. if(isset($comentarios[$data1])) {
  503. return $comentarios[$data1];
  504. } else {
  505. return 0;
  506. }
  507. }
  508. if($modo=="count") {
  509. if(filesize($archivo)<10) {
  510. return 0;
  511. } else {
  512. $linea=file($archivo);
  513. return count($linea);
  514. }
  515. }
  516. }
  517.  
  518. function config($modo,$data1,$data2) {
  519. $fp=fopen("config.php","r");
  520. $raw=fread($fp,filesize("config.php"));
  521. fclose($fp);
  522. if($modo=="simple") {
  523. $raw=str_replace($data1,$data2,$raw);
  524. }
  525. if($modo=="parcial") {
  526. $raw=str_replace($data1,$data2,$raw);
  527. }
  528. $fp=fopen("config.php","w");
  529. flock($fp,2);
  530. $ok=fwrite($fp,$raw);
  531. fclose($fp);
  532. return $ok;
  533. }
  534.  
  535. function cookies($modo,$ID,$data) {
  536. $cookie='topo'.$ID;
  537. global $$cookie;
  538. if($modo=="read") {
  539. if(isset($$cookie)) {
  540. return $$cookie;
  541. } else {
  542. return 0;
  543. }
  544. }
  545. if($modo=="write") {
  546. $ok=setcookie($cookie,time(),time()+$data,"/");
  547. return $ok;
  548. }
  549. }
  550.  
  551. function orden($modo,$criterio) {
  552. //$criterio=0: PARCIAL IN
  553. //$criterio=1: PARCIAL OUT
  554. //$criterio=2: TOTAL IN
  555. //$criterio=3: TOTAL OUT
  556. //$criterio=4: RATE
  557. if($modo=="update") {
  558. $linea=file("data/index.dat");
  559. foreach($linea as $raw) {
  560. $data=explode("||",$raw);
  561. if($data[2]) {
  562. $datos=datos("read",$data[0],0);
  563. //Ordenamos los votos.
  564. $ceros[6]="0"; $ceros[5]="00"; $ceros[4]="000";
  565. $ceros[3]="0000"; $ceros[2]="00000"; $ceros[1]="000000";
  566. //Reconstruimos la linea completando con ceros hasta 6 cifras.
  567. if($criterio==4) $clave=number_format(($datos[4]*$datos[5])/$datos[5],3,'.',0);
  568. $clave=$ceros[strlen($datos[$criterio])].$datos[$criterio];
  569. $desorden[$i++]=$clave."||".$data[0]."||";
  570. }
  571. }
  572. rsort($desorden);
  573. $i=1;
  574. foreach($desorden as $valor) {
  575. $aux=explode("||",$valor);
  576. $orden[$i++]=$aux[1];
  577. }
  578. $data_bin=serialize($orden);
  579. $fp=fopen("data/orden.dat","w");
  580. flock($fp,2);
  581. fwrite($fp,$data_bin);
  582. fclose($fp);
  583. @chmod("data/orden.dat",0666);
  584. return $orden;
  585. }
  586. if($modo=="read") {
  587. $fp=fopen("data/orden.dat","r");
  588. $data=fread($fp,filesize("data/orden.dat"));
  589. fclose($fp);
  590. return unserialize($data);
  591. }
  592. }
  593.  
  594. ?>