plugin.php 17 KB

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