invites.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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'] !== '') {
  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 [Not Ready]',
  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 invite-select',
  220. 'name' => 'INVITES-plexLibraries',
  221. 'label' => 'Libraries',
  222. 'value' => $GLOBALS['INVITES-plexLibraries'],
  223. 'options' => $libraryList
  224. )
  225. ),
  226. 'Emby Settings' => array(),
  227. 'FYI' => array(
  228. array(
  229. 'type' => 'html',
  230. 'label' => 'Note',
  231. 'html' => 'After enabling for the first time, please reload the page - Menu is located under User menu on top right'
  232. )
  233. )
  234. );
  235. }
  236. function inviteAction($username, $action = null, $type = null)
  237. {
  238. if ($action == null) {
  239. return false;
  240. }
  241. switch ($type) {
  242. case 'plex':
  243. if (!empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'])) {
  244. $url = "https://plex.tv/api/servers/" . $GLOBALS['plexID'] . "/shared_servers/";
  245. if ($GLOBALS['INVITES-plexLibraries'] !== "") {
  246. $libraries = explode(',', $GLOBALS['INVITES-plexLibraries']);
  247. } else {
  248. $libraries = '';
  249. }
  250. $headers = array(
  251. "Accept" => "application/json",
  252. "Content-Type" => "application/json",
  253. "X-Plex-Token" => $GLOBALS['plexToken']
  254. );
  255. $data = array(
  256. "server_id" => $GLOBALS['plexID'],
  257. "shared_server" => array(
  258. "library_section_ids" => $libraries,
  259. "invited_email" => $username
  260. )
  261. );
  262. try {
  263. switch ($action) {
  264. case 'share':
  265. $response = Requests::post($url, $headers, json_encode($data), array());
  266. break;
  267. case 'unshare':
  268. $id = (is_numeric($username) ? $username : convertPlexName($username, "id"));
  269. $url = $url . $id;
  270. $response = Requests::delete($url, $headers, array());
  271. break;
  272. default:
  273. return false;
  274. break;
  275. }
  276. if ($response->success) {
  277. writeLog('success', 'Plex Invite Function - Plex User now has access to system', $username);
  278. return true;
  279. } else {
  280. switch ($response->status_code) {
  281. case 400:
  282. writeLog('error', 'Plex Invite Function - Plex User already has access', $username);
  283. return false;
  284. break;
  285. case 401:
  286. writeLog('error', 'Plex Invite Function - Incorrect Token', 'SYSTEM');
  287. return false;
  288. break;
  289. case 404:
  290. writeLog('error', 'Plex Invite Function - Libraries not setup correct [' . $GLOBALS['INVITES-plexLibraries'] . ']', 'SYSTEM');
  291. return false;
  292. break;
  293. default:
  294. writeLog('error', 'Plex Invite Function - An error occurred [' . $response->status_code . ']', $username);
  295. return false;
  296. break;
  297. }
  298. }
  299. } catch (Requests_Exception $e) {
  300. writeLog('error', 'Plex Invite Function - Error: ' . $e->getMessage(), 'SYSTEM');
  301. return false;
  302. };
  303. } else {
  304. writeLog('error', 'Plex Invite Function - Plex Token/ID not set', 'SYSTEM');
  305. return false;
  306. }
  307. break;
  308. case 'emby':
  309. # code...
  310. break;
  311. default:
  312. return false;
  313. break;
  314. }
  315. return false;
  316. }