[+title+]\n"; var $flash_tpl = " "; function AdsManager($tags = '') { global $modx; $this->banners_shown = array(); if ($modx !== NULL) { $path = MODX_BASE_PATH; } else { $path = $_SERVER['SCRIPT_FILENAME']; if (strpos($path, '/assets/') !== FALSE) { $path = substr($path,0,strpos($path, '/assets/')); } } include_once $path . "/manager/includes/config.inc.php"; $this->modx_base_path = $path; if ($modx !== NULL) { global $dbase; global $database_server; global $database_user ; global $database_password; global $database_connection_charset; global $table_prefix; } date_default_timezone_set("UTC"); if ($tags != '') {$this->set_filter($tags);} $this->db_server = $database_server; $this->db_user = $database_user ; $this->db_pass = $database_password; $this->db_charset = $database_connection_charset; $this->db_name = str_replace('`', '', $dbase); $this->db_prefix = $table_prefix; $this->db_connect(); $this->banners_path = '/assets/snippets/adsmanager/banners'; } /***************************************************************************************** BEGIN OF SQL STACK ******************************************************************************************/ function db_connect() { mysql_connect($this->db_server,$this->db_user,$this->db_pass); if (!@ mysql_select_db($this->db_name)) { die("Failed to select the database '" . $this->db_name . "'!" . mysql_error() ); exit; } } function db_query($sql) { if (!$result = @ mysql_query($sql)) { die("Execution of a query to the database failed - " . mysql_error() ." - ". $sql); } else { //echo "
$sql
"; return $result; } } function db_table_exists($table) { if (!$result = @mysql_query("SELECT 1 FROM `".$table."` LIMIT 0")) { return false; } else { return true; } } function db_delete($from,$where='',$fields='') { if (!$from) return false; else { $table = $from; $where = ($where != "") ? "WHERE $where" : ""; return $this->db_query("DELETE $fields FROM $table $where"); } } function db_select($fields = "*", $from = "", $where = "", $orderby = "", $limit = "") { if (!$from) return false; else { $table = $from; $where = ($where != "") ? "WHERE $where" : ""; $orderby = ($orderby != "") ? "ORDER BY $orderby " : ""; $limit = ($limit != "") ? "LIMIT $limit" : ""; return $this->db_query("SELECT $fields FROM $table $where $orderby $limit"); } } function db_update($fields, $table, $where = "") { if (!$table) return false; else { if (!is_array($fields)) $flds = $fields; else { $flds = ''; foreach ($fields as $key => $value) { if (!empty ($flds)) $flds .= ","; $flds .= $key . "="; $flds .= "'" . $value . "'"; } } $where = ($where != "") ? "WHERE $where" : ""; return $this->db_query("UPDATE $table SET $flds $where"); } } function db_insert($fields, $intotable, $fromfields = "*", $fromtable = "", $where = "", $limit = "") { if (!$intotable) return false; else { if (!is_array($fields)) $flds = $fields; else { $keys = array_keys($fields); $values = array_values($fields); $flds = "(" . implode(",", $keys) . ") " . (!$fromtable && $values ? "VALUES('" . implode("','", $values) . "')" : ""); if ($fromtable) { $where = ($where != "") ? "WHERE $where" : ""; $limit = ($limit != "") ? "LIMIT $limit" : ""; $sql = "SELECT $fromfields FROM $fromtable $where $limit"; } } $rt = $this->db_query("INSERT INTO $intotable $flds $sql"); $lid = mysql_insert_id(); return $lid ? $lid : $rt; } } function getRow($ds, $mode = 'assoc') { if ($ds) { if ($mode == 'assoc') { return mysql_fetch_assoc($ds); } elseif ($mode == 'num') { return mysql_fetch_row($ds); } elseif ($mode == 'both') { return mysql_fetch_array($ds, MYSQL_BOTH); } else { die("Unknown get type ($mode) specified for fetchRow - must be empty, 'assoc', 'num' or 'both'."); } } } function db_escape($s) { if (function_exists('mysql_real_escape_string')) { $s = mysql_real_escape_string($s); } else { $s = mysql_escape_string($s); } return $s; } /***************************************************************************************** END OF SQL STACK ******************************************************************************************/ function set_filter($tags) { $this->tags = $tags; } function get_sql_filter() { $where = '`active`=1'; $tags = $this->tags; $taglist = split("[\ ,;]", $tags); if ($this->tags != '') { foreach ($taglist as $tag) { $where = "($where) AND (`tags` LIKE '%$tag%')"; } } return $where; } function show($banners_to_show) { for ($i=0;$i<$banners_to_show;$i++) { $this->show_banner($this->get_random_banner()); } } function get_active_banners_count() { $where = $this->get_sql_filter(); $countres = $this->db_select('count(0) as C', $this->db_prefix . 'ads', $where); $row = $this->getRow($countres); return intval($row['C']); } function get_banner($id) { $id = intval($id); $res = $this->db_select('*', $this->db_prefix . 'ads', "`id`=$id"); $row = $this->getRow($res); return $row; } function goto($id) { $banner = $this->get_banner($id); if ($banner != NULL) {$link = $banner['link'];} else {$link = '/';} echo " "; $this->increment_banner_click($id); } function increment_banner_click($id) { $data = $mysqldate = date('Y-m-d H:i:s'); $ip = $_SERVER["REMOTE_ADDR"]; $fields = array( 'banner'=>$id, 'data'=>$data, 'ip'=>$ip ); $res = $this->db_insert( $fields, $this->db_prefix . 'ads_clicks'); return; } function increment_banner_show($id) { $data = date('Y-m-d'); $where = "((`data`='$data') AND (`banner`=$id))"; $res = $this->db_select('id', $this->db_prefix . 'ads_views', $where); $row = $this->getRow($res); if ($row === false) { $fields = array( 'banner'=>$id, 'data'=>$data, 'views'=>1, ); $res = $this->db_insert( $fields, $this->db_prefix . 'ads_views'); } else { $res = $this->db_query( "UPDATE " . $this->db_prefix . "ads_views SET `views`=`views`+1 WHERE $where"); } return; } function show_banner($id) { if (empty($id)) return; $banner = $this->get_banner($id); $ext = strtolower(str_replace(".", "", strrchr($banner['img'], ".")));; if ($ext == 'swf') { $tpl = $this->flash_tpl; } else { $tpl = $this->image_tpl; } $this->increment_banner_show($id); $snippletPath = MODX_SITE_URL . 'assets/snippets/adsmanager/'; $link = $snippletPath.'adsmanager.inc.php?ad='.intval($id); $tpl = str_replace('[+link+]', $link, $tpl); $tpl = str_replace('[+img+]', $banner['img'], $tpl); $tpl = str_replace('[+title+]', $banner['title'], $tpl); $tpl = str_replace('[+width+]', $banner['width'], $tpl); $tpl = str_replace('[+height+]',$banner['height'], $tpl); echo $tpl; } function get_random_banner() { $banners = $this->get_active_banners_count(); $j = mt_rand(1, $banners); if (count($this->banners_shown) >= $banners) { $this->banners_shown = array(); } while (array_key_exists($j, $this->banners_shown) !== FALSE) { $j = mt_rand(1, $banners); } $this->banners_shown[$j] = true; $j=$j-1; $where = $this->get_sql_filter(); $res = $this->db_select('`id`', $this->db_prefix.'ads', $where, '`id`', "$j,1"); $row = $this->getRow($res); return $row['id']; } function get_banner_stats($start, $end, $active, $tag='') { if (($active == '1') || ($active == true)) { $act = '(active=1)'; } else if (($active == '0') || ($active == false)) { $act = '(active=0)'; } else { $act = ''; } if ($start == '') { $fromto = ''; } else { $fromto = "({$this->db_prefix}ads_clicks.data >= '$start') AND ({$this->db_prefix}ads_clicks.data <= '$end')"; $fromto .= " AND ({$this->db_prefix}ads_views.data >= '$start') AND ({$this->db_prefix}ads_views.data <= '$end')"; } $where = "(true)"; if ($fromto != '') {$where .= " AND $fromto";} if ($act != '') {$where .= " AND $act";} if ($tag != '') {$where .= " AND ({$this->db_prefix}ads.tags LIKE '%$tag%')";} $query = "SELECT {$this->db_prefix}ads.id, {$this->db_prefix}ads.tags, {$this->db_prefix}ads.title, COUNT({$this->db_prefix}ads_clicks.id) AS bclick, SUM({$this->db_prefix}ads_views.views) AS bshow, COUNT({$this->db_prefix}ads_clicks.id) / SUM({$this->db_prefix}ads_views.views) as ctr FROM {$this->db_prefix}ads LEFT JOIN {$this->db_prefix}ads_clicks ON ({$this->db_prefix}ads_clicks.banner={$this->db_prefix}ads.id) LEFT JOIN {$this->db_prefix}ads_views ON ({$this->db_prefix}ads_views.banner={$this->db_prefix}ads.id) WHERE ($where) GROUP BY {$this->db_prefix}ads.id ORDER BY {$this->db_prefix}ads.id "; $res = $this->db_query($query); $out = array(); while ($row = $this->getRow($res)) { $out[] = $row; } return $out; } function add_banner($title, $img, $width, $height, $link, $active, $tags) { $fields = array( 'title' =>$this->db_escape($title), 'img' =>$this->db_escape($img), 'width' =>intval($width), 'height' =>intval($height), 'link' =>$this->db_escape($link), 'active' =>intval($active), 'tags' =>$this->db_escape($tags) ); return $this->db_insert($fields, $this->db_prefix.'ads'); } function save_banner($id, $title, $img, $width, $height, $link, $active, $tags) { $fields = array( 'title' =>$this->db_escape($title), 'img' =>$this->db_escape($img), 'width' =>intval($width), 'height' =>intval($height), 'link' =>$this->db_escape($link), 'active' =>intval($active), 'tags' =>$this->db_escape($tags) ); $id = intval($id); return $this->db_update($fields, $this->db_prefix.'ads', "`id`=$id"); } function delete_banner($id) { $id = intval($id); return $this->db_delete($this->db_prefix.'ads', "`id`=$id"); } function get_tags() { $tags = array(); $res = $this->db_select('tags', $this->db_prefix . 'ads'); while ($row = $this->getRow($res)) { $tagline = $row['tags']; $taglist = split("[.;, ]+", $tagline); foreach ($taglist as $tag) { if (strlen(trim($tag)) > 0) {$tags[] = trim($tag);} } } $tags = array_unique($tags); return $tags; } function filter_tag_cloud() { $tags = $this->get_tags(); $out = '

Display Location Tags:

'; foreach ($tags as $tag) { $out .= " $tag "; } $out .= '
'; return $out; } function add_tag_cloud() { $tags = $this->get_tags(); $out = '

Add Display Location Tags:

'; foreach ($tags as $tag) { $out .= " $tag "; } $out .= '
'; return $out; } function show_clicks($id) { $id = intval($id); $res = $this->db_select('data, ip', $this->db_prefix . 'ads_clicks', "`banner`=$id"); $out = ''; while ($row = $this->getRow($res)) { $out .= ''.$row['data'].''.$row['ip'].''; } return $out; } function install() { $q1 = 'CREATE TABLE `'.$this->db_prefix.'ads` ( `id` int(10) unsigned NOT NULL auto_increment, `title` varchar(255) NOT NULL, `link` varchar(255) NOT NULL, `img` varchar(255) NOT NULL, `width` int(10) unsigned NOT NULL, `height` int(10) unsigned NOT NULL, `active` int(10) unsigned NOT NULL, `tags` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;'; $q2 = 'CREATE TABLE `'.$this->db_prefix.'ads_clicks` ( `id` int(10) unsigned NOT NULL auto_increment, `data` datetime NOT NULL, `banner` int(10) unsigned NOT NULL, `ip` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; '; $q3 = 'CREATE TABLE `'.$this->db_prefix.'ads_views` ( `id` int(10) unsigned NOT NULL auto_increment, `data` datetime NOT NULL, `banner` int(10) unsigned NOT NULL, `views` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; '; $out = ''; if ($this->db_query($q1)) {$out .= 'Table `ads` created
';} else {$out .= 'Cannot create table `ads`';} if ($this->db_query($q2)) {$out .= 'Table `ads_clicks` created
';} else {$out .= 'Cannot create table `ads_clicks`';} if ($this->db_query($q3)) {$out .= 'Table `ads_views` created
';} else {$out .= 'Cannot create table `ads_views`';} $dir = $this->modx_base_path . $this->banners_path; if (mkdir($dir)) {$out .= 'Directory '. $dir . ' sucessfully created';} else {$out .= "Cannot create $dir. It is required and make shure it is writeable!";} return $out; } } if (isset($_GET['ad'])) { $rotator = new AdsManager(); $rotator->goto($_GET['ad']); } ?>