public function execute()
 {
     if (!$this->getUser()->getRights('photos', 'edit')) {
         throw new waException(_w("Access denied"));
     }
     $moderation = waRequest::post('moderation', '', waRequest::TYPE_STRING_TRIM);
     $id = waRequest::post('id', '', waRequest::TYPE_INT);
     $photo_model = new photosPhotoModel();
     $photo = $photo_model->getById($id);
     if (!$photo) {
         $this->errors[] = _wp('Unknown photo');
     }
     if ($moderation == 'approve') {
         $photo_model->updateById($id, array('moderation' => 1));
         $photo_model->updateAccess($id, 1, array(0));
     }
     if ($moderation == 'decline') {
         $photo_model->updateById($id, array('moderation' => -1));
         $photo_model->updateAccess($id, 0, array(0));
     }
     $this->response['photo'] = $photo_model->getById($id);
     // update for making inline-editable widget
     $this->response['frontend_link_template'] = photosFrontendPhoto::getLink(array('url' => '%url%'));
     $this->response['counters'] = array('declined' => $photo_model->countByField('moderation', -1), 'awaiting' => $photo_model->countByField('moderation', 0));
     // l18n string
     $count = (int) waRequest::post('count');
     $total_count = (int) waRequest::post('total_count');
     $this->response['string'] = array('loaded' => _w('%d photo', '%d photos', $count), 'of' => sprintf(_w('of %d'), $total_count), 'chunk' => $count < $total_count ? _w('%d photo', '%d photos', min($this->getConfig()->getOption('photos_per_page'), $count - $total_count)) : false);
 }
 public function execute()
 {
     $stack = array();
     $parent_id = waRequest::post('parent_id', null, waRequest::TYPE_INT);
     $photo_id = (array) waRequest::post('photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $prev_denied_photo_id = waRequest::post('denied_photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $photo_model = new photosPhotoModel();
     $photo_rights_model = new photosPhotoRightsModel();
     if (!$photo_rights_model->checkRights($parent_id, true)) {
         throw new waException(_w("You don't have sufficient access rights"));
     }
     $allowed_photo_id = $photo_rights_model->filterAllowedPhotoIds($photo_id, true);
     $denied_photo_ids = array_diff($photo_id, $allowed_photo_id);
     if ($allowed_photo_id) {
         $parent = $photo_model->getById($parent_id);
         $stack[$parent_id] = $allowed_photo_id;
         if ($parent['stack_count'] > 0) {
             $photo_model->appendToStack($parent_id, $allowed_photo_id);
         } else {
             $photo_model->makeStack($parent_id, $allowed_photo_id);
         }
     }
     $denied_parent_ids = array();
     if ($denied_photo_ids) {
         foreach ($photo_model->getByField('id', $denied_photo_ids, 'id') as $photo) {
             $denied_parent_ids[] = $photo['parent_id'] > 0 ? $photo['parent_id'] : $photo['id'];
         }
     }
     $denied_photo_id = array_values(array_unique(array_merge($prev_denied_photo_id, $denied_parent_ids)));
     $this->response['denied_photo_ids'] = $denied_photo_id;
     $all_photos_length = waRequest::post('photos_length', 0, waRequest::TYPE_INT);
     if (!$all_photos_length) {
         $all_photos_length = count($photo_id);
     }
     $all_photos_length += 1;
     // plus parent photo
     $denied_photos_length = count($denied_photo_id);
     if ($denied_photos_length > 0 && $all_photos_length > 0) {
         $this->response['alert_msg'] = photosPhoto::sprintf_wplural("The operation was not performed to %d photo (%%s)", "The operation was not performed to %d photos (%%s)", $denied_photos_length, _w("out of %d selected", "out of %d selected", $all_photos_length)) . ', ' . _w("because you don't have sufficient access rights") . '.';
     }
     if ($stack) {
         /**
          * Extra actions after making stack
          * @event make_stack
          * @params array[int][int]int $stack[%parent_id%][]
          */
         wa()->event('make_stack', $stack);
         $this->log('photos_stack', 1);
     }
     $this->response['parent_id'] = $parent_id;
     $this->response['photo'] = $photo_model->getById($parent_id);
 }
 protected function save(waRequestFile $file, $data)
 {
     $id = $this->model->add($file, $data);
     if (!$id) {
         throw new waException(_w("Save error"));
     }
     $photo = $this->model->getById($id);
     $parent_id = (int) waRequest::post('parent_id');
     if ((int) waRequest::post('parent_id')) {
         $this->model->appendToStack($parent_id, array($id));
     }
     return array('name' => $file->name, 'type' => $file->type, 'size' => $file->size, 'thumbnail_url' => photosPhoto::getPhotoUrl($photo, photosPhoto::getThumbPhotoSize()), 'url' => '#/photo/' . $id . '/');
 }
 public function execute()
 {
     $photo_id = waRequest::get('photo_id', null, waRequest::TYPE_INT);
     $size = waRequest::get('size', null, waRequest::TYPE_STRING);
     $album = null;
     $photo_model = new photosPhotoModel();
     $photo = $photo_model->getById($photo_id);
     if (!$photo) {
         throw new waException(_w("Unknown photo"));
     }
     $photo['frontend_link'] = photosFrontendPhoto::getLink($photo, $album);
     $sizes = $this->getConfig()->getSizes();
     $contexts = array();
     foreach ($sizes as $sz) {
         $contexts[$sz]['html'] = photosPhoto::getEmbedImgHtml($photo, $sz);
         $contexts[$sz]['url'] = photosPhoto::getPhotoUrl($photo, $sz, true);
     }
     if (!$size || !isset($contexts[$size])) {
         $size = $sizes[0];
     }
     $domains = photosPhoto::getDomains(null, $photo);
     if (count($domains) <= 1) {
         $domains = array();
     }
     $this->view->assign('photo', $photo);
     $this->view->assign('sizes', $sizes);
     $this->view->assign('size', $size);
     $this->view->assign('contexts', $contexts);
     $this->view->assign('original_domain', wa()->getRootUrl(true));
     $this->view->assign('domains', $domains);
 }
 public function execute()
 {
     $photo_id = waRequest::get('photo_id', array(), waRequest::TYPE_ARRAY_INT);
     if (!$photo_id) {
         throw new waException(_w('Empty photo list'));
     }
     $photo_model = new photosPhotoModel();
     // dialog for one photo
     if (count($photo_id) == 1) {
         $photo_id = current($photo_id);
         $photo = $photo_model->getById($photo_id);
         $photo_right_model = new photosPhotoRightsModel();
         if (!$photo_right_model->checkRights($photo, true)) {
             $rights = array(0 => array('group_id' => 0, 'photo_id' => null));
         } else {
             $rights = $photo_right_model->getByField('photo_id', $photo_id, 'group_id');
         }
     } else {
         // dialog for several selected photos
         // dummies for correct template randering
         $photo = array('status' => 1);
         $rights = array(0 => array('group_id' => 0, 'photo_id' => null));
         $allowed_photo_id = (array) $photo_model->filterByField($photo_id, 'status', 1);
         $this->view->assign('photo_count', count($photo_id));
         $this->view->assign('disable_submit', count($allowed_photo_id) != count($photo_id));
     }
     $groups_model = new waGroupModel();
     $groups = $groups_model->getAll('id', true);
     $this->view->assign('groups', $groups);
     $this->view->assign('photo', $photo);
     $this->view->assign('rights', $rights);
 }
 public function execute()
 {
     $count = $this->getConfig()->getOption('photos_per_page');
     $id = waRequest::post('id', 0, waRequest::TYPE_INT);
     $hash = waRequest::post('hash', '', waRequest::TYPE_STRING_TRIM);
     $offset = waRequest::post('offset', 1, waRequest::TYPE_INT);
     $direction = waRequest::post('direction', 1, waRequest::TYPE_INT);
     $this->collection = new photosCollection($hash);
     if (strstr($hash, 'rate>0') !== false) {
         $this->collection->orderBy('p.rate DESC, p.id');
     }
     if ($id) {
         $photo_model = new photosPhotoModel();
         $photo = $photo_model->getById($id);
         $offset = $this->collection->getPhotoOffset($photo);
         if ($direction > 0) {
             $offset += 1;
         } else {
             $offset -= $count;
             if ($offset < 0) {
                 $count += $offset;
                 $offset = 0;
             }
         }
     }
     $photos = array_values($this->getPhotos($offset, $count));
     $photos = photosCollection::extendPhotos($photos);
     $loaded = count($photos) + $offset;
     $count = $this->collection->count();
     $this->response['photos'] = $photos;
     $this->response['hash'] = $hash;
     $this->response['string'] = array('loaded' => _w('%d photo', '%d photos', $loaded), 'of' => sprintf(_w('of %d'), $count), 'chunk' => $loaded < $count ? _w('%d photo', '%d photos', min($this->getConfig()->getOption('photos_per_page'), $count - $loaded)) : false);
 }
 public function execute()
 {
     $path = null;
     $photo_rights_model = new photosPhotoRightsModel();
     $photo_id = waRequest::get('photo_id', null, waRequest::TYPE_INT);
     if ($photo_rights_model->checkRights($photo_id, true)) {
         $photo_model = new photosPhotoModel();
         if ($photo = $photo_model->getById($photo_id)) {
             if (waRequest::get('original')) {
                 $path = photosPhoto::getOriginalPhotoPath($photo);
             } else {
                 $path = photosPhoto::getPhotoPath($photo);
             }
         }
     }
     if ($path) {
         if ($attach = waRequest::get('attach') ? true : false) {
             $response = $this->getResponse();
             $response->addHeader('Expires', 'tomorrow');
             $response->addHeader('Cache-Control', ($photo['status'] == 1 ? 'public' : 'private') . ', max-age=' . 86400 * 30);
         }
         waFiles::readFile($path, $attach ? null : basename($photo['name'] . '.' . $photo['ext']), true, !$attach);
     } else {
         throw new waException(_w("Photo not found"), 404);
     }
 }
 /**
  * @param array|int $photo photo or id of photo
  * @param boolean $check_edit
  * @return boolean
  */
 public function checkRights($photo, $check_edit = false)
 {
     if (!is_array($photo)) {
         $photo_model = new photosPhotoModel();
         $photo = $photo_model->getById((int) $photo);
     }
     if (!$photo) {
         return false;
     }
     $photo_id = $photo['id'];
     $user = wa()->getUser();
     if ($check_edit && $photo['contact_id'] != $user->getId() && !$user->getRights('photos', 'edit')) {
         return false;
     }
     if (!empty($photo['app_id'])) {
         return !!$user->getRights($photo['app_id'], 'backend');
     }
     if ($user->isAdmin()) {
         $where = "(group_id >= 0 OR group_id = -" . (int) $user->getId() . ")";
     } else {
         $groups = $user->getGroupIds();
         $where = "group_id IN ('" . implode("','", $groups) . "')";
     }
     $sql = "SELECT count(*) FROM " . $this->table . "\n                WHERE photo_id = " . (int) $photo_id . " AND " . $where . "\n                LIMIT 1";
     return (bool) $this->query($sql)->fetchField();
 }
 public function execute()
 {
     $photo_id = waRequest::get('id', null, waRequest::TYPE_INT);
     if (!$photo_id) {
         throw new waException(_w('Unknown photo'));
     }
     $photo_model = new photosPhotoModel();
     $photo = $photo_model->getById($photo_id);
     $this->view->assign('photo_name', $photo['name']);
 }
 public function execute()
 {
     $photo_id = waRequest::get('photo_id', null, waRequest::TYPE_INT);
     $photo_model = new photosPhotoModel();
     $photo = $photo_model->getById($photo_id);
     if (!$photo) {
         throw new waException(_w('Photo not found'), 404);
     }
     $vote_model = new photosPublicgalleryVoteModel();
     $this->view->assign(array('photo_name' => $photo['name'], 'distribution' => $vote_model->getDistribution($photo_id), 'rate' => $photo['rate'], 'votes_count' => $photo['votes_count'], 'users' => $vote_model->getVotedUsers($photo_id)));
 }
 public function execute()
 {
     $photo_id = waRequest::post('photo_id', null, waRequest::TYPE_ARRAY_INT);
     $prev_denied_photo_id = waRequest::post('denied_photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $photo_model = new photosPhotoModel();
     $photo_rights_model = new photosPhotoRightsModel();
     $allowed_photo_id = $photo_rights_model->filterAllowedPhotoIds($photo_id, true);
     $denied_photo_id = array_diff($photo_id, $allowed_photo_id);
     if ($allowed_photo_id) {
         // before deleting define if is it children photo in stack (one photo page)
         if (count($allowed_photo_id) == 1 && count($photo_id) == 1) {
             $photo = $photo_model->getById($allowed_photo_id);
             if ($photo) {
                 $photo = reset($photo);
                 if ($photo['parent_id'] > 0) {
                     $this->response['parent_id'] = $photo['parent_id'];
                 }
             }
         }
         foreach ($allowed_photo_id as $id) {
             $photo_model->delete($id);
             /**
              * Extend delete process
              * Make extra workup
              * @event photo_delete
              */
             wa()->event('photo_delete', $id);
         }
         $this->log('photos_delete', 1);
     }
     $denied_parent_id = array();
     if ($denied_photo_id) {
         foreach ($photo_model->getByField('id', $denied_photo_id, 'id') as $photo) {
             $denied_parent_id[] = $photo['parent_id'] > 0 ? $photo['parent_id'] : $photo['id'];
         }
     }
     $denied_photo_id = array_values(array_unique(array_merge($prev_denied_photo_id, $denied_parent_id)));
     $this->response['denied_photo_id'] = $denied_photo_id;
     $all_photos_length = waRequest::post('photos_length', 0, waRequest::TYPE_INT);
     if (!$all_photos_length) {
         $all_photos_length = count($photo_id);
     }
     $denied_photos_length = count($denied_photo_id);
     if ($denied_photos_length > 0 && $all_photos_length > 0) {
         $this->response['alert_msg'] = photosPhoto::sprintf_wplural("The operation was not performed to %d photo (%%s)", "The operation was not performed to %d photos (%%s)", $denied_photos_length, _w("out of %d selected", "out of %d selected", $all_photos_length)) . ', ' . _w("because you don't have sufficient access rights") . '.';
     }
     if ($denied_photos_length == $all_photos_length) {
         $this->response['denied_all'] = true;
     } else {
         $this->response['denied_all'] = false;
     }
 }
 protected function save(waRequestFile $file, $data = array())
 {
     wa('photos');
     $photo_model = new photosPhotoModel();
     $data['groups'] = array();
     $data['app_id'] = 'blog';
     $data['hash'] = '';
     $id = $photo_model->add($file, $data);
     if (!$id) {
         throw new waException(_w("Save error"));
     }
     $photo = $photo_model->getById($id);
     return array('id' => $id, 'photo' => $photo, 'name' => $file->name, 'type' => $file->type, 'size' => $file->size, 'url' => photosPhoto::getPhotoUrl($photo, null, !!waRequest::get('absolute')), 'thumbnail_url' => photosPhoto::getPhotoUrl($photo, photosPhoto::getThumbPhotoSize(), !!waRequest::get('absolute')));
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     $before_id = waRequest::post('before_id', 0, waRequest::TYPE_INT);
     if ($id) {
         $photo_rights_model = new photosPhotoRightsModel();
         if (!$photo_rights_model->checkRights($id, true)) {
             throw new waException(_w("You don't have sufficient access rights"));
         }
         $photo_model = new photosPhotoModel();
         $photo_model->moveStackSort($id, $before_id);
         $photo = $photo_model->getById($id);
         if ($stack = $photo_model->getStack($id, array('thumb' => true))) {
             $this->response['stack'] = $stack;
         }
     }
 }
 public function execute()
 {
     if (!$this->getUser()->getId()) {
         $this->errors[] = sprintf_wp("Please %ssign in%s to be able to vote for photos", '<a href="' . wa()->getRouteUrl('/login', null, true) . '">', '</a>');
         return;
     }
     $plugin = wa()->getPlugin('publicgallery');
     $photo_id = waRequest::post('photo_id', null, waRequest::TYPE_ARRAY_INT);
     $allowed_photo_id = $this->filterAllowedPhotoIds($photo_id);
     if (!$allowed_photo_id) {
         return;
     }
     $vote_model = new photosPublicgalleryVoteModel();
     $photo_model = new photosPhotoModel();
     if (wa()->getEnv() == 'frontend' && !$plugin->getSettings('self_vote')) {
         $photo = $photo_model->getById($allowed_photo_id);
         if (!$photo) {
             $this->errors[] = _w("Photo not found");
             return;
         }
         $photo = reset($photo);
         if ($photo && $photo['contact_id'] == wa()->getUser()->getId()) {
             $this->errors[] = _wp("You may not vote for your own photos");
             return;
         }
     }
     $vote = (int) waRequest::post('rate', 0);
     if ($vote > 0) {
         $vote_model->vote($allowed_photo_id, $vote);
     } else {
         $vote_model->clearVote($allowed_photo_id);
     }
     $this->response['photos'] = $photo_model->select('id, rate, votes_count')->where("id IN (" . implode(',', $photo_id) . ")")->fetchAll();
     foreach ($this->response['photos'] as &$p) {
         if ($p['votes_count']) {
             $p['votes_count_text'] = _wp('%d vote', '%d votes', $p['votes_count']);
         } else {
             $p['votes_count_text'] = '';
         }
     }
     unset($p);
     $this->response['count'] = $photo_model->countRated();
     if (count($photo_id) == 1) {
         $this->response['you_voted'] = (int) $vote_model->getByField(array('photo_id' => $photo_id[0], 'contact_id' => wa()->getUser()->getId()));
     }
 }
 public function execute()
 {
     if ($id = waRequest::get('id', waRequest::TYPE_INT)) {
         $photo_model = new photosPhotoModel();
         $photo = $photo_model->getById($id);
         $album_photos_model = new photosAlbumPhotosModel();
         $photo_albums = $album_photos_model->getByPhoto($id);
     } else {
         $photo = null;
         $photo_albums = array();
     }
     $this->view->assign('photo_albums', $photo_albums);
     $album_model = new photosAlbumModel();
     $albums = $album_model->getAlbums(false, photosAlbumModel::TYPE_STATIC, $this->getRights('edit') ? false : true, false);
     $this->view->assign('albums', $albums);
     $this->view->assign('photo', $photo);
 }
 public function execute()
 {
     $album_id = waRequest::post('album_id', 0, 'int');
     $photo_id = waRequest::post('photo_id', 0, 'int');
     if (!$album_id || !$photo_id) {
         throw new waException('Bad parameters', 404);
     }
     $album_rights_model = new photosAlbumRightsModel();
     if (!$album_rights_model->checkRights($album_id, true)) {
         throw new waException(_w("You don't have sufficient access rights"));
     }
     $photo_model = new photosPhotoModel();
     $photo = $photo_model->getById($photo_id);
     if (!$photo) {
         $this->errors[] = _w('Photo not found');
         return;
     }
     $album_model = new photosAlbumModel();
     $album_model->updateById($album_id, array('key_photo_id' => $photo_id));
     photosPhoto::generateThumbs($photo, array('192x192'));
 }
 public function execute()
 {
     $id = $this->post('id', true);
     $photo_model = new photosPhotoModel();
     $photo = $photo_model->getById($id);
     $clockwise = waRequest::post('clockwise', null, 1);
     if (!is_numeric($clockwise)) {
         $clockwise = strtolower(trim($clockwise));
         $clockwise = $clockwise === 'false' ? 0 : 1;
     }
     if ($photo) {
         try {
             $photo_model = new photosPhotoModel();
             $photo_model->rotate($id, $clockwise);
         } catch (waException $e) {
             throw new waAPIException('server_error', $e->getMessage(), 500);
         }
         $this->response = true;
     } else {
         throw new waAPIException('invalid_request', 'Photo not found', 404);
     }
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Can't rotate photo");
     }
     $direction = waRequest::post('direction', 'left', waRequest::TYPE_STRING_TRIM);
     $photo_model = new photosPhotoModel();
     $photo_model->rotate($id, $direction == 'right');
     $photo = $photo_model->getById($id);
     $photo['thumb'] = photosPhoto::getThumbInfo($photo, photosPhoto::getThumbPhotoSize());
     $photo['thumb_big'] = photosPhoto::getThumbInfo($photo, photosPhoto::getBigPhotoSize());
     $photo['thumb_middle'] = photosPhoto::getThumbInfo($photo, photosPhoto::getMiddlePhotoSize());
     $original_photo_path = photosPhoto::getOriginalPhotoPath($photo);
     if (wa('photos')->getConfig()->getOption('save_original') && file_exists($original_photo_path)) {
         $photo['original_exists'] = true;
     } else {
         $photo['original_exists'] = false;
     }
     $this->log('photo_edit', 1);
     $this->response['photo'] = $photo;
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     $filter = waRequest::post('filter', 'grayscale', waRequest::TYPE_STRING_TRIM);
     if (!$id) {
         throw new waException(_w("Can't apply a filter to photo: unknown photo id"));
     }
     if (!isset($this->filters[$filter])) {
         throw new waException(_w("Can't apply a filter to photo: unknown filter"));
     }
     $plugin = wa('photos')->getPlugin('imageeffects');
     $filter_params = $plugin->getSettings($filter);
     $filter_params = $filter_params ? $filter_params : array();
     $filter = $this->filters[$filter];
     $photo_model = new photosPhotoModel();
     $photo_rights_model = new photosPhotoRightsModel();
     $photo = $photo_model->getById($id);
     $photo_rights_model = new photosPhotoRightsModel();
     if (!$photo_rights_model->checkRights($photo, true)) {
         throw new waException(_w("You don't have sufficient access rights"));
     }
     $photo_path = photosPhoto::getPhotoPath($photo);
     $image = new photosImage($photo_path);
     if ($image->filter($filter, $filter_params)->save()) {
         waFiles::delete(photosPhoto::getPhotoThumbDir($photo));
         $edit_datetime = date('Y-m-d H:i:s');
         $photo_model->updateById($id, array('edit_datetime' => $edit_datetime));
         $photo['edit_datetime'] = $edit_datetime;
         $original_photo_path = photosPhoto::getOriginalPhotoPath($photo);
         if (wa('photos')->getConfig()->getOption('save_original') && file_exists($original_photo_path)) {
             $photo['original_exists'] = true;
         } else {
             $photo['original_exists'] = false;
         }
         $this->response['photo'] = $photo;
         $this->log('photo_edit', 1);
     }
 }
 public function execute()
 {
     $this->photo_ids = waRequest::post('photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $album_id = waRequest::post('album_id', array(), waRequest::TYPE_ARRAY_INT);
     $copy = waRequest::post('copy', 1, waRequest::TYPE_INT);
     $this->album_photos_model = new photosAlbumPhotosModel();
     $photo_rights_model = new photosPhotoRightsModel();
     if (!$copy) {
         // it means manage with one photo
         $photo_id = $this->photo_ids[0];
         if (!$photo_rights_model->checkRights($photo_id, true)) {
             throw new waException("You don't have sufficient access rights");
         }
         $early_albums = array_keys($this->album_photos_model->getByField('photo_id', $photo_id, 'album_id'));
         // TODO: check rights for editing (take into account deleting!)
         $this->album_photos_model->set($photo_id, $album_id);
         $this->log('photos_move', 1);
         $albums = $this->getAlbumsCounters();
         $old_albums = array();
         foreach ($early_albums as $a_id) {
             if (!isset($albums[$a_id])) {
                 $collection = new photosCollection('/album/' . $a_id);
                 $album = array('id' => $a_id, 'count' => $collection->count(), 'count_new' => 0);
                 $old_albums[] = $album;
             }
         }
         $this->response['albums'] = array_values($albums);
         $this->response['old_albums'] = $old_albums;
     } else {
         // otherwise copy photos to albums
         $allowed_photo_id = $photo_rights_model->filterAllowedPhotoIds($this->photo_ids, true);
         $denied_photo_id = array_values(array_diff($this->photo_ids, $allowed_photo_id));
         $album_rights_model = new photosAlbumRightsModel();
         $allowed_album_id = $album_rights_model->filterAllowedAlbumIds($album_id, true);
         $denied_album_id = array_values(array_diff($album_id, $allowed_album_id));
         if ($allowed_album_id && $allowed_photo_id) {
             $this->album_photos_model->add($allowed_photo_id, $allowed_album_id);
             $this->log('photos_move', 1);
         }
         $albums = $this->getAlbumsCounters();
         $this->response['albums'] = array_values($albums);
         if ($denied_photo_id) {
             $this->response['alert_msg'] = photosPhoto::sprintf_wplural("The operation was not performed to %d photo (%%s)", "The operation was not performed to %d photos (%%s)", count($denied_photo_id), _w("out of %d selected", "out of %d selected", count($this->photo_ids))) . ', ' . _w("because you don't have sufficient access rights") . '.';
         }
     }
     // Set cover photos for albums if first photo just been added to it
     $photo_model = new photosPhotoModel();
     $album_model = new photosAlbumModel();
     $allowed_photo_id = isset($allowed_photo_id) ? $allowed_photo_id : array($photo_id);
     $no_cover_album_ids = array();
     foreach (ifset($allowed_album_id, $album_id) as $album_id) {
         if (!empty($albums[$album_id]) && empty($albums[$album_id]['key_photo_id'])) {
             $no_cover_album_ids[] = $album_id;
         }
     }
     $photos = array();
     while ($allowed_photo_id && $no_cover_album_ids) {
         // Get random photo from added and make sure it exists
         shuffle($allowed_photo_id);
         $photo_id = array_pop($allowed_photo_id);
         if (!empty($photos[$photo_id])) {
             $photo = $photos[$photo_id];
         } else {
             $photos[$photo_id] = $photo = $photo_model->getById($photo_id);
         }
         if (!$photo) {
             continue;
         }
         // Photo exists, so add it back
         $allowed_photo_id[] = $photo_id;
         // Set cover for one album
         $album_id = array_pop($no_cover_album_ids);
         $album_model->updateById($album_id, array('key_photo_id' => $photo_id));
         photosPhoto::generateThumbs($photo, array('192x192'));
     }
 }
 public function execute()
 {
     $photo_id = waRequest::post('photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $status = waRequest::post('status', 0, waRequest::TYPE_INT);
     $groups = waRequest::post('groups', array(), waRequest::TYPE_ARRAY_INT);
     if (!$groups) {
         $status = -1;
         // only author have access to this photo
         $groups = array(-$this->getUser()->getId());
     }
     // necessary when manage access rights for one photo. When in one photo extra info is needed in response
     $is_one_photo = waRequest::post('one_photo', 0, waRequest::TYPE_INT);
     // necessary only when manage access rights for several photos
     $prev_allowed_photo_id = waRequest::post('allowed_photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $prev_denied_photo_id = waRequest::post('denied_photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $this->photo_model = new photosPhotoModel();
     $this->photo_rights_model = new photosPhotoRightsModel();
     $allowed_photo_id = $this->photo_rights_model->filterAllowedPhotoIds($photo_id, true);
     $denied_photo_id = array_diff($photo_id, $allowed_photo_id);
     $this->photo_model->updateAccess($allowed_photo_id, $status, $groups);
     // leave only id of parents
     $denied_parent_id = array();
     if ($denied_photo_id) {
         foreach ($this->photo_model->getByField('id', $denied_photo_id, 'id') as $photo) {
             $denied_parent_id[] = $photo['parent_id'] > 0 ? $photo['parent_id'] : $photo['id'];
         }
     }
     $denied_photo_id = array_values(array_unique(array_merge($prev_denied_photo_id, $denied_parent_id)));
     $this->response['denied_photo_id'] = $denied_photo_id;
     // leave only id of parents
     $allowed_parent_id = array();
     if ($allowed_photo_id) {
         foreach ($this->photo_model->getByField('id', $allowed_photo_id, 'id') as $photo) {
             $allowed_parent_id[] = $photo['parent_id'] > 0 ? $photo['parent_id'] : $photo['id'];
         }
     }
     $allowed_photo_id = array_values(array_unique(array_merge($prev_allowed_photo_id, $allowed_parent_id)));
     $this->response['allowed_photo_id'] = $allowed_photo_id;
     $all_photos_length = waRequest::post('photos_length', 0, waRequest::TYPE_INT);
     if (!$all_photos_length) {
         $all_photos_length = count($photo_id);
     }
     $denied_photos_length = count($denied_photo_id);
     if ($denied_photos_length > 0 && $all_photos_length > 0) {
         $this->response['alert_msg'] = photosPhoto::sprintf_wplural("The operation was not performed to %d photo (%%s)", "The operation was not performed to %d photos (%%s)", $denied_photos_length, _w("out of %d selected", "out of %d selected", $all_photos_length)) . ', ' . _w("because you don't have sufficient access rights") . '.';
     }
     // if one photo send extra info for update cache and widget
     if ($is_one_photo && $allowed_photo_id) {
         $frontend_link_template = photosFrontendPhoto::getLink(array('url' => '%url%'));
         if (count($photo_id) > 1) {
             // stack
             $stack = $this->photo_model->getStack($photo_id[0]);
             foreach ($stack as &$photo) {
                 $photo = $this->workup($photo);
             }
             unset($photo);
             $this->response['stack'] = array_values($stack);
         } else {
             // just photo
             $photo_id = $photo_id[0];
             $photo = $this->photo_model->getById($photo_id);
             $photo = $this->workup($photo);
             $this->response['photo'] = $photo;
         }
         $this->response['frontend_link_template'] = $frontend_link_template;
     }
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Can't restore photo");
     }
     $photo_model = new photosPhotoModel();
     $photo_rights_model = new photosPhotoRightsModel();
     $photo = $photo_model->getById($id);
     if (!$photo_rights_model->checkRights($photo, true)) {
         throw new waException("You don't have sufficient access rights");
     }
     $original_photo_path = photosPhoto::getOriginalPhotoPath($photo);
     if (!wa('photos')->getConfig()->getOption('save_original') || !file_exists($original_photo_path)) {
         throw new waException("Can't restore photo. Original photo doesn't exist");
     }
     $paths = array();
     try {
         $photo_path = photosPhoto::getPhotoPath($photo);
         $backup_photo_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $photo_path);
         if (waFiles::move($photo_path, $backup_photo_path)) {
             if (!waFiles::move($original_photo_path, $photo_path)) {
                 if (!waFiles::move($backup_photo_path, $photo_path)) {
                     throw new waException("Error while restore. Current file corupted but backuped");
                 }
                 $paths[] = $backup_photo_path;
                 throw new waException("Error while restore. Operation canceled");
             } else {
                 $image = new photosImage($photo_path);
                 $edit_datetime = date('Y-m-d H:i:s');
                 $data = array('edit_datetime' => $edit_datetime, 'width' => $image->width, 'height' => $image->height);
                 $photo_model->updateById($id, $data);
                 $photo = array_merge($photo, $data);
                 $thumb_dir = photosPhoto::getPhotoThumbDir($photo);
                 $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
                 $paths[] = $back_thumb_dir;
                 waFiles::delete($back_thumb_dir);
                 // old backups
                 if (!waFiles::move($thumb_dir, $back_thumb_dir) && !waFiles::delete($thumb_dir)) {
                     throw new waException("Error while rebuild thumbnails");
                 }
                 $photo['original_exists'] = false;
                 $photo['thumb'] = photosPhoto::getThumbInfo($photo, photosPhoto::getThumbPhotoSize());
                 $photo['thumb_big'] = photosPhoto::getThumbInfo($photo, photosPhoto::getBigPhotoSize());
                 $photo['thumb_middle'] = photosPhoto::getThumbInfo($photo, photosPhoto::getMiddlePhotoSize());
                 $sizes = $this->getConfig()->getSizes();
                 try {
                     photosPhoto::generateThumbs($photo, $sizes);
                 } catch (Exception $e) {
                     waLog::log($e->getMessage());
                 }
                 $this->response['photo'] = $photo;
                 $this->log('photo_reverttooriginal', 1);
             }
         } else {
             throw new waException("Error while restore. Operation canceled");
         }
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
     } catch (Exception $e) {
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
         throw $e;
     }
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Can't rotate photo");
     }
     $direction = waRequest::post('direction', 'left', waRequest::TYPE_STRING_TRIM);
     if (isset($this->derection_angles[$direction])) {
         $photo_model = new photosPhotoModel();
         $photo_rights_model = new photosPhotoRightsModel();
         $photo = $photo_model->getById($id);
         if (!$photo_rights_model->checkRights($photo, true)) {
             throw new waException(_w("You don't have sufficient access rights"));
         }
         $photo_path = photosPhoto::getPhotoPath($photo);
         $paths = array();
         try {
             $image = new photosImage($photo_path);
             $result_photo_path = preg_replace('/(\\.[^\\.]+)$/', '.result$1', $photo_path);
             $backup_photo_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $photo_path);
             $paths[] = $result_photo_path;
             $result = $image->rotate($this->derection_angles[$direction])->save($result_photo_path);
             if ($result) {
                 $count = 0;
                 while (!file_exists($result_photo_path) && ++$count < 5) {
                     sleep(1);
                 }
                 if (!file_exists($result_photo_path)) {
                     throw new waException("Error while rotate. I/O error");
                 }
                 $paths[] = $backup_photo_path;
                 if (waFiles::move($photo_path, $backup_photo_path)) {
                     if (!waFiles::move($result_photo_path, $photo_path)) {
                         if (!waFiles::move($backup_photo_path, $photo_path)) {
                             throw new waException("Error while rotate. Original file corupted but backuped");
                         }
                         throw new waException("Error while rotate. Operation canceled");
                     } else {
                         $edit_datetime = date('Y-m-d H:i:s');
                         $data = array('edit_datetime' => $edit_datetime, 'width' => $photo['height'], 'height' => $photo['width']);
                         $photo_model->updateById($id, $data);
                         $photo = array_merge($photo, $data);
                         $thumb_dir = photosPhoto::getPhotoThumbDir($photo);
                         $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
                         $paths[] = $back_thumb_dir;
                         waFiles::delete($back_thumb_dir);
                         if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) {
                             throw new waException("Error while rebuild thumbnails");
                         }
                     }
                     $photo['thumb'] = photosPhoto::getThumbInfo($photo, photosPhoto::getThumbPhotoSize());
                     $photo['thumb_big'] = photosPhoto::getThumbInfo($photo, photosPhoto::getBigPhotoSize());
                     $photo['thumb_middle'] = photosPhoto::getThumbInfo($photo, photosPhoto::getMiddlePhotoSize());
                     $original_photo_path = photosPhoto::getOriginalPhotoPath($photo);
                     if (wa('photos')->getConfig()->getOption('save_original') && file_exists($original_photo_path)) {
                         $photo['original_exists'] = true;
                     } else {
                         $photo['original_exists'] = false;
                     }
                     $this->response['photo'] = $photo;
                     $this->log('photo_edit', 1);
                     $obligatory_sizes = $this->getConfig()->getSizes();
                     try {
                         photosPhoto::generateThumbs($photo, $obligatory_sizes);
                     } catch (Exception $e) {
                         waLog::log($e->getMessage());
                     }
                 } else {
                     throw new waException("Error while rotate. Operation canceled");
                 }
             }
             foreach ($paths as $path) {
                 waFiles::delete($path);
             }
         } catch (Exception $e) {
             foreach ($paths as $path) {
                 waFiles::delete($path);
             }
             throw $e;
         }
     }
 }
 public function backendPhoto($photo_id)
 {
     $photo_model = new photosPhotoModel();
     $photo = $photo_model->getById($photo_id);
     if ($photo) {
         $votes_count_text = '';
         if ($photo['votes_count'] > 0) {
             $votes_count_text = _wp('%d vote', '%d votes', $photo['votes_count']);
         }
         $vote_model = new photosPublicgalleryVoteModel();
         $vote_item = $vote_model->getByField(array('photo_id' => $photo['id'], 'contact_id' => wa()->getUser()->getId()));
         $your_rate = 0;
         if ($vote_item) {
             $your_rate = $vote_item['rate'];
         }
         $html = '<a class="hint" href="javascript:void(0);" id="photo-rate-votes-count" data-you-voted="' . (int) ($your_rate > 0) . '"><u>' . $votes_count_text . '</u></a>' . '<span id="p-your-rate-wrapper">' . _wp('My vote: ') . '<a href="javascript:void(0);" id="your-rate" class="p-rate-photo" data-rate="' . $your_rate . '">' . photosPhoto::getRatingHtml($your_rate, 10, true) . '</a></span>' . '<a class="inline-link p-rate-clear small" href="javascript:void(0);" style="display:none;" id="clear-photo-rate"><b><i>' . _wp('cancel my vote') . '</b></i></a>';
         $html .= '<script>$.photos.publicgalleryInitYourRate();</script>';
         return array('after_rate' => $html);
     }
 }
 public function explainLogs($logs)
 {
     $logs = parent::explainLogs($logs);
     $photo_ids = array();
     foreach ($logs as $l_id => $l) {
         if (in_array($l['action'], array('photos_upload')) && $l['params']) {
             $photo_ids = array_merge($photo_ids, explode(',', $l['params']));
         }
     }
     if ($photo_ids) {
         $photo_model = new photosPhotoModel();
         $photos = $photo_model->getById($photo_ids);
     }
     foreach ($logs as $l_id => $l) {
         if (in_array($l['action'], array('photos_upload'))) {
             $ids = explode(',', $l['params']);
             $html = '';
             foreach ($ids as $id) {
                 if (!empty($photos[$id])) {
                     $p = $photos[$id];
                     $url = wa()->getConfig()->getBackendUrl(true) . $l['app_id'] . '/#/photo/' . $id . '/';
                     $_is_2x_enabled = $this->getOption('enable_2x');
                     $_image_size = '96x96';
                     if ($_is_2x_enabled) {
                         $_image_size = '96x96@2x';
                     }
                     $img = photosPhoto::getEmbedImgHtml($p, $_image_size);
                     $html .= '<div class="photo-item"><a href="' . $url . '">' . $img . '</a></div>';
                 }
             }
             if ($html) {
                 $logs[$l_id]['params_html'] = '<div class="activity-photo-wrapper"><div class="activity-photo-list">' . $html . '</div></div>';
             }
         }
     }
     return $logs;
 }
 public function execute()
 {
     $id = waRequest::post('id', 0, waRequest::TYPE_INT);
     $in_stack = waRequest::post('in_stack', 0, waRequest::TYPE_INT);
     $hash = waRequest::post('hash', null, waRequest::TYPE_STRING_TRIM);
     $hash = urldecode($hash);
     // get photo
     $this->photo_model = new photosPhotoModel();
     $this->photo = $this->photo_model->getById($id);
     if (!$this->photo) {
         throw new waException(_w("Photo doesn't exists"), 404);
     }
     $photo_rights_model = new photosPhotoRightsModel();
     if (!$photo_rights_model->checkRights($this->photo)) {
         throw new waRightsException(_w("You don't have sufficient access rights"));
     }
     $this->photo['name_not_escaped'] = $this->photo['name'];
     $this->photo = photosPhoto::escapeFields($this->photo);
     $this->photo['upload_datetime_formatted'] = waDateTime::format('humandate', $this->photo['upload_datetime']);
     $this->photo['upload_timestamp'] = strtotime($this->photo['upload_datetime']);
     $this->photo['edit_rights'] = $photo_rights_model->checkRights($this->photo, true);
     $this->photo['private_url'] = photosPhotoModel::getPrivateUrl($this->photo);
     $this->photo['thumb'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getThumbPhotoSize());
     $this->photo['thumb_big'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getBigPhotoSize());
     $this->photo['thumb_middle'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getMiddlePhotoSize());
     $original_photo_path = photosPhoto::getOriginalPhotoPath($this->photo);
     if (wa('photos')->getConfig()->getOption('save_original') && file_exists($original_photo_path)) {
         $this->photo['original_exists'] = true;
     } else {
         $this->photo['original_exists'] = false;
     }
     $photo_tags_model = new photosPhotoTagsModel();
     $tags = $photo_tags_model->getTags($id);
     $this->photo['tags'] = $tags;
     $this->response['photo'] = $this->photo;
     // get stack if it's possible
     if (!$in_stack && ($stack = $this->photo_model->getStack($id, array('thumb' => true, 'thumb_crop' => true, 'thumb_big' => true, 'thumb_middle' => true)))) {
         $this->response['stack'] = $stack;
     }
     // get albums
     $album_photos_model = new photosAlbumPhotosModel();
     $albums = $album_photos_model->getAlbums($id, array('id', 'name'));
     $this->response['albums'] = isset($albums[$id]) ? array_values($albums[$id]) : array();
     // exif info
     $exif_model = new photosPhotoExifModel();
     $exif = $exif_model->getByPhoto($this->photo['id']);
     if (isset($exif['DateTimeOriginal'])) {
         $exif['DateTimeOriginal'] = waDateTime::format('humandatetime', $exif['DateTimeOriginal'], date_default_timezone_get());
     }
     $this->response['exif'] = $exif;
     // get author
     $contact = new waContact($this->photo['contact_id']);
     $this->response['author'] = array('id' => $contact['id'], 'name' => photosPhoto::escape($contact['name']), 'photo_url' => $contact->getPhoto(photosPhoto::AUTHOR_PHOTO_SIZE), 'backend_url' => $this->getConfig()->getBackendUrl(true) . 'contacts/#/contact/' . $contact['id']);
     // for making inline-editable widget
     $this->response['frontend_link_template'] = photosFrontendPhoto::getLink(array('url' => '%url%'));
     $hooks = array();
     $parent_id = $this->photo_model->getStackParentId($this->photo);
     $photo_id = $parent_id ? $parent_id : $id;
     /**
      * Extend photo page
      * Add extra widget(s)
      * @event backend_photo
      * @return array[string][string]string $return[%plugin_id%]['bottom'] In bottom, under photo any widget
      */
     $hooks['backend_photo'] = wa()->event('backend_photo', $photo_id);
     $this->response['hooks'] = $hooks;
     if ($hash !== null) {
         $collection = new photosCollection($hash);
         if (strstr($hash, 'rate>0') !== false) {
             $collection->orderBy('p.rate DESC, p.id');
         }
         $this->response['photo_stream'] = $this->getPhotoStream($collection);
         if ($collection->getAlbum()) {
             $this->response['album'] = $collection->getAlbum();
         }
     }
 }