/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

    function PlaceJS() {
	global $action;
	global $postfix;
?>
<script type="text/javascript">
//<![CDATA[
    function Confirm() {
	window.location = "index.php?page=do.php&confirm&action=<?echo $action;?>&postfix=<?echo urlencode(json_encode($postfix));?>";
    }
//]]>
</script>
<?
    }

    function RequestConfirmation($msg, $list) {
	echo "<b>$msg</b>: ";
	echo "<div class=\"list\">";
	foreach ($list as $postfix) {
	    echo "cache*$postfix<br/>";
	}
	echo "</div>";
	echo "<br/>";
	echo "<button onclick=\"Confirm();\">" . translate("Confirm") . "</button>";
    }


    function GetAdminFile($prefix = false) {
	global $TMP_PATH;
    
	$dir = "adminscripts/";

	if (!is_dir($TMP_PATH . "/" .  $dir)) {
	    if (!@mkdir($TMP_PATH . "/" . $dir, 0755, true)) 
		throw new ADEIException(translate("Access to the temporary directory is denied"));
	}

	$fn = tempnam($TMP_PATH . "/" .  $dir, $prefix);

	if ($fn) unlink($fn);

	return $fn . ".php";
    }

    $action = $_REQUEST['action'];
    $params = split("__", $action, 2);
    $action_real = $params[0];
    $action_param = $params[1];
    $confirm = isset($_REQUEST['confirm']);

    echo "<br/><br/>";

    if (isset($_GET['postfix']))
	$postfix = json_decode(stripslashes($_GET['postfix']), true);
    else {
	$postfix = array();
	foreach ($_POST as $key => $value) {
	    if (($value == 1)&&(preg_match("/^postfix(.*)$/", $key, $m))) {
		array_push($postfix, $m[1]);
	    }
	}
    }
    
    if (!$postfix) {
	?><span class="error"><?echo translate("Error: The list of items is not supplied");?></span><?
	exit;
    }
    
    if (!is_array($postfix)) $postfix = array($postfix);

    switch ($action_real) {
	case "drop":
	    $script_name="drop";
	    $cmessage = translate("Do you really want to drop following tables");
	    break;
	case "drop_last":
	    $script_name="droplast";
	    $cmessage = translate("Do you really want to drop last %s seconds of data from the following tables", $params[1]);
	    break;
	case "rewidth":
	    $script_name="rewidth";
	    $cmessage = translate("Do you really want to resize following tables");
	    break;
        default:
	?><span class="error"><?
	    if ($action_real)
		echo translate("Error: Invalid action \"%s\" is specified", $action_real);
	    else
		echo translate("Error: Action is not specified");
	?></span><?
	    exit;
    }

    if ($confirm) {
	$name = GetAdminFile($script_name . "-");
	$f = fopen($name, "w");
	if (!$f)
	    throw new ADEIException(translate('Error creating temporary file: %s', $name));
		    
        try {
            fwrite ($f, '<?php
                $ADEI_SETUP = "' . $ADEI_SETUP . '";
	        $ADEI_ROOTDIR = "' . dirname($_SERVER['SCRIPT_FILENAME']) . '/../";
	        require("$ADEI_ROOTDIR/adei.php");
	        try {
		    $cache = new CACHEDB();
            ');

            switch ($action_real) {
                case "drop":
                    foreach ($postfix as $p) {
                        fwrite($f, '$cache->Drop("' . $p . '");' . "\n");
                    }
                    break;
                case "drop_last":
	            $duration = $action_param;
	            if ((!$duration)||($duration > 31536000))
		        throw new ADEIException(translate("The specified duration (%u) is not valid", $duration));
	            $drop_from = gettimeofday(true);
	            $drop_from = floor($drop_from / $duration) * $duration;
                
                    foreach ($postfix as $p) {
                        fwrite($f, '$cache->Drop("' . $p . '",' . $drop_from . ');' . "\n");
                    }
                    break;
	        case "rewidth":
		    foreach ($postfix as $p) {
			fwrite($f, '$cache->Rewidth("' . $p . '", true);' . "\n");
		    }
                    break;
            }

            fwrite($f, '
		} catch (ADEIException $ae) {
		    echo translate("Error: %s", $ae->getInfo()) . "\n";
		    exit(1);
		}
		echo translate("done...") . "\n";?>'
            );
	    fclose($f);
		    
            echo "The job is scheduled and will be executed in a next maintenance period. To run it faster, please, login to the server and run: ";
            if ($SETUP) {
		echo "php $name -setup $SETUP";
            } else {
		echo "php $name";
            }
	} catch (ADEIException $ae) {
            fclose($f);
            unlink($name);
	    ?><span class="error"><?echo translate("Error: %s", $ae->getInfo());?></span><?
	}
    } else {
        PlaceJS();
	RequestConfirmation($cmessage, $postfix);
    }            
?>