$page['start'] = 0;
}
$page['nb_pendings_per_page'] = 10;
// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok                      |
// +-----------------------------------------------------------------------+
check_status(ACCESS_ADMINISTRATOR);
// +-----------------------------------------------------------------------+
// | template init                                                         |
// +-----------------------------------------------------------------------+
$template->set_filenames(array('plugin_admin_content' => dirname(__FILE__) . '/admin_pendings.tpl'));
// +-----------------------------------------------------------------------+
// | pending photos list                                                   |
// +-----------------------------------------------------------------------+
$list = array();
$pending_ids = pfemail_get_pending_ids();
if (empty($pending_ids)) {
    $pending_ids[] = -1;
}
$query = '
SELECT
    id,
    path,
    date_creation,
    date_available,
    name,
    comment,
    author,
    file,
    comment,
function pfemail_check_accounts()
{
    global $conf, $user;
    conf_update_param('pfemail_last_check', date('Y-m-d H:i:s'));
    require_once PFEMAIL_PATH . 'include/ImapMailbox.php';
    $image_ids = array();
    $query = '
SELECT
    *
  FROM ' . PFEMAIL_MAILBOXES_TABLE . '
;';
    $accounts = query2array($query);
    foreach ($accounts as $account) {
        $mailbox = new ImapMailbox($account['path'], $account['login'], $account['password'], $conf['upload_dir'] . '/buffer', 'utf-8');
        $mails = array();
        // Get some mail
        $mailsIds = $mailbox->searchMailBox('UNSEEN');
        if (!$mailsIds) {
            continue;
            // check next email account
        }
        $mailId = reset($mailsIds);
        $mail = $mailbox->getMail($mailId);
        $attachments = $mail->getAttachments();
        include_once PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php';
        foreach ($attachments as $attachment) {
            $extension = strtolower(get_extension($attachment->{'name'}));
            if (!in_array($extension, $conf['picture_ext'])) {
                // the file has been downloaded, we have to remove it now
                unlink($attachment->{'filePath'});
                continue;
            }
            $moderate = get_boolean($account['moderated']);
            $image_id = add_uploaded_file($attachment->{'filePath'}, stripslashes($attachment->{'name'}), array($account['category_id']), $moderate ? 16 : 0, null);
            // the photo is added by nobody (using the current user may make the
            // photo editable by her with Admin Tools...)
            single_update(IMAGES_TABLE, array('added_by' => null, 'name' => pfemail_clean_email_subject($mail->subject)), array('id' => $image_id));
            $state = 'auto_validated';
            if ($moderate) {
                $state = 'moderation_pending';
            }
            list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
            single_insert(PFEMAIL_PENDINGS_TABLE, array('image_id' => $image_id, 'state' => $state, 'added_on' => $dbnow, 'from_name' => $mail->fromName, 'from_address' => $mail->fromAddress, 'subject' => $mail->subject));
            $image_ids[] = $image_id;
        }
    }
    if (count($image_ids) > 0) {
        include_once PHPWG_ROOT_PATH . 'admin/include/functions.php';
        invalidate_user_cache();
        // let's notify administrators
        $query = '
SELECT id
  FROM ' . GROUPS_TABLE . '
;';
        $group_ids = query2array($query, null, 'id');
        if (count($group_ids) > 0) {
            include_once PHPWG_ROOT_PATH . 'include/functions_mail.inc.php';
            $thumb_urls = array();
            // force $conf['derivative_url_style'] to 2 (script) to make sure we
            // will use i.php?/upload and not _data/i/upload because you don't
            // know when the cache will be flushed
            $previous_derivative_url_style = $conf['derivative_url_style'];
            $conf['derivative_url_style'] = 2;
            $query = '
SELECT
    id,
    path
  FROM ' . IMAGES_TABLE . '
  WHERE id IN (' . implode(',', $image_ids) . ')
;';
            $result = pwg_query($query);
            while ($row = pwg_db_fetch_assoc($result)) {
                $thumb = DerivativeImage::thumb_url(array('id' => $row['id'], 'path' => $row['path']));
                $thumb_urls[] = $thumb;
            }
            // restore configuration setting
            $conf['derivative_url_style'] = $previous_derivative_url_style;
            $thumbs_html_string = '';
            foreach ($thumb_urls as $thumb_url) {
                if (!empty($thumbs_html_string)) {
                    $thumbs_html_string .= ' ';
                }
                $thumbs_html_string .= '<img src="' . $thumb_url . '">';
            }
            $content = $thumbs_html_string;
            // how many photos pending?
            $pendings = pfemail_get_pending_ids();
            if (count($pendings) > 0) {
                $content .= '<br><br>';
                $content .= '<a href="' . get_absolute_root_url() . 'admin.php?page=plugin-photo_from_email-pendings' . '">';
                $content .= l10n('%d photos pending for validation', count($pendings));
                $content .= '</a>';
            }
            $real_user_id = $user['id'];
            $user['id'] = $conf['guest_id'];
            $subject = l10n('%d photos added by email', count($thumb_urls));
            foreach ($group_ids as $group_id) {
                pwg_mail_group($group_id, array('subject' => '[' . $conf['gallery_title'] . '] ' . $subject, 'mail_title' => $conf['gallery_title'], 'mail_subtitle' => $subject, 'content' => $content, 'content_format' => 'text/html'));
            }
        }
        // restore current user
        $user['id'] = $real_user_id;
    }
}