/** * Set the field values * * @param array $values * @param array $settings * @return Media */ public function setFieldValues(array $values = null, array $settings = []) { parent::setFieldValues($values); if ($_POST && $_FILES && count($settings) == 4) { $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if (!$upload->test($_FILES['file'])) { $this->getElement('file')->addValidator(new Validator\NotEqual($this->file, $upload->getErrorMessage())); } } return $this; }
/** * Set the field values * * @param array $values * @param array $settings * @return Media */ public function setFieldValues(array $values = null, array $settings = []) { parent::setFieldValues($values); if ($_POST && $_FILES && count($settings) == 4) { $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); foreach ($_FILES as $file) { if (!empty($file['name'])) { if (!$upload->test($file)) { $this->getElement('error')->addValidator(new Validator\NotEqual('1', $upload->getErrorMessage() . ' (' . $file['name'] . ')')); } } } } return $this; }
/** * Delete dynamic field files * * @param int $fieldId * @param int $modelId * @param string $model * @param boolean $encrypt * @param Application $app * @param string $uploadFolder * @param string $mediaLibrary * @return void */ protected static function saveFiles($fieldId, $modelId, $model, $encrypt, $app, $uploadFolder, $mediaLibrary = null) { $field = T\Fields::findById($fieldId); if (isset($field->id)) { $time = time(); $newValues = []; $oldValues = new Record(); $oldValues->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $oldValues->findRecordsBy(['model_id' => $modelId, 'model' => $model], ['order' => 'id ASC']); $old = $oldValues->rows(false); foreach ($_FILES as $key => $file) { $id = substr_count($key, '_') == 2 ? substr($key, strrpos($key, '_') + 1) : 0; if (!empty($_FILES[$key]['tmp_name']) && !empty($_FILES[$key]['name'])) { if (null !== $mediaLibrary) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES[$key])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES[$key], ['library_id' => $library->id]); $value = $media->file; if ($encrypt) { $value = (new Mcrypt())->create($value); } if (isset($old[$id])) { $replaceValue = new Record(); $replaceValue->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $replaceValue->findRecordById($old[$id]['id']); if (isset($replaceValue->id)) { $replaceValue->value = $value; $replaceValue->save(); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value'])) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value']); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $old[$id]['value'])) { $media = new \Phire\Media\Model\Media(); $media->getByFile($old[$id]['value']); if (isset($media->id)) { $media->remove(['rm_media' => [$media->id]]); } } } } else { $newValues[] = $value; } copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $app->module('phire-fields')->config()['max_size'], $app->module('phire-fields')->config()['disallowed_types'], $app->module('phire-fields')->config()['allowed_types']); $value = $upload->upload($_FILES[$key]); if ($encrypt) { $value = (new Mcrypt())->create($value); } if (isset($old[$id])) { $replaceValue = new Record(); $replaceValue->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $replaceValue->findRecordById($old[$id]['id']); if (isset($replaceValue->id)) { $replaceValue->value = $value; $replaceValue->save(); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value'])) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $old[$id]['value']); } } } else { $newValues[] = $value; } } } } foreach ($newValues as $v) { if (!empty($v)) { $fv = new Record(['model_id' => $modelId, 'model' => $model, 'timestamp' => $time, 'revision' => 0, 'value' => $v]); $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $fv->save(); $fvs = new Record(); $fvs->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $field->name); $sql = $fvs->getSql(); $sql->update(['timestamp' => ':timestamp'])->where('model_id = :model_id')->where('model = :model'); $fvs->execute($sql, ['timestamp' => $time, 'model_id' => $modelId, 'model' => $model]); } } } }
/** * Browser action method * * @param int $lid * @return void */ public function browser($lid = null) { if (null !== $this->request->getQuery('editor') && null !== $this->request->getQuery('type')) { $this->prepareView('media/browser.phtml'); $this->view->title = 'Media Browser'; if ($this->request->isPost()) { $library = new Model\MediaLibrary(); $library->getById($lid); $settings = $library->getSettings(); if (count($settings) == 4) { $upload = new \Pop\File\Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($upload->test($_FILES['file'])) { $media = new Model\Media(); $media->save($_FILES['file'], $this->request->getPost()); $this->sess->setRequestValue('saved', true); $this->redirect(str_replace('&error=1', '', $_SERVER['REQUEST_URI'])); } else { $this->redirect(str_replace('&error=1', '', $_SERVER['REQUEST_URI']) . '&error=1'); } } } if (null !== $lid && null !== $this->request->getQuery('asset') && null !== $this->request->getQuery('asset_type')) { $assets = []; $limit = $this->config->pagination; $page = $this->request->getQuery('page'); $pages = null; $library = new Model\MediaLibrary(); if ($this->request->getQuery('asset_type') == 'content' && $this->application->isRegistered('phire-content')) { $type = \Phire\Content\Table\ContentTypes::findById($lid); $content = \Phire\Content\Table\Content::findBy(['type_id' => $lid], ['order' => 'order, id ASC']); foreach ($content->rows() as $c) { $assets[] = ['id' => $c->id, 'title' => $c->title, 'uri' => BASE_PATH . $c->uri]; } if (isset($type->id)) { $this->view->assetType = $type->name; } } else { if ($this->request->getQuery('asset_type') == 'media') { $library->getById($lid); $media = new Model\Media(['lid' => $lid]); if ($this->request->getQuery('asset') == 'file') { $assets = $media->getAll(); } else { if ($this->request->getQuery('asset') == 'image') { $assets = $media->getAllImages(); } } $this->view->assetType = $library->name; } } if (count($assets) > $limit) { $pages = new Paginator(count($assets), $limit); $pages->useInput(true); $offset = null !== $page && (int) $page > 1 ? $page * $limit - $limit : 0; $assets = array_slice($assets, $offset, $limit, true); } $this->view->title = 'Media' . (null !== $this->view->assetType ? ' : ' . $this->view->assetType : null); $this->view->lid = $lid; $this->view->folder = $library->folder; $this->view->sizes = null !== $library->actions ? array_keys($library->actions) : []; $this->view->pages = $pages; $this->view->browserAssets = $assets; } else { $libraries = []; $limit = null; $pages = null; if ($this->request->getQuery('type') == 'file' && $this->application->isRegistered('phire-content')) { $types = \Phire\Content\Table\ContentTypes::findAll(['order' => 'order ASC']); if ($types->hasRows()) { $libraries['Content'] = []; foreach ($types->rows() as $type) { $libraries['Content'][$type->id] = $type->name; } } } $libraries['Media'] = []; $library = new Model\MediaLibrary(); $libs = $library->getAll(); foreach ($libs as $lib) { $libraries['Media'][$lib->id] = $lib->name; } $this->view->title = 'Media'; $this->view->pages = $pages; $this->view->lid = $lid; $this->view->libraries = $libraries; } $this->send(); } else { $this->redirect(BASE_PATH . APP_URI . '/media'); } }
/** * Ajax action method * * @param int $lid * @return void */ public function ajax($lid) { $library = new Model\MediaLibrary(); $library->getById($lid); $json = []; $categories = []; if (class_exists('Phire\\Categories\\Model\\Category') && isset($_POST['categories'])) { $categories = explode(',', $_POST['categories']); } if (!isset($library->id)) { $json['error'] = 'That library was not found.'; } else { $settings = $library->getSettings(); $folder = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $library->folder; if (!file_exists($folder)) { $library->createFolder($library->folder); } $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); foreach ($_FILES as $key => $file) { if (!empty($file['name'])) { $json['id'] = substr($key, strrpos($key, '_') + 1); if (!$upload->test($file)) { $json['error'] = $upload->getErrorMessage(); } else { $media = new Model\Media(); $media->save($file, ['library_id' => $lid]); if (count($categories) > 0) { foreach ($categories as $category) { $catItem = new \Phire\Categories\Table\CategoryItems(['category_id' => (int) $category, 'content_id' => null, 'media_id' => $media->id, 'order' => 0]); $catItem->save(); } } } } } } $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT)); $this->send(200, ['Content-Type' => 'application/json']); }
/** * Save dynamic field values to the EAV table * * @param Application $application * @param Table\Fields $field * @param mixed $value * @param string $model * @param int $modelId * @param string $uploadFolder * @param string $mediaLibrary * @return void */ public static function save(Application $application, $field, $value, $model, $modelId, $uploadFolder = null, $mediaLibrary = null) { $dynamicFieldIds = []; $fieldId = $field->id; $key = 'field_' . $fieldId; if ($field->dynamic) { $dynamicFieldIds[] = $field->id; } $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); if ($field->type == 'file' && isset($_FILES[$key]) && !empty($_FILES[$key]['tmp_name']) && !empty($_FILES[$key]['name'])) { if (isset($fv->field_id)) { $oldFile = json_decode($fv->value); if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $oldFile)) { unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $oldFile); } } if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES[$key])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES[$key], ['library_id' => $library->id]); $value = $media->file; copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $application->module('phire-fields')->config()['max_size'], $application->module('phire-fields')->config()['disallowed_types'], $application->module('phire-fields')->config()['allowed_types']); $value = $upload->upload($_FILES[$key]); } } if (!empty($value) && $value != ' ') { if ($field->encrypt && !is_array($value)) { $value = (new Mcrypt())->create($value); } } if (isset($fv->field_id)) { $oldValue = json_decode($fv->value, true); if (!empty($value) && $value != ' ') { if (strpos($field->type, '-history') !== false) { if ($value != $oldValue) { $ts = null !== $fv->timestamp ? $fv->timestamp : time() - 180; if (null !== $fv->history) { $history = json_decode($fv->history, true); $history[$ts] = $oldValue; if (count($history) > $application->module('phire-fields')->config()['history']) { $history = array_slice($history, 1, $application->module('phire-fields')->config()['history'], true); } $fv->history = json_encode($history); } else { $fv->history = json_encode([$ts => $oldValue]); } } } if ($field->dynamic && is_array($oldValue) && isset($oldValue[0])) { $oldValue[0] = $value; $newValue = json_encode($oldValue); } else { $newValue = json_encode($value); } $fv->value = $newValue; $fv->timestamp = time(); $fv->save(); } else { if (!$field->dynamic && $field->type != 'file') { $fv->delete(); } else { if ($field->dynamic && $field->type != 'file' && is_array($oldValue) && isset($oldValue[0])) { $oldValue[0] = ''; $newValue = json_encode($oldValue); $fv->value = $newValue; $fv->timestamp = time(); $fv->save(); } } } } else { if (!empty($value) && $value != ' ') { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => $field->dynamic ? json_encode([$value]) : json_encode($value), 'timestamp' => time()]); $fv->save(); } } foreach ($dynamicFieldIds as $fieldId) { $i = 1; $offset = 0; $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); $checkValue = json_decode($fv->value, true); if (is_array($checkValue) && isset($checkValue[0]) && is_array($checkValue[0])) { foreach ($checkValue as $k => $v) { $fieldToCheck = $k > 0 ? 'field_' . $fieldId . '_' . $k : 'field_' . $fieldId; if (!isset($_POST[$fieldToCheck])) { unset($checkValue[$k]); } } $checkValue = array_values($checkValue); $fv->value = json_encode($checkValue); $fv->timestamp = time(); $fv->save(); } while (isset($_POST['field_' . $fieldId . '_' . $i])) { if (!empty($_POST['field_' . $fieldId . '_' . $i]) && $_POST['field_' . $fieldId . '_' . $i] != ' ') { $postValue = $_POST['field_' . $fieldId . '_' . $i]; if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i - $offset])) { $value[$i - $offset] = $postValue; } else { $value[] = $postValue; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } else { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => json_encode([$postValue]), 'timestamp' => time()]); $fv->save(); } } else { if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i])) { unset($value[$i]); $value = array_values($value); $offset++; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } } $i++; } } foreach ($dynamicFieldIds as $fieldId) { $i = 1; $offset = 0; $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); while (isset($_FILES['field_' . $fieldId . '_' . $i])) { if (!empty($_FILES['field_' . $fieldId . '_' . $i]['tmp_name'])) { if (null !== $mediaLibrary && $application->isRegistered('phire-media')) { $library = new \Phire\Media\Model\MediaLibrary(); $library->getByFolder($mediaLibrary); if (isset($library->id)) { $settings = $library->getSettings(); $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); if ($mediaUpload->test($_FILES['field_' . $fieldId . '_' . $i])) { $media = new \Phire\Media\Model\Media(); $media->save($_FILES['field_' . $fieldId . '_' . $i], ['library_id' => $library->id]); $postValue = $media->file; copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file); } } } else { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $application->module('phire-fields')->config()['max_size'], $application->module('phire-fields')->config()['allowed_types']); $postValue = $upload->upload($_FILES['field_' . $fieldId . '_' . $i]); } if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (isset($value[$i - $offset])) { $value[$i - $offset] = $postValue; } else { $value[] = $postValue; } $fv->value = json_encode($value); $fv->timestamp = time(); $fv->save(); } else { $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => json_encode([$postValue]), 'timestamp' => time()]); $fv->save(); } } $i++; } } foreach ($dynamicFieldIds as $fieldId) { $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]); if (isset($fv->field_id)) { $value = json_decode($fv->value, true); if (is_array($value) && isset($value[0]) && is_array($value[0])) { foreach ($value as $key => $val) { if (is_array($val) && isset($val[0]) && (empty($val[0]) || $val[0] == ' ')) { unset($val[0]); $value[$key] = array_values($val); if (count($value[$key]) == 0) { unset($value[$key]); } } } $value = array_values($value); } else { if (is_array($value) && isset($value[0]) && (empty($value[0]) || $value[0] == ' ')) { unset($value[0]); $value = array_values($value); } } if (count($value) == 0) { $fv->delete(); } else { $fv->value = json_encode($value); $fv->save(); } } } }
/** * Process batch archive file * * @param string $file * @param array $fields * @return void */ public function processBatch($file, array $fields) { $tmp = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp'; mkdir($tmp); chmod($tmp, 0777); $batchFileName = (new Upload($tmp))->upload($file); $archive = new Archive($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName); $archive->extract($tmp); if (stripos($archive->getFilename(), '.tar') !== false && $archive->getFilename() != $batchFileName && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $archive->getFilename())) { unlink($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $archive->getFilename()); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName)) { unlink($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName); } $library = new MediaLibrary(); $library->getById($fields['library_id']); $settings = $library->getSettings(); $dir = new Dir($tmp, ['absolute' => true, 'recursive' => true, 'filesOnly' => true]); $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); foreach ($dir->getFiles() as $file) { $basename = basename($file); $testFile = ['name' => $basename, 'size' => filesize($file), 'error' => 0]; if ($upload->test($testFile)) { $fileName = $upload->checkFilename($basename); copy($file, $settings['folder'] . '/' . $fileName); $title = ucwords(str_replace(['_', '-'], [' ', ' '], substr($fileName, 0, strrpos($fileName, '.')))); if (null !== $library->adapter) { $class = 'Pop\\Image\\' . $library->adapter; $formats = array_keys($class::getFormats()); $fileParts = pathinfo($fileName); if (!empty($fileParts['extension']) && in_array(strtolower($fileParts['extension']), $formats)) { $this->processImage($fileName, $library); } } $media = new Table\Media(['library_id' => $fields['library_id'], 'title' => $title, 'file' => $fileName, 'size' => filesize($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $library->folder . DIRECTORY_SEPARATOR . $fileName), 'uploaded' => date('Y-m-d H:i:s'), 'order' => 0]); $media->save(); $this->data['ids'][] = $media->id; } } $dir->emptyDir(true); }