### Eclipse Workspace Patch 1.0
#P phpbbforum
Index: phpbbforum.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/phpbbforum/phpbbforum.install,v
retrieving revision 1.2
diff -u -r1.2 phpbbforum.install
--- phpbbforum.install	20 Apr 2008 17:08:48 -0000	1.2
+++ phpbbforum.install	24 Sep 2008 17:18:22 -0000
@@ -1,66 +1,82 @@
 <?php
 // $Id: phpbbforum.install,v 1.2 2008/04/20 17:08:48 vb Exp $
 /**
- * Copyright (Ñ) 2007-2008 by Vadim G.B. (http://vgb.org.ru)
+ * Copyright (ï¿½) 2007-2008 by Vadim G.B. (http://vgb.org.ru)
  */
 
 /**
  * Implementation of hook_install().
  */
-//function phpbbforum_install() {
-//}
+function phpbbforum_install() {
+  
+  drupal_install_schema('phpbbforum');
+  
+}
+
+/**
+ * Implementation of hook_schema().
+ */
+function phpbbforum_schema() {
+  
+  $schema = array();
+  $schema['drupal2phpbb'] = _phpbbforum_schema_drupal2phpbb();
+  
+  return $schema;
+
+}
 
 /**
  * Implementation of hook_uninstall().
  */
 function phpbbforum_uninstall() {
+  
+  drupal_uninstall_schema("phpbbforum");
+  
+  // Delete all the phpbbforum variables and then clear the variable cache
+  db_query("DELETE FROM {variable} WHERE name LIKE 'phpbbforum_%'");
+  cache_clear_all('variables', 'cache');
+  
+  drupal_set_message(t('phpBBforum module uninstalled successfully.')); 
+}
 
-  // Drop variables.
-  $variables = array(
-    'phpbbforum_root',
-    'phpbbforum_inc',
-    'phpbbforum_api_file',
-    'phpbbforum_master',
-    'phpbbforum_user_login',
-    'phpbbforum_qookie_test',
-    'phpbbforum_cookie_length',
-    'phpbbforum_login_cookie_length',
-    'phpbbforum_pm_display_mode',
-    'phpbbforum_num_whos_online',
-    'phpbbforum_num_recent_topics',
-    'phpbbforum_recent_topics_display_mode',
-    'phpbbforum_recent_topics_br',
-    'phpbbforum_num_recent_posts',
-    'phpbbforum_recent_posts_display_mode',
-    'phpbbforum_recent_posts_br',
-    'phpbbforum_num_top_posters',
-    'phpbbforum_user_ban',
-    'phpbbforum_set_msg',
-    'phpbbforum_log_msg',
-    'phpbbforum_sync_signature',
-    'phpbbforum_sync_avatar',
-    //'phpbbforum_map_username',
-    'phpbbforum_map_user_gender',
-    'phpbbforum_map_user_birthday',
-    'phpbbforum_map_user_from',
-    'phpbbforum_map_user_icq',
-    'phpbbforum_map_user_aim',
-    'phpbbforum_map_user_yim',
-    'phpbbforum_map_user_msnm',
-    'phpbbforum_map_user_jabber',
-    'phpbbforum_map_user_website',
-    'phpbbforum_page_frame',
-    'phpbbforum_page_width',
-    'phpbbforum_page_height',
-    'phpbbforum_page_scroll',
-    'phpbbforum_map_user_occ',
-    'phpbbforum_map_user_interests',
-    'phpbb_forum_title',
-  );
+/**
+ * This update creates the table 
+ * which is needed for the drupal2phpbb feature
+ */
+function phpbbforum_update_6000() {
+  
+  $ret = array();
+  
+   // New table to buffer mail messages during sending
+  db_create_table($ret, 'phpbbforum_drupal2phpbb', _phpbbforum_schema_drupal2phpbb());
+  
+  return $ret;
   
-  foreach ($variables as $variable) {
-    variable_del($variable);
-  }
+}
 
-  drupal_set_message(t('phpBBforum module uninstalled successfully.')); 
+function _phpbbforum_schema_drupal2phpbb() {
+
+  $schema = array(
+    'description' => t('Mapping table for phpbbtopics created in via drupal'),
+    'fields' => array(
+			'node_id' => array(
+                'description' => t('References the drupal node id'),
+                'type' => 'int',
+                'unsigned' => TRUE,
+                'not null' => TRUE),
+			'topic_id' => array(
+                'description' => t('References the phpbb topic id'),
+                'type' => 'int',
+                'unsigned' => TRUE,
+                'not null' => TRUE),
+			'linked' => array(
+                'description' => t('Shall a comment-link be added to the node?'),
+                'type' => 'int',
+                'unsigned' => TRUE,
+                'not null' => TRUE)),
+  			'primary key' => array('node_id', 'topic_id'),
+  );
+  
+  return $schema;
+  
 }
\ No newline at end of file
Index: phpbbforum.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/phpbbforum/phpbbforum.module,v
retrieving revision 1.27
diff -u -r1.27 phpbbforum.module
--- phpbbforum.module	22 Jun 2008 17:04:11 -0000	1.27
+++ phpbbforum.module	24 Sep 2008 17:18:24 -0000
@@ -48,13 +48,13 @@
 function _phpbbforum_set_globals() {
   global $phpbb_config, $base_url, $phpbb_root_path,
     $site_base_url, $site_phpbb_page, $site_forum_url, $phpbb_integration_mode;
-
+    
   $site_base_url = $base_url;
   $site_phpbb_page =  drupal_get_path_alias(PHPBB_PAGE);
   $phpbb_root_path = variable_get('phpbbforum_root', realpath('.') . PHPBB_PATH);
 
   $phpbb_integration_mode = variable_get("phpbbforum_page_frame", 0);
-
+  
   //$phpbburl = $phpbb_config['forum_url'];
   $site_forum_url = $base_url ."/". $site_phpbb_page;
   //$site_forum_url = $base_url ."?q=". $site_phpbb_page;
@@ -74,6 +74,7 @@
     }  
   }
   */
+  
 }
 
 function _phpbbforum_replace_links($output) {
@@ -326,6 +327,8 @@
  * Implementation of hook_form_alter().
  */
 function phpbbforum_form_alter(&$form, $form_state, $form_id) { // 6.x
+  
+  global $phpbb_user;
     
   if ($form_id == 'user_login_block' || $form_id == 'user_login') {
     $form['#validate'] = phpbbforum_login_default_validators($form);
@@ -355,6 +358,75 @@
       $form['links'] = array('#value' => theme('item_list', $items));
     }
   }
+  elseif ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
+    // Alter the node type's configuration form to add our setting. We don't
+    // need to worry about saving this value back to the variable, the form
+    // we're altering will do it for us.
+    $form['phpbbforum'] = array(
+      '#type'  => 'fieldset',
+      '#title' => t('phpBBforum settings'),
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE,
+    );
+
+    $form['phpbbforum']['phpbbforum_submission'] = array(
+      '#type' => 'radios',
+      '#title' => t('Drupal to phpBB node-submission'),
+      '#default_value' => variable_get('phpbbforum_submission_'. $form['#node_type']->type, 0),
+      '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
+      '#description' => t('Should this node be able to submit a post to the phpBB forum?'),
+    );
+    
+    $form['phpbbforum']['phpbbforum_commentlink_default'] = array(
+      '#type' => 'checkbox',
+      '#title' => t("Add comment link"),
+      '#default_value' => variable_get('phpbbforum_commentlink_default_'.$form['#node_type']->type, 0),
+      '#description' => t("Default setting")
+    );
+
+    $form['phpbbforum']['phpbbforum_commentlink_text_nocomments'] = array(
+      '#type' => 'textfield',
+      '#title' => t("Link text when no comments are posted"),
+      '#default_value' => variable_get('phpbbforum_commentlink_text_nocomments_'.$form['#node_type']->type, ''),
+    );
+    
+    $form['phpbbforum']['phpbbforum_commentlink_text_comments'] = array(
+      '#type' => 'textfield',
+      '#title' => t("Link text when one or more comments are posted"),
+      '#default_value' => variable_get('phpbbforum_commentlink_text_comments_'.$form['#node_type']->type, ''),
+    );
+    
+  }
+  // If the type and node field are set this may be a node edit form.
+  elseif (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
+    $node = $form['#node'];
+    if (variable_get('phpbbforum_submission_'. $form['type']['#value'], 0)) {
+      
+      $defaults = _phpbbforum_getDefaults ($node->nid, $node->type);
+
+	   $form['phpbbforum'] = array(
+	     '#type' => 'fieldset',
+	     '#title' => t('phpBBforum Settings'),
+	     '#collapsible' => TRUE,
+	     '#collapsed' => FALSE,
+	   );
+	   
+      $form['phpbbforum']['phpbbforum_forumid'] = array(
+        '#type' => 'select',
+        '#title' => t('Submit this node to'),
+        '#default_value' => $defaults['forum'],
+        '#description' => t('If you choose a forum, a phpbb-thread with the node\'s content will be created in the specified forum.'),
+      	'#options' => _phpbbforum_getAccessableForums($phpbb_user->data['user_id']),
+      );
+      
+      $form['phpbbforum']['phpbbforum_commentlink'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Add comment link'),
+        '#default_value' => $defaults['linked'],
+      );
+      
+    }
+  }
   elseif ($form_id == 'user_register') {
   
   }
@@ -364,6 +436,268 @@
   return $form;
 }
 
+/**
+ * Specifies the default parameters for the node.
+ * Checks if the node was already submitted and gets the parameters used.
+ *
+ * @param integer $node_id
+ * @return array default: array ('forum' => 0, 'linked' => 0); 
+ */
+function _phpbbforum_getDefaults ($node_id, $node_type) {
+  
+  $defaults = array (
+    'forum' => 0,
+    'linked' => variable_get ('phpbbforum_commentlink_default_'.$node_type, 0),
+  ); 
+   
+   if (!is_numeric ($node_id))
+     return $defaults;
+
+   $topic = db_fetch_object (db_query ('SELECT topic_id, linked FROM {phpbbforum_drupal2phpbb} WHERE node_id = %d', $node_id));
+
+   if (is_object ($topic)) {
+
+     $postInformation = _phpbbforum_getPostInformation($topic->topic_id);
+     if (isset($postInformation['forum_id']) && is_numeric ($postInformation['forum_id']) )
+       $defaults['forum'] = $postInformation['forum_id'];
+     
+     $defaults['linked'] = $topic->linked;
+       
+   }
+   
+   return $defaults;
+    
+}
+
+/**
+ * Returns an array with the forums, in which the user can create a thread
+ * and post messages.
+ *
+ * @param integer $user_id
+ * @return array (forum_id => forum_name);
+ */
+function _phpbbforum_getAccessableForums ($user_id) {
+  
+  $forums = array(0 => t('Don\'t submit this node'));
+  
+  if (!is_numeric ($user_id))
+    return $forums;
+  
+  global $auth, $db;
+
+  /**
+   * Just get the forums 
+   * - which are no categories (forum_type)
+   * - which are not closed (forum_status)
+   */
+  $sql = "SELECT forum_id, forum_name FROM ". FORUMS_TABLE ." WHERE forum_type != 0 AND forum_status != 1";
+  $db->sql_query ($sql);
+  while ($forum = $db->sql_fetchrow()) {
+    
+    // Check if the current user can post in this forum
+    if ($auth->acl_get("f_post", $forum['forum_id']) && $forum['forum_id'] !== 0)
+      $forums[$forum['forum_id']] = html_entity_decode($forum['forum_name']);
+
+  }
+  
+  return $forums;
+  
+}
+
+/**
+ * Implemantation of hook_nodeapi()
+ */
+function phpbbforum_nodeapi (&$node, $op, $teaser, $page) {
+  
+  if (!_phpbbforum_settings())
+    return;
+  
+  global $user;
+
+  switch ($op) {
+  
+    case 'validate':
+
+      if (variable_get ('phpbbforum_submission_'.$node->type, FALSE)) {
+        if (!_phpbbforum_nodeapi_validate_forumid ($node->phpbbforum_forumid)) 
+          form_set_error ('phpbbforum_forumid', t('Cannot submit node to the selected forum. Please try again.'));
+      }
+
+    break;
+
+    case 'insert':
+    case 'update':
+      
+      if (!variable_get ('phpbbforum_submission_'.$node->type, FALSE))
+        break;
+      if ($node->phpbbforum_forumid == 0)
+        break;
+        
+      $filteredText = check_markup ($node->body, $node->format);
+      
+      // data which will be provided by the content creation
+      $data['topic_title'] = check_markup ($node->title, $node->format);
+      $data['message'] = $filteredText;
+      $data['message_md5'] = md5 ($filteredText);
+      $data['forum_id'] = $node->phpbbforum_forumid;
+      
+      // using the approved mechanism for publishing and unpublishing topics
+      $approved = ($node->status == 1 ) ? 1 : 0;
+      $data['topic_approved'] = $approved;
+      $data['post_approved'] = $approved;
+      
+      if ($node->phpbbforum_commentlink != 1)
+        $node->phpbbforum_commentlink = 0;
+
+      // Hardcoded data to get the party working
+      // maybe it would be better to be able to fill this data via node creation as well
+      // but it's not necessary atm
+      $data['icon_id'] = 1;
+      $data['enable_bbcode'] = 1;
+      $data['enable_smilies'] = 1;
+      $data['enable_urls'] = 1;
+      $data['enable_sig'] = 1;
+      $data['bbcode_bitfield'] = '';
+      $data['bbcode_uid'] = '';
+      $data['post_edit_locked'] = 0;
+      $poll = array ();
+      
+      // changing the globals to the phpbb (like $user)
+      phpbb_save();
+      $post = db_fetch_object (db_query ("SELECT topic_id FROM {phpbbforum_drupal2phpbb} WHERE node_id = %d", $node->nid));
+      if ($post === false) {
+
+        submit_post('post', $node->title, $user->data['username'], POST_NORMAL, $poll, $data);
+        phpbb_load();
+        
+        if (is_numeric ($node->nid) && isset ($data['topic_id']) && is_numeric ($data['topic_id']))
+          db_query('INSERT INTO {phpbbforum_drupal2phpbb} (node_id, topic_id, linked) VALUES (%d, %d, %d)', $node->nid, $data['topic_id'], $node->phpbbforum_commentlink);
+        
+      } else {
+        
+        $postInformation = _phpbbforum_getPostInformation($post->topic_id);
+        $data['topic_id'] = $post->topic_id;
+        $data['topic_replies_real'] = $postInformation['countPosts']; 
+        $data['topic_first_post_id'] = $postInformation['post_id'];
+        $data['post_id'] = $postInformation['post_id'];
+        $data['poster_id'] = (int) $user->data['user_id'];
+        submit_post('edit', $node->title, $user->data['username'], POST_NORMAL, $poll, $data, TRUE);
+        phpbb_load();
+        
+        if (is_numeric ($node->nid) && isset ($data['topic_id']) && is_numeric ($data['topic_id']))
+          db_query('UPDATE {phpbbforum_drupal2phpbb} SET topic_id = %d, linked = %d WHERE node_id = %d', $data['topic_id'], $node->phpbbforum_commentlink, $node->nid);
+        
+      }
+    
+    break;
+  
+  }
+    
+}
+
+/**
+ * Validates the given forum_id against the accessable forums of the current user.
+ *
+ * @param integer $forum_id
+ * @return boolean false on error true otherwise
+ */
+function _phpbbforum_nodeapi_validate_forumid ($forum_id) {
+  
+  $return = false;
+  
+  if (!is_numeric ($forum_id))
+    return $return;
+    
+  global $phpbb_user;
+    
+  $forums = _phpbbforum_getAccessableForums($phpbb_user->data['user_id']);
+  
+  if (isset ($forums[$forum_id]))
+    $return = true;
+  
+  return $return;
+  
+}
+
+/**
+ * Implementation of hook_link()
+ */
+function phpbbforum_link ($type, $object, $teaser = FALSE) {
+  
+  global $phpbb_root_path, $phpEx;
+  
+  $links = array ();
+  
+  if ($type == 'node')
+  {
+    
+    $map = db_fetch_object (db_query ('SELECT topic_id, linked FROM {phpbbforum_drupal2phpbb} WHERE node_id = %d', $object->nid));
+    
+    if ($map === FALSE || $map->linked == 0)
+      return array();
+  
+    $postInformation = _phpbbforum_getPostInformation($map->topic_id);
+    
+    if (empty ($postInformation) || !isset ($postInformation['forum_id']) || !isset ($postInformation['topic_id']) || !isset ($postInformation['post_id']))
+      return array();
+
+    if ($postInformation['countPosts'] === 0)
+      $title = t('!comments '.variable_get('phpbbforum_commentlink_text_nocomments_'.$object->type, ''), array ('!comments' => format_plural ($postInformation['countPosts'], '1 comment', '@count comments')));
+    else
+      $title = t('!comments '.variable_get('phpbbforum_commentlink_text_comments_'.$object->type, ''), array ('!comments' => format_plural ($postInformation['countPosts'], '1 comment', '@count comments')));
+    
+    $links['phpbbforum_comment'] = array (
+      'title' => $title,
+      'href' => html_entity_decode(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=".$postInformation['forum_id']."&t=".$postInformation['topic_id']."#p".$postInformation['post_id'])),
+    );
+
+  }
+  
+  return $links;
+  
+}
+
+/**
+ * Gets information of the given topic and returns an array
+ * 
+ * 'countPosts' - how many posts
+ * 'forum_id'   - which forumid
+ * 'post_id'    - which is the first postid 
+ * 'topic_id'   - the topic id 
+ *
+ * @param integer $topic_id
+ * @return array 
+ */
+function _phpbbforum_getPostInformation($topic_id) {
+  
+  if (!is_numeric ($topic_id))
+    return array();
+  
+  global $db;
+    
+  $countRows = 0;
+
+  $sql = 'SELECT post_id, topic_id, forum_id FROM '. POSTS_TABLE . ' AS p 
+   		WHERE topic_id = '.$topic_id;
+  
+  $db->sql_query ($sql);
+  $post = $db->sql_fetchrow();
+  $countRows = $db->sql_affectedrows() - 1;
+  
+  if ($countRows === false || $countRows < 0 || !is_numeric ($countRows))
+    $countRows = 0;
+  
+  $postInformation = array (
+								'forum_id' => $post['forum_id'],
+                                'topic_id' => $post['topic_id'],
+                                'post_id' => $post['post_id'],
+                                'countPosts' => $countRows,
+                           );
+    
+  return $postInformation;
+  
+}
+
 function phpbbforum_login_default_validators($form) {
   
   $validators = array();
@@ -451,7 +785,7 @@
     $phpbbapifile = PHPBB_API_FILE;
     variable_set('phpbbforum_api_file', $phpbbapifile);
   }
-
+  
   if (file_exists($phpbbsettings)) {
     $phpbbsettings_exists = true;
     $form['phpbbstatus']['info3'] = array('#type' => 'markup',
@@ -476,6 +810,7 @@
         $phpbbinc = $phpbb_root_path;
       }
     }
+    
   }
   else {
     $phpbbsettings_exists = false;
@@ -537,6 +872,7 @@
       '#maxlength' => '128', '#size' => '60',
       '#default_value' => $phpbbapifile,
       '#description' => t('Enter phpBB api file name.'));
+
   }
 
   if ($phpbb_connection) {
Index: includes/phpbb_api.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/phpbbforum/includes/phpbb_api.php,v
retrieving revision 1.12
diff -u -r1.12 phpbb_api.php
--- includes/phpbb_api.php	21 May 2008 18:31:34 -0000	1.12
+++ includes/phpbb_api.php	24 Sep 2008 17:18:25 -0000
@@ -1,7 +1,7 @@
 <?php
 // $Id: phpbb_api.php,v 1.12 2008/05/21 18:31:34 vb Exp $
 /**
- * Copyright (Ñ) 2007-2008 by Vadim G.B. (http://vgb.org.ru)
+ * Copyright (ï¿½) 2007-2008 by Vadim G.B. (http://vgb.org.ru)
  */
 
 define('IN_PHPBB', true);
@@ -89,6 +89,7 @@
 require_once($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
 require_once($phpbb_root_path . 'includes/hooks/index.' . $phpEx);
 require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
+require_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
 /////////////////////////////////////////
 require_once(dirname(__FILE__) . '/phpbb_api_subs.php');
 
