
Dis.About=Ext.extend(Dis,{aboutWin:null,show:function(){if(!this.aboutWin){this.aboutWin=new Ext.Window({layout:'fit',id:'about-win',width:350,height:200,title:'About',html:'<div class="about"><h1>DISO FTP Manager 1.0</h1>&copy; 2010 by Digital Identification Solutions AG<br /><br />FTP: edisecure.de<br />TLS/SSL enabled<br /><br />Contact: <a href="mailto:webmaster@digital-identification.com">webmaster@digital-identification.com</a></div>'});this.aboutWin.show();}}});Dis.Password=Ext.extend(Dis,{getRandomNum:function(lbound,ubound){return(Math.floor(Math.random()*(ubound-lbound))+lbound);},getRandomChar:function(number,lower,upper,other,extra){var numberChars="0123456789";var lowerChars="abcdefghijklmnopqrstuvwxyz";var upperChars="ABCDEFGHIJKLMNOPQRSTUVWXYZ";var otherChars="`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";var charSet=extra;if(number==true){charSet+=numberChars;}
if(lower==true){charSet+=lowerChars;}
if(upper==true){charSet+=upperChars;}
if(other==true){charSet+=otherChars;}
return charSet.charAt(this.getRandomNum(0,charSet.length));},getPassword:function(length){var rc="";if(!this.config.extraChars){this.config.extraChars="";}
this.config.firstNumber=(this.config.firstNumber)?true:true;this.config.firstLower=(this.config.firstLower)?true:true;this.config.firstUpper=(this.config.firstUpper)?true:true;this.config.firstOther=(this.config.firstOther)?true:false;this.config.letterNumber=(this.config.letterNumber)?true:true;this.config.letterLower=(this.config.letterLower)?true:true;this.config.letterUpper=(this.config.letterUpper)?true:true;this.config.letterOther=(this.config.letterOther)?true:false;if(length>0){rc=rc+this.getRandomChar(this.config.firstNumber,this.config.firstLower,this.config.firstUpper,this.config.firstOther,this.config.extraChars);for(var idx=1;idx<length;++idx){rc=rc+this.getRandomChar(this.config.letterNumber,this.config.letterLower,this.config.letterUpper,this.config.letterOther,this.config.extraChars);}}
return rc;}});Dis.EditPassword=Ext.extend(Dis,{saveContent:function(){var extForm=this.tabEdit.getForm();if(!extForm.isValid()){return null;}
var pass1=this.tabEdit.findById('password').getValue();var pass2=this.tabEdit.findById('password2').getValue();if(pass1!=pass2){Dis.errorMsg("The passwords don't match!");return null;}
var saveData=this.tabEdit.getForm().getValues();var thisWin=this.win;Ext.MessageBox.show({msg:'Saving your data, please wait...',progressText:'Saving...',width:300,wait:true,icon:'ext-mb-download'});Ext.Ajax.request({url:this.baseURL,success:function(response){var obj=Ext.decode(response.responseText);if(obj.errors){Ext.MessageBox.hide();Dis.errorMsg(obj.errors.errorMsg);}else{Ext.MessageBox.hide();}},failure:function(response){Ext.Msg.alert('Warning!','A HTTP connection error has occured!');},params:{method:'savePassword',data:Ext.encode(saveData)}});},createWindow:function(){this.tabEdit=new Ext.FormPanel({labelAlign:'left',frame:true,bodyStyle:'padding:5px',width:600,items:[{xtype:'fieldset',title:'Password',collapsible:false,autoHeight:true,defaultType:'textfield',items:[{fieldLabel:'New',id:'password',value:'',width:300,inputType:'password',allowBlank:false},{fieldLabel:'Retype',id:'password2',value:'',width:300,inputType:'password',allowBlank:false}]}]});var resetButton=new Ext.Button({text:'Reset',iconCls:'resetButton',tooltip:'Reset'});resetButton.on('click',this.resetForm,this);this.win=new Ext.Window({id:this.winID,layout:'fit',autoScroll:true,width:500,height:240,modal:true,tbar:this.getToolbar(resetButton),items:[this.tabEdit],title:'Change Password'});this.win.show();this.win.setHeight(240);this.resetForm();}});Dis.EditUser=Ext.extend(Dis,{callMethod:'getUser',saveMethod:'saveUser',readerOptions:[{name:'username'},{name:'expire'},{name:'comment'},{name:'homedir'},{name:'read_only'}],readerProp:{totalProperty:'results',root:'rows',id:'id'},getValue:function(val,defval){if(!this.disStore){return defval;}
var record=this.disStore.getAt(0);if(record instanceof Ext.data.Record){if(record.data[val]!==null){return record.data[val];}}
return defval;},toBoolean:function(val){if(val=='1'||val=='true'||val==1||val==true){return true;}
return false;},resetForm:function(){var extForm=this.tabEdit.getForm();if(extForm){extForm.reset();}},generatePass:function(){if(this.tabEdit){var pass=new Dis.Password({extraChars:""});var randomPass=pass.getPassword(8);this.tabEdit.findById('passwd').setValue(randomPass);this.tabEdit.findById('passwd2').setValue(randomPass);}},saveContent:function(){var extForm=this.tabEdit.getForm();if(!extForm.isValid()){return null;}
var saveData=this.tabEdit.getForm().getValues();if(this.config.id){saveData.id=this.config.id;}
if(saveData.passwd!=saveData.passwd2){Ext.Msg.alert('Warning!','The passwords do not match!');return null;}
if(saveData.passwd&&saveData.passwd=="***"){delete saveData.passwd;delete saveData.passwd2;}
var configId=this.config.id;var thisWin=this.win;Ext.MessageBox.show({msg:'Saving your data, please wait...',progressText:'Saving...',width:300,wait:true,icon:'ext-mb-download'});Ext.Ajax.request({url:this.baseURL,success:function(response){var obj=Ext.decode(response.responseText);if(obj.errors){Ext.MessageBox.hide();Dis.errorMsg(obj.errors.errorMsg);}else{if(configId==0){thisWin.close();Ext.MessageBox.hide();}else{Ext.MessageBox.hide();}}},failure:function(response){Ext.Msg.alert('Warning!','A HTTP connection error has occured!');},params:{method:this.saveMethod,data:Ext.encode(saveData)}});this.hasChanged.save=true;},createWindow:function(){this.deleteParams={method:'deleteUser',id:this.config.id};var inputPassType=(this.config.id!="0")?"password":"text";this.tabEdit=new Ext.FormPanel({labelAlign:'left',frame:true,bodyStyle:'padding:5px',fileUpload:false,width:640,items:[{xtype:'fieldset',title:'FTP User',collapsible:false,autoHeight:true,items:[{xtype:'textfield',fieldLabel:'Username *',id:'username',anchor:'99%',allowBlank:false,value:this.getValue('username','')},{layout:'column',items:[{columnWidth:.50,layout:'form',items:[{xtype:'textfield',fieldLabel:'Password *',id:'passwd',anchor:'98%',allowBlank:false,value:'',inputType:inputPassType}]},{columnWidth:.50,layout:'form',items:[{xtype:'textfield',fieldLabel:'Retype Password',id:'passwd2',anchor:'98%',allowBlank:false,value:'',inputType:inputPassType}]}]}]},{xtype:'fieldset',title:'Optional configurations',collapsible:false,autoHeight:true,items:[{xtype:'textfield',fieldLabel:'Homedir',id:'homedir',anchor:'99%',allowBlank:true,value:this.getValue('homedir','')},{xtype:'textarea',fieldLabel:'Comment',id:'comment',anchor:'99%',height:80,allowBlank:true,value:this.getValue('comment','')},new Ext.form.DateField({fieldLabel:'Expiration Date',width:200,allowBlank:true,format:'Y-m-d',value:this.getValue('expire',''),id:'expire'}),new Ext.form.Checkbox({fieldLabel:'Read only',checked:this.toBoolean(this.getValue('read_only',false)),id:'read_only'})]}]});if(this.config.id!="0"){this.addionalButton=new Ext.Button({text:'Reset',iconCls:'resetButton',tooltip:'Reset',listeners:{click:{scope:this,fn:this.resetForm}}});}else{this.addionalButton=new Ext.Button({text:'Generate Password',iconCls:'generatePassButton',tooltip:'Generate a new password',listeners:{click:{scope:this,fn:this.generatePass}}});}
this.win=new Ext.Window({id:'dis-edit-win',layout:'fit',autoScroll:false,width:680,height:380,modal:true,tbar:this.getToolbar(this.addionalButton),items:[this.tabEdit],title:'Record ID: '+this.config.id});this.win.show();this.win.setHeight(380);if(this.config.id!="0"){this.tabEdit.findById('passwd').setValue("***");this.tabEdit.findById('passwd2').setValue("***");}else{this.resetForm();}
this.win.on('beforedestroy',this.destroy,this);},postReload:function(){this.hasChanged.save=true;}});Dis.TreeView=Ext.extend(Dis,{libTree:null,grid:null,libStore:null,treePanel:null,pagesize:50,reloadItem:'',selectedRecord:null,defaultPanelMsg:'Please select a record to see additional details.',getGridItems:function(){if(this.libStore){this.libStore.baseParams.search='';this.libStore.load({params:{start:0,limit:this.pagesize},callback:this.isError});}},searchItems:function(){if(this.libStore){this.libStore.baseParams.searchTerm=this.searchField.getValue();this.libStore.baseParams.search='on';this.grid.setTitle('Search results for "'+this.searchField.getValue()+'"');this.libStore.load({params:{start:0,limit:this.pagesize},callback:this.isError});}},onGridContextMenu:function(grid,rowIndex,cellIndex,e){e.stopEvent();var clickCoords=e.getXY();this.selectedRecord=this.libStore.getAt(rowIndex);this.showGridContextMenu(clickCoords);return false;},onCelldblClick:function(obj,rowIndex,columnIndex,e){this.selectedRecord=this.libStore.getAt(rowIndex);this.editUser();},onCellClick:function(obj,rowIndex,columnIndex,e){this.selectedRecord=this.libStore.getAt(rowIndex);if(columnIndex==6){this.editUser();}},showGridContextMenu:function(clickCoords){if(!(this.gridContextMenuType)){this.mAdd=new Ext.menu.Item({text:'Add new User',iconCls:'userAdd',listeners:{click:{scope:this,fn:this.addUser}}});this.mEdit=new Ext.menu.Item({text:'Edit User',iconCls:'userEdit',listeners:{click:{scope:this,fn:this.editUser}}});this.mDel=new Ext.menu.Item({text:'Delete User',iconCls:'userDel',listeners:{click:{scope:this,fn:this.deleteUser}}});this.gridContextMenuType=new Ext.menu.Menu({items:[this.mAdd,this.mEdit,this.mDel]});}
this.gridContextMenuType.showAt([clickCoords[0],clickCoords[1]]);},reloadGrid:function(){this.grid.setTitle("FTP User Overview");this.searchField.setValue("");this.libStore.baseParams.search='';this.libStore.baseParams.searchTerm='';this.getGridItems();},deleteUser:function(){if(this.gridContextMenuType!=null){this.gridContextMenuType.hide();}
this.deleteParams={method:'deleteUser',id:this.selectedRecord.data.id};this.reloadItem='grid';this.deleteData();},addUser:function(){if(this.gridContextMenuType!=null){this.gridContextMenuType.hide();}
var editWin=new Dis.EditUser({id:'0'});editWin.on('closeAndChanged',this.reloadGrid,this);editWin.show();},editUser:function(){if(this.gridContextMenuType!=null){this.gridContextMenuType.hide();}
var editWin=new Dis.EditUser({id:this.selectedRecord.data.id});editWin.on('closeAndChanged',this.reloadGrid,this);editWin.show();},getGrid:function(){this.libStore=new Ext.data.Store({baseParams:{method:'getUsers'},proxy:new Ext.data.HttpProxy({url:this.baseURL}),reader:new Ext.data.JsonReader({totalProperty:'results',root:'rows',id:'id'},[{name:'id'},{name:'username'},{name:'last_login'},{name:'count'},{name:'days'},{name:'expire'},{name:'creation_date'},{name:'comment'},{name:'shell'},{name:'homedir'},{name:'uid'},{name:'gid'},{name:'login'},{name:'read_only'}])});this.pagingBar=new Ext.PagingToolbar({pageSize:this.pagesize,store:this.libStore,displayInfo:true,displayMsg:'Displaying record {0} - {1} of {2}',emptyMsg:"No entries to display"});this.grid=new Ext.grid.GridPanel({store:this.libStore,columns:[{header:"Username",width:210,dataIndex:'username',sortable:true},{header:"Creation Date",width:115,dataIndex:'creation_date',sortable:true},{header:"Expiration Date",width:115,dataIndex:'expire',sortable:true},{header:"Last Login",width:115,dataIndex:'last_login',sortable:true},{header:"Days",width:45,dataIndex:'days',sortable:true},{header:"Logins",width:45,dataIndex:'count',sortable:true},{header:"Edit",width:30,dataIndex:'edit',sortable:false,renderer:this.renderEditColum}],loadMask:true,height:560,bbar:this.pagingBar,sm:new Ext.grid.RowSelectionModel({singleSelect:true}),title:'FTP User Overview',listeners:{cellcontextmenu:{scope:this,fn:this.onGridContextMenu,stopEvent:true},cellclick:{scope:this,fn:this.onCellClick},celldblclick:{scope:this,fn:this.onCelldblClick}}});this.grid.getSelectionModel().on('rowselect',function(sm,rowIdx,r){var detailPanel=Ext.getCmp('detailPanel');var detailTpl=new Ext.Template(['Username: {username}<br/>','Homedir: {homedir}<br/>','UID: {uid}<br/>','GID: {gid}<br/>','Shell: {shell}<br/>','Last Login: {last_login}<br/>','Expiration Date: {expire}<br/>','Creation Date: {creation_date}<br/>','Logins: {count}<br/>','Comment: {comment}<br/>','Created by: {login}']);detailTpl.overwrite(detailPanel.body,r.data);});return this.grid;},renderEditColum:function(value,metaData,record,rowIndex,colIndex,store){return'<img src="./img/user_edit.png" border="0">';},logout:function(){Ext.MessageBox.show({msg:'Closing session, please wait ...',progressText:'...',width:300,wait:true,waitConfig:{interval:150}});Ext.Ajax.request({url:this.baseURL,params:{method:'logout'},callback:function(){window.location.href=window.location.pathname;}});},showAbout:function(){var aboutWin=new Dis.About({id:0});aboutWin.show();},editPassword:function(){var editPassWin=new Dis.EditPassword({id:0});editPassWin.show();},getMenuBar:function(){this.searchField=new Ext.form.TextField({id:'quick-search',width:150,enableKeyEvents:true,listeners:{specialkey:{scope:this,fn:function(thisTextField,e){keyCode=e.getKey();if(keyCode==13){if(this.searchField.getValue().trim().length>1){this.searchItems();}}}}}});this.tb=new Ext.Toolbar({id:'main-toolbar',buttons:['->',{iconCls:'userAdd',text:'Add new User',listeners:{click:{scope:this,fn:this.addUser}}},'-',{iconCls:'reloadButton',text:'Reload',listeners:{click:{scope:this,fn:this.reloadGrid}}},'-',{iconCls:'changePwdButton',text:'Change Password',listeners:{click:{scope:this,fn:this.editPassword}}},'-',{iconCls:'helpIcon',text:'About',listeners:{click:{scope:this,fn:this.showAbout}}},'-',{iconCls:'logoutButton',text:'Logout',listeners:{click:{scope:this,fn:this.logout}}},'-',this.searchField,{iconCls:'disSearchItem',text:'Search',listeners:{click:{scope:this,fn:this.searchItems}}}]});return this.tb;},postReload:function(){this.reloadGrid();},show:function(){this.getGrid();this.treePanel=new Ext.FormPanel({height:588,width:960,tbar:this.getMenuBar(),layout:'column',items:[{columnWidth:.25,layout:'fit',border:false,items:[{id:'detailPanel',region:'center',bodyStyle:{background:'#ffffff',padding:'7px',height:'560px'},html:this.defaultPanelMsg}]},{columnWidth:.75,layout:'fit',border:false,items:[this.grid]}],renderTo:'libTreePanel'});this.getGridItems();}});Ext.onReady(function(){Ext.BLANK_IMAGE_URL='./extjs/resources/images/default/s.gif';Ext.QuickTips.init();var login=null;var winLogin=null;Ext.Ajax.request({url:'./ajax.php',success:function(response){var obj=Ext.decode(response.responseText);if(obj.errors){loginWindow();}else{showTreePanel(obj.profile);}},failure:function(response){Ext.Msg.alert('Warning!','A HTTP connection error has occured!');},params:{method:'getProfile'}});function submitLogin(){if(login.getForm().isValid()){var formData=login.getForm().getValues();formData.method='login';Ext.Ajax.request({url:'./ajax.php',success:function(response){var obj=Ext.decode(response.responseText);if(obj.errors){Ext.Msg.alert('Login failed!',obj.errors.errorMsg);}else{winLogin.destroy();showTreePanel(obj.profile);}},failure:function(response){Ext.Msg.alert('Warning!','A HTTP connection error has occured!');},params:formData});}}
function loginWindow(){var loginButton=new Ext.Button({text:'Login',formBind:true,handler:function(){submitLogin();}});login=new Ext.FormPanel({labelWidth:80,url:'./ajax.php',frame:true,title:'Please Login',defaultType:'textfield',monitorValid:true,items:[{fieldLabel:'Username',name:'user',allowBlank:false,width:200,enableKeyEvents:true,listeners:{specialkey:function(thisField,e){if(e.getKey()==13){submitLogin();}}}},{fieldLabel:'Password',name:'pass',inputType:'password',allowBlank:false,width:200,enableKeyEvents:true,listeners:{specialkey:function(thisField,e){if(e.getKey()==13){submitLogin();}}}}],buttons:[loginButton]});winLogin=new Ext.Window({title:'DISO FTP Manager',layout:'fit',width:340,height:150,closable:true,resizable:false,plain:true,border:false,items:[login],listeners:{close:{scope:this,fn:function(){window.location.href="http://www.digital-identification.com";}}}});winLogin.show(Ext.getBody());}
function showTreePanel(config){var treeViewPanel=new Dis.TreeView(config);treeViewPanel.show();}});
