// Convert contents into an array
$ccount_database = isset($data[1]) ? unserialize($data[1]) : array();
unset($data);
// Link ID
$id_old = preg_replace('/[^0-9a-zA-Z_\\-\\.]/', '', pj_REQUEST('id'));
// Link ID exists?
if (strlen($id_old) < 1 || !isset($ccount_database[$id_old])) {
    $_SESSION['PJ_MESSAGES']['ERROR'] = 'Invalid link ID';
    header('Location: admin.php');
    exit;
}
$id_new = $id_old;
// Add a new link?
if (pj_POST('action') == 'save' && pj_token_check()) {
    // Check demo mode
    pj_demo('edit_link.php?id=' . $id_old);
    // Link URL
    $ccount_database[$id_old]['L'] = pj_validateURL(pj_POST('url')) or $error_buffer['url'] = 'Enter a valid URL address.';
    // Link title
    $ccount_database[$id_old]['T'] = stripslashes(pj_input(pj_POST('title'))) or $title = '';
    // Link ID
    $id_new = pj_input(pj_POST('newid'));
    // Check ID syntax
    if (preg_match('/[^0-9a-zA-Z_\\-\\.]/', $id_new)) {
        $error_buffer['id'] = 'Invalid link ID. Leave it empty or use only these chars: a-z A-Z 0-9 _ - .';
    } elseif (strlen($id_new) > 0 && $id_new != $id_old) {
        // A duplicate ID?
        if (isset($ccount_database[$id_new])) {
            $error_buffer['id'] = 'Link with this ID already exists! Each link requires a unique ID (leave empty to use current one).';
        } else {
            $warn_new_link = true;
// Are we logged in?
pj_isLoggedIn(true);
// The settings file is in parent folder
$ccount_settings['db_file'] = '../' . $ccount_settings['db_file'];
// Get links
if (file_exists($ccount_settings['db_file'])) {
    // Get links database
    $data = explode('//', file_get_contents($ccount_settings['db_file']), 2);
    // Convert contents into an array
    $ccount_database = isset($data[1]) ? unserialize($data[1]) : array();
    unset($data);
    // Any special actions?
    $action = pj_GET('action');
    if ($action && pj_token_check()) {
        // Check demo mode
        pj_demo();
        // Link ID
        $modified_id = preg_replace('/[^0-9a-zA-Z_\\-\\.]/', '', pj_GET('id'));
        // Link ID exists?
        if ($action != 'reset_all' && (strlen($modified_id) < 1 || !isset($ccount_database[$modified_id]))) {
            $_SESSION['PJ_MESSAGES']['ERROR'] = 'Invalid link ID';
            header('Location: admin.php');
            exit;
        }
        // Do the action
        if ($action == 'reset') {
            $ccount_database[$modified_id]['C'] = 0;
            $ccount_database[$modified_id]['U'] = 0;
            $success_message = 'Link with ID ' . $modified_id . ' has been reset';
        } elseif ($action == 'delete') {
            unset($ccount_database[$modified_id]);
        }
        if ($restore_existing) {
            // Update database file
            if (@file_put_contents($ccount_settings['db_file'], $existing, LOCK_EX) === false) {
                $_SESSION['PJ_MESSAGES']['ERROR'] = 'Could not restore: Error writing to database file, please try again later.';
            }
        }
        unset($existing);
        unset($data);
    }
}
// END restore backup
// Import legacy backup?
if (pj_POST('legacy') && pj_token_check()) {
    // Check demo mode
    pj_demo('backups.php');
    // File uploaded?
    if (empty($_FILES['backupfile']['name'])) {
        $_SESSION['PJ_MESSAGES']['ERROR'] = 'No legacy backup file uploaded.';
    } else {
        $restore_existing = false;
        // Save current contents just in case
        $existing = file_get_contents($ccount_settings['db_file']);
        // Replace existing with new file
        if (!move_uploaded_file($_FILES['backupfile']['tmp_name'], dirname(dirname(__FILE__)) . '/' . substr($ccount_settings['db_file'], 3))) {
            $_SESSION['PJ_MESSAGES']['ERROR'] = 'Could not copy uploaded backup file over the existing one.';
        } else {
            $data = array();
            // Verify backup file
            $lines = file($ccount_settings['db_file']);
            foreach ($lines as $line) {
define('THIS_PAGE', 'SETTINGS');
// Require the settings file
require '../ccount_settings.php';
// Load functions
require '../inc/common.inc.php';
// Start session
pj_session_start();
// Are we logged in?
pj_isLoggedIn(true);
// The settings file is in parent folder
$ccount_settings['db_file_admin'] = '../' . $ccount_settings['db_file'];
$error_buffer = array();
// Save settings?
if (pj_POST('action') == 'save' && pj_token_check()) {
    // Check demo mode
    pj_demo('settings.php');
    // Admin password
    $ccount_settings['admin_pass'] = pj_input(pj_POST('admin_pass')) or $error_buffer['admin_pass'] = '******';
    // click.php URL
    $ccount_settings['click_url'] = pj_validateURL(pj_POST('click_url')) or $error_buffer['click_url'] = 'Enter a valid URL address of the click.php file on your server.';
    // Database file
    $ccount_settings['db_file'] = pj_input(pj_POST('db_file', 'ccount_database.php'));
    // Check database file
    if (preg_match('/[^0-9a-zA-Z_\\-\\.]/', $ccount_settings['db_file'])) {
        $error_buffer['db_file'] = 'Invalid file name. Use only these chars: a-z A-Z 0-9 _ - .';
    }
    // Unique hours
    $ccount_settings['unique_hours'] = intval(pj_POST('unique_hours', 24));
    if ($ccount_settings['unique_hours'] < 0) {
        $ccount_settings['unique_hours'] = 0;
    }
// Start session
pj_session_start();
// Are we logged in?
pj_isLoggedIn(true);
// The settings file is in parent folder
$ccount_settings['db_file'] = '../' . $ccount_settings['db_file'];
// Pre-set values
$url = '';
$title = '';
$id = '';
$count = 0;
$error_buffer = array();
// Add a new link?
if (pj_POST('action') == 'add' && pj_token_check()) {
    // Check demo mode
    pj_demo('new_link.php');
    // Link URL
    $url = pj_validateURL(pj_POST('url')) or $error_buffer['url'] = 'Enter a valid URL address.';
    // Link title
    $title = stripslashes(pj_input(pj_POST('title'))) or $title = '';
    // Link ID
    $id = pj_input(pj_POST('id')) or $id = '';
    // Check ID
    if (preg_match('/[^0-9a-zA-Z_\\-\\.]/', $id)) {
        $error_buffer['id'] = 'Invalid link ID. Leave it empty or use only these chars: a-z A-Z 0-9 _ - .';
    }
    // Count
    $count = intval(pj_POST('count', 0));
    // If no errors, check for duplicates/generate a new ID
    if (count($error_buffer) == 0) {
        // Get links database