Merge pull request #4676 from jmarshallnz/dont_set_scraper_on_tvshow_on_nfo
[vuplus_xbmc] / xbmc / PasswordManager.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "PasswordManager.h"
22 #include "profiles/ProfilesManager.h"
23 #include "profiles/dialogs/GUIDialogLockSettings.h"
24 #include "URL.h"
25 #include "utils/XMLUtils.h"
26 #include "threads/SingleLock.h"
27 #include "utils/log.h"
28 #include "filesystem/File.h"
29
30 using namespace std;
31
32 CPasswordManager &CPasswordManager::GetInstance()
33 {
34   static CPasswordManager sPasswordManager;
35   return sPasswordManager;
36 }
37
38 CPasswordManager::CPasswordManager()
39 {
40   m_loaded = false;
41 }
42
43 bool CPasswordManager::AuthenticateURL(CURL &url)
44 {
45   CSingleLock lock(m_critSection);
46
47   if (!m_loaded)
48     Load();
49   CStdString lookup(GetLookupPath(url));
50   map<CStdString, CStdString>::const_iterator it = m_temporaryCache.find(lookup);
51   if (it == m_temporaryCache.end())
52   { // second step, try something that doesn't quite match
53     it = m_temporaryCache.find(GetServerLookup(lookup));
54   }
55   if (it != m_temporaryCache.end())
56   {
57     CURL auth(it->second);
58     url.SetPassword(auth.GetPassWord());
59     url.SetUserName(auth.GetUserName());
60     return true;
61   }
62   return false;
63 }
64
65 bool CPasswordManager::PromptToAuthenticateURL(CURL &url)
66 {
67   CSingleLock lock(m_critSection);
68
69   CStdString passcode;
70   CStdString username = url.GetUserName();
71   CStdString path = GetLookupPath(url);
72
73   bool saveDetails = false;
74   if (!CGUIDialogLockSettings::ShowAndGetUserAndPassword(username, passcode, url.GetWithoutUserDetails(), &saveDetails))
75     return false;
76
77   url.SetPassword(passcode);
78   url.SetUserName(username);
79
80   // save the information for later
81   SaveAuthenticatedURL(url, saveDetails);
82   return true;
83 }
84
85 void CPasswordManager::SaveAuthenticatedURL(const CURL &url, bool saveToProfile)
86 {
87   // don't store/save authenticated url if it doesn't contain username
88   if (url.GetUserName().empty())
89     return;
90
91   CSingleLock lock(m_critSection);
92
93   CStdString path = GetLookupPath(url);
94   CStdString authenticatedPath = url.Get();
95
96   if (!m_loaded)
97     Load();
98
99   if (saveToProfile)
100   { // write to some random XML file...
101     m_permanentCache[path] = authenticatedPath;
102     Save();
103   }
104
105   // save for both this path and more generally the server as a whole.
106   m_temporaryCache[path] = authenticatedPath;
107   m_temporaryCache[GetServerLookup(path)] = authenticatedPath;
108 }
109
110 void CPasswordManager::Clear()
111 {
112   m_temporaryCache.clear();
113   m_permanentCache.clear();
114   m_loaded = false;
115 }
116
117 void CPasswordManager::Load()
118 {
119   Clear();
120   CStdString passwordsFile = CProfilesManager::Get().GetUserDataItem("passwords.xml");
121   if (XFILE::CFile::Exists(passwordsFile))
122   {
123     CXBMCTinyXML doc;
124     if (!doc.LoadFile(passwordsFile))
125     {
126       CLog::Log(LOGERROR, "%s - Unable to load: %s, Line %d\n%s", 
127         __FUNCTION__, passwordsFile.c_str(), doc.ErrorRow(), doc.ErrorDesc());
128       return;
129     }
130     const TiXmlElement *root = doc.RootElement();
131     if (root->ValueStr() != "passwords")
132       return;
133     // read in our passwords
134     const TiXmlElement *path = root->FirstChildElement("path");
135     while (path)
136     {
137       CStdString from, to;
138       if (XMLUtils::GetPath(path, "from", from) && XMLUtils::GetPath(path, "to", to))
139       {
140         m_permanentCache[from] = to;
141         m_temporaryCache[from] = to;
142         m_temporaryCache[GetServerLookup(from)] = to;
143       }
144       path = path->NextSiblingElement("path");
145     }
146   }
147   m_loaded = true;
148 }
149
150 void CPasswordManager::Save() const
151 {
152   if (m_permanentCache.empty())
153     return;
154
155   CXBMCTinyXML doc;
156   TiXmlElement rootElement("passwords");
157   TiXmlNode *root = doc.InsertEndChild(rootElement);
158   if (!root)
159     return;
160
161   for (map<CStdString, CStdString>::const_iterator i = m_permanentCache.begin(); i != m_permanentCache.end(); ++i)
162   {
163     TiXmlElement pathElement("path");
164     TiXmlNode *path = root->InsertEndChild(pathElement);
165     XMLUtils::SetPath(path, "from", i->first);
166     XMLUtils::SetPath(path, "to", i->second);
167   }
168
169   doc.SaveFile(CProfilesManager::Get().GetUserDataItem("passwords.xml"));
170 }
171
172 CStdString CPasswordManager::GetLookupPath(const CURL &url) const
173 {
174   return "smb://" + url.GetHostName() + "/" + url.GetShareName();
175 }
176
177 CStdString CPasswordManager::GetServerLookup(const CStdString &path) const
178 {
179   CURL url(path);
180   return "smb://" + url.GetHostName() + "/";
181 }