invites.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. writeLog('error', 'Invite Management Function - Error ['.$e.']', 'SYSTEM');
  122. return false;
  123. }
  124. break;
  125. case "get":
  126. try {
  127. $connect = new Dibi\Connection([
  128. 'driver' => 'sqlite3',
  129. 'database' => $GLOBALS['dbLocation'].$GLOBALS['dbName'],
  130. ]);
  131. $invites = $connect->fetchAll('SELECT * FROM invites');
  132. return $invites;
  133. } catch (Dibi\Exception $e) {
  134. writeLog('error', 'Invite Management Function - Error ['.$e.']', 'SYSTEM');
  135. return false;
  136. }
  137. break;
  138. case "delete":
  139. try {
  140. $connect = new Dibi\Connection([
  141. 'driver' => 'sqlite3',
  142. 'database' => $GLOBALS['dbLocation'].$GLOBALS['dbName'],
  143. ]);
  144. $connect->query('DELETE FROM invites WHERE id = ?', $id);
  145. return true;
  146. } catch (Dibi\Exception $e) {
  147. writeLog('error', 'Invite Management Function - Error ['.$e.']', 'SYSTEM');
  148. return false;
  149. }
  150. break;
  151. default:
  152. return false;
  153. }
  154. }
  155. }
  156. }
  157. /* GET PHPMAILER SETTINGS */
  158. function invitesGetSettings(){
  159. if($GLOBALS['plexID'] !== '' && $GLOBALS['plexToken'] !== '' && $GLOBALS['INVITES-type-include'] !== ''){
  160. $loop = libraryList($GLOBALS['INVITES-type-include'])['libraries'];
  161. foreach ($loop as $key => $value) {
  162. $libraryList[] = array(
  163. 'name' => $key,
  164. 'value' => $value
  165. );
  166. }
  167. }else{
  168. $libraryList = array(
  169. array(
  170. 'name'=>'Refresh page to update List',
  171. 'value'=>'',
  172. 'disabled' => true,
  173. ),
  174. );
  175. }
  176. return array(
  177. 'Backend' => array(
  178. array(
  179. 'type' => 'select',
  180. 'name' => 'INVITES-type-include',
  181. 'label' => 'Media Server',
  182. 'value' => $GLOBALS['INVITES-type-include'],
  183. 'options' => array(
  184. array(
  185. 'name'=>'N/A',
  186. 'value'=>'n/a'
  187. ),
  188. array(
  189. 'name'=>'Plex',
  190. 'value'=>'plex'
  191. ),
  192. array(
  193. 'name'=>'Emby [Not Ready]',
  194. 'value'=>'emby'
  195. )
  196. )
  197. )
  198. ),
  199. 'Plex Settings' => array(
  200. array(
  201. 'type' => 'password-alt',
  202. 'name' => 'plexToken',
  203. 'label' => 'Plex Token',
  204. 'value' => $GLOBALS['plexToken'],
  205. 'placeholder' => 'Use Get Token Button'
  206. ),
  207. array(
  208. 'type' => 'password-alt',
  209. 'name' => 'plexID',
  210. 'label' => 'Plex Machine',
  211. 'value' => $GLOBALS['plexID'],
  212. 'placeholder' => 'Use Get Plex Machine Button'
  213. ),
  214. array(
  215. 'type' => 'select2',
  216. 'name' => 'INVITES-plexLibraries',
  217. 'label' => 'Libraries',
  218. 'value' => $GLOBALS['INVITES-plexLibraries'],
  219. 'options' => $libraryList
  220. )
  221. ),
  222. 'Emby Settings' => array(
  223. ),
  224. 'FYI' => array(
  225. array(
  226. 'type' => 'html',
  227. 'label' => 'Note',
  228. 'html' => 'After enabling for the first time, please reload the page'
  229. ),
  230. array(
  231. 'type' => 'button',
  232. 'label' => 'Open Invite Modal',
  233. 'class' => 'inline-popups inviteModal',
  234. 'attr' => 'data-effect="mfp-zoom-out"',
  235. 'href' => '#invite-area',
  236. 'icon' => 'fa fa-paper-plane',
  237. 'text' => 'Open'
  238. )
  239. )
  240. );
  241. }
  242. function inviteAction($username,$action=null,$type=null){
  243. if($action == null){
  244. return false;
  245. }
  246. switch ($type) {
  247. case 'plex':
  248. if(!empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'])){
  249. $url = "https://plex.tv/api/servers/".$GLOBALS['plexID']."/shared_servers/";
  250. if($GLOBALS['INVITES-plexLibraries'] !== ""){
  251. $libraries = explode(',',$GLOBALS['INVITES-plexLibraries']);
  252. }else{
  253. $libraries = '';
  254. }
  255. $headers = array(
  256. "Accept" => "application/json",
  257. "Content-Type" => "application/json",
  258. "X-Plex-Token" => $GLOBALS['plexToken']
  259. );
  260. $data = array(
  261. "server_id" => $GLOBALS['plexID'],
  262. "shared_server" => array(
  263. "library_section_ids" => $libraries,
  264. "invited_email" => $username
  265. )
  266. );
  267. try{
  268. switch ($action) {
  269. case 'share':
  270. $response = Requests::post($url, $headers, json_encode($data), array());
  271. break;
  272. case 'unshare':
  273. $id = (is_numeric($username) ? $id : convertPlexName($username, "id"));
  274. $url = $url.$id;
  275. $response = Requests::delete($url, $headers, array());
  276. break;
  277. default:
  278. return false;
  279. break;
  280. }
  281. if($response->success){
  282. writeLog('success', 'Plex Invite Function - Plex User now has access to system', $username);
  283. return true;
  284. }else{
  285. switch ($response->status_code) {
  286. case 400:
  287. writeLog('error', 'Plex Invite Function - Plex User already has access', $username);
  288. return false;
  289. break;
  290. case 401:
  291. writeLog('error', 'Plex Invite Function - Incorrect Token', 'SYSTEM');
  292. return false;
  293. break;
  294. default:
  295. writeLog('error', 'Plex Invite Function - An error occured', $username);
  296. return false;
  297. break;
  298. }
  299. }
  300. return false;
  301. }catch( Requests_Exception $e ) {
  302. writeLog('error', 'Plex Invite Function - Error: '.$e->getMessage(), 'SYSTEM');
  303. return false;
  304. };
  305. }else{
  306. writeLog('error', 'Plex Invite Function - Plex Token/ID not set', 'SYSTEM');
  307. return false;
  308. }
  309. break;
  310. case 'emby':
  311. # code...
  312. break;
  313. default:
  314. return false;
  315. break;
  316. }
  317. return false;
  318. }