auth-functions.php 10 KB

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