/** * This function is used to uplodad file in /uploads/YEAR/MONTH directory * and to create Content record contextually. * * * @param type $inputfile * @param string $destfile * @return boolean */ public function upload($inputfile, $destfile = null) { if (strlen(trim($inputfile['name'])) === 0) { return FALSE; } $filename = strtolower($inputfile['name']); $sourcefile = $inputfile['tmp_name']; $file = new \Cake\Filesystem\File($sourcefile, true, 0775); $CONTENT_YEAR = date('Y'); $CONTENT_MONTH = date('m'); $UPLOAD_DIR = $this->path; $folder_dest = new Folder($UPLOAD_DIR); if (!$folder_dest->inCakePath($folder_dest->pwd() . DS . $CONTENT_YEAR)) { $folder_dest->create($folder_dest->pwd() . DS . $CONTENT_YEAR); } $folder_dest->cd($CONTENT_YEAR); if (!$folder_dest->inCakePath($folder_dest->pwd() . DS . $CONTENT_MONTH)) { $folder_dest->create($folder_dest->pwd() . DS . $CONTENT_MONTH); } $folder_dest->cd($CONTENT_MONTH); $path = DS . $CONTENT_YEAR . DS . $CONTENT_MONTH . DS; $permittedFilename = $this->getPermittedFilename($UPLOAD_DIR . $path, $filename); $destfile = $folder_dest->pwd() . DS . $permittedFilename; if ($file->copy($destfile, true)) { return $path . $permittedFilename; } else { return FALSE; } }
/** * Returns the full path of the file. * * @return string Full path to the file */ public function pwd() { if ($this->path === null) { $dir = $this->Folder->pwd(); if (is_dir($dir)) { $this->path = $this->Folder->slashTerm($dir) . $this->name; } } return $this->path; }
/** * testAddTestDirectoryRecursiveWithNonPhp * * @return void */ public function testAddTestDirectoryRecursiveWithNonPhp() { $this->skipIf(!is_writable(TMP), 'Cant addTestDirectoryRecursiveWithNonPhp unless the tmp folder is writable.'); $Folder = new Folder(TMP . 'MyTestFolder', true, 0777); touch($Folder->path . DS . 'BackupTest.php~'); touch($Folder->path . DS . 'SomeNotesTest.txt'); touch($Folder->path . DS . 'NotHiddenTest.php'); $suite = $this->getMock('Cake\\TestSuite\\TestSuite', ['addTestFile']); $suite->expects($this->exactly(1))->method('addTestFile'); $suite->addTestDirectoryRecursive($Folder->pwd()); $Folder->delete(); }
/** * main method * * @param string $tempDir an other directory to clear of all folders and files, if desired * @return void */ public function main($tempDir = null) { if (empty($tempDir)) { $tempDir = Configure::read('Attachments.tmpUploadsPath'); } if (!Folder::isAbsolute($tempDir)) { $this->out('The path must be absolute, "' . $tempDir . '" given.'); exit; } $Folder = new Folder(); if ($Folder->cd($tempDir) === false) { $this->out('Path "' . $tempDir . '" doesn\'t seem to exist.'); exit; } $dir = new Folder($tempDir); $folders = $dir->read(); $files = $dir->findRecursive(); $deletedFiles = 0; $deletedFolders = 0; $this->out('Found ' . count($folders[0]) . ' folders and ' . count($files) . ' files'); foreach ($files as $filePath) { $file = new File($filePath); // only delete if last change is longer than 24 hours ago if ($file->lastChange() < time() - 24 * 60 * 60 && $file->delete()) { $deletedFiles++; } $file->close(); } foreach ($folders[0] as $folderName) { $folder = new Folder($dir->pwd() . $folderName); // only delete if folder is empty if ($folder->dirsize() === 0 && $folder->delete()) { $deletedFolders++; } } $this->out('Deleted ' . $deletedFolders . ' folders and ' . $deletedFiles . ' files.'); }
/** * Dump static files * * @param DOMDocument $domDocument * * @throws \Exception */ protected function dumpStaticFiles(DOMDocument $domDocument) { $xpath = new DOMXPath($domDocument); $assetsNodeList = $xpath->query('/assetic/static/files/file'); $validFlags = ['GLOB_MARK' => GLOB_MARK, 'GLOB_NOSORT' => GLOB_NOSORT, 'GLOB_NOCHECK' => GLOB_NOCHECK, 'GLOB_NOESCAPE' => GLOB_NOESCAPE, 'GLOB_BRACE' => GLOB_BRACE, 'GLOB_ONLYDIR' => GLOB_ONLYDIR, 'GLOB_ERR' => GLOB_ERR]; /** @var $assetNode DOMElement */ foreach ($assetsNodeList as $assetNode) { $source = strtr($assetNode->getElementsByTagName('source')->item(0)->nodeValue, $this->paths); $destination = strtr($assetNode->getElementsByTagName('destination')->item(0)->nodeValue, $this->paths); $flag = $assetNode->getElementsByTagName('source')->item(0)->getAttribute('flag'); if (empty($flag)) { $flag = null; } else { if (!array_key_exists($flag, $validFlags)) { throw new \Exception(sprintf('This flag "%s" not valid.', $flag)); } $flag = $validFlags[$flag]; } $des = new Folder(WWW_ROOT . $destination, true, 0777); $allFiles = glob($source, $flag); foreach ($allFiles as $src) { if (is_dir($src)) { $srcFolder = new Folder($src); $srcFolder->copy($des->pwd()); } else { $srcFile = new File($src); $srcFile->copy($des->path . DS . $srcFile->name); } $this->out($src . ' <info> >>> </info> ' . WWW_ROOT . $destination); } } }
public function save($name, $dados = array()) { $dir = new Folder(ROOT . DS . 'config' . DS . 'schema' . DS, true, 0777); $file = new File($dir->pwd() . $name); $file->write(implode("\n", $dados)); $file->close(); }
/** * Process the tasks when they need to run * * @access private * @return void */ private function runjobs() { $dir = new Folder(TMP); // set processing flag so function takes place only once at any given time $processing = count($dir->find('\\.scheduler_running_flag')); $processingFlag = new File($dir->slashTerm($dir->pwd()) . '.scheduler_running_flag'); if ($processing && time() - $processingFlag->lastChange() < $this->processingTimeout) { $this->out("Scheduler already running! Exiting."); return false; } else { $processingFlag->delete(); $processingFlag->create(); } if (!$this->storePath) { $this->storePath = TMP; } // look for a store of the previous run $store = ""; $storeFilePath = $this->storePath . $this->storeFile; if (file_exists($storeFilePath)) { $store = file_get_contents($storeFilePath); } $this->out('Reading from: ' . $storeFilePath); // build or rebuild the store if ($store != '') { $store = json_decode($store, true); } else { $store = $this->schedule; } // run the jobs that need to be run, record the time foreach ($this->schedule as $name => $job) { $now = new DateTime(); $task = $job['task']; $action = $job['action']; // if the job has never been run before, create it if (!isset($store[$name])) { $store[$name] = $job; } // figure out the last run date $tmptime = $store[$name]['lastRun']; if ($tmptime == null) { $tmptime = new DateTime("1969-01-01 00:00:00"); } elseif (is_array($tmptime)) { $tmptime = new DateTime($tmptime['date'], new DateTimeZone($tmptime['timezone'])); } elseif (is_string($tmptime)) { $tmptime = new DateTime($tmptime); } // determine the next run time based on the last if (substr($job['interval'], 0, 1) === 'P') { $tmptime->add(new DateInterval($job['interval'])); // "P10DT4H" http://www.php.net/manual/en/class.dateinterval.php } else { $tmptime->modify($job['interval']); // "next day 10:30" http://www.php.net/manual/en/datetime.formats.relative.php } // is it time to run? has it never been run before? if ($tmptime <= $now) { $this->hr(); $this->out("Running {$name}"); $this->hr(); if (!isset($this->{$task})) { $this->{$task} = $this->Tasks->load($task); // load models if they aren't already // foreach ($this->$task->uses as $mk => $mv) { // if (!isset($this->$task->$mv)) { // App::uses('AppModel', 'Model'); // App::uses($mv, 'Model'); // $this->$task->$mv = new $mv(); // } // } } // grab the entire schedule record incase it was updated.. $store[$name] = $this->schedule[$name]; // execute the task and store the result $store[$name]['lastResult'] = call_user_func_array(array($this->{$task}, $action), $job['args']); // assign it the current time $now = new DateTime(); $store[$name]['lastRun'] = $now->format('Y-m-d H:i:s'); } } // write the store back to the file file_put_contents($this->storePath . $this->storeFile, json_encode($store)); // remove processing flag $processingFlag->delete(); }
public function sync($id = null) { $folder = new Folder(); $folders_to_copy = ['f1' => ['dev' => '../src', 'prod' => '../../production/src'], 'f2' => ['dev' => '../plugins', 'prod' => '../../production/plugins'], 'f3' => ['dev' => '../webroot', 'prod' => '../../production/webroot'], 'f4' => ['dev' => '../config', 'prod' => '../../production/config']]; $pass = 0; $fail = 0; foreach ($folders_to_copy as $ftc) { $p = new Folder($ftc['prod']); $d = new Folder($ftc['dev']); if ($folder->copy(['to' => $p->pwd(), 'from' => $d->pwd(), 'scheme' => Folder::MERGE])) { $pass++; } else { $fail++; } } if ($fail == 0) { $this->Flash->success(__('Production updated successfully.')); } else { $errors = ''; foreach ($folder->errors() as $error) { $errors .= ' : ' . $error; } $this->Flash->error(__('Unable to update {0} folders on production. Errors' . $errors, $fail)); } return $this->redirect($this->referer()); }
/** * testDirSize method * * @return void */ public function testDirSize() { $path = TMP . 'tests' . DS; $Folder = new Folder($path . 'config_non_existent', true); $this->assertEquals(0, $Folder->dirSize()); $File = new File($Folder->pwd() . DS . 'my.php', true, 0777); $File->create(); $File->write('something here'); $File->close(); $this->assertEquals(14, $Folder->dirSize()); }
/** * _removeExpiredLock * * @return void */ protected function _removeExpiredLock() { $dir = new Folder(CONFIG . $this->_config['lockout']['file_path'], true); $files = $dir->find(); foreach ($files as $fileName) { $file = new File($dir->pwd() . DS . $fileName); $lastChange = Time::createFromTimestamp($file->lastChange()); if ($lastChange->wasWithinLast($this->_config['lockout']['expires'])) { continue; } $file->delete(); } }
private function deleteLetter($fileName) { $deleted = false; $filePath = WWW_ROOT . 'letters'; try { $dir = new Folder($filePath); $file = new File($dir->pwd() . DS . $fileName); if ($file->delete()) { $deleted = true; } } catch (Exception $e) { $this->Flash->error(__('Ocurrió un error al tratar de borrar el archivo')); return $this->redirect(['action' => 'index']); } return $deleted; }
public function delete($id = null) { if ($this->request->session()->read('Auth.User.role') == 'rep' || $this->request->session()->read('Auth.User.role') == 'admin') { try { $invoice = $this->Invoices->get($id); } catch (RecordNotFoundException $record) { $this->Flash->error(__('La información que está tratando de recuperar no existe en la base de datos. Verifique e intente de nuevo')); return $this->redirect(['controller' => 'Associations', 'action' => 'init']); } $deleted = false; $filePath = WWW_ROOT . '/img/invoices'; $dir = new Folder($filePath); $file = new File($dir->pwd() . DS . $invoice['image_name']); if ($file->delete()) { $deleted = true; } if (!$this->Invoices->delete($invoice)) { $response = "0"; } else { if ($this->request->session()->read('Auth.User.role') == 'rep') { return $this->redirect(['action' => 'modify']); } else { return $this->redirect(['action' => 'admin-modify']); } } } else { return $this->redirect(['controller' => 'pages', 'action' => 'home']); } }
/** * Verify that the order using name is correct. */ public function testSortByName() { $Folder = new Folder(TMP . 'tests', true); $fileA = new File($Folder->pwd() . DS . 'a.txt'); $fileA->create(); $fileC = new File($Folder->pwd() . DS . 'c.txt'); $fileC->create(); sleep(1); $fileB = new File($Folder->pwd() . DS . 'b.txt'); $fileB->create(); $results = $Folder->find('.*', Folder::SORT_NAME); $this->assertSame(['a.txt', 'b.txt', 'c.txt'], $results); }