PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): LGPL-2.1
  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. #include "llviewerprecompiledheaders.h"
  28. #include "llurlwhitelist.h"
  29. #include <iostream>
  30. #include <fstream>
  31. LLUrlWhiteList* LLUrlWhiteList::sInstance = 0;
  32. ///////////////////////////////////////////////////////////////////////////////
  33. //
  34. LLUrlWhiteList::LLUrlWhiteList () :
  35. mLoaded ( false ),
  36. mFilename ( "url_whitelist.ini" ),
  37. mUrlList ( 0 ),
  38. mCurIndex ( 0 )
  39. {
  40. }
  41. ///////////////////////////////////////////////////////////////////////////////
  42. //
  43. LLUrlWhiteList::~LLUrlWhiteList ()
  44. {
  45. }
  46. ///////////////////////////////////////////////////////////////////////////////
  47. //static
  48. void LLUrlWhiteList::initClass ()
  49. {
  50. if ( ! sInstance )
  51. {
  52. sInstance = new LLUrlWhiteList ();
  53. }
  54. }
  55. //static
  56. void LLUrlWhiteList::cleanupClass ()
  57. {
  58. delete sInstance;
  59. sInstance = NULL;
  60. }
  61. LLUrlWhiteList* LLUrlWhiteList::getInstance ()
  62. {
  63. return sInstance;
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////
  66. //
  67. bool LLUrlWhiteList::load ()
  68. {
  69. // don't load if we're already loaded
  70. if ( mLoaded )
  71. return ( true );
  72. // remove current entries before we load over them
  73. clear ();
  74. // build filename for each user
  75. std::string resolvedFilename = gDirUtilp->getExpandedFilename ( LL_PATH_PER_SL_ACCOUNT, mFilename );
  76. // open a file for reading
  77. llifstream file ( resolvedFilename );
  78. if ( file.is_open () )
  79. {
  80. // add each line in the file to the list
  81. std::string line;
  82. while ( std::getline ( file, line ) )
  83. {
  84. addItem ( line, false );
  85. };
  86. file.close ();
  87. // flag as loaded
  88. mLoaded = true;
  89. return true;
  90. };
  91. return false;
  92. }
  93. ///////////////////////////////////////////////////////////////////////////////
  94. //
  95. bool LLUrlWhiteList::save ()
  96. {
  97. // build filename for each user
  98. std::string resolvedFilename = gDirUtilp->getExpandedFilename ( LL_PATH_PER_SL_ACCOUNT, mFilename );
  99. if (resolvedFilename.empty())
  100. {
  101. llinfos << "No per-user dir for saving URL whitelist - presumably not logged in yet. Skipping." << llendl;
  102. return false;
  103. }
  104. // open a file for writing
  105. llofstream file ( resolvedFilename );
  106. if ( file.is_open () )
  107. {
  108. // for each entry we have
  109. for ( string_list_t::iterator iter = mUrlList.begin (); iter != mUrlList.end (); ++iter )
  110. {
  111. file << ( *iter ) << std::endl;
  112. }
  113. file.close ();
  114. return true;
  115. };
  116. return false;
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////
  119. //
  120. bool LLUrlWhiteList::clear ()
  121. {
  122. mUrlList.clear ();
  123. mCurIndex = 0;
  124. return true;
  125. }
  126. std::string url_cleanup(std::string pattern)
  127. {
  128. LLStringUtil::trim(pattern);
  129. S32 length = pattern.length();
  130. S32 position = 0;
  131. std::string::reverse_iterator it = pattern.rbegin();
  132. ++it; // skip last char, might be '/'
  133. ++position;
  134. for (; it < pattern.rend(); ++it)
  135. {
  136. char c = *it;
  137. if (c == '/')
  138. {
  139. // found second to last '/'
  140. S32 desired_length = length - position;
  141. LLStringUtil::truncate(pattern, desired_length);
  142. break;
  143. }
  144. ++position;
  145. }
  146. return pattern;
  147. }
  148. ///////////////////////////////////////////////////////////////////////////////
  149. //
  150. bool LLUrlWhiteList::addItem ( const std::string& itemIn, bool saveAfterAdd )
  151. {
  152. std::string item = url_cleanup(itemIn);
  153. mUrlList.push_back ( item );
  154. // use this when all you want to do is call addItem ( ... ) where necessary
  155. if ( saveAfterAdd )
  156. save ();
  157. return true;
  158. }
  159. ///////////////////////////////////////////////////////////////////////////////
  160. //
  161. bool LLUrlWhiteList::getFirst ( std::string& valueOut )
  162. {
  163. if ( mUrlList.size () == 0 )
  164. return false;
  165. mCurIndex = 0;
  166. valueOut = mUrlList[mCurIndex++];
  167. return true;
  168. }
  169. ///////////////////////////////////////////////////////////////////////////////
  170. //
  171. bool LLUrlWhiteList::getNext ( std::string& valueOut )
  172. {
  173. if ( mCurIndex >= mUrlList.size () )
  174. return false;
  175. valueOut = mUrlList[mCurIndex++];
  176. return true;
  177. }
  178. ///////////////////////////////////////////////////////////////////////////////
  179. //
  180. bool LLUrlWhiteList::containsMatch ( const std::string& patternIn )
  181. {
  182. return false;
  183. }