<?php
/**
 * Internal class for CMSMSExt
 * Install/Update/Uninstall assistant
 * @package cmsmsext
 *
 */

namespace CMSMSExt\internal;


class IUA
{
  use \CMSMSExt\core\TSingleton;
  
  /**
   * Clears temporary files based on the session duration.
   *
   * @param array &$args The arguments passed by reference.
   * @return $this Returns the current object for method chaining.
   */
  private function _clear_temporary_files(&$args)
  {
    $mod              = \cms_utils::get_module('CMSMSExt');
    $config           = $mod->GetExtendedConfig();
    $tmp_dir          = \str_replace('cache', '', $config['tmp_cache_location']) . 'cmsmsext/';
    $session_duration = \ini_get('session.gc_maxlifetime');
    $ret              = FALSE;
    
    $files = \glob($tmp_dir . '/*.*');
    
    $currentTimestamp = \time();
    
    foreach ($files as $file)
    {
      $lastAccessTime = \filemtime($file);
      
      if (($currentTimestamp - $lastAccessTime) > $session_duration)
      {
        # while removing the file, set the return value to true if at least one is removed
        $ret = \unlink($file) || $ret;
      }
    }
    
    if(\is_array($args) && isset($args['return'])) { $args['return'] = $ret; }
    
    return $this;
  }
  
  /**
   * Function to perform any upgrade procedures. This is mostly used to for
   * updating databsae tables, but can do other duties as well. It should
   * return a string message if there is a failure. Returning nothing (FALSE)
   * will allow the upgrade procedure to proceed. Upgrades should have a path
   * so that they can be upgraded from more than one version back.  While not
   * a requirement, it makes life easy for your users.
   *
   * The default behavior of this method is to call a function called method.upgrade.php
   * in your module directory, if it exists.
   *
   * @param string $oldversion The version we are upgrading from
   * @param string $newversion The version we are upgrading to
   * @return bool
   */
  public function Upgrade($oldversion, $newversion)
  {
    try
    {
      # specific for old versions and new versions simultaneous comparisons
      //    if($oldversion < '1.0.0' && $newversion >= '1.0.0')
      //    {
      //      # do something
      //    }
      
      # specific for old versions comparison
      //    if($oldversion < '1.0.0')
      //    {
      //      # do something
      //    }
      
      # specific for new versions comparison
      //    if($newversion >= '1.0.0')
      //    {
      //      # do something
      //    }
      
      # specific for old versions upgrade path
      // switch($oldversion)
      // {
      //   case '1.0.0':
      //     # do something
      //   case '1.0.1':
      //     # do something
      // }
      
      # specific for new versions upgrade path
      // switch($newversion)
      // {
      //   case '1.0.0':
      //     # do something
      //   case '1.0.1':
      //     # do something
      // }
      
      
      # mandatory for all versions
      
      $this->_clear_temporary_files($args);
      return FALSE;
    
    }
    catch(\Exception $e)
    {
      return $e->getMessage();
    }
  
  }
}

?>