Ejemplo n.º 1
0
 public static function request(Station $station, $track_id)
 {
     if (DF_APPLICATION_ENV !== 'production') {
         return false;
     }
     $db = self::getDatabase();
     $em = self::getEntityManager();
     $settings = self::getSettings();
     // Forbid web crawlers from using this feature.
     if (\PVL\Utilities::isCrawler()) {
         throw new \DF\Exception('Search engine crawlers are not permitted to use this feature.');
     }
     // Verify that the station supports CentovaCast requests.
     $station_id = self::getStationID($station);
     if (!$station_id) {
         throw new \DF\Exception('This radio station is not capable of handling requests at this time.');
     }
     // Verify that Track ID exists with station.
     $media_item = StationMedia::getRepository()->findOneBy(array('id' => $track_id, 'station_id' => $station->id));
     if (!$media_item instanceof StationMedia) {
         throw new \DF\Exception('The song ID you specified could not be found in the station playlist.');
     }
     // Check the most recent song history.
     try {
         $last_play_time = $em->createQuery('SELECT sh.timestamp FROM Entity\\SongHistory sh WHERE sh.song_id = :song_id AND sh.station_id = :station_id ORDER BY sh.timestamp DESC')->setParameter('song_id', $media_item->song_id)->setParameter('station_id', $station->id)->setMaxResults(1)->getSingleScalarResult();
     } catch (\Exception $e) {
         $last_play_time = 0;
     }
     if ($last_play_time && $last_play_time > time() - 60 * 30) {
         throw new \DF\Exception('This song has been played too recently on the station.');
     }
     // Get or create a "requests" playlist for the station.
     $request_playlist_raw = $db->fetchAssoc('SELECT p.id FROM playlists AS p WHERE p.type = ? AND p.accountid = ?', array('request', $station_id));
     if ($request_playlist_raw) {
         $playlist_id = $request_playlist_raw['id'];
     } else {
         $new_playlist = array('title' => 'Automated Song Requests', 'type' => 'request', 'scheduled_repeat' => 'never', 'scheduled_monthdays' => 'date', 'interval_type' => 'songs', 'interval_length' => '0', 'general_weight' => '0', 'status' => 'disabled', 'general_order' => 'random', 'interval_style' => 'playall', 'stateid' => '0', 'accountid' => $station_id, 'scheduled_interruptible' => '0', 'scheduled_duration' => '0', 'scheduled_style' => 'sequential', 'general_starttime' => '00:00:00', 'general_endtime' => '00:00:00', 'track_interruptible' => '0');
         $db->insert('playlists', $new_playlist);
         $playlist_id = $db->lastInsertId('playlists');
     }
     // Check for an existing request from this user.
     $user_ip = $_SERVER['REMOTE_ADDR'];
     $existing_request = $db->fetchAll('SELECT ptr.* FROM playlist_tracks_requests AS ptr WHERE ptr.playlistid = ? AND ptr.senderip = ?', array($playlist_id, $user_ip));
     if (count($existing_request) > 0) {
         throw new \DF\Exception('You already have a pending request with this station! Please try again later.');
     }
     // Check for any request (on any station) within 5 minutes.
     $recent_threshold = time() - 60 * 5;
     $recent_requests = $em->createQuery('SELECT sr FROM Entity\\StationRequest sr WHERE sr.ip = :user_ip AND sr.timestamp >= :threshold')->setParameter('user_ip', $user_ip)->setParameter('threshold', $recent_threshold)->getArrayResult();
     if (count($recent_requests) > 0) {
         throw new \DF\Exception('You have submitted a request too recently! Please wait a while before submitting another one.');
     }
     // Enable the "Automated Song Requests" playlist.
     $db->update('playlists', array('status' => 'enabled'), array('id' => $playlist_id));
     $requesttime = new \DateTime('NOW');
     $requesttime->setTimezone(new \DateTimeZone($settings['timezone']));
     // Create a new request if all other checks pass.
     $new_request = array('playlistid' => $playlist_id, 'trackid' => $track_id, 'requesttime' => $requesttime->format('Y-m-d h:i:s'), 'sendername' => 'Ponyville Live!', 'senderemail' => '*****@*****.**', 'dedication' => '', 'senderip' => $user_ip);
     $db->insert('playlist_tracks_requests', $new_request);
     $request_id = $db->lastInsertId('playlist_tracks_requests');
     $media_item->logRequest();
     $media_item->save();
     return $request_id;
 }