Exemple #1
0
 /**
  * Use this to pass a bunch of data at once to the object
  *
  * @param $data , can be an array or a file of the types yaml, json or ini, PHP will be executed!
  * @param bool $override , by default all existing data are replaced
  */
 public static function init($data, $override = true)
 {
     $settingsObj = self::getInstance();
     if (is_string($data)) {
         ob_start();
         include $data;
         switch (FsUtils::getFileExtension($data)) {
             case 'yaml':
             case 'yml':
                 $data = Yaml::parse(ob_get_clean());
                 break;
             case 'json':
                 $data = json_decode(ob_get_clean(), 1);
                 break;
             case 'ini':
                 $data = parse_ini_string(ob_get_clean());
                 break;
         }
     }
     if ($override) {
         $settingsObj->data = $data;
     } else {
         $settingsObj->data = ArrayUtils::arrayMergeRecursiveDistinct($settingsObj->data, $data);
     }
 }
 /**
  * Use native format if possible, generic json otherwise
  * 
  * @param string $cacheFilePath
  * @return string class name
  */
 public static function getClass($cacheFilePath)
 {
     $extension = strtolower(FsUtils::getFileExtension($cacheFilePath));
     if ($extension === 'htm') {
         $extension = 'html';
     } else {
         if ($extension === 'yml') {
             $extension = 'yaml';
         }
     }
     $nativeFormatClassName = __NAMESPACE__ . '\\FileFormats\\' . ucfirst($extension) . 'Cache';
     return class_exists($nativeFormatClassName) ? $nativeFormatClassName : __NAMESPACE__ . '\\FileFormats\\GenericCache';
 }
 /**
  * @return array
  */
 public function convertToBase64()
 {
     $base64 = '';
     if (isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > min(Utils::iniGetBytes('post_max_size'), 1000000)) {
         return array('error' => 'File too large - max size is 1 MB');
     } else {
         $base64 .= 'data:' . FsUtils::getMimeType($this->resource['tmp_name']) . ';' . 'base64,' . base64_encode(file_get_contents($this->resource['tmp_name']));
         $oldFileSize = filesize($this->resource['tmp_name']);
         $newFileSize = strlen($base64);
         $sizeDiff = $newFileSize * 100 / $oldFileSize - 100;
         $diffPrefix = $sizeDiff < 0 ? '-' : '+';
         $sizeDiff = $diffPrefix . (string) round($sizeDiff, 1) . '%';
         $msg = basename($this->resource['name']) . ', ' . 'was ' . FsUtils::formatFileSize($oldFileSize) . ', ' . 'now ' . FsUtils::formatFileSize($newFileSize) . ' ' . '(' . $diffPrefix . (string) round($sizeDiff, 1) . '%, ' . 'about +30% is normal)';
         return array('success' => $msg, 'base64' => $base64);
     }
 }
Exemple #4
0
 /**
  * Some of the functions need a file as a resource for various reasons (e.g. to check the mime type).
  * If the resource is some type of variable instead this function will create a temporary file.   * 
  * 
  * @param string $fileName
  * @param string $data
  */
 protected static function tempStore($fileName, $data)
 {
     $filePath = rtrim(sys_get_temp_dir(), '/') . '/PHP_Jig/Converter/' . session_id();
     FsUtils::mkDir($filePath);
     file_put_contents($filePath . '/' . $fileName, $data);
     return $filePath . '/' . $fileName;
 }
 /**
  * Verify paths and encode the file
  * 
  * @param string $basedir
  * @param string $source
  * @throws \tao_models_classes_FileNotFoundException
  * @throws \common_exception_Error
  * @return string
  */
 protected static function secureEncode($basedir, $source)
 {
     $components = parse_url($source);
     if (!isset($components['scheme'])) {
         // relative path
         if (\tao_helpers_File::securityCheck($source, false)) {
             if (file_exists($basedir . $source)) {
                 return 'data:' . FsUtils::getMimeType($basedir . $source) . ';' . 'base64,' . base64_encode(file_get_contents($basedir . $source));
             } else {
                 throw new \tao_models_classes_FileNotFoundException($source);
             }
         } else {
             throw new \common_exception_Error('Invalid source path "' . $source . '"');
         }
     } else {
         // url, just return it as is
         return $source;
     }
 }
 /**
  * Short description of method getTranslationsInFile
  *
  * @access private
  * @author firstname and lastname of author, <*****@*****.**>
  * @param  string $filePath
  */
 private function getTranslationsInFile($filePath)
 {
     // File extension ?
     $extOk = in_array(\Jig\Utils\FsUtils::getFileExtension($filePath), $this->getFileTypes());
     if ($extOk) {
         foreach ($this->getBannedFileType() as $bannedExt) {
             $extOk &= substr_compare($filePath, $bannedExt, strlen($filePath) - strlen($bannedExt), strlen($bannedExt)) !== 0;
         }
     }
     if ($extOk) {
         // We read the file.
         $lines = file($filePath);
         foreach ($lines as $line) {
             $strings = $this->getTranslationPhrases($line);
             //preg_match_all("/__(\(| )+['\"](.*?)['\"](\))?/u", $line, $string);
             //lookup for __('to translate') or __ 'to translate'
             //    preg_match_all("/__(\(| )+(('(.*?)')|(\"(.*?)\"))(\))?/u", $line, $string);
             foreach ($strings as $s) {
                 $tu = new tao_helpers_translation_TranslationUnit();
                 $tu->setSource(tao_helpers_translation_POUtils::sanitize($s));
                 $tus = $this->getTranslationUnits();
                 $found = false;
                 // We must add the string as a new TranslationUnit only
                 // if a similiar source does not exist.
                 foreach ($tus as $t) {
                     if ($tu->getSource() == $t->getSource()) {
                         $found = true;
                         break;
                     }
                 }
                 if (!$found) {
                     $tus[] = $tu;
                     $this->setTranslationUnits($tus);
                 }
             }
         }
     }
 }
Exemple #7
0
 /**
  * Get the resolved absolute URL for a stylesheet
  * 
  * @param string $path
  * @return string
  */
 private function resolveStylesheetUrl($path)
 {
     $websource = WebsourceManager::singleton()->getWebsource($this->get(ThemeRegistry::WEBSOURCE));
     if (strpos($path, ThemeRegistry::WEBSOURCE) === 0) {
         return $websource->getAccessUrl(substr($path, strlen(ThemeRegistry::WEBSOURCE)));
     } else {
         // normalizing makes sure that whatever\\comes/in gets/out/properly
         return ROOT_URL . FsUtils::normalizePath($path);
     }
 }
 public function getBase64()
 {
     //TODO Resize the image
     if ($this->hasRequestParameter('upload')) {
         $filename = $this->getRequestParameter('upload');
         $return['base64'] = 'data:' . FsUtils::getMimeType($filename) . ';base64,' . base64_encode(file_get_contents($filename));
     } else {
         $return['base64'] = Template::img('tao-logo.png', 'tao');
     }
     $this->returnJson($return);
 }