homepageOrdercustomhtml, "homepageOrdernotice" => homepageOrdernotice, "homepageOrderplexsearch" => homepageOrderplexsearch, "homepageOrderspeedtest" => homepageOrderspeedtest, "homepageOrdernzbget" => homepageOrdernzbget, "homepageOrdersabnzbd" => homepageOrdersabnzbd, "homepageOrderplexnowplaying" => homepageOrderplexnowplaying, "homepageOrderplexrecent" => homepageOrderplexrecent, "homepageOrderplexplaylist" => homepageOrderplexplaylist, "homepageOrderembynowplaying" => homepageOrderembynowplaying, "homepageOrderembyrecent" => homepageOrderembyrecent, "homepageOrderombi" => homepageOrderombi, "homepageOrdercalendar" => homepageOrdercalendar, "homepageOrdernoticeguest" => homepageOrdernoticeguest, "homepageOrdertransmisson" => homepageOrdertransmisson, ); asort($homepageOrder); return $homepageOrder; } // Debugging output functions function debug_out($variable, $die = false) { $trace = debug_backtrace()[0]; echo "
"; echo "
Look's like something happened, here are the errors and perhaps how to fix them:
"; echo '
'.$trace['file'].':'.$trace['line']."\n\n".print_r($variable, true).'
'; if ($die) { http_response_code(503); die(); } } //Cookie Function function coookie($type, $name, $value = '', $days = -1, $http = true){ if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https"){ $Secure = true; $HTTPOnly = true; }elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { $Secure = true; $HTTPOnly = true; } else { $Secure = false; $HTTPOnly = false; } if(!$http){ $HTTPOnly = false; } $Path = '/'; $Domain = $_SERVER['HTTP_HOST']; $Port = strpos($Domain, ':'); if ($Port !== false) $Domain = substr($Domain, 0, $Port); $Port = strpos($Domain, ':'); $check = substr_count($Domain, '.'); if($check >= 3){ if(is_numeric($Domain[0])){ $Domain = ''; }else{ $Domain = '.'.explode('.',$Domain)[1].'.'.explode('.',$Domain)[2].'.'.explode('.',$Domain)[3]; } }elseif($check == 2){ $Domain = '.'.explode('.',$Domain)[1].'.'.explode('.',$Domain)[2]; }elseif($check == 1){ $Domain = '.' . $Domain; }else{ $Domain = ''; } if($type = 'set'){ $_COOKIE[$name] = $value; header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value) . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() + (86400 * $days)) . ' GMT') . (empty($Path) ? '' : '; path=' . $Path) . (empty($Domain) ? '' : '; domain=' . $Domain) . (!$Secure ? '' : '; secure') . (!$HTTPOnly ? '' : '; HttpOnly'), false); }elseif($type = 'delete'){ unset($_COOKIE[$name]); header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value) . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() - 3600) . ' GMT') . (empty($Path) ? '' : '; path=' . $Path) . (empty($Domain) ? '' : '; domain=' . $Domain) . (!$Secure ? '' : '; secure') . (!$HTTPOnly ? '' : '; HttpOnly'), false); } } // ==== Auth Plugins START ==== if (function_exists('ldap_connect')) : // Pass credentials to LDAP backend function plugin_auth_ldap($username, $password) { $ldapServers = explode(',',AUTHBACKENDHOST); foreach($ldapServers as $key => $value) { // Calculate parts $digest = parse_url(trim($value)); $scheme = strtolower((isset($digest['scheme'])?$digest['scheme']:'ldap')); $host = (isset($digest['host'])?$digest['host']:(isset($digest['path'])?$digest['path']:'')); $port = (isset($digest['port'])?$digest['port']:(strtolower($scheme)=='ldap'?389:636)); // Reassign $ldapServers[$key] = $scheme.'://'.$host.':'.$port; } // returns true or false $ldap = ldap_connect(implode(' ',$ldapServers)); if(empty(AUTHBACKENDDOMAINFORMAT)){ if ($bind = ldap_bind($ldap, AUTHBACKENDDOMAIN.'\\'.$username, $password)) { writeLog("success", "LDAP authentication success"); return true; } else { writeLog("error", "LDAP could not authenticate"); return false; } }else{ ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); $bind = @ldap_bind($ldap, sprintf(AUTHBACKENDDOMAINFORMAT, $username), $password); if ($bind) { writeLog("success", "LDAP authentication success"); return true; } else { writeLog("error", "LDAP could not authenticate"); return false; } } writeLog("error", "LDAP could not authenticate"); return false; } else : // Ldap Auth Missing Dependancy function plugin_auth_ldap_disabled() { return 'LDAP - Disabled (Dependancy: php-ldap missing!)'; } endif; // Pass credentials to FTP backend function plugin_auth_ftp($username, $password) { // Calculate parts $digest = parse_url(AUTHBACKENDHOST); $scheme = strtolower((isset($digest['scheme'])?$digest['scheme']:(function_exists('ftp_ssl_connect')?'ftps':'ftp'))); $host = (isset($digest['host'])?$digest['host']:(isset($digest['path'])?$digest['path']:'')); $port = (isset($digest['port'])?$digest['port']:21); // Determine Connection Type if ($scheme == 'ftps') { $conn_id = ftp_ssl_connect($host, $port, 20); } elseif ($scheme == 'ftp') { $conn_id = ftp_connect($host, $port, 20); } else { debug_out('Invalid FTP scheme. Use ftp or ftps'); writeLog("error", "invalid FTP scheme"); return false; } // Check if valid FTP connection if ($conn_id) { // Attempt login @$login_result = ftp_login($conn_id, $username, $password); ftp_close($conn_id); // Return Result if ($login_result) { writeLog("success", "$username authenticated"); return true; } else { writeLog("error", "$username could not authenticate"); return false; } } else { return false; } return false; } // Pass credentials to Emby Backend function plugin_auth_emby_local($username, $password) { $embyAddress = qualifyURL(EMBYURL); $headers = array( 'Authorization'=> 'MediaBrowser UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", Client="None", Device="Organizr", DeviceId="xxx", Version="1.0.0.0"', 'Content-Type' => 'application/json', ); $body = array( 'Username' => $username, 'Password' => sha1($password), 'PasswordMd5' => md5($password), ); $response = post_router($embyAddress.'/Users/AuthenticateByName', $body, $headers); if (isset($response['content'])) { $json = json_decode($response['content'], true); if (is_array($json) && isset($json['SessionInfo']) && isset($json['User']) && $json['User']['HasPassword'] == true) { // Login Success - Now Logout Emby Session As We No Longer Need It $headers = array( 'X-Mediabrowser-Token' => $json['AccessToken'], ); $response = post_router($embyAddress.'/Sessions/Logout', array(), $headers); return true; } } return false; } if (function_exists('curl_version')) : // Authenticate Against Emby Local (first) and Emby Connect function plugin_auth_emby_all($username, $password) { $localResult = plugin_auth_emby_local($username, $password); if ($localResult) { return $localResult; } else { return plugin_auth_emby_connect($username, $password); } } // Authenicate against emby connect function plugin_auth_emby_connect($username, $password) { $embyAddress = qualifyURL(EMBYURL); // Get A User $connectId = ''; $userIds = json_decode(@file_get_contents($embyAddress.'/Users?api_key='.EMBYTOKEN),true); if (is_array($userIds)) { foreach ($userIds as $key => $value) { // Scan for this user if (isset($value['ConnectUserName']) && isset($value['ConnectUserId'])) { // Qualifty as connect account if ($value['ConnectUserName'] == $username || $value['Name'] == $username) { $connectId = $value['ConnectUserId']; break; } } } if ($connectId) { $connectURL = 'https://connect.emby.media/service/user/authenticate'; $headers = array( 'Accept'=> 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', ); $body = array( 'nameOrEmail' => $username, 'rawpw' => $password, ); $result = curl_post($connectURL, $body, $headers); if (isset($result['content'])) { $json = json_decode($result['content'], true); if (is_array($json) && isset($json['AccessToken']) && isset($json['User']) && $json['User']['Id'] == $connectId) { return array( 'email' => $json['User']['Email'], 'image' => $json['User']['ImageUrl'], ); } } } } return false; } // Pass credentials to Plex Backend function plugin_auth_plex($username, $password) { // Quick out $isAdmin = false; if ((strtolower(PLEXUSERNAME) == strtolower($username)) && $password == PLEXPASSWORD) { writeLog("success", "Admin: ".$username." authenticated as plex Admin"); $isAdmin = true; } //Get User List $userURL = 'https://plex.tv/pms/friends/all'; $userHeaders = array( 'Authorization' => 'Basic '.base64_encode(PLEXUSERNAME.':'.PLEXPASSWORD), ); libxml_use_internal_errors(true); $userXML = simplexml_load_string(curl_get($userURL, $userHeaders)); if (is_array($userXML) || is_object($userXML)) { $isUser = false; $usernameLower = strtolower($username); foreach($userXML AS $child) { if(isset($child['username']) && strtolower($child['username']) == $usernameLower || isset($child['email']) && strtolower($child['email']) == $usernameLower) { $isUser = true; writeLog("success", $usernameLower." was found in plex friends list"); break; } } if ($isUser || $isAdmin) { //Login User $connectURL = 'https://plex.tv/users/sign_in.json'; $headers = array( 'Accept'=> 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'X-Plex-Product' => 'Organizr', 'X-Plex-Version' => '1.0', 'X-Plex-Client-Identifier' => '01010101-10101010', ); $body = array( 'user[login]' => $username, 'user[password]' => $password, ); $result = curl_post($connectURL, $body, $headers); if (isset($result['content'])) { $json = json_decode($result['content'], true); if ((is_array($json) && isset($json['user']) && isset($json['user']['username'])) && strtolower($json['user']['username']) == $usernameLower || strtolower($json['user']['email']) == $usernameLower) { writeLog("success", $json['user']['username']." was logged into organizr using plex credentials"); return array( 'email' => $json['user']['email'], 'image' => $json['user']['thumb'], 'token' => $json['user']['authToken'], 'type' => $isAdmin ? 'admin' : 'user', ); } }else{ writeLog("error", "error occured while trying to sign $username into plex"); } }else{ writeLog("error", "$username is not an authorized PLEX user or entered invalid password"); } }else{ writeLog("error", "error occured logging into plex might want to check curl.cainfo=/path/to/downloaded/cacert.pem in php.ini"); } return false; } else : // Plex Auth Missing Dependancy function plugin_auth_plex_disabled() { return 'Plex - Disabled (Dependancy: php-curl missing!)'; } // Emby Connect Auth Missing Dependancy function plugin_auth_emby_connect_disabled() { return 'Emby Connect - Disabled (Dependancy: php-curl missing!)'; } // Emby Both Auth Missing Dependancy function plugin_auth_emby_both_disabled() { return 'Emby Both - Disabled (Dependancy: php-curl missing!)'; } endif; // ==== Auth Plugins END ==== // ==== General Class Definitions START ==== $userLanguage = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : "en"; class setLanguage { private $language = null; private $langCode = null; function __construct($language = false) { // Default if (!$language) { $language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : "en"; } if (!file_exists("lang/{$language}.ini")) { $language = 'en'; } $this->langCode = $language; $this->language = parse_ini_file("lang/{$language}.ini", false, INI_SCANNER_RAW); if (file_exists("lang/{$language}.cust.ini")) { foreach($tmp = parse_ini_file("lang/{$language}.cust.ini", false, INI_SCANNER_RAW) as $k => $v) { $this->language[$k] = $v; } } } public function getLang() { return $this->langCode; } public function translate($originalWord) { $getArg = func_num_args(); if ($getArg > 1) { $allWords = func_get_args(); array_shift($allWords); } else { $allWords = array(); } $translatedWord = isset($this->language[$originalWord]) ? $this->language[$originalWord] : null; if (!$translatedWord) { return ucwords(str_replace("_", " ", strtolower($originalWord))); //echo "WHOA!!!!!!! $originalWord"; } $translatedWord = htmlspecialchars($translatedWord, ENT_QUOTES); return vsprintf($translatedWord, $allWords); } } $language = new setLanguage; // ==== General Class Definitions END ==== // Direct request to curl if it exists, otherwise handle if not HTTPS function post_router($url, $data, $headers = array(), $referer='') { if (function_exists('curl_version')) { return curl_post($url, $data, $headers, $referer); } else { return post_request($url, $data, $headers, $referer); } } if (function_exists('curl_version')) : // Curl Post function curl_post($url, $data, $headers = array(), $referer='') { // Initiate cURL $curlReq = curl_init($url); // As post request curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlReq, CURLOPT_CAINFO, getCert()); if(localURL($url)){ curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0); } // Format Data switch (isset($headers['Content-Type'])?$headers['Content-Type']:'') { case 'application/json': curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data)); break; case 'application/x-www-form-urlencoded': curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data)); break; default: $headers['Content-Type'] = 'application/x-www-form-urlencoded'; curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data)); } // Format Headers $cHeaders = array(); foreach ($headers as $k => $v) { $cHeaders[] = $k.': '.$v; } if (count($cHeaders)) { curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders); } // Execute $result = curl_exec($curlReq); $httpcode = curl_getinfo($curlReq); // Close curl_close($curlReq); // Return return array('content'=>$result, 'http_code'=>$httpcode); } // Curl Put function curl_put($url, $data, $headers = array(), $referer='') { // Initiate cURL $curlReq = curl_init($url); // As post request curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlReq, CURLOPT_CAINFO, getCert()); if(localURL($url)){ curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0); } // Format Data switch (isset($headers['Content-Type'])?$headers['Content-Type']:'') { case 'application/json': curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data)); break; case 'application/x-www-form-urlencoded': curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data)); break; default: $headers['Content-Type'] = 'application/x-www-form-urlencoded'; curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data)); } // Format Headers $cHeaders = array(); foreach ($headers as $k => $v) { $cHeaders[] = $k.': '.$v; } if (count($cHeaders)) { curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders); } // Execute $result = curl_exec($curlReq); $httpcode = curl_getinfo($curlReq); // Close curl_close($curlReq); // Return return array('content'=>$result, 'http_code'=>$httpcode); } //Curl Get Function function curl_get($url, $headers = array()) { // Initiate cURL $curlReq = curl_init($url); // As post request curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlReq, CURLOPT_CAINFO, getCert()); curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5); if(localURL($url)){ curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0); } // Format Headers $cHeaders = array(); foreach ($headers as $k => $v) { $cHeaders[] = $k.': '.$v; } if (count($cHeaders)) { curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders); } // Execute $result = curl_exec($curlReq); // Close curl_close($curlReq); // Return return $result; } //Curl Delete Function function curl_delete($url, $headers = array()) { // Initiate cURL $curlReq = curl_init($url); // As post request curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($curlReq, CURLOPT_CAINFO, getCert()); if(localURL($url)){ curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0); } // Format Headers $cHeaders = array(); foreach ($headers as $k => $v) { $cHeaders[] = $k.': '.$v; } if (count($cHeaders)) { curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders); } // Execute $result = curl_exec($curlReq); $httpcode = curl_getinfo($curlReq); // Close curl_close($curlReq); // Return return array('content'=>$result, 'http_code'=>$httpcode); } endif; //Case-Insensitive Function function in_arrayi($needle, $haystack) { return in_array(strtolower($needle), array_map('strtolower', $haystack)); } // HTTP post request (Removes need for curl, probably useless) function post_request($url, $data, $headers = array(), $referer='') { // Adapted from http://stackoverflow.com/a/28387011/6810513 // Convert the data array into URL Parameters like a=b&foo=bar etc. if (isset($headers['Content-Type'])) { switch ($headers['Content-Type']) { case 'application/json': $data = json_encode($data); break; case 'application/x-www-form-urlencoded': $data = http_build_query($data); break; } } else { $headers['Content-Type'] = 'application/x-www-form-urlencoded'; $data = http_build_query($data); } // parse the given URL $urlDigest = parse_url($url); // extract host and path: $host = $urlDigest['host']; $path = $urlDigest['path']; if ($urlDigest['scheme'] != 'http') { die('Error: Only HTTP request are supported, please use cURL to add HTTPS support! ('.$urlDigest['scheme'].'://'.$host.')'); } // open a socket connection on port 80 - timeout: 30 sec $fp = fsockopen($host, (isset($urlDigest['port'])?':'.$urlDigest['port']:80), $errno, $errstr, 30); if ($fp){ // send the request headers: fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); if ($referer != '') fputs($fp, "Referer: $referer\r\n"); fputs($fp, "Content-length: ". strlen($data) ."\r\n"); foreach($headers as $k => $v) { fputs($fp, $k.": ".$v."\r\n"); } fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $data); $result = ''; while(!feof($fp)) { // receive the results of the request $result .= fgets($fp, 128); } } else { return array( 'status' => 'err', 'error' => "$errstr ($errno)" ); } // close the socket connection: fclose($fp); // split the result header from the content $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; // return as structured array: return array( 'status' => 'ok', 'header' => $header, 'content' => $content, ); } // Format item from Emby for Carousel function resolveEmbyItem($address, $token, $item, $nowPlaying = false, $showNames = false, $role = false, $moreInfo = false) { // Static Height $height = 444; // Get Item Details $itemDetails = json_decode(@file_get_contents($address.'/Items?Ids='.$item['Id'].'&api_key='.$token),true)['Items'][0]; /*if (substr_count(EMBYURL, ':') == 2) { $URL = "http://app.emby.media/itemdetails.html?id=".$itemDetails['Id']; }else{ $URL = EMBYURL."/web/itemdetails.html?id=".$itemDetails['Id']; }*/ $URL = EMBYURL."/web/#!/itemdetails.html?id=".$itemDetails['Id']; switch ($itemDetails['Type']) { case 'Episode': case 'Series': $title = (isset($itemDetails['SeriesName'])?$itemDetails['SeriesName']:""); $imageId = (isset($itemDetails['SeriesId'])?$itemDetails['SeriesId']:$itemDetails['Id']); $width = 300; $style = ''; $image = 'slick-image-tall'; if(!$nowPlaying){ $imageType = (isset($itemDetails['ImageTags']['Primary']) ? "Primary" : false); $key = $itemDetails['Id'] . "-list"; $itemType = 'season'; }else{ $height = 281; $width = 500; $imageId = isset($itemDetails['ParentThumbItemId']) ? $itemDetails['ParentThumbItemId'] : (isset($itemDetails['ParentBackdropItemId']) ? $itemDetails['ParentBackdropItemId'] : false); $imageType = isset($itemDetails['ParentThumbItemId']) ? "Thumb" : (isset($itemDetails['ParentBackdropItemId']) ? "Backdrop" : false); $key = (isset($itemDetails['ParentThumbItemId']) ? $itemDetails['ParentThumbItemId']."-np" : "none-np"); $elapsed = $moreInfo['PlayState']['PositionTicks']; $duration = $moreInfo['NowPlayingItem']['RunTimeTicks']; $watched = (!empty($elapsed) ? floor(($elapsed / $duration) * 100) : 0); //$transcoded = floor($item->TranscodeSession['progress']- $watched); $stream = $moreInfo['PlayState']['PlayMethod']; $user = $role == "admin" ? $moreInfo['UserName'] : ""; $id = $moreInfo['DeviceId']; $streamInfo = buildStream(array( 'platform' => (string) $moreInfo['Client'], 'device' => (string) $moreInfo['DeviceName'], 'stream' => streamType($stream), 'video' => streamType($stream)." ".embyArray($moreInfo['NowPlayingItem']['MediaStreams'], "video"), 'audio' => streamType($stream)." ".embyArray($moreInfo['NowPlayingItem']['MediaStreams'], "audio"), )); $state = (($moreInfo['PlayState']['IsPaused'] == "1") ? "pause" : "play"); $topTitle = '
'.$title.' - '.$itemDetails['Name'].'
'; $bottomTitle = 'S'.$itemDetails['ParentIndexNumber'].' · E'.$itemDetails['IndexNumber'].''; if($showNames == "true"){ $bottomTitle .= ''.$user.''; } } break; case 'MusicAlbum': case 'Audio': $title = $itemDetails['Name']; $imageId = $itemDetails['Id']; $width = 444; $style = ''; $image = 'slick-image-short'; if(!$nowPlaying){ $imageType = (isset($itemDetails['ImageTags']['Primary']) ? "Primary" : false); $key = $itemDetails['Id'] . "-list"; $itemType = 'album'; }else{ $height = 281; $width = 500; $imageId = (isset($itemDetails['ParentBackdropItemId']) ? $itemDetails['ParentBackdropItemId'] : false); $imageType = (isset($itemDetails['ParentBackdropItemId']) ? "Backdrop" : false); $key = (isset($itemDetails['ParentBackdropItemId']) ? $itemDetails['ParentBackdropItemId'] : "no-np") . "-np"; $elapsed = $moreInfo['PlayState']['PositionTicks']; $duration = $moreInfo['NowPlayingItem']['RunTimeTicks']; $watched = (!empty($elapsed) ? floor(($elapsed / $duration) * 100) : 0); //$transcoded = floor($item->TranscodeSession['progress']- $watched); $stream = $moreInfo['PlayState']['PlayMethod']; $user = $role == "admin" ? $moreInfo['UserName'] : ""; $id = $moreInfo['DeviceId']; $streamInfo = buildStream(array( 'platform' => (string) $moreInfo['Client'], 'device' => (string) $moreInfo['DeviceName'], 'stream' => streamType($stream), 'audio' => streamType($stream)." ".embyArray($moreInfo['NowPlayingItem']['MediaStreams'], "audio"), )); $state = (($moreInfo['PlayState']['IsPaused'] == "1") ? "pause" : "play"); $topTitle = '
'.$itemDetails['AlbumArtist'].' - '.$itemDetails['Album'].'
'; $bottomTitle = ''.$title.''; if($showNames == "true"){ $bottomTitle .= ''.$user.''; } } break; case 'TvChannel': $title = $itemDetails['CurrentProgram']['Name']; $imageId = $itemDetails['Id']; $width = 300; $style = ''; $image = 'slick-image-tall'; if(!$nowPlaying){ $imageType = "Primary"; $key = $itemDetails['Id'] . "-list"; }else{ $height = 281; $width = 500; $imageType = "Thumb"; $key = $itemDetails['Id'] . "-np"; $useImage = "images/livetv.png"; $watched = "0"; $stream = $moreInfo['PlayState']['PlayMethod']; $user = $role == "admin" ? $moreInfo['UserName'] : ""; $id = $moreInfo['DeviceId']; $streamInfo = buildStream(array( 'platform' => (string) $moreInfo['Client'], 'device' => (string) $moreInfo['DeviceName'], 'stream' => streamType($stream), 'video' => streamType($stream)." ".embyArray($moreInfo['NowPlayingItem']['MediaStreams'], "video"), 'audio' => streamType($stream)." ".embyArray($moreInfo['NowPlayingItem']['MediaStreams'], "audio"), )); $state = (($moreInfo['PlayState']['IsPaused'] == "1") ? "pause" : "play"); $topTitle = '
'.$title.'
'; $bottomTitle = ''.$itemDetails['Name'].' - '.$itemDetails['ChannelNumber'].''; if($showNames == "true"){ $bottomTitle .= ''.$user.''; } } break; default: $title = $itemDetails['Name']; $imageId = $itemDetails['Id']; $width = 300; $style = ''; $image = 'slick-image-tall'; if(!$nowPlaying){ $imageType = (isset($itemDetails['ImageTags']['Primary']) ? "Primary" : false); $key = $itemDetails['Id'] . "-list"; $itemType = 'movie'; }else{ $height = 281; $width = 500; $imageType = isset($itemDetails['ImageTags']['Thumb']) ? "Thumb" : (isset($itemDetails['BackdropImageTags']) ? "Backdrop" : false); $key = $itemDetails['Id'] . "-np"; $elapsed = $moreInfo['PlayState']['PositionTicks']; $duration = $moreInfo['NowPlayingItem']['RunTimeTicks']; $watched = (!empty($elapsed) ? floor(($elapsed / $duration) * 100) : 0); //$transcoded = floor($item->TranscodeSession['progress']- $watched); $stream = $moreInfo['PlayState']['PlayMethod']; $user = $role == "admin" ? $moreInfo['UserName'] : ""; $id = $moreInfo['DeviceId']; $streamInfo = buildStream(array( 'platform' => (string) $moreInfo['Client'], 'device' => (string) $moreInfo['DeviceName'], 'stream' => streamType($stream), 'video' => streamType($stream)." ".embyArray($moreInfo['NowPlayingItem']['MediaStreams'], "video"), 'audio' => streamType($stream)." ".embyArray($moreInfo['NowPlayingItem']['MediaStreams'], "audio"), )); $state = (($moreInfo['PlayState']['IsPaused'] == "1") ? "pause" : "play"); $topTitle = '
'.$title.'
'; $bottomTitle = ''.$moreInfo['NowPlayingItem']['ProductionYear'].''; if($showNames == "true"){ $bottomTitle .= ''.$user.''; } } } // If No Overview if (!isset($itemDetails['Overview'])) { $itemDetails['Overview'] = ''; } $original_image_url = 'ajax.php?a=emby-image&type='.$imageType.'&img='.$imageId.'&height='.$height.'&width='.$width.'&key='.$key.'$'.randString(); if (file_exists('images/cache/'.$key.'.jpg')){ $image_url = 'images/cache/'.$key.'.jpg'; } if (file_exists('images/cache/'.$key.'.jpg') && (time() - 604800) > filemtime('images/cache/'.$key.'.jpg') || !file_exists('images/cache/'.$key.'.jpg')) { $image_url = 'ajax.php?a=emby-image&type='.$imageType.'&img='.$imageId.'&height='.$height.'&width='.$width.'&key='.$key.''; } if($nowPlaying){ if(!$imageType){ $original_image_url = $image_url = "images/no-np.png"; $key = "no-np"; } if(!$imageId){ $original_image_url = $image_url = "images/no-np.png"; $key = "no-np"; } }else{ if(!$imageType){ $original_image_url = $image_url = "images/no-list.png"; $key = "no-list"; } if(!$imageId){ $original_image_url = $image_url = "images/no-list.png"; $key = "no-list"; } } if(isset($useImage)){ $image_url = $useImage; } // Assemble Item And Cache Into Array if($nowPlaying){ //prettyPrint($itemDetails); return '
'.$itemDetails['Name'].'
'.$topTitle.''.$bottomTitle.'
'; }else{ return '
'.$itemDetails['Name'].''.$title.'
'; } } // Format item from Plex for Carousel function resolvePlexItem($server, $token, $item, $nowPlaying = false, $showNames = false, $role = false, $playlist = false) { // Static Height $height = 444; $widthOverride = 100; $playlist = ($playlist) ? " playlist-$playlist" : ""; switch ($item['type']) { case 'season': $title = $item['parentTitle']; $summary = $item['parentSummary']; $width = 300; $image = 'slick-image-tall'; $style = ''; if(!$nowPlaying){ $thumb = $item['thumb']; $key = $item['ratingKey'] . "-list"; }else { $height = 281; $width = 500; $thumb = $item['art']; $key = $item['ratingKey'] . "-np"; $elapsed = $item['viewOffset']; $duration = ($item['duration']) ? $item['duration'] : $item->Media['duration']; $watched = (!empty($elapsed) ? floor(($elapsed / $duration) * 100) : 0); $transcoded = floor($item->TranscodeSession['progress']- $watched); $stream = $item->Media->Part->Stream['decision']; $user = $role == "admin" ? $item->User['title'] : ""; $id = str_replace('"', '', $item->Player['machineIdentifier']); $streamInfo = buildStream(array( 'platform' => (string) $item->Player['platform'], 'device' => (string) $item->Player['device'], 'stream' => streamType($item->Media->Part['decision']), 'video' => streamType($item->Media->Part->Stream[0]['decision'])." (".$item->Media->Part->Stream[0]['codec'].") (".$item->Media->Part->Stream[0]['width']."x".$item->Media->Part->Stream[0]['height'].")", 'audio' => streamType($item->Media->Part->Stream[1]['decision'])." (".$item->Media->Part->Stream[1]['codec'].") (".$item->Media->Part->Stream[1]['channels']."ch)", )); $state = (($item->Player['state'] == "paused") ? "pause" : "play"); } break; case 'episode': $title = $item['grandparentTitle']; $summary = $item['title']; $width = 300; $image = 'slick-image-tall'; $style = ''; if(!$nowPlaying){ $thumb = ($item['parentThumb'] ? $item['parentThumb'] : $item['grandparentThumb']); $key = $item['ratingKey'] . "-list"; }else { $height = 281; $width = 500; $thumb = $item['art']; $key = $item['ratingKey'] . "-np"; $elapsed = $item['viewOffset']; $duration = ($item['duration']) ? $item['duration'] : $item->Media['duration']; $watched = (!empty($elapsed) ? floor(($elapsed / $duration) * 100) : 0); $transcoded = floor($item->TranscodeSession['progress']- $watched); $stream = $item->Media->Part->Stream['decision']; $user = $role == "admin" ? $item->User['title'] : ""; $id = str_replace('"', '', $item->Player['machineIdentifier']); $streamInfo = buildStream(array( 'platform' => (string) $item->Player['platform'], 'device' => (string) $item->Player['device'], 'stream' => streamType($item->Media->Part['decision']), 'video' => streamType($item->Media->Part->Stream[0]['decision'])." (".$item->Media->Part->Stream[0]['codec'].") (".$item->Media->Part->Stream[0]['width']."x".$item->Media->Part->Stream[0]['height'].")", 'audio' => streamType($item->Media->Part->Stream[1]['decision'])." (".$item->Media->Part->Stream[1]['codec'].") (".$item->Media->Part->Stream[1]['channels']."ch)", )); $state = (($item->Player['state'] == "paused") ? "pause" : "play"); $topTitle = '
'.$title.' - '.$item['title'].'
'; $bottomTitle = 'S'.$item['parentIndex'].' · E'.$item['index'].''; if($showNames == "true"){ $bottomTitle .= ''.$user.''; } } break; case 'clip': $title = $item['title']; $summary = $item['summary']; $width = 300; $image = 'slick-image-tall'; $style = ''; if(!$nowPlaying){ $thumb = $item['thumb']; $key = $item['ratingKey'] . "-list"; }else { $height = 281; $width = 500; $thumb = $item['art']; $key = isset($item['ratingKey']) ? $item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)"); $useImage = (isset($item['live']) ? "images/livetv.png" : null); $extraInfo = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)"); $elapsed = $item['viewOffset']; $duration = ($item['duration']) ? $item['duration'] : $item->Media['duration']; $watched = (!empty($elapsed) ? floor(($elapsed / $duration) * 100) : 0); $transcoded = floor($item->TranscodeSession['progress']- $watched); $stream = $item->Media->Part->Stream['decision']; $user = $role == "admin" ? $item->User['title'] : ""; $id = str_replace('"', '', $item->Player['machineIdentifier']); $streamInfo = buildStream(array( 'platform' => (string) $item->Player['platform'], 'device' => (string) $item->Player['device'], 'stream' => streamType($item->Media->Part['decision']), 'video' => streamType($item->Media->Part->Stream[0]['decision'])." (".$item->Media->Part->Stream[0]['codec'].") (".$item->Media->Part->Stream[0]['width']."x".$item->Media->Part->Stream[0]['height'].")", 'audio' => streamType($item->Media->Part->Stream[1]['decision'])." (".$item->Media->Part->Stream[1]['codec'].") (".$item->Media->Part->Stream[1]['channels']."ch)", )); $state = (($item->Player['state'] == "paused") ? "pause" : "play"); $topTitle = '
'.$title.'
'; $bottomTitle = ''.$extraInfo.''; if($showNames == "true"){ $bottomTitle .= ''.$user.''; } } break; case 'album': case 'track': $title = $item['parentTitle']; $summary = $item['title']; $image = 'slick-image-short'; $style = 'left: 160px !important;'; $item['ratingKey'] = $item['parentRatingKey']; if(!$nowPlaying){ $width = 444; $thumb = $item['thumb']; $key = $item['ratingKey'] . "-list"; }else { $height = 281; $width = 500; $thumb = ($item['parentThumb']) ? $item['parentThumb'] : $item['art']; $widthOverride = ($item['parentThumb']) ? 56 : 100; $key = $item['ratingKey'] . "-np"; $elapsed = $item['viewOffset']; $duration = ($item['duration']) ? $item['duration'] : $item->Media['duration']; $watched = (!empty($elapsed) ? floor(($elapsed / $duration) * 100) : 0); $transcoded = floor($item->TranscodeSession['progress']- $watched); $stream = $item->Media->Part->Stream['decision']; $user = $role == "admin" ? $item->User['title'] : ""; $id = str_replace('"', '', $item->Player['machineIdentifier']); $streamInfo = buildStream(array( 'platform' => (string) $item->Player['platform'], 'device' => (string) $item->Player['device'], 'stream' => streamType($item->Media->Part['decision']), 'audio' => streamType($item->Media->Part->Stream[0]['decision'])." (".$item->Media->Part->Stream[0]['codec'].") (".$item->Media->Part->Stream[0]['channels']."ch)", )); $state = (($item->Player['state'] == "paused") ? "pause" : "play"); $topTitle = '
'.$item['grandparentTitle'].' - '.$item['title'].'
'; $bottomTitle = ''.$title.''; if($showNames == "true"){ $bottomTitle .= ''.$user.''; } } break; default: $title = $item['title']; $summary = $item['summary']; $image = 'slick-image-tall'; $style = ''; if(!$nowPlaying){ $width = 300; $thumb = $item['thumb']; $key = $item['ratingKey'] . "-list"; }else { $height = 281; $width = 500; $thumb = $item['art']; $key = $item['ratingKey'] . "-np"; $elapsed = $item['viewOffset']; $duration = ($item['duration']) ? $item['duration'] : $item->Media['duration']; $watched = (!empty($elapsed) ? floor(($elapsed / $duration) * 100) : 0); $transcoded = floor($item->TranscodeSession['progress']- $watched); $stream = $item->Media->Part->Stream['decision']; $user = $role == "admin" ? $item->User['title'] : ""; $id = str_replace('"', '', $item->Player['machineIdentifier']); $streamInfo = buildStream(array( 'platform' => (string) $item->Player['platform'], 'device' => (string) $item->Player['device'], 'stream' => streamType($item->Media->Part['decision']), 'video' => streamType($item->Media->Part->Stream[0]['decision'])." (".$item->Media->Part->Stream[0]['codec'].") (".$item->Media->Part->Stream[0]['width']."x".$item->Media->Part->Stream[0]['height'].")", 'audio' => streamType($item->Media->Part->Stream[1]['decision'])." (".$item->Media->Part->Stream[1]['codec'].") (".$item->Media->Part->Stream[1]['channels']."ch)", )); $state = (($item->Player['state'] == "paused") ? "pause" : "play"); $topTitle = '
'.$title.'
'; $bottomTitle = ''.$item['year'].''; if($showNames == "true"){ $bottomTitle .= ''.$user.''; } } } if (PLEXTABURL) { $address = PLEXTABURL."/web/index.html#!/server/$server/details?key=/library/metadata/".$item['ratingKey']; }else{ $address = "https://app.plex.tv/web/app#!/server/$server/details?key=/library/metadata/".$item['ratingKey']; } // If No Overview if (!isset($itemDetails['Overview'])) { $itemDetails['Overview'] = ''; } $original_image_url = 'ajax.php?a=plex-image&img='.$thumb.'&height='.$height.'&width='.$width.'&key='.$key.'$'.randString(); if (file_exists('images/cache/'.$key.'.jpg')){ $image_url = 'images/cache/'.$key.'.jpg'; } if (file_exists('images/cache/'.$key.'.jpg') && (time() - 604800) > filemtime('images/cache/'.$key.'.jpg') || !file_exists('images/cache/'.$key.'.jpg')) { $image_url = 'ajax.php?a=plex-image&img='.$thumb.'&height='.$height.'&width='.$width.'&key='.$key.''; } if($nowPlaying){ if(!$thumb){ $original_image_url = $image_url = "images/no-np.png"; $key = "no-np"; } }else{ if(!$thumb){ $original_image_url = $image_url = "images/no-list.png"; $key = "no-list"; } } if(isset($useImage)){ $image_url = $useImage; } $openTab = (PLEXTABNAME) ? "true" : "false"; // Assemble Item And Cache Into Array if($nowPlaying){ $musicOverlay = ($widthOverride == 56) ? ' ' : ''; return '
'.$musicOverlay.''.$item['Name'].'
'.$topTitle.''.$bottomTitle.'
'; }else{ return '
'.$item['Name'].''.$title.'
'; } } //$hideMenu .= '
  • '.$api['title'].'
  • '; //Recent Added function outputRecentAdded($header, $items, $script = false, $array, $type) { $hideMenu = '
    '; // If None Populate Empty Item if (!count($items)) { return '
    '.$header.'

    No Media Found

    '; }else{ $className = str_replace(' ', '', $header.' on '.$type); return '
    '.$header.'
    '.$hideMenu.'


    '.implode('',$items).'
    '.($script?'':''); } } // Create Carousel function outputNowPlaying($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, $showNames, $role) { $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'], true, $showNames, $role, $value); } } return outputNowPlaying(translate('PLAYING_NOW_ON_EMBY')." ( ".count($playingItems)." Streams )", $size, 'streams-emby', $playingItems, ajaxLoop('emby-streams',NOWPLAYINGREFRESH)); } // Get Now Playing Streams From Plex function getPlexStreams($size, $showNames, $role){ $address = qualifyURL(PLEXURL); // Perform API requests $api = @curl_get($address."/status/sessions?X-Plex-Token=".PLEXTOKEN); libxml_use_internal_errors(true); $api = simplexml_load_string($api); if (is_array($api) || is_object($api)){ if (!$api->head->title){ $getServer = simplexml_load_string(@curl_get($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, $role); } return outputNowPlaying(translate('PLAYING_NOW_ON_PLEX')." ( ".count($items)." Streams )", $size, 'streams-plex', $items, ajaxLoop('plex-streams',NOWPLAYINGREFRESH)); }else{ writeLog("error", "PLEX STREAM ERROR: could not connect - check token - if HTTPS, is cert valid"); } }else{ writeLog("error", "PLEX STREAM ERROR: could not connect - check URL - if HTTPS, is cert valid"); } } // Get Recent Content From Emby function getEmbyRecent($array) { $address = qualifyURL(EMBYURL); $header = translate('RECENT_CONTENT'); // Currently Logged In User $username = false; if (isset($GLOBALS['USER'])) { $username = strtolower($GLOBALS['USER']->username); } // 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?EnableImages=false&Limit='.EMBYRECENTITEMS.'&api_key='.EMBYTOKEN.($showPlayed?'':'&IsPlayed=false')),true); // For Each Item In Category $items = array(); foreach ($latest as $k => $v) { $type = (string) $v['Type']; if(@$array[$type] == "true"){ $items[] = resolveEmbyItem($address, EMBYTOKEN, $v, false, false, false); } } $array["movie"] = $array["Movie"]; $array["season"] = $array["Episode"]; $array["album"] = $array["MusicAlbum"]; unset($array["Movie"]); unset($array["Episode"]); unset($array["MusicAlbum"]); unset($array["Series"]); return outputRecentAdded($header, $items, ajaxLoop('emby-recent',RECENTREFRESH,'loadSlick();'), $array, 'Emby'); } // Get Recent Content From Plex function getPlexRecent($array){ $address = qualifyURL(PLEXURL); $header = translate('RECENT_CONTENT'); // Perform Requests $api = @curl_get($address."/library/recentlyAdded?limit=".PLEXRECENTITEMS."&X-Plex-Token=".PLEXTOKEN); libxml_use_internal_errors(true); $api = simplexml_load_string($api); if (is_array($api) || is_object($api)){ if (!$api->head->title){ $getServer = simplexml_load_string(@curl_get($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, false); } } return outputRecentAdded($header, $items, ajaxLoop('plex-recent',RECENTREFRESH,'loadSlick();'), $array, 'Plex'); }else{ writeLog("error", "PLEX RECENT-ITEMS ERROR: could not connect - check token - if HTTPS, is cert valid"); } }else{ writeLog("error", "PLEX RECENT-ITEMS ERROR: could not connect - check URL - if HTTPS, is cert valid"); } } // Get Image From Emby function getEmbyImage() { $refresh = false; $embyAddress = qualifyURL(EMBYURL); if (!file_exists('images/cache')) { mkdir('images/cache', 0777, true); } $itemId = $_GET['img']; $key = $_GET['key']; if(strpos($key, '$') !== false){ $key = explode('$', $key)[0]; $refresh = true; } $itemType = $_GET['type']; $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/'.$itemType.'?'.implode('&', $imgParams); $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) && $refresh !== true) { header("Content-type: image/jpeg"); @readfile($cachefile); exit; } ob_start(); // Start the output buffer header('Content-type: image/jpeg'); //@readfile($image_src); echo @curl_get($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 { debug_out('Invalid Request',1); } } // Get Image From Plex function getPlexImage() { $refresh = false; $plexAddress = qualifyURL(PLEXURL); if (!file_exists('images/cache')) { mkdir('images/cache', 0777, true); } $image_url = $_GET['img']; $key = $_GET['key']; if(strpos($key, '$') !== false){ $key = explode('$', $key)[0]; $refresh = true; } $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) && $refresh == false) { header("Content-type: image/jpeg"); @readfile($cachefile); exit; } ob_start(); // Start the output buffer header('Content-type: image/jpeg'); //@readfile($image_src); echo @curl_get($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); } // Upgrade to 1.40 $config = loadConfig(); if (isset($config['database_Location']) && (!isset($config['CONFIG_VERSION']) || $config['CONFIG_VERSION'] < '1.40')) { // Upgrade database to latest version updateSQLiteDB($config['database_Location'],'1.38'); // Update Version and Commit $config['CONFIG_VERSION'] = '1.40'; copy('config/config.php', 'config/config['.date('Y-m-d_H-i-s').'][1.38].bak.php'); $createConfigSuccess = createConfig($config); unset($config); } // Upgrade to 1.50 $config = loadConfig(); if (isset($config['database_Location']) && (!isset($config['CONFIG_VERSION']) || $config['CONFIG_VERSION'] < '1.50')) { // Upgrade database to latest version updateSQLiteDB($config['database_Location'],'1.40'); // Update Version and Commit $config['CONFIG_VERSION'] = '1.50'; copy('config/config.php', 'config/config['.date('Y-m-d_H-i-s').'][1.40].bak.php'); $createConfigSuccess = createConfig($config); unset($config); } // Upgrade to 1.603 $config = loadConfig(); if (isset($config['database_Location']) && (!isset($config['CONFIG_VERSION']) || $config['CONFIG_VERSION'] < '1.603')) { // Update Version and Commit $config['CONFIG_VERSION'] = '1.603'; copy('config/config.php', 'config/config['.date('Y-m-d_H-i-s').'][1.601].bak.php'); $createConfigSuccess = createConfig($config); unset($config); if(file_exists('org.log')){ copy('org.log', DATABASE_LOCATION.'org.log'); unlink('org.log'); } if(file_exists('loginLog.json')){ copy('loginLog.json', DATABASE_LOCATION.'loginLog.json'); unlink('loginLog.json'); } } return true; } // Get OS from server function getOS(){ if(PHP_SHLIB_SUFFIX == "dll"){ return "win"; }else{ return "nix"; } } //Get Error by Server OS function getError($os, $error){ $ini = (!empty(php_ini_loaded_file()) ? php_ini_loaded_file() : "php.ini"); $ext = (!empty(ini_get('extension_dir')) ? "uncomment ;extension_dir = and make sure it says -> extension_dir = '".ini_get('extension_dir')."'" : "uncomment ;extension_dir = and add path to 'ext' to make it like extension_dir = 'C:\nginx\php\ext'"); $errors = array( 'pdo_sqlite' => array( 'win' => 'PDO:SQLite not enabled, uncomment ;extension=php_pdo_sqlite.dll in the file php.ini | '.$ext, 'nix' => 'PDO:SQLite not enabled, PHP7 -> run sudo apt-get install php7.0-sqlite | PHP5 -> run sudo apt-get install php5-sqlite', ), 'sqlite3' => array( 'win' => 'SQLite3 not enabled, uncomment ;extension=php_sqlite3.dll in the file php.ini | uncomment ;sqlite3.extension_dir = and add "ext" to make it sqlite3.extension_dir = ext', 'nix' => 'SQLite3 not enabled, run sudo apt-get install php-sqlite3', ), 'curl' => array( 'win' => 'cURL not enabled, uncomment ;extension=php_curl.dll in the file php.ini | '.$ext, 'nix' => 'cURL not enabled, PHP7 -> sudo apt-get install php-curl or sudo apt-get install php7.0-curl | PHP5 -> run sudo apt-get install php5.6-curl', ), 'zip' => array( 'win' => 'PHP Zip not enabled, uncomment ;extension=php_zip.dll in the file php.ini, if that doesn\'t work remove that line', 'nix' => 'PHP Zip not enabled, PHP7 -> run sudo apt-get install php7.0-zip | PHP5 -> run sudo apt-get install php5.6-zip', ), ); return (isset($errors[$error][$os]) ? $errors[$error][$os] : 'No Error Info Found'); } // Check if all software dependancies are met function dependCheck() { $output = array(); $i = 1; if (!extension_loaded('pdo_sqlite')) { $output["Step $i"] = getError(getOS(),'pdo_sqlite'); $i++; } if (!extension_loaded('curl')) { $output["Step $i"] = getError(getOS(),'curl'); $i++; } if (!extension_loaded('zip')) { $output["Step $i"] = getError(getOS(),'zip'); $i++; } //if (!extension_loaded('sqlite3')) { $output[] = getError(getOS(),'sqlite3'); } if ($output) { $output["Step $i"] = "Restart PHP and/or Webserver to apply changes"; $i++; $output["Step $i"] = "Please visit here to also check status of necessary components after you fix them: check.php"; $i++; 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!'); } } // Process file uploads function uploadAvatar($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)) { $fileType = getExtension($path); if(in_arrayi($fileType, array('jpg','jpeg','png','json','db','gif'))){ writeLog("success", "file was removed"); unlink($path); } else{ writeLog("error", "file was not removed"); echo json_encode('Invalid File Type'); } } else { writeLog("error", "file 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[] = ''; foreach($value['optgroup'] as $k => $v) { $output[] = ''; } } else { $output[] = ''; } } else { $output[] = ''; } } return implode('',$output); } // Check if user is allowed to continue function qualifyUser($type, $errOnFail = false) { if (!isset($GLOBALS['USER'])) { require_once("user.php"); $GLOBALS['USER'] = new User('registration_callback'); } if (is_bool($type)) { if ($type === true) { $authorized = ($GLOBALS['USER']->authenticated == true); } else { $authorized = true; } } elseif (is_string($type) || is_array($type)) { if ($type !== 'false') { if (!is_array($type)) { $type = explode('|',$type); } $authorized = ($GLOBALS['USER']->authenticated && in_array($GLOBALS['USER']->role,$type)); } else { $authorized = true; } } else { debug_out('Invalid Syntax!',1); } if (!$authorized && $errOnFail) { if ($GLOBALS['USER']->authenticated) { header('Location: '.rtrim(dirname($_SERVER['SCRIPT_NAME']), '/').'/error.php?error=401'); echo ''; } else { header('Location: '.rtrim(dirname($_SERVER['SCRIPT_NAME']), '/').'/error.php?error=999'); echo ''; } debug_out('Not Authorized' ,1); } else { return $authorized; } } // Build an (optionally) tabbed settings page. function buildSettings($array) { /* array( 'title' => '', 'id' => '', 'fields' => array( See buildField() ), 'tabs' => array( array( 'title' => '', 'id' => '', 'image' => '', 'fields' => array( See buildField() ), ), ), ); */ $notifyExplode = explode("-", NOTIFYEFFECT); $fieldFunc = function($fieldArr) { $fields = '
    '; foreach($fieldArr as $key => $value) { $isSingle = isset($value['type']); if ($isSingle) { $value = array($value); } $tmpField = ''; $sizeLg = max(floor(12/count($value)),2); $sizeMd = max(floor(($isSingle?12:6)/count($value)),3); foreach($value as $k => $v) { $tmpField .= buildField($v, 12, $sizeMd, $sizeLg); } $fields .= ($isSingle?$tmpField:'
    '.$tmpField.'
    '); } $fields .= '
    '; return $fields; }; $fields = (isset($array['fields'])?$fieldFunc($array['fields']):''); $tabSelectors = array(); $tabContent = array(); if (isset($array['tabs'])) { foreach($array['tabs'] as $key => $value) { $id = (isset($value['id'])?$value['id']:randString(32)); $tabSelectors[$key] = '
  • '; $tabContent[$key] = '
    '.$fieldFunc($value['fields']).'
    '; } } $pageID = (isset($array['id'])?$array['id']:str_replace(array(' ','"',"'"),array('_'),strtolower($array['id']))); $extraClick = ($pageID == 'appearance_settings' ? "$('#advanced_settings_form_submit').click();console.log('add theme settings');" : ""); return '

    '.$array['title'].'

    '; } // Build Settings Fields function buildField($params, $sizeSm = 12, $sizeMd = 12, $sizeLg = 12) { /* array( 'type' => '', 'placeholder' => '', 'label' => '', 'labelTranslate' => '', 'assist' => '', 'name' => '', 'pattern' => '', 'options' => array( // For SELECT only 'Display' => 'value', ), ) */ // Tags $tags = array(); foreach(array('placeholder','style','disabled','readonly','pattern','min','max','required','onkeypress','onchange','onfocus','onleave','href','onclick','autocomplete') as $value) { if (isset($params[$value])) { if (is_string($params[$value])) { $tags[] = $value.'="'.$params[$value].'"'; } else if ($params[$value] === true) { $tags[] = $value; } } } $format = (isset($params['format']) && in_array($params['format'],array(false,'colour','color'))?$params['format']:false); $name = (isset($params['name'])?$params['name']:(isset($params['id'])?$params['id']:'')); $id = (isset($params['id'])?$params['id']:(isset($params['name'])?$params['name'].'_id':randString(32))); $val = (isset($params['value'])?$params['value']:''); $class = (isset($params['class'])?' '.$params['class']:''); $wrapClass = (isset($params['wrapClass'])?$params['wrapClass']:'form-content'); $assist = (isset($params['assist'])?' - i.e. '.$params['assist']:''); $label = (isset($params['labelTranslate'])?translate($params['labelTranslate']):(isset($params['label'])?$params['label']:'')); $labelOut = '

    '.$label.$assist.'

    '; // Field Design switch ($params['type']) { case 'text': case 'number': case 'password': $field = ''; break; case 'select': case 'dropdown': $field = ''; break; case 'select-multi': case 'dropdown-multi': $field = ''; break; case 'check': case 'checkbox': case 'toggle': $checked = ((is_bool($val) && $val) || trim($val) === 'true'?' checked':''); $colour = (isset($params['colour'])?$params['colour']:'success'); $labelOut = ''.$label; $field = ''; break; case 'radio': $labelOut = ''; $checked = ((is_bool($val) && $val) || ($val && trim($val) !== 'false')?' checked':''); $bType = (isset($params['buttonType'])?$params['buttonType']:'success'); $field = '
    '; break; case 'date': $field = 'Unsupported, planned.'; break; case 'hidden': return ''; break; case 'header': $labelOut = ''; $headType = (isset($params['value'])?$params['value']:3); $field = ''.$label.''; break; case 'button': $labelOut = ''; $icon = (isset($params['icon'])?$params['icon']:'flask'); $bType = (isset($params['buttonType'])?$params['buttonType']:'success'); $bDropdown = (isset($params['buttonDrop'])?$params['buttonDrop']:''); $field = ($bDropdown?'
    ':'').''.($bDropdown?$bDropdown.'
    ':''); break; case 'textarea': $rows = (isset($params['rows'])?$params['rows']:5); $field = ''; break; case 'custom': // Settings $settings = array( '$id' => $id, '$name' => $name, '$val' => $val, '$label' => $label, '$labelOut' => $labelOut, ); // Get HTML $html = (isset($params['html'])?$params['html']:'Nothing Specified!'); // If LabelOut is in html dont print it twice $labelOut = (strpos($html,'$label')!==false?'':$labelOut); // Replace variables in settings $html = preg_replace_callback('/\$\w+\b/', function ($match) use ($settings) { return (isset($settings[$match[0]])?$settings[$match[0]]:'{'.$match[0].' is undefined}'); }, $html); // Build Field $field = '
    '.$html.'
    '; break; case 'space': $labelOut = ''; $field = str_repeat('
    ', (isset($params['value'])?$params['value']:1)); break; default: $field = 'Unsupported field type'; break; } // Field Formats switch ($format) { case 'colour': // Fuckin Eh, Canada! case 'color': $labelBef = '
    '.$label.'
    '; $wrapClass = 'gray-bg colour-field'; $labelAft = ''; $field = str_replace(' material input-sm','',$field); break; default: $labelBef = ''; $labelAft = $labelOut; } return '
    '.$labelBef.$field.$labelAft.'
    '; } // Tab Settings Generation function printTabRow($data) { $hidden = false; if ($data===false) { $hidden = true; $data = array( // New Tab Defaults 'id' => 'new', 'name' => '', 'url' => '', 'icon' => 'fa-diamond', 'iconurl' => '', 'active' => 'true', 'user' => 'true', 'guest' => 'true', 'window' => 'false', 'splash' => 'true', 'ping' => 'false', 'ping_url' => '', 'defaultz' => '', ); } $image = ''; $output = '
  • '.buildField(array( 'type' => 'custom', 'html' => '', ),12,1,1).' '.buildField(array( 'type' => 'hidden', 'id' => 'tab-'.$data['id'].'-id', 'name' => 'id['.$data['id'].']', 'value' => $data['id'], ),12,2,1).' '.buildField(array( 'type' => 'text', 'id' => 'tab-'.$data['id'].'-name', 'name' => 'name['.$data['id'].']', 'required' => true, 'placeholder' => 'Organizr Homepage', 'labelTranslate' => 'TAB_NAME', 'value' => $data['name'], 'class' => 'darkBold', ),12,2,1).' '.buildField(array( 'type' => 'text', 'id' => 'tab-'.$data['id'].'-url', 'name' => 'url['.$data['id'].']', 'required' => true, 'placeholder' => 'homepage.php', 'labelTranslate' => 'TAB_URL', 'value' => $data['url'], 'class' => 'darkBold', ),12,2,2).' '.buildField(array( 'type' => 'text', 'id' => 'tab-'.$data['id'].'-iconurl', 'name' => 'iconurl['.$data['id'].']', 'placeholder' => 'images/organizr.png', 'labelTranslate' => 'ICON_URL', 'value' => $data['iconurl'], 'class' => 'darkBold', ),12,2,1).' '.buildField(array( 'type' => 'text', 'id' => 'tab-'.$data['id'].'-icon', 'name' => 'icon['.$data['id'].']', 'placeholder' => 'fa-icon', 'labelTranslate' => 'OR_ICON_NAME', 'value' => $data['icon'], 'class' => 'iconpickeradd darkBold', ),12,1,1).' '.buildField(array( 'type' => 'text', 'id' => 'tab-'.$data['id'].'-ping_url', 'name' => 'ping_url['.$data['id'].']', 'placeholder' => 'host:port', 'labelTranslate' => 'PING_URL', 'value' => $data['ping_url'], 'class' => 'darkBold', ),12,2,1).' '.buildField(array( 'type' => 'radio', 'labelTranslate' => 'DEFAULT', 'name' => 'defaultz['.$data['id'].']', 'value' => $data['defaultz'], 'onclick' => "$('[type=radio][id!=\''+this.id+'\']').each(function() { this.checked=false; });", ),12,1,1).' '.buildField(array( 'type' => 'button', 'icon' => 'chevron-down', 'buttonType' => 'success', 'labelTranslate' => 'MORE', 'onclick' => "$(this).parent().parent().parent().find('.slideInUp').toggle()", 'class' => 'toggleTabExtra', ),12,1,1).' '.buildField(array( 'type' => 'button', 'icon' => 'trash', 'buttonType' => 'danger', 'labelTranslate' => 'REMOVE', 'onclick' => "$(this).parents('li').remove();", ),12,1,1).'
  • '; return $output; } // Timezone array function timezoneOptions() { $output = array(); $timezones = array(); $regions = array( 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Arctic' => DateTimeZone::ARCTIC, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC ); foreach ($regions as $name => $mask) { $zones = DateTimeZone::listIdentifiers($mask); foreach($zones as $timezone) { $time = new DateTime(NULL, new DateTimeZone($timezone)); $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : ''; $output[$name]['optgroup'][substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm]['value'] = $timezone; } } return $output; } // Build Database function createSQLiteDB($path = false) { if ($path === false) { if (DATABASE_LOCATION){ $path = DATABASE_LOCATION; } else { debug_out('No Path Specified!'); } } if (!is_file($path.'users.db') || filesize($path.'users.db') <= 0) { if (!isset($GLOBALS['file_db'])) { $GLOBALS['file_db'] = new PDO('sqlite:'.$path.'users.db'); $GLOBALS['file_db']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } // Create Users $users = $GLOBALS['file_db']->query('CREATE TABLE `users` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, `username` TEXT UNIQUE, `password` TEXT, `email` TEXT, `token` TEXT, `role` TEXT, `active` TEXT, `last` TEXT, `auth_service` TEXT DEFAULT \'internal\' );'); // Create Tabs $tabs = $GLOBALS['file_db']->query('CREATE TABLE `tabs` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, `order` INTEGER, `users_id` INTEGER, `name` TEXT, `url` TEXT, `defaultz` TEXT, `active` TEXT, `user` TEXT, `guest` TEXT, `icon` TEXT, `iconurl` TEXT, `window` TEXT, `splash` TEXT, `ping` TEXT, `ping_url` TEXT );'); // Create Options $options = $GLOBALS['file_db']->query('CREATE TABLE `options` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, `users_id` INTEGER UNIQUE, `title` TEXT UNIQUE, `topbar` TEXT, `bottombar` TEXT, `sidebar` TEXT, `hoverbg` TEXT, `topbartext` TEXT, `activetabBG` TEXT, `activetabicon` TEXT, `activetabtext` TEXT, `inactiveicon` TEXT, `inactivetext` TEXT, `loading` TEXT, `hovertext` TEXT );'); // Create Invites $invites = $GLOBALS['file_db']->query('CREATE TABLE `invites` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, `code` TEXT UNIQUE, `date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `email` TEXT, `username` TEXT, `dateused` TIMESTAMP, `usedby` TEXT, `ip` TEXT, `valid` TEXT );'); writeLog("success", "database created/saved"); return $users && $tabs && $options && $invites; } else { writeLog("error", "database was unable to be created/saved"); return false; } } // Upgrade Database function updateSQLiteDB($db_path = false, $oldVerNum = false) { if (!$db_path) { if (defined('DATABASE_LOCATION')) { $db_path = DATABASE_LOCATION; } else { debug_out('No Path Specified',1); } } if (!isset($GLOBALS['file_db'])) { $GLOBALS['file_db'] = new PDO('sqlite:'.$db_path.'users.db'); $GLOBALS['file_db']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } // Cache current DB $cache = array(); foreach($GLOBALS['file_db']->query('SELECT name FROM sqlite_master WHERE type="table";') as $table) { foreach($GLOBALS['file_db']->query('SELECT * FROM '.$table['name'].';') as $key => $row) { foreach($row as $k => $v) { if (is_string($k)) { $cache[$table['name']][$key][$k] = $v; } } } } // Remove Current Database $GLOBALS['file_db'] = null; $pathDigest = pathinfo($db_path.'users.db'); if (file_exists($db_path.'users.db')) { rename($db_path.'users.db', $pathDigest['dirname'].'/'.$pathDigest['filename'].'['.date('Y-m-d_H-i-s').']'.($oldVerNum?'['.$oldVerNum.']':'').'.bak.db'); } // Create New Database $success = createSQLiteDB($db_path); // Restore Items if ($success) { foreach($cache as $table => $tableData) { if ($tableData) { $queryBase = 'INSERT INTO '.$table.' (`'.implode('`,`',array_keys(current($tableData))).'`) values '; $insertValues = array(); reset($tableData); foreach($tableData as $key => $value) { $insertValues[] = '('.implode(',',array_map(function($d) { return (isset($d)?$GLOBALS['file_db']->quote($d):'null'); }, $value)).')'; } $GLOBALS['file_db']->query($queryBase.implode(',',$insertValues).';'); } } writeLog("success", "database values have been updated"); return true; } else { writeLog("error", "database values unable to be updated"); return false; } } // Commit colours to database function updateDBOptions($values) { if (!isset($GLOBALS['file_db'])) { $GLOBALS['file_db'] = new PDO('sqlite:'.DATABASE_LOCATION.'users.db'); $GLOBALS['file_db']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } // Commit new values to database if ($GLOBALS['file_db']->query('UPDATE options SET '.implode(',',array_map(function($d, $k) { return '`'.$k.'` = '.(isset($d)?"'".addslashes($d)."'":'null'); }, $values, array_keys($values))).';')->rowCount()) { return true; } else if ($GLOBALS['file_db']->query('INSERT OR IGNORE INTO options (`'.implode('`,`',array_keys($values)).'`) VALUES (\''.implode("','",$values).'\');')->rowCount()) { writeLog("success", "database values for options table have been updated"); return true; } else { writeLog("error", "database values for options table unable to be updated"); return false; } } // Send AJAX notification function sendNotification($success, $message = false, $send = true) { $notifyExplode = explode("-", NOTIFYEFFECT); if ($success) { $msg = array( 'html' => ($message?''.$message:''.translate("SETTINGS_SAVED").''), 'icon' => 'floppy-o', 'type' => 'success', 'length' => '5000', 'layout' => $notifyExplode[0], 'effect' => $notifyExplode[1], ); } else { $msg = array( 'html' => ($message?''.$message:''.translate("SETTINGS_NOT_SAVED").''), 'icon' => 'floppy-o', 'type' => 'failed', 'length' => '5000', 'layout' => $notifyExplode[0], 'effect' => $notifyExplode[1], ); } // Send and kill script? if ($send) { header('Content-Type: application/json'); echo json_encode(array('notify'=>$msg)); die(); } return $msg; } // Load colours from the database function loadAppearance() { // Defaults $defaults = array( 'title' => 'Organizr', 'topbartext' => '#66D9EF', 'topbar' => '#333333', 'bottombar' => '#333333', 'sidebar' => '#393939', 'hoverbg' => '#AD80FD', 'activetabBG' => '#F92671', 'activetabicon' => '#FFFFFF', 'activetabtext' => '#FFFFFF', 'inactiveicon' => '#66D9EF', 'inactivetext' => '#66D9EF', 'loading' => '#66D9EF', 'hovertext' => '#000000', ); if (DATABASE_LOCATION) { if(is_file(DATABASE_LOCATION.'users.db') && filesize(DATABASE_LOCATION.'users.db') > 0){ if (!isset($GLOBALS['file_db'])) { $GLOBALS['file_db'] = new PDO('sqlite:'.DATABASE_LOCATION.'users.db'); $GLOBALS['file_db']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } // Database Lookup $options = $GLOBALS['file_db']->query('SELECT * FROM options'); // Replace defaults with filled options foreach($options as $row) { foreach($defaults as $key => $value) { if (isset($row[$key]) && $row[$key]) { $defaults[$key] = $row[$key]; } } } } } // Return the Results return $defaults; } // Delete Database function deleteDatabase() { unset($_COOKIE['Organizr']); setcookie('Organizr', '', time() - 3600, '/'); unset($_COOKIE['OrganizrU']); setcookie('OrganizrU', '', time() - 3600, '/'); $GLOBALS['file_db'] = null; unlink(DATABASE_LOCATION.'users.db'); foreach(glob(substr_replace($userdirpath, "", -1).'/*') as $file) { if(is_dir($file)) { rmdir($file); } elseif (!is_dir($file)) { unlink($file); } } rmdir($userdirpath); writeLog("success", "database has been deleted"); return true; } // Upgrade the installation function upgradeInstall($branch = 'v1-master') { if ($branch == 'master'){ $branch = 'v1-master'; } if ($branch == 'develop'){ $branch = 'v1-develop'; } if ($branch == 'cero-dev'){ $branch = 'v1-develop'; } function downloadFile($url, $path){ ini_set('max_execution_time',0); $folderPath = "upgrade/"; if(!mkdir($folderPath)){ writeLog("error", "organizr could not create upgrade folder"); } $newfname = $folderPath . $path; $file = fopen ($url, 'rb'); if ($file) { $newf = fopen ($newfname, 'wb'); if ($newf) { while(!feof($file)) { fwrite($newf, fread($file, 1024 * 8), 1024 * 8); } } }else{ writeLog("error", "organizr could not download $url"); } if ($file) { fclose($file); writeLog("success", "organizr finished downloading the github zip file"); }else{ writeLog("error", "organizr could not download the github zip file"); } if ($newf) { fclose($newf); writeLog("success", "organizr created upgrade zip file from github zip file"); }else{ writeLog("error", "organizr could not create upgrade zip file from github zip file"); } } function unzipFile($zipFile){ $zip = new ZipArchive; $extractPath = "upgrade/"; if($zip->open($extractPath . $zipFile) != "true"){ writeLog("error", "organizr could not unzip upgrade.zip"); }else{ writeLog("success", "organizr unzipped upgrade.zip"); } /* Extract Zip File */ $zip->extractTo($extractPath); $zip->close(); } // Function to remove folders and files function rrmdir($dir) { if (is_dir($dir)) { $files = scandir($dir); foreach ($files as $file) if ($file != "." && $file != "..") rrmdir("$dir/$file"); rmdir($dir); } else if (file_exists($dir)) unlink($dir); } // Function to Copy folders and files function rcopy($src, $dst) { if (is_dir ( $src )) { if (!file_exists($dst)) : mkdir ( $dst ); endif; $files = scandir ( $src ); foreach ( $files as $file ) if ($file != "." && $file != "..") rcopy ( "$src/$file", "$dst/$file" ); } else if (file_exists ( $src )) copy ( $src, $dst ); } $url = 'https://github.com/causefx/Organizr/archive/'.$branch.'.zip'; $file = "upgrade.zip"; $source = __DIR__ . '/upgrade/Organizr-'.$branch.'/'; $cleanup = __DIR__ . "/upgrade/"; $destination = __DIR__ . "/"; writeLog("success", "starting organizr upgrade process"); downloadFile($url, $file); unzipFile($file); rcopy($source, $destination); writeLog("success", "new organizr files copied"); rrmdir($cleanup); writeLog("success", "organizr upgrade folder removed"); writeLog("success", "organizr has been updated"); return true; } // Transmission Items function transmissionConnect($list = 'listgroups') { $url = qualifyURL(TRANSMISSIONURL); $digest = parse_url($url); $scheme = (isset($digest['scheme'])) ? $digest['scheme'].'://' : 'http://'; $host = (isset($digest['host'])) ? $digest['host'] : ''; $port = (isset($digest['port'])) ? ':'.$digest['port'] : ''; $path = (isset($digest['path'])) ? $digest['path'] : ''; $passwordInclude = (TRANSMISSIONUSERNAME != '' && TRANSMISSIONPASSWORD != '') ? TRANSMISSIONUSERNAME.':'.TRANSMISSIONPASSWORD."@" : ''; $url = $scheme.$passwordInclude.$host.$port.$path.'/rpc'; $contextopts = array( 'http' => array( 'user_agent' => 'HTTP_UA', 'ignore_errors' => true, ) ); $context = stream_context_create( $contextopts ); $fp = @fopen( $url, 'r', false, $context ); $stream_meta = stream_get_meta_data( $fp ); fclose( $fp ); foreach( $stream_meta['wrapper_data'] as $header ){ if( strpos( $header, 'X-Transmission-Session-Id: ' ) === 0 ){ $session_id = trim( substr( $header, 27 ) ); break; } } $headers = array( 'X-Transmission-Session-Id' => $session_id, 'Content-Type' => 'application/json' ); $data = array( 'method' => 'torrent-get', 'arguments' => array( 'fields' => array( "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir" ), ), 'tags' => '' ); $api = curl_post($url, $data, $headers); $api = json_decode($api['content'], true); $gotTorrent = array(); if (is_array($api) || is_object($api)){ foreach ($api['arguments']['torrents'] AS $child) { $downloadName = htmlentities($child['name'], ENT_QUOTES); $downloadDirectory = $child['downloadDir']; $downloadPercent = $child['percentDone'] * 100; $progressBar = "progress-bar-striped active"; if($child['status'] == "6"){ $downloadStatus = "Seeding"; $downloadHealth = "success"; }elseif($child['status'] == "4"){ $downloadStatus = "Downloading"; $downloadHealth = "danger"; }elseif($child['status'] == "3"){ $downloadStatus = "Queued"; $downloadHealth = "warning"; }elseif($child['status'] == "0"){ $downloadStatus = "Complete"; $downloadHealth = "success"; } $gotTorrent[] = ' '.$downloadName.' '.$downloadStatus.' '.$downloadDirectory.' '.realSize($child['totalSize']).'

    '.round($downloadPercent).'%

    '.$downloadPercent.'% Complete
    '; } if ($gotTorrent) { return implode('',$gotTorrent); } else { return '

    No Results

    '; } }else{ writeLog("error", "TRANSMISSION ERROR: could not connect - check URL and/or check token and/or Username and Password - if HTTPS, is cert valid"); } } // NzbGET Items function nzbgetConnect($list = 'listgroups') { $url = qualifyURL(NZBGETURL); $api = curl_get($url.'/'.NZBGETUSERNAME.':'.NZBGETPASSWORD.'/jsonrpc/'.$list); $api = json_decode($api, true); $gotNZB = array(); if (is_array($api) || is_object($api)){ foreach ($api['result'] AS $child) { $downloadName = htmlentities($child['NZBName'], ENT_QUOTES); $downloadStatus = $child['Status']; $downloadCategory = $child['Category']; if($list == "history"){ $downloadPercent = "100"; $progressBar = ""; } if($list == "listgroups"){ $downloadPercent = (($child['FileSizeMB'] - $child['RemainingSizeMB']) / $child['FileSizeMB']) * 100; $progressBar = "progress-bar-striped active"; } if($child['Health'] <= "750"){ $downloadHealth = "danger"; }elseif($child['Health'] <= "900"){ $downloadHealth = "warning"; }elseif($child['Health'] <= "1000"){ $downloadHealth = "success"; } $gotNZB[] = ' '.$downloadName.' '.$downloadStatus.' '.$downloadCategory.' '.realSize(($child['FileSizeMB']*1024)*1024).'

    '.round($downloadPercent).'%

    '.$downloadPercent.'% Complete
    '; } if ($gotNZB) { return implode('',$gotNZB); } else { return '

    No Results

    '; } }else{ writeLog("error", "NZBGET ERROR: could not connect - check URL and/or check token and/or Username and Password - if HTTPS, is cert valid"); } } // Sabnzbd Items function sabnzbdConnect($list = 'queue') { $url = qualifyURL(SABNZBDURL); $api = @file_get_contents($url.'/api?mode='.$list.'&output=json&apikey='.SABNZBDKEY); $api = json_decode($api, true); $gotNZB = array(); if (is_array($api) || is_object($api)){ foreach ($api[$list]['slots'] AS $child) { if($list == "queue"){ $downloadName = $child['filename']; $downloadCategory = $child['cat']; $downloadPercent = (($child['mb'] - $child['mbleft']) / $child['mb']) * 100; $progressBar = "progress-bar-striped active"; } if($list == "history"){ $downloadName = $child['name']; $downloadCategory = $child['category']; $downloadPercent = "100"; $progressBar = ""; } $downloadStatus = $child['status']; $gotNZB[] = ' '.$downloadName.' '.$downloadStatus.' '.$downloadCategory.' '.$child['size'].'

    '.round($downloadPercent).'%

    '.$downloadPercent.'% Complete
    '; } if ($gotNZB) { return implode('',$gotNZB); } else { return '

    No Results

    '; } }else{ writeLog("error", "SABNZBD ERROR: could not connect - check URL and/or check token - if HTTPS, is cert valid"); } } // Apply new tab settings function updateTabs($tabs) { if (!isset($GLOBALS['file_db'])) { $GLOBALS['file_db'] = new PDO('sqlite:'.DATABASE_LOCATION.'users.db'); $GLOBALS['file_db']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } // Validate if (!isset($tabs['defaultz'])) { $tabs['defaultz'][current(array_keys($tabs['name']))] = 'true'; } if (isset($tabs['name']) && isset($tabs['url']) && is_array($tabs['name'])) { // Clear Existing Tabs $GLOBALS['file_db']->query("DELETE FROM tabs"); // Process New Tabs $totalValid = 0; foreach ($tabs['name'] as $key => $value) { // Qualify if (!$value || !isset($tabs['url']) || !$tabs['url'][$key]) { continue; } $totalValid++; $fields = array(); foreach(array('id','name','url','icon','iconurl','order','ping_url') as $v) { if (isset($tabs[$v]) && isset($tabs[$v][$key])) { $fields[$v] = $tabs[$v][$key]; } } foreach(array('active','user','guest','defaultz','window','splash','ping') as $v) { if (isset($tabs[$v]) && isset($tabs[$v][$key])) { $fields[$v] = ($tabs[$v][$key]!=='false'?'true':'false'); } } $GLOBALS['file_db']->query('INSERT INTO tabs (`'.implode('`,`',array_keys($fields)).'`) VALUES (\''.implode("','",$fields).'\');'); } writeLog("success", "tabs successfully saved"); return $totalValid; } else { writeLog("error", "tabs could not save"); return false; } writeLog("error", "tabs could not save"); return false; } // ============== function clean($strin) { $strout = null; for ($i = 0; $i < strlen($strin); $i++) { $ord = ord($strin[$i]); if (($ord > 0 && $ord < 32) || ($ord >= 127)) { $strout .= "&#{$ord};"; } else { switch ($strin[$i]) { case '<': $strout .= '<'; break; case '>': $strout .= '>'; break; case '&': $strout .= '&'; break; case '"': $strout .= '"'; break; default: $strout .= $strin[$i]; } } } return $strout; } function registration_callback($username, $email, $userdir){ global $data; $data = array($username, $email, $userdir); } function printArray($arrayName){ $messageCount = count($arrayName); $i = 0; foreach ( $arrayName as $item ) : $i++; if($i < $messageCount) : echo "" . $item . " & "; elseif($i = $messageCount) : echo "" . $item . ""; endif; endforeach; } function write_ini_file($content, $path) { if (!$handle = fopen($path, 'w')) { return false; } $success = fwrite($handle, trim($content)); fclose($handle); return $success; } function gotTimezone(){ $regions = array( 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Arctic' => DateTimeZone::ARCTIC, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC ); $timezones = array(); foreach ($regions as $name => $mask) { $zones = DateTimeZone::listIdentifiers($mask); foreach($zones as $timezone) { $time = new DateTime(NULL, new DateTimeZone($timezone)); $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : ''; $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm; } } print ''; } function getTimezone(){ $regions = array( 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Arctic' => DateTimeZone::ARCTIC, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC ); $timezones = array(); foreach ($regions as $name => $mask) { $zones = DateTimeZone::listIdentifiers($mask); foreach($zones as $timezone) { $time = new DateTime(NULL, new DateTimeZone($timezone)); $ampm = $time->format('H') > 12 ? ' ('. $time->format('g:i a'). ')' : ''; $timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' - ' . $time->format('H:i') . $ampm; } } print ''; } function explosion($string, $position){ $getWord = explode("|", $string); return $getWord[$position]; } function getServerPath() { if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https"){ $protocol = "https://"; }elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { $protocol = "https://"; } else { $protocol = "http://"; } $domain = ''; if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false){ $domain = $_SERVER['SERVER_NAME']; }elseif(isset($_SERVER['HTTP_HOST'])){ if (strpos($_SERVER['HTTP_HOST'], ':') !== false) { $domain = explode(':', $_SERVER['HTTP_HOST'])[0]; $port = explode(':', $_SERVER['HTTP_HOST'])[1]; if ($port == "80" || $port == "443"){ $domain = $domain; }else{ $domain = $_SERVER['HTTP_HOST']; } }else{ $domain = $_SERVER['HTTP_HOST']; } } return $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI'])); } function get_browser_name() { $user_agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera'; elseif (strpos($user_agent, 'Edge')) return 'Edge'; elseif (strpos($user_agent, 'Chrome')) return 'Chrome'; elseif (strpos($user_agent, 'Safari')) return 'Safari'; elseif (strpos($user_agent, 'Firefox')) return 'Firefox'; elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Internet Explorer'; return 'Other'; } function getSickrageCalendarWanted($array){ $array = json_decode($array, true); //$gotCalendar = ""; $gotCalendar = array(); $i = 0; foreach($array['data']['missed'] AS $child) { $i++; $seriesName = $child['show_name']; $episodeID = $child['tvdbid']; $episodeAirDate = $child['airdate']; $episodeAirDateTime = explode(" ",$child['airs']); $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2])); $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime); $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate); if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; } $downloaded = "0"; if($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; } //$gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded tvID--$episodeID\", imagetype: \"tv\" }, \n"; array_push($gotCalendar, array( "id" => "Sick-Miss-".$i, "title" => $seriesName, "start" => $episodeAirDate, "className" => $downloaded." tvID--".$episodeID, "imagetype" => "tv", )); } foreach($array['data']['today'] AS $child) { $i++; $seriesName = $child['show_name']; $episodeID = $child['tvdbid']; $episodeAirDate = $child['airdate']; $episodeAirDateTime = explode(" ",$child['airs']); $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2])); $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime); $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate); if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; } $downloaded = "0"; if($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; } //$gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded tvID--$episodeID\", imagetype: \"tv\" }, \n"; array_push($gotCalendar, array( "id" => "Sick-Today-".$i, "title" => $seriesName, "start" => $episodeAirDate, "className" => $downloaded." tvID--".$episodeID, "imagetype" => "tv", )); } foreach($array['data']['soon'] AS $child) { $i++; $seriesName = $child['show_name']; $episodeID = $child['tvdbid']; $episodeAirDate = $child['airdate']; $episodeAirDateTime = explode(" ",$child['airs']); $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2])); $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime); $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate); if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; } $downloaded = "0"; if($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; } //$gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded tvID--$episodeID\", imagetype: \"tv\" }, \n"; array_push($gotCalendar, array( "id" => "Sick-Soon-".$i, "title" => $seriesName, "start" => $episodeAirDate, "className" => $downloaded." tvID--".$episodeID, "imagetype" => "tv", )); } foreach($array['data']['later'] AS $child) { $i++; $seriesName = $child['show_name']; $episodeID = $child['tvdbid']; $episodeAirDate = $child['airdate']; $episodeAirDateTime = explode(" ",$child['airs']); $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2])); $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime); $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate); if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; } $downloaded = "0"; if($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; } //$gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded tvID--$episodeID\", imagetype: \"tv\" }, \n"; array_push($gotCalendar, array( "id" => "Sick-Later-".$i, "title" => $seriesName, "start" => $episodeAirDate, "className" => $downloaded." tvID--".$episodeID, "imagetype" => "tv", )); } if ($i != 0){ return $gotCalendar; } } function getSickrageCalendarHistory($array){ $array = json_decode($array, true); //$gotCalendar = ""; $gotCalendar = array(); $i = 0; foreach($array['data'] AS $child) { $i++; $seriesName = $child['show_name']; $episodeID = $child['tvdbid']; $episodeAirDate = $child['date']; $downloaded = "green-bg"; //$gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded tvID--$episodeID\", imagetype: \"tv\" }, \n"; array_push($gotCalendar, array( "id" => "Sick-History-".$i, "title" => $seriesName, "start" => $episodeAirDate, "className" => $downloaded." tvID--".$episodeID, "imagetype" => "tv", )); } if ($i != 0){ return $gotCalendar; } } function getSonarrCalendar($array){ $array = json_decode($array, true); //$gotCalendar = ""; $gotCalendar = array(); $i = 0; foreach($array AS $child) { $i++; $seriesName = $child['series']['title']; $episodeID = $child['series']['tvdbId']; if(!isset($episodeID)){ $episodeID = ""; } $episodeName = htmlentities($child['title'], ENT_QUOTES); if($child['episodeNumber'] == "1"){ $episodePremier = "true"; }else{ $episodePremier = "false"; } $episodeAirDate = $child['airDateUtc']; $episodeAirDate = strtotime($episodeAirDate); $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate); if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; } $downloaded = $child['hasFile']; $monitored = $child['monitored']; if($downloaded == "0" && isset($unaired) && $episodePremier == "true"){ $downloaded = "light-blue-bg"; }elseif(isset($unaired) && $monitored == "0"){ $downloaded = "light-gray-bg"; }elseif($downloaded == "0" && isset($unaired)){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg";}else{ $downloaded = "red-bg"; } //$gotCalendar .= "{ title: \"$seriesName\", start: \"$episodeAirDate\", className: \"$downloaded tvID--$episodeID\", imagetype: \"tv\" }, \n"; array_push($gotCalendar, array( "id" => "Sonarr-".$i, "title" => $seriesName, "start" => $episodeAirDate, "className" => $downloaded." tvID--".$episodeID, "imagetype" => "tv", )); } if ($i != 0){ return $gotCalendar; } } function getCouchCalendar(){ $url = qualifyURL(COUCHURL); $api = curl_get($url."/api/".COUCHAPI."/media.list"); $api = json_decode($api, true); $i = 0; $gotCalendar = array(); if (is_array($api) || is_object($api)){ foreach($api['movies'] AS $child) { if($child['status'] == "active" || $child['status'] == "done" ){ $i++; $movieName = $child['info']['original_title']; $movieID = $child['info']['tmdb_id']; if(!isset($movieID)){ $movieID = ""; } $physicalRelease = (isset($child['info']['released']) ? $child['info']['released'] : null); $backupRelease = (isset($child['info']['release_date']['theater']) ? $child['info']['release_date']['theater'] : null); $physicalRelease = (isset($physicalRelease) ? $physicalRelease : $backupRelease); $physicalRelease = strtotime($physicalRelease); $physicalRelease = date("Y-m-d", $physicalRelease); if (new DateTime() < new DateTime($physicalRelease)) { $notReleased = "true"; }else{ $notReleased = "false"; } $downloaded = ($child['status'] == "active") ? "0" : "1"; if($downloaded == "0" && $notReleased == "true"){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg"; }else{ $downloaded = "red-bg"; } array_push($gotCalendar, array( "id" => "CouchPotato-".$i, "title" => $movieName, "start" => $physicalRelease, "className" => $downloaded." movieID--".$movieID, "imagetype" => "film", )); } } if ($i != 0){ return $gotCalendar; } }else{ writeLog("error", "CouchPotato ERROR: could not connect - check URL and/or check API key - if HTTPS, is cert valid"); } } function getRadarrCalendar($array){ $array = json_decode($array, true); $gotCalendar = array(); $i = 0; foreach($array AS $child) { if(isset($child['physicalRelease'])){ $i++; $movieName = $child['title']; $movieID = $child['tmdbId']; if(!isset($movieID)){ $movieID = ""; } $physicalRelease = $child['physicalRelease']; $physicalRelease = strtotime($physicalRelease); $physicalRelease = date("Y-m-d", $physicalRelease); if (new DateTime() < new DateTime($physicalRelease)) { $notReleased = "true"; }else{ $notReleased = "false"; } $downloaded = $child['hasFile']; if($downloaded == "0" && $notReleased == "true"){ $downloaded = "indigo-bg"; }elseif($downloaded == "1"){ $downloaded = "green-bg"; }else{ $downloaded = "red-bg"; } array_push($gotCalendar, array( "id" => "Radarr-".$i, "title" => $movieName, "start" => $physicalRelease, "className" => $downloaded." movieID--".$movieID, "imagetype" => "film", )); } } if ($i != 0){ return $gotCalendar; } } function getHeadphonesCalendar($url, $key, $list){ $url = qualifyURL(HEADPHONESURL); $api = curl_get($url."/api?apikey=".$key."&cmd=$list"); $api = json_decode($api, true); $i = 0; //$gotCalendar = ""; $gotCalendar = array();; if (is_array($api) || is_object($api)){ foreach($api AS $child) { if($child['Status'] == "Wanted" && $list == "getWanted" && $child['ReleaseDate']){ $i++; $albumName = addslashes($child['AlbumTitle']); $albumArtist = htmlentities($child['ArtistName'], ENT_QUOTES); $albumDate = (strlen($child['ReleaseDate']) > 4) ? $child['ReleaseDate'] : $child['ReleaseDate']."-01-01"; $albumID = $child['AlbumID']; $albumDate = strtotime($albumDate); $albumDate = date("Y-m-d", $albumDate); $albumStatus = $child['Status']; if (new DateTime() < new DateTime($albumDate)) { $notReleased = "true"; }else{ $notReleased = "false"; } if($albumStatus == "Wanted" && $notReleased == "true"){ $albumStatusColor = "indigo-bg"; }elseif($albumStatus == "Downloaded"){ $albumStatusColor = "green-bg"; }else{ $albumStatusColor = "red-bg"; } //$gotCalendar .= "{ title: \"$albumArtist - $albumName\", start: \"$albumDate\", className: \"$albumStatusColor\", imagetype: \"music\", url: \"https://musicbrainz.org/release-group/$albumID\" }, \n"; array_push($gotCalendar, array( "id" => "Headphones-".$i, "title" => $albumArtist.' - '.$albumName, "start" => $albumDate, "className" => $albumStatusColor, "imagetype" => "music", 'url' => "https://musicbrainz.org/release-group/".$albumID, )); } if($child['Status'] == "Processed" && $list == "getHistory"){ $i++; $find = array('_','[', ']', '\n'); $replace = array(' ','(', ')', ' '); $albumName = addslashes(str_replace($find,$replace,$child['FolderName'])); $albumDate = $child['DateAdded']; $albumID = $child['AlbumID']; $albumDate = strtotime($albumDate); $albumDate = date("Y-m-d", $albumDate); $albumStatusColor = "green-bg"; if (new DateTime() < new DateTime($albumDate)) { $notReleased = "true"; }else{ $notReleased = "false"; } //$gotCalendar .= "{ title: \"$albumName\", start: \"$albumDate\", className: \"$albumStatusColor\", imagetype: \"music\", url: \"https://musicbrainz.org/release-group/$albumID\" }, \n"; array_push($gotCalendar, array( "id" => "Headphones-".$i, "title" => $albumName, "start" => $albumDate, "className" => $albumStatusColor, "imagetype" => "music", 'url' => "https://musicbrainz.org/release-group/".$albumID, )); } } if ($i != 0){ return $gotCalendar; } }else{ writeLog("error", "HEADPHONES $list ERROR: could not connect - check URL and/or check API key - if HTTPS, is cert valid"); } } function checkRootPath($string){ if($string == "\\" || $string == "/"){ return "/"; }else{ return str_replace("\\", "/", $string) . "/"; } } function strip($string){ $string = strip_tags($string); return preg_replace('/[ \t]+/', ' ', preg_replace('/\s*$^\s*/m', "\n", $string)); } function writeLog($type, $message){ if(file_exists(DATABASE_LOCATION."org.log")){ if(filesize(DATABASE_LOCATION."org.log") > 500000){ rename(DATABASE_LOCATION.'org.log',DATABASE_LOCATION.'org['.date('Y-m-d').'].log'); $message2 = date("Y-m-d H:i:s")."|".$type."|".strip("ORG LOG: Creating backup of org.log to org[".date('Y-m-d')."].log ")."\n"; file_put_contents(DATABASE_LOCATION."org.log", $message2, FILE_APPEND | LOCK_EX); } } $message = date("Y-m-d H:i:s")."|".$type."|".strip($message)."\n"; file_put_contents(DATABASE_LOCATION."org.log", $message, FILE_APPEND | LOCK_EX); } function readLog(){ $log = file(DATABASE_LOCATION."org.log",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $log = array_reverse($log); foreach($log as $line){ if(substr_count($line, '|') == 2){ $line = explode("|", strip($line)); $line[1] = ($line[1] == "error") ? 'Error' : 'Success'; echo "".$line[0]."".$line[2]."".$line[1].""; } } } function buildStream($array){ $result = ""; if (array_key_exists('platform', $array)) { $result .= '
    '.$array['platform'].'
    '; } if (array_key_exists('device', $array)) { $result .= '
    '.$array['device'].'
    '; } if (array_key_exists('stream', $array)) { $result .= '
    '.$array['stream'].'
    '; } if (array_key_exists('video', $array)) { $result .= '
    '.$array['video'].'
    '; } if (array_key_exists('audio', $array)) { $result .= '
    '.$array['audio'].'
    '; } return $result; } function streamType($value){ if($value == "transcode" || $value == "Transcode"){ return "Transcode"; }elseif($value == "copy" || $value == "DirectStream"){ return "Direct Stream"; }elseif($value == "directplay" || $value == "DirectPlay"){ return "Direct Play"; }else{ return "Direct Play"; } } function getPlatform($platform){ $allPlatforms = array( "Chrome" => "chrome.png", "tvOS" => "atv.png", "iOS" => "ios.png", "Xbox One" => "xbox.png", "Mystery 4" => "playstation.png", "Samsung" => "samsung.png", "Roku" => "roku.png", "Emby for iOS" => "ios.png", "Emby Mobile" => "emby.png", "Emby Theater" => "emby.png", "Emby Classic" => "emby.png", "Safari" => "safari.png", "Android" => "android.png", "AndroidTv" => "android.png", "Chromecast" => "chromecast.png", "Dashboard" => "emby.png", "Dlna" => "dlna.png", "Windows Phone" => "wp.png", "Windows RT" => "win8.png", "Kodi" => "kodi.png", ); if (array_key_exists($platform, $allPlatforms)) { return $allPlatforms[$platform]; }else{ return "pmp.png"; } } function getServer(){ $server = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"]; return $server; } function prettyPrint($v) { $trace = debug_backtrace()[0]; echo '
    '.$trace['file'].':'.$trace['line'].' '.gettype($v)."\n\n".print_r($v, 1).'

    '; } function checkFrame($array, $url){ if(array_key_exists("x-frame-options", $array)){ if($array['x-frame-options'] == "deny"){ return false; }elseif($array['x-frame-options'] == "sameorgin"){ $digest = parse_url($url); $host = (isset($digest['host'])?$digest['host']:''); if(getServer() == $host){ return true; }else{ return false; } } }else{ if(!$array){ return false; } return true; } } function frameTest($url){ $array = array_change_key_case(get_headers(qualifyURL($url), 1)); $url = qualifyURL($url); if(checkFrame($array, $url)){ return true; }else{ return false; } } function sendResult($result, $icon = "floppy-o", $message = false, $success = "WAS_SUCCESSFUL", $fail = "HAS_FAILED", $send = true) { $notifyExplode = explode("-", NOTIFYEFFECT); if ($result) { $msg = array( 'html' => ($message?''.$message.' '.translate($success).'':''.translate($success).''), 'icon' => $icon, 'type' => 'success', 'length' => '5000', 'layout' => $notifyExplode[0], 'effect' => $notifyExplode[1], ); } else { $msg = array( 'html' => ($message?''.$message.' '.translate($fail).'':''.translate($fail).''), 'icon' => $icon, 'type' => 'error', 'length' => '5000', 'layout' => $notifyExplode[0], 'effect' => $notifyExplode[1], ); } // Send and kill script? if ($send) { header('Content-Type: application/json'); echo json_encode(array('notify'=>$msg)); die(); } return $msg; } function buildHomepageNotice($layout, $type, $title, $message){ switch ($layout) { case 'elegant': return '

    '.$title.'

    '.$message.'
    '; break; case 'basic': return '

    '.$title.'

    '.$message.'
    '; break; case 'jumbotron'; return '

    '.$title.'

    '.$message.'

    '; } } function embyArray($array, $type) { $key = ($type == "video" ? "Height" : "Channels"); if (array_key_exists($key, $array)) { switch ($type) { case "video": $codec = $array["Codec"]; $height = $array["Height"]; $width = $array["Width"]; break; default: $codec = $array["Codec"]; $channels = $array["Channels"]; } return ($type == "video" ? "(".$codec.") (".$width."x".$height.")" : "(".$codec.") (".$channels."ch)"); } foreach ($array as $element) { if (is_array($element)) { if (embyArray($element, $type)) { return embyArray($element, $type); } } } } // Get Now Playing Streams From Plex function searchPlex($query){ $address = qualifyURL(PLEXURL); $openTab = (PLEXTABNAME) ? "true" : "false"; // Perform API requests $api = @curl_get($address."/search?query=".rawurlencode($query)."&X-Plex-Token=".PLEXTOKEN); libxml_use_internal_errors(true); $api = simplexml_load_string($api); $getServer = simplexml_load_string(@curl_get($address."/?X-Plex-Token=".PLEXTOKEN)); if (!$getServer) { return 'Could not load!'; } // Identify the local machine $server = $getServer['machineIdentifier']; $pre = ""; $items = ""; $albums = $movies = $shows = 0; $style = 'style="vertical-align: middle"'; foreach($api AS $child) { if($child['type'] != "artist" && $child['type'] != "episode" && isset($child['librarySectionID'])){ $time = (string)$child['addedAt']; $time = new DateTime("@$time"); $results = array( "title" => (string)$child['title'], "image" => (string)$child['thumb'], "type" => (string)ucwords($child['type']), "year" => (string)$child['year'], "key" => (string)$child['ratingKey']."-search", "ratingkey" => (string)$child['ratingKey'], "genre" => (string)$child->Genre['tag'], "added" => $time->format('Y-m-d'), "extra" => "", ); switch ($child['type']){ case "album": $push = array( "title" => (string)$child['parentTitle']." - ".(string)$child['title'], ); $results = array_replace($results,$push); $albums++; break; case "movie": $push = array( "extra" => "Content Rating: ".(string)$child['contentRating']."
    Movie Rating: ".(string)$child['rating'], ); $results = array_replace($results,$push); $movies++; break; case "show": $push = array( "extra" => "Seasons: ".(string)$child['childCount']."
    Episodes: ".(string)$child['leafCount'], ); $results = array_replace($results,$push); $shows++; break; } if (file_exists('images/cache/'.$results['key'].'.jpg')){ $image_url = 'images/cache/'.$results['key'].'.jpg'; } if (file_exists('images/cache/'.$results['key'].'.jpg') && (time() - 604800) > filemtime('images/cache/'.$results['key'].'.jpg') || !file_exists('images/cache/'.$results['key'].'.jpg')) { $image_url = 'ajax.php?a=plex-image&img='.$results['image'].'&height=150&width=100&key='.$results['key']; } if(!$results['image']){ $image_url = "images/no-search.png"; $key = "no-search"; } if (PLEXTABURL) { $link = PLEXTABURL."/web/index.html#!/server/$server/details?key=/library/metadata/".$results['ratingkey']; }else{ $link = "https://app.plex.tv/web/app#!/server/$server/details?key=/library/metadata/".$results['ratingkey']; } $items .= ''; } } $totals = '
     '.$movies.'  '.$shows.'  '.$albums.'
    '; return (!empty($items) ? $totals.$pre.$items."
    CoverTitleGenreYearTypeAddedExtra Info
    '.$results['title'].' '.$results['genre'].' '.$results['year'].' '.$results['type'].' '.$results['added'].' '.$results['extra'].'
    " : "

    No Results for $query

    " ); } function getBannedUsers($string){ if (strpos($string, ',') !== false) { $banned = explode(",", $string); }else{ $banned = array($string); } return $banned; } function getWhitelist($string){ if (strpos($string, ',') !== false) { $whitelist = explode(",", $string); }else{ $whitelist = array($string); } foreach($whitelist as &$ip){ $ip = is_numeric(substr($ip, 0, 1)) ? $ip : gethostbyname($ip); } return $whitelist; } function get_client_ip() { $ipaddress = ''; if (isset($_SERVER['HTTP_CLIENT_IP'])) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_X_FORWARDED'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_FORWARDED'])) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if(isset($_SERVER['REMOTE_ADDR'])) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; return $ipaddress; } //EMAIL SHIT function sendEmail($email, $username = "Organizr User", $subject, $body, $cc = null, $bcc = null){ $mail = new PHPMailer; $mail->isSMTP(); $mail->Host = SMTPHOST; $mail->SMTPAuth = SMTPHOSTAUTH; $mail->Username = SMTPHOSTUSERNAME; $mail->Password = SMTPHOSTPASSWORD; $mail->SMTPSecure = SMTPHOSTTYPE; $mail->Port = SMTPHOSTPORT; $mail->setFrom(SMTPHOSTSENDEREMAIL, SMTPHOSTSENDERNAME); $mail->addReplyTo(SMTPHOSTSENDEREMAIL, SMTPHOSTSENDERNAME); $mail->isHTML(true); if($email){ $mail->addAddress($email, $username); } if($cc){ $mail->addCC($cc); } if($bcc){ if(strpos($bcc , ',') === false){ $mail->addBCC($bcc); }else{ $allEmails = explode(",",$bcc); foreach($allEmails as $gotEmail){ $mail->addBCC($gotEmail); } } } $mail->Subject = $subject; $mail->Body = $body; //$mail->send(); if(!$mail->send()) { writeLog("error", "mail failed to send"); } else { writeLog("success", "mail has been sent"); } } //EMAIL SHIT function sendTestEmail($to, $from, $host, $auth, $username, $password, $type, $port, $sendername){ $mail = new PHPMailer; $mail->isSMTP(); $mail->Host = $host; $mail->SMTPAuth = $auth; $mail->Username = $username; $mail->Password = $password; $mail->SMTPSecure = $type; $mail->Port = $port; $mail->setFrom($from, $sendername); $mail->addReplyTo($from, $sendername); $mail->isHTML(true); $mail->addAddress($to, "Organizr Admin"); $mail->Subject = "Organizr Test E-Mail"; $mail->Body = "This was just a test!"; //$mail->send(); if(!$mail->send()) { writeLog("error", "EMAIL TEST: mail failed to send - Error:".$mail->ErrorInfo); return false; } else { writeLog("success", "EMAIL TEST: mail has been sent successfully"); return true; } } function libraryList(){ $address = qualifyURL(PLEXURL); $headers = array( "Accept" => "application/json", "X-Plex-Token" => PLEXTOKEN ); libxml_use_internal_errors(true); $getServer = simplexml_load_string(@curl_get($address."/?X-Plex-Token=".PLEXTOKEN)); if (!$getServer) { return 'Could not load!'; }else { $gotServer = $getServer['machineIdentifier']; } $api = simplexml_load_string(@curl_get("https://plex.tv/api/servers/$gotServer/shared_servers", $headers)); $libraryList = array(); foreach($api->SharedServer->Section AS $child) { $libraryList['libraries'][(string)$child['title']] = (string)$child['id']; } foreach($api->SharedServer AS $child) { if(!empty($child['username'])){ $username = (string)strtolower($child['username']); $email = (string)strtolower($child['email']); $libraryList['users'][$username] = (string)$child['id']; $libraryList['emails'][$email] = (string)$child['id']; $libraryList['both'][$username] = $email; } } return (!empty($libraryList) ? array_change_key_case($libraryList,CASE_LOWER) : null ); } function plexUserShare($username){ $address = qualifyURL(PLEXURL); $headers = array( "Accept" => "application/json", "Content-Type" => "application/json", "X-Plex-Token" => PLEXTOKEN ); libxml_use_internal_errors(true); $getServer = simplexml_load_string(@curl_get($address."/?X-Plex-Token=".PLEXTOKEN)); if (!$getServer) { return 'Could not load!'; }else { $gotServer = $getServer['machineIdentifier']; } $json = array( "server_id" => $gotServer, "shared_server" => array( //"library_section_ids" => "[26527637]", "invited_email" => $username ) ); $api = curl_post("https://plex.tv/api/servers/$gotServer/shared_servers/", $json, $headers); switch ($api['http_code']['http_code']){ case 400: writeLog("error", "PLEX INVITE: $username already has access to the shared libraries"); $result = "$username already has access to the shared libraries"; break; case 401: writeLog("error", "PLEX INVITE: Invalid Plex Token"); $result = "Invalid Plex Token"; break; case 200: writeLog("success", "PLEX INVITE: $username now has access to your Plex Library"); $result = "$username now has access to your Plex Library"; break; default: writeLog("error", "PLEX INVITE: unknown error"); $result = false; } return (!empty($result) ? $result : null ); } function plexUserDelete($username){ $address = qualifyURL(PLEXURL); $headers = array( "Accept" => "application/json", "Content-Type" => "application/json", "X-Plex-Token" => PLEXTOKEN ); libxml_use_internal_errors(true); $getServer = simplexml_load_string(@curl_get($address."/?X-Plex-Token=".PLEXTOKEN)); if (!$getServer) { return 'Could not load!'; }else { $gotServer = $getServer['machineIdentifier']; } $id = (is_numeric($username) ? $id : convertPlexName($username, "id")); $api = curl_delete("https://plex.tv/api/servers/$gotServer/shared_servers/$id", $headers); switch ($api['http_code']['http_code']){ case 401: writeLog("error", "PLEX INVITE: Invalid Plex Token"); $result = "Invalid Plex Token"; break; case 200: writeLog("success", "PLEX INVITE: $username doesn't have access to your Plex Library anymore"); $result = "$username doesn't have access to your Plex Library anymore"; break; default: writeLog("error", "PLEX INVITE: unknown error"); $result = false; } return (!empty($result) ? $result : null ); } function convertPlexName($user, $type){ $array = libraryList(); switch ($type){ case "username": $plexUser = array_search ($user, $array['users']); break; case "id": if (array_key_exists(strtolower($user), $array['users'])) { $plexUser = $array['users'][strtolower($user)]; } break; default: $plexUser = false; } return (!empty($plexUser) ? $plexUser : null ); } function randomCode($length = 5, $type = null) { switch ($type){ case "alpha": $legend = array_merge(range('A', 'Z')); break; case "numeric": $legend = array_merge(range(0,9)); break; default: $legend = array_merge(range(0,9),range('A', 'Z')); } $code = ""; for($i=0; $i < $length; $i++) { $code .= $legend[mt_rand(0, count($legend) - 1)]; } return $code; } function inviteCodes($action, $code = null, $usedBy = null) { if (!isset($GLOBALS['file_db'])) { $GLOBALS['file_db'] = new PDO('sqlite:'.DATABASE_LOCATION.'users.db'); $GLOBALS['file_db']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } $now = date("Y-m-d H:i:s"); switch ($action) { case "get": // Start Array $result = array(); // Database Lookup $invites = $GLOBALS['file_db']->query('SELECT * FROM invites WHERE valid = "Yes"'); // Get Codes foreach($invites as $row) { array_push($result, $row['code']); } // Return the Results return (!empty($result) ? $result : false ); break; case "check": // Start Array $result = array(); // Database Lookup $invites = $GLOBALS['file_db']->query('SELECT * FROM invites WHERE valid = "Yes" AND code = "'.$code.'"'); // Get Codes foreach($invites as $row) { $result = $row['code']; } // Return the Results return (!empty($result) ? $result : false ); break; case "use": $currentIP = get_client_ip(); $invites = $GLOBALS['file_db']->query('UPDATE invites SET valid = "No", usedby = "'.$usedBy.'", dateused = "'.$now.'", ip = "'.$currentIP.'" WHERE code = "'.$code.'"'); if(ENABLEMAIL){ if (!isset($GLOBALS['USER'])) { require_once("user.php"); $GLOBALS['USER'] = new User('registration_callback'); } $emailTemplate = array( 'type' => 'mass', 'body' => 'The user: {user} has reddemed the code: {inviteCode} his IP Address was '.$currentIP, 'subject' => 'Invite Code '.$code.' Has Been Used', 'user' => $usedBy, 'password' => null, 'inviteCode' => $code, ); $emailTemplate = emailTemplate($emailTemplate); $subject = $emailTemplate['subject']; $body = buildEmail($emailTemplate); sendEmail($GLOBALS['USER']->adminEmail, "Admin", $subject, $body); } return (!empty($invites) ? true : false ); break; } } function plexJoin($username, $email, $password){ $connectURL = 'https://plex.tv/users.json'; $headers = array( 'Accept'=> 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'X-Plex-Product' => 'Organizr', 'X-Plex-Version' => '1.0', 'X-Plex-Client-Identifier' => '01010101-10101010', ); $body = array( 'user[email]' => $email, 'user[username]' => $username, 'user[password]' => $password, ); $api = curl_post($connectURL, $body, $headers); $json = json_decode($api['content'], true); $errors = (!empty($json['errors']) ? true : false); $success = (!empty($json['user']) ? true : false); //Use This for later $usernameError = (!empty($json['errors']['username']) ? $json['errors']['username'][0] : false); $emailError = (!empty($json['errors']['email']) ? $json['errors']['email'][0] : false); $passwordError = (!empty($json['errors']['password']) ? $json['errors']['password'][0] : false); $errorMessage = ""; if($errors){ if($usernameError){ $errorMessage .= "[Username Error: ". $usernameError ."]"; } if($emailError){ $errorMessage .= "[Email Error: ". $emailError ."]"; } if($passwordError){ $errorMessage .= "[Password Error: ". $passwordError ."]"; } } switch ($api['http_code']['http_code']){ case 400: writeLog("error", "PLEX JOIN: Error: ".$api['http_code']['http_code']." $username already has access to the shared libraries $errorMessage"); break; case 401: writeLog("error", "PLEX JOIN: Error: ".$api['http_code']['http_code']." invalid Plex Token $errorMessage"); break; case 422: writeLog("error", "PLEX JOIN: Error: ".$api['http_code']['http_code']." user info error $errorMessage"); break; case 429: writeLog("error", "PLEX JOIN: Error: ".$api['http_code']['http_code']." too many requests to plex.tv please try later $errorMessage"); break; case 200: case 201: writeLog("success", "PLEX JOIN: $username now has access to your Plex Library"); break; default: writeLog("error", "PLEX JOIN: unknown error, $errorMessage Error: ".$api['http_code']['http_code']); } //prettyPrint($api); //prettyPrint(json_decode($api['content'], true)); return (!empty($success) && empty($errors) ? true : false ); } function getCert(){ $url = "http://curl.haxx.se/ca/cacert.pem"; $file = getcwd()."/config/cacert.pem"; $directory = getcwd()."/config/"; @mkdir($directory, 0770, true); if(!file_exists($file)){ file_put_contents( $file, fopen($url, 'r')); writeLog("success", "CERT PEM: pem file created"); }elseif (file_exists($file) && time() - 2592000 > filemtime($file)) { file_put_contents( $file, fopen($url, 'r')); writeLog("success", "CERT PEM: downloaded new pem file"); } return $file; } function customCSS(){ if(CUSTOMCSS == "true") { $template_file = "custom.css"; $file_handle = fopen($template_file, "rb"); echo "\n"; echo fread($file_handle, filesize($template_file)); fclose($file_handle); echo "\n"; } } function tvdbToken(){ $headers = array( "Accept" => "application/json", "Content-Type" => "application/json" ); $json = array( "apikey" => "FBE7B62621F4CAD7", "userkey" => "328BB46EB1E9A0F5", "username" => "causefx" ); $api = curl_post("https://api.thetvdb.com/login", $json, $headers); return json_decode($api['content'], true)['token']; } function tvdbGet($id){ $headers = array( "Accept" => "application/json", "Authorization" => "Bearer ".tvdbToken(), "trakt-api-key" => "4502cfdf8f7282fe454878ff8583f5636392cdc5fcac30d0cc4565f7173bf443", "trakt-api-version" => "2" ); $trakt = curl_get("https://api.trakt.tv/search/tvdb/$id?type=show", $headers); @$api['trakt'] = json_decode($trakt, true)[0]['show']['ids']; if(empty($api['trakt'])){ $series = curl_get("https://api.thetvdb.com/series/$id", $headers); $poster = curl_get("https://api.thetvdb.com/series/$id/images/query?keyType=poster", $headers); $backdrop = curl_get("https://api.thetvdb.com/series/$id/images/query?keyType=fanart", $headers); $api['series'] = json_decode($series, true)['data']; $api['poster'] = json_decode($poster, true)['data']; $api['backdrop'] = json_decode($backdrop, true)['data']; } return $api; } function tvdbSearch($name, $type){ $name = rawurlencode(preg_replace("/\(([^()]*+|(?R))*\)/","", $name)); $headers = array( "Accept" => "application/json", "Authorization" => "Bearer ".tvdbToken(), "trakt-api-key" => "4502cfdf8f7282fe454878ff8583f5636392cdc5fcac30d0cc4565f7173bf443", "trakt-api-version" => "2" ); $trakt = curl_get("https://api.trakt.tv/search/$type?query=$name", $headers); @$api['trakt'] = json_decode($trakt, true)[0][$type]['ids']; return $api; } function getPlexPlaylists(){ $address = qualifyURL(PLEXURL); // Perform API requests $api = @curl_get($address."/playlists?X-Plex-Token=".PLEXTOKEN); libxml_use_internal_errors(true); $api = simplexml_load_string($api); if (is_array($api) || is_object($api)){ if (!$api->head->title){ $getServer = simplexml_load_string(@curl_get($address."/?X-Plex-Token=".PLEXTOKEN)); if (!$getServer) { return 'Could not load!'; } // Identify the local machine $gotServer = $getServer['machineIdentifier']; $output = ""; $hideMenu = '
    '; return '
    All Playlists
    '.$hideMenu.'


    '.$output.'
    '; }else{ writeLog("error", "PLEX PLAYLIST ERROR: could not connect - check token - if HTTPS, is cert valid"); } }else{ writeLog("error", "PLEX PLAYLIST ERROR: could not connect - check URL - if HTTPS, is cert valid"); } } function readExternalLog($type,$filename,$name = null){ $log = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $log = array_reverse($log); foreach($log as $line){ if(!empty($line) && $line[0] != " "){ $line = strip($line); if($type == "single"){ if( strpos( strtolower($line), "ror" ) !== false ) { echo "".$line.""; }else{ echo "".$line.""; } }elseif($type == "all"){ if( strpos( strtolower($line), "ror" ) !== false ) { echo "".$name.""; echo "".$line.""; }else{ echo "".$name.""; echo "".$line.""; } } } } } function getLogs(){ $path = __DIR__ ."/logs/"; @mkdir($path, 0770, true); $logs = array(); $files = array_diff(scandir($path), array('.', '..')); foreach($files as $v){ $title = explode(".", $v)[0]; $logs[$title] = $path.$v; } return $logs; } function getBackups(){ $path = DATABASE_LOCATION ."backups/"; @mkdir($path, 0770, true); $backups = array(); $files = array_diff(scandir($path), array('.', '..')); return array_reverse($files); } function getExtension($string) { return preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $string); } function showFile(){ $file = $_GET['file']; $fileType = getExtension($file); if($fileType == 'css' || $fileType == 'js'){ header("Content-type: ".mimeTypes()[$fileType]); @readfile($file); } } function getCalendar(){ $sonarr = new Sonarr(SONARRURL, SONARRKEY); $radarr = new Sonarr(RADARRURL, RADARRKEY); $sickrage = new SickRage(SICKRAGEURL, SICKRAGEKEY); $startDate = date('Y-m-d',strtotime("-".CALENDARSTARTDAY." days")); $endDate = date('Y-m-d',strtotime("+".CALENDARENDDAY." days")); $calendarItems = array(); if (SONARRURL != "" && qualifyUser(SONARRHOMEAUTH)){ try { $sonarrCalendar = getSonarrCalendar($sonarr->getCalendar($startDate, $endDate, SONARRUNMONITORED)); if(!empty($sonarrCalendar)) { $calendarItems = array_merge($calendarItems, $sonarrCalendar); } } catch (Exception $e) { writeLog("error", "SONARR ERROR: ".strip($e->getMessage())); } } if (RADARRURL != "" && qualifyUser(RADARRHOMEAUTH)){ try { $radarrCalendar = getRadarrCalendar($radarr->getCalendar($startDate, $endDate)); if(!empty($radarrCalendar)) { $calendarItems = array_merge($calendarItems, $radarrCalendar); } } catch (Exception $e) { writeLog("error", "RADARR ERROR: ".strip($e->getMessage())); } } if (COUCHURL != "" && qualifyUser(COUCHHOMEAUTH)){ $couchCalendar = getCouchCalendar(); if(!empty($couchCalendar)) { $calendarItems = array_merge($calendarItems, $couchCalendar); } } if (HEADPHONESURL != "" && qualifyUser(HEADPHONESHOMEAUTH)){ $headphonesHistory = getHeadphonesCalendar(HEADPHONESURL, HEADPHONESKEY, "getHistory"); $headphonesWanted = getHeadphonesCalendar(HEADPHONESURL, HEADPHONESKEY, "getWanted"); if(!empty($headphonesHistory)) { $calendarItems = array_merge($calendarItems, $headphonesHistory); } if(!empty($headphonesWanted)) { $calendarItems = array_merge($calendarItems, $headphonesWanted); } } if (SICKRAGEURL != "" && qualifyUser(SICKRAGEHOMEAUTH)){ try { $sickrageFuture = getSickrageCalendarWanted($sickrage->future()); if(!empty($sickrageFuture)) { $calendarItems = array_merge($calendarItems, $sickrageFuture); } } catch (Exception $e) { writeLog("error", "SICKRAGE/BEARD ERROR: ".strip($e->getMessage())); } try { $sickrageHistory = getSickrageCalendarHistory($sickrage->history("100","downloaded")); if(!empty($sickrageHistory)) { $calendarItems = array_merge($calendarItems, $sickrageHistory); } } catch (Exception $e) { writeLog("error", "SICKRAGE/BEARD ERROR: ".strip($e->getMessage())); } } return $calendarItems; } function localURL($url){ if (strpos($url, 'https') !== false) { preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result); $result = (!empty($result) ? true : false); return $result; } } function fileArray($files){ foreach($files as $file){ if(file_exists($file)){ $list[] = $file; } } if(!empty($list)){ return $list; } } function backupDB(){ if (extension_loaded('ZIP')) { $directory = DATABASE_LOCATION."backups/"; @mkdir($directory, 0770, true); $orgFiles = array( 'css' => 'custom.css', 'temp' => 'cus.sd', 'orgLog' => DATABASE_LOCATION.'org.log', 'loginLog' => DATABASE_LOCATION.'loginLog.json', 'chatDB' => 'chatpack.db', 'config' => 'config/config.php', 'database' => DATABASE_LOCATION.'users.db' ); $files = fileArray($orgFiles); if(!empty($files)){ writeLog("success", "BACKUP: backup process started"); $zipname = $directory.'backup['.date('Y-m-d_H-i').']['.INSTALLEDVERSION.'].zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); writeLog("success", "BACKUP: backup process finished"); return true; }else{ return false; } }else{ return false; } } class Ping { private $host; private $ttl; private $timeout; private $port = 80; private $data = 'Ping'; private $commandOutput; /** * Called when the Ping object is created. * * @param string $host * The host to be pinged. * @param int $ttl * Time-to-live (TTL) (You may get a 'Time to live exceeded' error if this * value is set too low. The TTL value indicates the scope or range in which * a packet may be forwarded. By convention: * - 0 = same host * - 1 = same subnet * - 32 = same site * - 64 = same region * - 128 = same continent * - 255 = unrestricted * @param int $timeout * Timeout (in seconds) used for ping and fsockopen(). * @throws \Exception if the host is not set. */ public function __construct($host, $ttl = 255, $timeout = 10) { if (!isset($host)) { throw new \Exception("Error: Host name not supplied."); } $this->host = $host; $this->ttl = $ttl; $this->timeout = $timeout; } /** * Set the ttl (in hops). * * @param int $ttl * TTL in hops. */ public function setTtl($ttl) { $this->ttl = $ttl; } /** * Get the ttl. * * @return int * The current ttl for Ping. */ public function getTtl() { return $this->ttl; } /** * Set the timeout. * * @param int $timeout * Time to wait in seconds. */ public function setTimeout($timeout) { $this->timeout = $timeout; } /** * Get the timeout. * * @return int * Current timeout for Ping. */ public function getTimeout() { return $this->timeout; } /** * Set the host. * * @param string $host * Host name or IP address. */ public function setHost($host) { $this->host = $host; } /** * Get the host. * * @return string * The current hostname for Ping. */ public function getHost() { return $this->host; } /** * Set the port (only used for fsockopen method). * * Since regular pings use ICMP and don't need to worry about the concept of * 'ports', this is only used for the fsockopen method, which pings servers by * checking port 80 (by default). * * @param int $port * Port to use for fsockopen ping (defaults to 80 if not set). */ public function setPort($port) { $this->port = $port; } /** * Get the port (only used for fsockopen method). * * @return int * The port used by fsockopen pings. */ public function getPort() { return $this->port; } /** * Return the command output when method=exec. * @return string */ public function getCommandOutput(){ return $this->commandOutput; } /** * Matches an IP on command output and returns. * @return string */ public function getIpAddress() { $out = array(); if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->commandOutput, $out)){ return $out[0]; } return null; } /** * Ping a host. * * @param string $method * Method to use when pinging: * - exec (default): Pings through the system ping command. Fast and * robust, but a security risk if you pass through user-submitted data. * - fsockopen: Pings a server on port 80. * - socket: Creates a RAW network socket. Only usable in some * environments, as creating a SOCK_RAW socket requires root privileges. * * @throws InvalidArgumentException if $method is not supported. * * @return mixed * Latency as integer, in ms, if host is reachable or FALSE if host is down. */ public function ping($method = 'exec') { $latency = false; switch ($method) { case 'exec': $latency = $this->pingExec(); break; case 'fsockopen': $latency = $this->pingFsockopen(); break; case 'socket': $latency = $this->pingSocket(); break; default: throw new \InvalidArgumentException('Unsupported ping method.'); } // Return the latency. return $latency; } /** * The exec method uses the possibly insecure exec() function, which passes * the input to the system. This is potentially VERY dangerous if you pass in * any user-submitted data. Be SURE you sanitize your inputs! * * @return int * Latency, in ms. */ private function pingExec() { $latency = false; $ttl = escapeshellcmd($this->ttl); $timeout = escapeshellcmd($this->timeout); $host = escapeshellcmd($this->host); // Exec string for Windows-based systems. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // -n = number of pings; -i = ttl; -w = timeout (in milliseconds). $exec_string = 'ping -n 1 -i ' . $ttl . ' -w ' . ($timeout * 1000) . ' ' . $host; } // Exec string for Darwin based systems (OS X). else if(strtoupper(PHP_OS) === 'DARWIN') { // -n = numeric output; -c = number of pings; -m = ttl; -t = timeout. $exec_string = 'ping -n -c 1 -m ' . $ttl . ' -t ' . $timeout . ' ' . $host; } // Exec string for other UNIX-based systems (Linux). else { // -n = numeric output; -c = number of pings; -t = ttl; -W = timeout $exec_string = 'ping -n -c 1 -t ' . $ttl . ' -W ' . $timeout . ' ' . $host . ' 2>&1'; } exec($exec_string, $output, $return); // Strip empty lines and reorder the indexes from 0 (to make results more // uniform across OS versions). $this->commandOutput = implode($output, ''); $output = array_values(array_filter($output)); // If the result line in the output is not empty, parse it. if (!empty($output[1])) { // Search for a 'time' value in the result line. $response = preg_match("/time(?:=|<)(?