PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tmp/install_516bd17a11ff6/easyblogusers.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 437 lines | 320 code | 84 blank | 33 comment | 28 complexity | cb8472d8144141fce0e775bdb13191a4 MD5 | raw file
  1. <?php
  2. /**
  3. * @package EasyBlog
  4. * @copyright Copyright (C) 2010 Stack Ideas Private Limited. All rights reserved.
  5. * @license GNU/GPL, see LICENSE.php
  6. *
  7. * EasyBlog is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. defined('_JEXEC') or die('Restricted access');
  14. jimport('joomla.plugin.plugin');
  15. jimport('joomla.filesystem.file');
  16. class plgUserEasyBlogUsers extends JPlugin
  17. {
  18. function plgUserEasyBlogUsers(& $subject, $config)
  19. {
  20. if(JFile::exists(JPATH_ROOT.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_easyblog'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'helper.php'))
  21. {
  22. require_once (JPATH_ROOT.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_easyblog'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'helper.php');
  23. }
  24. parent::__construct($subject, $config);
  25. }
  26. function onUserAfterSave( $user )
  27. {
  28. //j.16
  29. $this->onAfterStoreUser( $user );
  30. }
  31. function onAfterStoreUser( $user )
  32. {
  33. //j.15
  34. $db = JFactory::getDBO();
  35. if( is_object($user))
  36. {
  37. $user = get_object_vars( $user );
  38. }
  39. if( !isset( $user['id'] ) && empty( $user['id'] ) )
  40. return;
  41. //update subscription tables.
  42. $userId = $user['id'];
  43. $userFullname = $user['name'];
  44. $userEmail = $user['email'];
  45. //blogger
  46. $query = 'UPDATE `#__easyblog_blogger_subscription` SET';
  47. $query .= ' `user_id` = ' . $db->Quote( $userId );
  48. $query .= ', `fullname` = ' . $db->Quote( $userFullname );
  49. $query .= ' WHERE `email` = ' . $db->Quote( $userEmail );
  50. $query .= ' AND `user_id` = ' . $db->Quote('0');
  51. $db->setQuery( $query );
  52. $db->query();
  53. //category
  54. $query = 'UPDATE `#__easyblog_category_subscription` SET';
  55. $query .= ' `user_id` = ' . $db->Quote( $userId );
  56. $query .= ', `fullname` = ' . $db->Quote( $userFullname );
  57. $query .= ' WHERE `email` = ' . $db->Quote( $userEmail );
  58. $query .= ' AND `user_id` = ' . $db->Quote('0');
  59. $db->setQuery( $query );
  60. $db->query();
  61. //post
  62. $query = 'UPDATE `#__easyblog_post_subscription` SET';
  63. $query .= ' `user_id` = ' . $db->Quote( $userId );
  64. $query .= ', `fullname` = ' . $db->Quote( $userFullname );
  65. $query .= ' WHERE `email` = ' . $db->Quote( $userEmail );
  66. $query .= ' AND `user_id` = ' . $db->Quote('0');
  67. $db->setQuery( $query );
  68. $db->query();
  69. //site
  70. $query = 'UPDATE `#__easyblog_site_subscription` SET';
  71. $query .= ' `user_id` = ' . $db->Quote( $userId );
  72. $query .= ', `fullname` = ' . $db->Quote( $userFullname );
  73. $query .= ' WHERE `email` = ' . $db->Quote( $userEmail );
  74. $query .= ' AND `user_id` = ' . $db->Quote('0');
  75. $db->setQuery( $query );
  76. $db->query();
  77. //teamblog
  78. $query = 'UPDATE `#__easyblog_team_subscription` SET';
  79. $query .= ' `user_id` = ' . $db->Quote( $userId );
  80. $query .= ', `fullname` = ' . $db->Quote( $userFullname );
  81. $query .= ' WHERE `email` = ' . $db->Quote( $userEmail );
  82. $query .= ' AND `user_id` = ' . $db->Quote('0');
  83. $db->setQuery( $query );
  84. $db->query();
  85. }
  86. function onUserBeforeDelete($user)
  87. {
  88. $this->onBeforeDeleteUser($user);
  89. }
  90. function onBeforeDeleteUser($user)
  91. {
  92. $mainframe = JFactory::getApplication();
  93. if( is_object($user))
  94. {
  95. $user = get_object_vars( $user );
  96. }
  97. $userId = $user['id'];
  98. $newOwnerShip = $this->_getnewOwnerShip( $userId );
  99. $this->ownerTransferCategory( $userId, $newOwnerShip );
  100. $this->ownerTransferTag( $userId, $newOwnerShip );
  101. $this->onwerTransferComment( $userId, $newOwnerShip );
  102. $this->ownerTransferPost( $userId, $newOwnerShip );
  103. $this->removeAssignedACLGroup( $userId );
  104. $this->removeAdsenseSetting( $userId );
  105. $this->removeFeedburnerSetting( $userId );
  106. $this->removeOAuthSetting( $userId );
  107. $this->removeFeaturedBlogger( $userId );
  108. $this->removeTeamBlogUser( $userId );
  109. $this->removeBloggerSubscription( $userId );
  110. $this->removeEasyBlogUser( $userId );
  111. }
  112. function _getnewOwnerShip( $curUserId )
  113. {
  114. $econfig = EasyBlogHelper::getConfig();
  115. // this should get from backend. If backend not defined, get the default superadmin.
  116. $user_id = (EasyBlogHelper::getJoomlaVersion() >= '1.6') ? '42' : '62';
  117. $newOwnerShip = $econfig->get('main_orphanitem_ownership', $user_id);
  118. /**
  119. * we check if the tobe deleted user is the same user id as the saved user id in config.
  120. * if yes, we try to get a next SA id.
  121. */
  122. if( $curUserId == $newOwnerShip)
  123. {
  124. // this is no no a big no! try to get the next admin.
  125. if(EasyBlogHelper::getJoomlaVersion() >= '1.6')
  126. {
  127. $saUsersId = EasyBlogHelper::getSAUsersIds();
  128. if( count($saUsersId) > 0 )
  129. {
  130. for($i = 0; $i < count($saUsersId); $i++)
  131. {
  132. if( $saUsersId[$i] != $curUserId )
  133. {
  134. $newOwnerShip = $saUsersId[$i];
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. else
  141. {
  142. $newOwnerShip = $this->_getSuperAdminId( $curUserId );
  143. }
  144. }
  145. $newOwnerShip = $this->_verifyOnwerShip($newOwnerShip);
  146. return $newOwnerShip;
  147. }
  148. function _verifyOnwerShip( $newOwnerShip )
  149. {
  150. $db = JFactory::getDBO();
  151. $query = 'SELECT `id` FROM `#__users` WHERE `id` = ' . $db->Quote($newOwnerShip);
  152. $db->setQuery($query);
  153. $result = $db->loadResult();
  154. if(empty($result))
  155. {
  156. if(EasyBlogHelper::getJoomlaVersion() >= '1.6')
  157. {
  158. $saUsersId = EasyBlogHelper::getSAUsersIds();
  159. $result = $saUsersId[0];
  160. }
  161. else
  162. {
  163. $result = $this->_getSuperAdminId();
  164. }
  165. }
  166. return $result;
  167. }
  168. function _getSuperAdminId( $curUserId = '')
  169. {
  170. $db = JFactory::getDBO();
  171. $query = 'SELECT `id` FROM `#__users`';
  172. $query .= ' WHERE (LOWER( usertype ) = ' . $db->Quote('super administrator');
  173. $query .= ' OR `gid` = ' . $db->Quote('25') . ')';
  174. if(! empty($curUserId) )
  175. {
  176. $query .= ' AND `id` != ' . $db->Quote( $curUserId );
  177. }
  178. $query .= ' ORDER BY `id` ASC';
  179. $query .= ' LIMIT 1';
  180. $db->setQuery($query);
  181. $result = $db->loadResult();
  182. $result = (empty($result)) ? '62' : $result;
  183. return $result;
  184. }
  185. function ownerTransferCategory( $userId, $newOwnerShip )
  186. {
  187. $db = JFactory::getDBO();
  188. $query = 'UPDATE `#__easyblog_category`';
  189. $query .= ' SET `created_by` = ' . $db->Quote($newOwnerShip);
  190. $query .= ' WHERE `created_by` = ' . $db->Quote($userId);
  191. $db->setQuery( $query );
  192. $db->query();
  193. if($db->getErrorNum())
  194. {
  195. JError::raiseError( 500, $db->stderr());
  196. }
  197. }
  198. function ownerTransferTag( $userId, $newOwnerShip )
  199. {
  200. $db = JFactory::getDBO();
  201. $query = 'UPDATE `#__easyblog_tag`';
  202. $query .= ' SET `created_by` = ' . $db->Quote($newOwnerShip);
  203. $query .= ' WHERE `created_by` = ' . $db->Quote($userId);
  204. $db->setQuery( $query );
  205. $db->query();
  206. if($db->getErrorNum())
  207. {
  208. JError::raiseError( 500, $db->stderr());
  209. }
  210. }
  211. function ownerTransferPost( $userId, $newOwnerShip )
  212. {
  213. $db = JFactory::getDBO();
  214. $query = 'UPDATE `#__easyblog_post`';
  215. $query .= ' SET `created_by` = ' . $db->Quote($newOwnerShip);
  216. $query .= ' WHERE `created_by` = ' . $db->Quote($userId);
  217. $db->setQuery( $query );
  218. $db->query();
  219. if($db->getErrorNum())
  220. {
  221. JError::raiseError( 500, $db->stderr());
  222. }
  223. }
  224. function onwerTransferComment( $userId, $newOwnerShip )
  225. {
  226. $db = JFactory::getDBO();
  227. $query = 'UPDATE `#__easyblog_comment`';
  228. $query .= ' SET `created_by` = ' . $db->Quote($newOwnerShip);
  229. $query .= ' WHERE `created_by` = ' . $db->Quote($userId);
  230. $db->setQuery( $query );
  231. $db->query();
  232. if($db->getErrorNum())
  233. {
  234. JError::raiseError( 500, $db->stderr());
  235. }
  236. }
  237. /**
  238. * Remove assigned user acl group
  239. */
  240. function removeAssignedACLGroup( $userId )
  241. {
  242. $db = JFactory::getDBO();
  243. $query = 'DELETE FROM `#__easyblog_acl_group`';
  244. $query .= ' WHERE `content_id` = ' . $db->Quote($userId);
  245. $query .= ' AND `type` = ' . $db->Quote('assigned');
  246. $db->setQuery( $query );
  247. $db->query();
  248. if($db->getErrorNum())
  249. {
  250. JError::raiseError( 500, $db->stderr());
  251. }
  252. }
  253. function removeAdsenseSetting( $userId )
  254. {
  255. $db = JFactory::getDBO();
  256. $query = 'DELETE FROM `#__easyblog_adsense`';
  257. $query .= ' WHERE `user_id` = ' . $db->Quote($userId);
  258. $db->setQuery( $query );
  259. $db->query();
  260. if($db->getErrorNum())
  261. {
  262. JError::raiseError( 500, $db->stderr());
  263. }
  264. }
  265. function removeFeedburnerSetting( $userId )
  266. {
  267. $db = JFactory::getDBO();
  268. $query = 'DELETE FROM `#__easyblog_feedburner`';
  269. $query .= ' WHERE `userid` = ' . $db->Quote($userId);
  270. $db->setQuery( $query );
  271. $db->query();
  272. if($db->getErrorNum())
  273. {
  274. JError::raiseError( 500, $db->stderr());
  275. }
  276. }
  277. /**
  278. * Since EasyBlog 2.0
  279. */
  280. function removeOAuthSetting( $userId )
  281. {
  282. $db = JFactory::getDBO();
  283. // removing oauth posts
  284. $query = 'DELETE FROM `#__easyblog_oauth_posts`';
  285. $query .= ' WHERE `oauth_id` IN (';
  286. $query .= ' select `id` from `#__easyblog_oauth` where `user_id` = ' . $db->Quote( $userId );
  287. $query .= ')';
  288. $db->setQuery( $query );
  289. $db->query();
  290. if($db->getErrorNum())
  291. {
  292. JError::raiseError( 500, $db->stderr());
  293. }
  294. // removing oauth
  295. $query = 'DELETE FROM `#__easyblog_oauth`';
  296. $query .= ' WHERE `user_id` = ' . $db->Quote($userId);
  297. $db->setQuery( $query );
  298. $db->query();
  299. if($db->getErrorNum())
  300. {
  301. JError::raiseError( 500, $db->stderr());
  302. }
  303. }
  304. function removeFeaturedBlogger( $userId )
  305. {
  306. $db = JFactory::getDBO();
  307. $query = 'DELETE FROM `#__easyblog_featured`';
  308. $query .= ' WHERE `content_id` = ' . $db->Quote($userId);
  309. $query .= ' AND `type` = ' . $db->Quote('blogger');
  310. $db->setQuery( $query );
  311. $db->query();
  312. if($db->getErrorNum())
  313. {
  314. JError::raiseError( 500, $db->stderr());
  315. }
  316. }
  317. function removeTeamBlogUser( $userId )
  318. {
  319. $db = JFactory::getDBO();
  320. $query = 'DELETE FROM `#__easyblog_team_users`';
  321. $query .= ' WHERE `user_id` = ' . $db->Quote($userId);
  322. $db->setQuery( $query );
  323. $db->query();
  324. if($db->getErrorNum())
  325. {
  326. JError::raiseError( 500, $db->stderr());
  327. }
  328. }
  329. function removeBloggerSubscription( $userId )
  330. {
  331. $db = JFactory::getDBO();
  332. $query = 'DELETE FROM `#__easyblog_blogger_subscription`';
  333. $query .= ' WHERE `blogger_id` = ' . $db->Quote($userId);
  334. $db->setQuery( $query );
  335. $db->query();
  336. if($db->getErrorNum())
  337. {
  338. JError::raiseError( 500, $db->stderr());
  339. }
  340. }
  341. function removeEasyBlogUser( $userId )
  342. {
  343. $db = JFactory::getDBO();
  344. $query = 'DELETE FROM `#__easyblog_users`';
  345. $query .= ' WHERE `id` = ' . $db->Quote($userId);
  346. $db->setQuery( $query );
  347. $db->query();
  348. if($db->getErrorNum())
  349. {
  350. JError::raiseError( 500, $db->stderr());
  351. }
  352. }
  353. }