PageRenderTime 120ms CodeModel.GetById 19ms app.highlight 75ms RepoModel.GetById 16ms app.codeStats 1ms

/indra/newview/llurlwhitelist.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 221 lines | 125 code | 38 blank | 58 comment | 15 complexity | 8fd8cc807a2bef2a3f258971ca69173d MD5 | raw file
  1/** 
  2 * @file llurlwhitelist.cpp
  3 * @author Callum Prentice
  4 * @brief maintains a "white list" of acceptable URLS that are stored on disk
  5 *
  6 * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#include "llviewerprecompiledheaders.h"
 29#include "llurlwhitelist.h"
 30
 31#include <iostream>
 32#include <fstream>
 33
 34LLUrlWhiteList* LLUrlWhiteList::sInstance = 0;
 35
 36///////////////////////////////////////////////////////////////////////////////
 37//
 38LLUrlWhiteList::LLUrlWhiteList () :
 39	mLoaded ( false ),
 40	mFilename ( "url_whitelist.ini" ),
 41	mUrlList ( 0 ),
 42	mCurIndex ( 0 )
 43{
 44}
 45
 46///////////////////////////////////////////////////////////////////////////////
 47//
 48LLUrlWhiteList::~LLUrlWhiteList ()
 49{
 50}
 51
 52///////////////////////////////////////////////////////////////////////////////
 53
 54//static
 55void LLUrlWhiteList::initClass ()
 56{
 57    if ( ! sInstance )
 58	{
 59        sInstance = new LLUrlWhiteList ();
 60	}
 61}
 62
 63//static
 64void LLUrlWhiteList::cleanupClass ()
 65{
 66	delete sInstance;
 67	sInstance = NULL;
 68}
 69
 70LLUrlWhiteList* LLUrlWhiteList::getInstance ()
 71{
 72	return sInstance;
 73}
 74
 75///////////////////////////////////////////////////////////////////////////////
 76//
 77bool LLUrlWhiteList::load ()
 78{
 79	// don't load if we're already loaded
 80	if ( mLoaded )
 81		return ( true );
 82
 83	// remove current entries before we load over them
 84	clear ();
 85
 86	// build filename for each user
 87	std::string resolvedFilename = gDirUtilp->getExpandedFilename ( LL_PATH_PER_SL_ACCOUNT, mFilename );
 88
 89	// open a file for reading
 90	llifstream file ( resolvedFilename );
 91	if ( file.is_open () )
 92	{
 93		// add each line in the file to the list
 94		std::string line;
 95		while ( std::getline ( file, line ) )
 96		{
 97			addItem ( line, false );
 98		};
 99
100		file.close ();
101
102		// flag as loaded
103		mLoaded = true;
104
105		return true;
106	};
107
108	return false;
109}
110
111///////////////////////////////////////////////////////////////////////////////
112//
113bool LLUrlWhiteList::save ()
114{
115	// build filename for each user
116	std::string resolvedFilename = gDirUtilp->getExpandedFilename ( LL_PATH_PER_SL_ACCOUNT, mFilename );
117
118	if (resolvedFilename.empty())
119	{
120		llinfos << "No per-user dir for saving URL whitelist - presumably not logged in yet.  Skipping." << llendl;
121		return false;
122	}
123
124	// open a file for writing
125	llofstream file ( resolvedFilename );
126	if ( file.is_open () )
127	{
128		// for each entry we have
129		for ( string_list_t::iterator iter = mUrlList.begin (); iter != mUrlList.end (); ++iter )
130		{
131			file << ( *iter ) << std::endl;
132		}
133
134		file.close ();
135
136		return true;
137	};
138
139	return false;
140}
141
142///////////////////////////////////////////////////////////////////////////////
143//
144bool LLUrlWhiteList::clear ()
145{
146	mUrlList.clear ();
147
148	mCurIndex = 0;
149
150	return true;
151}
152
153std::string url_cleanup(std::string pattern)
154{
155	LLStringUtil::trim(pattern);
156	S32 length = pattern.length();
157	S32 position = 0;
158	std::string::reverse_iterator it = pattern.rbegin();
159	++it;	// skip last char, might be '/'
160	++position;
161	for (; it < pattern.rend(); ++it)
162	{
163		char c = *it;
164		if (c == '/')
165		{
166			// found second to last '/'
167			S32 desired_length = length - position;
168			LLStringUtil::truncate(pattern, desired_length);
169			break;
170		}
171		++position;
172	}
173	return pattern;
174}
175
176///////////////////////////////////////////////////////////////////////////////
177//
178bool LLUrlWhiteList::addItem ( const std::string& itemIn, bool saveAfterAdd )
179{
180	std::string item = url_cleanup(itemIn);
181	
182	mUrlList.push_back ( item );
183
184	// use this when all you want to do is call addItem ( ... ) where necessary
185	if ( saveAfterAdd )
186		save ();
187
188	return true;
189}
190
191///////////////////////////////////////////////////////////////////////////////
192//
193bool LLUrlWhiteList::getFirst ( std::string& valueOut )
194{
195	if ( mUrlList.size () == 0 )
196		return false;
197
198	mCurIndex = 0;
199	valueOut = mUrlList[mCurIndex++];
200
201	return true;	
202}
203
204///////////////////////////////////////////////////////////////////////////////
205//
206bool LLUrlWhiteList::getNext ( std::string& valueOut )
207{
208	if ( mCurIndex >= mUrlList.size () )
209		return false;
210
211	valueOut = mUrlList[mCurIndex++];
212
213	return true;
214}
215
216///////////////////////////////////////////////////////////////////////////////
217//
218bool LLUrlWhiteList::containsMatch ( const std::string& patternIn )
219{
220	return false;
221}