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: functions.inc.php 15241 2004-07-15 01:56:03Z skwashd $ */
14
15	//NOTE: This is a hack to fix the register_globals problems with this app in 16
16	//TODO: Fix it properly
17	foreach($_GET as $name => $value)
18	{
19		$$name = $value;
20		$HTTP_GET_VARS[$name] = $value;
21	}
22
23	foreach($_POST as $name => $value)
24	{
25		$$name = $value;
26		$HTTP_POST_VARS[$name] = $value;
27	}
28
29	function createLink($string,$params='')
30	{
31		return $GLOBALS['phpgw']->link($string,$params);
32	}
33
34	function getConnectionInfo()
35	{
36		return $GLOBALS['phpgw']->session->appsession();
37	}
38
39	function phpftp_connect($host,$user,$pass)
40	{
41		// echo "connecting to $host with $user and $pass\n";
42		$ftp = ftp_connect($host);
43		if ( $ftp )
44		{
45			if ( ftp_login($ftp,$user,$pass) )
46			{
47				return $ftp;
48			}
49		}
50	}
51
52	function renameForm($session,$filename,$directory)
53	{
54		$rename_form_begin= '<form action="' . createLink($GLOBALS['target']) . '" method="post">'."\n"
55			. '<input type="hidden" name="action" value="rename">'."\n"
56			. '<input type="hidden" name="olddir" value="'.$directory.'">'."\n"
57			. '<input type="hidden" name="newdir" value="'.$directory.'">'."\n"
58			. '<input type="hidden" name="filename" value="'.$filename.'">'."\n";
59		$rename_form_end = '</form>'."\n";
60		$rename_form_from=  $filename;
61		$rename_form_to='<input type="text" name="newfilename" size="20" value="">';
62		$rename_form_submit='<input type="submit" name="confirm" value="' . lang('rename') . '">'."\n";
63		$rename_form_cancel='<input type="submit" name="cancel" value="' . lang('cancel') . '">'."\n";
64
65		$GLOBALS['phpgw']->template->set_var(array(
66			'rename_form_begin' => $rename_form_begin,
67			'rename_form_end'  => $rename_form_end,
68			'rename_form_from' => $rename_form_from,
69			'rename_form_to' => $rename_form_to,
70			'rename_form_submit' => $rename_form_submit,
71			'rename_form_cancel' => $rename_form_cancel,
72			'lang_rename_from' => lang('rename from'),
73			'lang_rename_to' => lang('rename to')
74		));
75
76		$GLOBALS['phpgw']->template->set_var('lang_message',lang('Rename file'));
77
78		$GLOBALS['phpgw']->template->parse('out','rename',true);
79		// $template->p('renameform');
80		$GLOBALS['phpgw']->template->set_var('return',$GLOBALS['phpgw']->template->get('out'));
81		return $GLOBALS['phpgw']->template->get('return');
82	}
83
84	function confirmDeleteForm($session,$filename,$directory,$type ='')
85	{
86		$delete_form_begin= '<form action="' . createLink($GLOBALS['target']) . '" method="post">'."\n"
87			. '<input type="hidden" name="action" value="delete">'."\n"
88			. '<input type="hidden" name="olddir" value="'.$directory.'">'."\n"
89			. '<input type="hidden" name="newdir" value="'.$directory.'">'."\n"
90			. '<input type="hidden" name="file" value="'.$filename.'">'."\n";
91		$delete_form_end = '</form>'."\n";
92		$delete_form_question = lang ('Are you sure you want to delete %1 ?', $filename);
93		$delete_form_from= $directory . '/' . $filename;
94		$delete_form_to='<input type="text" name="newname" size=20" value="">';
95		$delete_form_confirm='<input type="submit" name="confirm" value="' . lang('delete') . '">'."\n";
96		$delete_form_cancel='<input type="submit" name="cancel" value="' . lang('cancel') . '">'."\n";
97
98		$GLOBALS['phpgw']->template->set_var(array(
99			'delete_form_begin' => $delete_form_begin,
100			'delete_form_end'  => $delete_form_end,
101			'delete_form_question' => $delete_form_question,
102			'delete_form_confirm' => $delete_form_confirm,
103			'delete_form_cancel' => $delete_form_cancel
104		));
105
106		$GLOBALS['phpgw']->template->parse('out','confirm_delete',true);
107		$GLOBALS['phpgw']->template->set_var('return',$GLOBALS['phpgw']->template->get('out'));
108		return $GLOBALS['phpgw']->template->get('return');
109	}
110
111	function newLogin($dfhost,$dfuser,$dfpass)
112	{
113		$login_form_begin= '<form action="'.createLink($GLOBALS['target']).'" method="post">'."\n".'<input type="hidden" name="action" value="login">'."\n";
114		$login_form_end='</form>'."\n";
115		$login_form_username='<input type="text" name="username" value="'.$dfuser.'">';
116		$login_form_password='<input type="password" name="password" value="'.$dfpass.'">';
117		$login_form_ftpserver='<input type="text" name="ftpserver" value="'.$dfhost.'">';
118		$login_form_submit='<input type="submit" name="submit" value="'.lang('connect').'">'."\n";
119		$login_form_end="</form>";
120
121		$GLOBALS['phpgw']->template->set_var(array(
122			'login_form_begin' => $login_form_begin,
123			'login_form_end' => $login_form_end,
124			'login_form_username' => $login_form_username,
125			'login_form_password' => $login_form_password,
126			'login_form_ftpserver' => $login_form_ftpserver,
127			'login_form_submit' => $login_form_submit,
128			'lang_username' => lang('username'),
129			'lang_password' => lang('password'),
130			'langserver' => lang('Ftp Server')
131		));
132		$GLOBALS['phpgw']->template->set_var('lang_login',lang('Log into FTP server'));
133		$GLOBALS['phpgw']->template->set_var('lang_ftpserver',lang('FTP hostname'));
134
135		$GLOBALS['phpgw']->template->parse('loginform','login',false);
136		$GLOBALS['phpgw']->template->p('loginform');
137		return;
138	}
139
140	function phpftp_get( $ftp, $tempdir, $dir, $file )
141	{
142		srand((double)microtime()*1000000);
143		$randval = rand();
144		$tmpfile=$tempdir.'/'.$file.".".$randval;
145		ftp_chdir($ftp,$dir);
146		$remotefile=$dir . '/' . $file;
147		if ( ! ftp_get( $ftp, $tmpfile, $remotefile, FTP_BINARY ) )
148		{
149			echo 'tmpfile="' . $tmpfile . '",file="' . $remotefile . '"<br>' . "\n";
150			ftp_quit( $ftp );
151			echo macro_get_Link('newlogin','Start over?');
152			$retval=0;
153		}
154		else
155		{
156			ftp_quit( $ftp );
157			$b = CreateObject('phpgwapi.browser');
158			if ($GLOBALS['phpgw_info']['server']['ftp_use_mime'])
159			{
160				$mime = getMimeType($file);
161				$b->content_header($file,$mime);
162			}
163			else
164			{
165				$b->content_header($file);
166			}
167			//header( "Content-Type: application/octet-stream" );
168			//header( "Content-Disposition: attachment; filename=" . $file );
169			readfile( $tmpfile );
170			$retval=1;
171		}
172		@unlink( $tmpfile );
173		return $retval;
174	}
175
176	function getMimeType($file)
177	{
178		$file=basename($file);
179		$mimefile = PHPGW_APP_ROOT . SEP . 'mime.types';
180		$fp=fopen($mimefile,"r");
181		$contents = explode("\n",fread ($fp, filesize($mimefile)));
182		fclose($fp);
183
184		$parts=explode(".",$file);
185		$ext=$parts[(sizeof($parts)-1)];
186
187		for($i=0;$i<sizeof($contents);$i++)
188		{
189			if (! ereg("^#",$contents[$i]))
190			{
191				$line=split("[[:space:]]+", $contents[$i]);
192				if (sizeof($line) >= 2)
193				{
194					for($j=1;$j<sizeof($line);$j++)
195					{
196						if ($line[$j] == $ext)
197						{
198							$mimetype=$line[0];
199							return $mimetype;
200						}
201					}
202				}
203			}
204		}
205		return 'text/plain';
206	}
207
208	function phpftp_view( $ftp, $tempdir, $dir, $file )
209	{
210		srand((double)microtime()*1000000);
211		$randval = rand();
212		$tmpfile="$tempdir/" . $file . "." . $randval;
213		ftp_chdir($ftp,$dir);
214		$remotefile=$dir . "/" . $file;
215		if ( ! ftp_get( $ftp, $tmpfile, $remotefile, FTP_BINARY ) )
216		{
217			echo "tmpfile=\"$tmpfile\",file=\"$remotefile\"<BR>\n";
218			macro_get_Link('newlogin','Start over?');
219			$retval=0;
220		}
221		else
222		{
223			$content_type=getMimeType($remotefile);
224			header('Content-Type: '.$content_type);
225			readfile( $tmpfile );
226			$retval=1;
227		}
228		@unlink( $tmpfile );
229		return $retval;
230	}
231
232	function updateSession($string='')
233	{
234		$GLOBALS['phpgw']->common->appsession($string);
235		return;
236	}
237
238	function analysedir($dirline)
239	{
240		if (ereg("([-dl])[rwxst-]{9}",substr($dirline,0,10)))
241		{
242			$GLOBALS['systyp'] = 'UNIX';
243		}
244
245		if (substr($dirline,0,5) == 'total')
246		{
247			$dirinfo[0] = -1;
248		}
249		elseif($GLBALS['systyp'] == 'Windows_NT')
250		{
251			if (ereg("[-0-9]+ *[0-9:]+[PA]?M? +<DIR> {10}(.*)",$dirline,$regs))
252			{
253				$dirinfo[0] = 1;
254				$dirinfo[1] = 0;
255				$dirinfo[2] = $regs[1];
256			}
257			elseif(ereg("[-0-9]+ *[0-9:]+[PA]?M? +([0-9]+) (.*)",$dirline,$regs))
258			{
259				$dirinfo[0] = 0;
260				$dirinfo[1] = $regs[1];
261				$dirinfo[2] = $regs[2];
262			}
263		}
264		elseif($GLOBALS['systyp'] == 'UNIX')
265		{
266			//echo "$dirline <br />";
267			$ta = explode(' ',$dirline);
268
269			if(count($ta))
270			{
271				foreach($ta as $p)
272				{
273					if(trim($p) != '')//need to let 0 thru
274					{
275						$a[] = $p;
276					}
277				}
278				$fileinfo['permissions'] = $a[0];
279				$fileinfo['owner']       = $a[2];
280				$fileinfo['group']       = $a[3];
281				$fileinfo['size']        = $a[4];
282				$fileinfo['date']        = $a[5] . '-' . $a[6] . '-' . $a[7];
283				$fileinfo['name']        = $a[8];
284			}
285			else //if something goes wrong
286			{
287				$fileinfo['name'] = $ta[0];
288			}
289			//echo '<pre>'; print_r($fileinfo); echo '</pre>';
290		}
291
292		if (($dirinfo[2]=='.') || ($dirinfo[2]=='..'))
293		{
294			$dirinfo[0] = 0;
295		}
296
297		return $fileinfo;
298	}
299
300	function phpftp_getList($ftp,$dir,$start)
301	{
302		$GLOBALS['real_systyp'] = ftp_systype($ftp);
303
304		ftp_chdir($ftp,$dir);
305		$dirlist = ftp_rawlist($ftp,'');
306		for ($i=$start; $i<($start+$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs']); $i++)
307		{
308			if ($i < count($dirlist))
309			{
310				$dirinfo[] = analysedir($dirlist[$i]);
311			}
312		}
313		return $dirinfo;
314	}
315
316	function macro_get_Link($action,$string)
317	{
318		// globals everything it needs but the string to link
319		global $olddir, $newdir, $file;
320
321		$retval = '<a href="'
322			. $GLOBALS['phpgw']->link($GLOBALS['target'],
323					array(
324						'olddir'	=> $olddir,
325						'action'	=> $action,
326						'file'		=> $file,
327						'newdir'	=> $newdir
328					)
329				)
330			. '">';
331		$retval .= $string;
332		$retval .= '</a>';
333		return $retval;
334	}
335
336	function phpftp_delete($file,$confirm)
337	{
338	}
339
340	function phpftp_rename($origfile,$newfile,$confirm)
341	{
342	}
343?>
344