/**
  * Copies a file or directory.
  *
  * This method must work recursively and delete the destination
  * if it exists
  *
  * @param string $source
  * @param string $destination
  * @return void
  */
 public function copy($source, $destination)
 {
     if (Filesystem::is_file($source)) {
         Filesystem::copy($source, $destination);
     } else {
         Filesystem::mkdir($destination);
         $dh = Filesystem::opendir($source);
         if (is_resource($dh)) {
             while (($subnode = readdir($dh)) !== false) {
                 if ($subnode == '.' || $subnode == '..') {
                     continue;
                 }
                 $this->copy($source . '/' . $subnode, $destination . '/' . $subnode);
             }
         }
     }
     list($destinationDir, ) = \Sabre_DAV_URLUtil::splitPath($destination);
     $this->markDirty($destinationDir);
 }
Example #2
0
 public function getListing($FOLDER, $showdel)
 {
     // Get the listing from the database
     $requery = false;
     $uid = \OCP\User::getUser();
     $query = \OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name");
     $results = $query->execute(array($uid))->fetchAll();
     $results2 = $results;
     if ($results) {
         foreach ($results as $result) {
             foreach ($results2 as $result2) {
                 if ($result['id'] != $result2['id'] && $result['name'] == $result2['name'] && $result['grouping'] == $result2['grouping']) {
                     // We have a duplicate that should not exist. Need to remove the offending record first
                     $delid = -1;
                     if ($result['mtime'] == $result2['mtime']) {
                         // If the mtime's match, delete the oldest ID.
                         $delid = $result['id'];
                         if ($result['id'] > $result2['id']) {
                             $delid = $result2['id'];
                         }
                     } elseif ($result['mtime'] > $result2['mtime']) {
                         // Again, delete the oldest
                         $delid = $result2['id'];
                     } elseif ($result['mtime'] < $result2['mtime']) {
                         // The only thing left is if result is older
                         $delid = $result['id'];
                     }
                     if ($delid != -1) {
                         $delquery = \OCP\DB::prepare("DELETE FROM *PREFIX*ownnote WHERE id=?");
                         $delquery->execute(array($delid));
                         $requery = true;
                     }
                 }
             }
         }
     }
     if ($requery) {
         $query = \OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name");
         $results = $query->execute(array($uid))->fetchAll();
         $requery = false;
     }
     // Tests to add a bunch of notes
     //$now = new DateTime();
     //for ($x = 0; $x < 199; $x++) {
     //saveNote('', "Test ".$x, '', '', $now->getTimestamp());
     //}
     $farray = array();
     if ($FOLDER != '') {
         // Create the folder if it doesn't exist
         if (!\OC\Files\Filesystem::is_dir($FOLDER)) {
             if (!\OC\Files\Filesystem::mkdir($FOLDER)) {
                 echo "ERROR: Could not create ownNote directory.";
                 exit;
             }
         }
         // Synchronize files to the database
         $filearr = array();
         if ($listing = \OC\Files\Filesystem::opendir($FOLDER)) {
             if (!$listing) {
                 echo "ERROR: Error listing directory.";
                 exit;
             }
             while (($file = readdir($listing)) !== false) {
                 $tmpfile = $file;
                 if ($tmpfile == "." || $tmpfile == "..") {
                     continue;
                 }
                 if (!$this->endsWith($tmpfile, ".htm") && !$this->endsWith($tmpfile, ".html")) {
                     continue;
                 }
                 if ($info = \OC\Files\Filesystem::getFileInfo($FOLDER . "/" . $tmpfile)) {
                     // Check for EVERNOTE but wait to rename them to get around:
                     // https://github.com/owncloud/core/issues/16202
                     if ($this->endsWith($tmpfile, ".html")) {
                         $this->checkEvernote($FOLDER, $tmpfile);
                     }
                     // Separate the name and group name
                     $name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $tmpfile);
                     $group = "";
                     if (substr($name, 0, 1) == "[") {
                         $end = strpos($name, ']');
                         $group = substr($name, 1, $end - 1);
                         $name = substr($name, $end + 1, strlen($name) - $end + 1);
                         $name = trim($name);
                     }
                     // Set array for later checking
                     $filearr[] = $tmpfile;
                     // Check to see if the file is in the DB
                     $fileindb = false;
                     if ($results) {
                         foreach ($results as $result) {
                             if ($result['deleted'] == 0) {
                                 if ($name == $result['name'] && $group == $result['grouping']) {
                                     $fileindb = true;
                                     // If it is in the DB, check if the filesystem file is newer than the DB
                                     if ($result['mtime'] < $info['mtime']) {
                                         // File is newer, this could happen if a user updates a file
                                         $html = "";
                                         $html = \OC\Files\Filesystem::file_get_contents($FOLDER . "/" . $tmpfile);
                                         $this->saveNote('', $result['name'], $result['grouping'], $html, $info['mtime']);
                                         $requery = true;
                                     }
                                 }
                             }
                         }
                     }
                     if (!$fileindb) {
                         // If it's not in the DB, add it.
                         $html = "";
                         if ($html = \OC\Files\Filesystem::file_get_contents($FOLDER . "/" . $tmpfile)) {
                         } else {
                             $html = "";
                         }
                         $this->saveNote('', $name, $group, $html, $info['mtime']);
                         $requery = true;
                     }
                     // We moved the rename down here to overcome the OC issue
                     if ($this->endsWith($tmpfile, ".html")) {
                         $tmpfile = substr($tmpfile, 0, -1);
                         if (!\OC\Files\Filesystem::file_exists($FOLDER . "/" . $tmpfile)) {
                             \OC\Files\Filesystem::rename($FOLDER . "/" . $file, $FOLDER . "/" . $tmpfile);
                         }
                     }
                 }
             }
         }
         if ($requery) {
             $query = \OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name");
             $results = $query->execute(array($uid))->fetchAll();
         }
         // Now also make sure the files exist, they may not if the user switched folders in admin.
         if ($results) {
             foreach ($results as $result) {
                 if ($result['deleted'] == 0) {
                     $tmpfile = $result['name'] . ".htm";
                     if ($result['grouping'] != '') {
                         $tmpfile = '[' . $result['grouping'] . '] ' . $result['name'] . '.htm';
                     }
                     $filefound = false;
                     foreach ($filearr as $f) {
                         if ($f == $tmpfile) {
                             $filefound = true;
                             break;
                         }
                     }
                     if (!$filefound) {
                         $content = $this->editNote($result['name'], $result['grouping']);
                         $this->saveNote($FOLDER, $result['name'], $result['grouping'], $content, 0);
                     }
                 }
             }
         }
     }
     // Now loop through and return the listing
     if ($results) {
         $count = 0;
         $now = new DateTime();
         $filetime = new DateTime();
         $l = \OCP\Util::getL10N('ownnote');
         foreach ($results as $result) {
             if ($result['deleted'] == 0 || $showdel == true) {
                 $filetime->setTimestamp($result['mtime']);
                 $timestring = $this->getTimeString($filetime, $now, $l);
                 $f = array();
                 $f['id'] = $result['id'];
                 $f['name'] = $result['name'];
                 $f['group'] = $result['grouping'];
                 $f['timestring'] = $timestring;
                 $f['mtime'] = $result['mtime'];
                 $f['timediff'] = $now->getTimestamp() - $result['mtime'];
                 $f['deleted'] = $result['deleted'];
                 $farray[$count] = $f;
                 $count++;
             }
         }
     }
     return $farray;
 }
 /**
  * @deprecated OC_Filesystem is replaced by \OC\Files\Filesystem
  */
 public static function opendir($path)
 {
     return \OC\Files\Filesystem::opendir($path);
 }
Example #4
0
function renameGroup($FOLDER, $originalgroupname, $editgroupname) {
	$ret = "";
	if ($listing = \OC\Files\Filesystem::opendir($FOLDER)) {
		if (!$listing) {
			$ret .= "ERROR: Error listing directory.";
		} else {
			while (($file = readdir($listing)) !== false) {
				if (substr($file,0,1) == "[") {
					$group = "";
					$end = strpos($file, ']');
					$group = substr($file, 1, $end-1);
					if ($group == $originalgroupname) {
						$filename = substr($file, $end+1, strlen($file)-$end+1);
						$filename = trim($filename);
						$filename = "[".$editgroupname."] ".$filename;
						if (!\OC\Files\Filesystem::file_exists($FOLDER."/".$filename)) {
							if (\OC\Files\Filesystem::rename($FOLDER."/".$file, $FOLDER."/".$filename)) {
								$ret .= "SUCCESS";
							} else {
								$ret .= "FAIL";
							}
						} else {
							$ret .= "FAIL";
						}
					}
				}
			}
		}
	}
	return $ret;
}