/** * Common function to replace wordpress file uplaod url with * Windows Azure Storage URLs * * @param string $url original upload URL * * @return string Updated upload URL */ function getUpdatedUploadUrl($url) { $wp_upload_dir = wp_upload_dir(); $upload_dir_url = $wp_upload_dir['baseurl']; $storage_url_prefix = WindowsAzureStorageUtil::getStorageUrlPrefix(); return str_replace($upload_dir_url, $storage_url_prefix, $url); }
/** * Render Browse Tab in the Windows Azure Storage popup dialog * * @return void */ function windows_azure_storage_dialog_browse_tab() { // remove all registerd filters for the tabs unset($GLOBALS['wp_filter']['media_upload_tabs']); // register our filter for the tabs add_filter("media_upload_tabs", "windows_azure_storage_dialog_add_tab"); media_upload_header(); $azure_storage_account_name = WindowsAzureStorageUtil::getAccountName(); $azure_storage_account_primary_access_key = WindowsAzureStorageUtil::getAccountKey(); $default_azure_storage_account_container_name = WindowsAzureStorageUtil::getDefaultContainer(); if (empty($azure_storage_account_name) || empty($azure_storage_account_primary_access_key)) { echo '<h3 style="margin: 10px;">' . 'Azure Storage Account not yet configured</h3>'; echo '<p style="margin: 10px;">' . 'Please configure the account in Windows Azure Settings Tab.</p>'; } else { $storageClient = WindowsAzureStorageUtil::getStorageClient(); // Set selected container. If none, then use default container $selected_container_name = $default_azure_storage_account_container_name; if (!empty($_POST['selected_container'])) { $selected_container_name = $_POST['selected_container']; } else { if (!empty($_GET['selected_container'])) { $selected_container_name = $_GET['selected_container']; } } // Check if blob has to be deleted if (!empty($_GET['deleteBlob'])) { deleteBlob($selected_container_name, $_GET['deleteBlob']); } if (!empty($_POST['DeleteAllBlobs']) && $_POST['DeleteAllBlobs'] == 'true') { // Get list of blobs in specified container $listBlobResult = $storageClient->listBlobs($selected_container_name); // Delete each blob in specified container foreach ($listBlobResult->getBlobs() as $blob) { deleteBlob($selected_container_name, $blob->getName()); } echo '<p style="margin: 10px; color: red;">' . 'Deleted all files in Windows Azure Storage Container "' . $selected_container_name . '"</p><br/>'; } // Handle file search if (!empty($_POST['action']) && $_POST["action"] == "Search") { try { $fileTagFilter = $_POST["searchFileTag"]; $fileNameFilter = $_POST["searchFileName"]; $fileTypeFilter = $_POST["searchFileType"]; $searchContainer = $_POST["searchContainer"]; if (empty($fileTagFilter) && empty($fileNameFilter) && empty($fileTypeFilter)) { echo '<p style="margin: 10px;">' . 'Search criteria not specified.</p><br/>'; } else { $criteria = array(); if (!empty($fileNameFilter)) { $criteria[] = "file name like " . $fileNameFilter; } if (!empty($fileTypeFilter)) { $criteria[] = "file type like " . $fileTypeFilter; } if (!empty($fileTagFilter)) { $criteria[] = "tag like '" . $fileTagFilter . "'"; } $searchResult = array(); if ($searchContainer == "ALL_CONTAINERS") { $criteria[] = "in 'all containers'"; $listContainerResult = $storageClient->listContainers(); foreach ($listContainerResult->getContainers() as $container) { // Get list of blobs in specified container $listBlobResult = $storageClient->listBlobs($container->getName()); foreach ($listBlobResult->getBlobs() as $blob) { if (!empty($fileNameFilter)) { if (stripos($blob->getName(), $fileNameFilter) === false) { continue; } } // TODO This is a temporary fix (replacing space with %20) will be removed once fixed in the core $blobName = str_replace(" ", "%20", $blob->getName()); $getBlobMetadataResult = $storageClient->getBlobMetadata($container->getName(), $blobName); $metadata = $getBlobMetadataResult->getMetadata(); if (!empty($fileTypeFilter)) { if (stripos($metadata["mimetype"], $fileTypeFilter) === false) { continue; } } if (!empty($fileTagFilter)) { if (stripos($metadata["tag"], $fileTagFilter) === false) { continue; } } $searchResult[] = WindowsAzureStorageUtil::getStorageUrlPrefix(false) . "/" . $container->getName() . "/" . $blob->getName(); } } } else { $criteria[] = "in container '" . $searchContainer . "'"; // Get list of blobs in specified container $listBlobResult = $storageClient->listBlobs($searchContainer); foreach ($listBlobResult->getBlobs() as $blob) { if (!empty($fileNameFilter)) { if (stripos($blob->getName(), $fileNameFilter) === false) { continue; } } // TODO This is a temporary fix (replacing space with %20) will be removed once fixed in the core $blobName = str_replace(" ", "%20", $blob->getName()); $getBlobMetadataResult = $storageClient->getBlobMetadata($searchContainer, $blobName); $metadata = $getBlobMetadataResult->getMetadata(); if (!empty($fileTypeFilter)) { if (stripos($metadata["mimetype"], $fileTypeFilter) === false) { continue; } } if (!empty($fileTagFilter)) { if (stripos($metadata["tag"], $fileTagFilter) === false) { continue; } } $searchResult[] = WindowsAzureStorageUtil::getStorageUrlPrefix(false) . "/{$searchContainer}/" . $blob->getName(); } } echo '<h3 style="margin: 10px;">Search Result</h3>'; if (empty($searchResult)) { echo '<p style="margin: 10px;">No file found matching specified criteria (' . implode(', ', $criteria) . ')</p><br/>'; } else { echo '<p style="margin: 10px;">Found ' . count($searchResult) . ' file(s) matching specified criteria (' . implode(', ', $criteria) . ')</p><br/>'; foreach ($searchResult as $url) { echo "<img style='margin: 10px;' src=\"{$url}\" width=\"32\" height=\"32\""; echo "onmouseover=\"this.height = 50;this.width = 50; this.style.border = '3px solid yellow';\" onmouseout=\"this.height = 32;this.width = 32; this.style.border = '0px solid black'\" onclick=\"return insertImageTag('{$url}');\" /> "; } } echo "<hr/>"; } } catch (Exception $e) { echo '<p style="margin: 10px; color: red;">Error in searching files: ' . $e->getMessage() . "</p><br/>"; } } $first_container_name = ""; ?> <form name="SelectContainerForm" style="margin: 10px;" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?> "> <table style="margin: 10px; border-width: 2px;border-color: black;" > <tr> <td><b>Container Name:</b></td> <td> <select name="selected_container" title="Stoarge container to be used for storing media files" onChange="document.SelectContainerForm.submit()"> <?php try { $storageClient = WindowsAzureStorageUtil::getStorageClient(); $listContainerResult = $storageClient->listContainers(); foreach ($listContainerResult->getContainers() as $container) { if (empty($first_container_name)) { $first_container_name = $container->getName(); } ?> <option value="<?php echo $container->getName(); ?> " <?php echo $container->getName() == $selected_container_name ? 'selected="selected"' : ''; ?> > <?php echo $container->getName(); ?> </option> <?php } } catch (Exception $ex) { // Ignore exception as account keys are not yet set } ?> </select> </td> </tr> </table> </form> <table style="margin: 10px; border-width: 2px;border-color: black;"> <tr> <td> <?php try { if (empty($selected_container_name)) { echo '<p style="margin: 10px; color: red;">Default Azure Storage Container name is not yet configured. Please configure it in the Windows Azure Settings Tab.</p>'; $selected_container_name = $first_container_name; } // Get list of blobs in specified container $listBlobResult = $storageClient->listBlobs($selected_container_name); $blobs = $listBlobResult->getBlobs(); if (empty($blobs)) { echo "<p style='margin: 10px;'>No items in container '{$selected_container_name}'.</p>"; } else { echo '<p style="margin: 10px;">Note: Click on the image to insert image URL into the blog!</p><br/>'; foreach ($blobs as $blob) { $url = WindowsAzureStorageUtil::getStorageUrlPrefix(false) . "/{$selected_container_name}/" . $blob->getName(); $containsSignature = "false"; $fileExt = substr(strrchr($blob->getName(), '.'), 1); switch (strtolower($fileExt)) { case "jpg": case "jpeg": case "gif": case "bmp": case "png": case "tiff": echo "<img style='margin: 10px;' src=\"{$url}\" width=\"32\" height=\"32\""; echo "onmouseover=\"this.height = 50;this.width = 50; this.style.border = '3px solid yellow';\" onmouseout=\"this.height = 32;this.width = 32; this.style.border = '0px solid black'\" onclick=\"return insertImageTag('{$url}', '{$containsSignature}');\"/>"; break; default: echo "<a style='margin: 10px;' href=\"{$url}\""; echo "onclick=\"return insertImageTag('{$url}', '{$containsSignature}');\">" . $blob->getName() . "<a/>"; break; } $deleteLink = 'media-upload.php?post_id=0&tab=browse&deleteBlob=' . urlencode($blob->getName()) . '&selected_container=' . urlencode($selected_container_name); echo "<a style='color: red;' href=\"" . $deleteLink . "\">x</a> "; } } } catch (Exception $e) { echo '<p style="margin: 10px; color: red;">Error in listing storage containers: ' . $e->getMessage() . "</p><br/>"; } ?> </td> </tr> </table> <?php if (!empty($blobs)) { ?> <form name="DeleteAllBlobsForm" style="margin: 20px;" method="post" action=""<?php echo $_SERVER['REQUEST_URI']; ?> "> <input type='hidden' name='DeleteAllBlobs' value='true' /> <input type='hidden' name='selected_container' value='<?php echo $selected_container_name; ?> ' /> <u onMouseOver="style.cursor='hand'" onclick='document.DeleteAllBlobsForm.submit()' style="color: red;">Delete All Files</u> </form> <?php } } }