':2' ); protected $_xvfbPid = null; protected $_mozillaPid = null; protected $_star = null; protected $_sleep = 15; protected $_shotPerChild = 10; protected $_shotCount = 0; protected $_tmpDir = '/tmp'; public function setTmpDir( $tmp = '/tmp' ) { if( !is_dir( $tmp ) || !is_writable( $tmp ) ) { throw new Exception( $tmp . ' is no directory or not writable!' ); } $this->_tmpDir = $tmp; return true; } protected function _init() { WB::loadClass( 'WBShootingStar' ); $this->_star = new WBShootingStar(); $this->_startX(); $this->_startMozilla(); return true; } protected function _startX() { $pid = pcntl_fork(); if( $pid == -1 ) { WBLog::debug( 'Failed to fork' ); return false; } if( !$pid ) { // to child stuff here $args = array( $this->_env['DISPLAY'], '-screen', '0', '1024x1024x24' ); pcntl_exec( '/usr/bin/Xvfb', $args ); WBLog::debug( 'Failed to start Xvfb' ); } // remember chid's pid $this->_xvfbPid = $pid; WBLog::debug( 'Xvfb PID ' . $pid ); // wait for the X-Server sleep( 2 ); return true; } protected function _startMozilla() { // start browser $pid = pcntl_fork(); if( $pid == -1 ) { WBLog::debug( 'Failed to fork' ); return false; } // to child stuff if( !$pid ) { $args = array( 'about:' ); pcntl_exec( '/usr/bin/firefox', $args, $this->_env ); WBLog::debug( 'Failed to start browser' ); } // remember mozilla $this->_mozillaPid = $pid; WBLog::debug( 'Firefox PID ' . $pid ); // wait for thw browser sleep( 2 ); return true; } protected function _run() { $ids = $this->_star->getQueue(); foreach( $ids as $id ) { WBLog::debug( 'Extracted from queue ' . $id ); $data = $this->_star->get( $id ); $file = $this->_snapshot( $data['url'] ); $this->_star->addScreenShot( $id, $file ); $this->_star->unQueue( $id ); ++$this->_shotCount; if( ( $this->_shotCount % $this->_shotPerChild ) == 0 ) { $this->_stopMozilla(); $this->_startMozilla(); } } sleep( $this->_sleep ); return true; } protected function _snapshot( $url ) { WBLog::debug( 'Snapshot ' . $url ); // surf to URL exec( 'DISPLAY='. $this->_env['DISPLAY'] .' /usr/bin/firefox -a firefox -remote "openurl('.$url.')"' ); sleep( ( $this->_sleep * 3 ) ); // take screenshot $file = tempnam( $this->_tmpDir, 'shot' ); exec( 'xwd -display '. $this->_env['DISPLAY'] .' -root -silent >' . $file . '.xwd' ); exec( 'mogrify -format jpg ' . $file . '.xwd' ); // go back exec( 'DISPLAY='. $this->_env['DISPLAY'] .' /usr/bin/firefox -a firefox -remote "openurl(about:)"' ); // remove temporary files unlink( $file ); unlink( $file . '.xwd' ); WBLog::debug( 'Snapshot done' . $file . ' ' . filesize( $file . '.jpg' ) ); return $file . '.jpg'; } protected function hangUp() { $this->_stopMozilla(); $this->_stopX(); $this->_startX(); $this->_startMozilla(); return true; } protected function _stop() { $this->_stopMozilla(); $this->_stopX(); return true; } protected function _stopMozilla() { // first kill X application if( !$this->_mozillaPid ) { return false; } WBLog::debug( 'Kill Firefox' ); posix_kill( $this->_mozillaPid, SIGTERM ); $status = null; pcntl_waitpid( $this->_mozillaPid, $status ); $this->_mozillaPid = null; return true; } protected function _stopX() { // kill the X server if( !$this->_xvfbPid ) { return false; } WBLog::debug( 'Kill Xvfb' ); posix_kill( $this->_xvfbPid, SIGTERM ); $status = null; pcntl_waitpid( $this->_xvfbPid, $status ); $this->_xvfbPid = null; return true; } } ?>