<!-- Lists all resources of input type and resource type -->

<?php
require '../../src/Cloudinary.php';
require '../../src/Api.php';

//Insert acct info
$config = \Cloudinary::config(array(
	"cloud_name" => "",
	"api_key" => "",
	"api_secret" => ""
));

print "Listing resources on cloud name: ". $config["cloud_name"] . "<br>";

$api = new \Cloudinary\Api();
$myfile = fopen($config["cloud_name"]."-resources.txt", "w");
$counter = 0;

//Insert types
$resource_type = "";
$type = "";

$curr = $api->resources(array("max_results" => 500, "resource_type" => $resource_type, "type" => $type));

$temp_list = $curr["resources"];

foreach($temp_list as $res){
	if( (!$res["placeholder"]) && (!endsWith($res["public_id"],".zip")) ){
		echo($res["public_id"] . "<br>");
		$txt = $res["public_id"] . "\r\n";
		fwrite($myfile, $txt);
		$counter++;
	}
}

while(array_key_exists("next_cursor", $curr)){
	$curr = $api->resources(array("max_results" => 500, "resource_type" => $resource_type, "type" => $type, "next_cursor" => $curr["next_cursor"]));
	$temp_list = $curr["resources"];

	foreach($temp_list as $res){
		if( (!$res["placeholder"]) && (!endsWith($res["public_id"],".zip")) ){
			echo($res["public_id"] . "<br>");
			$txt = $res["public_id"] . "\r\n";
			fwrite($myfile, $txt);
			$counter++;
		}
	}
}

echo($counter);

fclose($myfile);

//Helper function
function endsWith($big_string, $small_string) {
	if (strlen($small_string) == 0) {return true;}
	return (substr($big_string, -strlen($small_string)) === $small_string);
}

?>