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);
     }
 }
 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()
 {
     $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;
     }
 }
 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;
     }
 }
 private function upAccess($photo_id, $data)
 {
     $status = $data['status'];
     $groups = $data['groups'];
     $photo = $this->getById($photo_id);
     if ($photo['status'] == $status && $status > 0) {
         return;
     }
     $old_path = photosPhoto::getPhotoPath($photo);
     $old_thumbs_dir = photosPhoto::getPhotoThumbDir($photo);
     $data = array('status' => $status);
     if ($status <= 0) {
         $data['hash'] = md5(uniqid(time(), true));
     } else {
         $data['hash'] = '';
     }
     $photo = $data + $photo;
     $path = photosPhoto::getPhotoPath($photo);
     if (!rename($old_path, $path)) {
         throw new waException('error');
     }
     waFiles::delete($old_thumbs_dir);
     // strictly needed
     $this->updateById($photo_id, $data);
     $this->upRights($photo_id, $groups);
 }
 public function importPhoto($id, $album)
 {
     $this->log('Import photo: ' . $id);
     $row = $this->query("SELECT * FROM PIXLIST WHERE PL_ID = " . (int) $id);
     $data = array('name' => preg_replace('/\\.[^\\.]+$/', '', $row['PL_FILENAME']), 'description' => $row['PL_DESC'], 'upload_datetime' => $row['PL_UPLOADDATETIME'], 'width' => $row['PL_WIDTH'], 'height' => $row['PL_HEIGHT'], 'size' => $row['PL_FILESIZE'], 'ext' => waFiles::extension($row['PL_DISKFILENAME']), 'contact_id' => $this->getContactId($row['C_ID']), 'status' => $album['status'] ? 1 : 0);
     if ($data['status'] <= 0) {
         $data['hash'] = md5(uniqid(time(), true));
     } else {
         $data['hash'] = '';
     }
     // insert photo
     $data['id'] = $this->getPhotoModel()->insert($data);
     // set url
     $this->getPhotoModel()->updateById($data['id'], array('url' => 'DSC_' . $data['id']));
     // copy file
     $new_path = photosPhoto::getPhotoPath($data);
     $this->moveFile($row, $new_path);
     // fix width and height for old photos
     if (!$data['width'] && !$data['height'] && file_exists($new_path)) {
         $image = waImage::factory($new_path);
         $this->getPhotoModel()->updateById($data['id'], array('width' => $image->width, 'height' => $image->height));
     }
     if ($exif_data = photosExif::getInfo($new_path)) {
         $this->getExifModel()->save($data['id'], $exif_data);
     }
     // set rights
     $sql = "INSERT IGNORE INTO photos_photo_rights SET group_id = 0, photo_id = " . (int) $data['id'];
     $this->dest->exec($sql);
     if (!$album['status']) {
         $sql = "INSERT IGNORE INTO photos_photo_rights\n                    SET group_id = -" . (int) $data['contact_id'] . ", photo_id = " . (int) $data['id'];
         $this->dest->exec($sql);
     }
     // add photo to album
     $sql = "INSERT IGNORE INTO photos_album_photos\n                SET album_id = " . $album['id'] . ", photo_id = " . (int) $data['id'] . ", sort = " . (int) $row['PL_SORT'];
     $this->dest->exec($sql);
     // save old id => new id (for widgets)
     $this->old_photos[$id] = $data['id'];
     return $data['id'];
 }