auth-functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 checkPlexToken($token = '')
  39. {
  40. try {
  41. if (($token !== '')) {
  42. $url = 'https://plex.tv/users/account.json';
  43. $headers = array(
  44. 'X-Plex-Token' => $token,
  45. 'Content-Type' => 'application/json',
  46. 'Accept' => 'application/json'
  47. );
  48. $response = Requests::get($url, $headers);
  49. if ($response->success) {
  50. return json_decode($response->body, true);
  51. }
  52. } else {
  53. return false;
  54. }
  55. } catch (Requests_Exception $e) {
  56. writeLog('success', 'Plex Token Check Function - Error: ' . $e->getMessage(), SYSTEM);
  57. }
  58. return false;
  59. }
  60. function checkPlexUser($username)
  61. {
  62. try {
  63. if (!empty($GLOBALS['plexToken'])) {
  64. $url = 'https://plex.tv/api/users';
  65. $headers = array(
  66. 'X-Plex-Token' => $GLOBALS['plexToken'],
  67. );
  68. $response = Requests::get($url, $headers);
  69. if ($response->success) {
  70. libxml_use_internal_errors(true);
  71. $userXML = simplexml_load_string($response->body);
  72. if (is_array($userXML) || is_object($userXML)) {
  73. $usernameLower = strtolower($username);
  74. foreach ($userXML as $child) {
  75. if (isset($child['username']) && strtolower($child['username']) == $usernameLower || isset($child['email']) && strtolower($child['email']) == $usernameLower) {
  76. if ((string)$child->Server['machineIdentifier'] == $GLOBALS['plexID']) {
  77. return true;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. return false;
  85. } catch (Requests_Exception $e) {
  86. writeLog('success', 'Plex User Check Function - Error: ' . $e->getMessage(), $username);
  87. }
  88. return false;
  89. }
  90. function allPlexUsers($newOnly = false)
  91. {
  92. try {
  93. if (!empty($GLOBALS['plexToken'])) {
  94. $url = 'https://plex.tv/api/users';
  95. $headers = array(
  96. 'X-Plex-Token' => $GLOBALS['plexToken'],
  97. );
  98. $response = Requests::get($url, $headers);
  99. if ($response->success) {
  100. libxml_use_internal_errors(true);
  101. $userXML = simplexml_load_string($response->body);
  102. if (is_array($userXML) || is_object($userXML)) {
  103. $results = array();
  104. foreach ($userXML as $child) {
  105. if (((string)$child['restricted'] == '0')) {
  106. if ($newOnly) {
  107. $taken = usernameTaken((string)$child['username'], (string)$child['email']);
  108. if (!$taken) {
  109. $results[] = array(
  110. 'username' => (string)$child['username'],
  111. 'email' => (string)$child['email']
  112. );
  113. }
  114. } else {
  115. $results[] = array(
  116. 'username' => (string)$child['username'],
  117. 'email' => (string)$child['email'],
  118. );
  119. }
  120. }
  121. }
  122. return $results;
  123. }
  124. }
  125. }
  126. return false;
  127. } catch (Requests_Exception $e) {
  128. writeLog('success', 'Plex User Function - Error: ' . $e->getMessage(), $username);
  129. }
  130. return false;
  131. }
  132. function plugin_auth_plex($username, $password)
  133. {
  134. try {
  135. $usernameLower = strtolower($username);
  136. if ((!empty($GLOBALS['plexAdmin']) && strtolower($GLOBALS['plexAdmin']) == $usernameLower) || checkPlexUser($username)) {
  137. //Login User
  138. $url = 'https://plex.tv/users/sign_in.json';
  139. $headers = array(
  140. 'Accept' => 'application/json',
  141. 'Content-Type' => 'application/x-www-form-urlencoded',
  142. 'X-Plex-Product' => 'Organizr',
  143. 'X-Plex-Version' => '2.0',
  144. 'X-Plex-Client-Identifier' => $GLOBALS['uuid'],
  145. );
  146. $data = array(
  147. 'user[login]' => $username,
  148. 'user[password]' => $password,
  149. );
  150. $response = Requests::post($url, $headers, $data);
  151. if ($response->success) {
  152. $json = json_decode($response->body, true);
  153. if ((is_array($json) && isset($json['user']) && isset($json['user']['username'])) && strtolower($json['user']['username']) == $usernameLower || strtolower($json['user']['email']) == $usernameLower) {
  154. //writeLog("success", $json['user']['username']." was logged into organizr using plex credentials");
  155. return array(
  156. 'username' => $json['user']['username'],
  157. 'email' => $json['user']['email'],
  158. 'image' => $json['user']['thumb'],
  159. 'token' => $json['user']['authToken']
  160. );
  161. }
  162. }
  163. }
  164. return false;
  165. } catch (Requests_Exception $e) {
  166. writeLog('success', 'Plex Auth Function - Error: ' . $e->getMessage(), $username);
  167. }
  168. return false;
  169. }
  170. if (function_exists('ldap_connect')) {
  171. // Pass credentials to LDAP backend
  172. function plugin_auth_ldap($username, $password)
  173. {
  174. if (!empty($GLOBALS['authBaseDN']) && !empty($GLOBALS['authBackendHost'])) {
  175. $ldapServers = explode(',', $GLOBALS['authBackendHost']);
  176. foreach ($ldapServers as $key => $value) {
  177. // Calculate parts
  178. $digest = parse_url(trim($value));
  179. $scheme = strtolower((isset($digest['scheme']) ? $digest['scheme'] : 'ldap'));
  180. $host = (isset($digest['host']) ? $digest['host'] : (isset($digest['path']) ? $digest['path'] : ''));
  181. $port = (isset($digest['port']) ? $digest['port'] : (strtolower($scheme) == 'ldap' ? 389 : 636));
  182. // Reassign
  183. $ldapServers[$key] = $scheme . '://' . $host . ':' . $port;
  184. }
  185. $ldap = ldap_connect(implode(' ', $ldapServers));
  186. ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
  187. ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
  188. $bind = @ldap_bind($ldap, sprintf($GLOBALS['authBaseDN'], $username), $password);
  189. return ($bind) ? true : false;
  190. }
  191. return false;
  192. }
  193. } else {
  194. // Ldap Auth Missing Dependency
  195. function plugin_auth_ldap_disabled()
  196. {
  197. return 'LDAP - Disabled (Dependency: php-ldap missing!)';
  198. }
  199. }
  200. // Pass credentials to FTP backend
  201. function plugin_auth_ftp($username, $password)
  202. {
  203. // Calculate parts
  204. $digest = parse_url($GLOBALS['authBackendHost']);
  205. $scheme = strtolower((isset($digest['scheme']) ? $digest['scheme'] : (function_exists('ftp_ssl_connect') ? 'ftps' : 'ftp')));
  206. $host = (isset($digest['host']) ? $digest['host'] : (isset($digest['path']) ? $digest['path'] : ''));
  207. $port = (isset($digest['port']) ? $digest['port'] : 21);
  208. // Determine Connection Type
  209. if ($scheme == 'ftps') {
  210. $conn_id = ftp_ssl_connect($host, $port, 20);
  211. } elseif ($scheme == 'ftp') {
  212. $conn_id = ftp_connect($host, $port, 20);
  213. } else {
  214. return false;
  215. }
  216. // Check if valid FTP connection
  217. if ($conn_id) {
  218. // Attempt login
  219. @$login_result = ftp_login($conn_id, $username, $password);
  220. ftp_close($conn_id);
  221. // Return Result
  222. if ($login_result) {
  223. return true;
  224. } else {
  225. return false;
  226. }
  227. } else {
  228. return false;
  229. }
  230. }
  231. // Pass credentials to Emby Backend
  232. function plugin_auth_emby_local($username, $password)
  233. {
  234. try {
  235. $url = qualifyURL($GLOBALS['embyURL']) . '/Users/AuthenticateByName';
  236. $headers = array(
  237. 'Authorization' => 'MediaBrowser UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", Client="None", Device="Organizr", DeviceId="xxx", Version="1.0.0.0"',
  238. 'Content-Type' => 'application/json',
  239. );
  240. $data = array(
  241. 'Username' => $username,
  242. 'Password' => sha1($password),
  243. 'PasswordMd5' => md5($password),
  244. );
  245. $response = Requests::post($url, $headers, json_encode($data));
  246. if ($response->success) {
  247. $json = json_decode($response->body, true);
  248. if (is_array($json) && isset($json['SessionInfo']) && isset($json['User']) && $json['User']['HasPassword'] == true) {
  249. // Login Success - Now Logout Emby Session As We No Longer Need It
  250. $headers = array(
  251. 'X-Mediabrowser-Token' => $json['AccessToken'],
  252. );
  253. $response = Requests::post(qualifyURL($GLOBALS['embyURL']) . '/Sessions/Logout', $headers, array());
  254. if ($response->success) {
  255. return true;
  256. }
  257. }
  258. }
  259. return false;
  260. } catch (Requests_Exception $e) {
  261. writeLog('error', 'Emby Local Auth Function - Error: ' . $e->getMessage(), $username);
  262. }
  263. return false;
  264. }
  265. // Authenticate against emby connect
  266. function plugin_auth_emby_connect($username, $password)
  267. {
  268. try {
  269. // Get A User
  270. $connectId = '';
  271. $url = qualifyURL($GLOBALS['embyURL']) . '/Users?api_key=' . $GLOBALS['embyToken'];
  272. $response = Requests::get($url);
  273. if ($response->success) {
  274. $json = json_decode($response->body, true);
  275. if (is_array($json)) {
  276. foreach ($json as $key => $value) { // Scan for this user
  277. if (isset($value['ConnectUserName']) && isset($value['ConnectUserId'])) { // Qualify as connect account
  278. if ($value['ConnectUserName'] == $username || $value['Name'] == $username) {
  279. $connectId = $value['ConnectUserId'];
  280. writeLog('success', 'Emby Connect Auth Function - Found User', $username);
  281. break;
  282. }
  283. }
  284. }
  285. if ($connectId) {
  286. $connectURL = 'https://connect.emby.media/service/user/authenticate';
  287. $headers = array(
  288. 'Accept' => 'application/json',
  289. 'Content-Type' => 'application/x-www-form-urlencoded',
  290. );
  291. $data = array(
  292. 'nameOrEmail' => $username,
  293. 'rawpw' => $password,
  294. );
  295. $response = Requests::post($connectURL, $headers, $data);
  296. if ($response->success) {
  297. $json = json_decode($response->body, true);
  298. if (is_array($json) && isset($json['AccessToken']) && isset($json['User']) && $json['User']['Id'] == $connectId) {
  299. return array(
  300. 'email' => $json['User']['Email'],
  301. 'image' => $json['User']['ImageUrl'],
  302. );
  303. }
  304. }
  305. }
  306. }
  307. }
  308. return false;
  309. } catch (Requests_Exception $e) {
  310. writeLog('error', 'Emby Connect Auth Function - Error: ' . $e->getMessage(), $username);
  311. return false;
  312. }
  313. }
  314. // Authenticate Against Emby Local (first) and Emby Connect
  315. function plugin_auth_emby_all($username, $password)
  316. {
  317. $localResult = plugin_auth_emby_local($username, $password);
  318. if ($localResult) {
  319. return $localResult;
  320. } else {
  321. return plugin_auth_emby_connect($username, $password);
  322. }
  323. }