homepage-connect-functions.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /** @noinspection PhpUndefinedFieldInspection */
  3. trait HomepageConnectFunctions
  4. {
  5. public function csvHomepageUrlToken($url, $token)
  6. {
  7. $list = array();
  8. $urlList = explode(',', $url);
  9. $tokenList = explode(',', $token);
  10. if (count($urlList) == count($tokenList)) {
  11. foreach ($urlList as $key => $value) {
  12. $list[$key] = array(
  13. 'url' => $this->qualifyURL($value),
  14. 'token' => $tokenList[$key]
  15. );
  16. }
  17. }
  18. return $list;
  19. }
  20. public function streamType($value)
  21. {
  22. if ($value == "transcode" || $value == "Transcode") {
  23. return "Transcode";
  24. } elseif ($value == "copy" || $value == "DirectStream") {
  25. return "Direct Stream";
  26. } elseif ($value == "directplay" || $value == "DirectPlay") {
  27. return "Direct Play";
  28. } else {
  29. return "Direct Play";
  30. }
  31. }
  32. public function getCacheImageSize($type)
  33. {
  34. switch ($type) {
  35. case 'height':
  36. case 'h':
  37. return 300 * $this->config['cacheImageSize'];
  38. case 'width':
  39. case 'w':
  40. return 200 * $this->config['cacheImageSize'];
  41. case 'nowPlayingHeight':
  42. case 'nph':
  43. return 675 * $this->config['cacheImageSize'];
  44. case 'nowPlayingWidth':
  45. case 'npw':
  46. return 1200 * $this->config['cacheImageSize'];
  47. }
  48. }
  49. public function ombiImport($type = null)
  50. {
  51. if (!empty($this->config['ombiURL']) && !empty($this->config['ombiToken']) && !empty($type)) {
  52. try {
  53. $url = $this->qualifyURL($this->config['ombiURL']);
  54. $headers = array(
  55. "Accept" => "application/json",
  56. "Content-Type" => "application/json",
  57. "Apikey" => $GLOBALS['ombiToken']
  58. );
  59. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  60. switch ($type) {
  61. case 'emby':
  62. case 'emby_local':
  63. case 'emby_connect':
  64. case 'emby_all':
  65. $response = Requests::post($url . "/api/v1/Job/embyuserimporter", $headers, $options);
  66. break;
  67. case 'plex':
  68. $response = Requests::post($url . "/api/v1/Job/plexuserimporter", $headers, $options);
  69. break;
  70. default:
  71. return false;
  72. break;
  73. }
  74. if ($response->success) {
  75. $this->writeLog('success', 'OMBI Connect Function - Ran User Import', 'SYSTEM');
  76. return true;
  77. } else {
  78. $this->writeLog('error', 'OMBI Connect Function - Error: Connection Unsuccessful', 'SYSTEM');
  79. return false;
  80. }
  81. } catch (Requests_Exception $e) {
  82. $this->writeLog('error', 'OMBI Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  83. return false;
  84. }
  85. }
  86. return false;
  87. }
  88. }
  89. function testAPIConnection($array)
  90. {
  91. switch ($array['data']['action']) {
  92. case 'ldap_login':
  93. $username = $array['data']['data']['username'];
  94. $password = $array['data']['data']['password'];
  95. if (empty($username) || empty($password)) {
  96. return 'Missing Username or Password';
  97. }
  98. if (!empty($GLOBALS['authBaseDN']) && !empty($GLOBALS['authBackendHost'])) {
  99. $ad = new \Adldap\Adldap();
  100. // Create a configuration array.
  101. $ldapServers = explode(',', $GLOBALS['authBackendHost']);
  102. $i = 0;
  103. foreach ($ldapServers as $key => $value) {
  104. // Calculate parts
  105. $digest = parse_url(trim($value));
  106. $scheme = strtolower((isset($digest['scheme']) ? $digest['scheme'] : 'ldap'));
  107. $host = (isset($digest['host']) ? $digest['host'] : (isset($digest['path']) ? $digest['path'] : ''));
  108. $port = (isset($digest['port']) ? $digest['port'] : (strtolower($scheme) == 'ldap' ? 389 : 636));
  109. // Reassign
  110. $ldapHosts[] = $host;
  111. $ldapServersNew[$key] = $scheme . '://' . $host . ':' . $port; // May use this later
  112. if ($i == 0) {
  113. $ldapPort = $port;
  114. }
  115. $i++;
  116. }
  117. $config = [
  118. // Mandatory Configuration Options
  119. 'hosts' => $ldapHosts,
  120. 'base_dn' => $GLOBALS['authBaseDN'],
  121. 'username' => (empty($GLOBALS['ldapBindUsername'])) ? null : $GLOBALS['ldapBindUsername'],
  122. 'password' => (empty($GLOBALS['ldapBindPassword'])) ? null : decrypt($GLOBALS['ldapBindPassword']),
  123. // Optional Configuration Options
  124. 'schema' => (($GLOBALS['ldapType'] == '1') ? Adldap\Schemas\ActiveDirectory::class : (($GLOBALS['ldapType'] == '2') ? Adldap\Schemas\OpenLDAP::class : Adldap\Schemas\FreeIPA::class)),
  125. 'account_prefix' => (empty($GLOBALS['authBackendHostPrefix'])) ? null : $GLOBALS['authBackendHostPrefix'],
  126. 'account_suffix' => (empty($GLOBALS['authBackendHostSuffix'])) ? null : $GLOBALS['authBackendHostSuffix'],
  127. 'port' => $ldapPort,
  128. 'follow_referrals' => false,
  129. 'use_ssl' => $GLOBALS['ldapSSL'],
  130. 'use_tls' => $GLOBALS['ldapTLS'],
  131. 'version' => 3,
  132. 'timeout' => 5,
  133. // Custom LDAP Options
  134. 'custom_options' => [
  135. // See: http://php.net/ldap_set_option
  136. //LDAP_OPT_X_TLS_REQUIRE_CERT => LDAP_OPT_X_TLS_HARD
  137. ]
  138. ];
  139. // Add a connection provider to Adldap.
  140. $ad->addProvider($config);
  141. try {
  142. // If a successful connection is made to your server, the provider will be returned.
  143. $provider = $ad->connect();
  144. //prettyPrint($provider);
  145. if ($provider->auth()->attempt($username, $password, true)) {
  146. // Passed.
  147. $user = $provider->search()->find($username);
  148. //return $user->getFirstAttribute('cn');
  149. //return $user->getGroups(['cn']);
  150. //return $user;
  151. //return $user->getUserPrincipalName();
  152. //return $user->getGroups(['cn']);
  153. return true;
  154. } else {
  155. // Failed.
  156. return 'Username/Password Failed to authenticate';
  157. }
  158. } catch (\Adldap\Auth\BindException $e) {
  159. $detailedError = $e->getDetailedError();
  160. writeLog('error', 'LDAP Function - Error: ' . $detailedError->getErrorMessage(), $username);
  161. return $detailedError->getErrorMessage();
  162. // There was an issue binding / connecting to the server.
  163. } catch (Adldap\Auth\UsernameRequiredException $e) {
  164. $detailedError = $e->getDetailedError();
  165. writeLog('error', 'LDAP Function - Error: ' . $detailedError->getErrorMessage(), $username);
  166. return $detailedError->getErrorMessage();
  167. // The user didn't supply a username.
  168. } catch (Adldap\Auth\PasswordRequiredException $e) {
  169. $detailedError = $e->getDetailedError();
  170. writeLog('error', 'LDAP Function - Error: ' . $detailedError->getErrorMessage(), $username);
  171. return $detailedError->getErrorMessage();
  172. // The user didn't supply a password.
  173. }
  174. }
  175. break;
  176. case 'ldap':
  177. if (!empty($GLOBALS['authBaseDN']) && !empty($GLOBALS['authBackendHost'])) {
  178. $ad = new \Adldap\Adldap();
  179. // Create a configuration array.
  180. $ldapServers = explode(',', $GLOBALS['authBackendHost']);
  181. $i = 0;
  182. foreach ($ldapServers as $key => $value) {
  183. // Calculate parts
  184. $digest = parse_url(trim($value));
  185. $scheme = strtolower((isset($digest['scheme']) ? $digest['scheme'] : 'ldap'));
  186. $host = (isset($digest['host']) ? $digest['host'] : (isset($digest['path']) ? $digest['path'] : ''));
  187. $port = (isset($digest['port']) ? $digest['port'] : (strtolower($scheme) == 'ldap' ? 389 : 636));
  188. // Reassign
  189. $ldapHosts[] = $host;
  190. if ($i == 0) {
  191. $ldapPort = $port;
  192. }
  193. $i++;
  194. }
  195. $config = [
  196. // Mandatory Configuration Options
  197. 'hosts' => $ldapHosts,
  198. 'base_dn' => $GLOBALS['authBaseDN'],
  199. 'username' => (empty($GLOBALS['ldapBindUsername'])) ? null : $GLOBALS['ldapBindUsername'],
  200. 'password' => (empty($GLOBALS['ldapBindPassword'])) ? null : decrypt($GLOBALS['ldapBindPassword']),
  201. // Optional Configuration Options
  202. 'schema' => (($GLOBALS['ldapType'] == '1') ? Adldap\Schemas\ActiveDirectory::class : (($GLOBALS['ldapType'] == '2') ? Adldap\Schemas\OpenLDAP::class : Adldap\Schemas\FreeIPA::class)),
  203. 'account_prefix' => '',
  204. 'account_suffix' => '',
  205. 'port' => $ldapPort,
  206. 'follow_referrals' => false,
  207. 'use_ssl' => $GLOBALS['ldapSSL'],
  208. 'use_tls' => $GLOBALS['ldapTLS'],
  209. 'version' => 3,
  210. 'timeout' => 5,
  211. // Custom LDAP Options
  212. 'custom_options' => [
  213. // See: http://php.net/ldap_set_option
  214. //LDAP_OPT_X_TLS_REQUIRE_CERT => LDAP_OPT_X_TLS_HARD
  215. ]
  216. ];
  217. // Add a connection provider to Adldap.
  218. $ad->addProvider($config);
  219. try {
  220. // If a successful connection is made to your server, the provider will be returned.
  221. $provider = $ad->connect();
  222. } catch (\Adldap\Auth\BindException $e) {
  223. $detailedError = $e->getDetailedError();
  224. writeLog('error', 'LDAP Function - Error: ' . $detailedError->getErrorMessage(), 'SYSTEM');
  225. return $detailedError->getErrorMessage();
  226. // There was an issue binding / connecting to the server.
  227. }
  228. return ($provider) ? true : false;
  229. }
  230. return false;
  231. break;
  232. default :
  233. return false;
  234. }
  235. return false;
  236. }