<commit_before><?php

#Refuse if a direct call has been made
if(!defined('Sprinklers')){header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);exit();}

#Include configuration
require_once("config.php");

date_default_timezone_set('UTC');

#Change time out to 5 seconds (default is 60)
ini_set('default_socket_timeout', 10);

#Set script timeout to 6 seconds to give time for socket to timeout and return error
set_time_limit(10);

if (!isset($_SESSION["woeid"]) && isset($_REQUEST["os_ip"])) $_SESSION["woeid"] = get_woeid();

#Get Base URL of Site
if (isset($_SERVER['SERVER_NAME'])) $base_url = (($force_ssl) ? "https://" : "http://").$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];

#Call action if requested and allowed
if (isset($_REQUEST['action'])) {
	if (is_callable($_REQUEST['action'])) {
		if (in_array($_REQUEST["action"], array("current_status","submit_stations","make_stations_list","get_weather","runonce","send_en_mm","test_ip","make_settings_list","make_list_status","make_list_manual","fresh_program","make_all_programs","make_runonce","spoff","spon","mm_off","mm_on","en_on","en_off","rbt","rsn","raindelay","submit_options","delete_program","update_program","get_preview","import_config","export_config"))) {
			call_user_func($_REQUEST['action']);
		}
		exit();
	} else {
		exit();
	}
}

#Weather functions

#Resolve location to WOEID
function get_woeid() {
    $options = get_options();
    $data = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.placefinder%20where%20text=%22".urlencode($options["loc"])."%22");
    preg_match("/<woeid>(\d+)<\/woeid>/", $data, $woeid);
    return intval($woeid[1]);
}

#Get the current weather code and temp
function get_weather_data() {
    $data = file_get_contents("http://weather.yahooapis.com/forecastrss?w=".$_SESSION["woeid"]);
    if ($data === false) return array();
    preg_match("/<yweather:condition\s+text=\"([\w|\s]+)\"\s+code=\"(\d+)\"\s+temp=\"(\d+)\"\s+date=\"(.*)\"/", $data, $newdata);
    preg_match("/<title>Yahoo! Weather - (.*)<\/title>/",$data,$loc);
    preg_match("/<yweather:location .*?country=\"(.*?)\"\/>/",$data,$region);
    $region = $region[1];
    if ($region == "United States" || $region == "Bermuda" || $region == "Palau") {
        $temp = $newdata[3]."&#176;F";
    } else {
        $temp = intval(($newdata[3]-32)*(5/9))."&#176;C";
    }
    return array("text"=>$newdata[1],"code"=>$newdata[2],"temp"=>$temp,"date"=>$newdata[4],"location"=>$loc[1]);
}

function get_weather() {
    echo json_encode(get_weather_data());
}

#Export/Import
function export_config() {
    $data = get_from_os("/gp?d=0");

    preg_match("/pd=\[\];(.*);/", $data, $progs);
    $progs = explode(";", $progs[1]);

    $i = 0;
    foreach ($progs as $prog) {
        $tmp = explode("=", $prog);
        $newdata["programs"][$i] = $tmp[1];
        $i++;
    }
    $newdata["options"] = get_options();

    $vs = get_stations();
    $newdata["stations"] = $vs["stations"];
    $newdata["masop"] = $vs["masop"];

    echo json_encode($newdata);
}

function import_config() {
    if (!isset($_REQUEST["data"])) echo 0;
    $data = json_decode($_REQUEST["data"],true);
    if (is_null($data)) echo 0;
    $cs = "/cs?pw="; $co = "/co?pw="; $cp_start = "/cp?pw="; $i = 0;
    foreach ($data["options"] as $key => $value) {
        if (is_array($value)) {
            if (in_array($key, array(16,21,22,25)) && $value["val"] == 0) continue; 
            $co .= "&o".$key."=".$value["val"];
        } else if ($key == "loc") {
            $co .= "&".$key."=".urlencode($value);
        }
    }
    send_to_os($co);
    foreach ($data["stations"] as $station) {
        $cs .= "&s".$i."=".urlencode($station);
        $i++;
    }
    $i = 0;
    foreach ($data["masop"] as $bit) {
        $cs .= "&m".$i."=".urlencode($bit);
        $i++;
    }
    send_to_os($cs);
    send_to_os("/dp?pw=&pid=-1");
    foreach ($data["programs"] as $prog) {
        send_to_os($cp_start."&pid=-1&v=".$prog);
    }
}

#OpenSprinkler functions

#Get station names
function get_stations() {
    $data = get_from_os("/vs");
    preg_match("/snames=\[(.*)\];/", $data, $matches);
    $data = str_getcsv($matches[1],",","'");
    foreach ($data as $station) {
        $station = preg_replace("/\\\u([0-9a-eA-E]{4})/", "&#x\\1;", $station);
        $stations[] = $station;
    }

    preg_match("/masop=\[(.*?)\]/", $data, $masop);
    $masop = explode(",",$masop[1]);

    #Pop the last element off the array which is always an extra empty string
    array_pop($stations);
    return array("stations" => $stations,"masop" => $masop);
}

#Get program information
function get_programs() {
    $data = get_from_os("/gp?d=0");

    preg_match_all("/(nprogs|nboards|ipas|mnp)=[\w|\d|.\"]+/", $data, $opts);

    foreach ($opts[0] as $variable) {
        if ($variable === "") continue;
        $tmp = str_replace('"','',explode("=", $variable));
        $newdata[$tmp[0]] = $tmp[1];
    }

    preg_match("/pd=\[\];(.*);/", $data, $progs);
    if (empty($progs)) return $progs;
    $progs = explode(";", $progs[1]);

    $i = 0;
    foreach ($progs as $prog) {
        $tmp = explode("=", $prog);
        $tmp2 = str_replace("[", "", $tmp[1]);
        $tmp2 = str_replace("]", "", $tmp2); 
        $program = explode(",", $tmp2);

        #Reset variables
        $days0 = $program[1]; $days1 = $program[2]; $even = false; $odd = false; $interval = false; $days = ""; $stations = "";

        $newdata["programs"][$i]["en"] = $program[0];
        $newdata["programs"][$i]["start"] = $program[3];
        $newdata["programs"][$i]["end"] = $program[4];
        $newdata["programs"][$i]["interval"] = $program[5];
        $newdata["programs"][$i]["duration"] = $program[6];

        for ($n=0; $n < $newdata["nboards"]; $n++) {
            $bits = $program[7+$n];
            for ($s=0; $s < 8; $s++) { 
                $stations .= ($bits&(1<<$s)) ? "1" : "0";
            }
        }
        $newdata["programs"][$i]["stations"] = $stations;

        if(($days0&0x80)&&($days1>1)){
            #This is an interval program
            $days=array($days1,$days0&0x7f);
            $interval = true;
        } else {
            #This is a weekly program 
            for($d=0;$d<7;$d++) {
                if ($days0&(1<<$d)) {
                    $days .= "1";
                } else {
                    $days .= "0";
                }
            }
            if(($days0&0x80)&&($days1==0))  {$even = true;}
            if(($days0&0x80)&&($days1==1))  {$odd = true;}
        }

        $newdata["programs"][$i]["days"] = $days;
        $newdata["programs"][$i]["is_even"] = $even;
        $newdata["programs"][$i]["is_odd"] = $odd;
        $newdata["programs"][$i]["is_interval"] = $interval;
        $i++;
    }
    return $newdata;
}

function get_preview() {
    process_programs($_REQUEST["m"],$_REQUEST["d"],$_REQUEST["y"]);
}

function process_programs($month,$day,$year) {
    $newdata = array();

    $newdata["settings"] = get_settings();
    $vs = get_stations();
    $newdata["stations"] = $vs["stations"];

    $data = get_from_os("/gp?d=".$day."&m=".$month."&y=".$year);
    preg_match_all("/(seq|mas|wl|sdt|mton|mtoff|devday|devmin|dd|mm|yy|nprogs|nboards|ipas|mnp)=[\w|\d|.\"]+/", $data, $opts);

    foreach ($opts[0] as $variable) {
        if ($variable === "") continue;
        $tmp = str_replace('"','',explode("=", $variable));
        $newdata[$tmp[0]] = $tmp[1];
    }

    #Fix for missing sequential option on RPi interval program
    if (!isset($newdata["seq"])) $newdata["seq"] = 1;

    preg_match("/masop=\[(.*?)\]/", $data, $masop);
    $newdata["masop"] = explode(",",$masop[1]);<commit_msg>Several bug fixes<commit_after><?php

#Refuse if a direct call has been made
if(!defined('Sprinklers')){header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);exit();}

#Include configuration
require_once("config.php");

date_default_timezone_set('UTC');

#Change time out to 5 seconds (default is 60)
ini_set('default_socket_timeout', 10);

#Set script timeout to 6 seconds to give time for socket to timeout and return error
set_time_limit(10);

if (!isset($_SESSION["woeid"]) && isset($_REQUEST["os_ip"])) $_SESSION["woeid"] = get_woeid();

#Get Base URL of Site
if (isset($_SERVER['SERVER_NAME'])) $base_url = (($force_ssl) ? "https://" : "http://").$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];

#Call action if requested and allowed
if (isset($_REQUEST['action'])) {
	if (is_callable($_REQUEST['action'])) {
		if (in_array($_REQUEST["action"], array("current_status","submit_stations","make_stations_list","get_weather","runonce","send_en_mm","test_ip","make_settings_list","make_list_status","make_list_manual","fresh_program","make_all_programs","make_runonce","spoff","spon","mm_off","mm_on","en_on","en_off","rbt","rsn","raindelay","submit_options","delete_program","update_program","get_preview","import_config","export_config"))) {
			call_user_func($_REQUEST['action']);
		}
		exit();
	} else {
		exit();
	}
}

#Weather functions

#Resolve location to WOEID
function get_woeid() {
    $options = get_options();
    $data = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.placefinder%20where%20text=%22".urlencode($options["loc"])."%22");
    preg_match("/<woeid>(\d+)<\/woeid>/", $data, $woeid);
    return intval($woeid[1]);
}

#Get the current weather code and temp
function get_weather_data() {
    $data = file_get_contents("http://weather.yahooapis.com/forecastrss?w=".$_SESSION["woeid"]);
    if ($data === false) return array();
    preg_match("/<yweather:condition\s+text=\"([\w|\s]+)\"\s+code=\"(\d+)\"\s+temp=\"(\d+)\"\s+date=\"(.*)\"/", $data, $newdata);
    preg_match("/<title>Yahoo! Weather - (.*)<\/title>/",$data,$loc);
    preg_match("/<yweather:location .*?country=\"(.*?)\"\/>/",$data,$region);
    $region = $region[1];
    if ($region == "United States" || $region == "Bermuda" || $region == "Palau") {
        $temp = $newdata[3]."&#176;F";
    } else {
        $temp = intval(($newdata[3]-32)*(5/9))."&#176;C";
    }
    return array("text"=>$newdata[1],"code"=>$newdata[2],"temp"=>$temp,"date"=>$newdata[4],"location"=>$loc[1]);
}

function get_weather() {
    echo json_encode(get_weather_data());
}

#Export/Import
function export_config() {
    $data = get_from_os("/gp?d=0");

    preg_match("/pd=\[\];(.*);/", $data, $progs);
    $progs = explode(";", $progs[1]);

    $i = 0;
    foreach ($progs as $prog) {
        $tmp = explode("=", $prog);
        $newdata["programs"][$i] = $tmp[1];
        $i++;
    }
    $newdata["options"] = get_options();

    $vs = get_stations();
    $newdata["stations"] = $vs["stations"];
    $newdata["masop"] = $vs["masop"];

    echo json_encode($newdata);
}

function import_config() {
    if (!isset($_REQUEST["data"])) echo 0;
    $data = json_decode($_REQUEST["data"],true);
    if (is_null($data)) echo 0;
    $cs = "/cs?pw="; $co = "/co?pw="; $cp_start = "/cp?pw="; $i = 0;
    foreach ($data["options"] as $key => $value) {
        if (is_array($value)) {
            if (in_array($key, array(16,21,22,25)) && $value["val"] == 0) continue; 
            $co .= "&o".$key."=".$value["val"];
        } else if ($key == "loc") {
            $co .= "&".$key."=".urlencode($value);
        }
    }
    send_to_os($co);
    foreach ($data["stations"] as $station) {
        $cs .= "&s".$i."=".urlencode($station);
        $i++;
    }
    $i = 0;
    foreach ($data["masop"] as $bit) {
        $cs .= "&m".$i."=".urlencode($bit);
        $i++;
    }
    send_to_os($cs);
    send_to_os("/dp?pw=&pid=-1");
    foreach ($data["programs"] as $prog) {
        send_to_os($cp_start."&pid=-1&v=".$prog);
    }
}

#OpenSprinkler functions

#Get station names
function get_stations() {
    $data = get_from_os("/vs");
    preg_match("/snames=\[(.*)\];/", $data, $matches);
    $rawstations = str_getcsv($matches[1],",","'");
    preg_match("/nboards=(\d+)/", $data, $matches);
    $total = $matches[1] * 8; $current = 1;
    foreach ($rawstations as $station) {
        if ($current > $total) break;
        $station = preg_replace("/\\\u([0-9a-eA-E]{4})/", "&#x\\1;", $station);
        $stations[] = $station;
        $current++;
    }

    preg_match("/masop=\[(.*?)\]/", $data, $masop);
    $masop = explode(",",$masop[1]);

    return array("stations" => $stations,"masop" => $masop);
}

#Get program information
function get_programs() {
    $data = get_from_os("/gp?d=0");

    preg_match_all("/(nprogs|nboards|ipas|mnp)=[\w|\d|.\"]+/", $data, $opts);

    foreach ($opts[0] as $variable) {
        if ($variable === "") continue;
        $tmp = str_replace('"','',explode("=", $variable));
        $newdata[$tmp[0]] = $tmp[1];
    }

    preg_match("/pd=\[\];(.*);/", $data, $progs);
    if (empty($progs)) return $progs;
    $progs = explode(";", $progs[1]);

    $i = 0;
    foreach ($progs as $prog) {
        $tmp = explode("=", $prog);
        $tmp2 = str_replace("[", "", $tmp[1]);
        $tmp2 = str_replace("]", "", $tmp2); 
        $program = explode(",", $tmp2);

        #Reset variables
        $days0 = $program[1]; $days1 = $program[2]; $even = false; $odd = false; $interval = false; $days = ""; $stations = "";

        $newdata["programs"][$i]["en"] = $program[0];
        $newdata["programs"][$i]["start"] = $program[3];
        $newdata["programs"][$i]["end"] = $program[4];
        $newdata["programs"][$i]["interval"] = $program[5];
        $newdata["programs"][$i]["duration"] = $program[6];

        for ($n=0; $n < $newdata["nboards"]; $n++) {
            $bits = $program[7+$n];
            for ($s=0; $s < 8; $s++) { 
                $stations .= ($bits&(1<<$s)) ? "1" : "0";
            }
        }
        $newdata["programs"][$i]["stations"] = $stations;

        if(($days0&0x80)&&($days1>1)){
            #This is an interval program
            $days=array($days1,$days0&0x7f);
            $interval = true;
        } else {
            #This is a weekly program 
            for($d=0;$d<7;$d++) {
                if ($days0&(1<<$d)) {
                    $days .= "1";
                } else {
                    $days .= "0";
                }
            }
            if(($days0&0x80)&&($days1==0))  {$even = true;}
            if(($days0&0x80)&&($days1==1))  {$odd = true;}
        }

        $newdata["programs"][$i]["days"] = $days;
        $newdata["programs"][$i]["is_even"] = $even;
        $newdata["programs"][$i]["is_odd"] = $odd;
        $newdata["programs"][$i]["is_interval"] = $interval;
        $i++;
    }
    return $newdata;
}

function get_preview() {
    process_programs($_REQUEST["m"],$_REQUEST["d"],$_REQUEST["y"]);
}

function process_programs($month,$day,$year) {
    $newdata = array();

    $newdata["settings"] = get_settings();
    $vs = get_stations();
    $newdata["stations"] = $vs["stations"];

    $data = get_from_os("/gp?d=".$day."&m=".$month."&y=".$year);
    preg_match_all("/(seq|mas|wl|sdt|mton|mtoff|devday|devmin|dd|mm|yy|nprogs|nboards|ipas|mnp)=[\w|\d|.\"]+/", $data, $opts);

    foreach ($opts[0] as $variable) {
        if ($variable === "") continue;
        $tmp = str_replace('"','',explode("=", $variable));
        $newdata[$tmp[0]] = $tmp[1];
    }

    preg_match("/masop=\[(.*?)\]/", $data, $masop);
    $newdata["masop"] = explode(",",$masop[1]);
{'fraction_non_alphanumeric': 0.24801716291769602, 'fraction_numerical': 0.01729293979976596, 'mean_word_length': 3.3639716312056738, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 32, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4371409', 'n_tokens_mistral': 5823, 'n_tokens_neox': 5648, 'n_words': 922}