Пример #1
0
 /**
  * Uploads a file
  * 
  * @internal
  * 
  * @param  fActiveRecord $object            The fActiveRecord instance
  * @param  array         &$values           The current values
  * @param  array         &$old_values       The old values
  * @param  array         &$related_records  Any records related to this record
  * @param  array         &$cache            The cache array for the record
  * @param  string        $method_name       The method that was called
  * @param  array         $parameters        The parameters passed to the method
  * @return fFile  The uploaded file
  */
 public static function upload($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
 {
     $class = get_class($object);
     list($action, $column) = fORM::parseMethod($method_name);
     $existing_temp_file = FALSE;
     // Try to upload the file putting it in the temp dir incase there is a validation problem with the record
     try {
         $upload_dir = self::$file_upload_columns[$class][$column];
         $temp_dir = self::prepareTempDir($upload_dir);
         if (!fUpload::check($column)) {
             throw new fExpectedException('Please upload a file');
         }
         $uploader = self::setUpFUpload($class, $column);
         $file = $uploader->move($temp_dir, $column);
         // If there was an eror, check to see if we have an existing file
     } catch (fExpectedException $e) {
         // If there is an existing file and none was uploaded, substitute the existing file
         $existing_file = fRequest::get('existing-' . $column);
         $delete_file = fRequest::get('delete-' . $column, 'boolean');
         $no_upload = $e->getMessage() == self::compose('Please upload a file');
         if ($existing_file && $delete_file && $no_upload) {
             $file = NULL;
         } elseif ($existing_file) {
             $file_path = $upload_dir->getPath() . $existing_file;
             $file = fFilesystem::createObject($file_path);
             $current_file = $values[$column];
             // If the existing file is the same as the current file, we can just exit now
             if ($current_file && $file->getPath() == $current_file->getPath()) {
                 return;
             }
             $existing_temp_file = TRUE;
         } else {
             $file = NULL;
         }
     }
     // Assign the file
     fActiveRecord::assign($values, $old_values, $column, $file);
     // Perform the file upload inheritance
     if (!empty(self::$column_inheritence[$class][$column])) {
         foreach (self::$column_inheritence[$class][$column] as $other_column) {
             if ($file) {
                 // Image columns will only inherit if it is an fImage object
                 if (!$file instanceof fImage && isset(self::$image_upload_columns[$class][$other_column])) {
                     continue;
                 }
                 $other_upload_dir = self::$file_upload_columns[$class][$other_column];
                 $other_temp_dir = self::prepareTempDir($other_upload_dir);
                 if ($existing_temp_file) {
                     $other_file = fFilesystem::createObject($other_temp_dir->getPath() . $file->getName());
                 } else {
                     $other_file = $file->duplicate($other_temp_dir, FALSE);
                 }
             } else {
                 $other_file = $file;
             }
             fActiveRecord::assign($values, $old_values, $other_column, $other_file);
             if (!$existing_temp_file && $other_file) {
                 self::processImage($class, $other_column, $other_file);
             }
         }
     }
     // Process the file
     if (!$existing_temp_file && $file) {
         self::processImage($class, $column, $file);
     }
     return $file;
 }
Пример #2
0
 /**
  * Moves an uploaded file from the temp directory to a permanent location
  * 
  * @throws fValidationException  When `$directory` is somehow invalid or ::validate() thows an exception
  * 
  * @param  string|fDirectory $directory  The directory to upload the file to
  * @param  string            $field      The file upload field to get the file from
  * @param  mixed             $index      If the field was an array file upload field, upload the file corresponding to this index
  * @return fFile|NULL  An fFile (or fImage) object, or `NULL` if no file was uploaded
  */
 public function move($directory, $field, $index = NULL)
 {
     if (!is_object($directory)) {
         $directory = new fDirectory($directory);
     }
     if (!$directory->isWritable()) {
         throw new fProgrammerException('The directory specified, %s, is not writable', $directory->getPath());
     }
     if (!self::check($field)) {
         throw new fProgrammerException('The field specified, %s, does not appear to be a file upload field', $field);
     }
     $file_array = $this->extractFileUploadArray($field, $index);
     $error = $this->validateField($file_array);
     if ($error) {
         throw new fValidationException($error);
     }
     // This will only ever be true if the file is optional
     if ($file_array['name'] == '' || $file_array['tmp_name'] == '' || $file_array['size'] == 0) {
         return NULL;
     }
     $file_name = fFilesystem::makeURLSafe($file_array['name']);
     $file_name = $directory->getPath() . $file_name;
     if (!$this->enable_overwrite) {
         $file_name = fFilesystem::makeUniqueName($file_name);
     }
     if (!move_uploaded_file($file_array['tmp_name'], $file_name)) {
         throw new fEnvironmentException('There was an error moving the uploaded file');
     }
     if (!chmod($file_name, 0644)) {
         throw new fEnvironmentException('Unable to change permissions on the uploaded file');
     }
     return fFilesystem::createObject($file_name);
 }
Пример #3
0
<?php

define('MY_ROOT', realpath(dirname(__FILE__) . '/../'));
include MY_ROOT . '/inc/init.php';
//header('Content-type: application/json; charset=utf-8');
///////////////// PACHUBE ///////////////////
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://radiation.crowdmap.com/feed/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// grab URL and pass it to the browser
$output = curl_exec($ch);
try {
    $file = new fFile(DOC_ROOT . '/feeds/crowdmapStatic.xml');
} catch (fExpectedException $e) {
    $file = fFilesystem::createObject(DOC_ROOT . '/feeds/crowdmapStatic.xml');
}
$file->write($output);
// close cURL resource, and free up system resources
curl_close($ch);
echo $output;
?>


Пример #4
0
 /**
  * Performs a [http://php.net/scandir scandir()] on a directory, removing the `.` and `..` entries
  * 
  * If the `$filter` looks like a valid PCRE pattern - matching delimeters
  * (a delimeter can be any non-alphanumeric, non-backslash, non-whitespace
  * character) followed by zero or more of the flags `i`, `m`, `s`, `x`,
  * `e`, `A`, `D`,  `S`, `U`, `X`, `J`, `u` - then
  * [http://php.net/preg_match `preg_match()`] will be used.
  * 
  * Otherwise the `$filter` will do a case-sensitive match with `*` matching
  * zero or more characters and `?` matching a single character.
  * 
  * On all OSes (even Windows), directories will be separated by `/`s when
  * comparing with the `$filter`.
  * 
  * @param  string $filter  A PCRE or glob pattern to filter files/directories by path - directories can be detected by checking for a trailing / (even on Windows)
  * @return array  The fFile (or fImage) and fDirectory objects for the files/directories in this directory
  */
 public function scan($filter = NULL)
 {
     $this->tossIfDeleted();
     $files = array_diff(scandir($this->directory), array('.', '..'));
     $objects = array();
     if ($filter && !preg_match('#^([^a-zA-Z0-9\\\\\\s]).*\\1[imsxeADSUXJu]*$#D', $filter)) {
         $filter = '#^' . strtr(preg_quote($filter, '#'), array('\\*' => '.*', '\\?' => '.')) . '$#D';
     }
     natcasesort($files);
     foreach ($files as $file) {
         if ($filter) {
             $test_path = is_dir($this->directory . $file) ? $file . '/' : $file;
             if (!preg_match($filter, $test_path)) {
                 continue;
             }
         }
         $objects[] = fFilesystem::createObject($this->directory . $file);
     }
     return $objects;
 }
Пример #5
0
 /**
  * Performs a [http://php.net/scandir scandir()] on a directory, removing the `.` and `..` entries
  * 
  * @param  string $regex_filter  A PCRE to filter files/directories by path, directories can be detected by checking for a trailing / (even on Windows)
  * @return array  The fFile (or fImage) and fDirectory objects for the files/directories in this directory
  */
 public function scan($regex_filter = NULL)
 {
     $this->tossIfException();
     $files = array_diff(scandir($this->directory), array('.', '..'));
     $objects = array();
     foreach ($files as $file) {
         $file = $this->directory . $file;
         if ($regex_filter) {
             $test_path = is_dir($file) ? $file . '/' : $file;
             if (!preg_match($regex_filter, $test_path)) {
                 continue;
             }
         }
         $objects[] = fFilesystem::createObject($file);
     }
     return $objects;
 }
Пример #6
0
 /**
  * Moves an uploaded file from the temp directory to a permanent location
  * 
  * @throws fValidationException  When `$directory` is somehow invalid or ::validate() thows an exception
  * 
  * @param  string|fDirectory $directory  The directory to upload the file to
  * @param  string            $field      The file upload field to get the file from
  * @param  integer           $index      If the field was an array file upload field, upload the file corresponding to this index
  * @return fFile  An fFile (or fImage) object
  */
 public function move($directory, $field, $index = NULL, $param_filename = NULL)
 {
     if (!is_object($directory)) {
         $directory = new fDirectory($directory);
     }
     if (!$directory->isWritable()) {
         throw new fProgrammerException('The directory specified, %s, is not writable', $directory->getPath());
     }
     if (!self::check($field)) {
         throw new fProgrammerException('The field specified, %s, does not appear to be a file upload field', $field);
     }
     $file_array = $this->validate($field, $index);
     $file_name = fFilesystem::makeURLSafe($param_filename == NULL ? $file_array['name'] : $param_filename);
     $file_name = $directory->getPath() . $file_name;
     if (!$this->enable_overwrite) {
         $file_name = fFilesystem::makeUniqueName($file_name);
     }
     if (!move_uploaded_file($file_array['tmp_name'], $file_name)) {
         throw new fEnvironmentException('There was an error moving the uploaded file');
     }
     if (!chmod($file_name, 0644)) {
         throw new fEnvironmentException('Unable to change permissions on the uploaded file');
     }
     return fFilesystem::createObject($file_name);
 }
Пример #7
0
foreach ($flickrFeeds as $feed) {
    $ch = curl_init();
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $feed);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $data = curl_exec($ch);
    // execute curl session
    $data = str_replace('jsonFlickrApi(', '', $data);
    $data = substr($data, 0, strlen($data) - 1);
    //strip out last paren
    $object = json_decode($data);
    // stdClass object
    $items = $object->photos->photo;
    foreach ($items as $item) {
        $newFeed .= '{"title": "' . $item->title . '","title_jp": "","description": "' . $item->tags . '","creator": "probe","feed": "http://api.flickr.com/services/rest","location": {"lon":' . $item->longitude . ', "lat":' . $item->latitude . ', "name": ""},"id":"' . $item->owner . '","datastreams": [{"at": "' . $item->datetaken . '","max_value": "0","min_value": "0","current_value": "' . $item->url_m . '","id": "1","unit": {"type": "photo","label": "photo","symbol": "photo"}}]},';
    }
    curl_close($ch);
    $total += count($items);
}
$newFeed = substr($newFeed, 0, strlen($newFeed) - 1);
//strip out last comma
$newFeed .= '], "itemsPerPage": ' . $total . ', "startIndex": 0, "totalResults": ' . $total . '}';
try {
    $file = new fFile(DOC_ROOT . '/feeds/probe002.json');
} catch (fExpectedException $e) {
    $file = fFilesystem::createObject(DOC_ROOT . '/feeds/probe002.json');
}
$file->write($newFeed);
// close cURL resource, and free up system resources
print_r($newFeed);