public function testEnableWithCustomClasses()
 {
     FulltextSearchable::enable(array('File'));
     $this->assertTrue(File::has_extension('FulltextSearchable'));
     File::remove_extension('FulltextSearchable');
     $this->assertFalse(File::has_extension('FulltextSearchable'));
 }
 public function __construct(ImportTask $task, $parameters = array())
 {
     parent::__construct($task, $parameters);
     if (!File::has_extension('LegacyDataObject')) {
         throw new Exception("File does not have the LegacyDataObject extension");
     }
     if (!empty($parameters['where'])) {
         $this->targetWhere = $parameters['where'];
     }
 }
 /**
  * Handles media objects from kapost
  * @param {mixed} $blog_id Site Config related to this content object
  * @param {array} $content Content object to be handled
  * @return {xmlrpcresp} XML-RPC Response object
  */
 protected function newMediaObject($blog_id, $content)
 {
     $fileName = $content['name'];
     $validator = new Upload_Validator(array('name' => $fileName));
     $validator->setAllowedExtensions(File::config()->allowed_extensions);
     //Verify we have a valid extension
     if ($validator->isValidExtension() == false) {
         return $this->httpError(403, _t('KapostService.FILE_NOT_ALLOWED', '_File extension is not allowed'));
     }
     //Generate default filename
     $nameFilter = FileNameFilter::create();
     $file = $nameFilter->filter($fileName);
     while ($file[0] == '_' || $file[0] == '.') {
         $file = substr($file, 1);
     }
     $doubleBarrelledExts = array('.gz', '.bz', '.bz2');
     $ext = "";
     if (preg_match('/^(.*)(\\.[^.]+)$/', $file, $matches)) {
         $file = $matches[1];
         $ext = $matches[2];
         // Special case for double-barrelled
         if (in_array($ext, $doubleBarrelledExts) && preg_match('/^(.*)(\\.[^.]+)$/', $file, $matches)) {
             $file = $matches[1];
             $ext = $matches[2] . $ext;
         }
     }
     $origFile = $file;
     //Find the kapost media folder
     $kapostMediaFolder = Folder::find_or_make($this->config()->kapost_media_folder);
     if (file_exists($kapostMediaFolder->getFullPath() . '/' . $file . $ext)) {
         if (self::config()->duplicate_assets == 'overwrite') {
             $obj = File::get()->filter('Filename', Convert::raw2sql($kapostMediaFolder->Filename . $file . $ext))->first();
             if (!empty($obj) && $obj !== false && $obj->ID > 0) {
                 //Update the Title for the image
                 $obj->Title = !empty($content['alt']) ? $content['alt'] : str_replace(array('-', '_'), ' ', preg_replace('/\\.[^.]+$/', '', $obj->Name));
                 $obj->write();
                 //Write the file to the file system
                 $f = fopen($kapostMediaFolder->getFullPath() . '/' . $file . $ext, 'w');
                 fwrite($f, $content['bits']);
                 fclose($f);
                 return array('id' => $obj->ID, 'url' => $obj->getAbsoluteURL());
             }
             return $this->httpError(404, _t('KapostService.FILE_NOT_FOUND', '_File not found'));
         } else {
             if (self::config()->duplicate_assets == 'ignore') {
                 return $this->httpError(409, _t('KapostService.DUPLICATE_FILE', '_Duplicate file detected, please rename the file and try again'));
             } else {
                 if (self::config()->duplicate_assets == 'smart_rename' && file_exists($kapostMediaFolder->getFullPath() . '/' . $file . $ext)) {
                     $obj = File::get()->filter('Filename', Convert::raw2sql($kapostMediaFolder->Filename . $file . $ext))->first();
                     if (!empty($obj) && $obj !== false && $obj->ID > 0) {
                         $fileHash = sha1_file($kapostMediaFolder->getFullPath() . '/' . $file . $ext);
                         if ($fileHash == sha1($content['bits'])) {
                             return array('id' => $obj->ID, 'url' => $obj->getAbsoluteURL());
                         }
                     }
                 }
                 $i = 1;
                 while (file_exists($kapostMediaFolder->getFullPath() . '/' . $file . $ext)) {
                     $i++;
                     $oldFile = $file;
                     if (strpos($file, '.') !== false) {
                         $file = preg_replace('/[0-9]*(\\.[^.]+$)/', $i . '\\1', $file);
                     } else {
                         if (strpos($file, '_') !== false) {
                             $file = preg_replace('/_([^_]+$)/', '_' . $i, $file);
                         } else {
                             $file .= '_' . $i;
                         }
                     }
                     if ($oldFile == $file && $i > 2) {
                         return $this->httpError(500, _t('KapostService.FILE_RENAME_FAIL', '_Could not fix {filename} with {attempts} attempts', array('filename' => $file . $ext, 'attempts' => $i)));
                     }
                 }
                 //Write the file to the file system
                 $f = fopen($kapostMediaFolder->getFullPath() . '/' . $file . $ext, 'w');
                 fwrite($f, $content['bits']);
                 fclose($f);
                 //Write the file to the database
                 $className = File::get_class_for_file_extension(substr($ext, 1));
                 $obj = new $className();
                 $obj->Name = $file . $ext;
                 $obj->Title = !empty($content['alt']) ? $content['alt'] : str_replace(array('-', '_'), ' ', preg_replace('/\\.[^.]+$/', '', $obj->Name));
                 $obj->FileName = $kapostMediaFolder->getRelativePath() . '/' . $file . $ext;
                 $obj->ParentID = $kapostMediaFolder->ID;
                 //If subsites is enabled add it to the correct subsite
                 if (File::has_extension('FileSubsites')) {
                     $obj->SubsiteID = $blog_id;
                 }
                 $obj->write();
                 $this->extend('updateNewMediaAsset', $blog_id, $content, $obj);
                 return array('id' => $obj->ID, 'url' => $obj->getAbsoluteURL());
             }
         }
     } else {
         //Write the file to the file system
         $f = fopen($kapostMediaFolder->getFullPath() . '/' . $file . $ext, 'w');
         fwrite($f, $content['bits']);
         fclose($f);
         //Write the file to the database
         $className = File::get_class_for_file_extension(substr($ext, 1));
         $obj = new $className();
         $obj->Name = $file . $ext;
         $obj->Title = !empty($content['alt']) ? $content['alt'] : str_replace(array('-', '_'), ' ', preg_replace('/\\.[^.]+$/', '', $obj->Name));
         $obj->FileName = $kapostMediaFolder->getRelativePath() . '/' . $file . $ext;
         $obj->ParentID = $kapostMediaFolder->ID;
         //If subsites is enabled add it to the correct subsite
         if (File::has_extension('FileSubsites')) {
             $obj->SubsiteID = $blog_id;
         }
         $obj->write();
         $this->extend('updateNewMediaAsset', $blog_id, $content, $obj);
         return array('id' => $obj->ID, 'url' => $obj->getAbsoluteURL());
     }
 }