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 = 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 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()
 {
     $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'));
     }
 }
 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 . '/');
 }
Пример #6
0
 public function rotate($id, $clockwise = true)
 {
     $photo_rights_model = new photosPhotoRightsModel();
     $photo = $this->getById($id);
     if (!$photo || !$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;
         $angle = $clockwise ? '90' : '-90';
         $result = $image->rotate($angle)->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']);
                     $this->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");
                     }
                 }
                 $obligatory_sizes = wa('photos')->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;
     }
 }