invites.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. // PLUGIN INFORMATION
  3. $GLOBALS['plugins'][]['Invites'] = array( // Plugin Name
  4. 'name' => 'Invites', // Plugin Name
  5. 'author' => 'CauseFX', // Who wrote the plugin
  6. 'category' => 'Management', // One to Two Word Description
  7. 'link' => '', // Link to plugin info
  8. 'license' => 'personal', // License Type use , for multiple
  9. 'idPrefix' => 'INVITES', // html element id prefix
  10. 'configPrefix' => 'INVITES', // config file prefix for array items without the hypen
  11. 'version' => '1.0.0', // SemVer of plugin
  12. 'image' => 'plugins/images/invites.png', // 1:1 non transparent image for plugin
  13. 'settings' => true, // does plugin need a settings page? true or false
  14. 'homepage' => false // Is plugin for use on homepage? true or false
  15. );
  16. class Invites extends Organizr
  17. {
  18. public function _invitesPluginGetCodes()
  19. {
  20. $response = [
  21. array(
  22. 'function' => 'fetchAll',
  23. 'query' => 'SELECT * FROM invites'
  24. )
  25. ];
  26. return $this->processQueries($response);
  27. }
  28. public function _invitesPluginCreateCode($array)
  29. {
  30. $code = ($array['code']) ?? null;
  31. $username = ($array['username']) ?? null;
  32. $email = ($array['email']) ?? null;
  33. if (!$code) {
  34. $this->setAPIResponse('error', 'Code not supplied', 409);
  35. return false;
  36. }
  37. if (!$username) {
  38. $this->setAPIResponse('error', 'Username not supplied', 409);
  39. return false;
  40. }
  41. if (!$email) {
  42. $this->setAPIResponse('error', 'Email not supplied', 409);
  43. return false;
  44. }
  45. $newCode = [
  46. 'code' => $code,
  47. 'email' => $email,
  48. 'username' => $username,
  49. 'valid' => 'Yes',
  50. 'type' => $this->config['INVITES-type-include'],
  51. ];
  52. $response = [
  53. array(
  54. 'function' => 'query',
  55. 'query' => array(
  56. 'INSERT INTO [invites]',
  57. $newCode
  58. )
  59. )
  60. ];
  61. $query = $this->processQueries($response);
  62. if ($query) {
  63. $this->writeLog('success', 'Invite Management Function - Added Invite [' . $code . ']', $this->user['username']);
  64. if ($this->config['PHPMAILER-enabled']) {
  65. $PhpMailer = new PhpMailer();
  66. $emailTemplate = array(
  67. 'type' => 'invite',
  68. 'body' => $this->config['PHPMAILER-emailTemplateInviteUser'],
  69. 'subject' => $this->config['PHPMAILER-emailTemplateInviteUserSubject'],
  70. 'user' => $username,
  71. 'password' => null,
  72. 'inviteCode' => $code,
  73. );
  74. $emailTemplate = $PhpMailer->_phpMailerPluginEmailTemplate($emailTemplate);
  75. $sendEmail = array(
  76. 'to' => $email,
  77. 'subject' => $emailTemplate['subject'],
  78. 'body' => $PhpMailer->_phpMailerPluginBuildEmail($emailTemplate),
  79. );
  80. $PhpMailer->_phpMailerPluginSendEmail($sendEmail);
  81. }
  82. $this->setAPIResponse('success', 'Invite Code: ' . $code . ' has been created', 200);
  83. return true;
  84. } else {
  85. return false;
  86. }
  87. }
  88. public function _invitesPluginVerifyCode($code)
  89. {
  90. $response = [
  91. array(
  92. 'function' => 'fetchAll',
  93. 'query' => array(
  94. 'SELECT * FROM invites WHERE valid = "Yes" AND code = ? COLLATE NOCASE',
  95. $code
  96. )
  97. )
  98. ];
  99. if ($this->processQueries($response)) {
  100. $this->setAPIResponse('success', 'Code has been verified', 200);
  101. return true;
  102. } else {
  103. $this->setAPIResponse('error', 'Code is invalid', 401);
  104. return false;
  105. }
  106. }
  107. public function _invitesPluginDeleteCode($code)
  108. {
  109. $response = [
  110. array(
  111. 'function' => 'fetchAll',
  112. 'query' => 'SELECT * FROM invites WHERE code = ? COLLATE NOCASE',
  113. $code
  114. )
  115. ];
  116. $info = $this->processQueries($response);
  117. if (!$info) {
  118. $this->setAPIResponse('error', 'Code not found', 404);
  119. return false;
  120. }
  121. $response = [
  122. array(
  123. 'function' => 'query',
  124. 'query' => array(
  125. 'DELETE FROM invites WHERE code = ? COLLATE NOCASE',
  126. $code
  127. )
  128. )
  129. ];
  130. $this->setAPIResponse('success', 'Code has been deleted', 200);
  131. return $this->processQueries($response);
  132. }
  133. public function _invitesPluginUseCode($code, $array)
  134. {
  135. $code = ($code) ?? null;
  136. $usedBy = ($array['usedby']) ?? null;
  137. $now = date("Y-m-d H:i:s");
  138. $currentIP = $this->userIP();
  139. if ($this->_invitesPluginVerifyCode($code)) {
  140. $updateCode = [
  141. 'valid' => 'No',
  142. 'usedby' => $usedBy,
  143. 'dateused' => $now,
  144. 'ip' => $currentIP
  145. ];
  146. $response = [
  147. array(
  148. 'function' => 'query',
  149. 'query' => array(
  150. 'UPDATE invites SET',
  151. $updateCode,
  152. 'WHERE code=? COLLATE NOCASE',
  153. $code
  154. )
  155. )
  156. ];
  157. $query = $this->processQueries($response);
  158. $this->writeLog('success', 'Invite Management Function - Invite Used [' . $code . ']', 'SYSTEM');
  159. return $this->_invitesPluginAction($usedBy, 'share', $this->config['INVITES-type-include']);
  160. } else {
  161. return false;
  162. }
  163. }
  164. public function _invitesPluginLibraryList($type = null)
  165. {
  166. switch ($type) {
  167. case 'plex':
  168. if (!empty($this->config['plexToken']) && !empty($this->config['plexID'])) {
  169. $url = 'https://plex.tv/api/servers/' . $this->config['plexID'];
  170. try {
  171. $headers = array(
  172. "Accept" => "application/json",
  173. "X-Plex-Token" => $this->config['plexToken']
  174. );
  175. $response = Requests::get($url, $headers, array());
  176. libxml_use_internal_errors(true);
  177. if ($response->success) {
  178. $libraryList = array();
  179. $plex = simplexml_load_string($response->body);
  180. foreach ($plex->Server->Section as $child) {
  181. $libraryList['libraries'][(string)$child['title']] = (string)$child['id'];
  182. }
  183. $libraryList = array_change_key_case($libraryList, CASE_LOWER);
  184. return $libraryList;
  185. }
  186. } catch (Requests_Exception $e) {
  187. $this->writeLog('error', 'Plex Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  188. return false;
  189. };
  190. }
  191. break;
  192. default:
  193. # code...
  194. break;
  195. }
  196. return false;
  197. }
  198. public function _invitesPluginGetSettings()
  199. {
  200. if ($this->config['plexID'] !== '' && $this->config['plexToken'] !== '' && $this->config['INVITES-type-include'] == 'plex') {
  201. $loop = $this->_invitesPluginLibraryList($this->config['INVITES-type-include'])['libraries'];
  202. foreach ($loop as $key => $value) {
  203. $libraryList[] = array(
  204. 'name' => $key,
  205. 'value' => $value
  206. );
  207. }
  208. } else {
  209. $libraryList = array(
  210. array(
  211. 'name' => 'Refresh page to update List',
  212. 'value' => '',
  213. 'disabled' => true,
  214. ),
  215. );
  216. }
  217. return array(
  218. 'Backend' => array(
  219. array(
  220. 'type' => 'select',
  221. 'name' => 'INVITES-type-include',
  222. 'label' => 'Media Server',
  223. 'value' => $this->config['INVITES-type-include'],
  224. 'options' => array(
  225. array(
  226. 'name' => 'N/A',
  227. 'value' => 'n/a'
  228. ),
  229. array(
  230. 'name' => 'Plex',
  231. 'value' => 'plex'
  232. ),
  233. array(
  234. 'name' => 'Emby',
  235. 'value' => 'emby'
  236. )
  237. )
  238. )
  239. ),
  240. 'Plex Settings' => array(
  241. array(
  242. 'type' => 'password-alt',
  243. 'name' => 'plexToken',
  244. 'label' => 'Plex Token',
  245. 'value' => $this->config['plexToken'],
  246. 'placeholder' => 'Use Get Token Button'
  247. ),
  248. array(
  249. 'type' => 'button',
  250. 'label' => 'Get Plex Token',
  251. 'icon' => 'fa fa-ticket',
  252. 'text' => 'Retrieve',
  253. 'attr' => 'onclick="showPlexTokenForm(\'#INVITES-settings-items [name=plexToken]\')"'
  254. ),
  255. array(
  256. 'type' => 'password-alt',
  257. 'name' => 'plexID',
  258. 'label' => 'Plex Machine',
  259. 'value' => $this->config['plexID'],
  260. 'placeholder' => 'Use Get Plex Machine Button'
  261. ),
  262. array(
  263. 'type' => 'button',
  264. 'label' => 'Get Plex Machine',
  265. 'icon' => 'fa fa-id-badge',
  266. 'text' => 'Retrieve',
  267. 'attr' => 'onclick="showPlexMachineForm(\'#INVITES-settings-items [name=plexID]\')"'
  268. ),
  269. array(
  270. 'type' => 'select2',
  271. 'class' => 'select2-multiple',
  272. 'id' => 'invite-select',
  273. 'name' => 'INVITES-plexLibraries',
  274. 'label' => 'Libraries',
  275. 'value' => $this->config['INVITES-plexLibraries'],
  276. 'options' => $libraryList
  277. ),
  278. array(
  279. 'type' => 'text',
  280. 'name' => 'INVITES-plex-tv-labels',
  281. 'label' => 'TV Labels (comma separated)',
  282. 'value' => $this->config['INVITES-plex-tv-labels'],
  283. 'placeholder' => 'All'
  284. ),
  285. array(
  286. 'type' => 'text',
  287. 'name' => 'INVITES-plex-movies-labels',
  288. 'label' => 'Movies Labels (comma separated)',
  289. 'value' => $this->config['INVITES-plex-movies-labels'],
  290. 'placeholder' => 'All'
  291. ),
  292. array(
  293. 'type' => 'text',
  294. 'name' => 'INVITES-plex-music-labels',
  295. 'label' => 'Music Labels (comma separated)',
  296. 'value' => $this->config['INVITES-plex-music-labels'],
  297. 'placeholder' => 'All'
  298. ),
  299. ),
  300. 'Emby Settings' => array(
  301. array(
  302. 'type' => 'password-alt',
  303. 'name' => 'embyToken',
  304. 'label' => 'Emby API key',
  305. 'value' => $this->config['embyToken'],
  306. 'placeholder' => 'enter key from emby'
  307. ),
  308. array(
  309. 'type' => 'text',
  310. 'name' => 'embyURL',
  311. 'label' => 'Emby server adress',
  312. 'value' => $this->config['embyURL'],
  313. 'placeholder' => 'localhost:8086'
  314. ),
  315. array(
  316. 'type' => 'text',
  317. 'name' => 'INVITES-EmbyTemplate',
  318. 'label' => 'Emby User to be used as template for new users',
  319. 'value' => $this->config['INVITES-EmbyTemplate'],
  320. 'placeholder' => 'AdamSmith'
  321. )
  322. ),
  323. 'FYI' => array(
  324. array(
  325. 'type' => 'html',
  326. 'label' => 'Note',
  327. 'html' => 'After enabling for the first time, please reload the page - Menu is located under User menu on top right'
  328. )
  329. )
  330. );
  331. }
  332. public function _invitesPluginAction($username, $action = null, $type = null)
  333. {
  334. if ($action == null) {
  335. $this->setAPIResponse('error', 'No Action supplied', 409);
  336. return false;
  337. }
  338. switch ($type) {
  339. case 'plex':
  340. if (!empty($this->config['plexToken']) && !empty($this->config['plexID'])) {
  341. $url = "https://plex.tv/api/servers/" . $this->config['plexID'] . "/shared_servers/";
  342. if ($this->config['INVITES-plexLibraries'] !== "") {
  343. $libraries = explode(',', $this->config['INVITES-plexLibraries']);
  344. } else {
  345. $libraries = '';
  346. }
  347. if ($this->config['INVITES-plex-tv-labels'] !== "") {
  348. $tv_labels = "label=" . $this->config['INVITES-plex-tv-labels'];
  349. } else {
  350. $tv_labels = "";
  351. }
  352. if ($this->config['INVITES-plex-movies-labels'] !== "") {
  353. $movies_labels = "label=" . $this->config['INVITES-plex-movies-labels'];
  354. } else {
  355. $movies_labels = "";
  356. }
  357. if ($this->config['INVITES-plex-music-labels'] !== "") {
  358. $music_labels = "label=" . $this->config['INVITES-plex-music-labels'];
  359. } else {
  360. $music_labels = "";
  361. }
  362. $headers = array(
  363. "Accept" => "application/json",
  364. "Content-Type" => "application/json",
  365. "X-Plex-Token" => $this->config['plexToken']
  366. );
  367. $data = array(
  368. "server_id" => $this->config['plexID'],
  369. "shared_server" => array(
  370. "library_section_ids" => $libraries,
  371. "invited_email" => $username
  372. ),
  373. "sharing_settings" => array(
  374. "filterTelevision" => $tv_labels,
  375. "filterMovies" => $movies_labels,
  376. "filterMusic" => $music_labels
  377. )
  378. );
  379. try {
  380. switch ($action) {
  381. case 'share':
  382. $response = Requests::post($url, $headers, json_encode($data), array());
  383. break;
  384. case 'unshare':
  385. $id = (is_numeric($username) ? $username : $this->_invitesPluginConvertPlexName($username, "id"));
  386. $url = $url . $id;
  387. $response = Requests::delete($url, $headers, array());
  388. break;
  389. default:
  390. $this->setAPIResponse('error', 'No Action supplied', 409);
  391. return false;
  392. }
  393. if ($response->success) {
  394. $this->writeLog('success', 'Plex Invite Function - Plex User now has access to system', $username);
  395. $this->setAPIResponse('success', 'Plex User now has access to system', 200);
  396. return true;
  397. } else {
  398. switch ($response->status_code) {
  399. case 400:
  400. $this->writeLog('error', 'Plex Invite Function - Plex User already has access', $username);
  401. $this->setAPIResponse('error', 'Plex User already has access', 409);
  402. return false;
  403. case 401:
  404. $this->writeLog('error', 'Plex Invite Function - Incorrect Token', 'SYSTEM');
  405. $this->setAPIResponse('error', 'Incorrect Token', 409);
  406. return false;
  407. case 404:
  408. $this->writeLog('error', 'Plex Invite Function - Libraries not setup correct [' . $this->config['INVITES-plexLibraries'] . ']', 'SYSTEM');
  409. $this->setAPIResponse('error', 'Libraries not setup correct', 409);
  410. return false;
  411. default:
  412. $this->writeLog('error', 'Plex Invite Function - An error occurred [' . $response->status_code . ']', $username);
  413. $this->setAPIResponse('error', 'An Error Occurred', 409);
  414. return false;
  415. }
  416. }
  417. } catch (Requests_Exception $e) {
  418. $this->writeLog('error', 'Plex Invite Function - Error: ' . $e->getMessage(), 'SYSTEM');
  419. $this->setAPIResponse('error', $e->getMessage(), 409);
  420. return false;
  421. };
  422. } else {
  423. $this->writeLog('error', 'Plex Invite Function - Plex Token/ID not set', 'SYSTEM');
  424. $this->setAPIResponse('error', 'Plex Token/ID not set', 409);
  425. return false;
  426. }
  427. break;
  428. case 'emby':
  429. try {
  430. #add emby user to system
  431. $this->setAPIResponse('success', 'User now has access to system', 200);
  432. return true;
  433. } catch (Requests_Exception $e) {
  434. $this->writeLog('error', 'Emby Invite Function - Error: ' . $e->getMessage(), 'SYSTEM');
  435. $this->setAPIResponse('error', $e->getMessage(), 409);
  436. return false;
  437. }
  438. default:
  439. return false;
  440. }
  441. return false;
  442. }
  443. public function _invitesPluginConvertPlexName($user, $type)
  444. {
  445. $array = $this->userList('plex');
  446. switch ($type) {
  447. case "username":
  448. case "u":
  449. $plexUser = array_search($user, $array['users']);
  450. break;
  451. case "id":
  452. if (array_key_exists(strtolower($user), $array['users'])) {
  453. $plexUser = $array['users'][strtolower($user)];
  454. }
  455. break;
  456. default:
  457. $plexUser = false;
  458. }
  459. return (!empty($plexUser) ? $plexUser : null);
  460. }
  461. }