/** * Constructs a new Debug-Object. * * The first parameter has to be the file name with extension, but without directory. The second * parameter has to be a valid and existing directory path with trainling directory separator * (make sure the directory is writable). Standard value for this paramteer is null. If the * directory is null or invalid "data/logs/" will be used. If the specified file doesn't exist * it will created. If it is not possible to create a file a NonFatalException will be thrown. * * @param string File for saving the logdata * @param string Valid Directory for saving the logfile or null (directory will be "data/logs/") * @throws NonFatalException */ public function __construct($file, $dir = null) { if ($dir === null || is_dir($dir) === false) { $dir = 'data/logs/'; } $this->file = new File($dir.basename($file)); if ($this->file->create() === false) { throw new NonFatalException('Could not create log file "'.$this->file->relPath().'".'); } if ($this->file->readable() === false || $this->file->writable() === false) { $this->file->setPermissions(666); } $this->logs = array(); $this->benchmarks = array(); $this->temp = array(); }
public function fetch($key) { $cacheFile = CacheStorageFile::getCacheFile($key); if (File::exists($cacheFile) && File::readable($cacheFile)) { return @file_get_contents($cacheFile); } return false; }
/** * Constructs a new Debug-Object. * * The first parameter has to be the file name with extension, but without directory. * Standard value is null, then the filename will be "internal.log". * The second parameter has to be a valid and existing directory with trailing slash. * Standard value is also null, then the directory will be "data/logs/" (make sure that it is writable). * If the specified file doesn't exist, it will created. * If it is not possible to create a file nn Exception will be thrown. * * @param string File for saving the logdata or null (filename is internal.log then) * @param string Directory for saving the logfile or null (directory is data/logs/ then) * @throws Exception */ public function __construct($file = null, $dir = null) { if ($dir == null || is_dir($dir) == false) { $dir = 'data/logs/'; } if ($file == null) { $file = 'internal.log'; } $this->file = new File($dir . basename($file)); if ($this->file->create() == false) { throw new Exception('Could not create log file in method Debug::__construct().'); } if ($this->file->readable() == false || $this->file->writable() == false) { $writable = new CHMOD(666); $this->file->setChmod($writable); } $this->logs = array(); $this->benchmarks = array(); $this->temp = array(); }
private function loadContainers($containers) { if (!File::exists($containers)) { throw new Exception("Invalid container path"); } foreach (glob("{$containers}/*.js") as $file) { if (!File::readable($file)) { throw new Exception("Could not read container config: {$file}"); } if (is_dir($file)) { // support recursive loading of sub directories $this->loadContainers($file); } else { $this->loadFromFile($file); } } }
public function open($dir, $filename, $write = false) { $this->_currentFile = null; $this->_currentFileLastModified = null; $ds = $this->getDataSource(); $file = new File($filepath = $ds->config['path'] . DS . $dir . DS . $filename, false); if (!$file) { return false; } if (!$file->readable()) { return false; } if ($write && !$file->writable()) { return false; } $this->_currentFile = $file; $this->_currentFileLastChange = $file->lastChange(); return $file; }
/** * beforeRender callback function * * @param Controller $controller * @return array contents for panel */ public function beforeRender(Controller $controller) { $data = array(); $dir = new Folder(LOGS); $files = $dir->find(); foreach ($files as $log) { $file = new File(LOGS . $log); $name = $file->name(); $data[$name] = array(); if (!$file->readable()) { $data[$name]['content'] = __('This log file is unreadable.'); continue; } $data[$name]['lastChange'] = date('Y-m-d H:i:s', $file->lastChange()); if ($file->size() > $this->readBytes) { $file->offset(-$this->readBytes, SEEK_END); } $data[$name]['content'] = $file->read($this->readBytes); } return $data; }
/** * @param keyFile The file containing your private key for signing requests. */ public function __construct($keyFile = null) { $this->keyName = 'http://' . $_SERVER["HTTP_HOST"] . Config::get('web_prefix') . '/public.cer'; if (!empty($keyFile)) { $rsa_private_key = false; $privateKey = null; try { if (File::exists($keyFile)) { if (File::readable($keyFile)) { $rsa_private_key = @file_get_contents($keyFile); } else { throw new Exception("Could not read keyfile ({$keyFile}), check the file name and permission"); } } if (!$rsa_private_key) { $rsa_private_key = ''; } else { $phrase = Config::get('private_key_phrase') != '' ? Config::get('private_key_phrase') : null; if (strpos($rsa_private_key, "-----BEGIN") === false) { $privateKey .= "-----BEGIN PRIVATE KEY-----\n"; $chunks = str_split($rsa_private_key, 64); foreach ($chunks as $chunk) { $privateKey .= $chunk . "\n"; } $privateKey .= "-----END PRIVATE KEY-----"; } else { $privateKey = $rsa_private_key; } if (!($rsa_private_key = @openssl_pkey_get_private($privateKey, $phrase))) { throw new Exception("Could not create the key"); } } } catch (Exception $e) { throw new Exception("Error loading private key: " . $e); } $this->privateKey = $rsa_private_key; } }
/** * Tests a file's contents against magic items * * @param string $file Absolute path to a file * @param array $items * @return mixed A string containing the MIME type of the file or false if no pattern matched * @access private */ function __test($file, $items) { $File = new File($file); if (!$File->readable()) { return false; } $File->open('rb'); foreach ($items as $item) { if ($result = $this->__testRecursive($File, $item)) { return $result; } } return false; }
/** * Handles existent but not readable files * * @access protected * @return mixed */ function _handleNotReadable() { if (!$this->__File->readable() && $this->__File->exists()) { $message = 'File `' . $this->shortPath($this->__File->pwd()) . '`'; $message .= ' exists but is not readable.'; $this->out($message); return true; } return false; }
/** * Setup for display or download the given file. * * If $_SERVER['HTTP_RANGE'] is set a slice of the file will be * returned instead of the entire file. * * ### Options keys * * - name: Alternate download name * - download: If `true` sets download header and forces file to be downloaded rather than displayed in browser * * @param string $path Path to file. If the path is not an absolute path that resolves * to a file, `APP` will be prepended to the path. * @param array $options Options See above. * @return void * @throws NotFoundException */ public function file($path, $options = array()) { $options += array('name' => null, 'download' => null); if (strpos($path, '../') !== false || strpos($path, '..\\') !== false) { throw new NotFoundException(__d('cake_dev', 'The requested file contains `..` and will not be read.')); } if (!is_file($path)) { $path = APP . $path; } $file = new File($path); if (!$file->exists() || !$file->readable()) { if (Configure::read('debug')) { throw new NotFoundException(__d('cake_dev', 'The requested file %s was not found or not readable', $path)); } throw new NotFoundException(__d('cake', 'The requested file was not found')); } $extension = strtolower($file->ext()); $download = $options['download']; if ((!$extension || $this->type($extension) === false) && $download === null) { $download = true; } $fileSize = $file->size(); if ($download) { $agent = env('HTTP_USER_AGENT'); if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) { $contentType = 'application/octet-stream'; } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) { $contentType = 'application/force-download'; } if (!empty($contentType)) { $this->type($contentType); } if ($options['name'] === null) { $name = $file->name; } else { $name = $options['name']; } $this->download($name); $this->header('Content-Transfer-Encoding', 'binary'); } $this->header('Accept-Ranges', 'bytes'); $httpRange = env('HTTP_RANGE'); if (isset($httpRange)) { $this->_fileRange($file, $httpRange); } else { $this->header('Content-Length', $fileSize); } $this->_clearBuffer(); $this->_file = $file; }
public function grabFiles() { $validItems = $this->getFileRules(); App::uses('Folder', 'Utility'); App::uses('File', 'Utility'); $result = array(); foreach ($validItems as $k => &$item) { $dir = new Folder($item['path']); $files = $dir->find($item['regex'], true); foreach ($files as $file) { $f = new File($item['path'] . DS . $file); $validItems[$k]['files'][] = array('filename' => $file, 'filesize' => $f->size(), 'read' => $f->readable(), 'write' => $f->writable(), 'execute' => $f->executable()); } } return $validItems; }
/** * undocumented function * * @return void */ function parent() { $config = $this->config(); if ($config['id'] == 1 || empty($config['fork'])) { return array(); } if (!empty($config['fork'])) { $path = Configure::read("Content.{$config['repo']['type']}") . 'repo' . DS . $config['url'] . '.git' . DS; } $File = new File($path . 'permissions.ini'); if (!$File->readable()) { if (!$File->create()) { return false; } } return $File->read(); }
/** * Retrieve (cached) metadata of a file * * @param Model $Model * @param string $file An absolute path to a file * @param integer $level level of amount of info to add, `0` disable, `1` for basic, `2` for detailed info * @return mixed Array with results or false if file is not readable */ function metadata(&$Model, $file, $level = 1) { if ($level < 1) { return array(); } extract($this->settings[$Model->alias]); $File = new File($file); if (!$File->readable()) { return false; } $checksum = $File->md5(true); if (isset($this->__cached[$Model->alias][$checksum])) { $data = $this->__cached[$Model->alias][$checksum]; } if ($level > 0 && !isset($data[1])) { $data[1] = array('size' => $File->size(), 'mime_type' => MimeType::guessType($File->pwd()), 'checksum' => $checksum); } if ($level > 1 && !isset($data[2])) { $Media = Media::factory($File->pwd()); if ($Media->name === 'Audio') { $data[2] = array('artist' => $Media->artist(), 'album' => $Media->album(), 'title' => $Media->title(), 'track' => $Media->track(), 'year' => $Media->year(), 'length' => $Media->duration(), 'quality' => $Media->quality(), 'sampling_rate' => $Media->samplingRate(), 'bit_rate' => $Media->bitRate()); } elseif ($Media->name === 'Image') { $data[2] = array('width' => $Media->width(), 'height' => $Media->height(), 'ratio' => $Media->ratio(), 'quality' => $Media->quality(), 'megapixel' => $Media->megapixel()); } elseif ($Media->name === 'Text') { $data[2] = array('characters' => $Media->characters(), 'syllables' => $Media->syllables(), 'sentences' => $Media->sentences(), 'words' => $Media->words(), 'flesch_score' => $Media->fleschScore(), 'lexical_density' => $Media->lexicalDensity()); } elseif ($Media->name === 'Video') { $data[2] = array('title' => $Media->title(), 'year' => $Media->year(), 'length' => $Media->duration(), 'width' => $Media->width(), 'height' => $Media->height(), 'ratio' => $Media->ratio(), 'quality' => $Media->quality(), 'bit_rate' => $Media->bitRate()); } else { $data[2] = array(); } } for ($i = $level, $result = array(); $i > 0; $i--) { $result = array_merge($result, $data[$i]); } $this->__cached[$Model->alias][$checksum] = $data; return Set::filter($result); }
function __loadDbItems() { // variable used to determine the read dir time $acdate = strtotime("now"); // check to see whether a valid directory was passed to the script if ($this->Session->read('User.dirname_get')) { // if it is valid, we'll set it as the directory to read data from $this->dirpath = $this->Session->read('User.dirname_get'); } else { // if it is invalid, we'll use the default directory $this->dirpath = Configure::read('default_get_dir'); } // use Folder class $dir = new Folder($this->dirpath); // try to change the current working directory to the one from wich i want to read contents from if (!$dir->cd($this->dirpath)) { // if the change failed, I'll use the default directory $this->dirpath = Configure::read('default_get_dir'); $dir->cd(Configure::read('default_get_dir')); } // once the current working directory is set, it is opened and read from $dir_listing = $dir->read(true, false, true); if ($dir_listing) { // while there are still entries foreach ($dir_listing[1] as $entry) { // if the entry is to be shown (not part of the 'not_to_be_shown' array) if (!in_array($entry, Configure::read('not_to_be_shown'))) { $file = new File($entry); if ($file->readable()) { // store the file extension $fext = $file->ext(); // store the filename $fname = $file->name; // store the lowercased extension $lfext = strtolower($fext); // store size of file into KB $fsize = round($file->size() / 1024, 2); // store date of file $fidate = $file->lastChange(); // store dirpath with file $finfokey = $entry; // store absfilename $fnameabs = $file->name(); // define check for filestatus_status (if updated) $update_status = Configure::read('msg_items_file_unselected'); // check table fileinfo for update or insert $file_info = $this->FileInfo->find('first', array('conditions' => array('fileinfo_id' => $finfokey), 'fields' => array('fileinfo_id', 'fileinfo_filedate'))); if (!empty($file_info)) { $this->FileInfo->read(null, $file_info['FileInfo']['fileinfo_id']); $this->FileInfo->set(array('fileinfo_dirname' => $this->dirpath, 'fileinfo_filename' => $fname, 'fileinfo_absfilename' => $fnameabs, 'fileinfo_ext' => $lfext, 'fileinfo_size' => $fsize, 'fileinfo_filedate' => $fidate, 'fileinfo_timenow' => $acdate)); $this->FileInfo->save(); // check data modified file is changed if ($fidate > $file_info['FileInfo']['fileinfo_filedate']) { $update_status = Configure::read('msg_items_file_updated'); } } else { $this->FileInfo->create(); $this->FileInfo->set(array('fileinfo_id' => $finfokey, 'fileinfo_dirname' => $this->dirpath, 'fileinfo_filename' => $fname, 'fileinfo_absfilename' => $fnameabs, 'fileinfo_ext' => $lfext, 'fileinfo_size' => $fsize, 'fileinfo_filedate' => $fidate, 'fileinfo_timenow' => $acdate)); $this->FileInfo->save(); } // check table filestatus for update or insert $file_status = $this->FileStatus->find('first', array('conditions' => array('filestatus_fileinfo_key' => $finfokey, 'filestatus_users_id' => $this->Session->read('User.id')), 'fields' => array('filestatus_id', 'filestatus_status'))); if (!empty($file_status)) { if ($file_status['FileStatus']['filestatus_status'] == Configure::read('msg_items_file_selected') && $update_status != Configure::read('msg_items_file_updated')) { $update_status = Configure::read('msg_items_file_selected'); } $this->FileStatus->read(null, $file_status['FileStatus']['filestatus_id']); $this->FileStatus->set(array('filestatus_status' => $update_status, 'filestatus_users_id' => $this->Session->read('User.id'), 'filestatus_timenow' => $acdate)); $this->FileStatus->save(); } else { $this->FileStatus->create(); $this->FileStatus->set(array('filestatus_fileinfo_key' => $finfokey, 'filestatus_status' => $update_status, 'filestatus_users_id' => $this->Session->read('User.id'), 'filestatus_timenow' => $acdate)); $this->FileStatus->save(); } } } } // check consistency : delete from db files that's removed from directory $file_info_del = $this->FileInfo->deleteAll(array('fileinfo_timenow < ' => $acdate)); if (!$file_info_del) { $this->log('DownloadsController:__loadDbItems - Unable delete FileInfo model record', Configure::read('log_file')); } // check consistency : delete from db files that's removed from directory $file_status_del = $this->FileStatus->deleteAll(array('filestatus_timenow < ' => $acdate, 'filestatus_users_id' => $this->Session->read('User.id'))); if (!$file_status_del) { $this->log('DownloadsController:__loadDbItems - Unable delete FileStatus model record', Configure::read('log_file')); } } }
public function get($key, $expiration = false) { if (!$expiration) { // if no expiration time was given, fall back on the global config $expiration = Config::get('cache_time'); } $cacheFile = $this->getCacheFile($key); // See if this cache file is locked, if so we wait upto 5 seconds for the lock owning process to // complete it's work. If the lock is not released within that time frame, it's cleaned up. // This should give us a fair amount of 'Cache Stampeding' protection if ($this->isLocked($cacheFile)) { $this->waitForLock($cacheFile); } if (File::exists($cacheFile) && File::readable($cacheFile)) { $now = time(); if (($mtime = @filemtime($cacheFile)) !== false && $now - $mtime < $expiration) { if (($data = @file_get_contents($cacheFile)) !== false) { $data = unserialize($data); return $data; } } } return false; }
function uploadFinalPredictions() { $this->Cookie = $this->Components->load('Cookie'); $this->Cookie->type('rijndael'); // $this->Cookie->time = '999 hours'; if ($this->request->is(array('post', 'put'))) { // debug($this->request->data); $email = $this->request->data['Participant']['email']; $code = $this->request->data['Participant']['code']; $task = $this->request->data['Participant']["task"]; $run = $this->request->data['Participant']["run"]; $maxUploadTask = Configure::read('max_participant_task_upload'); $tasks = Configure::read('biocreative_tasks'); $finalDate = Configure::read('final_date_to_upload_tasks'); $startDate = Configure::read('initial_date_to_upload_tasks'); if (!in_array($task, $tasks) && $run < 0 && $run > 5) { $this->Session->setFlash("Incorrect data"); return $this->redirect(array('controller' => 'Participants', 'action' => 'analysis')); } App::uses('CakeTime', 'Utility'); $isEnd = CakeTime::isPast($finalDate); $isFuture = CakeTime::isFuture($startDate); if ($isEnd || $isFuture) { $this->Session->setFlash("The final delivery date has expired"); return $this->redirect(array('controller' => 'Participants', 'action' => 'analysis')); } $project_id = -1; if ($this->data['Participant']['remember_me'] && isset($code) && isset($email)) { $cookie = array(); $cookie['email'] = $email; $cookie['code'] = $code; $this->Cookie->write('participantData', $cookie, true, '+2 weeks'); } else { if ($this->Cookie->check('participantData')) { $this->Cookie->destroy('participantData'); } } App::uses('Folder', 'Utility'); App::uses('File', 'Utility'); // debug($this->request->data); if ($this->request->data['Participant']['final_prediction']['size'] > 0) { /* ============================================= */ /* ==============Load analysis=================== */ /* ============================================= */ if ($this->request->data['Participant']['final_prediction']['size'] > $this->filesize2bytes(Configure::read('max_file_size'))) { $this->Session->setFlash("The file can not be more than " . Configure::read('max_file_size')); return $this->redirect(array('controller' => 'Participants', 'action' => 'analysis')); } $file = $this->request->data['Participant']['final_prediction']['name']; if (pathinfo($file, PATHINFO_EXTENSION) != 'tsv') { $this->Session->setFlash("The file must be in TSV format"); return $this->redirect(array('controller' => 'Participants', 'action' => 'analysis')); } $file = new File($this->request->data['Participant']['final_prediction']['tmp_name']); if ($file->readable()) { $content = $file->read(); $file->close(); $lines = explode("\n", $content); $incorrectFormat = empty($lines); $count = 0; $size = count($lines); $correctColumns = 5; if ($task == "CPD") { $correctColumns = 4; } for ($index = 0; $index < $size; $index++) { if (strlen(trim($lines[$index])) > 0) { if (!$incorrectFormat) { $columns = explode("\t", $lines[$index]); for ($i = 0; $i < count($columns); $i++) { if (strlen(trim($columns[$i])) == 0) { $incorrectFormat = true; } } $incorrectFormat = $incorrectFormat || sizeof($columns) != $correctColumns; $count++; } else { break; } } } // $correctFormat = $this->correctTsvFormat($file, 5); if ($incorrectFormat) { // $count=$this->incorrecLineTsvFormat($file); if ($task == "CPD") { $this->Session->setFlash("Incorrect content file. Line {$count} is incorrect. " . "Content file must be contain 4 columns"); } else { $this->Session->setFlash("Incorrect content file. Line {$count} is incorrect. " . "Content file must be in this format WO2009026621A1->A:12:24->1->0.99->paliperidone"); } return $this->redirect(array('controller' => 'Participants', 'action' => 'analysis')); } $participant = $this->Participant->find('first', array("recursive" => -1, "fields" => array("id", "team_id"), "conditions" => array('Participant.email' => $email, 'Participant.code' => $code))); $participantID = $participant["Participant"]["id"]; $team_id = $participant["Participant"]["team_id"]; $this->request->data['Participant']['id'] = $participantID; $this->participantSaveConnection($this->request->data['Participant'], "uploadTeamPrediction"); $path = Configure::read('participantsPath'); $path = $path . DS . $task . DS . $team_id; $dir = new Folder($path, true, 0755); $tempPath = $this->request->data['Participant']['final_prediction']['tmp_name']; $file = new File($tempPath); if ($file->readable()) { $content = $file->read(); $newPath = $dir->pwd() . DS . "{$run}.tsv"; $file->copy($newPath); $path = $newPath; chmod($newPath, 0200); $data = array('participant_id' => $participantID, 'biocreative_task' => $task, 'run' => $run); $finalfile = $this->Participant->FinalPrediction->find('first', array("conditions" => $data, 'fields' => 'id')); if (!empty($finalfile)) { $this->Participant->FinalPrediction->id = $finalfile['FinalPrediction']['id']; $this->Session->setFlash("The file has been updated successfully", "success"); $type = "updated"; } else { $this->Participant->FinalPrediction->create(); $this->Session->setFlash("The file has been created successfully", "success"); $type = "uploaded"; } $data = array('participant_id' => $participantID, 'run' => $run, 'email' => $email, 'biocreative_task' => $task, 'file' => $content, 'file_name' => $this->request->data['Participant']['final_prediction']['name']); if (!$this->Participant->FinalPrediction->save($data)) { $this->Session->setFlash("File could not be saved"); return $this->redirect(array('controller' => 'Participants', 'action' => 'analysis')); } else { $this->sendMailWithAttachment("finalPredictionSuccess", $email, "[MARKYT - BIOCREATIVE V (CHEMDNER)] Final submision", array("run" => $run, "type" => $type, "task" => $task), array("{$run}.tsv" => $tempPath)); } return $this->redirect(array('controller' => 'Participants', 'action' => 'analysis')); } } else { throw new Exception("Ops! file could not be readed"); } } else { $this->Session->setFlash("One team prediction file is needed"); return $this->redirect(array('controller' => 'Participants', 'action' => 'analysis')); } } }
/** * Retrieve (cached) metadata of a file * * @param Model $Model * @param string $file An absolute path to a file * @param integer $level level of amount of info to add, `0` disable, `1` for basic, `2` for detailed info * @return mixed Array with results or false if file is not readable */ function metadata(&$Model, $file, $level = 1) { if ($level < 1) { return array(); } extract($this->settings[$Model->alias]); $File = new File($file); if (!$File->readable()) { return false; } $checksum = $File->md5(true); if (isset($this->__cached[$Model->alias][$checksum])) { $data = $this->__cached[$Model->alias][$checksum]; } if ($level > 0 && !isset($data[1])) { $data[1] = array('size' => $File->size(), 'mime_type' => Mime_Type::guessType($File->pwd()), 'checksum' => $checksum); } if ($level > 1 && !isset($data[2])) { $data[2] = array(); try { $Info = Media_Info::factory(array('source' => $File->pwd())); foreach ($Info->all() as $key => $value) { $data[2][Inflector::underscore($key)] = $value; } } catch (Exception $E) { } } for ($i = $level, $result = array(); $i > 0; $i--) { $result = array_merge($result, $data[$i]); } $this->__cached[$Model->alias][$checksum] = $data; return $result; }
function importAnnotationsAndDocuments() { $this->Round = $this->Project->Round; $this->Document = $this->Project->Document; $this->UsersRound = $this->Project->User->UsersRound; $this->AnnotatedDocument = $this->Round->AnnotatedDocument; $this->Type = $this->Project->Type; $this->Annotation = $this->Type->Annotation; $maxAnnotation = $this->Annotation->find('first', array('recursive' => -1, 'fields' => 'id', 'order' => 'id DESC')); if (isset($maxAnnotation['Annotation']['id'])) { $maxAnnotation = $maxAnnotation['Annotation']['id']; } else { $maxAnnotation = 0; } $maxType = $this->Project->Type->find('first', array('recursive' => -1, 'fields' => 'id', 'order' => 'id DESC')); if (isset($maxType['Type']['id'])) { $maxType = $maxType['Type']['id']; } else { $maxType = 0; } if ($this->request->is('post') || $this->request->is('put')) { $maxAnnotation++; App::uses('Folder', 'Utility'); App::uses('File', 'Utility'); App::uses('CakeTime', 'Utility'); $scriptTimeLimit = Configure::read('scriptTimeLimit'); set_time_limit($scriptTimeLimit); $users = $this->request->data['User']['User']; //App::import('Vendor', 'htmLawed', array('file' => 'htmLawed' . DS . 'htmLawed.php')); $zipFile = new File($this->request->data['Project']['File']['tmp_name'], false, 0777); $projectName = ucwords(pathinfo($this->request->data['Project']['File']['name'], PATHINFO_FILENAME)); if ($zipFile->exists() && !empty($users)) { $filesAllowed = Configure::read('filesAllowed'); $zip = new ZipArchive(); if (!$zip->open($zipFile->pwd(), ZipArchive::CREATE)) { $zipFile->remove(); throw new Exception("Failed to open Zip archive\n"); } $path = Configure::read('uploadFolder'); $path = $path . uniqid(); //creamos la carpeta de descarga para el usuario si no existe $extractFolder = new Folder($path, true, 0700); $zip->extractTo($extractFolder->pwd()); $zip->close(); $zipFile->delete(); // I am deleting this file $rounds = array(); $db = $this->Project->getDataSource(); $db->query("ALTER TABLE annotations AUTO_INCREMENT = {$maxAnnotation}"); $db->execute("LOCK TABLES " . $this->Annotation->table . " " . $this->Type->table . " WRITE"); $db->begin(); $db->useNestedTransactions = false; $this->Project->create(); if ($this->Project->save(array('Project' => array("title" => $projectName, "description" => ""), "User" => array('User' => $users)))) { //creamos round de anotacion automatica $this->Round->create(); if (!$this->Round->save(array('Round' => array("title" => "Round for job " . date("Y/m/d"), "project_id" => $this->Project->id, "description" => "-", 'ends_in_date' => date("Y-m-d", strtotime("+30 day")), 'trim_helper' => true, 'whole_word_helper' => true, 'punctuation_helper' => true, 'start_document' => 0, 'end_document' => 0, 'is_visible' => 0)))) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("Round work can not be saved"); } array_push($rounds, $this->Round->id); foreach ($users as $user) { foreach ($rounds as $round) { $this->UsersRound->create(); if (!$this->UsersRound->save(array('state' => 0, 'round_id' => $round, 'user_id' => $user))) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("This UsersRound can not be save:" . $fileName); } } } $typesText = new File($extractFolder->pwd() . DS . 'types.txt', false, 0644); if (!$typesText->exists()) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("types.txt not exist in zip file"); } $count = new File($extractFolder->pwd() . DS . 'count.txt', false, 0644); if (!$count->exists()) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("count.txt not exist in zip filer"); } $typesText = $typesText->read(); $count = $count->read(); $count = intval($count) + 1; //buscamos la anotacion final con dicho Id si ya existe no se puede crear el proyecto $existLastId = $this->Annotation->find('count', array('recursive' => -1, 'conditions' => array('id' => $count))); $existInitialId = $this->Annotation->find('count', array('recursive' => -1, 'conditions' => array('id' => $count - ($count - $maxAnnotation)))); if ($existInitialId > 0) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("the first id already exist in Marky"); } if ($existLastId > 0) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("the last id already exist in Marky"); } $types = preg_split("/[\r\n]+/", trim($typesText)); $typesMap = array(); $defaultColors = Configure::read('defaultColors'); $colorCont = 0; foreach ($types as $type) { //debug($types); $type = explode("\t", $type); if (sizeof($type) > 1) { $typeName = $type[0]; $typeId = $type[1]; $typeName = str_replace(' ', '_', $typeName); if (!empty($defaultColors[$colorCont])) { $color = $defaultColors[$colorCont]; $colorCont++; } else { $color = array(rand(50, 255), rand(50, 255), rand(50, 255)); } $this->Type->create(); if ($this->Type->save(array('Type' => array('id' => $typeId, 'name' => $typeName, 'project_id' => $this->Project->id, 'colour' => $color[0] . ',' . $color[1] . ',' . $color[2] . ',1'), 'Round' => array('Round' => $rounds)))) { $typesMap[$typeName] = $this->Type->id; } else { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); // debug($types); // debug($this->Project->Type->validationErrors); throw new Exception("This type can not be save" . $type); } } else { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); // debug($types); // debug($this->Project->Type->validationErrors); throw new Exception("This type can not be save" . $type); } } $files = $extractFolder->find('.*.html', true); if (empty($files)) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("The html documents can not be read"); } //$config = array('cdata' => 1, 'safe' => 1, 'keep_bad' => 6, 'no_depreca ted_attr' => 2, 'valid_xhtml' => 1, 'abs_url' => 1); $annotationsAcumulate = array(); foreach ($files as $file) { //sleep(5); $fileName = substr($file, 0, strrpos($file, '.')); $file = new File($extractFolder->pwd() . DS . $file); if ($file->readable()) { //$content = '<p>' . preg_replace("/\.\s*\n/", '.</p><p>', $file->read()) . '</p>'; $content = $file->read(); if (strlen($content) > 1) { $file->close(); $date = date("Y-m-d H:i:s"); $document = $this->Document->find('first', array('recursive' => -1, 'conditions' => array('external_id' => $fileName))); if (empty($document)) { $this->Document->create(); if (!$this->Document->save(array('Document' => array('external_id' => $fileName, 'title' => $fileName, 'html' => strip_tags($content), 'created' => $date), 'Project' => array('Project' => $this->Project->id)))) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("This Document can not be save:" . $fileName); } } else { $this->Document->id = $document['Document']['id']; if (!$this->Document->save(array('Document' => array('id' => $this->Document->id), 'Project' => array('Project' => $this->Project->id)))) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("This Document - Project can not be save"); } } $annotations = $this->getAnnotations($content, $typesMap, $this->Document->id); $tam = count($annotations); $modes = Configure::read('annotationsModes'); // $results = gzdeflate($content, 9); foreach ($users as $user) { foreach ($rounds as $round) { $this->AnnotatedDocument->create(); if (!$this->AnnotatedDocument->save(array('text_marked' => $content, 'document_id' => $this->Document->id, 'mode' => $modes["AUTOMATIC"], 'round_id' => $round, 'user_id' => $user))) { $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("This UsersRound can not be save:" . $fileName); } if (!empty($annotations)) { for ($index = 0; $index < $tam; $index++) { $annotations[$index]['user_id'] = $user; $annotations[$index]['round_id'] = $round; //// if ($round == $this->Round->id) { ////// $annotations[$index]['id'] = $maxAnnotation; ////// $maxAnnotation++; //// } } $annotationsAcumulate = array_merge($annotationsAcumulate, $annotations); } } } if (count($annotationsAcumulate) > 500) { if (!$this->Project->Type->Annotation->saveMany($annotationsAcumulate)) { //debug($annotations); debug($this->Project->Type->Annotation->validationErrors[0]); debug($fileName); $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("Annotations can not be save"); } $annotationsAcumulate = array(); } } } } if (count($annotationsAcumulate) > 0) { if (!$this->Annotation->saveMany($annotationsAcumulate)) { //debug($annotations); debug($this->Project->Type->Annotation->validationErrors[0]); debug($fileName); $extractFolder->delete(); $db->rollback(); $db->execute("UNLOCK TABLES"); throw new Exception("Annotations can not be save"); } } // throw new Exception; $extractFolder->delete(); $db->commit(); $db->execute("UNLOCK TABLES"); $this->Session->setFlash(__('Project has been created with documents imported'), 'success'); $this->redirect(array('action' => 'index')); } $extractFolder->delete(); } else { $this->Session->setFlash(__('Choose almost one user and one file')); } } $userConditions = array('group_id' => 2); $users = $this->Project->User->find('list', array('conditions' => $userConditions)); $this->set(compact('users', 'maxAnnotation', 'maxType')); }
/** * uploadFile method * Esta funcion es la encargada de gestionar los ficheros del * usurio. * En projectos add gestiona (crear,eliminar) * En projectos edit gestiona (crear) dado que los ficheros se eliminan de la BD * La direccion de la carpeta del usuario se establece con la variable * Configure::read('uploadFilesPath') y el mail del usuario (e mail es unico) * @require App::uses('Folder', 'Utility'); * @require App::uses('File', 'Utility'); * @require UploadHandler.php * @throws exception * @return void */ public function uploadFile($projectId = null) { App::import('Vendor', 'uploader', array('file' => 'jQuery-File-Upload' . DS . 'UploadHandler.php')); App::uses('Folder', 'Utility'); App::uses('File', 'Utility'); $uploadPath = Configure::read('uploadFilesPath'); $filesAllowed = Configure::read('filesAllowed'); $maxUploads = Configure::read('max_upload_files'); $maxFileSize = $this->filesize2bytes(Configure::read('max_file_size')); if ($maxUploads == 0) { $maxUploads = 99999; } $path = $uploadPath; //para que no moleste con los permisos //ini_set("display_errors", 0); //error_reporting(0); $this->autoRender = false; $email = $this->Auth->user('email'); //si no esta logueado if (!$this->Auth->loggedIn()) { print "One joker!!"; exit; } else { $folder = new Folder(); //si se puede crear la carpeta if ($folder->create($path)) { //chmod($path, 0600); // $path = $path . DS . $email; $path = $path . DS . tempnam(sys_get_temp_dir(), ''); if ($folder->create($path)) { //si no existe la carpeta se crea $folder = new Folder($path, true, 0700); //chmod($path, 0600); $absolutePath = $folder->path . DS; $options = array('script_url' => Router::url(array('controller' => 'ProjectResources', 'action' => 'uploadFile')), 'upload_dir' => $absolutePath, 'upload_url' => $this->webroot . $path . DS, 'user_dirs' => false, 'mkdir_mode' => 0700, 'param_name' => 'files', 'delete_type' => 'DELETE', 'access_control_allow_origin' => '*', 'access_control_allow_credentials' => false, 'access_control_allow_methods' => array('OPTIONS', 'HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'), 'access_control_allow_headers' => array('Content-Type', 'Content-Range', 'Content-Disposition'), 'download_via_php' => false, 'accept_file_types' => '/(\\.|\\/)' . $filesAllowed . '$/i', 'max_file_size' => $maxFileSize, 'min_file_size' => 1, 'max_number_of_files' => $maxUploads, 'max_width' => null, 'max_height' => null, 'min_width' => 1, 'min_height' => 1, 'discard_aborted_uploads' => true, 'orient_image' => false); $upload_handler = new UploadHandler($options, false); switch ($_SERVER['REQUEST_METHOD']) { case 'HEAD': case 'GET': throw new Exception(); $upload_handler->get(); break; case 'POST': case 'PUT': $group_id = $this->Session->read('group_id'); if ($group_id == 1) { $this->ProjectResource->Project->id = $projectId; if (!$this->ProjectResource->Project->exists()) { throw new NotFoundException(__('Invalid project ')); } $response = $upload_handler->post(); $packagedFiles = array(); $files = $folder->find('.*.' . $filesAllowed); if (!empty($files)) { foreach ($files as $file) { $file = new File($folder->pwd() . DS . $file, 644); if ($file->readable()) { // $md5 = $file->md5(); $name = $file->name(); $ext = $file->ext(); $content = $file->read(); $fileSize = $file->size(); $file->close(); $data = array('name' => $name, 'file' => $content, 'extension' => $ext, 'project_id' => $projectId, 'size' => $fileSize); $this->ProjectResource->create(); if ($this->ProjectResource->save($data)) { $packagedFiles[$name . "." . $ext] = $this->ProjectResource->id; } } } if (!empty($packagedFiles)) { $files = $response['files']; $size = sizeof($files); for ($index = 0; $index < $size; $index++) { $file = $files[$index]; if (isset($packagedFiles[$file->name])) { $file->url = Router::url(array('controller' => 'ProjectResources', 'action' => 'downloadFile', $packagedFiles[$file->name], $projectId)); $file->deleteUrl = Router::url(array('controller' => 'ProjectResources', 'action' => 'deleteFile', $packagedFiles[$file->name], $projectId)); } else { $file->error = "Could not be saved"; } } return $this->correctResponseJson($response); // $this->correctResponseJson(array("error" => "Could not be saved")); } } if (!$folder->delete()) { throw new Exception("Error deleting files"); } return $this->correctResponseJson($response); } break; case 'DELETE': break; default: // header('HTTP/1.0 405 Method Not Allowed'); } exit; } } else { throw new Exception(); } } }
private static function loadFile($fileName) { // this hack prevents loadFile from trying to load .jar resources if (empty($fileName) || strpos($fileName, 'res://') !== false) { return ''; } if (Config::get('debug')) { if (!File::exists($fileName)) { throw new Exception("JsLibrary file missing: {$fileName}"); } if (!is_file($fileName)) { throw new Exception("JsLibrary file is not a file: {$fileName}"); } if (!File::readable($fileName)) { throw new Exception("JsLibrary file not readable: {$fileName}"); } } if (!($content = @file_get_contents($fileName))) { throw new Exception("JsLibrary error reading file: {$fileName}"); } return $content; }
/** * Returns the average server load in the last minute. * * Note: This is currently not available on windows. * * @return Load average of the last minute */ public static function getLoad() { $serverload = -1; // Not implemented on Windows if(self::getOS() == self::WINDOWS) { return $serverload; } if (function_exists('sys_getloadavg')) { list($serverload) = sys_getloadavg(); } $proc = new File('/proc/loadavg'); if($serverload == -1 && $proc->exists() && $proc->readable()) { $load = $proc->read(File::READ_STRING, File::BINARY); if ($load !== false) { list($serverload) = explode(b" ", $load); $serverload = trim($serverload); } } if ($serverload == -1 && function_exists('exec')) { $load = @exec("uptime"); if(preg_match("~load averages?:\s?([\d\.]+)~i", $load, $serverload)) { list(,$serverload) = $serverload; } } if (empty($serverload)) { $serverload = -1; } return $serverload; }
/** * Setup for display or download the given file * * @param string $path Path to file * @param array $options Options * ### Options keys * - name: Alternate download name * - download: If `true` sets download header and forces file to be downloaded rather than displayed in browser * @return void * @throws NotFoundException */ public function file($path, $options = array()) { $options += array('name' => null, 'download' => null); if (!is_file($path)) { $path = APP . $path; } $file = new File($path); if (!$file->exists() || !$file->readable()) { if (Configure::read('debug')) { throw new NotFoundException(__d('cake_dev', 'The requested file %s was not found or not readable', $path)); } throw new NotFoundException(__d('cake', 'The requested file was not found')); } $extension = strtolower($file->ext()); $download = $options['download']; if ((!$extension || $this->type($extension) === false) && is_null($download)) { $download = true; } $fileSize = $file->size(); if ($download) { $agent = env('HTTP_USER_AGENT'); if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) { $contentType = 'application/octetstream'; } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) { $contentType = 'application/force-download'; } if (!empty($contentType)) { $this->type($contentType); } if (is_null($options['name'])) { $name = $file->name; } else { $name = $options['name']; } $this->download($name); $this->header('Accept-Ranges', 'bytes'); $httpRange = env('HTTP_RANGE'); if (isset($httpRange)) { list(, $range) = explode('=', $httpRange); $size = $fileSize - 1; $length = $fileSize - $range; $this->header(array('Content-Length' => $length, 'Content-Range' => 'bytes ' . $range . $size . '/' . $fileSize)); $this->statusCode(206); $file->open('rb', true); $file->offset($range); } else { $this->header('Content-Length', $fileSize); } } else { $this->header('Content-Length', $fileSize); } $this->_clearBuffer(); $this->_file = $file; }
/** * Handles existent but not readable files * * @access protected * @return mixed */ function _handleNotReadable() { if (!$this->__File->readable() && $this->__File->exists()) { $this->out('File exists but is not readable'); return true; } }
/** * testReadable method * * @return void */ public function testReadable() { $someFile = new File(TMP . 'some_file.txt', false); $this->assertTrue($someFile->open()); $this->assertTrue($someFile->readable()); $someFile->close(); $someFile->delete(); }
public function importTsv() { App::uses('Folder', 'Utility'); App::uses('File', 'Utility'); if ($this->request->is('post') || $this->request->is('put')) { if (isset($this->request->data['Document']['File']) && $this->request->data['Document']['File']['size'] > 0) { $fileName = $this->request->data['Document']['File']['name']; $extension = pathinfo($fileName, PATHINFO_EXTENSION); $checkextension = array("tsv", "txt"); if (in_array($extension, $checkextension)) { //your code goes here. $db = $this->Document->getDataSource(); $timeLimit = Configure::read('scriptTimeLimit'); set_time_limit($timeLimit); $db->begin(); $project_id = $this->request->data['Project']['Project']; $file = new File($this->request->data['Document']['File']['tmp_name']); if ($file->readable()) { $content = $file->read(); $file->close(); $lines = explode("\n", $content); if (!empty($lines)) { $size = count($lines); for ($index = 0; $index < $size; $index++) { if (strlen($lines[$index]) > 1) { $section = explode("\t", $lines[$index]); if (sizeof($section) == 3) { $external_id = strtoupper($section[0]); $title = $section[1]; $corpus = $section[2]; $html = "<div class='corpusSections'><h3 class='title'>" . $title . "</h3><div class='abstract'>" . $corpus . "</div></div>"; $data = array('title' => $title, 'external_id' => $external_id, 'html' => $html); if ($project_id != 0) { $data = array('Project' => array('id' => $project_id), 'Document' => $data); } $conditions = array('external_id' => $external_id); if (!$this->Document->hasAny($conditions)) { $this->Document->create(); if (!$this->Document->save($data)) { $db->rollback(); $index++; throw new Exception("Line " . $index . " could not be save"); } } else { $this->Document->DocumentsProject->create(); $document = $this->Document->find('first', array('recursive' => -1, 'fields' => 'id', 'conditions' => $conditions)); $document_id = $document['Document']['id']; $data = array('project_id' => $project_id, 'document_id' => $document_id); if (!$this->Document->DocumentsProject->save($data)) { $db->rollback(); $index++; throw new Exception("Line " . $index . " could not be save"); } } } else { $db->rollback(); $index++; throw new Exception("Line " . $index . " is incorrect"); } } } $this->Session->setFlash(__('All documents have been saved'), 'success'); $db->commit(); if ($project_id != 0) { $this->redirect(array('controller' => 'projects', 'action' => 'view', $project_id)); } else { $this->redirect(array('controller' => 'documents', 'action' => 'index')); } } } } } $this->Session->setFlash(__('The document could not be processed. Incorrect format or file empty (try txt or tsv file).')); } $projects = $this->Document->Project->find('list'); $this->set(compact('projects')); }