/** * Saves options to the database, inserting if none exists or updating on duplicate key * * @param array $options An array of options * @param string $group A unique string representing the options group * @return boolean */ public function save($group, $folder = null) { //Save the user config $configini = \Library\Config::$ini; $fileHandler = \Library\Folder\Files::getInstance(); $userfolders = $this->config->getParam('site-users-folder', '/users'); $prefdir = FSPATH . $userfolders . DS . $this->user->get("user_name_id") . DS . 'preferences' . DS; if (!$fileHandler->is($prefdir, true)) { //if its not a folder $folderHandler = \Library\Folder::getInstance(); if (!$folderHandler->create($prefdir)) { $this->setError(_("Could not create the target uploads folder. Please check that you have write permissions")); throw new \Platform\Exception($this->getError()); } } $paramsfolder = str_replace(array('/', '\\'), DS, $prefdir); $paramsconf = array($group); $filename = $group . ".ini"; if ($configini::saveParams($filename, $paramsconf, $paramsfolder) == FALSE) { $this->setError($this->config->getError()); return false; } return true; }
/** * Saves options to the database, inserting if none exists or updating on duplicate key * * @param array $options An array of options * @param string $group A unique string representing the options group * @return boolean true. Will throw an exception upon any failure. */ public function store($uri = null) { $fileHandler = $this->container->file; $uploadsFolder = $this->config->getParam('site-users-folder', '/users'); $allowedTypes = $this->allowed; if (empty($allowedTypes)) { $attachmentTypes = $this->config->getParamSection("attachments"); foreach ($attachmentTypes as $group => $types) { $allowedTypes = array_merge($allowedTypes, $types); } } //Check User Upload Limit; //Check File upload limit; //Validate the file $fileName = preg_replace('/[^' . $this->_validChars . ']|\\.+$/i', "", basename($file['name'])); if (strlen($fileName) == 0 || strlen($fileName) > $this->_maxNameLength) { $this->setError(_("Invalid file name")); throw new \Platform\Exception($this->getError()); } //Check that the file has a valid extension $fileExtension = $fileHandler->getExtension($fileName); if (!array_key_exists(strtolower($fileExtension), $allowedTypes)) { $this->setError(_("Attempting to upload an invalid file type")); throw new \Platform\Exception($this->getError()); } //The target folder //Check that folder exists, otherwise create it and set the appropriate permission; $uploadsFolder = FSPATH . $uploadsFolder; if (isset($this->_owner)) { $uploadsFolder .= DS . $this->_owner; } $uploadsFolder .= DS . "attachments"; //All uploads are saved in the attachments folder $uploadsFolder = str_replace(array('/', '\\'), DS, $uploadsFolder); if (!$fileHandler->is($uploadsFolder, true)) { //if its not a folder $folderHandler = \Library\Folder::getInstance(); if (!$folderHandler->create($uploadsFolder)) { $this->setError(_("Could not create the target uploads folder. Please check that you have write permissions")); throw new \Platform\Exception($this->getError()); } } $_uploadFileName = str_replace(array(" "), "_", $fileName); $uploadFileName = $uploadsFolder . DS . time() . $_uploadFileName; //adding a timestamp to avoid name collisions if (!move_uploaded_file($file['tmp_name'], $uploadFileName)) { $this->setError(_("Could not move the uploaded folder to the target directory")); throw new \Platform\Exception($this->getError()); } //Get the uploaded file extension type. $this->_fileType = $fileHandler::getMimeType($uploadFileName); //Validate the file MimeType against the allowed extenion type, if fails, //delete the file and throw an error. foreach (array("media_title" => basename($file['name']), "media_actor" => $this->user->get("user_id"), "attachment_name" => $fileName, "attachment_title" => basename($file['name']), "attachment_size" => $file['size'], "attachment_src" => str_replace(FSPATH, '', $uploadFileName), "attachment_ext" => $fileExtension, "attachment_owner" => $this->user->get("user_name_id"), "attachment_type" => $this->_fileType) as $property => $value) { $this->setPropertyValue($property, $value); } if (!$this->saveObject(NULL, "attachment")) { //Null because the system can autogenerate an ID for this attachment $fileHandler->delete($uploadFileName); $this->setError(_("Could not store the attachment properties to the database")); throw new \Platform\Exception($this->getError()); } return true; }