1<?php
2/**************************************************************************\
3* phpGroupWare - Ftp Module                                                *
4* http://www.phpgroupware.org                                              *
5* Written by Scott Moser <smoser@brickies.net>                             *
6* --------------------------------------------                             *
7*  This program is free software; you can redistribute it and/or modify it *
8*  under the terms of the GNU General Public License as published by the   *
9*  Free Software Foundation; either version 2 of the License, or (at your  *
10*  option) any later version.                                              *
11\**************************************************************************/
12
13	/* $Id: index.php 15241 2004-07-15 01:56:03Z skwashd $ */
14
15	$GLOBALS['phpgw_info']['flags'] = array(
16		'currentapp'              => 'ftp',
17		'enable_nextmatchs_class' => True
18	);
19
20	if ($_GET['action'] == 'get' || $_GET['action'] == 'view')
21	{
22		$GLOBALS['phpgw_info']['flags']['nonavbar'] = True;
23		$GLOBALS['phpgw_info']['flags']['noheader'] = True;
24	}
25	include('../header.inc.php');
26
27	$action = get_var('action', array('POST', 'GET'));
28	$newdir = get_var('newdir', array('POST', 'GET'));
29	$olddir = @urldecode(get_var('olddir', array('POST', 'GET')));
30	$start = intval(get_var('start', array('GET')));
31
32	$default_login  = $GLOBALS['phpgw_info']['user']['account_lid'];
33	$default_pass   = $GLOBALS['phpgw']->session->appsession('password','phpgwapi');
34	$default_server = $GLOBALS['phpgw_info']['server']['default_ftp_server'];
35
36	$sessionUpdated=false;
37
38	$em_bg      = $GLOBALS['phpgw_info']['theme']['table_bg'];
39	$em_bg_text = $GLOBALS['phpgw_info']['theme']['table_text'];
40	$bgcolor[0] = $GLOBALS['phpgw_info']['theme']['row_on'];
41	$bgcolor[1] = $GLOBALS['phpgw_info']['theme']['row_off'];
42	$bgclass = array('row_on', 'row_off');
43	$tempdir    = $GLOBALS['phpgw_info']['server']['temp_dir'];
44
45	$GLOBALS['target']='/'.$GLOBALS['phpgw_info']['flags']['currentapp'].'/index.php';
46
47	$GLOBALS['phpgw']->template->set_file(array(
48		'main_' => 'main.tpl',
49		'login' => 'login.tpl',
50		'rename' => 'rename.tpl',
51		'confirm_delete' => 'confirm_delete.tpl',
52		'bad_connect' => 'bad_connection.tpl'
53	));
54	$GLOBALS['phpgw']->template->set_var(array(
55		'em_bgcolor' => $em_bg,
56		'em_text_color' => $em_bg_text,
57		'bgclass' => $bgclass[0]
58	));
59
60	$GLOBALS['phpgw']->template->set_block('main_','main');
61	$GLOBALS['phpgw']->template->set_block('main_','row');
62	$GLOBALS['phpgw']->template->set_var('th_bg',$GLOBALS['phpgw_info']['theme']['th_bg']);
63	$GLOBALS['phpgw']->template->set_var('row_on',$GLOBALS['phpgw_info']['theme']['row_on']);
64	$GLOBALS['phpgw']->template->set_var('row_off',$GLOBALS['phpgw_info']['theme']['row_off']);
65
66	$GLOBALS['phpgw']->template->set_var('module_name',lang('Ftp Client'));
67
68	if (!$action || $action=='login')
69	{
70		// if theres no action, try to login to default host with user and pass
71		if ($action=='login')
72		{
73			// username, ftpserver and password should have been passed in
74			// via POST
75			$connInfo['username']  = $_POST['username'];
76			$connInfo['password']  = $_POST['password'];
77			$connInfo['ftpserver'] = $_POST['ftpserver'];
78		}
79		else
80		{
81			// try to default with session id and passwd
82			if (!($connInfo=getConnectionInfo()))
83			{
84				$connInfo['username']  = $default_login;
85				$connInfo['password']  = $default_pass;
86				$connInfo['ftpserver'] = $default_server;
87
88				$tried_default=true;
89			}
90		}
91		updateSession($connInfo);
92		$sessionUpdated=true;
93	}
94
95	if ($action != 'newlogin')
96	{
97		if (empty($connInfo))
98		{
99			$connInfo=getConnectionInfo();
100		}
101		$ftp=@phpftp_connect($connInfo['ftpserver'],$connInfo['username'],$connInfo['password']);
102		if ($ftp)
103		{
104			$homedir=ftp_pwd($ftp);
105			$retval=ftp_pasv($ftp,1);
106			if ($action == 'delete' || $action == 'rmdir')
107			{
108				if ($_POST['confirm'])
109				{
110					if ($action=='delete')
111					{
112						$retval=@ftp_delete($ftp,$olddir . '/' . $file);
113					}
114					else
115					{
116						$retval=@ftp_rmdir($ftp,$olddir . '/' . $file);
117					}
118					if ($retval)
119					{
120						$GLOBALS['phpgw']->template->set_var("misc_data",lang('Successfully deleted %1',"$olddir/$file"), true);
121					}
122					else
123					{
124						$GLOBALS['phpgw']->template->set_var('misc_data',lang('failed to delete %1', "$olddir/$file"), true);
125					}
126				} else if (!$_POST['cancel'])
127				{
128					$GLOBALS['phpgw']->template->set_var('misc_data',confirmDeleteForm($session,$file,$olddir),true);
129				}
130			}
131
132			if ($action == 'rename')
133			{
134				if ($confirm)
135				{
136					if (ftp_rename($ftp,$olddir . '/' . $filename, $olddir . '/' . $newfilename))
137					{
138						$GLOBALS['phpgw']->template->set_var('misc_data',lang('renamed %1 to %2',
139							"$filename", "$newfilename"), true);
140					}
141					else
142					{
143						$GLOBALS['phpgw']->template->set_var('misc_data',lang('failed to rename %1 to %2',
144							"$filename", "$newfilename"), true);
145					}
146				}
147				else
148				{
149					$GLOBALS['phpgw']->template->set_var('misc_data', renameForm($session,$file,$olddir), true);
150				}
151			}
152			if ($action == 'get')
153			{
154				phpftp_get($ftp,$tempdir,$olddir,$file);
155				$GLOBALS['phpgw']->common->phpgw_exit();
156			}
157			if ($action == 'view')
158			{
159				phpftp_view($ftp,$tempdir,$olddir,$file);
160				$GLOBALS['phpgw']->common->phpgw_exit();
161			}
162			if ($action == 'upload')
163			{
164				$newfile=$olddir . '/' . $uploadfile_name;
165				if (ftp_put($ftp,$newfile, $uploadfile, FTP_BINARY))
166				{
167					$GLOBALS['phpgw']->template->set_var('misc_data',lang('Successfully uploaded %1',$newfile), true);
168				}
169				else
170				{
171					$GLOBALS['phpgw']->template->set_var('misc_data',lang('failed to upload %1',$newfile), true);
172				}
173				unlink($uploadfile);
174			}
175			if ($action == 'mkdir')
176			{
177				if ($newdirname!='')
178				{
179					if (ftp_mkdir($ftp,$olddir . '/' . $newdirname))
180					{
181						$GLOBALS['phpgw']->template->set_var('misc_data',lang('Successfully created directory %1',
182							"$olddir/$newdirname"), true);
183					}
184					else
185					{
186						$GLOBALS['phpgw']->template->set_var('misc_data',lang('failed to create directory %1',
187							"$olddir/$newdirname"), true);
188					}
189				}
190				else
191				{
192					$GLOBALS['phpgw']->template->set_var('misc_data',lang('Attempt to create a directory with empty name'),true);
193				}
194			}
195
196			// heres where most of the work takes place
197			if ($action == 'cwd')
198			{
199				if ($olddir == $newdir)
200				{
201					ftp_chdir($ftp,$newdir);
202				}
203				else
204				{
205					ftp_chdir($ftp,$olddir . $newdir . '/');
206					if($oldir == '/')
207					{
208						$olddir = $newdir;
209					}
210					elseif(! ($file && $newdir) )
211					{
212						$olddir = $newdir = '';
213					}
214					{
215						$olddir = $olddir . $newdir . '/';
216					}
217				}
218			}
219			elseif ($action == 'up')
220			{
221				ftp_chdir($ftp,$connInfo['cwd']);
222				ftp_cdup($ftp);
223			}
224			elseif ($action == '' && $connInfo['cwd'] != '')
225			{
226				// this must have come back from another module, try to
227				// get into the old directory
228				ftp_chdir($ftp,$connInfo['cwd']);
229			}
230			elseif ($olddir)
231			{
232				ftp_chdir($ftp,$olddir);
233			}
234
235			if (! $olddir)
236			{
237				$olddir = ftp_pwd($ftp);
238			}
239			$cwd = ftp_pwd($ftp);
240			$connInfo['cwd'] = $cwd;
241
242			// set up the upload form
243			$ul_form_open='<form name="upload" action="'.createLink($GLOBALS['target'])
244				. '" enctype="multipart/form-data" method="post">'."\n"
245				. '<input type="hidden" name="olddir" value="'.$cwd.'">'."\n"
246				. '<input type="hidden" name="action" value="upload">'."\n";
247			$ul_select='<input type="file" name="uploadfile" size="30">'."\n" ;
248			$ul_submit='<input type="submit" name="upload" value="Upload">'."\n";
249			$ul_form_close='</form>'."\n";
250
251			// set up the create directory
252			$crdir_form_open='<form name="mkdir" action="'.createLink($GLOBALS['target']).'" method="post" >'."\n"
253				. "\t".'<input type="hidden" name="olddir" value="'.$cwd.'">'."\n"
254				. "\t".'<input type="hidden" name="action" value="mkdir">'."\n";
255
256			$crdir_form_close='</form>'."\n";
257			$crdir_textfield="\t".'<input type="text" size="30" name="newdirname" value="">'."\n";
258			$crdir_submit="\t".'<input type="submit" name="submit" value="Create New Dir">'."\n";
259			$ftp_location='ftp://' . $connInfo['username'] . '@' . $connInfo['ftpserver'] . $cwd;
260
261			$newdir=''; $temp=$olddir; $olddir=$homedir;
262			$home_link= macro_get_Link('cwd','<img border="0" src="'.$GLOBALS['phpgw']->common->image('ftp','home.gif').'">') . "\n";
263			$olddir=$temp;
264
265			// set up all the global variables for the template
266			$GLOBALS['phpgw']->template->set_var(array(
267				'ftp_location' => $ftp_location,
268				'relogin_link'=> macro_get_Link('newlogin',lang('logout/relogin')),
269				'home_link' => $home_link,
270				'ul_select' => $ul_select,
271				'ul_submit' => $ul_submit,
272				'ul_form_open' => $ul_form_open,
273				'ul_form_close' => $ul_form_close,
274				'crdir_form_open' => $crdir_form_open,
275				'crdir_form_close' => $crdir_form_close,
276				'crdir_textfield' => $crdir_textfield,
277				'crdir_submit' => $crdir_submit
278			));
279
280			$total = count(ftp_rawlist($ftp,''));
281			$GLOBALS['phpgw']->template->set_var('nextmatchs_left',$GLOBALS['phpgw']->nextmatchs->left('/ftp/index.php',$start,$total));
282			$GLOBALS['phpgw']->template->set_var('nextmatchs_right',$GLOBALS['phpgw']->nextmatchs->right('/ftp/index.php',$start,$total));
283
284			$contents = phpftp_getList($ftp,'.',$start);
285
286			$GLOBALS['phpgw']->template->set_var('lang_name',lang('Name'));
287			$GLOBALS['phpgw']->template->set_var('lang_owner',lang('Owner'));
288			$GLOBALS['phpgw']->template->set_var('lang_group',lang('Group'));
289			$GLOBALS['phpgw']->template->set_var('lang_permissions',lang('Permissions'));
290			$GLOBALS['phpgw']->template->set_var('lang_size',lang('Size'));
291			$GLOBALS['phpgw']->template->set_var('lang_delete',lang('Delete'));
292			$GLOBALS['phpgw']->template->set_var('lang_rename',lang('Rename'));
293
294
295			$newdir = $olddir;
296			$GLOBALS['phpgw']->template->set_var('name',macro_get_link('up','..'));
297			$GLOBALS['phpgw']->template->set_var('del_link','&nbsp;');
298			$GLOBALS['phpgw']->template->set_var('rename_link','&nbsp;');
299			$GLOBALS['phpgw']->template->set_var('owner','');
300			$GLOBALS['phpgw']->template->set_var('group','');
301			$GLOBALS['phpgw']->template->set_var('permissions','');
302			$GLOBALS['phpgw']->template->fp('rowlist_dir','row',True);
303
304			if(is_array($contents))
305			{
306				$i = 1; //this is done as the first line is already set higher up
307				foreach($contents as $null => $fileinfo)
308				{
309					//echo '<pre>'; print_r($fileinfo); echo '</pre>';
310					$newdir = $fileinfo['name'];
311					$GLOBALS['phpgw']->template->set_var('owner',$fileinfo['owner']);
312					$GLOBALS['phpgw']->template->set_var('group',$fileinfo['group']);
313					$GLOBALS['phpgw']->template->set_var('permissions',$fileinfo['permissions']);
314
315					if ($fileinfo['size'] < 1024)
316					{
317						$fileinfo['size'] = $fileinfo['size'] . ' b';
318					}
319					elseif ($fileinfo['size'] < 999999)
320					{
321						$fileinfo['size'] = round(10*($fileinfo['size']/1024))/10 .' k';
322					}
323					else
324					{
325						//  round to W.XYZ megs by rounding WX.YZ
326						$fileinfo['size'] = round($fileinfo['size']/(1024*100));
327						// then bring it back one digit and add the MB string
328						$fileinfo['size'] = ($fileinfo['size']/10) .' MB';
329					}
330					if (substr($fileinfo['permissions'],0,1) == 'd')
331					{
332						$file = $fileinfo['name'];
333						$GLOBALS['phpgw']->template->set_var('name',macro_get_link('cwd',$fileinfo['name']));
334						$GLOBALS['phpgw']->template->set_var('del_link',macro_get_link('rmdir',lang('Delete')));
335						$GLOBALS['phpgw']->template->set_var('size','- ' . lang('dir') . ' -');
336					}
337					else
338					{
339						$file = $fileinfo['name'];
340						$GLOBALS['phpgw']->template->set_var('del_link',macro_get_link('delete',lang('Delete')));
341						$GLOBALS['phpgw']->template->set_var('name',macro_get_link('get',$fileinfo['name']));
342						$GLOBALS['phpgw']->template->set_var('size',$fileinfo['size']);
343					}
344					$GLOBALS['phpgw']->template->set_var('rename_link',macro_get_link('rename',lang('Rename')));
345					$GLOBALS['phpgw']->template->set_var('bgclass', $bgclass[($i % 2)]);
346					$GLOBALS['phpgw']->template->fp('rowlist_dir','row',True);
347					$i++;
348				}
349			}
350			ftp_quit($ftp);
351			$GLOBALS['phpgw']->template->pfp('out','main');
352		}
353		else
354		{
355			updateSession();
356			$sessionUpdated=true;
357			if (!$tried_default)
358			{
359				// don't put out an error on the default login
360				for($i=0;$i<strlen($connInfo['password']);$i++)
361				{
362					 $pass.="*";
363				}
364				$GLOBALS['phpgw']->template->set_var('error_message', lang('Failed to connect to %1 with user %2 and password %3',
365					$connInfo['ftpserver'], $connInfo['username'], $pass), true);
366				$GLOBALS['phpgw']->template->parse('out','bad_connect',false);
367				$GLOBALS['phpgw']->template->p('out');
368			}
369			newLogin($connInfo['ftpserver'],$connInfo['username'],'');
370		}
371	}
372	else
373	{
374		// set the login and such to ""
375		updateSession('');
376		$sessionUpdated=true;
377		// $GLOBALS['phpgw']->modsession(
378		newLogin($default_server,$default_login,'');
379	}
380	if (!$sessionUpdated && $action=='cwd')
381	{
382		// echo "updating session with new cwd<BR>\n";
383		updateSession($connInfo);
384	}
385
386	$GLOBALS['phpgw']->common->phpgw_footer();
387?>
388