// Prepare the parameters
                 $parameters_complete = array();
                 while (list($key, $val) = each($parameters)) {
                     if (isset($overwrite_source) && $overwrite_source[$val] != '') {
                         $overwrite_link[$val] .= '|source:' . $overwrite_source[$val];
                     }
                     $parameters_complete[] = 'alink' . $key . '={targeturl:' . $overwrite_link[$val] . '}&atar' . $key . '=' . $overwrite_target[$val];
                 }
                 $parameter = implode('&', $parameters_complete);
                 $row['htmltemplate'] = str_replace('{swf_param}', $parameter, $row['htmltemplate']);
             }
             $row['pluginversion'] = phpAds_SWFVersion($result);
             $row['htmlcache'] = addslashes(phpAds_getBannerCache($row));
             $row['htmltemplate'] = addslashes($row['htmltemplate']);
             // Store the HTML Template
             $res = phpAds_dbQuery("\n\t\t\t\t\tUPDATE " . $phpAds_config['tbl_banners'] . " \n\t\t\t\t\tSET url='" . $row['url'] . "', target='" . $row['target'] . "', pluginversion='" . $row['pluginversion'] . "', \n\t\t\t\t\t\thtmltemplate='" . $row['htmltemplate'] . "', htmlcache='" . $row['htmlcache'] . "'\n\t\t\t\t\tWHERE bannerid = '" . $bannerid . "'\n\t\t\t\t") or die(phpAds_dbError());
             // Store the banner
             phpAds_ImageStore($row['storagetype'], $row['filename'], $result, true);
             // Rebuild cache
             if (!defined('LIBVIEWCACHE_INCLUDED')) {
                 include phpAds_path . '/libraries/deliverycache/cache-' . $phpAds_config['delivery_caching'] . '.inc.php';
             }
             phpAds_cacheDelete();
         }
     }
 }
 if (phpAds_isUser(phpAds_Client)) {
     Header('Location: stats-campaign-banners.php?clientid=' . $clientid . '&campaignid=' . $campaignid);
 } else {
     Header('Location: banner-acl.php?clientid=' . $clientid . '&campaignid=' . $campaignid . '&bannerid=' . $bannerid);
 }
function phpAds_SendMaintenanceReport($clientid, $first_unixtimestamp, $last_unixtimestamp, $update = true)
{
    global $phpAds_config, $phpAds_CharSet, $date_format, $strBanner, $strCampaign, $strViews, $strClicks, $strConversions, $strLinkedTo, $strMailSubject, $strMailHeader, $strMailBannerStats, $strMailFooter, $strMailReportPeriod, $strMailReportPeriodAll, $strLogErrorBanners, $strLogErrorClients, $strLogErrorViews, $strLogErrorClicks, $strLogErrorConversions, $strNoStatsForCampaign, $strNoViewLoggedInInterval, $strNoClickLoggedInInterval, $strNoCampaignLoggedInInterval, $strTotal, $strTotalThisPeriod;
    // Convert timestamps to SQL format
    $last_sqltimestamp = date("YmdHis", $last_unixtimestamp);
    $first_sqltimestamp = date("YmdHis", $first_unixtimestamp);
    // Get Client information
    $res_client = phpAds_dbQuery("SELECT" . " clientid" . ",clientname" . ",contact" . ",email" . ",language" . ",report" . ",reportinterval" . ",reportlastdate" . ",UNIX_TIMESTAMP(reportlastdate) AS reportlastdate_t" . " FROM " . $phpAds_config['tbl_clients'] . " WHERE clientid=" . $clientid);
    if (phpAds_dbNumRows($res_client) > 0) {
        $client = phpAds_dbFetchArray($res_client);
        // Load client language strings
        @(include phpAds_path . '/language/english/default.lang.php');
        if ($client['language'] != '') {
            $phpAds_config['language'] = $client['language'];
        }
        if ($phpAds_config['language'] != 'english' && file_exists(phpAds_path . '/language/' . $phpAds_config['language'] . '/default.lang.php')) {
            @(include phpAds_path . '/language/' . $phpAds_config['language'] . '/default.lang.php');
        }
        $active_campaigns = false;
        $log = "";
        // Fetch all campaigns belonging to client
        $res_campaigns = phpAds_dbQuery("SELECT" . " campaignid" . ",campaignname" . ",views" . ",clicks" . ",conversions" . ",expire" . ",UNIX_TIMESTAMP(expire) as expire_st" . ",activate" . ",UNIX_TIMESTAMP(activate) as activate_st" . ",active" . " FROM " . $phpAds_config['tbl_campaigns'] . " WHERE clientid=" . $client['clientid']) or die($strLogErrorClients);
        while ($campaign = phpAds_dbFetchArray($res_campaigns)) {
            // Fetch all banners belonging to campaign
            $res_banners = phpAds_dbQuery("SELECT" . " bannerid" . ",campaignid" . ",URL" . ",active" . ",description" . ",alt" . " FROM " . $phpAds_config['tbl_banners'] . " WHERE campaignid=" . $campaign['campaignid']) or die($strLogErrorBanners);
            $active_banners = false;
            $log .= "\n" . $strCampaign . "  " . strip_tags(phpAds_buildName($campaign['campaignid'], $campaign['campaignname'])) . "\n";
            $log .= "=======================================================\n\n";
            while ($row_banners = phpAds_dbFetchArray($res_banners)) {
                $adviews = phpAds_totalViews($row_banners["bannerid"]);
                $client["views_used"] = $adviews;
                $adclicks = phpAds_totalClicks($row_banners["bannerid"]);
                $campaign["clicks_used"] = $adclicks;
                $adconversions = phpAds_totalConversions($row_banners["bannerid"]);
                $campaign["conversions_used"] = $adconversions;
                if ($adviews > 0 || $adclicks > 0 || $adconversions > 0) {
                    $log .= $strBanner . "  " . strip_tags(phpAds_buildBannerName($row_banners['bannerid'], $row_banners['description'], $row_banners['alt'])) . "\n";
                    $log .= $strLinkedTo . ": " . $row_banners['URL'] . "\n";
                    $log .= "-------------------------------------------------------\n";
                    $active_banner_stats = false;
                    if ($adviews > 0) {
                        $log .= $strViews . " (" . $strTotal . "):    " . $adviews . "\n";
                        // Fetch all adviews belonging to banner belonging to client, grouped by day
                        $res_adviews = phpAds_dbQuery("SELECT" . " SUM(views) as qnt" . ",DATE_FORMAT(day, '{$date_format}') as t_stamp_f" . ",TO_DAYS(day) AS the_day" . " FROM " . $phpAds_config['tbl_adstats'] . " WHERE bannerid=" . $row_banners['bannerid'] . " AND views>0" . " AND UNIX_TIMESTAMP(day)>=" . $first_unixtimestamp . " AND UNIX_TIMESTAMP(day)<" . $last_unixtimestamp . " GROUP BY day" . " ORDER BY day DESC") or die($strLogErrorViews . " " . phpAds_dbError());
                        if (phpAds_dbNumRows($res_adviews)) {
                            $total = 0;
                            while ($row_adviews = phpAds_dbFetchArray($res_adviews)) {
                                $log .= "      " . $row_adviews['t_stamp_f'] . ":   " . $row_adviews['qnt'] . "\n";
                                $total += $row_adviews['qnt'];
                            }
                            $log .= $strTotalThisPeriod . ": " . $total . "\n";
                            $active_banner_stats = true;
                        } else {
                            $log .= "      " . $strNoViewLoggedInInterval . "\n";
                        }
                    }
                    if ($adclicks > 0) {
                        // Total adclicks
                        $log .= "\n" . $strClicks . " (" . $strTotal . "):   " . $adclicks . "\n";
                        // Fetch all adclicks belonging to banner belonging to client, grouped by day
                        $res_adclicks = phpAds_dbQuery("SELECT" . " SUM(clicks) as qnt" . ",DATE_FORMAT(day, '{$date_format}') as t_stamp_f" . ",TO_DAYS(day) AS the_day" . " FROM " . $phpAds_config['tbl_adstats'] . " WHERE bannerid = " . $row_banners['bannerid'] . " AND clicks>0" . " AND UNIX_TIMESTAMP(day)>=" . $first_unixtimestamp . " AND UNIX_TIMESTAMP(day)<" . $last_unixtimestamp . " GROUP BY day" . " ORDER BY day DESC") or die($strLogErrorClicks . " " . phpAds_dbError());
                        if (phpAds_dbNumRows($res_adclicks)) {
                            $total = 0;
                            while ($row_adclicks = phpAds_dbFetchArray($res_adclicks)) {
                                $log .= "      " . $row_adclicks['t_stamp_f'] . ":   " . $row_adclicks['qnt'] . "\n";
                                $total += $row_adclicks['qnt'];
                            }
                            $log .= $strTotalThisPeriod . ": " . $total . "\n";
                            $active_banner_stats = true;
                        } else {
                            $log .= "      " . $strNoClickLoggedInInterval . "\n";
                        }
                    }
                    if ($adconversions > 0) {
                        // Total adconversions
                        $log .= "\n" . $strConversions . " (" . $strTotal . "):   " . $adconversions . "\n";
                        // Fetch all adclicks belonging to banner belonging to client, grouped by day
                        $res_adconversions = phpAds_dbQuery("SELECT" . " SUM(conversions) as qnt" . ",DATE_FORMAT(day, '{$date_format}') as t_stamp_f" . ",TO_DAYS(day) AS the_day" . " FROM " . $phpAds_config['tbl_adstats'] . " WHERE bannerid = " . $row_banners['bannerid'] . " AND conversions>0" . " AND UNIX_TIMESTAMP(day)>=" . $first_unixtimestamp . " AND UNIX_TIMESTAMP(day)<" . $last_unixtimestamp . " GROUP BY day" . " ORDER BY day DESC") or die($strLogErrorConversions . " " . phpAds_dbError());
                        if (phpAds_dbNumRows($res_adconversions)) {
                            $total = 0;
                            while ($row_adconversions = phpAds_dbFetchArray($res_adconversions)) {
                                $log .= "      " . $row_adcconversions['t_stamp_f'] . ":   " . $row_adconversions['qnt'] . "\n";
                                $total += $row_adconversions['qnt'];
                            }
                            $log .= $strTotalThisPeriod . ": " . $total . "\n";
                            $active_banner_stats = true;
                        } else {
                            $log .= "      " . $strNoConversionLoggedInInterval . "\n";
                        }
                    }
                    $log .= "\n\n";
                    if ($active_banner_stats == true || $active_banner_stats == false && $campaign['active'] == 't') {
                        $active_banners = true;
                    }
                }
            }
            if ($active_banners == true) {
                $active_campaigns = true;
            } else {
                $log .= $strNoStatsForCampaign . "\n\n\n";
            }
        }
        // E-mail Stats to active clients
        if ($client["email"] != '' && $active_campaigns == true) {
            $Subject = $strMailSubject . ": " . $client["clientname"];
            $Body = "{$strMailHeader}\n";
            $Body .= "{$strMailBannerStats}\n";
            if ($first_unixtimestamp == 0) {
                $Body .= "{$strMailReportPeriodAll}\n\n";
            } else {
                $Body .= "{$strMailReportPeriod}\n\n";
            }
            $Body .= "{$log}\n";
            $Body .= "{$strMailFooter}";
            $Body = str_replace("{clientname}", $client['clientname'], $Body);
            $Body = str_replace("{contact}", $client['contact'], $Body);
            $Body = str_replace("{adminfullname}", $phpAds_config['admin_fullname'], $Body);
            $Body = str_replace("{startdate}", date(str_replace('%', '', $date_format), $first_unixtimestamp), $Body);
            $Body = str_replace("{enddate}", date(str_replace('%', '', $date_format), $last_unixtimestamp), $Body);
            if ($phpAds_config['userlog_email']) {
                phpAds_userlogAdd(phpAds_actionAdvertiserReportMailed, $client['clientid'], $Subject . "\n\n" . $Body);
            }
            if (phpAds_sendMail($client['email'], $client['contact'], $Subject, $Body)) {
                // Update last run
                if ($update == true) {
                    $res_update = phpAds_dbQuery("UPDATE " . $phpAds_config['tbl_clients'] . " SET reportlastdate=NOW() WHERE clientid=" . $client['clientid']);
                }
                return true;
            }
        }
    }
    return false;
}
function phpAds_sqlDie()
{
    global $phpAds_dbmsname, $phpAds_version_readable, $phpAds_version, $phpAds_productname;
    global $phpAds_last_query;
    $error = phpAds_dbError();
    $corrupt = false;
    if ($phpAds_dbmsname == 'MySQL') {
        $errornumber = phpAds_dbErrorNo();
        if ($errornumber == 1027 || $errornumber == 1039) {
            $corrupt = true;
        }
        if ($errornumber == 1016 || $errornumber == 1030) {
            // Probably corrupted table, do additional check
            eregi("[0-9]+", $error, $matches);
            if ($matches[0] == 126 || $matches[0] == 127 || $matches[0] == 132 || $matches[0] == 134 || $matches[0] == 135 || $matches[0] == 136 || $matches[0] == 141 || $matches[0] == 144 || $matches[0] == 145) {
                $corrupt = true;
            }
        }
    }
    if ($corrupt) {
        $title = $GLOBALS['strErrorDBSerious'];
        $message = $GLOBALS['strErrorDBNoDataSerious'];
        if (phpAds_isLoggedIn() && phpAds_isUser(phpAds_Admin)) {
            $message .= " (" . $error . ").<br><br>" . $GLOBALS['strErrorDBCorrupt'];
        } else {
            $message .= ".<br>" . $GLOBALS['strErrorDBContact'];
        }
    } else {
        $title = $GLOBALS['strErrorDBPlain'];
        $message = $GLOBALS['strErrorDBNoDataPlain'];
        if (phpAds_isLoggedIn() && phpAds_isUser(phpAds_Admin)) {
            $message .= $GLOBALS['strErrorDBSubmitBug'];
            $last_query = $phpAds_last_query;
            $message .= "<br><br><table cellpadding='0' cellspacing='0' border='0'>";
            $message .= "<tr><td valign='top' nowrap><b>Version:</b>&nbsp;&nbsp;&nbsp;</td><td>" . $phpAds_productname . " " . $phpAds_version_readable . " (" . $phpAds_version . ")</td></tr>";
            $message .= "<tr><td>&nbsp;</td><td>PHP " . phpversion() . " / " . $phpAds_dbmsname . " " . phpAds_dbResult(phpAds_dbQuery('SELECT VERSION()'), 0, 0) . "</td></tr>";
            $message .= "<tr><td valign='top' nowrap><b>Page:</b></td><td>" . $_SERVER['PHP_SELF'] . "</td></tr>";
            $message .= "<tr><td valign='top' nowrap><b>Error:</b></td><td>" . $error . "</td></tr>";
            $message .= "<tr><td valign='top' nowrap><b>Query:</b></td><td>" . $last_query . "</td></tr>";
            $message .= "</table>";
        }
    }
    phpAds_Die($title, $message);
}
function phpAds_SendMaintenanceReport($clientid, $first_unixtimestamp, $last_unixtimestamp, $update = true)
{
    global $phpAds_config;
    global $date_format;
    global $strMailSubject, $strMailHeader, $strMailBannerStats, $strMailFooter, $strMailReportPeriod;
    global $strLogErrorClients, $strLogErrorBanners, $strLogErrorViews, $strNoStatsForCampaign;
    global $strLogErrorClicks, $strNoClickLoggedInInterval, $strNoViewLoggedInInterval;
    global $strTotal, $strTotalThisPeriod;
    global $strCampaign, $strBanner, $strLinkedTo, $strViews, $strClicks, $strMailReportPeriodAll;
    global $phpAds_CharSet;
    // Convert timestamps to SQL format
    $last_sqltimestamp = date("YmdHis", $last_unixtimestamp);
    $first_sqltimestamp = date("YmdHis", $first_unixtimestamp);
    // Get Client information
    $res_client = phpAds_dbQuery("\n\t\tSELECT\n\t\t\tclientid,\n\t\t\tclientname,\n\t\t\tcontact,\n\t\t\temail,\n\t\t\tlanguage,\n\t\t\treport,\n\t\t\treportinterval,\n\t\t\treportlastdate,\n\t\t\tUNIX_TIMESTAMP(reportlastdate) AS reportlastdate_t\n\t\tFROM\n\t\t\t" . $phpAds_config['tbl_clients'] . "\n\t\tWHERE\n\t\t\tclientid='" . $clientid . "'\n\t\t");
    if (phpAds_dbNumRows($res_client) > 0) {
        $client = phpAds_dbFetchArray($res_client);
        // Load client language strings
        @(include phpAds_path . '/language/english/default.lang.php');
        if ($client['language'] != '') {
            $phpAds_config['language'] = $client['language'];
        }
        if ($phpAds_config['language'] != 'english' && file_exists(phpAds_path . '/language/' . $phpAds_config['language'] . '/default.lang.php')) {
            @(include phpAds_path . '/language/' . $phpAds_config['language'] . '/default.lang.php');
        }
        $active_campaigns = false;
        $log = "";
        // Fetch all campaings belonging to client
        $res_campaigns = phpAds_dbQuery("\n\t\t\tSELECT\n\t\t\t\tclientid,\n\t\t\t\tclientname,\n\t\t\t\tviews,\n\t\t\t\tclicks,\n\t\t\t\texpire,\n\t\t\t\tUNIX_TIMESTAMP(expire) as expire_st,\n\t\t\t\tactivate,\n\t\t\t\tUNIX_TIMESTAMP(activate) as activate_st,\n\t\t\t\tactive\n\t\t\tFROM\n\t\t\t\t" . $phpAds_config['tbl_clients'] . "\n\t\t\tWHERE\n\t\t\t\tparent = " . $client['clientid'] . "\n\t\t\n\t\t") or die($strLogErrorClients);
        while ($campaign = phpAds_dbFetchArray($res_campaigns)) {
            $current_log = '';
            // Fetch all banners belonging to campaign
            $res_banners = phpAds_dbQuery("\n\t\t\t\tSELECT\n\t\t\t\t\tbannerid,\n\t\t\t\t\tclientid,\n\t\t\t\t\turl,\n\t\t\t\t\tactive,\n\t\t\t\t\tdescription,\n\t\t\t\t\talt\n\t\t\t\tFROM\n\t\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\t\tWHERE\n\t\t\t\t\tclientid = " . $campaign['clientid'] . "\n\t\t\t\t") or die($strLogErrorBanners);
            $active_banners = false;
            $current_log .= "\n" . $strCampaign . "  " . phpAds_buildClientName($campaign['clientid'], $campaign['clientname'], false) . "\n";
            $current_log .= "=======================================================\n\n";
            while ($row_banners = phpAds_dbFetchArray($res_banners)) {
                $adviews = phpAds_totalViews($row_banners["bannerid"]);
                $client["views_used"] = $adviews;
                $adclicks = phpAds_totalClicks($row_banners["bannerid"]);
                $campaign["clicks_used"] = $adclicks;
                $current_log .= $strBanner . "  " . phpAds_buildBannerName($row_banners['bannerid'], $row_banners['description'], $row_banners['alt'], 0, false) . "\n";
                $current_log .= $strLinkedTo . ": " . $row_banners['url'] . "\n";
                $current_log .= "-------------------------------------------------------\n";
                $active_banner_stats = false;
                if ($adviews > 0) {
                    $current_log .= $strViews . " (" . $strTotal . "):    " . $adviews . "\n";
                    // Fetch all adviews belonging to banner belonging to client, grouped by day
                    if ($phpAds_config['compact_stats']) {
                        $res_adviews = phpAds_dbQuery("\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\tSUM(views) as qnt,\n\t\t\t\t\t\t\t\tDATE_FORMAT(day, '{$date_format}') as t_stamp_f,\n\t\t\t\t\t\t\t\tTO_DAYS(day) AS the_day\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_adstats'] . "\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tbannerid = " . $row_banners['bannerid'] . " AND\n\t\t\t\t\t\t\t\tviews > 0 AND\n\t\t\t\t\t\t\t\tUNIX_TIMESTAMP(day) >= {$first_unixtimestamp} AND\n\t\t\t\t\t\t\t\tUNIX_TIMESTAMP(day) < {$last_unixtimestamp}\n\t\t\t\t\t\t\tGROUP BY\n\t\t\t\t\t\t\t\tday\n\t\t\t\t\t\t\tORDER BY\n\t\t\t\t\t\t\t\tday DESC\n\t\t\t\t\t\t\t") or die("{$strLogErrorViews} " . phpAds_dbError());
                    } else {
                        $res_adviews = phpAds_dbQuery("\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\t*,\n\t\t\t\t\t\t\t\tcount(*) as qnt,\n\t\t\t\t\t\t\t\tDATE_FORMAT(t_stamp, '{$date_format}') as t_stamp_f,\n\t\t\t\t\t\t\t\tTO_DAYS(t_stamp) AS the_day\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_adviews'] . "\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tbannerid = " . $row_banners['bannerid'] . " AND\n\t\t\t\t\t\t\t\tt_stamp >= {$first_sqltimestamp} AND\n\t\t\t\t\t\t\t\tt_stamp < {$last_sqltimestamp}\n\t\t\t\t\t\t\tGROUP BY\n\t\t\t\t\t\t\t\tthe_day\n\t\t\t\t\t\t\tORDER BY\n\t\t\t\t\t\t\t\tthe_day DESC\n\t\t\t\t\t\t\t") or die("{$strLogErrorViews} " . phpAds_dbError());
                    }
                    if (phpAds_dbNumRows($res_adviews)) {
                        $total = 0;
                        while ($row_adviews = phpAds_dbFetchArray($res_adviews)) {
                            $current_log .= "      " . $row_adviews['t_stamp_f'] . ":   " . $row_adviews['qnt'] . "\n";
                            $total += $row_adviews['qnt'];
                        }
                        $current_log .= $strTotalThisPeriod . ": " . $total . "\n";
                        $active_banner_stats = true;
                    } else {
                        $current_log .= "      " . $strNoViewLoggedInInterval . "\n";
                    }
                }
                if ($adclicks > 0) {
                    // Total adclicks
                    $current_log .= "\n" . $strClicks . " (" . $strTotal . "):   " . $adclicks . "\n";
                    // Fetch all adclicks belonging to banner belonging to client, grouped by day
                    if ($phpAds_config['compact_stats']) {
                        $res_adclicks = phpAds_dbQuery("\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\tSUM(clicks) as qnt,\n\t\t\t\t\t\t\t\tDATE_FORMAT(day, '{$date_format}') as t_stamp_f,\n\t\t\t\t\t\t\t\tTO_DAYS(day) AS the_day\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_adstats'] . "\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tbannerid = " . $row_banners['bannerid'] . " AND\n\t\t\t\t\t\t\t\tclicks > 0 AND\n\t\t\t\t\t\t\t\tUNIX_TIMESTAMP(day) >= {$first_unixtimestamp} AND\n\t\t\t\t\t\t\t\tUNIX_TIMESTAMP(day) < {$last_unixtimestamp}\n\t\t\t\t\t\t\tGROUP BY\n\t\t\t\t\t\t\t\tday\n\t\t\t\t\t\t\tORDER BY\n\t\t\t\t\t\t\t\tday DESC\n\t\t\t\t\t\t\t") or die("{$strLogErrorClicks} " . phpAds_dbError());
                    } else {
                        $res_adclicks = phpAds_dbQuery("\n\t\t\t\t\t\t\tSELECT\n\t\t\t\t\t\t\t\tcount(*) as qnt,\n\t\t\t\t\t\t\t\tDATE_FORMAT(t_stamp, '{$date_format}') as t_stamp_f,\n\t\t\t\t\t\t\t\tTO_DAYS(t_stamp) AS the_day\n\t\t\t\t\t\t\tFROM\n\t\t\t\t\t\t\t\t" . $phpAds_config['tbl_adclicks'] . "\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tbannerid = " . $row_banners['bannerid'] . " AND\n\t\t\t\t\t\t\t\tt_stamp >= {$first_sqltimestamp} AND\n\t\t\t\t\t\t\t\tt_stamp < {$last_sqltimestamp}\n\t\t\t\t\t\t\tGROUP BY\n\t\t\t\t\t\t\t\tthe_day\n\t\t\t\t\t\t\tORDER BY\n\t\t\t\t\t\t\t\tthe_day DESC\n\t\t\t\t\t\t\t") or die("{$strLogErrorClicks} " . phpAds_dbError());
                    }
                    if (phpAds_dbNumRows($res_adclicks)) {
                        $total = 0;
                        while ($row_adclicks = phpAds_dbFetchArray($res_adclicks)) {
                            $current_log .= "      " . $row_adclicks['t_stamp_f'] . ":   " . $row_adclicks['qnt'] . "\n";
                            $total += $row_adclicks['qnt'];
                        }
                        $current_log .= $strTotalThisPeriod . ": " . $total . "\n";
                        $active_banner_stats = true;
                    } else {
                        $current_log .= "      " . $strNoClickLoggedInInterval . "\n";
                    }
                }
                if ($adclicks == 0 && $adviews == 0) {
                    $current_log .= "      " . $strNoStatsForCampaign . "\n";
                }
                $current_log .= "\n\n";
                if ($campaign['active'] == 't' || $active_banner_stats == true) {
                    $active_banners = true;
                }
            }
            if ($active_banners == true) {
                $active_campaigns = true;
                $log .= $current_log;
            }
        }
        // E-mail Stats to active clients
        if ($client["email"] != '' && $active_campaigns == true) {
            $Subject = $strMailSubject . ": " . $client["clientname"];
            $Body = "{$strMailHeader}\n";
            $Body .= "{$strMailBannerStats}\n";
            if ($first_unixtimestamp == 0) {
                $Body .= "{$strMailReportPeriodAll}\n\n";
            } else {
                $Body .= "{$strMailReportPeriod}\n\n";
            }
            $Body .= "{$log}\n";
            $Body .= "{$strMailFooter}";
            $Body = str_replace("{clientname}", $client['clientname'], $Body);
            $Body = str_replace("{contact}", $client['contact'], $Body);
            $Body = str_replace("{adminfullname}", $phpAds_config['admin_fullname'], $Body);
            $Body = str_replace("{startdate}", date(str_replace('%', '', $date_format), $first_unixtimestamp), $Body);
            $Body = str_replace("{enddate}", date(str_replace('%', '', $date_format), $last_unixtimestamp), $Body);
            if ($phpAds_config['userlog_email']) {
                phpAds_userlogAdd(phpAds_actionAdvertiserReportMailed, $client['clientid'], $Subject . "\n\n" . $Body);
            }
            phpAds_sendMail($client['email'], $client['contact'], $Subject, $Body);
            // Update last run
            if ($update == true) {
                $res_update = phpAds_dbQuery("UPDATE " . $phpAds_config['tbl_clients'] . " SET reportlastdate=NOW() WHERE clientid=" . $client['clientid']);
            }
            return true;
        }
    }
    return false;
}