<commit_before><?php
/**
  * @copyright 2010 Modo Labs Inc.
  * @package Core
  *
   */

/**
  * Provides an abstraction for an on disk cache
  * @package Core
  *
  */
class DiskCache {

    private $path;
    private $timeout = PHP_INT_MAX;
    private $error;
    private $prefix = "";
    private $suffix = "";
    private $serialize = TRUE;

    public function __construct($path, $timeout=NULL, $mkdir=FALSE) {
        if (empty($path)) {
            throw new Exception("Invalid path");
        }
    
        $this->path = $path;
    
        if ($mkdir) {
            if (!file_exists($path)) {
                if (!mkdir($path, 0755, true)) {
                    throw new Exception("Could not create $path");
                }
            }
        }
        
        if (!is_writable($path)) {
            throw new Exception("Path $path is not writable");
        }
        
        if ($timeout !== NULL) {
            $this->timeout = $timeout;
        }
    }

  public function preserveFormat() {
    $this->serialize = FALSE;
  }

  public function setTimeout($timeout) {
    $this->timeout = $timeout;
  }

  public function setPrefix($prefix) {
    $this->prefix = $prefix;
  }

  public function getPrefix() {
    return $this->prefix;
  }

  public function getSuffix() {
    return $this->suffix;
  }

  public function setSuffix($suffix) {
    $this->suffix = $suffix;
  }

  public function getError() {
    return $this->error;
  }

  public function getFullPath($filename=NULL) {
    if ($filename === NULL) {
      return $this->path;
    } else {
      // this replaces %20 with + signs
      $filename = urlencode(urldecode($filename));

      return $this->path . '/' 
           . $this->prefix 
           . $filename 
           . $this->suffix;
    }
  }

  public function writeImage($image, $filename) {
    $success = FALSE;
    $path = $this->getFullPath($filename);
    $suffix = $this->suffix ? $this->suffix : substr($filename, -4);
    switch ($suffix) {
     case '.png':
       $success = imagepng($image, $path);
       break;
     case '.jpg':
       $success = imagejpeg($image, $path);
       break;
     case '.gif':
       $success = imagegif($image, $path);
       break;
    }
    return $success;
  }

  public function readImage($filename) {
    $path = $this->getFullPath($filename);
    $suffix = $this->suffix ? $this->suffix : substr($filename, -4);
    switch ($suffix) {
     case '.png':
       return imagecreatefrompng($path);
       break;
     case '.jpg':
         return imagecreatefromjpeg($path);
       break;
     case '.gif':
       return imagecreatefromgif($path);
       break;
    }
  }

  public function write($object, $filename=NULL, $date=null) {
    if (!$object) {
      $this->error = "tried to cache a non-object";
    }

    $path = $this->getFullPath($filename);
    $fh = fopen($path, 'w');
    if ($fh !== FALSE) {
      if ($this->serialize) {
        fwrite($fh, serialize($object));
      } else {
        fwrite($fh, $object);
      }
      fclose($fh);
      // set the modification time if present
      if ($date) {
        touch($this->getFullPath($filename), $date);
      }
      return TRUE;

    } else {
      $this->error = "could not open $path for writing";
    }

    // stop doing this here after users handle error on their own
    if ($this->error)
      error_log($this->error);

    return FALSE;
  }

  public function getImageSize($filename=NULL) {
    $path = $this->getFullPath($filename);<commit_msg>set permissions of cache files to be more restrictive<commit_after><?php
/**
  * @copyright 2010 Modo Labs Inc.
  * @package Core
  *
   */

/**
  * Provides an abstraction for an on disk cache
  * @package Core
  *
  */
class DiskCache {

    private $path;
    private $timeout = PHP_INT_MAX;
    private $error;
    private $prefix = "";
    private $suffix = "";
    private $serialize = TRUE;

    public function __construct($path, $timeout=NULL, $mkdir=FALSE) {
        if (empty($path)) {
            throw new Exception("Invalid path");
        }
    
        $this->path = $path;
    
        if ($mkdir) {
            if (!file_exists($path)) {
                if (!mkdir($path, 0700, true)) {
                    throw new Exception("Could not create $path");
                }
            }
        }
        
        if (!is_writable($path)) {
            throw new Exception("Path $path is not writable");
        }
        
        if ($timeout !== NULL) {
            $this->timeout = $timeout;
        }
    }

  public function preserveFormat() {
    $this->serialize = FALSE;
  }

  public function setTimeout($timeout) {
    $this->timeout = $timeout;
  }

  public function setPrefix($prefix) {
    $this->prefix = $prefix;
  }

  public function getPrefix() {
    return $this->prefix;
  }

  public function getSuffix() {
    return $this->suffix;
  }

  public function setSuffix($suffix) {
    $this->suffix = $suffix;
  }

  public function getError() {
    return $this->error;
  }

  public function getFullPath($filename=NULL) {
    if ($filename === NULL) {
      return $this->path;
    } else {
      // this replaces %20 with + signs
      $filename = urlencode(urldecode($filename));

      return $this->path . '/' 
           . $this->prefix 
           . $filename 
           . $this->suffix;
    }
  }

  public function writeImage($image, $filename) {
    $success = FALSE;
    $path = $this->getFullPath($filename);
    $suffix = $this->suffix ? $this->suffix : substr($filename, -4);
    switch ($suffix) {
     case '.png':
       $success = imagepng($image, $path);
       break;
     case '.jpg':
       $success = imagejpeg($image, $path);
       break;
     case '.gif':
       $success = imagegif($image, $path);
       break;
    }
    return $success;
  }

  public function readImage($filename) {
    $path = $this->getFullPath($filename);
    $suffix = $this->suffix ? $this->suffix : substr($filename, -4);
    switch ($suffix) {
     case '.png':
       return imagecreatefrompng($path);
       break;
     case '.jpg':
         return imagecreatefromjpeg($path);
       break;
     case '.gif':
       return imagecreatefromgif($path);
       break;
    }
  }

  public function write($object, $filename=NULL, $date=null) {
    if (!$object) {
      $this->error = "tried to cache a non-object";
    }

    $path = $this->getFullPath($filename);
    $umask = umask(0077);
    $fh = fopen($path, 'w');
    if ($fh !== FALSE) {
      if ($this->serialize) {
        fwrite($fh, serialize($object));
      } else {
        fwrite($fh, $object);
      }
      fclose($fh);
      // set the modification time if present
      if ($date) {
        touch($this->getFullPath($filename), $date);
      }
      umask($umask);
      return TRUE;

    } else {
      $this->error = "could not open $path for writing";
    }

    // stop doing this here after users handle error on their own
    if ($this->error)
      error_log($this->error);

    umask($umask);
    return FALSE;
  }

  public function getImageSize($filename=NULL) {
    $path = $this->getFullPath($filename);
{'fraction_non_alphanumeric': 0.1492014569907537, 'fraction_numerical': 0.003922667413841412, 'mean_word_length': 2.0904761904761906, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4847001', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2126, 'n_words': 537}