plugin.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. $libraryList = array_change_key_case($libraryList, CASE_LOWER);
  186. return $libraryList;
  187. }
  188. } catch (Requests_Exception $e) {
  189. $this->writeLog('error', 'Plex Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  190. return false;
  191. };
  192. }
  193. break;
  194. default:
  195. # code...
  196. break;
  197. }
  198. return false;
  199. }
  200. public function _invitesPluginGetSettings()
  201. {
  202. if ($this->config['plexID'] !== '' && $this->config['plexToken'] !== '' && $this->config['INVITES-type-include'] == 'plex') {
  203. $loop = $this->_invitesPluginLibraryList($this->config['INVITES-type-include'])['libraries'];
  204. foreach ($loop as $key => $value) {
  205. $libraryList[] = array(
  206. 'name' => $key,
  207. 'value' => $value
  208. );
  209. }
  210. } else {
  211. $libraryList = array(
  212. array(
  213. 'name' => 'Refresh page to update List',
  214. 'value' => '',
  215. 'disabled' => true,
  216. ),
  217. );
  218. }
  219. return array(
  220. 'Backend' => array(
  221. array(
  222. 'type' => 'select',
  223. 'name' => 'INVITES-type-include',
  224. 'label' => 'Media Server',
  225. 'value' => $this->config['INVITES-type-include'],
  226. 'options' => array(
  227. array(
  228. 'name' => 'N/A',
  229. 'value' => 'n/a'
  230. ),
  231. array(
  232. 'name' => 'Plex',
  233. 'value' => 'plex'
  234. ),
  235. array(
  236. 'name' => 'Emby',
  237. 'value' => 'emby'
  238. )
  239. )
  240. )
  241. ),
  242. 'Plex Settings' => array(
  243. array(
  244. 'type' => 'password-alt',
  245. 'name' => 'plexToken',
  246. 'label' => 'Plex Token',
  247. 'value' => $this->config['plexToken'],
  248. 'placeholder' => 'Use Get Token Button'
  249. ),
  250. array(
  251. 'type' => 'button',
  252. 'label' => 'Get Plex Token',
  253. 'icon' => 'fa fa-ticket',
  254. 'text' => 'Retrieve',
  255. 'attr' => 'onclick="showPlexTokenForm(\'#INVITES-settings-items [name=plexToken]\')"'
  256. ),
  257. array(
  258. 'type' => 'password-alt',
  259. 'name' => 'plexID',
  260. 'label' => 'Plex Machine',
  261. 'value' => $this->config['plexID'],
  262. 'placeholder' => 'Use Get Plex Machine Button'
  263. ),
  264. array(
  265. 'type' => 'button',
  266. 'label' => 'Get Plex Machine',
  267. 'icon' => 'fa fa-id-badge',
  268. 'text' => 'Retrieve',
  269. 'attr' => 'onclick="showPlexMachineForm(\'#INVITES-settings-items [name=plexID]\')"'
  270. ),
  271. array(
  272. 'type' => 'select2',
  273. 'class' => 'select2-multiple',
  274. 'id' => 'invite-select',
  275. 'name' => 'INVITES-plexLibraries',
  276. 'label' => 'Libraries',
  277. 'value' => $this->config['INVITES-plexLibraries'],
  278. 'options' => $libraryList
  279. ),
  280. array(
  281. 'type' => 'text',
  282. 'name' => 'INVITES-plex-tv-labels',
  283. 'label' => 'TV Labels (comma separated)',
  284. 'value' => $this->config['INVITES-plex-tv-labels'],
  285. 'placeholder' => 'All'
  286. ),
  287. array(
  288. 'type' => 'text',
  289. 'name' => 'INVITES-plex-movies-labels',
  290. 'label' => 'Movies Labels (comma separated)',
  291. 'value' => $this->config['INVITES-plex-movies-labels'],
  292. 'placeholder' => 'All'
  293. ),
  294. array(
  295. 'type' => 'text',
  296. 'name' => 'INVITES-plex-music-labels',
  297. 'label' => 'Music Labels (comma separated)',
  298. 'value' => $this->config['INVITES-plex-music-labels'],
  299. 'placeholder' => 'All'
  300. ),
  301. ),
  302. 'Emby Settings' => array(
  303. array(
  304. 'type' => 'password-alt',
  305. 'name' => 'embyToken',
  306. 'label' => 'Emby API key',
  307. 'value' => $this->config['embyToken'],
  308. 'placeholder' => 'enter key from emby'
  309. ),
  310. array(
  311. 'type' => 'text',
  312. 'name' => 'embyURL',
  313. 'label' => 'Emby server adress',
  314. 'value' => $this->config['embyURL'],
  315. 'placeholder' => 'localhost:8086'
  316. ),
  317. array(
  318. 'type' => 'text',
  319. 'name' => 'INVITES-EmbyTemplate',
  320. 'label' => 'Emby User to be used as template for new users',
  321. 'value' => $this->config['INVITES-EmbyTemplate'],
  322. 'placeholder' => 'AdamSmith'
  323. )
  324. ),
  325. 'FYI' => array(
  326. array(
  327. 'type' => 'html',
  328. 'label' => 'Note',
  329. 'html' => 'After enabling for the first time, please reload the page - Menu is located under User menu on top right'
  330. )
  331. )
  332. );
  333. }
  334. public function _invitesPluginAction($username, $action = null, $type = null)
  335. {
  336. if ($action == null) {
  337. $this->setAPIResponse('error', 'No Action supplied', 409);
  338. return false;
  339. }
  340. switch ($type) {
  341. case 'plex':
  342. if (!empty($this->config['plexToken']) && !empty($this->config['plexID'])) {
  343. $url = "https://plex.tv/api/servers/" . $this->config['plexID'] . "/shared_servers/";
  344. if ($this->config['INVITES-plexLibraries'] !== "") {
  345. $libraries = explode(',', $this->config['INVITES-plexLibraries']);
  346. } else {
  347. $libraries = '';
  348. }
  349. if ($this->config['INVITES-plex-tv-labels'] !== "") {
  350. $tv_labels = "label=" . $this->config['INVITES-plex-tv-labels'];
  351. } else {
  352. $tv_labels = "";
  353. }
  354. if ($this->config['INVITES-plex-movies-labels'] !== "") {
  355. $movies_labels = "label=" . $this->config['INVITES-plex-movies-labels'];
  356. } else {
  357. $movies_labels = "";
  358. }
  359. if ($this->config['INVITES-plex-music-labels'] !== "") {
  360. $music_labels = "label=" . $this->config['INVITES-plex-music-labels'];
  361. } else {
  362. $music_labels = "";
  363. }
  364. $headers = array(
  365. "Accept" => "application/json",
  366. "Content-Type" => "application/json",
  367. "X-Plex-Token" => $this->config['plexToken']
  368. );
  369. $data = array(
  370. "server_id" => $this->config['plexID'],
  371. "shared_server" => array(
  372. "library_section_ids" => $libraries,
  373. "invited_email" => $username
  374. ),
  375. "sharing_settings" => array(
  376. "filterTelevision" => $tv_labels,
  377. "filterMovies" => $movies_labels,
  378. "filterMusic" => $music_labels
  379. )
  380. );
  381. try {
  382. switch ($action) {
  383. case 'share':
  384. $response = Requests::post($url, $headers, json_encode($data), array());
  385. break;
  386. case 'unshare':
  387. $id = (is_numeric($username) ? $username : $this->_invitesPluginConvertPlexName($username, "id"));
  388. $url = $url . $id;
  389. $response = Requests::delete($url, $headers, array());
  390. break;
  391. default:
  392. $this->setAPIResponse('error', 'No Action supplied', 409);
  393. return false;
  394. }
  395. if ($response->success) {
  396. $this->writeLog('success', 'Plex Invite Function - Plex User now has access to system', $username);
  397. $this->setAPIResponse('success', 'Plex User now has access to system', 200);
  398. return true;
  399. } else {
  400. switch ($response->status_code) {
  401. case 400:
  402. $this->writeLog('error', 'Plex Invite Function - Plex User already has access', $username);
  403. $this->setAPIResponse('error', 'Plex User already has access', 409);
  404. return false;
  405. case 401:
  406. $this->writeLog('error', 'Plex Invite Function - Incorrect Token', 'SYSTEM');
  407. $this->setAPIResponse('error', 'Incorrect Token', 409);
  408. return false;
  409. case 404:
  410. $this->writeLog('error', 'Plex Invite Function - Libraries not setup correct [' . $this->config['INVITES-plexLibraries'] . ']', 'SYSTEM');
  411. $this->setAPIResponse('error', 'Libraries not setup correct', 409);
  412. return false;
  413. default:
  414. $this->writeLog('error', 'Plex Invite Function - An error occurred [' . $response->status_code . ']', $username);
  415. $this->setAPIResponse('error', 'An Error Occurred', 409);
  416. return false;
  417. }
  418. }
  419. } catch (Requests_Exception $e) {
  420. $this->writeLog('error', 'Plex Invite Function - Error: ' . $e->getMessage(), 'SYSTEM');
  421. $this->setAPIResponse('error', $e->getMessage(), 409);
  422. return false;
  423. };
  424. } else {
  425. $this->writeLog('error', 'Plex Invite Function - Plex Token/ID not set', 'SYSTEM');
  426. $this->setAPIResponse('error', 'Plex Token/ID not set', 409);
  427. return false;
  428. }
  429. break;
  430. case 'emby':
  431. try {
  432. #add emby user to system
  433. $this->setAPIResponse('success', 'User now has access to system', 200);
  434. return true;
  435. } catch (Requests_Exception $e) {
  436. $this->writeLog('error', 'Emby Invite Function - Error: ' . $e->getMessage(), 'SYSTEM');
  437. $this->setAPIResponse('error', $e->getMessage(), 409);
  438. return false;
  439. }
  440. default:
  441. return false;
  442. }
  443. return false;
  444. }
  445. public function _invitesPluginConvertPlexName($user, $type)
  446. {
  447. $array = $this->userList('plex');
  448. switch ($type) {
  449. case "username":
  450. case "u":
  451. $plexUser = array_search($user, $array['users']);
  452. break;
  453. case "id":
  454. if (array_key_exists(strtolower($user), $array['users'])) {
  455. $plexUser = $array['users'][strtolower($user)];
  456. }
  457. break;
  458. default:
  459. $plexUser = false;
  460. }
  461. return (!empty($plexUser) ? $plexUser : null);
  462. }
  463. }