public function generate()
 {
     $amount = $this->get_amount();
     $splitter = $this->get_splitter();
     $pattern = $this->get_pattern();
     $pieces = explode($splitter, $pattern);
     $result = array();
     for ($i = 0; $i < $amount; $i++) {
         $serial = array();
         foreach ($pieces as $piece) {
             $len = strlen($piece);
             $item = hocwp_random_string($len, $this->get_characters());
             $serial[] = $item;
         }
         $serial = implode($splitter, $serial);
         $result[] = strtoupper($serial);
     }
     if (2 > $amount) {
         $result = current($result);
     }
     return $result;
 }
function hocwp_upload($args = array())
{
    $name = isset($args['name']) ? $args['name'] : '';
    $path = isset($args['path']) ? $args['path'] : '';
    $size = isset($args['size']) ? $args['size'] : 0;
    $max_size = isset($args['max_size']) ? $args['max_size'] : -1;
    $is_image = isset($args['is_image']) ? $args['is_image'] : false;
    $extensions = isset($args['extensions']) ? $args['extensions'] : array();
    $tmp_name = isset($args['tmp_name']) ? $args['tmp_name'] : '';
    $duplicate_exists = isset($args['duplicate_exists']) ? $args['duplicate_exists'] : true;
    $result = array('success' => false);
    $result['image_base64'] = hocwp_image_base64($tmp_name);
    $name = strtolower($name);
    $basename = basename($name);
    $basename = hocwp_sanitize_file_name($basename);
    $file_path = $path . '/' . $basename;
    $file_type = pathinfo($file_path, PATHINFO_EXTENSION);
    if ($is_image && !empty($tmp_name)) {
        $check = getimagesize($tmp_name);
        if ($check === false) {
            $result['message'][] = sprintf(__('File %s is not a picture.', 'hocwp-theme'), $name);
            return $result;
        }
    }
    if (file_exists($file_path)) {
        if ($duplicate_exists) {
            $path_info = pathinfo($file_path);
            $name = $path_info['filename'] . '-' . hocwp_random_string() . '.' . $file_type;
            $name = strtolower($name);
            $basename = basename($name);
            $basename = hocwp_sanitize_file_name($basename);
            $file_path = $path . '/' . $basename;
        } else {
            $result['message'][] = sprintf(__('File %s already exists', 'hocwp-theme'), $name);
            return $result;
        }
    }
    if ($max_size > 0 && $size > $max_size) {
        $result['message'][] = sprintf(__('File size should not exceed %s', 'hocwp-theme'), $max_size);
        return $result;
    }
    if (count($extensions) > 0 && !in_array($file_type, $extensions)) {
        $result['message'][] = sprintf(__('You are not allowed to upload files with extension %s', 'hocwp-theme'), $file_type);
        return $result;
    }
    $file_path = strtolower($file_path);
    if (move_uploaded_file($tmp_name, $file_path)) {
        $result['success'] = true;
    } else {
        $result['message'][] = __('There was an error occurred, file is not uploaded.', 'hocwp-theme');
    }
    $result['name'] = $name;
    $result['path'] = $file_path;
    return $result;
}
 public function generate_image()
 {
     $dir = wp_normalize_path($this->get_save_path());
     if (!wp_mkdir_p($dir)) {
         return false;
     }
     $this->cleanup_expired();
     $code = hocwp_random_string($this->get_length(), $this->get_chars());
     $filename = '';
     $dir = trailingslashit($dir);
     if ($this->get_uppercase()) {
         $code = strtoupper($code);
     }
     $size = $this->get_size();
     if ($im = @imagecreatetruecolor($size[0], $size[1])) {
         $fonts = $this->get_fonts();
         if (!hocwp_array_has_value($fonts)) {
             return false;
         }
         $filename = md5(hocwp_random_string());
         $background = $this->get_background();
         $foreground = $this->get_foreground();
         $bg = @imagecolorallocate($im, $background[0], $background[1], $background[2]);
         $fg = @imagecolorallocate($im, $foreground[0], $foreground[1], $foreground[2]);
         @imagefill($im, 0, 0, $bg);
         if ($this->get_pixel()) {
             $pixel_colors = $this->get_pixel_color();
             $pixel_color = @imagecolorallocate($im, $pixel_colors[0], $pixel_colors[1], $pixel_colors[2]);
             $pixels = rand(300, 600);
             for ($i = 0; $i < $pixels; $i++) {
                 @imagesetpixel($im, rand(1, 100), rand(1, 100), $pixel_color);
             }
         }
         $base = $this->get_base();
         $x = $base[0] + mt_rand(-2, 2);
         for ($i = 0; $i < strlen($code); $i++) {
             $font = $fonts[array_rand($fonts)];
             $font = wp_normalize_path($font);
             $y = $base[1] + mt_rand(-2, 2);
             $angle = mt_rand(-20, 20);
             @imagettftext($im, $this->get_font_size(), $angle, $x, $y, $fg, $font, $code[$i]);
             $x += $this->get_font_char_width();
         }
         if ($this->get_line()) {
             $lines = rand(5, 10);
             $line_colors = $this->get_line_color();
             $line_color = @imagecolorallocate($im, $line_colors[0], $line_colors[1], $line_colors[2]);
             for ($i = 0; $i < $lines; $i++) {
                 @imageline($im, rand(1, 100), rand(1, 100), rand(1, 100), rand(1, 100), $line_color);
             }
         }
         switch ($this->get_image_type()) {
             case 'jpeg':
                 $filename = sanitize_file_name($filename . '.jpeg');
                 $file = wp_normalize_path($dir . $filename);
                 @imagejpeg($im, $file);
                 break;
             case 'gif':
                 $filename = sanitize_file_name($filename . '.gif');
                 $file = wp_normalize_path($dir . $filename);
                 @imagegif($im, $file);
                 break;
             case 'png':
             default:
                 $filename = sanitize_file_name($filename . '.png');
                 $file = wp_normalize_path($dir . $filename);
                 @imagepng($im, $file);
         }
         @imagedestroy($im);
         @chmod($file, $this->get_file_mode());
     }
     if (!empty($filename)) {
         if ($this->get_lowercase()) {
             $code = strtolower($code);
         }
         $data = array('hashed' => wp_hash_password($code), 'timestamp' => strtotime(hocwp_get_current_date('Y-m-d H:i:s')));
         $this->set_session_data($data);
     }
     $this->set_code($code);
     return trailingslashit($this->get_save_url()) . $filename;
 }
function hocwp_random_string_number($length = 6)
{
    return hocwp_random_string($length, '0123456789');
}