<?php
if (!isset($config)) {
    require_once('config.php');
}
define("DEBUG_SYNC_HONAPKEPE", "No");
$targetDate         = 30;
$articleContent     = "<p>{gallery}img/honapkepe/" . date('Ym') . "{/gallery}</p>"; // 571-es cikk
$targetDir          = $folder . '/public/img/honapkepe/' . date('Ym');
$sourceDir          = $folder . '/public/userupload/honapkepe/' . date('Ym');
$articleContent2    = "<p>{gallery}img/honapkepe_live/" . date('Ym') . "{/gallery}</p>"; // 746-os és 780-as cikk
$targetDir2         = $folder . '/public/img/honapkepe_live/' . date('Ym');
$sourceDir2         = $folder . '/public/userupload/honapkepe_live/' . date('Ym');

/*if(DEBUG_SYNC_HONAPKEPE == "Yes") {
    $date               = "202003";
    $articleContent     = "<p>{gallery}img/honapkepe/".$date."{/gallery}</p>"; // 571-es cikk
    $targetDir          = $folder.'/public/img/honapkepe/'.$date;
    $sourceDir          = $folder.'/public/userupload/honapkepe/'.$date;
    $articleContent2    = "<p>{gallery}img/honapkepe_live/".$date."{/gallery}</p>"; // 746-os és 780-as cikk
    $targetDir2         = $folder.'/public/img/honapkepe_live/'.$date;
    $sourceDir2         = $folder.'/public/userupload/honapkepe_live/'.$date;
}*/

if (date('j') == $targetDate) {
    $message = 'Sync Honapkepe';
    mail("mate.nagy@cadline.hu", "Sync Honapkepe", $message);
}

if ((int)date('j') == $targetDate || DEBUG_SYNC_HONAPKEPE == "Yes") {
    // honapkepe
    if (!is_dir($targetDir))
        mkdir($targetDir);

    $dirs = array();
    foreach (scandir($sourceDir) as $dir)
        if (!in_array($dir, array(".", ".."))) $dirs[] = $dir;

    if (count($dirs)) {
        foreach ($dirs as $dir) {
            $files = array();

            if (is_dir($sourceDir . '/' . $dir)) {
                foreach (scandir($sourceDir . '/' . $dir) as $file)
                    if (!in_array($file, array(".", ".."))) $files[] = $file;
            }
            if (count($files)) {
                foreach ($files as $file) {
                    if (is_file($sourceDir . "/" . $dir . "/" . $file))
                        var_dump(copy($sourceDir . '/' . $dir . '/' . $file, $targetDir . "/" . $dir . "-" . $file));
                }
            }
        }
    }

    // honapkepe_live
    if (folder_exist($sourceDir2)) {
        if (!is_dir($targetDir2))
            mkdir($targetDir2);

        $dirs2 = array();
        foreach (scandir($sourceDir2) as $dir)
            if (!in_array($dir, array(".", ".."))) $dirs2[] = $dir;

        if (count($dirs2)) {
            foreach ($dirs2 as $dir) {
                $files2 = array();

                if (is_dir($sourceDir2 . '/' . $dir)) {
                    foreach (scandir($sourceDir2 . '/' . $dir) as $file)
                        if (!in_array($file, array(".", ".."))) $files2[] = $file;
                }

                if (count($files2)) {
                    foreach ($files2 as $file) {
                        if (is_file($sourceDir2 . "/" . $dir . "/" . $file))
                            var_dump(copy($sourceDir2 . '/' . $dir . '/' . $file, $targetDir2 . "/" . $dir . "-" . $file));
                    }
                }
            }
        }
        $msh2->query("UPDATE `" . JMLADATBAZIS . "`.`jml_content` SET `introtext`='" . $articleContent2 . "' WHERE `id`=746;");
        $msh2->query("UPDATE `" . JMLADATBAZIS . "`.`eng_content` SET `introtext`='" . $articleContent2 . "' WHERE `id`=780;");
    }

    $msh2->query("UPDATE `" . JMLADATBAZIS . "`.`jml_content` SET `introtext`='" . $articleContent . "' WHERE `id`=571;");
    $msh2->query("UPDATE `" . JMLADATBAZIS . "`.`eng_content` SET `introtext`='" . $articleContent . "' WHERE `id`=571;");
}

function folder_exist($folder)
{
    $path = realpath($folder);

    return ($path !== false and is_dir($path)) ? $path : false;
}
