diff --git a/commands/TestController.php b/commands/TestController.php index 2909054..5860df5 100644 --- a/commands/TestController.php +++ b/commands/TestController.php @@ -9,7 +9,7 @@ class TestController extends Controller { public function actionIndex() { - $client = new \app\components\ApiClient(Yii::$app->params['apiEndpoint']); + $client = new \app\components\ApiClient(Yii::$app->params['zmUrl'].'/zm/api'); print_r($client->getVersion()); } } diff --git a/config/params.php b/config/params.php index 7c914bf..7196401 100644 --- a/config/params.php +++ b/config/params.php @@ -4,5 +4,9 @@ return [ 'adminEmail' => 'admin@example.com', 'senderEmail' => 'noreply@example.com', 'senderName' => 'Example.com mailer', - 'apiEndpoint' => 'http://192.168.0.5:8800/zm/api', + 'zmUrl' => 'http://192.168.0.5:8800/', + // mapping from our internal id to ZM id + 'cameras' => [ + '1' => 1, + ] ]; diff --git a/controllers/CameraController.php b/controllers/CameraController.php new file mode 100644 index 0000000..15fea96 --- /dev/null +++ b/controllers/CameraController.php @@ -0,0 +1,36 @@ +params['cameras']; + if (!isset($cms[$id])) + { + throw new NotFoundHttpException(); + } + $zm_id = $cms[$id]; + $source_url = Yii::$app->params['zmUrl'].'/cgi-bin/nph-zms?scale=50&mode=jpeg&maxfps=30&buffer=1000&monitor='.$zm_id.'&connkey='.rand(0, 65536); + $fp = fopen($source_url, 'r', false); + if ($fp) + { + Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; + // send mjpeg header: + header("Cache-Control: no-cache"); + header("Cache-Control: private"); + header("Pragma: no-cache"); + header("Content-type: multipart/x-mixed-replace; boundary=boundarydonotcross"); + + // pass data + fpassthru($fp); + fclose($fp); + } + else throw new NotFoundHttpException(); + } +} diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 67c3f50..70cc76d 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -3,40 +3,11 @@ namespace app\controllers; use Yii; -use yii\filters\AccessControl; use yii\web\Controller; -use yii\web\Response; -use yii\filters\VerbFilter; -use app\models\LoginForm; -use app\models\ContactForm; +use yii\helpers\Url; class SiteController extends Controller { - /** - * {@inheritdoc} - */ - public function behaviors() - { - return [ - 'access' => [ - 'class' => AccessControl::class, - 'only' => ['logout'], - 'rules' => [ - [ - 'actions' => ['logout'], - 'allow' => true, - 'roles' => ['@'], - ], - ], - ], - 'verbs' => [ - 'class' => VerbFilter::class, - 'actions' => [ - 'logout' => ['post'], - ], - ], - ]; - } /** * {@inheritdoc} @@ -47,82 +18,19 @@ class SiteController extends Controller 'error' => [ 'class' => 'yii\web\ErrorAction', ], - 'captcha' => [ - 'class' => 'yii\captcha\CaptchaAction', - 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, - ], ]; } - /** - * Displays homepage. - * - * @return string - */ public function actionIndex() { - return $this->render('index'); - } - - /** - * Login action. - * - * @return Response|string - */ - public function actionLogin() - { - if (!Yii::$app->user->isGuest) { - return $this->goHome(); - } - - $model = new LoginForm(); - if ($model->load(Yii::$app->request->post()) && $model->login()) { - return $this->goBack(); - } - - $model->password = ''; - return $this->render('login', [ - 'model' => $model, - ]); - } - - /** - * Logout action. - * - * @return Response - */ - public function actionLogout() - { - Yii::$app->user->logout(); - - return $this->goHome(); - } - - /** - * Displays contact page. - * - * @return Response|string - */ - public function actionContact() - { - $model = new ContactForm(); - if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) { - Yii::$app->session->setFlash('contactFormSubmitted'); - - return $this->refresh(); - } - return $this->render('contact', [ - 'model' => $model, - ]); - } - - /** - * Displays about page. - * - * @return string - */ - public function actionAbout() - { - return $this->render('about'); + $cameras = []; + foreach (Yii::$app->params['cameras'] as $id => $zm_id) + { + $cameras[$id] = Url::to(['camera/stream', 'id' => $id]); + } + return $this->render('index', + [ + 'cameras' => $cameras + ]); } } diff --git a/models/ContactForm.php b/models/ContactForm.php deleted file mode 100644 index f001d21..0000000 --- a/models/ContactForm.php +++ /dev/null @@ -1,65 +0,0 @@ - 'Verification Code', - ]; - } - - /** - * Sends an email to the specified email address using the information collected by this model. - * @param string $email the target email address - * @return bool whether the model passes validation - */ - public function contact($email) - { - if ($this->validate()) { - Yii::$app->mailer->compose() - ->setTo($email) - ->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']]) - ->setReplyTo([$this->email => $this->name]) - ->setSubject($this->subject) - ->setTextBody($this->body) - ->send(); - - return true; - } - return false; - } -} diff --git a/models/LoginForm.php b/models/LoginForm.php deleted file mode 100644 index dce15cc..0000000 --- a/models/LoginForm.php +++ /dev/null @@ -1,81 +0,0 @@ -hasErrors()) { - $user = $this->getUser(); - - if (!$user || !$user->validatePassword($this->password)) { - $this->addError($attribute, 'Incorrect username or password.'); - } - } - } - - /** - * Logs in a user using the provided username and password. - * @return bool whether the user is logged in successfully - */ - public function login() - { - if ($this->validate()) { - return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); - } - return false; - } - - /** - * Finds user by [[username]] - * - * @return User|null - */ - public function getUser() - { - if ($this->_user === false) { - $this->_user = User::findByUsername($this->username); - } - - return $this->_user; - } -} diff --git a/views/layouts/main.php b/views/layouts/main.php index c204435..3b02db4 100644 --- a/views/layouts/main.php +++ b/views/layouts/main.php @@ -39,19 +39,7 @@ $this->registerLinkTag(['rel' => 'icon', 'type' => 'image/x-icon', 'href' => Yii echo Nav::widget([ 'options' => ['class' => 'navbar-nav'], 'items' => [ - ['label' => 'Home', 'url' => ['/site/index']], - ['label' => 'About', 'url' => ['/site/about']], - ['label' => 'Contact', 'url' => ['/site/contact']], - Yii::$app->user->isGuest - ? ['label' => 'Login', 'url' => ['/site/login']] - : '