plugin.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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->logger->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->logger->info('Updated Invites Database');
  62. } else {
  63. $this->logger->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. $response = [
  77. array(
  78. 'function' => 'fetchAll',
  79. 'query' => array(
  80. 'SELECT * FROM invites WHERE invitedby = ?',
  81. $this->user['username']
  82. )
  83. )
  84. ];
  85. }
  86. return $this->processQueries($response);
  87. }
  88. public function _invitesPluginCreateCode($array)
  89. {
  90. $code = ($array['code']) ?? null;
  91. $username = ($array['username']) ?? null;
  92. $email = ($array['email']) ?? null;
  93. $invites = $this->_invitesPluginGetCodes();
  94. $inviteCount = count($invites);
  95. if (!$this->qualifyRequest(1, false)) {
  96. if ($this->config['INVITES-maximum-invites'] != 0 && $inviteCount >= $this->config['INVITES-maximum-invites']) {
  97. $this->setAPIResponse('error', 'Maximum number of invites reached', 409);
  98. return false;
  99. }
  100. }
  101. if (!$code) {
  102. $this->setAPIResponse('error', 'Code not supplied', 409);
  103. return false;
  104. }
  105. if (!$username) {
  106. $this->setAPIResponse('error', 'Username not supplied', 409);
  107. return false;
  108. }
  109. if (!$email) {
  110. $this->setAPIResponse('error', 'Email not supplied', 409);
  111. return false;
  112. }
  113. $newCode = [
  114. 'code' => $code,
  115. 'email' => $email,
  116. 'username' => $username,
  117. 'valid' => 'Yes',
  118. 'type' => $this->config['INVITES-type-include'],
  119. 'invitedby' => $this->user['username'],
  120. 'date' => gmdate('Y-m-d H:i:s')
  121. ];
  122. $response = [
  123. array(
  124. 'function' => 'query',
  125. 'query' => array(
  126. 'INSERT INTO [invites]',
  127. $newCode
  128. )
  129. )
  130. ];
  131. $query = $this->processQueries($response);
  132. if ($query) {
  133. $this->setLoggerChannel('Invites')->info('Added Invite [' . $code . ']');
  134. if ($this->config['PHPMAILER-enabled']) {
  135. $PhpMailer = new PhpMailer();
  136. $emailTemplate = array(
  137. 'type' => 'invite',
  138. 'body' => $this->config['PHPMAILER-emailTemplateInviteUser'],
  139. 'subject' => $this->config['PHPMAILER-emailTemplateInviteUserSubject'],
  140. 'user' => $username,
  141. 'password' => null,
  142. 'inviteCode' => $code,
  143. );
  144. $emailTemplate = $PhpMailer->_phpMailerPluginEmailTemplate($emailTemplate);
  145. $sendEmail = array(
  146. 'to' => $email,
  147. 'subject' => $emailTemplate['subject'],
  148. 'body' => $PhpMailer->_phpMailerPluginBuildEmail($emailTemplate),
  149. );
  150. $PhpMailer->_phpMailerPluginSendEmail($sendEmail);
  151. }
  152. $this->setAPIResponse('success', 'Invite Code: ' . $code . ' has been created', 200);
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. public function _invitesPluginVerifyCode($code)
  159. {
  160. $response = [
  161. array(
  162. 'function' => 'fetchAll',
  163. 'query' => array(
  164. 'SELECT * FROM invites WHERE valid = "Yes" AND code = ? COLLATE NOCASE',
  165. $code
  166. )
  167. )
  168. ];
  169. if ($this->processQueries($response)) {
  170. $this->setAPIResponse('success', 'Code has been verified', 200);
  171. return true;
  172. } else {
  173. $this->setAPIResponse('error', 'Code is invalid', 401);
  174. return false;
  175. }
  176. }
  177. public function _invitesPluginDeleteCode($code)
  178. {
  179. if ($this->qualifyRequest(1, false)) {
  180. $response = [
  181. array(
  182. 'function' => 'fetch',
  183. 'query' => array(
  184. 'SELECT * FROM invites WHERE code = ? COLLATE NOCASE',
  185. $code
  186. )
  187. )
  188. ];
  189. } else {
  190. if ($this->config['INVITES-allow-delete']) {
  191. $response = [
  192. array(
  193. 'function' => 'fetch',
  194. 'query' => array(
  195. 'SELECT * FROM invites WHERE invitedby = ? AND code = ? COLLATE NOCASE',
  196. $this->user['username'],
  197. $code
  198. )
  199. )
  200. ];
  201. } else {
  202. $this->setAPIResponse('error', 'You are not permitted to delete invites.', 409);
  203. return false;
  204. }
  205. }
  206. $info = $this->processQueries($response);
  207. if (!$info) {
  208. $this->setAPIResponse('error', 'Code not found', 404);
  209. return false;
  210. }
  211. $response = [
  212. array(
  213. 'function' => 'query',
  214. 'query' => array(
  215. 'DELETE FROM invites WHERE code = ? COLLATE NOCASE',
  216. $code
  217. )
  218. )
  219. ];
  220. $this->setAPIResponse('success', 'Code has been deleted', 200);
  221. return $this->processQueries($response);
  222. }
  223. public function _invitesPluginUseCode($code, $array)
  224. {
  225. $code = ($code) ?? null;
  226. $mail = $this->_getEmailFronInviteCode($code);
  227. $usedBy = ($array['usedby']) ?? null;
  228. $now = date("Y-m-d H:i:s");
  229. $currentIP = $this->userIP();
  230. if ($this->_invitesPluginVerifyCode($code)) {
  231. $updateCode = [
  232. 'valid' => 'No',
  233. 'usedby' => $usedBy,
  234. 'dateused' => $now,
  235. 'ip' => $currentIP
  236. ];
  237. $response = [
  238. array(
  239. 'function' => 'query',
  240. 'query' => array(
  241. 'UPDATE invites SET',
  242. $updateCode,
  243. 'WHERE code=? COLLATE NOCASE',
  244. $code
  245. )
  246. )
  247. ];
  248. $query = $this->processQueries($response);
  249. $this->setLoggerChannel('Invites')->info('Invite Used [' . $code . '] by ' . $usedBy);
  250. return $this->_invitesPluginAction($usedBy, 'share', $this->config['INVITES-type-include'], $mail);
  251. } else {
  252. return false;
  253. }
  254. }
  255. public function _invitesPluginLibraryList($type = null)
  256. {
  257. switch ($type) {
  258. case 'plex':
  259. if (!empty($this->config['plexToken']) && !empty($this->config['plexID'])) {
  260. $url = 'https://plex.tv/api/servers/' . $this->config['plexID'];
  261. try {
  262. $headers = array(
  263. "Accept" => "application/json",
  264. "X-Plex-Token" => $this->config['plexToken']
  265. );
  266. $response = Requests::get($url, $headers, array());
  267. libxml_use_internal_errors(true);
  268. if ($response->success) {
  269. $libraryList = array();
  270. $plex = simplexml_load_string($response->body);
  271. foreach ($plex->Server->Section as $child) {
  272. $libraryList['libraries'][(string)$child['title']] = (string)$child['id'];
  273. }
  274. if ($this->config['INVITES-plexLibraries'] !== '') {
  275. $noLongerId = 0;
  276. $libraries = explode(',', $this->config['INVITES-plexLibraries']);
  277. foreach ($libraries as $child) {
  278. if (!$this->search_for_value($child, $libraryList)) {
  279. $libraryList['libraries']['No Longer Exists - ' . $noLongerId] = $child;
  280. $noLongerId++;
  281. }
  282. }
  283. }
  284. $libraryList = array_change_key_case($libraryList, CASE_LOWER);
  285. return $libraryList;
  286. }
  287. } catch (Requests_Exception $e) {
  288. $this->setLoggerChannel('Plex')->error($e);
  289. return false;
  290. };
  291. }
  292. break;
  293. default:
  294. # code...
  295. break;
  296. }
  297. return false;
  298. }
  299. public function _invitesPluginGetSettings()
  300. {
  301. if ($this->config['plexID'] !== '' && $this->config['plexToken'] !== '' && $this->config['INVITES-type-include'] == 'plex') {
  302. $loop = $this->_invitesPluginLibraryList($this->config['INVITES-type-include'])['libraries'];
  303. foreach ($loop as $key => $value) {
  304. $libraryList[] = array(
  305. 'name' => $key,
  306. 'value' => $value
  307. );
  308. }
  309. } else {
  310. $libraryList = array(
  311. array(
  312. 'name' => 'Refresh page to update List',
  313. 'value' => '',
  314. 'disabled' => true,
  315. ),
  316. );
  317. }
  318. $komgaRoles = $this->_getKomgaRoles();
  319. $komgalibrary = $this->_getKomgaLibraries();
  320. $nextcloudRoles = $this->_getNextcloudGroups();
  321. return array(
  322. 'Backend' => array(
  323. array(
  324. 'type' => 'select',
  325. 'name' => 'INVITES-type-include',
  326. 'label' => 'Media Server',
  327. 'value' => $this->config['INVITES-type-include'],
  328. 'options' => array(
  329. array(
  330. 'name' => 'N/A',
  331. 'value' => 'n/a'
  332. ),
  333. array(
  334. 'name' => 'Plex',
  335. 'value' => 'plex'
  336. ),
  337. array(
  338. 'name' => 'Emby',
  339. 'value' => 'emby'
  340. )
  341. )
  342. ),
  343. array(
  344. 'type' => 'select',
  345. 'name' => 'INVITES-Auth-include',
  346. 'label' => 'Minimum Authentication',
  347. 'value' => $this->config['INVITES-Auth-include'],
  348. 'options' => $this->groupSelect()
  349. ),
  350. array(
  351. 'type' => 'switch',
  352. 'name' => 'INVITES-allow-delete-include',
  353. 'label' => 'Allow users to delete invites',
  354. 'help' => 'This must be disabled to enforce invitation limits.',
  355. 'value' => $this->config['INVITES-allow-delete-include']
  356. ),
  357. array(
  358. 'type' => 'number',
  359. 'name' => 'INVITES-maximum-invites',
  360. 'label' => 'Maximum number of invites permitted for users.',
  361. 'help' => 'Set to 0 to disable the limit.',
  362. 'value' => $this->config['INVITES-maximum-invites'],
  363. 'placeholder' => '0'
  364. ),
  365. ),
  366. 'Plex Settings' => array(
  367. array(
  368. 'type' => 'password-alt',
  369. 'name' => 'plexToken',
  370. 'label' => 'Plex Token',
  371. 'value' => $this->config['plexToken'],
  372. 'placeholder' => 'Use Get Token Button'
  373. ),
  374. array(
  375. 'type' => 'button',
  376. 'label' => 'Get Plex Token',
  377. 'icon' => 'fa fa-ticket',
  378. 'text' => 'Retrieve',
  379. 'attr' => 'onclick="PlexOAuth(oAuthSuccess,oAuthError, oAuthMaxRetry, null, null, \'#INVITES-settings-items [name=plexToken]\')"'
  380. ),
  381. array(
  382. 'type' => 'password-alt',
  383. 'name' => 'plexID',
  384. 'label' => 'Plex Machine',
  385. 'value' => $this->config['plexID'],
  386. 'placeholder' => 'Use Get Plex Machine Button'
  387. ),
  388. array(
  389. 'type' => 'button',
  390. 'label' => 'Get Plex Machine',
  391. 'icon' => 'fa fa-id-badge',
  392. 'text' => 'Retrieve',
  393. 'attr' => 'onclick="showPlexMachineForm(\'#INVITES-settings-items [name=plexID]\')"'
  394. ),
  395. array(
  396. 'type' => 'select2',
  397. 'class' => 'select2-multiple',
  398. 'id' => 'invite-select-' . $this->random_ascii_string(6),
  399. 'name' => 'INVITES-plexLibraries',
  400. 'label' => 'Libraries',
  401. 'value' => $this->config['INVITES-plexLibraries'],
  402. 'options' => $libraryList
  403. ),
  404. array(
  405. 'type' => 'text',
  406. 'name' => 'INVITES-plex-tv-labels',
  407. 'label' => 'TV Labels (comma separated)',
  408. 'value' => $this->config['INVITES-plex-tv-labels'],
  409. 'placeholder' => 'All'
  410. ),
  411. array(
  412. 'type' => 'text',
  413. 'name' => 'INVITES-plex-movies-labels',
  414. 'label' => 'Movies Labels (comma separated)',
  415. 'value' => $this->config['INVITES-plex-movies-labels'],
  416. 'placeholder' => 'All'
  417. ),
  418. array(
  419. 'type' => 'text',
  420. 'name' => 'INVITES-plex-music-labels',
  421. 'label' => 'Music Labels (comma separated)',
  422. 'value' => $this->config['INVITES-plex-music-labels'],
  423. 'placeholder' => 'All'
  424. ),
  425. array(
  426. 'type' => 'switch',
  427. 'name' => 'INVITES-add-plex-home',
  428. 'label' => 'When user subscribe add him to Plex Home',
  429. 'value' => $this->config['INVITES-add-plex-home']
  430. )
  431. ),
  432. 'Komga Settings' => array(
  433. array(
  434. 'type' => 'switch',
  435. 'name' => 'INVITES-komga-enabled',
  436. 'label' => 'Enable Komga for auto create account',
  437. 'value' => $this->config['INVITES-komga-enabled'],
  438. ),
  439. array(
  440. 'type' => 'input',
  441. 'name' => 'INVITES-komga-uri',
  442. 'label' => 'URL',
  443. 'value' => $this->config['INVITES-komga-uri'],
  444. 'placeholder' => 'http(s)://hostname:port'
  445. ),
  446. array(
  447. 'type' => 'password-alt',
  448. 'name' => 'INVITES-komga-api-key',
  449. 'label' => 'Komga Api Key',
  450. 'value' => $this->config['INVITES-komga-api-key']
  451. ),
  452. array(
  453. 'type' => 'password-alt',
  454. 'name' => 'INVITES-komga-default-user-password',
  455. 'label' => 'Default password for new user',
  456. 'value' => $this->config['INVITES-komga-default-user-password']
  457. ),
  458. array(
  459. 'type' => 'select2',
  460. 'class' => 'select2-multiple',
  461. 'id' => 'INVITES-select-' . $this->random_ascii_string(6),
  462. 'name' => 'INVITES-komga-roles',
  463. 'label' => 'Roles',
  464. 'value' => $this->config['INVITES-komga-roles'],
  465. 'options' => $komgaRoles
  466. ),
  467. array(
  468. 'type' => 'select2',
  469. 'class' => 'select2-multiple',
  470. 'id' => 'INVITES-select-' . $this->random_ascii_string(6),
  471. 'name' => 'INVITES-komga-libraryIds',
  472. 'label' => 'Libraries',
  473. 'value' => $this->config['INVITES-komga-libraryIds'],
  474. 'options' => $komgalibrary
  475. )
  476. ),
  477. 'Nextcloud Settings' => array(
  478. array(
  479. 'type' => 'switch',
  480. 'name' => 'INVITES-nextcloud-enabled',
  481. 'label' => 'Enable Nextcloud for auto create account',
  482. 'value' => $this->config['INVITES-nextcloud-enabled'],
  483. ),
  484. array(
  485. 'type' => 'switch',
  486. 'name' => 'INVITES-nextcloud-plex-sso',
  487. 'label' => 'Enable if you have Plex SSO app installed on Nextcloud',
  488. 'value' => $this->config['INVITES-nextcloud-plex-sso'],
  489. ),
  490. array(
  491. 'type' => 'input',
  492. 'name' => 'INVITES-nextcloud-url',
  493. 'label' => 'Nextcloud URI',
  494. 'value' => $this->config['INVITES-nextcloud-url']
  495. ),
  496. array(
  497. 'type' => 'input',
  498. 'name' => 'INVITES-nextcloud-admin-user',
  499. 'label' => 'Nextcloud Admin User',
  500. 'value' => $this->config['INVITES-nextcloud-admin-user']
  501. ),
  502. array(
  503. 'type' => 'password',
  504. 'name' => 'INVITES-nextcloud-admin-password',
  505. 'label' => 'Nextcloud Admin Password',
  506. 'value' => $this->config['INVITES-nextcloud-admin-password']
  507. ),
  508. array(
  509. 'type' => 'input',
  510. 'name' => 'INVITES-nextcloud-quota',
  511. 'label' => 'Storage quota for user after subscription (empty for no-limit)',
  512. 'value' => $this->config['INVITES-nextcloud-quota'],
  513. 'placeholder' => '10GB'
  514. ),
  515. array(
  516. 'type' => 'select2',
  517. 'class' => 'select2-multiple',
  518. 'id' => 'INVITES-select-' . $this->random_ascii_string(6),
  519. 'name' => 'INVITES-nextcloud-groups-member',
  520. 'label' => 'Nextcloud Groups after subscription',
  521. 'value' => $this->config['INVITES-nextcloud-groups-member'],
  522. 'options' => $nextcloudRoles
  523. )
  524. ),
  525. 'Emby Settings' => array(
  526. array(
  527. 'type' => 'password-alt',
  528. 'name' => 'embyToken',
  529. 'label' => 'Emby API key',
  530. 'value' => $this->config['embyToken'],
  531. 'placeholder' => 'enter key from emby'
  532. ),
  533. array(
  534. 'type' => 'text',
  535. 'name' => 'embyURL',
  536. 'label' => 'Emby server adress',
  537. 'value' => $this->config['embyURL'],
  538. 'placeholder' => 'localhost:8086'
  539. ),
  540. array(
  541. 'type' => 'text',
  542. 'name' => 'INVITES-EmbyTemplate',
  543. 'label' => 'Emby User to be used as template for new users',
  544. 'value' => $this->config['INVITES-EmbyTemplate'],
  545. 'placeholder' => 'AdamSmith'
  546. )
  547. ),
  548. 'FYI' => array(
  549. array(
  550. 'type' => 'html',
  551. 'label' => 'Note',
  552. 'html' => '<span lang="en">After enabling for the first time, please reload the page - Menu is located under User menu on top right</span>'
  553. )
  554. )
  555. );
  556. }
  557. public function _invitesPluginAction($username, $action = null, $type = null, $mail)
  558. {
  559. if ($action == null) {
  560. $this->setAPIResponse('error', 'No Action supplied', 409);
  561. return false;
  562. }
  563. switch ($type) {
  564. case 'plex':
  565. if (!empty($this->config['plexToken']) && !empty($this->config['plexID'])) {
  566. $url = "https://plex.tv/api/servers/" . $this->config['plexID'] . "/shared_servers/";
  567. if ($this->config['INVITES-plexLibraries'] !== "") {
  568. $libraries = explode(',', $this->config['INVITES-plexLibraries']);
  569. } else {
  570. $libraries = '';
  571. }
  572. if ($this->config['INVITES-plex-tv-labels'] !== "") {
  573. $tv_labels = "label=" . $this->config['INVITES-plex-tv-labels'];
  574. } else {
  575. $tv_labels = "";
  576. }
  577. if ($this->config['INVITES-plex-movies-labels'] !== "") {
  578. $movies_labels = "label=" . $this->config['INVITES-plex-movies-labels'];
  579. } else {
  580. $movies_labels = "";
  581. }
  582. if ($this->config['INVITES-plex-music-labels'] !== "") {
  583. $music_labels = "label=" . $this->config['INVITES-plex-music-labels'];
  584. } else {
  585. $music_labels = "";
  586. }
  587. $headers = array(
  588. "Accept" => "application/json",
  589. "Content-Type" => "application/json",
  590. "X-Plex-Token" => $this->config['plexToken']
  591. );
  592. $data = array(
  593. "server_id" => $this->config['plexID'],
  594. "shared_server" => array(
  595. "library_section_ids" => $libraries,
  596. "invited_email" => $username
  597. ),
  598. "sharing_settings" => array(
  599. "filterTelevision" => $tv_labels,
  600. "filterMovies" => $movies_labels,
  601. "filterMusic" => $music_labels
  602. )
  603. );
  604. try {
  605. switch ($action) {
  606. case 'share':
  607. $response = Requests::post($url, $headers, json_encode($data), array());
  608. if($this->config['INVITES-add-plex-home']) {
  609. $this->_addUserPlexHome($mail);
  610. }
  611. if($this->config['INVITES-komga-enabled']) {
  612. $this->_createKomgaAccount($mail);
  613. }
  614. if ($this->config['INVITES-nextcloud-enabled']) {
  615. $nextcloudAccountCreated = $this->_createNextcloudAccount($mail, $username);
  616. }
  617. break;
  618. case 'unshare':
  619. $id = (is_numeric($username) ? $username : $this->_invitesPluginConvertPlexName($username, "id"));
  620. $url = $url . $id;
  621. $response = Requests::delete($url, $headers, array());
  622. break;
  623. default:
  624. $this->setAPIResponse('error', 'No Action supplied', 409);
  625. return false;
  626. }
  627. if ($response->success) {
  628. $this->setLoggerChannel('Invites')->info('Plex User now has access to system');
  629. $this->setAPIResponse('success', 'Plex User now has access to system', 200);
  630. return true;
  631. } else {
  632. switch ($response->status_code) {
  633. case 400:
  634. $this->setLoggerChannel('Plex')->warning('Plex User already has access');
  635. $this->setAPIResponse('success', 'Plex User already has access', 200);
  636. return true;
  637. case 401:
  638. $this->setLoggerChannel('Plex')->warning('Incorrect Token');
  639. $this->setAPIResponse('error', 'Incorrect Token', 409);
  640. return false;
  641. case 404:
  642. $this->setLoggerChannel('Plex')->warning('Libraries not setup correctly');
  643. $this->setAPIResponse('error', 'Libraries not setup correct', 409);
  644. return false;
  645. default:
  646. $this->setLoggerChannel('Plex')->warning('An error occurred [' . $response->status_code . ']');
  647. $this->setAPIResponse('error', 'An Error Occurred', 409);
  648. return false;
  649. }
  650. }
  651. } catch (Requests_Exception $e) {
  652. $this->setLoggerChannel('Plex')->error($e);
  653. $this->setAPIResponse('error', $e->getMessage(), 409);
  654. return false;
  655. }
  656. } else {
  657. $this->setLoggerChannel('Plex')->warning('Plex Token/ID not set');
  658. $this->setAPIResponse('error', 'Plex Token/ID not set', 409);
  659. return false;
  660. }
  661. break;
  662. case 'emby':
  663. try {
  664. #add emby user to system
  665. $this->setAPIResponse('success', 'User now has access to system', 200);
  666. return true;
  667. } catch (Requests_Exception $e) {
  668. $this->setLoggerChannel('Emby')->error($e);
  669. $this->setAPIResponse('error', $e->getMessage(), 409);
  670. return false;
  671. }
  672. default:
  673. return false;
  674. }
  675. return false;
  676. }
  677. public function _invitesPluginConvertPlexName($user, $type)
  678. {
  679. $array = $this->userList('plex');
  680. switch ($type) {
  681. case "username":
  682. case "u":
  683. $plexUser = array_search($user, $array['users']);
  684. break;
  685. case "id":
  686. if (array_key_exists(strtolower($user), $array['users'])) {
  687. $plexUser = $array['users'][strtolower($user)];
  688. }
  689. break;
  690. default:
  691. $plexUser = false;
  692. }
  693. return (!empty($plexUser) ? $plexUser : null);
  694. }
  695. /**
  696. * Creates a new Komga user account using the provided email address.
  697. *
  698. * @param string $email The email address for the new Komga user account.
  699. * @return bool True if the account was successfully created, false otherwise.
  700. */
  701. private function _createKomgaAccount($email) {
  702. $this->logger->info('Try to create Komga account for ' . $email);
  703. if(!$this->_checkKomgaVar()) {
  704. return false;
  705. }
  706. if (empty($email)) {
  707. $this->setLoggerChannel('Invites')->info('User email empty');
  708. return false;
  709. }
  710. $endpoint = rtrim($this->config['INVITES-komga-uri'], '/') . '/api/v2/users';
  711. $apiKey = $this->config['INVITES-komga-api-key'];
  712. $password = $this->decrypt($this->config['INVITES-komga-default-user-password']);
  713. $rolesStr = $this->config['INVITES-komga-roles'] ?? '';
  714. $roles = array_values(array_filter(array_map('trim', explode(';', $rolesStr))));
  715. $libIdsStr = $this->config['INVITES-komga-libraryIds'] ?? '';
  716. $libraryIds = array_values(array_filter(array_map('trim', explode(';', $libIdsStr))));
  717. $headers = array(
  718. 'accept' => 'application/json',
  719. 'X-API-Key' => $apiKey,
  720. 'Content-Type' => 'application/json'
  721. );
  722. $payload = array(
  723. 'email' => $email,
  724. 'password' => $password,
  725. 'roles' => $roles,
  726. 'sharedLibraries' => array(
  727. 'all' => false,
  728. 'libraryIds' => $libraryIds
  729. )
  730. );
  731. try {
  732. $response = Requests::post($endpoint, $headers, json_encode($payload));
  733. if ($response->success) {
  734. $this->setLoggerChannel('Komga')->info('User created ' . $email . ' with roles: ' . implode(',', $roles) . ' and libraries: ' . implode(',', $libraryIds));
  735. return true;
  736. }
  737. $this->setLoggerChannel('Komga')->warning('User not created ' . $email . ' HTTP ' . $response->status_code);
  738. } catch (Requests_Exception $e) {
  739. $this->setLoggerChannel('Komga')->error('User not created ' . $email . ' Requests_Exception: ' . $e->getMessage());
  740. }
  741. return false;
  742. }
  743. /**
  744. * Retrieves a list of Komga roles
  745. *
  746. * @return array An array of associative arrays, each containing 'name' and 'value' for a Komga role.
  747. */
  748. public function _getKomgaRoles() {
  749. $komgaRoles = array();
  750. $roleNames = array(
  751. 'ADMIN' => 'Administrator',
  752. 'FILE_DOWNLOAD' => 'File download',
  753. 'PAGE_STREAMING' => 'Page streaming',
  754. 'KOBO_SYNC' => 'Kobo Sync',
  755. 'KOREADER_SYNC' => 'Koreader Sync'
  756. );
  757. foreach ($roleNames as $value => $name) {
  758. $komgaRoles[] = array(
  759. 'name' => $name,
  760. 'value' => $value
  761. );
  762. }
  763. return $komgaRoles;
  764. }
  765. /**
  766. * Fetches the list of Komga libraries from the Komga API.
  767. *
  768. * @return array|false Returns an array of libraries with 'name' and 'id' on success, or false on failure.
  769. */
  770. public function _getKomgaLibraries() {
  771. $this->logger->info('Try to fetch Komga libraries');
  772. if(!$this->_checkKomgaVar()) {
  773. return false;
  774. }
  775. $endpoint = rtrim($this->config['komgaURL'], '/') . '/api/v1/libraries';
  776. $apiKey = $this->config['INVITES-komga-api-key'];
  777. $libraryListDefault = array(
  778. array(
  779. 'name' => 'Refresh page to update List',
  780. 'value' => '',
  781. 'disabled' => true,
  782. ),
  783. );
  784. $headers = array(
  785. 'accept' => 'application/json',
  786. 'X-API-Key' => $apiKey
  787. );
  788. try {
  789. $response = Requests::get($endpoint, $headers);
  790. if ($response->success) {
  791. $libraries = json_decode($response->body, true);
  792. // Komga retourne un tableau d'objets librairie
  793. $result = array();
  794. foreach ($libraries as $library) {
  795. $result[] = array(
  796. 'name' => $library['name'],
  797. 'id' => $library['id']
  798. );
  799. }
  800. $this->logger->info('Fetched libraries: ' . json_encode($result));
  801. if(!empty($result)) {
  802. return $result;
  803. }
  804. } else {
  805. $this->logger->warning("Error HTTP ".$response->status_code.' body='.$response->body);
  806. }
  807. } catch (Requests_Exception $e) {
  808. $this->logger->warning("Exception: " . $e->getMessage());
  809. }
  810. return $libraryListDefault;
  811. }
  812. /**
  813. * Fetches the list of Nextcloud groups using the configured Nextcloud admin credentials.
  814. *
  815. * @return array|false Returns an array of groups with 'name' and 'value' keys on success,
  816. * or false on failure.
  817. */
  818. public function _getNextcloudGroups() {
  819. $this->logger->info('Try to fetch Nextcloud groups');
  820. if(!$this->_checkNextcloudVar()) {
  821. return false;
  822. }
  823. $url = rtrim($this->config['INVITES-nextcloud-url'], '/') . '/ocs/v1.php/cloud/groups';
  824. $adminUser = $this->config['INVITES-nextcloud-admin-user'];
  825. $adminPass = $this->decrypt($this->config['INVITES-nextcloud-admin-password']);
  826. $headers = array(
  827. 'OCS-APIRequest' => 'true',
  828. 'Accept' => 'application/json',
  829. );
  830. try {
  831. $options = array(
  832. 'auth' => array($adminUser, $adminPass),
  833. );
  834. $response = Requests::get($url, $headers, $options);
  835. if ($response->success) {
  836. $body = json_decode($response->body, true);
  837. if (isset($body['ocs']['data']['groups'])) {
  838. $this->logger->info('Fetched groups: ' . implode(', ', $body['ocs']['data']['groups']));
  839. $groups = $body['ocs']['data']['groups'];
  840. $result = array();
  841. foreach ($groups as $group) {
  842. $result[] = array(
  843. 'name' => $group,
  844. 'value' => $group
  845. );
  846. }
  847. return $result;
  848. } else {
  849. $this->logger->warning('Groups not found in response');
  850. }
  851. } else {
  852. $this->logger->warning("Error HTTP ".$response->status_code.' body='.$response->body);
  853. }
  854. } catch (Requests_Exception $e) {
  855. $this->logger->warning("Exception: " . $e->getMessage());
  856. }
  857. return false;
  858. }
  859. /**
  860. * Checks if all required Nextcloud configuration variables are set.
  861. *
  862. * @return bool Returns true if all required Nextcloud configuration variables are set; false otherwise.
  863. */
  864. public function _checkNextcloudVar() {
  865. if (empty($this->config['INVITES-nextcloud-enabled'])) {
  866. $this->logger->info('Nextcloud disabled in config');
  867. return false;
  868. }
  869. if (empty($this->config['INVITES-nextcloud-url'])) {
  870. $this->logger->info('Nextcloud URL missing');
  871. return false;
  872. }
  873. if (empty($this->config['INVITES-nextcloud-admin-user']) || empty($this->config['INVITES-nextcloud-admin-password'])) {
  874. $this->logger->info('Nextcloud admin credentials missing');
  875. return false;
  876. }
  877. return true;
  878. }
  879. /**
  880. * Checks if all required komga configuration variables are set.
  881. *
  882. * @return bool Returns true if all required komga configuration variables are set; false otherwise.
  883. */
  884. public function _checkKomgaVar() {
  885. if (empty($this->config['INVITES-komga-uri'])) {
  886. $this->setLoggerChannel('Invites')->info('Komga uri is missing');
  887. return false;
  888. }
  889. if (empty($this->config['INVITES-komga-api-key'])) {
  890. $this->setLoggerChannel('Invites')->info('Komga api key is missing');
  891. return false;
  892. }
  893. if (empty($this->config['INVITES-komga-roles'])) {
  894. $this->setLoggerChannel('Invites')->info('Komga roles empty');
  895. return false;
  896. }
  897. if (empty($this->config['INVITES-komga-libraryIds'])) {
  898. $this->setLoggerChannel('Invites')->info('Komga library empty');
  899. return false;
  900. }
  901. if (empty($this->config['INVITES-komga-default-user-password'])) {
  902. $this->setLoggerChannel('Invites')->info('Komga default user password empty');
  903. return false;
  904. }
  905. return true;
  906. }
  907. /**
  908. * Creates a Nextcloud account for a user based on their email and other parameters.
  909. *
  910. * @param string $email The email address of the user to create in Nextcloud.
  911. * @param string $displayName The display name for the Nextcloud user.
  912. * @param string $nextcloudGroupsMember A semicolon-separated list of Nextcloud groups to add the user to.
  913. * @param string $nextcloudQuota The storage quota to assign to the user (e.g., "5GB").
  914. *
  915. * @return bool Returns true if the account was successfully created, false otherwise.
  916. */
  917. public function _createNextcloudAccount($email, $displayName) {
  918. $this->logger->info('Try to create Nextcloud account');
  919. if(!$this->_checkNextcloudVar()) {
  920. return false;
  921. }
  922. $nextcloudGroupsMember = $this->config['INVITES-nextcloud-groups-member'] ?? '';
  923. $nextcloudQuota = $this->config['INVITES-nextcloud-quota'] ?? '';
  924. $userid = $email;
  925. if($this->config['INVITES-nextcloud-plex-sso']) {
  926. $plexUserId = $this->_getPlexUserIdByEmail($email);
  927. $this->logger->warning('plexUserId=' . $plexUserId);
  928. if (!empty($plexUserId)) {
  929. $userid = 'PlexTv-' . $plexUserId;
  930. }
  931. }
  932. try {
  933. $password = bin2hex(random_bytes(12));
  934. } catch (\Throwable $e) {
  935. $password = bin2hex(openssl_random_pseudo_bytes(12));
  936. }
  937. if (empty($password)) {
  938. $this->logger->warning('Error generating password');
  939. return false;
  940. }
  941. $url = rtrim($this->config['INVITES-nextcloud-url'], '/') . '/ocs/v1.php/cloud/users';
  942. $adminUser = $this->config['INVITES-nextcloud-admin-user'];
  943. $adminPass = $this->decrypt($this->config['INVITES-nextcloud-admin-password']);
  944. $headers = array(
  945. 'OCS-APIRequest' => 'true',
  946. 'Accept' => 'application/json',
  947. );
  948. $data = array(
  949. 'userid' => $userid,
  950. 'password' => $password,
  951. 'email' => $email,
  952. 'displayName' => $displayName,
  953. );
  954. if (!empty($nextcloudGroupsMember)) {
  955. $groups = array_values(array_filter(array_map('trim', explode(';', $nextcloudGroupsMember))));
  956. foreach ($groups as $group) {
  957. $data['groups[]'] = $group;
  958. }
  959. }
  960. if (!empty($nextcloudQuota)) {
  961. $data['quota'] = $nextcloudQuota;
  962. }
  963. try {
  964. $options = array(
  965. 'auth' => array($adminUser, $adminPass),
  966. );
  967. $response = Requests::post($url, $headers, $data, $options);
  968. if ($response->success) {
  969. $this->logger->info("User created ($email)");
  970. return true;
  971. }
  972. $this->logger->warning("Error ($email) HTTP ".$response->status_code.' body='.$response->body);
  973. } catch (Requests_Exception $e) {
  974. $this->logger->warning("Exception: " . $e->getMessage());
  975. }
  976. return false;
  977. }
  978. /**
  979. * Retrieves the Plex user ID associated with a given email address.
  980. *
  981. * @param string $email The email address to search for in the shared Plex users.
  982. * @return string|null The Plex user ID if found, or null if not found or on error.
  983. */
  984. public function _getPlexUserIdByEmail($email) {
  985. $this->logger->info("Try to get Plex userID for $email");
  986. if (empty($this->config['plexToken']) || empty($this->config['plexID'])) {
  987. $this->logger->warning("PlexToken ou plexID missing");
  988. return null;
  989. }
  990. $url = "https://clients.plex.tv/api/invites/requested";
  991. $headers = array(
  992. "Accept" => "application/json",
  993. "X-Plex-Token" => $this->config['plexToken']
  994. );
  995. try {
  996. $response = Requests::get($url, $headers);
  997. if ($response->success) {
  998. $xml = simplexml_load_string($response->body);
  999. // Parcourt les éléments <Invite> du MediaContainer
  1000. foreach ($xml->Invite as $invite) {
  1001. $inviteEmail = (string)$invite['email'];
  1002. $inviteId = (string)$invite['id'];
  1003. if (strcasecmp($inviteEmail, $email) === 0) {
  1004. $this->logger->info("Find id=$inviteId for $email");
  1005. return $inviteId;
  1006. }
  1007. }
  1008. }
  1009. $this->logger->warning("No userId found for $email");
  1010. } catch (Requests_Exception $e) {
  1011. $this->logger->warning("Exception: " . $e->getMessage());
  1012. }
  1013. return null;
  1014. }
  1015. /**
  1016. * Retrieves the email address associated with a given invite code.
  1017. *
  1018. * @param string $inviteCode The invite code to look up.
  1019. * @return string|false The email address associated with the invite code, or false if not found.
  1020. */
  1021. public function _getEmailFronInviteCode($inviteCode) {
  1022. if (empty($inviteCode)) {
  1023. $this->logger->warning('Invite code not found');
  1024. return false;
  1025. }
  1026. $emailLookupQuery = [
  1027. array(
  1028. 'function' => 'fetch',
  1029. 'query' => array(
  1030. 'SELECT email FROM invites WHERE code = ? COLLATE NOCASE',
  1031. $inviteCode
  1032. )
  1033. )
  1034. ];
  1035. $emailRow = $this->processQueries($emailLookupQuery);
  1036. if ($emailRow && !empty($emailRow['email'])) {
  1037. $this->logger->info("Email foud via the code [$inviteCode] : ".$emailRow['email']);
  1038. return $emailRow['email'];
  1039. } else {
  1040. $this->logger->warning("No mail found for the code [$inviteCode]");
  1041. return false;
  1042. }
  1043. }
  1044. /**
  1045. * Adds a user to Plex Home using the provided email address.
  1046. *
  1047. * @param string $email The email address of the user to invite.
  1048. * @return array|false Returns the decoded response from Plex API on success, or false on failure.
  1049. */
  1050. public function _addUserPlexHome($email){
  1051. if (empty($email) || empty($this->config['plexToken'])) {
  1052. $this->logger->warning('_addUserPlexHome: email or plexToken missing');
  1053. return false;
  1054. }
  1055. $url = 'https://clients.plex.tv/api/home/users?invitedEmail=' . urlencode($email) . '&skipFriendship=1&X-Plex-Token=' . urlencode($this->config['plexToken']);
  1056. try {
  1057. $response = Requests::post($url, $headers);
  1058. if ($response->success) {
  1059. $this->logger->info('User added on plex home');
  1060. return json_decode($response->body, true);
  1061. } else {
  1062. $this->logger->info('_getPlexHomeUserByEmail: error (HTTP ' . $response->status_code . ')');
  1063. }
  1064. } catch (Requests_Exception $e) {
  1065. $this->logger->info('_addUserPlexHome: ' . $e->getMessage());
  1066. }
  1067. return false;
  1068. }
  1069. }