<?php
/**
  @licence GPL 2005-2010  The osCSS developers - osCSS Open Source E-commerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.0.9
  @date  28/05/10, 17:03
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8

  @param GET
    @a string $_GET['set']
    @a string $_GET['manager'] le type de prise en charge, (general|process|plugin)

*/


  require_once(DIR_WS_FUNCTIONS . 'lib.configuration.php');

  $set = (isset($_GET['set']) ? $_GET['set'] : '');
  $manager = (isset($_GET['manager']) ? $_GET['manager'] : 'general');
  $module_language = DIR_FS_CATALOG_LANGUAGES;

  switch ($manager){
    case 'process':
      $code_inistalletd_array['checkout_process']=array('key'=>'MODULE_CHECKOUT_PROCESS_INSTALLED', 'value'=>MODULE_CHECKOUT_PROCESS_INSTALLED, 'module_directory'=> 'process/');
      $code_inistalletd_array['shipping']=array('key'=>'MODULE_SHIPPING_INSTALLED', 'value'=>MODULE_SHIPPING_INSTALLED, 'module_directory'=>'shipping/');
      $code_inistalletd_array['payment']=array('key'=>'MODULE_PAYMENT_INSTALLED', 'value'=>MODULE_PAYMENT_INSTALLED, 'module_directory'=> 'payment/');
      $code_inistalletd_array['order_total']=array('key'=>'MODULE_ORDER_TOTAL_INSTALLED', 'value'=>MODULE_ORDER_TOTAL_INSTALLED, 'module_directory'=> 'order_total/');
    break;
    case 'general':
    default:
      $code_inistalletd_array['generic']=array('key'=>'MODULE_GENERIC_INSTALLED', 'value'=>MODULE_GENERIC_INSTALLED, 'key_BO'=>'MODULE_GENERIC_INSTALLED_BO', 'value_BO'=>MODULE_GENERIC_INSTALLED_BO,'module_directory'=>'generic/','module_language'=>DIR_FS_ADMIN.'includes/languages/');
      $code_inistalletd_array['products']=array('key'=>'MODULE_ACAPRO_INSTALLED', 'value'=>MODULE_ACAPRO_INSTALLED,'key_BO'=>'MODULE_ACAPRO_INSTALLED_BO', 'value_BO'=>MODULE_ACAPRO_INSTALLED_BO,'module_directory'=> 'products/','module_language'=>DIR_FS_ADMIN.'includes/languages/' );
      $code_inistalletd_array['content']=array('key'=>'MODULE_CONTENT_INSTALLED', 'value'=>MODULE_CONTENT_INSTALLED, 'key_BO'=>'MODULE_CONTENT_INSTALLED_BO', 'value_BO'=>MODULE_CONTENT_INSTALLED_BO,'module_directory'=>'content/','module_language'=>DIR_FS_ADMIN.'includes/languages/');
      $code_inistalletd_array['categories']=array('key'=>'MODULE_ACACAT_INSTALLED', 'value'=>MODULE_ACACAT_INSTALLED ,'key_BO'=>'MODULE_ACACAT_INSTALLED_BO', 'value_BO'=>MODULE_ACACAT_INSTALLED_BO,'module_directory'=> 'categories/','module_language'=>DIR_FS_ADMIN.DIR_WS_LANGUAGES );
      $code_inistalletd_array['account']=array('key'=>'MODULE_ACCOUNT_INSTALLED', 'value'=>MODULE_ACCOUNT_INSTALLED,'key_BO'=>'MODULE_ACCOUNT_INSTALLED_BO', 'value_BO'=>MODULE_ACCOUNT_INSTALLED_BO,'module_directory'=> 'account/','module_language'=>DIR_FS_ADMIN.DIR_WS_LANGUAGES  );
      $code_inistalletd_array['listing']=array('key'=>'MODULE_LISTING_INSTALLED', 'value'=>MODULE_LISTING_INSTALLED,'key_BO'=>'MODULE_LISTING_INSTALLED_BO', 'value_BO'=>@MODULE_LISTING_INSTALLED_BO,'module_directory'=> 'listing/','module_language'=>DIR_FS_ADMIN.DIR_WS_LANGUAGES );
      $code_inistalletd_array['home']=array('key'=>'MODULE_HOME_INSTALLED', 'value'=>MODULE_HOME_INSTALLED, 'key_BO'=>'MODULE_HOME_INSTALLED_BO', 'value_BO'=>MODULE_HOME_INSTALLED_BO ,'module_directory'=>'home/','module_language'=>DIR_FS_ADMIN.DIR_WS_LANGUAGES );
  }


  switch ($set) {
    case '':
        $module_type = '';
        $module_directory = DIR_FS_CATALOG_MODULES . '';
        $module_key = 'MODULE_PAYMENT_INSTALLED';
        define('HEADING_TITLE', HEADING_TITLE_MODULE_PAYMENT);
        break;
    case (tep_not_null($set) && isset($code_inistalletd_array[$set])):
        $module_type = $set;
        $module_directory = $code_inistalletd_array[$set]['module_directory'];
        $module_key = $code_inistalletd_array[$set]['key'];
        define('HEADING_TITLE', constant('HEADING_TITLE_'.str_replace('_INSTALLED','',$code_inistalletd_array[$set]['key'])));
        if(isset($code_inistalletd_array[$set]['module_language']))$module_language= $code_inistalletd_array[$set]['module_language'];
    break;
  }



if (tep_not_null($action)) {
  switch ($action) {
    case 'setflag':
      $t=$ts=array();
      $module=$_GET['set'];
      $tclass=$_GET['class'];
      $flag=$_GET['flag'];
      $core=(!isset($_GET['core']))? '' :'_'.$_GET['core'];
      $t=(tep_not_null($code_inistalletd_array[$module]['value'.$core])? explode(';',$code_inistalletd_array[$module]['value'.$core]) : array() );
      if($flag ==0) {
        foreach($t as $v){
          if(!empty($v) && $tclass.'.php' != $v)$ts[]=$v;
        }
      }
      else {
        $ts=((count($t)>0)?$t:array());
        $ts[]=$tclass.'.php';
      }
      if(count($ts)==1)$ts[]='';
      $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = '" . $code_inistalletd_array[$module]['key'].$core . "'");
      if (tep_db_num_rows($check_query)) {
        $check = tep_db_fetch_array($check_query);
        tep_db_query($sql="update " . TABLE_CONFIGURATION . " set configuration_value = '" . implode(';', $ts) . "', last_modified = now() where configuration_key = '" . $code_inistalletd_array[$module]['key'].$core . "'");

      } else {
        tep_db_query($sql="insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Installed Modules ".$type."', '" . $code_inistalletd_array[$module]['key'].$core . "', '" .$ts . ";', 'This is automatically updated. No need to edit.', '6', '0', now())");
      }

      tep_redirect(tep_href_link(FILENAME_MODULES, 'manager='.$manager.'&set=' . $set ));

    break;
    case 'save':
      while (list($key, $value) = each($_POST['configuration'])) {
        tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $value . "' where configuration_key = '" . $key . "'");
      }
      tep_redirect(tep_href_link(FILENAME_MODULES, 'manager='.$manager.'&set=' . $set ));
    break;
//       case 'install':
//       case 'remove':
//         $file_extension = substr($PHP_SELF, strrpos($PHP_SELF, '.'));
//         $class = basename($_GET['module']);
//         include_once($module_language . $language . '/modules/' . $set . '/' . $class.$file_extension);
//         if (file_exists($module_directory . $class . $file_extension)) {
//           require_once($module_directory . $class . $file_extension);
//           $module = new $class;
//           if ($action == 'install') {
//             $module->install();
//           } elseif ($action == 'remove') {
//             $module->remove();
//           }
//         }
//         tep_redirect(tep_href_link(FILENAME_MODULES, 'set=' . $set . '&module=' . $class));
//         break;
    }
  }



  $file_extension = '.php';
  $directory_array =$modules_array= array();
  foreach($code_inistalletd_array as $key=>$value) {
   $module_directory3235=DIR_FS_CATALOG_MODULES.$value['module_directory'];
   $module_directory_admin32355=DIR_FS_ADMIN.DIR_WS_MODULES.$value['module_directory'];
    if ($dir = @dir($module_directory3235)) {
        while ($file = $dir->read()) {

            if (!is_dir($module_directory3235 . $file) ){
                $f=substr($file, 0, (strlen($file)-strlen($file_extension)));
                if (substr($file, strrpos($file, '.')) == $file_extension) {
                    if (file_exists($module_directory_admin32355.$file))$modules_array[$key][$f]['path_BO']=$module_directory_admin32355;
                    if(file_exists($module_directory3235.$file)) $modules_array[$key][$f]['path']=$module_directory3235;
                    $modules_array[$key][$f]['file']=$file;
                    $modules_full[$f]='';
                }
            }
        }
        sort($directory_array);
        $dir->close();
    }
    if ($dir = @dir($module_directory_admin32355)) {
        while ($file = $dir->read()) {

            if (!is_dir($module_directory_admin32355 . $file) ){
                $f=substr($file, 0, (strlen($file)-strlen($file_extension)));
                if (substr($file, strrpos($file, '.')) == $file_extension) {
                    if (file_exists($module_directory_admin32355.$file))$modules_array[$key][$f]['path_BO']=$module_directory_admin32355;
                    if(file_exists($module_directory3235.$file)) $modules_array[$key][$f]['path']=$module_directory3235;
                    $modules_array[$key][$f]['file']=$file;
                    $modules_full[$f]='';
                }
            }
        }
        sort($directory_array);
        $dir->close();
    }
  }

?>