<?php 

function ads($type = 'foot', $str = 'all', $limit = false, $rand = false) 
{ 
  if ($_SERVER['PHP_SELF'] == '/index.php') {
    $str = 'home';
  } else {
    $str = 'other';
  }
  
  if ($limit) {
    $limit = 'LIMIT ' . (int) $limit;
  }
  
  $q = mysql_query("SELECT * FROM `ads` WHERE `active` = '1' AND `time_end` > '" . time() . "' AND `type` = '$type' AND (`str` = '$str' OR `str` = 'all') ORDER BY " . ($rand ? 'rand()' : '`id` DESC') . " $limit");
  
  while($post = mysql_fetch_assoc($q)) {
    if ($post['redirect'] == 0) {
      $post['url'] = '/go.php?go=' . base64_encode(html_entity_decode($post['url']));
    }
    $color = ($post['color'] ? 'color: ' . $post['color'] . ';' : '');
    $fat = ($post['fat'] == 1 ? 'font-weight: bold;' : '');
    
    echo '<div class="ads"><a href="' . $post['url'] . '" style="' . $color . $fat . '" target="_blank">' . $post['name'] . '</a></div>';
  }
}
