', $items[0]);
// Add Buttons
$buttons = '';
if (count($items) > 1) {
$buttons = '
Previous
Next';
}
return '
'.$header.'
'.implode('',$items).'
'.$buttons.'
'.($script?'':'');
}
//Recent Added
function outputRecentAdded($header, $items, $script = false, $array) {
$hideMenu = '
';
// If None Populate Empty Item
if (!count($items)) {
return '
';
}else{
return '
'.($script?'':'');
}
}
// Create Carousel
function outputPlexNowPlaying($header, $size, $type, $items, $script = false) {
// If None Populate Empty Item
if (!count($items)) {
return '
'.($script?'':'');
}else{
return '
'.$header.'
'.implode('',$items).''.($script?'':'');
}
}
// Get Now Playing Streams From Emby
function getEmbyStreams($size) {
$address = qualifyURL(EMBYURL);
$api = json_decode(@file_get_contents($address.'/Sessions?api_key='.EMBYTOKEN),true);
if (!is_array($api)) { return 'Could not load!'; }
$playingItems = array();
foreach($api as $key => $value) {
if (isset($value['NowPlayingItem'])) {
$playingItems[] = resolveEmbyItem($address, EMBYTOKEN, $value['NowPlayingItem']);
}
}
return outputCarousel(translate('PLAYING_NOW_ON_EMBY'), $size, 'streams-emby', $playingItems, "
setInterval(function() {
$('
').load('ajax.php?a=emby-streams',function() {
var element = $(this).find('[id]');
var loadedID = element.attr('id');
$('#'+loadedID).replaceWith(element);
console.log('Loaded updated: '+loadedID);
});
}, 10000);
");
}
// Get Now Playing Streams From Plex
function getPlexStreams($size, $showNames){
$address = qualifyURL(PLEXURL);
// Perform API requests
$api = @file_get_contents($address."/status/sessions?X-Plex-Token=".PLEXTOKEN);
$api = simplexml_load_string($api);
$getServer = simplexml_load_string(@file_get_contents($address."/?X-Plex-Token=".PLEXTOKEN));
if (!$getServer) { return 'Could not load!'; }
// Identify the local machine
$gotServer = $getServer['machineIdentifier'];
$items = array();
foreach($api AS $child) {
$items[] = resolvePlexItem($gotServer, PLEXTOKEN, $child, true, $showNames);
}
return outputPlexNowPlaying(translate('PLAYING_NOW_ON_PLEX'), $size, 'streams-plex', $items, "
setInterval(function() {
$('
').load('ajax.php?a=plex-streams',function() {
var element = $(this).find('[id]');
var loadedID = element.attr('id');
$('#'+loadedID).replaceWith(element);
console.log('Loaded updated: '+loadedID);
});
}, 15000);
");
}
// Get Recent Content From Emby
function getEmbyRecent($type, $size) {
$address = qualifyURL(EMBYURL);
// Currently Logged In User
$username = false;
if (isset($GLOBALS['USER'])) {
$username = strtolower($GLOBALS['USER']->username);
}
// Resolve Types
switch ($type) {
case 'movie':
$embyTypeQuery = 'IncludeItemTypes=Movie&';
$header = translate('MOVIES');
break;
case 'season':
$embyTypeQuery = 'IncludeItemTypes=Episode&';
$header = translate('TV_SHOWS');
break;
case 'album':
$embyTypeQuery = 'IncludeItemTypes=MusicAlbum&';
$header = translate('MUSIC');
break;
default:
$embyTypeQuery = '';
$header = translate('RECENT_CONTENT');
}
// Get A User
$userIds = json_decode(@file_get_contents($address.'/Users?api_key='.EMBYTOKEN),true);
if (!is_array($userIds)) { return 'Could not load!'; }
$showPlayed = true;
foreach ($userIds as $value) { // Scan for admin user
if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
$userId = $value['Id'];
}
if ($username && strtolower($value['Name']) == $username) {
$userId = $value['Id'];
$showPlayed = false;
break;
}
}
// Get the latest Items
$latest = json_decode(file_get_contents($address.'/Users/'.$userId.'/Items/Latest?'.$embyTypeQuery.'EnableImages=false&api_key='.EMBYTOKEN.($showPlayed?'':'&IsPlayed=false')),true);
// For Each Item In Category
$items = array();
foreach ($latest as $k => $v) {
$items[] = resolveEmbyItem($address, EMBYTOKEN, $v);
}
return outputCarousel($header, $size, $type.'-emby', $items);
}
// Get Recent Content From Plex
function getPlexRecent($array){
$address = qualifyURL(PLEXURL);
$header = translate('RECENT_CONTENT');
// Perform Requests
$api = @file_get_contents($address."/library/recentlyAdded?limit=100&X-Plex-Token=".PLEXTOKEN);
$api = simplexml_load_string($api);
$getServer = simplexml_load_string(@file_get_contents($address."/?X-Plex-Token=".PLEXTOKEN));
if (!$getServer) { return 'Could not load!'; }
// Identify the local machine
$gotServer = $getServer['machineIdentifier'];
$items = array();
foreach($api AS $child) {
$type = (string) $child['type'];
if($array[$type] == "true"){
$items[] = resolvePlexItem($gotServer, PLEXTOKEN, $child, false, false);
}
}
return outputRecentAdded($header, $items, "", $array);
}
// Get Image From Emby
function getEmbyImage() {
$embyAddress = qualifyURL(EMBYURL);
$itemId = $_GET['img'];
$imgParams = array();
if (isset($_GET['height'])) { $imgParams['height'] = 'maxHeight='.$_GET['height']; }
if (isset($_GET['width'])) { $imgParams['width'] = 'maxWidth='.$_GET['width']; }
if(isset($itemId)) {
$image_src = $embyAddress . '/Items/'.$itemId.'/Images/Primary?'.implode('&', $imgParams);
header('Content-type: image/jpeg');
@readfile($image_src);
die();
} else {
debug_out('Invalid Request',1);
}
}
// Get Image From Plex
function getPlexImage() {
$plexAddress = qualifyURL(PLEXURL);
if (!file_exists('images/cache')) {
mkdir('images/cache', 0777, true);
}
$image_url = $_GET['img'];
$key = $_GET['key'];
$image_height = $_GET['height'];
$image_width = $_GET['width'];
if(isset($image_url) && isset($image_height) && isset($image_width)) {
$image_src = $plexAddress . '/photo/:/transcode?height='.$image_height.'&width='.$image_width.'&upscale=1&url=' . $image_url . '&X-Plex-Token=' . PLEXTOKEN;
$cachefile = 'images/cache/'.$key.'.jpg';
$cachetime = 604800;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime > filemtime($cachefile)) {
header("Content-type: image/jpeg");
@readfile($cachefile);
exit;
}
ob_start(); // Start the output buffer
header('Content-type: image/jpeg');
@readfile($image_src);
// Cache the output to a file
$fp = fopen($cachefile, 'wb');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser
die();
} else {
echo "Invalid Plex Request";
}
}
// Simplier access to class
function translate($string) {
if (isset($GLOBALS['language'])) {
return $GLOBALS['language']->translate($string);
} else {
return '!Translations Not Loaded!';
}
}
// Generate Random string
function randString($length = 10, $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
$tmp = '';
for ($i = 0; $i < $length; $i++) {
$tmp .= substr(str_shuffle($chars), 0, 1);
}
return $tmp;
}
// Create config file in the return syntax
function createConfig($array, $path = 'config/config.php', $nest = 0) {
// Define Initial Value
$output = array();
// Sort Items
ksort($array);
// Update the current config version
if (!$nest) {
// Inject Current Version
$output[] = "\t'CONFIG_VERSION' => '".(isset($array['apply_CONFIG_VERSION'])?$array['apply_CONFIG_VERSION']:INSTALLEDVERSION)."'";
}
unset($array['CONFIG_VERSION']);
unset($array['apply_CONFIG_VERSION']);
// Process Settings
foreach ($array as $k => $v) {
$allowCommit = true;
switch (gettype($v)) {
case 'boolean':
$item = ($v?'true':'false');
break;
case 'integer':
case 'double':
case 'integer':
case 'NULL':
$item = $v;
break;
case 'string':
$item = "'".str_replace(array('\\',"'"),array('\\\\',"\'"),$v)."'";
break;
case 'array':
$item = createConfig($v, false, $nest+1);
break;
default:
$allowCommit = false;
}
if($allowCommit) {
$output[] = str_repeat("\t",$nest+1)."'$k' => $item";
}
}
// Build output
$output = (!$nest?" $v) {
$current[$k] = $v;
}
// Return Create
return createConfig($current);
}
// Inject Defaults As Needed
function fillDefaultConfig($array, $path = 'config/configDefaults.php') {
if (is_string($path)) {
$loadedDefaults = loadConfig($path);
} else {
$loadedDefaults = $path;
}
return (is_array($loadedDefaults) ? fillDefaultConfig_recurse($array, $loadedDefaults) : false);
}
// support function for fillDefaultConfig()
function fillDefaultConfig_recurse($current, $defaults) {
foreach($defaults as $k => $v) {
if (!isset($current[$k])) {
$current[$k] = $v;
} else if (is_array($current[$k]) && is_array($v)) {
$current[$k] = fillDefaultConfig_recurse($current[$k], $v);
}
}
return $current;
};
// Define Scalar Variables (nest non-secular with underscores)
function defineConfig($array, $anyCase = true, $nest_prefix = false) {
foreach($array as $k => $v) {
if (is_scalar($v) && !defined($nest_prefix.$k)) {
define($nest_prefix.$k, $v, $anyCase);
} else if (is_array($v)) {
defineConfig($v, $anyCase, $nest_prefix.$k.'_');
}
}
}
// This function exists only because I am lazy
function configLazy($path = 'config/config.php') {
// Load config or default
if (file_exists($path)) {
$config = fillDefaultConfig(loadConfig($path));
} else {
$config = loadConfig('config/configDefaults.php');
}
if (is_array($config)) {
defineConfig($config);
}
return $config;
}
// Qualify URL
function qualifyURL($url) {
//local address?
if(substr($url, 0,1) == "/"){
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$protocol = "https://";
} else {
$protocol = "http://";
}
$url = $protocol.getServer().$url;
}
// Get Digest
$digest = parse_url($url);
// http/https
if (!isset($digest['scheme'])) {
if (isset($digest['port']) && in_array($digest['port'], array(80,8080,8096,32400,7878,8989,8182,8081,6789))) {
$scheme = 'http';
} else {
$scheme = 'https';
}
} else {
$scheme = $digest['scheme'];
}
// Host
$host = (isset($digest['host'])?$digest['host']:'');
// Port
$port = (isset($digest['port'])?':'.$digest['port']:'');
// Path
$path = (isset($digest['path'])?$digest['path']:'');
// Output
return $scheme.'://'.$host.$port.$path;
}
// Function to be called at top of each to allow upgrading environment as the spec changes
function upgradeCheck() {
// Upgrade to 1.31
if (file_exists('homepageSettings.ini.php')) {
$databaseConfig = parse_ini_file('databaseLocation.ini.php', true);
$homepageConfig = parse_ini_file('homepageSettings.ini.php', true);
$databaseConfig = array_merge($databaseConfig, $homepageConfig);
$databaseData = '; ' . "\r\n";
foreach($databaseConfig as $k => $v) {
if(substr($v, -1) == "/") : $v = rtrim($v, "/"); endif;
$databaseData .= $k . " = \"" . $v . "\"\r\n";
}
write_ini_file($databaseData, 'databaseLocation.ini.php');
unlink('homepageSettings.ini.php');
unset($databaseData);
unset($homepageConfig);
}
// Upgrade to 1.32
if (file_exists('databaseLocation.ini.php')) {
// Load Existing
$config = parse_ini_file('databaseLocation.ini.php', true);
// Refactor
$config['database_Location'] = preg_replace('/\/\/$/','/',$config['databaseLocation'].'/');
$config['user_home'] = $config['database_Location'].'users/';
unset($config['databaseLocation']);
// Turn Off Emby And Plex Recent
$config["embyURL"] = $config["embyURL"].(!empty($config["embyPort"])?':'.$config["embyPort"]:'');
unset($config["embyPort"]);
$config["plexURL"] = $config["plexURL"].(!empty($config["plexPort"])?':'.$config["plexPort"]:'');
unset($config["plexPort"]);
$config["nzbgetURL"] = $config["nzbgetURL"].(!empty($config["nzbgetPort"])?':'.$config["nzbgetPort"]:'');
unset($config["nzbgetPort"]);
$config["sabnzbdURL"] = $config["sabnzbdURL"].(!empty($config["sabnzbdPort"])?':'.$config["sabnzbdPort"]:'');
unset($config["sabnzbdPort"]);
$config["headphonesURL"] = $config["headphonesURL"].(!empty($config["headphonesPort"])?':'.$config["headphonesPort"]:'');
unset($config["headphonesPort"]);
// Write config file
$config['CONFIG_VERSION'] = '1.32';
copy('config/config.php', 'config/config['.date('Y-m-d_H-i-s').'][pre1.32].bak.php');
$createConfigSuccess = createConfig($config);
// Create new config
if ($createConfigSuccess) {
if (file_exists('config/config.php')) {
// Remove Old ini file
unlink('databaseLocation.ini.php');
} else {
debug_out('Something is not right here!');
}
} else {
debug_out('Couldn\'t create updated configuration.' ,1);
}
}
// Upgrade to 1.33
$config = loadConfig();
if (isset($config['database_Location']) && (!isset($config['CONFIG_VERSION']) || $config['CONFIG_VERSION'] < '1.33')) {
// Fix User Directory
$config['database_Location'] = preg_replace('/\/\/$/','/',$config['database_Location'].'/');
$config['user_home'] = $config['database_Location'].'users/';
unset($config['USER_HOME']);
// Backend auth merge
if (isset($config['authBackendPort']) && !isset(parse_url($config['authBackendHost'])['port'])) {
$config['authBackendHost'] .= ':'.$config['authBackendPort'];
}
unset($config['authBackendPort']);
// If auth is being used move it to embyURL as that is now used in auth functions
if ((isset($config['authType']) && $config['authType'] == 'true') && (isset($config['authBackendHost']) && $config['authBackendHost'] == 'true') && (isset($config['authBackend']) && in_array($config['authBackend'], array('emby_all','emby_local','emby_connect')))) {
$config['embyURL'] = $config['authBackendHost'];
}
// Upgrade database to latest version
updateSQLiteDB($config['database_Location'],'1.32');
// Update Version and Commit
$config['apply_CONFIG_VERSION'] = '1.33';
copy('config/config.php', 'config/config['.date('Y-m-d_H-i-s').'][1.32].bak.php');
$createConfigSuccess = createConfig($config);
unset($config);
}
// Upgrade to 1.34
$config = loadConfig();
if (isset($config['database_Location']) && (!isset($config['CONFIG_VERSION']) || $config['CONFIG_VERSION'] < '1.34')) {
// Upgrade database to latest version
updateSQLiteDB($config['database_Location'],'1.33');
// Update Version and Commit
$config['CONFIG_VERSION'] = '1.34';
copy('config/config.php', 'config/config['.date('Y-m-d_H-i-s').'][1.33].bak.php');
$createConfigSuccess = createConfig($config);
unset($config);
}
return true;
}
// Check if all software dependancies are met
function dependCheck() {
$output = array();
if (!extension_loaded('pdo_sqlite')) { $output[] = 'PDO:SQLite not enabled, please add "extension = php_pdo_sqlite.dll" to php.ini'; }
//if (!extension_loaded('sqlite3')) { $output[] = 'SQLite3 not enabled, please add "extension = php_sqlite3.dll" to php.ini'; }
if ($output) {
debug_out($output,1);
}
return true;
}
// Process file uploads
function uploadFiles($path, $ext_mask = null) {
if (isset($_FILES) && count($_FILES)) {
require_once('class.uploader.php');
$uploader = new Uploader();
$data = $uploader->upload($_FILES['files'], array(
'limit' => 10,
'maxSize' => 10,
'extensions' => $ext_mask,
'required' => false,
'uploadDir' => str_replace('//','/',$path.'/'),
'title' => array('name'),
'removeFiles' => true,
'replace' => true,
));
if($data['isComplete']){
$files = $data['data'];
writeLog("success", $files['metas'][0]['name']." was uploaded");
echo json_encode($files['metas'][0]['name']);
}
if($data['hasErrors']){
$errors = $data['errors'];
writeLog("error", $files['metas'][0]['name']." was not able to upload");
echo json_encode($errors);
}
} else {
writeLog("error", "image was not uploaded");
echo json_encode('No files submitted!');
}
}
// Remove file
function removeFiles($path) {
if(is_file($path)) {
writeLog("success", "image was removed");
unlink($path);
} else {
writeLog("error", "image was not removed");
echo json_encode('No file specified for removal!');
}
}
// Lazy select options
function resolveSelectOptions($array, $selected = '', $multi = false) {
$output = array();
$selectedArr = ($multi?explode('|', $selected):array());
foreach ($array as $key => $value) {
if (is_array($value)) {
if (isset($value['optgroup'])) {
$output[] = '