Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ipb.inc
1 <?php
30 class Ipb
31 {
32 
38  var $db;
39 
45  var $needed_files = Array(
46  '/conf_global.php',
47  '/ips_kernel/class_db_mysql.php',
48  '/ips_kernel/class_converge.php',
49  );
50 
51  var $info = NULL;
52  var $version = '2_0_4';
53  var $registry = NULL;
54  var $command = 'fetch_row';
55  var $command_free = 'free_result';
56 
57 
63  function Ipb($path, $version)
64  {
65  if (!empty($path) && !empty($version)) {
66  $this->version = $version;
67  $this->setup($path);
68  }
69 
70  }//end constructor
71 
72 
73 //-- SET UP --//
74 
75 
88  function setup($path)
89  {
90  $ipb_root_path = $path;
91  $ipb_kernel_path = $ipb_root_path.'/ips_kernel';
92 
93  // Let's check to see if IPB can be found
94  if (!is_dir($ipb_root_path) || !is_dir($ipb_kernel_path)) {
95  return FALSE;
96  }
97 
98  // Database details
99  $INFO = Array();
100  if (is_file($ipb_root_path.'/conf_global.php')) {
101  include $ipb_root_path.'/conf_global.php';
102  } else {
103  return FALSE;
104  }
105  $this->info =& $INFO;
106 
107  // set URL
108  switch ($this->version) {
109  case '2_0_4' :
110  if (isset($INFO['html_url'])) {
111  $this->ipb_url = $INFO['html_url'];
112  } else if (isset($INFO['board_url'])) {
113  $this->ipb_url = $INFO['board_url'];
114  }
115  break;
116  case '2_1_3' :
117  case '2_2_1' :
118  case '3_0_2' :
119  $this->ipb_url = $INFO['board_url'];
120  break;
121  }//end switch
122 
123  $INFO['sql_driver'] = ! $INFO['sql_driver'] ? 'mysql' : strtolower($INFO['sql_driver']);
124 
125  if ($this->version != '3_0_2') {
126  require_once $ipb_kernel_path.'/class_db_'.$INFO['sql_driver'].'.php';
127  }//end if
128 
129  switch ($this->version) {
130  case '2_0_4' :
131  if (class_exists('db_driver')) {
132  $this->db = new db_driver;
133  } else {
134  return FALSE;
135  }
136  break;
137  case '2_1_3' :
138  case '2_2_1' :
139  $driver = 'db_driver_'.$INFO['sql_driver'];
140  $this->db = new $driver;
141  break;
142  case '3_0_2' :
143  require_once($ipb_root_path.'/initdata.php');
144  require_once(IPS_ROOT_PATH.'/sources/base/ipsRegistry.php');
145  $this->registry = ipsRegistry::instance();
146  $this->registry->init();
147  error_reporting(E_ALL);
148  $this->db = $this->registry->DB();
149  $this->command = 'fetch';
150  $this->command_free = 'freeResult';
151  break;
152  }
153 
154  if (!defined('IN_IPB')) {
155  define ('IN_IPB', TRUE);
156  }//end if
157  $this->db->obj['sql_database'] = $INFO['sql_database'];
158  $this->db->obj['sql_user'] = $INFO['sql_user'];
159  $this->db->obj['sql_pass'] = $INFO['sql_pass'];
160  $this->db->obj['sql_host'] = $INFO['sql_host'];
161  $this->db->obj['sql_tbl_prefix'] = $INFO['sql_tbl_prefix'];
162  $this->db->obj['query_cache_file'] = $ipb_root_path.'/sources/sql/'.$INFO['sql_driver'].'_queries.php';
163  if ($this->version == '3_0_2') {
164  $this->db->obj['query_cache_file'] = '';
165  }//end if
166  $this->db->obj['use_shutdown'] = 1;
167 
168  if (is_array($this->db->connect_vars) && count($this->db->connect_vars)) {
169  foreach ($this->db->connect_vars as $k => $v) {
170  $this->db->connect_vars[$k] = (isset($INFO[$k]) ? $INFO[$k] : '');
171  }
172  }
173  if ($this->db->connect()) return TRUE;
174 
175  return FALSE;
176 
177  }//end setup()
178 
179 
186  function isConnected()
187  {
188  if (!is_null($this->db)) {
189  return TRUE;
190  } else {
191  return FALSE;
192  }
193 
194  }//end isConnected()
195 
196 
203  function disconnect()
204  {
205  if (!is_null($this->db)) {
206  $result = $this->db->close_db();
207  $this->_assertValidResult($this->db, 'Could not close database');
208  return TRUE;
209  } else {
210  return FALSE;
211  }
212 
213  }//end disconnect()
214 
215 
216 //-- URL --//
217 
218 
225  function getIpbURL()
226  {
227  return $this->ipb_url;
228 
229  }//end getIpbURL()
230 
231 
232 //-- SEARCH --//
233 
234 
246  function search($assetid)
247  {
248  $tmp = explode('_', $assetid);
249  $id = $tmp[1];
250  $prefix = $tmp[0];
251 
252  if (empty($id) || empty($prefix)) {
253  return FALSE;
254  }
255  if ( $prefix == 'u') {
256  // ipb member
257  $result = $this->getMemberGroupInfo('members', Array(), $id);
258  } else if ( $prefix == 'g' ) {
259  // ipb group
260  $result = $this->getMemberGroupInfo('groups', Array(), $id);
261  }
262 
263  if (empty($result)) return FALSE;
264 
265  return $result;
266 
267  }//end search()
268 
269 
270 //-- AUTHENTICATION --//
271 
272 
284  function authenticateUser(&$bridge, $username, $password, $is_email_login)
285  {
286  switch ($this->version) {
287  case '3_0_2':
288  // New style of authentication
289  $member_key = '';
290  if ($is_email_login) {
291  if (!IPSMember::checkByEmail($username)) return FALSE;
292  $member_key = $username;
293  } else {
294  $sql = 'SELECT m.member_id FROM '.$this->db->obj['sql_tbl_prefix'].'members m
295  WHERE m.name=\''.str_replace('&#039', '&#39', htmlspecialchars($username, ENT_QUOTES)).'\';';
296  $member_info = $this->_getResult($sql, Array(), TRUE);
297  $member_id = array_get_index($member_info, 'member_id', 0);
298  if (empty($member_id)) return FALSE;
299  $member_key = $member_id;
300  }//end if
301  if (empty($member_key)) return FALSE;
302  $md5_password = md5($password);
303  $status = IPSMember::authenticateMember($member_key, $md5_password);
304  return $status;
305  break;
306  default:
307  // Old method of authentication
308  require_once $bridge->attr('location').'/ips_kernel/class_converge.php';
309  $converge = new class_converge($this->db);
310 
311  if ($is_email_login) {
312  if (!$converge->converge_check_for_member_by_email($username)) {
313  return FALSE;
314  }
315  $converge->converge_load_member_by_email($username);
316  } else {
317  $sql = 'SELECT
318  id
319  FROM
320  '.$this->db->obj['sql_tbl_prefix'].'members
321  WHERE
322  name=\''.str_replace('&#039', '&#39', htmlspecialchars($username, ENT_QUOTES)).'\';
323  ';
324  $db_result = $this->db->query($sql);
325  $this->_assertValidResult($this->db, 'Could not authenticate IPB user');
326 
327  while ($row = $this->db->{$this->command}($db_result)) {
328  $test[] = $row;
329  }
330  $this->db->{$this->command_free}($db_result);
331  if (empty($test)) return FALSE;
332 
333  @$converge->converge_load_member_by_id($test[0]['id']);
334  }
335 
336  return $converge->converge_authenticate_member(md5($password));
337  }//end switch
338 
339  }//end authenticateUser()
340 
341 
342 //-- GROUPS AND MEMBERS --//
343 
344 
379  function getAllInfo($cols=Array(), $id=0)
380  {
381  // add column names
382  $columns = Array();
383  $custom_columns = FALSE;
384  $table_name = 'members';
385  $table_short_name = 'm';
386  $member_id_field = $this->_getMemberIdField();
387  $member_group_id_field = $this->_getMemberGroupIdField();
388  $m_cols = Array($member_id_field, 'name', $member_group_id_field, 'email', 'title', 'member_login_key');
389  $g_cols = Array('g_id', 'g_title', 'g_mem_info', 'prefix', 'suffix');
390 
391  if (empty($cols)) {
392  foreach ($m_cols as $c) {
393  $columns[] = 'm.'.$c;
394  }
395  foreach ($g_cols as $c) {
396  $columns[] = 'g.'.$c;
397  }
398  } else {
399  $custom_columns = TRUE;
400  }
401 
402  // build query
403  $where = '';
404  if ($id) {
405  $where = 'WHERE '.$table_short_name.'.'.$member_id_field.'='.$id;
406  }
407  $join = 'LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'groups g ON (m.'.$member_group_id_field.'=g.g_id)';
408  $sql = $this->_buildQuery($columns, $table_name, $table_short_name, $where, $join);
409 
410  // get the result
411  $db_result = $this->db->query($sql);
412  $this->_assertValidResult($this->db, 'Could not get IPB info');
413 
414  $result = Array();
415  $cols = array_merge($m_cols, $g_cols);
416  while ($row = $this->db->{$this->command}($db_result)) {
417  if (!$custom_columns) {
418  $key = $row[$cols[0]];
419  $tmp_cols = array_slice($cols, 1);
420  $data = Array();
421  foreach ($tmp_cols as $c) {
422  $data[$c] = $row[$c];
423  }
424  $result[$key] = $data;
425  } else {
426  $tmp = Array();
427  foreach ($cols as $c) {
428  $tmp[$c] = $row[$c];
429  }
430  $result[] = $tmp;
431  }
432  }
433  $this->db->{$this->command_free}($db_result);
434 
435  $result = $this->_maintainDataFormat($result);
436 
437  return $result;
438 
439  }//end getAllInfo()
440 
441 
476  function getMemberGroupInfo($table_name='members', $cols=Array(), $id=0)
477  {
478  // add column names
479  $columns = Array();
480  $custom_columns = FALSE;
481  $member_id_field = $this->_getMemberIdField();
482  $member_group_id_field = $this->_getMemberGroupIdField();
483 
484  if ($table_name == 'members') {
485  $table_short_name='u';
486  } else if ($table_name == 'groups') {
487  $table_short_name='g';
488  }
489 
490  switch($this->version) {
491  case '3_0_2':
492  if (empty($cols)) {
493  if ($table_name == 'members') {
494  $cols = 'u.'.$member_id_field.', u.name, u.'.$member_group_id_field.', u.email, u.title, u.member_login_key, u.bday_day, u.bday_month, u.bday_year, \'\' as vdir, me.field_3 as aim_name, me.field_4 as msnname, me.field_5 as website, me.field_6 as icq_number, me.field_8 as location, me.field_9 as interests, me.field_10 as yahoo, ue.avatar_type as photo_type, ue.avatar_location as photo_location, ue.avatar_size as photo_dimensions, ue.notes, ue.links, ue.bio, ue.ta_size, ue.signature, ue.avatar_location, ue.avatar_size, ue.avatar_type ';
495  } else if ($table_name == 'groups') {
496  $cols = 'g.g_id, g.g_title, g.g_mem_info, g.prefix, g.suffix ';
497  }//end if
498  } else {
499  $custom_columns = TRUE;
500  }//end if
501 
502  $from = $this->db->obj['sql_tbl_prefix'].$table_name.' '.$table_short_name;
503  $join = '';
504  if ($table_name == 'members') {
505  $join = ' LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'profile_portal ue ON u.'.$member_id_field.'=ue.pp_member_id';
506  $join .= ' LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'pfields_content me ON me.member_id=u.'.$member_id_field;
507  }
508  // build query
509  $where = '';
510  if ($id) {
511  if ($table_name == 'members') {
512  $where = ' WHERE '.$table_short_name.'.'.$member_id_field.'='.$id;
513  } else if ($table_name == 'groups') {
514  $where = ' WHERE '.$table_short_name.'.g_id='.$id;
515  }
516  }
517  $sql = 'SELECT '.$cols.$from.$join.$where;
518  break;
519  default:
520  if (empty($cols)) {
521  if ($table_name == 'members') {
522  $cols = Array(
523  'u.'.$member_id_field,
524  'u.name',
525  'u.'.$member_group_id_field,
526  'u.email',
527  'u.title',
528  'u.member_login_key',
529  'u.bday_day',
530  'u.bday_month',
531  'u.bday_year',
532  'ue.photo_type',
533  'ue.photo_location',
534  'ue.aim_name',
535  'ue.icq_number',
536  'ue.website',
537  'ue.yahoo',
538  'ue.interests',
539  'ue.msnname',
540  'ue.vdirs',
541  'ue.location',
542  'ue.signature',
543  'ue.avatar_location',
544  'ue.avatar_size',
545  'ue.avatar_type',
546  );
547  } else if ($table_name == 'groups') {
548  $cols = Array('g.g_id', 'g.g_title', 'g.g_mem_info', 'g.prefix', 'g.suffix');
549  }
550  } else {
551  $custom_columns = TRUE;
552  }
553 
554  $columns = $cols;
555 
556  $join = '';
557  if ($table_name == 'members') {
558  $join = ' LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'member_extra ue ON u.'.$member_id_field.'=ue.id';
559  }
560  // build query
561  $where = '';
562  if ($id) {
563  if ($table_name == 'members') {
564  $where = ' WHERE '.$table_short_name.'.'.$member_id_field.'='.$id;
565  } else if ($table_name == 'groups') {
566  $where = ' WHERE '.$table_short_name.'.g_id='.$id;
567  }
568  }
569  $sql = $this->_buildQuery($columns, $table_name, $table_short_name, $where, $join);
570  }//end switch
571 
572  // get the result
573  $db_result = $this->db->query($sql);
574  $this->_assertValidResult($this->db, 'Could not get IPB member group info');
575 
576  $result = Array();
577  while ($row = $this->db->{$this->command}($db_result)) {
578  if (!$custom_columns) {
579  $tmp_col_name = explode('.', $cols[0]);
580  $key = $row[$tmp_col_name[1]];
581  $tmp_cols = array_slice($cols, 1);
582  $data = Array();
583  foreach ($tmp_cols as $c) {
584  $tmp_col_name = explode('.', $c);
585  $data[$tmp_col_name[1]] = $row[$tmp_col_name[1]];
586  }
587  $data['type'] = $table_name;
588  $result[$table_short_name.'_'.$key] = $data;
589  } else {
590  $tmp = Array('type' => $table_name);
591  foreach ($cols as $c) {
592  if ($c == $member_id_field || $c == 'g_id') {
593  $tmp[$c] = $table_short_name.'_'.$row[$c];
594  } else {
595  $tmp[$c] = $row[$c];
596  }
597  }
598  $result[] = $tmp;
599  }
600  }
601  $this->db->{$this->command_free}($db_result);
602 
603  $result = $this->_maintainDataFormat($result);
604 
605  return $result;
606 
607  }//end getMemberGroupInfo()
608 
609 
621  function getMembers($member_id=0, $group_id=0, $cols=Array())
622  {
623  // add column names
624  $columns = Array();
625  $custom_columns = FALSE;
626  $table_name = 'members';
627  $table_short_name='m';
628  $member_id_field = $this->_getMemberIdField();
629  $member_group_id_field = $this->_getMemberGroupIdField();
630  if (empty($cols)) {
631  $cols = Array($member_id_field, 'name', $member_group_id_field, 'email', 'title', 'member_login_key');
632  } else {
633  $custom_columns = TRUE;
634  }
635 
636  // add prefix
637  foreach ($cols as $c) {
638  $columns[] = $table_short_name.'.'.$c;
639  }
640 
641  // build query
642  $where = '';
643  if ($member_id || $group_id) {
644  $where = ' WHERE ';
645  if ($member_id) {
646  $where .= $table_short_name.'.'.$member_id_field.'='.$member_id.(($group_id) ? ' and ' : '');
647  }
648  if ($group_id) {
649  $where .= $table_short_name.'.'.$member_group_id_field.'='.$group_id;
650  }
651  }
652 
653  $sql = $this->_buildQuery($columns, $table_name, $table_short_name, $where);
654 
655  // get the result
656  $db_result = $this->db->query($sql);
657  $this->_assertValidResult($this->db, 'Could not get IPB member(s)');
658 
659  $result = Array();
660  while ($row = $this->db->{$this->command}($db_result)) {
661  if (!$custom_columns) {
662  $key = $row[$cols[0]];
663  $tmp_cols = array_slice($cols, 1);
664  $data = Array();
665  foreach ($tmp_cols as $c) {
666  if ($c == $member_group_id_field) {
667  $data[$c] = 'g_'.$row[$c];
668  } else {
669  $data[$c] = $row[$c];
670  }
671  }
672  $data['type'] = $table_name;
673  $result['u_'.$key] = $data;
674  } else {
675  $tmp = Array('type' => $table_name);
676  foreach ($cols as $c) {
677  if ($c == $member_id_field) {
678  $tmp[$c] = 'u_'.$row[$c];
679  } else {
680  $tmp[$c] = $row[$c];
681  }
682  }
683  $result[] = $tmp;
684  }
685  }
686  $this->db->{$this->command_free}($db_result);
687 
688  $result = $this->_maintainDataFormat($result);
689 
690  return $result;
691 
692  }//end getMembers()
693 
694 
702  function getCustomFields()
703  {
704  $sql = $this->_buildQuery('*', 'pfields_data', 'pfd');
705 
706  return $this->_getResult($sql, Array(), FALSE);
707 
708  }//end getCustomFields()
709 
710 
720  function getMemberCustomFieldData($member_id)
721  {
722  $tmp = explode(':', $member_id);
723  if (!isset($tmp[1])) return Array();
724  $tmp = explode('_', $tmp[1]);
725  $id = $tmp[1];
726 
727  $sql = $this->_buildQuery('*', 'pfields_content', 'pfc', ' WHERE pfc.member_id='.$id);
728 
729  return $this->_getResult($sql, Array(), FALSE);
730 
731  }//end getMemberCustomFieldData()
732 
733 
734 //-- GROUPS --//
735 
736 
745  function getGroupInfo($group_id)
746  {
747  $tmp = explode(':', $group_id);
748  if (!isset($tmp[1])) return Array();
749  $tmp = explode('_', $tmp[1]);
750  $id = $tmp[1];
751 
752  $sql = $this->_buildQuery('*', 'groups', 'g', ' WHERE g.g_id='.$id);
753  return $this->_getResult($sql, Array(), TRUE);
754 
755  }//end getGroupInfo()
756 
757 
764  function getListOfGroups()
765  {
766  $fields = Array(
767  'g.g_id',
768  'g.g_title',
769  );
770 
771  $sql = $this->_buildQuery($fields, 'groups', 'g');
772 
773  return $this->_getResult($sql, Array('g_id' => 'id'), FALSE);
774 
775  }//end getListOfGroups()
776 
777 
778 //-- MEMBERS --//
779 
780 
790  function canViewBoard($username)
791  {
792  $member_group_id_field = $this->_getMemberGroupIdField();
793  $sql = 'SELECT
794  m.name, g.g_view_board
795  FROM
796  '.$this->db->obj['sql_tbl_prefix'].'members m,
797  '.$this->db->obj['sql_tbl_prefix'].'groups g
798  WHERE
799  m.'.$member_group_id_field.'=g.g_id and
800  m.name=\''.str_replace('&#039', '&#39', htmlspecialchars($username, ENT_QUOTES)).'\'
801  ;';
802  return $this->_getResult($sql, Array(), TRUE);
803 
804  }//end canViewBoard()
805 
806 
815  function getMemberInfo($member_id)
816  {
817  $tmp = explode(':', $member_id);
818  if (!isset($tmp[1])) return Array();
819  $tmp = explode('_', $tmp[1]);
820  $id = $tmp[1];
821  switch ($this->version) {
822  case '3_0_2':
823  $sql = 'SELECT m.*, \'\' as vdir, me.field_3 as aim_name, me.field_4 as msnname, me.field_5 as website, me.field_6 as icq_number, me.field_8 as location, me.field_9 as interests, me.field_10 as yahoo, ue.avatar_type as photo_type, ue.avatar_location as photo_location, ue.avatar_size as photo_dimensions, ue.notes, ue.links, ue.bio, ue.ta_size, ue.signature, ue.avatar_location, ue.avatar_size, ue.avatar_type FROM '.$this->db->obj['sql_tbl_prefix'].'members m LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'profile_portal ue ON ue.pp_member_id=m.member_id LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'pfields_content me ON me.member_id=m.member_id WHERE m.member_id='.$id;
824  break;
825  default:
826  $sql = $this->_buildQuery('*', 'members', 'm', ' WHERE m.id='.$id, ' LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'member_extra me ON me.id=m.id');
827  }//end switch
828 
829  $data = $this->_getResult($sql, Array(), TRUE, TRUE);
830 
831  return $data;
832 
833  }//end getMemberInfo()
834 
835 
844  function getMemberInfoByName($member_name)
845  {
846  switch ($this->version) {
847  case '3_0_2':
848  $member_id_field = $this->_getMemberIdField();
849  $sql = 'SELECT m.*, \'\' as vdir, me.field_3 as aim_name, me.field_4 as msnname, me.field_5 as website, me.field_6 as icq_number, me.field_8 as location, me.field_9 as interests, me.field_10 as yahoo, ue.avatar_type as photo_type, ue.avatar_location as photo_location, ue.avatar_size as photo_dimensions, ue.notes, ue.links, ue.bio, ue.ta_size, ue.signature, ue.avatar_location, ue.avatar_size, ue.avatar_type FROM '.$this->db->obj['sql_tbl_prefix'].'members m LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'profile_portal ue ON ue.pp_member_id=m.member_id LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'pfields_content me ON me.member_id=m.member_id WHERE m.name=\''.str_replace('&#039', '&#39', htmlspecialchars($member_name, ENT_QUOTES)).'\'';
850  break;
851  default:
852  $sql = $this->_buildQuery('*', 'members', 'm', ' WHERE m.name=\''.str_replace('&#039', '&#39', htmlspecialchars($member_name, ENT_QUOTES)).'\'', ' LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'member_extra me ON me.id=m.id');
853  }//end switch
854 
855  $data = $this->_getResult($sql, Array(), TRUE, TRUE);
856 
857  return $data;
858 
859  }//end getMemberInfoByName()
860 
861 
870  function getMemberParent($member_id)
871  {
872  $member_id_field = $this->_getMemberIdField();
873  $member_group_id_field = $this->_getMemberGroupIdField();
874  switch ($this->version) {
875  case '3_0_2':
876  $member_id_field = 'member_id';
877  $member_group_id_field = 'member_group_id';
878  break;
879  default:
880  $member_id_field = 'id';
881  $member_group_id_field = 'mgroup';
882  }//end switch
883  $tmp = explode(':', $member_id);
884  if (!isset($tmp[1])) return Array();
885  $tmp = explode('_', $tmp[1]);
886  $id = $tmp[1];
887 
888  $sql = $this->_buildQuery(Array($member_group_id_field), 'members', 'm', ' WHERE m.'.$member_id_field.'='.$id);
889 
890  $data = $this->_getResult($sql, Array(), TRUE, TRUE);
891 
892  return $data;
893 
894  }//end getMemberParent()
895 
896 
905  function getListOfMembers($group_id=0)
906  {
907  $member_id_field = $this->_getMemberIdField();
908  $member_group_id_field = $this->_getMemberGroupIdField();
909  $fields = Array(
910  'u.'.$member_id_field,
911  'u.name',
912  'u.'.$member_group_id_field,
913  );
914 
915  if ($group_id== 0) {
916  $sql = $this->_buildQuery($fields, 'members', 'u');
917  } else {
918  $tmp = explode(':', $group_id);
919  if (!isset($tmp[1])) return Array();
920  $tmp = explode('_', $group_id);
921  $id = $tmp[1];
922  $sql = $this->_buildQuery($fields, 'members', 'u', ' WHERE u.'.$member_group_id_field.'='.$id);
923  }
924 
925  $results = $this->_getResult($sql, Array(), FALSE);
926 
927  // Different format, so extra cleaning is involved
928  foreach ($results as $index => $result) {
929  $results[$index] = $this->_maintainDataFormat($result);
930  }//end foreach
931 
932  return $results;
933 
934  }//end getListOfMembers()
935 
936 
937 //-- ROOT FORUMS --//
938 
939 
948  function getRootForumInfo($forum_id)
949  {
950  $tmp = explode(':', $forum_id);
951  if (!isset($tmp[1])) return Array();
952  $tmp = explode('_', $tmp[1]);
953  $id = $tmp[1];
954 
955  $fields = Array(
956  'f.id',
957  'f.name',
958  'f.description',
959  );
960  $sql = $this->_buildQuery($fields, 'forums', 'f', ' WHERE f.id='.$id);
961 
962  return $this->_getResult($sql, Array(), TRUE);
963 
964  }//end getRootForumInfo()
965 
966 
974  {
975  $fields = Array(
976  'f.id',
977  'f.name',
978  );
979  $sql = $this->_buildQuery($fields, 'forums', 'f', ' WHERE f.parent_id=-1');
980 
981  return $this->_getResult($sql, Array(), FALSE);
982 
983  }//end getListOfRootForums()
984 
985 
986 //-- FORUMS --//
987 
988 
997  function getForumInfo($forum_id)
998  {
999  $tmp = explode(':', $forum_id);
1000  if (!isset($tmp[1])) return Array();
1001  $tmp = explode('_', $tmp[1]);
1002  $id = $tmp[1];
1003 
1004  $fields = Array(
1005  'f.id',
1006  'f.name',
1007  'f.description',
1008  'f.topics',
1009  'f.parent_id',
1010  'f.last_post',
1011  'f.last_poster_name',
1012  );
1013  $sql = $this->_buildQuery($fields, 'forums', 'f', ' WHERE f.id='.$id);
1014 
1015  return $this->_getResult($sql, Array(), TRUE);
1016 
1017  }//end getForumInfo()
1018 
1019 
1028  function getForumParent($forum_id)
1029  {
1030  $tmp = explode(':', $forum_id);
1031  if (!isset($tmp[1])) return Array();
1032  $tmp = explode('_', $tmp[1]);
1033  $id = $tmp[1];
1034 
1035  $sql = $this->_buildQuery(Array('parent_id'), 'forums', 'f', ' WHERE f.id='.$id);
1036 
1037  return $this->_getResult($sql, Array(), TRUE);
1038 
1039  }//end getForumParent()
1040 
1041 
1050  function getListOfForums($root_forum_id)
1051  {
1052  $tmp = explode('_', $root_forum_id);
1053  $id = $tmp[1];
1054 
1055  $fields = Array(
1056  'f.id',
1057  'f.name',
1058  'f.description',
1059  );
1060  $sql = $this->_buildQuery($fields, 'forums', 'f', ' WHERE f.parent_id='.$id);
1061 
1062  return $this->_getResult($sql, Array(), FALSE);
1063 
1064  }//end getListOfForums()
1065 
1066 
1067 //-- TOPICS --//
1068 
1069 
1078  function getTopicInfo($topic_id)
1079  {
1080  $tmp = explode(':', $topic_id);
1081  if (!isset($tmp[1])) return Array();
1082  $tmp = explode('_', $tmp[1]);
1083  $id = $tmp[1];
1084 
1085  $fields = Array(
1086  't.tid',
1087  't.title',
1088  't.forum_id',
1089  't.description',
1090  't.posts',
1091  't.starter_name',
1092  't.start_date',
1093  't.last_poster_name',
1094  't.last_post',
1095  't.poll_state',
1096  );
1097  $sql = $this->_buildQuery($fields, 'topics', 't', ' WHERE t.tid='.$id);
1098 
1099  return $this->_getResult($sql, Array('title' => 'name'), TRUE);
1100 
1101  }//end getTopicInfo()
1102 
1103 
1112  function getTopicParent($topic_id)
1113  {
1114  $tmp = explode(':', $topic_id);
1115  if (!isset($tmp[1])) return Array();
1116  $tmp = explode('_', $tmp[1]);
1117  $id = $tmp[1];
1118 
1119  $sql = 'SELECT
1120  t.forum_id,
1121  f.parent_id
1122  FROM
1123  '.$this->db->obj['sql_tbl_prefix'].'topics t,
1124  '.$this->db->obj['sql_tbl_prefix'].'forums f
1125  WHERE
1126  t.forum_id=f.id
1127  and t.tid='.$id.';
1128  ';
1129 
1130  return $this->_getResult($sql, Array(), TRUE);
1131 
1132  }//end getTopicParent()
1133 
1134 
1143  function getListOfTopics($forum_id)
1144  {
1145  $tmp = explode('_', $forum_id);
1146  $id = $tmp[1];
1147 
1148  $fields = Array(
1149  't.tid',
1150  't.title',
1151  't.description',
1152  );
1153  $where = ' WHERE t.forum_id='.$id.' AND o.pid IS NULL';
1154  $join = ' LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'polls o ON t.tid=o.tid';
1155  $sql = $this->_buildQuery($fields, 'topics', 't', $where, $join);
1156 
1157  return $this->_getResult($sql, Array('tid' => 'id'));
1158 
1159  }//end getListOfTopics()
1160 
1161 
1162 //-- POSTS --//
1163 
1164 
1173  function getPostInfo($post_id)
1174  {
1175  $tmp = explode(':', $post_id);
1176  if (!isset($tmp[1])) return Array();
1177  $tmp = explode('_', $tmp[1]);
1178  $id = $tmp[1];
1179 
1180  $sql = 'select
1181  p.pid,
1182  p.author_name,
1183  p.post_date,
1184  p.post_title,
1185  p.post_parent,
1186  p.post,
1187  t.tid,
1188  t.title as topic_name,
1189  f.name as forum_name
1190  from
1191  '.$this->db->obj['sql_tbl_prefix'].'posts p,
1192  '.$this->db->obj['sql_tbl_prefix'].'topics t,
1193  '.$this->db->obj['sql_tbl_prefix'].'forums f
1194  where
1195  p.pid='.$id.'
1196  and p.topic_id = t.tid
1197  and t.forum_id = f.id
1198  order by
1199  post_date desc;';
1200 
1201  return $this->_getResult($sql, Array(), TRUE);
1202 
1203  }//end getPostInfo()
1204 
1205 
1214  function getPostParent($post_id)
1215  {
1216  $tmp = explode(':', $post_id);
1217  if (!isset($tmp[1])) return Array();
1218  $tmp = explode('_', $tmp[1]);
1219  $id = $tmp[1];
1220 
1221  $sql = 'SELECT
1222  t.tid AS topic_id,
1223  t.forum_id,
1224  f.parent_id
1225  FROM
1226  '.$this->db->obj['sql_tbl_prefix'].'posts p,
1227  '.$this->db->obj['sql_tbl_prefix'].'topics t,
1228  '.$this->db->obj['sql_tbl_prefix'].'forums f
1229  WHERE
1230  t.forum_id=f.id
1231  and p.topic_id=t.tid
1232  and p.pid='.$id.';
1233  ';
1234 
1235  return $this->_getResult($sql, Array(), TRUE);
1236 
1237  }//end getPostParent()
1238 
1239 
1250  function getListOfPosts($topic_id, $forum_id=NULL, $limit=0)
1251  {
1252  if (is_null($forum_id)) {
1253  $tmp = explode('_', $topic_id);
1254  $id = $tmp[1];
1255  $fields = Array(
1256  'p.pid',
1257  );
1258  if ($limit != 0) {
1259  $sql = $this->_buildQuery($fields, 'posts', 'p', ' WHERE p.topic_id='.$id, ' LIMIT '.$limit);
1260  } else {
1261  $sql = $this->_buildQuery($fields, 'posts', 'p', ' WHERE p.topic_id='.$id);
1262  }
1263  } else {
1264  $sql = 'SELECT
1265  p.pid, t.tid, p.author_id, p.post_date
1266  FROM
1267  '.$this->db->obj['sql_tbl_prefix'].'posts p,
1268  '.$this->db->obj['sql_tbl_prefix'].'topics t
1269  WHERE
1270  p.topic_id=t.tid AND
1271  t.forum_id='.$forum_id.'
1272  ORDER BY
1273  p.post_date DESC
1274  ';
1275  if ($limit != 0) $sql .= ' LIMIT '.$limit;
1276  $sql .= ';';
1277  }
1278 
1279  return $this->_getResult($sql, Array('pid' => 'id'));
1280 
1281  }//end getListOfPosts()
1282 
1283 
1284 //-- POLLS --//
1285 
1286 
1295  function getPollInfo($poll_id)
1296  {
1297  $tmp = explode(':', $poll_id);
1298  if (!isset($tmp[1])) return Array();
1299  $tmp = explode('_', $tmp[1]);
1300  $id = $tmp[1];
1301 
1302  $fields = Array(
1303  'o.pid',
1304  'o.tid',
1305  'o.start_date',
1306  'o.choices',
1307  'o.starter_id',
1308  'o.votes',
1309  'o.forum_id',
1310  'o.poll_question',
1311  't.poll_state as poll_state',
1312  );
1313  $join = ' LEFT JOIN '.$this->db->obj['sql_tbl_prefix'].'topics t ON t.tid=o.tid';
1314  $sql = $this->_buildQuery($fields, 'polls', 'o', ' WHERE o.pid='.$id, $join);
1315 
1316  return $this->_getResult($sql, Array(), TRUE);
1317 
1318  }//end getPollInfo()
1319 
1320 
1327  function getListOfPolls()
1328  {
1329  $sql = $this->_buildQuery('*', 'polls', 'o');
1330 
1331  return $this->_getResult($sql, Array('pid' => 'id'));
1332 
1333  }//end getListOfPolls()
1334 
1335 
1336 //-- QUERIES --//
1337 
1338 
1353  function _buildQuery($columns, $table_name, $table_short_name, $where='', $join='', $order_by='', $limit='')
1354  {
1355  if ($columns == '*') {
1356  $sql = 'SELECT * ';
1357  } else {
1358  $sql = 'SELECT '.implode(', ', $columns);
1359  }
1360 
1361  $sql .= ' FROM '.$this->db->obj['sql_tbl_prefix'].$table_name.' '.$table_short_name;
1362 
1363  // JOIN clause
1364  if (!empty($join)) $sql .= $join;
1365 
1366  // WHERE clause
1367  if (!empty($where)) $sql .= $where;
1368 
1369  // ORDER BY clause
1370  if (!empty($order_by)) $sql .= $order_by;
1371 
1372  // LIMIT clause
1373  if (!empty($limit)) $sql .= $limit;
1374 
1375  $sql .= ';';
1376 
1377  return $sql;
1378 
1379  }//end _buildQuery()
1380 
1381 
1394  function _getResult($sql, $key_change=Array(), $one_element=FALSE, $backward=FALSE)
1395  {
1396  $db_result = $this->db->query($sql);
1397  $this->_assertValidResult($this->db, 'Could not get result from IPB');
1398 
1399  $result = Array();
1400  while ($row = $this->db->{$this->command}($db_result)) {
1401  if (!empty($key_change)) {
1402  foreach ($key_change as $old_key => $new_key) {
1403  $row[$new_key] = $row[$old_key];
1404  }
1405  }
1406  $result[] = $row;
1407  }
1408  $this->db->{$this->command_free}($db_result);
1409  if (empty($result)) return Array();
1410  if ($one_element) $result = array_pop($result);
1411 
1412  if ($backward) {
1413  $result = $this->_maintainDataFormat($result);
1414  }//end if
1415 
1416  return $this->_htmlDecode($result);
1417 
1418  }//end _getResult()
1419 
1420 
1421 //-- HTML DECODE --//
1422 
1423 
1436  function _htmlDecode($result)
1437  {
1438 
1439  // list from post_parser.php
1440  $html_code_table = Array(
1441  '&#39;' => '\'',
1442  '&#039;' => '\'',
1443  '&#33;' => '!',
1444  '&#036;' => '$',
1445  '&#124;' => '|',
1446  '&amp;' => '&',
1447  '&gt;' => '>',
1448  '&lt;' => '<',
1449  '&quot;' => '"',
1450  '&#153;' => '(tm)',
1451  '&#58;' => ':',
1452  '&#91;' => '[',
1453  '&#93;' => ']',
1454  '&#41;' => ')',
1455  '&#40;' => '(',
1456  '&#60;' => '<',
1457  '&#62;' => '>',
1458  '&#043;' => '+',
1459  '&#045;' => '-',
1460  'j&#097;v&#097;script' => 'javascript',
1461  '&#097;lert' => 'alert',
1462  '&#097;lert' => 'about:',
1463  '&#111;nmouseover' => 'onmouseover',
1464  '&#111;nclick' => 'onclick',
1465  '&#111;nload' => 'onload',
1466  '&#111;nsubmit' => 'onsubmit',
1467  );
1468 
1469  foreach ($result as $key => $value) {
1470  if (is_string($value)) {
1471  foreach ($html_code_table as $html_code => $char) {
1472  $new_result[$key] = str_replace($html_code, $char, $value);
1473  $value = $new_result[$key];
1474  }
1475  } else {
1476  $new_result[$key] = $value;
1477  }
1478  }
1479 
1480  return $new_result;
1481 
1482  }//end _htmlDecode()
1483 
1484 
1496  protected function _assertValidResult($db, $message)
1497  {
1498  if (!empty($db->error)) {
1499  trigger_error($message.': '.$db->error, E_USER_ERROR);
1500  }
1501 
1502  }//end _assertValidResult()
1503 
1504 
1511  protected function _getMemberIdField()
1512  {
1513  switch ($this->version) {
1514  case '3_0_2':
1515  return 'member_id';
1516  default:
1517  return 'id';
1518  }//end switch
1519 
1520  }//end _getMemberIdField()
1521 
1522 
1529  protected function _getMemberGroupIdField()
1530  {
1531  switch ($this->version) {
1532  case '3_0_2':
1533  return 'member_group_id';
1534  default:
1535  return 'mgroup';
1536  }//end switch
1537 
1538  }//end _getMemberIdField()
1539 
1540 
1549  protected function _maintainDataFormat($data=Array())
1550  {
1551  switch ($this->version) {
1552  case '3_0_2':
1553  if (isset($data['member_id'])) {
1554  $data['id'] = $data['member_id'];
1555  }//end if
1556  if (isset($data['member_group_id'])) {
1557  $data['mgroup'] = $data['member_group_id'];
1558  }//end if
1559  break;
1560  }//end switch
1561 
1562  return $data;
1563 
1564  }//end _maintainDataFormat()
1565 
1566 
1567 }//end class
1568 
1569 ?>