Source for file selfregistration-defs.php

Documentation is available at selfregistration-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: reg-defs.php */
  22. /* Author: Mark Kessell */
  23. /* Description: Definitions for managing self registering USERS */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package user *//** Self-registration class.
  27. * Used to provide means for users to register themselves on an
  28. * Axyl-based website.
  29. * @package user
  30. */
  31. class user_self_registration extends HTMLObject {
  32. // this new class caters for user self registration to any site created using axyl.
  33. var $user_id;
  34. var $password;
  35. var $full_name;
  36. var $email;
  37. var $address;
  38. var $phone;
  39. var $mobile;
  40. var $enabled = 'f';
  41. var $auth_code;
  42. var $user_confirmation = 'f';
  43. var $error_msg;
  44. var $regmode;
  45. var $displaypw;
  46. var $savepw = 'f';
  47. var $forumfor = 'Axyl Default Site';
  48. var $forumemail = 'axyl@catalyst.net.nz';
  49. var $confirmsubject = 'Forums Confirmation of Registration';
  50.  
  51. function user_self_registration ($confirmation='f', $rmode='', $user='', $ff='', $fe='') {
  52. // main class contructor.
  53. if ( trim($confirmation) != '' )
  54. $this->user_confirmation = trim($confirmation);
  55.  
  56. if ( trim($ff) != "" )
  57. $this->forumfor = trim($ff);
  58.  
  59. if ( trim($fe) != "" )
  60. $this->forumemail = trim($fe);
  61.  
  62. // if no confirmation is required, account is enabled from start.
  63. if ( $this->user_confirmation == 'f' )
  64. $this->enabled = 't';
  65.  
  66. // set the mode for internal use.
  67. if ( trim($rmode) != '')
  68. $this->regmode = trim($rmode);
  69.  
  70. // post process
  71. $this->POSTprocess();
  72.  
  73. if ( trim($user) != "" ) {
  74. $this->user_id = trim($user);
  75. $this->get_user();
  76. }
  77. } // user_self_registration
  78.  
  79.  
  80. function get_user() {
  81. // gets the user so they can edit details
  82. if ( trim($this->auth_code) != "" ) {
  83. $q = "select * from ax_user where auth_code='$this->auth_code'";
  84. } else {
  85. $q = "select * from ax_user where user_id='$this->user_id'";
  86. }
  87. $Q = new dbrecords($q);
  88.  
  89. if ( $Q->hasdata ) {
  90. $this->user_id = $Q->field("user_id");
  91. $this->full_name = $Q->field("full_name");
  92. $this->email = $Q->field("email");
  93. $this->address = $Q->field("address");
  94. $this->phone = $Q->field("phone");
  95. $this->mobile = $Q->field("mobile");
  96. $this->auth_code = $Q->field("auth_code");
  97. $this->displaypw = $Q->field("password");
  98. $this->enabled = $Q->field("enabled");
  99.  
  100. return TRUE;
  101. }
  102.  
  103. return FALSE;
  104. } // get_user
  105.  
  106.  
  107. function save() {
  108. // save the record to the database
  109. if ( trim($this->auth_code) == "" ) {
  110. // it's an insert
  111. $query = new dbinsert("ax_user");
  112. $query->set("user_id", $this->user_id);
  113. $query->set("password", $this->password);
  114.  
  115. $seed = $this->user_id . $this->full_name . microtime();
  116. $this->auth_code = md5($seed);
  117. $query->set("full_name", $this->full_name);
  118. $query->set("address", $this->address);
  119. $query->set("email", $this->email);
  120. $query->set("phone", $this->phone);
  121. $query->set("mobile", $this->mobile);
  122. $query->set("auth_code", $this->auth_code);
  123.  
  124. $q = new dbrecords("select group_id from ax_group where group_desc='User'");
  125. $ug = new dbinsert("ax_user_group");
  126. $ug->set("group_id", $q->field("group_id"));
  127. $ug->set("user_id", $this->user_id);
  128.  
  129. $regins = 'true';
  130. } else {
  131. // it's an update
  132. $query = new dbupdate("ax_user");
  133. $query->where("user_id='$this->user_id'");
  134. $query->set("full_name", $this->full_name);
  135. $query->set("address", $this->address);
  136. $query->set("email", $this->email);
  137. $query->set("phone", $this->phone);
  138. $query->set("mobile", $this->mobile);
  139.  
  140. if ( $this->savepw == 't' )
  141. $query->set("password", $this->password);
  142. }
  143.  
  144. $query->set("enabled", $this->enabled);
  145.  
  146. begin_transaction();
  147. if ( $query->execute() ) {
  148. if ( $regins == "true" ) {
  149. // only execute the 2nd query if it's an insert.
  150. if ( $ug->execute() ) {
  151. $this->regmode = "confirm";
  152. commit();
  153. return TRUE;
  154. } else {
  155. $this->regmode = "newreg";
  156. $this->error_msg = "ERROR assigning User Group to User.";
  157. }
  158. } else {
  159. $this->regmode = "confirm";
  160. commit();
  161. return TRUE;
  162. }
  163. } else {
  164. $this->regmode = "newreg";
  165. $this->error_msg = "ERROR in Saving User Record.";
  166. }
  167.  
  168. rollback();
  169. return FALSE;
  170. } // save
  171.  
  172.  
  173. function send_confirmation_email() {
  174. // send the confirmation email
  175. global $SERVER_NAME, $SCRIPT_NAME;
  176. $subject = $this->forumfor . " " . $this->confirmsubject;
  177.  
  178. $body = "Please click on the link below to enable your registration to the ".$this->forumfor." Forums\n\n";
  179. $body .= "http://".$SERVER_NAME.$SCRIPT_NAME."?regmode=confirm&auth_code=$this->auth_code";
  180. $email = new email($this->forumemail,
  181. $this->email,
  182. $subject,
  183. $body);
  184.  
  185. $email->send();
  186. } // send confirmation_email
  187.  
  188.  
  189. function check_authcode($auth_code) {
  190. // if auth_code exists in database, enable user
  191. //$q = "select * from ax_user where auth_code='$auth_code'";
  192. //$Q = new dbrecords($q);
  193.  
  194. //if ( $Q->hasdata ) {
  195. // auth_code confirmed
  196. $this->auth_code = $auth_code;
  197. if ( $this->get_user() ) {
  198.  
  199. $this->enabled = 't';
  200.  
  201. return TRUE;
  202. }
  203.  
  204. return FALSE;
  205. } // check_authcode
  206.  
  207.  
  208. function html() {
  209. // this displays either the reg form or the appropriate messages.
  210. global $RESPONSE, $auth_code, $regmode, $SaveReg_x, $LIBDIR;
  211.  
  212. $s = "";
  213.  
  214. $T = new table("SelfRegistrationForm");
  215. $T->setborder(0);
  216. $T->setalign("center");
  217. $T->setwidth("80%");
  218.  
  219. switch ($this->regmode) {
  220. case "noauth":
  221. $T->tr();
  222. $T->td("It Seems that that Authorisation code doesn't exist in our database. Please click <a href=\"?regmode=newreg\" class=\"forumlinkgold\">HERE</a> to register to the $this->forumfor Forums.");
  223. break;
  224. case "confirm":
  225. if ( trim($auth_code) == "" ) {
  226. // means the save button was pressed
  227. if ( trim($this->user_confirmation) == 't' ) {
  228. // means the user has to confirm their use of the site
  229. $T->tr();
  230. $T->td("A Confirmation Email has been send to $this->email.<br>
  231. Please click the link in that email to Enable your account.");
  232. } else {
  233. // means the account is activated immediately
  234. $T->tr();
  235. $T->td("Your Account has been created and enabled. You may login immediately! :D.");
  236. }
  237. } else {
  238. // means someone is returning to activate their user account
  239. if ( isset($SaveReg_x) ) {
  240. $T->tr();
  241. $T->td("Registration Updated. Click <a href=\"\" class=\"forumlink\">HERE</a> to view the FORUMS.");
  242. } else {
  243. $T->tr();
  244. $T->td("$this->full_name, your account has now been Enabled. You can now login<br>
  245. and start using the forums. Thankyou for registering for the $this->forumfor forums.");
  246. }
  247. }
  248. break;
  249. case "editreg": // displays the form for editing the registration
  250. case "newreg": // displays the main registration form.
  251. debugbr("new registration form");
  252. $T->tr();
  253. $T->td("<b>Registration</b>", "forumheadings" );
  254. $T->td_alignment("center");
  255. $T->tr();
  256. $T->td("<hr>");
  257.  
  258. if ( trim($this->error_msg) != "" ) {
  259. $T->tr();
  260. $T->td("$this->error_msg", "formerror" );
  261. $T->td_alignment("center");
  262. $T->tr();
  263. $T->td("<hr>");
  264. }
  265. $F = new form("RegistrationForm");
  266.  
  267. if ( trim($this->regmode) == "editreg" ) {
  268. $userid = new form_labelfield("User id", $this->user_id);
  269. $uidh = new form_hiddenfield("user_id", $this->user_id);
  270. $F->add($uidh);
  271. } else {
  272. // user_id
  273. $userid = new form_textfield("user_id", "<font color=\"red\">User id</font>", $this->user_id);
  274. $userid->setstyle("width: 250");
  275. $userid->setclass("axtxtbox");
  276. }
  277.  
  278. // password
  279. if ( trim($this->regmode) == "editreg" ) {
  280. $password = new form_passwordfield("password", "Change Password", $this->password);
  281. } else {
  282. $password = new form_passwordfield("password", "<font color=\"red\">Password</font>", $this->password);
  283. }
  284. $password->setstyle("width: 250");
  285. $password->setclass("axtxtbox");
  286.  
  287. // cpassword
  288. if ( trim($this->regmode) == "editreg" ) {
  289. $cpassword = new form_passwordfield("cpassword", "Confirm Password");
  290. } else {
  291. $cpassword = new form_passwordfield("cpassword", "<font color=\"red\">Confirm Password</font>");
  292. }
  293. $cpassword->setstyle("width: 250");
  294. $cpassword->setclass("axtxtbox");
  295.  
  296. // full_name
  297. $fname = new form_textfield("full_name", "Full Name", $this->full_name);
  298. $fname->setstyle("width: 250");
  299. $fname->setclass("axtxtbox");
  300.  
  301. // email
  302. $email_addy = new form_textfield("email", "<font color=\"red\">Email Address</dont>", $this->email);
  303. $email_addy->setstyle("width: 250");
  304. $email_addy->setclass("axtxtbox");
  305.  
  306. // address
  307. $addy = new form_textfield("address", "Address", $this->address);
  308. $addy->setstyle("width: 250");
  309. $addy->setclass("axtxtbox");
  310.  
  311. // phone
  312. $ph = new form_textfield("phone", "Phone", $this->phone);
  313. $ph->setstyle("width: 250");
  314. $ph->setclass("axtxtbox");
  315.  
  316. // mobile
  317. $mob = new form_textfield("mobile", "Mobile", $this->mobile);
  318. $mob->setstyle("width: 250");
  319. $mob->setclass("axtxtbox");
  320.  
  321. // POST buttons
  322. $pb = new form_imagebutton("SaveReg", "SaveReg", "", "$LIBDIR/img/_save.gif", "", 57, 15);
  323. $cb = new form_imagebutton("CancelReg", "CancelReg", "", "$LIBDIR/img/_cancel.gif", "", 57, 15);
  324.  
  325. // hidden field(s)
  326. $rm = new form_hiddenfield("regmode", $this->regmode);
  327. $ac = new form_hiddenfield("auth_code", $this->auth_code);
  328.  
  329. // add the form objects
  330. $F->add($userid);
  331. // display password only
  332. if ( $this->regmode == "editreg" ) {
  333. $disppw = new form_labelfield("Password", $this->displaypw);
  334. $F->add($disppw);
  335. }
  336. $F->add($password);
  337. $F->add($cpassword);
  338. $F->add($fname);
  339. $F->add($email_addy);
  340. $F->add($addy);
  341. $F->add($ph);
  342. $F->add($mob);
  343. $F->add($pb);
  344. $F->add($cb);
  345. $F->add($rm);
  346. $F->add($ac);
  347.  
  348.  
  349. $T->tr();
  350. $T->td($F->render());
  351.  
  352. break;
  353. default: // this is the default display if the regmode isn't what is expected.
  354. $T->tr();
  355. $T->td("<font color=\"red\"><b>INVALID REGISTRATION MODE</b></font>");
  356. break;
  357. }
  358.  
  359. $s = $T->render();
  360. return $s;
  361. } // html
  362.  
  363.  
  364. function POSTprocess() {
  365. // perform all form stuffs
  366. global $RESPONSE, $phone, $mobile, $email, $address, $cpassword, $password;
  367. global $user_id, $full_name, $SaveReg_x, $auth_code, $CancelReg_x;
  368.  
  369.  
  370. if ( isset($SaveReg_x) ) {
  371. // save button was pressed
  372. debugbr("AUTH_CODE: $auth_code");
  373. $this->user_id = trim($user_id);
  374. $this->phone = trim($phone);
  375. $this->email = trim($email);
  376. $this->mobile = trim($mobile);
  377. $this->password = trim($password);
  378. $this->full_name = trim($full_name);
  379. $this->address = trim($address);
  380.  
  381. if ( trim($auth_code) == "" ) {
  382. if ( trim($user_id) != "" && $password == $cpassword && trim($password) != "" && trim($email) != "" &&
  383. eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $this->email)) {
  384. // the form requirements have been meet
  385. if ( $this->save() ) {
  386. debugbr("SENDING CONFIRMATION EMAIL!");
  387. $this->send_confirmation_email();
  388. $this->regmode = "confirm";
  389. }
  390. } else {
  391. // make the error
  392. $this->regmode = "newreg";
  393. $this->error_msg = "<ul>";
  394. if ( trim($user_id) == "" ) $this->error_msg .= "<li>User Id MUST Exist.</li>";
  395. if ( trim($email) == "" ) $this->error_msg .= "<li>Email MUST Exist.</li>";
  396. if ( !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $this->email) ) {
  397. $this->error_msg .= "<li>Invalid Email Address.</li>";
  398. }
  399. if ( trim($password) == "" ) $this->error_msg .= "<li>You MUST Have a Password.</li>";
  400. if ( $password != $cpassword ) $this->error_msg .= "<li>Your Confirmation Password MUST Match your Password.</li>";
  401. $this->error_msg .= "</ul>";
  402. }
  403. } else {
  404. debugbr("AUTH CODE HAS A VALUE!");
  405. $this->auth_code = $auth_code;
  406. $this->enabled = 't';
  407. if ( trim($password) == "" && trim($email) != "" &&
  408. eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $this->email)) {
  409. if ( $this->save() )
  410. $this->regmode = "confirm";
  411. } else {
  412. if ( $password != $cpassword || trim($email) == "" ||
  413. !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $this->email)) {
  414. // make the error
  415. $this->regmode = "editreg";
  416. $this->error_msg = "<ul>";
  417. if ( trim($email) == "" ) $this->error_msg .= "<li>Email MUST Exist.</li>";
  418. if ( $password != $cpassword ) $this->error_msg .= "<li>Your Confirmation Password MUST Match your Password.</li>";
  419. if ( !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $this->email) ) {
  420. $this->error_msg .= "<li>Invalid Email Address.</li>";
  421. }
  422. $this->error_msg .= "</ul>";
  423. } else {
  424. $this->savepw = 't';
  425. if ( $this->save() )
  426. $this->regmode = "confirm";
  427. }
  428. }
  429. }
  430. } else {
  431. debugbr("was the cancel button pressed: $CancelReg_x");
  432. if ( trim($auth_code) != "" && trim($CancelReg_x) == "" ) {
  433. debugbr("it's telling me that an authcode exists, and the cancel button WASN'T pressed.");
  434. // means that a confirmation email has been replied to
  435. // and th eauth_code has been passed back.
  436. if ( $this->check_authcode(trim($auth_code)) ) {
  437. debugbr("AUTHCODE CHECKS OUT!");
  438. // auth_code is authentic
  439. $this->enabled = 't';
  440. if ( !$this->save() ) {
  441. debugbr("DID NOT ENABLE THE USER!");
  442. $this->error_msg = "ERROR in Enabling your Account!";
  443. }
  444. } else {
  445. $this->regmode = "noauth";
  446. }
  447. }
  448. }
  449. } // POSTprocess
  450.  
  451. } // class user_self_registration
  452.  
  453. ?>

Documentation generated by phpDocumentor 1.3.0RC3