2a21a474a93152e7a1637578953dfc3093f374b2vboxsync<?php
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync/*
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * Sample client for the VirtualBox webservice, written in PHP.
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync *
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * Run the VirtualBox web service server first; see the VirtualBox
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * SDK reference for details.
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync *
5c65eaa08f2ec993a19c9bef6e5463918e40e0ebvboxsync * Copyright (C) 2009-2010 Oracle Corporation
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * Contributed by James Lucas (mjlucas at eng.uts.edu.au).
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync *
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * The following license applies to this file only:
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync *
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * Permission is hereby granted, free of charge, to any person
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * obtaining a copy of this software and associated documentation
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * files (the "Software"), to deal in the Software without
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * restriction, including without limitation the rights to use,
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * copy, modify, merge, publish, distribute, sublicense, and/or
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * sell copies of the Software, and to permit persons to whom the
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * Software is furnished to do so, subject to the following conditions:
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync *
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * The above copyright notice and this permission notice shall be
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * included in all copies or substantial portions of the Software.
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync *
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync * OTHER DEALINGS IN THE SOFTWARE.
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync */
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsyncrequire_once('./vboxServiceWrappers.php");
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync//Connect to webservice
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync$connection = new SoapClient("vboxwebService.wsdl", array('location" => "http://localhost:18083/"));
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync//Logon to webservice
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync$websessionManager = new IWebsessionManager($connection);
74cbfe8cd9e0617fc789820f0e81e995df2fd7a0vboxsync// Dummy username and password (change to appropriate values or set authentication method to null)
74cbfe8cd9e0617fc789820f0e81e995df2fd7a0vboxsync$virtualbox = $websessionManager->logon("username","password");
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync//Get a list of registered machines
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync$machines = $virtualbox->machines;
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync//Take a screenshot of the first vm we find that is running
2a21a474a93152e7a1637578953dfc3093f374b2vboxsyncforeach ($machines as $machine)
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync{
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync if ( 'Running" == $machine->state )
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync {
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync $session = $websessionManager->getSessionObject($virtualbox->handle);
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync $uuid = $machine->id;
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync $machine->lockMachine($session->handle, "Shared");
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync try
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync {
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync $console = $session->console;
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync $display = $console->display;
b7307c9c040a1a8aed0dbabed6574466d0bff2e4vboxsync list($screenWidth, $screenHeight, $screenBpp, $screenX, $screenY, $screenStatus) = $display->getScreenResolution(0 /* First screen */);
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync
d01bb88591e2ae070aa233b99c81e58598d557e1vboxsync $imageraw = $display->takeScreenShotToArray(0 /* First screen */, $screenWidth, $screenHeight, "RGBA");
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync echo "Screenshot size: " . sizeof($imageraw) . "\n";
b7307c9c040a1a8aed0dbabed6574466d0bff2e4vboxsync
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync $filename = 'screenshot.png";
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync echo "Saving screenshot of " . $machine->name . " (${screenWidth}x${screenHeight}, ${screenBpp}BPP) to $filename\n";
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync $image = imagecreatetruecolor($screenWidth, $screenHeight);
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync for ($height = 0; $height < $screenHeight; $height++)
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync {
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync for ($width = 0; $width < $screenWidth; $width++)
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync {
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync $start = ($height*$screenWidth + $width)*4;
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync $red = $imageraw[$start];
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync $green = $imageraw[($start+1)];
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync $blue = $imageraw[($start+2)];
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync //$alpha = $image[$start+3];
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync $colour = imagecolorallocate($image, $red, $green, $blue);
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync imagesetpixel($image, $width, $height, $colour);
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync }
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync }
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync imagepng($image, $filename);
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync }
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync catch (Exception $ex)
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync {
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync echo $ex->getMessage();
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync }
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync $session->unlockMachine();
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync $machine->releaseRemote();
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync $session->releaseRemote();
fd50d469f529bf08da58eb189b69a2addbb900cdvboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync break;
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync }
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync}
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync
2a21a474a93152e7a1637578953dfc3093f374b2vboxsync$websessionManager->logoff($virtualbox->handle);
28cc8ba3eb149237f8572fc972f8283cae5aa27evboxsync
28cc8ba3eb149237f8572fc972f8283cae5aa27evboxsync?>
28cc8ba3eb149237f8572fc972f8283cae5aa27evboxsync