<?php
if($gads_show) {
    $module_dir = "gads_page_level";

    if($gads_show && isset($page_level_params) && is_array($page_level_params) && count($page_level_params) >0) {

        //echo '<pre>$page_level_params = ', var_export($page_level_params, true), '</pre>';

        $modules_path_dir = dirname(__FILE__) . "/..";

        // Pull in the NuSOAP code
        require_once("$modules_path_dir/wsbin/lib/nusoap.php");
        include_once(dirname(__FILE__) . "/../common/gads_content/functions.php");

        $webservices_url = "https://www.apps24.com/srvdispatcher.php?wsdl";

        $out_path_cache = dirname(__FILE__) . "/../..";
        $all_cache_folder = "$out_path_cache/all_cache/" . $app_conf["addonname"];
        $all_cache_folder = str_ireplace("//", "/", $all_cache_folder);
        $all_cache_normal_gads_content = "$all_cache_folder/gads_content";

        if(isset($page_level_params['ws_ad_unit_id']) && $page_level_params['ws_ad_unit_id'] >0) {
            $ad_unit_id = $page_level_params['ws_ad_unit_id'];

            $ws_request_ad_unit = array(
                'server_name' 		=> 'gads_content',
                'function_name'		=> 'getAdSenseContentUnitInfo',
                'function_params'	=> $ad_unit_id
            );

            /* Check if cache folders are in place ******************************************************/
            if(!file_exists("$all_cache_folder") || !is_dir("$all_cache_folder")) {
                if( mkdir("$all_cache_folder", 0755) ) {

                    if( mkdir("$all_cache_normal_gads_content", 0755) ) {
                        //echo "<br/>$all_cache_normal_gads_content created";
                    }
                    else {
                        //echo "<br/>Cannot create dir [$all_cache_normal_gads_content]";
                    }
                }
                else {
                    //echo "<br/>Cannot create dir [$all_cache_folder]";
                }
            }
            else {
                if(!file_exists("$all_cache_normal_gads_content") || !is_dir("$all_cache_normal_gads_content")) {
                    if( mkdir("$all_cache_normal_gads_content", 0755) ) {
                        //chmod("$general_path/all_cache/weather", 777);
                        //echo "<br/>$all_cache_normal_gads_content created";
                    }
                    else {
                        //echo "<br/>Cannot create dir [$all_cache_normal_gads_content]";
                    }
                }
            }
            /* END Check for cache folders  *************************************************************/

            $have_it_on_cache = false;
            $md5 = md5(serialize($ws_request_ad_unit));
            $cache_file = $all_cache_normal_gads_content . "/" . $md5;
            if(file_exists($cache_file)) {
                //echo "<br/>[$cache_file] exists";
                $cache_time = filemtime($cache_file);
                $current_time = time();
                if($current_time - $cache_time < 120) {
                    $have_it_on_cache = true;
                    //echo "<br/>$cache_file time is earlier than 15 min";
                }
                else {
                    unlink($cache_file);
                    //echo "<br/>$cache_file time is older than 15 min";
                }
            }
            if($have_it_on_cache) {
                $cache_get = file_get_contents($cache_file);

                if($cache_get != "") {
                    $cache_get = unserialize($cache_get);
                    $current_ad = $cache_get;
                }
            }
            else {
                //echo "<br/> --- here";
                $current_ad = getAdSenseContentUnitInfo($webservices_url, $ws_request_ad_unit);
                //echo "<br/>DO NOT Have it on cache";
                $cache_put = serialize($current_ad);
                if( true == ($fc = fopen($cache_file, "w") ) ) {
                    fwrite($fc, $cache_put, strlen($cache_put));
                    fclose($fc);
                }
            }

            if(!empty($current_ad)) {

                $show_gad = true;
                switch($current_ad['ws_ad_unit_type']) {
                    case "page_level":
                        $gads['page_level'] += 1;
                        if($gads['page_level'] > 1) $show_gad = false;
                        break;
                }


                if($show_gad) {
                    $ad_code_js = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>';
                    $ad_unit_code = stripslashes($current_ad['ws_ad_unit_code']);
                    $ad_unit_code = str_replace($ad_code_js,'', $ad_unit_code);
                    echo $ad_unit_code;
                    //echo stripslashes($current_ad['ws_ad_unit_code']);
                    ?>

                    <?php
                }
            }
        }
    }
}