| @@ -33,12 +33,12 @@ | ||
| 33 | 33 | */ | 
| 34 | 34 |  interface ICallbackResponse { | 
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * Outputs the content that should be printed | |
| 38 | - * | |
| 39 | - * @param IOutput $output a small wrapper that handles output | |
| 40 | - * @since 8.1.0 | |
| 41 | - */ | |
| 42 | - public function callback(IOutput $output); | |
| 36 | + /** | |
| 37 | + * Outputs the content that should be printed | |
| 38 | + * | |
| 39 | + * @param IOutput $output a small wrapper that handles output | |
| 40 | + * @since 8.1.0 | |
| 41 | + */ | |
| 42 | + public function callback(IOutput $output); | |
| 43 | 43 | |
| 44 | 44 | } | 
| @@ -52,119 +52,119 @@ | ||
| 52 | 52 | */ | 
| 53 | 53 |  interface IManager { | 
| 54 | 54 | |
| 55 | - /** | |
| 56 | - * This function is used to search and find contacts within the users address books. | |
| 57 | - * In case $pattern is empty all contacts will be returned. | |
| 58 | - * | |
| 59 | - * Example: | |
| 60 | - * Following function shows how to search for contacts for the name and the email address. | |
| 61 | - * | |
| 62 | -	 *		public static function getMatchingRecipient($term) { | |
| 63 | - * $cm = \OC::$server->getContactsManager(); | |
| 64 | - * // The API is not active -> nothing to do | |
| 65 | -	 *			if (!$cm->isEnabled()) { | |
| 66 | - * return array(); | |
| 67 | - * } | |
| 68 | - * | |
| 69 | -	 *			$result = $cm->search($term, array('FN', 'EMAIL')); | |
| 70 | - * $receivers = array(); | |
| 71 | -	 *			foreach ($result as $r) { | |
| 72 | - * $id = $r['id']; | |
| 73 | - * $fn = $r['FN']; | |
| 74 | - * $email = $r['EMAIL']; | |
| 75 | -	 *				if (!is_array($email)) { | |
| 76 | - * $email = array($email); | |
| 77 | - * } | |
| 78 | - * | |
| 79 | - * // loop through all email addresses of this contact | |
| 80 | -	 *				foreach ($email as $e) { | |
| 81 | - * $displayName = $fn . " <$e>"; | |
| 82 | - * $receivers[] = array( | |
| 83 | - * 'id' => $id, | |
| 84 | - * 'label' => $displayName, | |
| 85 | - * 'value' => $displayName); | |
| 86 | - * } | |
| 87 | - * } | |
| 88 | - * | |
| 89 | - * return $receivers; | |
| 90 | - * } | |
| 91 | - * | |
| 92 | - * | |
| 93 | - * @param string $pattern which should match within the $searchProperties | |
| 94 | - * @param array $searchProperties defines the properties within the query pattern should match | |
| 95 | - * @param array $options - for future use. One should always have options! | |
| 96 | - * @return array an array of contacts which are arrays of key-value-pairs | |
| 97 | - * @since 6.0.0 | |
| 98 | - */ | |
| 99 | - public function search($pattern, $searchProperties = array(), $options = array()); | |
| 55 | + /** | |
| 56 | + * This function is used to search and find contacts within the users address books. | |
| 57 | + * In case $pattern is empty all contacts will be returned. | |
| 58 | + * | |
| 59 | + * Example: | |
| 60 | + * Following function shows how to search for contacts for the name and the email address. | |
| 61 | + * | |
| 62 | +     *		public static function getMatchingRecipient($term) { | |
| 63 | + * $cm = \OC::$server->getContactsManager(); | |
| 64 | + * // The API is not active -> nothing to do | |
| 65 | +     *			if (!$cm->isEnabled()) { | |
| 66 | + * return array(); | |
| 67 | + * } | |
| 68 | + * | |
| 69 | +     *			$result = $cm->search($term, array('FN', 'EMAIL')); | |
| 70 | + * $receivers = array(); | |
| 71 | +     *			foreach ($result as $r) { | |
| 72 | + * $id = $r['id']; | |
| 73 | + * $fn = $r['FN']; | |
| 74 | + * $email = $r['EMAIL']; | |
| 75 | +     *				if (!is_array($email)) { | |
| 76 | + * $email = array($email); | |
| 77 | + * } | |
| 78 | + * | |
| 79 | + * // loop through all email addresses of this contact | |
| 80 | +     *				foreach ($email as $e) { | |
| 81 | + * $displayName = $fn . " <$e>"; | |
| 82 | + * $receivers[] = array( | |
| 83 | + * 'id' => $id, | |
| 84 | + * 'label' => $displayName, | |
| 85 | + * 'value' => $displayName); | |
| 86 | + * } | |
| 87 | + * } | |
| 88 | + * | |
| 89 | + * return $receivers; | |
| 90 | + * } | |
| 91 | + * | |
| 92 | + * | |
| 93 | + * @param string $pattern which should match within the $searchProperties | |
| 94 | + * @param array $searchProperties defines the properties within the query pattern should match | |
| 95 | + * @param array $options - for future use. One should always have options! | |
| 96 | + * @return array an array of contacts which are arrays of key-value-pairs | |
| 97 | + * @since 6.0.0 | |
| 98 | + */ | |
| 99 | + public function search($pattern, $searchProperties = array(), $options = array()); | |
| 100 | 100 | |
| 101 | - /** | |
| 102 | - * This function can be used to delete the contact identified by the given id | |
| 103 | - * | |
| 104 | - * @param object $id the unique identifier to a contact | |
| 105 | - * @param string $address_book_key identifier of the address book in which the contact shall be deleted | |
| 106 | - * @return bool successful or not | |
| 107 | - * @since 6.0.0 | |
| 108 | - */ | |
| 109 | - public function delete($id, $address_book_key); | |
| 101 | + /** | |
| 102 | + * This function can be used to delete the contact identified by the given id | |
| 103 | + * | |
| 104 | + * @param object $id the unique identifier to a contact | |
| 105 | + * @param string $address_book_key identifier of the address book in which the contact shall be deleted | |
| 106 | + * @return bool successful or not | |
| 107 | + * @since 6.0.0 | |
| 108 | + */ | |
| 109 | + public function delete($id, $address_book_key); | |
| 110 | 110 | |
| 111 | - /** | |
| 112 | - * This function is used to create a new contact if 'id' is not given or not present. | |
| 113 | - * Otherwise the contact will be updated by replacing the entire data set. | |
| 114 | - * | |
| 115 | - * @param array $properties this array if key-value-pairs defines a contact | |
| 116 | - * @param string $address_book_key identifier of the address book in which the contact shall be created or updated | |
| 117 | - * @return array an array representing the contact just created or updated | |
| 118 | - * @since 6.0.0 | |
| 119 | - */ | |
| 120 | - public function createOrUpdate($properties, $address_book_key); | |
| 111 | + /** | |
| 112 | + * This function is used to create a new contact if 'id' is not given or not present. | |
| 113 | + * Otherwise the contact will be updated by replacing the entire data set. | |
| 114 | + * | |
| 115 | + * @param array $properties this array if key-value-pairs defines a contact | |
| 116 | + * @param string $address_book_key identifier of the address book in which the contact shall be created or updated | |
| 117 | + * @return array an array representing the contact just created or updated | |
| 118 | + * @since 6.0.0 | |
| 119 | + */ | |
| 120 | + public function createOrUpdate($properties, $address_book_key); | |
| 121 | 121 | |
| 122 | - /** | |
| 123 | - * Check if contacts are available (e.g. contacts app enabled) | |
| 124 | - * | |
| 125 | - * @return bool true if enabled, false if not | |
| 126 | - * @since 6.0.0 | |
| 127 | - */ | |
| 128 | - public function isEnabled(); | |
| 122 | + /** | |
| 123 | + * Check if contacts are available (e.g. contacts app enabled) | |
| 124 | + * | |
| 125 | + * @return bool true if enabled, false if not | |
| 126 | + * @since 6.0.0 | |
| 127 | + */ | |
| 128 | + public function isEnabled(); | |
| 129 | 129 | |
| 130 | - /** | |
| 131 | - * Registers an address book | |
| 132 | - * | |
| 133 | - * @param \OCP\IAddressBook $address_book | |
| 134 | - * @return void | |
| 135 | - * @since 6.0.0 | |
| 136 | - */ | |
| 137 | - public function registerAddressBook(\OCP\IAddressBook $address_book); | |
| 130 | + /** | |
| 131 | + * Registers an address book | |
| 132 | + * | |
| 133 | + * @param \OCP\IAddressBook $address_book | |
| 134 | + * @return void | |
| 135 | + * @since 6.0.0 | |
| 136 | + */ | |
| 137 | + public function registerAddressBook(\OCP\IAddressBook $address_book); | |
| 138 | 138 | |
| 139 | - /** | |
| 140 | - * Unregisters an address book | |
| 141 | - * | |
| 142 | - * @param \OCP\IAddressBook $address_book | |
| 143 | - * @return void | |
| 144 | - * @since 6.0.0 | |
| 145 | - */ | |
| 146 | - public function unregisterAddressBook(\OCP\IAddressBook $address_book); | |
| 139 | + /** | |
| 140 | + * Unregisters an address book | |
| 141 | + * | |
| 142 | + * @param \OCP\IAddressBook $address_book | |
| 143 | + * @return void | |
| 144 | + * @since 6.0.0 | |
| 145 | + */ | |
| 146 | + public function unregisterAddressBook(\OCP\IAddressBook $address_book); | |
| 147 | 147 | |
| 148 | - /** | |
| 149 | - * In order to improve lazy loading a closure can be registered which will be called in case | |
| 150 | - * address books are actually requested | |
| 151 | - * | |
| 152 | - * @param \Closure $callable | |
| 153 | - * @return void | |
| 154 | - * @since 6.0.0 | |
| 155 | - */ | |
| 156 | - public function register(\Closure $callable); | |
| 148 | + /** | |
| 149 | + * In order to improve lazy loading a closure can be registered which will be called in case | |
| 150 | + * address books are actually requested | |
| 151 | + * | |
| 152 | + * @param \Closure $callable | |
| 153 | + * @return void | |
| 154 | + * @since 6.0.0 | |
| 155 | + */ | |
| 156 | + public function register(\Closure $callable); | |
| 157 | 157 | |
| 158 | - /** | |
| 159 | - * @return array | |
| 160 | - * @since 6.0.0 | |
| 161 | - */ | |
| 162 | - public function getAddressBooks(); | |
| 158 | + /** | |
| 159 | + * @return array | |
| 160 | + * @since 6.0.0 | |
| 161 | + */ | |
| 162 | + public function getAddressBooks(); | |
| 163 | 163 | |
| 164 | - /** | |
| 165 | - * removes all registered address book instances | |
| 166 | - * @return void | |
| 167 | - * @since 6.0.0 | |
| 168 | - */ | |
| 169 | - public function clear(); | |
| 164 | + /** | |
| 165 | + * removes all registered address book instances | |
| 166 | + * @return void | |
| 167 | + * @since 6.0.0 | |
| 168 | + */ | |
| 169 | + public function clear(); | |
| 170 | 170 | } | 
| @@ -33,111 +33,111 @@ | ||
| 33 | 33 | namespace OC\Archive; | 
| 34 | 34 | |
| 35 | 35 |  abstract class Archive { | 
| 36 | - /** | |
| 37 | - * @param $source | |
| 38 | - */ | |
| 39 | - public abstract function __construct($source); | |
| 40 | - /** | |
| 41 | - * add an empty folder to the archive | |
| 42 | - * @param string $path | |
| 43 | - * @return bool | |
| 44 | - */ | |
| 45 | - public abstract function addFolder($path); | |
| 46 | - /** | |
| 47 | - * add a file to the archive | |
| 48 | - * @param string $path | |
| 49 | - * @param string $source either a local file or string data | |
| 50 | - * @return bool | |
| 51 | - */ | |
| 52 | - public abstract function addFile($path, $source=''); | |
| 53 | - /** | |
| 54 | - * rename a file or folder in the archive | |
| 55 | - * @param string $source | |
| 56 | - * @param string $dest | |
| 57 | - * @return bool | |
| 58 | - */ | |
| 59 | - public abstract function rename($source, $dest); | |
| 60 | - /** | |
| 61 | - * get the uncompressed size of a file in the archive | |
| 62 | - * @param string $path | |
| 63 | - * @return int | |
| 64 | - */ | |
| 65 | - public abstract function filesize($path); | |
| 66 | - /** | |
| 67 | - * get the last modified time of a file in the archive | |
| 68 | - * @param string $path | |
| 69 | - * @return int | |
| 70 | - */ | |
| 71 | - public abstract function mtime($path); | |
| 72 | - /** | |
| 73 | - * get the files in a folder | |
| 74 | - * @param string $path | |
| 75 | - * @return array | |
| 76 | - */ | |
| 77 | - public abstract function getFolder($path); | |
| 78 | - /** | |
| 79 | - * get all files in the archive | |
| 80 | - * @return array | |
| 81 | - */ | |
| 82 | - public abstract function getFiles(); | |
| 83 | - /** | |
| 84 | - * get the content of a file | |
| 85 | - * @param string $path | |
| 86 | - * @return string | |
| 87 | - */ | |
| 88 | - public abstract function getFile($path); | |
| 89 | - /** | |
| 90 | - * extract a single file from the archive | |
| 91 | - * @param string $path | |
| 92 | - * @param string $dest | |
| 93 | - * @return bool | |
| 94 | - */ | |
| 95 | - public abstract function extractFile($path, $dest); | |
| 96 | - /** | |
| 97 | - * extract the archive | |
| 98 | - * @param string $dest | |
| 99 | - * @return bool | |
| 100 | - */ | |
| 101 | - public abstract function extract($dest); | |
| 102 | - /** | |
| 103 | - * check if a file or folder exists in the archive | |
| 104 | - * @param string $path | |
| 105 | - * @return bool | |
| 106 | - */ | |
| 107 | - public abstract function fileExists($path); | |
| 108 | - /** | |
| 109 | - * remove a file or folder from the archive | |
| 110 | - * @param string $path | |
| 111 | - * @return bool | |
| 112 | - */ | |
| 113 | - public abstract function remove($path); | |
| 114 | - /** | |
| 115 | - * get a file handler | |
| 116 | - * @param string $path | |
| 117 | - * @param string $mode | |
| 118 | - * @return resource | |
| 119 | - */ | |
| 120 | - public abstract function getStream($path, $mode); | |
| 121 | - /** | |
| 122 | - * add a folder and all its content | |
| 123 | - * @param string $path | |
| 124 | - * @param string $source | |
| 125 | - * @return boolean|null | |
| 126 | - */ | |
| 127 | -	public function addRecursive($path, $source) { | |
| 128 | - $dh = opendir($source); | |
| 129 | -		if(is_resource($dh)) { | |
| 130 | - $this->addFolder($path); | |
| 131 | -			while (($file = readdir($dh)) !== false) { | |
| 132 | -				if($file === '.' || $file === '..') { | |
| 133 | - continue; | |
| 134 | - } | |
| 135 | -				if(is_dir($source.'/'.$file)) { | |
| 136 | - $this->addRecursive($path.'/'.$file, $source.'/'.$file); | |
| 137 | -				}else{ | |
| 138 | - $this->addFile($path.'/'.$file, $source.'/'.$file); | |
| 139 | - } | |
| 140 | - } | |
| 141 | - } | |
| 142 | - } | |
| 36 | + /** | |
| 37 | + * @param $source | |
| 38 | + */ | |
| 39 | + public abstract function __construct($source); | |
| 40 | + /** | |
| 41 | + * add an empty folder to the archive | |
| 42 | + * @param string $path | |
| 43 | + * @return bool | |
| 44 | + */ | |
| 45 | + public abstract function addFolder($path); | |
| 46 | + /** | |
| 47 | + * add a file to the archive | |
| 48 | + * @param string $path | |
| 49 | + * @param string $source either a local file or string data | |
| 50 | + * @return bool | |
| 51 | + */ | |
| 52 | + public abstract function addFile($path, $source=''); | |
| 53 | + /** | |
| 54 | + * rename a file or folder in the archive | |
| 55 | + * @param string $source | |
| 56 | + * @param string $dest | |
| 57 | + * @return bool | |
| 58 | + */ | |
| 59 | + public abstract function rename($source, $dest); | |
| 60 | + /** | |
| 61 | + * get the uncompressed size of a file in the archive | |
| 62 | + * @param string $path | |
| 63 | + * @return int | |
| 64 | + */ | |
| 65 | + public abstract function filesize($path); | |
| 66 | + /** | |
| 67 | + * get the last modified time of a file in the archive | |
| 68 | + * @param string $path | |
| 69 | + * @return int | |
| 70 | + */ | |
| 71 | + public abstract function mtime($path); | |
| 72 | + /** | |
| 73 | + * get the files in a folder | |
| 74 | + * @param string $path | |
| 75 | + * @return array | |
| 76 | + */ | |
| 77 | + public abstract function getFolder($path); | |
| 78 | + /** | |
| 79 | + * get all files in the archive | |
| 80 | + * @return array | |
| 81 | + */ | |
| 82 | + public abstract function getFiles(); | |
| 83 | + /** | |
| 84 | + * get the content of a file | |
| 85 | + * @param string $path | |
| 86 | + * @return string | |
| 87 | + */ | |
| 88 | + public abstract function getFile($path); | |
| 89 | + /** | |
| 90 | + * extract a single file from the archive | |
| 91 | + * @param string $path | |
| 92 | + * @param string $dest | |
| 93 | + * @return bool | |
| 94 | + */ | |
| 95 | + public abstract function extractFile($path, $dest); | |
| 96 | + /** | |
| 97 | + * extract the archive | |
| 98 | + * @param string $dest | |
| 99 | + * @return bool | |
| 100 | + */ | |
| 101 | + public abstract function extract($dest); | |
| 102 | + /** | |
| 103 | + * check if a file or folder exists in the archive | |
| 104 | + * @param string $path | |
| 105 | + * @return bool | |
| 106 | + */ | |
| 107 | + public abstract function fileExists($path); | |
| 108 | + /** | |
| 109 | + * remove a file or folder from the archive | |
| 110 | + * @param string $path | |
| 111 | + * @return bool | |
| 112 | + */ | |
| 113 | + public abstract function remove($path); | |
| 114 | + /** | |
| 115 | + * get a file handler | |
| 116 | + * @param string $path | |
| 117 | + * @param string $mode | |
| 118 | + * @return resource | |
| 119 | + */ | |
| 120 | + public abstract function getStream($path, $mode); | |
| 121 | + /** | |
| 122 | + * add a folder and all its content | |
| 123 | + * @param string $path | |
| 124 | + * @param string $source | |
| 125 | + * @return boolean|null | |
| 126 | + */ | |
| 127 | +    public function addRecursive($path, $source) { | |
| 128 | + $dh = opendir($source); | |
| 129 | +        if(is_resource($dh)) { | |
| 130 | + $this->addFolder($path); | |
| 131 | +            while (($file = readdir($dh)) !== false) { | |
| 132 | +                if($file === '.' || $file === '..') { | |
| 133 | + continue; | |
| 134 | + } | |
| 135 | +                if(is_dir($source.'/'.$file)) { | |
| 136 | + $this->addRecursive($path.'/'.$file, $source.'/'.$file); | |
| 137 | +                }else{ | |
| 138 | + $this->addFile($path.'/'.$file, $source.'/'.$file); | |
| 139 | + } | |
| 140 | + } | |
| 141 | + } | |
| 142 | + } | |
| 143 | 143 | } | 
| @@ -32,45 +32,45 @@ | ||
| 32 | 32 | */ | 
| 33 | 33 |  class File extends \OCP\Search\Provider { | 
| 34 | 34 | |
| 35 | - /** | |
| 36 | - * Search for files and folders matching the given query | |
| 37 | - * @param string $query | |
| 38 | - * @return \OCP\Search\Result | |
| 39 | - */ | |
| 40 | -	public function search($query) { | |
| 41 | - $files = Filesystem::search($query); | |
| 42 | - $results = array(); | |
| 43 | - // edit results | |
| 44 | -		foreach ($files as $fileData) { | |
| 45 | - // skip versions | |
| 46 | -			if (strpos($fileData['path'], '_versions') === 0) { | |
| 47 | - continue; | |
| 48 | - } | |
| 49 | - // skip top-level folder | |
| 50 | -			if ($fileData['name'] === 'files' && $fileData['parent'] === -1) { | |
| 51 | - continue; | |
| 52 | - } | |
| 53 | - // create audio result | |
| 54 | -			if($fileData['mimepart'] === 'audio'){ | |
| 55 | - $result = new \OC\Search\Result\Audio($fileData); | |
| 56 | - } | |
| 57 | - // create image result | |
| 58 | -			elseif($fileData['mimepart'] === 'image'){ | |
| 59 | - $result = new \OC\Search\Result\Image($fileData); | |
| 60 | - } | |
| 61 | - // create folder result | |
| 62 | -			elseif($fileData['mimetype'] === 'httpd/unix-directory'){ | |
| 63 | - $result = new \OC\Search\Result\Folder($fileData); | |
| 64 | - } | |
| 65 | - // or create file result | |
| 66 | -			else{ | |
| 67 | - $result = new \OC\Search\Result\File($fileData); | |
| 68 | - } | |
| 69 | - // add to results | |
| 70 | - $results[] = $result; | |
| 71 | - } | |
| 72 | - // return | |
| 73 | - return $results; | |
| 74 | - } | |
| 35 | + /** | |
| 36 | + * Search for files and folders matching the given query | |
| 37 | + * @param string $query | |
| 38 | + * @return \OCP\Search\Result | |
| 39 | + */ | |
| 40 | +    public function search($query) { | |
| 41 | + $files = Filesystem::search($query); | |
| 42 | + $results = array(); | |
| 43 | + // edit results | |
| 44 | +        foreach ($files as $fileData) { | |
| 45 | + // skip versions | |
| 46 | +            if (strpos($fileData['path'], '_versions') === 0) { | |
| 47 | + continue; | |
| 48 | + } | |
| 49 | + // skip top-level folder | |
| 50 | +            if ($fileData['name'] === 'files' && $fileData['parent'] === -1) { | |
| 51 | + continue; | |
| 52 | + } | |
| 53 | + // create audio result | |
| 54 | +            if($fileData['mimepart'] === 'audio'){ | |
| 55 | + $result = new \OC\Search\Result\Audio($fileData); | |
| 56 | + } | |
| 57 | + // create image result | |
| 58 | +            elseif($fileData['mimepart'] === 'image'){ | |
| 59 | + $result = new \OC\Search\Result\Image($fileData); | |
| 60 | + } | |
| 61 | + // create folder result | |
| 62 | +            elseif($fileData['mimetype'] === 'httpd/unix-directory'){ | |
| 63 | + $result = new \OC\Search\Result\Folder($fileData); | |
| 64 | + } | |
| 65 | + // or create file result | |
| 66 | +            else{ | |
| 67 | + $result = new \OC\Search\Result\File($fileData); | |
| 68 | + } | |
| 69 | + // add to results | |
| 70 | + $results[] = $result; | |
| 71 | + } | |
| 72 | + // return | |
| 73 | + return $results; | |
| 74 | + } | |
| 75 | 75 | |
| 76 | 76 | } | 
| @@ -34,246 +34,246 @@ | ||
| 34 | 34 | use OCP\ITempManager; | 
| 35 | 35 | |
| 36 | 36 |  class TempManager implements ITempManager { | 
| 37 | - /** @var string[] Current temporary files and folders, used for cleanup */ | |
| 38 | - protected $current = []; | |
| 39 | - /** @var string i.e. /tmp on linux systems */ | |
| 40 | - protected $tmpBaseDir; | |
| 41 | - /** @var ILogger */ | |
| 42 | - protected $log; | |
| 43 | - /** @var IConfig */ | |
| 44 | - protected $config; | |
| 37 | + /** @var string[] Current temporary files and folders, used for cleanup */ | |
| 38 | + protected $current = []; | |
| 39 | + /** @var string i.e. /tmp on linux systems */ | |
| 40 | + protected $tmpBaseDir; | |
| 41 | + /** @var ILogger */ | |
| 42 | + protected $log; | |
| 43 | + /** @var IConfig */ | |
| 44 | + protected $config; | |
| 45 | 45 | |
| 46 | - /** Prefix */ | |
| 47 | - const TMP_PREFIX = 'oc_tmp_'; | |
| 46 | + /** Prefix */ | |
| 47 | + const TMP_PREFIX = 'oc_tmp_'; | |
| 48 | 48 | |
| 49 | - /** | |
| 50 | - * @param \OCP\ILogger $logger | |
| 51 | - * @param \OCP\IConfig $config | |
| 52 | - */ | |
| 53 | -	public function __construct(ILogger $logger, IConfig $config) { | |
| 54 | - $this->log = $logger; | |
| 55 | - $this->config = $config; | |
| 56 | - $this->tmpBaseDir = $this->getTempBaseDir(); | |
| 57 | - } | |
| 49 | + /** | |
| 50 | + * @param \OCP\ILogger $logger | |
| 51 | + * @param \OCP\IConfig $config | |
| 52 | + */ | |
| 53 | +    public function __construct(ILogger $logger, IConfig $config) { | |
| 54 | + $this->log = $logger; | |
| 55 | + $this->config = $config; | |
| 56 | + $this->tmpBaseDir = $this->getTempBaseDir(); | |
| 57 | + } | |
| 58 | 58 | |
| 59 | - /** | |
| 60 | - * Builds the filename with suffix and removes potential dangerous characters | |
| 61 | - * such as directory separators. | |
| 62 | - * | |
| 63 | - * @param string $absolutePath Absolute path to the file / folder | |
| 64 | - * @param string $postFix Postfix appended to the temporary file name, may be user controlled | |
| 65 | - * @return string | |
| 66 | - */ | |
| 67 | -	private function buildFileNameWithSuffix($absolutePath, $postFix = '') { | |
| 68 | -		if($postFix !== '') { | |
| 69 | - $postFix = '.' . ltrim($postFix, '.'); | |
| 70 | - $postFix = str_replace(['\\', '/'], '', $postFix); | |
| 71 | - $absolutePath .= '-'; | |
| 72 | - } | |
| 59 | + /** | |
| 60 | + * Builds the filename with suffix and removes potential dangerous characters | |
| 61 | + * such as directory separators. | |
| 62 | + * | |
| 63 | + * @param string $absolutePath Absolute path to the file / folder | |
| 64 | + * @param string $postFix Postfix appended to the temporary file name, may be user controlled | |
| 65 | + * @return string | |
| 66 | + */ | |
| 67 | +    private function buildFileNameWithSuffix($absolutePath, $postFix = '') { | |
| 68 | +        if($postFix !== '') { | |
| 69 | + $postFix = '.' . ltrim($postFix, '.'); | |
| 70 | + $postFix = str_replace(['\\', '/'], '', $postFix); | |
| 71 | + $absolutePath .= '-'; | |
| 72 | + } | |
| 73 | 73 | |
| 74 | - return $absolutePath . $postFix; | |
| 75 | - } | |
| 74 | + return $absolutePath . $postFix; | |
| 75 | + } | |
| 76 | 76 | |
| 77 | - /** | |
| 78 | - * Create a temporary file and return the path | |
| 79 | - * | |
| 80 | - * @param string $postFix Postfix appended to the temporary file name | |
| 81 | - * @return string | |
| 82 | - */ | |
| 83 | -	public function getTemporaryFile($postFix = '') { | |
| 84 | -		if (is_writable($this->tmpBaseDir)) { | |
| 85 | - // To create an unique file and prevent the risk of race conditions | |
| 86 | - // or duplicated temporary files by other means such as collisions | |
| 87 | - // we need to create the file using `tempnam` and append a possible | |
| 88 | - // postfix to it later | |
| 89 | - $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); | |
| 90 | - $this->current[] = $file; | |
| 77 | + /** | |
| 78 | + * Create a temporary file and return the path | |
| 79 | + * | |
| 80 | + * @param string $postFix Postfix appended to the temporary file name | |
| 81 | + * @return string | |
| 82 | + */ | |
| 83 | +    public function getTemporaryFile($postFix = '') { | |
| 84 | +        if (is_writable($this->tmpBaseDir)) { | |
| 85 | + // To create an unique file and prevent the risk of race conditions | |
| 86 | + // or duplicated temporary files by other means such as collisions | |
| 87 | + // we need to create the file using `tempnam` and append a possible | |
| 88 | + // postfix to it later | |
| 89 | + $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); | |
| 90 | + $this->current[] = $file; | |
| 91 | 91 | |
| 92 | - // If a postfix got specified sanitize it and create a postfixed | |
| 93 | - // temporary file | |
| 94 | -			if($postFix !== '') { | |
| 95 | - $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); | |
| 96 | - touch($fileNameWithPostfix); | |
| 97 | - chmod($fileNameWithPostfix, 0600); | |
| 98 | - $this->current[] = $fileNameWithPostfix; | |
| 99 | - return $fileNameWithPostfix; | |
| 100 | - } | |
| 92 | + // If a postfix got specified sanitize it and create a postfixed | |
| 93 | + // temporary file | |
| 94 | +            if($postFix !== '') { | |
| 95 | + $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); | |
| 96 | + touch($fileNameWithPostfix); | |
| 97 | + chmod($fileNameWithPostfix, 0600); | |
| 98 | + $this->current[] = $fileNameWithPostfix; | |
| 99 | + return $fileNameWithPostfix; | |
| 100 | + } | |
| 101 | 101 | |
| 102 | - return $file; | |
| 103 | -		} else { | |
| 104 | - $this->log->warning( | |
| 105 | -				'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', | |
| 106 | - [ | |
| 107 | - 'dir' => $this->tmpBaseDir, | |
| 108 | - ] | |
| 109 | - ); | |
| 110 | - return false; | |
| 111 | - } | |
| 112 | - } | |
| 102 | + return $file; | |
| 103 | +        } else { | |
| 104 | + $this->log->warning( | |
| 105 | +                'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', | |
| 106 | + [ | |
| 107 | + 'dir' => $this->tmpBaseDir, | |
| 108 | + ] | |
| 109 | + ); | |
| 110 | + return false; | |
| 111 | + } | |
| 112 | + } | |
| 113 | 113 | |
| 114 | - /** | |
| 115 | - * Create a temporary folder and return the path | |
| 116 | - * | |
| 117 | - * @param string $postFix Postfix appended to the temporary folder name | |
| 118 | - * @return string | |
| 119 | - */ | |
| 120 | -	public function getTemporaryFolder($postFix = '') { | |
| 121 | -		if (is_writable($this->tmpBaseDir)) { | |
| 122 | - // To create an unique directory and prevent the risk of race conditions | |
| 123 | - // or duplicated temporary files by other means such as collisions | |
| 124 | - // we need to create the file using `tempnam` and append a possible | |
| 125 | - // postfix to it later | |
| 126 | - $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); | |
| 127 | - $this->current[] = $uniqueFileName; | |
| 114 | + /** | |
| 115 | + * Create a temporary folder and return the path | |
| 116 | + * | |
| 117 | + * @param string $postFix Postfix appended to the temporary folder name | |
| 118 | + * @return string | |
| 119 | + */ | |
| 120 | +    public function getTemporaryFolder($postFix = '') { | |
| 121 | +        if (is_writable($this->tmpBaseDir)) { | |
| 122 | + // To create an unique directory and prevent the risk of race conditions | |
| 123 | + // or duplicated temporary files by other means such as collisions | |
| 124 | + // we need to create the file using `tempnam` and append a possible | |
| 125 | + // postfix to it later | |
| 126 | + $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); | |
| 127 | + $this->current[] = $uniqueFileName; | |
| 128 | 128 | |
| 129 | - // Build a name without postfix | |
| 130 | - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); | |
| 131 | - mkdir($path, 0700); | |
| 132 | - $this->current[] = $path; | |
| 129 | + // Build a name without postfix | |
| 130 | + $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); | |
| 131 | + mkdir($path, 0700); | |
| 132 | + $this->current[] = $path; | |
| 133 | 133 | |
| 134 | - return $path . '/'; | |
| 135 | -		} else { | |
| 136 | - $this->log->warning( | |
| 137 | -				'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', | |
| 138 | - [ | |
| 139 | - 'dir' => $this->tmpBaseDir, | |
| 140 | - ] | |
| 141 | - ); | |
| 142 | - return false; | |
| 143 | - } | |
| 144 | - } | |
| 134 | + return $path . '/'; | |
| 135 | +        } else { | |
| 136 | + $this->log->warning( | |
| 137 | +                'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', | |
| 138 | + [ | |
| 139 | + 'dir' => $this->tmpBaseDir, | |
| 140 | + ] | |
| 141 | + ); | |
| 142 | + return false; | |
| 143 | + } | |
| 144 | + } | |
| 145 | 145 | |
| 146 | - /** | |
| 147 | - * Remove the temporary files and folders generated during this request | |
| 148 | - */ | |
| 149 | -	public function clean() { | |
| 150 | - $this->cleanFiles($this->current); | |
| 151 | - } | |
| 146 | + /** | |
| 147 | + * Remove the temporary files and folders generated during this request | |
| 148 | + */ | |
| 149 | +    public function clean() { | |
| 150 | + $this->cleanFiles($this->current); | |
| 151 | + } | |
| 152 | 152 | |
| 153 | - /** | |
| 154 | - * @param string[] $files | |
| 155 | - */ | |
| 156 | -	protected function cleanFiles($files) { | |
| 157 | -		foreach ($files as $file) { | |
| 158 | -			if (file_exists($file)) { | |
| 159 | -				try { | |
| 160 | - \OC_Helper::rmdirr($file); | |
| 161 | -				} catch (\UnexpectedValueException $ex) { | |
| 162 | - $this->log->warning( | |
| 163 | -						"Error deleting temporary file/folder: {file} - Reason: {error}", | |
| 164 | - [ | |
| 165 | - 'file' => $file, | |
| 166 | - 'error' => $ex->getMessage(), | |
| 167 | - ] | |
| 168 | - ); | |
| 169 | - } | |
| 170 | - } | |
| 171 | - } | |
| 172 | - } | |
| 153 | + /** | |
| 154 | + * @param string[] $files | |
| 155 | + */ | |
| 156 | +    protected function cleanFiles($files) { | |
| 157 | +        foreach ($files as $file) { | |
| 158 | +            if (file_exists($file)) { | |
| 159 | +                try { | |
| 160 | + \OC_Helper::rmdirr($file); | |
| 161 | +                } catch (\UnexpectedValueException $ex) { | |
| 162 | + $this->log->warning( | |
| 163 | +                        "Error deleting temporary file/folder: {file} - Reason: {error}", | |
| 164 | + [ | |
| 165 | + 'file' => $file, | |
| 166 | + 'error' => $ex->getMessage(), | |
| 167 | + ] | |
| 168 | + ); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + } | |
| 172 | + } | |
| 173 | 173 | |
| 174 | - /** | |
| 175 | - * Remove old temporary files and folders that were failed to be cleaned | |
| 176 | - */ | |
| 177 | -	public function cleanOld() { | |
| 178 | - $this->cleanFiles($this->getOldFiles()); | |
| 179 | - } | |
| 174 | + /** | |
| 175 | + * Remove old temporary files and folders that were failed to be cleaned | |
| 176 | + */ | |
| 177 | +    public function cleanOld() { | |
| 178 | + $this->cleanFiles($this->getOldFiles()); | |
| 179 | + } | |
| 180 | 180 | |
| 181 | - /** | |
| 182 | - * Get all temporary files and folders generated by oc older than an hour | |
| 183 | - * | |
| 184 | - * @return string[] | |
| 185 | - */ | |
| 186 | -	protected function getOldFiles() { | |
| 187 | - $cutOfTime = time() - 3600; | |
| 188 | - $files = []; | |
| 189 | - $dh = opendir($this->tmpBaseDir); | |
| 190 | -		if ($dh) { | |
| 191 | -			while (($file = readdir($dh)) !== false) { | |
| 192 | -				if (substr($file, 0, 7) === self::TMP_PREFIX) { | |
| 193 | - $path = $this->tmpBaseDir . '/' . $file; | |
| 194 | - $mtime = filemtime($path); | |
| 195 | -					if ($mtime < $cutOfTime) { | |
| 196 | - $files[] = $path; | |
| 197 | - } | |
| 198 | - } | |
| 199 | - } | |
| 200 | - } | |
| 201 | - return $files; | |
| 202 | - } | |
| 181 | + /** | |
| 182 | + * Get all temporary files and folders generated by oc older than an hour | |
| 183 | + * | |
| 184 | + * @return string[] | |
| 185 | + */ | |
| 186 | +    protected function getOldFiles() { | |
| 187 | + $cutOfTime = time() - 3600; | |
| 188 | + $files = []; | |
| 189 | + $dh = opendir($this->tmpBaseDir); | |
| 190 | +        if ($dh) { | |
| 191 | +            while (($file = readdir($dh)) !== false) { | |
| 192 | +                if (substr($file, 0, 7) === self::TMP_PREFIX) { | |
| 193 | + $path = $this->tmpBaseDir . '/' . $file; | |
| 194 | + $mtime = filemtime($path); | |
| 195 | +                    if ($mtime < $cutOfTime) { | |
| 196 | + $files[] = $path; | |
| 197 | + } | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } | |
| 201 | + return $files; | |
| 202 | + } | |
| 203 | 203 | |
| 204 | - /** | |
| 205 | - * Get the temporary base directory configured on the server | |
| 206 | - * | |
| 207 | - * @return string Path to the temporary directory or null | |
| 208 | - * @throws \UnexpectedValueException | |
| 209 | - */ | |
| 210 | -	public function getTempBaseDir() { | |
| 211 | -		if ($this->tmpBaseDir) { | |
| 212 | - return $this->tmpBaseDir; | |
| 213 | - } | |
| 204 | + /** | |
| 205 | + * Get the temporary base directory configured on the server | |
| 206 | + * | |
| 207 | + * @return string Path to the temporary directory or null | |
| 208 | + * @throws \UnexpectedValueException | |
| 209 | + */ | |
| 210 | +    public function getTempBaseDir() { | |
| 211 | +        if ($this->tmpBaseDir) { | |
| 212 | + return $this->tmpBaseDir; | |
| 213 | + } | |
| 214 | 214 | |
| 215 | - $directories = []; | |
| 216 | -		if ($temp = $this->config->getSystemValue('tempdirectory', null)) { | |
| 217 | - $directories[] = $temp; | |
| 218 | - } | |
| 219 | -		if ($temp = \OC::$server->getIniWrapper()->get('upload_tmp_dir')) { | |
| 220 | - $directories[] = $temp; | |
| 221 | - } | |
| 222 | -		if ($temp = getenv('TMP')) { | |
| 223 | - $directories[] = $temp; | |
| 224 | - } | |
| 225 | -		if ($temp = getenv('TEMP')) { | |
| 226 | - $directories[] = $temp; | |
| 227 | - } | |
| 228 | -		if ($temp = getenv('TMPDIR')) { | |
| 229 | - $directories[] = $temp; | |
| 230 | - } | |
| 231 | -		if ($temp = sys_get_temp_dir()) { | |
| 232 | - $directories[] = $temp; | |
| 233 | - } | |
| 215 | + $directories = []; | |
| 216 | +        if ($temp = $this->config->getSystemValue('tempdirectory', null)) { | |
| 217 | + $directories[] = $temp; | |
| 218 | + } | |
| 219 | +        if ($temp = \OC::$server->getIniWrapper()->get('upload_tmp_dir')) { | |
| 220 | + $directories[] = $temp; | |
| 221 | + } | |
| 222 | +        if ($temp = getenv('TMP')) { | |
| 223 | + $directories[] = $temp; | |
| 224 | + } | |
| 225 | +        if ($temp = getenv('TEMP')) { | |
| 226 | + $directories[] = $temp; | |
| 227 | + } | |
| 228 | +        if ($temp = getenv('TMPDIR')) { | |
| 229 | + $directories[] = $temp; | |
| 230 | + } | |
| 231 | +        if ($temp = sys_get_temp_dir()) { | |
| 232 | + $directories[] = $temp; | |
| 233 | + } | |
| 234 | 234 | |
| 235 | -		foreach ($directories as $dir) { | |
| 236 | -			if ($this->checkTemporaryDirectory($dir)) { | |
| 237 | - return $dir; | |
| 238 | - } | |
| 239 | - } | |
| 235 | +        foreach ($directories as $dir) { | |
| 236 | +            if ($this->checkTemporaryDirectory($dir)) { | |
| 237 | + return $dir; | |
| 238 | + } | |
| 239 | + } | |
| 240 | 240 | |
| 241 | - $temp = tempnam(dirname(__FILE__), ''); | |
| 242 | -		if (file_exists($temp)) { | |
| 243 | - unlink($temp); | |
| 244 | - return dirname($temp); | |
| 245 | - } | |
| 246 | -		throw new \UnexpectedValueException('Unable to detect system temporary directory'); | |
| 247 | - } | |
| 241 | + $temp = tempnam(dirname(__FILE__), ''); | |
| 242 | +        if (file_exists($temp)) { | |
| 243 | + unlink($temp); | |
| 244 | + return dirname($temp); | |
| 245 | + } | |
| 246 | +        throw new \UnexpectedValueException('Unable to detect system temporary directory'); | |
| 247 | + } | |
| 248 | 248 | |
| 249 | - /** | |
| 250 | - * Check if a temporary directory is ready for use | |
| 251 | - * | |
| 252 | - * @param mixed $directory | |
| 253 | - * @return bool | |
| 254 | - */ | |
| 255 | -	private function checkTemporaryDirectory($directory) { | |
| 256 | - // suppress any possible errors caused by is_writable | |
| 257 | - // checks missing or invalid path or characters, wrong permissions etc | |
| 258 | -		try { | |
| 259 | -			if (is_writable($directory)) { | |
| 260 | - return true; | |
| 261 | - } | |
| 262 | -		} catch (\Exception $e) { | |
| 263 | - } | |
| 264 | -		$this->log->warning('Temporary directory {dir} is not present or writable', | |
| 265 | - ['dir' => $directory] | |
| 266 | - ); | |
| 267 | - return false; | |
| 268 | - } | |
| 249 | + /** | |
| 250 | + * Check if a temporary directory is ready for use | |
| 251 | + * | |
| 252 | + * @param mixed $directory | |
| 253 | + * @return bool | |
| 254 | + */ | |
| 255 | +    private function checkTemporaryDirectory($directory) { | |
| 256 | + // suppress any possible errors caused by is_writable | |
| 257 | + // checks missing or invalid path or characters, wrong permissions etc | |
| 258 | +        try { | |
| 259 | +            if (is_writable($directory)) { | |
| 260 | + return true; | |
| 261 | + } | |
| 262 | +        } catch (\Exception $e) { | |
| 263 | + } | |
| 264 | +        $this->log->warning('Temporary directory {dir} is not present or writable', | |
| 265 | + ['dir' => $directory] | |
| 266 | + ); | |
| 267 | + return false; | |
| 268 | + } | |
| 269 | 269 | |
| 270 | - /** | |
| 271 | - * Override the temporary base directory | |
| 272 | - * | |
| 273 | - * @param string $directory | |
| 274 | - */ | |
| 275 | -	public function overrideTempBaseDir($directory) { | |
| 276 | - $this->tmpBaseDir = $directory; | |
| 277 | - } | |
| 270 | + /** | |
| 271 | + * Override the temporary base directory | |
| 272 | + * | |
| 273 | + * @param string $directory | |
| 274 | + */ | |
| 275 | +    public function overrideTempBaseDir($directory) { | |
| 276 | + $this->tmpBaseDir = $directory; | |
| 277 | + } | |
| 278 | 278 | |
| 279 | 279 | } | 
| @@ -32,68 +32,68 @@ | ||
| 32 | 32 | * @package OC\Security | 
| 33 | 33 | */ | 
| 34 | 34 |  class TrustedDomainHelper { | 
| 35 | - /** @var IConfig */ | |
| 36 | - private $config; | |
| 35 | + /** @var IConfig */ | |
| 36 | + private $config; | |
| 37 | 37 | |
| 38 | - /** | |
| 39 | - * @param IConfig $config | |
| 40 | - */ | |
| 41 | -	public function __construct(IConfig $config) { | |
| 42 | - $this->config = $config; | |
| 43 | - } | |
| 38 | + /** | |
| 39 | + * @param IConfig $config | |
| 40 | + */ | |
| 41 | +    public function __construct(IConfig $config) { | |
| 42 | + $this->config = $config; | |
| 43 | + } | |
| 44 | 44 | |
| 45 | - /** | |
| 46 | - * Strips a potential port from a domain (in format domain:port) | |
| 47 | - * @param string $host | |
| 48 | - * @return string $host without appended port | |
| 49 | - */ | |
| 50 | -	private function getDomainWithoutPort($host) { | |
| 51 | - $pos = strrpos($host, ':'); | |
| 52 | -		if ($pos !== false) { | |
| 53 | - $port = substr($host, $pos + 1); | |
| 54 | -			if (is_numeric($port)) { | |
| 55 | - $host = substr($host, 0, $pos); | |
| 56 | - } | |
| 57 | - } | |
| 58 | - return $host; | |
| 59 | - } | |
| 45 | + /** | |
| 46 | + * Strips a potential port from a domain (in format domain:port) | |
| 47 | + * @param string $host | |
| 48 | + * @return string $host without appended port | |
| 49 | + */ | |
| 50 | +    private function getDomainWithoutPort($host) { | |
| 51 | + $pos = strrpos($host, ':'); | |
| 52 | +        if ($pos !== false) { | |
| 53 | + $port = substr($host, $pos + 1); | |
| 54 | +            if (is_numeric($port)) { | |
| 55 | + $host = substr($host, 0, $pos); | |
| 56 | + } | |
| 57 | + } | |
| 58 | + return $host; | |
| 59 | + } | |
| 60 | 60 | |
| 61 | - /** | |
| 62 | - * Checks whether a domain is considered as trusted from the list | |
| 63 | - * of trusted domains. If no trusted domains have been configured, returns | |
| 64 | - * true. | |
| 65 | - * This is used to prevent Host Header Poisoning. | |
| 66 | - * @param string $domainWithPort | |
| 67 | - * @return bool true if the given domain is trusted or if no trusted domains | |
| 68 | - * have been configured | |
| 69 | - */ | |
| 70 | -	public function isTrustedDomain($domainWithPort) { | |
| 71 | - $domain = $this->getDomainWithoutPort($domainWithPort); | |
| 61 | + /** | |
| 62 | + * Checks whether a domain is considered as trusted from the list | |
| 63 | + * of trusted domains. If no trusted domains have been configured, returns | |
| 64 | + * true. | |
| 65 | + * This is used to prevent Host Header Poisoning. | |
| 66 | + * @param string $domainWithPort | |
| 67 | + * @return bool true if the given domain is trusted or if no trusted domains | |
| 68 | + * have been configured | |
| 69 | + */ | |
| 70 | +    public function isTrustedDomain($domainWithPort) { | |
| 71 | + $domain = $this->getDomainWithoutPort($domainWithPort); | |
| 72 | 72 | |
| 73 | - // Read trusted domains from config | |
| 74 | -		$trustedList = $this->config->getSystemValue('trusted_domains', []); | |
| 75 | -		if (!is_array($trustedList)) { | |
| 76 | - return false; | |
| 77 | - } | |
| 73 | + // Read trusted domains from config | |
| 74 | +        $trustedList = $this->config->getSystemValue('trusted_domains', []); | |
| 75 | +        if (!is_array($trustedList)) { | |
| 76 | + return false; | |
| 77 | + } | |
| 78 | 78 | |
| 79 | - // Always allow access from localhost | |
| 80 | -		if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) { | |
| 81 | - return true; | |
| 82 | - } | |
| 83 | - // Reject misformed domains in any case | |
| 84 | -		if (strpos($domain,'-') === 0 || strpos($domain,'..') !== false) { | |
| 85 | - return false; | |
| 86 | - } | |
| 87 | - // Match, allowing for * wildcards | |
| 88 | -		foreach ($trustedList as $trusted) { | |
| 89 | -			if (gettype($trusted) !== 'string') { | |
| 90 | - break; | |
| 91 | - } | |
| 92 | -			$regex = '/^' . implode('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/'; | |
| 93 | -			if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) { | |
| 94 | - return true; | |
| 95 | - } | |
| 96 | - } | |
| 97 | - return false; | |
| 98 | - } | |
| 79 | + // Always allow access from localhost | |
| 80 | +        if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) { | |
| 81 | + return true; | |
| 82 | + } | |
| 83 | + // Reject misformed domains in any case | |
| 84 | +        if (strpos($domain,'-') === 0 || strpos($domain,'..') !== false) { | |
| 85 | + return false; | |
| 86 | + } | |
| 87 | + // Match, allowing for * wildcards | |
| 88 | +        foreach ($trustedList as $trusted) { | |
| 89 | +            if (gettype($trusted) !== 'string') { | |
| 90 | + break; | |
| 91 | + } | |
| 92 | +            $regex = '/^' . implode('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/'; | |
| 93 | +            if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) { | |
| 94 | + return true; | |
| 95 | + } | |
| 96 | + } | |
| 97 | + return false; | |
| 98 | + } | |
| 99 | 99 | } | 
| @@ -33,193 +33,193 @@ | ||
| 33 | 33 | use OCP\IMemcache; | 
| 34 | 34 | |
| 35 | 35 |  class Memcached extends Cache implements IMemcache { | 
| 36 | - use CASTrait; | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * @var \Memcached $cache | |
| 40 | - */ | |
| 41 | - private static $cache = null; | |
| 42 | - | |
| 43 | - use CADTrait; | |
| 44 | - | |
| 45 | -	public function __construct($prefix = '') { | |
| 46 | - parent::__construct($prefix); | |
| 47 | -		if (is_null(self::$cache)) { | |
| 48 | - self::$cache = new \Memcached(); | |
| 49 | - | |
| 50 | - $defaultOptions = [ | |
| 51 | - \Memcached::OPT_CONNECT_TIMEOUT => 50, | |
| 52 | - \Memcached::OPT_RETRY_TIMEOUT => 50, | |
| 53 | - \Memcached::OPT_SEND_TIMEOUT => 50, | |
| 54 | - \Memcached::OPT_RECV_TIMEOUT => 50, | |
| 55 | - \Memcached::OPT_POLL_TIMEOUT => 50, | |
| 56 | - | |
| 57 | - // Enable compression | |
| 58 | - \Memcached::OPT_COMPRESSION => true, | |
| 59 | - | |
| 60 | - // Turn on consistent hashing | |
| 61 | - \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, | |
| 62 | - | |
| 63 | - // Enable Binary Protocol | |
| 64 | - //\Memcached::OPT_BINARY_PROTOCOL => true, | |
| 65 | - ]; | |
| 66 | - // by default enable igbinary serializer if available | |
| 67 | -			if (\Memcached::HAVE_IGBINARY) { | |
| 68 | - $defaultOptions[\Memcached::OPT_SERIALIZER] = | |
| 69 | - \Memcached::SERIALIZER_IGBINARY; | |
| 70 | - } | |
| 71 | -			$options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); | |
| 72 | -			if (is_array($options)) { | |
| 73 | - $options = $options + $defaultOptions; | |
| 74 | - self::$cache->setOptions($options); | |
| 75 | -			} else { | |
| 76 | -				throw new HintException("Expected 'memcached_options' config to be an array, got $options"); | |
| 77 | - } | |
| 78 | - | |
| 79 | -			$servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); | |
| 80 | -			if (!$servers) { | |
| 81 | -				$server = \OC::$server->getSystemConfig()->getValue('memcached_server'); | |
| 82 | -				if ($server) { | |
| 83 | - $servers = [$server]; | |
| 84 | -				} else { | |
| 85 | - $servers = [['localhost', 11211]]; | |
| 86 | - } | |
| 87 | - } | |
| 88 | - self::$cache->addServers($servers); | |
| 89 | - } | |
| 90 | - } | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * entries in XCache gets namespaced to prevent collisions between owncloud instances and users | |
| 94 | - */ | |
| 95 | -	protected function getNameSpace() { | |
| 96 | - return $this->prefix; | |
| 97 | - } | |
| 98 | - | |
| 99 | -	public function get($key) { | |
| 100 | - $result = self::$cache->get($this->getNameSpace() . $key); | |
| 101 | -		if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { | |
| 102 | - return null; | |
| 103 | -		} else { | |
| 104 | - return $result; | |
| 105 | - } | |
| 106 | - } | |
| 107 | - | |
| 108 | -	public function set($key, $value, $ttl = 0) { | |
| 109 | -		if ($ttl > 0) { | |
| 110 | - $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); | |
| 111 | -		} else { | |
| 112 | - $result = self::$cache->set($this->getNameSpace() . $key, $value); | |
| 113 | - } | |
| 114 | -		if ($result !== true) { | |
| 115 | - $this->verifyReturnCode(); | |
| 116 | - } | |
| 117 | - return $result; | |
| 118 | - } | |
| 119 | - | |
| 120 | -	public function hasKey($key) { | |
| 121 | - self::$cache->get($this->getNameSpace() . $key); | |
| 122 | - return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; | |
| 123 | - } | |
| 124 | - | |
| 125 | -	public function remove($key) { | |
| 126 | - $result= self::$cache->delete($this->getNameSpace() . $key); | |
| 127 | -		if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { | |
| 128 | - $this->verifyReturnCode(); | |
| 129 | - } | |
| 130 | - return $result; | |
| 131 | - } | |
| 132 | - | |
| 133 | -	public function clear($prefix = '') { | |
| 134 | - $prefix = $this->getNameSpace() . $prefix; | |
| 135 | - $allKeys = self::$cache->getAllKeys(); | |
| 136 | -		if ($allKeys === false) { | |
| 137 | - // newer Memcached doesn't like getAllKeys(), flush everything | |
| 138 | - self::$cache->flush(); | |
| 139 | - return true; | |
| 140 | - } | |
| 141 | - $keys = array(); | |
| 142 | - $prefixLength = strlen($prefix); | |
| 143 | -		foreach ($allKeys as $key) { | |
| 144 | -			if (substr($key, 0, $prefixLength) === $prefix) { | |
| 145 | - $keys[] = $key; | |
| 146 | - } | |
| 147 | - } | |
| 148 | -		if (method_exists(self::$cache, 'deleteMulti')) { | |
| 149 | - self::$cache->deleteMulti($keys); | |
| 150 | -		} else { | |
| 151 | -			foreach ($keys as $key) { | |
| 152 | - self::$cache->delete($key); | |
| 153 | - } | |
| 154 | - } | |
| 155 | - return true; | |
| 156 | - } | |
| 157 | - | |
| 158 | - /** | |
| 159 | - * Set a value in the cache if it's not already stored | |
| 160 | - * | |
| 161 | - * @param string $key | |
| 162 | - * @param mixed $value | |
| 163 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 | |
| 164 | - * @return bool | |
| 165 | - * @throws \Exception | |
| 166 | - */ | |
| 167 | -	public function add($key, $value, $ttl = 0) { | |
| 168 | - $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); | |
| 169 | -		if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { | |
| 170 | - $this->verifyReturnCode(); | |
| 171 | - } | |
| 172 | - return $result; | |
| 173 | - } | |
| 174 | - | |
| 175 | - /** | |
| 176 | - * Increase a stored number | |
| 177 | - * | |
| 178 | - * @param string $key | |
| 179 | - * @param int $step | |
| 180 | - * @return int | bool | |
| 181 | - */ | |
| 182 | -	public function inc($key, $step = 1) { | |
| 183 | - $this->add($key, 0); | |
| 184 | - $result = self::$cache->increment($this->getPrefix() . $key, $step); | |
| 185 | - | |
| 186 | -		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { | |
| 187 | - return false; | |
| 188 | - } | |
| 189 | - | |
| 190 | - return $result; | |
| 191 | - } | |
| 192 | - | |
| 193 | - /** | |
| 194 | - * Decrease a stored number | |
| 195 | - * | |
| 196 | - * @param string $key | |
| 197 | - * @param int $step | |
| 198 | - * @return int | bool | |
| 199 | - */ | |
| 200 | -	public function dec($key, $step = 1) { | |
| 201 | - $result = self::$cache->decrement($this->getPrefix() . $key, $step); | |
| 202 | - | |
| 203 | -		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { | |
| 204 | - return false; | |
| 205 | - } | |
| 206 | - | |
| 207 | - return $result; | |
| 208 | - } | |
| 209 | - | |
| 210 | -	static public function isAvailable() { | |
| 211 | -		return extension_loaded('memcached'); | |
| 212 | - } | |
| 213 | - | |
| 214 | - /** | |
| 215 | - * @throws \Exception | |
| 216 | - */ | |
| 217 | -	private function verifyReturnCode() { | |
| 218 | - $code = self::$cache->getResultCode(); | |
| 219 | -		if ($code === \Memcached::RES_SUCCESS) { | |
| 220 | - return; | |
| 221 | - } | |
| 222 | - $message = self::$cache->getResultMessage(); | |
| 223 | -		throw new \Exception("Error $code interacting with memcached : $message"); | |
| 224 | - } | |
| 36 | + use CASTrait; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @var \Memcached $cache | |
| 40 | + */ | |
| 41 | + private static $cache = null; | |
| 42 | + | |
| 43 | + use CADTrait; | |
| 44 | + | |
| 45 | +    public function __construct($prefix = '') { | |
| 46 | + parent::__construct($prefix); | |
| 47 | +        if (is_null(self::$cache)) { | |
| 48 | + self::$cache = new \Memcached(); | |
| 49 | + | |
| 50 | + $defaultOptions = [ | |
| 51 | + \Memcached::OPT_CONNECT_TIMEOUT => 50, | |
| 52 | + \Memcached::OPT_RETRY_TIMEOUT => 50, | |
| 53 | + \Memcached::OPT_SEND_TIMEOUT => 50, | |
| 54 | + \Memcached::OPT_RECV_TIMEOUT => 50, | |
| 55 | + \Memcached::OPT_POLL_TIMEOUT => 50, | |
| 56 | + | |
| 57 | + // Enable compression | |
| 58 | + \Memcached::OPT_COMPRESSION => true, | |
| 59 | + | |
| 60 | + // Turn on consistent hashing | |
| 61 | + \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, | |
| 62 | + | |
| 63 | + // Enable Binary Protocol | |
| 64 | + //\Memcached::OPT_BINARY_PROTOCOL => true, | |
| 65 | + ]; | |
| 66 | + // by default enable igbinary serializer if available | |
| 67 | +            if (\Memcached::HAVE_IGBINARY) { | |
| 68 | + $defaultOptions[\Memcached::OPT_SERIALIZER] = | |
| 69 | + \Memcached::SERIALIZER_IGBINARY; | |
| 70 | + } | |
| 71 | +            $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); | |
| 72 | +            if (is_array($options)) { | |
| 73 | + $options = $options + $defaultOptions; | |
| 74 | + self::$cache->setOptions($options); | |
| 75 | +            } else { | |
| 76 | +                throw new HintException("Expected 'memcached_options' config to be an array, got $options"); | |
| 77 | + } | |
| 78 | + | |
| 79 | +            $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); | |
| 80 | +            if (!$servers) { | |
| 81 | +                $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); | |
| 82 | +                if ($server) { | |
| 83 | + $servers = [$server]; | |
| 84 | +                } else { | |
| 85 | + $servers = [['localhost', 11211]]; | |
| 86 | + } | |
| 87 | + } | |
| 88 | + self::$cache->addServers($servers); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * entries in XCache gets namespaced to prevent collisions between owncloud instances and users | |
| 94 | + */ | |
| 95 | +    protected function getNameSpace() { | |
| 96 | + return $this->prefix; | |
| 97 | + } | |
| 98 | + | |
| 99 | +    public function get($key) { | |
| 100 | + $result = self::$cache->get($this->getNameSpace() . $key); | |
| 101 | +        if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { | |
| 102 | + return null; | |
| 103 | +        } else { | |
| 104 | + return $result; | |
| 105 | + } | |
| 106 | + } | |
| 107 | + | |
| 108 | +    public function set($key, $value, $ttl = 0) { | |
| 109 | +        if ($ttl > 0) { | |
| 110 | + $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); | |
| 111 | +        } else { | |
| 112 | + $result = self::$cache->set($this->getNameSpace() . $key, $value); | |
| 113 | + } | |
| 114 | +        if ($result !== true) { | |
| 115 | + $this->verifyReturnCode(); | |
| 116 | + } | |
| 117 | + return $result; | |
| 118 | + } | |
| 119 | + | |
| 120 | +    public function hasKey($key) { | |
| 121 | + self::$cache->get($this->getNameSpace() . $key); | |
| 122 | + return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; | |
| 123 | + } | |
| 124 | + | |
| 125 | +    public function remove($key) { | |
| 126 | + $result= self::$cache->delete($this->getNameSpace() . $key); | |
| 127 | +        if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { | |
| 128 | + $this->verifyReturnCode(); | |
| 129 | + } | |
| 130 | + return $result; | |
| 131 | + } | |
| 132 | + | |
| 133 | +    public function clear($prefix = '') { | |
| 134 | + $prefix = $this->getNameSpace() . $prefix; | |
| 135 | + $allKeys = self::$cache->getAllKeys(); | |
| 136 | +        if ($allKeys === false) { | |
| 137 | + // newer Memcached doesn't like getAllKeys(), flush everything | |
| 138 | + self::$cache->flush(); | |
| 139 | + return true; | |
| 140 | + } | |
| 141 | + $keys = array(); | |
| 142 | + $prefixLength = strlen($prefix); | |
| 143 | +        foreach ($allKeys as $key) { | |
| 144 | +            if (substr($key, 0, $prefixLength) === $prefix) { | |
| 145 | + $keys[] = $key; | |
| 146 | + } | |
| 147 | + } | |
| 148 | +        if (method_exists(self::$cache, 'deleteMulti')) { | |
| 149 | + self::$cache->deleteMulti($keys); | |
| 150 | +        } else { | |
| 151 | +            foreach ($keys as $key) { | |
| 152 | + self::$cache->delete($key); | |
| 153 | + } | |
| 154 | + } | |
| 155 | + return true; | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Set a value in the cache if it's not already stored | |
| 160 | + * | |
| 161 | + * @param string $key | |
| 162 | + * @param mixed $value | |
| 163 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 | |
| 164 | + * @return bool | |
| 165 | + * @throws \Exception | |
| 166 | + */ | |
| 167 | +    public function add($key, $value, $ttl = 0) { | |
| 168 | + $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); | |
| 169 | +        if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { | |
| 170 | + $this->verifyReturnCode(); | |
| 171 | + } | |
| 172 | + return $result; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * Increase a stored number | |
| 177 | + * | |
| 178 | + * @param string $key | |
| 179 | + * @param int $step | |
| 180 | + * @return int | bool | |
| 181 | + */ | |
| 182 | +    public function inc($key, $step = 1) { | |
| 183 | + $this->add($key, 0); | |
| 184 | + $result = self::$cache->increment($this->getPrefix() . $key, $step); | |
| 185 | + | |
| 186 | +        if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { | |
| 187 | + return false; | |
| 188 | + } | |
| 189 | + | |
| 190 | + return $result; | |
| 191 | + } | |
| 192 | + | |
| 193 | + /** | |
| 194 | + * Decrease a stored number | |
| 195 | + * | |
| 196 | + * @param string $key | |
| 197 | + * @param int $step | |
| 198 | + * @return int | bool | |
| 199 | + */ | |
| 200 | +    public function dec($key, $step = 1) { | |
| 201 | + $result = self::$cache->decrement($this->getPrefix() . $key, $step); | |
| 202 | + | |
| 203 | +        if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { | |
| 204 | + return false; | |
| 205 | + } | |
| 206 | + | |
| 207 | + return $result; | |
| 208 | + } | |
| 209 | + | |
| 210 | +    static public function isAvailable() { | |
| 211 | +        return extension_loaded('memcached'); | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * @throws \Exception | |
| 216 | + */ | |
| 217 | +    private function verifyReturnCode() { | |
| 218 | + $code = self::$cache->getResultCode(); | |
| 219 | +        if ($code === \Memcached::RES_SUCCESS) { | |
| 220 | + return; | |
| 221 | + } | |
| 222 | + $message = self::$cache->getResultMessage(); | |
| 223 | +        throw new \Exception("Error $code interacting with memcached : $message"); | |
| 224 | + } | |
| 225 | 225 | } | 
| @@ -37,172 +37,172 @@ | ||
| 37 | 37 | */ | 
| 38 | 38 |  class CleanTags implements IRepairStep { | 
| 39 | 39 | |
| 40 | - /** @var IDBConnection */ | |
| 41 | - protected $connection; | |
| 42 | - | |
| 43 | - /** @var IUserManager */ | |
| 44 | - protected $userManager; | |
| 45 | - | |
| 46 | - protected $deletedTags = 0; | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * @param IDBConnection $connection | |
| 50 | - * @param IUserManager $userManager | |
| 51 | - */ | |
| 52 | -	public function __construct(IDBConnection $connection, IUserManager $userManager) { | |
| 53 | - $this->connection = $connection; | |
| 54 | - $this->userManager = $userManager; | |
| 55 | - } | |
| 56 | - | |
| 57 | - /** | |
| 58 | - * @return string | |
| 59 | - */ | |
| 60 | -	public function getName() { | |
| 61 | - return 'Clean tags and favorites'; | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * Updates the configuration after running an update | |
| 66 | - */ | |
| 67 | -	public function run(IOutput $output) { | |
| 68 | - $this->deleteOrphanTags($output); | |
| 69 | - $this->deleteOrphanFileEntries($output); | |
| 70 | - $this->deleteOrphanTagEntries($output); | |
| 71 | - $this->deleteOrphanCategoryEntries($output); | |
| 72 | - } | |
| 73 | - | |
| 74 | - /** | |
| 75 | - * Delete tags for deleted users | |
| 76 | - */ | |
| 77 | -	protected function deleteOrphanTags(IOutput $output) { | |
| 78 | - $offset = 0; | |
| 79 | -		while ($this->checkTags($offset)) { | |
| 80 | - $offset += 50; | |
| 81 | - } | |
| 82 | - | |
| 83 | -		$output->info(sprintf('%d tags of deleted users have been removed.', $this->deletedTags)); | |
| 84 | - } | |
| 85 | - | |
| 86 | -	protected function checkTags($offset) { | |
| 87 | - $query = $this->connection->getQueryBuilder(); | |
| 88 | -		$query->select('uid') | |
| 89 | -			->from('vcategory') | |
| 90 | -			->groupBy('uid') | |
| 91 | -			->orderBy('uid') | |
| 92 | - ->setMaxResults(50) | |
| 93 | - ->setFirstResult($offset); | |
| 94 | - $result = $query->execute(); | |
| 95 | - | |
| 96 | - $users = []; | |
| 97 | - $hadResults = false; | |
| 98 | -		while ($row = $result->fetch()) { | |
| 99 | - $hadResults = true; | |
| 100 | -			if (!$this->userManager->userExists($row['uid'])) { | |
| 101 | - $users[] = $row['uid']; | |
| 102 | - } | |
| 103 | - } | |
| 104 | - $result->closeCursor(); | |
| 105 | - | |
| 106 | -		if (!$hadResults) { | |
| 107 | - // No more tags, stop looping | |
| 108 | - return false; | |
| 109 | - } | |
| 110 | - | |
| 111 | -		if (!empty($users)) { | |
| 112 | - $query = $this->connection->getQueryBuilder(); | |
| 113 | -			$query->delete('vcategory') | |
| 114 | -				->where($query->expr()->in('uid', $query->createNamedParameter($users, IQueryBuilder::PARAM_STR_ARRAY))); | |
| 115 | - $this->deletedTags += $query->execute(); | |
| 116 | - } | |
| 117 | - return true; | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * Delete tag entries for deleted files | |
| 122 | - */ | |
| 123 | -	protected function deleteOrphanFileEntries(IOutput $output) { | |
| 124 | - $this->deleteOrphanEntries( | |
| 125 | - $output, | |
| 126 | - '%d tags for delete files have been removed.', | |
| 127 | - 'vcategory_to_object', 'objid', | |
| 128 | - 'filecache', 'fileid', 'path_hash' | |
| 129 | - ); | |
| 130 | - } | |
| 131 | - | |
| 132 | - /** | |
| 133 | - * Delete tag entries for deleted tags | |
| 134 | - */ | |
| 135 | -	protected function deleteOrphanTagEntries(IOutput $output) { | |
| 136 | - $this->deleteOrphanEntries( | |
| 137 | - $output, | |
| 138 | - '%d tag entries for deleted tags have been removed.', | |
| 139 | - 'vcategory_to_object', 'categoryid', | |
| 140 | - 'vcategory', 'id', 'uid' | |
| 141 | - ); | |
| 142 | - } | |
| 143 | - | |
| 144 | - /** | |
| 145 | - * Delete tags that have no entries | |
| 146 | - */ | |
| 147 | -	protected function deleteOrphanCategoryEntries(IOutput $output) { | |
| 148 | - $this->deleteOrphanEntries( | |
| 149 | - $output, | |
| 150 | - '%d tags with no entries have been removed.', | |
| 151 | - 'vcategory', 'id', | |
| 152 | - 'vcategory_to_object', 'categoryid', 'type' | |
| 153 | - ); | |
| 154 | - } | |
| 155 | - | |
| 156 | - /** | |
| 157 | - * Deletes all entries from $deleteTable that do not have a matching entry in $sourceTable | |
| 158 | - * | |
| 159 | - * A query joins $deleteTable.$deleteId = $sourceTable.$sourceId and checks | |
| 160 | - * whether $sourceNullColumn is null. If it is null, the entry in $deleteTable | |
| 161 | - * is being deleted. | |
| 162 | - * | |
| 163 | - * @param string $repairInfo | |
| 164 | - * @param string $deleteTable | |
| 165 | - * @param string $deleteId | |
| 166 | - * @param string $sourceTable | |
| 167 | - * @param string $sourceId | |
| 168 | - * @param string $sourceNullColumn If this column is null in the source table, | |
| 169 | - * the entry is deleted in the $deleteTable | |
| 170 | - * @suppress SqlInjectionChecker | |
| 171 | - */ | |
| 172 | -	protected function deleteOrphanEntries(IOutput $output, $repairInfo, $deleteTable, $deleteId, $sourceTable, $sourceId, $sourceNullColumn) { | |
| 173 | - $qb = $this->connection->getQueryBuilder(); | |
| 174 | - | |
| 175 | -		$qb->select('d.' . $deleteId) | |
| 176 | - ->from($deleteTable, 'd') | |
| 177 | -			->leftJoin('d', $sourceTable, 's', $qb->expr()->eq('d.' . $deleteId, 's.' . $sourceId)) | |
| 178 | - ->where( | |
| 179 | -				$qb->expr()->eq('d.type', $qb->expr()->literal('files')) | |
| 180 | - ) | |
| 181 | - ->andWhere( | |
| 182 | -				$qb->expr()->isNull('s.' . $sourceNullColumn) | |
| 183 | - ); | |
| 184 | - $result = $qb->execute(); | |
| 185 | - | |
| 186 | - $orphanItems = array(); | |
| 187 | -		while ($row = $result->fetch()) { | |
| 188 | - $orphanItems[] = (int) $row[$deleteId]; | |
| 189 | - } | |
| 190 | - | |
| 191 | -		if (!empty($orphanItems)) { | |
| 192 | - $orphanItemsBatch = array_chunk($orphanItems, 200); | |
| 193 | -			foreach ($orphanItemsBatch as $items) { | |
| 194 | - $qb->delete($deleteTable) | |
| 195 | - ->where( | |
| 196 | -						$qb->expr()->eq('type', $qb->expr()->literal('files')) | |
| 197 | - ) | |
| 198 | -					->andWhere($qb->expr()->in($deleteId, $qb->createParameter('ids'))); | |
| 199 | -				$qb->setParameter('ids', $items, IQueryBuilder::PARAM_INT_ARRAY); | |
| 200 | - $qb->execute(); | |
| 201 | - } | |
| 202 | - } | |
| 203 | - | |
| 204 | -		if ($repairInfo) { | |
| 205 | - $output->info(sprintf($repairInfo, count($orphanItems))); | |
| 206 | - } | |
| 207 | - } | |
| 40 | + /** @var IDBConnection */ | |
| 41 | + protected $connection; | |
| 42 | + | |
| 43 | + /** @var IUserManager */ | |
| 44 | + protected $userManager; | |
| 45 | + | |
| 46 | + protected $deletedTags = 0; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * @param IDBConnection $connection | |
| 50 | + * @param IUserManager $userManager | |
| 51 | + */ | |
| 52 | +    public function __construct(IDBConnection $connection, IUserManager $userManager) { | |
| 53 | + $this->connection = $connection; | |
| 54 | + $this->userManager = $userManager; | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @return string | |
| 59 | + */ | |
| 60 | +    public function getName() { | |
| 61 | + return 'Clean tags and favorites'; | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Updates the configuration after running an update | |
| 66 | + */ | |
| 67 | +    public function run(IOutput $output) { | |
| 68 | + $this->deleteOrphanTags($output); | |
| 69 | + $this->deleteOrphanFileEntries($output); | |
| 70 | + $this->deleteOrphanTagEntries($output); | |
| 71 | + $this->deleteOrphanCategoryEntries($output); | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Delete tags for deleted users | |
| 76 | + */ | |
| 77 | +    protected function deleteOrphanTags(IOutput $output) { | |
| 78 | + $offset = 0; | |
| 79 | +        while ($this->checkTags($offset)) { | |
| 80 | + $offset += 50; | |
| 81 | + } | |
| 82 | + | |
| 83 | +        $output->info(sprintf('%d tags of deleted users have been removed.', $this->deletedTags)); | |
| 84 | + } | |
| 85 | + | |
| 86 | +    protected function checkTags($offset) { | |
| 87 | + $query = $this->connection->getQueryBuilder(); | |
| 88 | +        $query->select('uid') | |
| 89 | +            ->from('vcategory') | |
| 90 | +            ->groupBy('uid') | |
| 91 | +            ->orderBy('uid') | |
| 92 | + ->setMaxResults(50) | |
| 93 | + ->setFirstResult($offset); | |
| 94 | + $result = $query->execute(); | |
| 95 | + | |
| 96 | + $users = []; | |
| 97 | + $hadResults = false; | |
| 98 | +        while ($row = $result->fetch()) { | |
| 99 | + $hadResults = true; | |
| 100 | +            if (!$this->userManager->userExists($row['uid'])) { | |
| 101 | + $users[] = $row['uid']; | |
| 102 | + } | |
| 103 | + } | |
| 104 | + $result->closeCursor(); | |
| 105 | + | |
| 106 | +        if (!$hadResults) { | |
| 107 | + // No more tags, stop looping | |
| 108 | + return false; | |
| 109 | + } | |
| 110 | + | |
| 111 | +        if (!empty($users)) { | |
| 112 | + $query = $this->connection->getQueryBuilder(); | |
| 113 | +            $query->delete('vcategory') | |
| 114 | +                ->where($query->expr()->in('uid', $query->createNamedParameter($users, IQueryBuilder::PARAM_STR_ARRAY))); | |
| 115 | + $this->deletedTags += $query->execute(); | |
| 116 | + } | |
| 117 | + return true; | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Delete tag entries for deleted files | |
| 122 | + */ | |
| 123 | +    protected function deleteOrphanFileEntries(IOutput $output) { | |
| 124 | + $this->deleteOrphanEntries( | |
| 125 | + $output, | |
| 126 | + '%d tags for delete files have been removed.', | |
| 127 | + 'vcategory_to_object', 'objid', | |
| 128 | + 'filecache', 'fileid', 'path_hash' | |
| 129 | + ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * Delete tag entries for deleted tags | |
| 134 | + */ | |
| 135 | +    protected function deleteOrphanTagEntries(IOutput $output) { | |
| 136 | + $this->deleteOrphanEntries( | |
| 137 | + $output, | |
| 138 | + '%d tag entries for deleted tags have been removed.', | |
| 139 | + 'vcategory_to_object', 'categoryid', | |
| 140 | + 'vcategory', 'id', 'uid' | |
| 141 | + ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Delete tags that have no entries | |
| 146 | + */ | |
| 147 | +    protected function deleteOrphanCategoryEntries(IOutput $output) { | |
| 148 | + $this->deleteOrphanEntries( | |
| 149 | + $output, | |
| 150 | + '%d tags with no entries have been removed.', | |
| 151 | + 'vcategory', 'id', | |
| 152 | + 'vcategory_to_object', 'categoryid', 'type' | |
| 153 | + ); | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Deletes all entries from $deleteTable that do not have a matching entry in $sourceTable | |
| 158 | + * | |
| 159 | + * A query joins $deleteTable.$deleteId = $sourceTable.$sourceId and checks | |
| 160 | + * whether $sourceNullColumn is null. If it is null, the entry in $deleteTable | |
| 161 | + * is being deleted. | |
| 162 | + * | |
| 163 | + * @param string $repairInfo | |
| 164 | + * @param string $deleteTable | |
| 165 | + * @param string $deleteId | |
| 166 | + * @param string $sourceTable | |
| 167 | + * @param string $sourceId | |
| 168 | + * @param string $sourceNullColumn If this column is null in the source table, | |
| 169 | + * the entry is deleted in the $deleteTable | |
| 170 | + * @suppress SqlInjectionChecker | |
| 171 | + */ | |
| 172 | +    protected function deleteOrphanEntries(IOutput $output, $repairInfo, $deleteTable, $deleteId, $sourceTable, $sourceId, $sourceNullColumn) { | |
| 173 | + $qb = $this->connection->getQueryBuilder(); | |
| 174 | + | |
| 175 | +        $qb->select('d.' . $deleteId) | |
| 176 | + ->from($deleteTable, 'd') | |
| 177 | +            ->leftJoin('d', $sourceTable, 's', $qb->expr()->eq('d.' . $deleteId, 's.' . $sourceId)) | |
| 178 | + ->where( | |
| 179 | +                $qb->expr()->eq('d.type', $qb->expr()->literal('files')) | |
| 180 | + ) | |
| 181 | + ->andWhere( | |
| 182 | +                $qb->expr()->isNull('s.' . $sourceNullColumn) | |
| 183 | + ); | |
| 184 | + $result = $qb->execute(); | |
| 185 | + | |
| 186 | + $orphanItems = array(); | |
| 187 | +        while ($row = $result->fetch()) { | |
| 188 | + $orphanItems[] = (int) $row[$deleteId]; | |
| 189 | + } | |
| 190 | + | |
| 191 | +        if (!empty($orphanItems)) { | |
| 192 | + $orphanItemsBatch = array_chunk($orphanItems, 200); | |
| 193 | +            foreach ($orphanItemsBatch as $items) { | |
| 194 | + $qb->delete($deleteTable) | |
| 195 | + ->where( | |
| 196 | +                        $qb->expr()->eq('type', $qb->expr()->literal('files')) | |
| 197 | + ) | |
| 198 | +                    ->andWhere($qb->expr()->in($deleteId, $qb->createParameter('ids'))); | |
| 199 | +                $qb->setParameter('ids', $items, IQueryBuilder::PARAM_INT_ARRAY); | |
| 200 | + $qb->execute(); | |
| 201 | + } | |
| 202 | + } | |
| 203 | + | |
| 204 | +        if ($repairInfo) { | |
| 205 | + $output->info(sprintf($repairInfo, count($orphanItems))); | |
| 206 | + } | |
| 207 | + } | |
| 208 | 208 | } | 
| @@ -37,81 +37,81 @@ | ||
| 37 | 37 | */ | 
| 38 | 38 |  class BackgroundRepair extends TimedJob { | 
| 39 | 39 | |
| 40 | - /** @var IJobList */ | |
| 41 | - private $jobList; | |
| 40 | + /** @var IJobList */ | |
| 41 | + private $jobList; | |
| 42 | 42 | |
| 43 | - /** @var ILogger */ | |
| 44 | - private $logger; | |
| 43 | + /** @var ILogger */ | |
| 44 | + private $logger; | |
| 45 | 45 | |
| 46 | - /** @var EventDispatcher */ | |
| 47 | - private $dispatcher; | |
| 46 | + /** @var EventDispatcher */ | |
| 47 | + private $dispatcher; | |
| 48 | 48 | |
| 49 | -	public function setDispatcher(EventDispatcher $dispatcher) { | |
| 50 | - $this->dispatcher = $dispatcher; | |
| 51 | - } | |
| 52 | - /** | |
| 53 | - * run the job, then remove it from the job list | |
| 54 | - * | |
| 55 | - * @param JobList $jobList | |
| 56 | - * @param ILogger|null $logger | |
| 57 | - */ | |
| 58 | -	public function execute($jobList, ILogger $logger = null) { | |
| 59 | - // add an interval of 15 mins | |
| 60 | - $this->setInterval(15*60); | |
| 49 | +    public function setDispatcher(EventDispatcher $dispatcher) { | |
| 50 | + $this->dispatcher = $dispatcher; | |
| 51 | + } | |
| 52 | + /** | |
| 53 | + * run the job, then remove it from the job list | |
| 54 | + * | |
| 55 | + * @param JobList $jobList | |
| 56 | + * @param ILogger|null $logger | |
| 57 | + */ | |
| 58 | +    public function execute($jobList, ILogger $logger = null) { | |
| 59 | + // add an interval of 15 mins | |
| 60 | + $this->setInterval(15*60); | |
| 61 | 61 | |
| 62 | - $this->jobList = $jobList; | |
| 63 | - $this->logger = $logger; | |
| 64 | - parent::execute($jobList, $logger); | |
| 65 | - } | |
| 62 | + $this->jobList = $jobList; | |
| 63 | + $this->logger = $logger; | |
| 64 | + parent::execute($jobList, $logger); | |
| 65 | + } | |
| 66 | 66 | |
| 67 | - /** | |
| 68 | - * @param array $argument | |
| 69 | - * @throws \Exception | |
| 70 | - * @throws \OC\NeedsUpdateException | |
| 71 | - */ | |
| 72 | -	protected function run($argument) { | |
| 73 | -		if (!isset($argument['app']) || !isset($argument['step'])) { | |
| 74 | - // remove the job - we can never execute it | |
| 75 | - $this->jobList->remove($this, $this->argument); | |
| 76 | - return; | |
| 77 | - } | |
| 78 | - $app = $argument['app']; | |
| 67 | + /** | |
| 68 | + * @param array $argument | |
| 69 | + * @throws \Exception | |
| 70 | + * @throws \OC\NeedsUpdateException | |
| 71 | + */ | |
| 72 | +    protected function run($argument) { | |
| 73 | +        if (!isset($argument['app']) || !isset($argument['step'])) { | |
| 74 | + // remove the job - we can never execute it | |
| 75 | + $this->jobList->remove($this, $this->argument); | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + $app = $argument['app']; | |
| 79 | 79 | |
| 80 | -		try { | |
| 81 | - $this->loadApp($app); | |
| 82 | -		} catch (NeedsUpdateException $ex) { | |
| 83 | - // as long as the app is not yet done with it's offline migration | |
| 84 | - // we better not start with the live migration | |
| 85 | - return; | |
| 86 | - } | |
| 80 | +        try { | |
| 81 | + $this->loadApp($app); | |
| 82 | +        } catch (NeedsUpdateException $ex) { | |
| 83 | + // as long as the app is not yet done with it's offline migration | |
| 84 | + // we better not start with the live migration | |
| 85 | + return; | |
| 86 | + } | |
| 87 | 87 | |
| 88 | - $step = $argument['step']; | |
| 89 | - $repair = new Repair([], $this->dispatcher); | |
| 90 | -		try { | |
| 91 | - $repair->addStep($step); | |
| 92 | -		} catch (\Exception $ex) { | |
| 93 | - $this->logger->logException($ex,[ | |
| 94 | - 'app' => 'migration' | |
| 95 | - ]); | |
| 88 | + $step = $argument['step']; | |
| 89 | + $repair = new Repair([], $this->dispatcher); | |
| 90 | +        try { | |
| 91 | + $repair->addStep($step); | |
| 92 | +        } catch (\Exception $ex) { | |
| 93 | + $this->logger->logException($ex,[ | |
| 94 | + 'app' => 'migration' | |
| 95 | + ]); | |
| 96 | 96 | |
| 97 | - // remove the job - we can never execute it | |
| 98 | - $this->jobList->remove($this, $this->argument); | |
| 99 | - return; | |
| 100 | - } | |
| 97 | + // remove the job - we can never execute it | |
| 98 | + $this->jobList->remove($this, $this->argument); | |
| 99 | + return; | |
| 100 | + } | |
| 101 | 101 | |
| 102 | - // execute the repair step | |
| 103 | - $repair->run(); | |
| 102 | + // execute the repair step | |
| 103 | + $repair->run(); | |
| 104 | 104 | |
| 105 | - // remove the job once executed successfully | |
| 106 | - $this->jobList->remove($this, $this->argument); | |
| 107 | - } | |
| 105 | + // remove the job once executed successfully | |
| 106 | + $this->jobList->remove($this, $this->argument); | |
| 107 | + } | |
| 108 | 108 | |
| 109 | - /** | |
| 110 | - * @codeCoverageIgnore | |
| 111 | - * @param $app | |
| 112 | - * @throws NeedsUpdateException | |
| 113 | - */ | |
| 114 | -	protected function loadApp($app) { | |
| 115 | - OC_App::loadApp($app); | |
| 116 | - } | |
| 109 | + /** | |
| 110 | + * @codeCoverageIgnore | |
| 111 | + * @param $app | |
| 112 | + * @throws NeedsUpdateException | |
| 113 | + */ | |
| 114 | +    protected function loadApp($app) { | |
| 115 | + OC_App::loadApp($app); | |
| 116 | + } | |
| 117 | 117 | } |