<?php
  error_reporting(E_ALL ^ E_NOTICE);
  //获取存放图片文件夹
  $xml = file_get_contents('./iv-includes/include/config.xml');
  preg_match_all('/\>(.*)\<\/contentfolder\>/U', $xml, $preg_match);
  $picdir = $preg_match[1][0];
  //得到网站地址
  $website = "http://".$_SERVER['HTTP_HOST'].str_replace("api.php","",$_SERVER["SCRIPT_NAME"]);
  switch($_GET['method']) {
  	case 'get.gallery.name':
  	  $xml = file_get_contents($picdir . 'folders.xml');
  	  preg_match_all('/name\=\"(.*)\"/U', $xml, $preg_match);
  	  $count = count($preg_match[1]);
  	  for($i=1;$i< $count;$i++) {
  	  	/*检测相册是否隐藏或者加锁//目前隐藏和加锁都不返回结果*/
  	  	$gallery_info['hidden'] = file_get_contents($website . 'api.php?method=get.gallery.hidden&name=' . $preg_match[1][$i]);
  	  	$gallery_info['password'] = file_get_contents($website . 'api.php?method=get.gallery.password&name=' . $preg_match[1][$i]);
  	  	/*检测相册目录属性//属性为gallery才给予显示*/
  	  	$gallery_info['page'] = file_get_contents($website . 'api.php?method=get.gallery.page&name=' . $preg_match[1][$i]);
  	  	if($gallery_info['hidden'] != 'true' && $gallery_info['password'] != 'true' && $gallery_info['page'] == 'gallery') {
  	      $gallery_id[$i] = $preg_match[1][$i];
  	    }	
  	  }
  	  /*分别得到有设置起始结果和无起始结果的值*/
  	  $gallery = array();
  	  if($_GET['start-index'] != '' && $_GET['max-results'] != '') {
  	  	$max = $_GET['start-index'] + $_GET['max-results'];
  	    for($j=$_GET['start-index'];$j< $max;$j++) {
  	      $gallery[$j] = $gallery_id[$j];
  	    }	
  	  } else {
  	    $gallery = $gallery_id;	
  	  }
  	  echo json_encode($gallery);	   
  	break;  
  	
  	/*获取某一相册的相册信息*/	
  	case 'get.gallery.info':
  	  if($_GET['name'] == '') {
  	    $error = 'gallery name must be define!';
  	    echo $error;	
  	  } else {
	  	  $xml = file_get_contents($picdir . 'folders.xml');
	  	  $item = array(0 => 'mTime', 
	  	                1 => 'name',
	  	                2 => 'date',
	  	                3 => 'fileCount',
	  	                4 => 'totalFileCount',
	  	                5 => 'maxThumbWidth',
	  	                6 => 'maxThumbHeight',
	  	                7 => 'path',
	  	                8 => 'title',
	  	                9 => 'previewimage',
	  	                10 => 'description',
	  	                11 => 'pageContent',
	  	                12 => 'sort',
	  	                13 => 'hidden',
	  	                14 => 'page',
	  	                15 => 'fileMod',
	  	                16 => 'viewAs',
	  	                17 => 'order',
	  	                18 => 'parameters',
	  	                19 => 'password');
	  	  preg_match_all('/name\=\"(.*)\"/U', $xml, $preg_match);
	  	  $count = count($preg_match[1]);
	  	  $gallery = array();
	  	  for($i=0;$i < 20; $i++) {
	 		 	  preg_match_all('/' . $item[$i] . '\=\"(.*)\"/U', $xml, $preg_match);
	 		 	  for($j=0;$j< $count;$j++) {
	 		 	    $gallery[$j][$item[$i]] = $preg_match[1][$j];		 	   
	 		 	  }
	  	  }
	  	  for($k=0;$k< $count;$k++) {
	  	    if($gallery[$k]['name'] == $_GET['name']) {
	  	      $result = $k;	
	  	    }
	  	  }
	  	  //判断是否存在该相册//若不存在则返回false
	  	  if($result != '') {
	  	  	//判断该相册是否隐藏和加密//若非隐藏且非加密则返回相册信息//否则返回false
		  	  if($gallery[$result]['hidden'] != 'true' && $gallery[$result]['password'] == '') {
		  	    echo json_encode($gallery[$result]);	
		  	  } else {
		  	    echo 'false';	
		  	  }
	  	  } else {
	  	    echo 'false';	
	  	  } 
  	  }
  	break;
  	
  	/*获取特定相册下的所有相片*/
    case 'get.photos':
  	  if($_GET['name'] == '') {
  	    $error = 'gallery name must be define!';
  	    echo $error;	
  	  } else {
	  	  $xml = file_get_contents($picdir . 'folders.xml');
	  	  preg_match_all('/path\=\"(.*)\"/U', $xml, $preg_match_path);
	  	  preg_match_all('/name\=\"(.*)\"/U', $xml, $preg_match_name);
	  	  for($i=0;$i< count($preg_match_name[1]);$i++) {
	  	    if($_GET['name'] == $preg_match_name[1][$i]) {
	  	      $path = $preg_match_path[1][$i];
	  	    }	
	  	  }
	      $xml = file_get_contents($path . '/folderdata.xml');
	      preg_match_all('/\<file name\=\"(.*)\" title\=\"(.*)\" description\=\"(.*)\" order\=\"(.*)\"\/\>/U', $xml, $photos);
	      $photo = array();
	      $count = count($photos[1]);
	      for($i=0;$i < $count;$i++) {
	      	//相片文件名
		      $photo[$i]['file'] = $photos[1][$i];
		      //相片标题
		      $photo[$i]['title'] = $photos[2][$i];
		      //相片描述
		      $photo[$i]['descirption'] = $photos[3][$i];
		      //相片排序的序号
		      $photo[$i]['order'] = $photos[4][$i];
		      //自定义增加原图绝对地址
		      $photo[$i]['url'] = $website . $picdir . $_GET['name'] . '/' . $photo[$i]['file'];
		      //自定义增加缩略图绝对地址
		      $photo[$i]['thumbnail'] = $website . $picdir . $_GET['name'] . '/tn_' . substr($photo[$i]['file'], 0, -3) . 'jpg';
		    }
	  	  /*分别得到有设置起始结果和无起始结果的值*/
	  	  $picture = array();
	  	  if($_GET['start-index'] != '' && $_GET['max-results'] != '') {
	  	  	$max = $_GET['start-index'] + $_GET['max-results'];
	  	    for($j=$_GET['start-index'];$j< $max;$j++) {
	  	      $picture[$j] = $photo[$j];
	  	    }	
	  	  } else {
	  	    $picture = $photo;	
	  	  }
		    echo json_encode($picture);
		  }
	  break;
	  
	  /*判断相册是否为隐藏相册//是返回true否则返回false*/
	  case 'get.gallery.hidden':
  	  if($_GET['name'] == '') {
  	    $error = 'gallery name must be define!';
  	    echo $error;	
  	  } else {
	  	  $xml = file_get_contents($picdir . 'folders.xml');
	  	  preg_match_all('/hidden\=\"(.*)\"/U', $xml, $preg_match_hidden);
	  	  preg_match_all('/name\=\"(.*)\"/U', $xml, $preg_match_name);
	  	  for($i=0;$i< count($preg_match_name[1]);$i++) {
	  	    if($_GET['name'] == $preg_match_name[1][$i]) {
	  	      $hidden = $preg_match_hidden[1][$i];
	  	    }	
	  	  }
	  	  if($hidden == 'true') {
	  	    echo $hidden;
	  	  } else {
	  	    echo 'false';
	  	  }
	  	  
	  	}
	  break;
	  
	  /*判断相册是否为加密相册//是返回true否则返回false*/
	  case 'get.gallery.password':
	    if($_GET['name'] == '') {
  	    $error = 'gallery name must be define!';
  	    echo $error;	
	    } else {
	  	  $xml = file_get_contents($picdir . 'folders.xml');
	  	  preg_match_all('/password\=\"(.*)\"/U', $xml, $preg_match_password);
	  	  preg_match_all('/name\=\"(.*)\"/U', $xml, $preg_match_name);
	  	  for($i=0;$i< count($preg_match_name[1]);$i++) {
	  	    if($_GET['name'] == $preg_match_name[1][$i]) {
	  	      $password = $preg_match_password[1][$i];
	  	    }	
	  	  }
	  	  if($hidden != '') {
	  	    echo 'true';
	  	  } else {
	  	    echo 'false';
	  	  }
	    }
	  break;

    /*获取相册的作用属性*/	  
	  case 'get.gallery.page':
	    if($_GET['name'] == '') {
  	    $error = 'gallery name must be define!';
  	    echo $error;	
	    } else {
	  	  $xml = file_get_contents($picdir . 'folders.xml');
	  	  preg_match_all('/page\=\"(.*)\"/U', $xml, $preg_match_page);
	  	  preg_match_all('/name\=\"(.*)\"/U', $xml, $preg_match_name);
	  	  for($i=0;$i< count($preg_match_name[1]);$i++) {
	  	    if($_GET['name'] == $preg_match_name[1][$i]) {
	  	      $page = $preg_match_page[1][$i];
	  	    }	
	  	  }
	  	  echo $page;
	    }
	  break;
	  
	  /*获取某相册的文件总数//不包括文件夹*/
	  case 'get.gallery.filecount':
	    if($_GET['name'] == '') {
  	    $error = 'gallery name must be define!';
  	    echo $error;	
	    } else {
	  	  $xml = file_get_contents($picdir . 'folders.xml');
	  	  preg_match_all('/totalFileCount\=\"(.*)\"/U', $xml, $preg_match_count);
	  	  preg_match_all('/name\=\"(.*)\"/U', $xml, $preg_match_name);
	  	  for($i=0;$i< count($preg_match_name[1]);$i++) {
	  	    if($_GET['name'] == $preg_match_name[1][$i]) {
	  	      $count = $preg_match_count[1][$i];
	  	    }	
	  	  }
	  	  if($count != '') {
	  	    echo $count;
	  	  } else {
	  	    echo 'false';	
	  	  }
	    }
	  break;
	  
	  /*获取某相册的父相册//若为子相册则返回上一级父相册若本身是父相册则返回false*/
	  case 'get.gallery.children':
	    if($_GET['name'] == '') {
  	    $error = 'gallery name must be define!';
  	    echo $error;	
	    } else {
	  	  $xml = file_get_contents($picdir . 'folders.xml');
	  	  preg_match_all('/path\=\"(.*)\"/U', $xml, $preg_match_path);
	  	  preg_match_all('/name\=\"(.*)\"/U', $xml, $preg_match_name);
	  	  for($i=0;$i< count($preg_match_name[1]);$i++) {
	  	    if($_GET['name'] == $preg_match_name[1][$i]) {
	  	      $path = $preg_match_path[1][$i];
	  	    }	
	  	  }
	  	  $folder = explode('/', $path);
	  	  $search = array(substr($picdir, 0, -1), $_GET['name'], '');
	  	  foreach($search as $item) {
	  	  	$k = array_search($item,$folder);
	      	unset($folder[$k]);
	  	  }
	  	  if($folder != '') {
	  	    echo $folder[count($folder)];	
	  	  } else {
	  	    echo 'false';	
	  	  }
	  	}
	  break;
	  
	  default:
	    if($_GET['method'] == '') {
	      echo 'Welcome to ImageVue API Page, it made by <a href="http://imnerd.org">Austin.</a>.';
	    } else {
	      echo 'oops!The method API is not supported now.Maybe you can email <a href="mailto:i@imnerd.org">author</a> to update it!';
	    }
	  break;
  }
?>
