<?php if (!defined('HTTP_SERVER')) die('You can not access this file directly!');
/**
  @licence GPL 2005-2010  The osCSS developers - osCSS Open Source E-commerce
  @portion code Copyright (c) 2002 osCommerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.1.0
  @date  02/10/10, 22:52
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8
  @class plugins
*/

if(!class_exists('package')) include_once(DIR_WS_MODULES .'pages/package.php');

class plugins
  extends package
  implements ExtModPage,  InterfaceModule {

  const FILENAME =FILENAME_PLUGINS;

  public static $code;

  public static $action;

  public static $list=array();


  public static $code_inistalletd_array;

  public static $file_extension;

  private static $plugin_directory;

  private static $src;

  /**
    class constructor
  */
  public function __construct() {
    self::$code=__CLASS__;


  self::$file_extension = '.php';
  $set = (isset($_GET['set']) ? $_GET['set'] : '');


      self::$code_inistalletd_array['generic']=array('key'=>'PLUGIN_GENERIC_INSTALLED', 'value'=>PLUGIN_GENERIC_INSTALLED);
      self::$code_inistalletd_array['product']=array('key'=>'PLUGIN_PRODUCT_INSTALLED', 'value'=>PLUGIN_PRODUCT_INSTALLED  );
//       $code_inistalletd_array['categories']=array('key'=>'MODULE_ACACAT_INSTALLED', 'value'=>MODULE_ACACAT_INSTALLED  );
      self::$code_inistalletd_array['account']=array('key'=>'PLUGIN_ACCOUNT_INSTALLED', 'value'=>PLUGIN_ACCOUNT_INSTALLED  );
      self::$code_inistalletd_array['listing']=array('key'=>'PLUGIN_LISTING_INSTALLED', 'value'=>PLUGIN_LISTING_INSTALLED );
      self::$code_inistalletd_array['home']=array('key'=>'PLUGIN_HOME_INSTALLED', 'value'=>PLUGIN_HOME_INSTALLED  );




      $module_type = '';
      self::$plugin_directory = DIR_FS_TEMPLATES . TEMPLATE_NAME . '/includes/plugins/' ;

  require(DIR_WS_FUNCTIONS . 'lib.configuration.php');
  }

  public function check_action($actions){
    global $messageStack, $oscss;

    self::$action=$actions;
  switch (self::$action) {
    case 'setflag':
      $t=$ts=array();
      $module=$_GET['set'];
      $tclass=$_GET['class'];
      $flag=$_GET['flag'];
      $core=(!isset($_GET['core']))? '' :'_'.$_GET['core'];
      $t=explode(';',self::$code_inistalletd_array[$module]['value'.$core]);
      if($flag ==0) {
        foreach($t as $v){
          if($tclass.'.php' != $v)$ts[]=$v;
        }
      }
      else {
        $ts=$t;
        $ts[]=$tclass.'.php';
      }

      $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = '" . self::$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 = '" . self::$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."', '" . self::$code_inistalletd_array[$module]['key'].$core . "', '" . implode(';', $ts) . "', 'This is automatically updated. No need to edit.', '6', '0', now())");
      }

      //! purge cache  sys
      osCSS_Cache::clear('plugins');
      osCSS_Cache::clear('Global');

      tep_redirect(tep_href_link(self::FILENAME));

    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_PLUGINS, 'set=' . $set . '&module=' . $_GET['module']));
    break;
    }
    return self::$action;
  }

  public function get_header(){
    ?>
    <script type="text/javascript">
    $(function(){
      $(".tirroir").slideToggle("slow");
      $("h5").click(function () {
	$(this).next().slideToggle("slow");

      });
    });
    </script>
    <?php
  }

  public static function load_db_values($ID){ return false; }


  public static function tep_get_list($page=1,$rowbyp=10,$sOrder='',$sWhere='',$options=''){

    $class='packageRepository';
    if(!tep_class_exists('exts_package_'.$class)) die('class no found '.$class);
    $objChild=new $class;
    $objChild->SourceUpdate();
    self::$src=parent::load_repository();

    $modules_array= array();
    foreach(self::$code_inistalletd_array as $key=>$value) {
      $plugin_directory3235=self::$plugin_directory.$key.'/';

      $modules_array=self::cstr_menu($plugin_directory3235,$modules_array,$key);

    }
  }

  public function display_view(){

    switch(self::$action){

      default:
      self:: tep_get_list();
      return tep_get_include_contents(__CLASS__.'.listing');
    }
  }

  public static function getrightcolum(){
    $heading = array();
    $contents = array();

    switch(self::$action){
      case 'edit':

    @include_once(DIR_FS_CATALOG. 'templates/'.TEMPLATE_NAME.'/includes/plugins/' . $_GET['set']. '/'. $_GET['class']  . '/languages/' . $language .'.txt');

    require_once(DIR_FS_CATALOG. 'templates/'.TEMPLATE_NAME.'/includes/plugins/' . $_GET['set'] . '/'. $_GET['class'] . '/admin_config.php');


      $module = new $_GET['class'];

      if(!$module->check()){
          $module->remove();
          $module->install();
      }

      $module_info = array('code' => $module->code,
                      'title' => $module->title,
                      'description' => $module->description,
                      'status' => $module->check());
      $keys = '';
       $module_keys = $module->keys();

        $keys_extra = array();
        for ($j=0, $k=sizeof($module_keys); $j<$k; $j++) {
          $key_value_query = tep_db_query("select configuration_title, configuration_value, configuration_description, use_function, set_function from " . TABLE_CONFIGURATION . " where configuration_key = '" . $module_keys[$j] . "'");
          $key_value = tep_db_fetch_array($key_value_query);

          $keys_extra[$module_keys[$j]]['title'] = $key_value['configuration_title'];
          $keys_extra[$module_keys[$j]]['value'] = $key_value['configuration_value'];
          $keys_extra[$module_keys[$j]]['description'] = $key_value['configuration_description'];
          $keys_extra[$module_keys[$j]]['use_function'] = $key_value['use_function'];
          $keys_extra[$module_keys[$j]]['set_function'] = $key_value['set_function'];
        }

        $module_info['keys'] = $keys_extra;

        $mInfo = new objectInfo($module_info);

      foreach($mInfo->keys as $key=>$value){
        $keys .= '<strong>' . $value['title'] . '</strong><br />' . $value['description'] . '<br />';

        if (isset($value['set_function']))  eval('$keys .= ' . $value['set_function'] . "'" . $value['value'] . "', '" . $key . "');");
        else  $keys .= tep_draw_input_field('configuration[' . $key . ']', "configuration$key", $value['value']);

        if (isset($value['use_function']))  eval('$keys .= ' . $value['use_function'] . "('" . $value['value'] . "');");
        else  $keys .= $value['value'];

        $keys .= '<br /><br />';
      }
      $keys = substr($keys, 0, strrpos($keys, '<br /><br />'));

      $heading[] = array('text' => '['.$_GET['set'].']'.$mInfo->title );

      $contents = array('form' => tep_draw_form('modules', FILENAME_MODULES, 'manager='.$manager.'&amp;set='.$set_view.'&amp;class='.$class.'&amp;action=save'));
      $contents[] = array('class' => 'block_input','text' => $keys);
      $contents[] = array('class' => 'button_nav', 'text' => tep_image_submit('button_update.gif', IMAGE_UPDATE) . ' <a class="button" href="' . tep_href_link(FILENAME_MODULES) . '">' .  IMAGE_CANCEL . '</a>');
      break;
    }

    if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
      $box = new box;
      echo $box->infoBox($heading, $contents);

    }
  }

  private static function cstr_menu($direct,$modules_array=array(),$type='generic'){
    if ($dir = @dir($direct)) {
      while ($file = $dir->read()) {
	      if (!is_dir($direct . $file) && $file=='includes_header.php'){
		  $f=substr($file, 0, (strlen($file)-strlen(self::$file_extension)));
		  if (substr($file, strrpos($file, '.')) == self::$file_extension) {
		      $k=preg_replace('#(.*/)#i','',$direct);

		      if(file_exists($direct.'/admin_config.php')) {
			$modules_array[$type][$k]=array('path'=>$direct,'file'=>$file, 'edit'=>'admin_config.php');
			self::$list[]=array('key'=>$k,'type'=>$type,'path'=>$direct,'file'=>$file, 'edit'=>'admin_config.php', 'detail'=>self::SeraccInSrc($k));
		      }else{
			$modules_array[$type][$k]=array('path'=>$direct,'file'=>$file);
			self::$list[]=array('key'=>$k,'type'=>$type,'path'=>$direct,'file'=>$file, 'detail'=>self::SeraccInSrc($k));
		      }


		  }
	      }
	      elseif($file !='.' && $file !='..')  $modules_array=self::cstr_menu($direct.$file,$modules_array,$type);
	  }
	  $dir->close();
      }
      return $modules_array;
  }

  private static function SeraccInSrc($ident){
    $ident=strtoupper($ident);

    if(is_array(self::$src) || is_object(self::$src) )
      foreach(self::$src as $item){
	if( strpos( (string)$item->ident,$ident )===0) return $item;
      }
  }

/** Interface module */

  function check() { return false; }
  function install() { return false; }
  function remove() { return false;  }
  function keys() { return false; }
}

?>