Advertisement
Guest User

user extended class

a guest
Aug 16th, 2010
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.16 KB | None | 0 0
  1. <?php
  2. /*
  3. + ----------------------------------------------------------------------------+
  4. | e107 website system
  5. |
  6. | ©Steve Dunstan 2001-2002
  7. | http://e107.org
  8. | jalist@e107.org
  9. |
  10. | Released under the terms and conditions of the
  11. | GNU General Public License (http://gnu.org).
  12. |
  13. | $Source: /cvs_backup/e107_0.7/e107_handlers/user_extended_class.php,v $
  14. | $Revision: 11346 $
  15. | $Date: 2010-02-17 13:56:14 -0500 (Wed, 17 Feb 2010) $
  16. | $Author: secretr $
  17. +----------------------------------------------------------------------------+
  18. */
  19.  
  20. if (!defined('e107_INIT')) { exit; }
  21.  
  22. /*
  23.  
  24. User_extended rewrite for version 0.7
  25.  
  26. this code uses two tables,
  27. user_extended
  28. user_extended_struct
  29. to store its data and structural information.
  30.  
  31. */
  32.  
  33. include_lan(e_LANGUAGEDIR.e_LANGUAGE."/lan_user_extended.php");
  34.  
  35. class e107_user_extended
  36. {
  37. var $user_extended_types;
  38. var $extended_xml;
  39. var $typeArray;
  40. var $reserved_names;
  41.  
  42. function e107_user_extended()
  43. {
  44. // 1 = text
  45. // 2 = radio
  46. // 3 = dropdown
  47. // 4 = db field
  48. // 5 = textarea
  49. // 6 = integer
  50. // 7 = date
  51. // 8 = language
  52. // 9 = checkbox
  53.  
  54. $this->typeArray = array(
  55. 'text' => 1,
  56. 'radio' => 2,
  57. 'dropdown' => 3,
  58. 'db field' => 4,
  59. 'textarea' => 5,
  60. 'integer' => 6,
  61. 'date' => 7,
  62. 'language' => 8,
  63. 'checkbox' => 9
  64. );
  65. $this->user_extended_types = array(
  66. 1 => UE_LAN_1,
  67. 2 => UE_LAN_2,
  68. 3 => UE_LAN_3,
  69. 4 => UE_LAN_4,
  70. 5 => UE_LAN_5,
  71. 6 => UE_LAN_6,
  72. 7 => UE_LAN_7,
  73. 8 => UE_LAN_8,
  74. 9 => UE_LAN_12
  75. );
  76.  
  77. //load array with field names from main user table, so we can disallow these
  78. $this->reserved_names = array (
  79. 'id', 'name', 'loginname', 'customtitle', 'password',
  80. 'sess', 'email', 'signature', 'image', 'timezone', 'hideemail',
  81. 'join', 'lastvisit', 'currentvisit', 'lastpost', 'chats',
  82. 'comments', 'forums', 'ip', 'ban', 'prefs', 'new', 'viewed',
  83. 'visits', 'admin', 'login', 'class', 'perms', 'realm', 'pwchange',
  84. 'xup'
  85. );
  86.  
  87. }
  88.  
  89. function user_extended_reserved($name)
  90. {
  91. return (in_array($name, $this->reserved_names));
  92. }
  93.  
  94.  
  95. // $val is whatever the user entered.
  96. // $params is the field definition
  97. // Return FALSE if acceptable, TRUE if fail , error message on regex fail if the message is defined
  98. function user_extended_validate_entry($val, $params)
  99. {
  100. global $tp;
  101. $parms = explode("^,^", $params['user_extended_struct_parms']);
  102. $requiredField = $params['user_extended_struct_required'] == 1;
  103. $regex = $tp->toText($parms[1]);
  104. $regexfail = $tp->toText($parms[2]);
  105. if (defined($regexfail)) { $regexfail = constant($regexfail); }
  106. if($val == '' && $requiredField) return TRUE;
  107. switch ($type)
  108. {
  109. case 7 :
  110. if ($requiredField && ($val == '0000-00-00')) return TRUE;
  111. break;
  112. }
  113. if($regex != "" && $val != "")
  114. {
  115. if(!preg_match($regex, $val)) return $regexfail ? $regexfail : TRUE;
  116. }
  117. return FALSE; // Pass by default here
  118. }
  119.  
  120.  
  121.  
  122. function user_extended_get_categories($byID = TRUE)
  123. {
  124. $ret = array();
  125. global $sql;
  126. if($sql->db_Select("user_extended_struct", "*", "user_extended_struct_type = 0 ORDER BY user_extended_struct_order ASC"))
  127. {
  128. if($byID == TRUE)
  129. {
  130. while($row = $sql->db_Fetch())
  131. {
  132. $ret[$row['user_extended_struct_id']][] = $row;
  133. }
  134. }
  135. else
  136. {
  137. $ret = $sql->db_getList();
  138. }
  139. }
  140. return $ret;
  141. }
  142.  
  143.  
  144. // Get the definition of all fields, or those in a specific category, grouped by category ID
  145. function user_extended_get_fields($cat = "")
  146. {
  147. global $sql;
  148. $ret = array();
  149. $more = ($cat) ? " AND user_extended_struct_parent = ".intval($cat)." " : "";
  150. if($sql->db_Select("user_extended_struct", "*", "user_extended_struct_type > 0 {$more} ORDER BY user_extended_struct_order ASC"))
  151. {
  152. while($row = $sql->db_Fetch())
  153. {
  154. $ret[$row['user_extended_struct_parent']][] = $row;
  155. }
  156. }
  157. return $ret;
  158. }
  159.  
  160. // Get the definition of all fields, or those in a specific category, indexed by field ID
  161. function user_extended_get_fieldList($cat = "")
  162. {
  163. global $sql;
  164. $more = ($cat != '') ? " AND user_extended_struct_parent = ".intval($cat)." " : "";
  165. if($sql->db_Select("user_extended_struct", "*", "user_extended_struct_type > 0 {$more} ORDER BY user_extended_struct_order ASC"))
  166. {
  167. while($row = $sql->db_Fetch())
  168. {
  169. $ret[$row['user_extended_struct_id']] = $row;
  170. }
  171. }
  172. return $ret;
  173. }
  174.  
  175.  
  176. function user_extended_type_text($type, $default)
  177. {
  178. global $tp;
  179. switch ($type)
  180. {
  181.  
  182. case 6:
  183. // integer,
  184. $db_type = 'INT(11)';
  185. break;
  186.  
  187. case 7:
  188. // date,
  189. $db_type = 'DATE NOT NULL';
  190. break;
  191.  
  192. case 1:
  193. case 2:
  194. case 3:
  195. case 4:
  196. case 8:
  197. case 9:
  198. //text, dropdown, radio, db_field, language, checkbox
  199. $db_type = 'VARCHAR(255)';
  200. break;
  201.  
  202. case 5:
  203. //textarea
  204. $db_type = 'TEXT';
  205. break;
  206. }
  207. if($type != 4 && $default != '')
  208. {
  209. $default_text = " DEFAULT '".$tp -> toDB($default, true)."'";
  210. }
  211. else
  212. {
  213. $default_text = '';
  214. }
  215. return $db_type.$default_text;
  216. }
  217.  
  218.  
  219. function user_extended_field_exist($name)
  220. {
  221. global $sql, $tp;
  222. return $sql->db_Count('user_extended_struct','(*)', "WHERE user_extended_struct_name = '".$tp -> toDB($name, true)."'");
  223. }
  224.  
  225. function user_extended_add($name, $text, $type, $parms, $values, $default, $required, $read, $write, $applicable, $order='', $parent=0)
  226. {
  227. global $sql, $tp;
  228. if(is_array($name))
  229. {
  230. extract($name);
  231. }
  232. if(!is_numeric($type))
  233. {
  234. $type = $this->typeArray[$type];
  235. }
  236.  
  237. if (!$this->user_extended_field_exist($name) && !$this->user_extended_reserved($name))
  238. {
  239. $field_info = $this->user_extended_type_text($type, $default);
  240. if($order === '')
  241. {
  242. if($sql->db_Select("user_extended_struct","MAX(user_extended_struct_order) as maxorder","1"))
  243. {
  244. $row = $sql->db_Fetch();
  245. if(is_numeric($row['maxorder']))
  246. {
  247. $order = $row['maxorder']+1;
  248. }
  249. }
  250. }
  251. $sql->db_Select_gen("ALTER TABLE #user_extended ADD user_".$tp -> toDB($name, true)." ".$field_info);
  252. $sql->db_Insert("user_extended_struct","0,'".$tp -> toDB($name, true)."','".$tp -> toDB($text, true)."','".intval($type)."','".$tp -> toDB($parms, true)."','".$tp -> toDB($values, true)."', '".$tp -> toDB($default, true)."', '".intval($read)."', '".intval($write)."', '".intval($required)."', '0', '".intval($applicable)."', '".intval($order)."', '".intval($parent)."'");
  253. if ($this->user_extended_field_exist($name))
  254. {
  255. return TRUE;
  256. }
  257. }
  258. return FALSE;
  259. }
  260.  
  261.  
  262.  
  263. function user_extended_modify($id, $name, $text, $type, $parms, $values, $default, $required, $read, $write, $applicable, $parent)
  264. {
  265. global $sql, $tp;
  266. if ($this->user_extended_field_exist($name))
  267. {
  268. $field_info = $this->user_extended_type_text($type, $default);
  269. $sql->db_Select_gen("ALTER TABLE #user_extended MODIFY user_".$tp -> toDB($name, true)." ".$field_info);
  270. $newfield_info = "
  271. user_extended_struct_text = '".$tp -> toDB($text, true)."',
  272. user_extended_struct_type = '".intval($type)."',
  273. user_extended_struct_parms = '".$tp -> toDB($parms, true)."',
  274. user_extended_struct_values = '".$tp -> toDB($values, true)."',
  275. user_extended_struct_default = '".$tp -> toDB($default, true)."',
  276. user_extended_struct_required = '".intval($required)."',
  277. user_extended_struct_read = '".intval($read)."',
  278. user_extended_struct_write = '".intval($write)."',
  279. user_extended_struct_applicable = '".intval($applicable)."',
  280. user_extended_struct_parent = '".intval($parent)."'
  281. WHERE user_extended_struct_id = '".intval($id)."'
  282. ";
  283. return $sql->db_Update("user_extended_struct", $newfield_info);
  284. }
  285. }
  286.  
  287. function user_extended_remove($id, $name)
  288. {
  289. global $sql, $tp;
  290. if ($this->user_extended_field_exist($name))
  291. {
  292. $sql->db_Select_gen("ALTER TABLE #user_extended DROP user_".$tp -> toDB($name, true));
  293. if(is_numeric($id))
  294. {
  295. $sql->db_Delete("user_extended_struct", "user_extended_struct_id = '".intval($id)."' ");
  296. }
  297. else
  298. {
  299. $sql->db_Delete("user_extended_struct", "user_extended_struct_name = '".$tp -> toDB($id, true)."' ");
  300. }
  301. return !($this->user_extended_field_exist($name));
  302. }
  303. }
  304.  
  305. function user_extended_hide($struct, $curval)
  306. {
  307. $chk = ($curval) ? " checked='checked' " : "";
  308. $name = "hide[user_".$struct['user_extended_struct_name']."]";
  309. return "<input type='checkbox' {$chk} value='1' name='{$name}' />&nbsp;".UE_LAN_HIDE;
  310. }
  311.  
  312.  
  313.  
  314. function user_extended_edit($struct, $curval)
  315. {
  316. global $cal, $tp;
  317. if(trim($curval) == "" && $struct['user_extended_struct_default'] != "")
  318. {
  319. $curval = $struct['user_extended_struct_default'];
  320. }
  321. $choices = explode(",",$struct['user_extended_struct_values']);
  322. foreach($choices as $k => $v)
  323. {
  324. $choices[$k] = str_replace("[E_COMMA]", ",", $choices[$k]);
  325. }
  326. $parms = explode("^,^",$struct['user_extended_struct_parms']);
  327. $include = preg_replace("/\n/", " ", $tp->toHtml($parms[0]));
  328. $regex = $tp->toText($parms[1]);
  329. $regexfail = $tp->toText($parms[2]);
  330. $fname = "ue[user_".$struct['user_extended_struct_name']."]";
  331. if(strpos($include, 'class') === FALSE)
  332. {
  333. $include .= " class='tbox' ";
  334. }
  335.  
  336. switch($struct['user_extended_struct_type'])
  337. {
  338. case 1: //textbox
  339. case 6: //integer
  340. $ret = "<input name='{$fname}' value='{$curval}' {$include} />";
  341. return $ret;
  342. break;
  343.  
  344. case 9: //Checkboxes
  345. foreach($choices as $choice)
  346. {
  347. $choice = trim($choice);
  348. $chk = ($curval >= $choice)? " checked='checked' " : "";
  349. $ret .= "<input {$include} type='checkbox' name='{$fname}' value='{$choice}' {$chk} /> {$choice}";
  350. }
  351. return $ret;
  352. break;
  353.  
  354. case 2: //radio
  355. foreach($choices as $choice)
  356. {
  357. $choice = trim($choice);
  358. $chk = ($curval = $choice)? " checked='checked' " : "";
  359. $ret .= "<input {$include} type='radio' name='{$fname}' value='{$choice}' {$chk} /> {$choice}";
  360. }
  361. return $ret;
  362. break;
  363.  
  364. case 3: //dropdown
  365. $ret = "<select {$include} name='{$fname}'>\n";
  366. $ret .= "<option value=''>&nbsp;</option>\n"; // ensures that the user chose it.
  367. foreach($choices as $choice)
  368. {
  369. $choice = trim($choice);
  370. $sel = ($curval == $choice) ? " selected='selected' " : "";
  371. $ret .= "<option value='{$choice}' {$sel}>{$choice}</option>\n";
  372. }
  373. $ret .= "</select>\n";
  374. return $ret;
  375. break;
  376.  
  377. case 4: //db_field
  378. global $sql;
  379. $order = ($choices[3]) ? "ORDER BY ".$tp -> toDB($choices[3], true) : "";
  380.  
  381. if($sql->db_Select($tp -> toDB($choices[0], true), $tp -> toDB($choices[1], true).",".$tp -> toDB($choices[2], true), "1 $order")){
  382. $choiceList = $sql->db_getList('ALL',FALSE);
  383. $ret = "<select {$include} name='{$fname}' >\n";
  384. $ret .= "<option value=''>&nbsp;</option>\n"; // ensures that the user chose it.
  385. foreach($choiceList as $cArray)
  386. {
  387. $cID = trim($cArray[$choices[1]]);
  388. $cText = trim($cArray[$choices[2]]);
  389. $sel = ($curval == $cID) ? " selected='selected' " : "";
  390. $ret .= "<option value='{$cID}' {$sel}>{$cText}</option>\n";
  391. }
  392. $ret .= "</select>\n";
  393. return $ret;
  394. } else {
  395. return "";
  396. }
  397. break;
  398.  
  399. case 5: //textarea
  400. return "<textarea {$include} name='{$fname}' >{$curval}</textarea>";
  401. break;
  402.  
  403. case 7: //date
  404. return $cal->make_input_field(
  405. array(
  406. 'ifFormat' => '%Y-%m-%d'
  407. ),
  408. array(
  409. 'class' => 'tbox',
  410. 'name' => $fname,
  411. 'value' => $curval
  412. )
  413. );
  414. break;
  415.  
  416. case 8: // language
  417. require_once(e_HANDLER."file_class.php");
  418. $fl = new e_file;
  419. $lanlist = $fl->get_dirs(e_LANGUAGEDIR);
  420. sort($lanlist);
  421. $ret = "<select {$include} name='{$fname}'>\n";
  422. $ret .= "<option value=''>&nbsp;</option>\n"; // ensures that the user chose it.
  423. foreach($lanlist as $choice)
  424. {
  425. $choice = trim($choice);
  426. $sel = ($curval == $choice || (!USER && $choice == e_LANGUAGE))? " selected='selected' " : "";
  427. $ret .= "<option value='{$choice}' {$sel}>{$choice}</option>\n";
  428. }
  429. $ret .= "</select>\n";
  430. break;
  431.  
  432. }
  433.  
  434. return $ret;
  435. }
  436.  
  437. function user_extended_getStruct($orderby="user_extended_struct_order")
  438. {
  439. if($ueStruct = getcachedvars('ue_struct'))
  440. {
  441. return $ueStruct;
  442. }
  443. global $tp;
  444. $ret = array();
  445. $parms = "";
  446. if($orderby != "")
  447. {
  448. $parms = "1 ORDER BY ".$tp -> toDB($orderby, true);
  449. }
  450. $sql_ue = new db; // Use our own db to avoid interference with other objects
  451. if($sql_ue->db_Select('user_extended_struct','*',$parms))
  452. {
  453. while($row = $sql_ue->db_Fetch())
  454. {
  455. $ret['user_'.$row['user_extended_struct_name']] = $row;
  456. }
  457. }
  458. cachevars('ue_struct',$ret);
  459. return $ret;
  460. }
  461.  
  462.  
  463. function parse_extended_xml($contents, $no_cache = FALSE)
  464. {
  465. if($no_cache == FALSE && $this->extended_xml)
  466. {
  467. return $this->extended_xml;
  468. }
  469.  
  470. require_once(e_HANDLER."xml_class.php");
  471. $xml = new CXml;
  472. if("getfile" == $contents)
  473. {
  474. $contents = file_get_contents(e_FILE."cache/user_extended.xml");
  475. }
  476. $xml->Set_XML_data($contents);
  477. $data = $xml->obj_data->e107_extended_user_fields[0];
  478. $ret['version'] = $data->version;
  479. unset($info);
  480. foreach($data->item as $item)
  481. {
  482. $info = array(
  483. "name" => $item->name,
  484. "text" => "UE_LAN_".strtoupper($item->name),
  485. "type" => $item->type[0],
  486. "values" => $item->values[0],
  487. "default" => $item->default[0],
  488. "required" => $item->required[0],
  489. "read" => $item->read[0],
  490. "write" => $item->write[0],
  491. "applicable" => $item->applicable[0],
  492. "include_text" => $item->include_text[0],
  493. "parms" => $item->include_text[0],
  494. "regex" => $item->regex[0]
  495. );
  496. if(is_array($item->default) && $item->default[0] == '')
  497. {
  498. $info['default'] = 0;
  499. }
  500. if($item->regex[0])
  501. {
  502. $info['parms'] .= $item->include_text[0]."^,^".$item->regex[0]."^,^LAN_UE_FAIL_".strtoupper($item->name);
  503. }
  504. $ret[$item->name] = $info;
  505. }
  506. $this->extended_xml = $ret;
  507. return $this->extended_xml;
  508. }
  509.  
  510. function convert_old_fields()
  511. {
  512. global $sql;
  513. $preList = $this->parse_extended_xml('getfile');
  514. $flist = array('user_aim', 'user_birthday', 'user_homepage', 'user_icq', 'user_msn', 'user_location');
  515. foreach($flist as $f)
  516. {
  517. $f = substr($f, 5);
  518. $preList[$f]['parms'] = addslashes($preList[$f]['parms']);
  519. $this->user_extended_add($preList[$f]);
  520. }
  521. $sql->db_Select_gen("INSERT IGNORE INTO #user_extended (user_extended_id) SELECT user_id FROM #user ");
  522. $qry = "
  523. UPDATE #user_extended AS ue , #user as u SET
  524. ue.user_aim = u.user_aim,
  525. ue.user_birthday = u.user_birthday,
  526. ue.user_homepage = u.user_homepage,
  527. ue.user_icq = u.user_icq,
  528. ue.user_msn = u.user_msn,
  529. ue.user_location = u.user_location
  530. WHERE ue.user_extended_id = u.user_id
  531. ";
  532. $sql->db_Select_gen($qry);
  533. $dlist = implode(", DROP ", $flist);
  534. $dlist = "DROP ".$dlist;
  535. $qry = "ALTER TABLE #user ".$dlist;
  536. $sql->db_Select_gen($qry);
  537. }
  538. }
  539. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement