PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/packages/webusers/webuser.class.php

https://bitbucket.org/navigatecms/navigatecms
PHP | 890 lines | 731 code | 129 blank | 30 comment | 66 complexity | 1e1893e6589700f0c376222c111d91c7 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause, AGPL-3.0, Apache-2.0
  1. <?php
  2. class webuser
  3. {
  4. public $id;
  5. public $website;
  6. public $username;
  7. public $password;
  8. public $email;
  9. public $email_verification_date;
  10. public $groups;
  11. public $fullname;
  12. public $gender; // male / female / company / (empty)
  13. public $avatar;
  14. public $birthdate;
  15. public $language; // ISO 639-1 (2 chars) (en => English, es => EspaĂąol)
  16. public $country; // ISO-3166-1993 (US => United States of America, ES => Spain)
  17. public $timezone; // PHP5 Timezone Code (, "Europe/Madrid")
  18. public $address;
  19. public $zipcode;
  20. public $location;
  21. public $phone;
  22. public $social_website;
  23. public $joindate;
  24. public $lastseen;
  25. public $newsletter;
  26. public $private_comment;
  27. public $activation_key;
  28. public $cookie_hash;
  29. public $access; // 0: allowed, 1 => blocked, 2 => allowed within a date range
  30. public $access_begin; // timestamp, 0 => infinite
  31. public $access_end; // timestamp, 0 => infinite
  32. public $properties;
  33. public function load($id)
  34. {
  35. global $DB;
  36. if($DB->query('SELECT * FROM nv_webusers WHERE id = '.intval($id)))
  37. {
  38. $data = $DB->result();
  39. $this->load_from_resultset($data);
  40. }
  41. }
  42. public function load_by_hash($hash)
  43. {
  44. global $DB;
  45. global $session;
  46. global $events;
  47. $ok = $DB->query('SELECT * FROM nv_webusers WHERE cookie_hash = '.protect($hash));
  48. if($ok)
  49. $data = $DB->result();
  50. if(!empty($data))
  51. {
  52. $this->load_from_resultset($data);
  53. // check if the user is still allowed to sign in
  54. $blocked = 1;
  55. if( $this->access == 0 ||
  56. ( $this->access == 2 &&
  57. ($this->access_begin==0 || $this->access_begin < time()) &&
  58. ($this->access_end==0 || $this->access_end > time())
  59. )
  60. )
  61. {
  62. $blocked = 0;
  63. }
  64. if($blocked==1)
  65. return false;
  66. $session['webuser'] = $this->id;
  67. // maybe this function is called without initializing $events
  68. if(method_exists($events, 'trigger'))
  69. {
  70. $events->trigger(
  71. 'webuser',
  72. 'sign_in',
  73. array(
  74. 'webuser' => $this,
  75. 'by' => 'cookie'
  76. )
  77. );
  78. }
  79. }
  80. }
  81. public function load_by_profile($network, $network_user_id)
  82. {
  83. global $DB;
  84. global $session;
  85. // the profile exists (connected to a social network)?
  86. $swuser = $DB->query_single(
  87. 'webuser',
  88. 'nv_webuser_profiles',
  89. ' network = '.protect($network).' AND '.
  90. ' network_user_id = '.protect($network_user_id)
  91. );
  92. if(!empty($swuser))
  93. $this->load($swuser);
  94. }
  95. public function load_from_resultset($rs)
  96. {
  97. $main = $rs[0];
  98. $this->id = $main->id;
  99. $this->website = $main->website;
  100. $this->username = $main->username;
  101. $this->password = $main->password;
  102. $this->email = $main->email;
  103. $this->email_verification_date = $main->email_verification_date;
  104. $this->fullname = $main->fullname;
  105. $this->gender = $main->gender;
  106. $this->avatar = $main->avatar;
  107. $this->birthdate = $main->birthdate;
  108. $this->language = $main->language;
  109. $this->country = $main->country;
  110. $this->timezone = $main->timezone;
  111. $this->address = $main->address;
  112. $this->zipcode = $main->zipcode;
  113. $this->location = $main->location;
  114. $this->phone = $main->phone;
  115. $this->social_website = $main->social_website;
  116. $this->joindate = $main->joindate;
  117. $this->lastseen = $main->lastseen;
  118. $this->newsletter = $main->newsletter;
  119. $this->private_comment = $main->private_comment;
  120. $this->activation_key = $main->activation_key;
  121. $this->cookie_hash = $main->cookie_hash;
  122. $this->access = $main->access;
  123. $this->access_begin = $main->access_begin;
  124. $this->access_end = $main->access_end;
  125. // to get the array of groups first we remove the "g" character
  126. $groups = str_replace('g', '', $main->groups);
  127. $this->groups = explode(',', $groups);
  128. if(!is_array($this->groups)) $this->groups = array($groups);
  129. }
  130. public function load_from_post()
  131. {
  132. //$this->website = $_REQUEST['webuser-website'];
  133. $this->username = trim($_REQUEST['webuser-username']);
  134. if(!empty($_REQUEST['webuser-password']))
  135. $this->set_password($_REQUEST['webuser-password']);
  136. $this->email = $_REQUEST['webuser-email'];
  137. $this->groups = $_REQUEST['webuser-groups'];
  138. $this->fullname = $_REQUEST['webuser-fullname'];
  139. $this->gender = $_REQUEST['webuser-gender'][0];
  140. $this->avatar = $_REQUEST['webuser-avatar'];
  141. if(!empty($_REQUEST['webuser-birthdate']))
  142. $this->birthdate = core_date2ts($_REQUEST['webuser-birthdate']);
  143. else
  144. $this->birthdate = '';
  145. $this->language = $_REQUEST['webuser-language'];
  146. $this->newsletter = ($_REQUEST['webuser-newsletter']=='1'? '1' : '0');
  147. $this->access = $_REQUEST['webuser-access'];
  148. $this->access_begin = (empty($_REQUEST['webuser-access-begin'])? '' : core_date2ts($_REQUEST['webuser-access-begin']));
  149. $this->access_end = (empty($_REQUEST['webuser-access-end'])? '' : core_date2ts($_REQUEST['webuser-access-end']));
  150. $this->country = $_REQUEST['webuser-country'];
  151. $this->timezone = $_REQUEST['webuser-timezone'];
  152. $this->address = $_REQUEST['webuser-address'];
  153. $this->zipcode = $_REQUEST['webuser-zipcode'];
  154. $this->location = $_REQUEST['webuser-location'];
  155. $this->phone = $_REQUEST['webuser-phone'];
  156. $this->social_website = $_REQUEST['webuser-social_website'];
  157. $this->private_comment = $_REQUEST['webuser-private_comment'];
  158. // social profiles is a navigate cms private field
  159. }
  160. public function save($trigger_webuser_modified=true)
  161. {
  162. if(!empty($this->id))
  163. return $this->update($trigger_webuser_modified);
  164. else
  165. return $this->insert();
  166. }
  167. public function delete()
  168. {
  169. global $DB;
  170. global $events;
  171. if(!empty($this->id))
  172. {
  173. // remove all social profiles
  174. $DB->execute('
  175. DELETE FROM nv_webuser_profiles
  176. WHERE webuser = '.intval($this->id)
  177. );
  178. // remove properties
  179. property::remove_properties('webuser', $this->id);
  180. // remove grid notes
  181. grid_notes::remove_all('webuser', $this->id);
  182. // finally remove webuser account
  183. $DB->execute('
  184. DELETE FROM nv_webusers
  185. WHERE id = '.intval($this->id).'
  186. LIMIT 1 '
  187. );
  188. $events->trigger(
  189. 'webuser',
  190. 'delete',
  191. array(
  192. 'webuser' => $this
  193. )
  194. );
  195. }
  196. return $DB->get_affected_rows();
  197. }
  198. public function insert()
  199. {
  200. global $DB;
  201. global $website;
  202. global $events;
  203. $groups = '';
  204. if(is_array($this->groups))
  205. {
  206. $this->groups = array_unique($this->groups); // remove duplicates
  207. $this->groups = array_filter($this->groups); // remove empty
  208. if(!empty($this->groups))
  209. $groups = 'g'.implode(',g', $this->groups);
  210. }
  211. if($groups == 'g')
  212. $groups = '';
  213. $ok = $DB->execute('
  214. INSERT INTO nv_webusers
  215. ( id, website, username, password, email, groups, fullname, gender, avatar, birthdate,
  216. language, country, timezone, address, zipcode, location, phone, social_website,
  217. joindate, lastseen, newsletter, private_comment, activation_key, cookie_hash,
  218. access, access_begin, access_end, email_verification_date
  219. )
  220. VALUES
  221. (
  222. :id, :website, :username, :password, :email, :groups, :fullname, :gender, :avatar, :birthdate,
  223. :language, :country, :timezone, :address, :zipcode, :location, :phone, :social_website,
  224. :joindate, :lastseen, :newsletter, :private_comment, :activation_key, :cookie_hash,
  225. :access, :access_begin, :access_end, :email_verification_date
  226. )',
  227. array(
  228. ":id" => 0,
  229. ":website" => $website->id,
  230. ":username" => is_null($this->username)? '' : $this->username,
  231. ":password" => is_null($this->password)? '' : $this->password,
  232. ":email" => is_null($this->email)? '' : $this->email,
  233. ":groups" => $groups,
  234. ":fullname" => is_null($this->fullname)? '' : $this->fullname,
  235. ":gender" => is_null($this->gender)? '' : $this->gender,
  236. ":avatar" => is_null($this->avatar)? '' : $this->avatar,
  237. ":birthdate" => value_or_default($this->birthdate, 0),
  238. ":language" => is_null($this->language)? '' : $this->language,
  239. ":country" => is_null($this->country)? '' : $this->country,
  240. ":timezone" => is_null($this->timezone)? '' : $this->timezone,
  241. ":address" => is_null($this->address)? '' : $this->address,
  242. ":zipcode" => is_null($this->zipcode)? '' : $this->zipcode,
  243. ":location" => is_null($this->location)? '' : $this->location,
  244. ":phone" => is_null($this->phone)? '' : $this->phone,
  245. ":social_website" => is_null($this->social_website)? '' : $this->social_website,
  246. ":joindate" => core_time(),
  247. ":lastseen" => 0,
  248. ":newsletter" => is_null($this->newsletter)? '0' : $this->newsletter,
  249. ":private_comment" => is_null($this->private_comment)? '' : $this->private_comment,
  250. ":activation_key" => is_null($this->activation_key)? '' : $this->activation_key,
  251. ":cookie_hash" => is_null($this->cookie_hash)? '' : $this->cookie_hash,
  252. ":access" => value_or_default($this->access, 0),
  253. ":access_begin" => value_or_default($this->access_begin, 0),
  254. ":access_end" => value_or_default($this->access_end, 0),
  255. ":email_verification_date" => value_or_default($this->email_verification_date, 0)
  256. )
  257. );
  258. if(!$ok)
  259. throw new Exception($DB->get_last_error());
  260. $this->id = $DB->get_last_id();
  261. $events->trigger(
  262. 'webuser',
  263. 'save',
  264. array(
  265. 'webuser' => $this
  266. )
  267. );
  268. $this->new_webuser_notification();
  269. return true;
  270. }
  271. public function update($trigger_webuser_modified=true)
  272. {
  273. global $DB;
  274. global $events;
  275. $groups = '';
  276. if(is_array($this->groups))
  277. {
  278. $this->groups = array_unique($this->groups); // remove duplicates
  279. $this->groups = array_filter($this->groups); // remove empty
  280. if(!empty($this->groups))
  281. $groups = 'g'.implode(',g', $this->groups);
  282. }
  283. if($groups == 'g')
  284. $groups = '';
  285. $ok = $DB->execute('
  286. UPDATE nv_webusers
  287. SET
  288. website = :website,
  289. username = :username,
  290. password = :password,
  291. email = :email,
  292. groups = :groups,
  293. fullname = :fullname,
  294. gender = :gender,
  295. avatar = :avatar,
  296. birthdate = :birthdate,
  297. language = :language,
  298. lastseen = :lastseen,
  299. country = :country,
  300. timezone = :timezone,
  301. address = :address,
  302. zipcode = :zipcode,
  303. location = :location,
  304. phone = :phone,
  305. social_website = :social_website,
  306. newsletter = :newsletter,
  307. private_comment = :private_comment,
  308. activation_key = :activation_key,
  309. cookie_hash = :cookie_hash,
  310. access = :access,
  311. access_begin = :access_begin,
  312. access_end = :access_end,
  313. email_verification_date = :email_verification_date
  314. WHERE id = :id
  315. ',
  316. array(
  317. ':website' => $this->website,
  318. ':username' => $this->username,
  319. ':password' => $this->password,
  320. ':email' => $this->email,
  321. ':groups' => $groups,
  322. ':fullname' => $this->fullname,
  323. ':gender' => value_or_default($this->gender, ""),
  324. ':avatar' => $this->avatar,
  325. ':birthdate' => value_or_default($this->birthdate, 0),
  326. ':language' => value_or_default($this->language, ""),
  327. ':lastseen' => $this->lastseen,
  328. ':country' => $this->country,
  329. ':timezone' => $this->timezone,
  330. ':address' => $this->address,
  331. ':zipcode' => $this->zipcode,
  332. ':location' => $this->location,
  333. ':phone' => $this->phone,
  334. ':social_website' => value_or_default($this->social_website, ""),
  335. ':newsletter' => value_or_default($this->newsletter, 0),
  336. ':private_comment' => value_or_default($this->private_comment, ""),
  337. ':activation_key' => value_or_default($this->activation_key, ""),
  338. ':cookie_hash' => value_or_default($this->cookie_hash, ""),
  339. ":access" => value_or_default($this->access, 0),
  340. ":access_begin" => value_or_default($this->access_begin, 0),
  341. ":access_end" => value_or_default($this->access_end, 0),
  342. ':id' => $this->id,
  343. ':email_verification_date' => value_or_default($this->email_verification_date, 0)
  344. )
  345. );
  346. if(!$ok) throw new Exception($DB->get_last_error());
  347. if($trigger_webuser_modified)
  348. {
  349. $events->trigger(
  350. 'webuser',
  351. 'save',
  352. array(
  353. 'webuser' => $this
  354. )
  355. );
  356. }
  357. return true;
  358. }
  359. public function access_allowed()
  360. {
  361. // check if the user is still allowed to sign in
  362. if( $this->access == 0 ||
  363. ( $this->access == 2 &&
  364. ($this->access_begin==0 || $this->access_begin < time()) &&
  365. ($this->access_end==0 || $this->access_end > time())
  366. )
  367. )
  368. {
  369. return true;
  370. }
  371. return false;
  372. }
  373. public function authenticate($website, $username, $password)
  374. {
  375. global $DB;
  376. global $events;
  377. $username = trim($username);
  378. $username = mb_strtolower($username);
  379. $A1 = md5($username.':'.APP_REALM.':'.$password);
  380. $website_check = '';
  381. if($website > 0)
  382. $website_check = 'AND website = '.protect($website);
  383. if($DB->query('SELECT *
  384. FROM nv_webusers
  385. WHERE ( access = 0 OR
  386. (access = 2 AND
  387. (access_begin = 0 OR access_begin < '.time().') AND
  388. (access_end = 0 OR access_end > '.time().')
  389. )
  390. )
  391. '.$website_check.'
  392. AND LOWER(username) = '.protect($username))
  393. )
  394. {
  395. $data = $DB->result();
  396. if(!empty($data))
  397. {
  398. if($data[0]->password==$A1)
  399. {
  400. $this->load_from_resultset($data);
  401. // maybe this function is called without initializing $events
  402. if(method_exists($events, 'trigger'))
  403. {
  404. $events->trigger(
  405. 'webuser',
  406. 'sign_in',
  407. array(
  408. 'webuser' => $this,
  409. 'by' => 'authenticate'
  410. )
  411. );
  412. }
  413. return true;
  414. }
  415. }
  416. }
  417. return false;
  418. }
  419. public function check_password($password)
  420. {
  421. $match = ($this->password == md5(mb_strtolower($this->username).':'.APP_REALM.':'.$password));
  422. return $match;
  423. }
  424. public function set_password($newpass)
  425. {
  426. $this->password = md5(mb_strtolower($this->username).':'.APP_REALM.':'.$newpass);
  427. }
  428. public function set_cookie()
  429. {
  430. global $session;
  431. $session['webuser'] = $this->id;
  432. $this->cookie_hash = sha1(rand(1, 9999999));
  433. $this->update();
  434. setcookie('webuser', $this->cookie_hash, time()+60*60*24*365, '/', substr($_SERVER['SERVER_NAME'], strpos($_SERVER['SERVER_NAME'], "."))); // 365 days
  435. }
  436. public static function unset_cookie()
  437. {
  438. global $session;
  439. global $events;
  440. $webuser_sign_out_id = $session['webuser'];
  441. $session['webuser'] = '';
  442. setcookie('webuser', NULL, -1, '/', substr($_SERVER['SERVER_NAME'], strpos($_SERVER['SERVER_NAME'], ".")));
  443. if(method_exists($events, 'trigger'))
  444. {
  445. $events->trigger(
  446. 'webuser',
  447. 'sign_out',
  448. array(
  449. 'webuser_id' => $webuser_sign_out_id
  450. )
  451. );
  452. }
  453. }
  454. public static function email_verification($email, $hash)
  455. {
  456. global $DB;
  457. $status = false;
  458. $DB->query('
  459. SELECT id, activation_key
  460. FROM nv_webusers
  461. WHERE email = '.protect($email).'
  462. AND activation_key = '.protect($hash).'
  463. ');
  464. $rs = $DB->first();
  465. if(!empty($rs->id))
  466. {
  467. $wu = new webuser();
  468. $wu->load($rs->id);
  469. // access is only enabled for blocked users (access==1) which don't have a password nor an email verification date
  470. if($wu->access==1 && empty($wu->password) && empty($wu->email_verification_date))
  471. {
  472. // email is confirmed through a newsletter subscribe request
  473. $wu->email_verification_date = time();
  474. $wu->access = 0;
  475. $wu->activation_key = "";
  476. $status = $wu->save();
  477. }
  478. }
  479. return $status;
  480. }
  481. public function quicksearch($text)
  482. {
  483. $like = ' LIKE '.protect('%'.$text.'%');
  484. $cols[] = 'id' . $like;
  485. $cols[] = 'LOWER(username)' . mb_strtolower($like);
  486. $cols[] = 'email' . $like;
  487. $cols[] = 'fullname' . $like;
  488. $where = ' AND ( ';
  489. $where.= implode( ' OR ', $cols);
  490. $where .= ')';
  491. return $where;
  492. }
  493. public static function social_network_profile_update($network, $network_user_id, $extra='', $data=array())
  494. {
  495. global $DB;
  496. global $webuser;
  497. global $website;
  498. $already_updated = false;
  499. if(is_array($extra))
  500. $extra = serialize($extra);
  501. // the profile exists?
  502. $swuser = $DB->query_single(
  503. 'webuser',
  504. 'nv_webuser_profiles',
  505. ' network = '.protect($network).' AND '.
  506. ' network_user_id = '.protect($network_user_id)
  507. );
  508. // the webuser already exists or is logged in?
  509. $wuser = new webuser();
  510. if(!empty($webuser->id))
  511. {
  512. // an existing webuser is already signed in, but we don't have his/her social profile
  513. if(empty($swuser))
  514. {
  515. $DB->execute('
  516. INSERT nv_webuser_profiles
  517. (id, network, network_user_id, webuser, extra)
  518. VALUES
  519. ( 0, :network, :network_user_id, :webuser, :extra )',
  520. array(
  521. 'network' => $network,
  522. 'network_user_id' => $network_user_id,
  523. 'webuser' => $webuser->id,
  524. 'extra' => $extra
  525. )
  526. );
  527. }
  528. $wuser->load($webuser->id);
  529. }
  530. else
  531. {
  532. // there is no webuser logged in, it's a new user!
  533. if(empty($swuser))
  534. {
  535. // and we don't have any social profile that matches the one used to sign in
  536. // Example: signed in with Facebook without having a previous webuser account in the current website
  537. $wuser->website = $website->id;
  538. $wuser->joindate = core_time();
  539. $wuser->lastseen = core_time();
  540. $wuser->access = 0;
  541. foreach ($data as $field => $value)
  542. { $wuser->$field = $value; }
  543. $already_updated = true;
  544. $wuser->insert();
  545. $DB->execute('
  546. INSERT nv_webuser_profiles
  547. (id, network, network_user_id, webuser, extra)
  548. VALUES
  549. ( 0, :network, :network_user_id, :webuser, :extra )',
  550. array(
  551. 'network' => $network,
  552. 'network_user_id' => $network_user_id,
  553. 'webuser' => $wuser->id,
  554. 'extra' => $extra
  555. )
  556. );
  557. }
  558. else
  559. {
  560. // BUT we have a social profile matching a previous webuser in database
  561. // Ex. Signed in with Facebook having a webuser account previously
  562. $wuser->load($swuser);
  563. }
  564. }
  565. if(!$already_updated)
  566. {
  567. // either way, now we have a webuser account that we need to update
  568. foreach ($data as $field => $value)
  569. $wuser->$field = $value;
  570. $wuser->update();
  571. }
  572. return $wuser->id;
  573. }
  574. public static function available($username, $website_id)
  575. {
  576. global $DB;
  577. // remove spaces and make username lowercase (only to compare case insensitive)
  578. $username = trim($username);
  579. $username = mb_strtolower($username);
  580. $data = NULL;
  581. if($DB->query('SELECT COUNT(*) as total
  582. FROM nv_webusers
  583. WHERE LOWER(username) = '.protect($username).'
  584. AND website = '.$website_id))
  585. {
  586. $data = $DB->first();
  587. }
  588. return ($data->total <= 0);
  589. }
  590. public function property($property_name, $raw=false)
  591. {
  592. global $theme;
  593. // load properties if not already done
  594. if(empty($this->properties))
  595. $this->properties = property::load_properties('webuser', $theme->name, 'webuser', $this->id);
  596. for($p=0; $p < count($this->properties); $p++)
  597. {
  598. if($this->properties[$p]->name==$property_name || $this->properties[$p]->id==$property_name)
  599. {
  600. if($raw)
  601. $out = $this->properties[$p]->value;
  602. else
  603. $out = $this->properties[$p]->value;
  604. break;
  605. }
  606. }
  607. return $out;
  608. }
  609. public function property_definition($property_name)
  610. {
  611. global $theme;
  612. // load properties if not already done
  613. if(empty($this->properties))
  614. $this->properties = property::load_properties('webuser', $theme->name, 'webuser', $this->id);
  615. for($p=0; $p < count($this->properties); $p++)
  616. {
  617. if($this->properties[$p]->name==$property_name || $this->properties[$p]->id==$property_name)
  618. {
  619. $out = $this->properties[$p];
  620. break;
  621. }
  622. }
  623. return $out;
  624. }
  625. public function property_exists($property_name)
  626. {
  627. global $theme;
  628. // load properties if not already done
  629. if(empty($this->properties))
  630. $this->properties = property::load_properties('webuser', $theme->name, 'webuser', $this->id);
  631. for($p=0; $p < count($this->properties); $p++)
  632. {
  633. if($this->properties[$p]->name==$property_name || $this->properties[$p]->id==$property_name)
  634. return true;
  635. }
  636. return false;
  637. }
  638. public function new_webuser_notification()
  639. {
  640. global $website;
  641. // notify about the new webuser account,
  642. // only if the current user is not logged in Navigate CMS
  643. if (empty($_SESSION['APP_USER#' . APP_UNIQUE]))
  644. {
  645. $subject = $website->name . ' | ' . t(661, 'New web user signed up') . ' [' . $this->username . ']';
  646. $body = navigate_compose_email(
  647. array(
  648. array(
  649. 'title' => t(177, "Website"),
  650. 'content' => '<a href="' . $website->absolute_path() . $website->homepage() . '">' . $website->name . '</a>'
  651. ),
  652. array(
  653. 'title' => "ID (".t(647,"Webuser").")",
  654. 'content' => $this->id
  655. ),
  656. array(
  657. 'title' => t(1, "User"),
  658. 'content' => value_or_default($this->username, "&nbsp;")
  659. ),
  660. array(
  661. 'title' => t(44, "E-Mail"),
  662. 'content' => value_or_default($this->email, "&nbsp;")
  663. ),
  664. array(
  665. 'title' => t(159, "Name"),
  666. 'content' => value_or_default($this->fullname, "&nbsp;")
  667. ),
  668. array(
  669. 'title' => t(249, "Newsletter"),
  670. 'content' => $this->newsletter ? "&#x2714;" : "&mdash;"
  671. ),
  672. array(
  673. 'footer' => '<a href="' . NAVIGATE_URL . '?fid=webusers&act=edit&id=' . $this->id . '">' .
  674. t(170, 'Edit') .
  675. '</a>'
  676. )
  677. )
  678. );
  679. navigate_send_email($subject, $body);
  680. }
  681. }
  682. public static function export($type='csv')
  683. {
  684. global $DB;
  685. global $website;
  686. $out = array();
  687. $DB->query('
  688. SELECT id, website, username, email, groups, fullname, gender,
  689. '/*avatar,*/.'
  690. birthdate, language, country, timezone,
  691. address, zipcode, location, phone, social_website,
  692. joindate, lastseen, newsletter, private_comment,
  693. access, access_begin, access_end
  694. FROM nv_webusers
  695. WHERE website = '.protect($website->id), 'array');
  696. $fields = array(
  697. "id",
  698. t(177, 'Website').' [NV]',
  699. t(1, 'User'),
  700. t(44, 'E-Mail'),
  701. t(506, 'Groups'),
  702. t(159, 'Name'),
  703. t(304, 'Gender'),
  704. //(246, 'Avatar'),
  705. t(248, 'Birthdate'),
  706. t(46, 'Language'),
  707. t(224, 'Country'),
  708. t(97, 'Timezone'),
  709. t(233, 'Address'),
  710. t(318, 'Zip code'),
  711. t(319, 'Location'),
  712. t(320, 'Phone'),
  713. t(177, 'Website'),
  714. t(247, 'Date joined'),
  715. t(563, 'Last seen'),
  716. t(249, 'Newsletter'),
  717. t(538, 'Private comment'),
  718. t(364, 'Access'),
  719. t(364, 'Access').' / '.t(623, 'Begin'),
  720. t(364, 'Access').' / '.t(624, 'End')
  721. );
  722. $out = $DB->result();
  723. $temp_file = tempnam("", 'nv_');
  724. $fp = fopen($temp_file, 'w');
  725. fputcsv($fp, $fields);
  726. foreach ($out as $fields)
  727. fputcsv($fp, $fields);
  728. header('Content-Description: File Transfer');
  729. header('Content-Type: text/csv');
  730. header('Content-Disposition: attachment; filename='.basename('webusers.csv'));
  731. header('Expires: 0');
  732. header('Cache-Control: must-revalidate');
  733. header('Pragma: public');
  734. header('Content-Length: ' . filesize($temp_file));
  735. ob_clean();
  736. flush();
  737. fclose($fp);
  738. readfile($temp_file);
  739. @unlink($temp_file);
  740. core_terminate();
  741. }
  742. public function backup($type='json')
  743. {
  744. global $DB;
  745. global $website;
  746. $out = array();
  747. $DB->query('SELECT * FROM nv_webusers WHERE website = '.protect($website->id), 'object');
  748. if($type='json')
  749. $out['nv_webusers'] = json_encode($DB->result());
  750. $DB->query('SELECT nwp.* FROM nv_webuser_profiles nwp, nv_webusers nw
  751. WHERE nwp.webuser = nw.id
  752. AND nw.website = '.protect($website->id),
  753. 'object');
  754. if($type='json')
  755. $out['nv_webuser_profiles'] = json_encode($DB->result());
  756. if($type='json')
  757. $out = json_encode($out);
  758. return $out;
  759. }
  760. }
  761. ?>