invites.php 13 KB

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