<?php
if (basename($_SERVER['SCRIPT_NAME']) == basename(__FILE__)) {
  die(base64_decode('U2NyaXB0IFY1IGhlY2hvIHBvciBBbGFuOTgu'));
}
$cookie = explode('%', $_COOKIE['clanpirata']);
if ($_COOKIE['clanpirata'] && mysql_num_rows($query = mysql_query('SELECT * FROM users WHERE id = \'' . strclean($cookie[0]) . '\' && uuid = \'' . strclean($cookie[1]) . '\' && ban = \'0\''))) {
  $user = mysql_fetch_assoc($query);
  if (mysql_num_rows(mysql_query('SELECT id FROM bans WHERE user = \'' . $user['id'] . '\''))) {
    $user = false;
  }
  $realip = $_SERVER['REMOTE_ADDR'] ? $_SERVER['REMOTE_ADDR'] : $_SERVER['X_FORWARDER_FOR'];
  if ($user['ip'] != $realip) {
    mysql_query('UPDATE users SET ip = \'' . strclean($realip) . '\' WHERE id = \'' . $user['id'] . '\' && active = \'1\'');
  }
  mysql_query('UPDATE users SET lastaction = \'' . time() . '\' WHERE id = \'' . $user['id'] . '\' LIMIT 1');
  $pts = explode('-', $user['points2']);
  if ($pts[1] != date('dmy')) {
    list($rankpoints) = mysql_fetch_row(mysql_query('SELECT `points` FROM `ranks` WHERE `id` = \'' . $user['rank'] . '\''));
    $user['points2'] = $rankpoints;
    mysql_query('UPDATE `users` SET `points2` = \'' . $rankpoints . '-' . date('dmy') . '\' WHERE `id` = \'' . $user['id'] . '\'');
  }
  else {
    $user['points2'] = $pts[0];
  }
  // medalla fb
  if(!empty($user['fbid']) && !mysql_num_rows(mysql_query('SELECT id FROM medals WHERE user = \''.$user['id'].'\' && medal = \'8\''))){
    mysql_query('INSERT INTO medals (user, medal, time) VALUES (\''.$user['id'].'\', \'8\', \''.time().'\')');
    mysql_query('INSERT INTO notifications (author, id_user, time, what, type) VALUES (\'0\', \''.$user['id'].'\', \''.time().'\', \'8\', \'0\')');
  } elseif(empty($user['fbid']) && mysql_num_rows(mysql_query('SELECT id FROM medals WHERE user = \''.$user['id'].'\' && medal = \'8\''))){
    mysql_query('DELETE FROM medals WHERE user = \''.$user['id'].'\' && medal = \'8\'');
  }
}
function strclean($string) {
  if (!get_magic_quotes_gpc()) {
   // $string = stripslashes($string);
  }
  $string = htmlspecialchars($string, ENT_QUOTES);
  $string = mysql_real_escape_string($string);
//$string = str_replace('\\', '\\\\', $string);
  return $string;
}
function getQueries() {
  $queries = mysql_query('SHOW STATUS');
  while ($temp = mysql_fetch_assoc($queries)) {
    if ($temp['Variable_name'] == 'Questions') {
      return $temp['Value'];
      break;
    }
  }
}
function cutString($str, $how, $add = '...') {
  if (strlen($str) > $how) {
    $str = substr($str, 0, $how) . $add;
  }
  return $str;
}
function isAllowed($permission) {
  global $user;

  if ($user['id']) {
    if (!mysql_num_rows($query = mysql_query('SELECT permissions FROM ranks WHERE id = \'' . $user['rank'] . '\''))) {
      return false;
    }
    $row = mysql_fetch_row($query);
    $permissions = explode(';', $row[0]);
    if (in_array($permission, $permissions)) {
      return true;
    }
  }
  return false;
}
function error($mensaje, $titulo = 'CHAN!!', $value = 'Ir a p&aacute;gina principal', $onclick = 'location.href=\'/\'') {
  global $timestart, $memstart;
  echo '<div id="alertmsg">
<h1>' , $titulo , '</h1>
<p>' , $mensaje , '</p>
<input type="button" class="ui-btn mBtn btnOk" value="' , $value , '" title="' , $value , '" onclick="' , $onclick , '">
</div></div>';
  include ('footer.php');
  die;
}
function url($text) {
  $text = strtr($text, '() ', '[]-');
  $text = preg_replace('/([^a-z0-9\[\]-_]+)/i', '-', $text);
  return empty ($text) ? '-' : $text;
}
function jerror($data = '', $status = 0) {
  die(json_encode(array('status' => $status, 'data' => $data)));
}
function timefrom($time) {
  $d = time() - $time;
  if ($d < 60) {
    return 'Hace menos de un minuto';
  }
  if ($d < 3600) {
    $t = ceil($d / 60);
    return 'Hace ' . $t . ' minuto' . ($t == 1 ? '' : 's');
  }
  if ($d < 86400) {
    $t = ceil($d / 3600);
    return 'Hace ' . $t . ' hora' . ($t == 1 ? '' : 's');
  }
  if ($d < 604800) {
    $t = ceil($d / 86400);
    return 'Hace ' . $t . ' d&iacute;a' . ($t != 1 ? 's' : '');
  }
  if ($d < 2419200) {
    $t = ceil($d / 604800);
    return ($t == 1 ? 'La semana pasada' : 'Hace ' . $t . ' semanas');
  }
  if ($d < 31104000) {
    $t = ceil($d / 2592000);
    return ($t == 1 ? 'El mes pasado' : 'Hace ' . $t . ' meses');
  }
  $t = ceil($d / 31104000);
  return ($t == 1 ? 'El a&ntilde;o pasado' : 'Hace ' . $t . ' a&ntilde;os');
}
function groups_ranks($rank) {
  switch ($rank) {
    case '3' :
      echo 'Posteador';
      break;
    case '2' :
      echo 'Comentador';
      break;
    case '1' :
      echo 'Visitante';
      break;
    case '4' :
      echo 'Moderador';
      break;
    case '5' :
      echo 'Administrador';
      break;
    default :
      echo 'Ex Integrante';
      break;
  }
}

function showAds($service, $size = '300x250'){
 if($size === '300x250'){
  return '<a href="http://track.adjal.com/aff_c?offer_id=4172&aff_id=29132&url_id=16880&file_id=295688" target="_blank"><img src="http://media.adjal.com/brand/files/adjal/4172/Goplay_Games_ES_ES_300x250.jpg" width="300" height="250" border="0" /></a><img src="http://track.adjal.com/aff_i?offer_id=4172&aff_id=29132&url_id=16880&file_id=295688" width="1" height="1" />';
 }
 if($size === '468x60'){
   return '<a href="http://track.adjal.com/aff_c?offer_id=4228&aff_id=29132&file_id=300304" target="_blank"><img src="http://media.adjal.com/brand/files/adjal/4228/Ovooz_468x60-reproducir-ahora_2.gif" width="468" height="60" border="0" /></a><img src="http://track.adjal.com/aff_i?offer_id=4228&aff_id=29132&file_id=300304" width="1" height="1" />';
 }
 if($size === '160x600'){
   return '<a href="http://track.adjal.com/aff_c?offer_id=1826&aff_id=29132&file_id=120734" target="_blank"><img src="http://media.adjal.com/brand/files/adjal/1826/160x600_TEST.gif" width="160" height="600" border="0" /></a><img src="http://track.adjal.com/aff_i?offer_id=1826&aff_id=29132&file_id=120734" width="1" height="1" />';
 }
 if($size === '728x90'){
    return '<a href="http://track.adjal.com/aff_c?offer_id=4172&aff_id=29132&url_id=16886&file_id=295796" target="_blank"><img src="http://media.adjal.com/brand/files/adjal/4172/Soong_ES_ES_728x90.jpg" width="728" height="90" border="0" /></a><img src="http://track.adjal.com/aff_i?offer_id=4172&aff_id=29132&url_id=16886&file_id=295796" width="1" height="1" />';
  }
}

//mirá lo que tengo que hacer :/
function getMyUrl($url, $uagent = false){
  $output = array('type' => 'link', 'id' => htmlspecialchars($url));
  if(mysql_num_rows($q = mysql_query('SELECT title, `desc` FROM urls WHERE url = \''.mysql_real_escape_string($url).'\''))){
    list($output['title'], $output['description']) = mysql_fetch_row($q);
  } else {
    if(ini_get('safe_mode') != '1') set_time_limit(1000);
    if(!function_exists('curl_init')){
      $obtainedData = file_get_contents($url);
    } else {
      $c = curl_init($url);
      curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
      if(ini_get('safe_mode') != '1') curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
      if($uagent) curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17 AlexaToolbar/alxg-3.1');
      $obtainedData = curl_exec($c);
      curl_close($c);
    }
    if(strpos(strtolower($obtainedData), '</head>') !== false){
      preg_match('#<title>(.*)</title>#i', $obtainedData, $title);
      $output['title'] = htmlspecialchars(htmlspecialchars_decode($title[1]));
      $tags = get_meta_tags($url);
      $output['description'] = (isset($tags['description']) ? htmlspecialchars(cutString(htmlspecialchars_decode($tags['description']), 350)) : $output['id']);
    } else {
      $output['title'] = $output['description'] = $output['id'];
    }
  }
  $output['url'] = $output['id'];
  return $output;
}

/* Hecho por DatsiK - rompela.net */

function download_photo($url, $saveto){
	if (!curlSaveToFile($url, $saveto)) return false;
   	return true;
}

function curlSaveToFile($url, $local){
	$ch = curl_init();
	$fh = fopen($local, 'w');
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_FILE, $fh);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_VERBOSE, false);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_NOPROGRESS, true);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36');
	curl_exec($ch);

	if(curl_errno($ch)) return false;

	curl_close($ch);
	fclose($fh);

	if(filesize($local) > 10) return true;

	return false;
}
?>