/** * Gets the data for a field and save it in the database * @param SPEntry $entry * @param string $request * @param bool $clone * @throws SPException * @return bool */ public function saveData(&$entry, $request = 'POST', $clone = false) { if (!$this->enabled) { return false; } $del = SPRequest::bool($this->nid . '_delete', false, $request); if ($clone) { $orgSid = SPRequest::sid(); $this->loadData($orgSid); $files = $this->getExistingFiles(); $cloneFiles = array(); if (isset($files['original']) && file_exists(SOBI_ROOT . '/' . $files['original'])) { return $this->cloneFiles($entry, $request, $files, $cloneFiles); } } $fileSize = SPRequest::file($this->nid, 'size'); $cropped = null; static $store = null; $cache = false; if ($store == null) { $store = SPFactory::registry()->get('requestcache_stored'); } if (is_array($store) && isset($store[$this->nid])) { if (!strstr($store[$this->nid], 'file://') && !strstr($store[$this->nid], 'directory://')) { $data = $store[$this->nid]; $cache = true; $orgName = SPRequest::file($this->nid, 'name', $request); } else { SPRequest::set($this->nid, $store[$this->nid]); $orgName = SPRequest::file($this->nid, 'name'); $data = SPRequest::file($this->nid, 'tmp_name'); } } else { $data = SPRequest::file($this->nid, 'tmp_name'); $orgName = SPRequest::file($this->nid, 'name'); } $sPath = $this->parseName($entry, $orgName, $this->savePath); $path = SPLoader::dirPath($sPath, 'root', false); /** Wed, Oct 15, 2014 13:51:03 * Implemented a cropper with Ajax checker. * This is the actual method to get those files * Other methods left for BC * */ if (!$data) { $directory = SPRequest::string($this->nid, $store[$this->nid], false, $request); if (strlen($directory)) { list($data, $dirName, $files, $coordinates) = $this->getAjaxFiles($directory); if (count($files)) { foreach ($files as $file) { if ($file == '.') { continue; } if ($file == '..') { continue; } if (strpos($file, 'icon_') !== false) { continue; } if (strpos($file, 'resized_') !== false) { continue; } if (strpos($file, 'cropped_') !== false) { $cropped = $dirName . $file; SPFs::upload($cropped, $path . basename($cropped)); continue; } if (strpos($file, '.var') !== false) { continue; } $fileSize = filesize($dirName . $file); $orgName = $file; } } if (strlen($coordinates)) { $coordinates = json_decode(SPLang::clean($coordinates), true); /** @var SPImage $croppedImage */ $croppedImage = SPFactory::Instance('base.fs.image', $dirName . $orgName); $croppedImage->crop($coordinates['width'], $coordinates['height'], $coordinates['x'], $coordinates['y']); $cropped = 'cropped_' . $orgName; $croppedImage->saveAs($path . $cropped); } $data = strlen($cropped) ? $cropped : $dirName . $file; } } $files = array(); /* if we have an image */ if ($data && $orgName) { if ($fileSize > $this->maxSize) { throw new SPException(SPLang::e('FIELD_IMG_TOO_LARGE', $this->name, $fileSize, $this->maxSize)); } if ($cropped) { SPFs::upload($dirName . $orgName, $path . $orgName); } /** * @var SPImage $orgImage */ if ($cache) { $orgImage = SPFactory::Instance('base.fs.image', $data); $orgImage->move($path . $orgName); } else { $orgImage = SPFactory::Instance('base.fs.image'); $nameArray = explode('.', $orgName); $ext = strtolower(array_pop($nameArray)); $nameArray[] = $ext; $orgName = implode('.', $nameArray); if ($cropped) { // Fri, Jul 3, 2015 17:15:05 // it has been actually uploaded at ~425 // not sure why we are trying to upload it again if (SPFs::exists($dirName . $data)) { $orgImage->upload($dirName . $data, $path . basename($data)); } else { $orgImage->setFile($path . basename($data)); } } else { $orgImage->upload($dirName . $orgName, $path . $orgName); } } $files['data']['exif'] = $orgImage->exif(); $this->cleanExif($files['data']['exif']); if (Sobi::Cfg('image_field.fix_rotation', true)) { if ($orgImage->fixRotation()) { $orgImage->save(); } } if ($this->resize) { $image = clone $orgImage; try { $image->resample($this->resizeWidth, $this->resizeHeight, false); $files['image'] = $this->parseName($entry, $orgName, $this->imageName, true); $image->saveAs($path . $files['image']); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); $image->delete(); throw new SPException(SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage())); } } if ($this->generateThumb) { $thumb = clone $orgImage; try { $thumb->resample($this->thumbWidth, $this->thumbHeight, false); $files['thumb'] = $this->parseName($entry, $orgName, $this->thumbName, true); $thumb->saveAs($path . $files['thumb']); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); $thumb->delete(); throw new SPException(SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage())); } } $ico = clone $orgImage; try { $icoSize = explode(':', Sobi::Cfg('image.ico_size', '80:80')); $ico->resample($icoSize[0], $icoSize[1], false); $files['ico'] = $this->parseName($entry, strtolower($orgName), 'ico_{orgname}_' . $this->nid, true); $ico->saveAs($path . $files['ico']); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); $ico->delete(); throw new SPException(SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage())); } if (!$this->keepOrg) { $orgImage->delete(); } else { $files['original'] = $this->parseName($entry, $orgName, '{orgname}', true); } foreach ($files as $i => $file) { if ($i == 'data') { continue; } $files[$i] = $sPath . $file; } } elseif ($del) { $this->delImgs(); $files = array(); } else { return true; } $this->storeData($entry, $request, $files); }
/** * Get file from the request and upload to the given path * @param string $name - file name from the request * @param string $destination - destination path * @throws SPException * @return bool */ public function upload($name, $destination) { $destination = Sobi::FixPath($destination); if (SPFs::upload($name, $destination)) { $this->_filename = $destination; return $this->_filename; } else { // Sun, Jan 18, 2015 20:41:09 // stupid windows exception. I am not going to waste my time trying to find why the hell it doesn't work as it should if (SPFs::upload(Sobi::FixPath($name), $destination)) { $this->_filename = $destination; return $this->_filename; } else { throw new SPException(SPLang::e('CANNOT_UPLOAD_FILE_TO', str_replace(SOBI_ROOT, null, $destination))); } } }
public function save(&$attr) { static $lang = null; static $defLang = null; if (!$lang) { $lang = Sobi::Lang(); $defLang = Sobi::DefLang(); } $file = SPRequest::file('spfieldsopts', 'tmp_name'); if ($file) { $data = parse_ini_file($file, true); } elseif (is_string($attr['options'])) { $data = parse_ini_string($attr['options'], true); } else { $data = null; } $options = $this->parseOptsFile($data); if (!count($options) && count($attr['options'])) { $p = 0; $hold = array(); foreach ($attr['options'] as $o) { if (is_numeric($o['id'])) { $o['id'] = $this->nid . '_' . $o['id']; } if (isset($o['id'])) { $i = 0; $oid = $o['id']; while (isset($hold[$oid])) { $oid = $o['id'] . '_' . ++$i; } $options[] = array('id' => $oid, 'name' => $o['name'], 'parent' => null, 'position' => ++$p); $hold[$oid] = $oid; } } } if (count($options)) { unset($attr['options']); $optionsArr = array(); $labelsArr = array(); $optsIds = array(); $defLabelsArr = array(); $duplicates = false; foreach ($options as $i => $option) { /* check for doubles */ foreach ($options as $pos => $opt) { if ($i == $pos) { continue; } if ($option['id'] == $opt['id']) { $option['id'] = $option['id'] . '_' . substr((string) microtime(), 2, 8) . rand(1, 100); $duplicates = true; } } $optionsArr[] = array('fid' => $this->id, 'optValue' => $option['id'], 'optPos' => $option['position'], 'optParent' => $option['parent']); $defLabelsArr[] = array('sKey' => $option['id'], 'sValue' => $option['name'], 'language' => $defLang, 'oType' => 'field_option', 'fid' => $this->id); $labelsArr[] = array('sKey' => $option['id'], 'sValue' => $option['name'], 'language' => $lang, 'oType' => 'field_option', 'fid' => $this->id); $optsIds[] = $option['id']; } if ($duplicates) { SPFactory::message()->warning('FIELD_WARN_DUPLICATE_OPT_ID'); } $db = SPFactory::db(); /* try to delete the existing labels */ try { $db->delete('spdb_field_option', array('fid' => $this->id)); $db->delete('spdb_language', array('oType' => 'field_option', 'fid' => $this->id, '!sKey' => $optsIds)); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_STORE_FIELD_OPTIONS_DB_ERR', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } /* insert new values */ try { $db->insertArray('spdb_field_option', $optionsArr); $db->insertArray('spdb_language', $labelsArr, true); if ($defLang != $lang) { $db->insertArray('spdb_language', $defLabelsArr, false, true); } } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_DELETE_SELECTED_OPTIONS', $x->getMessage()), SPC::ERROR, 500, __LINE__, __FILE__); } } if (!isset($attr['params'])) { $attr['params'] = array(); } $myAttr = $this->getAttr(); $properties = array(); if (count($myAttr)) { foreach ($myAttr as $property) { $properties[$property] = isset($attr[$property]) ? $attr[$property] : null; } } $this->sets['field.options'] = SPFactory::Instance('types.array')->toINIString($data); /** handle upload of new definition file */ $XMLFile = SPRequest::file('select-list-dependency', 'tmp_name'); if ($XMLFile && file_exists($XMLFile)) { $XMLFileName = SPRequest::file('select-list-dependency', 'name'); if (SPFs::getExt($XMLFileName) == 'zip') { $arch = SPFactory::Instance('base.fs.archive'); $name = str_replace('.zip', null, $XMLFileName); $path = SPLoader::dirPath('tmp.install.' . $name, 'front', false); $c = 0; while (SPFs::exists($path)) { $path = SPLoader::dirPath('tmp.install.' . $name . '_' . ++$c, 'front', false); } $arch->upload($XMLFile, $path . '/' . $XMLFileName); $arch->extract($path); $files = scandir($path); if (count($files)) { foreach ($files as $defFile) { switch (SPFs::getExt($defFile)) { case 'xml': $properties['dependencyDefinition'] = $defFile; SPFs::move($path . '/' . $defFile, SOBI_PATH . '/etc/fields/select-list/' . $defFile); break; case 'ini': $defLang = explode('.', $defFile); $defLang = $defLang[0]; if (file_exists(SOBI_ROOT . '/language/' . $defLang)) { SPFs::move($path . '/' . $defFile, SOBI_ROOT . '/language/' . $defLang . '/' . $defFile); } break; } } } } elseif (SPFs::getExt($XMLFileName) == 'xml') { if (SPFs::upload($XMLFile, SOBI_PATH . '/etc/fields/select-list/' . $XMLFileName)) { $properties['dependencyDefinition'] = $XMLFileName; } } } /** if we use it - let's transform the XML file */ if ($properties['dependency'] && $properties['dependencyDefinition']) { $this->parseDependencyDefinition($properties['dependencyDefinition']); } $attr['params'] = $properties; }
/** * Gets the data for a field and save it in the database * @param SPEntry $entry * @param string $request * @throws SPException * @return bool */ public function saveData(&$entry, $request = 'POST') { if (!$this->enabled) { return false; } $del = SPRequest::bool($this->nid . '_delete', false, $request); $fileSize = SPRequest::file($this->nid, 'size'); $cropped = null; static $store = null; $cache = false; if ($store == null) { $store = SPFactory::registry()->get('requestcache_stored'); } if (is_array($store) && isset($store[$this->nid])) { if (!strstr($store[$this->nid], 'file://') && !strstr($store[$this->nid], 'directory://')) { $data = $store[$this->nid]; $cache = true; $orgName = SPRequest::file($this->nid, 'name', $request); } else { SPRequest::set($this->nid, $store[$this->nid]); $orgName = SPRequest::file($this->nid, 'name'); $data = SPRequest::file($this->nid, 'tmp_name'); } } else { $data = SPRequest::file($this->nid, 'tmp_name'); $orgName = SPRequest::file($this->nid, 'name'); } $sPath = $this->parseName($entry, $orgName, $this->savePath); $path = SPLoader::dirPath($sPath, 'root', false); /** Wed, Oct 15, 2014 13:51:03 * Implemented a cropper with Ajax checker. * This is the actual method to get those files * Other methods left for BC * */ if (!$data) { $directory = SPRequest::string($this->nid, $store[$this->nid], false, $request); if (strlen($directory)) { list($data, $dirName, $files, $coordinates) = $this->getAjaxFiles($directory); if (count($files)) { foreach ($files as $file) { if ($file == '.') { continue; } if ($file == '..') { continue; } if (strpos($file, 'icon_') !== false) { continue; } if (strpos($file, 'resized_') !== false) { continue; } if (strpos($file, 'cropped_') !== false) { $cropped = $dirName . $file; SPFs::upload($cropped, $path . basename($cropped)); continue; } if (strpos($file, '.var') !== false) { continue; } $fileSize = filesize($dirName . $file); $orgName = $file; } } if (strlen($coordinates)) { $coordinates = json_decode(SPLang::clean($coordinates), true); /** @var SPImage $croppedImage */ $croppedImage = SPFactory::Instance('base.fs.image', $dirName . $orgName); $croppedImage->crop($coordinates['width'], $coordinates['height'], $coordinates['x'], $coordinates['y']); $cropped = 'cropped_' . $orgName; $croppedImage->saveAs($path . $cropped); } $data = strlen($cropped) ? $cropped : $dirName . $file; } } $files = array(); /* if we have an image */ if ($data && $orgName) { if ($fileSize > $this->maxSize) { throw new SPException(SPLang::e('FIELD_IMG_TOO_LARGE', $this->name, $fileSize, $this->maxSize)); } if ($cropped) { SPFs::upload($dirName . $orgName, $path . $orgName); } /** * @var SPImage $orgImage */ if ($cache) { $orgImage = SPFactory::Instance('base.fs.image', $data); $orgImage->move($path . $orgName); } else { $orgImage = SPFactory::Instance('base.fs.image'); $nameArray = explode('.', $orgName); $ext = strtolower(array_pop($nameArray)); $nameArray[] = $ext; $orgName = implode('.', $nameArray); if ($cropped) { $orgImage->upload($dirName . $data, $path . basename($data)); } else { $orgImage->upload($dirName . $orgName, $path . $orgName); } } $files['data']['exif'] = $orgImage->exif(); $this->cleanExif($files['data']['exif']); if (Sobi::Cfg('image_field.fix_rotation', true)) { if ($orgImage->fixRotation()) { $orgImage->save(); } } if ($this->resize) { $image = clone $orgImage; try { $image->resample($this->resizeWidth, $this->resizeHeight, false); $files['image'] = $this->parseName($entry, $orgName, $this->imageName, true); $image->saveAs($path . $files['image']); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); $image->delete(); throw new SPException(SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage())); } } if ($this->generateThumb) { $thumb = clone $orgImage; try { $thumb->resample($this->thumbWidth, $this->thumbHeight, false); $files['thumb'] = $this->parseName($entry, $orgName, $this->thumbName, true); $thumb->saveAs($path . $files['thumb']); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); $thumb->delete(); throw new SPException(SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage())); } } $ico = clone $orgImage; try { $icoSize = explode(':', Sobi::Cfg('image.ico_size', '80:80')); $ico->resample($icoSize[0], $icoSize[1], false); $files['ico'] = $this->parseName($entry, strtolower($orgName), 'ico_{orgname}', true); $ico->saveAs($path . $files['ico']); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); $ico->delete(); throw new SPException(SPLang::e('FIELD_IMG_CANNOT_RESAMPLE', $x->getMessage())); } if (!$this->keepOrg) { $orgImage->delete(); } else { $files['original'] = $this->parseName($entry, $orgName, '{orgname}', true); } foreach ($files as $i => $file) { if ($i == 'data') { continue; } $files[$i] = $sPath . $file; } } elseif ($del) { $this->delImgs(); $files = array(); } else { return true; } /* @var SPdb $db */ $db =& SPFactory::db(); $this->verify($entry, $request); $time = SPRequest::now(); $IP = SPRequest::ip('REMOTE_ADDR', 0, 'SERVER'); $uid = Sobi::My('id'); /* if we are here, we can save these data */ /* collect the needed params */ $save = count($files) ? SPConfig::serialize($files) : null; $params = array(); $params['publishUp'] = $entry->get('publishUp'); $params['publishDown'] = $entry->get('publishDown'); $params['fid'] = $this->fid; $params['sid'] = $entry->get('id'); $params['section'] = Sobi::Reg('current_section'); $params['lang'] = Sobi::Lang(); $params['enabled'] = $entry->get('state'); $params['baseData'] = $db->escape($save); $params['approved'] = $entry->get('approved'); $params['confirmed'] = $entry->get('confirmed'); /* if it is the first version, it is new entry */ if ($entry->get('version') == 1) { $params['createdTime'] = $time; $params['createdBy'] = $uid; $params['createdIP'] = $IP; } $params['updatedTime'] = $time; $params['updatedBy'] = $uid; $params['updatedIP'] = $IP; $params['copy'] = !$entry->get('approved'); if (Sobi::My('id') == $entry->get('owner')) { --$this->editLimit; } $params['editLimit'] = $this->editLimit; /* save it */ try { $db->insertUpdate('spdb_field_data', $params); } catch (SPException $x) { Sobi::Error($this->name(), SPLang::e('CANNOT_SAVE_FIELDS_DATA_DB_ERR', $x->getMessage()), SPC::WARNING, 0, __LINE__, __FILE__); } }