<commit_before><?php

/**
 * The cronjob keeps your software backuped, fast and clean. Set up the execution
 * intervals in the "app/config/Candy.inc.php" and lean back.
 *
 * @link http://github.com/marcoraddatz/candyCMS
 * @author <NAME> <http://marcoraddatz.com>
 * @license MIT
 * @since 1.5
 *
 */

namespace CandyCMS\Plugins;

use CandyCMS\Core\Controllers\Logs;
use CandyCMS\Core\Controllers\Mails;
use CandyCMS\Core\Helpers\AdvancedException;
use CandyCMS\Core\Helpers\Helper;
use CandyCMS\Core\Helpers\I18n;
use CandyCMS\Core\Models\Main;
use PDO;

final class Cronjob {

  /**
   * holds the database connection
   *
   * @access private
   */
  private $_oDB;

  /**
   * Create a new Cronjob Object
   *
   * @access public
   *
   */
  public function __construct() {
    // write to the file, so there will be no duplicate execution
    $this->_writeTimestamp();

    $this->_oDB = Main::connectToDatabase();
  }

  /**
   * Finish up the execution of the Cronjob
   *
   * @access public
   *
   */
  public function __destruct() {
    // update the timestamp
    $this->_writeTimestamp();
  }

  /**
   * write a timestamp to the backup file that indicates the last execution time,
   * when no timestamp is given, write the current time to the file
   *
   * @param int $iTimestamp the timestamp to write
   */
  private function _writeTimestamp($iTimestamp = 0) {
    // check for the file and read its timestamp
    $sBackupFile = PATH_STANDARD . '/app/backup/lastrun.timestamp';

    $oFile = @fopen($sBackupFile, 'w');
    if ($oFile) {
      @fwrite($oFile, $iTimestamp ? $iTimestamp : time());
      @fclose($oFile);
    }
  }


  /**
   * Cleanup our temp folders.
   *
   * @final
   * @access public
   * @param array $aFolders temp folders to clean
   *
   */
  public final function cleanup($aFolders) {
    foreach ($aFolders as $sFolder) {
      $sTempPath = Helper::removeSlash(PATH_UPLOAD . '/temp/' . $sFolder);
      $oDir = opendir($sTempPath);

      while ($sFile = readdir($oDir)) {
        if (substr($sFile, 0, 1) == '.' || filemtime($sTempPath . '/' . $sFile) > strtotime("-10 days"))
          continue;

        unlink($sTempPath . '/' . $sFile);
      }
    }
  }


  /**
   * Optimize tables and delete old sessions.
   *
   * @final
   * @access public
   *
   */
  public final function optimize() {
    try {
      $this->_oDB->query("OPTIMIZE TABLE
                          " . SQL_PREFIX . "blogs,
                          " . SQL_PREFIX . "comments,
                          " . SQL_PREFIX . "calendars,
                          " . SQL_PREFIX . "contents,
                          " . SQL_PREFIX . "downloads,
                          " . SQL_PREFIX . "gallery_albums,
                          " . SQL_PREFIX . "gallery_files,
                          " . SQL_PREFIX . "mails,
                          " . SQL_PREFIX . "migrations,
                          " . SQL_PREFIX . "logs,
                          " . SQL_PREFIX . "sessions,
                          " . SQL_PREFIX . "users");
    }
    catch (AdvancedException $e) {
      $this->_oDB->rollBack();
      AdvancedException::reportBoth('0109 - ' . $e->getMessage());
      exit('SQL error.');
    }

    try {
      $oQuery = $this->_oDB->prepare("DELETE FROM
                                              " . SQL_PREFIX . "sessions
                                            WHERE
                                              date < :date");

      # Half a year
      $iDate = time() - 15552000;
      $oQuery->bindParam('date', $iDate, PDO::PARAM_INT);
      return $oQuery->execute();
    }
    catch (AdvancedException $e) {
      $this->_oDB->rollBack();
      AdvancedException::reportBoth('0109 - ' . $e->getMessage());
      exit('SQL error.');
    }
  }

  /**
   * Create a Backup of one Table
   *
   * @final
   * @access private
   * @param string $sTable the Table Name
   * @param string $sFileText the string to write the backup to
   * @return integer number of Columns
   * @throws AdvancedException
   *
   */
  private final function _backupTableInfo($sTable, &$sFileText) {<commit_msg>fixing sessions cleanup statement of cronjob<commit_after><?php

/**
 * The cronjob keeps your software backuped, fast and clean. Set up the execution
 * intervals in the "app/config/Candy.inc.php" and lean back.
 *
 * @link http://github.com/marcoraddatz/candyCMS
 * @author <NAME> <http://marcoraddatz.com>
 * @license MIT
 * @since 1.5
 *
 */

namespace CandyCMS\Plugins;

use CandyCMS\Core\Controllers\Logs;
use CandyCMS\Core\Controllers\Mails;
use CandyCMS\Core\Helpers\AdvancedException;
use CandyCMS\Core\Helpers\Helper;
use CandyCMS\Core\Helpers\I18n;
use CandyCMS\Core\Models\Main;
use PDO;

final class Cronjob {

  /**
   * holds the database connection
   *
   * @access private
   */
  private $_oDB;

  /**
   * Create a new Cronjob Object
   *
   * @access public
   *
   */
  public function __construct() {
    // write to the file, so there will be no duplicate execution
    $this->_writeTimestamp();

    $this->_oDB = Main::connectToDatabase();
  }

  /**
   * Finish up the execution of the Cronjob
   *
   * @access public
   *
   */
  public function __destruct() {
    // update the timestamp
    $this->_writeTimestamp();
  }

  /**
   * write a timestamp to the backup file that indicates the last execution time,
   * when no timestamp is given, write the current time to the file
   *
   * @param int $iTimestamp the timestamp to write
   */
  private function _writeTimestamp($iTimestamp = 0) {
    // check for the file and read its timestamp
    $sBackupFile = PATH_STANDARD . '/app/backup/lastrun.timestamp';

    $oFile = @fopen($sBackupFile, 'w');
    if ($oFile) {
      @fwrite($oFile, $iTimestamp ? $iTimestamp : time());
      @fclose($oFile);
    }
  }


  /**
   * Cleanup our temp folders.
   *
   * @final
   * @access public
   * @param array $aFolders temp folders to clean
   *
   */
  public final function cleanup($aFolders) {
    foreach ($aFolders as $sFolder) {
      $sTempPath = Helper::removeSlash(PATH_UPLOAD . '/temp/' . $sFolder);
      $oDir = opendir($sTempPath);

      while ($sFile = readdir($oDir)) {
        if (substr($sFile, 0, 1) == '.' || filemtime($sTempPath . '/' . $sFile) > strtotime("-10 days"))
          continue;

        unlink($sTempPath . '/' . $sFile);
      }
    }
  }


  /**
   * Optimize tables and delete old sessions.
   *
   * @final
   * @access public
   *
   */
  public final function optimize() {
    try {
      $this->_oDB->query("OPTIMIZE TABLE
                          " . SQL_PREFIX . "blogs,
                          " . SQL_PREFIX . "comments,
                          " . SQL_PREFIX . "calendars,
                          " . SQL_PREFIX . "contents,
                          " . SQL_PREFIX . "downloads,
                          " . SQL_PREFIX . "gallery_albums,
                          " . SQL_PREFIX . "gallery_files,
                          " . SQL_PREFIX . "mails,
                          " . SQL_PREFIX . "migrations,
                          " . SQL_PREFIX . "logs,
                          " . SQL_PREFIX . "sessions,
                          " . SQL_PREFIX . "users");
    }
    catch (AdvancedException $e) {
      $this->_oDB->rollBack();
      AdvancedException::reportBoth('0109 - ' . $e->getMessage());
      exit('SQL error.');
    }

    try {
      $oQuery = $this->_oDB->prepare("DELETE FROM
                                              " . SQL_PREFIX . "sessions
                                            WHERE
                                              date < DATE_SUB(NOW(), INTERVAL 6 MONTH)");

      return $oQuery->execute();
    }
    catch (AdvancedException $e) {
      $this->_oDB->rollBack();
      AdvancedException::reportBoth('0109 - ' . $e->getMessage());
      exit('SQL error.');
    }
  }

  /**
   * Create a Backup of one Table
   *
   * @final
   * @access private
   * @param string $sTable the Table Name
   * @param string $sFileText the string to write the backup to
   * @return integer number of Columns
   * @throws AdvancedException
   *
   */
  private final function _backupTableInfo($sTable, &$sFileText) {
{'fraction_non_alphanumeric': 0.1264465830186381, 'fraction_numerical': 0.005238153246436838, 'mean_word_length': 1.8636205092431113, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3841850', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2476, 'n_words': 617}