This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.sqlConfDriver.php
878 lines (798 loc) · 32.1 KB
/
class.sqlConfDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* Configuration stored in an SQL Database
* @package AjaXplorer_Plugins
* @subpackage Conf
*/
class sqlConfDriver extends AbstractConfDriver
{
public $sqlDriver = array();
/**
* Initialise the driver.
*
* Expects options containing a key 'SQL_DRIVER' with constructor values from dibi::connect()
*
* Example:
* "SQL_DRIVER" => Array(
* 'driver' => 'sqlite',
* 'file' => "./server/ajxp.db"
* )
*
* Example 2:
* "SQL_DRIVER" => Array(
* 'driver' => 'mysql',
* 'host' => 'localhost',
* 'username' => 'root',
* 'password' => '***',
* 'database' => 'dbname'
* )
*
* @see AbstractConfDriver#init($options)
*/
public function init($options)
{
parent::init($options);
require_once(AJXP_BIN_FOLDER."/dibi.compact.php");
$this->sqlDriver = AJXP_Utils::cleanDibiDriverParameters($options["SQL_DRIVER"]);
try {
dibi::connect($this->sqlDriver);
} catch (DibiException $e) {
//throw $e;
echo get_class($e), ': ', $e->getMessage(), "\n";
exit(1);
}
}
public function performChecks()
{
if(!isSet($this->options)) return;
$test = AJXP_Utils::cleanDibiDriverParameters($this->options["SQL_DRIVER"]);
if (!count($test)) {
throw new Exception("You probably did something wrong! To fix this issue you have to remove the file \"bootsrap.json\" and rename the backup file \"bootstrap.json.bak\" into \"bootsrap.json\" in data/plugins/boot.conf/");
}
}
public function _loadPluginConfig($pluginId, &$options)
{
$res_opts = dibi::query('SELECT * FROM [ajxp_plugin_configs] WHERE [id] = %s', $pluginId);
$config_row = $res_opts->fetchPairs();
$confOpt = unserialize($config_row[$pluginId]);
if (is_array($confOpt)) {
foreach($confOpt as $key => $value) $options[$key] = $value;
}
}
/**
*
* @param String $pluginId
* @param String $options
*/
public function _savePluginConfig($pluginId, $options)
{
$res_opts = dibi::query('SELECT COUNT(*) FROM [ajxp_plugin_configs] WHERE [id] = %s', $pluginId);
if ($res_opts->fetchSingle()) {
dibi::query('UPDATE [ajxp_plugin_configs] SET [configs] = %bin WHERE [id] = %s', serialize($options), $pluginId);
} else {
dibi::query('INSERT INTO [ajxp_plugin_configs] ([id],[configs]) VALUES (%s,%bin)', $pluginId, serialize($options));
}
}
/**
* Create a Repository object from a Database Result
*
* The method expects the following schema:
* CREATE TABLE ajxp_repo ( uuid VARCHAR(33) PRIMARY KEY,
* path VARCHAR(255),
* display VARCHAR(255),
* accessType VARCHAR(20),
* recycle VARCHAR(255) ,
* bcreate BOOLEAN, -- For some reason 'create' is a reserved keyword
* writeable BOOLEAN,
* enabled BOOLEAN );
*
* Additionally, the options are stored in a separate table:
* CREATE TABLE ajxp_repo_options ( oid INTEGER PRIMARY KEY, uuid VARCHAR(33), name VARCHAR(50), val VARCHAR(255) );
*
* I recommend an index to increase performance of uuid lookups:
* CREATE INDEX ajxp_repo_options_uuid_idx ON ajxp_repo_options ( uuid );
*
*
* @param $result Result of a dibi::query() as array
* @param array|\Result $options_result Result of dibi::query() for options as array
* @return Repository object
*/
public function repoFromDb($result, $options_result = Array())
{
$repo = new Repository($result['id'], $result['display'], $result['accessType']);
$repo->uuid = $result['uuid'];
$repo->setOwnerData($result['parent_uuid'], $result['owner_user_id'], $result['child_user_id']);
$repo->path = $result['path'];
$repo->create = $result['bcreate'];
$repo->writeable = $result['writeable'];
$repo->writeable = true;
$repo->enabled = $result['enabled'];
$repo->recycle = "";
$repo->setSlug($result['slug']);
if (isSet($result['groupPath']) && !empty($result['groupPath'])) {
$repo->setGroupPath($result['groupPath']);
}
$repo->isTemplate = intval($result['isTemplate']) == 1 ? true : false;
$repo->setInferOptionsFromParent(intval($result['inferOptionsFromParent']) == 1 ? true : false);
foreach ($options_result as $k => $v) {
if (strpos($v, '$phpserial$') !== false && strpos($v, '$phpserial$') === 0) {
$v = unserialize(substr($v, strlen('$phpserial$')));
} else if ($k == "META_SOURCES") {
$v = unserialize($v);
}
$repo->options[$k] = $v;
}
return $repo;
}
/**
* Convert a repository object to an array, which will be stored in the database.
*
* @param $repository Repository
* @return Array containing row values, and another array with the key "options" to be stored as repo options.
*/
public function repoToArray($repository)
{
$repository_row = Array(
'uuid' => $repository->getUniqueId(),
'parent_uuid' => $repository->getParentId(),
'owner_user_id' => $repository->getOwner(),
'child_user_id' => $repository->getUniqueUser(),
'path' => $repository->options['PATH'],
'display' => $repository->getDisplay(),
'accessType' => $repository->getAccessType(),
'recycle' => $repository->recycle,
'bcreate' => intval($repository->getCreate()),
'writeable' => $repository->isWriteable(),
'enabled' => $repository->isEnabled(),
'options' => $repository->options,
'groupPath' => $repository->getGroupPath(),
'slug' => $repository->getSlug(),
'isTemplate' => $repository->isTemplate,
'inferOptionsFromParent' => $repository->getInferOptionsFromParent()
);
return $repository_row;
}
/**
* Get a list of repositories
*
* The list is an associative array of Array( 'uuid' => [Repository Object] );
* @param AbstractAjxpUser $user
* @return Repository[]
* @see AbstractConfDriver#listRepositories()
*/
public function listRepositories($user = null)
{
if ($user != null) {
$acls = $user->mergedRole->listAcls();
$limitRepositories = array_keys($acls);
if(!count($limitRepositories)) return array();
// we use (%s) instead of %in to pass int as string ('1' instead of 1)
$res = dibi::query('SELECT * FROM [ajxp_repo] WHERE [uuid] IN (%s) ORDER BY [display] ASC', $limitRepositories);
} else {
$res = dibi::query('SELECT * FROM [ajxp_repo] ORDER BY [display] ASC');
}
$all = $res->fetchAll();
$repositories = Array();
foreach ($all as $repo_row) {
$res_opts = dibi::query('SELECT * FROM [ajxp_repo_options] WHERE [uuid] = %s', $repo_row['uuid']);
$opts = $res_opts->fetchPairs('name', 'val');
$repo = $this->repoFromDb($repo_row, $opts);
$repositories[$repo->getUniqueId()] = $repo;
}
return $repositories;
}
/**
* Get repository by Unique ID (a hash calculated from the serialised object).
*
* @param String $repositoryId hash uuid
* @return Repository object
*/
public function getRepositoryById($repositoryId)
{
$res = dibi::query('SELECT * FROM [ajxp_repo] WHERE [uuid] = %s', $repositoryId);
$repo_row = $res->fetchAll();
if (count($repo_row) > 0) {
$repo_row = $repo_row[0];
$res_opts = dibi::query('SELECT * FROM [ajxp_repo_options] WHERE [uuid] = %s', $repo_row['uuid']);
$opts = $res_opts->fetchPairs('name', 'val');
$repository = $this->repoFromDb($repo_row, $opts);
return $repository;
}
return null;
}
/**
* Retrieve a Repository given its alias.
*
* @param String $repositorySlug
* @return Repository
*/
public function getRepositoryByAlias($repositorySlug)
{
$res = dibi::query('SELECT * FROM [ajxp_repo] WHERE [slug] = %s', $repositorySlug);
$repo_row = $res->fetchAll();
if (count($repo_row) > 0) {
$repo_row = $repo_row[0];
$res_opts = dibi::query('SELECT * FROM [ajxp_repo_options] WHERE [uuid] = %s', $repo_row['uuid']);
$opts = $res_opts->fetchPairs('name', 'val');
$repository = $this->repoFromDb($repo_row, $opts);
return $repository;
}
return null;
}
/**
* Store a newly created repository
*
* @param Repository $repositoryObject
* @param Boolean $update
* @return -1 if failed
*/
public function saveRepository($repositoryObject, $update = false)
{
try {
$repository_array = $this->repoToArray($repositoryObject);
$options = $repository_array['options'];
unset($repository_array['options']);
if (!$update) {
dibi::query('INSERT INTO [ajxp_repo]', $repository_array);
foreach ($options as $k => $v) {
if (!is_string($v)) {
$v = '$phpserial$'.serialize($v);
}
dibi::query('INSERT INTO [ajxp_repo_options] ([uuid],[name],[val]) VALUES (%s,%s,%bin)', $repositoryObject->getUniqueId(), $k,$v);
}
/*
//set maximum rights to the repositorie's creator jcg
$user_right['login'] = $_SESSION["AJXP_USER"]->id;
$user_right['repo_uuid'] = $repository_array['uuid'];
$user_right['rights'] = 'rw';
dibi::query('INSERT INTO [ajxp_user_rights]', $user_right);
$userid=$_SESSION["AJXP_USER"]->id;
*/
} else {
dibi::query('DELETE FROM [ajxp_repo] WHERE [uuid] = %s',$repositoryObject->getUniqueId());
dibi::query('DELETE FROM [ajxp_repo_options] WHERE [uuid] = %s',$repositoryObject->getUniqueId());
dibi::query('INSERT INTO [ajxp_repo]', $repository_array);
foreach ($options as $k => $v) {
if (!is_string($v)) {
$v = '$phpserial$'.serialize($v);
}
dibi::query('INSERT INTO [ajxp_repo_options] ([uuid],[name],[val]) VALUES (%s,%s,%bin)',$repositoryObject->getUniqueId(),$k,$v);
}
}
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
exit(1);
return -1;
}
}
/**
* Delete a repository, given its unique ID.
*
* @param String $repositoryId
*/
public function deleteRepository($repositoryId)
{
try {
$result = dibi::query('DELETE FROM [ajxp_repo] WHERE [uuid] = %s', $repositoryId);
$result_opts = dibi::query('DELETE FROM [ajxp_repo_options] WHERE [uuid] = %s', $repositoryId);
$result_opts_rights = dibi::query('DELETE FROM [ajxp_user_rights] WHERE [repo_uuid] = %s',$repositoryId); //jcg
switch ($this->sqlDriver["driver"]) {
case "sqlite":
case "sqlite3":
case "postgre":
$children_results = dibi::query('SELECT * FROM [ajxp_roles] WHERE [searchable_repositories] LIKE %~like~ GROUP BY [role_id]', '"'.$repositoryId.'";s:');
break;
case "mysql":
$children_results = dibi::query('SELECT * FROM [ajxp_roles] WHERE [serial_role] LIKE %~like~ GROUP BY [role_id]', '"'.$repositoryId.'";s:');
break;
default:
return "ERROR!, DB driver "+ $this->sqlDriver["driver"] +" not supported yet in __FUNCTION__";
}
$all = $children_results->fetchAll();
foreach ($all as $item) {
$role = unserialize($item["serial_role"]);
$role->setAcl($repositoryId, "");
$this->updateRole($role);
}
} catch (DibiException $e) {
return -1;
}
// Deleting a non-existent repository also qualifies as an error jcg Call to a member function getAffectedRows() on a non-object
/*
if (false === $result->getAffectedRows()) {
return -1;
}
*/
}
public function getUserChildren( $userId )
{
$children = array();
$children_results = dibi::query('SELECT [ajxp_users].[login] FROM [ajxp_user_rights],[ajxp_users] WHERE [repo_uuid] = %s AND [rights] = %s AND [ajxp_user_rights].[login] = [ajxp_users].[login]', "ajxp.parent_user", $userId);
$all = $children_results->fetchAll();
foreach ($all as $item) {
$children[] = $this->createUserObject($item["login"]);
}
return $children;
}
/**
* @param string $repositoryId
* @return array()
*/
public function getUsersForRepository($repositoryId)
{
$result = array();
// OLD METHOD
$children_results = dibi::query('SELECT [ajxp_users].[login] FROM [ajxp_user_rights],[ajxp_users] WHERE [repo_uuid] = %s AND [ajxp_user_rights].[login] = [ajxp_users].[login] GROUP BY [ajxp_users].[login]', $repositoryId);
$all = $children_results->fetchAll();
foreach ($all as $item) {
$result[$item["login"]] = $this->createUserObject($item["login"]);
}
// NEW METHOD : SEARCH PERSONAL ROLE
switch ($this->sqlDriver["driver"]) {
case "sqlite":
case "sqlite3":
case "postgre":
$children_results = dibi::query('SELECT [role_id] FROM [ajxp_roles] WHERE [searchable_repositories] LIKE %~like~ GROUP BY [role_id]', '"'.$repositoryId.'";s:');
break;
case "mysql":
$children_results = dibi::query('SELECT [role_id] FROM [ajxp_roles] WHERE [serial_role] LIKE %~like~ GROUP BY [role_id]', '"'.$repositoryId.'";s:');
break;
default:
return "ERROR!, DB driver "+ $this->sqlDriver["driver"] +" not supported yet in __FUNCTION__";
}
$all = $children_results->fetchAll();
foreach ($all as $item) {
$rId = $item["role_id"];
if (strpos($rId, "AJXP_USR/") == 0) {
$id = substr($rId, strlen("AJXP_USR/")+1);
$result[$id] = $this->createUserObject($id);
}
}
return $result;
}
// SAVE / EDIT / CREATE / DELETE USER OBJECT (except password)
/**
* Instantiate the right class
*
* @param AbstractAjxpUser $userId
*/
public function instantiateAbstractUserImpl($userId)
{
return new AJXP_SqlUser($userId, $this);
}
/**
* Get the full path to the Ajxp user class.
*
* @see AbstractConfDriver#getUserClassFileName()
*/
public function getUserClassFileName()
{
return AJXP_INSTALL_PATH."/plugins/conf.sql/class.AJXP_SqlUser.php";
}
public function listRoles($roleIds = array(), $excludeReserved = false)
{
$wClauses = array();
if (count($roleIds)) {
// We use (%s) instead of %in to pass everyting as string ('1' instead of 1)
$wClauses[] = array('[role_id] IN (%s)', $roleIds);
}
if ($excludeReserved) {
$wClauses[] = array('[role_id] NOT LIKE %like~', 'AJXP_');
}
$res = dibi::query('SELECT * FROM [ajxp_roles] %if', count($wClauses), 'WHERE %and', $wClauses);
$all = $res->fetchAll();
$roles = Array();
foreach ($all as $role_row) {
$id = $role_row['role_id'];
$serialized = $role_row['serial_role'];
$object = unserialize($serialized);
if (is_a($object, "AjxpRole") || is_a($object, "AJXP_Role")) {
$roles[$id] = $object;
}
}
return $roles;
}
/**
* @param AJXP_Role[] $roles
*/
public function saveRoles($roles)
{
dibi::query("DELETE FROM [ajxp_roles]");
foreach ($roles as $roleId => $roleObject) {
switch ($this->sqlDriver["driver"]) {
case "sqlite":
case "sqlite3":
case "postgre":
dibi::query("INSERT INTO [ajxp_roles] ([role_id],[serial_role],[searchable_repositories]) VALUES (%s, %bin, %s)", $roleId, serialize($roleObject), serialize($roleObject->listAcls()));
break;
case "mysql":
dibi::query("INSERT INTO [ajxp_roles] ([role_id],[serial_role]) VALUES (%s, %s)", $roleId, serialize($roleObject));
break;
default:
return "ERROR!, DB driver "+ $this->sqlDriver["driver"] +" not supported yet in __FUNCTION__";
}
}
}
/**
* @param AJXP_Role $role
*/
public function updateRole($role, $userObject = null)
{
dibi::query("DELETE FROM [ajxp_roles] WHERE [role_id]=%s", $role->getId());
switch ($this->sqlDriver["driver"]) {
case "sqlite":
case "sqlite3":
case "postgre":
dibi::query("INSERT INTO [ajxp_roles] ([role_id],[serial_role],[searchable_repositories]) VALUES (%s, %bin,%s)", $role->getId(), serialize($role), serialize($role->listAcls()));
break;
case "mysql":
dibi::query("INSERT INTO [ajxp_roles] ([role_id],[serial_role]) VALUES (%s, %s)", $role->getId(), serialize($role));
break;
default:
return "ERROR!, DB driver "+ $this->sqlDriver["driver"] +" not supported yet in __FUNCTION__";
}
}
/**
* @param AJXP_Role $role
*/
public function deleteRole($role)
{
// Mixed input Object or ID
if(is_a($role, "AJXP_Role")) $roleId = $role->getId();
else $roleId = $role;
dibi::query("DELETE FROM [ajxp_roles] WHERE [role_id]=%s", $roleId);
}
public function countAdminUsers()
{
$rows = dibi::query("SELECT COUNT(*) FROM ajxp_user_rights WHERE [repo_uuid] = %s AND [rights] = %s", "ajxp.admin", "1");
return $rows->fetchSingle();
}
/**
* @param AbstractAjxpUser[] $flatUsersList
* @param string $baseGroup
* @param bool $fullTree
* @return void
* @todo
*/
public function filterUsersByGroup(&$flatUsersList, $baseGroup = "/", $fullTree = false)
{
}
/**
* @param string $groupPath
* @param string $groupLabel
* @return mixed
*/
public function createGroup($groupPath, $groupLabel)
{
$test = dibi::query("SELECT COUNT(*) FROM [ajxp_groups] WHERE [groupPath] = %s", $groupPath);
if ($test->fetchSingle()) {
dibi::query("UPDATE [ajxp_groups] SET [groupLabel]=%s WHERE [groupPath]=%s", $groupLabel, $groupPath);
} else {
dibi::query("INSERT INTO [ajxp_groups]",array("groupPath" => $groupPath, "groupLabel" => $groupLabel));
}
}
public function relabelGroup($groupPath, $groupLabel)
{
dibi::query("UPDATE [ajxp_groups] SET [groupLabel]=%s WHERE [groupPath]=%s", $groupLabel, $groupPath);
}
public function deleteGroup($groupPath)
{
// Delete users of this group, as well as subgroups
$ids = array();
$res = dibi::query("SELECT * FROM [ajxp_users] WHERE [groupPath] LIKE %like~ ORDER BY [login] ASC", $groupPath);
$rows = $res->fetchAll();
$subUsers = array();
foreach ($rows as $row) {
$this->deleteUser($row["login"], $subUsers);
dibi::query("DELETE FROM [ajxp_users] WHERE [login] = %s", $row["login"]);
}
dibi::query("DELETE FROM [ajxp_groups] WHERE [groupPath] LIKE %like~", $groupPath);
dibi::query('DELETE FROM [ajxp_roles] WHERE [role_id] = %s', 'AJXP_GRP_'.$groupPath);
}
/**
* @param string $baseGroup
* @return string[]
*/
public function getChildrenGroups($baseGroup = "/")
{
$searchGroup = $baseGroup;
if($baseGroup[strlen($baseGroup)-1] != "/") $searchGroup.="/";
$res = dibi::query("SELECT * FROM [ajxp_groups] WHERE [groupPath] LIKE %like~ AND [groupPath] NOT LIKE %s", $searchGroup, $searchGroup."%/%");
$pairs = $res->fetchPairs("groupPath", "groupLabel");
foreach ($pairs as $path => $label) {
if(strlen($path) <= strlen($baseGroup)) unset($pairs[$path]);
if ($baseGroup != "/") {
unset($pairs[$path]);
$pairs[substr($path, strlen($baseGroup))] = $label;
}
}
return $pairs;
}
/**
* Function for deleting a user
*
* @param String $userId
* @param Array $deletedSubUsers
* @throws Exception
* @return void
*/
public function deleteUser($userId, &$deletedSubUsers)
{
$children = array();
try {
// FIND ALL CHILDREN FIRST
$children_results = dibi::query('SELECT [login] FROM [ajxp_user_rights] WHERE [repo_uuid] = %s AND [rights] = %s', "ajxp.parent_user", $userId);
$all = $children_results->fetchAll();
foreach ($all as $item) {
$children[] = $item["login"];
}
dibi::begin();
//This one is done by AUTH_DRIVER, not CONF_DRIVER
//dibi::query('DELETE FROM [ajxp_users] WHERE [login] = %s', $userId);
dibi::query('DELETE FROM [ajxp_user_rights] WHERE [login] = %s', $userId);
dibi::query('DELETE FROM [ajxp_user_prefs] WHERE [login] = %s', $userId);
dibi::query('DELETE FROM [ajxp_user_bookmarks] WHERE [login] = %s', $userId);
dibi::query('DELETE FROM [ajxp_roles] WHERE [role_id] = %s', 'AJXP_USR_/'.$userId);
dibi::commit();
foreach ($children as $childId) {
$this->deleteUser($childId, $deletedSubUsers);
$deletedSubUsers[] = $childId;
}
} catch (DibiException $e) {
throw new Exception('Failed to delete user, Reason: '.$e->getMessage());
}
}
protected function simpleStoreSet($storeID, $dataID, $data, $dataType = "serial", $relatedObjectId = null)
{
$values = array(
"store_id" => $storeID,
"object_id" => $dataID
);
if ($relatedObjectId != null) {
$values["related_object_id"] = $relatedObjectId;
}
if ($dataType == "serial") {
$values["serialized_data"] = serialize($data);
} else if ($dataType == "binary") {
$values["binary_data"] = $data;
} else {
throw new Exception("Unsupported format type ".$dataType);
}
dibi::query("DELETE FROM [ajxp_simple_store] WHERE [store_id]=%s AND [object_id]=%s", $storeID, $dataID);
dibi::query("INSERT INTO [ajxp_simple_store] ([object_id],[store_id],[serialized_data],[binary_data],[related_object_id]) VALUES (%s,%s,%bin,%bin,%s)",
$dataID, $storeID, $values["serialized_data"], $values["binary_data"], $values["related_object_id"]);
}
protected function simpleStoreClear($storeID, $dataID)
{
dibi::query("DELETE FROM [ajxp_simple_store] WHERE [store_id]=%s AND [object_id]=%s", $storeID, $dataID);
}
//$dataType = "serial"
protected function simpleStoreGet($storeID, $dataID, $dataType, &$data)
{
$children_results = dibi::query("SELECT * FROM [ajxp_simple_store] WHERE [store_id]=%s AND [object_id]=%s", $storeID, $dataID);
$value = $children_results->fetchAll();
if(!count($value)) return false;
$value = $value[0];
if ($dataType == "serial") {
$data = unserialize($value["serialized_data"]);
} else {
$data = $value["binary_data"];
}
if (isSet($value["related_object_id"])) {
return $value["related_object_id"];
} else {
return false;
}
}
protected function binaryContextToStoreID($context)
{
$storage = "binaries";
if (isSet($context["USER"])) {
$storage ="users_binaries.".$context["USER"];
} else if (isSet($context["REPO"])) {
$storage ="repos_binaries.".$context["REPO"];
} else if (isSet($context["ROLE"])) {
$storage ="roles_binaries.".$context["ROLE"];
} else if (isSet($context["PLUGIN"])) {
$storage ="plugins_binaries.".$context["PLUGIN"];
}
return $storage;
}
/**
* @param array $context
* @param String $fileName
* @param String $ID
* @return String $ID
*/
public function saveBinary($context, $fileName, $ID = null)
{
if (empty($ID)) {
$ID = substr(md5(microtime()*rand(0,100)), 0, 12);
$ID .= ".".pathinfo($fileName, PATHINFO_EXTENSION);
}
$store = $this->binaryContextToStoreID($context);
$this->simpleStoreSet($store, $ID, file_get_contents($fileName), "binary");
return $ID;
}
/**
* @abstract
* @param array $context
* @param String $ID
* @return boolean
*/
public function deleteBinary($context, $ID)
{
$store = $this->binaryContextToStoreID($context);
$this->simpleStoreClear($store, $ID);
}
/**
* @param array $context
* @param String $ID
* @param Resource $outputStream
* @return boolean
*/
public function loadBinary($context, $ID, $outputStream = null)
{
$store = $this->binaryContextToStoreID($context);
$data = "";
$this->simpleStoreGet($store, $ID, "binary", $data);
if ($outputStream != null) {
fwrite($outputStream, $data, strlen($data));
} else {
header("Content-Type: ".AJXP_Utils::getImageMimeType($ID));
echo $data;
}
}
/**
* @abstract
* @param String $keyType
* @param String $keyId
* @param String $userId
* @param Array $data
* @return boolean
*/
public function saveTemporaryKey($keyType, $keyId, $userId, $data)
{
$this->simpleStoreSet("temporakey_".$keyType, $keyId, $data, "serial", $userId);
}
/**
* @abstract
* @param String $keyType
* @param String $keyId
* @return array
*/
public function loadTemporaryKey($keyType, $keyId)
{
$data = array();
$userId = $this->simpleStoreGet("temporakey_".$keyType, $keyId, "serial", $data);
$data['user_id'] = $userId;
return $data;
}
/**
* @abstract
* @param String $keyType
* @param String $keyId
* @return boolean
*/
public function deleteTemporaryKey($keyType, $keyId)
{
$this->simpleStoreClear("temporakey_".$keyType, $keyId);
}
/**
* @abstract
* @param String $keyType
* @param String $expiration
* @return null
*/
public function pruneTemporaryKeys($keyType, $expiration)
{
dibi::query("DELETE FROM [ajxp_simple_store] WHERE [store_id] = %s AND [insertion_date] < (CURRENT_TIMESTAMP - %i)", "temporakey_".$keyType, $expiration*60);
}
public function installSQLTables($param)
{
$p = AJXP_Utils::cleanDibiDriverParameters($param["SQL_DRIVER"]);
return AJXP_Utils::runCreateTablesQuery($p, $this->getBaseDir()."/create.sql");
}
public function supportsUserTeams()
{
return true;
}
public function listUserTeams()
{
if (AuthService::getLoggedUser() == null) {
return array();
}
dibi::query("DELETE FROM [ajxp_user_teams] WHERE [user_id] NOT IN (SELECT [login] FROM [ajxp_users])");
$res = dibi::query("SELECT * FROM [ajxp_user_teams] WHERE [owner_id] = %s ORDER BY [team_id]", AuthService::getLoggedUser()->getId());
$data = $res->fetchAll();
$all = array();
foreach ($data as $row) {
$teamId = $row["team_id"];
$userId = $row["user_id"];
$teamLabel = $row["team_label"];
if(!isSet($all[$teamId])) $all[$teamId] = array("LABEL" => $teamLabel, "USERS" => array());
$all[$teamId]["USERS"][$userId] = $userId;
}
return $all;
}
public function teamIdToUsers($teamId)
{
$res = array();
$teams = $this->listUserTeams();
$teamData = $teams[$teamId];
foreach ($teamData["USERS"] as $userId) {
if (AuthService::userExists($userId)) {
$res[] = $userId;
} else {
$this->removeUserFromTeam($teamId, $userId);
}
}
return $res;
}
private function addUserToTeam($teamId, $userId, $teamLabel = null)
{
if ($teamLabel == null) {
$res = dibi::query("SELECT [team_label] FROM [ajxp_user_teams] WHERE [team_id] = %s AND [owner_id] = %s",
$teamId, AuthService::getLoggedUser()->getId());
$teamLabel = $res->fetchSingle();
}
dibi::query("INSERT INTO [ajxp_user_teams] ([team_id],[user_id],[team_label],[owner_id]) VALUES (%s,%s,%s,%s)",
$teamId, $userId, $teamLabel, AuthService::getLoggedUser()->getId()
);
}
private function removeUserFromTeam($teamId, $userId = null)
{
if ($userId == null) {
dibi::query("DELETE FROM [ajxp_user_teams] WHERE [team_id] = %s AND [owner_id] = %s",
$teamId, AuthService::getLoggedUser()->getId()
);
} else {
dibi::query("DELETE FROM [ajxp_user_teams] WHERE [team_id] = %s AND [user_id] = %s AND [owner_id] = %s",
$teamId, $userId, AuthService::getLoggedUser()->getId()
);
}
}
public function userTeamsActions($actionName, $httpVars, $fileVars)
{
switch ($actionName) {
case "user_team_create":
$userIds = $httpVars["user_ids"];
$teamLabel = $httpVars["team_label"];
$teamId = AJXP_Utils::slugify($teamLabel)."-".intval(rand(0,1000));
foreach ($userIds as $userId) {
$this->addUserToTeam($teamId, $userId, $teamLabel);
}
echo 'Created Team $teamId';
break;
case "user_team_delete":
$this->removeUserFromTeam($httpVars["team_id"], null);
break;
case "user_team_add_user":
$this->addUserToTeam($httpVars["team_id"], $httpVars["user_id"], null);
break;
case "user_team_delete_user":
$this->removeUserFromTeam($httpVars["team_id"], $httpVars["user_id"]);
break;
}
}
}