/**
  * Adds a Flexiskin defined by data via request parameter
  * @return String encoded result JSON string
  */
 public function addFlexiskin()
 {
     $aData = FormatJson::decode($this->getMain()->getVal('data', ""));
     $oData = $aData[0];
     if (is_null($oData->template)) {
         return FormatJson::encode(array('success' => false, 'msg' => wfMessage('bs-flexiskin-error-templatenotexists')->plain()));
     }
     if (empty($oData->template)) {
         $oData->template = 'default';
     }
     $sId = str_replace(" ", "_", strtolower($oData->name));
     $sFlexiskinPath = BsFileSystemHelper::getDataDirectory('flexiskin' . DS . md5($sId));
     if (is_dir($sFlexiskinPath)) {
         return FormatJson::encode(array('success' => false, 'msg' => wfMessage('bs-flexiskin-error-skinexists')->plain()));
     }
     //PW(27.11.2013) TODO: add check template really exists before add
     if (empty($oData->name)) {
         //PW(27.11.2013) TODO: add msg
         return FormatJson::encode(array('success' => false, 'msg' => wfMessage('bs-flexiskin-error-nameempty')->plain()));
     }
     if ($oData->template != 'default') {
         $oConfig = $this->getConfigFromId($oData->template);
         if (!$oConfig->isGood()) {
             return FormatJson::encode(array('success' => false, 'msg' => wfMessage('bs-flexiskin-error-templatenotexists')->plain()));
         }
         $oConf = FormatJson::decode($oConfig->getValue());
         $oConf[0]->name = $oData->name;
         $oConf[0]->desc = $oData->desc;
         $sConfigFile = FormatJson::encode($oConf);
     } else {
         $sConfigFile = Flexiskin::generateConfigFile($oData);
     }
     $oStatus = BsFileSystemHelper::saveToDataDirectory('conf.json', $sConfigFile, "flexiskin" . DS . md5($sId));
     if (!$oStatus->isGood()) {
         return FormatJson::encode(array('success' => false, 'msg' => wfMessage('bs-flexiskin-error-fail-add-skin', $this->getErrorMessage($oStatus))->plain()));
     }
     if ($oData->template != 'default') {
         $oStatus = BsFileSystemHelper::copyFile('variables.less', "flexiskin" . DS . $oData->template, "flexiskin" . DS . md5($sId));
         $oStatus = BsFileSystemHelper::copyFolder("images", "flexiskin" . DS . $oData->template, "flexiskin" . DS . md5($sId));
     } else {
         $oStatus = BsFileSystemHelper::saveToDataDirectory('variables.less', Flexiskin::generateStyleFile($sConfigFile), "flexiskin" . DS . md5($sId));
     }
     $oStatus = BsFileSystemHelper::saveToDataDirectory('screen.less', Flexiskin::generateScreenFile(), "flexiskin" . DS . md5($sId));
     $oStatus = BsFileSystemHelper::saveToDataDirectory('screen.tmp.less', Flexiskin::generateScreenFile(true), "flexiskin" . DS . md5($sId));
     //tbd: check 1st, 2nd and 3rd status
     if (!$oStatus->isGood()) {
         return FormatJson::encode(array('success' => false, 'msg' => wfMessage('bs-flexiskin-error-fail-add-skin', $this->getErrorMessage($oStatus))->plain()));
     }
     BsFileSystemHelper::ensureDataDirectory("flexiskin" . DS . md5($sId) . DS . "images");
     return FormatJson::encode(array('success' => true));
 }
 /**
  * Generate an avatar image
  * @param User $oUser
  * @return string Relative URL to avatar image
  */
 public function generateAvatar($oUser, $aParams = array(), $bOverwrite = false)
 {
     $iAvatarDefaultSize = BsConfig::get('MW::Avatars::DefaultSize');
     $iAvatarHeight = isset($aParams['height']) ? $aParams['height'] : $iAvatarDefaultSize;
     $iAvatarWidth = isset($aParams['width']) ? $aParams['width'] : $iAvatarDefaultSize;
     $iUserId = $oUser->getId();
     $sUserName = $oUser->getName();
     $sUserRealName = $oUser->getRealName();
     $sAvatarFileName = self::$sAvatarFilePrefix . $iUserId . ".png";
     # TODO: Check if this is more expensive than a simple file_exists()
     $oFile = BsFileSystemHelper::getFileFromRepoName($sAvatarFileName, 'Avatars');
     # If avatar doesn't yet exit, create one
     if (!$oFile->exists() || $bOverwrite) {
         $sGenerator = BsConfig::get('MW::Avatars::Generator');
         switch ($sGenerator) {
             case 'Identicon':
                 require_once __DIR__ . "/includes/lib/Identicon/identicon.php";
                 $sRawPNGAvatar = generateIdenticon($iUserId, $iAvatarDefaultSize);
                 # non-random
                 break;
             case 'InstantAvatar':
                 require_once __DIR__ . "/includes/lib/InstantAvatar/instantavatar.php";
                 $iFontSize = round(18 / 40 * $iAvatarDefaultSize);
                 $oIA = new InstantAvatar(__DIR__ . '/includes/lib/InstantAvatar/Comfortaa-Regular.ttf', $iFontSize, $iAvatarDefaultSize, $iAvatarDefaultSize, 2, __DIR__ . '/includes/lib/InstantAvatar/glass.png');
                 if ($sUserRealName) {
                     preg_match_all('#(^| )(.)#u', $sUserRealName, $aMatches);
                     $sChars = implode('', $aMatches[2]);
                     if (mb_strlen($sChars) < 2) {
                         $sChars = $sUserRealName;
                     }
                 } else {
                     $sChars = $sUserName;
                 }
                 $oIA->generateRandom($sChars);
                 # random
                 $sRawPNGAvatar = $oIA->getRawPNG();
                 break;
             default:
                 throw new MWException('FATAL: Avatar generator not found!');
                 break;
         }
         $oStatus = BsFileSystemHelper::saveToDataDirectory($sAvatarFileName, $sRawPNGAvatar, $this->mInfo[EXTINFO::NAME]);
         if (!$oStatus->isGood()) {
             throw new MWException('FATAL: Avatar could not be saved!');
         }
         # found no way to regenerate thumbs. just delete thumb folder if it exists
         $oStatus = BsFileSystemHelper::deleteFolder('Avatars' . DS . 'thumb' . DS . $sAvatarFileName, true);
         if (!$oStatus->isGood()) {
             throw new MWException('FATAL: Avatar thumbs could no be deleted!');
         }
         $oFile = BsFileSystemHelper::getFileFromRepoName($sAvatarFileName, 'Avatars');
     }
     $sNewUserImageSrc = $oFile->createThumb($iAvatarWidth, $iAvatarHeight);
     return $sNewUserImageSrc;
 }