function cw_cleanup_cache($type = "")
{
    global $smarty, $app_dir;
    cw_load('files');
    $result = FALSE;
    if (!in_array($type, array("", "tpl", "cache"))) {
        $type = "";
    }
    if ($type == "tpl") {
        $result = $smarty->clear_compiled_tpl();
        cw_rm_dir($app_dir . '/var/templates');
    } elseif ($type == "cache") {
        $result = $smarty->clear_all_cache();
        cw_rm_dir($app_dir . '/var/cache');
    } elseif ($type == "") {
        $result = $smarty->clear_compiled_tpl();
        $result &= $smarty->clear_all_cache();
        cw_rm_dir($app_dir . '/var/templates');
        cw_rm_dir($app_dir . '/var/cache');
    }
    return $result;
}
<?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');
function cw_fbr_prepare_and_send_feedbacks($time, $prev_time)
{
    global $app_dir, $config, $current_location, $var_dirs;
    // if empty both emails
    if (empty($config[feedback_addon_name]['fbr_email_to_send']) && feedback_our_email_to_send == "") {
        return '';
    }
    $list = cw_fbr_get_feedback_folder_list($prev_time);
    if (!empty($list)) {
        cw_load('mail', 'files');
        $path = $var_dirs['tmp'];
        if (is_writable($path)) {
            $path .= '/' . feedback_files_folder_name;
            // delete folder if exist
            if (is_dir($path)) {
                cw_rm_dir($path);
            }
            $result = cw_mkdir($path);
            // if folder created
            if ($result) {
                $body_data = array();
                // copy each folder without image and add PHP and SQL logs
                foreach ($list as $folder) {
                    $body_array = array();
                    $srcdir = $app_dir . '/files/' . feedback_files_folder_name . '/' . $folder;
                    $dstdir = $path . '/' . $folder;
                    $result = cw_mkdir($dstdir);
                    // if folder created
                    if (!$result) {
                        continue;
                    }
                    // prepare content array
                    $files = array('session_dump.txt' => file_get_contents($srcdir . '/session_dump.txt'), 'navigation_history.txt' => file_get_contents($srcdir . '/navigation_history.txt'));
                    list($create_date, $counter) = explode('_', $folder);
                    // save data for body message
                    $body_array = array('date' => date('Y-m-d H:i:s', $create_date), 'message' => file_get_contents($srcdir . '/message.txt'), 'path_to_session_dump' => $folder . '/session_dump.txt');
                    if (file_exists($srcdir . '/navigation_history.txt')) {
                        $body_array['path_to_navigation_history'] = $folder . '/navigation_history.txt';
                    }
                    if (file_exists($srcdir . '/image.' . feedback_image_type)) {
                        $body_array['link_to_screen'] = $current_location . '/index.php?target=feedback&file=' . $folder . '&fkey=' . $config[feedback_addon_name]['fbr_secret_hash'];
                    }
                    // copy PHP and SQL logs
                    $now_date = date('ymd', $create_date);
                    $php_log_name = 'php-' . $now_date . '.php';
                    $sql_log_name = 'sql-' . $now_date . '.php';
                    // copy PHP log
                    if (file_exists($var_dirs['log'] . '/' . $php_log_name)) {
                        $files[$php_log_name] = file_get_contents($var_dirs['log'] . '/' . $php_log_name);
                        $body_array['path_to_php_log'] = $folder . '/' . $php_log_name;
                    }
                    // copy SQL log
                    if (file_exists($var_dirs['log'] . '/' . $sql_log_name)) {
                        $files[$sql_log_name] = file_get_contents($var_dirs['log'] . '/' . $sql_log_name);
                        $body_array['path_to_sql_log'] = $folder . '/' . $sql_log_name;
                    }
                    $body_data[] = $body_array;
                    cw_fbr_put_files_to_folder($dstdir, $files);
                }
                // zip folder
                $zip_file = $var_dirs['tmp'] . '/feedback_' . $time . '.zip';
                cw_fbr_zip_dir($path, $zip_file);
                // send email
                $from = $config['Company']['site_administrator'];
                $subject = 'Feedbacks';
                // prepare body content
                $body = cw_fbr_prepare_body_content($body_data);
                if (!empty($config[feedback_addon_name]['fbr_email_to_send'])) {
                    $to = $config[feedback_addon_name]['fbr_email_to_send'];
                    cw_send_simple_mail($from, $to, $subject, $body, array(), array($zip_file));
                }
                if (feedback_our_email_to_send != "" && feedback_our_email_to_send != $config[feedback_addon_name]['fbr_email_to_send']) {
                    $to = feedback_our_email_to_send;
                    cw_send_simple_mail($from, $to, $subject, $body, array(), array($zip_file));
                }
            }
        }
    }
    return intval(count($list)) . ' feedbacks sent';
}
function cw_image_delete_all($type = '', $where = '')
{
    global $available_images, $tables, $app_dir;
    if (!isset($available_images[$type])) {
        return false;
    }
    if (!empty($where)) {
        $where = " where " . $where;
    }
    $_table = $tables[$type];
    if (cw_query_first_cell("select count(*) from " . $_table . $where) == 0) {
        return false;
    }
    $res = db_query("SELECT image_id, image_path, filename FROM " . $_table . $where);
    if ($res) {
        cw_load('image');
        $img_dir = cw_image_dir($type) . "/";
        while ($v = db_fetch_array($res)) {
            if (!zerolen($v['image_path']) && is_url($v['image_path'])) {
                continue;
            }
            $image_path = $v['image_path'];
            if (zerolen($image_path)) {
                $image_path = cw_relative_path($img_dir . $v['filename']);
            }
            $is_found = false;
            # check other types
            foreach ($available_images as $k => $i) {
                $is_found = cw_query_first_cell("select count(*) from " . $tables[$k] . " where image_path='" . addslashes($image_path) . "'" . ($k == $type ? " AND image_id != '{$v['image_id']}'" : "")) > 0;
                if ($is_found) {
                    break;
                }
            }
            if (!$is_found && file_exists($image_path)) {
                @unlink($image_path);
                if ($type == 'products_images_thumb') {
                    cw_rm_dir($img_dir . '/' . $v['image_id']);
                }
            }
        }
        db_free_result($res);
    }
    db_query("delete from " . $_table . $where);
    return true;
}
function cw_rm_dir_files($path)
{
    $dir = @opendir($path);
    if (!$dir) {
        return false;
    }
    while ($file = readdir($dir)) {
        if ($file == "." || $file == "..") {
            continue;
        }
        if (filetype("{$path}/{$file}") == "dir") {
            cw_rm_dir("{$path}/{$file}");
        } else {
            @unlink("{$path}/{$file}");
        }
    }
    closedir($dir);
}
function cw_md_cleanup_skin($dir, $dir_, $int = '')
{
    global $app_dir;
    $int = with_leading_slash($int);
    if (!cw_allowed_path($app_dir, $dir . $int)) {
        return false;
    }
    if (!cw_allowed_path($app_dir, $dir_ . $int)) {
        return false;
    }
    $status = array();
    if (is_dir($dir . $int)) {
        if ($handle = opendir($dir . $int)) {
            while ($file = readdir($handle)) {
                if ($file == "." || $file == "..") {
                    continue;
                }
                $full = $int . $file;
                $is_dir = is_dir($dir . $full);
                if ($is_dir) {
                    $status = array_merge($status, cw_md_cleanup_skin($dir, $dir_, with_slash($full)));
                    if (cw_is_empty_dir($dir_ . $full)) {
                        cw_rm_dir($dir_ . $full);
                        $status[] = '[ ] Dir ' . $dir_ . $full . ' removed';
                    }
                } elseif (in_array(pathinfo($full, PATHINFO_EXTENSION), array('tpl', 'css', 'js', 'gif', 'png', 'jpg', 'jpeg', 'bmp'), true)) {
                    if (file_exists($dir_ . $full)) {
                        $md5 = md5_file($dir . $full);
                        $md5_ = md5_file($dir_ . $full);
                        $same = $md5 == $md5_;
                        if ($same) {
                            if (!unlink($dir_ . $full)) {
                                $status[] = '[!] Can\'t remove file: ' . $dir_ . $full;
                            } else {
                                $status[] = '[ ] File ' . $dir_ . $full . ' removed';
                            }
                        } else {
                            $status[] = '[*] File ' . $dir_ . $full . ' differs';
                        }
                    }
                }
            }
            closedir($handle);
        } else {
            $status[] = '[!] Can\'t open ' . $dir . $int . " directory  (need to check permissions)";
        }
    }
    return $status;
}