@@ -11,77 +11,77 @@ |
||
| 11 | 11 | |
| 12 | 12 | class StringUtil |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Return size to human readable filesize |
|
| 16 | - * |
|
| 17 | - * @static |
|
| 18 | - * @param int $filesize in bytes |
|
| 19 | - * @param int $decimals digits |
|
| 20 | - * |
|
| 21 | - * @return string human readable filesize |
|
| 22 | - */ |
|
| 23 | - static function human_filesize($bytes, $decimals = 2) |
|
| 24 | - { |
|
| 25 | - $sz = ' KMGTP'; |
|
| 26 | - $factor = (int)floor((strlen($bytes) - 1) / 3); |
|
| 27 | - return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . " " . @$sz[$factor] . "B"; |
|
| 28 | - } |
|
| 14 | + /** |
|
| 15 | + * Return size to human readable filesize |
|
| 16 | + * |
|
| 17 | + * @static |
|
| 18 | + * @param int $filesize in bytes |
|
| 19 | + * @param int $decimals digits |
|
| 20 | + * |
|
| 21 | + * @return string human readable filesize |
|
| 22 | + */ |
|
| 23 | + static function human_filesize($bytes, $decimals = 2) |
|
| 24 | + { |
|
| 25 | + $sz = ' KMGTP'; |
|
| 26 | + $factor = (int)floor((strlen($bytes) - 1) / 3); |
|
| 27 | + return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . " " . @$sz[$factor] . "B"; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * check if a string starts with a specific char |
|
| 32 | - * |
|
| 33 | - * @static |
|
| 34 | - * @param string $haystack |
|
| 35 | - * @param string $needle |
|
| 36 | - * |
|
| 37 | - * @return boolean true if $needle is found in $haystack |
|
| 38 | - */ |
|
| 39 | - static function startsWith($haystack, $needle) |
|
| 40 | - { |
|
| 41 | - return !strncmp($haystack, $needle, strlen($needle)); |
|
| 42 | - } |
|
| 30 | + /** |
|
| 31 | + * check if a string starts with a specific char |
|
| 32 | + * |
|
| 33 | + * @static |
|
| 34 | + * @param string $haystack |
|
| 35 | + * @param string $needle |
|
| 36 | + * |
|
| 37 | + * @return boolean true if $needle is found in $haystack |
|
| 38 | + */ |
|
| 39 | + static function startsWith($haystack, $needle) |
|
| 40 | + { |
|
| 41 | + return !strncmp($haystack, $needle, strlen($needle)); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Creates a random string |
|
| 46 | - * |
|
| 47 | - * @static |
|
| 48 | - * @param int length The length of the random string |
|
| 49 | - * |
|
| 50 | - * @return string a random string |
|
| 51 | - */ |
|
| 52 | - static function randomstring($length = 6) |
|
| 53 | - { |
|
| 54 | - // $chars - all allowed characters |
|
| 55 | - $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
|
| 44 | + /** |
|
| 45 | + * Creates a random string |
|
| 46 | + * |
|
| 47 | + * @static |
|
| 48 | + * @param int length The length of the random string |
|
| 49 | + * |
|
| 50 | + * @return string a random string |
|
| 51 | + */ |
|
| 52 | + static function randomstring($length = 6) |
|
| 53 | + { |
|
| 54 | + // $chars - all allowed characters |
|
| 55 | + $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
|
| 56 | 56 | |
| 57 | - srand((double)microtime() * 1000000); |
|
| 58 | - $i = 0; |
|
| 59 | - $pass = ""; |
|
| 60 | - while ($i < $length) { |
|
| 61 | - $num = rand() % strlen($chars); |
|
| 62 | - $tmp = substr($chars, $num, 1); |
|
| 63 | - $pass = $pass . $tmp; |
|
| 64 | - $i++; |
|
| 65 | - } |
|
| 66 | - return $pass; |
|
| 67 | - } |
|
| 57 | + srand((double)microtime() * 1000000); |
|
| 58 | + $i = 0; |
|
| 59 | + $pass = ""; |
|
| 60 | + while ($i < $length) { |
|
| 61 | + $num = rand() % strlen($chars); |
|
| 62 | + $tmp = substr($chars, $num, 1); |
|
| 63 | + $pass = $pass . $tmp; |
|
| 64 | + $i++; |
|
| 65 | + } |
|
| 66 | + return $pass; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * check if a string ends with a specific char |
|
| 71 | - * |
|
| 72 | - * @static |
|
| 73 | - * @param string $haystack |
|
| 74 | - * @param string $needle |
|
| 75 | - * |
|
| 76 | - * @return boolean true if $haystack ends with $needle |
|
| 77 | - */ |
|
| 78 | - static function endsWith($haystack, $needle) |
|
| 79 | - { |
|
| 80 | - $length = strlen($needle); |
|
| 81 | - if ($length == 0) { |
|
| 82 | - return true; |
|
| 83 | - } |
|
| 69 | + /** |
|
| 70 | + * check if a string ends with a specific char |
|
| 71 | + * |
|
| 72 | + * @static |
|
| 73 | + * @param string $haystack |
|
| 74 | + * @param string $needle |
|
| 75 | + * |
|
| 76 | + * @return boolean true if $haystack ends with $needle |
|
| 77 | + */ |
|
| 78 | + static function endsWith($haystack, $needle) |
|
| 79 | + { |
|
| 80 | + $length = strlen($needle); |
|
| 81 | + if ($length == 0) { |
|
| 82 | + return true; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - return (substr($haystack, -$length) === $needle); |
|
| 86 | - } |
|
| 85 | + return (substr($haystack, -$length) === $needle); |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | static function human_filesize($bytes, $decimals = 2) |
| 24 | 24 | { |
| 25 | 25 | $sz = ' KMGTP'; |
| 26 | - $factor = (int)floor((strlen($bytes) - 1) / 3); |
|
| 26 | + $factor = (int) floor((strlen($bytes) - 1) / 3); |
|
| 27 | 27 | return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . " " . @$sz[$factor] . "B"; |
| 28 | 28 | } |
| 29 | 29 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | // $chars - all allowed characters |
| 55 | 55 | $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
| 56 | 56 | |
| 57 | - srand((double)microtime() * 1000000); |
|
| 57 | + srand((double) microtime() * 1000000); |
|
| 58 | 58 | $i = 0; |
| 59 | 59 | $pass = ""; |
| 60 | 60 | while ($i < $length) { |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | // if (!mb_detect_encoding($str, "UTF-8", true)) {
|
| 12 | 12 | // $str = utf8_encode($str); |
| 13 | 13 | // } |
| 14 | - return $str; |
|
| 14 | + return $str; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | |
@@ -4,157 +4,157 @@ |
||
| 4 | 4 | |
| 5 | 5 | class ArrayUtil |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Sort multidimensional array by any key |
|
| 9 | - * |
|
| 10 | - * @static |
|
| 11 | - * @param array $arr the array to sort |
|
| 12 | - * @param string $key the key to sort |
|
| 13 | - * @param string $dir ASC or DESC sort direction |
|
| 14 | - * |
|
| 15 | - * @return array the sorted array |
|
| 16 | - */ |
|
| 17 | - static function sort_by_key($arr, $key, $dir) |
|
| 18 | - { |
|
| 19 | - global $key2sort; |
|
| 20 | - $key2sort = $key; |
|
| 21 | - |
|
| 22 | - if ($dir == "DESC") { |
|
| 23 | - usort($arr, array('self', 'invsort')); |
|
| 24 | - } else { |
|
| 25 | - usort($arr, array('self', 'sort')); |
|
| 26 | - } |
|
| 27 | - return ($arr); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Sort multidimensional properties array by any key |
|
| 32 | - * |
|
| 33 | - * @static |
|
| 34 | - * @param array $arr the array to sort |
|
| 35 | - * @param string $key the key to sort |
|
| 36 | - * @param string $dir ASC or DESC sort direction |
|
| 37 | - * |
|
| 38 | - * @return array the sorted array |
|
| 39 | - */ |
|
| 40 | - static function sort_props_by_key($arr, $key, $dir) |
|
| 41 | - { |
|
| 42 | - global $key2sort; |
|
| 43 | - $key2sort = $key; |
|
| 44 | - |
|
| 45 | - if ($dir == "DESC") { |
|
| 46 | - usort($arr, array('self', 'invpropsort')); |
|
| 47 | - } else { |
|
| 48 | - usort($arr, array('self', 'propsort')); |
|
| 49 | - } |
|
| 50 | - return ($arr); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * compare function for multidimensional array sorting |
|
| 55 | - * |
|
| 56 | - * @static |
|
| 57 | - * @param array $a this argument will be compared with argument $b |
|
| 58 | - * @param array $b this argument will be compared with argument $a |
|
| 59 | - * |
|
| 60 | - * @return int compare value. If $a < $b the return value will be -1. |
|
| 61 | - */ |
|
| 62 | - static function sort($a, $b) |
|
| 63 | - { |
|
| 64 | - global $key2sort; |
|
| 65 | - |
|
| 66 | - if ($a['isFolder'] == $b['isFolder']) { |
|
| 67 | - if (is_numeric($a[$key2sort]) && is_numeric($b[$key2sort])) { |
|
| 68 | - if ($a[$key2sort] == $b[$key2sort]) { |
|
| 69 | - return 0; |
|
| 70 | - } |
|
| 71 | - return ($a[$key2sort] < $b[$key2sort]) ? -1 : 1; |
|
| 72 | - } else { |
|
| 73 | - return (strcasecmp($a[$key2sort], $b[$key2sort])); |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - return ((int)$a['isFolder'] - (int)$b['isFolder']); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * inverse compare function for multidimensional array sorting |
|
| 82 | - * |
|
| 83 | - * @static |
|
| 84 | - * @param array $a this argument will be compared with argument $b |
|
| 85 | - * @param array $b this argument will be compared with argument $a |
|
| 86 | - * |
|
| 87 | - * @return int compare value. If $a < $b the return value will be 1. |
|
| 88 | - */ |
|
| 89 | - static function invsort($a, $b) |
|
| 90 | - { |
|
| 91 | - global $key2sort; |
|
| 92 | - |
|
| 93 | - if ($a['isFolder'] == $b['isFolder']) { |
|
| 94 | - if (is_numeric($a[$key2sort]) && is_numeric($b[$key2sort])) { |
|
| 95 | - if ($a[$key2sort] == $b[$key2sort]) { |
|
| 96 | - return 0; |
|
| 97 | - } |
|
| 98 | - return ($a[$key2sort] < $b[$key2sort]) ? 1 : -1; |
|
| 99 | - } else { |
|
| 100 | - return (-1 * strcasecmp($a[$key2sort], $b[$key2sort])); |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return ((int)$b['isFolder'] - (int)$a['isFolder']); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * compare function for multidimensional array sorting |
|
| 109 | - * |
|
| 110 | - * @static |
|
| 111 | - * @param array $a this argument will be compared with argument $b |
|
| 112 | - * @param array $b this argument will be compared with argument $a |
|
| 113 | - * |
|
| 114 | - * @return int compare value. If $a < $b the return value will be -1. |
|
| 115 | - */ |
|
| 116 | - static function propsort($a, $b) |
|
| 117 | - { |
|
| 118 | - global $key2sort; |
|
| 119 | - |
|
| 120 | - if ($a['props']['type'] == $b['props']['type']) { |
|
| 121 | - if (is_numeric($a['props'][$key2sort]) && is_numeric($b['props'][$key2sort])) { |
|
| 122 | - if ($a['props'][$key2sort] == $b['props'][$key2sort]) { |
|
| 123 | - return 0; |
|
| 124 | - } |
|
| 125 | - return ($a['props'][$key2sort] < $b['props'][$key2sort]) ? -1 : 1; |
|
| 126 | - } else { |
|
| 127 | - return (strcasecmp($a['props'][$key2sort], $b['props'][$key2sort])); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return ($a['props']['type'] - $b['props']['type']); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * inverse compare function for multidimensional array sorting |
|
| 136 | - * |
|
| 137 | - * @static |
|
| 138 | - * @param array $a this argument will be compared with argument $b |
|
| 139 | - * @param array $b this argument will be compared with argument $a |
|
| 140 | - * |
|
| 141 | - * @return int compare value. If $a < $b the return value will be 1. |
|
| 142 | - */ |
|
| 143 | - static function invpropsort($a, $b) |
|
| 144 | - { |
|
| 145 | - global $key2sort; |
|
| 146 | - |
|
| 147 | - if ($a['props']['type'] == $b['props']['type']) { |
|
| 148 | - if (is_numeric($a['props'][$key2sort]) && is_numeric($b['props'][$key2sort])) { |
|
| 149 | - if ($a['props'][$key2sort] == $b['props'][$key2sort]) { |
|
| 150 | - return 0; |
|
| 151 | - } |
|
| 152 | - return ($a['props'][$key2sort] < $b['props'][$key2sort]) ? 1 : -1; |
|
| 153 | - } else { |
|
| 154 | - return (-1 * strcasecmp($a['props'][$key2sort], $b['props'][$key2sort])); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return ($b['props']['type'] - $a['props']['type']); |
|
| 159 | - } |
|
| 7 | + /** |
|
| 8 | + * Sort multidimensional array by any key |
|
| 9 | + * |
|
| 10 | + * @static |
|
| 11 | + * @param array $arr the array to sort |
|
| 12 | + * @param string $key the key to sort |
|
| 13 | + * @param string $dir ASC or DESC sort direction |
|
| 14 | + * |
|
| 15 | + * @return array the sorted array |
|
| 16 | + */ |
|
| 17 | + static function sort_by_key($arr, $key, $dir) |
|
| 18 | + { |
|
| 19 | + global $key2sort; |
|
| 20 | + $key2sort = $key; |
|
| 21 | + |
|
| 22 | + if ($dir == "DESC") { |
|
| 23 | + usort($arr, array('self', 'invsort')); |
|
| 24 | + } else { |
|
| 25 | + usort($arr, array('self', 'sort')); |
|
| 26 | + } |
|
| 27 | + return ($arr); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Sort multidimensional properties array by any key |
|
| 32 | + * |
|
| 33 | + * @static |
|
| 34 | + * @param array $arr the array to sort |
|
| 35 | + * @param string $key the key to sort |
|
| 36 | + * @param string $dir ASC or DESC sort direction |
|
| 37 | + * |
|
| 38 | + * @return array the sorted array |
|
| 39 | + */ |
|
| 40 | + static function sort_props_by_key($arr, $key, $dir) |
|
| 41 | + { |
|
| 42 | + global $key2sort; |
|
| 43 | + $key2sort = $key; |
|
| 44 | + |
|
| 45 | + if ($dir == "DESC") { |
|
| 46 | + usort($arr, array('self', 'invpropsort')); |
|
| 47 | + } else { |
|
| 48 | + usort($arr, array('self', 'propsort')); |
|
| 49 | + } |
|
| 50 | + return ($arr); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * compare function for multidimensional array sorting |
|
| 55 | + * |
|
| 56 | + * @static |
|
| 57 | + * @param array $a this argument will be compared with argument $b |
|
| 58 | + * @param array $b this argument will be compared with argument $a |
|
| 59 | + * |
|
| 60 | + * @return int compare value. If $a < $b the return value will be -1. |
|
| 61 | + */ |
|
| 62 | + static function sort($a, $b) |
|
| 63 | + { |
|
| 64 | + global $key2sort; |
|
| 65 | + |
|
| 66 | + if ($a['isFolder'] == $b['isFolder']) { |
|
| 67 | + if (is_numeric($a[$key2sort]) && is_numeric($b[$key2sort])) { |
|
| 68 | + if ($a[$key2sort] == $b[$key2sort]) { |
|
| 69 | + return 0; |
|
| 70 | + } |
|
| 71 | + return ($a[$key2sort] < $b[$key2sort]) ? -1 : 1; |
|
| 72 | + } else { |
|
| 73 | + return (strcasecmp($a[$key2sort], $b[$key2sort])); |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + return ((int)$a['isFolder'] - (int)$b['isFolder']); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * inverse compare function for multidimensional array sorting |
|
| 82 | + * |
|
| 83 | + * @static |
|
| 84 | + * @param array $a this argument will be compared with argument $b |
|
| 85 | + * @param array $b this argument will be compared with argument $a |
|
| 86 | + * |
|
| 87 | + * @return int compare value. If $a < $b the return value will be 1. |
|
| 88 | + */ |
|
| 89 | + static function invsort($a, $b) |
|
| 90 | + { |
|
| 91 | + global $key2sort; |
|
| 92 | + |
|
| 93 | + if ($a['isFolder'] == $b['isFolder']) { |
|
| 94 | + if (is_numeric($a[$key2sort]) && is_numeric($b[$key2sort])) { |
|
| 95 | + if ($a[$key2sort] == $b[$key2sort]) { |
|
| 96 | + return 0; |
|
| 97 | + } |
|
| 98 | + return ($a[$key2sort] < $b[$key2sort]) ? 1 : -1; |
|
| 99 | + } else { |
|
| 100 | + return (-1 * strcasecmp($a[$key2sort], $b[$key2sort])); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return ((int)$b['isFolder'] - (int)$a['isFolder']); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * compare function for multidimensional array sorting |
|
| 109 | + * |
|
| 110 | + * @static |
|
| 111 | + * @param array $a this argument will be compared with argument $b |
|
| 112 | + * @param array $b this argument will be compared with argument $a |
|
| 113 | + * |
|
| 114 | + * @return int compare value. If $a < $b the return value will be -1. |
|
| 115 | + */ |
|
| 116 | + static function propsort($a, $b) |
|
| 117 | + { |
|
| 118 | + global $key2sort; |
|
| 119 | + |
|
| 120 | + if ($a['props']['type'] == $b['props']['type']) { |
|
| 121 | + if (is_numeric($a['props'][$key2sort]) && is_numeric($b['props'][$key2sort])) { |
|
| 122 | + if ($a['props'][$key2sort] == $b['props'][$key2sort]) { |
|
| 123 | + return 0; |
|
| 124 | + } |
|
| 125 | + return ($a['props'][$key2sort] < $b['props'][$key2sort]) ? -1 : 1; |
|
| 126 | + } else { |
|
| 127 | + return (strcasecmp($a['props'][$key2sort], $b['props'][$key2sort])); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return ($a['props']['type'] - $b['props']['type']); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * inverse compare function for multidimensional array sorting |
|
| 136 | + * |
|
| 137 | + * @static |
|
| 138 | + * @param array $a this argument will be compared with argument $b |
|
| 139 | + * @param array $b this argument will be compared with argument $a |
|
| 140 | + * |
|
| 141 | + * @return int compare value. If $a < $b the return value will be 1. |
|
| 142 | + */ |
|
| 143 | + static function invpropsort($a, $b) |
|
| 144 | + { |
|
| 145 | + global $key2sort; |
|
| 146 | + |
|
| 147 | + if ($a['props']['type'] == $b['props']['type']) { |
|
| 148 | + if (is_numeric($a['props'][$key2sort]) && is_numeric($b['props'][$key2sort])) { |
|
| 149 | + if ($a['props'][$key2sort] == $b['props'][$key2sort]) { |
|
| 150 | + return 0; |
|
| 151 | + } |
|
| 152 | + return ($a['props'][$key2sort] < $b['props'][$key2sort]) ? 1 : -1; |
|
| 153 | + } else { |
|
| 154 | + return (-1 * strcasecmp($a['props'][$key2sort], $b['props'][$key2sort])); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return ($b['props']['type'] - $a['props']['type']); |
|
| 159 | + } |
|
| 160 | 160 | } |
| 161 | 161 | \ No newline at end of file |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - return ((int)$a['isFolder'] - (int)$b['isFolder']); |
|
| 77 | + return ((int) $a['isFolder'] - (int) $b['isFolder']); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - return ((int)$b['isFolder'] - (int)$a['isFolder']); |
|
| 104 | + return ((int) $b['isFolder'] - (int) $a['isFolder']); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -4,24 +4,24 @@ |
||
| 4 | 4 | // TODO: implement logging levels |
| 5 | 5 | class Logger |
| 6 | 6 | { |
| 7 | - public static function log($context, $msg) |
|
| 8 | - { |
|
| 9 | - if (PLUGIN_FILESBROWSER_LOGLEVEL === "DEBUG" || PLUGIN_FILESBROWSER_LOGLEVEL === "NORMAL") { |
|
| 10 | - error_log("[INFO][$context] " . print_r($msg, true)); |
|
| 11 | - } |
|
| 12 | - } |
|
| 7 | + public static function log($context, $msg) |
|
| 8 | + { |
|
| 9 | + if (PLUGIN_FILESBROWSER_LOGLEVEL === "DEBUG" || PLUGIN_FILESBROWSER_LOGLEVEL === "NORMAL") { |
|
| 10 | + error_log("[INFO][$context] " . print_r($msg, true)); |
|
| 11 | + } |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - public static function error($context, $msg) |
|
| 15 | - { |
|
| 16 | - if (PLUGIN_FILESBROWSER_LOGLEVEL === "ERROR" || PLUGIN_FILESBROWSER_LOGLEVEL === "DEBUG" || PLUGIN_FILESBROWSER_LOGLEVEL === "NORMAL") { |
|
| 17 | - error_log("[ERROR][$context] " . print_r($msg, true)); |
|
| 18 | - } |
|
| 19 | - } |
|
| 14 | + public static function error($context, $msg) |
|
| 15 | + { |
|
| 16 | + if (PLUGIN_FILESBROWSER_LOGLEVEL === "ERROR" || PLUGIN_FILESBROWSER_LOGLEVEL === "DEBUG" || PLUGIN_FILESBROWSER_LOGLEVEL === "NORMAL") { |
|
| 17 | + error_log("[ERROR][$context] " . print_r($msg, true)); |
|
| 18 | + } |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public static function debug($context, $msg) |
|
| 22 | - { |
|
| 23 | - if (PLUGIN_FILESBROWSER_LOGLEVEL === "DEBUG") { |
|
| 24 | - error_log("[DBG][$context] " . print_r($msg, true)); |
|
| 25 | - } |
|
| 26 | - } |
|
| 21 | + public static function debug($context, $msg) |
|
| 22 | + { |
|
| 23 | + if (PLUGIN_FILESBROWSER_LOGLEVEL === "DEBUG") { |
|
| 24 | + error_log("[DBG][$context] " . print_r($msg, true)); |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | 27 | } |
| 28 | 28 | \ No newline at end of file |
@@ -5,37 +5,37 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Exception extends \Exception |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * The exception title to show as a message box title at client side. |
|
| 10 | - */ |
|
| 11 | - public $title = null; |
|
| 8 | + /** |
|
| 9 | + * The exception title to show as a message box title at client side. |
|
| 10 | + */ |
|
| 11 | + public $title = null; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @constructor |
|
| 15 | - * @param string $message The error message |
|
| 16 | - * @param int $code The error code |
|
| 17 | - */ |
|
| 18 | - public function __construct($message, $code = 0) |
|
| 19 | - { |
|
| 20 | - parent::__construct($message, $code); |
|
| 21 | - } |
|
| 13 | + /** |
|
| 14 | + * @constructor |
|
| 15 | + * @param string $message The error message |
|
| 16 | + * @param int $code The error code |
|
| 17 | + */ |
|
| 18 | + public function __construct($message, $code = 0) |
|
| 19 | + { |
|
| 20 | + parent::__construct($message, $code); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Function sets title of an exception that will be sent to the client side |
|
| 25 | - * to show it to user. |
|
| 26 | - * @param string $title title of an exception. |
|
| 27 | - */ |
|
| 28 | - public function setTitle($title) |
|
| 29 | - { |
|
| 30 | - $this->title = $title; |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Function sets title of an exception that will be sent to the client side |
|
| 25 | + * to show it to user. |
|
| 26 | + * @param string $title title of an exception. |
|
| 27 | + */ |
|
| 28 | + public function setTitle($title) |
|
| 29 | + { |
|
| 30 | + $this->title = $title; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @return string returns title that should be sent to client to display as a message box |
|
| 35 | - * title. |
|
| 36 | - */ |
|
| 37 | - public function getTitle() |
|
| 38 | - { |
|
| 39 | - return $this->title; |
|
| 40 | - } |
|
| 33 | + /** |
|
| 34 | + * @return string returns title that should be sent to client to display as a message box |
|
| 35 | + * title. |
|
| 36 | + */ |
|
| 37 | + public function getTitle() |
|
| 38 | + { |
|
| 39 | + return $this->title; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -13,40 +13,40 @@ |
||
| 13 | 13 | |
| 14 | 14 | class RecipientHandler |
| 15 | 15 | { |
| 16 | - const LOG_CONTEXT = "RecipientHandler"; // Context for the Logger |
|
| 17 | - |
|
| 18 | - public static function doGetRecipients() |
|
| 19 | - { |
|
| 20 | - // parse account id. |
|
| 21 | - // wo only need to parse one string because it is |
|
| 22 | - // only possible to download files from one backend at a time. |
|
| 23 | - if (isset($_GET["ids"])) { |
|
| 24 | - $tmpId = $_GET["ids"][0]; |
|
| 25 | - } else { |
|
| 26 | - $tmpId = $_GET["id"]; |
|
| 27 | - } |
|
| 28 | - $accountID = substr($tmpId, 3, (strpos($tmpId, '/') - 3)); |
|
| 29 | - |
|
| 30 | - // Initialize the account and backendstore |
|
| 31 | - $accountStore = new \Files\Core\AccountStore(); |
|
| 32 | - $backendStore = \Files\Backend\BackendStore::getInstance(); |
|
| 33 | - |
|
| 34 | - $account = $accountStore->getAccount($accountID); |
|
| 35 | - |
|
| 36 | - // initialize the backend |
|
| 37 | - $initializedBackend = $backendStore->getInstanceOfBackend($account->getBackend()); |
|
| 38 | - $initializedBackend->init_backend($account->getBackendConfig()); |
|
| 39 | - |
|
| 40 | - try { |
|
| 41 | - $initializedBackend->open(); |
|
| 42 | - } catch (\Files\Backend\Exception $e) { |
|
| 43 | - Logger::error(self::LOG_CONTEXT, "Could not open the backend: " . $e->getMessage()); |
|
| 16 | + const LOG_CONTEXT = "RecipientHandler"; // Context for the Logger |
|
| 17 | + |
|
| 18 | + public static function doGetRecipients() |
|
| 19 | + { |
|
| 20 | + // parse account id. |
|
| 21 | + // wo only need to parse one string because it is |
|
| 22 | + // only possible to download files from one backend at a time. |
|
| 23 | + if (isset($_GET["ids"])) { |
|
| 24 | + $tmpId = $_GET["ids"][0]; |
|
| 25 | + } else { |
|
| 26 | + $tmpId = $_GET["id"]; |
|
| 27 | + } |
|
| 28 | + $accountID = substr($tmpId, 3, (strpos($tmpId, '/') - 3)); |
|
| 29 | + |
|
| 30 | + // Initialize the account and backendstore |
|
| 31 | + $accountStore = new \Files\Core\AccountStore(); |
|
| 32 | + $backendStore = \Files\Backend\BackendStore::getInstance(); |
|
| 33 | + |
|
| 34 | + $account = $accountStore->getAccount($accountID); |
|
| 35 | + |
|
| 36 | + // initialize the backend |
|
| 37 | + $initializedBackend = $backendStore->getInstanceOfBackend($account->getBackend()); |
|
| 38 | + $initializedBackend->init_backend($account->getBackendConfig()); |
|
| 39 | + |
|
| 40 | + try { |
|
| 41 | + $initializedBackend->open(); |
|
| 42 | + } catch (\Files\Backend\Exception $e) { |
|
| 43 | + Logger::error(self::LOG_CONTEXT, "Could not open the backend: " . $e->getMessage()); |
|
| 44 | 44 | echo json_encode(array('success' => false, 'response' => $e->getCode(), 'message' => $e->getMessage())); |
| 45 | - die(); |
|
| 46 | - } |
|
| 47 | - $responsedata = $initializedBackend->getRecipients($_GET["query"]); |
|
| 48 | - header('Content-Type: application/json'); |
|
| 49 | - echo json_encode($responsedata); |
|
| 50 | - } |
|
| 45 | + die(); |
|
| 46 | + } |
|
| 47 | + $responsedata = $initializedBackend->getRecipients($_GET["query"]); |
|
| 48 | + header('Content-Type: application/json'); |
|
| 49 | + echo json_encode($responsedata); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | 52 | } |
@@ -18,369 +18,369 @@ |
||
| 18 | 18 | |
| 19 | 19 | class AccountStore |
| 20 | 20 | { |
| 21 | - const LOG_CONTEXT = "AccountStore"; // Context for the Logger |
|
| 22 | - const ACCOUNT_STORAGE_PATH = "zarafa/v1/plugins/files/accounts"; |
|
| 23 | - const ACCOUNT_VERSION = 1; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var Account[] Account array |
|
| 27 | - */ |
|
| 28 | - private $accounts = []; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * |
|
| 32 | - */ |
|
| 33 | - function __construct() |
|
| 34 | - { |
|
| 35 | - $this->initialiseAccounts(); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @param $name |
|
| 40 | - * @param $backend |
|
| 41 | - * @param Array $backendConfig Backend specific account settings |
|
| 42 | - * like username, password, serveraddress, ... |
|
| 43 | - * |
|
| 44 | - * @return Account |
|
| 45 | - */ |
|
| 46 | - public function createAccount($name, $backend, $backendConfig) |
|
| 47 | - { |
|
| 48 | - $newID = $this->createNewId($backendConfig); // create id out of the configuration |
|
| 49 | - |
|
| 50 | - // create instance of backend to get features |
|
| 51 | - $backendStore = BackendStore::getInstance(); |
|
| 52 | - $backendInstance = $backendStore->getInstanceOfBackend($backend); |
|
| 53 | - $features = $backendInstance->getAvailableFeatures(); |
|
| 54 | - |
|
| 55 | - // check backend_config for validity |
|
| 56 | - $status = $this->checkBackendConfig($backendInstance, $backendConfig); |
|
| 57 | - |
|
| 58 | - // get sequence number |
|
| 59 | - $sequence = $this->getNewSequenceNumber(); |
|
| 60 | - |
|
| 61 | - $newAccount = new Account($newID, strip_tags($name), $status[0], $status[1], strip_tags($backend), $backendConfig, $features, $sequence, false); |
|
| 62 | - |
|
| 63 | - // now store all the values to the user settings |
|
| 64 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/id", $newAccount->getId()); |
|
| 65 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/name", $newAccount->getName()); |
|
| 66 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/status", $newAccount->getStatus()); |
|
| 67 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/status_description", $newAccount->getStatusDescription()); |
|
| 68 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/backend", $newAccount->getBackend()); |
|
| 69 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/account_sequence", $newAccount->getSequence()); |
|
| 70 | - // User defined accounts are never administrative. So set cannot_change to false. |
|
| 71 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/cannot_change", false); |
|
| 72 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/backend_config/version", self::ACCOUNT_VERSION); |
|
| 73 | - // store all backend configurations |
|
| 74 | - foreach ($newAccount->getBackendConfig() as $key => $value) { |
|
| 75 | - if ($key !== "version") { |
|
| 76 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/backend_config/" . $key, $this->encryptBackendConfigProperty($value, self::ACCOUNT_VERSION)); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // store all features |
|
| 81 | - foreach ($newAccount->getFeatures() as $feature) { |
|
| 82 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/backend_features/" . $feature, true); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $GLOBALS["settings"]->saveSettings(); // save to MAPI storage |
|
| 86 | - |
|
| 87 | - // add account to our local store after it was saved to the zarafa-settings |
|
| 88 | - $this->accounts[$newID] = $newAccount; |
|
| 89 | - |
|
| 90 | - return $newAccount; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param Account $account |
|
| 95 | - * |
|
| 96 | - * @return Account |
|
| 97 | - */ |
|
| 98 | - public function updateAccount($account) |
|
| 99 | - { |
|
| 100 | - $accId = $account->getId(); |
|
| 101 | - $isAdministrativeAccount = $account->getCannotChangeFlag(); |
|
| 102 | - |
|
| 103 | - // create instance of backend to get features |
|
| 104 | - $backendStore = BackendStore::getInstance(); |
|
| 105 | - $backendInstance = $backendStore->getInstanceOfBackend($account->getBackend()); |
|
| 106 | - $features = $backendInstance->getAvailableFeatures(); |
|
| 107 | - $account->setFeatures($features); |
|
| 108 | - |
|
| 109 | - // check backend_config for validity |
|
| 110 | - $status = $this->checkBackendConfig($backendInstance, $account->getBackendConfig()); |
|
| 111 | - $account->setStatus($status[0]); // update status |
|
| 112 | - $account->setStatusDescription($status[1]); // update status description |
|
| 113 | - |
|
| 114 | - // add account to local store |
|
| 115 | - $this->accounts[$accId] = $account; |
|
| 116 | - |
|
| 117 | - // save values to MAPI settings |
|
| 118 | - // now store all the values to the user settings |
|
| 119 | - // but if we have an administrative account only save the account sequence |
|
| 120 | - if (!$isAdministrativeAccount) { |
|
| 121 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/name", $account->getName()); |
|
| 122 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/status", $account->getStatus()); |
|
| 123 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/status_description", $account->getStatusDescription()); |
|
| 124 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/backend", $account->getBackend()); |
|
| 125 | - |
|
| 126 | - $acc = $account->getBackendConfig(); |
|
| 127 | - $version = 0; |
|
| 128 | - if (isset($acc["version"])) { |
|
| 129 | - $version = $acc["version"]; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Unable to decrypt, don't update |
|
| 133 | - if ($version == 0 && !defined('FILES_PASSWORD_IV') && !defined('FILES_PASSWORD_KEY')) { |
|
| 134 | - Logger::error(self::LOG_CONTEXT, "Unable to update the account to as FILES_PASSWORD_IV/FILES_PASSWORD_KEY is not set"); |
|
| 135 | - } else { |
|
| 136 | - // store all backend configurations |
|
| 137 | - foreach ($account->getBackendConfig() as $key => $value) { |
|
| 138 | - if ($key !== "version") { |
|
| 139 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/backend_config/" . $key, $this->encryptBackendConfigProperty($value, self::ACCOUNT_VERSION)); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/backend_config/version", self::ACCOUNT_VERSION); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // store all features |
|
| 147 | - foreach ($account->getFeatures() as $feature) { |
|
| 148 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/backend_features/" . $feature, true); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - // when getSequence returns 0, there is no account_sequence setting yet. So create one. |
|
| 152 | - $account_sequence = ($account->getSequence() === 0 ? $this->getNewSequenceNumber() : $account->getSequence()); |
|
| 153 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/account_sequence", $account_sequence); |
|
| 154 | - |
|
| 155 | - $GLOBALS["settings"]->saveSettings(); // save to MAPI storage |
|
| 156 | - |
|
| 157 | - return $account; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Delete account from local store and from the MAPI settings |
|
| 162 | - * |
|
| 163 | - * @param $accountId |
|
| 164 | - * |
|
| 165 | - * @return bool |
|
| 166 | - */ |
|
| 167 | - public function deleteAccount($accountId) |
|
| 168 | - { |
|
| 169 | - $account = $this->getAccount($accountId); |
|
| 170 | - // Do not allow deleting administrative accounts, but fail silently. |
|
| 171 | - if (!$account->getCannotChangeFlag()) { |
|
| 172 | - $GLOBALS["settings"]->delete(self::ACCOUNT_STORAGE_PATH . "/" . $accountId); |
|
| 173 | - $GLOBALS["settings"]->saveSettings(); // save to MAPI storage |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return true; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Return the instance of the local account |
|
| 181 | - * |
|
| 182 | - * @param $accountId |
|
| 183 | - * |
|
| 184 | - * @return Account |
|
| 185 | - */ |
|
| 186 | - public function getAccount($accountId) |
|
| 187 | - { |
|
| 188 | - return $this->accounts[$accountId]; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @return Account[] all Accounts |
|
| 193 | - */ |
|
| 194 | - public function getAllAccounts() |
|
| 195 | - { |
|
| 196 | - return $this->accounts; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Initialize the accountstore. Reads all accountinformation from the MAPI settings. |
|
| 201 | - */ |
|
| 202 | - private function initialiseAccounts() |
|
| 203 | - { |
|
| 204 | - // Parse accounts from the Settings |
|
| 205 | - $tmpAccs = $GLOBALS["settings"]->get(self::ACCOUNT_STORAGE_PATH); |
|
| 206 | - |
|
| 207 | - if (is_array($tmpAccs)) { |
|
| 208 | - $this->accounts = array(); |
|
| 209 | - |
|
| 210 | - foreach ($tmpAccs as $acc) { |
|
| 211 | - // set backend_features if it is not set to prevent warning |
|
| 212 | - if (!isset($acc["backend_features"])) { |
|
| 213 | - $acc["backend_features"] = array(); |
|
| 214 | - } |
|
| 215 | - // account_sequence was introduced later. So set and save it if missing. |
|
| 216 | - if (!isset($acc["account_sequence"])) { |
|
| 217 | - $acc["account_sequence"] = $this->getNewSequenceNumber(); |
|
| 218 | - Logger::debug(self::LOG_CONTEXT, "Account sequence missing. New seq: " . $acc["account_sequence"]); |
|
| 219 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $acc["id"] . "/account_sequence", $acc["account_sequence"]); |
|
| 220 | - $GLOBALS["settings"]->saveSettings(); |
|
| 221 | - } |
|
| 222 | - // cannot_change flag was introduced later. So set it to false and save it if missing. |
|
| 223 | - if (!isset($acc["cannot_change"])) { |
|
| 224 | - $acc["cannot_change"] = false; |
|
| 225 | - Logger::debug(self::LOG_CONTEXT, "Cannot change flag missing. Setting to false."); |
|
| 226 | - $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $acc["id"] . "/cannot_change", false); |
|
| 227 | - $GLOBALS["settings"]->saveSettings(); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $backend_config = $acc["backend_config"]; |
|
| 231 | - $version = 0; |
|
| 232 | - |
|
| 233 | - if (isset($acc["backend_config"]) && isset($acc["backend_config"]["version"])) { |
|
| 234 | - $version = $acc["backend_config"]["version"]; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - if (($version === 0 && defined('FILES_PASSWORD_IV') && defined('FILES_PASSWORD_KEY')) || $version === self::ACCOUNT_VERSION) { |
|
| 238 | - $backend_config = $this->decryptBackendConfig($acc["backend_config"], $version); |
|
| 239 | - // version is lost after decryption, add it again |
|
| 240 | - $backend_config["version"] = $version; |
|
| 241 | - } else if ($version === 0) { |
|
| 242 | - Logger::error(self::LOG_CONTEXT, "FILES_PASSWORD_IV or FILES_PASSWORD_KEY not set, unable to decrypt backend configuration"); |
|
| 243 | - } else { |
|
| 244 | - Logger::error(self::LOG_CONTEXT, "Unsupported account version $version, unable to decrypt backend configuration"); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $this->accounts[$acc["id"]] = new Account($acc["id"], |
|
| 248 | - $acc["name"], |
|
| 249 | - $acc["status"], |
|
| 250 | - $acc["status_description"], |
|
| 251 | - $acc["backend"], |
|
| 252 | - $backend_config, |
|
| 253 | - array_keys($acc["backend_features"]), |
|
| 254 | - $acc["account_sequence"], |
|
| 255 | - $acc["cannot_change"] |
|
| 256 | - ); |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - Logger::debug(self::LOG_CONTEXT, "Found " . count($this->accounts) . " accounts."); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * @param AbstractBackend $backendInstance |
|
| 264 | - * @param Array $backendConfig Backend specific account settings |
|
| 265 | - * like username, password, serveraddress, ... |
|
| 266 | - * |
|
| 267 | - * @return Array |
|
| 268 | - */ |
|
| 269 | - private function checkBackendConfig($backendInstance, $backendConfig) |
|
| 270 | - { |
|
| 271 | - $status = Account::STATUS_NEW; |
|
| 272 | - $description = _('Account is ready to use.'); |
|
| 273 | - try { |
|
| 274 | - $backendInstance->init_backend($backendConfig); |
|
| 275 | - $backendInstance->open(); |
|
| 276 | - $backendInstance->ls("/"); |
|
| 277 | - $status = Account::STATUS_OK; |
|
| 278 | - } catch (BackendException $e) { |
|
| 279 | - $status = Account::STATUS_ERROR; |
|
| 280 | - $description = $e->getMessage(); |
|
| 281 | - |
|
| 282 | - Logger::error(self::LOG_CONTEXT, "Account check failed: " . $description); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - return array($status, $description); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @param Array $backendConfig Backend specific account settings |
|
| 290 | - * like username, password, serveraddress, ... |
|
| 291 | - * |
|
| 292 | - * @return an unique id |
|
| 293 | - */ |
|
| 294 | - private function createNewId($backendConfig) |
|
| 295 | - { |
|
| 296 | - // lets create a hash |
|
| 297 | - return md5(json_encode($backendConfig) . time()); // json_encode is faster than serialize |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Generate a new sequence number. It will always be the highest used sequence number +1. |
|
| 302 | - * |
|
| 303 | - * @return int |
|
| 304 | - */ |
|
| 305 | - private function getNewSequenceNumber() { |
|
| 306 | - $seq = 0; |
|
| 307 | - foreach($this->accounts as $acc) { |
|
| 308 | - if($acc->getSequence() > $seq) { |
|
| 309 | - $seq = $acc->getSequence(); |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - return $seq + 1; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Decrypt the backend configuration using the standard grommunio Web key. |
|
| 318 | - * |
|
| 319 | - * @param Array $backendConfig Backend specific account settings |
|
| 320 | - * like username, password, serveraddress, ... |
|
| 321 | - * @return array |
|
| 322 | - */ |
|
| 323 | - private function decryptBackendConfig($backendConfig, $version=0) { |
|
| 324 | - $decBackendConfig = array(); |
|
| 325 | - |
|
| 326 | - foreach($backendConfig as $key => $value) { |
|
| 327 | - if ($key !== "version") { |
|
| 328 | - try { |
|
| 329 | - $decBackendConfig[$key] = $this->decryptBackendConfigProperty($value, $version); |
|
| 330 | - } catch (Exception $e) { |
|
| 331 | - Logger::error(self::LOG_CONTEXT, sprintf("Unable to decrypt backend configuration: '%s'", $e->getMessage())); |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - return $decBackendConfig; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Encrypt the given string. |
|
| 341 | - * |
|
| 342 | - * @param $value |
|
| 343 | - * @param $version the storage version used to identify what encryption to use |
|
| 344 | - * @return string |
|
| 345 | - */ |
|
| 346 | - private function encryptBackendConfigProperty($value, $version=0) { |
|
| 347 | - if ($version == self::ACCOUNT_VERSION && !is_bool($value)) { |
|
| 348 | - $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
|
| 349 | - $key = $GLOBALS["operations"]->getFilesEncryptionKey(); |
|
| 350 | - $encrypted = sodium_crypto_secretbox($value, $nonce, $key); |
|
| 351 | - $value = bin2hex($nonce) . bin2hex($encrypted); |
|
| 352 | - } else if ($version !== self::ACCOUNT_VERSION) { |
|
| 353 | - throw Exception("Unable to encrypt backend configuration unsupported version $version"); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - return $value; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Decrypt the given string. |
|
| 361 | - * |
|
| 362 | - * @param $value |
|
| 363 | - * @param $version the storage version used to identify what encryption to use |
|
| 364 | - * @return string |
|
| 365 | - */ |
|
| 366 | - private function decryptBackendConfigProperty($value, $version=0) { |
|
| 367 | - if (is_bool($value)) { |
|
| 368 | - return $value; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - if ($version == self::ACCOUNT_VERSION) { |
|
| 372 | - $value = hex2bin($value); |
|
| 373 | - $nonce = substr($value, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
|
| 374 | - $encrypted = substr($value, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, strlen($value)); |
|
| 375 | - $key = $GLOBALS["operations"]->getFilesEncryptionKey(); |
|
| 376 | - $value = sodium_crypto_secretbox_open($encrypted, $nonce, $key); |
|
| 377 | - |
|
| 378 | - // Decryption failed, password might have changed |
|
| 379 | - if ($value === false) { |
|
| 380 | - throw new Exception("invalid password"); |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - return $value; |
|
| 385 | - } |
|
| 21 | + const LOG_CONTEXT = "AccountStore"; // Context for the Logger |
|
| 22 | + const ACCOUNT_STORAGE_PATH = "zarafa/v1/plugins/files/accounts"; |
|
| 23 | + const ACCOUNT_VERSION = 1; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var Account[] Account array |
|
| 27 | + */ |
|
| 28 | + private $accounts = []; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * |
|
| 32 | + */ |
|
| 33 | + function __construct() |
|
| 34 | + { |
|
| 35 | + $this->initialiseAccounts(); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @param $name |
|
| 40 | + * @param $backend |
|
| 41 | + * @param Array $backendConfig Backend specific account settings |
|
| 42 | + * like username, password, serveraddress, ... |
|
| 43 | + * |
|
| 44 | + * @return Account |
|
| 45 | + */ |
|
| 46 | + public function createAccount($name, $backend, $backendConfig) |
|
| 47 | + { |
|
| 48 | + $newID = $this->createNewId($backendConfig); // create id out of the configuration |
|
| 49 | + |
|
| 50 | + // create instance of backend to get features |
|
| 51 | + $backendStore = BackendStore::getInstance(); |
|
| 52 | + $backendInstance = $backendStore->getInstanceOfBackend($backend); |
|
| 53 | + $features = $backendInstance->getAvailableFeatures(); |
|
| 54 | + |
|
| 55 | + // check backend_config for validity |
|
| 56 | + $status = $this->checkBackendConfig($backendInstance, $backendConfig); |
|
| 57 | + |
|
| 58 | + // get sequence number |
|
| 59 | + $sequence = $this->getNewSequenceNumber(); |
|
| 60 | + |
|
| 61 | + $newAccount = new Account($newID, strip_tags($name), $status[0], $status[1], strip_tags($backend), $backendConfig, $features, $sequence, false); |
|
| 62 | + |
|
| 63 | + // now store all the values to the user settings |
|
| 64 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/id", $newAccount->getId()); |
|
| 65 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/name", $newAccount->getName()); |
|
| 66 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/status", $newAccount->getStatus()); |
|
| 67 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/status_description", $newAccount->getStatusDescription()); |
|
| 68 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/backend", $newAccount->getBackend()); |
|
| 69 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/account_sequence", $newAccount->getSequence()); |
|
| 70 | + // User defined accounts are never administrative. So set cannot_change to false. |
|
| 71 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/cannot_change", false); |
|
| 72 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/backend_config/version", self::ACCOUNT_VERSION); |
|
| 73 | + // store all backend configurations |
|
| 74 | + foreach ($newAccount->getBackendConfig() as $key => $value) { |
|
| 75 | + if ($key !== "version") { |
|
| 76 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/backend_config/" . $key, $this->encryptBackendConfigProperty($value, self::ACCOUNT_VERSION)); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // store all features |
|
| 81 | + foreach ($newAccount->getFeatures() as $feature) { |
|
| 82 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $newID . "/backend_features/" . $feature, true); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $GLOBALS["settings"]->saveSettings(); // save to MAPI storage |
|
| 86 | + |
|
| 87 | + // add account to our local store after it was saved to the zarafa-settings |
|
| 88 | + $this->accounts[$newID] = $newAccount; |
|
| 89 | + |
|
| 90 | + return $newAccount; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param Account $account |
|
| 95 | + * |
|
| 96 | + * @return Account |
|
| 97 | + */ |
|
| 98 | + public function updateAccount($account) |
|
| 99 | + { |
|
| 100 | + $accId = $account->getId(); |
|
| 101 | + $isAdministrativeAccount = $account->getCannotChangeFlag(); |
|
| 102 | + |
|
| 103 | + // create instance of backend to get features |
|
| 104 | + $backendStore = BackendStore::getInstance(); |
|
| 105 | + $backendInstance = $backendStore->getInstanceOfBackend($account->getBackend()); |
|
| 106 | + $features = $backendInstance->getAvailableFeatures(); |
|
| 107 | + $account->setFeatures($features); |
|
| 108 | + |
|
| 109 | + // check backend_config for validity |
|
| 110 | + $status = $this->checkBackendConfig($backendInstance, $account->getBackendConfig()); |
|
| 111 | + $account->setStatus($status[0]); // update status |
|
| 112 | + $account->setStatusDescription($status[1]); // update status description |
|
| 113 | + |
|
| 114 | + // add account to local store |
|
| 115 | + $this->accounts[$accId] = $account; |
|
| 116 | + |
|
| 117 | + // save values to MAPI settings |
|
| 118 | + // now store all the values to the user settings |
|
| 119 | + // but if we have an administrative account only save the account sequence |
|
| 120 | + if (!$isAdministrativeAccount) { |
|
| 121 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/name", $account->getName()); |
|
| 122 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/status", $account->getStatus()); |
|
| 123 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/status_description", $account->getStatusDescription()); |
|
| 124 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/backend", $account->getBackend()); |
|
| 125 | + |
|
| 126 | + $acc = $account->getBackendConfig(); |
|
| 127 | + $version = 0; |
|
| 128 | + if (isset($acc["version"])) { |
|
| 129 | + $version = $acc["version"]; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Unable to decrypt, don't update |
|
| 133 | + if ($version == 0 && !defined('FILES_PASSWORD_IV') && !defined('FILES_PASSWORD_KEY')) { |
|
| 134 | + Logger::error(self::LOG_CONTEXT, "Unable to update the account to as FILES_PASSWORD_IV/FILES_PASSWORD_KEY is not set"); |
|
| 135 | + } else { |
|
| 136 | + // store all backend configurations |
|
| 137 | + foreach ($account->getBackendConfig() as $key => $value) { |
|
| 138 | + if ($key !== "version") { |
|
| 139 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/backend_config/" . $key, $this->encryptBackendConfigProperty($value, self::ACCOUNT_VERSION)); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/backend_config/version", self::ACCOUNT_VERSION); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // store all features |
|
| 147 | + foreach ($account->getFeatures() as $feature) { |
|
| 148 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/backend_features/" . $feature, true); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + // when getSequence returns 0, there is no account_sequence setting yet. So create one. |
|
| 152 | + $account_sequence = ($account->getSequence() === 0 ? $this->getNewSequenceNumber() : $account->getSequence()); |
|
| 153 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $accId . "/account_sequence", $account_sequence); |
|
| 154 | + |
|
| 155 | + $GLOBALS["settings"]->saveSettings(); // save to MAPI storage |
|
| 156 | + |
|
| 157 | + return $account; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Delete account from local store and from the MAPI settings |
|
| 162 | + * |
|
| 163 | + * @param $accountId |
|
| 164 | + * |
|
| 165 | + * @return bool |
|
| 166 | + */ |
|
| 167 | + public function deleteAccount($accountId) |
|
| 168 | + { |
|
| 169 | + $account = $this->getAccount($accountId); |
|
| 170 | + // Do not allow deleting administrative accounts, but fail silently. |
|
| 171 | + if (!$account->getCannotChangeFlag()) { |
|
| 172 | + $GLOBALS["settings"]->delete(self::ACCOUNT_STORAGE_PATH . "/" . $accountId); |
|
| 173 | + $GLOBALS["settings"]->saveSettings(); // save to MAPI storage |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return true; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Return the instance of the local account |
|
| 181 | + * |
|
| 182 | + * @param $accountId |
|
| 183 | + * |
|
| 184 | + * @return Account |
|
| 185 | + */ |
|
| 186 | + public function getAccount($accountId) |
|
| 187 | + { |
|
| 188 | + return $this->accounts[$accountId]; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @return Account[] all Accounts |
|
| 193 | + */ |
|
| 194 | + public function getAllAccounts() |
|
| 195 | + { |
|
| 196 | + return $this->accounts; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Initialize the accountstore. Reads all accountinformation from the MAPI settings. |
|
| 201 | + */ |
|
| 202 | + private function initialiseAccounts() |
|
| 203 | + { |
|
| 204 | + // Parse accounts from the Settings |
|
| 205 | + $tmpAccs = $GLOBALS["settings"]->get(self::ACCOUNT_STORAGE_PATH); |
|
| 206 | + |
|
| 207 | + if (is_array($tmpAccs)) { |
|
| 208 | + $this->accounts = array(); |
|
| 209 | + |
|
| 210 | + foreach ($tmpAccs as $acc) { |
|
| 211 | + // set backend_features if it is not set to prevent warning |
|
| 212 | + if (!isset($acc["backend_features"])) { |
|
| 213 | + $acc["backend_features"] = array(); |
|
| 214 | + } |
|
| 215 | + // account_sequence was introduced later. So set and save it if missing. |
|
| 216 | + if (!isset($acc["account_sequence"])) { |
|
| 217 | + $acc["account_sequence"] = $this->getNewSequenceNumber(); |
|
| 218 | + Logger::debug(self::LOG_CONTEXT, "Account sequence missing. New seq: " . $acc["account_sequence"]); |
|
| 219 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $acc["id"] . "/account_sequence", $acc["account_sequence"]); |
|
| 220 | + $GLOBALS["settings"]->saveSettings(); |
|
| 221 | + } |
|
| 222 | + // cannot_change flag was introduced later. So set it to false and save it if missing. |
|
| 223 | + if (!isset($acc["cannot_change"])) { |
|
| 224 | + $acc["cannot_change"] = false; |
|
| 225 | + Logger::debug(self::LOG_CONTEXT, "Cannot change flag missing. Setting to false."); |
|
| 226 | + $GLOBALS["settings"]->set(self::ACCOUNT_STORAGE_PATH . "/" . $acc["id"] . "/cannot_change", false); |
|
| 227 | + $GLOBALS["settings"]->saveSettings(); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $backend_config = $acc["backend_config"]; |
|
| 231 | + $version = 0; |
|
| 232 | + |
|
| 233 | + if (isset($acc["backend_config"]) && isset($acc["backend_config"]["version"])) { |
|
| 234 | + $version = $acc["backend_config"]["version"]; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + if (($version === 0 && defined('FILES_PASSWORD_IV') && defined('FILES_PASSWORD_KEY')) || $version === self::ACCOUNT_VERSION) { |
|
| 238 | + $backend_config = $this->decryptBackendConfig($acc["backend_config"], $version); |
|
| 239 | + // version is lost after decryption, add it again |
|
| 240 | + $backend_config["version"] = $version; |
|
| 241 | + } else if ($version === 0) { |
|
| 242 | + Logger::error(self::LOG_CONTEXT, "FILES_PASSWORD_IV or FILES_PASSWORD_KEY not set, unable to decrypt backend configuration"); |
|
| 243 | + } else { |
|
| 244 | + Logger::error(self::LOG_CONTEXT, "Unsupported account version $version, unable to decrypt backend configuration"); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $this->accounts[$acc["id"]] = new Account($acc["id"], |
|
| 248 | + $acc["name"], |
|
| 249 | + $acc["status"], |
|
| 250 | + $acc["status_description"], |
|
| 251 | + $acc["backend"], |
|
| 252 | + $backend_config, |
|
| 253 | + array_keys($acc["backend_features"]), |
|
| 254 | + $acc["account_sequence"], |
|
| 255 | + $acc["cannot_change"] |
|
| 256 | + ); |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + Logger::debug(self::LOG_CONTEXT, "Found " . count($this->accounts) . " accounts."); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * @param AbstractBackend $backendInstance |
|
| 264 | + * @param Array $backendConfig Backend specific account settings |
|
| 265 | + * like username, password, serveraddress, ... |
|
| 266 | + * |
|
| 267 | + * @return Array |
|
| 268 | + */ |
|
| 269 | + private function checkBackendConfig($backendInstance, $backendConfig) |
|
| 270 | + { |
|
| 271 | + $status = Account::STATUS_NEW; |
|
| 272 | + $description = _('Account is ready to use.'); |
|
| 273 | + try { |
|
| 274 | + $backendInstance->init_backend($backendConfig); |
|
| 275 | + $backendInstance->open(); |
|
| 276 | + $backendInstance->ls("/"); |
|
| 277 | + $status = Account::STATUS_OK; |
|
| 278 | + } catch (BackendException $e) { |
|
| 279 | + $status = Account::STATUS_ERROR; |
|
| 280 | + $description = $e->getMessage(); |
|
| 281 | + |
|
| 282 | + Logger::error(self::LOG_CONTEXT, "Account check failed: " . $description); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + return array($status, $description); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @param Array $backendConfig Backend specific account settings |
|
| 290 | + * like username, password, serveraddress, ... |
|
| 291 | + * |
|
| 292 | + * @return an unique id |
|
| 293 | + */ |
|
| 294 | + private function createNewId($backendConfig) |
|
| 295 | + { |
|
| 296 | + // lets create a hash |
|
| 297 | + return md5(json_encode($backendConfig) . time()); // json_encode is faster than serialize |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Generate a new sequence number. It will always be the highest used sequence number +1. |
|
| 302 | + * |
|
| 303 | + * @return int |
|
| 304 | + */ |
|
| 305 | + private function getNewSequenceNumber() { |
|
| 306 | + $seq = 0; |
|
| 307 | + foreach($this->accounts as $acc) { |
|
| 308 | + if($acc->getSequence() > $seq) { |
|
| 309 | + $seq = $acc->getSequence(); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + return $seq + 1; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Decrypt the backend configuration using the standard grommunio Web key. |
|
| 318 | + * |
|
| 319 | + * @param Array $backendConfig Backend specific account settings |
|
| 320 | + * like username, password, serveraddress, ... |
|
| 321 | + * @return array |
|
| 322 | + */ |
|
| 323 | + private function decryptBackendConfig($backendConfig, $version=0) { |
|
| 324 | + $decBackendConfig = array(); |
|
| 325 | + |
|
| 326 | + foreach($backendConfig as $key => $value) { |
|
| 327 | + if ($key !== "version") { |
|
| 328 | + try { |
|
| 329 | + $decBackendConfig[$key] = $this->decryptBackendConfigProperty($value, $version); |
|
| 330 | + } catch (Exception $e) { |
|
| 331 | + Logger::error(self::LOG_CONTEXT, sprintf("Unable to decrypt backend configuration: '%s'", $e->getMessage())); |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + return $decBackendConfig; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Encrypt the given string. |
|
| 341 | + * |
|
| 342 | + * @param $value |
|
| 343 | + * @param $version the storage version used to identify what encryption to use |
|
| 344 | + * @return string |
|
| 345 | + */ |
|
| 346 | + private function encryptBackendConfigProperty($value, $version=0) { |
|
| 347 | + if ($version == self::ACCOUNT_VERSION && !is_bool($value)) { |
|
| 348 | + $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
|
| 349 | + $key = $GLOBALS["operations"]->getFilesEncryptionKey(); |
|
| 350 | + $encrypted = sodium_crypto_secretbox($value, $nonce, $key); |
|
| 351 | + $value = bin2hex($nonce) . bin2hex($encrypted); |
|
| 352 | + } else if ($version !== self::ACCOUNT_VERSION) { |
|
| 353 | + throw Exception("Unable to encrypt backend configuration unsupported version $version"); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + return $value; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Decrypt the given string. |
|
| 361 | + * |
|
| 362 | + * @param $value |
|
| 363 | + * @param $version the storage version used to identify what encryption to use |
|
| 364 | + * @return string |
|
| 365 | + */ |
|
| 366 | + private function decryptBackendConfigProperty($value, $version=0) { |
|
| 367 | + if (is_bool($value)) { |
|
| 368 | + return $value; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + if ($version == self::ACCOUNT_VERSION) { |
|
| 372 | + $value = hex2bin($value); |
|
| 373 | + $nonce = substr($value, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
|
| 374 | + $encrypted = substr($value, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, strlen($value)); |
|
| 375 | + $key = $GLOBALS["operations"]->getFilesEncryptionKey(); |
|
| 376 | + $value = sodium_crypto_secretbox_open($encrypted, $nonce, $key); |
|
| 377 | + |
|
| 378 | + // Decryption failed, password might have changed |
|
| 379 | + if ($value === false) { |
|
| 380 | + throw new Exception("invalid password"); |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + return $value; |
|
| 385 | + } |
|
| 386 | 386 | } |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | // Unable to decrypt, don't update |
| 133 | - if ($version == 0 && !defined('FILES_PASSWORD_IV') && !defined('FILES_PASSWORD_KEY')) { |
|
| 133 | + if ($version == 0 && !defined('FILES_PASSWORD_IV') && !defined('FILES_PASSWORD_KEY')) { |
|
| 134 | 134 | Logger::error(self::LOG_CONTEXT, "Unable to update the account to as FILES_PASSWORD_IV/FILES_PASSWORD_KEY is not set"); |
| 135 | 135 | } else { |
| 136 | 136 | // store all backend configurations |
@@ -304,8 +304,8 @@ discard block |
||
| 304 | 304 | */ |
| 305 | 305 | private function getNewSequenceNumber() { |
| 306 | 306 | $seq = 0; |
| 307 | - foreach($this->accounts as $acc) { |
|
| 308 | - if($acc->getSequence() > $seq) { |
|
| 307 | + foreach ($this->accounts as $acc) { |
|
| 308 | + if ($acc->getSequence() > $seq) { |
|
| 309 | 309 | $seq = $acc->getSequence(); |
| 310 | 310 | } |
| 311 | 311 | } |
@@ -320,10 +320,10 @@ discard block |
||
| 320 | 320 | * like username, password, serveraddress, ... |
| 321 | 321 | * @return array |
| 322 | 322 | */ |
| 323 | - private function decryptBackendConfig($backendConfig, $version=0) { |
|
| 323 | + private function decryptBackendConfig($backendConfig, $version = 0) { |
|
| 324 | 324 | $decBackendConfig = array(); |
| 325 | 325 | |
| 326 | - foreach($backendConfig as $key => $value) { |
|
| 326 | + foreach ($backendConfig as $key => $value) { |
|
| 327 | 327 | if ($key !== "version") { |
| 328 | 328 | try { |
| 329 | 329 | $decBackendConfig[$key] = $this->decryptBackendConfigProperty($value, $version); |
@@ -343,7 +343,7 @@ discard block |
||
| 343 | 343 | * @param $version the storage version used to identify what encryption to use |
| 344 | 344 | * @return string |
| 345 | 345 | */ |
| 346 | - private function encryptBackendConfigProperty($value, $version=0) { |
|
| 346 | + private function encryptBackendConfigProperty($value, $version = 0) { |
|
| 347 | 347 | if ($version == self::ACCOUNT_VERSION && !is_bool($value)) { |
| 348 | 348 | $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
| 349 | 349 | $key = $GLOBALS["operations"]->getFilesEncryptionKey(); |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | * @param $version the storage version used to identify what encryption to use |
| 364 | 364 | * @return string |
| 365 | 365 | */ |
| 366 | - private function decryptBackendConfigProperty($value, $version=0) { |
|
| 366 | + private function decryptBackendConfigProperty($value, $version = 0) { |
|
| 367 | 367 | if (is_bool($value)) { |
| 368 | 368 | return $value; |
| 369 | 369 | } |
@@ -19,148 +19,148 @@ |
||
| 19 | 19 | |
| 20 | 20 | class DownloadHandler |
| 21 | 21 | { |
| 22 | - const LOG_CONTEXT = "DownloadHandler"; // Context for the Logger |
|
| 23 | - |
|
| 24 | - public static function doDownload() |
|
| 25 | - { |
|
| 26 | - // parse account id. |
|
| 27 | - // wo only need to parse one string because it is |
|
| 28 | - // only possible to download files from one backend at a time. |
|
| 29 | - if (isset($_GET["ids"])) { |
|
| 30 | - $tmpId = $_GET["ids"][0]; |
|
| 31 | - } else { |
|
| 32 | - $tmpId = $_GET["id"]; |
|
| 33 | - } |
|
| 34 | - $accountID = substr($tmpId, 3, (strpos($tmpId, '/') - 3)); |
|
| 35 | - |
|
| 36 | - // Initialize the account and backendstore |
|
| 37 | - $accountStore = new \Files\Core\AccountStore(); |
|
| 38 | - $backendStore = \Files\Backend\BackendStore::getInstance(); |
|
| 39 | - |
|
| 40 | - $account = $accountStore->getAccount($accountID); |
|
| 41 | - |
|
| 42 | - // initialize the backend |
|
| 43 | - $initializedBackend = $backendStore->getInstanceOfBackend($account->getBackend()); |
|
| 44 | - $initializedBackend->init_backend($account->getBackendConfig()); |
|
| 45 | - |
|
| 46 | - try { |
|
| 47 | - $initializedBackend->open(); |
|
| 48 | - } catch (\Files\Backend\Exception $e) { |
|
| 49 | - Logger::error(self::LOG_CONTEXT, "Could not open the backend: " . $e->getMessage()); |
|
| 50 | - |
|
| 51 | - if ((isset($_GET["inline"]) && $_GET["inline"] == "false") || (isset($_GET["contentDispositionType"]) && $_GET["contentDispositionType"] == "attachment")) { |
|
| 52 | - // Javascript error message |
|
| 53 | - echo "<script>alert('" . _('File backend not responding. Please try again later.') . "');</script>"; |
|
| 54 | - } else { |
|
| 55 | - // Text error message that is shown in the preview box |
|
| 56 | - echo _('File backend not responding. Please try again later.'); |
|
| 57 | - } |
|
| 58 | - die(); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - try { |
|
| 62 | - if (isset($_GET["ids"])) { |
|
| 63 | - $zip = new \ZipArchive; |
|
| 64 | - $zipname = TMP_PATH . '/files_' . date("dmY_Hi") . '.zip'; |
|
| 65 | - Logger::debug(self::LOG_CONTEXT, "Download file tmp path: " . $zipname); |
|
| 66 | - $res = $zip->open($zipname, \ZipArchive::CREATE); |
|
| 67 | - if ($res !== true) { |
|
| 68 | - Logger::error(self::LOG_CONTEXT, "Zip creation failed: " . $res); |
|
| 69 | - echo "<script>alert('" . _('Zip file generation failed. Please inform the administrator.') . "');</script>"; |
|
| 70 | - |
|
| 71 | - die(); |
|
| 72 | - } |
|
| 73 | - $i = 0; |
|
| 74 | - $tmpfiles = array(); |
|
| 75 | - foreach ($_GET["ids"] as $id) { |
|
| 76 | - // relative node ID. We need to trim off the #R# and account ID |
|
| 77 | - $relNodeId = substr($id, strpos($id, '/')); |
|
| 78 | - |
|
| 79 | - $tmpfiles[$i] = tempnam(TMP_PATH, stripslashes(base64_encode($relNodeId))); |
|
| 80 | - $initializedBackend->get_file($relNodeId, $tmpfiles[$i]); |
|
| 81 | - |
|
| 82 | - $res = $zip->addFile($tmpfiles[$i], PathUtil::getFilenameFromPath($relNodeId)); |
|
| 83 | - $i++; |
|
| 84 | - if ($res !== true) { |
|
| 85 | - Logger::error(self::LOG_CONTEXT, "Zip addFile failed: " . $res . " file: " . $tmpfiles[$i] . " id: " . $relNodeId); |
|
| 86 | - echo "<script>alert('" . _('Zip file generation failed. Please inform the administrator.') . "');</script>"; |
|
| 87 | - |
|
| 88 | - die(); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - $zip->close(); |
|
| 92 | - |
|
| 93 | - // no caching |
|
| 94 | - header('Content-Disposition: attachment; filename="' . basename($zipname) . '"'); |
|
| 95 | - header("Expires: 0"); // set expiration time |
|
| 96 | - header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
| 97 | - header('Content-Length: ' . filesize($zipname)); |
|
| 98 | - header('Content-Type: application/zip'); |
|
| 99 | - readfile($zipname); |
|
| 100 | - unlink($zipname); |
|
| 101 | - foreach ($tmpfiles as $tmpfile) { |
|
| 102 | - unlink($tmpfile); |
|
| 103 | - } |
|
| 104 | - die(); |
|
| 105 | - } else { |
|
| 106 | - // relative node ID. We need to trim off the #R# and account ID |
|
| 107 | - $relNodeId = substr($_GET["id"], strpos($_GET["id"], '/')); |
|
| 108 | - $stream = false; |
|
| 109 | - |
|
| 110 | - $tmpfile; |
|
| 111 | - if (!$initializedBackend->supports(\Files\Backend\BackendStore::FEATURE_STREAMING)) { |
|
| 112 | - $tmpfile = tempnam(TMP_PATH, stripslashes(base64_encode($relNodeId))); |
|
| 113 | - $initializedBackend->get_file($relNodeId, $tmpfile); |
|
| 114 | - $filesize = filesize($tmpfile); |
|
| 115 | - } else { |
|
| 116 | - $gpi = $initializedBackend->gpi($relNodeId); |
|
| 117 | - $stream = true; |
|
| 118 | - $filesize = $gpi["getcontentlength"]; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $mime = PathUtil::get_mime($relNodeId); |
|
| 122 | - |
|
| 123 | - // set headers here |
|
| 124 | - if ((isset($_GET["inline"]) && $_GET["inline"] == "false") || (isset($_GET["contentDispositionType"]) && $_GET["contentDispositionType"] == "attachment")) { |
|
| 125 | - header('Content-Disposition: attachment; filename="' . PathUtil::getFilenameFromPath($relNodeId) . '"'); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // no caching |
|
| 129 | - header("Expires: 0"); // set expiration time |
|
| 130 | - header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
| 131 | - header('Content-Length: ' . $filesize); |
|
| 132 | - header('Content-Type: ' . $mime); |
|
| 133 | - flush(); |
|
| 134 | - |
|
| 135 | - if (!$stream) { |
|
| 136 | - // print the downloaded file |
|
| 137 | - readfile($tmpfile); |
|
| 138 | - ignore_user_abort(true); |
|
| 139 | - unlink($tmpfile); |
|
| 140 | - } else { |
|
| 141 | - // stream the file directly from the backend - much faster |
|
| 142 | - $fh = $initializedBackend->getStreamReader($relNodeId); |
|
| 143 | - while (!feof($fh)) { |
|
| 144 | - set_time_limit(0); |
|
| 145 | - print fread($fh, 4096); |
|
| 146 | - ob_flush(); |
|
| 147 | - flush(); |
|
| 148 | - } |
|
| 149 | - fclose($fh); |
|
| 150 | - } |
|
| 151 | - die(); |
|
| 152 | - } |
|
| 153 | - } catch (\Files\Backend\Exception $e) { |
|
| 154 | - Logger::error(self::LOG_CONTEXT, "Downloading failed: " . $e->getMessage()); |
|
| 155 | - |
|
| 156 | - if (isset($_GET["inline"]) && $_GET["inline"] == "false") { |
|
| 157 | - // Javascript error message |
|
| 158 | - echo "<script>alert('" . _('This file is no longer available. Please reload the folder.') . "');</script>"; |
|
| 159 | - } else { |
|
| 160 | - // Text error message that is shown in the preview box |
|
| 161 | - echo _('This file is no longer available. Please reload the folder.'); |
|
| 162 | - } |
|
| 163 | - die(); |
|
| 164 | - } |
|
| 165 | - } |
|
| 22 | + const LOG_CONTEXT = "DownloadHandler"; // Context for the Logger |
|
| 23 | + |
|
| 24 | + public static function doDownload() |
|
| 25 | + { |
|
| 26 | + // parse account id. |
|
| 27 | + // wo only need to parse one string because it is |
|
| 28 | + // only possible to download files from one backend at a time. |
|
| 29 | + if (isset($_GET["ids"])) { |
|
| 30 | + $tmpId = $_GET["ids"][0]; |
|
| 31 | + } else { |
|
| 32 | + $tmpId = $_GET["id"]; |
|
| 33 | + } |
|
| 34 | + $accountID = substr($tmpId, 3, (strpos($tmpId, '/') - 3)); |
|
| 35 | + |
|
| 36 | + // Initialize the account and backendstore |
|
| 37 | + $accountStore = new \Files\Core\AccountStore(); |
|
| 38 | + $backendStore = \Files\Backend\BackendStore::getInstance(); |
|
| 39 | + |
|
| 40 | + $account = $accountStore->getAccount($accountID); |
|
| 41 | + |
|
| 42 | + // initialize the backend |
|
| 43 | + $initializedBackend = $backendStore->getInstanceOfBackend($account->getBackend()); |
|
| 44 | + $initializedBackend->init_backend($account->getBackendConfig()); |
|
| 45 | + |
|
| 46 | + try { |
|
| 47 | + $initializedBackend->open(); |
|
| 48 | + } catch (\Files\Backend\Exception $e) { |
|
| 49 | + Logger::error(self::LOG_CONTEXT, "Could not open the backend: " . $e->getMessage()); |
|
| 50 | + |
|
| 51 | + if ((isset($_GET["inline"]) && $_GET["inline"] == "false") || (isset($_GET["contentDispositionType"]) && $_GET["contentDispositionType"] == "attachment")) { |
|
| 52 | + // Javascript error message |
|
| 53 | + echo "<script>alert('" . _('File backend not responding. Please try again later.') . "');</script>"; |
|
| 54 | + } else { |
|
| 55 | + // Text error message that is shown in the preview box |
|
| 56 | + echo _('File backend not responding. Please try again later.'); |
|
| 57 | + } |
|
| 58 | + die(); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + try { |
|
| 62 | + if (isset($_GET["ids"])) { |
|
| 63 | + $zip = new \ZipArchive; |
|
| 64 | + $zipname = TMP_PATH . '/files_' . date("dmY_Hi") . '.zip'; |
|
| 65 | + Logger::debug(self::LOG_CONTEXT, "Download file tmp path: " . $zipname); |
|
| 66 | + $res = $zip->open($zipname, \ZipArchive::CREATE); |
|
| 67 | + if ($res !== true) { |
|
| 68 | + Logger::error(self::LOG_CONTEXT, "Zip creation failed: " . $res); |
|
| 69 | + echo "<script>alert('" . _('Zip file generation failed. Please inform the administrator.') . "');</script>"; |
|
| 70 | + |
|
| 71 | + die(); |
|
| 72 | + } |
|
| 73 | + $i = 0; |
|
| 74 | + $tmpfiles = array(); |
|
| 75 | + foreach ($_GET["ids"] as $id) { |
|
| 76 | + // relative node ID. We need to trim off the #R# and account ID |
|
| 77 | + $relNodeId = substr($id, strpos($id, '/')); |
|
| 78 | + |
|
| 79 | + $tmpfiles[$i] = tempnam(TMP_PATH, stripslashes(base64_encode($relNodeId))); |
|
| 80 | + $initializedBackend->get_file($relNodeId, $tmpfiles[$i]); |
|
| 81 | + |
|
| 82 | + $res = $zip->addFile($tmpfiles[$i], PathUtil::getFilenameFromPath($relNodeId)); |
|
| 83 | + $i++; |
|
| 84 | + if ($res !== true) { |
|
| 85 | + Logger::error(self::LOG_CONTEXT, "Zip addFile failed: " . $res . " file: " . $tmpfiles[$i] . " id: " . $relNodeId); |
|
| 86 | + echo "<script>alert('" . _('Zip file generation failed. Please inform the administrator.') . "');</script>"; |
|
| 87 | + |
|
| 88 | + die(); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + $zip->close(); |
|
| 92 | + |
|
| 93 | + // no caching |
|
| 94 | + header('Content-Disposition: attachment; filename="' . basename($zipname) . '"'); |
|
| 95 | + header("Expires: 0"); // set expiration time |
|
| 96 | + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
| 97 | + header('Content-Length: ' . filesize($zipname)); |
|
| 98 | + header('Content-Type: application/zip'); |
|
| 99 | + readfile($zipname); |
|
| 100 | + unlink($zipname); |
|
| 101 | + foreach ($tmpfiles as $tmpfile) { |
|
| 102 | + unlink($tmpfile); |
|
| 103 | + } |
|
| 104 | + die(); |
|
| 105 | + } else { |
|
| 106 | + // relative node ID. We need to trim off the #R# and account ID |
|
| 107 | + $relNodeId = substr($_GET["id"], strpos($_GET["id"], '/')); |
|
| 108 | + $stream = false; |
|
| 109 | + |
|
| 110 | + $tmpfile; |
|
| 111 | + if (!$initializedBackend->supports(\Files\Backend\BackendStore::FEATURE_STREAMING)) { |
|
| 112 | + $tmpfile = tempnam(TMP_PATH, stripslashes(base64_encode($relNodeId))); |
|
| 113 | + $initializedBackend->get_file($relNodeId, $tmpfile); |
|
| 114 | + $filesize = filesize($tmpfile); |
|
| 115 | + } else { |
|
| 116 | + $gpi = $initializedBackend->gpi($relNodeId); |
|
| 117 | + $stream = true; |
|
| 118 | + $filesize = $gpi["getcontentlength"]; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $mime = PathUtil::get_mime($relNodeId); |
|
| 122 | + |
|
| 123 | + // set headers here |
|
| 124 | + if ((isset($_GET["inline"]) && $_GET["inline"] == "false") || (isset($_GET["contentDispositionType"]) && $_GET["contentDispositionType"] == "attachment")) { |
|
| 125 | + header('Content-Disposition: attachment; filename="' . PathUtil::getFilenameFromPath($relNodeId) . '"'); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // no caching |
|
| 129 | + header("Expires: 0"); // set expiration time |
|
| 130 | + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
| 131 | + header('Content-Length: ' . $filesize); |
|
| 132 | + header('Content-Type: ' . $mime); |
|
| 133 | + flush(); |
|
| 134 | + |
|
| 135 | + if (!$stream) { |
|
| 136 | + // print the downloaded file |
|
| 137 | + readfile($tmpfile); |
|
| 138 | + ignore_user_abort(true); |
|
| 139 | + unlink($tmpfile); |
|
| 140 | + } else { |
|
| 141 | + // stream the file directly from the backend - much faster |
|
| 142 | + $fh = $initializedBackend->getStreamReader($relNodeId); |
|
| 143 | + while (!feof($fh)) { |
|
| 144 | + set_time_limit(0); |
|
| 145 | + print fread($fh, 4096); |
|
| 146 | + ob_flush(); |
|
| 147 | + flush(); |
|
| 148 | + } |
|
| 149 | + fclose($fh); |
|
| 150 | + } |
|
| 151 | + die(); |
|
| 152 | + } |
|
| 153 | + } catch (\Files\Backend\Exception $e) { |
|
| 154 | + Logger::error(self::LOG_CONTEXT, "Downloading failed: " . $e->getMessage()); |
|
| 155 | + |
|
| 156 | + if (isset($_GET["inline"]) && $_GET["inline"] == "false") { |
|
| 157 | + // Javascript error message |
|
| 158 | + echo "<script>alert('" . _('This file is no longer available. Please reload the folder.') . "');</script>"; |
|
| 159 | + } else { |
|
| 160 | + // Text error message that is shown in the preview box |
|
| 161 | + echo _('This file is no longer available. Please reload the folder.'); |
|
| 162 | + } |
|
| 163 | + die(); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | } |
@@ -11,223 +11,223 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Account |
| 13 | 13 | { |
| 14 | - private $id; |
|
| 15 | - private $name; // account name - for better usability |
|
| 16 | - private $status; |
|
| 17 | - private $statusDescription; |
|
| 18 | - private $backend; |
|
| 19 | - private $backendConfig; // This array will hold the backend configuration variables |
|
| 20 | - private $features; |
|
| 21 | - private $sequence; // for ordering |
|
| 22 | - private $cannot_change; // for locking accounts |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Status variables |
|
| 26 | - */ |
|
| 27 | - const STATUS_NEW = "new"; |
|
| 28 | - const STATUS_OK = "ok"; |
|
| 29 | - const STATUS_ERROR = "err"; |
|
| 30 | - const STATUS_UNKNOWN = "unk"; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @param $id |
|
| 34 | - * @param $name |
|
| 35 | - * @param $status |
|
| 36 | - * @param $statusDescription |
|
| 37 | - * @param $backend |
|
| 38 | - * @param $backendConfig |
|
| 39 | - * @param array $features |
|
| 40 | - */ |
|
| 41 | - function __construct($id, $name, $status, $statusDescription, $backend, $backendConfig, $features = array(), $sequence, $cannot_change) |
|
| 42 | - { |
|
| 43 | - $this->id = $id; |
|
| 44 | - $this->name = $name; |
|
| 45 | - $this->status = $status; |
|
| 46 | - $this->statusDescription = $statusDescription; |
|
| 47 | - $this->backend = $backend; |
|
| 48 | - $this->backendConfig = $backendConfig; |
|
| 49 | - $this->features = $features; |
|
| 50 | - $this->sequence = $sequence; |
|
| 51 | - $this->cannot_change = $cannot_change; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return mixed |
|
| 56 | - */ |
|
| 57 | - public function getBackend() |
|
| 58 | - { |
|
| 59 | - return $this->backend; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param mixed $backend |
|
| 64 | - */ |
|
| 65 | - public function setBackend($backend) |
|
| 66 | - { |
|
| 67 | - $this->backend = $backend; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @return mixed |
|
| 72 | - */ |
|
| 73 | - public function getId() |
|
| 74 | - { |
|
| 75 | - return $this->id; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param mixed $id |
|
| 80 | - */ |
|
| 81 | - public function setId($id) |
|
| 82 | - { |
|
| 83 | - $this->id = $id; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @return mixed |
|
| 88 | - */ |
|
| 89 | - public function getName() |
|
| 90 | - { |
|
| 91 | - return $this->name; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param mixed $name |
|
| 96 | - */ |
|
| 97 | - public function setName($name) |
|
| 98 | - { |
|
| 99 | - $this->name = $name; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return mixed |
|
| 104 | - */ |
|
| 105 | - public function getStatus() |
|
| 106 | - { |
|
| 107 | - return $this->status; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param mixed $status |
|
| 112 | - */ |
|
| 113 | - public function setStatus($status) |
|
| 114 | - { |
|
| 115 | - $this->status = $status; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return mixed |
|
| 120 | - */ |
|
| 121 | - public function getBackendConfig() |
|
| 122 | - { |
|
| 123 | - // always add the accountID to the backendConfig |
|
| 124 | - $this->backendConfig["current_account_id"] = $this->getId(); |
|
| 125 | - |
|
| 126 | - return $this->backendConfig; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param mixed $backendConfig |
|
| 131 | - */ |
|
| 132 | - public function setBackendConfig($backendConfig) |
|
| 133 | - { |
|
| 134 | - $this->backendConfig = $backendConfig; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @return mixed |
|
| 139 | - */ |
|
| 140 | - public function getFeatures() |
|
| 141 | - { |
|
| 142 | - return $this->features; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @param mixed $features |
|
| 147 | - */ |
|
| 148 | - public function setFeatures($features) |
|
| 149 | - { |
|
| 150 | - $this->features = $features; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @return string |
|
| 155 | - */ |
|
| 156 | - public function getStatusDescription() |
|
| 157 | - { |
|
| 158 | - return $this->statusDescription; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param string $statusDescription |
|
| 163 | - */ |
|
| 164 | - public function setStatusDescription($statusDescription) |
|
| 165 | - { |
|
| 166 | - $this->statusDescription = $statusDescription; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @param $property |
|
| 171 | - * |
|
| 172 | - * @return mixed |
|
| 173 | - */ |
|
| 174 | - public function getConfigValue($property) |
|
| 175 | - { |
|
| 176 | - if (is_array($this->backendConfig) && in_array($property, $this->backendConfig)) { |
|
| 177 | - return $this->backendConfig[$property]; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return false; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * @param $property |
|
| 185 | - * @param $value |
|
| 186 | - */ |
|
| 187 | - public function setConfigValue($property, $value) |
|
| 188 | - { |
|
| 189 | - if (!is_array($this->backendConfig)) { |
|
| 190 | - $this->backendConfig = array(); |
|
| 191 | - } |
|
| 192 | - $this->backendConfig[$property] = $value; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * This function is executed before the account gets deleted. |
|
| 197 | - */ |
|
| 198 | - public function beforeDelete() { |
|
| 199 | - $backendstore = BackendStore::getInstance(); |
|
| 200 | - $backendinstance = $backendstore->getInstanceOfBackend($this->backend); |
|
| 201 | - $backendinstance->init_backend($this->backendConfig); |
|
| 202 | - $backendinstance->open(); |
|
| 203 | - $backendinstance->beforeDeleteAccount($this); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @return int |
|
| 208 | - */ |
|
| 209 | - public function getSequence() |
|
| 210 | - { |
|
| 211 | - if(!$this->sequence) { |
|
| 212 | - $this->sequence = 0; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return $this->sequence; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @param int $sequence |
|
| 220 | - */ |
|
| 221 | - public function setSequence($sequence) |
|
| 222 | - { |
|
| 223 | - $this->sequence = $sequence; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @return boolean |
|
| 228 | - */ |
|
| 229 | - public function getCannotChangeFlag() |
|
| 230 | - { |
|
| 231 | - return isset($this->cannot_change) ? $this->cannot_change : false; |
|
| 232 | - } |
|
| 14 | + private $id; |
|
| 15 | + private $name; // account name - for better usability |
|
| 16 | + private $status; |
|
| 17 | + private $statusDescription; |
|
| 18 | + private $backend; |
|
| 19 | + private $backendConfig; // This array will hold the backend configuration variables |
|
| 20 | + private $features; |
|
| 21 | + private $sequence; // for ordering |
|
| 22 | + private $cannot_change; // for locking accounts |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Status variables |
|
| 26 | + */ |
|
| 27 | + const STATUS_NEW = "new"; |
|
| 28 | + const STATUS_OK = "ok"; |
|
| 29 | + const STATUS_ERROR = "err"; |
|
| 30 | + const STATUS_UNKNOWN = "unk"; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @param $id |
|
| 34 | + * @param $name |
|
| 35 | + * @param $status |
|
| 36 | + * @param $statusDescription |
|
| 37 | + * @param $backend |
|
| 38 | + * @param $backendConfig |
|
| 39 | + * @param array $features |
|
| 40 | + */ |
|
| 41 | + function __construct($id, $name, $status, $statusDescription, $backend, $backendConfig, $features = array(), $sequence, $cannot_change) |
|
| 42 | + { |
|
| 43 | + $this->id = $id; |
|
| 44 | + $this->name = $name; |
|
| 45 | + $this->status = $status; |
|
| 46 | + $this->statusDescription = $statusDescription; |
|
| 47 | + $this->backend = $backend; |
|
| 48 | + $this->backendConfig = $backendConfig; |
|
| 49 | + $this->features = $features; |
|
| 50 | + $this->sequence = $sequence; |
|
| 51 | + $this->cannot_change = $cannot_change; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return mixed |
|
| 56 | + */ |
|
| 57 | + public function getBackend() |
|
| 58 | + { |
|
| 59 | + return $this->backend; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param mixed $backend |
|
| 64 | + */ |
|
| 65 | + public function setBackend($backend) |
|
| 66 | + { |
|
| 67 | + $this->backend = $backend; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @return mixed |
|
| 72 | + */ |
|
| 73 | + public function getId() |
|
| 74 | + { |
|
| 75 | + return $this->id; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param mixed $id |
|
| 80 | + */ |
|
| 81 | + public function setId($id) |
|
| 82 | + { |
|
| 83 | + $this->id = $id; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @return mixed |
|
| 88 | + */ |
|
| 89 | + public function getName() |
|
| 90 | + { |
|
| 91 | + return $this->name; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param mixed $name |
|
| 96 | + */ |
|
| 97 | + public function setName($name) |
|
| 98 | + { |
|
| 99 | + $this->name = $name; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @return mixed |
|
| 104 | + */ |
|
| 105 | + public function getStatus() |
|
| 106 | + { |
|
| 107 | + return $this->status; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param mixed $status |
|
| 112 | + */ |
|
| 113 | + public function setStatus($status) |
|
| 114 | + { |
|
| 115 | + $this->status = $status; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return mixed |
|
| 120 | + */ |
|
| 121 | + public function getBackendConfig() |
|
| 122 | + { |
|
| 123 | + // always add the accountID to the backendConfig |
|
| 124 | + $this->backendConfig["current_account_id"] = $this->getId(); |
|
| 125 | + |
|
| 126 | + return $this->backendConfig; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param mixed $backendConfig |
|
| 131 | + */ |
|
| 132 | + public function setBackendConfig($backendConfig) |
|
| 133 | + { |
|
| 134 | + $this->backendConfig = $backendConfig; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @return mixed |
|
| 139 | + */ |
|
| 140 | + public function getFeatures() |
|
| 141 | + { |
|
| 142 | + return $this->features; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @param mixed $features |
|
| 147 | + */ |
|
| 148 | + public function setFeatures($features) |
|
| 149 | + { |
|
| 150 | + $this->features = $features; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @return string |
|
| 155 | + */ |
|
| 156 | + public function getStatusDescription() |
|
| 157 | + { |
|
| 158 | + return $this->statusDescription; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param string $statusDescription |
|
| 163 | + */ |
|
| 164 | + public function setStatusDescription($statusDescription) |
|
| 165 | + { |
|
| 166 | + $this->statusDescription = $statusDescription; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @param $property |
|
| 171 | + * |
|
| 172 | + * @return mixed |
|
| 173 | + */ |
|
| 174 | + public function getConfigValue($property) |
|
| 175 | + { |
|
| 176 | + if (is_array($this->backendConfig) && in_array($property, $this->backendConfig)) { |
|
| 177 | + return $this->backendConfig[$property]; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * @param $property |
|
| 185 | + * @param $value |
|
| 186 | + */ |
|
| 187 | + public function setConfigValue($property, $value) |
|
| 188 | + { |
|
| 189 | + if (!is_array($this->backendConfig)) { |
|
| 190 | + $this->backendConfig = array(); |
|
| 191 | + } |
|
| 192 | + $this->backendConfig[$property] = $value; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * This function is executed before the account gets deleted. |
|
| 197 | + */ |
|
| 198 | + public function beforeDelete() { |
|
| 199 | + $backendstore = BackendStore::getInstance(); |
|
| 200 | + $backendinstance = $backendstore->getInstanceOfBackend($this->backend); |
|
| 201 | + $backendinstance->init_backend($this->backendConfig); |
|
| 202 | + $backendinstance->open(); |
|
| 203 | + $backendinstance->beforeDeleteAccount($this); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @return int |
|
| 208 | + */ |
|
| 209 | + public function getSequence() |
|
| 210 | + { |
|
| 211 | + if(!$this->sequence) { |
|
| 212 | + $this->sequence = 0; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return $this->sequence; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @param int $sequence |
|
| 220 | + */ |
|
| 221 | + public function setSequence($sequence) |
|
| 222 | + { |
|
| 223 | + $this->sequence = $sequence; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @return boolean |
|
| 228 | + */ |
|
| 229 | + public function getCannotChangeFlag() |
|
| 230 | + { |
|
| 231 | + return isset($this->cannot_change) ? $this->cannot_change : false; |
|
| 232 | + } |
|
| 233 | 233 | } |
@@ -208,7 +208,7 @@ |
||
| 208 | 208 | */ |
| 209 | 209 | public function getSequence() |
| 210 | 210 | { |
| 211 | - if(!$this->sequence) { |
|
| 211 | + if (!$this->sequence) { |
|
| 212 | 212 | $this->sequence = 0; |
| 213 | 213 | } |
| 214 | 214 | |