PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/flag.php

https://github.com/enjrolas/personal-DJ
PHP | 342 lines | 280 code | 34 blank | 28 comment | 45 complexity | 536d5d75f9c088c3e7625d08a16e1881 MD5 | raw file
  1. <?php
  2. /*
  3. Copyright (c) 2001 - 2006 Ampache.org
  4. All rights reserved.
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. //
  18. //
  19. function add_to_edit_queue($flags=0)
  20. {
  21. $oldflags = 0;
  22. if(empty($flags)) $flags = 0;
  23. if($_SESSION['edit_queue'])
  24. {
  25. $oldflags = $_SESSION['edit_queue'];
  26. if(!is_array($oldflags)) $oldflags = array($oldflags);
  27. }
  28. if(!is_array($flags))
  29. {
  30. if($flags !== 0) $flags = array($flags);
  31. }
  32. if(is_array($flags))
  33. {
  34. if(is_array($oldflags)) $new_array = array_merge($flags, $oldflags);
  35. else $new_array = $flags;
  36. }
  37. elseif (is_array($oldflags)) $new_array = $oldflags;
  38. if(count($new_array))
  39. {
  40. $_SESSION['edit_queue'] = $new_array;
  41. return count($new_array);
  42. }
  43. else
  44. {
  45. unset($_SESSION['edit_queue']);
  46. return 0;
  47. }
  48. }
  49. function show_edit_flagged($flag=0)
  50. {
  51. if(empty($flag)||$flag === 0)
  52. {
  53. $flag = array_pop($_SESSION['edit_queue']);
  54. }
  55. $flaginfo = get_flag($flag);
  56. if($flaginfo['type'] === 'badid3')
  57. {
  58. show_edit_badid3($flaginfo['song'],$flag);
  59. }
  60. else
  61. {
  62. echo "I don't know how to edit already edited songs yet: $flag.<br />";
  63. }
  64. }
  65. function show_edit_badid3($songid,$flagid)
  66. {
  67. $song = get_song_info($songid);
  68. require(conf('prefix')."/templates/song_edit.inc");
  69. }
  70. function get_flag($id)
  71. {
  72. if(!is_array($id)) $id = array($id);
  73. $results = array();
  74. $newid = array_pop($id);
  75. $sql = "SELECT flagged.id,user.username,type,song,date,comment" .
  76. " FROM flagged,user WHERE flagged.user = user.username AND (flagged.song = '$newid'";
  77. foreach($id as $num)
  78. {
  79. $sql .= " OR flagged.song = '$num'";
  80. }
  81. $sql .= ")";
  82. $result = mysql_query($sql, dbh());
  83. while ($row = mysql_fetch_array($result))
  84. {
  85. $results[] = $row;
  86. }
  87. if(count($results) == 1) return $results[0];
  88. else return $results;
  89. }
  90. function get_flagged_songs($user = 0)
  91. {
  92. $sql = "SELECT flagged.id,user.username,type,song,date,comment" .
  93. " FROM flagged,user WHERE flagged.user = user.username AND flagged.type <> 'notify' AND flagged.type <> 'done'";
  94. // If the user is not an admin, they can only see songs they've flagged
  95. if($user)
  96. {
  97. if($_SESSION['userdata']['access'] === 'admin')
  98. {
  99. $sql .= " AND user.username = '$user'";
  100. }
  101. else
  102. {
  103. $sql .= " AND user.username = '".$_SESSION['userdata']['username']."'";
  104. }
  105. }
  106. $sql .= " ORDER BY date";
  107. $result = mysql_query($sql, dbh());
  108. $arr = array();
  109. while ($flag = mysql_fetch_array($result))
  110. {
  111. $arr[] = $flag;
  112. }
  113. return $arr;
  114. }
  115. function accept_new_tags($flags)
  116. {
  117. if(!is_array($flags)) $flags = array($flags);
  118. foreach($flags as $flag)
  119. {
  120. copy_updated_tag($flag);
  121. }
  122. set_flag_value($flags, 'setid3');
  123. }
  124. function reject_new_tags($flags)
  125. {
  126. if(!is_array($flags)) $flags = array($flags);
  127. $oldflags = $flags;
  128. $flag = array_pop($flags);
  129. $sql = "DELETE FROM flagged_songs WHERE song = '$flag'";
  130. foreach($flags as $flag)
  131. {
  132. $sql .= " OR song = '$flag'";
  133. }
  134. $result = mysql_query($sql, dbh());
  135. $user = $_SESSION['userdata']['username'];
  136. set_flag_value($oldflags, 'notify', "Tag changes rejected by $user");
  137. }
  138. function set_flag_value($flags, $val, $comment = '')
  139. {
  140. if(!is_array($flags)) $flags = array($flags);
  141. $user = $_SESSION['userdata']['id'];
  142. /* $flagid = array_pop($flags);*/
  143. $dbh = dbh();
  144. foreach($flags as $flagid)
  145. {
  146. $sql = "REPLACE INTO flagged (type,song,comment,user,date)".
  147. " VALUES ('$val','$flagid','$comment','$user','".time()."')";
  148. $result = mysql_query($sql, $dbh);
  149. }
  150. return $result;
  151. }
  152. function copy_updated_tag($flag)
  153. {
  154. $flagdata = get_flag($flag);
  155. $sql = "SELECT * FROM flagged_song WHERE song = '".$flagdata['song']."'";
  156. $result = mysql_query($sql, dbh());
  157. $newtag = mysql_fetch_array($result);
  158. if($newtag['new_artist'])
  159. {
  160. $newtag['artist'] = insert_artist($newtag['new_artist']);
  161. }
  162. if($newtag['new_album'])
  163. {
  164. $newtag['album'] = insert_album($newtag['new_album']);
  165. }
  166. $sql = "UPDATE song SET ".
  167. "title = '".$newtag['title']."',".
  168. "artist = '".$newtag['artist']."',".
  169. "album = '".$newtag['album']."',".
  170. "track = '".$newtag['track']."',".
  171. "genre = '".$newtag['genre']."',".
  172. "year = '".$newtag['year']."' ".
  173. "WHERE song.id = '".$newtag['song']."'";
  174. $result = mysql_query($sql, dbh());
  175. if($result)
  176. {
  177. $sql2 = "DELETE FROM flagged_song WHERE song='".$flagdata['song']."'";
  178. $result2 = mysql_query($sql2, dbh());
  179. }
  180. return ($result && $result2);
  181. }
  182. function update_flags($songs)
  183. {
  184. $accepted = array();
  185. $rejected = array();
  186. $newflags = array();
  187. foreach($songs as $song)
  188. {
  189. $accept = scrub_in($_REQUEST[$song.'_accept']);
  190. if($accept === 'accept') $accepted[] = $song;
  191. elseif ($accept === 'reject') $rejected[] = $song;
  192. else
  193. {
  194. $newflag = scrub_in($_REQUEST[$song.'_newflag']);
  195. $newflags[$song] = $newflag;
  196. }
  197. }
  198. if(count($accepted))
  199. {
  200. accept_new_tags($accepted);
  201. }
  202. if(count($rejected))
  203. {
  204. reject_new_tags($rejected);
  205. }
  206. if(count($newflags))
  207. {
  208. foreach($newflags as $flag=>$type)
  209. {
  210. set_flag_value($flag, $type);
  211. }
  212. }
  213. }
  214. function update_song_info($song)
  215. {
  216. $user = $_SESSION['userdata'];
  217. $title = scrub_in($_REQUEST['title']);
  218. $track = scrub_in($_REQUEST['track']);
  219. $genre = scrub_in($_REQUEST['genre']);
  220. $year = scrub_in($_REQUEST['year']);
  221. if(isset($_REQUEST['update_id3']))
  222. $update_id3 = 1;
  223. if(isset($_REQUEST['new_artist']) && $_REQUEST['new_artist'] !== '')
  224. {
  225. $create_artist = 1;
  226. $artist = scrub_in($_REQUEST['new_artist']);
  227. }
  228. else
  229. $artist = scrub_in($_REQUEST['artist']);
  230. if(isset($_REQUEST['new_album']) && $_REQUEST['new_album'] !== '')
  231. {
  232. $create_album = 1;
  233. $album = scrub_in($_REQUEST['new_album']);
  234. }
  235. else
  236. $album = scrub_in($_REQUEST['album']);
  237. if(is_array($_REQUEST['genre'])) {
  238. $genre = $genre[0];
  239. }
  240. if($user['access'] == 'admin')
  241. // Update the file directly
  242. {
  243. if($create_artist)
  244. {
  245. $artist = insert_artist($artist);
  246. }
  247. if($create_album)
  248. {
  249. $album = insert_album($album);
  250. }
  251. // Escape data (prevent " or ' snafu's)
  252. $title = sql_escape($title);
  253. $artist = sql_escape($artist);
  254. $album = sql_escape($album);
  255. $genre = sql_escape($genre);
  256. $year = sql_escape($year);
  257. $sql = "UPDATE song SET" .
  258. " title = '$title'," .
  259. " track = '$track'," .
  260. " genre = '$genre'," .
  261. " year = '$year'," .
  262. " artist = '$artist',".
  263. " album = '$album'," .
  264. " update_time = '".time()."'" .
  265. " WHERE id = '$song' LIMIT 1";
  266. $result = mysql_query($sql, dbh() );
  267. if($result && $update_id3 )
  268. {
  269. //Add to flagged table so we can fix the id3 tags
  270. $date = time();
  271. $sql = "REPLACE INTO flagged SET " .
  272. " type = 'setid3', song = '$song', date = '$date', user = '".$user['id']."'";
  273. $result = mysql_query($sql, dbh());
  274. }
  275. }
  276. else
  277. // Stick in the flagged_songs table to be updated by an admin
  278. {
  279. if($create_artist) $artist_field = 'new_artist';
  280. else $artist_field = 'artist';
  281. if($create_album) $album_field = 'new_album';
  282. else $album_field = 'album';
  283. $sql = "INSERT INTO flagged_song(song,title,track,genre,year,$artist_field,$album_field,update_time) " .
  284. "VALUES ('$song','$title','$track','$genre','$year','$artist','$album','".time()."')";
  285. $result = mysql_query($sql, dbh() );
  286. if($result && $update_id3 )
  287. {
  288. //Add to flagged table so we can fix the id3 tags
  289. $date = time();
  290. $sql = "REPLACE INTO flagged SET " .
  291. " type = 'newid3', song = '$song', date = '$date', user = '".$user['id']."'";
  292. $result = mysql_query($sql, dbh());
  293. }
  294. echo "Thanks for helping to keep the catalog up to date. Someone will review your changes, and you will be notified on the main page when they're approved.";
  295. }
  296. }