plugin.php 18 KB

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