open($zipFile) === true) { $zip->extractTo($folder); $zip->close(); return true; } else { return false; } } function in5_getIn5UploadsFolder($custom_path = '') { $uploadsDir = wp_upload_dir(); $in5Folder = trailingslashit($uploadsDir['basedir']); $in5Folder = str_replace('wp-content/uploads', '', $in5Folder); $in5Folder = trailingslashit($in5Folder); return $in5Folder; } function in5_getIndexFileUrl() { $uploadsDir = wp_upload_dir(); $in5Folder = trailingslashit($uploadsDir['baseurl']); // $in5Folder .= 'in5-archives/'; $in5Folder = str_replace('wp-content/uploads', '', $in5Folder); $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 || strpos($fileName, '.zhtml') !== 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); } if (strpos($fileName, '.zhtml') !== false) { return substr($fileName, 0, -6); } return; } function createFolderName($fileName, $customPath = '') { $folderName = in5_removeExtension($fileName); $folderName = strtolower($folderName); $folderName = str_replace(' ', '-', $folderName); $uploadsFolder = in5_getIn5UploadsFolder(); if ($customPath !== '') { // return $uploadsFolder . $customPath . '/' . $folderName; return $uploadsFolder . $customPath . '/' . $folderName; } else { // print_r('No custom path'); return $uploadsFolder . $folderName; } } function in5_ifFolderExists($fileName, $customPath = '') { $folderPath = createFolderName($fileName, $customPath); $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, $customPath = '') { // So if custom path is filled we should use it? // TODO: Add custom path $finalFolderName = in5_ifFolderExists($fileName, $customPath); 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; } $getJsLocation = $archiveLocation . '/assets/js/in5.config.js'; $getJs = file_get_contents($getJsLocation); preg_match('/var pageW = (.*?), pageH = (.*?);/i', $getJs, $matchesWH); preg_match('/var pageMode = (.*?);/i', $getJs, $matchesFlip); //preg_match('/var scaleMode = (.*?);/i', $getJs, $matchesScale); if (!isset($matchesWH) || empty($matchesWH)) { $attributes = array('width' => '100%', 'height' => '500'); return $attributes; } else { $width = (int) $matchesWH[1]; $height = (int) $matchesWH[2]; if(isset($matchesFlip) && $matchesFlip[1] == "'flip'") { $height *= .5; } $attributes = array('width' => $width, 'height' => $height); return $attributes; } } /*deprecated*/ function in5_getFileAttributesFromCSS($archiveLocation){ $getCssLocation = $archiveLocation . '/assets/css/pages.css'; $getCss = file_get_contents($getCssLocation); preg_match('/.page { width:(.*?)px; height:(.*?)px; /i', $getCss, $matches); return $matches; } function in5_extractMetaData($archiveLocation, $size) { $index = file_get_contents(in5_findIndexFile($archiveLocation)); preg_match('/