Skip to content

Instantly share code, notes, and snippets.

@tvaliasek
Last active August 29, 2015 14:14
Show Gist options
  • Save tvaliasek/c92bf6852c95fb847480 to your computer and use it in GitHub Desktop.
Save tvaliasek/c92bf6852c95fb847480 to your computer and use it in GitHub Desktop.
nette2 - phpseclib handle
<?php
namespace App\AdminModule\Presenters;
use Nette,
App\Model,
Nette\Application\Responses,
Net_SSH2, //pear phpseclib 0.3.9. composer: phpseclib/phpseclib
Net_SCP;
/**
* HomepageSettings presenter.
*/
class HmpsetPresenter extends BasePresenter {
const WEBCAM_IP = '127.0.0.1'; //target ip or url
const WEBCAM1_PORT = 2201;
const WEBCAM2_PORT = 2202;
const WEBCAM_UNAME = 'uname';
const WEBCAM_PWD = 'pwd';
public function renderWebcam() {
$this->template->webcam_ip = self::WEBCAM_IP;
$this->template->webcam1_port = self::WEBCAM1_PORT;
$this->template->webcam2_port = self::WEBCAM2_PORT;
$webcamPath = __DIR__.'/../../../www/uploads/content/webcam/';
$this->template->fEW1 = file_exists($webcamPath.'webcam1_pub.jpg');
if($this->template->fEW1){
$this->template->fTime1 = filemtime($webcamPath.'webcam1_pub.jpg');
}
$this->template->fEW2 = file_exists($webcamPath.'webcam2_pub.jpg');
if($this->template->fEW2){
$this->template->fTime2 = filemtime($webcamPath.'webcam2_pub.jpg');
}
}
public function handleRefreshWebcam($webcamId = 1) {
$ajax = $this->isAjax();
$port = ($webcamId == 1) ? self::WEBCAM1_PORT : self::WEBCAM2_PORT;
$ssh2 = new \Net_SSH2(self::WEBCAM_IP, $port, 6);
if (!$ssh2->login(self::WEBCAM_UNAME, self::WEBCAM_PWD)) {
if ($ajax) {
$this->sendResponse(new Responses\JsonResponse(array('login' => 'fail')));
} else {
$this->flashMessage('Nepodařilo se přihlásit k SSH.', 'error');
}
} else {
$scp = new \Net_SCP($ssh2);
if ($scp->get('/tmp/snap.jpeg', __DIR__ . '/../../../www/uploads/content/webcam/' . $webcamId . '.jpg')) {
$image = new Nette\Utils\Image(imagecreatefromjpeg(__DIR__ . '/../../../www/uploads/content/webcam/' . $webcamId . '.jpg'));
$image->resize(640, 480, Nette\Utils\Image::EXACT);
$image->save(__DIR__ . '/../../../www/uploads/content/webcam/webcam' . $webcamId . '_pub.jpg', 60);
unset($image);
chmod(__DIR__ . '/../../../www/uploads/content/webcam/webcam' . $webcamId . '_pub.jpg', 0754);
}
$ssh2->disconnect();
}
if ($ajax) {
$this->redrawControl('snpWebcam' . $webcamId);
} else {
$this->redirect('Hmpset:webcam');
}
}
}
{block slogan}Test webkamer{/block}
{block content}
<div class="container">
<div class="row">
<div class="col-sm-6 col-xs-12">
<div class="panel panel-primary">
<div class="panel-heading">
<span class="panel-title">
Webkamera 1: {$webcam_ip}:{$webcam1_port}
</span>
<a n:href="refreshWebcam! 1" class="btn btn-xs btn-success pull-right ajax"><i class="fa fa-1x fa-refresh"></i></a>
</div>
<div class="panel-body" n:snippet="snpWebcam1">
<img n:if="$fEW1" src="/uploads/content/webcam/webcam1_pub.jpg?cacheBreaker=#" alt="webcam1" class="img img-responsive center-block v-offset-bottom-1 img-no-cache" />
<p n:ifset="$fTime1" class="text-center">Poslední update: {$fTime1|date:'j.n.Y G:i:s'}</p>
</div>
</div>
</div>
<div class="col-sm-6 col-xs-12">
<div class="panel panel-primary">
<div class="panel-heading">
<span class="panel-title">
Webkamera 2: {$webcam_ip}:{$webcam2_port}
</span>
<a n:href="refreshWebcam! 2" class="btn btn-xs btn-success pull-right ajax"><i class="fa fa-1x fa-refresh"></i></a></div>
<div class="panel-body" n:snippet="snpWebcam2">
<img n:if="$fEW2" src="/uploads/content/webcam/webcam2_pub.jpg?cacheBreaker=#" alt="webcam2" class="img img-responsive center-block v-offset-bottom-1 img-no-cache" />
<p n:ifset="$fTime2" class="text-center">Poslední update: {$fTime2|date:'j.n.Y G:i:s'}</p>
</div>
</div>
</div>
</div>
</div>
{/block}
{block scripts}
{include parent}
<script type="text/javascript">
jQuery(function(){
$(document).ajaxComplete(function(event, xhr, settings){
var XHRurl = settings.url;
if(XHRurl.indexOf('do=refreshWebcam')>-1){
var $els = $('img.img-no-cache');
if($els.length > 0){
var $timestampParam = '?cacheBreaker=' + new Date().getTime();
$els.each(function(){
var $origSrc = $(this).attr('src');
$(this).attr('src', $origSrc.replace(/(\?cacheBreaker=).*/,$timestampParam));
});
}
}
});
});
</script>
{/block}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment