2007-10-08 01:12:07 +02:00
< ? php
/**
* Postfix Admin
*
* LICENSE
* This source file is subject to the GPL license that is bundled with
* this package in the file LICENSE . TXT .
*
* Further details on the project are available at :
* http :// www . postfixadmin . com or http :// postfixadmin . sf . net
*
* @ version $Id $
* @ license GNU GPL v2 or later .
*
* File : fetchmail . php
* Responsible for setting up fetchmail
2009-12-24 15:58:01 +01:00
* template : fetchmail . tpl
2007-10-08 01:12:07 +02:00
*
* @ version $Id $
* @ license GNU GPL v2 or later .
*
* Template Variables :
*
* Form POST \ GET Variables :
*
2007-10-26 00:59:52 +02:00
* GET :
* - edit
* - delete
* - new
*
* POST :
* - save
* - cancel
* - all editable form values , see $fm_struct
2007-10-08 01:12:07 +02:00
*/
require_once ( 'common.php' );
authentication_require_role ( 'admin' );
2007-10-23 02:01:31 +02:00
$extra_options = 0 ;
if ( $CONF [ 'fetchmail_extra_options' ] == 'YES' ) $extra_options = 1 ;
2007-10-26 00:59:52 +02:00
# import control GET/POST variables. Form values are imported below.
$new = ( int ) safeget ( " new " ) == 1 ? 1 : 0 ;
$edit = ( int ) safeget ( " edit " );
$delete = ( int ) safeget ( " delete " );
$save = safepost ( " save " ) != " " ? 1 : 0 ;
$cancel = safepost ( " cancel " ) != " " ? 1 : 0 ;
$display_status = 1 ;
if ( $new || $edit ) $display_status = 0 ;
2007-11-12 23:06:01 +01:00
$fm_struct = array ( // list($editible,$view,$type)
# field name allow editing? display field? type
" id " => array ( 0 , 0 , 'id' ),
" mailbox " => array ( 1 , 1 , 'enum' ),
" src_server " => array ( 1 , 1 , 'text' ),
" src_auth " => array ( 1 , 1 , 'enum' ),
" src_user " => array ( 1 , 1 , 'text' ),
" src_password " => array ( 1 , 0 , 'password' ),
" src_folder " => array ( 1 , 1 , 'text' ),
" poll_time " => array ( 1 , 1 , 'num' ),
" fetchall " => array ( 1 , 1 , 'bool' ),
" keep " => array ( 1 , 1 , 'bool' ),
" protocol " => array ( 1 , 1 , 'enum' ),
2008-11-12 22:48:31 +01:00
" usessl " => array ( 1 , 1 , 'bool' ),
2007-11-12 23:06:01 +01:00
" extra_options " => array ( $extra_options , $extra_options , 'longtext' ),
" mda " => array ( $extra_options , $extra_options , 'longtext' ),
" date " => array ( 0 , $display_status , 'text' ),
" returned_text " => array ( 0 , $display_status , 'longtext' ),
2007-10-08 01:12:07 +02:00
);
2007-11-12 23:06:01 +01:00
# labels and descriptions are taken from $PALANG['pFetchmail_field_xxx'] and $PALANG['pFetchmail_desc_xxx']
# TODO: After pressing save or cancel in edit form, date and returned text are not displayed in list view.
# TODO: Reason: $display_status is set before $new and $edit are reset to 0.
# TODO: Fix: split the "display field?" column into "display in list" and "display in edit mode".
2007-10-08 01:12:07 +02:00
$SESSID_USERNAME = authentication_get_username ();
if ( ! $SESSID_USERNAME )
2007-10-26 01:12:51 +02:00
exit ;
2007-10-08 01:12:07 +02:00
$fm_defaults = array (
2007-10-26 01:12:51 +02:00
" id " => 0 ,
" mailbox " => array ( $SESSID_USERNAME ),
2007-10-26 00:59:52 +02:00
" poll_time " => 10 ,
2007-10-26 01:12:51 +02:00
" src_auth " =>
array ( 'password' , 'kerberos_v5' , 'kerberos' , 'kerberos_v4' , 'gssapi' , 'cram-md5' , 'otp' , 'ntlm' , 'msn' , 'ssh' , 'any' ),
" protocol " =>
array ( 'POP3' , 'IMAP' , 'POP2' , 'ETRN' , 'AUTO' ),
2007-10-08 01:12:07 +02:00
);
2008-09-06 22:32:17 +02:00
$table_fetchmail = table_by_key ( 'fetchmail' );
$table_mailbox = table_by_key ( 'mailbox' );
2007-10-23 02:01:31 +02:00
2008-11-12 22:48:31 +01:00
if ( authentication_has_role ( 'global-admin' )) {
$list_domains = list_domains ();
} else {
$list_domains = list_domains_for_admin ( authentication_get_username ());
}
2007-10-26 00:59:52 +02:00
$user_domains = implode ( " , " , array_values ( $list_domains )); # for displaying
2007-10-23 02:01:31 +02:00
$user_domains_sql = implode ( " ',' " , escape_string ( array_values ( $list_domains ))); # for SQL
2008-09-06 22:32:17 +02:00
$sql = " SELECT username FROM $table_mailbox WHERE domain in (' " . $user_domains_sql . " ') " ; # TODO: replace with domain selection dropdown
2007-10-08 01:12:07 +02:00
$res = db_query ( $sql );
if ( $res [ 'rows' ] > 0 ){
2007-10-26 01:12:51 +02:00
$fm_defaults [ " mailbox " ] = array ();
while ( $name = db_array ( $res [ 'result' ])){
$fm_defaults [ " mailbox " ][] = $name [ " username " ];
}
2007-10-08 01:12:07 +02:00
}
else {
2007-10-26 01:12:51 +02:00
$fm_defaults [ " mailbox " ] = array ();
$fm_defaults [ " mailbox " ][] = $SESSID_USERNAME ; # TODO: Does this really make sense? Or should we display a message "please create a mailbox first!"?
2007-10-08 01:12:07 +02:00
}
2007-10-26 00:59:52 +02:00
$row_id = 0 ;
if ( $delete ) {
$row_id = $delete ;
} elseif ( $edit ) {
$row_id = $edit ;
}
2007-10-08 01:12:07 +02:00
2009-03-27 00:07:45 +01:00
$user_mailboxes_sql = " ' " . implode ( " ',' " , escape_string ( array_values ( $fm_defaults [ " mailbox " ]))) . " ' " ; # mailboxes as SQL
2007-10-26 00:59:52 +02:00
if ( $row_id ) {
2009-03-27 00:07:45 +01:00
$result = db_query ( " SELECT " . implode ( " , " , escape_string ( array_keys ( $fm_struct ))) . " FROM $table_fetchmail WHERE id= $row_id AND mailbox IN ( $user_mailboxes_sql ) " );
# TODO: the "AND mailbox IN ..." part should obsolete the check_owner call. Remove it after checking again.
2007-10-26 00:59:52 +02:00
if ( $result [ 'rows' ] > 0 ) {
$edit_row = db_array ( $result [ 'result' ]);
$account = $edit_row [ 'src_user' ] . " @ " . $edit_row [ 'src_server' ];
}
$edit_row_domain = explode ( '@' , $edit_row [ 'mailbox' ]);
if ( $result [ 'rows' ] <= 0 || ! check_owner ( $SESSID_USERNAME , $edit_row_domain [ 1 ])) { # owner check for $edit and $delete
flash_error ( sprintf ( $PALANG [ 'pFetchmail_error_invalid_id' ], $row_id ));
$edit = 0 ; $delete = 0 ;
}
2007-10-08 01:12:07 +02:00
}
2007-10-26 00:59:52 +02:00
2007-11-12 23:06:01 +01:00
if ( $cancel ) { # cancel $new or $edit
2007-10-26 01:12:51 +02:00
$edit = 0 ;
2007-11-02 01:27:32 +01:00
$new = 0 ;
2007-11-12 23:06:01 +01:00
} elseif ( $delete ) { # delete an entry
2008-09-06 21:27:45 +02:00
$result = db_query ( " delete from $table_fetchmail WHERE id= " . $delete );
2007-10-26 00:59:52 +02:00
if ( $result [ 'rows' ] != 1 )
{
2007-11-12 23:06:01 +01:00
flash_error ( $PALANG [ 'pDelete_delete_error' ]) . '</span>' ;
2007-10-26 00:59:52 +02:00
} else {
flash_info ( sprintf ( $PALANG [ 'pDelete_delete_success' ], $account ));
}
$delete = 0 ;
2007-11-12 23:06:01 +01:00
} elseif ( ( $edit || $new ) && $save ) { # $edit or $new AND save button pressed
2007-10-26 00:59:52 +02:00
$formvars = array ();
2007-10-26 01:12:51 +02:00
foreach ( $fm_struct as $key => $row ){
2007-11-12 23:06:01 +01:00
list ( $editible , $view , $type ) = $row ;
2007-10-26 01:12:51 +02:00
if ( $editible != 0 ){
$func = " _inp_ " . $type ;
$val = safepost ( $key );
2007-10-26 00:59:52 +02:00
if ( $type != " password " || strlen ( $val ) > 0 ) { # skip on empty (aka unchanged) password
$formvars [ $key ] = escape_string ( function_exists ( $func ) ? $func ( $val ) : $val );
}
2007-10-26 01:12:51 +02:00
}
}
2007-10-26 00:59:52 +02:00
$formvars [ 'id' ] = $edit ; # results in 0 on $new
2008-04-26 23:55:16 +02:00
if ( $CONF [ 'database_type' ] == 'pgsql' && $new ) {
// skip - shouldn't need to specify this as it will default to the next available value anyway.
unset ( $formvars [ 'id' ]);
}
2007-10-26 00:59:52 +02:00
if ( ! in_array ( $formvars [ 'mailbox' ], $fm_defaults [ 'mailbox' ])) {
flash_error ( $PALANG [ 'pFetchmail_invalid_mailbox' ]);
$save = 0 ;
}
if ( $formvars [ 'src_server' ] == '' ) {
flash_error ( $PALANG [ 'pFetchmail_server_missing' ]);
# TODO: validate domain name
$save = 0 ;
}
if ( empty ( $formvars [ 'src_user' ]) ) {
flash_error ( $PALANG [ 'pFetchmail_user_missing' ]);
$save = 0 ;
}
if ( $new && empty ( $formvars [ 'src_password' ]) ) {
flash_error ( $PALANG [ 'pFetchmail_password_missing' ]);
$save = 0 ;
}
if ( $save ) {
2008-04-26 23:55:16 +02:00
if ( $new ) {
2008-09-06 21:27:45 +02:00
$sql = " INSERT INTO $table_fetchmail ( " . implode ( " , " , escape_string ( array_keys ( $formvars ))) . " ) VALUES (' " . implode ( " ',' " , escape_string ( $formvars )) . " ') " ;
2007-10-26 00:59:52 +02:00
} else { # $edit
foreach ( array_keys ( $formvars ) as $key ) {
$formvars [ $key ] = escape_string ( $key ) . " =' " . escape_string ( $formvars [ $key ]) . " ' " ;
}
2008-09-06 21:27:45 +02:00
$sql = " UPDATE $table_fetchmail SET " . implode ( " , " , $formvars ) . " ,returned_text='', date=NOW() WHERE id= " . $edit ;
2007-10-26 00:59:52 +02:00
}
$result = db_query ( $sql );
if ( $result [ 'rows' ] != 1 )
{
flash_error ( $PALANG [ 'pFetchmail_database_save_error' ]);
} else {
flash_info ( $PALANG [ 'pFetchmail_database_save_success' ]);
$edit = 0 ; $new = 0 ; # display list after saving
}
} else {
$formvars [ 'src_password' ] = '' ; # never display password
}
2007-11-12 23:06:01 +01:00
} elseif ( $edit ) { # edit entry form
2007-10-26 00:59:52 +02:00
$formvars = $edit_row ;
$formvars [ 'src_password' ] = '' ;
2007-11-12 23:06:01 +01:00
} elseif ( $new ) { # create entry form
2007-10-26 00:59:52 +02:00
foreach ( array_keys ( $fm_struct ) as $value ) {
if ( isset ( $fm_defaults [ $value ])) {
$formvars [ $value ] = $fm_defaults [ $value ];
} else {
$formvars [ $value ] = '' ;
}
}
2007-10-08 01:12:07 +02:00
}
2008-04-04 00:16:15 +02:00
$tFmail = array ();
2007-10-26 00:59:52 +02:00
if ( $edit + $new == 0 ) { # display list
2009-03-27 00:07:45 +01:00
# TODO: ORDER BY would even be better if it would order by the _domain_ of the target mailbox first
$res = db_query ( " SELECT " . implode ( " , " , escape_string ( array_keys ( $fm_struct ))) . " FROM $table_fetchmail WHERE mailbox IN ( $user_mailboxes_sql ) ORDER BY mailbox,src_server,src_user " );
2007-10-26 00:59:52 +02:00
if ( $res [ 'rows' ] > 0 ) {
while ( $row = db_array ( $res [ 'result' ])) {
$tFmail [] = $row ;
}
}
2007-10-08 01:12:07 +02:00
}
function _inp_num ( $val ){
2007-10-26 01:12:51 +02:00
return ( int )( $val );
2007-10-08 01:12:07 +02:00
}
function _inp_bool ( $val ){
2008-04-26 23:55:16 +02:00
return $val ? db_get_boolean ( true ) : db_get_boolean ( false );
2007-10-08 01:12:07 +02:00
}
function _inp_password ( $val ){
2007-10-26 01:12:51 +02:00
return base64_encode ( $val );
2007-10-08 01:12:07 +02:00
}
2009-11-05 15:55:39 +01:00
//*****
$headers = array ();
foreach ( array_keys ( $fm_struct ) as $row ){
list ( $editible , $view , $type ) = $fm_struct [ $row ];
$title = $PALANG [ 'pFetchmail_field_' . $row ];
$comment = $PALANG [ 'pFetchmail_desc_' . $row ];
if ( $view ){
$headers [] = $title ;
// $headers[]=array($editible, $view, $type, $title, $comment);
}
}
function fetchmail_edit_row ( $data = array ())
{
global $fm_struct , $fm_defaults , $PALANG ;
$id = $data [ " id " ];
$_id = $data [ " id " ] * 100 + 1 ;
$ret = " <table> " ;
$ret .= '<tr><td colspan="3"><h3>' . $PALANG [ 'pMenu_fetchmail' ] . '</h3></td></tr>' ;
# TODO: $formvars possibly contains db-specific boolean values
# TODO: no problems with MySQL, to be tested with PgSQL
# TODO: undefined values may also occour
foreach ( $fm_struct as $key => $struct ){
list ( $editible , $view , $type ) = $struct ;
$title = $PALANG [ 'pFetchmail_field_' . $key ];
$comment = $PALANG [ 'pFetchmail_desc_' . $key ];
if ( $editible ){
$ret .= " <tr><td align='left' valign='top'><label for=' ${ _id } ' style='width:20em;'> ${ title } : </label></td> " ;
$ret .= " <td align=left style='padding-left:.25em;padding-right:.25em;background-color:white;'> " ;
$func = " _edit_ " . $type ;
if ( ! function_exists ( $func ))
$func = " _edit_text " ;
$val = isset ( $data [ $key ])
? $data [ $key ]
: ( ! is_array ( $fm_defaults [ $key ])
? $fm_defaults [ $key ]
: ''
);
$fm_defaults_key = " " ; if ( isset ( $fm_defaults [ $key ])) $fm_defaults_key = $fm_defaults [ $key ];
$ret .= $func ( $_id ++ , $key , $fm_defaults_key , $val );
$ret .= " </td><td align=left valign=top><i> ${ comment } </i></td></tr> \n " ;
}
elseif ( $view ){
$func = " _view_ " . $type ;
$val = isset ( $data [ $key ])
? ( function_exists ( $func )
? $func ( $data [ $key ])
: nl2br ( $data [ $key ])
)
: " --x-- " ;
$ret .= " <tr><td align=left valign=top> ${ title } : </label></td> " ;
$ret .= " <td align=left valign=top style='padding-left:.25em;padding-right:.25em;background-color:white;'> " . $val ;
$ret .= " </td><td align=left valign=top><i> ${ comment } </i></td></tr> \n " ;
}
}
$ret .= " <tr><td align=center colspan=3>
< input type = submit name = save value = '" . $PALANG[' save '] . "' > & nbsp ;
< input type = submit name = cancel value = '" . $PALANG[' cancel '] . "' >
" ;
if ( $id ){
$ret .= " <input type=hidden name=edit value=' ${ id } '> " ;
}
$ret .= " </td></tr> \n " ;
$ret .= " </table> \n " ;
$ret .= " <p /> \n " ;
$ret .= " </form> \n " ;
$ret .= " </div> \n " ;
return $ret ;
}
function _edit_text ( $id , $key , $def_vals , $val = " " ){
$val = htmlspecialchars ( $val );
2010-09-29 22:41:52 +02:00
return " <input type=text name= ${ key } id= ${ id } value=' ${ val } ' /> " ;
2009-11-05 15:55:39 +01:00
}
function _edit_password ( $id , $key , $def_vals , $val = " " ){
$val = preg_replace ( " { .} " , " * " , $val );
2010-09-29 22:41:52 +02:00
return " <input type=password name= ${ key } id= ${ id } value=' ${ val } ' /> " ;
2009-11-05 15:55:39 +01:00
}
function _edit_num ( $id , $key , $def_vals , $val = " " ){
$val = ( int )( $val );
2010-09-29 22:41:52 +02:00
return " <input type=text name= ${ key } id= ${ id } value=' ${ val } ' /> " ;
2009-11-05 15:55:39 +01:00
}
function _edit_bool ( $id , $key , $def_vals , $val = " " ){
$ret = " <input type=checkbox name= ${ key } id= ${ id } " ;
if ( $val )
2010-09-29 22:41:52 +02:00
$ret .= ' checked="checked"' ;
$ret .= " /> " ;
2009-11-05 15:55:39 +01:00
return $ret ;
}
function _edit_longtext ( $id , $key , $def_vals , $val = " " ){
$val = htmlspecialchars ( $val );
return " <textarea name= ${ key } id= ${ id } rows=2 style='width:20em;'> ${ val } </textarea> " ;
}
function _edit_enum ( $id , $key , $def_vals , $val = " " ){
$ret = " <select name= ${ key } id= ${ id } > " ;
foreach ( $def_vals as $opt_val ){
$ret .= " <option " ;
if ( $opt_val == $val )
$ret .= " selected " ;
$ret .= " > ${ opt_val } </option> \n " ;
}
$ret .= " </select> \n " ;
return $ret ;
}
function _listview_id ( $val ){
return " <a href='?edit= ${ val}'> ${val } </a> " ;
}
function _listview_bool ( $val ){
return $val ? " + " : " " ;
}
function _listview_longtext ( $val ){
return strlen ( $val ) ? " Text - " . strlen ( $val ) . " chars " : " --x-- " ;
}
function _listview_text ( $val ){
return sizeof ( $val ) ? $val : " --x-- " ;
}
function _listview_password ( $val ){
return preg_replace ( " { .} " , " * " , $val );
}
$smarty -> assign ( 'edit' , $edit );
$smarty -> assign ( 'new' , $new );
2009-12-24 15:58:01 +01:00
$smarty -> assign ( 'fetchmail_edit_row' , fetchmail_edit_row ( $formvars ), false );
2009-11-05 15:55:39 +01:00
$smarty -> assign ( 'headers' , $headers );
$smarty -> assign ( 'user_domains' , $user_domains );
$smarty -> assign ( 'tFmail' , $tFmail );
2007-10-08 01:12:07 +02:00
2009-11-05 15:55:39 +01:00
$smarty -> assign ( 'smarty_template' , 'fetchmail' );
$smarty -> display ( 'index.tpl' );
2007-10-08 01:12:07 +02:00
2007-10-26 00:59:52 +02:00
/* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */
2007-10-08 01:12:07 +02:00
?>