edit_group.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once('database.php');
  16. require_once('templating.php');
  17. require_once('data/User.php');
  18. require_once('data/Group.php');
  19. require_once('data/TagCloud.php');
  20. if ($logged_in == false) {
  21. $smarty->assign('pageheading', 'Error!');
  22. $smarty->assign('details', 'Not logged in! You shouldn\'t be here!');
  23. $smarty->display('error.tpl');
  24. die();
  25. }
  26. if ($_REQUEST['group'] == 'new') {
  27. if ($_REQUEST['new']) {
  28. try {
  29. $result = Group::create(strtolower($_REQUEST['new']), $this_user);
  30. } catch (Exception $e) {
  31. $smarty->assign('pageheading', 'Error!');
  32. $smarty->assign('details', $e->getMessage());
  33. $smarty->display('error.tpl');
  34. die();
  35. }
  36. if ($result instanceof Group) {
  37. header('Location: ' . $base_url . '/edit_group.php?group=' . $_REQUEST['new']);
  38. exit();
  39. }
  40. } else {
  41. $smarty->assign('newform', true);
  42. try {
  43. $aTagCloud = TagCloud::GenerateTagCloud(TagCloud::scrobblesTable(), 'artist');
  44. $smarty->assign('tagcloud', $aTagCloud);
  45. } catch (Exception $e) {}
  46. $smarty->display('edit_group.tpl');
  47. exit();
  48. }
  49. }
  50. $group = new Group($_REQUEST['group']);
  51. if ($group->owner->name != $this_user->name) {
  52. $smarty->assign('pageheading', 'Error!');
  53. $smarty->assign('details', 'You don\'t own this group!');
  54. $smarty->display('error.tpl');
  55. die();
  56. }
  57. $errors = array();
  58. if ($_POST['submit']) {
  59. if (!empty($_POST['homepage'])) {
  60. # Need better URI validation, but this will do for now. I think
  61. # PEAR has a suitable module to help out here.
  62. if (!preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['homepage'])) {
  63. $errors[] = 'Homepage must be a URI.';
  64. }
  65. if (preg_match('/\s/', $_POST['homepage'])) {
  66. $errors[] = 'Homepage must be a URI. Valid URIs cannot contain whitespace.';
  67. }
  68. }
  69. if (!empty($_POST['avatar_uri'])) {
  70. # Need better URI validation, but this will do for now. I think
  71. # PEAR has a suitable module to help out here.
  72. if (!preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['avatar_uri'])) {
  73. $errors[] = 'Avatar must be a URI.';
  74. }
  75. if (preg_match('/\s/', $_POST['avatar_uri'])) {
  76. $errors[] = 'Avatar must be a URI. Valid URIs cannot contain whitespace.';
  77. }
  78. }
  79. if (!isset($errors[0])) {
  80. if ($_POST['owner'] != $group->owner->username) {
  81. try {
  82. $new_owner = new User($_POST['owner']);
  83. } catch (Exception $e) {
  84. $smarty->assign('pageheading', 'Error!');
  85. $smarty->assign('details', 'Cannot assign group ownership to someone who does not exist!');
  86. $smarty->display('error.tpl');
  87. die();
  88. }
  89. if (!$group->memberCheck($new_owner)) {
  90. $smarty->assign('pageheading', 'Error!');
  91. $smarty->assign('details', 'Cannot assign group ownership to someone who is not a member!');
  92. $smarty->display('error.tpl');
  93. die();
  94. } else {
  95. $group->owner = $new_owner;
  96. }
  97. }
  98. $group->fullname = $_POST['fullname'];
  99. $group->homepage = $_POST['homepage'];
  100. $group->bio = $_POST['bio'];
  101. $group->avatar_uri = $_POST['avatar_uri'];
  102. $group->save();
  103. header('Location: ' . $group->getURL());
  104. exit;
  105. }
  106. if (isset($errors[0])) {
  107. header('Content-Type: text/plain');
  108. //($errors);
  109. exit;
  110. }
  111. }
  112. if (isset($group->name)) {
  113. # Stuff which cannot be changed.
  114. $smarty->assign('group', $group->name);
  115. if ($_POST['submit']) {
  116. $smarty->assign('fullname', $_POST['fullname']);
  117. $smarty->assign('bio', $_POST['bio']);
  118. $smarty->assign('homepage', $_POST['homepage']);
  119. $smarty->assign('avatar_uri', $_POST['avatar_uri']);
  120. } else {
  121. $smarty->assign('fullname', $group->fullname);
  122. $smarty->assign('bio', $group->bio);
  123. $smarty->assign('homepage', $group->homepage);
  124. $smarty->assign('avatar_uri', $group->avatar_uri);
  125. }
  126. $smarty->assign('members', $group->getUsers());
  127. $smarty->assign('owner', $group->owner);
  128. # And display the page.
  129. $smarty->assign('pageheading', $errors);
  130. $smarty->assign('newform', false);
  131. try {
  132. $aUserTagCloud = $group->tagCloudData();
  133. $smarty->assign('tagcloud', $aTagCloud);
  134. } catch (Exception $e) {}
  135. $smarty->display('edit_group.tpl');
  136. } else {
  137. $smarty->assign('pageheading', 'Group not found');
  138. $smarty->assign('details', 'Shall I call in a missing peoples report? This shouldn\'t happen.');
  139. $smarty->display('error.tpl');
  140. }