예제 #1
0
 public function __set($name, $value)
 {
     switch ($name) {
         case 'maintemplate':
         case 'viewtemplate':
             if (!BrightUtils::endsWith($value, '.tpl')) {
                 $value .= '.tpl';
             }
             break;
     }
     $this->_data[$name] = $value;
 }
예제 #2
0
파일: Files.php 프로젝트: rsids/bright_api
 /**
  * Downloads a file from the given url and stores it on the server
  * @param string $url The url of the file
  * @param string $filename The filename on the local server
  * @param string $parent The parent folder
  * @return object
  * @throws \Exception
  */
 public function uploadFromUrl($url, $filename, $parent)
 {
     if (!$this->IS_AUTH) {
         throw $this->throwException(AuthenticationException::NO_USER_AUTH);
     }
     if ($filename === '') {
         $ua = explode('/', $url);
         $filename = array_pop($ua);
     }
     $url = str_replace(' ', '%20', $url);
     $url = filter_var($url, FILTER_VALIDATE_URL);
     $filename = filter_var($filename, FILTER_SANITIZE_STRING);
     $filename = preg_replace('/[\\s&\\|\\+\\!\\(\\)]/', '-', $filename);
     $filename = str_replace('%20', '-', $filename);
     while (strpos($filename, '--')) {
         $filename = str_replace('--', '-', $filename);
     }
     $parent = filter_var($parent, FILTER_SANITIZE_STRING);
     if ($url === false || $filename === false || $parent === false) {
         throw $this->throwException(ParameterException::STRING_EXCEPTION, __LINE__);
     }
     if (strpos($filename, '/') !== false || strpos($filename, '\\') !== false) {
         throw $this->throwException(ParameterException::STRING_EXCEPTION, __LINE__);
     }
     if (strpos($parent, '.') !== false) {
         throw $this->throwException(ParameterException::STRING_EXCEPTION, __LINE__);
     }
     if (!is_dir(BASEPATH . UPLOADFOLDER . $parent)) {
         throw $this->throwException(FilesException::FOLDER_NOT_FOUND);
     }
     $local = BASEPATH . UPLOADFOLDER . $parent . '/' . $filename;
     $i = 1;
     $fileParts = explode('.', $filename);
     $ext = array_pop($fileParts);
     if (count($fileParts) > 0) {
         $file = implode('.', $fileParts);
     } else {
         $file = $ext;
     }
     if (BrightUtils::endsWith($file, '-')) {
         $file = substr($file, 0, -1);
     }
     while (file_exists($local)) {
         $local = BASEPATH . UPLOADFOLDER . "{$parent}/{$file}-{$i}.{$ext}";
         $i++;
     }
     try {
         $ext = @file_get_contents($url);
     } catch (\Exception $e) {
         throw $this->throwException(FilesException::UPLOAD_FAILED);
     }
     if ($ext === false) {
         throw $this->throwException(FilesException::UPLOAD_FAILED);
     }
     $res = file_put_contents($local, $ext);
     if ($res === false) {
         throw $this->throwException(FilesException::UPLOAD_FAILED);
     }
     if (strpos($filename, '.') === false) {
         // No extension supplied, add it.
         $imgType = exif_imagetype($local);
         if ($imgType !== false) {
             $add = '';
             switch ($imgType) {
                 case IMAGETYPE_GIF:
                     $add = '.gif';
                     break;
                 case IMAGETYPE_JPEG:
                     $add = '.jpg';
                     break;
                 case IMAGETYPE_PNG:
                     $add = '.png';
                     break;
                 case IMAGETYPE_SWF:
                     $add = '.swf';
                     break;
                 case IMAGETYPE_PSD:
                     $add = '.psd';
                     break;
                 case IMAGETYPE_BMP:
                     $add = '.bmp';
                     break;
                 case IMAGETYPE_TIFF_II:
                     $add = '.tiff';
                     break;
                 case IMAGETYPE_TIFF_MM:
                     $add = '.tiff';
                     break;
             }
             if ($add != '') {
                 // Found correct extension
                 rename($local, $local . $add);
                 $filename .= $add;
             }
         }
     }
     return (object) array('files' => $this->getFiles($parent), 'file' => $filename);
 }