album-add.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/Album.php');
  18. try {
  19. $artist = new Artist($_GET['artist']);
  20. } catch (Exception $e) {
  21. displayError("Artist not found",
  22. "The artist {$_GET['artist']} was not found in the database.");
  23. }
  24. if (!isset($this_user) || !$this_user->manages($artist->name)) {
  25. displayError("Permission denied",
  26. "You don't have permission to edit this artist's details.");
  27. }
  28. $edit = false;
  29. if (isset($_GET['album'])) {
  30. $edit = true;
  31. try {
  32. $album = new Album($_GET['album'], $artist->name);
  33. } catch (Exception $e) {
  34. displayError("Album not found",
  35. "The album {$_GET['album']} by artist {$artist->name} was not found in the database.");
  36. }
  37. }
  38. $smarty->assign('artist', $artist);
  39. $smarty->assign('edit', $edit);
  40. if ($edit) {
  41. $name = $album->name;
  42. $smarty->assign('name', $name);
  43. $smarty->assign('image', $album->image);
  44. $smarty->assign('pageheading', '<a href="' . $artist->getURL() . '">' . $artist->name . '</a> &mdash; Edit Album');
  45. } else {
  46. $smarty->assign('pageheading', '<a href="' . $artist->getURL() . '">' . $artist->name . '</a> &mdash; Add Album');
  47. }
  48. if (isset($_POST['submit'])) {
  49. if(!$edit) {
  50. if (empty($_POST['name'])) {
  51. $errors[] = 'An album name must be specified.';
  52. }
  53. $name = $_POST['name'];
  54. }
  55. if (empty($_POST['image'])) {
  56. $image = '';
  57. } else if (!preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['image'])) {
  58. $errors[] = 'Cover image must be a valid URL';
  59. } else if (preg_match('/\s/', $_POST['homepage'])) {
  60. $errors[] = 'Cover image must be a URL, as such it cannot contain whitespace.';
  61. } else {
  62. $image = $_POST['image'];
  63. }
  64. if ($errors) {
  65. $smarty->assign('errors', $errors);
  66. $smarty->assign('image', $image);
  67. $smarty->assign('name', $_POST['name']);
  68. } else {
  69. if($edit) {
  70. $album->setImage($image);
  71. } else {
  72. $album = Album::create($name, $artist->name, $image);
  73. }
  74. // If the creation was successful send the user back to the view page
  75. header('Location: ' . $album->getURL());
  76. }
  77. }
  78. $smarty->display('album-add.tpl');