123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889 |
- <?php
-
- require_once('smart_album.inc.php');
- require_once('albumutil.php');
-
- class SmartAlbumAPI extends WebAPI {
-
- function __construct() {
- parent::__construct(SZ_WEBAPI_API_DESCRIPTION_PATH);
- }
-
- protected function Process() {
- if (!strcasecmp($this->method, "list")) {
- session_write_close();
- $this->SmartAlbumList();
- } elseif (!strcasecmp($this->method, "getinfo")) {
- session_write_close();
- $this->GetInfo();
- } else {
- csSYNOPhotoMisc::CheckSessionTimeOut();
-
- if (!strcasecmp($this->method, "create")) {
- $this->Create();
- } elseif (!strcasecmp($this->method, "edit")) {
- $this->Edit();
- } elseif (!strcasecmp($this->method, "delete")) {
- $this->Delete();
- }
- }
- }
-
- private function ExtractAlbumToArray($data)
- {
- $params = array();
- $idArr = array();
- $items = explode(',', $data);
- foreach ($items as $item) {
- $arr = explode('_', $item);
- if (2 !== count($arr)) {
- return $idArr;
- }
- if ('album' !== $arr[0]) {
- return $idArr;
- }
- $sharename = trim($arr[1]);
- if ('' != $sharename) {
- $sharename = @pack('H*', $arr[1]);
- array_push($params, $sharename);
- }
- }
- $albums = Album::GetBySharename($params);
- foreach ($albums as $album) {
- $id = $album['shareid'];
- array_push($idArr, $id);
- }
- return $idArr;
- }
-
- private function ExtractTagStringToArray($tagString)
- {
- $tagArr = array();
- $arr = explode(',', $tagString);
- foreach ($arr as $tag) {
- $split = explode('tag_', $tag);
- if (2 !== count($split)) {
- return $tagArr;
- }
- array_push($tagArr, $split[1]);
- }
- return $tagArr;
- }
-
- private function ExtractExifStringToArray($exifString, $prefix = 'tag_')
- {
- $exifArr = array();
- $arr = explode(',', $exifString);
- foreach ($arr as $exif) {
- $split = explode($prefix, $exif);
- if (2 !== count($split)) {
- return false;
- }
- array_push($exifArr, $split[1]);
- }
- return $exifArr;
- }
-
- private function GetParams_List() {
- $params = array();
- if (!isset($_REQUEST['offset']) || !isset($_REQUEST['limit'])) {
- return false;
- }
- $params['offset'] = $_REQUEST['offset'];
- $params['limit'] = $_REQUEST['limit'];
-
- $params['sort_by'] = (isset($_REQUEST['sort_by'])) ? $_REQUEST['sort_by'] : 'title';
- if (!in_array($params['sort_by'], array('title'))) {
- return false;
- }
- $params['sort_direction'] = (isset($_REQUEST['sort_direction'])) ? $_REQUEST['sort_direction'] : 'asc';
- if (!in_array($params['sort_direction'], array('asc', 'desc'))) {
- return false;
- }
-
- // additional operator
- $params['additional'] = array();
- $params['additional'] = explode(',', $_REQUEST['additional']);
-
- return $params;
- }
-
- /**
- *
- * @return
- * params object - success
- * -1 - bad parameter
- * -2 - smart album not exist
- */
- private function GetParams_Info() {
- $params = array();
- if (!isset($_REQUEST['id'])) {
- return -1;
- }
- $arr = explode(',', $_REQUEST['id']);
- $params['id'] = array();
- foreach ($arr as $smartID) {
- if (!$this->CheckSmartAlbumExist($smartID)) {
- return -2;
- }
- $split = explode('smart_', $smartID);
- array_push($params['id'], $split[1]);
- }
- // additional operator
- $params['additional'] = array();
- $params['additional'] = explode(',', $_REQUEST['additional']);
-
- return $params;
- }
-
- /**
- *
- * @return
- * -1 - bad parameter
- * -2 - smart album not exist
- */
- private function GetPrams_Edit() {
- $params = array();
-
- if (!isset($_REQUEST['id'])) {
- return -1;
- }
- // check smart album exist
- if (!$this->CheckSmartAlbumExist($_REQUEST['id'])) {
- return -2;
- }
- $arr = explode('smart_', $_REQUEST['id']);
- if (2 !== count($arr)) {
- return -1;
- }
- $params['id'] = $arr[1];
- $smartName = @pack('H*', $arr[1]);
- $params['name'] = (isset($_REQUEST['name']) && '' !== $_REQUEST['name']) ? $_REQUEST['name'] : $smartName;
- $params['orig_name'] = $smartName;
-
- // get original smart album setting
- $smartAlbumCond = SmartAlbum::GetSmartAlbumInstance()->GetSmartCondition($smartName);
- // type
- if (isset($_REQUEST['type'])) {
- $params['type'] = $_REQUEST['type'];
- $arr = explode(',', $params['type']);
- $count = 0;
- foreach ($arr as $type) {
- if (!in_array($type, array('photo', 'video'))) {
- return -1;
- }
- $count++;
- }
- if (1 > $count) {
- return -1;
- }
- } else {
- $params['type'] = $smartAlbumCond['type'];
- }
- // albums
- if (isset($_REQUEST['albums'])) {
- if (empty($_REQUEST['albums'])) {
- $params['albums'] = null;
- } else {
- $params['albums'] = $this->ExtractAlbumToArray($_REQUEST['albums']);
- }
- } else {
- $arr = $this->ExtractAlbumToArray($smartAlbumCond['albums']);
- $params['albums'] = empty($arr) ? null : $arr;
- }
- // keyword
- if (isset($_REQUEST['keyword'])) {
- if ('' === $_REQUEST['keyword']) {
- $params['keyword'] = null;
- $params['keyword_op'] = null;
- } else {
- $params['keyword'] = $_REQUEST['keyword'];
- $default = isset($smartAlbumCond['keyword_op']) ? $smartAlbumCond['keyword_op'] : 'any';
- $params['keyword_op'] = (isset($_REQUEST['keyword_op'])) ? $_REQUEST['keyword_op'] : $default;
- if (isset($params['keyword_op']) && !in_array($params['keyword_op'], array('any', 'all', 'exact'))) {
- return -1;
- }
- }
- } else {
- $params['keyword'] = $smartAlbumCond['keyword'];
- $params['keyword_op'] = $smartAlbumCond['keyword_op'];
- }
- // date
- if (isset($_REQUEST['date'])) {
- if (empty($_REQUEST['date'])) {
- $params['date'] = null;
- $params['date_op'] = null;
- } else {
- $default = isset($smartAlbumCond['date_op']) ? $smartAlbumCond['date_op'] : 'taken';
- $params['date_op'] = (isset($_REQUEST['date_op'])) ? $_REQUEST['date_op'] : $default;
- if (in_array($params['date_op'], array('recently_add', 'recently_comment'))) {
- $params['date'] = $_REQUEST['date'];
-
- } else if (in_array($params['date_op'], array('taken', 'upload'))) {
- $dates = explode(',', $_REQUEST['date']);
- try {
- if ($dates[0]) {
- $date1 = new DateTime($dates[0]);
- }
- if ($dates[1]) {
- $date2 = new DateTime($dates[1]);
- }
- } catch (Exception $e) {
- return -1;
- }
- if (($date1 && $date2 && $date2 < $date1) || (!$date1 && !$date2)) {
- return -1;
- }
- $params['date'] = ($date1 ? $date1->format('Y-m-d') : "").",".($date2 ? $date2->format("Y-m-d") : "");
- } else {
- return -1;
- }
- }
- } else {
- $params['date'] = $smartAlbumCond['date'];
- $params['date_op'] = $smartAlbumCond['date_op'];
- }
- // peopel_tag
- if (isset($_REQUEST['people_tag'])) {
- if (empty($_REQUEST['people_tag'])) {
- $params['people_tag'] = null;
- $params['people_tag_op'] = null;
- } else {
- // get people_tag
- $params['people_tag'] = $this->ExtractTagStringToArray($_REQUEST['people_tag']);
- // get people_tag_op
- $default = isset($smartAlbumCond['people_tag_op']) ? $smartAlbumCond['people_tag_op'] : 'all';
- $params['people_tag_op'] = (isset($_REQUEST['people_tag_op'])) ? $_REQUEST['people_tag_op'] : $default;
- if (isset($params['people_tag_op']) && !in_array($params['people_tag_op'], array('all', 'any'))) {
- return -1;
- }
- }
- } else {
- $arr = $this->ExtractTagStringToArray($smartAlbumCond['people_tag']);
- $params['people_tag'] = empty($arr) ? null : $arr;
- $params['people_tag_op'] = $smartAlbumCond['people_tag_op'];
- }
- // geo_tag
- if (isset($_REQUEST['geo_tag'])) {
- if (empty($_REQUEST['geo_tag'])) {
- $params['geo_tag'] = null;
- $params['geo_tag_op'] = null;
- } else {
- // get geo_tag
- $params['geo_tag'] = $this->ExtractTagStringToArray($_REQUEST['geo_tag']);
- // get geo_tag_op
- $default = isset($smartAlbumCond['geo_tag_op']) ? $smartAlbumCond['geo_tag_op'] : 'all';
- $params['geo_tag_op'] = (isset($_REQUEST['geo_tag_op'])) ? $_REQUEST['geo_tag_op'] : $default;
- if (isset($params['geo_tag_op']) && !in_array($params['geo_tag_op'], array('all', 'any'))) {
- return -1;
- }
- }
- } else {
- $arr = $this->ExtractTagStringToArray($smartAlbumCond['geo_tag']);
- $params['geo_tag'] = empty($arr) ? null : $arr;
- $params['geo_tag_op'] = $smartAlbumCond['geo_tag_op'];
- }
- // desc_tag
- if (isset($_REQUEST['desc_tag'])) {
- if (empty($_REQUEST['desc_tag'])) {
- $params['desc_tag'] = null;
- $params['desc_tag_op'] = null;
- } else {
- // get desc_tag
- $params['desc_tag'] = $this->ExtractTagStringToArray($_REQUEST['desc_tag']);
- // get desc_tag_op
- $default = isset($smartAlbumCond['desc_tag_op']) ? $smartAlbumCond['desc_tag_op'] : 'all';
- $params['desc_tag_op'] = (isset($_REQUEST['desc_tag_op'])) ? $_REQUEST['desc_tag_op'] : $default;
- if (isset($params['desc_tag_op']) && !in_array($params['desc_tag_op'], array('all', 'any'))) {
- return -1;
- }
- }
- } else {
- $arr = $this->ExtractTagStringToArray($smartAlbumCond['desc_tag']);
- $params['desc_tag'] = empty($arr) ? null : $arr;
- $params['desc_tag_op'] = $smartAlbumCond['desc_tag_op'];
- }
-
- foreach(SmartAlbum::$exif2IdPrefix as $label => $prefix) {
- $exifParam = $this->GetExifParams_Edit($label, $prefix.'_');
- if (-1 === $exifParam['success']) {
- return -1;
- } else {
- $params[$label] = $exifParam['label'];
- $params[$label.'_op'] = $exifParam['op'];
- }
- }
-
- if ((null === $params['keyword'] || '' === $params['keyword']) && empty($params['date']) && empty($params['people_tag']) && empty($params['geo_tag'])
- && empty($params['desc_tag']) && empty($params['albums']) && empty($params['camera']) && empty($params['model']) && empty($params['exposure']) && empty($params['aperture'])
- && empty($params['iso']) && empty($params['focal']) && empty($params['lens']) && empty($params['flash'])) {
- return -1;
- }
- return $params;
- }
-
- private function GetExifParams_Edit($label, $prefix) {
- $ret['success'] = true;
- $op = $label."_op";
- if (isset($_REQUEST[$label])) {
- if (empty($_REQUEST[$label])) {
- $ret['label'] = null;
- $ret['op'] = null;
- } else {
- $ret['label'] = array();
- $ret['label'] = $this->ExtractExifStringToArray($_REQUEST[$label], $prefix);
- $defalt = isset($smartAlbumCond[$op]) ? $smartAlbumCond[$op] : 'any';
- $ret['op'] = (isset($_REQUEST[$op])) ? $_REQUEST[$op] : $default;
- if (isset($ret['op']) && 'any' !== $ret['op']) {
- $ret['success'] = false;
- }
- }
- } else {
- $arr = $this->ExtractExifStringToArray($smartAlbumCond[$lable]);
- $ret['label'] = empty($arr) ? null : $arr;
- $ret['op'] = $smartAlbumCond[$op];
- }
- return $ret;
- }
-
- private function GetParams_Create() {
- $params = array();
-
- if (!isset($_REQUEST['name']) || !isset($_REQUEST['type'])) {
- return false;
- }
- $params['name'] = $_REQUEST['name'];
- // type
- $params['type'] = $_REQUEST['type'];
- $arr = explode(',', $params['type']);
- $count = 0;
- foreach ($arr as $type) {
- if (!in_array($type, array('photo', 'video'))) {
- return false;
- }
- $count++;
- }
- if (1 > $count) {
- return false;
- }
-
- $albums = (isset($_REQUEST['albums'])) ? $_REQUEST['albums'] : null;
- if (!empty($_REQUEST['albums'])) {
- $params['albums'] = $this->ExtractAlbumToArray($albums);
- }
-
- $params['keyword'] = (isset($_REQUEST['keyword'])) ? $_REQUEST['keyword'] : null;
- if (!empty($_REQUEST['keyword']) || '0' === $_REQUEST['keyword']) {
- $default = 'all';
- $params['keyword_op'] = (isset($_REQUEST['keyword_op'])) ? $_REQUEST['keyword_op'] : $default;
- if (isset($params['keyword_op']) && !in_array($params['keyword_op'], array('any', 'all', 'exact'))) {
- return false;
- }
- }
- $params['date'] = (isset($_REQUEST['date'])) ? $_REQUEST['date'] : null;
- if (!empty($_REQUEST['date'])) {
- $default = 'taken';
- $params['date_op'] = (isset($_REQUEST['date_op'])) ? $_REQUEST['date_op'] : $default;
- if (isset($params['date_op']) && !in_array($params['date_op'], array('taken', 'upload', 'recently_add', 'recently_comment'))) {
- return false;
- }
- }
- $people_tags = (isset($_REQUEST['people_tag'])) ? $_REQUEST['people_tag'] : null;
- if (!empty($_REQUEST['people_tag'])) {
- // get people_tag
- $params['people_tag'] = array();
- $params['people_tag'] = $this->ExtractTagStringToArray($people_tags);
- // get people_tag_op
- $default = 'all';
- $params['people_tag_op'] = (isset($_REQUEST['people_tag_op'])) ? $_REQUEST['people_tag_op'] : $default;
- if (isset($params['people_tag_op']) && !in_array($params['people_tag_op'], array('all', 'any'))) {
- return false;
- }
- }
- $geo_tags = (isset($_REQUEST['geo_tag'])) ? $_REQUEST['geo_tag'] : null;
- if (!empty($_REQUEST['geo_tag'])) {
- // get geo_tag
- $params['geo_tag'] = array();
- $params['geo_tag'] = $this->ExtractTagStringToArray($geo_tags);
- // get geo_tag_op
- $default = 'all';
- $params['geo_tag_op'] = (isset($_REQUEST['geo_tag_op'])) ? $_REQUEST['geo_tag_op'] : $default;
- if (isset($params['geo_tag_op']) && !in_array($params['geo_tag_op'], array('all', 'any'))) {
- return false;
- }
- }
- $desc_tags = (isset($_REQUEST['desc_tag'])) ? $_REQUEST['desc_tag'] : null;
- if (!empty($_REQUEST['desc_tag'])) {
- // get desc_tag
- $params['desc_tag'] = array();
- $params['desc_tag'] = $this->ExtractTagStringToArray($desc_tags);
- // get desc_tag_op
- $default = 'all';
- $params['desc_tag_op'] = (isset($_REQUEST['desc_tag_op'])) ? $_REQUEST['desc_tag_op'] : $default;
- if (isset($params['desc_tag_op']) && !in_array($params['desc_tag_op'], array('all', 'any'))) {
- return false;
- }
- }
- foreach(SmartAlbum::$exif2IdPrefix as $label => $prefix) {
- $value = (isset($_REQUEST[$label])) ? $_REQUEST[$label] : null;
- $op = $label.'_op';
- if (!empty($_REQUEST[$label])) {
- $params[$label] = $this->ExtractExifStringToArray($value, $prefix.'_');
- $default = 'any';
- $params[$op] = (isset($_REQUEST[$op])) ? $_REQUEST[$op] : $default;
- if (isset($params[$op]) && 'any' !== $params[$op]) {
- return false;
- }
- }
- }
-
- if ((null === $params['keyword'] || '' === $params['keyword']) && empty($params['albums']) && empty($params['date']) && empty($params['people_tag'])
- && empty($params['geo_tag']) && empty($params['desc_tag']) && empty($params['camera']) && empty($params['model'])
- && empty($params['exposure']) && empty($params['aperture']) && empty($params['iso']) && empty($params['focal'])
- && empty($params['lens']) && empty($params['flash'])) {
- return false;
- }
-
- return $params;
- }
-
- /**
- *
- * @param
- * $tagIDs - array
- * $type - 0 means people tag
- * 1 means geo tag
- * 2 means desc tag
- */
- private function CheckTagExist($tagIDs, $type) {
- if (!is_array($tagIDs)) {
- return false;
- }
- foreach ($tagIDs as $id) {
- $query = "SELECT count(id) FROM photo_label WHERE id={$id} AND category={$type}";
- $db_result = PHOTO_DB_Query($query);
- $row = PHOTO_DB_FetchRow($db_result);
- if (1 !== (int)$row[0]) {
- return false;
- }
- }
-
- return true;
- }
- private function CheckExifExist($exifNames, $columnName) {
- if (!is_array($exifNames)) {
- return false;
- }
- foreach($exifNames as $exifName) {
- if ('flash_v2' == $columnName) {
- $exifName = str_replace('#', ',', $exifName);
- }
- $query = "SELECT count(". $columnName .") FROM photo_image WHERE ". $columnName ."=?";
- $db_result = PHOTO_DB_Query($query, array($exifName));
- $row = PHOTO_DB_FetchRow($db_result);
- if (1 > (int)$row[0]) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- *
- * @param
- * $smartID - ex: smart_id
- */
- private function CheckSmartAlbumExist($smartID) {
- $arr = explode('smart_', $smartID);
- if (2 !== count($arr)) {
- return false;
- }
- $smartName = @pack('H*', $arr[1]);
- $result = SmartAlbum::GetSmartAlbumInstance()->ReadSmartAlbumFile();
- if (!isset($result['smart_albums'][$smartName])) {
- return false;
- }
- return true;
- }
-
- private function ConvertToSmartExifCondition($data, $label, $op){
- $rule_set['field'] = $label;
- $rule_set['operator'] = $data[$op];
- $strings = implode(',', $data[$label]);
- $rule_set['value'] = $strings;
- return $rule_set;
- }
-
- private function ConvertToSmartCondition($data) {
- $rule_sets = array();
- $rule_sets[0] = array();
-
- $ret['orig_name'] = $data['orig_name'];
- $ret['name'] = $data['name'];
- $ret['desc'] = '';
- $ret['show_photo'] = strstr($data['type'], 'photo') ? true : false;
- $ret['show_video'] = strstr($data['type'], 'video') ? true : false;
-
- if (isset($data['albums'])) {
- $rule_set['field'] = 'albums';
- $rule_set['operator'] = 'any';
- $albumString = implode(',', $data['albums']);
- $rule_set['value'] = $albumString;
- array_push($rule_sets[0], $rule_set);
- }
- if (isset($data['keyword']) && isset($data['keyword_op'])) {
- $rule_set['field'] = 'keyword';
- $rule_set['operator'] = $data['keyword_op'];
- $rule_set['value'] = $data['keyword'];
- array_push($rule_sets[0], $rule_set);
- }
- if (isset($data['date']) && isset($data['date_op'])) {
- $rule_set['field'] = 'date';
- $rule_set['operator'] = $data['date_op'];
- $rule_set['value'] = $data['date'];
- array_push($rule_sets[0], $rule_set);
- }
- if (isset($data['people_tag']) && isset($data['people_tag_op'])) {
- $rule_set['field'] = 'people';
- $rule_set['operator'] = $data['people_tag_op'];
- $peopleString = implode(',', $data['people_tag']);
- $rule_set['value'] = $peopleString;
- array_push($rule_sets[0], $rule_set);
- }
- if (isset($data['geo_tag']) && isset($data['geo_tag_op'])) {
- $rule_set['field'] = 'geo';
- $rule_set['operator'] = $data['geo_tag_op'];
- $geoString = implode(',', $data['geo_tag']);
- $rule_set['value'] = $geoString;
- array_push($rule_sets[0], $rule_set);
- }
- if (isset($data['desc_tag']) && isset($data['desc_tag_op'])) {
- $rule_set['field'] = 'desc';
- $rule_set['operator'] = $data['desc_tag_op'];
- $descString = implode(',', $data['desc_tag']);
- $rule_set['value'] = $descString;
- array_push($rule_sets[0], $rule_set);
- }
- foreach(SmartAlbum::$exif2IdPrefix as $label => $prefix) {
- $op = $label.'_op';
- if (isset($data[$label]) && isset($data[$op])) {
- $rule_set = $this->ConvertToSmartExifCondition($data, $label, $op);
- array_push($rule_sets[0], $rule_set);
- }
- }
-
- $ret['rule_sets'] = $rule_sets;
- return json_encode($ret);
- }
-
- private function FormSmartAlbums($smartNames = false, $params = array()) {
- $smartAlbumList = SmartAlbum::GetSmartAlbumInstance()->ReadSmartAlbumFile();
- $smartAlbumArr = array();
-
- if (empty($smartAlbumList) || !is_array($smartAlbumList['smart_albums'])) {
- return $smartAlbumArr;
- }
- $smartAlbums = $smartAlbumList['smart_albums'];
- if (false != $smartNames) {
- foreach($smartNames as $name) {
- if ($smartAlbums[$name]) {
- $list[$name] = $smartAlbums[$name];
- }
- }
- } else {
- $list = $smartAlbums;
- }
-
- $needThumbSize = in_array('thumb_size', $params['additional']);
- foreach ($list as $key => $item) {
- $item['name'] = (string)$key;
- $smartAlbum = Decorator::SmartFormula($item, array(
- 'param'=> $params,
- 'needThumbSize' => $needThumbSize
- ));
- if (-1 === $smartAlbum) {
- continue;
- }
- $smartAlbumArr[] = $smartAlbum;
- }
- return $smartAlbumArr;
- }
-
- private function SortTitle_ASC($a, $b) {
- if (strtoupper($a['name']) == strtoupper($b['name'])) {
- return 0;
- }
- return (strtoupper($a['name']) < strtoupper($b['name'])) ? -1 : 1;
- }
-
- private function SortTitle_DESC($a, $b) {
- if (strtoupper($a['name']) == strtoupper($b['name'])) {
- return 0;
- }
- return (strtoupper($a['name']) > strtoupper($b['name'])) ? -1 : 1;
- }
-
- private function SortItem($smartAlbums, $sortBy, $sortDirection, $offset, $limit) {
- if ('title' ===$sortBy) {
- if ('asc' === $sortDirection) {
- usort($smartAlbums, 'self::SortTitle_ASC');
- } else {
- usort($smartAlbums, 'self::SortTitle_DESC');
- }
- }
- $cutItemSort = array();
- if (0 > (int)$limit) {
- // array_slice() will reorder and reset the numeric array indices by default.
- // Preserve_keys are set to TRUE to solve bug#2195.
- $cutItemsSort = array_slice($smartAlbums, $offset, NULL, true);
- } else {
- $cutItemsSort = array_slice($smartAlbums, $offset, $limit, true);
- }
- return $cutItemsSort;
- }
-
- private function SmartAlbumList() {
- // Get and check params
- $params = array();
- $params = $this->GetParams_List();
- if (!$params) {
- $this->SetError(WEBAPI_ERR_BAD_REQUEST);
- goto End;
- }
- $resp = array(
- 'total' => 0,
- 'offset' => 0,
- 'smart_albums'=> array()
- );
- $smartAlbumList = SmartAlbum::GetSmartAlbumInstance()->ReadSmartAlbumFile();
- if (empty($smartAlbumList) || !is_array($smartAlbumList['smart_albums'])) {
- goto End;
- }
- $smartAlbums = $smartAlbumList['smart_albums'];
-
- $selectedAlbums = array();
- if ('title' == $params['sort_by']) {
- if ('asc' === $params['sort_direction']) {
- ksort($smartAlbums);
- } else {
- krsort($smartAlbums);
- }
-
- if (0 > (int)$limit) {
- // array_slice() will reorder and reset the numeric array indices by default.
- // Preserve_keys are set to TRUE to solve bug#2195.
- $selectedAlbums = array_slice($smartAlbums, $params['offset'], NULL, true);
- } else {
- $selectedAlbums = array_slice($smartAlbums, $params['offset'], $params['limit'], true);
- }
- }
-
- if (count($selectedAlbums)) {
- $albumNames = array_keys($selectedAlbums);
- } else {
- $albumNames = false;
- }
-
- // Get and form to webapi format
- $smart_albums = array();
- $smart_albums = $this->FormSmartAlbums($albumNames, $params);
- $total = count($smart_albums);
-
- // sort and set offset, limit
- $smartSort = $smart_albums;
- $smartSort = $this->SortItem($smart_albums, $params['sort_by'], $params['sort_direction'], $params['offset'], $params['limit']);
- $resp['total'] = $total;
- $offset = (0 > $params['limit']) ? $resp['total'] : $params['offset'] + $params['limit'];
- $resp['offset'] = ($offset > $total) ? $total : $offset;
- $resp['smart_albums'] = $smartSort;
- End:
- $this->SetResponse($resp);
- return;
- }
-
- private function GetInfo() {
- // Get and check params
- $params = array();
- $params = $this->GetParams_Info();
- if (-1 === $params) {
- $this->SetError(WEBAPI_ERR_BAD_REQUEST);
- goto End;
- } elseif (-2 === $params) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_NOT_EXIST);
- goto End;
- }
-
- // Get and form to webapi format
- $smartNames = array();
- foreach ($params['id'] as $id) {
- $smartName = @pack('H*', $id);
- array_push($smartNames, $smartName);
- }
- $smart_albums = $this->FormSmartAlbums($smartNames, $params);
-
- $resp['smart_albums'] = $smart_albums;
- $this->SetResponse($resp);
- End:
- return;
- }
-
- private function Create() {
- $isAdmin = isset($_SESSION[SYNOPHOTO_ADMIN_USER]['admin_syno_user']);
- if (false === $isAdmin) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_ACCESS_DENY);
- goto End;
- }
-
- // check parameter
- if (false === ($params = $this->GetParams_Create())) {
- $this->SetError(WEBAPI_ERR_BAD_REQUEST);
- goto End;
- }
-
- // check tag_id exist
- if (is_array($params['people_tag']) && !$this->CheckTagExist($params['people_tag'], 0)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_TAG_NOT_EXIST);
- goto End;
- }
- if (is_array($params['geo_tag']) && !$this->CheckTagExist($params['geo_tag'], 1)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_TAG_NOT_EXIST);
- goto End;
- }
- if (is_array($params['desc_tag']) && !$this->CheckTagExist($params['desc_tag'], 2)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_TAG_NOT_EXIST);
- goto End;
- }
- foreach(SmartAlbum::$exif2Column as $label => $column) {
- if (is_array($params[$label]) && !$this->CheckExifExist($params[$label], $column)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_TAG_NOT_EXIST);
- goto End;
- }
- }
-
- // add smart album
- $jsonRuleData = $this->ConvertToSmartCondition($params);
- $jsonRet = SmartAlbum::GetSmartAlbumInstance()->AddSmartAlbum($jsonRuleData);
- $ret = json_decode($jsonRet, true);
- if (false === $ret['success']) {
- if ($ret["error"]["code"] == 103) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_CREATE_FAIL_EXIST);
- } else {
- $this->SetError(PHOTOSTATION_SMARTALBUM_CREATE_FAIL);
- }
- goto End;
- }
-
- $resp['id'] = 'smart_'.bin2hex($params['name']);
- $this->SetResponse($resp);
- End:
- return;
- }
-
- private function Edit() {
- $isAdmin = isset($_SESSION[SYNOPHOTO_ADMIN_USER]['admin_syno_user']);
- if (false === $isAdmin) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_ACCESS_DENY);
- goto End;
- }
- // get parameter
- $params = $this->GetPrams_Edit();
- if (-1 === $params) {
- $this->SetError(WEBAPI_ERR_BAD_REQUEST);
- goto End;
- } elseif (-2 === $params) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_NOT_EXIST);
- goto End;
- }
-
- // check tag_id exist
- if (is_array($params['people_tag']) && !$this->CheckTagExist($params['people_tag'], 0)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_TAG_NOT_EXIST);
- goto End;
- }
- if (is_array($params['geo_tag']) && !$this->CheckTagExist($params['geo_tag'], 1)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_TAG_NOT_EXIST);
- goto End;
- }
- if (is_array($params['desc_tag']) && !$this->CheckTagExist($params['desc_tag'], 2)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_TAG_NOT_EXIST);
- goto End;
- }
- foreach(SmartAlbum::$exif2Column as $label => $column) {
- if (is_array($params[$label]) && !$this->CheckExifExist($params[$label], $column)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_TAG_NOT_EXIST);
- goto End;
- }
- }
-
- // edit smart album
- $jsonRuleData = $this->ConvertToSmartCondition($params);
- $jsonRet = SmartAlbum::GetSmartAlbumInstance()->EditSmartAlbum($jsonRuleData);
- $ret = json_decode($jsonRet, true);
- if (empty($ret) || false === $ret['success']) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_EDIT_FAIL);
- goto End;
- }
-
- // category hook
- if ($params['name'] !== $params['orig_name']) {
- $newID = bin2hex($params['name']);
- $query = "UPDATE category_items SET smart_id='{$newID}' WHERE smart_id='{$params['id']}'";
- if (false === PHOTO_DB_Query($query)) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_EDIT_FAIL);
- goto End;
- }
- }
-
- $resp['id'] = 'smart_'.bin2hex($params['name']);
- $this->SetResponse($resp);
- End:
- return;
- }
-
- private function Delete() {
- $isAdmin = isset($_SESSION[SYNOPHOTO_ADMIN_USER]['admin_syno_user']);
- if (false === $isAdmin) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_ACCESS_DENY);
- goto End;
- }
- // Get and check params
- $params = $this->GetParams_Info();
- if (-1 === $params) {
- $this->SetError(WEBAPI_ERR_BAD_REQUEST);
- goto End;
- } elseif (-2 === $params) {
- $this->SetError(PHOTOSTATION_SMARTALBUM_NOT_EXIST);
- goto End;
- }
-
- // get smart album names
- $smartNames = array();
- foreach ($params['id'] as $id) {
- $smartName = @pack('H*', $id);
- array_push($smartNames, $smartName);
- }
-
- // delete
- foreach ($smartNames as $smartName) {
- $data['name'] = $smartName;
- $jsonData = json_encode($data);
- SmartAlbum::GetSmartAlbumInstance()->DeleteSmartAlbum($jsonData);
-
- // category hook
- $id = bin2hex($smartName);
- $query = "DELETE FROM category_items WHERE smart_id='{$id}'";
- PHOTO_DB_Query($query);
- }
-
-
-
- End:
- return;
- }
- }
-
- $api = new SmartAlbumAPI();
- $api->Run();
-
- ?>
|