auth-functions.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. function authRegister($username, $password, $defaults, $email)
  3. {
  4. if ($GLOBALS['authBackend'] !== '') {
  5. ombiImport($GLOBALS['authBackend']);
  6. }
  7. if (createUser($username, $password, $defaults, $email)) {
  8. writeLog('success', 'Registration Function - A User has registered', $username);
  9. if ($GLOBALS['PHPMAILER-enabled']) {
  10. $emailTemplate = array(
  11. 'type' => 'registration',
  12. 'body' => $GLOBALS['PHPMAILER-emailTemplateRegisterUser'],
  13. 'subject' => $GLOBALS['PHPMAILER-emailTemplateRegisterUserSubject'],
  14. 'user' => $username,
  15. 'password' => null,
  16. 'inviteCode' => null,
  17. );
  18. $emailTemplate = phpmEmailTemplate($emailTemplate);
  19. $sendEmail = array(
  20. 'to' => $email,
  21. 'user' => $username,
  22. 'subject' => $emailTemplate['subject'],
  23. 'body' => phpmBuildEmail($emailTemplate),
  24. );
  25. phpmSendEmail($sendEmail);
  26. }
  27. if (createToken($username, $email, gravatar($email), $defaults['group'], $defaults['group_id'], $GLOBALS['organizrHash'], 7)) {
  28. writeLoginLog($username, 'success');
  29. writeLog('success', 'Login Function - A User has logged in', $username);
  30. return true;
  31. }
  32. } else {
  33. writeLog('error', 'Registration Function - An error occurred', $username);
  34. return 'username taken';
  35. }
  36. return false;
  37. }
  38. function checkPlexUser($username)
  39. {
  40. try {
  41. if (!empty($GLOBALS['plexToken'])) {
  42. $url = 'https://plex.tv/pms/friends/all';
  43. $headers = array(
  44. 'X-Plex-Token' => $GLOBALS['plexToken'],
  45. );
  46. $response = Requests::get($url, $headers);
  47. if ($response->success) {
  48. libxml_use_internal_errors(true);
  49. $userXML = simplexml_load_string($response->body);
  50. if (is_array($userXML) || is_object($userXML)) {
  51. $usernameLower = strtolower($username);
  52. foreach ($userXML as $child) {
  53. if (isset($child['username']) && strtolower($child['username']) == $usernameLower || isset($child['email']) && strtolower($child['email']) == $usernameLower) {
  54. return true;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. return false;
  61. } catch (Requests_Exception $e) {
  62. writeLog('success', 'Plex User Check Function - Error: ' . $e->getMessage(), $username);
  63. }
  64. return false;
  65. }
  66. function plugin_auth_plex($username, $password)
  67. {
  68. try {
  69. $usernameLower = strtolower($username);
  70. if ((!empty($GLOBALS['plexAdmin']) && strtolower($GLOBALS['plexAdmin']) == $usernameLower) || checkPlexUser($username)) {
  71. //Login User
  72. $url = 'https://plex.tv/users/sign_in.json';
  73. $headers = array(
  74. 'Accept' => 'application/json',
  75. 'Content-Type' => 'application/x-www-form-urlencoded',
  76. 'X-Plex-Product' => 'Organizr',
  77. 'X-Plex-Version' => '2.0',
  78. 'X-Plex-Client-Identifier' => '01010101-10101010',
  79. );
  80. $data = array(
  81. 'user[login]' => $username,
  82. 'user[password]' => $password,
  83. );
  84. $response = Requests::post($url, $headers, $data);
  85. if ($response->success) {
  86. $json = json_decode($response->body, true);
  87. if ((is_array($json) && isset($json['user']) && isset($json['user']['username'])) && strtolower($json['user']['username']) == $usernameLower || strtolower($json['user']['email']) == $usernameLower) {
  88. //writeLog("success", $json['user']['username']." was logged into organizr using plex credentials");
  89. return array(
  90. 'username' => $json['user']['username'],
  91. 'email' => $json['user']['email'],
  92. 'image' => $json['user']['thumb'],
  93. 'token' => $json['user']['authToken']
  94. );
  95. }
  96. }
  97. }
  98. return false;
  99. } catch (Requests_Exception $e) {
  100. writeLog('success', 'Plex Auth Function - Error: ' . $e->getMessage(), $username);
  101. }
  102. return false;
  103. }
  104. if (function_exists('ldap_connect')) {
  105. // Pass credentials to LDAP backend
  106. function plugin_auth_ldap($username, $password)
  107. {
  108. if (!empty($GLOBALS['authBaseDN']) && !empty($GLOBALS['authBackendHost'])) {
  109. $ldapServers = explode(',', $GLOBALS['authBackendHost']);
  110. foreach ($ldapServers as $key => $value) {
  111. // Calculate parts
  112. $digest = parse_url(trim($value));
  113. $scheme = strtolower((isset($digest['scheme']) ? $digest['scheme'] : 'ldap'));
  114. $host = (isset($digest['host']) ? $digest['host'] : (isset($digest['path']) ? $digest['path'] : ''));
  115. $port = (isset($digest['port']) ? $digest['port'] : (strtolower($scheme) == 'ldap' ? 389 : 636));
  116. // Reassign
  117. $ldapServers[$key] = $scheme . '://' . $host . ':' . $port;
  118. }
  119. $ldap = ldap_connect(implode(' ', $ldapServers));
  120. ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
  121. ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
  122. $bind = @ldap_bind($ldap, sprintf($GLOBALS['authBaseDN'], $username), $password);
  123. return ($bind) ? true : false;
  124. }
  125. return false;
  126. }
  127. } else {
  128. // Ldap Auth Missing Dependency
  129. function plugin_auth_ldap_disabled()
  130. {
  131. return 'LDAP - Disabled (Dependency: php-ldap missing!)';
  132. }
  133. }
  134. // Pass credentials to FTP backend
  135. function plugin_auth_ftp($username, $password)
  136. {
  137. // Calculate parts
  138. $digest = parse_url($GLOBALS['authBackendHost']);
  139. $scheme = strtolower((isset($digest['scheme']) ? $digest['scheme'] : (function_exists('ftp_ssl_connect') ? 'ftps' : 'ftp')));
  140. $host = (isset($digest['host']) ? $digest['host'] : (isset($digest['path']) ? $digest['path'] : ''));
  141. $port = (isset($digest['port']) ? $digest['port'] : 21);
  142. // Determine Connection Type
  143. if ($scheme == 'ftps') {
  144. $conn_id = ftp_ssl_connect($host, $port, 20);
  145. } elseif ($scheme == 'ftp') {
  146. $conn_id = ftp_connect($host, $port, 20);
  147. } else {
  148. return false;
  149. }
  150. // Check if valid FTP connection
  151. if ($conn_id) {
  152. // Attempt login
  153. @$login_result = ftp_login($conn_id, $username, $password);
  154. ftp_close($conn_id);
  155. // Return Result
  156. if ($login_result) {
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. } else {
  162. return false;
  163. }
  164. }
  165. // Pass credentials to Emby Backend
  166. function plugin_auth_emby_local($username, $password)
  167. {
  168. try {
  169. $url = qualifyURL($GLOBALS['embyURL']) . '/Users/AuthenticateByName';
  170. $headers = array(
  171. 'Authorization' => 'MediaBrowser UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", Client="None", Device="Organizr", DeviceId="xxx", Version="1.0.0.0"',
  172. 'Content-Type' => 'application/json',
  173. );
  174. $data = array(
  175. 'Username' => $username,
  176. 'Password' => sha1($password),
  177. 'PasswordMd5' => md5($password),
  178. );
  179. $response = Requests::post($url, $headers, json_encode($data));
  180. if ($response->success) {
  181. $json = json_decode($response->body, true);
  182. if (is_array($json) && isset($json['SessionInfo']) && isset($json['User']) && $json['User']['HasPassword'] == true) {
  183. // Login Success - Now Logout Emby Session As We No Longer Need It
  184. $headers = array(
  185. 'X-Mediabrowser-Token' => $json['AccessToken'],
  186. );
  187. $response = Requests::post(qualifyURL($GLOBALS['embyURL']) . '/Sessions/Logout', $headers, array());
  188. if ($response->success) {
  189. return true;
  190. }
  191. }
  192. }
  193. return false;
  194. } catch (Requests_Exception $e) {
  195. writeLog('error', 'Emby Local Auth Function - Error: ' . $e->getMessage(), $username);
  196. }
  197. return false;
  198. }
  199. // Authenticate against emby connect
  200. function plugin_auth_emby_connect($username, $password)
  201. {
  202. try {
  203. // Get A User
  204. $connectId = '';
  205. $url = qualifyURL($GLOBALS['embyURL']) . '/Users?api_key=' . $GLOBALS['embyToken'];
  206. $response = Requests::get($url);
  207. if ($response->success) {
  208. $json = json_decode($response->body, true);
  209. if (is_array($json)) {
  210. foreach ($json as $key => $value) { // Scan for this user
  211. if (isset($value['ConnectUserName']) && isset($value['ConnectUserId'])) { // Qualify as connect account
  212. if ($value['ConnectUserName'] == $username || $value['Name'] == $username) {
  213. $connectId = $value['ConnectUserId'];
  214. writeLog('success', 'Emby Connect Auth Function - Found User', $username);
  215. break;
  216. }
  217. }
  218. }
  219. if ($connectId) {
  220. $connectURL = 'https://connect.emby.media/service/user/authenticate';
  221. $headers = array(
  222. 'Accept' => 'application/json',
  223. 'Content-Type' => 'application/x-www-form-urlencoded',
  224. );
  225. $data = array(
  226. 'nameOrEmail' => $username,
  227. 'rawpw' => $password,
  228. );
  229. $response = Requests::post($connectURL, $headers, $data);
  230. if ($response->success) {
  231. $json = json_decode($response->body, true);
  232. if (is_array($json) && isset($json['AccessToken']) && isset($json['User']) && $json['User']['Id'] == $connectId) {
  233. return array(
  234. 'email' => $json['User']['Email'],
  235. 'image' => $json['User']['ImageUrl'],
  236. );
  237. }
  238. }
  239. }
  240. }
  241. }
  242. return false;
  243. } catch (Requests_Exception $e) {
  244. writeLog('error', 'Emby Connect Auth Function - Error: ' . $e->getMessage(), $username);
  245. return false;
  246. }
  247. }
  248. // Authenticate Against Emby Local (first) and Emby Connect
  249. function plugin_auth_emby_all($username, $password)
  250. {
  251. $localResult = plugin_auth_emby_local($username, $password);
  252. if ($localResult) {
  253. return $localResult;
  254. } else {
  255. return plugin_auth_emby_connect($username, $password);
  256. }
  257. }