PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/loginmgr/accounts.php

https://gitlab.com/billyprice1/ruTorrent
PHP | 279 lines | 252 code | 27 blank | 0 comment | 32 complexity | 0c3b6eb3b7159b46afb9be62c859abe0 MD5 | raw file
  1. <?php
  2. require_once( dirname(__FILE__)."/../../php/util.php" );
  3. require_once( $rootPath.'/php/cache.php');
  4. require_once( $rootPath.'/php/Snoopy.class.inc');
  5. eval( getPluginConf( 'loginmgr' ) );
  6. class privateData
  7. {
  8. public $hash = '';
  9. public $cookies = null;
  10. public $referer = null;
  11. public $loaded = false;
  12. static public function load( $owner, $client = null )
  13. {
  14. $rt = new privateData($owner);
  15. if($client)
  16. {
  17. $cache = new rCache('/accounts');
  18. if($cache->get($rt))
  19. {
  20. $client->cookies = $rt->cookies;
  21. $client->referer = $rt->referer;
  22. $rt->loaded = true;
  23. }
  24. }
  25. return($rt);
  26. }
  27. public function __construct( $owner )
  28. {
  29. $this->hash = $owner.".dat";
  30. $this->loaded = false;
  31. }
  32. public function remove()
  33. {
  34. $cache = new rCache('/accounts');
  35. $cache->remove($this);
  36. }
  37. public function store( $client )
  38. {
  39. $this->cookies = $client->cookies;
  40. $this->referer = $client->referer;
  41. $cache = new rCache('/accounts');
  42. return($cache->set($this));
  43. }
  44. static public function getModified($owner)
  45. {
  46. $rt = new privateData($owner);
  47. $cache = new rCache('/accounts');
  48. return($cache->getModified($rt));
  49. }
  50. }
  51. abstract class commonAccount
  52. {
  53. public $url = 'http://abstract.com';
  54. public function getName()
  55. {
  56. $className = get_class($this);
  57. $pos = strpos($className, "Account");
  58. if($pos!==false)
  59. $className = substr($className,0,$pos);
  60. return($className);
  61. }
  62. abstract protected function isOK($client);
  63. abstract protected function login($client,$login,$password,&$url,&$method,&$content_type,&$body,&$is_result_fetched);
  64. public function test($url)
  65. {
  66. return( stripos($url,$this->url)===0 );
  67. }
  68. protected function updateCached($client,&$url,&$method,&$content_type,&$body)
  69. {
  70. return(true);
  71. }
  72. protected function isOKPostFetch($client,$url,$method,$content_type,$body)
  73. {
  74. return($this->isOK($client));
  75. }
  76. public function fetch( $client, $url, $login, $password, $method, $content_type, $body )
  77. {
  78. $is_result_fetched = false;
  79. $data = privateData::load( $this->getName(), $client );
  80. $ret = ( ($data->loaded &&
  81. $this->updateCached($client,$url,$method,$content_type,$body) &&
  82. $client->fetch($url,$method,$content_type,$body) &&
  83. $this->isOKPostFetch($client,$url,$method,$content_type,$body)) ||
  84. ($this->login($client,$login,$password,$url,$method,$content_type,$body,$is_result_fetched) &&
  85. $client->status>=200 &&
  86. $client->status<400 &&
  87. $this->isOK($client) &&
  88. ($is_result_fetched || $client->fetch($url,$method,$content_type,$body)) &&
  89. $this->isOKPostFetch($client,$url,$method,$content_type,$body) &&
  90. $data->store($client)) );
  91. if(!$ret)
  92. $data->remove();
  93. return($ret);
  94. }
  95. public function check( $client, $login, $password, $auto )
  96. {
  97. $modified = privateData::getModified($this->getName());
  98. if( ($modified===false) || ((time()-$modified)>=$auto))
  99. {
  100. $data = privateData::load( $this->getName() );
  101. if($this->login($client,$login,$password,$url,$method,$content_type,$body,$is_result_fetched) &&
  102. $client->status>=200 &&
  103. $client->status<400 &&
  104. $this->isOK($client))
  105. $data->store($client);
  106. else
  107. $data->remove();
  108. }
  109. }
  110. }
  111. class accountManager
  112. {
  113. public $hash = "loginmgr.dat";
  114. public $accounts = array();
  115. static public function load()
  116. {
  117. $cache = new rCache();
  118. $ar = new accountManager();
  119. return($cache->get($ar) ? $ar : false);
  120. }
  121. public function store()
  122. {
  123. $cache = new rCache();
  124. return($cache->set($this));
  125. }
  126. public function obtain( $dir = '../plugins/loginmgr/accounts' )
  127. {
  128. $oldAccounts = $this->accounts;
  129. $this->accounts = array();
  130. if( $handle = opendir($dir) )
  131. {
  132. while(false !== ($file = readdir($handle)))
  133. {
  134. if(is_file($dir.'/'.$file))
  135. {
  136. $name = basename($file,".php");
  137. $this->accounts[$name] = array( "name"=>$name, "path"=>fullpath($dir.'/'.$file), "object"=>$name."Account", "login"=>'', "password"=>'', "enabled"=>0, "auto"=>0 );
  138. if(array_key_exists($name,$oldAccounts) && array_key_exists("login",$oldAccounts[$name]))
  139. {
  140. $this->accounts[$name]["login"] = $oldAccounts[$name]["login"];
  141. $this->accounts[$name]["password"] = $oldAccounts[$name]["password"];
  142. $this->accounts[$name]["enabled"] = $oldAccounts[$name]["enabled"];
  143. if(array_key_exists("auto",$oldAccounts[$name]))
  144. $this->accounts[$name]["auto"] = $oldAccounts[$name]["auto"];
  145. }
  146. }
  147. }
  148. closedir($handle);
  149. }
  150. ksort($this->accounts);
  151. $this->store();
  152. $this->setHandlers();
  153. }
  154. public function get()
  155. {
  156. $ret = "theWebUI.theAccounts = {";
  157. foreach( $this->accounts as $name=>$nfo )
  158. $ret.="'".$name."': { login: ".quoteAndDeslashEachItem($nfo["login"]).", password: ".quoteAndDeslashEachItem($nfo["password"]).", enabled: ".$nfo["enabled"].", auto: ".$nfo["auto"]." },";
  159. $len = strlen($ret);
  160. if($ret[$len-1]==',')
  161. $ret = substr($ret,0,$len-1);
  162. return($ret."};\n");
  163. }
  164. public function set()
  165. {
  166. foreach( $this->accounts as $name=>$nfo )
  167. {
  168. if(isset($_REQUEST[$name."_enabled"]))
  169. $this->accounts[$name]["enabled"] = $_REQUEST[$name."_enabled"];
  170. if(isset($_REQUEST[$name."_login"]))
  171. $this->accounts[$name]["login"] = $_REQUEST[$name."_login"];
  172. if(isset($_REQUEST[$name."_password"]))
  173. $this->accounts[$name]["password"] = $_REQUEST[$name."_password"];
  174. if(isset($_REQUEST[$name."_auto"]))
  175. $this->accounts[$name]["auto"] = intval($_REQUEST[$name."_auto"]);
  176. $data = new privateData( $name );
  177. $data->remove();
  178. }
  179. $this->store();
  180. $this->setHandlers();
  181. }
  182. public function getAccount( $url )
  183. {
  184. foreach( $this->accounts as $name=>$nfo )
  185. {
  186. if($nfo["enabled"])
  187. {
  188. require_once( $nfo["path"] );
  189. $object = new $nfo["object"]();
  190. if($object->test($url))
  191. return( $name );
  192. }
  193. }
  194. return(false);
  195. }
  196. public function fetch( $acc, $client, $url, $method="GET", $content_type="", $body="" )
  197. {
  198. if(array_key_exists($acc,$this->accounts))
  199. {
  200. $nfo = $this->accounts[$acc];
  201. require_once( $nfo["path"] );
  202. $object = new $nfo["object"]();
  203. return($object->fetch( $client, $url, $nfo["login"], $nfo["password"], $method, $content_type, $body ));
  204. }
  205. return(false);
  206. }
  207. public function getInfo()
  208. {
  209. $ret = array();
  210. foreach( $this->accounts as $name=>$nfo )
  211. {
  212. require_once( $nfo["path"] );
  213. $nfo["name"] = $name;
  214. $object = new $nfo["object"]();
  215. $nfo["url"] = $object->url;
  216. unset($nfo["object"]);
  217. unset($nfo["path"]);
  218. $ret[] = $nfo;
  219. }
  220. return($ret);
  221. }
  222. public function hasAuto()
  223. {
  224. foreach( $this->accounts as $name=>$nfo )
  225. if($nfo["enabled"] && !empty($nfo["auto"]))
  226. return(true);
  227. return(false);
  228. }
  229. public function setHandlers()
  230. {
  231. if(rTorrentSettings::get()->linkExist)
  232. {
  233. $req = new rXMLRPCRequest( $this->hasAuto() ?
  234. rTorrentSettings::get()->getScheduleCommand("loginmgr",1440,
  235. getCmd('execute').'={sh,-c,'.escapeshellarg(getPHP()).' '.escapeshellarg(dirname(__FILE__).'/update.php').' '.escapeshellarg(getUser()).' & exit 0}' ) :
  236. rTorrentSettings::get()->getRemoveScheduleCommand("loginmgr") );
  237. $req->success();
  238. }
  239. }
  240. public function checkAuto()
  241. {
  242. foreach( $this->accounts as $name=>$nfo )
  243. {
  244. if($nfo["enabled"] && !empty($nfo["auto"]))
  245. {
  246. require_once( $nfo["path"] );
  247. $object = new $nfo["object"]();
  248. $object->check( new Snoopy(), $nfo["login"], $nfo["password"], $nfo["auto"] );
  249. }
  250. }
  251. }
  252. }