Advertisement
Guest User

user extended class

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