Moved the console output files to a new directory called tmp. Moved the lock files...
[matrix-gui-v2:matrix-gui-v2.git] / run_script.php
1 <?php
2
3 $i = 0;
4 $lock_string = "";
5 $currently_locked = false;
6
7
8 $handle = fopen("json.txt", "rb");
9 $contents = fread($handle,filesize("json.txt"));
10 fclose($handle);
11
12
13 $var = json_decode($contents,true);
14
15
16 if(isset($_GET["submenu"]))
17         $submenu = $_GET["submenu"];
18 else
19         $submenu = "top";
20
21
22 for($i = 0;$i<count($var[$submenu]["apps"]);$i++)
23 {
24         if($var[$submenu]["apps"][$i]["Name"]==$_GET["app"])
25         {       
26                 
27                 $found_app = $var[$submenu]["apps"][$i];
28                 break;
29                 
30                 
31         }
32
33 }
34
35 $lock_list = $found_app["Lock"];
36
37 //Verify that there is a lock specified for this application
38 if($lock_list != -1)
39 {
40         $lock_list_array = explode($lock_list," ");
41         //Check if the lock list only has one lock. If so add to the array
42         //Since index 0 will be empty
43
44         if(count($lock_list_array) == 1)
45                 $lock_list_array[0] = $lock_list;
46
47         for($x = 0;$x<count($lock_list_array);$x++)
48         {
49                 if(file_exists  ("lock/".$lock_list_array[$x])==true)
50                 {
51                         $currently_locked = true;
52                         break;
53                 }
54
55         }
56 }
57
58 if($currently_locked==false)
59 {
60         $script_link = $found_app["Exec"];
61
62         $random_string = strval(rand());
63         $random_string .= strval(rand());
64
65         $script_command = "./test.sh \"".$script_link. "\" ".$random_string.".txt ".$lock_list;
66
67         $last_line = system($script_command." > /dev/null 2>/dev/null & ", $retval);
68 }
69
70 $enable_exit_link = true;
71
72 ?>
73
74
75
76
77         <?php include "menubar.php"; ?>
78         <?php if($currently_locked==false){ ?>
79
80
81
82
83                 <div id="container"></div>
84   
85
86   
87
88         
89
90
91 <script>
92
93         <?php echo "var uri_link = \"$random_string\";"; ?>
94         $('.exit_link').hide();
95         $('.back_link').hide();
96         
97         var fail_count = 0;
98         
99         function update()
100         {
101                 
102                 //This is a fix for IE 8. IE 8 likes to cache Ajax results therefore you need to change the link
103                 //to something different each time so that IE 8 doesn't cache the results
104                 var uri = "tmp/"+uri_link+".txt?rand="+Math.round((Math.random()*2356))+Math.round((Math.random()*4321))+Math.round((Math.random()*3961));
105                 
106                 $.get(uri, function(data) 
107                 {
108                         fail_count = 0;
109                         data = jQuery.trim(data);
110                         data = data.replace(/\n/g, '<br>');
111                         var script_complete = data.indexOf("_?!!MATRIX_SCRIPT_COMPLETED!!?_");
112                         if(script_complete != -1)
113                                 data = data.replace("_?!!MATRIX_SCRIPT_COMPLETED!!?_", "Script Complete");
114
115                         <?php if($found_app["ProgramType"]!="gui"){ ?>
116                                 $('#container').html(data);
117                                 $('#container').scrollTop(document.getElementById("container").scrollHeight);           
118                         <?php } ?>
119
120                         if(script_complete != -1)
121                         {
122                                 $('.exit_link').show();
123                                 $('.back_link').show();
124                                 <?php if($found_app["ProgramType"]=="gui"){ ?>
125                                         $('.back_link').click();                
126                                 <?php } ?>
127                         }
128                         else
129                                 setTimeout("update()",3000);
130
131                 })
132                 .error(function() 
133                 { 
134                         //Sometimes the output file isn't written fast enough to be able to be
135                         //read back in to check the status. This function will attempt to read the
136                         //file 3 times with an increase delay for each attempt. If it still hasn't 
137                         //Been able to read the file then it displays an error and allows the user to
138                         //get back out to the main menu
139                         fail_count++;
140                         if(fail_count==3)
141                         {
142                                 $('#container').html("Failed to read output file");
143                                 $('.exit_link').show();
144                                 $('.back_link').show();
145                         }
146                         else
147                         {
148                                 setTimeout("update()",fail_count*1500);
149                         }
150                 });
151
152                 
153         }
154
155         setTimeout("update()",500);
156
157
158         </script>
159         <?php }else{?>
160                 This program can't run since a program is already running that contains a lock that this program is trying to use
161         <?php } ?>
162
163