Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
lib/api: intelligent Photobooth URL detection
Browse files Browse the repository at this point in the history
Change-Id: I87cb2d86e69d47d2edb9cd29e9d8ab116814cd7c
  • Loading branch information
andi34 committed Sep 29, 2021
1 parent 276e477 commit d4dc7ec
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
14 changes: 5 additions & 9 deletions api/qrcode.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<?php
require_once '../lib/config.php';

if (!isset($config['webserver']['ip'])) {
$SERVER_IP = $_SERVER['HTTP_HOST'];
} else {
$SERVER_IP = $config['webserver']['ip'];
}
require_once '../lib/helper.php';

$filename = $_GET['filename'];
$api_path = getrootpath('../api');
$photoboothUrl = getPhotoboothUrl();

include '../vendor/phpqrcode/lib/full/qrlib.php';
$url = 'http://' . $SERVER_IP . $api_path . '/download.php?image=';
QRcode::png($url . $filename, false, QR_ECLEVEL_H, 10);
$url = $photoboothUrl . '/api/download.php?image=' . $filename;
QRcode::png($url, false, QR_ECLEVEL_H, 10);
2 changes: 1 addition & 1 deletion config/config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
$config['dev']['error_messages'] = true;
$config['dev']['reload_on_error'] = true;
$config['qr']['enabled'] = true;
$config['webserver']['ip'] = '127.0.0.1';
$config['webserver']['ip'] = NULL;
$config['webserver']['ssid'] = 'Photobooth';
$config['download']['enabled'] = true;
$config['download']['thumbs'] = false;
Expand Down
16 changes: 10 additions & 6 deletions lib/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
require_once __DIR__ . '/arrayDeepMerge.php';
require_once __DIR__ . '/helper.php';

$default_config_file = __DIR__ . '/../config/config.inc.php';
$my_config_file = __DIR__ . '/../config/my.config.inc.php';
Expand Down Expand Up @@ -157,6 +158,15 @@
$config['background']['chroma'] = 'url(' . getrootpath('../resources/img/bg_bluegray.jpg') . ')';
}

if (!isset($config['webserver']['ip'])) {
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$protocol = 'https://';
} else {
$protocol = 'http://';
}
$config['webserver']['ip'] = str_replace($protocol, '', getPhotoboothUrl());
}

if (file_exists($my_config_file) && !is_writable($my_config_file)) {
die('Abort. Can not write config/my.config.inc.php.');
} elseif (!file_exists($my_config_file) && !is_writable(__DIR__ . '/../config/')) {
Expand Down Expand Up @@ -186,9 +196,3 @@
}

$config['folders']['lang'] = getrootpath('../resources/lang');

function getrootpath($relative_path) {
$realpath = realpath($relative_path);
$rootpath = str_replace($_SERVER['DOCUMENT_ROOT'], '', $realpath);
return $rootpath;
}
44 changes: 44 additions & 0 deletions lib/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

function getrootpath($relative_path) {
$realpath = realpath($relative_path);
$rootpath = str_replace($_SERVER['DOCUMENT_ROOT'], '', $realpath);

return $rootpath;
}

function getPhotoboothUrl() {
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$protocol = 'https';
} else {
$protocol = 'http';
}

$ip = getPhotoboothIp();
$path = getrootpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
if ($path == $_SERVER['DOCUMENT_ROOT']) {
$url = $protocol . '://' . $ip;
} else {
$folder = str_replace($_SERVER['DOCUMENT_ROOT'], '', $path);
$url = $protocol . '://' . $ip . $folder;
}

return $url;
}

function getPhotoboothIp() {
$os = DIRECTORY_SEPARATOR == '\\' || strtolower(substr(PHP_OS, 0, 3)) === 'win' ? 'windows' : 'linux';
if ($os == 'linux') {
$get_ip = shell_exec('hostname -I | cut -d " " -f 1');

if (!$get_ip) {
$ip = $_SERVER['HTTP_HOST'];
} else {
$ip = $get_ip;
}
} else {
$ip = $_SERVER['HTTP_HOST'];
}

return $ip;
}
2 changes: 1 addition & 1 deletion resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
"manual:general:start_screen_title": "Enter the title visible on startpage.",
"manual:general:start_screen_title_visible": "If enabled, entered title is visible on start screen.",
"manual:general:ui_language": "Choose interface language.<p>If you're missing a language or like to help improving translations visit the <a href=\"https://github.com/andi34/photobooth/wiki/FAQ#how-to-update-or-add-translations\" target=\"_blank\">\"How to update or add translations?\"</a> inside the Photobooth Wiki for instructions.</p>",
"manual:general:webserver_ip": "Please define the IP address of the Photobooth web server to make the QR-Code working if you're accessing Photobooth via \"localhost\", \"127.0.0.1\" or if you have Photobooth installed inside a subfolder. <p>Example if Photobooth can be accessed directly: <code>192.168.0.50</code>.</p><p>Example if Photobooth is installed inside a subfolder: <code>192.168.0.50/photobooth</code>.</p>",
"manual:general:webserver_ip": "Please define the IP address of the Photobooth web server if you're accessing Photobooth via \"localhost\", \"127.0.0.1\" or if you have Photobooth installed inside a subfolder. <p>Example if Photobooth can be accessed directly: <code>192.168.0.50</code>.</p><p>Example if Photobooth is installed inside a subfolder: <code>192.168.0.50/photobooth</code>.</p>",
"manual:general:webserver_ssid": "Please define the wireless network name (SSID) to be used to access the Photobooth. The wireless network name (SSID) is displayed on the results page when the QR code is called up.",
"manual:get_request:get_request_collage": "Define GET request at countdown while taking a collage.",
"manual:get_request:get_request_countdown": "If enabled, a GET request will be made to defined Server at countdown. GET request needs to be defined for picture and collage.",
Expand Down
28 changes: 7 additions & 21 deletions welcome.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
<?php
require_once 'lib/config.php';
require_once 'lib/helper.php';

if (!is_file('.skip_welcome')) {
touch('.skip_welcome');
}

if ($os == 'linux') {
$get_ip = shell_exec('hostname -I | cut -d " " -f 1');

if (!$get_ip) {
$IP = $_SERVER['HTTP_HOST'];
} else {
$IP = $get_ip;
}

if (getcwd() == '/var/www/html/photobooth') {
$URL = $IP . '/photobooth';
} else {
$URL = $IP;
}
} else {
$URL = $_SERVER['HTTP_HOST'];
}

$URL = getPhotoboothUrl();
$PHOTOBOOTH_HOME = getcwd();


?>
<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -75,10 +61,10 @@
<p><b>Databases are placed at:</b> <?=$config['foldersAbs']['data']?></p>
<p><b>Add your own files (e.g. background images, frames, overrides.css) inside:</b> <?=$PHOTOBOOTH_HOME . "/private"?><br>
<i>All files and folders inside this path will be ignored on git and won't cause trouble while updating Photobooth.</i></p>
<p>You can change the settings and look of Photobooth using the Admin panel at <a href="admin" target="_blank" rel="noopener noreferrer">http://<?=$URL;?>/admin</a>.<br>
A standalone gallery can be found at <a href="gallery.php" target="_blank" rel="noopener noreferrer">http://<?=$URL;?>/gallery.php</a>.<br>
A standalone slideshow can be found at <a href="slideshow" target="_blank" rel="noopener noreferrer">http://<?=$URL;?>/slideshow</a>.<br>
An integrated FAQ to answer a lot of questions can be found at <a href="faq" target="_blank" rel="noopener noreferrer">http://<?=$URL;?>/faq</a>.</p>
<p>You can change the settings and look of Photobooth using the Admin panel at <a href="admin" target="_blank" rel="noopener noreferrer"><?=$URL;?>/admin</a>.<br>
A standalone gallery can be found at <a href="gallery.php" target="_blank" rel="noopener noreferrer"><?=$URL;?>/gallery.php</a>.<br>
A standalone slideshow can be found at <a href="slideshow" target="_blank" rel="noopener noreferrer"><?=$URL;?>/slideshow</a>.<br>
An integrated FAQ to answer a lot of questions can be found at <a href="faq" target="_blank" rel="noopener noreferrer"><?=$URL;?>/faq</a>.</p>
<p></p>
<p>You are missing some translation or your language isn't supported yet? Don't worry! You can request new language support at <a href="https://github.com/andi34/photobooth/issues" target="_blank" rel="noopener noreferrer">GitHub</a>,
you can translate Photobooth at <a href="https://crowdin.com/project/photobooth" target="_blank" rel="noopener noreferrer">Crowdin</a>.</p>
Expand Down

0 comments on commit d4dc7ec

Please sign in to comment.