<?php

require_once('./modules/mse-drupal-modules/autoformat.inc');

global $nice_ff_names;
$nice_ff_names = array(
	'Function' => 'Functions',
	'Type'     => 'Types',
	'Script'   => 'Scripting'
);

define('FROMFILE_DIR', 'doc');

/**
 * @file
 * Read pages from the filesystem
 */

/**
 * Implementation of hook_help().
 */
function fromfile_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Read pages from the filesystem.');
    case 'admin/settings/search':
      return t('From file path settings');
  }
}

/**
 * Implementation of hook_menu().
 */
function fromfile_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => FROMFILE_DIR,
      'title' => t('fromfile'),
      'callback' => 'fromfile_page',
      'access' => user_access('access content'),
      'type' => MENU_SUGGESTED_ITEM);
  }

  return $items;
}


/**
 * Menu callback: show file
 */
function fromfile_page() {
  global $nice_ff_names;
  $path = variable_get('fromfile_path', '');
  if ($path=='') return drupal_not_found();
  $args = func_get_args();
  $file = implode('/', $args);
  
  $file = str_replace('..','',$file);
  // open file
  if (preg_match("/\.(png|jpg|gif)$/",$file,$type) && file_exists("$path/$file")) {
     $type = $type[1];
     if ($type == 'jpg') $type = 'jpeg';
     header("Content-Type: image/$type");
     if ($fd = fopen("$path/$file", 'rb')) {
       fpassthru($fd);
       exit();
     } else {
       echo "ERROR Opening image file";
     }
  }
  $file1 = "$path/$file.txt";
  $file2 = "$path/$file/index.txt";
  $file1 = str_replace('//','/',$file1);
  $file2 = str_replace('//','/',$file2);
  if (file_exists($file1)) {
	$full_path = $file1;
  } elseif (file_exists($file2)) {
	$full_path = $file2;
  } else {
	return drupal_not_found();
  }
  $raw = file($full_path);
  global $current_doc_file;
  $current_doc_file = "$file";
  
  foreach($raw as $k=>$l) $raw[$k] = preg_replace("/\r?\n/","",$l);
  
  // format the file
  $title   = array_shift($raw);
  $content = autoformat($raw);
  
  // breadcrumbs
  $breadcrumbs = array(l(t('Home'), NULL));
  $path = FROMFILE_DIR;
  for ($i = -1 ; $i < count($args) ; ++$i) {
    if ($i != -1) $path .= '/' . $args[$i];
    if ($i == count($args) - 1) {
	$name = $title;
	$name = preg_replace("/.*: ?/","",$name);
    } elseif ($i == -1) {
	$name = 'Documentation';
    } else {
	$name = ucfirst($args[$i]);
	if (isset($nice_ff_names[$name])) $name = $nice_ff_names[$name];
    }
    $breadcrumbs[] = l(t($name), $path);
  }
  
  // done
  drupal_set_breadcrumb($breadcrumbs);
  drupal_set_title($title);
  // view as a node
  $node = array(
	'title'   => $title,
	'body'    => $content,
	'created' => 0,
	'links'   => FALSE,
	'name'    => '',
	'nid'     => ''
  );
  //return node_view((object)$node, FALSE, TRUE);
  return theme('node', (object)$node, FALSE, TRUE);
}



/**
 * Menu callback; displays a module's settings page.
 */
function fromfile_settings() {

  // From file settings:
  $form['fromfile'] = array(
    '#type' => 'fieldset', '#title' => t('From file settings'),
    '#collapsible' => TRUE, '#collapsed' => FALSE
  );
  $form['fromfile']['fromfile_path'] = array(
    '#type' => 'textfield', '#title' => t('Path'), '#default_value' => variable_get('fromfile_path', ''),
    '#description' => t('Path for the fromfile module.'), '#required' => FALSE
  );
  
  return $form;
}
