We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

edit_group.php 4.65 KB
Newer Older
tobyink's avatar
tobyink committed
1 2
<?php

3
/* GNU FM -- a free network service for sharing your music listening habits
tobyink's avatar
tobyink committed
4

5
   Copyright (C) 2009 Free Software Foundation, Inc
tobyink's avatar
tobyink committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

require_once('database.php');
require_once('templating.php');
require_once('data/User.php');
require_once('data/Group.php');
require_once('data/TagCloud.php');

if($logged_in == false)
{
	$smarty->assign('error', 'Error!');
	$smarty->assign('details', 'Not logged in! You shouldn\'t be here!');
	$smarty->display('error.tpl');
	die();
}

tobyink's avatar
tobyink committed
36 37 38 39
if ($_REQUEST['group']=='new')
{
	if ($_REQUEST['new'])
	{
40 41 42
		try {
			$result = Group::create(strtolower($_REQUEST['new']), $this_user);
		} catch (exception $e) {
43
			$smarty->assign('error', 'Error!');
44
			$smarty->assign('details', $e->getMessage());
45 46 47
			$smarty->display('error.tpl');
			die();
		}
48 49 50 51
		if ($result instanceof Group) {
			header('Location: ' . $base_url . '/edit_group.php?group=' . $_REQUEST['new']);
			exit();
		}
tobyink's avatar
tobyink committed
52 53 54 55
	}
	else
	{
		$smarty->assign('newform', true);
56 57
		try {
			$aTagCloud = TagCloud::GenerateTagCloud(TagCloud::scrobblesTable(), 'artist');
58
			$smarty->assign('tagcloud', $aTagCloud);
59
		} catch (exception $e) {}
tobyink's avatar
tobyink committed
60 61 62 63 64
		$smarty->display('edit_group.tpl');
		exit();
	}
}

tobyink's avatar
tobyink committed
65 66
$group = new Group($_REQUEST['group']);

67
if ($group->owner->name != $this_user->name)
tobyink's avatar
tobyink committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
	$smarty->assign('error', 'Error!');
	$smarty->assign('details', 'You don\'t own this group!');
	$smarty->display('error.tpl');
	die();
}

$errors = array();

if ($_POST['submit'])
{
	if (!empty($_POST['homepage']))
	{
		# Need better URI validation, but this will do for now. I think
		# PEAR has a suitable module to help out here.
		if ( !preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['homepage']) )
elleo's avatar
elleo committed
84
			$errors[] = 'Homepage must be a URI.';
tobyink's avatar
tobyink committed
85
		if ( preg_match('/\s/', $_POST['homepage']) )
elleo's avatar
elleo committed
86
			$errors[] = 'Homepage must be a URI. Valid URIs cannot contain whitespace.';
tobyink's avatar
tobyink committed
87 88 89 90 91 92 93
	}

	if (!empty($_POST['avatar_uri']))
	{
		# Need better URI validation, but this will do for now. I think
		# PEAR has a suitable module to help out here.
		if ( !preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['avatar_uri']) )
elleo's avatar
elleo committed
94
			$errors[] = 'Avatar must be a URI.';
tobyink's avatar
tobyink committed
95
		if ( preg_match('/\s/', $_POST['avatar_uri']) )
elleo's avatar
elleo committed
96
			$errors[] = 'Avatar must be a URI. Valid URIs cannot contain whitespace.';
tobyink's avatar
tobyink committed
97 98 99 100
	}

	if (!isset($errors[0]))
	{
101 102 103
		if ($_POST['owner'] != $group->owner->username)
		{
			$new_owner = new User($_POST['owner']);
clint's avatar
clint committed
104

105 106 107 108 109
			if (! $group->memberCheck($new_owner))
			{
				$smarty->assign('error', 'Error!');
				$smarty->assign('details', 'Cannot assign group ownership to someone who is not a member!');
				$smarty->display('error.tpl');
clint's avatar
clint committed
110
				die();
111 112 113 114 115 116
			}
			else
			{
				$group->owner = $new_owner;
			}
		}
clint's avatar
clint committed
117

tobyink's avatar
tobyink committed
118 119 120 121
		$group->fullname    = $_POST['fullname'];
		$group->homepage    = $_POST['homepage'];
		$group->bio         = $_POST['bio'];
		$group->avatar_uri  = $_POST['avatar_uri'];
clint's avatar
clint committed
122

tobyink's avatar
tobyink committed
123 124
		$group->save();

elleo's avatar
elleo committed
125
		header('Location: ' . $group->getURL());
tobyink's avatar
tobyink committed
126 127 128 129 130
		exit;
	}

	if (isset($errors[0]))
	{
elleo's avatar
elleo committed
131
		header('Content-Type: text/plain');
132
		//($errors);
tobyink's avatar
tobyink committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		exit;
	}
}

if(isset($group->name))
{
	# Stuff which cannot be changed.
	$smarty->assign('group', $group->name);

	if ($_POST['submit'])
	{
		$smarty->assign('fullname',     $_POST['fullname']);
		$smarty->assign('bio',          $_POST['bio']);
		$smarty->assign('homepage',     $_POST['homepage']);
		$smarty->assign('avatar_uri',   $_POST['avatar_uri']);
	}
	else
	{
151 152 153 154
		$smarty->assign('fullname',     $group->fullname);
		$smarty->assign('bio',          $group->bio);
		$smarty->assign('homepage',     $group->homepage);
		$smarty->assign('avatar_uri',   $group->avatar_uri);
tobyink's avatar
tobyink committed
155 156
	}

157 158 159
	$smarty->assign('members', $group->getUsers());
	$smarty->assign('owner',   $group->owner);

tobyink's avatar
tobyink committed
160 161
	# And display the page.
	$smarty->assign('errors', $errors);
tobyink's avatar
tobyink committed
162
	$smarty->assign('newform', false);
163 164
	try {
		$aUserTagCloud = $group->tagCloudData();
165
		$smarty->assign('tagcloud', $aTagCloud);
166
	} catch (exception $e) {}
tobyink's avatar
tobyink committed
167 168 169 170 171 172 173 174 175 176
	$smarty->display('edit_group.tpl');
}

else
{
	$smarty->assign('error', 'Group not found');
	$smarty->assign('details', 'Shall I call in a missing peoples report? This shouldn\'t happen.');
	$smarty->display('error.tpl');
}