plugin.php 14 KB

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