<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/********************************************************/
/* NSN Banner Ads                                       */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright © 2000-2005 by NukeScripts Network         */
/********************************************************/
defined('MAXIMUS') or Header("Location: index.php");

function bacookiedecode($baclient) {
  global $bacookie, $prefix, $db;
  $baclient = base64_decode($baclient);
  $bacookie = explode(":", $baclient);
  $result = $db->sql_query("select `passwd` from `".TABLE_NSNBA_CLIENTS."` where `login`='$bacookie[1]'");
  $row = $db->sql_fetchrow($result);
  $pass = stripslashes($row['passwd']);
  if($bacookie[2] == $pass && $pass != "") {
    return $bacookie;
  } else {
    unset($baclient);
    unset($bacookie);
  }
}

function is_baclient($baclient) {
  global $prefix, $db;
  if(!is_array($baclient)) {
    $baclient = base64_decode($baclient);
    $baclient = explode(":", $baclient);
    $cid = "$baclient[0]";
    $pwd = "$baclient[2]";
  } else {
    $cid = "$baclient[0]";
    $pwd = "$baclient[2]";
  }
  if($cid != "" AND $pwd != "") {
    $result = $db->sql_query("select `passwd` from `".TABLE_NSNBA_CLIENTS."` where `cid`='$cid'");
    $row = $db->sql_fetchrow($result);
    $pass = stripslashes($row['passwd']);
    if($pass == $pwd && $pass != "") { return 1; }
  }
  return 0;
}

function baget_client_info($baclient) {
  global $clientinfo, $prefix, $db;
  $client2 = base64_decode($baclient);
  $client3 = explode(":", $client2);
  $result = $db->sql_query("select `cid`, `name`, `email`, `login`, `passwd` from `".TABLE_NSNBA_CLIENTS."` where `login`='$client3[1]' and `passwd`='$client3[2]'");
  if($db->sql_numrows($result) == 1) { $clientinfo = $db->sql_fetchrow($result); }
  return $clientinfo;
}

function ClientCheck($cname, $email) {
  global $stop, $prefix, $db;
  if((!$email) || ($email=="") || (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$",$email))) $stop = "<center>"._BA_ERR_INVEML."</center>";
  if(strrpos($email,' ') > 0) $stop = "<center>"._BA_ERR_EMLNOSPC."</center>";
  if((!$cname) || ($cname=="") || (ereg("[^a-zA-Z0-9_-]",$cname))) $stop = "<center>"._BA_ERR_INVCLT."</center>";
  if(strlen($cname) < 4 OR strlen($cname) > 25) $stop = "<center>"._BA_ERR_TOLONG."</center>";
  if(eregi("^((root)|(adm)|(linux)|(webmaster)|(admin)|(god)|(administrator)|(administrador)|(nobody)|(anonymous)|(anonimo)|(anónimo)|(operator))$",$cname)) $stop = "<center>"._BA_ERR_IDRESRV."</center>";
  if(eregi("(admin)|(nsn)|(nuke)|(staff)", $cname) ) $stop = "<center>"._BA_ERR_IDRESRV."</center>";
  if(strrpos($cname,' ') > 0) $stop = "<center>"._BA_ERR_IDNOSPC."</center>";
  if($db->sql_numrows($db->sql_query("SELECT `login` FROM `".TABLE_NSNBA_CLIENTS."` WHERE `login`='$cname'")) > 0) $stop = "<center>"._BA_ERR_IDTAKEN."</center>";
  return($stop);
}

function BAMenu() {
  global $module_name, $bacookie, $wherefrom;
  echo "<script language='JavaScript'>\n";
  echo "<!-- Begin\n";
  echo "function NewWindow(mypage, myname, w, h, scroll) {\n";
  echo "var winl = (screen.width - w) / 2;\n";
  echo "var wint = (screen.height - h) / 2;\n";
  echo "winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'\n";
  echo "win = window.open(mypage, myname, winprops)\n";
  echo "if(parseInt(navigator.appVersion) >= 4) { win.window.focus(); }\n";
  echo "}\n";
  echo "//  End -->\n";
  echo "</script>\n";
  Max_OpenTable();
  echo "<center><b>"._BA_CLTID." - ".$bacookie[1]."</b></center><br />\n";
  echo "<center><table cellpadding='3'><td align='center' valign='top' width='150'>\n";
  echo "<a href='modules.php?name=$module_name&amp;op=BAClientView' onclick=\"NewWindow(this.href,'ClientView','400','200','no');return false;\">"._BA_VIEWCLT."</a><br />\n";
  echo "<a href='modules.php?name=$module_name&amp;op=BAClientEdit'>"._BA_EDITCLT."</a><br />\n";
  echo "</td><td align='center' valign='top' width='150'>\n";
  echo "<a href='modules.php?name=$module_name&amp;op=BAImageList'>"._BA_LISTBANN."</a><br />\n";
  echo "</td><td align='center' valign='top' width='150'>\n";
  echo "<a href='modules.php?name=$module_name&amp;op=BALogout'>"._BA_CLTOUT."</a><br />\n";
  echo "</td></table></center>\n";
  Max_CloseTable();
}

function BACookie($setcid, $login, $new_pass) {
  $info = base64_encode("$setcid:$login:$new_pass");
  setcookie("baclient","$info",time()+2419200);
}

function BAPass() {
  $lets = "abcdefghijklmnopqrstuvwxyz";
  $nums = "23456789";
  for($x=0; $x < 6; $x++) {
    mt_srand ((double) microtime() * 1000000);
    $let[$x] = substr($lets, mt_rand(0, strlen($lets)-1), 1);
    $num[$x] = substr($nums, mt_rand(0, strlen($nums)-1), 1);
  }
  $BAPass = $let[0] . $num[0] .$let[2] . $let[1] . $num[1] . $let[3] . $num[3] . $let[4];
  return($BAPass);
}

function baclear_text($str) {
  $str = trim($str);
  $str = stripslashes($str);
  $str = strip_tags($str);
  $str = ereg_replace("\"", "", $str);
  $str = ereg_replace("'", "", $str);
  return $str;
}

function basave_config($config_name, $config_value){
  global $prefix, $db;
  $db->sql_query("UPDATE `".$prefix."_nsnba_config` SET `config_value`='$config_value' WHERE `config_name`='$config_name'");
}

function baget_configs(){
  global $prefix, $db;
  $configresult = $db->sql_query("SELECT `config_name`, `config_value` FROM `".$prefix."_nsnba_config`");
  while($raw = $db->sql_fetchrow($configresult)) {
  $config_name = stripslashes($raw['config_name']);
  $config_value = stripslashes($raw['config_value']);
    $config[$config_name] = $config_value;
  }
  return $config;
}