invites.php 10 KB

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