PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/plex/web/index.php

https://gitlab.com/x33n/ampache
PHP | 215 lines | 176 code | 18 blank | 21 comment | 16 complexity | a423b77d1b863967e09b1e7f83942723 MD5 | raw file
  1. <?php
  2. /* vim:set softtabstop=4 shiftwidth=4 expandtab: */
  3. /**
  4. *
  5. * LICENSE: GNU General Public License, version 2 (GPLv2)
  6. * Copyright 2001 - 2015 Ampache.org
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License v2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. require_once 'init.php';
  23. ?>
  24. <html>
  25. <head>
  26. <title>Ampache/Plex Configuration</title>
  27. <link rel="stylesheet" href="style.css" />
  28. <script>
  29. function changeUniqid()
  30. {
  31. if (confirm("<?php echo T_('Changing the server UUID could break clients connectivity. Do you confirm?'); ?>")) {
  32. document.location='/web/?plexact=change_uniqid';
  33. }
  34. }
  35. </script>
  36. </head>
  37. <body>
  38. <div id="main">
  39. <div id="maincontainer">
  40. <img src="/images/plex-icon-256.png" /><br />
  41. <?php
  42. function init_db()
  43. {
  44. if (!Preference::exists('myplex_username')) {
  45. Preference::insert('myplex_username','myPlex Username','','25','string','internal');
  46. Preference::insert('myplex_authtoken','myPlex Auth Token','','25','string','internal');
  47. Preference::insert('myplex_published','Plex Server is published to myPlex','0','25','boolean','internal');
  48. Preference::insert('plex_uniqid','Plex Server Unique Id', uniqid(),'25','string','internal');
  49. Preference::insert('plex_servername','Plex Server Name','Ampache','25','string','internal');
  50. Preference::insert('plex_public_address','Plex Public Address','','25','string','internal');
  51. Preference::insert('plex_public_port','Plex Public Port','32400','25','string','internal');
  52. Preference::insert('plex_local_auth','myPlex authentication required on local network','0','25','boolean','internal');
  53. Preference::insert('plex_match_email','Link myPlex users to Ampache based on e-mail address','1','25','boolean','internal');
  54. User::rebuild_all_preferences();
  55. }
  56. }
  57. init_db();
  58. $myplex_username = Plex_XML_Data::getMyPlexUsername();
  59. $myplex_authtoken = Plex_XML_Data::getMyPlexAuthToken();
  60. $myplex_published = Plex_XML_Data::getMyPlexPublished();
  61. $plex_servername = Plex_XML_Data::getServerName();
  62. $plex_public_address = Plex_XML_Data::getServerPublicAddress();
  63. $plex_public_port = Plex_XML_Data::getServerPublicPort();
  64. $plex_local_port = Plex_XML_Data::getServerPort();
  65. $plex_local_auth = AmpConfig::get('plex_local_auth');
  66. $plex_match_email = AmpConfig::get('plex_match_email');
  67. $plexact = $_REQUEST['plexact'];
  68. switch ($plexact) {
  69. case 'auth_myplex':
  70. $myplex_username = $_POST['myplex_username'];
  71. $myplex_password = $_POST['myplex_password'];
  72. $plex_public_port = $_POST['plex_public_port'];
  73. if (!empty($myplex_username)) {
  74. // Register the server on myPlex and get auth token
  75. $myplex_authtoken = Plex_Api::validateMyPlex($myplex_username, $myplex_password);
  76. if (!empty($myplex_authtoken)) {
  77. echo T_('myPlex authentication completed.') . "<br />\r\n";
  78. Preference::update('myplex_username', -1, $myplex_username, true, true);
  79. Preference::update('myplex_authtoken', -1, $myplex_authtoken, true, true);
  80. Preference::update('plex_public_port', -1, $plex_public_port, true, true);
  81. AmpConfig::set('plex_public_port', $plex_public_port, true);
  82. $plex_public_address = Plex_Api::getPublicIp();
  83. Preference::update('plex_public_address', -1, $plex_public_address, true, true);
  84. $ret = Plex_Api::registerMyPlex($myplex_authtoken);
  85. if ($ret['status'] == '201') {
  86. Plex_Api::publishDeviceConnection($myplex_authtoken);
  87. $myplex_published = true;
  88. echo T_('Server registration completed.') . "<br />\r\n";
  89. } else {
  90. $myplex_published = false;
  91. echo "<p class='error'>" . T_('Cannot register the server on myPlex.') . "</p>";
  92. }
  93. Preference::update('myplex_published', -1, $myplex_published, true, true);
  94. } else {
  95. $myplex_authtoken = '';
  96. $myplex_published = false;
  97. echo "<p class='error'>" . T_('Cannot authenticate on myPlex.') . "</p>";
  98. }
  99. }
  100. break;
  101. case 'unauth_myplex':
  102. Plex_Api::unregisterMyPlex($myplex_authtoken);
  103. $myplex_username = '';
  104. $myplex_authtoken = '';
  105. $myplex_published = false;
  106. Preference::update('myplex_username', -1, $myplex_username, true, true);
  107. Preference::update('myplex_authtoken', -1, $myplex_authtoken, true, true);
  108. Preference::update('myplex_published', -1, $myplex_published, true, true);
  109. break;
  110. case 'save':
  111. $plex_servername = $_POST['plex_servername'];
  112. $plex_local_auth = $_POST['plex_local_auth'] ?: '0';
  113. $plex_match_email = $_POST['plex_match_email'] ?: '0';
  114. Preference::update('plex_servername', -1, $plex_servername, true, true);
  115. Preference::update('plex_local_auth', -1, $plex_local_auth, true, true);
  116. Preference::update('plex_match_email', -1, $plex_match_email, true, true);
  117. break;
  118. case 'change_uniqid':
  119. Preference::update('plex_uniqid', -1,uniqid(), true, true);
  120. echo T_('Server UUID changed.') . "<br />\r\n";
  121. break;
  122. }
  123. ?>
  124. <p class="info">Configure your Plex server settings bellow.</p>
  125. <div class="configform">
  126. <h3>Server Settings</h3>
  127. <form action="" method="POST" enctype="multipart/form-data">
  128. <input type="hidden" name="plexact" value="save" />
  129. <div class="field">
  130. <label for="plex_servername">Server Name:</label>
  131. <input id="plex_servername" class="field_value" type="text" name="plex_servername" value="<?php echo $plex_servername; ?>" />
  132. </div>
  133. <div class="field">
  134. <label for="plex_local_auth">myPlex authentication required on local network</label>
  135. <input type="checkbox" id="plex_local_auth" name="plex_local_auth" value="1" <?php if ($plex_local_auth) { echo "checked"; } ?>>
  136. </div>
  137. <div class="field">
  138. <label for="plex_match_email">Link myPlex users to Ampache based on e-mail address</label>
  139. <input type="checkbox" id="plex_match_email" name="plex_match_email" value="1" <?php if ($plex_match_email) { echo "checked"; } ?>>
  140. </div>
  141. <div class="formbuttons">
  142. <input type="submit" value="Save" />
  143. </div>
  144. </form>
  145. </div><br />
  146. <?php if (empty($myplex_authtoken)) { ?>
  147. <div class="configform">
  148. <h3>myPlex authentication / server publish</h3>
  149. <form action="" method="POST" enctype="multipart/form-data">
  150. <input type="hidden" name="plexact" value="auth_myplex" />
  151. <div class="field">
  152. <label for="myplex_username">myPlex Username:</label>
  153. <input type="text" id="myplex_username" class="field_value" name="myplex_username" value="<?php echo $myplex_username; ?>" />
  154. </div>
  155. <div class="field">
  156. <label for="myplex_password">myPlex Password:</label>
  157. <input id="myplex_password" type="password" class="field_value" name="myplex_password" />
  158. </div>
  159. <div class="field">
  160. <label for="plex_public_port">Public Server Port (optional):</label>
  161. <input type="text" id="plex_public_port" class="field_value" name="plex_public_port" value="<?php echo $plex_public_port; ?>" />
  162. </div>
  163. <?php if ($plex_local_port != 32400) { ?>
  164. <div style="color: orange;">
  165. Plex servers should locally listen on port 32400. Current local listing port for your Plex backend is <?php echo $plex_local_port; ?>. Ampache applies a small URI `hack` to work with custom port
  166. as Plex server, but be aware that this will not work with all clients.
  167. </div>
  168. <?php } ?>
  169. <div class="formbuttons">
  170. <input type="submit" value="Auth/Publish" />
  171. </div>
  172. </form>
  173. </div><br />
  174. <?php } else { ?>
  175. <div class="configform">
  176. <h3>myPlex authentication / server publish</h3>
  177. <form action="" method="POST" enctype="multipart/form-data">
  178. <label>myPlex user: <b><?php echo $myplex_username; ?></b></label><br />
  179. <label>Public server address: <b><?php echo $plex_public_address; ?>:<?php echo $plex_public_port; ?></b></label>
  180. <input type="hidden" name="plexact" value="unauth_myplex" />
  181. <div class="formbuttons">
  182. <input type="submit" value="Unregister" />
  183. </div>
  184. </form>
  185. </div><br />
  186. <?php } ?>
  187. <br />
  188. <div class="configform">
  189. <h3>Tools</h3><form>
  190. <div class="formbuttons">
  191. <input type="button" value="Change Server UUID" onclick="changeUniqid();" />
  192. </div></form>
  193. </div><br />
  194. </div>
  195. </div>
  196. </body>
  197. </html>