plugin.php 14 KB

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