PostmanOptions::getInstance()->getConnectionTimeout(), 'headers' => $headers, 'body' => $parameters, ); if ( PostmanUtils::$logger->isTrace() ) { PostmanUtils::$logger->trace( sprintf( 'Posting to %s', $url ) ); PostmanUtils::$logger->trace( 'Post header:' ); PostmanUtils::$logger->trace( $headers ); PostmanUtils::$logger->trace( 'Posting args:' ); PostmanUtils::$logger->trace( $parameters ); } $response = wp_remote_post( $url, $args ); // pre-process the response if ( is_wp_error( $response ) ) { PostmanUtils::$logger->error( $response->get_error_message() ); throw new Exception( 'Error executing wp_remote_post: ' . $response->get_error_message() ); } else { return $response; } } /** * A facade function that handles redirects. * Inside WordPress we can use wp_redirect(). Outside WordPress, not so much. **Load it before postman-core.php** * * @param unknown $url */ static function redirect( $url ) { // redirections back to THIS SITE should always be relative because of IIS bug if ( PostmanUtils::$logger->isTrace() ) { PostmanUtils::$logger->trace( sprintf( "Redirecting to '%s'", $url ) ); } wp_redirect( $url ); exit(); } static function parseBoolean( $var ) { return filter_var( $var, FILTER_VALIDATE_BOOLEAN ); } static function logMemoryUse( $startingMemory, $description ) { PostmanUtils::$logger->trace( sprintf( $description . ' memory used: %s', PostmanUtils::roundBytes( memory_get_usage() - $startingMemory ) ) ); } /** * Rounds the bytes returned from memory_get_usage to smaller amounts used IEC binary prefixes * See http://en.wikipedia.org/wiki/Binary_prefix * * @param unknown $size * @return string */ static function roundBytes( $size ) { $unit = array( 'B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', ); return @round( $size / pow( 1024, ($i = floor( log( $size, 1024 ) )) ), 2 ) . ' ' . $unit [ $i ]; } /** * Unblock threads waiting on lock() */ static function unlock() { if ( PostmanState::getInstance()->isFileLockingEnabled() ) { PostmanUtils::deleteLockFile(); } } /** * Processes will block on this method until unlock() is called * Inspired by http://cubicspot.blogspot.ca/2010/10/forget-flock-and-system-v-semaphores.html * * @throws Exception */ static function lock() { if ( PostmanState::getInstance()->isFileLockingEnabled() ) { $attempts = 0; while ( true ) { // create the semaphore $lock = PostmanUtils::createLockFile(); if ( $lock ) { // if we got the lock, return return; } else { $attempts ++; if ( $attempts >= 10 ) { throw new Exception( sprintf( 'Could not create lockfile %s', '/tmp' . '/.postman.lock' ) ); } sleep( 1 ); } } } } static function lockFileExists() { $path = PostmanUtils::calculateTemporaryLockPath( null ); return file_exists($path); } static function deleteLockFile( $tempDirectory = null ) { $path = PostmanUtils::calculateTemporaryLockPath( $tempDirectory ); $success = @unlink( $path ); if ( PostmanUtils::$logger->isTrace() ) { PostmanUtils::$logger->trace( sprintf( 'Deleting file %s : %s', $path, $success ) ); } return $success; } static function createLockFile( $tempDirectory = null ) { if ( self::lockFileExists() ) { self::deleteLockFile(); } $path = PostmanUtils::calculateTemporaryLockPath( $tempDirectory ); $success = @fopen( $path, 'xb' ); if ( PostmanUtils::$logger->isTrace() ) { PostmanUtils::$logger->trace( sprintf( 'Creating file %s : %s', $path, $success ) ); } return $success; } /** * Creates the pathname of the lockfile * * @param unknown $tempDirectory * @return string */ private static function calculateTemporaryLockPath( $tempDirectory ) { if ( empty( $tempDirectory ) ) { $options = PostmanOptions::getInstance(); $tempDirectory = $options->getTempDirectory(); } $fullPath = sprintf( '%s/.postman_%s.lock', $tempDirectory, self::generateUniqueLockKey() ); return $fullPath; } /** * * @return string */ private static function generateUniqueLockKey() { // for single sites, use the network_site_url to generate the key because // it is unique for every wordpress site unlike the blog ID which may be the same $key = hash( 'crc32', network_site_url( '/' ) ); // TODO for multisites // if the subsite is sharing the config - use the network_site_url of site 0 // if the subsite has its own config - use the network_site_url of the subsite return $key; } /** * From http://stackoverflow.com/a/381275/4368109 * * @param unknown $text * @return boolean */ public static function isEmpty( $text ) { // Function for basic field validation (present and neither empty nor only white space return ( ! isset( $text ) || trim( $text ) === ''); } /** * Warning! This can only be called on hook 'init' or later */ public static function isAdmin() { /** * is_admin() will return false when trying to access wp-login.php. * is_admin() will return true when trying to make an ajax request. * is_admin() will return true for calls to load-scripts.php and load-styles.php. * is_admin() is not intended to be used for security checks. It will return true * whenever the current URL is for a page on the admin side of WordPress. It does * not check if the user is logged in, nor if the user even has access to the page * being requested. It is a convenience function for plugins and themes to use for * various purposes, but it is not suitable for validating secured requests. * * Good to know. */ $logger = PostmanUtils::$logger = new PostmanLogger( 'PostmanUtils' ); if ( $logger->isTrace() ) { $logger->trace( 'calling current_user_can' ); } return current_user_can( Postman::MANAGE_POSTMAN_CAPABILITY_NAME ) && is_admin(); } /** * Validate an e-mail address * * @param unknown $email * @return number */ static function validateEmail( $email ) { if ( PostmanOptions::getInstance()->isEmailValidationDisabled() ) { return true; } require_once 'Postman-Mail/Zend-1.12.10/Exception.php'; require_once 'Postman-Mail/Zend-1.12.10/Registry.php'; require_once 'Postman-Mail/Zend-1.12.10/Validate/Exception.php'; require_once 'Postman-Mail/Zend-1.12.10/Validate/Interface.php'; require_once 'Postman-Mail/Zend-1.12.10/Validate/Abstract.php'; require_once 'Postman-Mail/Zend-1.12.10/Validate/Ip.php'; require_once 'Postman-Mail/Zend-1.12.10/Validate/Hostname.php'; require_once 'Postman-Mail/Zend-1.12.10/Validate/EmailAddress.php'; if ( ! isset( PostmanUtils::$emailValidator ) ) { PostmanUtils::$emailValidator = new Postman_Zend_Validate_EmailAddress(); } return PostmanUtils::$emailValidator->isValid( $email ); } /** * From http://stackoverflow.com/questions/13430120/str-getcsv-alternative-for-older-php-version-gives-me-an-empty-array-at-the-e * * @param unknown $string * @return multitype: */ static function postman_strgetcsv_impl( $string ) { $fh = fopen( 'php://temp', 'r+' ); fwrite( $fh, $string ); rewind( $fh ); $row = fgetcsv( $fh ); fclose( $fh ); return $row; } /** * * @return Ambigous */ static function postmanGetServerName() { if ( ! empty( $_SERVER ['SERVER_NAME'] ) ) { $serverName = $_SERVER ['SERVER_NAME']; } else if ( ! empty( $_SERVER ['HTTP_HOST'] ) ) { $serverName = $_SERVER ['HTTP_HOST']; } else { $serverName = 'localhost.localdomain'; } return $serverName; } /** * Does this hostname belong to Google? * * @param unknown $hostname * @return boolean */ static function isGoogle( $hostname ) { return PostmanUtils::endsWith( $hostname, 'gmail.com' ) || PostmanUtils::endsWith( $hostname, 'googleapis.com' ); } /** * * @param unknown $actionName * @param unknown $callbackName */ public static function registerAdminMenu( $viewController, $callbackName ) { $logger = PostmanUtils::$logger; if ( $logger->isTrace() ) { $logger->trace( 'Registering admin menu ' . $callbackName ); } add_action( 'admin_menu', array( $viewController, $callbackName, ) ); } /** * * @param unknown $actionName * @param unknown $callbackName */ public static function registerAjaxHandler( $actionName, $class, $callbackName ) { if ( is_admin() ) { $fullname = 'wp_ajax_' . $actionName; // $this->logger->debug ( 'Registering ' . 'wp_ajax_' . $fullname . ' Ajax handler' ); add_action( $fullname, array( $class, $callbackName, ) ); } } /** * * @param unknown $parameterName * @return mixed */ public static function getBooleanRequestParameter( $parameterName ) { return filter_var( self::getRequestParameter( $parameterName ), FILTER_VALIDATE_BOOLEAN ); } /** * * @param unknown $parameterName * @return unknown */ public static function getRequestParameter( $parameterName ) { $logger = PostmanUtils::$logger; if ( isset( $_POST [ $parameterName ] ) ) { $value = filter_var( $_POST [ $parameterName ], FILTER_SANITIZE_STRING ); if ( $logger->isTrace() ) { $logger->trace( sprintf( 'Found parameter "%s"', $parameterName ) ); $logger->trace( $value ); } return $value; } } public static function getServerName() { $result = 'localhost.localdomain'; if (isset($_SERVER) and array_key_exists('SERVER_NAME', $_SERVER)) { $result = $_SERVER['SERVER_NAME']; } elseif (function_exists('gethostname') and gethostname() !== false) { $result = gethostname(); } elseif (php_uname('n') !== false) { $result = php_uname('n'); } if ( $result !== 'localhost.localdomain' ) { // get the current result ip $ip = gethostbyname($result); // dns query failed if ( $ip == $result ) { return $result; } // get the current ip hostname - reverse dns $host = gethostbyaddr($ip); // dns query failed if ( $host == $ip ) { return $result; } // if hostname is not equal to the result set the ptr if ( $result !== $host ) { $result = $host; } } return $result; } public static function getHost( $url ) { $host = parse_url( trim( $url ), PHP_URL_HOST ); return str_replace('www.', '', $host ); } } PostmanUtils::staticInit();