Group.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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($install_path . '/database.php');
  16. require_once($install_path . '/data/sanitize.php');
  17. require_once($install_path . '/utils/human-time.php');
  18. require_once($install_path . '/data/Server.php');
  19. require_once($install_path . '/data/TagCloud.php');
  20. require_once($install_path . '/data/User.php');
  21. /**
  22. * Represents Group data
  23. *
  24. * General attributes are accessible as public variables.
  25. *
  26. * @deprecated This class hasnt been used in a long time and needs to be reviewed before use. 20120308 kabniel
  27. */
  28. class Group {
  29. public $id, $gid, $name, $owner, $fullname, $bio, $homepage, $count, $grouptype, $avatar_uri, $users;
  30. /**
  31. * User constructor
  32. *
  33. * @param string $name The name of the user to load
  34. */
  35. function __construct($name, $data = null) {
  36. global $base_url;
  37. $base = preg_replace('#/$#', '', $base_url);
  38. if (is_array($data)) {
  39. $row = $data;
  40. } else {
  41. global $adodb;
  42. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  43. try {
  44. $res = $adodb->GetRow('SELECT * FROM Groups WHERE lower(groupname) = lower(' . $adodb->qstr($name) . ')');
  45. } catch (Exception $e) {
  46. header('Content-Type: text/plain');
  47. exit;
  48. }
  49. if ($res) {
  50. $row = $res;
  51. }
  52. }
  53. if (is_array($row)) {
  54. $this->gid = $row['id'];
  55. $this->name = $row['groupname'];
  56. $this->fullname = $row['fullname'];
  57. $this->homepage = $row['homepage'];
  58. $this->bio = $row['bio'];
  59. $this->avatar_uri = $row['avatar_uri'];
  60. $this->owner = User::new_from_uniqueid_number($row['owner']);
  61. $this->count = -1;
  62. $this->users = array();
  63. if (!preg_match('/\:/', $this->id)) {
  64. $this->id = $base . '/group/' . rawurlencode($this->name) . '#group';
  65. }
  66. }
  67. }
  68. /**
  69. * Selects a random nixtape group.
  70. *
  71. * @return object a Group object on success, or false if there are no groups existing.
  72. * @author tobyink
  73. */
  74. static function random() {
  75. global $adodb;
  76. if (strtolower(substr($connect_string, 0, 5)) == 'mysql') {
  77. $random = 'RAND';
  78. } else if (strtolower(substr($connect_string, 0, 5)) == 'mssql') {
  79. $random = 'NEWID'; // I don't think we try to support MSSQL, but here's how it's done theoretically anyway
  80. } else {
  81. $random = 'RANDOM'; // postgresql, sqlite, possibly others
  82. }
  83. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  84. try {
  85. $res = $adodb->GetRow("SELECT * FROM Groups ORDER BY {$random}() LIMIT 1");
  86. } catch (Exception $e) {
  87. return $res;
  88. }
  89. if ($res) {
  90. $row = $res;
  91. return new Group($row['groupname'], $row);
  92. } else {
  93. // No groups found.
  94. return false;
  95. }
  96. }
  97. /**
  98. * Create a new nixtape group.
  99. *
  100. * @param string $name the name of the group (used to generate its URL).
  101. * @param object $owner a User object representing the person who owns this group.
  102. * @return object a Group object on success, throw an Exception object otherwise.
  103. * @author tobyink
  104. */
  105. static function create($name, $owner) {
  106. global $adodb;
  107. if (!preg_match('/^[A-Za-z0-9][A-Za-z0-9_\.-]*[A-Za-z0-9]$/', $name)) {
  108. throw (new Exception('Group names should only contain letters, numbers, hyphens, underscores and full stops (a.k.a. dots/periods), must be at least two characters long, and can\'t start or end with punctuation.'));
  109. }
  110. if (in_array(strtolower($name), array('new', 'search'))) {
  111. throw (new Exception("Not allowed to create a group called '{$name}' (reserved word)!"));
  112. }
  113. // Check to make sure no existing group with same name (case-insensitive).
  114. $q = sprintf('SELECT groupname FROM Groups WHERE LOWER(groupname)=LOWER(%s)'
  115. , $adodb->qstr($name));
  116. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  117. try {
  118. $res = $adodb->GetRow($q);
  119. } catch (Exception $e) {
  120. return $res;
  121. }
  122. if ($res) {
  123. $row = $res;
  124. $existing = $row['groupname'];
  125. throw (new Exception(
  126. ($existing == $name) ?
  127. "There is already a group called '{$existing}'." :
  128. "The name '{$name}' it too similar to existing group '{$existing}'"
  129. ));
  130. }
  131. // Create new group
  132. $q = sprintf('INSERT INTO Groups (groupname, owner, created, modified) VALUES (%s, %s, %d, %d)'
  133. , $adodb->qstr($name)
  134. , (int)($owner->uniqueid)
  135. , time()
  136. , time());
  137. try {
  138. $res = $adodb->Execute($q);
  139. } catch (Exception $e) {
  140. return $res;
  141. }
  142. // Get ID number for group
  143. $q = sprintf('SELECT id FROM Groups WHERE lower(groupname) = lower(%s)', $adodb->quote($name, 'text'));
  144. try {
  145. $res = $adodb->GetOne($q);
  146. } catch (Exception $e) {
  147. return $res;
  148. }
  149. if (!$res) {
  150. throw (new Exception('Something has gone horribly, horribly wrong!'));
  151. }
  152. $grp = $res;
  153. // Group owner must be a member of the group
  154. $q = sprintf('INSERT INTO Group_Members (grp, member, joined) VALUES (%s, %s, %d)'
  155. , (int)($grp)
  156. , (int)($owner->uniqueid)
  157. , time());
  158. try {
  159. $res = $adodb->Execute($q);
  160. } catch (Exception $e) {
  161. return null;
  162. }
  163. // Return the newly created group. Callers should check the return value.
  164. return new Group($name);
  165. }
  166. static function groupList($user = false) {
  167. global $adodb;
  168. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  169. try {
  170. if ($user) {
  171. $res = $adodb->GetAll('SELECT gc.* FROM '
  172. . 'Group_Members m '
  173. . 'INNER JOIN (SELECT g.id, g.groupname, g.owner, g.fullname, g.bio, g.homepage, g.created, g.modified, g.avatar_uri, g.grouptype, COUNT(*) AS member_count '
  174. . 'FROM Groups g '
  175. . 'LEFT JOIN Group_Members gm ON gm.grp=g.id '
  176. . 'GROUP BY g.id, g.groupname, g.owner, g.fullname, g.bio, g.homepage, g.created, g.modified, g.avatar_uri, g.grouptype) gc '
  177. . 'ON m.grp=gc.id '
  178. . 'WHERE m.member=' . (int)($user->uniqueid));
  179. } else {
  180. $res = $adodb->GetAll('SELECT g.groupname, g.owner, g.fullname, g.bio, g.homepage, g.created, g.modified, g.avatar_uri, g.grouptype, COUNT(*) AS member_count '
  181. . 'FROM Groups g '
  182. . 'LEFT JOIN Group_Members gm ON gm.grp=g.id '
  183. . 'GROUP BY g.groupname, g.owner, g.fullname, g.bio, g.homepage, g.created, g.modified, g.avatar_uri, g.grouptype');
  184. }
  185. } catch (Exception $e) {
  186. header('Content-Type: text/plain');
  187. exit;
  188. }
  189. $list = array();
  190. foreach ($res as &$row) {
  191. $g = new Group($row['group_name'], $row);
  192. $g->count = $row['member_count'];
  193. $list[] = $g;
  194. }
  195. return $list;
  196. }
  197. function save() {
  198. global $adodb;
  199. $q = sprintf('UPDATE Groups SET '
  200. . 'owner=%s, '
  201. . 'fullname=%s, '
  202. . 'homepage=%s, '
  203. . 'bio=%s, '
  204. . 'avatar_uri=%s, '
  205. . 'modified=%d '
  206. . 'WHERE groupname=%s'
  207. , (int)($this->owner->uniqueid)
  208. , $adodb->qstr($this->fullname)
  209. , $adodb->qstr($this->homepage)
  210. , $adodb->qstr($this->bio)
  211. , $adodb->qstr($this->avatar_uri)
  212. , time()
  213. , $adodb->qstr($this->name));
  214. try {
  215. $res = $adodb->Execute($q);
  216. } catch (Exception $e) {
  217. header('Content-Type: text/plain');
  218. exit;
  219. }
  220. return 1;
  221. }
  222. /**
  223. * Retrieve a user's avatar via the gravatar service
  224. *
  225. * @param int $size The desired size of the avatar (between 1 and 512 pixels)
  226. * @return A URL to the user's avatar image
  227. */
  228. function getAvatar($size = 64) {
  229. global $base_uri;
  230. if (!empty($this->avatar_uri)) {
  231. return $this->avatar_uri;
  232. }
  233. return $base_url . '/themes/librefm/images/default-avatar-stream.png';
  234. }
  235. function getURL() {
  236. return Server::getGroupURL($this->name);
  237. }
  238. function getURLAction ($action) {
  239. $url = $this->getURL();
  240. if (strstr($url, '?')) {
  241. return $url . '&action=' . rawurlencode($action);
  242. } else {
  243. return $url . '?action=' . rawurlencode($action);
  244. }
  245. }
  246. function getUsers () {
  247. global $adodb;
  248. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  249. if (!isset($this->users[0])) {
  250. $res = $adodb->GetAll('SELECT u.* '
  251. . 'FROM Users u '
  252. . 'INNER JOIN Group_Members gm ON u.uniqueid=gm.member '
  253. . 'WHERE gm.grp=' . (int)($this->gid)
  254. . ' ORDER BY gm.joined');
  255. if ($res) {
  256. foreach ($res as &$row) {
  257. try {
  258. $this->users[$row['username']] = new User($row['username'], $row);
  259. } catch (Exception $e) {}
  260. }
  261. }
  262. $this->count = count($this->users);
  263. }
  264. return $this->users;
  265. }
  266. function memberCheck ($user) {
  267. $users = $this->getUsers();
  268. if ($users[$user->name]->name == $user->name) {
  269. return true;
  270. }
  271. return false;
  272. }
  273. function memberJoin ($user) {
  274. if ($this->memberCheck($user)) {
  275. return false;
  276. }
  277. global $adodb;
  278. try {
  279. $res = $adodb->Execute(sprintf('INSERT INTO Group_Members (grp, member, joined) VALUES (%s, %s, %d)',
  280. (int)($this->gid),
  281. (int)($user->uniqueid),
  282. time()));
  283. } catch (Exception $e) {
  284. return false;
  285. }
  286. $this->users[$user->name] = $user;
  287. return true;
  288. }
  289. function memberLeave($user) {
  290. if (!$this->memberCheck($user)) {
  291. return false;
  292. }
  293. // Group owner cannot leave, so we need a way to reassign ownership.
  294. if ($this->owner->name == $user->name) {
  295. return false;
  296. }
  297. global $adodb;
  298. try {
  299. $res = $adodb->Execute(sprintf('DELETE FROM Group_Members WHERE grp=%s AND member=%s',
  300. (int)($this->gid),
  301. (int)($user->uniqueid)));
  302. } catch (Exception $e) {
  303. return false;
  304. }
  305. $this->users[$user->name] = null;
  306. // The array key still exists though. That's annoying. PHP needs an equivalent of Perl's 'delete'.
  307. // This shouldn't actually cause us any problems, but people should be aware of the oddness.
  308. return true;
  309. }
  310. function tagCloudData () {
  311. try {
  312. return TagCloud::generateTagCloud(
  313. TagCloud::scrobblesTable('group') . ' s LEFT JOIN Users u ON s.userid=u.uniqueid LEFT JOIN Group_Members gm ON u.uniqueid=gm.member LEFT JOIN Groups g ON gm.grp=g.id',
  314. 'artist',
  315. 40,
  316. $this->name,
  317. 'groupname');
  318. } catch (Exception $e) {
  319. throw $e;
  320. }
  321. }
  322. }