/**
 * Delete a directory.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @param string $dirname Directory name that needs to be deleted.
 * @return bool
 */
function geodir_delete_directory($dirname)
{
    $dir_handle = '';
    if (is_dir($dirname)) {
        $dir_handle = opendir($dirname);
    }
    if (!$dir_handle) {
        return false;
    }
    while ($file = readdir($dir_handle)) {
        if ($file != "." && $file != "..") {
            if (!is_dir($dirname . "/" . $file)) {
                unlink($dirname . "/" . $file);
            } else {
                geodir_delete_directory($dirname . '/' . $file);
            }
        }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}
/**
 * Add or remove images for a comment.
 *
 * @since 1.0.0
 * @package GeoDirectory_Review_Rating_Manager
 *
 * @param object $comment The comment object.
 * @param array $newArr Images array.
 * @return array Comment Images array.
 */
function geodir_reviewrating_add_remove_images($comment, $newArr)
{
    global $wpdb, $current_user;
    $temp_folder_name = 'temp_' . $current_user->data->ID;
    if ($current_user->data->ID == '') {
        $temp_folder_name = 'temp_' . session_id();
    }
    $wp_upload_dir = wp_upload_dir();
    $temp_folder = $wp_upload_dir['path'] . '/' . $temp_folder_name;
    $comment_img_path = $wp_upload_dir['basedir'] . '/comment_images';
    if (!is_dir($comment_img_path)) {
        wp_mkdir_p($comment_img_path);
    }
    $comment_images = array();
    foreach ($newArr as $img) {
        $file_ext = pathinfo($img, PATHINFO_EXTENSION);
        $file_name = basename($img, "." . $file_ext);
        $filename = $temp_folder . '/' . basename($img);
        $new_file_name = $comment_img_path . '/' . $file_name . '_' . time() . '.' . $file_ext;
        copy($filename, $new_file_name);
        $comment_images[] = $wp_upload_dir['baseurl'] . '/comment_images/' . $file_name . '_' . time() . '.' . $file_ext;
    }
    geodir_delete_directory($temp_folder);
    /*if ( is_dir( $temp_folder ) ) {
    		$dirPath = $temp_folder;
    		if ( substr( $dirPath, strlen( $dirPath ) - 1, 1) != '/' ) {
    			$dirPath .= '/';
    		}
    		$files = glob( $dirPath . '*', GLOB_MARK );
    		foreach ( $files as $file ) {
    			if ( is_dir( $file ) ) {
    				self::deleteDir( $file );
    			} else {
    				unlink( $file );
    			}
    		}
    		rmdir($dirPath);
    	}*/
    return $comment_images;
}