auth-functions.php 11 KB

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