public function execute()
 {
     $photo_id = $this->post('id', true);
     if (!is_array($photo_id)) {
         if (strpos($photo_id, ',') !== false) {
             $photo_id = array_map('intval', explode(',', $photo_id));
         } else {
             $photo_id = array($photo_id);
         }
     }
     $album_id = waRequest::post('album_id', '');
     if (!$album_id) {
         $album_id = array();
     }
     if (!is_array($album_id)) {
         if (strpos($album_id, ',') !== false) {
             $album_id = explode(',', $album_id);
         } else {
             $album_id = array($album_id);
         }
     }
     $album_id = array_map('trim', $album_id);
     $album_photos_model = new photosAlbumPhotosModel();
     $photo_rights_model = new photosPhotoRightsModel();
     $allowed_photo_id = $photo_rights_model->filterAllowedPhotoIds($photo_id, true);
     if ($allowed_photo_id) {
         $album_photos_model->deletePhotos($album_id, $allowed_photo_id);
         $this->response = true;
     } else {
         throw new waAPIException('access_denied', 403);
     }
 }
 public function execute()
 {
     $routes = $this->getRoutes();
     $app_id = wa()->getApp();
     $album_model = new photosAlbumModel();
     $album_photos_model = new photosAlbumPhotosModel();
     $page_model = new photosPageModel();
     $real_domain = $this->routing->getDomain(null, true, false);
     foreach ($routes as $route) {
         $this->routing->setRoute($route);
         $albums = $album_model->getByField(array('type' => photosAlbumModel::TYPE_STATIC, 'status' => 1), 'id');
         $favorites_lastmod_time = null;
         // albums and photos in albums
         if ($albums) {
             $current_album_id = null;
             $current_album_lastmod_time = null;
             foreach ((array) $album_photos_model->getPhotos(array_keys($albums)) as $photo) {
                 if ($photo['album_id'] != $current_album_id) {
                     if ($current_album_id) {
                         $this->addUrl(photosFrontendAlbum::getLink($albums[$current_album_id]), $current_album_lastmod_time);
                     }
                     $current_album_id = $photo['album_id'];
                 }
                 $photo_url = photosFrontendPhoto::getLink($photo, $albums[$current_album_id]);
                 $lastmod_time = max($photo['edit_datetime'], $photo['upload_datetime']);
                 $this->addUrl($photo_url, $lastmod_time);
                 $current_album_lastmod_time = max($current_album_lastmod_time, $lastmod_time);
                 if ($photo['rate'] > 0) {
                     $favorites_lastmod_time = max($favorites_lastmod_time, $lastmod_time);
                 }
             }
         }
         // just photos (that aren't inside any album)
         foreach ((array) $album_photos_model->getPhotos() as $photo) {
             $photo_url = photosFrontendPhoto::getLink($photo);
             $lastmod_time = max($photo['edit_datetime'], $photo['upload_datetime']);
             $this->addUrl($photo_url, $lastmod_time);
             if ($photo['rate'] > 0) {
                 $favorites_lastmod_time = max($favorites_lastmod_time, $lastmod_time);
             }
         }
         // favorite page
         $this->addUrl(photosCollection::getFrontendLink('favorites', false), $favorites_lastmod_time ? $favorites_lastmod_time : time());
         // pages
         $main_url = wa()->getRouteUrl($app_id . "/frontend", array(), true, $real_domain);
         $domain = $this->routing->getDomain(null, true);
         $sql = "SELECT full_url, url, create_datetime, update_datetime FROM " . $page_model->getTableName() . '
                 WHERE status = 1 AND domain = s:domain AND route = s:route';
         $pages = $page_model->query($sql, array('domain' => $domain, 'route' => $route['url']))->fetchAll();
         foreach ($pages as $p) {
             $this->addUrl($main_url . $p['full_url'], $p['update_datetime'] ? $p['update_datetime'] : $p['create_datetime'], self::CHANGE_MONTHLY, 0.6);
         }
         // main page
         $this->addUrl($main_url, time(), self::CHANGE_DAILY, 1.0);
     }
 }
 public function execute()
 {
     $album_id = waRequest::get('id', null, waRequest::TYPE_INT);
     // check rights
     $album_rights_model = new photosAlbumRightsModel();
     if (!$album_rights_model->checkRights($album_id, true)) {
         throw new waRightsException(_w("Access denied"));
     }
     $photo_id = waRequest::post('photo_id', null, waRequest::TYPE_ARRAY_INT);
     $album_photos_model = new photosAlbumPhotosModel();
     $album_photos_model->deletePhotos($album_id, $photo_id);
 }
 public function execute()
 {
     $album_id = waRequest::post('album_id', null, waRequest::TYPE_INT);
     if ($album_id) {
         $album_photos_model = new photosAlbumPhotosModel();
         $this->response['photo_id'] = array_keys($album_photos_model->getByField('album_id', $album_id, 'photo_id'));
     } else {
         $photo_model = new photosPhotoModel();
         $photo_id = waRequest::post('photo_id', null, waRequest::TYPE_ARRAY_INT);
         $this->response['photo_id'] = array_keys($photo_model->getPhotos($photo_id));
     }
 }
 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()
 {
     $photo_id = waRequest::post('photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $album_id = waRequest::post('album_id', null, waRequest::TYPE_INT);
     $before_id = waRequest::post('before_id', null, waRequest::TYPE_INT);
     if (!$photo_id || !$album_id) {
         throw new waException(_w("Can't move photo"));
     }
     $album_rights_model = new photosAlbumRightsModel();
     if (!$album_rights_model->checkRights($album_id, true)) {
         throw new waException(_w("You don't have sufficient access rights"));
     }
     if ($photo_id && $album_id) {
         $album_photos_model = new photosAlbumPhotosModel();
         $album_photos_model->movePhoto($photo_id, $album_id, $before_id);
     }
 }
 public function execute()
 {
     $routes = $this->getRoutes();
     $app_id = wa()->getApp();
     $album_model = new photosAlbumModel();
     $album_photos_model = new photosAlbumPhotosModel();
     foreach ($routes as $route) {
         $this->routing->setRoute($route);
         $albums = $album_model->getByField(array('type' => photosAlbumModel::TYPE_STATIC, 'status' => 1), 'id');
         $favorites_lastmod_time = null;
         // albums and photos in albums
         if ($albums) {
             $current_album_id = null;
             $current_album_lastmod_time = null;
             foreach ((array) $album_photos_model->getPhotos(array_keys($albums)) as $photo) {
                 if ($photo['album_id'] != $current_album_id) {
                     if ($current_album_id) {
                         $this->addUrl(photosFrontendAlbum::getLink($albums[$current_album_id]), $current_album_lastmod_time);
                     }
                     $current_album_id = $photo['album_id'];
                 }
                 $photo_url = photosFrontendPhoto::getLink($photo, $albums[$current_album_id]);
                 $lastmod_time = max($photo['edit_datetime'], $photo['upload_datetime']);
                 $this->addUrl($photo_url, $lastmod_time);
                 $current_album_lastmod_time = max($current_album_lastmod_time, $lastmod_time);
                 if ($photo['rate'] > 0) {
                     $favorites_lastmod_time = max($favorites_lastmod_time, $lastmod_time);
                 }
             }
         }
         // just photos (that aren't inside any album)
         foreach ((array) $album_photos_model->getPhotos() as $photo) {
             $photo_url = photosFrontendPhoto::getLink($photo);
             $lastmod_time = max($photo['edit_datetime'], $photo['upload_datetime']);
             $this->addUrl($photo_url, $lastmod_time);
             if ($photo['rate'] > 0) {
                 $favorites_lastmod_time = max($favorites_lastmod_time, $lastmod_time);
             }
         }
         // favorite page
         $this->addUrl(photosCollection::getFrontendLink('favorites', false), $favorites_lastmod_time);
         // main page
         wa()->getRouteUrl($app_id . "/frontend", array(), true);
     }
 }
 private function getAlbumsCounters()
 {
     $config = $this->getConfig();
     $last_activity_datetime = $config->getLastLoginTime();
     $albums = array();
     $photo_albums = $this->album_photos_model->getAlbums($this->photo_ids, array('id', 'name'));
     foreach ($photo_albums as &$p_albums) {
         foreach ($p_albums as $a_id => &$album) {
             if (!isset($albums[$a_id])) {
                 $collection = new photosCollection('/album/' . $a_id);
                 $album['count'] = $collection->count();
                 //$album['count_new'] = XXX;
                 $album['count_new'] = 0;
                 $albums[$a_id] = $album;
             }
         }
         unset($album);
     }
     unset($p_albums);
     return $albums;
 }
 private function isNeedNofollow()
 {
     if ($this->album && $this->album['status'] == 1) {
         return false;
     }
     if (empty($this->hash)) {
         $album_photos_model = new photosAlbumPhotosModel();
         return (bool) $album_photos_model->countByField('photo_id', $this->photo['id']);
     }
     return true;
 }
 private function renderAlbums()
 {
     $album_photos_model = new photosAlbumPhotosModel();
     $albums = $album_photos_model->getAlbums($this->photo['id'], array('id', 'name', 'full_url'), true);
     $albums = isset($albums[$this->photo['id']]) ? $albums[$this->photo['id']] : array();
     return $this->renderer->getAlbums($albums);
 }
 protected function save(waRequestFile $file)
 {
     // check image
     if (!($image = $file->waImage())) {
         throw new waException(_w('Incorrect image'));
     }
     $exif_data = photosExif::getInfo($file->tmp_name);
     $image_changed = false;
     if (!empty($exif_data['Orientation'])) {
         $image_changed = $this->correctOrientation($exif_data['Orientation'], $image);
     }
     /**
      * Extend upload proccess
      * Make extra workup
      * @event photo_upload
      */
     $event = wa()->event('photo_upload', $image);
     if ($event && !$image_changed) {
         foreach ($event as $plugin_id => $result) {
             if ($result) {
                 $image_changed = true;
                 break;
             }
         }
     }
     $data = array('name' => preg_replace('/\\.[^\\.]+$/', '', basename($file->name)), 'ext' => $file->extension, 'size' => $file->size, 'type' => $image->type, 'width' => $image->width, 'height' => $image->height, 'contact_id' => $this->getUser()->getId(), 'status' => $this->status, 'upload_datetime' => date('Y-m-d H:i:s'));
     if ($this->status <= 0) {
         $data['hash'] = md5(uniqid(time(), true));
     }
     $photo_id = $data['id'] = $this->model->insert($data);
     if (!$photo_id) {
         throw new waException(_w('Database error'));
     }
     // update url
     $url = $this->generateUrl($data['name'], $photo_id);
     $this->model->updateById($photo_id, array('url' => $url));
     // check rigths to upload folder
     $photo_path = photosPhoto::getPhotoPath($data);
     if (file_exists($photo_path) && !is_writable($photo_path) || !file_exists($photo_path) && !waFiles::create($photo_path)) {
         $this->model->deleteById($photo_id);
         throw new waException(sprintf(_w("The insufficient file write permissions for the %s folder."), substr($photo_path, strlen($this->getConfig()->getRootPath()))));
     }
     if ($image_changed) {
         $image->save($photo_path);
         // save original
         if ($this->getConfig()->getOption('save_original')) {
             $original_file = photosPhoto::getOriginalPhotoPath($photo_path);
             $file->moveTo($original_file);
         }
     } else {
         $file->moveTo($photo_path);
     }
     unset($image);
     // free variable
     // add to album
     if ($photo_id && $this->album_id) {
         $album_photos_model = new photosAlbumPhotosModel();
         // update note if album is empty and note is yet null
         $r = $album_photos_model->getByField('album_id', $this->album_id);
         if (!$r) {
             $album_model = new photosAlbumModel();
             $sql = "UPDATE " . $album_model->getTableName() . " SET note = IFNULL(note, s:note) WHERE id = i:album_id";
             $time = !empty($exif_data['DateTimeOriginal']) ? strtotime($exif_data['DateTimeOriginal']) : time();
             $album_model->query($sql, array('note' => mb_strtolower(_ws(date('F', $time))) . ' ' . _ws(date('Y', $time)), 'album_id' => $this->album_id));
         }
         // add to album iteself
         $sort = (int) $album_photos_model->query("SELECT sort + 1 AS sort FROM " . $album_photos_model->getTableName() . " WHERE album_id = i:album_id ORDER BY sort DESC LIMIT 1", array('album_id' => $this->album_id))->fetchField('sort');
         $album_photos_model->insert(array('photo_id' => $photo_id, 'album_id' => $this->album_id, 'sort' => $sort));
     }
     // save rights for groups
     if ($this->groups) {
         $rights_model = new photosPhotoRightsModel();
         $rights_model->multiInsert(array('photo_id' => $photo_id, 'group_id' => $this->groups));
     }
     // save exif data
     if (!empty($exif_data)) {
         $exif_model = new photosPhotoExifModel();
         $exif_model->save($photo_id, $exif_data);
     }
     $sizes = $this->getConfig()->getSizes();
     photosPhoto::generateThumbs($data, $sizes);
     return array('name' => $file->name, 'type' => $file->type, 'size' => $file->size, 'thumbnail_url' => photosPhoto::getPhotoUrl($data, photosPhoto::getThumbPhotoSize()), 'url' => '#/photo/' . $photo_id . '/');
 }
 private function getPhotoOffsetCondition($photo, $order, $eq = false)
 {
     $order = trim($order);
     $order = explode(' ', $order);
     if (!isset($order[1])) {
         $order[1] = 'asc';
     }
     $order[1] = strtolower($order[1]);
     list($t, $f) = explode('.', $order[0]);
     if ($t == 'ap' && $this->update_count) {
         $model = new photosAlbumPhotosModel();
         $row = $model->getByField(array('album_id' => $this->update_count['id'], 'photo_id' => $photo['id']));
         $v = $row['sort'];
     } else {
         $v = $photo[$f];
     }
     // return condition
     return $order[0] . ($eq ? ' = ' : ($order[1] == 'asc' ? ' < ' : ' > ')) . "'{$v}'";
 }
 public function getAlbums($public_only = false, $type = null, $owned_only = false, $count = true)
 {
     $user = wa()->getUser();
     if ($count) {
         $sql = "SELECT a.*, ac.count FROM " . $this->table . " a LEFT JOIN\n            photos_album_count ac ON a.id = ac.album_id AND ac.contact_id = i:contact_id";
     } else {
         $sql = "SELECT a.* FROM " . $this->table . " a";
     }
     if ($public_only) {
         $sql .= " WHERE a.status = 1";
     } else {
         $sql .= " JOIN photos_album_rights r ON a.id = r.album_id AND ";
         if ($user->isAdmin('photos')) {
             $sql .= '(r.group_id >= 0 OR r.group_id = -i:contact_id)';
         } else {
             $group_ids = $user->getGroups();
             $group_ids[] = 0;
             $group_ids[] = -$user->getId();
             $sql .= 'r.group_id IN (' . implode(",", $group_ids) . ')';
         }
         $sql .= " WHERE 1";
         if ($type !== null) {
             $sql .= " AND a.type = " . $this->escape($type, 'int');
         }
         if ($owned_only) {
             $sql .= " AND a.contact_id = i:contact_id";
         }
     }
     $sql .= " ORDER BY parent_id, sort";
     $albums = $this->query($sql, array('contact_id' => $user->getId()))->fetchAll($this->id);
     if ($count && $user->getId()) {
         $album_photos_model = new photosAlbumPhotosModel();
         $counter = $album_photos_model->getCountByAlbum();
         foreach ($albums as $id => &$album) {
             $album['count_new'] = 0;
             if (isset($counter[$id])) {
                 $album['count_new'] = max(0, $counter[$id] - $album['count']);
             }
         }
         unset($album);
     }
     return $albums;
 }
 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();
         }
     }
 }
 private function _makeStack($parent_id, $photo_ids, $op = 'make')
 {
     $photo_ids = (array) $photo_ids;
     if ($op == 'make') {
         $where = $this->getWhereByField('id', $photo_ids);
         // get description - first not empty description but description of parent is first-priority
         $sql = "SELECT description FROM {$this->table} WHERE id = i:parent_id AND description IS NOT NULL\n                    UNION\n                    SELECT description FROM {$this->table} WHERE {$where} AND description IS NOT NULL LIMIT 0,1";
         $description = $this->query($sql, array('parent_id' => $parent_id))->fetchField('description');
         // get max rate of all photos
         $sql = "SELECT MAX(rate) rate FROM {$this->table} WHERE id = i:parent_id OR {$where}";
         $rate = $this->query($sql, array('parent_id' => $parent_id))->fetchField('rate');
         // get status
         $sql = "SELECT status FROM {$this->table} WHERE id = i:parent_id";
         $status = $this->query($sql, array('parent_id' => $parent_id))->fetchField('status');
         $stack_count = 1;
         $sort = 1;
     } else {
         $parent = $this->getById($parent_id);
         $rate = $parent['rate'];
         $description = $parent['description'];
         $status = $parent['status'];
         $stack_count = $parent['stack_count'];
         // get last sort value of stack plus 1
         $sql = "SELECT sort FROM {$this->table} WHERE parent_id = i:parent_id ORDER BY sort DESC LIMIT 1";
         $sort = $this->query($sql, array('parent_id' => $parent_id))->fetchField('sort') + 1;
     }
     // get groups
     $photo_rights_model = new photosPhotoRightsModel();
     $groups = array_keys($photo_rights_model->getByField('photo_id', $parent_id, 'group_id'));
     // make first of all operations connected with file-manipulations
     foreach ($photo_ids as $id) {
         // update access
         $this->upAccess($id, array('status' => $status, 'groups' => $groups));
     }
     // make children of stack
     foreach ($photo_ids as $id) {
         $this->updateById($id, array('parent_id' => $parent_id, 'description' => $description, 'rate' => $rate, 'sort' => $sort++, 'stack_count' => 0));
     }
     // make parent of stack
     $this->updateById($parent_id, array('parent_id' => 0, 'description' => $description, 'rate' => $rate, 'stack_count' => $stack_count + count($photo_ids), 'sort' => 0));
     if ($op == 'make') {
         $photo_ids[] = $parent_id;
     } else {
         $photo_ids = array_keys($this->getByField('parent_id', $parent_id, 'id'));
         $photo_ids[] = $parent_id;
     }
     // merge tags for stack
     $photo_tags_model = new photosPhotoTagsModel();
     $tag_ids = array_keys($photo_tags_model->getByField('photo_id', $photo_ids, 'tag_id'));
     $photo_tags_model->assign($photo_ids, $tag_ids);
     // merge albums for stack
     $album_photos_model = new photosAlbumPhotosModel();
     $album_ids = array_keys($album_photos_model->getByField('photo_id', $photo_ids, 'album_id'));
     $album_photos_model->add($photo_ids, $album_ids, false);
 }