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 | $virtualbox->openExistingSession($session, $uuid);
|
---|
56 | try
|
---|
57 | {
|
---|
58 | $console = $session->console;
|
---|
59 | $display = $console->display;
|
---|
60 | $screenWidth = $display->width;
|
---|
61 | $screenHeight = $display->height;
|
---|
62 | $imageraw = $display->takeScreenShotSlow($screenWidth, $screenHeight);
|
---|
63 | $session->close();
|
---|
64 | $filename = './screenshot.png';
|
---|
65 | echo "Saving screenshot of " . $machine->name . " (${screenWidth}x${screenHeight}) to $filename\n";
|
---|
66 | $image = imagecreatetruecolor($screenWidth, $screenHeight);
|
---|
67 |
|
---|
68 | for ($height = 0; $height < $screenHeight; $height++)
|
---|
69 | {
|
---|
70 | for ($width = 0; $width < $screenWidth; $width++)
|
---|
71 | {
|
---|
72 | $start = ($height*$screenWidth + $width)*4;
|
---|
73 | $red = $imageraw[$start];
|
---|
74 | $green = $imageraw[$start+1];
|
---|
75 | $blue = $imageraw[$start+2];
|
---|
76 | //$alpha = $imageraw[$start+3];
|
---|
77 |
|
---|
78 | $colour = imagecolorallocate($image, $red, $green, $blue);
|
---|
79 |
|
---|
80 | imagesetpixel($image, $width, $height, $colour);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | imagepng($image, $filename);
|
---|
85 | }
|
---|
86 | catch (Exception $ex)
|
---|
87 | {
|
---|
88 | // Ensure we close the VM Session if we hit a error, ensure we don't have a aborted VM
|
---|
89 | echo $ex->getMessage();
|
---|
90 | $session->close();
|
---|
91 | }
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | $websessionManager->logoff($virtualbox->handle);
|
---|