示例#1
0
 /**
  * Appends data to the file specified. The file will be created if not exists.
  * @param string $filename
  * @param string $data
  * @throws \InvalidArgumentException
  * @throws \App\InvalidConfigOptionException
  * @return boolean TRUE if data is suceessfully written. FALSE otherwise.
  */
 function write($filename, $data)
 {
     $app =& \App::$instance;
     if (!is_string($filename) || strlen($filename) === 0) {
         throw new \InvalidArgumentException('The filename argument must be of type string and must not be empty');
     }
     if (!is_string($data)) {
         throw new \InvalidArgumentException('The data argument must be of type string');
     }
     if ($app->config->logsDir === null) {
         throw new \App\InvalidConfigOptionException('Config option dataDir is not set');
     }
     try {
         $microtime = microtime(true);
         $microtimeParts = explode('.', $microtime);
         $logData = date('H:i:s', $microtime) . ':' . (isset($microtimeParts[1]) ? $microtimeParts[1] : '0') . "\n" . $data . "\n\n";
         \App\Utilities\File::makeDir($app->config->logsDir . $filename);
         $fileHandler = fopen($app->config->logsDir . $filename, 'ab');
         $result = fwrite($fileHandler, $logData);
         fclose($fileHandler);
         return is_int($result);
     } catch (\Exception $e) {
         return false;
     }
 }
示例#2
0
 /**
  * Returns the local filename for a given URL path
  * @param string $path The path part of the asset url
  * @throws \InvalidArgumentException
  * @throws \App\InvalidConfigOptionException
  * @return boolean|string The localfileneme or FALSE if file does not exists
  */
 function getFilename($path)
 {
     if (!is_string($path)) {
         throw new \InvalidArgumentException('');
     }
     $app =& \App::$instance;
     if ($app->config->assetsPathPrefix === null) {
         throw new \App\InvalidConfigOptionException('Config option assetsPathPrefix is not set');
     }
     if (strpos($path, $app->config->assetsPathPrefix) !== 0) {
         return false;
     }
     $path = substr($path, strlen($app->config->assetsPathPrefix));
     $partParts = explode('/', $path, 2);
     if (sizeof($partParts) !== 2) {
         return false;
     }
     $hash = substr($partParts[0], 0, 10);
     $type = substr($partParts[0], 10, 1);
     $optionsString = (string) substr($partParts[0], 11);
     $path = $partParts[1];
     if ($type === 'a' && strlen($app->config->addonsDir) > 0) {
         $filename = $app->config->addonsDir . $path;
     } elseif ($type === 'p' && strlen($app->config->appDir) > 0) {
         $filename = $app->config->appDir . $path;
     } elseif ($type === 'd' && strlen($app->config->dataDir) > 0) {
         if (!$app->data->isPublic($path)) {
             return false;
         }
         $filename = $app->config->dataDir . 'objects/' . $path;
     } else {
         return false;
     }
     if ($hash === substr(md5(md5($filename) . md5($optionsString)), 0, 10) && is_file($filename)) {
         if ($optionsString === '') {
             return is_file($filename) ? $filename : false;
         }
         $options = explode('-', $optionsString);
         $width = null;
         $height = null;
         foreach ($options as $option) {
             if (substr($option, 0, 1) === 'w') {
                 $value = (int) substr($option, 1);
                 if ($value >= 1 && $value <= 100000) {
                     $width = $value;
                 }
             }
             if (substr($option, 0, 1) === 'h') {
                 $value = (int) substr($option, 1);
                 if ($value >= 1 && $value <= 100000) {
                     $height = $value;
                 }
             }
         }
         if ($app->config->dataDir === null) {
             throw new \App\InvalidConfigOptionException('Config option dataDir is not set');
         }
         $pathinfo = pathinfo($filename);
         if (isset($pathinfo['extension'])) {
             $tempFilename = $app->config->dataDir . 'objects/.temp/assets/' . md5(md5($filename) . md5($optionsString));
             if (!is_file($tempFilename)) {
                 \App\Utilities\File::makeDir($tempFilename);
                 if ($width !== null || $height !== null) {
                     if ($width === null) {
                         $imageSize = \App\Utilities\Graphics::getSize($filename);
                         $width = (int) floor($imageSize[0] / $imageSize[1] * $height);
                     } elseif ($height === null) {
                         $imageSize = \App\Utilities\Graphics::getSize($filename);
                         $height = (int) floor($imageSize[1] / $imageSize[0] * $width);
                     }
                     \App\Utilities\Graphics::resize($filename, $tempFilename, $width, $height, $pathinfo['extension']);
                 }
             }
             return $tempFilename;
         } else {
             return false;
         }
     }
     return false;
 }