1 | <?php
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Sample client for the VirtualBox webservice, written in PHP.
|
---|
5 | *
|
---|
6 | * Run the VirtualBox web service server first; see the VirtualBox
|
---|
7 | * SDK reference for details.
|
---|
8 | *
|
---|
9 | * Copyright (C) 2009-2010 Oracle Corporation
|
---|
10 | * Contributed by James Lucas (mjlucas at eng.uts.edu.au).
|
---|
11 | *
|
---|
12 | * The following license applies to this file only:
|
---|
13 | *
|
---|
14 | * Permission is hereby granted, free of charge, to any person
|
---|
15 | * obtaining a copy of this software and associated documentation
|
---|
16 | * files (the "Software"), to deal in the Software without
|
---|
17 | * restriction, including without limitation the rights to use,
|
---|
18 | * copy, modify, merge, publish, distribute, sublicense, and/or
|
---|
19 | * sell copies of the Software, and to permit persons to whom the
|
---|
20 | * Software is furnished to do so, subject to the following conditions:
|
---|
21 | *
|
---|
22 | * The above copyright notice and this permission notice shall be
|
---|
23 | * included in all copies or substantial portions of the Software.
|
---|
24 | *
|
---|
25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
27 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
28 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
29 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
30 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
31 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
32 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
33 | */
|
---|
34 |
|
---|
35 | require_once('./vboxServiceWrappers.php');
|
---|
36 |
|
---|
37 | //Connect to webservice
|
---|
38 | $connection = new SoapClient("vboxwebService.wsdl", array('location' => "http://localhost:18083/"));
|
---|
39 |
|
---|
40 | //Logon to webservice
|
---|
41 | $websessionManager = new IWebsessionManager($connection);
|
---|
42 | // Dummy username and password (change to appropriate values or set authentication method to null)
|
---|
43 | $virtualbox = $websessionManager->logon("username","password");
|
---|
44 |
|
---|
45 | //Get a list of registered machines
|
---|
46 | $machines = $virtualbox->machines;
|
---|
47 |
|
---|
48 | //Take a screenshot of the first vm we find that is running
|
---|
49 | foreach ($machines as $machine)
|
---|
50 | {
|
---|
51 | if ( 'Running' == $machine->state )
|
---|
52 | {
|
---|
53 | $session = $websessionManager->getSessionObject($virtualbox->handle);
|
---|
54 | $uuid = $machine->id;
|
---|
55 | $machine->lockMachine($session->handle, "Shared");
|
---|
56 | try
|
---|
57 | {
|
---|
58 | $console = $session->console;
|
---|
59 | $display = $console->display;
|
---|
60 | list($screenWidth, $screenHeight, $screenBpp, $screenX, $screenY, $screenStatus) = $display->getScreenResolution(0 /* First screen */);
|
---|
61 |
|
---|
62 | $imageraw = $display->takeScreenShotToArray(0 /* First screen */, $screenWidth, $screenHeight, "RGBA");
|
---|
63 | echo "Screenshot size: " . sizeof($imageraw) . "\n";
|
---|
64 |
|
---|
65 | $filename = 'screenshot.png';
|
---|
66 | echo "Saving screenshot of " . $machine->name . " (${screenWidth}x${screenHeight}, ${screenBpp}BPP) to $filename\n";
|
---|
67 | $image = imagecreatetruecolor($screenWidth, $screenHeight);
|
---|
68 |
|
---|
69 | for ($height = 0; $height < $screenHeight; $height++)
|
---|
70 | {
|
---|
71 | for ($width = 0; $width < $screenWidth; $width++)
|
---|
72 | {
|
---|
73 | $start = ($height*$screenWidth + $width)*4;
|
---|
74 | $red = $imageraw[$start];
|
---|
75 | $green = $imageraw[($start+1)];
|
---|
76 | $blue = $imageraw[($start+2)];
|
---|
77 | //$alpha = $image[$start+3];
|
---|
78 |
|
---|
79 | $colour = imagecolorallocate($image, $red, $green, $blue);
|
---|
80 |
|
---|
81 | imagesetpixel($image, $width, $height, $colour);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | imagepng($image, $filename);
|
---|
86 | }
|
---|
87 | catch (Exception $ex)
|
---|
88 | {
|
---|
89 | echo $ex->getMessage();
|
---|
90 | }
|
---|
91 |
|
---|
92 | $session->unlockMachine();
|
---|
93 |
|
---|
94 | $machine->releaseRemote();
|
---|
95 | $session->releaseRemote();
|
---|
96 |
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | $websessionManager->logoff($virtualbox->handle);
|
---|
102 |
|
---|
103 | ?>
|
---|
104 |
|
---|