/**
* Prints the supplied string to "standard error" (STDERR) instead of the "standard output" (STDOUT) stream
*
* @param string	$source_csv_filename		The CSV import file containing the asset attribute and metadata field specifications
* @param string	$asset_type_code			The Matrix asset type code or the assets which are to be created
* @param object	$parent_id					The asset ID of the parent asset under which the new assets are to reside
* @param int	$schema_id					The asset ID of the Metadata Schema to associate with the new assets
* @param array	$metadata_mapping			A structure containing Supplied Field Name to Metadata Field asset ID associations
* @param array	$attribute_mapping			A structure containing Supplied Field Name to Asset Attribute Name associations
* @param boolean$new_assets_live			When set to TRUE, assets created during the import will be set to 'Live'
* @param string $unique_record_field		The CSV field to be treated as a primary identifier for editing and deletion purposes
* @param string $record_modification_field	The CSV field to determine whether the associated record is to be (A)dded, (E)dited, or (D)eleted
* @param array	$ignore_fields				The fields for the ignoring
*
* @return array
* @access public
*/
function importAssets($source_csv_filename, $asset_type_code, $parent_id, $schema_id, array $metadata_mapping, array $attribute_mapping, $new_assets_live = FALSE, $unique_record_field = '', $record_modification_field = '', array $ignore_fields = array())
{
    $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
    $num_assets_imported = 0;
    $num_assets_modified = 0;
    $num_assets_deleted = 0;
    $csv_fd = fopen($source_csv_filename, 'r');
    if (!$csv_fd) {
        printUsage();
        printStdErr("* The supplied CSV import file was not found\n\n");
        fclose($csv_fd);
        exit(-6);
    }
    $parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($parent_id);
    if (!$parent_asset->id) {
        printUsage();
        printStdErr("* The specified parent asset was not found\n\n");
        exit(-7);
    }
    $header_line = TRUE;
    $headers = array();
    $trash = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('trash_folder');
    $root_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_folder');
    // Set to true if temporary trash folder is created, where all the assets to be deleted are moved to
    $temp_trash_folder = FALSE;
    while (($data = fgetcsv($csv_fd, 0, ',')) !== FALSE) {
        $num_fields = count($data);
        $asset_spec = array();
        foreach ($data as $key => $val) {
            if ($header_line) {
                $headers[$key] = trim($val);
            } else {
                $asset_spec[$headers[$key]] = $val;
            }
        }
        if ($header_line) {
            $header_line = FALSE;
        } else {
            // If a Record Modification Field was specified, we also require that a Unique Field was specified for the import
            // These two fields must be present in the CSV file for us to Edit and Delete existing assets. Otherwise, we'll just add
            $record_handling = IMPORT_ADD_RECORD;
            if (!empty($unique_record_field) && !empty($record_modification_field) && isset($asset_spec[$unique_record_field]) && isset($asset_spec[$record_modification_field])) {
                $record_modification_state = strtoupper($asset_spec[$record_modification_field]);
                switch ($record_modification_state) {
                    case 'D':
                        $record_handling = IMPORT_DELETE_RECORD;
                        break;
                    case 'E':
                        $record_handling = IMPORT_EDIT_RECORD;
                        break;
                }
                // Okey dokey, let's find the existing asset as we are either performing an (E)dit or (D)elete operation...
                // Also try to find an existing asset as we may be unfortunately performing an (A)dd that matches the unique
                // identifier, in which case we are probably intending to (E)dit the existing matching asset
                $existing_asset_id = 0;
                // Our search is limited to the exact asset type used for the import, and the parent root node (and children) specified
                // The unique field may be either one to be assigned to an attribute or to a metadata field
                $search_field = '';
                $search_value = '';
                if (isset($metadata_mapping[$unique_record_field])) {
                    $search_type = 'metadata';
                    $search_field = $metadata_mapping[$unique_record_field];
                    $search_value = $asset_spec[$unique_record_field];
                }
                if (isset($attribute_mapping[$unique_record_field])) {
                    $search_type = 'attribute';
                    $search_field = $attribute_mapping[$unique_record_field];
                    $search_value = $asset_spec[$unique_record_field];
                }
                $search = array($search_type => array('field' => $search_field, 'value' => $search_value));
                $existing_assets = findAsset($parent_id, $asset_type_code, $search);
                if (count($existing_assets) > 1) {
                    // Multiple matching assets - skip
                    echo "\n*\t* The record for '" . $search_value . "' matched multiple existing assets. Cannot determine how to proceed - continuing to the next record.\n";
                    continue;
                }
                $existing_asset_id = reset($existing_assets);
                // If it is an (E)dit request and the asset was not found, then let's make it an (A)dd instead
                if (empty($existing_assets) && $record_handling == IMPORT_EDIT_RECORD) {
                    echo "\n*\t* The following 'Edit' request for '" . $search_value . "' has been changed to 'Add' as there is not an existing matching asset\n";
                    $record_handling = IMPORT_ADD_RECORD;
                }
                // If it's there and we wanted to (A)dd, then make it an (E)dit instead
                if ($existing_asset_id > 0 && $record_handling == IMPORT_ADD_RECORD) {
                    echo "\n*\t* The following 'Add' request for '" . $search_value . "' has been changed to 'Edit' as this asset already exists.\n";
                    $record_handling = IMPORT_EDIT_RECORD;
                }
                // If it is a (D)elete request and the asset was not found, then skip this record gracefully
                if (empty($existing_assets) && $record_handling == IMPORT_DELETE_RECORD) {
                    echo "\n*\t* Deletion request for asset with unique field value '" . $search_value . "' was aborted due to a missing matching asset. Continuing to the next record.\n";
                    continue;
                }
                if ($record_handling == IMPORT_DELETE_RECORD) {
                    // Deletify
                    echo '- Deleting asset';
                    $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($existing_asset_id);
                    if ($asset) {
                        // Create temporary trash folder, if not already created
                        if (!$temp_trash_folder) {
                            $GLOBALS['SQ_SYSTEM']->am->includeAsset('folder');
                            $temp_trash_folder = new Folder();
                            $temp_trash_folder->setAttrValue('name', 'temp_trash_folder');
                            $link_array = array('asset' => $root_folder, 'value' => '', 'link_type' => SQ_LINK_TYPE_1);
                            $linkid = $temp_trash_folder->create($link_array);
                            // If cannot create the temporary trash folder then we cannot delete any asset
                            if (!$linkid) {
                                echo "\n*\t* Deletion request for asset with unique field value '" . $search_value . "' was aborted due to unable to create temporary trash folder. Continuing to the next record.\n";
                                $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
                                continue;
                            }
                        }
                        // Move the asset to the temporary trash folder
                        $asset_linkid_old = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($parent_id, $asset->id);
                        $linkid = $GLOBALS['SQ_SYSTEM']->am->moveLink($asset_linkid_old['linkid'], $temp_trash_folder->id, SQ_LINK_TYPE_1, -1);
                        // If cannot move the asset to temporary trash folder then it cannot be deleted
                        if (!$linkid) {
                            echo "\n*\t* Deletion request for asset with unique field value '" . $search_value . "' was aborted due to unable to move this asset to temporary trash folder. Continuing to the next record.\n";
                            $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
                            continue;
                        }
                        echo $search_value . ',' . $existing_asset_id . ",D\n";
                        $num_assets_deleted++;
                    }
                    // End if asset
                } else {
                    if ($record_handling == IMPORT_EDIT_RECORD) {
                        // Editise
                        // Ok we are editing - has the user specified fields to ignore at this point? If so, let's eliminificate
                        foreach ($ignore_fields as $ignore_field_name => $val) {
                            if (isset($asset_spec[$ignore_field_name])) {
                                unset($asset_spec[$ignore_field_name]);
                            }
                        }
                        echo '- Modifying asset with unique field value';
                        editAsset($existing_asset_id, $asset_spec, $attribute_mapping, $metadata_mapping, $schema_id);
                        echo $search_value . ',' . $existing_asset_id . ",E\n";
                        $num_assets_modified++;
                    }
                }
            }
            if ($record_handling == IMPORT_ADD_RECORD) {
                $asset_info = createAsset($asset_spec, $asset_type_code, $parent_asset, $schema_id, $metadata_mapping, $attribute_mapping);
                $asset_id = 0;
                if (is_array($asset_info)) {
                    $asset_id = reset($asset_info);
                }
                if ($asset_id) {
                    // Ok see if we need to set it live
                    if ($new_assets_live) {
                        $new_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset_id);
                        $new_asset->processStatusChange(SQ_STATUS_LIVE);
                        $GLOBALS['SQ_SYSTEM']->am->forgetAsset($new_asset);
                    }
                    echo key($asset_info) . ',' . $asset_id . ",A\n";
                    $num_assets_imported++;
                }
            }
        }
    }
    // End while
    fclose($csv_fd);
    $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
    // Now actually delete all the assets moved to "temporary purge folder" by purging this folder
    if ($temp_trash_folder && $GLOBALS['SQ_SYSTEM']->am->trashAsset($temp_trash_folder->id)) {
        $trash_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('trash_folder');
        $trash_linkid = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($trash_folder->id, $temp_trash_folder->id);
        if (isset($trash_linkid['linkid']) && $trash_linkid['linkid'] > 0) {
            $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
            $vars = array('purge_root_linkid' => $trash_linkid['linkid']);
            $errors = $hh->freestyleHipo('hipo_job_purge_trash', $vars);
            if (!empty($errors)) {
                $error_msg = '';
                foreach ($errors as $error) {
                    $error_msg .= ' * ' . $error['message'];
                }
                echo "Following errors occured while deleting asset(s):\n{$error_msg}\n";
            }
        }
    }
    $status_report = array('num_added' => $num_assets_imported, 'num_modified' => $num_assets_modified, 'num_deleted' => $num_assets_deleted);
    return $status_report;
}
// Encoding Standard Options
$options->archiveWindowLenght = new \DateInterval("PT1H");
// AES Dynamic Encription Options
$options->aesDynamicEncription = true;
$options->tokenRestriction = true;
$options->tokenType = TokenType::JWT;
// Archive options
$options->deleteArchiveAsset = true;
// change this to false to keep the asset archive
echo "Azure SDK for PHP - Live Features" . PHP_EOL;
// 0 - set up the MediaServicesService object to call into the Media Services REST API.
$restProxy = ServicesBuilder::getInstance()->createMediaServicesService(new MediaServicesSettings($account, $secret));
// 1 - Create and Start new Channel.
$channel = createAndStartChannel($restProxy, $options);
// 2 - Create the Live Streaming Asset
$assetResult = createAsset($restProxy, $options);
// 3 - Create and Start new Program, also apply AES encryption (if set)
$program = createAndStartProgram($restProxy, $assetResult->asset, $channel, $options);
// 4 - Publish the Live Streaming Asset
$assetResult->streamingUrl = publishLiveAsset($restProxy, $assetResult->asset);
// 5 - Display the ingest URL, the preview URL, the token (if applies) and the Streaming URL
echo "Ingest URL: {$channel->getInput()->getEndpoints()[0]->getUrl()}" . PHP_EOL;
echo "Preview URL: {$channel->getPreview()->getEndpoints()[0]->getUrl()}" . PHP_EOL;
echo "Streaming URL: {$assetResult->streamingUrl}" . PHP_EOL;
if (isset($assetResult->generatedTestToken)) {
    echo "Token: Bearer {$assetResult->generatedTestToken}" . PHP_EOL;
}
// 6 - Wait for user decide to shutdown.
echo PHP_EOL . "Press ENTER to shutdown the live stream and cleanup resources." . PHP_EOL;
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
    $ooyala = get_option('ooyala');
    try {
        $api = new OoyalaApi($ooyala['api_key'], $ooyala['api_secret']);
        $response = $api->put("assets/" . $asset_id . "/upload_status", array('status' => 'uploaded'));
        return $response;
    } catch (Exception $e) {
        http500($e->getMessage());
    }
}
// End of functions, begin our script here.
// We can't use $_POST since that only works if we are posting urlencoded data
// and not pure JSON.
$requestObject = json_decode(file_get_contents("php://input"));
switch ($_GET['request']) {
    case 'asset-create':
        $response = createAsset((object) $_POST);
        echo json_encode($response);
        break;
    case 'asset-upload':
        $response = uploadAsset((object) $_GET);
        echo json_encode($response);
        break;
    case 'asset-status':
        if (!empty($_GET['asset_id'])) {
            $response = uploadStatus(sanitize_text_field($_GET['asset_id']));
            echo json_encode($response);
        }
        break;
    case 'labels-create':
        break;
    case 'labels-assign':
/**
* Searches the Matrix System for an existing year/month/day folder under the specified parent, based on the supplied timestamp.
* If the expected folders do not exist under the parent, they will be created.
* The asset ID of the matching year/month/day folder is returned.
*
* @param int		$parent_id			The asset ID from which to search for matching a year/month folder combination
* @param int		$create_timestamp	The folder specification to search (eg; where period = "month", create_timestamp = 0 = 1 Jan 1970 = search for folder structure 1970 > 01)
* @param string		$period				The period to search or create - one of "year", "month", or "day"
* @param int		$folder_link_type	The asset link type to use when creating new dated folders
* @param boolean	$make_folders_live      Whether to make the newly-created dated folders live (default = Under Construction)
*
* @return int
* @access public
*/
function searchExistingDatedFolder($parent_id, $create_timestamp, $period, $folder_link_type, $make_folders_live = FALSE)
{
    $am =& $GLOBALS['SQ_SYSTEM']->am;
    // Year and month folder names. Month and day are zero-padded numbers
    $year = date('Y', $create_timestamp);
    $month = str_pad(date('m', $create_timestamp), 2, '0', STR_PAD_LEFT);
    $day = str_pad(date('d', $create_timestamp), 2, '0', STR_PAD_LEFT);
    // Variable housekeeping
    $matching_year_folder_id = 0;
    $matching_month_folder_id = 0;
    $matching_day_folder_id = 0;
    $matching_folder_id = 0;
    $matching_year_folders = searchExistingAsset($parent_id, $year, 'folder');
    $num_found_assets = count($matching_year_folders);
    if ($num_found_assets > 1) {
        echo "\n- FAILED - found " . $num_found_assets . ' year folders for ' . $year . "\n";
        return 0;
    }
    if ($num_found_assets == 1) {
        $matching_year_folder_id = $matching_year_folders[0];
    }
    if ($matching_year_folder_id == 0) {
        echo "\n- Creating Year Folder " . $year . '... ';
        $matching_year_folder_id = createAsset('folder', $year, $parent_id, $folder_link_type);
        echo 'asset #' . $matching_year_folder_id . "\n";
        if ($make_folders_live) {
            setAssetStatus($matching_year_folder_id, SQ_STATUS_LIVE);
        }
    }
    $matching_folder_id = $matching_year_folder_id;
    // If we're looking for a month or day, dig deeper
    if ($matching_year_folder_id > 0 && $period != 'year') {
        $matching_month_folders = searchExistingAsset($matching_year_folder_id, $month, 'folder');
        $num_found_assets = count($matching_month_folders);
        if ($num_found_assets > 1) {
            echo "\n- FAILED - found " . $num_found_assets . ' month folders for year/month ' . $year . '/' . $month . "\n";
            return 0;
        }
        if ($num_found_assets == 1) {
            $matching_month_folder_id = $matching_month_folders[0];
        }
        if ($matching_month_folder_id == 0) {
            echo "\n- Creating Month Folder " . $month . ' under Year ' . $year . '... ';
            $matching_month_folder_id = createAsset('folder', $month, $matching_year_folder_id, $folder_link_type);
            echo 'asset #' . $matching_month_folder_id . "\n";
            if ($make_folders_live) {
                setAssetStatus($matching_month_folder_id, SQ_STATUS_LIVE);
            }
        }
        $matching_folder_id = $matching_month_folder_id;
    }
    // Searching for a day - the last possible level
    if ($matching_month_folder_id > 0 && $period == 'day') {
        $matching_day_folders = searchExistingAsset($matching_month_folder_id, $day, 'folder');
        $num_found_assets = count($matching_day_folders);
        if ($num_found_assets > 1) {
            echo "\n- FAILED - found " . $num_found_assets . ' day folders for year/month/day ' . $year . '/' . $month . '/' . $day . "\n";
            return 0;
        }
        if ($num_found_assets == 1) {
            $matching_day_folder_id = $matching_day_folders[0];
        }
        if ($matching_day_folder_id == 0) {
            echo "\n- Creating Day Folder " . $day . ' under Year/Month ' . $year . '/' . $month . '... ';
            $matching_day_folder_id = createAsset('folder', $day, $matching_month_folder_id, $folder_link_type);
            echo 'asset #' . $matching_day_folder_id . "\n";
            if ($make_folders_live) {
                setAssetStatus($matching_day_folder_id, SQ_STATUS_LIVE);
            }
        }
        $matching_folder_id = $matching_day_folder_id;
    }
    return $matching_folder_id;
}