open( $zipFile ) === true ) { $zip->extractTo( $folder ); $zip->close(); return true; } else { return false; } } function in5_getIn5UploadsFolder() { $uploadsDir = wp_upload_dir(); $in5Folder = trailingslashit( $uploadsDir['basedir'] ); $in5Folder .= 'in5-archives'; $in5Folder = trailingslashit( $in5Folder ); return $in5Folder; } function in5_getIndexFileUrl() { $uploadsDir = wp_upload_dir(); $in5Folder = trailingslashit( $uploadsDir['baseurl'] ); $in5Folder .= 'in5-archives'; $in5Folder = trailingslashit( $in5Folder ); return $in5Folder; } function in5_convertSizeToBytes( $size ) { $cleanSize = substr( $size, 0, - 1 ); $unit = substr( $size, strlen( $cleanSize ), strlen( $size ) ); switch ( strtolower( $unit ) ) { case 'g': $cleanSize *= 1024; case 'm': $cleanSize *= 1024; case 'k': $cleanSize *= 1024; } return $cleanSize; } // Convert bytes to a human size with the appropriate unit function in5_convertBytesToSize( $bytes ) { $unitLetter = 'BKMGTP'; $factor = floor( ( strlen( $bytes ) - 1 ) / 3 ); switch ( $unitLetter[ $factor ] ) { case 'B': $unit = " bytes"; break; case 'K': $unit = " KB"; break; case 'M': $unit = " MB"; break; case 'G': $unit = " GB"; break; case 'T': $unit = " TB"; break; case 'P': $unit = " PB"; break; } $sizeWithUnit = sprintf( "%.0f", $bytes / pow( 1024, $factor ) ) . @$unit; return $sizeWithUnit; } function in5_validateFile( $fileName, $type ) { switch ( $type ) { case 'application/x-zip-compressed': /*windows*/ case 'application/octet-stream': case 'application/zip': case 'application/x-compressed': if ( strpos( $fileName, '.zip' ) !== false || strpos( $fileName, '.hpub' ) !== false ) { return true; } else { return false; } default: return false; } } function in5_validateSize( $size ) { $maxSize = ini_get( 'upload_max_filesize' ); $byteSized = in5_convertSizeToBytes( $maxSize ); if ( $byteSized < $size ) { return false; } else { return true; } } function in5_removeExtension( $fileName ) { if ( strpos( $fileName, '.zip' ) !== false ) { return substr( $fileName, 0, - 4 ); } if ( strpos( $fileName, '.hpub' ) !== false ) { return substr( $fileName, 0, - 5 ); } return; } function createFolderName( $fileName ) { $folderName = in5_removeExtension( $fileName ); $folderName = strtolower( $folderName ); $folderName = str_replace( ' ', '-', $folderName ); $uploadsFolder = in5_getIn5UploadsFolder(); return $uploadsFolder . $folderName; } function in5_ifFolderExists( $fileName ) { $folderPath = createFolderName( $fileName ); $i = 1; while ( file_exists( $folderPath ) ) { $folderPath .= "-" . $i; $i ++; } return $folderPath; } // Delete the folder recursively function in5_rrmdir( $dir ) { if ( is_dir( $dir ) ) { $objects = scandir( $dir ); foreach ( $objects as $object ) { if ( $object != "." && $object != ".." ) { if ( filetype( $dir . "/" . $object ) == "dir" ) { in5_rrmdir( $dir . "/" . $object ); } else { unlink( $dir . "/" . $object ); } } } reset( $objects ); rmdir( $dir ); } } function in5_doesIndexExist( $finalFolderName ) { $finalFolderName = trailingslashit( $finalFolderName ); if ( file_exists( $finalFolderName . 'index.html' ) ) { return true; } if ( $handle = opendir( $finalFolderName ) ) { $blacklist = array( '__MACOSX', '.', '..' ); while ( false !== ( $file = readdir( $handle ) ) ) { if ( in_array( $file, $blacklist ) ) { continue; } if ( is_file( $file ) ) { continue; } if ( file_exists( trailingslashit( $finalFolderName . $file ) . 'index.html' ) ) { return array( 'finalFolderName' => trailingslashit( $finalFolderName . $file ) ); break; } else { continue; } } closedir( $handle ); } return false; } function in5_doArchiveUpload( $fileName, $tempFileLocation ) { $finalFolderName = in5_ifFolderExists( $fileName ); mkdir( $finalFolderName ); in5_extractArchive( $tempFileLocation, $finalFolderName ); $doesIndexExist = in5_doesIndexExist( $finalFolderName ); if ( ! is_array( $doesIndexExist ) && $doesIndexExist !== false ) { return $finalFolderName; } elseif ( is_array( $doesIndexExist ) ) { if ( isset( $doesIndexExist['finalFolderName'] ) ) { return $doesIndexExist['finalFolderName']; } } else { in5_rrmdir( $finalFolderName ); return false; } return ''; } function in5_getNextEntryID() { $files = in5_get_metadata(); $decode = json_decode( $files ); return count( $decode->files ) + 1; } function in5_findIndexFile( $folder ) { if ( file_exists( trailingslashit( $folder ) . 'index.html' ) ) { return trailingslashit( $folder ) . 'index.html'; } elseif ( file_exists( trailingslashit( $folder ) . 'pages.html' ) ) { return trailingslashit( $folder ) . 'pages.html'; } return ''; } function in5_getFileAttributes( $archiveLocation ) { if ( ! file_exists( $archiveLocation ) ) { return false; } $getCssLocation = $archiveLocation . '/assets/css/pages.css'; $getCss = file_get_contents( $getCssLocation ); preg_match( '/.page { width:(.*?)px; height:(.*?)px; /i', $getCss, $matches ); if ( ! isset( $matches ) ) { $attributes = array( 'width' => 0, 'height' => 0 ); return $attributes; } else { $width = (int) $matches[1]; $height = (int) $matches[2]; $attributes = array( 'width' => $width, 'height' => $height ); return $attributes; } } function in5_extractMetaData( $archiveLocation, $size ) { $index = file_get_contents( in5_findIndexFile( $archiveLocation ) ); preg_match( '/