<?php 
defined( '_JEXEC' ) or die( 'Restricted access' ); 

//error_reporting(E_ALL ^E_NOTICE);
function chmFileProperty($id)
{
	 
    $db = JFactory::getDBO();
    $sql = "SELECT f.* FROM #__jx_index AS f WHERE f.id = $id";
    $db->setQuery($sql);
    $row = $db->loadObject();
    $val = jxPermsToNum($row->perms);
    ?>
		<table class="adminform" summary="" border="0" cellpadding="0" cellspacing="0">
		<tr>
		<td>
		
		  <table border="0" cellpadding="0" cellspacing="0">
		  <tr valign="top" align="center">
		    <td rowspan="2"><?php echo JText::_('Chmode'); ?>: </td>
		    <td align="center"><center><b>r</b></center></td>
		    <td align="center"><center><b>w</b></center></td>
		    <td align="center"><center><b>x</b></center></td>
		    <td>-</td>
		    <td align="center"><center><b>r</b></center></td>
		    <td align="center"><center><b>w</b></center></td>
		    <td align="center"><center><b>x</b></center></td>
		    <td>-</td>
		    <td align="center"><center><b>r</b></center></td>
		    <td align="center"><center><b>w</b></center></td>
		    <td align="center"><center><b>x</b></center></td>
		  </tr>
		  <tr valign="top">
		    <td><input type="checkbox" name="chmod[1][]" value="4" <? echo $val['p'][0]==1 ? 'checked':'' ?> ></td>
		    <td><input type="checkbox" name="chmod[1][]" value="2" <? echo $val['p'][1]==1 ? 'checked':'' ?>></td>
		    <td><input type="checkbox" name="chmod[1][]" value="1" <? echo $val['p'][2]==1 ? 'checked':'' ?>></td>
		    <td>-</td>
		    <td><input type="checkbox" name="chmod[2][]" value="4" <? echo $val['o'][0]==1 ? 'checked':'' ?>></td>
		    <td><input type="checkbox" name="chmod[2][]" value="2" <? echo $val['o'][1]==1 ? 'checked':'' ?>></td>
		    <td><input type="checkbox" name="chmod[2][]" value="1" <? echo $val['o'][2]==1 ? 'checked':'' ?>></td>
		    <td>-</td>
		    <td><input type="checkbox" name="chmod[3][]" value="4" <? echo $val['g'][0]==1 ? 'checked':'' ?>></td>
		    <td><input type="checkbox" name="chmod[3][]" value="2" <? echo $val['g'][1]==1 ? 'checked':'' ?>></td>
		    <td><input type="checkbox" name="chmod[3][]" value="1" <? echo $val['g'][2]==1 ? 'checked':'' ?>></td>
		  </tr>
		  </table>
		
		
		</td>
		</tr>
		<TR>
		<TD align="right">
		<input type="button" class="button" value="<?php echo JText::_('Ok'); ?>" 
		      onclick="pBarOut(); xajax_jxSaveChm(xajax.getFormValues('jxPropForm'));" />
		<input type="button" class="button" value="<?php echo JText::_('Close'); ?>" 
		      onclick="pBarOut();">
		 
		</TD>
		</TR>
		</table>
       <input type="hidden" name="id[<?php echo $id;?>]" value="<?php echo $id;?>" />
    <?php
	
}
function jxFileProperty($id)
{
    
    //echo $id;
    $db = JFactory::getDBO();
    $sql = "SELECT f.*, ROUND((f.rate_total / f.rate_times) / 5 * 100) AS rating,  u.name AS uname FROM #__jx_index AS f LEFT JOIN #__users AS u ON u.id = f.user_id WHERE f.id = $id";
    $db->setQuery($sql);
    $rows = $db->loadObjectList();
    $row = $rows[0];
    //echo $row->dir;
    ?>
    <DIV class="prop_box">
      <div style="float:right;padding:10px;"><?php echo JHTML::image(JURI::root().'/administrator/components/com_jxplorer/images/icons/68/'.jxIconCrystal($row->icon, $row->type), NULL, array('id' => 'icon_'.$row->id)) ?></div>
      <TABLE cellpadding="2" cellpadding="0" border="0" width="100%">
        <TR>
          <TD><?php echo jcText::_('Name'); ?>:</TD>
          <TD><input type="text" readonly="readonly" value="<?php echo $row->name ?>" /></TD>
        </TR>
        <TR>
          <TD><?php echo jcText::_('Home Dir'); ?>:</TD>
          <TD><input type="text" readonly="readonly" value="<?php echo $row->dir ?>"/></TD>
        </TR>
        <TR>
          <TD><?php echo jcText::_('Type'); ?>:</TD>
          <TD><?php echo $row->type ?></TD>
        </TR>
        <TR>
          <TD><?php echo jcText::_('Owner'); ?>:</TD>
          <TD><?php echo $row->uname ? $row->uname : jcText::_('Super Admin') ?></TD>
        </TR>
        <TR>
          <TD><?php echo jcText::_('Rating'); ?>:</TD>
          <TD><span id="rating_<?php echo $id; ?>"><?php echo jxHtml::jxRating($row->rating)?></span> <input type="button" value="<?php echo jcText::_('Reset'); ?>" onclick="pBar(); xajax_jxResetRating(<?php echo $id ?>)" /></TD>
        </TR>
        <?php if($row->type == 'dir'){ ?>
        <TR>
          <TD><?php echo jcText::_('View'); ?>:</TD>
          <TD>
            <?php 
            $o[] = JHTML::_('select.option' , NULL, jcText::_('Default'));
            $o[] = JHTML::_('select.option' , 'Thumbnails', jcText::_('Thumbnails'));
            $o[] = JHTML::_('select.option' , 'Icons', jcText::_('Icons'));
            $o[] = JHTML::_('select.option' , 'Table', jcText::_('Table'));
            echo JHTML::_('select.genericlist' , $o, 'view['.$id.']', '', 'value', 'text', $row->view);
             ?>
          </TD>
        </TR>
        <?php }; ?>
        <TR valign="top">
          <TD><?php echo jcText::_('Description'); ?>:</TD>
          <TD><textarea name="description[<?php echo $id;?>]" cols="30" rows="4"><?php echo htmlspecialchars( $row->description )   ?></textarea></TD>
        </TR>
        <TR valign="top">
          <TD><?php echo jcText::_('Icon'); ?>:<br>
          
          </TD>
          <TD><?php if($row->type == 'dir') echo jxSpecialIcons($row->id);?></TD>
        </TR>
      </TABLE>
      <input type="hidden" name="hid_icon_<?php echo $id;?>" id="hid_icon_<?php echo $id;?>" value="" />
      <input type="hidden" name="id[<?php echo $id;?>]" value="<?php echo $id;?>" />
    </DIV>
    <?php
}
function jxListDirectorySearch($_special, $client)
{

}
function jxListDirectory($directory, $client, $task = false, $_special = '')
{
    global $database, $mainframe, $option;
    $SORTCOL = 'type';
    $SORTORDER = 'DESC';

    ($task == 'search' ? $search = $_special : FALSE);
    //echo $task;
    
    if(!$directory) $directory = jxInitialDirectory();
    
    if($search)
    {
        $search = explode(' ', $search);
        $search = $search[0];
        //$search = str_replace('*', '%', $search);
        $search = $database->getEscaped($search);
    }
    
    $filter_order		  = $mainframe->getUserStateFromRequest( "$option.$client.filter_order", 		'filter_order', 	$SORTCOL );
    $filter_order_Dir	= $mainframe->getUserStateFromRequest( "$option.$client.filter_order_Dir",	'filter_order_Dir',	$SORTORDER );
    $filter_state 		= $mainframe->getUserStateFromRequest( "$option.$client.filter_state", 		'filter_state', 	'' );
    //$filter_position 	= $mainframe->getUserStateFromRequest( "$option.$client.filter_position", 	'filter_position', 	0 );
    //$filter_type	 	  = $mainframe->getUserStateFromRequest( "$option.$client.filter_type", 		'filter_type', 		0 );
    $limit 				= $mainframe->getUserStateFromRequest( "limit", 'limit', $mainframe->getCfg('list_limit') );
    $limitstart 	= $mainframe->getUserStateFromRequest( "$option.limitstart", 'limitstart',0 );
    //$search 			= $mainframe->getUserStateFromRequest( "$option.$client.search", 'search','' );
    //$search 			= $database->getEscaped( trim( strtolower( $search ) ) );
    if(!$filter_order_Dir)$filter_order_Dir = 'DESC';
    //echo $filter_order_Dir;
    
    $query = "SELECT COUNT(*) FROM #__jx_index WHERE trash = 0 AND dir= '{$directory}'";
    if($search)
    {
        $query = "SELECT COUNT(*) FROM #__jx_index WHERE trash = '0' AND `name` LIKE '%{$search}%'";
    }
    if($task == 'recycle')
    {
        $query = "SELECT COUNT(*) FROM #__jx_index WHERE trash = 1";
    }
  
    $database->setQuery( $query );
    $total = $database->loadResult();
    //exit;
      
    require_once( JPATH_ADMINISTRATOR .DS.'includes'.DS.'pageNavigation.php' );
    $pageNav = new mosPageNav( $total, $limitstart, $limit );
  
    
    
    $orderby 	= " ORDER BY $filter_order $filter_order_Dir";    
    
    $query = "SELECT j.*, g.name as groupname FROM #__jx_index AS j LEFT JOIN #__groups AS g ON g.id = j.access WHERE j.trash = 0 AND dir = '{$directory}' ".$orderby;
    if($search)
    {
        $query = "SELECT j.*, g.name as groupname FROM #__jx_index AS j LEFT JOIN #__groups AS g ON g.id = j.access WHERE j.trash = 0 AND j.name LIKE '%{$search}%' ".$orderby;
    }
    if($task == 'recycle')
    {
        $query = "SELECT j.*, g.name as groupname FROM #__jx_index AS j LEFT JOIN #__groups AS g ON g.id = j.access WHERE j.trash = 1 ".$orderby;
    }    

    $database->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
    $rows = $database->loadObjectList();
    
    if ($filter_order_Dir == 'DESC' ) {
  		$lists['order_Dir'] = 'ASC';
  	} else {
  		$lists['order_Dir'] = 'DESC';
  	}
    
    $lists['order2'] = $filter_order_Dir;
    $lists['order'] = $filter_order;
  
    jxBuildFileListTable($rows, $lists, $pageNav, $search, $task);
}


function jxBuildFileListTable($rows, $lists, $pageNav, $search = false, $task)
{
    $CHARSET = JEConfig::get('general.charset');
    $SHOWNAME = 1;
    $SHOWTYPE = JEConfig::get('view.show_type');
    $SHOWSIZE = JEConfig::get('view.show_size');
    $SHOWPERM = JEConfig::get('view.show_perms');
    $SHOWACCESS = JEConfig::get('view.show_access');
    $SHOWPUB = JEConfig::get('view.show_pub');
    $SHOWCTIME = JEConfig::get('view.show_ctime');
    $SHOWMTIME = JEConfig::get('view.show_mtime');
    $SHOWACT =1;
    
    if($search)
    {
        $search = str_replace('%',' ',$search);
        $search = explode(' ', $search);
    }
    else
    $search = array(0);
    IF($task == 'recycle') echo '<br>';
    ?>
    <table class="adminlist">
      <TR>
      <thead>
        <TH width="20"><input type="checkbox" name="toggle" id="checkMain" onclick="checkAll(<?php echo count($rows) + 1?>);" /></TH>
        <?php if($SHOWNAME){?>
        <TH><?php echo JHTML::_( 'grid.sort',   'File Name', 'name', @$lists['order_Dir'], @$lists['order'] );  ?></TH>
        <?php 
        }
        if($task != 'recycle')
        {
         ?>
        <?php if($SHOWACCESS){?>
        <TH><?php echo JHTML::_( 'grid.sort',   'Access', 'access', @$lists['order_Dir'], @$lists['order']  );  ?></TH>
        <?php };?>
        <?php if($SHOWPUB){?>
        <TH><?php echo JHTML::_( 'grid.sort',   'Published', 'published', @$lists['order_Dir'], @$lists['order'] );  ?></TH>
        <?php };?>
        <?php if($SHOWACT){?>
        <TH colspan="3"><?php echo JText::_('Actions'); ?></TH>
        <?php };?>
        <?php 
        }
         ?>
        <?php if($SHOWSIZE){?>
        <TH nowrap="nowrap" width="2%"><?php echo JHTML::_('grid.sort',   'Size', 'size', @$lists['order_Dir'], @$lists['order'] );  ?></TH>
        <?php };?>
        <?php if($SHOWTYPE){?>
        <TH><?php echo JHTML::_( 'grid.sort',   'Type', 'type', @$lists['order_Dir'], @$lists['order']  );  ?></TH>
        <?php };?>
        <?php if($SHOWPERM){?>
        <TH><?php echo JHTML::_( 'grid.sort',   'Permission', 'perms', @$lists['order_Dir'], @$lists['order']  );  ?></TH>
        <?php };?>
        <?php if($SHOWCTIME){?>
        <TH><?php echo JHTML::_( 'grid.sort',   'Create Time', 'ctime', @$lists['order_Dir'], @$lists['order'] );  ?></TH>
        <?php };?>
        <?php if($SHOWMTIME){?>
        <TH><?php echo JHTML::_( 'grid.sort',   'Last Modify', 'mtime', @$lists['order_Dir'], @$lists['order'] );  ?></TH>
        <?php };?>
        
      </TR>
      </thead>
      
      <?php 
      $i=0; $k = 0;
      foreach($rows AS $file)
      {
        $i++;
        $file->type = str_replace('.','',$file->type);
        foreach($search AS $word)
        {
            $file->name = JString::str_ireplace($word, '<b>'.$word.'</b>', $file->name);
        }
        $name = "<a href=\"javascript:void(0)\" onclick=\"editItem('cb{$i}')\">$file->name</a>";
        $chm = "<a id=\"chm_{$file->id}\" href=\"javascript:void(0)\" onclick=\"editChm('cb{$i}')\">$file->perms</a>";
        $img = '<img src="components/com_jxplorer/images/icons/16/'.jxIconCrystal($file->icon, $file->type).'" alt="'.$alt.'" id="img_'.$file->id.'"/>';
        $link = '<a href="javascript:void(0);" onclick="javascript:document.adminForm.directory.value=\''.$file->dir.$file->name.'/\';submitbutton(\'chdir\'); return false;">'.$img.'</a>';
        
      ?>
      <tbody id="row_<?php echo $file->id?>">
      <tr class="<?php echo "row$k"; ?>">
        <td><?php echo JHTML::_('grid.checkedout',   $file, $i );?></td>
        <?php if($SHOWNAME){?>
        <td><?php echo(strtolower($file->type) == 'dir') ? $link : $img ?>
        <?php  echo ($task == 'recycle') ? $file->name : $name ?></td>
        <?php 
        }
        if($task != 'recycle')
        {
        ?>
        <?php if($SHOWACCESS){?>
        <td align="center"><?php echo jxHelper::AccessProcessingX( $file, 'jx_index' ); ?></td>
        <?php };?>
        <?php if($SHOWPUB){?>
        <td align="center"><?php echo jxHelper::PublishedProcessingX( $file, 'jx_index' ); ?></td>
        <?php };?>
        <?php if($SHOWACT){?>
        <td>
        <?php 
        if(jxIsArchive(jxUtf2Fs($file->dir.$file->name)))
        {
            ?>
            <a href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i ?>','extract')" title="<?php echo JText::_('Extract Archive'); ?>">
        		<img src="components/com_jxplorer/images/extract.gif" border="0" alt="<?php echo JText::_('Extract Archive'); ?>" /></a>
            <?php
        }
        ?>
        </td>
        <td>
        <?php 
        $finfo = pathinfo(jxUtf2Fs($file->dir.$file->name));
        $ext = $finfo['extension'];
        $ext = strtolower($ext);
        if($ext == 'txt' || $ext == 'html' ||$ext == 'php' ||$ext == 'css' ||
           $ext == 'html' || $ext == 'js' || $ext == 'asp' || $ext == 'aspx' ||
           $ext == 'xml' || $ext == 'dtd' || $ext == 'htaccess' || $ext == 'info')
        {
            ?>
            <a href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i ?>','editfile')" title="<?php echo JText::_('Edit File'); ?>">
        		<img src="components/com_jxplorer/images/edit.gif" border="0" alt="<?php echo JText::_('Edit File'); ?>" /></a>
            <?php
        }
        ?>
        </td>
        <td></td>
        <?php };?>
        <?php 
        }
         ?>
        <?php if($SHOWSIZE){?>
        <td><?php echo $file->size?></td>
        <?php };?>
        <?php if($SHOWTYPE){?>
        <td><?php echo $file->type?></td>
        <?php };?>
        <?php if($SHOWPERM){?>
        <td><?php echo $chm?></td>
        <?php };?>
        <?php if($SHOWCTIME){?>
        <td><?php echo date("d M, Y h:i:s", $file->ctime)?></td>
        <?php };?>
        <?php if($SHOWMTIME){?>
        <td><?php echo date("d M, Y h:i:s", $file->mtime)?></td>
        <?php };?>
        
      </tr> </tbody>
      <?php
          $k = 1 - $k;
      }
      ?>
     
      <tfoot>
      <td colspan="20">
      <?php echo $pageNav->getListFooter(); ?>
      </td>
      </tfoot>
    </table><br />

    <input type="hidden" name="filter_order" value="<?php echo $lists['order']; ?>" />
    <input type="hidden" name="filter_order_Dir" value="<?php echo $lists['order2']; ?>" />
    <?php 
}

Function jxUploadForm()
{
    global $destination;
?>
<BR>
<div class="centermain">
	<div class="main">
    <table class="adminlist">
      <THEAD>
      <tr>
        <th width="5%"><?php echo JText::_('Destination Directory'); ?>:</th>
        <TH width="10">#</TH>
        <TH width="20%" nowrap="nowrap"><?php echo JText::_('Create Directory in Destination Directory'); ?></TH>
        <TH nowrap="nowrap" width="1%"><input type="checkbox" name="toggle" value=""  onclick="checkAll(11);" /><?php echo JText::_('Overwrite'); ?></TH>
        <!--<TH nowrap="nowrap" width="1%"><input type="checkbox" name="toggle" value=""  onclick="checkAll(11);" /> <?php echo JText::_('Publish'); ?></TH>-->
        <TH>File</TH>
      </tr>
      </THEAD>
      <TBODY>
      <?php 
      $k = 0;
      for($i=1;$i<=10;$i++)
      {
      $file->id = $i;
      ?>
        <TR class="row<?php echo $k?>">
          <?php 
          if($i == 1) echo '<td rowspan="10">'.jxDestinationList($destination, 25).'</td>';
          ?>
          <TD width="10"><?php echo $i?></TD>
          <TD><input type="text" class="inputbox" name="dir<?php echo $i ?>" size="40"></TD>
          <TD><input type="checkbox" id="cb<?php echo $i ?>" name="cid[<?php echo $i ?>]" value="<?php echo $i ?>" onclick="isChecked(this.checked);" /></TD>
          <!--<TD><input type="checkbox" id="cb2<?php echo $i ?>" name="cid2[<?php echo $i ?>]" value="<?php echo $i ?>" onclick="isChecked(this.checked);" /></TD>-->
          <TD><input type="file" name="file<?php echo $i ?>"  class="inputbox" size="20"></TD>
        </TR>
      <?php
           $k = 1 - $k;
      }
      ?>
      </TBODY>
     </table>
  </div>
</div>
<?php
}


function jxEditForm($cid, $directory)
{
    jimport( 'joomla.filesystem.file' );
    $file = jxGetNameById($cid);
    $name = $directory.$file;
    ?>
	<BR>
	<div class="centermain">
		<div class="main">
	    <table class="adminform" width="10%">
	      <tr>
	        <th width="5%"><?php echo JText::_('File Contents'); ?>: <?php echo $directory.$file; ?></th>
	      </tr>
	        <TR class="row<?php echo $k?>">
	          <TD width="5%">
	          <textarea rows="30" cols="100" name="filecontents" class="text_area"><?php echo JFile::read(realpath(jxUtf2Fs($name))) ?></textarea>
	          </TD>
	        </TR>
	     </table>
	  </div>
	</div>
	<input type="hidden" name="cid[]" value="<?php echo $cid ?>">
	<input type="hidden" name="filename" value="<?php echo $file ?>">
	<input type="hidden" name="filedir" value="<?php echo $directory ?>">
	<?php
}

function jxDestinationForm($cid)
{
 
?>
<TABLE class="adminform">
  <TR valign="top">
    <TD width="200px"><?php echo JText::_('Choose the Destination'); ?></TD>
    <TD><?php echo jxDestinationList($destination, 30);?></TD>
  </TR>
</TABLE>
<input type="hidden" name="cid2" value="<?php echo base64_encode(serialize($cid))?>">
<?php
}
function jxPermsToStr($perm)
{
    $str = array(
        0 => '---',
        1 => '--x',
        2 => '-w-',
        3 => '-wx',
        4 => 'r--',
        5 => 'r-x',
        6 => 'rw-',
        7 => 'rwx'
    );
    
    $g = substr($perm, -3, 1);
    $o = substr($perm, -2, 1);
    $p = substr($perm, -1);
    
    return $str[$g].$str[$o].$str[$p];
}
function jxPermsToNum($perm)
{
	$str = array(
        '---' => array(0,0,0),
        'r--' => array(1,0,0),
        '-w-' => array(0,1,0),
        'rw-' => array(1,1,0),
        '--x' => array(0,0,1),
        'r-x' => array(1,0,1),
        '-wx' => array(0,1,1),
        'rwx' => array(1,1,1) 
    );
    
    $g = substr($perm, -3, 3);
    $o = substr($perm, -6, 3);
    $p = substr($perm, -9 ,3);
    
   return array('p'=>$str[$p],'o' => $str[$o], 'g'=>$str[$g]);
}
?>