function execute(&$vars, &$response)
 {
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     $error =& $response->getError();
     if ($this->execute_without_transaction($vars, $error)) {
         $transaction->commit();
         $response->setResult(true);
     } else {
         $transaction->rollback();
         $response->setResult(false);
     }
 }
 function execute(&$vars, &$response)
 {
     global $xoopsUser;
     $success = array();
     $error = false;
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     $itemtype_handler =& xoonips_getormhandler('xoonips', 'item_type');
     foreach (array_keys($vars[0]) as $key) {
         assert(!($vars[0][$key]->getImportAsNewFlag() && $vars[0][$key]->getUpdateFlag()));
         //skip this item if don't import as new and update
         if (!$vars[0][$key]->getImportAsNewFlag() && !$vars[0][$key]->getUpdateFlag()) {
             continue;
         }
         $item_handler =& xoonips_getormcompohandler('xoonips', 'item');
         if ($vars[0][$key]->getUpdateFlag() && !$item_handler->getPerm($vars[0][$key]->getUpdateItemId(), $xoopsUser->getVar('uid'), 'write')) {
             //no write permission to updating exist item -> error
             $vars[0][$key]->setErrors(E_XOONIPS_UPDATE_CERTIFY_REQUEST_LOCKED, "can't update locked item(" . $vars[0][$key]->getUpdateItemId() . ")");
             $error = true;
             break;
         }
         $basic =& $vars[0][$key]->getVar('basic');
         $itemtype =& $itemtype_handler->get($basic->get('item_type_id'));
         $handler =& xoonips_gethandler($itemtype->get('name'), 'import_item');
         $handler->import($vars[0][$key]);
         $error = $error || count($vars[0][$key]->getErrors()) > 0;
     }
     if ($error) {
         $transaction->rollback();
     } else {
         foreach (array_keys($vars[0]) as $key) {
             $basic =& $vars[0][$key]->getVar('basic');
             $itemtype =& $itemtype_handler->get($basic->get('item_type_id'));
             $handler =& xoonips_gethandler($itemtype->get('name'), 'import_item');
             $handler->onImportFinished($vars[0][$key], $vars[0]);
             $error = $error || count($vars[0][$key]->getErrors()) > 0;
         }
         $transaction->commit();
         $this->_remove_files();
     }
     $success['import_items'] =& $vars[0];
     $response->setResult(!$error);
     $response->setSuccess($success);
 }
 /**
  * create root index
  *
  * @access private
  * @param string $title index title
  * @param bool $is_user true: for user index, false: for group index
  * @param int $ugid user id or group id
  * @return int created index id, false if failure
  */
 function _createRootIndex($title, $is_user, $ugid)
 {
     // transaction
     require_once XOOPS_ROOT_PATH . '/modules/xoonips/class/base/transaction.class.php';
     $transaction =& XooNIpsTransaction::getInstance();
     $transaction->start();
     // create item basic
     $ib_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $ib_obj =& $ib_handler->create();
     $ib_obj->set('item_type_id', ITID_INDEX);
     if ($is_user) {
         $ib_obj->set('uid', $ugid);
     } else {
         // uid is session owner for group index
         $uid = UID_GUEST;
         if (isset($GLOBALS['xoopsUser']) && is_object($GLOBALS['xoopsUser'])) {
             $uid = intval($GLOBALS['xoopsUser']->getVar('uid', 'n'));
         }
         $ib_obj->set('uid', $uid);
     }
     if (!$ib_handler->insert($ib_obj)) {
         $transaction->rollback();
         return false;
     }
     // create item title
     $it_handler =& xoonips_getormhandler('xoonips', 'title');
     $item_id = $ib_obj->getVar('item_id', 'n');
     $it_obj =& $it_handler->create();
     $it_obj->set('item_id', $item_id);
     $it_obj->set('title_id', DEFAULT_INDEX_TITLE_OFFSET);
     $it_obj->set('title', $title);
     if (!$it_handler->insert($it_obj)) {
         $transaction->rollback();
         return false;
     }
     // get sort number
     $sort_number = 0;
     if ($is_user) {
         // for user index
         $criteria = new CriteriaCompo(new Criteria('parent_index_id', IID_ROOT));
         $criteria->add(new Criteria('open_level', OL_PRIVATE));
         if ($this->getCount($criteria) == 0) {
             // first creation
             // this value will be used for admin on install xoonips module
             $sort_number = 2147483647;
         } else {
             $idx_objs =& $this->getObjects($criteria, false, 'MIN(sort_number) AS min_value');
             $idx_obj =& $idx_objs[0];
             $sort_number = intval($idx_obj->getExtraVar('min_value')) - 1;
             unset($idx_objs);
             unset($idx_obj);
         }
     } else {
         // for group index
         $criteria = new CriteriaCompo(new Criteria('parent_index_id', IID_ROOT));
         $criteria2 = new CriteriaCompo();
         $criteria2->add(new Criteria('open_level', OL_PUBLIC));
         $criteria2->add(new Criteria('open_level', OL_GROUP_ONLY), 'OR');
         $criteria->add($criteria2);
         if ($this->getCount($criteria) == 0) {
             // not reaeched
             $transaction->rollback();
             return false;
         } else {
             $idx_objs =& $this->getObjects($criteria, false, 'MAX(sort_number) AS max_value');
             $idx_obj =& $idx_objs[0];
             $sort_number = intval($idx_obj->getExtraVar('max_value')) + 1;
             unset($idx_objs);
             unset($idx_obj);
         }
     }
     // create index
     $idx_obj =& $this->create();
     $idx_obj->set('index_id', $item_id);
     $idx_obj->set('parent_index_id', IID_ROOT);
     if ($is_user) {
         $idx_obj->set('uid', $ugid);
         $idx_obj->set('open_level', OL_PRIVATE);
     } else {
         $idx_obj->set('gid', $ugid);
         $idx_obj->set('open_level', OL_GROUP_ONLY);
     }
     $idx_obj->set('sort_number', $sort_number);
     if (!$this->insert($idx_obj)) {
         $transaction->rollback();
         return false;
     }
     $transaction->commit();
     return $idx_obj->getVar('index_id', 'n');
 }
 /**
  * unlock rankings table for update
  *
  * @access protected
  * @param int $timeout timeout time
  * @return bool false if failure
  */
 function _unlock($timeout)
 {
     // transaction
     $transaction =& XooNIpsTransaction::getInstance();
     $transaction->commit();
     $old_timeout = $this->_get_config('lock_timeout');
     if ($old_timeout != $timeout) {
         return false;
     }
     $this->_set_config('lock_timeout', 0);
     return true;
 }
 /**
  * execute login
  *
  * @param[in] $vars[0] id (use '' if guest login)
  * @param[in] $vars[1] pass (use '' if guest login)
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error error information
  * @param[out] $response->success session id
  * @return false if error
  */
 function execute(&$vars, &$response)
 {
     $myxoopsConfig =& xoonips_get_xoops_configs(XOOPS_CONF);
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 2) {
         $error->add(XNPERR_EXTRA_PARAM);
     }
     if (count($vars) < 2) {
         $error->add(XNPERR_MISSING_PARAM);
     }
     //
     if (isset($vars[0]) && strlen($vars[0]) > 25) {
         $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
     }
     //
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return;
     } else {
         $id = $vars[0];
         $pass = $vars[1];
     }
     $member_handler =& xoonips_gethandler('xoonips', 'member');
     $user_handler =& xoonips_getormhandler('xoonips', 'users');
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     $xconfig_handler =& xoonips_getormhandler('xoonips', 'config');
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     if ($id == '') {
         $target_user = $xconfig_handler->getValue(XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_KEY);
         if ($pass != '' || $target_user != XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_ALL) {
             $transaction->rollback();
             $response->error->add(XNPERR_AUTH_FAILURE);
             $response->setResult(false);
             return false;
         }
         $user = false;
         $uid = UID_GUEST;
         $groups = array();
     } else {
         $user =& $member_handler->loginUser($id, $pass);
         if (!$user) {
             $transaction->rollback();
             // insert login failure event
             if (!$eventlog_handler->recordLoginFailureEvent($id)) {
                 $response->error->add(XNPERR_SERVER_ERROR, "cannot insert event");
             }
             // return error
             $response->error->add(XNPERR_AUTH_FAILURE);
             $response->setResult(false);
             return false;
         }
         $xoonips_user = $user->getVar('xoonips_user');
         $uid = $xoonips_user->get('uid');
         $xoops_user_handler =& xoops_gethandler('user');
         $xoops_user = $xoops_user_handler->get($uid);
         if (0 == $xoops_user->getVar('level', 'n') || !$xoonips_user->get('activate')) {
             // not activated, not certified
             // return error
             $transaction->rollback();
             $response->error->add(XNPERR_AUTH_FAILURE);
             $response->setResult(false);
             return false;
         }
         $groups = $xoops_user->getGroups();
     }
     if ($myxoopsConfig['closesite'] == 1) {
         $allowed = false;
         if ($user) {
             foreach ($groups as $group) {
                 if (in_array($group, $myxoopsConfig['closesite_okgrp']) || XOOPS_GROUP_ADMIN == $group) {
                     $allowed = true;
                     break;
                 }
             }
         }
         if (!$allowed) {
             // site closed
             $transaction->rollback();
             $response->error->add(XNPERR_AUTH_FAILURE);
             $response->setResult(false);
             return false;
         }
     }
     // remove expired xoonips sessions
     $session_handler =& xoonips_getormhandler('xoonips', 'session');
     if (!$session_handler->gcSession()) {
         $transaction->rollback();
         $response->error->add(XNPERR_SERVER_ERROR, "failed to gc session");
         $response->setResult(false);
         return false;
     }
     // record $uid
     $_SESSION = array();
     $_SESSION['xoopsUserId'] = $uid;
     $_SESSION['xoopsUserGroups'] = $groups;
     // set XNPSID(for old routines)
     $_SESSION['XNPSID'] = $uid == UID_GUEST ? SID_GUEST : session_id();
     if ($user) {
         // update last_login
         $xoops_user->setVar('last_login', time());
         if (!$xoops_user_handler->insert($xoops_user)) {
         }
         // init xoonips_session
         $session_handler->initSession($uid);
         // insert login event
         $eventlog_handler->recordLoginSuccessEvent($uid);
     }
     $transaction->commit();
     $response->setSuccess(session_id());
     $response->setResult(true);
     return true;
 }
function uncertify($to_index_ids, $group_index_id)
{
    // transaction
    require_once __DIR__ . '/class/base/transaction.class.php';
    $transaction =& XooNIpsTransaction::getInstance();
    $transaction->start();
    $index_group_index_link_handler =& xoonips_getormhandler('xoonips', 'index_group_index_link');
    foreach ($to_index_ids as $to_index_id) {
        if (!$index_group_index_link_handler->rejectMakePublic($to_index_id, array($group_index_id))) {
            $transaction->rollback();
            redirect_header(XOOPS_URL . '/', 3, _MD_XOONIPS_GROUP_TREE_TO_PUBLIC_INDEX_TREE_FAILED);
            exit;
        }
    }
    $index_group_index_link_handler->notifyMakePublicGroupIndex($to_index_ids, array($group_index_id), 'group_item_rejected');
    $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
    $eventlog_handler->recordRejectGroupIndexEvent($group_index_id);
    $transaction->commit();
    redirect_header(XOOPS_URL . '/modules/xoonips/groupcertify.php', 3, 'Succeed');
}
 /**
  * execute updateFile
  *
  * @param[in]  $vars[0] session ID
  * @param[in]  $vars[1] item ID
  * @param[in]  $vars[2] identifier type of $vars[1] parameter('item_id'|'ext_id')
  * @param[in]  $vars[3] field name to add/update file
  * @param[in]  $vars[4] XooNIpsFile file information
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error  error information
  * @param[out] $response->success new file id
  */
 function execute(&$vars, &$response)
 {
     /*
     check permission
     check field name
     check filesize <= upload_max_filesize
     get XooNIpsFile if file_id exist ($oldfile)
     create XooNIpsFile from item_id and file information ($newfile)
     start transaction
     insert $newfile
     create search_text and update xoonips_file table
     if oldfile exists, set is_deleted of oldfile to 1
     update certify_state, notify certify_required, auto_certify, update item_status, udpate RSS
     commit
     delete oldfile
     */
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 5) {
         $error->add(XNPERR_EXTRA_PARAM);
     } else {
         if (count($vars) < 5) {
             $error->add(XNPERR_MISSING_PARAM);
         } else {
             if (isset($vars[0]) && strlen($vars[0]) > 32) {
                 $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
             }
             if ($vars[2] != 'item_id' && $vars[2] != 'ext_id') {
                 $error->add(XNPERR_INVALID_PARAM, 'invalid parameter 3');
             }
             if ($vars[2] == 'item_id' && !is_int($vars[1]) && !ctype_digit($vars[1])) {
                 $error->add(XNPERR_INVALID_PARAM, 'not integer parameter 2');
             }
             if ($vars[2] == 'item_id' && strlen($vars[1]) > 10) {
                 $error->add(XNPERR_INVALID_PARAM, 'too long parameter 2');
             }
             // file size check
             $upload_max_filesize = $this->returnBytes(ini_get('upload_max_filesize'));
             if (filesize($vars[4]->getFilepath()) > $upload_max_filesize) {
                 $error->add(XNPERR_INVALID_PARAM, 'too large file');
             }
             $vars[4]->set('file_size', filesize($vars[4]->getFilepath()));
         }
     }
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return false;
     } else {
         $sessionid = $vars[0];
         $id = $vars[1];
         $id_type = $vars[2];
         $field_name = $vars[3];
         $file = $vars[4];
     }
     list($result, $uid, $session) = $this->restoreSession($sessionid);
     if (!$result) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_SESSION);
         return false;
     }
     // get item and item_id
     $item_handler =& xoonips_getormcompohandler('xoonips', 'item');
     $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     if ($id_type == 'item_id') {
         $item = $item_handler->get($id);
     } else {
         if ($id_type == 'ext_id') {
             if (strlen($id) == 0) {
                 $response->setResult(false);
                 $error->add(XNPERR_INVALID_PARAM, "ext_id is empty");
                 return false;
             } else {
                 $basics =& $item_basic_handler->getObjects(new Criteria('doi', addslashes($id)));
                 if (false === $basics) {
                     $response->setResult(false);
                     $error->add(XNPERR_SERVER_ERROR, "cannot get basic information");
                     return false;
                 } else {
                     if (count($basics) >= 2) {
                         $response->setResult(false);
                         $error->add(XNPERR_SERVER_ERROR, "ext_id is duplicated");
                         return false;
                     } else {
                         if (count($basics) == 1) {
                             $item = $item_handler->get($basics[0]->get('item_id'));
                         } else {
                             $item = false;
                         }
                     }
                 }
             }
         }
     }
     if (!$item) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND, "id={$id}");
         return false;
     }
     $basic = $item->getVar('basic');
     $item_id = $basic->get('item_id');
     // can modify?
     if (!$item_handler->getPerm($item_id, $uid, 'write')) {
         $item_lock_handler =& xoonips_getormhandler('xoonips', 'item_lock');
         $response->setResult(false);
         if ($item_lock_handler->isLocked($item_id)) {
             $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot update file because item is " . $this->getLockTypeString($item_lock_handler->getLockType($item_id)));
         } else {
             $error->add(XNPERR_ACCESS_FORBIDDEN);
         }
         return false;
     }
     // item -> itemtype, detail_item_type
     $item_type_handler =& xoonips_getormhandler('xoonips', 'item_type');
     $item_type = $item_type_handler->get($basic->get('item_type_id'));
     if (!$item_type) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get itemtype of that item");
         return false;
     }
     $detail_item_type_handler =& xoonips_getormhandler($item_type->getVar('name'), 'item_type');
     $detail_item_type = $detail_item_type_handler->get($item_type->getVar('item_type_id'));
     if (!$detail_item_type) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get detail itemtype of that item");
         return false;
     }
     // is file_type_id proper?
     $file_type_handler =& xoonips_getormhandler('xoonips', 'file_type');
     $file_type_id = $file->getVar('file_type_id');
     $file_type = $file_type_handler->get($file_type_id);
     if (!$file_type) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "bad filetype({$file_type_id})");
         return false;
     }
     $file_type_name = $file_type->getVar('name');
     if (!in_array($file_type_name, $detail_item_type->getFileTypeNames())) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_PARAM, "bad filetype({$file_type_name})");
         return false;
     }
     // add file to non-multiple field?
     $file_handler =& xoonips_getormhandler('xoonips', 'file');
     if (!$detail_item_type->getMultiple($file_type_name) && !$file->getVar('file_id')) {
         $c = new CriteriaCompo();
         $c->add(new Criteria('item_id', $item_id));
         $c->add(new Criteria('file_type_id', $file_type_id));
         $c->add(new Criteria('is_deleted', 0));
         $same_file_num = $file_handler->getCount($c);
         if ($same_file_num > 0) {
             // already file exists
             $response->setResult(false);
             $error->add(XNPERR_INVALID_PARAM, "multiple file not allowed for {$file_type_name}");
             return false;
         }
     }
     // get oldfile from oldfile_id
     $oldfile_id = $file->getVar('file_id');
     if ($oldfile_id) {
         $oldfile = $file_handler->get($oldfile_id);
         if ($oldfile->getVar('is_deleted')) {
             $response->setResult(false);
             $error->add(XNPERR_INVALID_PARAM, "cannot update deleted file(file_id={$oldfile_id})");
             return false;
         } else {
             if ($oldfile->getVar('item_id') != $item_id) {
                 $response->setResult(false);
                 $error->add(XNPERR_INVALID_PARAM, "item(item_id={$item_id}) does not have that file(file_id={$oldfile_id})");
                 return false;
             }
             // check if $oldfile.item_type_id == $file.item_type_id
             $file_type_handler =& xoonips_getormhandler('xoonips', 'file_type');
             $file_type = $file_type_handler->get($oldfile->getVar('file_type_id'));
             if (!$file_type) {
                 $response->setResult(false);
                 $error->add(XNPERR_SERVER_ERROR, "old file has unknown file type id");
                 return false;
             }
             if ($file_type->getVar('name') != $file_type_name) {
                 $response->setResult(false);
                 $error->add(XNPERR_INVALID_PARAM, "cannot change filetype");
                 return false;
             }
         }
     } else {
         $oldfile = false;
     }
     // check item storage limit(private/group)
     $old_size = $this->getSizeOfItem($item);
     if ($oldfile) {
         $new_size = $old_size - $oldfile->getVar('file_size') + $file->getVar('file_size');
     } else {
         $new_size = $old_size + $file->getVar('file_size');
     }
     if (!$this->isEnoughSpace($error, $uid, $new_size, $item->getVar('indexes'), $old_size, $item->getVar('indexes'))) {
         $response->setResult(false);
         //$error->add(XNPERR_SERVER_ERROR, "not enough disk space"); // isEnoughSpace() adds errors.
         return false;
     }
     // set mime-type, thumbnail
     $file->setVar('mime_type', $this->guessMimeType($file));
     if ($detail_item_type->getPreviewFileName() == $file_type_name) {
         if (!$this->createThumbnail($error, $file)) {
             $response->setResult(false);
             return false;
         }
     }
     $data = file_get_contents($file->getFilepath());
     if ($data === false) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get file content");
         return false;
     }
     $file->setVar('file_id', null);
     $file->setVar('item_id', $item_id);
     $file->setNew();
     // start transaction
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     // insert $file
     if (!$file_handler->insert($file)) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot insert file");
         return false;
     }
     // create file search_text
     $admin_file_handler =& xoonips_gethandler('xoonips', 'admin_file');
     $admin_file_handler->updateFileSearchText($file->get('file_id'), true);
     if ($oldfile) {
         // set oldfile.is_deleted = 1;
         $oldfile->setVar('is_deleted', 1);
         if (!$file_handler->insert($oldfile)) {
             $transaction->rollback();
             $response->setResult(false);
             $error->add(XNPERR_SERVER_ERROR, "cannot update old file");
             return false;
         }
         // delete search_text of old file
         $search_text_handler =& xoonips_getormhandler('xoonips', 'search_text');
         $search_text = $search_text_handler->get($oldfile->get('file_id'));
         if ($search_text && !$search_text_handler->delete($search_text)) {
             $transaction->rollback();
             $response->setResult(false);
             $error->add(XNPERR_INVALID_PARAM, "cannot remove search text");
             return false;
         }
     }
     // event log ( update item )
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     if (!$eventlog_handler->recordUpdateItemEvent($item_id)) {
         $error->add(XNPERR_SERVER_ERROR, "cannot insert event");
         $response->setResult(false);
         return false;
     }
     // item insert/update/certify_required/certified event, change certify_state, send notification, update RSS, update item_status.
     if (!$this->touchItem($error, $item, true)) {
         $transaction->rollback();
         $response->setResult(false);
         return false;
     }
     // commit
     $transaction->commit();
     // unlink old file
     if ($oldfile) {
         $file_handler->deleteFile($oldfile);
     }
     // return
     $response->setSuccess($file->getVar('file_id'));
     $response->setResult(true);
     return true;
 }
 /**
  * execute putItem
  *
  * @param[in]  $vars[0] session ID
  * @param[in]  $vars[1] XooNIpsItemCompo item information
  * @param[in]  array $vars[2] XooNIpsFile[] meta information of attachment files
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error  error information
  * @param[out] $response$response->success item id of registered item
  */
 function execute(&$vars, &$response)
 {
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 3) {
         $error->add(XNPERR_EXTRA_PARAM);
     } else {
         if (count($vars) < 3) {
             $error->add(XNPERR_MISSING_PARAM);
         } else {
             if (isset($vars[0]) && strlen($vars[0]) > 32) {
                 $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
             }
             // file size check
             if (is_array($vars[2])) {
                 $upload_max_filesize = $this->returnBytes(ini_get('upload_max_filesize'));
                 for ($i = 0; $i < count($vars[2]); $i++) {
                     if (filesize($vars[2][$i]->getFilepath()) > $upload_max_filesize) {
                         $error->add(XNPERR_INVALID_PARAM, 'too large file(file_id=' . $vars[2][$i]->get('file_id') . ')');
                     }
                     $vars[2][$i]->set('file_size', filesize($vars[2][$i]->getFilepath()));
                 }
             }
         }
     }
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return;
     } else {
         $response->setResult(false);
         $sessionid = $vars[0];
         $item = $vars[1];
         $files = $vars[2];
     }
     list($result, $uid, $session) = $this->restoreSession($sessionid);
     if (!$result) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_SESSION);
         return false;
     }
     if ($uid == UID_GUEST) {
         $response->setResult(false);
         $error->add(XNPERR_ACCESS_FORBIDDEN, "guest user cannot putItem");
         // test E4
         return false;
     }
     // start transaction
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     // set uid, title_id, keyword_id, creation_date, last_update_date
     $now = time();
     $basic = $item->getVar('basic');
     $basic->setVar('uid', $uid, true);
     $basic->setVar('creation_date', $now, true);
     $basic->setVar('last_update_date', $now, true);
     $item->setVar('basic', $basic);
     $titles = $item->getVar('titles');
     for ($i = 0; $i < count($titles); $i++) {
         $titles[$i]->setVar('title_id', $i, true);
     }
     $item->setVar('titles', $titles);
     $keywords = $item->getVar('keywords');
     for ($i = 0; $i < count($keywords); $i++) {
         $keywords[$i]->setVar('keyword_id', $i, true);
     }
     $item->setVar('keywords', $keywords);
     // ext_id duplicated?
     $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $ext_id = $basic->get('doi');
     if (strlen($ext_id)) {
         $basics =& $item_basic_handler->getObjects(new Criteria('doi', addslashes($ext_id)));
         if (count($basics)) {
             $transaction->rollback();
             $response->setResult(false);
             $error->add(XNPERR_INCOMPLETE_PARAM, "{$ext_id} is duplicated");
             return false;
         }
     }
     //          // filled?
     //          $missing = array();
     //          if (!$item->isFilledRequired($missing)) {
     //              $response->setResult(false);
     //              $error->add(XNPERR_INCOMPLETE_PARAM); // test E3
     //              return false;
     //          }
     // can access that indexes? calculate get add_to_private, add_to_gids, add_to_public
     $index_handler =& xoonips_getormhandler('xoonips', 'index');
     $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
     $index_item_links = $item->getVar('indexes');
     $add_to_private = false;
     $add_to_gids = array();
     $add_to_public = false;
     foreach ($index_item_links as $index_item_link) {
         $index_id = $index_item_link->get('index_id');
         $index = $index_handler->get($index_id);
         if (!$index) {
             $error->add(XNPERR_INVALID_PARAM, "no such index(index_id={$index_id})");
             // test e5
         } else {
             if (!$index_handler->getPerm($index_id, $uid, 'read')) {
                 $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot access index(index_id={$index_id})");
                 // test e5
             } else {
                 $open_level = $index->get('open_level');
                 if ($open_level == OL_PRIVATE) {
                     $add_to_private = true;
                 } else {
                     if ($open_level == OL_GROUP_ONLY) {
                         $add_to_gids[$index->get('gid')] = true;
                     } else {
                         if ($open_level == OL_PUBLIC) {
                             $add_to_public = true;
                         }
                     }
                 }
             }
         }
     }
     $add_to_gids = array_keys($add_to_gids);
     // error if no private index is selected.
     if (!$add_to_private) {
         $error->add(XNPERR_INVALID_PARAM, "select at least 1 private index");
         // test e5
     }
     // item_type_id -> item_type_name, detail_item_type_handler, detail_item_type, detail_item_handler
     $item_type_handler =& xoonips_getormhandler('xoonips', 'item_type');
     $basic = $item->getVar('basic');
     $item_type_id = $basic->get('item_type_id');
     $item_type = $item_type_handler->get($item_type_id);
     if (!$item_type) {
         $response->setResult(false);
         $transaction->rollback();
         $error->add(XNPERR_INVALID_PARAM, "bad itemtype(item_type_id={$item_type_id})");
         // test E6*
         return false;
     }
     $item_type_name = $item_type->get('name');
     $detail_item_type_handler =& xoonips_getormhandler($item_type_name, 'item_type');
     if (!$detail_item_type_handler) {
         $response->setResult(false);
         $transaction->rollback();
         $error->add(XNPERR_SERVER_ERROR, "cannot get item type handler(item_type_id={$item_type_id})");
         // test E7*
         return false;
     }
     $detail_item_type = $detail_item_type_handler->get($item_type_id);
     if (!$detail_item_type) {
         $response->setResult(false);
         $transaction->rollback();
         $error->add(XNPERR_SERVER_ERROR, "cannot get item type(item_type_id={$item_type_id})");
         // test E8*
         return false;
     }
     $detail_item_handler =& xoonips_getormcompohandler($item_type_name, 'item');
     // can access that related_tos?
     $related_tos = $item->getVar('related_tos');
     foreach ($related_tos as $related_to) {
         $item_id = $related_to->get('item_id');
         $item_basic = $item_basic_handler->get($item_id);
         if (!$item_basic) {
             $error->add(XNPERR_INVALID_PARAM, "no such related_to(item_id={$item_id})");
             // test e5
         } else {
             if (!$detail_item_handler->getPerm($item_id, $uid, 'read')) {
                 $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot access related_tos(item_id={$item_id})");
                 // test e5
             }
         }
     }
     if ($error->get()) {
         $transaction->rollback();
         $response->setResult(false);
         return false;
     }
     // error if add to public/group and no rights/readme input
     if ($add_to_public || count($add_to_gids)) {
         if (!$detail_item_handler->isValidForPubicOrGroupShared($item)) {
             $response->setResult(false);
             $error->add(XNPERR_INCOMPLETE_PARAM, "item cannot be public nor group-shared");
             return false;
         }
     }
     if (!$this->isPublicationDateValid($response, $basic->get('publication_year'), $basic->get('publication_month'), $basic->get('publication_mday'), $detail_item_type->getRequired('publication_year'), $detail_item_type->getRequired('publication_month'), $detail_item_type->getRequired('publication_mday'))) {
         $response->setResult(false);
         return false;
     }
     // item number/storage limit check
     $size = 0;
     foreach ($files as $file) {
         $size += $file->get('file_size');
     }
     if (!$this->isEnoughSpace($error, $uid, $size, $item->getVar('indexes'))) {
         $transaction->rollback();
         $response->setResult(false);
         // test E11
         return false;
     }
     // get filetypes
     $file_type_handler =& xoonips_getormhandler('xoonips', 'file_type');
     $file_types =& $file_type_handler->getObjects(null, true);
     // $item->setVar( '...', $files[...] ) // check exactly 1:1 ? correct filetype ? multiple files for non-multiple field?
     $pseudo2files = array();
     for ($i = 0; $i < count($files); $i++) {
         $pseudo_file_id = $files[$i]->get('file_id');
         if (isset($pseudo2files[$pseudo_file_id])) {
             $error->add(XNPERR_INVALID_PARAM, "pseudo file_id conflicts(pseudo file_id={$pseudo_file_id})");
             // test e12
         } else {
             $pseudo2files[$pseudo_file_id] = array('used' => false, 'file' => $files[$i]);
         }
     }
     foreach ($detail_item_type->getFileTypeNames() as $field_name) {
         $detail_files = $item->getVar($field_name);
         if (!$detail_item_type->getMultiple($field_name)) {
             $detail_file = $detail_files;
             if ($detail_file->get('file_id') == 0) {
                 $item->setVar($field_name, false);
                 continue;
                 // this filetype maybe optional and omitted.
             }
             $detail_files = array($detail_file);
         }
         for ($i = 0; $i < count($detail_files); $i++) {
             $pseudo_id = $detail_files[$i]->get('file_id');
             if ($pseudo_id == 0) {
                 unset($detail_files[$i]);
                 continue;
             }
             if (!isset($pseudo_id) || !isset($pseudo2files[$pseudo_id])) {
                 $error->add(XNPERR_INVALID_PARAM, "unknown pseudo file_id in {$field_name}(pseudo file_id={$pseudo_id})");
                 // test e12
             } else {
                 if ($pseudo2files[$pseudo_id]['used']) {
                     $error->add(XNPERR_INVALID_PARAM, "file referred twice(pseudo file_id={$pseudo_id})");
                     // test e12
                 } else {
                     $pseudo2files[$pseudo_id]['used'] = true;
                     $file_type_id = $pseudo2files[$pseudo_id]['file']->get('file_type_id');
                     if (!isset($file_types[$file_type_id])) {
                         $error->add(XNPERR_INVALID_PARAM, "bad filetype(file_type_id={$file_type_id} pseudo file_id={$pseudo_id})");
                         // test e12
                     } else {
                         if ($file_types[$file_type_id]->get('name') != $field_name) {
                             $error->add(XNPERR_INVALID_PARAM, "filetype not match(pseudo file_id={$pseudo_id})");
                             // test e12
                         } else {
                             $detail_files[$i] = $pseudo2files[$pseudo_id]['file'];
                             $detail_files[$i]->setVar('mime_type', $this->guessMimeType($detail_files[$i]), true);
                             if ($field_name == $detail_item_type->getPreviewFileName()) {
                                 $this->createThumbnail($error, $detail_files[$i]);
                             }
                         }
                     }
                 }
             }
         }
         if ($detail_item_type->getMultiple($field_name)) {
             $item->setVar($field_name, $detail_files);
         } else {
             if (count($detail_files)) {
                 if (count($detail_files) >= 2) {
                     $error->add(XNPERR_INVALID_PARAM, "too many files for {$field_name}");
                     // test e12
                 }
                 $item->setVar($field_name, $detail_files[0]);
             }
         }
     }
     if (count($pseudo2files) != 0) {
         foreach ($pseudo2files as $pseudo_id => $info) {
             if ($info['used'] == false) {
                 $error->add(XNPERR_INVALID_PARAM, "redundant file(pseudo file_id={$pseudo_id})");
                 // test e12
             }
         }
     }
     if ($error->get()) {
         $transaction->rollback();
         $response->setResult(false);
         return false;
     }
     // insert item
     $detail_item_handler->setNew($item);
     $detail_item_handler->setDirty($item);
     if (!$detail_item_handler->insert($item)) {
         $error->add(XNPERR_SERVER_ERROR, "cannot insert item");
         // test E13
         $transaction->rollback();
         $response->setResult(false);
         return false;
     }
     $detail_item_handler->unsetNew($item);
     $basic = $item->getVar('basic');
     $item_id = $basic->get('item_id');
     // event log ( insert item )
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     if (!$eventlog_handler->recordInsertItemEvent($item_id)) {
         $transaction->rollback();
         $error->add(XNPERR_SERVER_ERROR, "cannot insert event");
         // test E14
         return false;
     }
     // item insert/update/certify_required/certified event, change certify_state, update item_status.
     if (!$this->touchItem1($error, $item, $uid)) {
         $transaction->rollback();
         $response->setResult(false);
         // test E16
         return false;
     }
     // commit
     $transaction->commit();
     // update search_text
     $file_handler =& xoonips_getormhandler('xoonips', 'file');
     $files =& $file_handler->getObjects(new Criteria('item_id', $item_id));
     if (false === $files) {
         $transaction->rollback();
         $error->add(XNPERR_SERVER_ERROR, "cannot get file");
         return false;
     } else {
         $admin_file_handler =& xoonips_gethandler('xoonips', 'admin_file');
         foreach ($files as $file) {
             $admin_file_handler->updateFileSearchText($file->get('file_id'), true);
         }
     }
     // notification and rss
     $this->touchItem2($error, $item, $uid);
     $response->setSuccess($item_id);
     $response->setResult(true);
     return true;
 }
 /**
  * @param[in]  $vars[0] session ID
  * @param[in]  $vars[1] XooNIpsItem item information
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error  error information
  * @param[out] $response->success item id of updated item
  */
 function execute(&$vars, &$response)
 {
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 2) {
         $error->add(XNPERR_EXTRA_PARAM);
     } else {
         if (count($vars) < 2) {
             $error->add(XNPERR_MISSING_PARAM);
         } else {
             if (isset($vars[0]) && strlen($vars[0]) > 32) {
                 $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
             }
             if (!is_subclass_of($vars[1], 'XooNIpsItemInfoCompo')) {
                 $error->add(XNPERR_INVALID_PARAM, 'parameter2 must be subclass of XooNIpsItemCompo');
             }
             $basic = $vars[1]->getVar('basic');
             if ($basic->get('item_id') == false) {
                 $error->add(XNPERR_MISSING_PARAM, 'parameter 2 missing basic.item_id');
             }
             if ($basic->get('item_type_id') == false) {
                 $error->add(XNPERR_MISSING_PARAM, 'parameter 2 missing basic.item_type_id');
             }
         }
     }
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return;
     } else {
         $sessionid = $vars[0];
         $item = $vars[1];
     }
     list($result, $uid, $session) = $this->restoreSession($sessionid);
     if (!$result) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_SESSION);
         return false;
     }
     // item_id -> item_type_id
     $basic = $item->getVar('basic');
     $item_id = $basic->getVar('item_id');
     $basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $old_basic = $basic_handler->get($item_id);
     $item_type_id = $old_basic->get('item_type_id');
     // item_type_id -> item_handler
     $item_type_handler =& xoonips_getormhandler('xoonips', 'item_type');
     $item_type = $item_type_handler->get($item_type_id);
     if (!$item_type) {
         $response->setResult(false);
         $error->add(XNPERR_ERROR, "unsupported itemtype({$item_type_id})");
         return false;
     }
     $item_handler =& xoonips_getormcompohandler($item_type->get('name'), 'item');
     //
     // check ext_id(doi)
     // error if readable item(public, group, own private item) has same doi
     $basic = $item->getVar('basic');
     if (strlen($basic->get('doi')) > 0) {
         $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
         $criteria = new CriteriaCompo(new Criteria('doi', addslashes($basic->get('doi'))));
         $criteria->add(new Criteria('item_id', (int) $basic->get('item_id'), '<>'));
         $objs =& $item_basic_handler->getObjects($criteria);
         if (count($objs) > 0) {
             // error if other item(in public, group, private of all users) has same doi
             $response->setResult(false);
             $error->add(XNPERR_INCOMPLETE_PARAM, $basic->get('doi') . " already exists");
             return false;
         }
     }
     // check permission
     if (!$item_handler->getPerm($item_id, $uid, 'write')) {
         $item_lock_handler =& xoonips_getormhandler('xoonips', 'item_lock');
         $response->setResult(false);
         if ($item_lock_handler->isLocked($item_id)) {
             $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot update item because item is " . $this->getLockTypeString($item_lock_handler->getLockType($item_id)));
         } else {
             $error->add(XNPERR_ACCESS_FORBIDDEN);
         }
         return false;
     }
     // item_id -> old_item
     $old_item = $item_handler->get($item_id);
     if (!$old_item) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND);
         return false;
     }
     // item_type_id -> item_type_name, detail_item_type_handler
     $item_type_name = $item_type->getVar('name');
     $detail_item_type_handler =& xoonips_getormhandler($item_type_name, 'item_type');
     if (!$detail_item_type_handler) {
         $response->setResult(false);
         $error->add(XNPERR_ERROR, "unsupported itemtype({$item_type_id})");
         return false;
     }
     $detail_item_type = $detail_item_type_handler->get($item_type_id);
     if (!$detail_item_type) {
         $response->setResult(false);
         $error->add(XNPERR_ERROR, "unsupported itemtype({$item_type_id})");
         return false;
     }
     // error if unchangable fields changed:itemtype, username, last_modified_date, registration_date, url, file_id
     // but logic cannot detect it...
     $user_handler =& xoonips_getormcompohandler('xoonips', 'user');
     $user = $user_handler->get($old_basic->getVar('uid'));
     if (!$user) {
         $error->add(XNPERR_SERVER_ERROR, "cannot get item owner");
     }
     if ($old_basic->getVar('uid') != $basic->getVar('uid')) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change username");
     }
     if ($old_basic->getVar('last_update_date') != $basic->getVar('last_update_date')) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change last_update_date");
     }
     if ($old_basic->getVar('creation_date') != $basic->getVar('creation_date')) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change registration_date");
     }
     if ($old_basic->getVar('item_type_id') != $basic->getVar('item_type_id')) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change itemtype");
         $response->setResult(false);
         return false;
     }
     $old_file_ids = array();
     $new_file_ids = array();
     foreach ($detail_item_type->getFileTypeNames() as $file_type_name) {
         $old_files = $old_item->getVar($file_type_name);
         $new_files = $item->getVar($file_type_name);
         if (is_array($old_files)) {
             foreach ($old_files as $old_file) {
                 $old_file_ids[] = $old_file->getVar('file_id');
             }
         } else {
             if ($old_files && $old_files->getVar('file_id')) {
                 $old_file_ids[] = $old_files->getVar('file_id');
             }
         }
         if (is_array($new_files)) {
             foreach ($new_files as $new_file) {
                 $new_file_ids[] = $new_file->getVar('file_id');
             }
         } else {
             if ($new_files && $new_files->getVar('file_id')) {
                 $new_file_ids[] = $new_files->getVar('file_id');
             }
         }
     }
     sort($old_file_ids);
     sort($new_file_ids);
     if (implode(',', $old_file_ids) != implode(',', $new_file_ids)) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change file_id");
     }
     if ($error->get()) {
         $response->setResult(false);
         return false;
     }
     // can access that indexes?
     $index_handler =& xoonips_getormhandler('xoonips', 'index');
     $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
     $index_item_links = $item->getVar('indexes');
     $add_to_private = false;
     $add_to_group = false;
     $add_to_public = false;
     foreach ($index_item_links as $index_item_link) {
         $index_id = $index_item_link->get('index_id');
         $index = $index_handler->get($index_id);
         if (false == $index) {
             $error->add(XNPERR_NOT_FOUND, "index not found({$index_id})");
         } else {
             if (!$index_handler->getPerm($index_id, $uid, 'read')) {
                 $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot access index({$index_id})");
             }
             $open_level = $index->get('open_level');
             if ($open_level == OL_PRIVATE) {
                 $add_to_private = true;
             } else {
                 if ($open_level == OL_GROUP_ONLY) {
                     $add_to_group = true;
                 } else {
                     if ($open_level == OL_PUBLIC) {
                         $add_to_public = true;
                     }
                 }
             }
         }
     }
     // error if no private index is selected.
     if (!$add_to_private) {
         $error->add(XNPERR_INVALID_PARAM, "select at least 1 private index");
     }
     // related_to items exist?
     $related_tos = $item->getVar('related_tos');
     $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     foreach ($related_tos as $related_to) {
         $related_item_id = $related_to->getVar('item_id');
         $related_item_basic = $item_basic_handler->get($related_item_id);
         if (!$related_item_basic) {
             $error->add(XNPERR_INVALID_PARAM, "related_to has non-existent item(item_id={$related_item_id})");
         } else {
             if (!$item_handler->getPerm($related_item_id, $uid, 'read')) {
                 $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot access related_tos(item_id={$related_item_id})");
             }
         }
     }
     if ($error->get()) {
         $response->setResult(false);
         return false;
     }
     // error if add to public/group and no rights input
     if ($add_to_public || $add_to_group) {
         $detail = $item->getVar('detail');
         if ($detail_item_type->getFieldByName('detail', 'rights')) {
             if ($detail_item_type->getFieldByName('detail', 'use_cc')) {
                 $use_cc = $detail->get('use_cc');
             } else {
                 $use_cc = 0;
             }
             if ($detail->get('rights') == '' && $use_cc == 0) {
                 $response->setResult(false);
                 $error->add(XNPERR_INCOMPLETE_PARAM, "rights is required");
                 return false;
             }
         }
         // error if add to public/group and no readme input
         if ($detail_item_type->getFieldByName('detail', 'readme')) {
             if ($detail->get('readme') == '') {
                 $response->setResult(false);
                 $error->add(XNPERR_INCOMPLETE_PARAM, "readme is required");
                 return false;
             }
         }
     }
     // check item storage/number limit(private/group)
     $size = $this->getSizeOfItem($item);
     if (!$this->isEnoughSpace($error, $uid, $size, $item->getVar('indexes'), $size, $old_item->getVar('indexes'))) {
         $response->setResult(false);
         return false;
     }
     // only private index changed?
     $only_private_index_changed = $this->isOnlyPrivateIndexChanged($error, $detail_item_type->getIteminfo(), $item, $old_item);
     // start transaction
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     // insert
     if (!$item_handler->insert($item)) {
         $transaction->rollback();
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot update item");
         return false;
     }
     $item_handler->unsetNew($item);
     // update item_basic.last_update_date
     $basic->setVar('last_update_date', time());
     if (!$basic_handler->insert($basic)) {
         $error->add(XNPERR_SERVER_ERROR, "cannot update item_basic");
         $response->setResult(false);
         return false;
     }
     // event log ( update item )
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     if (!$eventlog_handler->recordUpdateItemEvent($item_id)) {
         $error->add(XNPERR_SERVER_ERROR, "cannot insert event");
         $response->setResult(false);
         return false;
     }
     // item insert/update/certify_required/certified event, change certify_state, send notification, update RSS, update item_status.
     if (!$only_private_index_changed && !$this->touchItem($error, $item, $uid)) {
         $transaction->rollback();
         $response->setResult(false);
         return false;
     }
     // commit
     $transaction->commit();
     $response->setSuccess($item_id);
     $response->setResult(true);
     return true;
 }
 /**
  * execute removeItem
  *
  * @param[in]  $vars[0] session ID
  * @param[in]  $vars[1] item identifier
  * @param[in]  $vars[2] ext_id or item_id
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error  error information
  * @param[out] $response->success item id of deleted item
  */
 function execute(&$vars, &$response)
 {
     /*
     check permission
     insert event DELETE_ITEM
     delete item
     delete files
     delete search_text
     
     XooNIpsItemCompoHandler::delete() does:
     1. delete item from index keywords
     3. delete item
     4. delete title
     5. delete keyword
     6. delete item from related_to
     7. delete item changelog
     
     update item_status
     */
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 3) {
         $error->add(XNPERR_EXTRA_PARAM);
     } else {
         if (count($vars) < 3) {
             $error->add(XNPERR_MISSING_PARAM);
         } else {
             if (isset($vars[0]) && strlen($vars[0]) > 32) {
                 $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
             }
             if ($vars[2] != 'item_id' && $vars[2] != 'ext_id') {
                 $error->add(XNPERR_INVALID_PARAM, 'invalid parameter 3');
             }
             if ($vars[2] == 'item_id' && !is_int($vars[1]) && !ctype_digit($vars[1])) {
                 $error->add(XNPERR_INVALID_PARAM, 'not integer parameter 2');
             }
             if ($vars[2] == 'item_id' && strlen($vars[1]) > 10) {
                 $error->add(XNPERR_INVALID_PARAM, 'too long parameter 2');
             }
         }
     }
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return;
     } else {
         $sessionid = $vars[0];
         $id = $vars[1];
         $id_type = $vars[2];
     }
     list($result, $uid, $session) = $this->restoreSession($sessionid);
     if (!$result) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_SESSION);
         return false;
     }
     // get item and item_id
     $item_handler =& xoonips_getormcompohandler('xoonips', 'item');
     $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     if ($id_type == 'item_id') {
         $item = $item_handler->get($id);
     } else {
         if ($id_type == 'ext_id') {
             if (strlen($id) == 0) {
                 $response->setResult(false);
                 $error->add(XNPERR_INVALID_PARAM, "ext_id is empty");
                 return false;
             } else {
                 $basics =& $item_basic_handler->getObjects(new Criteria('doi', addslashes($id)));
                 if (false === $basics) {
                     $response->setResult(false);
                     $error->add(XNPERR_SERVER_ERROR, "cannot get basic information");
                     return false;
                 } else {
                     if (count($basics) >= 2) {
                         $response->setResult(false);
                         $error->add(XNPERR_SERVER_ERROR, "ext_id is duplicated");
                         return false;
                     } else {
                         if (count($basics) == 1) {
                             $item = $item_handler->get($basics[0]->get('item_id'));
                         } else {
                             $item = false;
                         }
                     }
                 }
             }
         } else {
             $response->setResult(false);
             $error->add(XNPERR_INVALID_PARAM, "bad id_type({$id_type})");
             return false;
         }
     }
     if (!$item) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND);
         return false;
     }
     $basic = $item->getVar('basic');
     $item_id = $basic->get('item_id');
     // can delete?
     if (!$item_handler->getPerm($item_id, $uid, 'delete')) {
         $item_lock_handler =& xoonips_getormhandler('xoonips', 'item_lock');
         $response->setResult(false);
         if ($item_lock_handler->isLocked($item_id)) {
             $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot remove item because item is " . $this->getLockTypeString($item_lock_handler->getLockType($item_id)));
         } else {
             $error->add(XNPERR_ACCESS_FORBIDDEN);
         }
         return false;
     }
     // item -> detail_item
     $item_type_id = $basic->get('item_type_id');
     $item_type_handler =& xoonips_getormhandler('xoonips', 'item_type');
     $item_type = $item_type_handler->get($item_type_id);
     if (!$item_type) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get itemtype(item_type_id={$item_type_id})");
         return false;
     }
     $item_type_name = $item_type->get('name');
     $detail_item_handler =& xoonips_getormcompohandler($item_type_name, 'item');
     if (!$detail_item_handler) {
         $response->setResult(false);
         $error->add(XNPERR_ERROR, "unsupported itemtype({$item_type_id})");
         return false;
     }
     $detail_item = $detail_item_handler->get($item_id);
     if (!$detail_item) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get item");
         return false;
     }
     // get files
     $delete_later = array();
     $detail_item_type_handler =& xoonips_getormhandler($item_type_name, 'item_type');
     $detail_item_type = $detail_item_type_handler->get($item_type_id);
     foreach ($detail_item_type->getFileTypeNames() as $field_name) {
         $files = $detail_item->getVar($field_name);
         if ($files) {
             if (!$detail_item_type->getMultiple($field_name)) {
                 $files = array($files);
             }
             foreach ($files as $file) {
                 $delete_later[] = $file;
             }
         }
     }
     // start transaction
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     // delete detail item
     if (!$detail_item_handler->delete($detail_item)) {
         $transaction->rollback();
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot delete item");
         return false;
     }
     // delete item from related_to
     $r_handler =& xoonips_getormhandler('xoonips', 'related_to');
     if (!$r_handler->deleteChildItemIds($item_id)) {
         $transaction->rollback();
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot delete item_id in the related_tos");
         return false;
     }
     // update item_status
     $item_status_handler =& xoonips_getormhandler('xoonips', 'item_status');
     $item_status = $item_status_handler->get($item_id);
     if ($item_status && $item_status->get('is_deleted') == 0) {
         $item_status->setVar('is_deleted', 1, true);
         $item_status->setVar('deleted_timestamp', time(), true);
         if (!$item_status_handler->insert($item_status)) {
             $transaction->rollback();
             $response->setResult(false);
             $error->add(XNPERR_SERVER_ERROR, "cannot update item_status");
             return false;
         }
     }
     // event log ( delete item )
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     if (!$eventlog_handler->recordDeleteItemEvent($item_id)) {
         $transaction->rollback();
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot insert event");
         return false;
     }
     // commit
     $transaction->commit();
     // unlink files. cannot rollback
     // delete search_text. cannot rollback because search_text contains fulltext column(MyISAM).
     $search_text_handler =& xoonips_getormhandler('xoonips', 'search_text');
     $file_handler =& xoonips_getormhandler('xoonips', 'file');
     foreach ($delete_later as $file) {
         if ($file->isNew()) {
             continue;
         }
         // no need to delete file becaue this is a created item.
         $file_id = $file->get('file_id');
         if (!$file_handler->deleteFile($file)) {
             $error->add(XNPERR_SERVER_ERROR, "cannot delete file(file_id={$file_id})");
         }
         $search_text = $search_text_handler->get($file_id);
         if ($search_text && !$search_text_handler->delete($search_text)) {
             $error->add(XNPERR_SERVER_ERROR, "cannot delete search_text(file_id={$file_id})");
         }
     }
     if ($error->get()) {
         $response->setResult(false);
         return false;
     }
     // call item event listener
     $this->_include_view_php();
     $dispatcher =& XooNIpsItemEventDispatcher::getInstance();
     $dispatcher->onDelete($item_id);
     $response->setSuccess($item_id);
     $response->setResult(true);
     return true;
 }
Пример #11
0
 /**
  * insert/update basic information
  *
  * @param object $itemlib_obj
  * @return bool false if failure
  */
 function insertBasicInformation($itemlib_obj)
 {
     $item_basic_obj =& $itemlib_obj->getItemBasicObject();
     $is_new = $item_basic_obj->get('item_id') == 0;
     $certify_required_onupdate = $is_new ? false : $itemlib_obj->isCertifyRequired();
     $itemlib_obj->truncateObject();
     // transaction
     $transaction =& XooNIpsTransaction::getInstance();
     $transaction->start();
     // basic information
     if (!$this->_item_basic_handler->insert($item_basic_obj)) {
         $transaction->rollback();
         return false;
     }
     // get inserted item_id
     $item_id = $item_basic_obj->get('item_id');
     // title
     $item_title_objs =& $itemlib_obj->getTitleObjects();
     if (!$this->_item_title_handler->updateAllObjectsByForeignKey('item_id', $item_id, $item_title_objs)) {
         $transaction->rollback();
         return false;
     }
     // keyword
     $item_keyword_objs =& $itemlib_obj->getKeywordObjects();
     if (!$this->_item_keyword_handler->updateAllObjectsByForeignKey('item_id', $item_id, $item_keyword_objs)) {
         $transaction->rollback();
         return false;
     }
     // related to
     $related_to_ids = array_unique(array_merge($itemlib_obj->getRelatedTo(), $itemlib_obj->getRelatedToCheck()));
     if (!$this->_related_to_handler->insertChildItemIds($item_id, $related_to_ids)) {
         $transaction->rollback();
         return false;
     }
     // update certify state on update
     if ($certify_required_onupdate) {
         $xconfig_handler =& xoonips_getormhandler('xoonips', 'config');
         $certify_item = $xconfig_handler->getValue('certify_item');
         $certify_state = $certify_item == 'auto' ? CERTIFIED : CERTIFY_REQUIRED;
         $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
         $index_item_link_objs =& $index_item_link_handler->getByItemId($item_id, array(OL_PUBLIC, OL_GROUP_ONLY));
         foreach ($index_item_link_objs as $index_item_link_obj) {
             $index_item_link_obj->set('certify_state', $certify_state);
             $index_item_link_handler->insert($index_item_link_obj);
         }
     }
     // update item status
     $item_status_handler =& xoonips_getormhandler('xoonips', 'item_status');
     $item_status_handler->updateItemStatus($item_id);
     // success
     $transaction->commit();
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     if ($is_new) {
         // record event log (insert item)
         $eventlog_handler->recordInsertItemEvent($item_id);
         // notification
         xoonips_insert_event_and_send_notification_of_certification($item_id);
     } else {
         // record event log (update item)
         $eventlog_handler->recordUpdateItemEvent($item_id);
         // notification
         xoonips_insert_event_and_send_notification_of_certification($item_id);
     }
     return true;
 }
 /**
  * execute getFile
  *
  * @param[in]  $vars[0] session ID
  * @param[in]  $vars[1] file ID
  * @param[in]  $vars[2] agreement to license(0:not agreed, 1:agreed)
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error  error information
  * @param[out] $response->success XooNIpsFile file information
  */
 function execute(&$vars, &$response)
 {
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 3) {
         $error->add(XNPERR_EXTRA_PARAM);
     }
     if (count($vars) < 3) {
         $error->add(XNPERR_MISSING_PARAM);
     }
     //
     if (isset($vars[0]) && strlen($vars[0]) > 32) {
         $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
     }
     if (!is_int($vars[1]) && !ctype_digit($vars[1])) {
         $error->add(XNPERR_INVALID_PARAM, 'not integer parameter 2');
     }
     if ($vars[2] != '1' && $vars[2] != '0') {
         $error->add(XNPERR_INVALID_PARAM, 'parameter 3 must be 0 or 1');
     }
     //
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return false;
     } else {
         $sessionid = $vars[0];
         $file_id = intval($vars[1]);
         $agreement = intval($vars[2]);
     }
     list($result, $uid, $session) = $this->restoreSession($sessionid);
     if (!$result) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_SESSION);
         return false;
     }
     // file_id -> file, item_id
     $file_handler =& xoonips_getormhandler('xoonips', 'file');
     $file = $file_handler->get($file_id);
     if (!$file) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND);
         return false;
     }
     $item_id = $file->get('item_id');
     if (empty($item_id)) {
         $response->setResult(false);
         $error->add(XNPERR_ACCESS_FORBIDDEN);
         // maybe belong to other session
         return false;
     }
     // can user access that item?
     $item_handler =& xoonips_getormcompohandler('xoonips', 'item');
     if (!$item_handler->getPerm($item_id, $uid, 'read')) {
         $response->setResult(false);
         $error->add(XNPERR_ACCESS_FORBIDDEN);
         return false;
     }
     // already deleted?
     if ($file->get('is_deleted')) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND, "already deleted or replaced");
         return false;
     }
     // item_id -> item, itemtype
     $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $basic = $item_basic_handler->get($item_id);
     if (!$basic) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get item_basic");
         return false;
     }
     $item_type_handler =& xoonips_getormhandler('xoonips', 'item_type');
     $item_type = $item_type_handler->get($basic->get('item_type_id'));
     if (!$item_type) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get itemtype of that item");
         return false;
     }
     // item_type, item_id -> detail
     $detail_item_handler =& xoonips_getormcompohandler($item_type->get('name'), 'item');
     $detail_item = $detail_item_handler->get($item_id);
     if (!$detail_item) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get item");
         return false;
     }
     $detail = $detail_item->getVar('detail');
     // license agreement?
     if (!$agreement && $detail->get('rights')) {
         $response->setResult(false);
         $error->add(XNPERR_ERROR, "need license agreement");
         return false;
     }
     if (!$detail_item_handler->hasDownloadPermission($uid, $file_id)) {
         $response->setResult(false);
         $error->add(XNPERR_ACCESS_FORBIDDEN);
         return false;
     }
     // start transaction
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     // increment download count
     $file->setVar('download_count', $file->get('download_count') + 1, true);
     if (!$file_handler->insert($file)) {
         $transaction->rollback();
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot update file table");
         return false;
     }
     // insert event log ( file downloaded )
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     if (!$eventlog_handler->recordDownloadFileEvent($item_id, $file_id)) {
         $transaction->rollback();
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot insert event log");
         return false;
     }
     // get module option 'enable_dl_limit'
     $iteminfo = $detail_item_handler->getIteminfo();
     $mhandler =& xoops_gethandler('module');
     $module = $mhandler->getByDirname($iteminfo['ormcompo']['module']);
     $chandler =& xoops_gethandler('config');
     $assoc = $chandler->getConfigsByCat(false, $module->mid());
     if (isset($assoc['enable_dl_limit']) && $assoc['enable_dl_limit'] == '1') {
         // send download-notification
         if ($detail->get('attachment_dl_limit') && $detail->get('attachment_dl_notify')) {
             xoonips_notification_user_file_downloaded($file_id, $uid);
         }
     }
     // end transaction
     $transaction->commit();
     $response->setSuccess($file);
     $response->setResult(true);
     return true;
 }
 /**
  * execute removeFile
  *
  * @param[in]  $vars[0] session ID
  * @param[in]  $vars[1] file ID
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error  error information
  * @param[out] $response->success file ID of deleted file
  */
 function execute(&$vars, &$response)
 {
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 2) {
         $error->add(XNPERR_EXTRA_PARAM);
     }
     if (count($vars) < 2) {
         $error->add(XNPERR_MISSING_PARAM);
     }
     //
     if (isset($vars[0]) && strlen($vars[0]) > 32) {
         $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
     }
     if (!is_int($vars[1]) && !ctype_digit($vars[1])) {
         $error->add(XNPERR_INVALID_PARAM, 'not integer parameter 2');
     }
     //
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return;
     } else {
         $sessionid = $vars[0];
         $file_id = intval($vars[1]);
     }
     list($result, $uid, $session) = $this->restoreSession($sessionid);
     if (!$result) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_SESSION);
         return false;
     }
     // file_id -> file, item_id
     $file_handler =& xoonips_getormhandler('xoonips', 'file');
     $file = $file_handler->get($file_id);
     if (!$file) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND);
         // not found
         return false;
     }
     $item_id = $file->getVar('item_id');
     if (empty($item_id)) {
         $response->setResult(false);
         $error->add(XNPERR_ACCESS_FORBIDDEN);
         // maybe belong to other session
         return false;
     }
     // item_id -> basic -> item_type_id -> item_type_name -> item_handler
     $basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $basic = $basic_handler->get($item_id);
     if (!$basic) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "non-existent item(item_id={$item_id}) owns that file");
         return false;
     }
     $item_type_id = $basic->get('item_type_id');
     $item_type_handler =& xoonips_getormhandler('xoonips', 'item_type');
     $item_type = $item_type_handler->get($item_type_id);
     if (!$item_type) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_PARAM, "bad itemtype(item_type_id={$item_type_id})");
         return false;
     }
     $item_type_name = $item_type->get('name');
     $item_handler =& xoonips_getormcompohandler($item_type_name, 'item');
     if (!$item_handler) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get item handler(item_type_id={$item_type_id})");
         return false;
     }
     // can modify?
     if (!$item_handler->getPerm($item_id, $uid, 'write')) {
         $item_lock_handler =& xoonips_getormhandler('xoonips', 'item_lock');
         $response->setResult(false);
         if ($item_lock_handler->isLocked($item_id)) {
             $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot remove file because item is " . $this->getLockTypeString($item_lock_handler->getLockType($item_id)));
         } else {
             $error->add(XNPERR_ACCESS_FORBIDDEN);
         }
         return false;
     }
     // already deleted?
     if ($file->getVar('is_deleted')) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND, "already deleted or replaced");
         return false;
     }
     // file -> file_type_name
     $file_type_handler =& xoonips_getormhandler('xoonips', 'file_type');
     $file_type = $file_type_handler->get($file->getVar('file_type_id'));
     if (!$file_type) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "that file has unkonwn file type");
         return false;
     }
     $file_type_name = $file_type->getVar('name');
     // item_type -> detail_item_type
     $detail_item_type_handler =& xoonips_getormhandler($item_type->getVar('name'), 'item_type');
     $detail_item_type = $detail_item_type_handler->get($item_type_id);
     if (!$detail_item_type) {
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot get detail itemtype of that item");
         return false;
     }
     // is that file optional? or required?
     if ($detail_item_type->getRequired($file_type_name)) {
         // is that the last one?
         $criteria = new CriteriaCompo();
         $criteria->add(new Criteria('item_id', $item_id));
         $criteria->add(new Criteria('file_type_id', $file->getVar('file_type_id')));
         $criteria->add(new Criteria('is_deleted', 0));
         $count = $file_handler->getCount($criteria);
         if ($count == 0) {
             $response->setResult(false);
             $error->add(XNPERR_SERVER_ERROR, "cannot count files");
             return false;
         } else {
             if ($count == 1) {
                 $response->setResult(false);
                 $error->add(XNPERR_ERROR, "that file is not optional and the last one");
                 return false;
             }
         }
     }
     // start transaction
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     // set is_deleted = 1;
     $file->setVar('is_deleted', 1);
     if (!$file_handler->insert($file)) {
         $transaction->rollback();
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot update file table");
         return false;
     }
     // event log ( update item )
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     if (!$eventlog_handler->recordUpdateItemEvent($item_id)) {
         $error->add(XNPERR_SERVER_ERROR, "cannot insert event");
         $response->setResult(false);
         return false;
     }
     // item insert/update/certify_required/certified event, change certify_state, send notification, update RSS, update item_status.
     $item = $item_handler->get($item_id);
     if (!$this->touchItem($error, $item, $uid)) {
         $transaction->rollback();
         $response->setResult(false);
         return false;
     }
     // delete search_text
     $search_text_handler =& xoonips_getormhandler('xoonips', 'search_text');
     $search_text = $search_text_handler->get($file_id);
     if ($search_text) {
         if (!$search_text_handler->delete($search_text)) {
             $transaction->rollback();
             $response->setResult(false);
             $error->add(XNPERR_SERVER_ERROR, "cannot remove search text");
             return false;
         }
     }
     // commit
     $transaction->commit();
     // unlink file
     $file_handler->deleteFile($file);
     $response->setSuccess($file_id);
     $response->setResult(true);
     return true;
 }
 /**
  * @param[in]  $vars[0] session ID
  * @param[in]  $vars[1] XooNIpsItem item information
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error  error information
  * @param[out] $response->success item id of updated item
  */
 function execute(&$vars, &$response)
 {
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 4) {
         $error->add(XNPERR_EXTRA_PARAM);
     } else {
         if (count($vars) < 4) {
             $error->add(XNPERR_MISSING_PARAM);
         } else {
             if (isset($vars[0]) && strlen($vars[0]) > 32) {
                 $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
             }
             if (!is_subclass_of($vars[1], 'XooNIpsItemInfoCompo')) {
                 $error->add(XNPERR_INVALID_PARAM, 'parameter2 must be subclass of XooNIpsItemCompo');
             }
             $basic = $vars[1]->getVar('basic');
             if ($basic->get('item_id') == false) {
                 $error->add(XNPERR_MISSING_PARAM, 'parameter 2 missing basic.item_id');
             }
             if ($basic->get('item_type_id') == false) {
                 $error->add(XNPERR_MISSING_PARAM, 'parameter 2 missing basic.item_type_id');
             }
             $upload_max_filesize = $this->returnBytes(ini_get('upload_max_filesize'));
             for ($i = 0; $i < count($vars[2]); $i++) {
                 if (filesize($vars[2][$i]->getFilepath()) > $upload_max_filesize) {
                     $error->add(XNPERR_INVALID_PARAM, 'too large file(file_id=' . $vars[2][$i]->get('file_id') . ')');
                 }
                 $vars[2][$i]->set('file_size', filesize($vars[2][$i]->getFilepath()));
             }
             if (!is_array($vars[3])) {
                 $error->add(XNPERR_INVALID_PARAM, 'parameter 4 must be array of int');
             } else {
                 foreach ($vars[3] as $file_id) {
                     if (!is_int($file_id) && !ctype_digit($file_id)) {
                         $error->add(XNPERR_INVALID_PARAM, 'parameter 4 must be array of int');
                         break;
                     }
                 }
             }
         }
     }
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return;
     } else {
         $sessionid = $vars[0];
         $item = $vars[1];
         $add_files = $vars[2];
         $delete_file_ids = $vars[3];
     }
     list($result, $uid, $session) = $this->restoreSession($sessionid);
     if (!$result) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_SESSION);
         return false;
     }
     // start transaction
     $transaction = XooNIpsTransaction::getInstance();
     $transaction->start();
     // item exists?
     $basic = $item->getVar('basic');
     $item_id = $basic->get('item_id');
     $item_compo_handler =& xoonips_getormcompohandler('xoonips', 'item');
     $basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $old_basic = $basic_handler->get($item_id);
     if ($old_basic == false) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND);
         return false;
     }
     // check permission
     if (!$item_compo_handler->getPerm($item_id, $uid, 'write')) {
         $item_lock_handler =& xoonips_getormhandler('xoonips', 'item_lock');
         $response->setResult(false);
         if ($item_lock_handler->isLocked($item_id)) {
             $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot update item because item is " . $this->getLockTypeString($item_lock_handler->getLockType($item_id)));
         } else {
             $error->add(XNPERR_ACCESS_FORBIDDEN);
         }
         return false;
     }
     // item_id -> item_type_id
     $item_type_id = $old_basic->get('item_type_id');
     // item_type_id -> item_handler
     $item_type_handler =& xoonips_getormhandler('xoonips', 'item_type');
     $item_type = $item_type_handler->get($item_type_id);
     if (!$item_type) {
         $response->setResult(false);
         $error->add(XNPERR_ERROR, "unsupported itemtype({$item_type_id})");
         return false;
     }
     $item_handler =& xoonips_getormcompohandler($item_type->get('name'), 'item');
     //
     // check ext_id(doi) confliction
     $basic = $item->getVar('basic');
     $ext_id = $basic->get('doi');
     if (strlen($ext_id)) {
         $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
         $criteria = new CriteriaCompo(new Criteria('doi', addslashes($ext_id)));
         $criteria->add(new Criteria('item_id', $item_id, '<>'));
         $objs =& $item_basic_handler->getObjects($criteria);
         if (count($objs) > 0) {
             // error if other item(in public, group,
             // private of all users) has same doi
             $response->setResult(false);
             $error->add(XNPERR_INCOMPLETE_PARAM, "{$ext_id} is duplicated");
             return false;
         }
     }
     // item_id -> old_item
     $old_item = $item_handler->get($item_id);
     if (!$old_item) {
         $response->setResult(false);
         $error->add(XNPERR_NOT_FOUND);
         return false;
     }
     // item_type_id -> item_type_name, detail_item_type_handler
     $item_type_name = $item_type->get('name');
     $detail_item_type_handler =& xoonips_getormhandler($item_type_name, 'item_type');
     if (!$detail_item_type_handler) {
         $response->setResult(false);
         $error->add(XNPERR_ERROR, "unsupported itemtype(item_type_id={$item_type_id})");
         return false;
     }
     $detail_item_type = $detail_item_type_handler->get($item_type_id);
     if (!$detail_item_type) {
         $response->setResult(false);
         $error->add(XNPERR_ERROR, "unsupported itemtype(item_type_id={$item_type_id})");
         return false;
     }
     // error if unchangable fields changed:
     //  itemtype, username, last_modified_date, registration_date, url
     $user_handler =& xoonips_getormcompohandler('xoonips', 'user');
     $user = $user_handler->get($old_basic->get('uid'));
     if (!$user) {
         $error->add(XNPERR_SERVER_ERROR, "cannot get item owner");
     }
     if ($old_basic->get('uid') != $basic->get('uid')) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change username");
     }
     if ($old_basic->get('last_update_date') != $basic->get('last_update_date')) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change last_update_date");
     }
     if ($old_basic->get('creation_date') != $basic->get('creation_date')) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change registration_date");
     }
     if ($old_basic->get('item_type_id') != $basic->get('item_type_id')) {
         $error->add(XNPERR_INVALID_PARAM, "cannot change itemtype");
         $response->setResult(false);
         return false;
     }
     if ($error->get()) {
         $response->setResult(false);
         return false;
     }
     // can access that indexes?
     $index_handler =& xoonips_getormhandler('xoonips', 'index');
     $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
     $index_item_links = $item->getVar('indexes');
     $add_to_private = false;
     $add_to_group = false;
     $add_to_public = false;
     foreach ($index_item_links as $index_item_link) {
         $index_id = $index_item_link->get('index_id');
         $index = $index_handler->get($index_id);
         if (false == $index) {
             $error->add(XNPERR_NOT_FOUND, "no such index(index_id={$index_id})");
         } else {
             if (!$index_handler->getPerm($index_id, $uid, 'read')) {
                 $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot access index(index_id={$index_id})");
             }
             $open_level = $index->get('open_level');
             if ($open_level == OL_PRIVATE) {
                 $add_to_private = true;
             } else {
                 if ($open_level == OL_GROUP_ONLY) {
                     $add_to_group = true;
                 } else {
                     if ($open_level == OL_PUBLIC) {
                         $add_to_public = true;
                     }
                 }
             }
         }
     }
     // error if no private index is selected.
     if (!$add_to_private) {
         $error->add(XNPERR_INVALID_PARAM, "select at least 1 private index");
     }
     // related_to items exist?
     $related_tos = $item->getVar('related_tos');
     $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     foreach ($related_tos as $related_to) {
         $related_item_id = $related_to->get('item_id');
         $related_item_basic = $item_basic_handler->get($related_item_id);
         if (!$related_item_basic) {
             $error->add(XNPERR_INVALID_PARAM, "no such related_to(item_id={$related_item_id})");
         } else {
             if (!$item_handler->getPerm($related_item_id, $uid, 'read')) {
                 $error->add(XNPERR_ACCESS_FORBIDDEN, "cannot access related_tos(item_id={$related_item_id})");
             }
         }
     }
     if ($error->get()) {
         $response->setResult(false);
         return false;
     }
     // error if add to public/group and no rights input
     if ($add_to_public || $add_to_group) {
         if (!$item_handler->isValidForPubicOrGroupShared($item)) {
             $response->setResult(false);
             $error->add(XNPERR_INCOMPLETE_PARAM, "item cannot be public nor group-shared");
             return false;
         }
     }
     if (!$this->isPublicationDateValid($response, $basic->get('publication_year'), $basic->get('publication_month'), $basic->get('publication_mday'), $detail_item_type->getRequired('publication_year'), $detail_item_type->getRequired('publication_month'), $detail_item_type->getRequired('publication_mday'))) {
         $response->setResult(false);
         return false;
     }
     // get old and new file ids
     $old_file_id_to_types = $this->extractFileIdToTypesFromItem($detail_item_type, $old_item);
     $new_file_id_to_types = $this->extractFileIdToTypesFromItem($detail_item_type, $item);
     //remove files to be deleted from $new_file_id_to_types
     foreach ($delete_file_ids as $file_id) {
         unset($new_file_id_to_types[$file_id]);
     }
     $valid_file_type_names = $detail_item_type->getFileTypeNames();
     $file_type_handler =& xoonips_getormhandler('xoonips', 'file_type');
     foreach ($add_files as $file) {
         $file_id = $file->get('file_id');
         if (isset($old_file_ids[$file_id])) {
             $response->setResult(false);
             $error->add(XNPERR_INVALID_PARAM, "file id conflicts");
             return false;
         }
         $file_type = $file_type_handler->get($file->get('file_type_id'));
         if (!in_array($file_type->get('name'), $valid_file_type_names)) {
             $response->setResult(false);
             $error->add(XNPERR_INVALID_PARAM, "filetype not match(pseudo file_id={$file_id})");
             return false;
         }
     }
     // check item storage/number limit(private/group)
     $new_size = $this->getSizeOfItem($item);
     $old_size = $this->getSizeOfItem($old_item);
     if (!$this->isEnoughSpace($error, $uid, $new_size, $item->getVar('indexes'), $old_size, $old_item->getVar('indexes'))) {
         $response->setResult(false);
         return false;
     }
     if (!$this->isFilesConsistent($error, $old_file_id_to_types, $new_file_id_to_types, $add_files, $delete_file_ids)) {
         $response->setResult(false);
         return false;
     }
     // avoid XooNIpsRelatedObjectHandler::insert()
     // from deleting old mainfile.
     foreach ($detail_item_type->getFileTypeNames() as $file_type_name) {
         $file = $old_item->getVar($file_type_name);
         if (!empty($file) && !is_array($file)) {
             // avoid error in inserting empty file
             if ($file->get('file_id') == 0) {
                 $file->unsetNew();
                 $file->unsetDirty();
             }
         }
         $item->setVar($file_type_name, $file);
     }
     // insert
     if (!$item_handler->insert($item)) {
         $transaction->rollback();
         $response->setResult(false);
         $error->add(XNPERR_SERVER_ERROR, "cannot update item");
         return false;
     }
     if (count($add_files) == 0 && count($delete_file_ids) == 0 && $this->isOnlyPrivateIndexChanged($error, $detail_item_type->getIteminfo(), $item, $old_item)) {
         $transaction->commit();
         $response->setSuccess($item_id);
         $response->setResult(true);
         return true;
     }
     // add new files
     $file_handler =& xoonips_getormhandler('xoonips', 'file');
     for ($i = 0; $i < count($add_files); $i++) {
         $add_files[$i]->setVar('mime_type', $this->guessMimeType($add_files[$i]), true);
         if ($new_file_id_to_types[$add_files[$i]->get('file_id')] == $detail_item_type->getPreviewFileName()) {
             $this->createThumbnail($error, $add_files[$i]);
         }
         $add_files[$i]->setNew();
         $add_files[$i]->set('item_id', $item_id);
         if (!$file_handler->insert($add_files[$i])) {
             $transaction->rollback();
             $response->setResult(false);
             $error->add(XNPERR_SERVER_ERROR, "cannot insert file");
             return false;
         }
     }
     $item_handler->unsetNew($item);
     $basic->setVar('last_update_date', time(), true);
     if (!$basic_handler->insert($basic)) {
         $transaction->rollback();
         $error->add(XNPERR_SERVER_ERROR, "cannot update item_basic");
         $response->setResult(false);
         return false;
     }
     // event log ( update item )
     $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
     if (!$eventlog_handler->recordUpdateItemEvent($item_id)) {
         $transaction->rollback();
         $error->add(XNPERR_SERVER_ERROR, "cannot insert event");
         $response->setResult(false);
         return false;
     }
     // item insert/update/certify_required/certified event,
     // change certify_state,
     // send notification, update RSS, update item_status.
     if (!$only_private_index_changed && !$this->touchItem1($error, $item, $uid)) {
         $transaction->rollback();
         $response->setResult(false);
         return false;
     }
     // remove files
     foreach ($delete_file_ids as $file_id) {
         $file = $file_handler->get($file_id);
         $file->set('is_deleted', 1);
         if (!$file_handler->insert($file)) {
             $transaction->rollback();
             $error->add(XNPERR_SERVER_ERROR, "cannot update file");
             $response->setResult(false);
             return false;
         }
     }
     // commit
     $transaction->commit();
     foreach ($delete_file_ids as $file_id) {
         $file = $file_handler->get($file_id);
         // unlink file
         $file_handler->deleteFile($file);
         // delete search_text
         $search_text_handler =& xoonips_getormhandler('xoonips', 'search_text');
         $search_text = $search_text_handler->get($file_id);
         if ($search_text) {
             $search_text_handler->delete($search_text);
         }
     }
     // update search_text
     $files =& $file_handler->getObjects(new Criteria('item_id', $item_id));
     if (!empty($files)) {
         $admin_file_handler =& xoonips_gethandler('xoonips', 'admin_file');
         foreach ($files as $file) {
             $admin_file_handler->updateFileSearchText($file->get('file_id'), true);
         }
     }
     if ($add_to_group || $add_to_public) {
         $this->touchItem2($error, $item, $uid);
     }
     $response->setSuccess($item_id);
     $response->setResult(true);
     return true;
 }