/**
 * Return the folder list in provided directory
 * folders are returned with absolute path
 *
 * @param string $dir
 * @param int $mode - binary flag [1-files; 2-folders; 3-both]
 * @param boolean $recursive
 * @return array
 */
function cw_files_get_dir($dir, $mode = 1, $recursive = false)
{
    if (!is_dir($dir)) {
        return false;
    }
    // if
    $folders = array();
    if ($dirstream = @opendir($dir)) {
        while (false !== ($filename = readdir($dirstream))) {
            $path = with_slash($dir) . $filename;
            if ($filename != '.' && $filename != '..') {
                if ($mode & 1 && is_file($path)) {
                    $folders[] = $path;
                }
                if ($mode & 2 && is_dir($path)) {
                    $folders[] = $path;
                }
                if ($recursive && is_dir($path)) {
                    $sub_folders = cw_files_get_dir($path, $mode, $recursive);
                    if (is_array($sub_folders)) {
                        $folders = array_merge($folders, $sub_folders);
                    }
                    // if
                }
                // if
            }
            // if
        }
        // while
    }
    // if
    closedir($dirstream);
    return $folders;
}
$logged_data = cw_query($s = "select ld.*, IF(ld.customer_id>0,1,0) as is_logged from {$tables['logged_data']} as ld {$where_string_qry} {$orderby_qry} {$limit_qry}");
$session_ids = array();
foreach ($logged_data as $ld_k => $ld_v) {
    $session_ids[$ld_v['cwsid']] = 1;
    $logged_data[$ld_k]['GET_POST'] = unserialize($ld_v['GET_POST']);
}
$smarty->assign('logged_data', $logged_data);
$_sess_data = cw_query("select * from {$tables['logged_data_sessions']} where cwsid in ('" . implode("', '", array_keys($session_ids)) . "')");
$sess_data = array();
foreach ($_sess_data as $s_data) {
    $s_data['SERVER'] = unserialize($s_data['SERVER']);
    $s_data['user_account'] = unserialize($s_data['user_account']);
    $_cwsid = $s_data['cwsid'];
    unset($s_data['cwsid']);
    $sess_data[$_cwsid] = $s_data;
}
$smarty->assign('sess_data', $sess_data);
$unq_target_code = cw_query_column("select distinct target_code from {$tables['logged_data']} as ld", 'target_code');
$smarty->assign('unq_target_code', $unq_target_code);
$_all_arch_files = cw_files_get_dir($var_dirs['logs_archive'], 1, true);
$all_arch_files = array();
foreach ($_all_arch_files as $f_name) {
    if (is_file($f_name)) {
        $all_arch_files[] = substr($f_name, strlen($var_dirs['logs_archive']));
    }
}
asort($all_arch_files);
$smarty->assign('all_arch_files', $all_arch_files);
$smarty->assign('logging_search', $logging_search);
$smarty->assign('log_columns', $log_columns);
$smarty->assign('main', 'logging');
    if ($action == 'delete_email') {
        foreach ($_POST['delete'] as $mail_id => $v) {
            db_query("DELETE FROM {$tables['mail_spool']} WHERE mail_id='{$mail_id}'");
        }
    }
    if ($action == 'check_email' && !empty($_POST['email'])) {
        cw_call('cw_send_mail', array($config['Company']['site_administrator'], $_REQUEST['email'], 'mail/' . $_REQUEST['subject'], 'mail/' . $_REQUEST['body']));
    }
    if ($action == 'pause_email_send') {
        db_query("REPLACE {$tables['config']} (name, config_category_id, value) values ('pause_email_sending', 1, '{$pause_email_value}')");
        cw_add_ajax_block(array('id' => 'pause_email_sending_container', 'action' => 'append', 'content' => ''));
        return;
    }
    cw_header_location("index.php?target={$target}");
}
$mail_templates = cw_files_get_dir($app_dir . $app_skin_dir . '/mail', 1, true);
foreach ($mail_templates as $mt) {
    $mt = str_replace($app_dir . $app_skin_dir . '/mail/', '', $mt);
    if (strpos($mt, '_subj.tpl') !== false) {
        $subjects[] = $mt;
    } else {
        $bodies[] = $mt;
    }
}
$mail_spool_total = cw_query_first_cell("SELECT count(*) FROM {$tables['mail_spool']}");
$mail_spool = cw_query("SELECT mail_id, mail_to, subject, body FROM {$tables['mail_spool']} ORDER BY mail_id DESC LIMIT 20");
$smarty->assign(array('subjects' => $subjects, 'bodies' => $bodies));
$smarty->assign('mail_spool_total', $mail_spool_total);
$smarty->assign('mail_spool', $mail_spool);
$smarty->assign('pause_email_sending', $config['pause_email_sending']);
// E-mail
<?php

$dir_to_unpack = $var_dirs['tmp'] . '/addons';
$addonname =& cw_session_register('addonname');
$files_to_import = array();
$files_to_import = cw_files_get_dir($dir_to_unpack, 2);
if (is_array($files_to_import)) {
    $addonname = basename($files_to_import[0]);
} else {
    $addonname = '';
}
if ($action == 'upload') {
    $file_path = cw_move_uploaded_file('filename');
    if (is_file($file_path)) {
        cw_rm_dir($dir_to_unpack);
        @mkdir($dir_to_unpack);
        system('tar -xzf ' . escapeshellarg($file_path) . ' -C ' . $dir_to_unpack);
        cw_header_location('index.php?target=addons_manager&action=install');
    }
} elseif ($action == 'install') {
    $smarty->assign('addonname', $addonname);
}
if (!file_exists($dir_to_unpack . "/{$addonname}/INSTALLED")) {
    $smarty->assign('uploaded', $addonname);
}
$smarty->assign('main', 'addons_manager');
                        break;
                }
                cw_event('on_cms_update', array($contentsection_id, $content_section));
                $presaved_content_section = array();
                cw_header_location('index.php?target=cms&mode=update&contentsection_id=' . $contentsection_id);
            }
            break;
    }
    cw_header_location('index.php?target=cms&mode=add');
}
$categories = cw_ab_get_cms_categories($contentsection_id);
$products = cw_ab_get_cms_products($contentsection_id);
$manufacturers = cw_ab_get_cms_manufacturers($contentsection_id);
$clean_urls = cw_ab_get_cms_clean_urls($contentsection_id);
$restricted_attributes = cw_ab_get_cms_restrict_attributes($contentsection_id);
$skins = cw_files_get_dir($app_dir . '/skins/addons/cms/skins', 2);
$skins = array_map('basename', $skins);
$presaved_content_section['image'] = 0;
//cw_image_get('cms_images', intval($presaved_content_section['contentsection_id']));
$attributes = cw_func_call('cw_attributes_get', array('item_id' => 0, 'item_type' => 'AB', 'language' => $edited_language));
if (!empty($contentsection_id)) {
    $query = "SELECT *, service_code as service_code FROM {$tables['cms']} WHERE contentsection_id = '" . intval($contentsection_id) . "'";
    $content_section = cw_query_first($query);
    if (!empty($content_section) && is_array($content_section) && $mode != 'add') {
        $content_section_alt_languages = cw_query_first("SELECT name, alt, url, content FROM {$tables['cms_alt_languages']} WHERE contentsection_id = '" . intval($contentsection_id) . "' AND code = '" . $current_language . "'");
        if (!empty($content_section_alt_languages) && is_array($content_section_alt_languages)) {
            $content_section['name'] = $content_section_alt_languages['name'];
            $content_section['alt'] = $content_section_alt_languages['alt'];
            $content_section['url'] = $content_section_alt_languages['url'];
            $content_section['content'] = $content_section_alt_languages['content'];
        }
/**
 * Scan tmplates dir and declare hooks for all files named as hook pattern, eg:
 * file.tpl@label
 * file.tpl#pre
 * file.tpl@lable#post
 */
function cw_addons_scan_skin($skin_dir)
{
    global $app_dir;
    if (!($skins_hooks = cw_cache_get(basename($skin_dir), 'skins_hooks')) || defined('DEV_MODE')) {
        $skins_hooks = array();
        $files = cw_files_get_dir($skin_dir, 1, true);
        foreach ($files as $file) {
            if (strpos($file, '.tpl@') !== false || strpos($file, '.tpl#') !== false) {
                $file = str_replace($app_dir . '/' . basename($skin_dir) . '/', '', $file);
                $type = '';
                list($fname, $type) = explode('#', $file);
                if (empty($type)) {
                    $type = 'replace';
                }
                $skins_hooks[] = array($type, $fname, $file);
            }
        }
        cw_cache_save($skins_hooks, basename($skin_dir), 'skins_hooks');
    }
    if (!empty($skins_hooks)) {
        foreach ($skins_hooks as $hook) {
            cw_addons_set_template($hook);
        }
    }
}
    cw_event_listen('on_build_order_hash', 'cw_seller_on_build_order_hash');
    cw_addons_set_hooks(array('post', 'cw_doc_place_order', 'cw_seller_doc_place_order'), array('post', 'cw_product_get', 'cw_seller_product_get'));
    cw_addons_set_template(array('pre', 'customer/products/additional_data.tpl', 'addons/' . seller_addon_name . '/products/seller_owner.tpl'), array('pre', 'customer/products/search.tpl', 'addons/' . seller_addon_name . '/products/seller_section.tpl'));
}
if (APP_AREA == 'admin') {
    cw_set_controller(APP_AREA . '/user_V.php', 'addons/' . seller_addon_name . '/core/user_V.php', EVENT_REPLACE);
    cw_addons_set_hooks(array('post', 'cw_product_get', 'cw_seller_product_get'));
    cw_addons_set_template(array('pre', 'main/products/product/details.tpl', 'addons/' . seller_addon_name . '/products/seller.tpl'));
}
if (APP_AREA == 'seller') {
    /* Map controllers from seller area to their real location under addons/seller/core */
    if (!file_exists($app_main_dir . '/seller')) {
        // There is no symlink /core/seller, map all files explicitly
        if (!($addon_hooks = cw_cache_get('seller', 'addon_hooks')) || defined('DEV_MODE')) {
            $addon_hooks = array();
            $files = cw_files_get_dir($app_main_dir . '/addons/seller/core', 1, false);
            foreach ($files as $file) {
                if (strpos($file, '.php') !== false) {
                    $file = str_replace($app_main_dir . '/addons/seller/core/', '', $file);
                    $addon_hooks[] = $file;
                }
            }
            cw_cache_save($addon_hooks, 'seller', 'addon_hooks');
        }
        foreach ($addon_hooks as $h) {
            cw_set_controller("seller/{$h}", 'addons/' . seller_addon_name . "/core/{$h}", EVENT_REPLACE);
        }
    } else {
        // Symlink is used, everything mapped by filesystem
    }
    cw_set_controller(APP_AREA . '/products_clients.php', 'admin/products_clients.php', EVENT_REPLACE);
function cw_ebay_get_avail_export_files_list()
{
    global $current_location;
    cw_load('files');
    $files_list = array();
    $files = cw_files_get_dir(trim(addon_files_location_path, "/"));
    if (is_array($files)) {
        foreach ($files as $file) {
            $paths = explode("/", $file);
            $file_name = array_pop($paths);
            $files_list[] = array("path" => $current_location . "/" . $file, "name" => $file_name);
        }
    }
    return $files_list;
}