All pastes #510795 Raw Edit

Unnamed

public text v1 · immutable
#510795 ·published 2007-05-26 02:08 UTC
rendered paste body
<?php

// $Id: phorum_connector.php,v 1.1 2007/02/18 12:38:26 alfaguru Exp $

class drupalPhorumConnector extends PhorumConnectorBase {

	var $_elements = array();
	var $name = 'Drupal/Phorum integration';
  var $page_path = '';

  function drupalPhorumConnector($page) {
  	$this->page_path = variable_get('phorum_integrate_url', 'discuss'). '/'. $page;
  }

  /**
   * Return the user_id for the logged in user.
   * (automatically syncs the user in the Phorum DB when required)
   * @return unknown
   */
  function get_user_id() {
  	global $user;
  	if($user->uid) {
  		$table_prefix = variable_get('phorum_integrate_db_prefix','phorum_');
  		$user_table = $table_prefix.'users';
    	$result = db_fetch_array(db_query('SELECT active, user_id FROM {'.$user_table.'} WHERE user_id = %d', $user->uid));
    	if(!$result) {
        // if the user is not in the phorum user table but has access,
        // insert an entry in the Phorum user table for them
    	  if(user_access('access forum')) {
	    		$id = $user->uid;
	    		$name = $user->name;
	    		$email = $user->mail;
	    		$admin = user_access('administer phorum');
	    		db_query("INSERT INTO {".$user_table."} (user_id, username, email, admin, active) VALUES (%d, '%s', '%s', %d, 1)", $id, $name, $email, $admin);
    		}
    	} else{
        // the user is known to phorum
        // if their access is different to their active state
        // update their entry (drupal setting overrides the phorum one)
    	  $active = $result['active'];
    		$access = user_access('access phorum');
    		if($access != $active) {
    			db_query('UPDATE {'.$user_table.'} SET active = '.(int)$access.' WHERE user_id='.$user->uid);
    		}
    	}
    	return $user->uid;
  	} else {
  		return '';
  	}
  }

  /**
   * Handle the page elements generated by Phorum
   *
   * @param array $elements
   */
  function process_page_elements($elements) {
  	$this->_elements = $elements;
  }

  /**
   * Get a page element by name
   *
   * @param string $name
   * @return mixed
   */
  function get_element($name) {
    return array_key_exists($name, $this->_elements)? $this->_elements[$name]: '';
  }

  /**
   * Translate a Phorum URL into its Drupal equivalent
   *
   * @param string $page
   * @param array $query_items
   * @param string $suffix
   * @return string
   */
  function get_url($page, $query_items, $suffix) {
  	$path = variable_get('phorum_integrate_url', 'discuss'). '/'. $page;
    if(! variable_get('clean_url', 0)) {
      $path = "?q=$path";
    }
    $url = ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
         ? "https://" : "http://") .
         $_SERVER["HTTP_HOST"] . '/'. $path;
    if (count($query_items)) $url .= "," . implode(",", $query_items);
    if (!empty($suffix)) $url .= $suffix;

    return $url;
  }

  /* This method parses the request and must take care of:
   *
   * - setting the query string that Phorum should use in Phorum's query
   *   override variable $PHORUM_CUSTOM_QUERY_STRING
   * - returning the Phorum page that should be shown
   *
   * @param $qstr - The HTTP query string to process
   * @return The Phorum page to show
   */
  function parse_request($qstr) {
  	$qstr = $_GET['q'];

    // remove the Drupal part of the path from the query string
  	$path = variable_get('phorum_integrate_url', 'discuss');
    $qstr = substr($qstr, strlen($path) + 1);

    // In case the Phorum page is at the start of the query string.
    if (preg_match("/^(\w[\w_-]+)(,|$)/", $qstr, $match)) {
      $query = str_replace($match[0], "", $qstr);
      $GLOBALS["PHORUM_CUSTOM_QUERY_STRING"] = $query;
      $page = basename($match[1]);
    // In case the Phorum page is sent as page=<page> in the query string.
    } elseif (isset($_REQUEST["page"])) {
      $page = basename($_REQUEST["page"]);
      $getparts = array();
      foreach (explode("&", $qstr) as $q) {
        if (substr($q, 0, 5) != "page=") {
            $getparts[] = $q;
        }
      }
      $GLOBALS["PHORUM_CUSTOM_QUERY_STRING"] = implode(",", $getparts);
    // We do not know what page we are handling now. Fallback to the index.
    } else {
      $page="index";
    }
    return $page;

  }
  /**
   * This method must return the Phorum template to use or NULL
   * in case Phorum can decide what template to use.
   *
   * @return The name of the Phorum template to use.
   */
  function get_template() {
  	return variable_get('phorum_integrate_template', 'embed_phorum');
  }

  /**
   * Called during Phorum initialization
   *
   */
  function hook_common_pre() {
    // when not using clean urls need to add an extra hidden field
    // to forms so search etc works
    if(! variable_get('clean_url', 0)) {
      $GLOBALS["PHORUM"]["DATA"]["POST_VARS"] .=
        "<input type=\"hidden\" name=\"q\" value=\"{$this->page_path}\"/>\n";
    }

  }
}