invites.php 11 KB

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