PageRenderTime 68ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_virtuemart/models/user.php

https://github.com/srgg6701/auction-ruseasons
PHP | 1488 lines | 927 code | 243 blank | 318 comment | 248 complexity | 069ce57227124e9d22151054a33b2324 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, JSON
  1. <?php
  2. /**
  3. *
  4. * Data module for shop users
  5. *
  6. * @package VirtueMart
  7. * @subpackage User
  8. * @author Oscar van Eijk
  9. * @author Max Milbers
  10. * @author RickG
  11. * @link http://www.virtuemart.net
  12. * @copyright Copyright (c) 2004 - 2010 VirtueMart Team. All rights reserved.
  13. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  14. * VirtueMart is free software. This version may have been modified pursuant
  15. * to the GNU General Public License, and as distributed it includes or
  16. * is derivative of works licensed under the GNU General Public License or
  17. * other free or open source software licenses.
  18. * @version $Id: user.php 6543 2012-10-16 06:41:27Z Milbo $
  19. */
  20. // Check to ensure this file is included in Joomla!
  21. defined('_JEXEC') or die('Restricted access');
  22. // Hardcoded groupID of the Super Admin
  23. define ('__SUPER_ADMIN_GID', 25);
  24. // Load the model framework
  25. jimport('joomla.version');
  26. if(!class_exists('VmModel'))require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'vmmodel.php');
  27. /**
  28. * Model class for shop users
  29. *
  30. * @package VirtueMart
  31. * @subpackage User
  32. * @author RickG
  33. * @author Max Milbers
  34. */
  35. class VirtueMartModelUser extends VmModel {
  36. /**
  37. * Constructor for the user model.
  38. *
  39. * The user ID is read and determined if it is an array of ids or just one single id.
  40. */
  41. function __construct(){
  42. parent::__construct('virtuemart_user_id');
  43. $this->setMainTable('vmusers');
  44. $this->setToggleName('user_is_vendor');
  45. $this->addvalidOrderingFieldName(array('ju.username','ju.name','sg.virtuemart_shoppergroup_id','shopper_group_name','shopper_group_desc') );
  46. array_unshift($this->_validOrderingFieldName,'ju.id');
  47. // $user = JFactory::getUser();
  48. // $this->_id = $user->id;
  49. }
  50. /**
  51. * public function Resets the user id and data
  52. *
  53. *
  54. * @author Max Milbers
  55. */
  56. public function setId($cid){
  57. $user = JFactory::getUser();
  58. //anonymous sets to 0 for a new entry
  59. if(empty($user->id)){
  60. $userId = 0;
  61. //echo($this->_id,'Recognized anonymous case');
  62. } else {
  63. //not anonymous, but no cid means already registered user edit own data
  64. if(empty($cid)){
  65. $userId = $user->id;
  66. // vmdebug('setId setCurrent $user',$user->get('id'));
  67. } else {
  68. if($cid != $user->id){
  69. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
  70. if(Permissions::getInstance()->check("admin")) {
  71. $userId = $cid;
  72. // vmdebug('Admin watches user, setId '.$cid);
  73. } else {
  74. JError::raiseWarning(1,'Hacking attempt');
  75. $userId = $user->id;
  76. }
  77. }else {
  78. $userId = $user->id;
  79. }
  80. }
  81. }
  82. $this->setUserId($userId);
  83. return $userId;
  84. }
  85. /**
  86. * Internal function
  87. *
  88. * @param unknown_type $id
  89. */
  90. private function setUserId($id){
  91. $app = JFactory::getApplication();
  92. // if($app->isAdmin()){
  93. if($this->_id!=$id){
  94. $this->_id = (int)$id;
  95. $this->_data = null;
  96. }
  97. // }
  98. }
  99. public function getCurrentUser(){
  100. $user = JFactory::getUser();
  101. $this->setUserId($user->id);
  102. return $this->getUser();
  103. }
  104. private $_defaultShopperGroup = 0;
  105. /**
  106. * Sets the internal user id with given vendor Id
  107. *
  108. * @author Max Milbers
  109. * @param int $vendorId
  110. */
  111. function getVendor($vendorId=1,$return=TRUE){
  112. $vendorModel = VmModel::getModel('vendor');
  113. $userId = VirtueMartModelVendor::getUserIdByVendorId($vendorId);
  114. if($userId){
  115. $this->setUserId($userId);
  116. if($return){
  117. return $this->getUser();
  118. }
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * Retrieve the detail record for the current $id if the data has not already been loaded.
  125. * @author Max Milbers
  126. */
  127. function getUser(){
  128. if(!empty($this->_data)) return $this->_data;
  129. if(empty($this->_db)) $this->_db = JFactory::getDBO();
  130. $this->_data = $this->getTable('vmusers');
  131. $this->_data->load((int)$this->_id);
  132. // vmdebug('$this->_data->vmusers',$this->_data);
  133. $this->_data->JUser = JUser::getInstance($this->_id);
  134. // vmdebug('$this->_data->JUser',$this->_data->JUser);
  135. //if(empty($this->_data->perms)){
  136. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
  137. $this->_data->perms = Permissions::getInstance()->getPermissions((int)$this->_id);
  138. //}
  139. // Add the virtuemart_shoppergroup_ids
  140. $xrefTable = $this->getTable('vmuser_shoppergroups');
  141. $this->_data->shopper_groups = $xrefTable->load($this->_id);
  142. if(empty($this->_data->shopper_groups)){
  143. $shoppergroupmodel = VmModel::getModel('ShopperGroup');
  144. $site = JFactory::getApplication ()->isSite ();
  145. $this->_data->shopper_groups = array();
  146. $shoppergroupmodel->appendShopperGroups($this->_data->shopper_groups,$this->_data->JUser,$site);
  147. }
  148. $q = 'SELECT `virtuemart_userinfo_id` FROM `#__virtuemart_userinfos` WHERE `virtuemart_user_id` = "' . (int)$this->_id.'"';
  149. $this->_db->setQuery($q);
  150. $userInfo_ids = $this->_db->loadResultArray(0);
  151. // vmdebug('my query',$this->_db->getQuery());
  152. // vmdebug('my $_ui',$userInfo_ids,$this->_id);
  153. $this->_data->userInfo = array ();
  154. $BTuid = 0;
  155. foreach($userInfo_ids as $uid){
  156. $this->_data->userInfo[$uid] = $this->getTable('userinfos');
  157. $this->_data->userInfo[$uid]->load($uid);
  158. if ($this->_data->userInfo[$uid]->address_type == 'BT') {
  159. $BTuid = $uid;
  160. $this->_data->userInfo[$BTuid]->name = $this->_data->JUser->name;
  161. $this->_data->userInfo[$BTuid]->email = $this->_data->JUser->email;
  162. $this->_data->userInfo[$BTuid]->username = $this->_data->JUser->username;
  163. $this->_data->userInfo[$BTuid]->address_type = 'BT';
  164. // vmdebug('$this->_data->vmusers',$this->_data);
  165. }
  166. }
  167. // vmdebug('user_is_vendor ?',$this->_data->user_is_vendor);
  168. if($this->_data->user_is_vendor){
  169. $vendorModel = VmModel::getModel('vendor');
  170. if(Vmconfig::get('multix','none')==='none'){
  171. $this->_data->virtuemart_vendor_id = 1;
  172. }
  173. $vendorModel->setId($this->_data->virtuemart_vendor_id);
  174. $this->_data->vendor = $vendorModel->getVendor();
  175. }
  176. return $this->_data;
  177. }
  178. /**
  179. * Retrieve contact info for a user if any
  180. *
  181. * @return array of null
  182. */
  183. function getContactDetails()
  184. {
  185. if ($this->_id) {
  186. $this->_db->setQuery('SELECT * FROM #__contact_details WHERE user_id = ' . $this->_id);
  187. $_contacts = $this->_db->loadObjectList();
  188. if (count($_contacts) > 0) {
  189. return $_contacts[0];
  190. }
  191. }
  192. return null;
  193. }
  194. /**
  195. * Functions belonging to get_groups_below_me Taken with correspondence from CommunityBuilder
  196. * adjusted to the our needs
  197. * @version $Id: user.php 6543 2012-10-16 06:41:27Z Milbo $
  198. * @package Community Builder
  199. * @subpackage cb.acl.php
  200. * @author Beat and mambojoe
  201. * @author Max Milbers
  202. * @copyright (C) Beat, www.joomlapolis.com
  203. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
  204. */
  205. function get_object_id( $var_1 = null, $var_2 = null, $var_3 = null ) {
  206. if ( JVM_VERSION === 2) {
  207. $return = $var_2;
  208. } else {
  209. $return = $this->_acl->get_object_id( $var_1, $var_2, $var_3 );
  210. }
  211. return $return;
  212. }
  213. /**
  214. * Taken with correspondence from CommunityBuilder
  215. * adjusted to the our needs
  216. * @version $Id: user.php 6543 2012-10-16 06:41:27Z Milbo $
  217. * @package Community Builder
  218. * @subpackage cb.acl.php
  219. * @author Beat and mambojoe
  220. * @author Max Milbers
  221. * @copyright (C) Beat, www.joomlapolis.com
  222. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
  223. */
  224. function get_object_groups( $var_1 = null, $var_2 = null, $var_3 = null ) {
  225. if ( version_compare(JVERSION,'1.6.0','ge') ) {
  226. $user_id = ( is_integer( $var_1 ) ? $var_1 : $var_2 );
  227. $recurse = ( $var_3 == 'RECURSE' ? true : false );
  228. $return = $this->_acl->getGroupsByUser( $user_id, $recurse );
  229. } else {
  230. if ( ! $var_2 ) {
  231. $var_2 = 'ARO';
  232. }
  233. if ( ! $var_3 ) {
  234. $var_3 = 'NO_RECURSE';
  235. }
  236. $return = $this->_acl->get_object_groups( $var_1, $var_2, $var_3 );
  237. }
  238. return $return;
  239. }
  240. /** * Remap literal groups (such as in default values) to the hardcoded CMS values
  241. *
  242. * @param string|array $name of int|string
  243. * @return int|array of int
  244. */
  245. function mapGroupNamesToValues( $name ) {
  246. static $ps = null;
  247. $selected = (array) $name;
  248. foreach ( $selected as $k => $v ) {
  249. if ( ! is_numeric( $v ) ) {
  250. if ( ! $ps ) {
  251. if ( JVM_VERSION === 2 ) {
  252. $ps = array( 'Root' => 0 , 'Users' => 0 , 'Public' => 1, 'Registered' => 2, 'Author' => 3, 'Editor' => 4, 'Publisher' => 5, 'Backend' => 0 , 'Manager' => 6, 'Administrator' => 7, 'Superadministrator' => 8 );
  253. } else {
  254. $ps = array( 'Root' => 17, 'Users' => 28, 'Public' => 29, 'Registered' => 18, 'Author' => 19, 'Editor' => 20, 'Publisher' => 21, 'Backend' => 30, 'Manager' => 23, 'Administrator' => 24, 'Superadministrator' => 25 );
  255. }
  256. }
  257. if ( array_key_exists( $v, $ps ) ) {
  258. if ( $ps[$v] != 0 ) {
  259. $selected[$k] = $ps[$v];
  260. } else {
  261. unset( $selected[$k] );
  262. }
  263. } else {
  264. $selected[$k] = (int) $v;
  265. }
  266. }
  267. }
  268. if ( ! is_array( $name ) ) {
  269. $selected = $selected[0];
  270. }
  271. return $selected;
  272. }
  273. function get_group_children_tree( $var_1 = null, $var_2 = null, $var_3 = null, $var_4 = null ) {
  274. $_CB_database = &$this->getDbo();
  275. if ( ! $var_4 ) {
  276. $var_4 = true;
  277. }
  278. if ( JVM_VERSION === 2 ) {
  279. $query = 'SELECT a.' . $_CB_database->NameQuote( 'id' ) . ' AS value'
  280. . ', a.' . $_CB_database->NameQuote( 'title' ) . ' AS text'
  281. . ', COUNT( DISTINCT b.' . $_CB_database->NameQuote( 'id' ) . ' ) AS level'
  282. . "\n FROM " . $_CB_database->NameQuote( '#__usergroups' ) . " AS a"
  283. . "\n LEFT JOIN " . $_CB_database->NameQuote( '#__usergroups' ) . " AS b"
  284. . ' ON a.' . $_CB_database->NameQuote( 'lft' ) . ' > b.' . $_CB_database->NameQuote( 'lft' )
  285. . ' AND a.' . $_CB_database->NameQuote( 'rgt' ) . ' < b.' . $_CB_database->NameQuote( 'rgt' )
  286. . "\n GROUP BY a." . $_CB_database->NameQuote( 'id' )
  287. . "\n ORDER BY a." . $_CB_database->NameQuote( 'lft' ) . " ASC";
  288. $_CB_database->setQuery( $query );
  289. $groups = $_CB_database->loadObjectList();
  290. $user_groups = array();
  291. for ( $i = 0, $n = count( $groups ); $i < $n; $i++ ) {
  292. $groups[$i]->text = str_repeat( '- ', $groups[$i]->level ) . JText::_( $groups[$i]->text );
  293. if ( $var_4 ) {
  294. $user_groups[$i] = JHtml::_( 'select.option', $groups[$i]->value, $groups[$i]->text );
  295. } else {
  296. $user_groups[$i] = array( 'value' => $groups[$i]->value, 'text' => $groups[$i]->text );
  297. }
  298. }
  299. $return = $user_groups;
  300. } else {
  301. if ( ! $var_3 ) {
  302. $var_3 = true;
  303. }
  304. $return = $this->_acl->get_group_children_tree( $var_1, $var_2, $var_3, $var_4 );
  305. }
  306. return $return;
  307. }
  308. /**
  309. * Return a list with groups that can be set by the current user
  310. *
  311. * @return mixed Array with groups that can be set, or the groupname (string) if it cannot be changed.
  312. */
  313. function getGroupList()
  314. {
  315. if(JVM_VERSION === 2) {
  316. //hm CB thing also not help
  317. // $_grpList = $this->get_groups_below_me();
  318. // return $_grpList;
  319. /* if(!class_exists('UsersModelUser')) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_users'.DS.'models'.DS.'user.php');
  320. $jUserModel = new UsersModelUser();
  321. $list = $jUserModel->getGroups();
  322. $user = JFactory::getUser();
  323. if ($user->authorise('core.edit', 'com_users') && $user->authorise('core.manage', 'com_users'))
  324. {
  325. $model = JModel::getInstance('Groups', 'UsersModel', array('ignore_request' => true));
  326. return $model->getItems();
  327. }
  328. else
  329. {
  330. return null;
  331. }*/
  332. $user = JFactory::getUser();
  333. $authGroups = JAccess::getGroupsByUser($user->id);
  334. // $authGroups = $user->getAuthorisedGroups();
  335. // vmdebug('getGroupList j17',$authGroups);
  336. $db = $this->getDbo();
  337. $where = implode($authGroups,'" OR `id` = "').'"';
  338. $q = 'SELECT `id` as value,`title` as text FROM #__usergroups WHERE `id` = "'.$where;
  339. $db->setQuery($q);
  340. $list = $db->loadAssocList();
  341. // foreach($list as $item){
  342. // vmdebug('getGroupList $item ',$item);
  343. // }
  344. // vmdebug('getGroupList $q '.$list);
  345. return $list;
  346. } else {
  347. $_aclObject = JFactory::getACL();
  348. if(empty($this->_data)) $this->getUser();
  349. if (JVM_VERSION>1){
  350. //TODO fix this latter. It's just an workarround to make it working on 1.6
  351. $gids = $this->_data->JUser->get('groups');
  352. return array_flip($gids);
  353. }
  354. $_usr = $_aclObject->get_object_id ('users', $this->_data->JUser->get('id'), 'ARO');
  355. $_grp = $_aclObject->get_object_groups ($_usr, 'ARO');
  356. $_grpName = strtolower ($_aclObject->get_group_name($_grp[0], 'ARO'));
  357. $_currentUser = JFactory::getUser();
  358. $_my_usr = $_aclObject->get_object_id ('users', $_currentUser->get('id'), 'ARO');
  359. $_my_grp = $_aclObject->get_object_groups ($_my_usr, 'ARO');
  360. $_my_grpName = strtolower ($_aclObject->get_group_name($_my_grp[0], 'ARO'));
  361. // administrators can't change each other and frontend-only users can only see groupnames
  362. if (( $_grpName == $_my_grpName && $_my_grpName == 'administrator' ) ||
  363. !$_aclObject->is_group_child_of($_my_grpName, 'Public Backend')) {
  364. return $_grpName;
  365. } else {
  366. $_grpList = $_aclObject->get_group_children_tree(null, 'USERS', false);
  367. $_remGroups = $_aclObject->get_group_children( $_my_grp[0], 'ARO', 'RECURSE' );
  368. if (!$_remGroups) {
  369. $_remGroups = array();
  370. }
  371. // Make sure privs higher than my own can't be granted
  372. if (in_array($_grp[0], $_remGroups)) {
  373. // nor can privs of users with higher privs be decreased.
  374. return $_grpName;
  375. }
  376. $_i = 0;
  377. $_j = count($_grpList);
  378. while ($_i < $_j) {
  379. if (in_array($_grpList[$_i]->value, $_remGroups)) {
  380. array_splice( $_grpList, $_i, 1 );
  381. $_j = count($_grpList);
  382. } else {
  383. $_i++;
  384. }
  385. }
  386. return $_grpList;
  387. }
  388. }
  389. }
  390. /**
  391. * Bind the post data to the JUser object and the VM tables, then saves it
  392. * It is used to register new users
  393. * This function can also change already registered users, this is important when a registered user changes his email within the checkout.
  394. *
  395. * @author Max Milbers
  396. * @author Oscar van Eijk
  397. * @return boolean True is the save was successful, false otherwise.
  398. */
  399. public function store(&$data){
  400. $message = '';
  401. $user = '';
  402. $newId = 0;
  403. JRequest::checkToken() or jexit( 'Invalid Token, while trying to save user' );
  404. $mainframe = JFactory::getApplication() ;
  405. if(empty($data)){
  406. vmError('Developer notice, no data to store for user');
  407. return false;
  408. }
  409. //To find out, if we have to register a new user, we take a look on the id of the usermodel object.
  410. //The constructor sets automatically the right id.
  411. $new = ($this->_id < 1);
  412. if(empty($this->_id)){
  413. $user = JFactory::getUser();
  414. } else {
  415. $user = JFactory::getUser($this->_id);
  416. }
  417. $gid = $user->get('gid'); // Save original gid
  418. // Preformat and control user datas by plugin
  419. JPluginHelper::importPlugin('vmuserfield');
  420. $dispatcher = JDispatcher::getInstance();
  421. $valid = true ;
  422. $dispatcher->trigger('plgVmOnBeforeUserfieldDataSave',array(&$valid,$this->_id,&$data,$user ));
  423. // $valid must be false if plugin detect an error
  424. if( $valid == false ) {
  425. return false;
  426. }
  427. // Before I used this "if($cart && !$new)"
  428. // This construction is necessary, because this function is used to register a new JUser, so we need all the JUser data in $data.
  429. // On the other hand this function is also used just for updating JUser data, like the email for the BT address. In this case the
  430. // name, username, password and so on is already stored in the JUser and dont need to be entered again.
  431. if(empty ($data['email'])){
  432. $email = $user->get('email');
  433. if(!empty($email)){
  434. $data['email'] = $email;
  435. }
  436. } else {
  437. $data['email'] = JRequest::getString('email', '', 'post', 'email');
  438. }
  439. $data['email'] = str_replace(array('\'','"',',','%','*','/','\\','?','^','`','{','}','|','~'),array(''),$data['email']);
  440. //This is important, when a user changes his email address from the cart,
  441. //that means using view user layout edit_address (which is called from the cart)
  442. $user->set('email',$data['email']);
  443. if(empty ($data['name'])){
  444. $name = $user->get('name');
  445. if(!empty($name)){
  446. $data['name'] = $name;
  447. }
  448. } else {
  449. $data['name'] = JRequest::getString('name', '', 'post', 'name');
  450. }
  451. $data['name'] = str_replace(array('\'','"',',','%','*','/','\\','?','^','`','{','}','|','~'),array(''),$data['name']);
  452. if(empty ($data['username'])){
  453. $username = $user->get('username');
  454. if(!empty($username)){
  455. $data['username'] = $username;
  456. } else {
  457. $data['username'] = JRequest::getVar('username', '', 'post', 'username');
  458. }
  459. }
  460. if(empty ($data['password'])){
  461. $data['password'] = JRequest::getVar('password', '', 'post', 'string' ,JREQUEST_ALLOWRAW);
  462. }
  463. if(empty ($data['password2'])){
  464. $data['password2'] = JRequest::getVar('password2', '', 'post', 'string' ,JREQUEST_ALLOWRAW);
  465. }
  466. if(!$new && !empty($data['password']) && empty($data['password2'])){
  467. unset($data['password']);
  468. unset($data['password2']);
  469. }
  470. // Bind Joomla userdata
  471. if (!$user->bind($data)) {
  472. foreach($user->getErrors() as $error) {
  473. // vmError('user bind '.$error);
  474. vmError('user bind '.$error,JText::sprintf('COM_VIRTUEMART_USER_STORE_ERROR',$error));
  475. }
  476. $message = 'Couldnt bind data to joomla user';
  477. array('user'=>$user,'password'=>$data['password'],'message'=>$message,'newId'=>$newId,'success'=>false);
  478. }
  479. if($new){
  480. // If user registration is not allowed, show 403 not authorized.
  481. // But it is possible for admins and storeadmins to save
  482. $usersConfig = JComponentHelper::getParams( 'com_users' );
  483. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
  484. if (!Permissions::getInstance()->check("admin,storeadmin") && $usersConfig->get('allowUserRegistration') == '0') {
  485. JError::raiseError( 403, JText::_('COM_VIRTUEMART_ACCESS_FORBIDDEN'));
  486. return;
  487. }
  488. $authorize = JFactory::getACL();
  489. // Initialize new usertype setting
  490. $newUsertype = $usersConfig->get( 'new_usertype' );
  491. if (!$newUsertype) {
  492. if ( JVM_VERSION===1){
  493. $newUsertype = 'Registered';
  494. } else {
  495. $newUsertype=2;
  496. }
  497. }
  498. // Set some initial user values
  499. $user->set('usertype', $newUsertype);
  500. if ( JVM_VERSION===1){
  501. $user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));
  502. } else {
  503. $user->groups[] = $newUsertype;
  504. }
  505. $date = JFactory::getDate();
  506. $user->set('registerDate', $date->toMySQL());
  507. // If user activation is turned on, we need to set the activation information
  508. $useractivation = $usersConfig->get( 'useractivation' );
  509. $doUserActivation=false;
  510. if ( JVM_VERSION===1){
  511. if ($useractivation == '1' ) {
  512. $doUserActivation=true;
  513. }
  514. } else {
  515. if ($useractivation == '1' or $useractivation == '2') {
  516. $doUserActivation=true;
  517. }
  518. }
  519. vmdebug('user',$useractivation , $doUserActivation);
  520. if ($doUserActivation )
  521. {
  522. jimport('joomla.user.helper');
  523. $user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
  524. $user->set('block', '1');
  525. //$user->set('lastvisitDate', '0000-00-00 00:00:00');
  526. }
  527. }
  528. $option = JRequest::getCmd( 'option');
  529. // If an exising superadmin gets a new group, make sure enough admins are left...
  530. if (!$new && $user->get('gid') != $gid && $gid == __SUPER_ADMIN_GID) {
  531. if ($this->getSuperAdminCount() <= 1) {
  532. vmError(JText::_('COM_VIRTUEMART_USER_ERR_ONLYSUPERADMIN'));
  533. return false;
  534. }
  535. }
  536. // Save the JUser object
  537. if (!$user->save()) {
  538. vmError(JText::_( $user->getError()) , JText::_( $user->getError()));
  539. return false;
  540. }
  541. //vmdebug('my user, why logged in? ',$user);
  542. $newId = $user->get('id');
  543. $data['virtuemart_user_id'] = $newId; //We need this in that case, because data is bound to table later
  544. $this->setUserId($newId);
  545. //Save the VM user stuff
  546. if(!$this->saveUserData($data) || !self::storeAddress($data)){
  547. vmError('COM_VIRTUEMART_NOT_ABLE_TO_SAVE_USER_DATA');
  548. // vmError(Jtext::_('COM_VIRTUEMART_NOT_ABLE_TO_SAVE_USERINFO_DATA'));
  549. } else {
  550. if ($new) {
  551. $this->sendRegistrationEmail($user,$user->password_clear, $doUserActivation);
  552. if ($doUserActivation ) {
  553. vmInfo('COM_VIRTUEMART_REG_COMPLETE_ACTIVATE');
  554. } else {
  555. vmInfo('COM_VIRTUEMART_REG_COMPLETE');
  556. }
  557. } else {
  558. vmInfo('COM_VIRTUEMART_USER_DATA_STORED');
  559. }
  560. }
  561. if((int)$data['user_is_vendor']==1){
  562. // vmdebug('vendor recognised');
  563. if($this ->storeVendorData($data)){
  564. if ($new) {
  565. if ($doUserActivation ) {
  566. vmInfo('COM_VIRTUEMART_REG_VENDOR_COMPLETE_ACTIVATE');
  567. } else {
  568. vmInfo('COM_VIRTUEMART_REG_VENDOR_COMPLETE');
  569. }
  570. } else {
  571. vmInfo('COM_VIRTUEMART_VENDOR_DATA_STORED');
  572. }
  573. }
  574. }
  575. return array('user'=>$user,'password'=>$data['password'],'message'=>$message,'newId'=>$newId,'success'=>true);
  576. }
  577. /**
  578. * This function is NOT for anonymous. Anonymous just get the information directly sent by email.
  579. * This function saves the vm Userdata for registered JUsers.
  580. * TODO, setting of shoppergroup isnt done
  581. *
  582. * TODO No reason not to use this function for new users, but it requires a Joomla <user> plugin
  583. * that gets fired by the onAfterStoreUser. I'll built that (OvE)
  584. *
  585. * Notice:
  586. * As long we do not have the silent registration, an anonymous does not get registered. It is enough to send the virtuemart_order_id
  587. * with the email. The order is saved with all information in an extra table, so there is
  588. * no need for a silent registration. We may think about if we actually need/want the feature silent registration
  589. * The information of anonymous is stored in the order table and has nothing todo with the usermodel!
  590. *
  591. * @author Max Milbers
  592. * @author Oscar van Eijk
  593. * return boolean
  594. */
  595. public function saveUserData(&$data,$trigger=true){
  596. if(empty($this->_id)){
  597. echo 'This is a notice for developers, you used this function for an anonymous user, but it is only designed for already registered ones';
  598. vmError( 'This is a notice for developers, you used this function for an anonymous user, but it is only designed for already registered ones');
  599. return false;
  600. }
  601. $noError = true;
  602. $usertable = $this->getTable('vmusers');
  603. $alreadyStoredUserData = $usertable->load($this->_id);
  604. $app = JFactory::getApplication();
  605. unset($data['virtuemart_vendor_id']);
  606. unset($data['user_is_vendor']);
  607. $data['user_is_vendor'] = $alreadyStoredUserData->user_is_vendor;
  608. $data['virtuemart_vendor_id'] = $alreadyStoredUserData->virtuemart_vendor_id;
  609. vmdebug('saveUserData',$data);
  610. unset($data['customer_number']);
  611. if(empty($alreadyStoredUserData->customer_number)){
  612. //if(!class_exists('vmUserPlugin')) require(JPATH_VM_SITE.DS.'helpers'.DS.'vmuserplugin.php');
  613. ///if(!$returnValues){
  614. $data['customer_number'] = md5($data['username']);
  615. //We set this data so that vmshopper plugin know if they should set the customer nummer
  616. $data['customer_number_bycore'] = 1;
  617. //}
  618. } else {
  619. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
  620. if(!Permissions::getInstance()->check("admin,storeadmin")) {
  621. $data['customer_number'] = $alreadyStoredUserData->customer_number;
  622. }
  623. }
  624. if($app->isSite()){
  625. unset($data['perms']);
  626. if(!empty($alreadyStoredUserData->perms)){
  627. $data['perms'] = $alreadyStoredUserData->perms;
  628. } else {
  629. $data['perms'] = 'shopper';
  630. }
  631. } else {
  632. }
  633. if($trigger){
  634. JPluginHelper::importPlugin('vmshopper');
  635. $dispatcher = JDispatcher::getInstance();
  636. $plg_datas = $dispatcher->trigger('plgVmOnUserStore',array(&$data));
  637. foreach($plg_datas as $plg_data){
  638. // $data = array_merge($plg_data,$data);
  639. }
  640. }
  641. $usertable -> bindChecknStore($data);
  642. $errors = $usertable->getErrors();
  643. foreach($errors as $error){
  644. $this->setError($error);
  645. vmError('storing user adress data'.$error);
  646. $noError = false;
  647. }
  648. if(Permissions::getInstance()->check("admin,storeadmin")) {
  649. $shoppergroupmodel = VmModel::getModel('ShopperGroup');
  650. if(empty($this->_defaultShopperGroup)){
  651. $this->_defaultShopperGroup = $shoppergroupmodel->getDefault(0);
  652. }
  653. if(empty($data['virtuemart_shoppergroup_id']) or $data['virtuemart_shoppergroup_id']==$this->_defaultShopperGroup->virtuemart_shoppergroup_id){
  654. $data['virtuemart_shoppergroup_id'] = array();
  655. }
  656. // Bind the form fields to the table
  657. if(!empty($data['virtuemart_shoppergroup_id'])){
  658. $shoppergroupData = array('virtuemart_user_id'=>$this->_id,'virtuemart_shoppergroup_id'=>$data['virtuemart_shoppergroup_id']);
  659. $user_shoppergroups_table = $this->getTable('vmuser_shoppergroups');
  660. $shoppergroupData = $user_shoppergroups_table -> bindChecknStore($shoppergroupData);
  661. $errors = $user_shoppergroups_table->getErrors();
  662. foreach($errors as $error){
  663. $this->setError($error);
  664. vmError('Set shoppergroup '.$error);
  665. $noError = false;
  666. }
  667. }
  668. }
  669. if($trigger){
  670. $plg_datas = $dispatcher->trigger('plgVmAfterUserStore',array($data));
  671. foreach($plg_datas as $plg_data){
  672. $data = array_merge($plg_data);
  673. }
  674. }
  675. return $noError;
  676. }
  677. public function storeVendorData($data){
  678. if($data['user_is_vendor']){
  679. $vendorModel = VmModel::getModel('vendor');
  680. //TODO Attention this is set now to virtuemart_vendor_id=1, because using a vendor with different id then 1 is not completly supported and can lead to bugs
  681. //So we disable the possibility to store vendors not with virtuemart_vendor_id = 1
  682. if(Vmconfig::get('multix','none')==='none' ){
  683. $data['virtuemart_vendor_id'] = 1;
  684. }
  685. $vendorModel->setId($data['virtuemart_vendor_id']);
  686. if (!$vendorModel->store($data)) {
  687. vmError('storeVendorData '.$vendorModel->getError());
  688. vmdebug('Error storing vendor',$vendorModel);
  689. return false;
  690. }
  691. }
  692. return true;
  693. }
  694. /**
  695. * Take a data array and save any address info found in the array.
  696. *
  697. * @author unknown, oscar, max milbers
  698. * @param array $data (Posted) user data
  699. * @param sting $_table Table name to write to, null (default) not to write to the database
  700. * @param boolean $_cart Attention, this was deleted, the address to cart is now done in the controller (True to write to the session (cart))
  701. * @return boolean True if the save was successful, false otherwise.
  702. */
  703. function storeAddress(&$data){
  704. // if(empty($data['address_type'])){
  705. // vmError('storeAddress no address_type given');
  706. // return false;
  707. // }
  708. $user =JFactory::getUser();
  709. $userinfo = $this->getTable('userinfos');
  710. if($data['address_type'] == 'BT'){
  711. if(isset($data['virtuemart_userinfo_id']) and $data['virtuemart_userinfo_id']!=0){
  712. $data['virtuemart_userinfo_id'] = (int)$data['virtuemart_userinfo_id'];
  713. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'permissions.php');
  714. if(!Permissions::getInstance()->check('admin')){
  715. $userinfo->load($data['virtuemart_userinfo_id']);
  716. if($userinfo->virtuemart_user_id!=$user->id){
  717. vmError('Hacking attempt as admin?','Hacking attempt storeAddress');
  718. return false;
  719. }
  720. }
  721. } else {
  722. $q = 'SELECT `virtuemart_userinfo_id` FROM #__virtuemart_userinfos
  723. WHERE `virtuemart_user_id` = '.$user->id.'
  724. AND `address_type` = "BT"';
  725. $this->_db->setQuery($q);
  726. $total = $this->_db->loadResultArray();
  727. if (count($total) > 0) {
  728. $data['virtuemart_userinfo_id'] = (int)$total[0];
  729. } else {
  730. $data['virtuemart_userinfo_id'] = 0;//md5(uniqid($this->virtuemart_user_id));
  731. }
  732. $userinfo->load($data['virtuemart_userinfo_id']);
  733. //unset($data['virtuemart_userinfo_id']);
  734. }
  735. if(!$this->validateUserData((array)$data,'BT')){
  736. return false;
  737. }
  738. $userInfoData = self::_prepareUserFields($data, 'BT',$userinfo);
  739. //vmdebug('model user storeAddress',$data);
  740. if (!$userinfo->bindChecknStore($userInfoData)) {
  741. vmError('storeAddress '.$userinfo->getError());
  742. }
  743. }
  744. // Check for fields with the the 'shipto_' prefix; that means a (new) shipto address.
  745. if($data['address_type'] == 'ST' or isset($data['shipto_virtuemart_userinfo_id'])){
  746. $dataST = array();
  747. $_pattern = '/^shipto_/';
  748. foreach ($data as $_k => $_v) {
  749. if (preg_match($_pattern, $_k)) {
  750. $_new = preg_replace($_pattern, '', $_k);
  751. $dataST[$_new] = $_v;
  752. }
  753. }
  754. $userinfo = $this->getTable('userinfos');
  755. if(isset($dataST['virtuemart_userinfo_id']) and $dataST['virtuemart_userinfo_id']!=0){
  756. $dataST['virtuemart_userinfo_id'] = (int)$dataST['virtuemart_userinfo_id'];
  757. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'permissions.php');
  758. if(!Permissions::getInstance()->check('admin')){
  759. $userinfo->load($dataST['virtuemart_userinfo_id']);
  760. $user = JFactory::getUser();
  761. if($userinfo->virtuemart_user_id!=$user->id){
  762. vmError('Hacking attempt as admin?','Hacking attempt store address');
  763. return false;
  764. }
  765. }
  766. }
  767. if(empty($userinfo->virtuemart_user_id)){
  768. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'permissions.php');
  769. if(!Permissions::getInstance()->check('admin')){
  770. $dataST['virtuemart_user_id'] = $user->id;
  771. } else {
  772. if(isset($data['virtuemart_user_id'])){
  773. $dataST['virtuemart_user_id'] = (int)$data['virtuemart_user_id'];
  774. } else {
  775. //Disadvantage is that admins should not change the ST address in the FE (what should never happen anyway.)
  776. $dataST['virtuemart_user_id'] = $user->id;
  777. }
  778. }
  779. }
  780. if(!$this->validateUserData((array)$dataST,'ST')){
  781. return false;
  782. }
  783. $dataST['address_type'] = 'ST';
  784. $userfielddata = self::_prepareUserFields($dataST, 'ST',$userinfo);
  785. if (!$userinfo->bindChecknStore($userfielddata)) {
  786. vmError($userinfo->getError());
  787. }
  788. }
  789. return $userinfo->virtuemart_userinfo_id;
  790. }
  791. /**
  792. * Test userdata if valid
  793. *
  794. * @author Max Milbers
  795. * @param String if BT or ST
  796. * @param Object If given, an object with data address data that must be formatted to an array
  797. * @return redirectMsg, if there is a redirectMsg, the redirect should be executed after
  798. */
  799. public function validateUserData($data,$type='BT') {
  800. if (!class_exists('VirtueMartModelUserfields'))
  801. require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'userfields.php');
  802. $userFieldsModel = VmModel::getModel('userfields');
  803. if ($type == 'BT') {
  804. $fieldtype = 'account';
  805. }else {
  806. $fieldtype = 'shipment';
  807. }
  808. $neededFields = $userFieldsModel->getUserFields(
  809. $fieldtype
  810. , array('required' => true, 'delimiters' => true, 'captcha' => true, 'system' => false)
  811. , array('delimiter_userinfo', 'name','username', 'password', 'password2', 'address_type_name', 'address_type', 'user_is_vendor', 'agreed'));
  812. $app = JFactory::getApplication();
  813. if($app->isSite()){
  814. if(!class_exists('VirtueMartCart')) require(JPATH_VM_SITE.DS.'helpers'.DS.'cart.php');
  815. $cart = VirtueMartCart::getCart();
  816. }
  817. $i = 0 ;
  818. $return = true;
  819. foreach ($neededFields as $field) {
  820. if($field->required && empty($data[$field->name]) && $field->name != 'virtuemart_state_id'){
  821. //more than four fields missing, this is not a normal error (should be catche by js anyway, so show the address again.
  822. if($i>3 && $type=='BT'){
  823. vmInfo('COM_VIRTUEMART_CHECKOUT_PLEASE_ENTER_ADDRESS');
  824. return false;
  825. } else {
  826. //vmdebug('validateUserData ',$field,$field->name,$data[$field->name],$data);
  827. //vmTrace('validateUserData ');
  828. vmInfo(JText::sprintf('COM_VIRTUEMART_MISSING_VALUE_FOR_FIELD',JText::_($field->title)) );
  829. $i++;
  830. $return = false;
  831. }
  832. }
  833. //This is a special test for the virtuemart_state_id. There is the speciality that the virtuemart_state_id could be 0 but is valid.
  834. else if ($field->required and $field->name == 'virtuemart_state_id') {
  835. if(!empty($data['virtuemart_country_id']) && !empty($data['virtuemart_state_id']) ){
  836. if (!class_exists('VirtueMartModelState')) require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'state.php');
  837. if (!$msg = VirtueMartModelState::testStateCountry($data['virtuemart_country_id'], $data['virtuemart_state_id'])) {
  838. $i++;
  839. vmInfo(JText::sprintf('COM_VIRTUEMART_MISSING_VALUE_FOR_FIELD',JText::_($field->title)) );
  840. $return = false;
  841. }
  842. }
  843. }
  844. }
  845. return $return;
  846. }
  847. function _prepareUserFields(&$data, $type,$userinfo = 0)
  848. {
  849. if(!class_exists('VirtueMartModelUserfields')) require(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'userfields.php' );
  850. $userFieldsModel = VmModel::getModel('userfields');
  851. if ($type == 'ST') {
  852. $prepareUserFields = $userFieldsModel->getUserFields(
  853. 'shipment'
  854. , array() // Default toggles
  855. );
  856. } else { // BT
  857. // The user is not logged in (anonymous), so we need tome extra fields
  858. $prepareUserFields = $userFieldsModel->getUserFields(
  859. 'account'
  860. , array() // Default toggles
  861. , array('delimiter_userinfo', 'name', 'username', 'password', 'password2', 'user_is_vendor') // Skips
  862. );
  863. }
  864. $admin = false;
  865. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
  866. if(Permissions::getInstance()->check('admin','storeadmin')){
  867. $admin = true;
  868. }
  869. // Format the data
  870. foreach ($prepareUserFields as $fld) {
  871. if(empty($data[$fld->name])) $data[$fld->name] = '';
  872. if(!$admin and $fld->readonly){
  873. $fldName = $fld->name;
  874. unset($data[$fldName]);
  875. if($userinfo!==0){
  876. if(property_exists($userinfo,$fldName)){
  877. vmdebug('property_exists userinfo->$fldName '.$fldName,$userinfo);
  878. $data[$fldName] = $userinfo->$fldName;
  879. } else {
  880. vmError('Your tables seem to be broken, you have fields in your form which have no corresponding field in the db');
  881. }
  882. }
  883. } else {
  884. $data[$fld->name] = $userFieldsModel->prepareFieldDataSave($fld, $data);
  885. }
  886. }
  887. return $data;
  888. }
  889. function getBTuserinfo_id($id = 0){
  890. if(empty($this->_db)) $this->_db = JFactory::getDBO();
  891. if($id == 0){
  892. $id = $this->_id;
  893. //vmdebug('getBTuserinfo_id is '.$this->_id);
  894. }
  895. $q = 'SELECT `virtuemart_userinfo_id` FROM `#__virtuemart_userinfos` WHERE `virtuemart_user_id` = "' .(int)$id .'" AND `address_type`="BT" ';
  896. $this->_db->setQuery($q);
  897. return $this->_db->loadResult();
  898. }
  899. /**
  900. *
  901. * @author Max Milbers
  902. */
  903. function getUserInfoInUserFields($layoutName, $type,$uid,$cart=true,$isVendor=false ){
  904. // if(!class_exists('VirtueMartModelUserfields')) require(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'userfields.php' );
  905. // $userFieldsModel = new VirtuemartModelUserfields();
  906. $userFieldsModel = VmModel::getModel('userfields');
  907. $prepareUserFields = $userFieldsModel->getUserFieldsFor( $layoutName, $type, $uid );
  908. if($type=='ST'){
  909. $preFix = 'shipto_';
  910. } else {
  911. $preFix = '';
  912. }
  913. /*
  914. * JUser or $this->_id is the logged user
  915. */
  916. if(!empty($this->_data->JUser)){
  917. $JUser = $this->_data->JUser;
  918. } else {
  919. $JUser = JUser::getInstance($this->_id);
  920. }
  921. $userFields = array();
  922. if(!empty($uid)){
  923. $data = $this->getTable('userinfos');
  924. $data->load($uid);
  925. // vmdebug('$data',$data);
  926. if($data->virtuemart_user_id!==0 and !$isVendor){
  927. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
  928. if(!Permissions::getInstance()->check("admin")) {
  929. if($data->virtuemart_user_id!=$this->_id){
  930. vmError('Hacking attempt loading userinfo, you got logged');
  931. echo 'Hacking attempt loading userinfo, you got logged';
  932. return false;
  933. }
  934. }
  935. }
  936. if ($data->address_type != 'ST' ) {
  937. $BTuid = $uid;
  938. $data->name = $JUser->name;
  939. $data->email = $JUser->email;
  940. $data->username = $JUser->username;
  941. $data->address_type = 'BT';
  942. }
  943. // vmdebug('getUserInfoInUserFields ',$data);
  944. } else {
  945. //New Address is filled here with the data of the cart (we are in the userview)
  946. if($cart){
  947. if (!class_exists('VirtueMartCart'))
  948. require(JPATH_VM_SITE . DS . 'helpers' . DS . 'cart.php');
  949. $cart = VirtueMartCart::getCart();
  950. $adType = $type.'address';
  951. if(empty($cart->$adType)){
  952. $data = $cart->$type;
  953. if(empty($data)) $data = array();
  954. if($JUser){
  955. if(empty($data['name'])){
  956. $data['name'] = $JUser->name;
  957. }
  958. if(empty($data['email'])){
  959. $data['email'] = $JUser->email;
  960. }
  961. if(empty($data['username'])){
  962. $data['username'] = $JUser->username;
  963. }
  964. }
  965. }
  966. $data = (object)$data;
  967. } else {
  968. if($JUser){
  969. if(empty($data['name'])){
  970. $data['name'] = $JUser->name;
  971. }
  972. if(empty($data['email'])){
  973. $data['email'] = $JUser->email;
  974. }
  975. if(empty($data['username'])){
  976. $data['username'] = $JUser->username;
  977. }
  978. $data = (object)$data;
  979. } else {
  980. $data = null;
  981. }
  982. }
  983. }
  984. $userFields[$uid] = $userFieldsModel->getUserFieldsFilled(
  985. $prepareUserFields
  986. ,$data
  987. ,$preFix
  988. );
  989. return $userFields;
  990. }
  991. /**
  992. * This should store the userdata given in userfields
  993. *
  994. * @author Max Milbers
  995. */
  996. function storeUserDataByFields($data,$type, $toggles, $skips){
  997. if(!class_exists('VirtueMartModelUserfields')) require(JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'userfields.php' );
  998. $userFieldsModel = VmModel::getModel('userfields');
  999. $prepareUserFields = $userFieldsModel->getUserFields(
  1000. $type,
  1001. $toggles,
  1002. $skips
  1003. );
  1004. // Format the data
  1005. foreach ($prepareUserFields as $_fld) {
  1006. if(empty($data[$_fld->name])) $data[$_fld->name] = '';
  1007. $data[$_fld->name] = $userFieldsModel->prepareFieldDataSave($_fld,$data);
  1008. }
  1009. $this->store($data);
  1010. return true;
  1011. }
  1012. /**
  1013. * This uses the shopfunctionsF::renderAndSendVmMail function, which uses a controller and task to render the content
  1014. * and sents it then.
  1015. *
  1016. *
  1017. * @author Oscar van Eijk
  1018. * @author Max Milbers
  1019. * @author Christopher Roussel
  1020. * @author ValĂŠrie Isaksen
  1021. */
  1022. private function sendRegistrationEmail($user, $password, $doUserActivation){
  1023. if(!class_exists('shopFunctionsF')) require(JPATH_VM_SITE.DS.'helpers'.DS.'shopfunctionsf.php');
  1024. $vars = array('user' => $user);
  1025. // Send registration confirmation mail
  1026. $password = preg_replace('/[\x00-\x1F\x7F]/', '', $password); //Disallow control chars in the email
  1027. $vars['password'] = $password;
  1028. if ($doUserActivation) {
  1029. jimport('joomla.user.helper');
  1030. if(JVM_VERSION === 2) {
  1031. $com_users = 'com_users';
  1032. $activationLink = 'index.php?option='.$com_users.'&task=registration.activate&token='.$user->get('activation');
  1033. } else {
  1034. $com_users = 'com_user';
  1035. $activationLink = 'index.php?option='.$com_users.'&task=activate&activation='.$user->get('activation');
  1036. }
  1037. $vars['activationLink'] = $activationLink;
  1038. }
  1039. $vars['doVendor']=true;
  1040. // public function renderMail ($viewName, $recipient, $vars=array(),$controllerName = null)
  1041. shopFunctionsF::renderMail('user', $user->get('email'), $vars);
  1042. //get all super administrator
  1043. $query = 'SELECT name, email, sendEmail' .
  1044. ' FROM #__users' .
  1045. ' WHERE LOWER( usertype ) = "super administrator"';
  1046. $this->_db->setQuery( $query );
  1047. $rows = $this->_db->loadObjectList();
  1048. $vars['doVendor']=false;
  1049. // get superadministrators id
  1050. foreach ( $rows as $row )
  1051. {
  1052. if ($row->sendEmail)
  1053. {
  1054. //$message2 = sprintf ( JText::_( 'COM_VIRTUEMART_SEND_MSG_ADMIN' ), $row->name, $sitename, $name, $email, $username);
  1055. //$message2 = html_entity_decode($message2, ENT_QUOTES);
  1056. //JUtility::sendMail($mailfrom, $fromname, $row->email, $subject2, $message2);
  1057. //shopFunctionsF::renderMail('user', $row->email, $vars);
  1058. }
  1059. }
  1060. }
  1061. /**
  1062. * Delete all record ids selected
  1063. *
  1064. * @return boolean True is the remove was successful, false otherwise.
  1065. */
  1066. function remove($userIds)
  1067. {
  1068. if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
  1069. if(Permissions::getInstance()->check('admin','storeadmin')) {
  1070. $userInfo = $this->getTable('userinfos');
  1071. $vm_shoppergroup_xref = $this->getTable('vmuser_shoppergroups');
  1072. $vmusers = $this->getTable('vmusers');
  1073. $_status = true;
  1074. foreach($userIds as $userId) {
  1075. $_JUser = JUser::getInstance($userId);
  1076. if ($this->getSuperAdminCount() <= 1) {
  1077. // Prevent deletion of the only Super Admin
  1078. //$_u = JUser::getInstance($userId);
  1079. if ($_JUser->get('gid') == __SUPER_ADMIN_GID) {
  1080. vmError(JText::_('COM_VIRTUEMART_USER_ERR_LASTSUPERADMIN'));
  1081. $_status = false;
  1082. continue;
  1083. }
  1084. }
  1085. if(Permissions::getInstance()->check('storeadmin')) {
  1086. if ($_JUser->get('gid') == __SUPER_ADMIN_GID) {
  1087. vmError(JText::_('COM_VIRTUEMART_USER_ERR_LASTSUPERADMIN'));
  1088. $_status = false;
  1089. continue;
  1090. }
  1091. }
  1092. if (!$userInfo->delete($userId)) {
  1093. vmError($userInfo->getError());
  1094. return false;
  1095. }
  1096. if (!$vm_shoppergroup_xref->delete($userId)) {
  1097. vmError($vm_shoppergroup_xref->getError()); // Signal but continue
  1098. $_status = false;
  1099. continue;
  1100. }
  1101. if (!$vmusers->delete($userId)) {
  1102. vmError($vmusers->getError()); // Signal but continue
  1103. $_status = false;
  1104. continue;
  1105. }
  1106. if (!$_JUser->delete()) {
  1107. vmError($_JUser->getError());
  1108. $_status = false;
  1109. continue;
  1110. }
  1111. }
  1112. }
  1113. return $_status;
  1114. }
  1115. /**
  1116. * Retrieve a list of users from the database.
  1117. *
  1118. * @author Max Milbers
  1119. * @return object List of user objects
  1120. */
  1121. function getUserList() {
  1122. //$select = ' * ';
  1123. //$joinedTables = ' FROM #__users AS ju LEFT JOIN #__virtuemart_vmusers AS vmu ON ju.id = vmu.virtuemart_user_id';
  1124. $select = ' DISTINCT ju.id AS id
  1125. , ju.name AS name
  1126. , ju.username AS username
  1127. , ju.email AS email
  1128. , ju.usertype AS usertype
  1129. , IFNULL(vmu.user_is_vendor,"0") AS is_vendor
  1130. , IFNULL(sg.shopper_group_name, "") AS shopper_group_name ';
  1131. $joinedTables = ' FROM #__users AS ju
  1132. LEFT JOIN #__virtuemart_vmusers AS vmu ON ju.id = vmu.virtuemart_user_id
  1133. LEFT JOIN #__virtuemart_vmuser_shoppergroups AS vx ON ju.id = vx.virtuemart_user_id
  1134. LEFT JOIN #__virtuemart_shoppergroups AS sg ON vx.virtuemart_shoppergroup_id = sg.virtuemart_shoppergroup_id ';
  1135. return $this->_data = $this->exeSortSearchListQuery(0,$select,$joinedTables,$this->_getFilter(),' GROUP BY ju.id',$this->_getOrdering());
  1136. }
  1137. /**
  1138. * If a filter was set, get the SQL WHERE clase
  1139. *
  1140. * @return string text to add to the SQL statement
  1141. */
  1142. function _getFilter()
  1143. {
  1144. if ($search = JRequest::getWord('search', false)) {
  1145. $search = '"%' . $this->_db->getEscaped( $search, true ) . '%"' ;
  1146. //$search = $this->_db->Quote($search, false);
  1147. $where = ' WHERE `name` LIKE '.$search.' OR `username` LIKE ' .$search.' OR `email` LIKE ' .$search.' OR `perms` LIKE ' .$search.' OR `usertype` LIKE ' .$search.' OR `shopper_group_name` LIKE ' .$search;
  1148. return ($where);
  1149. }
  1150. return ('');
  1151. }
  1152. /**
  1153. * Retrieve a single address for a user
  1154. *
  1155. * @param $_uid int User ID
  1156. * @param $_virtuemart_userinfo_id string Optional User Info ID
  1157. * @param $_type string, addess- type, ST (ShipTo, default) or BT (BillTo). Empty string to ignore
  1158. */
  1159. function getUserAddressList($_uid = 0, $_type = 'ST',$_virtuemart_userinfo_id = -1){
  1160. //Todo, add perms, allow admin to see 0 entries.
  1161. if($_uid==0 and $this->_id==0){
  1162. return array();
  1163. }
  1164. $_q = 'SELECT * FROM #__virtuemart_userinfos WHERE virtuemart_user_id="' . (($_uid==0)?$this->_id:(int)$_uid) .'"';
  1165. if ($_virtuemart_userinfo_id !== -1) {
  1166. $_q .= ' AND virtuemart_userinfo_id="'.(int)$_virtuemart_userinfo_id.'"';
  1167. } else {
  1168. if ($_type !== '') {
  1169. $_q .= ' AND address_type="'.$_type.'"';
  1170. }
  1171. }
  1172. // vmdebug('getUserAddressList query '.$_q);
  1173. return ($this->_getList($_q));
  1174. }
  1175. /**
  1176. * Retrieves the Customer Number of the user specified by ID
  1177. *
  1178. * @param int $_id User ID
  1179. * @return string Customer Number
  1180. */
  1181. function getCustomerNumberById($_id = 0)
  1182. {
  1183. $_q = "SELECT `customer_number` FROM `#__virtuemart_vmusers` "
  1184. ."WHERE `virtuemart_user_id`='" . (($_id==0)?$this->_id:(int)$_id) . "' ";
  1185. $_r = $this->_getList($_q);
  1186. if(!empty($_r[0])){
  1187. return $_r[0]->customer_number;
  1188. }else {
  1189. return false;
  1190. }
  1191. }
  1192. /**
  1193. * Get the number of active Super Admins
  1194. *
  1195. * @return integer
  1196. */
  1197. function getSuperAdminCount()
  1198. {
  1199. $this->_db->setQuery('SELECT COUNT(id) FROM #__users'
  1200. . ' WHERE gid = ' . __SUPER_ADMIN_GID . ' AND block = 0');
  1201. return ($this->_db->loadResult());
  1202. }
  1203. /**
  1204. * Return a list of Joomla ACL groups.
  1205. *
  1206. * The returned object list includes a group anme and a group name with spaces
  1207. * prepended to the name for displaying an indented tree.
  1208. *
  1209. * @author RickG
  1210. * @return ObjectList List of acl group objects.
  1211. */
  1212. function getAclGroupIndentedTree()
  1213. {
  1214. //TODO check this out
  1215. if (JVM_VERSION===1) {
  1216. $name = 'name';
  1217. $as = '` AS `title`';
  1218. $table = '#__core_acl_aro_groups';
  1219. $and = 'AND `parent`.`lft` > 2 ';
  1220. }
  1221. else {
  1222. $name = 'title';
  1223. $as = '`';
  1224. $table = '#__usergroups';
  1225. $and = '';
  1226. }
  1227. //Ugly thing, produces Select_full_join
  1228. $query = 'SELECT `node`.`' . $name . $as . ', CONCAT(REPEAT("&nbsp;&nbsp;&nbsp;", (COUNT(`parent`.`' . $name . '`) - 1)), `node`.`' . $name . '`) AS `text` ';
  1229. $query .= 'FROM `' . $table . '` AS node, `' . $table . '` AS parent ';
  1230. $query .= 'WHERE `node`.`lft` BETWEEN `parent`.`lft` AND `parent`.`rgt` ';
  1231. $query .= $and;
  1232. $query .= 'GROUP BY `node`.`' . $name . '` ';
  1233. $query .= ' ORDER BY `node`.`lft`';
  1234. $this->_db->setQuery($query);
  1235. //$app = JFactory::getApplication();
  1236. //$app -> enqueueMessage($this->_db->getQuery());
  1237. $objlist = $this->_db->loadObjectList();
  1238. // vmdebug('getAclGroupIndentedTree',$objlist);
  1239. return $objlist;
  1240. }
  1241. }
  1242. //No Closing tag