<?php defined('APPPATH') or die('No direct access allowed.');
/**
 * HSDN SME
 *
 * Site Management Engine
 *
 * @package        ELISA
 * @author        HSDN Team
 * @copyright    Copyright (c) 2009-2010, Information Networks Ltd.
 * @link        http://www.hsdn.org
 * @since        Version 2
 */

// ------------------------------------------------------------------------

/**
 * 
 *
 * @author        HSDN Team
 */
class Screenshot_model
{
    public 
$wait_time 15;

    public 
$resolution '1024x768';

    public 
$bitrate 24;

    public 
$temp_path 'temp';

    private 
$xvfb_path '/usr/local/bin/Xvfb';

    private 
$opera_path '/usr/local/bin/opera';

    private 
$xwd_path '/usr/local/bin/xwd';

    private 
$xwdtopnm_path '/usr/local/bin/xwdtopnm';

    private 
$pnmtojpeg_path '/usr/local/bin/pnmtojpeg';

    private 
$sockstat_path '/usr/bin/sockstat';

    private 
$pgrep_path '/bin/pgrep';

    private 
$killall_path '/usr/bin/killall';

    private 
$share_path '';

    public function 
__construct()
    {
        
$this->share_path SME::config('share_path''catalog');
    }

    public function 
initialize()
    {
        
$this->kill_process('Xvfb');
        
$this->xvfb_start();
    }

    public function 
get($url)
    {
        
$this->kill_process('opera');

        if (
$this->remove_session()
            AND 
$this->opera_start($url)
            AND (
$export $this->export())
            AND 
$this->kill_process('opera')
            )
        {
            return 
$export;
        }

        return 
FALSE;
    }

    public function 
xvfb_start()
    {
        if (!
$this->socket_status('Xvfb'))
        {
            return 
TRUE;
        }

        if (
$this->exec_command($this->xvfb_path.' :2 -screen 0 '.$this->resolution.'x'.$this->bitrate.' > /dev/null &') === TRUE)
        {
            
sleep(3);

            return 
TRUE;
        }

        return 
FALSE;
    }

    public function 
xvfb_stop()
    {
        if (
$this->socket_status('Xvfb'))
        {
            
$this->kill_process('Xvfb');
        }
    }

    private function 
opera_start($url)
    {
        if (
$this->exec_command($this->opera_path
            
.' -display :2.0 -personaldir '
            
.$this->share_path.DIRECTORY_SEPARATOR
            
.'.opera -nomail -nosession -fullscreen -geometry '.$this->resolution.'+0+0 '.$url.' > /dev/null &') === TRUE)
        {
            
sleep($this->wait_time);

            return 
TRUE;
        }

        return 
FALSE;
    }

    private function 
export()
    {
        
$jpeg $this->exec_command($this->xwd_path.' -display :2.0 -root -silent | '.$this->xwdtopnm_path.' | '.$this->pnmtojpeg_path.' -quality=100 -optimize');

        if (
$jpeg != '' AND (@imagecreatefromstring($jpeg) !== FALSE))
        {
            return 
$jpeg;
        }

        return 
FALSE;
    }

    private function 
remove_session()
    {
        @
rmdir($this->share_path.DIRECTORY_SEPARATOR.'.opera');
        @
mkdir($this->share_path.DIRECTORY_SEPARATOR.'.opera');

        if (@
copy($this->share_path
                    
.DIRECTORY_SEPARATOR.'operaprefs.ini'
                
$this->share_path
                    
.DIRECTORY_SEPARATOR.'.opera'
                    
.DIRECTORY_SEPARATOR.'operaprefs.ini')
            )
        {
            return 
TRUE;
        }

        return 
FALSE;
    }

    private function 
kill_process($process)
    {
        if ((
$return $this->exec_command($this->killall_path.' -9 '.$process) != ''
            AND !
preg_match('/^No matching/is'$return))
        {
            return 
TRUE;
        }

        return 
FALSE;
    }

    private function 
socket_status($process)
    {
        if (
$this->exec_command($this->sockstat_path.' | '.$this->pgrep_path.' '.$process) == '')
        {
            return 
FALSE;
        }

        return 
TRUE;
    }
    
    private function 
exec_command($command)
    {
        if ((
$return = @shell_exec('nohup '.$command)) == '')
        {
            return 
TRUE;
        }

        return 
$return;
    }
}

/* End of file */