def prepare(self): if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"): log.info("api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip)) raise APIException("rejected", text="You are not coming from a trusted address.") if self.allow_cors: self.set_header("Access-Control-Allow-Origin", "*") self.set_header("Access-Control-Max-Age", "600") self.set_header("Access-Control-Allow-Credentials", "false") if not isinstance(self.locale, locale.RainwaveLocale): self.locale = self.get_browser_locale() self.setup_output() if 'in_order' in self.request.arguments: self._output = [] self._output_array = True else: self._output = {} if not self.sid: self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None)) hostname = self.request.headers.get('Host', None) if hostname: hostname = unicode(hostname).split(":")[0] if hostname in config.station_hostnames: self.sid = config.station_hostnames[hostname] sid_arg = fieldtypes.integer(self.get_argument("sid", None)) if sid_arg is not None: self.sid = sid_arg if self.sid is None and self.sid_required: raise APIException("missing_station_id", http_code=400) self.arg_parse() self.sid_check() if self.sid: self.set_cookie("r4_sid", str(self.sid), expires_days=365) if self.phpbb_auth: self.do_phpbb_auth() else: self.rainwave_auth() if not self.user and self.auth_required: raise APIException("auth_required", http_code=403) elif not self.user and not self.auth_required: self.user = User(1) self.user.ip_address = self.request.remote_ip self.user.refresh(self.sid) if self.user and config.get("store_prefs"): self.user.save_preferences(self.request.remote_ip, self.get_cookie("r4_prefs", None)) self.permission_checks()
def prepare(self): if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"): log.info("api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip)) raise APIException("rejected", text="You are not coming from a trusted address.") if self.allow_cors: self.set_header("Access-Control-Allow-Origin", "*") self.set_header("Access-Control-Max-Age", "600") self.set_header("Access-Control-Allow-Credentials", "false") if not isinstance(self.locale, locale.RainwaveLocale): self.locale = self.get_browser_locale() self.setup_output() if 'in_order' in self.request.arguments: self._output = [] self._output_array = True else: self._output = {} self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None)) hostname = self.request.headers.get('Host', None) if hostname: hostname = unicode(hostname).split(":")[0] if hostname in config.station_hostnames: self.sid = config.station_hostnames[hostname] sid_arg = fieldtypes.integer(self.get_argument("sid", None)) if sid_arg is not None: self.sid = sid_arg if self.sid is None and self.sid_required: raise APIException("missing_station_id", http_code=400) self.arg_parse() self.sid_check() if self.sid: self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain")) if self.phpbb_auth: self.do_phpbb_auth() else: self.rainwave_auth() if not self.user and self.auth_required: raise APIException("auth_required", http_code=403) elif not self.user and not self.auth_required: self.user = User(1) self.user.ip_address = self.request.remote_ip self.user.refresh(self.sid) if self.user and config.get("store_prefs"): self.user.save_preferences(self.request.remote_ip, self.get_cookie("r4_prefs", None)) self.permission_checks()
def on_message(self, message_text): try: message = WSMessage() message.update(json.loads(message_text)) except: self.write_message({ "wserror": { "tl_key": "invalid_json", "text": self.locale.translate("invalid_json") } }) return if not "action" in message or not message['action']: self.write_message({ "wserror": { "tl_key": "missing_argument", "text": self.locale.translate("missing_argument", argument="action") } }) return if not self.authorized and message['action'] != "auth": self.write_message({ "wserror": { "tl_key": "auth_required", "text": self.locale.translate("auth_required") } }) return if not self.authorized and message['action'] == "auth": self._do_auth(message) return if message['action'] == "vote": if not message['entry_id']: self.write_message({ "wserror": { "tl_key": "missing_argument", "text": self.locale.translate("missing_argument", argument="entry_id") } }) elif not fieldtypes.integer(message['entry_id']): self.write_message({ "wserror": { "tl_key": "invalid_argument", "text": self.locale.translate("missing_argument", argument="entry_id", reason=fieldtypes.integer_error) } }) message['elec_id'] = rainwave.schedule.get_elec_id_for_entry(self.sid, message['entry_id']) self._process_message(message)
def do_phpbb_auth(self): phpbb_cookie_name = config.get("phpbb_cookie_name") + "_" user_id = fieldtypes.integer( self.get_cookie(phpbb_cookie_name + "u", "")) if not user_id: pass else: if self._verify_phpbb_session(user_id): # update_phpbb_session is done by verify_phpbb_session if successful self.user = User(user_id) self.user.ip_address = self.request.remote_ip self.user.authorize(self.sid, None, bypass=True) return True if not self.user and self.get_cookie(phpbb_cookie_name + "k"): can_login = db.c.fetch_var( "SELECT 1 FROM phpbb_sessions_keys WHERE key_id = %s AND user_id = %s", (hashlib.md5(self.get_cookie(phpbb_cookie_name + "k")).hexdigest(), user_id)) if can_login == 1: self._update_phpbb_session( self._get_phpbb_session(user_id)) self.user = User(user_id) self.user.ip_address = self.request.remote_ip self.user.authorize(self.sid, None, bypass=True) return True return False
def prepare(self): # TODO: Language self.info = [] self.sid = fieldtypes.integer(self.get_cookie("r4sid", "1")) if not self.sid: self.sid = 1 if self.request.host == "game.rainwave.cc": self.sid = 1 elif self.request.host == "ocr.rainwave.cc": self.sid = 2 elif self.request.host == "covers.rainwave.cc" or self.request.host == "cover.rainwave.cc": self.sid = 3 elif self.request.host == "chiptune.rainwave.cc": self.sid = 4 elif self.request.host == "all.rainwave.cc": self.sid = 5 self.set_cookie("r4sid", str(self.sid), expires_days=365, domain=".rainwave.cc") self.user = None if not fieldtypes.integer(self.get_cookie("phpbb3_38ie8_u", "")): self.user = User(1) else: user_id = int(self.get_cookie("phpbb3_38ie8_u")) if self.get_cookie("phpbb3_38ie8_sid"): session_id = db.c_old.fetch_var("SELECT session_id FROM phpbb_sessions WHERE session_id = %s AND session_user_id = %s", (self.get_cookie("phpbb3_38ie8_sid"), user_id)) if session_id: db.c_old.update("UPDATE phpbb_sessions SET session_last_visit = %s, session_page = %s WHERE session_id = %s", (time.time(), "rainwave", session_id)) self.user = User(user_id) self.user.authorize(self.sid, None, None, True) if not self.user and self.get_cookie("phpbb3_38ie8_k"): can_login = db.c_old.fetch_var("SELECT 1 FROM phpbb_sessions_keys WHERE key_id = %s AND user_id = %s", (hashlib.md5(self.get_cookie("phpbb3_38ie8_k")).hexdigest(), user_id)) if can_login == 1: self.user = User(user_id) self.user.authorize(self.sid, None, None, True) if not self.user: self.user = User(1) self.user.ensure_api_key(self.request.remote_ip) self.user.data['sid'] = self.sid
def do_phpbb_auth(self): phpbb_cookie_name = config.get("phpbb_cookie_name") user_id = fieldtypes.integer(self.get_cookie(phpbb_cookie_name + "u", "")) if not user_id: pass else: if self._verify_phpbb_session(user_id): # update_phpbb_session is done by verify_phpbb_session if successful self.user = User(user_id) self.user.authorize(self.sid, None, None, True) return True if not self.user and self.get_cookie(phpbb_cookie_name + "k"): can_login = db.c.fetch_var("SELECT 1 FROM phpbb_sessions_keys WHERE key_id = %s AND user_id = %s", (hashlib.md5(self.get_cookie(phpbb_cookie_name + "k")).hexdigest(), user_id)) if can_login == 1: self._update_phpbb_session(self._get_phpbb_session(user_id)) self.user = User(user_id) self.user.authorize(self.sid, None, None, True) return True return False
def do_phpbb_auth(self): phpbb_cookie_name = config.get("phpbb_cookie_name") self.user = None if not fieldtypes.integer(self.get_cookie(phpbb_cookie_name + "u", "")): self.user = User(1) else: user_id = int(self.get_cookie(phpbb_cookie_name + "u")) if self._verify_phpbb_session(user_id): # update_phpbb_session is done by verify_phpbb_session if successful self.user = User(user_id) self.user.authorize(self.sid, None, None, True) return True if not self.user and self.get_cookie(phpbb_cookie_name + "k"): can_login = db.c_old.fetch_var("SELECT 1 FROM phpbb_sessions_keys WHERE key_id = %s AND user_id = %s", (hashlib.md5(self.get_cookie(phpbb_cookie_name + "k")).hexdigest(), user_id)) if can_login == 1: self._update_phpbb_session(self._get_phpbb_session(user_id)) self.user = User(user_id) self.user.authorize(self.sid, None, None, True) return True return False
def load_tag_from_file(self, filename): """ Reads ID3 tags and sets object-level variables. """ # log.debug("playlist", u"reading tag info from {}".format(filename)) f = MP3(filename) self.filename = filename if not f.tags: raise PassableScanError("Song filename \"%s\" has no tags." % filename) w = f.tags.getall('TIT2') if len(w) > 0 and len(unicode(w[0])) > 0: self.data['title'] = unicode(w[0]).strip() else: raise PassableScanError("Song filename \"%s\" has no title tag." % filename) w = f.tags.getall('TPE1') if len(w) > 0 and len(unicode(w[0])) > 0: self.artist_tag = unicode(w[0]) else: raise PassableScanError("Song filename \"%s\" has no artist tag." % filename) w = f.tags.getall('TALB') if len(w) > 0 and len(unicode(w[0]).strip()) > 0: self.album_tag = unicode(w[0]).strip() else: raise PassableScanError("Song filename \"%s\" has no album tag." % filename) w = f.tags.getall('TRCK') if w is not None and len(w) > 0: try: self.data['track_number'] = fieldtypes.integer(unicode(w[0])) except ValueError: pass w = f.tags.getall('TPOS') if w is not None and len(w) > 0: try: self.data['disc_number'] = fieldtypes.integer(unicode(w[0])) except ValueError: pass w = f.tags.getall('TCON') if len(w) > 0 and len(unicode(w[0])) > 0: self.genre_tag = unicode(w[0]) w = f.tags.getall('COMM') if len(w) > 0 and len(unicode(w[0])) > 0: self.data['link_text'] = unicode(w[0]).strip() # what the heck is pylint code w0511? a warning with X X X # even happens on comments. # guess it hates p**n but that's what the ID3 wildcard tag is! w = f.tags.getall('WXXX') #pylint: disable=W0511 if len(w) > 0 and len(unicode(w[0])) > 0: self.data['url'] = unicode(w[0]).strip() else: self.data['url'] = None w = f.tags.getall('TYER') if w is not None and len(w) > 0: try: self.data['year'] = fieldtypes.integer(unicode(w[0])) except ValueError: pass w = f.tags.getall('TDRC') if self.data['year'] is None and w is not None and len(w) > 0: try: self.data['year'] = fieldtypes.integer(unicode(w[0])) except ValueError: pass self.replay_gain = self._get_replaygain(f) if not self.replay_gain and config.get("mp3gain_scan"): # Run mp3gain quietly, finding peak while not clipping, output DB friendly, and preserving original timestamp # gain_std is unused, we'll grab the output from mp3gain from the tags themselves #pylint: disable=W0612 gain_std, gain_error = subprocess.Popen([_mp3gain_path, "-o", "-q", "-s", "i", "-p", "-k", "-T", self.filename ], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() #pylint: enable=W0612 if len(gain_error) > 0: raise Exception("Error when replay gaining \"%s\": %s" % (filename, gain_error)) f = MP3(filename) self.replay_gain = self._get_replaygain(f) self.data['length'] = int(f.info.length)
def prepare(self): if self.local_only and not self.request.remote_ip in config.get( "api_trusted_ip_addresses"): log.info( "api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip)) raise APIException( "rejected", text="You are not coming from a trusted address.") if not isinstance(self.locale, locale.RainwaveLocale): self.locale = self.get_browser_locale() if not self.return_name: self.return_name = self.url[self.url.rfind("/") + 1:] + "_result" else: self.return_name = self.return_name if self.admin_required or self.dj_required: self.login_required = True if 'in_order' in self.request.arguments: self._output = [] self._output_array = True else: self._output = {} self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None)) hostname = self.request.headers.get('Host', None) if hostname: hostname = unicode(hostname).split(":")[0] if hostname in config.station_hostnames: self.sid = config.station_hostnames[hostname] self.sid = fieldtypes.integer(self.get_argument("sid", None)) or self.sid if self.sid and not self.sid in config.station_ids: self.sid = None if not self.sid and self.sid_required: raise APIException("missing_station_id", http_code=400) for field, field_attribs in self.__class__.fields.iteritems(): type_cast, required = field_attribs if required and field not in self.request.arguments: raise APIException("missing_argument", argument=field, http_code=400) elif not required and field not in self.request.arguments: self.cleaned_args[field] = None else: parsed = type_cast(self.get_argument(field), self) if parsed == None and required != None: raise APIException( "invalid_argument", argument=field, reason="%s %s" % (field, getattr(fieldtypes, "%s_error" % type_cast.__name__)), http_code=400) else: self.cleaned_args[field] = parsed if not self.sid and not self.sid_required: self.sid = 5 if not self.sid in config.station_ids: raise APIException("invalid_station_id", http_code=400) self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain")) if self.phpbb_auth: self.do_phpbb_auth() else: self.rainwave_auth() if not self.user and self.auth_required: raise APIException("auth_required", http_code=403) elif not self.user and not self.auth_required: self.user = User(1) self.user.ip_address = self.request.remote_ip self.user.refresh(self.sid) if self.login_required and (not self.user or self.user.is_anonymous()): raise APIException("login_required", http_code=403) if self.tunein_required and (not self.user or not self.user.is_tunedin()): raise APIException("tunein_required", http_code=403) if self.admin_required and (not self.user or not self.user.is_admin()): raise APIException("admin_required", http_code=403) if self.dj_required and (not self.user or not self.user.is_dj()): raise APIException("dj_required", http_code=403) if self.perks_required and (not self.user or not self.user.has_perks()): raise APIException("perks_required", http_code=403) if self.unlocked_listener_only and not self.user: raise APIException("auth_required", http_code=403) elif self.unlocked_listener_only and self.user.data[ 'lock'] and self.user.data['lock_sid'] != self.sid: raise APIException( "unlocked_only", station=config.station_id_friendly[self.user.data['lock_sid']], lock_counter=self.user.data['lock_counter'], http_code=403)
def load_tag_from_file(self, filename): """ Reads ID3 tags and sets object-level variables. """ # log.debug("playlist", u"reading tag info from {}".format(filename)) f = MP3(filename, translate=False) self.filename = filename if not f.tags: raise PassableScanError("Song filename \"%s\" has no tags." % filename) w = f.tags.getall('TIT2') if len(w) > 0 and len(unicode(w[0])) > 0: self.data['title'] = unicode(w[0]).strip() else: raise PassableScanError("Song filename \"%s\" has no title tag." % filename) w = f.tags.getall('TPE1') if len(w) > 0 and len(unicode(w[0])) > 0: self.artist_tag = unicode(w[0]) else: raise PassableScanError("Song filename \"%s\" has no artist tag." % filename) w = f.tags.getall('TALB') if len(w) > 0 and len(unicode(w[0]).strip()) > 0: self.album_tag = unicode(w[0]).strip() else: raise PassableScanError("Song filename \"%s\" has no album tag." % filename) if config.get("scanner_use_tracknumbers"): w = f.tags.getall('TRCK') if w is not None and len(w) > 0: try: self.data['track_number'] = fieldtypes.integer( unicode(w[0])) except ValueError: pass w = f.tags.getall('TPOS') if w is not None and len(w) > 0: try: self.data['disc_number'] = fieldtypes.integer(unicode( w[0])) except ValueError: pass w = f.tags.getall('TCON') if len(w) > 0 and len(unicode(w[0])) > 0: self.genre_tag = unicode(w[0]) w = f.tags.getall('COMM') if len(w) > 0 and len(unicode(w[0])) > 0: self.data['link_text'] = unicode(w[0]).strip() # what the heck is pylint code w0511? a warning with X X X # even happens on comments. # guess it hates p**n but that's what the ID3 wildcard tag is! w = f.tags.getall('WXXX') #pylint: disable=W0511 if len(w) > 0 and len(unicode(w[0])) > 0: self.data['url'] = unicode(w[0]).strip() else: self.data['url'] = None w = f.tags.getall('TYER') if w is not None and len(w) > 0: try: self.data['year'] = fieldtypes.integer(unicode(w[0])) except ValueError: pass w = f.tags.getall('TDRC') if self.data['year'] is None and w is not None and len(w) > 0: try: self.data['year'] = fieldtypes.integer(unicode(w[0])) except ValueError: pass self.replay_gain = self._get_replaygain(f) if not self.replay_gain and config.get("mp3gain_scan"): # Run mp3gain quietly, finding peak while not clipping, output DB friendly, and preserving original timestamp # gain_std is unused, we'll grab the output from mp3gain from the tags themselves #pylint: disable=W0612 gain_std, gain_error = subprocess.Popen( [ _mp3gain_path, "-o", "-q", "-s", "i", "-p", "-k", "-T", self.filename ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=set_umask).communicate() #pylint: enable=W0612 if len(gain_error) > 0: raise Exception("Error when replay gaining \"%s\": %s" % (filename, gain_error)) f = MP3(filename, translate=False) self.replay_gain = self._get_replaygain(f) self.data['length'] = int(f.info.length)
def prepare(self): self._startclock = time.time() self.user = None if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"): log.info("api", "Rejected %s request from %s" % (self.url, self.request.remote_ip)) self.set_status(403) self.finish() if not isinstance(self.locale, locale.RainwaveLocale): self.locale = self.get_browser_locale() if not self.return_name: self.return_name = self.url[self.url.rfind("/")+1:] + "_result" else: self.return_name = self.return_name if self.admin_required or self.dj_required: self.login_required = True if 'in_order' in self.request.arguments: self._output = [] self._output_array = True else: self._output = {} self._output_array = False self.sid = fieldtypes.integer(self.get_cookie("r4_sid", "1")) if "sid" in self.request.arguments: self.sid = int(self.get_argument("sid")) elif not self.sid: for possible_sid in config.station_ids: if self.request.host == config.get_station(possible_sid, "host"): self.sid = possible_sid break if not self.sid and self.sid_required: raise APIException("missing_station_id", http_code=400) if self.sid and not self.sid in config.station_ids: raise APIException("invalid_station_id", http_code=400) self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain")) for field, field_attribs in self.__class__.fields.iteritems(): type_cast, required = field_attribs if required and field not in self.request.arguments: raise APIException("missing_argument", argument=field, http_code=400) elif not required and field not in self.request.arguments: self.cleaned_args[field] = None else: parsed = type_cast(self.get_argument(field), self) if parsed == None and required != None: raise APIException("invalid_argument", argument=field, reason=getattr(fieldtypes, "%s_error" % type_cast.__name__), http_code=400) else: self.cleaned_args[field] = parsed if self.phpbb_auth: self.do_phpbb_auth() else: self.rainwave_auth() if self.auth_required and not self.user: raise APIException("auth_required", http_code=403) if self.login_required and (not self.user or self.user.is_anonymous()): raise APIException("login_required", http_code=403) if self.tunein_required and (not self.user or not self.user.is_tunedin()): raise APIException("tunein_required", http_code=403) if self.admin_required and (not self.user or not self.user.is_admin()): raise APIException("admin_required", http_code=403) if self.dj_required and (not self.user or not self.user.is_dj()): raise APIException("dj_required", http_code=403) if self.perks_required and (not self.user or not self.user.has_perks()): raise APIException("perks_required", http_code=403) if self.unlocked_listener_only and not self.user: raise APIException("auth_required", http_code=403) elif self.unlocked_listener_only and self.user.data['listener_lock'] and self.user.data['listener_lock_sid'] != self.sid: raise APIException("unlocked_only", station=config.station_id_friendly[self.user.data['listener_lock_sid']], lock_counter=self.user.data['listener_lock_counter'], http_code=403)
def load_tag_from_file(self, filename): """ Reads ID3 tags and sets object-level variables. """ with open(filename, "rb") as mp3file: f = MP3(mp3file, translate=False) self.filename = filename if not f.tags: raise PassableScanError('Song filename "%s" has no tags.' % filename) w = f.tags.getall("TIT2") if len(w) > 0 and len(str(w[0])) > 0: self.data["title"] = str(w[0]).strip() else: raise PassableScanError( 'Song filename "%s" has no title tag.' % filename) w = f.tags.getall("TPE1") if len(w) > 0 and len(str(w[0])) > 0: self.artist_tag = str(w[0]) else: raise PassableScanError( 'Song filename "%s" has no artist tag.' % filename) w = f.tags.getall("TALB") if len(w) > 0 and len(str(w[0]).strip()) > 0: self.album_tag = str(w[0]).strip() else: raise PassableScanError( 'Song filename "%s" has no album tag.' % filename) if config.get("scanner_use_tracknumbers"): w = f.tags.getall("TRCK") if w is not None and len(w) > 0: try: self.data["track_number"] = fieldtypes.integer( str(w[0])) except ValueError: pass w = f.tags.getall("TPOS") if w is not None and len(w) > 0: try: self.data["disc_number"] = fieldtypes.integer(str( w[0])) except ValueError: pass w = f.tags.getall("TCON") if len(w) > 0 and len(str(w[0])) > 0: self.genre_tag = str(w[0]) w = f.tags.getall("COMM") if len(w) > 0 and len(str(w[0])) > 0: self.data["link_text"] = str(w[0]).strip() w = f.tags.getall("WXXX") if len(w) > 0 and len(str(w[0])) > 0: self.data["url"] = str(w[0]).strip() else: self.data["url"] = None w = f.tags.getall("TYER") if w is not None and len(w) > 0: try: self.data["year"] = fieldtypes.integer(str(w[0])) except ValueError: pass w = f.tags.getall("TDRC") if self.data["year"] is None and w is not None and len(w) > 0: try: self.data["year"] = fieldtypes.integer(str(w[0])) except ValueError: pass self.data["length"] = int(f.info.length)
def prepare(self): if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"): log.info("api", "Rejected %s request from %s" % (self.url, self.request.remote_ip)) self.set_status(403) self.finish() # Tornado doesn't want to read the JSON cookies at all; have to look into why and work around it. #if self.get_cookie("r3prefs"): # try: # self.cookie_prefs = tornado.escape.json_decode(self.get_cookie("r3prefs")) # except Exception as e: # print self.cookies # print self.get_cookie("r3prefs") # print e if not isinstance(self.locale, locale.RainwaveLocale): self.locale = self.get_browser_locale() if not self.return_name: self.return_name = self.url[self.url.rfind("/")+1:] + "_result" else: self.return_name = self.return_name if self.admin_required or self.dj_required: self.login_required = True if 'in_order' in self.request.arguments: self._output = [] self._output_array = True else: self._output = {} if not self.sid: self.sid = fieldtypes.integer(self.get_cookie("r4_sid", "5")) if "sid" in self.request.arguments: possible_sid = fieldtypes.integer(self.get_argument("sid")) if possible_sid in config.station_ids: self.sid = possible_sid if not self.sid in config.station_ids: self.sid = None if not self.sid: host = self.request.headers.get('Host', 'game.rainwave.cc') for possible_sid in config.station_ids: if host == config.get_station(possible_sid, "host"): self.sid = possible_sid break if not self.sid and self.sid_required: raise APIException("missing_station_id", http_code=400) elif not self.sid: self.sid = 5 if self.sid and not self.sid in config.station_ids: raise APIException("invalid_station_id", http_code=400) self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain")) for field, field_attribs in self.__class__.fields.iteritems(): type_cast, required = field_attribs if required and field not in self.request.arguments: raise APIException("missing_argument", argument=field, http_code=400) elif not required and field not in self.request.arguments: self.cleaned_args[field] = None else: parsed = type_cast(self.get_argument(field), self) if parsed == None and required != None: raise APIException("invalid_argument", argument=field, reason="%s %s" % (field, getattr(fieldtypes, "%s_error" % type_cast.__name__)), http_code=400) else: self.cleaned_args[field] = parsed if self.phpbb_auth: self.do_phpbb_auth() else: self.rainwave_auth() if self.auth_required and not self.user: raise APIException("auth_required", http_code=403) if self.login_required and (not self.user or self.user.is_anonymous()): raise APIException("login_required", http_code=403) if self.tunein_required and (not self.user or not self.user.is_tunedin()): raise APIException("tunein_required", http_code=403) if self.admin_required and (not self.user or not self.user.is_admin()): raise APIException("admin_required", http_code=403) if self.dj_required and (not self.user or not self.user.is_dj()): raise APIException("dj_required", http_code=403) if self.perks_required and (not self.user or not self.user.has_perks()): raise APIException("perks_required", http_code=403) if self.unlocked_listener_only and not self.user: raise APIException("auth_required", http_code=403) elif self.unlocked_listener_only and self.user.data['lock'] and self.user.data['lock_sid'] != self.sid: raise APIException("unlocked_only", station=config.station_id_friendly[self.user.data['lock_sid']], lock_counter=self.user.data['lock_counter'], http_code=403)
def prepare(self): if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"): log.info("api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip)) raise APIException("rejected", text="You are not coming from a trusted address.") if not isinstance(self.locale, locale.RainwaveLocale): self.locale = self.get_browser_locale() if not self.return_name: self.return_name = self.url[self.url.rfind("/")+1:] + "_result" else: self.return_name = self.return_name if self.admin_required: self.login_required = True if 'in_order' in self.request.arguments: self._output = [] self._output_array = True else: self._output = {} self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None)) hostname = self.request.headers.get('Host', None) if hostname: hostname = unicode(hostname).split(":")[0] if hostname in config.station_hostnames: self.sid = config.station_hostnames[hostname] sid_arg = fieldtypes.integer(self.get_argument("sid", None)) if sid_arg is not None: self.sid = sid_arg if self.sid is None and self.sid_required: raise APIException("missing_station_id", http_code=400) for field, field_attribs in self.__class__.fields.iteritems(): type_cast, required = field_attribs if required and field not in self.request.arguments: raise APIException("missing_argument", argument=field, http_code=400) elif not required and field not in self.request.arguments: self.cleaned_args[field] = None else: parsed = type_cast(self.get_argument(field), self) if parsed == None and required != None: raise APIException("invalid_argument", argument=field, reason="%s %s" % (field, getattr(fieldtypes, "%s_error" % type_cast.__name__)), http_code=400) else: self.cleaned_args[field] = parsed if self.sid is None and not self.sid_required: self.sid = config.get("default_station") if self.sid == 0 and self.allow_sid_zero: pass elif not self.sid in config.station_ids: raise APIException("invalid_station_id", http_code=400) if self.sid: self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain")) if self.phpbb_auth: self.do_phpbb_auth() else: self.rainwave_auth() if not self.user and self.auth_required: raise APIException("auth_required", http_code=403) elif not self.user and not self.auth_required: self.user = User(1) self.user.ip_address = self.request.remote_ip self.user.refresh(self.sid) if self.user and config.get("store_prefs"): self.user.save_preferences(self.request.remote_ip, self.get_cookie("r4_prefs", None)) if self.login_required and (not self.user or self.user.is_anonymous()): raise APIException("login_required", http_code=403) if self.tunein_required and (not self.user or not self.user.is_tunedin()): raise APIException("tunein_required", http_code=403) if self.admin_required and (not self.user or not self.user.is_admin()): raise APIException("admin_required", http_code=403) if self.perks_required and (not self.user or not self.user.has_perks()): raise APIException("perks_required", http_code=403) if self.unlocked_listener_only and not self.user: raise APIException("auth_required", http_code=403) elif self.unlocked_listener_only and self.user.data['lock'] and self.user.data['lock_sid'] != self.sid: raise APIException("unlocked_only", station=config.station_id_friendly[self.user.data['lock_sid']], lock_counter=self.user.data['lock_counter'], http_code=403) is_dj = False if self.dj_required and not self.user: raise APIException("dj_required", http_code=403) if self.dj_required and not self.user.is_admin(): #pylint: disable=E1103 potential_dj_ids = [] if cache.get_station(self.sid, "sched_current") and cache.get_station(self.sid, "sched_current").dj_user_id: potential_dj_ids.append(cache.get_station(self.sid, "sched_current").dj_user_id) if cache.get_station(self.sid, "sched_next"): for evt in cache.get_station(self.sid, "sched_next"): potential_dj_ids.append(evt.dj_user_id) if cache.get_station(self.sid, "sched_history") and cache.get_station(self.sid, "sched_history")[-1].dj_user_id: potential_dj_ids.append(cache.get_station(self.sid, "sched_history")[-1].dj_user_id) if not self.user.id in potential_dj_ids: raise APIException("dj_required", http_code=403) is_dj = True #pylint: enable=E1103 if self.dj_preparation and not is_dj and not self.user.is_admin(): if not db.c.fetch_var("SELECT COUNT(*) FROM r4_schedule WHERE sched_used = 0 AND sched_dj_user_id = %s", (self.user.id,)): raise APIException("dj_required", http_code=403)
def prepare(self): if self.local_only and not self.request.remote_ip in config.get("api_trusted_ip_addresses"): log.info("api", "Rejected %s request from %s, untrusted address." % (self.url, self.request.remote_ip)) raise APIException("rejected", text="You are not coming from a trusted address.") if not isinstance(self.locale, locale.RainwaveLocale): self.locale = self.get_browser_locale() if not self.return_name: self.return_name = self.url[self.url.rfind("/")+1:] + "_result" else: self.return_name = self.return_name if self.admin_required or self.dj_required: self.login_required = True if 'in_order' in self.request.arguments: self._output = [] self._output_array = True else: self._output = {} self.sid = fieldtypes.integer(self.get_cookie("r4_sid", None)) hostname = self.request.headers.get('Host', None) if hostname: hostname = unicode(hostname).split(":")[0] if hostname in config.station_hostnames: self.sid = config.station_hostnames[hostname] self.sid = fieldtypes.integer(self.get_argument("sid", None)) or self.sid if self.sid and not self.sid in config.station_ids: self.sid = None if not self.sid and self.sid_required: raise APIException("missing_station_id", http_code=400) for field, field_attribs in self.__class__.fields.iteritems(): type_cast, required = field_attribs if required and field not in self.request.arguments: raise APIException("missing_argument", argument=field, http_code=400) elif not required and field not in self.request.arguments: self.cleaned_args[field] = None else: parsed = type_cast(self.get_argument(field), self) if parsed == None and required != None: raise APIException("invalid_argument", argument=field, reason="%s %s" % (field, getattr(fieldtypes, "%s_error" % type_cast.__name__)), http_code=400) else: self.cleaned_args[field] = parsed if not self.sid and not self.sid_required: self.sid = 5 if not self.sid in config.station_ids: raise APIException("invalid_station_id", http_code=400) self.set_cookie("r4_sid", str(self.sid), expires_days=365, domain=config.get("cookie_domain")) if self.phpbb_auth: self.do_phpbb_auth() else: self.rainwave_auth() if not self.user and self.auth_required: raise APIException("auth_required", http_code=403) elif not self.user and not self.auth_required: self.user = User(1) self.user.ip_address = self.request.remote_ip self.user.refresh(self.sid) if self.login_required and (not self.user or self.user.is_anonymous()): raise APIException("login_required", http_code=403) if self.tunein_required and (not self.user or not self.user.is_tunedin()): raise APIException("tunein_required", http_code=403) if self.admin_required and (not self.user or not self.user.is_admin()): raise APIException("admin_required", http_code=403) if self.dj_required and (not self.user or not self.user.is_dj()): raise APIException("dj_required", http_code=403) if self.perks_required and (not self.user or not self.user.has_perks()): raise APIException("perks_required", http_code=403) if self.unlocked_listener_only and not self.user: raise APIException("auth_required", http_code=403) elif self.unlocked_listener_only and self.user.data['lock'] and self.user.data['lock_sid'] != self.sid: raise APIException("unlocked_only", station=config.station_id_friendly[self.user.data['lock_sid']], lock_counter=self.user.data['lock_counter'], http_code=403)