invites.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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' => 'https://github.com/PHPMailer/PHPMailer', // Link to plugin info
  8. 'license' => 'personal', // License Type use , for multiple
  9. //'fileName'=>'php-mailer.php',
  10. //'configFile'=>'php-mailer.php',
  11. //'apiFile'=>'php-mailer.php',
  12. 'idPrefix' => 'INVITES', // html element id prefix
  13. 'configPrefix' => 'INVITES', // config file prefix for array items without the hypen
  14. 'version' => '1.0.0', // SemVer of plugin
  15. 'image' => 'plugins/images/invites.png', // 1:1 non transparent image for plugin
  16. 'settings' => true, // does plugin need a settings page? true or false
  17. 'homepage' => false // Is plugin for use on homepage? true or false
  18. );
  19. // INCLUDE/REQUIRE FILES
  20. // PLUGIN FUNCTIONS
  21. function inviteCodes($array)
  22. {
  23. $action = isset($array['data']['action']) ? $array['data']['action'] : null;
  24. $code = isset($array['data']['code']) ? $array['data']['code'] : null;
  25. $usedBy = isset($array['data']['usedby']) ? $array['data']['usedby'] : null;
  26. $username = isset($array['data']['username']) ? $array['data']['username'] : null;
  27. $email = isset($array['data']['email']) ? $array['data']['email'] : null;
  28. $id = isset($array['data']['id']) ? $array['data']['id'] : null;
  29. $now = date("Y-m-d H:i:s");
  30. $currentIP = userIP();
  31. switch ($action) {
  32. case "check":
  33. try {
  34. $connect = new Dibi\Connection([
  35. 'driver' => 'sqlite3',
  36. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  37. ]);
  38. $all = $connect->fetch('SELECT * FROM invites WHERE valid = "Yes" AND code = ?', $code);
  39. return ($all) ? true : false;
  40. } catch (Dibi\Exception $e) {
  41. return false;
  42. }
  43. break;
  44. case "use":
  45. try {
  46. if (inviteCodes(array('data' => array('action' => 'check', 'code' => $code)))) {
  47. $connect = new Dibi\Connection([
  48. 'driver' => 'sqlite3',
  49. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  50. ]);
  51. $connect->query('
  52. UPDATE invites SET', [
  53. 'valid' => 'No',
  54. 'usedby' => $usedBy,
  55. 'dateused' => $now,
  56. 'ip' => $currentIP,
  57. ], '
  58. WHERE code=?', $code);
  59. writeLog('success', 'Invite Management Function - Invite Used [' . $code . ']', 'SYSTEM');
  60. return inviteAction($usedBy, 'share', $GLOBALS['INVITES-type-include']);
  61. } else {
  62. return false;
  63. }
  64. } catch (Dibi\Exception $e) {
  65. return false;
  66. }/*
  67. if(ENABLEMAIL){
  68. if (!isset($GLOBALS['USER'])) {
  69. require_once("user.php");
  70. $GLOBALS['USER'] = new User('registration_callback');
  71. }
  72. $emailTemplate = array(
  73. 'type' => 'mass',
  74. 'body' => 'The user: {user} has reddemed the code: {inviteCode} his IP Address was '.$currentIP,
  75. 'subject' => 'Invite Code '.$code.' Has Been Used',
  76. 'user' => $usedBy,
  77. 'password' => null,
  78. 'inviteCode' => $code,
  79. );
  80. $emailTemplate = emailTemplate($emailTemplate);
  81. $subject = $emailTemplate['subject'];
  82. $body = buildEmail($emailTemplate);
  83. sendEmail($GLOBALS['USER']->adminEmail, "Admin", $subject, $body);
  84. }*/
  85. break;
  86. default:
  87. if (qualifyRequest(1)) {
  88. switch ($action) {
  89. case "create":
  90. try {
  91. $connect = new Dibi\Connection([
  92. 'driver' => 'sqlite3',
  93. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  94. ]);
  95. $newCode = [
  96. 'code' => $code,
  97. 'email' => $email,
  98. 'username' => $username,
  99. 'valid' => 'Yes',
  100. 'type' => $GLOBALS['INVITES-type-include'],
  101. ];
  102. $connect->query('INSERT INTO [invites]', $newCode);
  103. writeLog('success', 'Invite Management Function - Added Invite [' . $code . ']', $GLOBALS['organizrUser']['username']);
  104. if ($GLOBALS['PHPMAILER-enabled']) {
  105. $emailTemplate = array(
  106. 'type' => 'invite',
  107. 'body' => $GLOBALS['PHPMAILER-emailTemplateInviteUser'],
  108. 'subject' => $GLOBALS['PHPMAILER-emailTemplateInviteUserSubject'],
  109. 'user' => $username,
  110. 'password' => null,
  111. 'inviteCode' => $code,
  112. );
  113. $emailTemplate = phpmEmailTemplate($emailTemplate);
  114. $sendEmail = array(
  115. 'to' => $email,
  116. 'subject' => $emailTemplate['subject'],
  117. 'body' => phpmBuildEmail($emailTemplate),
  118. );
  119. phpmSendEmail($sendEmail);
  120. }
  121. return true;
  122. } catch (Dibi\Exception $e) {
  123. writeLog('error', 'Invite Management Function - Error [' . $e . ']', 'SYSTEM');
  124. return false;
  125. }
  126. break;
  127. case "get":
  128. try {
  129. $connect = new Dibi\Connection([
  130. 'driver' => 'sqlite3',
  131. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  132. ]);
  133. $invites = $connect->fetchAll('SELECT * FROM invites');
  134. return $invites;
  135. } catch (Dibi\Exception $e) {
  136. writeLog('error', 'Invite Management Function - Error [' . $e . ']', 'SYSTEM');
  137. return false;
  138. }
  139. break;
  140. case "delete":
  141. try {
  142. $connect = new Dibi\Connection([
  143. 'driver' => 'sqlite3',
  144. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  145. ]);
  146. $connect->query('DELETE FROM invites WHERE id = ?', $id);
  147. return true;
  148. } catch (Dibi\Exception $e) {
  149. writeLog('error', 'Invite Management Function - Error [' . $e . ']', 'SYSTEM');
  150. return false;
  151. }
  152. break;
  153. default:
  154. return false;
  155. }
  156. }
  157. }
  158. }
  159. /* GET PHPMAILER SETTINGS */
  160. function invitesGetSettings()
  161. {
  162. if ($GLOBALS['plexID'] !== '' && $GLOBALS['plexToken'] !== '' && $GLOBALS['INVITES-type-include'] == 'plex') {
  163. $loop = libraryList($GLOBALS['INVITES-type-include'])['libraries'];
  164. foreach ($loop as $key => $value) {
  165. $libraryList[] = array(
  166. 'name' => $key,
  167. 'value' => $value
  168. );
  169. }
  170. } else {
  171. $libraryList = array(
  172. array(
  173. 'name' => 'Refresh page to update List',
  174. 'value' => '',
  175. 'disabled' => true,
  176. ),
  177. );
  178. }
  179. return array(
  180. 'Backend' => array(
  181. array(
  182. 'type' => 'select',
  183. 'name' => 'INVITES-type-include',
  184. 'label' => 'Media Server',
  185. 'value' => $GLOBALS['INVITES-type-include'],
  186. 'options' => array(
  187. array(
  188. 'name' => 'N/A',
  189. 'value' => 'n/a'
  190. ),
  191. array(
  192. 'name' => 'Plex',
  193. 'value' => 'plex'
  194. ),
  195. array(
  196. 'name' => 'Emby',
  197. 'value' => 'emby'
  198. )
  199. )
  200. )
  201. ),
  202. 'Plex Settings' => array(
  203. array(
  204. 'type' => 'password-alt',
  205. 'name' => 'plexToken',
  206. 'label' => 'Plex Token',
  207. 'value' => $GLOBALS['plexToken'],
  208. 'placeholder' => 'Use Get Token Button'
  209. ),
  210. array(
  211. 'type' => 'password-alt',
  212. 'name' => 'plexID',
  213. 'label' => 'Plex Machine',
  214. 'value' => $GLOBALS['plexID'],
  215. 'placeholder' => 'Use Get Plex Machine Button'
  216. ),
  217. array(
  218. 'type' => 'select2',
  219. 'class' => 'select2-multiple',
  220. 'id' => 'invite-select',
  221. 'name' => 'INVITES-plexLibraries',
  222. 'label' => 'Libraries',
  223. 'value' => $GLOBALS['INVITES-plexLibraries'],
  224. 'options' => $libraryList
  225. ),
  226. array(
  227. 'type' => 'text',
  228. 'name' => 'INVITES-plex-tv-labels',
  229. 'label' => 'TV Labels (comma separated)',
  230. 'value' => $GLOBALS['INVITES-plex-tv-labels'],
  231. 'placeholder' => 'All'
  232. ),
  233. array(
  234. 'type' => 'text',
  235. 'name' => 'INVITES-plex-movies-labels',
  236. 'label' => 'Movies Labels (comma separated)',
  237. 'value' => $GLOBALS['INVITES-plex-movies-labels'],
  238. 'placeholder' => 'All'
  239. ),
  240. array(
  241. 'type' => 'text',
  242. 'name' => 'INVITES-plex-music-labels',
  243. 'label' => 'Music Labels (comma separated)',
  244. 'value' => $GLOBALS['INVITES-plex-music-labels'],
  245. 'placeholder' => 'All'
  246. ),
  247. ),
  248. 'Emby Settings' => array(
  249. array(
  250. 'type' => 'password-alt',
  251. 'name' => 'embyToken',
  252. 'label' => 'Emby API key',
  253. 'value' => $GLOBALS['embyToken'],
  254. 'placeholder' => 'enter key from emby'
  255. ),
  256. array(
  257. 'type' => 'text',
  258. 'name' => 'embyURL',
  259. 'label' => 'Emby server adress',
  260. 'value' => $GLOBALS['embyURL'],
  261. 'placeholder' => 'localhost:8086'
  262. ),
  263. array(
  264. 'type' => 'text',
  265. 'name' => 'INVITES-EmbyTemplate',
  266. 'label' => 'Emby User to be used as template for new users',
  267. 'value' => $GLOBALS['INVITES-EmbyTemplate'],
  268. 'placeholder' => 'AdamSmith'
  269. )
  270. ),
  271. 'FYI' => array(
  272. array(
  273. 'type' => 'html',
  274. 'label' => 'Note',
  275. 'html' => 'After enabling for the first time, please reload the page - Menu is located under User menu on top right'
  276. )
  277. )
  278. );
  279. }
  280. function inviteAction($username, $action = null, $type = null)
  281. {
  282. if ($action == null) {
  283. return false;
  284. }
  285. switch ($type) {
  286. case 'plex':
  287. if (!empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'])) {
  288. $url = "https://plex.tv/api/servers/" . $GLOBALS['plexID'] . "/shared_servers/";
  289. if ($GLOBALS['INVITES-plexLibraries'] !== "") {
  290. $libraries = explode(',', $GLOBALS['INVITES-plexLibraries']);
  291. } else {
  292. $libraries = '';
  293. }
  294. if ($GLOBALS['INVITES-plex-tv-labels'] !== "") {
  295. $tv_labels = "label=" . $GLOBALS['INVITES-plex-tv-labels'];
  296. } else { $tv_labels = ""; }
  297. if ($GLOBALS['INVITES-plex-movies-labels'] !== "") {
  298. $movies_labels = "label=" . $GLOBALS['INVITES-plex-movies-labels'];
  299. } else { $movies_labels = ""; }
  300. if ($GLOBALS['INVITES-plex-music-labels'] !== "") {
  301. $music_labels = "label=" . $GLOBALS['INVITES-plex-music-labels'];
  302. } else { $music_labels = ""; }
  303. $headers = array(
  304. "Accept" => "application/json",
  305. "Content-Type" => "application/json",
  306. "X-Plex-Token" => $GLOBALS['plexToken']
  307. );
  308. $data = array(
  309. "server_id" => $GLOBALS['plexID'],
  310. "shared_server" => array(
  311. "library_section_ids" => $libraries,
  312. "invited_email" => $username
  313. ),
  314. "sharing_settings" => array(
  315. "filterTelevision" => $tv_labels,
  316. "filterMovies" => $movies_labels,
  317. "filterMusic" => $music_labels
  318. )
  319. );
  320. try {
  321. switch ($action) {
  322. case 'share':
  323. $response = Requests::post($url, $headers, json_encode($data), array());
  324. break;
  325. case 'unshare':
  326. $id = (is_numeric($username) ? $username : convertPlexName($username, "id"));
  327. $url = $url . $id;
  328. $response = Requests::delete($url, $headers, array());
  329. break;
  330. default:
  331. return false;
  332. break;
  333. }
  334. if ($response->success) {
  335. writeLog('success', 'Plex Invite Function - Plex User now has access to system', $username);
  336. return true;
  337. } else {
  338. switch ($response->status_code) {
  339. case 400:
  340. writeLog('error', 'Plex Invite Function - Plex User already has access', $username);
  341. return false;
  342. break;
  343. case 401:
  344. writeLog('error', 'Plex Invite Function - Incorrect Token', 'SYSTEM');
  345. return false;
  346. break;
  347. case 404:
  348. writeLog('error', 'Plex Invite Function - Libraries not setup correct [' . $GLOBALS['INVITES-plexLibraries'] . ']', 'SYSTEM');
  349. return false;
  350. break;
  351. default:
  352. writeLog('error', 'Plex Invite Function - An error occurred [' . $response->status_code . ']', $username);
  353. return false;
  354. break;
  355. }
  356. }
  357. } catch (Requests_Exception $e) {
  358. writeLog('error', 'Plex Invite Function - Error: ' . $e->getMessage(), 'SYSTEM');
  359. return false;
  360. };
  361. } else {
  362. writeLog('error', 'Plex Invite Function - Plex Token/ID not set', 'SYSTEM');
  363. return false;
  364. }
  365. break;
  366. case 'emby':
  367. try {
  368. #add emby user to sytem
  369. return true;
  370. } catch (Requests_Exception $e) {
  371. writeLog('error', 'Emby Invite Function - Error: ' . $e->getMessage(), 'SYSTEM');
  372. return false;
  373. }
  374. break;
  375. default:
  376. return false;
  377. break;
  378. }
  379. return false;
  380. }