-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnephthys.class.php
2898 lines (2409 loc) · 83.9 KB
/
nephthys.class.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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/***************************************************************************
*
* Nephthys - file sharing management
* Copyright (c) by Andreas Unterkircher, [email protected]
*
* This file is part of Nephthys.
*
* Nephthys is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Nephthys 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nephthys. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
require_once "nephthys_db.php";
require_once "nephthys_buckets.php";
require_once "nephthys_addressbook.php";
require_once "nephthys_users.php";
require_once "nephthys_profile.php";
require_once "nephthys_upload.php";
class NEPHTHYS {
public $cfg;
public $db;
public $tmpl;
public $current_user;
public $browser_info;
public $sort_order;
private $runtime_error = false;
private $_translationTable; // currently loaded translation table
private $_loadedTranslationTables; // array of all loaded translation tables
/**
* class constructor
*
* this function will be called on class construct
* and will check requirements, loads configuration,
* open databases and start the user session
*/
public function __construct()
{
$GLOBALS['nephthys'] =& $this;
/* load config, exit if it fails */
if(!$this->load_config()) {
$this->_error("Error during load_config()");
exit(1);
}
// if servername has not been set in the configuration
// get it from the webserver. Only necessary if not
// called from command line.
if(!isset($this->cfg->servername) && !$this->is_cmdline()) {
if(!isset($_SERVER['SERVER_NAME']))
die("Can't get server name out of \$_SERVER['SERVER_NAME']");
$this->cfg->servername = $_SERVER['SERVER_NAME'];
}
/* Check necessary requirements */
if(!$this->checkRequirements()) {
exit(1);
}
$this->browser_info = new Net_UserAgent_Detect();
/* verify if browser supports javascript
... if not called from command line
... if not called via RPC handler
... and ignore Javascript check is not set
*/
if(!$this->is_cmdline() && !defined('RPC_CALL') && (!isset($this->cfg->ignore_js) || empty($this->cfg->ignore_js))) {
if(!$this->browser_info->hasFeature('javascript')) {
$this->_error("It seems your browser is not capable of supporting JavaScript or it has been disabled.");
$this->_error("Nephthys will not work without JavaScript!");
exit;
}
}
/* if database type is set to sqlite, database exists
but is not readable ...
*/
if($this->cfg->db_type == "sqlite" &&
file_exists($this->cfg->sqlite_path) &&
!is_readable($this->cfg->sqlite_path)) {
$this->_error("[". $this->cfg->sqlite_path ."] SQLite database is not readable for user ". $this->getuid());
exit(1);
}
/* if database type is set to sqlite, database exists
but is not writeable ...
*/
if($this->cfg->db_type == "sqlite" &&
file_exists($this->cfg->sqlite_path) &&
!is_writable($this->cfg->sqlite_path)) {
$this->_error("[". $this->cfg->sqlite_path ."] SQLite database is not writeable for user ". $this->getuid());
exit(1);
}
/* if database type is set to sqlite, database does not exist
yet and directory to store database is not writeable...
*/
if($this->cfg->db_type == "sqlite" &&
!file_exists($this->cfg->sqlite_path) &&
!is_writable(dirname($this->cfg->sqlite_path))) {
$this->_error("[". $this->cfg->sqlite_path ."] SQLite database can not be created in directory by user ". $this->getuid());
exit(1);
}
$this->db = new NEPHTHYS_DB();
$this->check_db_tables();
if(!is_writable($this->cfg->tmpl_path ."/templates_c")) {
$this->_error("[". $this->cfg->tmpl_path ."/templates_c] directory is not writeable for user ". $this->getuid());
exit(1);
}
/* check if the bucket root directory ($data_path) exists */
if(!file_exists($this->cfg->data_path)) {
$this->_error("[". $this->cfg->data_path ."] directory does not exist");
exit(1);
}
/* check if the webservers user is allowed to modify the bucket
root directory ($data_path). This is necessary to create &
delete bucket directories.
*/
if(!is_writeable($this->cfg->data_path)) {
$this->_error("[". $this->cfg->data_path ."] directory is not writeable for user ". $this->getuid());
exit(1);
}
/* if session is not yet started, do it now */
if(session_id() == "")
session_start();
/*if(!isset($_SERVER['REMOTE_USER']) || empty($_SERVER['REMOTE_USER'])) {
$this->parent->_error("It seems you are not authenticated through the server");
exit(1);
}
*/
if(!$this->is_cmdline() &&
isset($this->cfg->allow_server_auth) && $this->cfg->allow_server_auth == true
&& (!isset($_SERVER['REMOTE_USER']) || empty($_SERVER['REMOTE_USER']))) {
$this->_error("Server authentication is enabled in Nephthys config but server does not "
."provide details in REMOTE_USER variable.");
exit(1);
}
/* if server-authentication is allowed... */
if(isset($this->cfg->allow_server_auth) &&
$this->cfg->allow_server_auth == true) {
/* if the user exists in Nephthys user table ... */
if($user = $this->get_user_details_by_name($_SERVER['REMOTE_USER'])) {
/* if user is active, register informations to session */
if($user->user_active == 'Y') {
$_SESSION['login_name'] = $user->user_name;
$_SESSION['login_idx'] = $user->user_idx;
/* update the last login time of this user */
$this->update_last_login($user->user_idx);
}
}
/* otherwise, if auto-creation is enabled, create it... */
else {
/* is user-auto-creation enabled? */
if(isset($this->cfg->user_auto_create) &&
$this->cfg->user_auto_create == true) {
if(isset($_SERVER['REMOTE_USER']) &&
$idx = $this->create_user($_SERVER['REMOTE_USER'])) {
if($user = $this->get_user_details_by_idx($idx)) {
$_SESSION['login_name'] = $user->user_name;
$_SESSION['login_idx'] = $user->user_idx;
/* update the last login time of this user */
$this->update_last_login($user->user_idx);
}
}
}
}
}
else {
/* local authentication, if login data is already available */
if(isset($_SESSION['login_idx']) && is_numeric($_SESSION['login_idx']))
$user = $this->get_user_details_by_idx($_SESSION['login_idx']);
}
/* if the user-object was found in database (or has been auto-created)
and the user has selected its own prefered language...
*/
if(isset($user) &&
isset($user->user_language) &&
!empty($user->user_language) &&
in_array($user->user_language, array_keys($this->cfg->avail_langs))) {
$this->cfg->language = $user->user_language;
}
/* load translation table for the current language */
$this->load_translation_table();
/* overload Smarty class if our own template handler */
require_once "nephthys_tmpl.php";
$this->tmpl = new NEPHTHYS_TMPL();
if(isset($user->user_email) && !empty($user->user_email))
$this->tmpl->assign('login_email', $user->user_email);
/* if browser is type Internet Explorer set a template variable to
inidicate to templates that browser is IE.
*/
if(isset($this->browser_info) && $this->browser_info->isIE())
$this->tmpl->assign('is_ie', true);
if($this->browser_info->getOS(array("vista", "win7"))) {
$this->tmpl->assign('is_vista', true);
}
$this->tmpl->assign('hide_logout', $this->cfg->hide_logout);
$this->tmpl->assign('disk_used', $this->get_unit($this->get_used_diskspace()));
$this->tmpl->assign('disk_free', $this->get_unit($this->get_free_diskspace()));
/* pre-define default sort order, if not set yet */
if(!isset($_SESSION['sort_order']) || !is_array($_SESSION['sort_order'])) {
$_SESSION['sort_order'] = Array(
'buckets' => Array(
'column' => 'bucket_name',
'order' => 'asc'),
'addressbook' => Array(
'column' => 'contact_name',
'order' => 'asc'),
'users' => Array(
'column' => 'user_name',
'order' => 'asc'),
);
}
} // __construct()
public function __destruct()
{
} // __destruct()
/**
* init - generate html output
*
* this function can be called after the constructor has prepared
* everyhing. it will load the index.tpl Smarty template and exit
* successfully.
*/
public function init()
{
print $this->tmpl->fetch("index.tpl");
exit(0);
} // init()
/**
* outputs the main content template
*/
public function show()
{
return $this->tmpl->fetch("main.tpl");
} // show()
/**
* outputs the menu template()
*/
public function get_menu()
{
return $this->tmpl->fetch("menu.tpl");
} // get_menu()
/**
* return main content
*/
public function get_content()
{
/* if no user-login yet, show the login box */
if(!$this->is_logged_in()) {
return $this->tmpl->fetch("login_box.tpl");
}
else {
/* if the user has been auto-created, but its email address has not
been set yet - and the nephthys config option
$force_profile_update
is set to true, forward the user to the profile page instead of
everything else.
*/
if(isset($this->cfg->force_profile_update) &&
!empty($this->cfg->force_profile_update) &&
$this->is_auto_created($_SESSION['login_idx']) &&
!$this->get_user_email($_SESSION['login_idx'])) {
$request = "profile";
}
}
/* if the requests has not been overruled yet */
if(!isset($request)) {
/* page-id via HTTP GET */
if(isset($_GET['id']) && is_string($_GET['id']))
$request = $_GET['id'];
/* page-id via HTTP POST */
if(isset($_POST['id']) && is_string($_POST['id']))
$request = $_POST['id'];
}
switch($request) {
case 'main':
$obj = $this;
break;
case 'users':
$obj = new NEPHTHYS_USERS();
break;
case 'buckets':
$obj = new NEPHTHYS_BUCKETS();
break;
case 'profile':
$obj = new NEPHTHYS_PROFILE();
break;
case 'addressbook':
$obj = new NEPHTHYS_ADDRESSBOOK();
break;
case 'about':
return $this->tmpl->fetch("about.tpl");
break;
case 'help':
return $this->tmpl->fetch("help.tpl");
break;
case 'savedbucket':
$obj = new NEPHTHYS_BUCKETS();
return $obj->showBucket();
break;
}
if(isset($obj))
return $obj->show();
} // get_content()
public function store()
{
if(!$this->is_logged_in()) {
return "login first";
}
if(isset($_POST['module'])) {
switch($_POST['module']) {
case 'users':
$obj = new NEPHTHYS_USERS;
break;
case 'buckets':
$obj = new NEPHTHYS_BUCKETS;
break;
case 'profile':
$obj = new NEPHTHYS_PROFILE;
break;
case 'addressbook':
$obj = new NEPHTHYS_ADDRESSBOOK;
break;
default:
return "unkown module";
break;
}
if(isset($obj)) {
switch($_POST['mode']) {
case 'modify': return $obj->store(); break;
case 'delete': return $obj->delete(); break;
case 'toggle': return $obj->toggleStatus(); break;
}
}
}
} // store()
/**
* check if all requirements are met
*/
private function checkRequirements()
{
/* Check for HTML_AJAX PEAR package, lent from Horde project */
ini_set('track_errors', 1);
@include_once 'HTML/AJAX/Server.php';
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("PEAR HTML_AJAX package is missing");
$missing = true;
}
@include_once 'MDB2.php';
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("PEAR MDB2 package is missing");
$missing = true;
unset($php_errormsg);
}
// If database type is set to MySQL
if($this->cfg->db_type == "mysql") {
@include_once 'MDB2/Driver/mysqli.php';
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("PEAR MDB2-mysqli package is missing");
$missing = true;
unset($php_errormsg);
}
}
// If database type is set to SQLite
if($this->cfg->db_type == "sqlite") {
@include_once 'MDB2/Driver/sqlite.php';
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("PEAR MDB2-sqlite package is missing");
$missing = true;
unset($php_errormsg);
}
}
@include_once 'Mail.php';
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("PEAR Mail package is missing");
$missing = true;
unset($php_errormsg);
}
@include_once 'Net/UserAgent/Detect.php';
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("PEAR Net_UserAgent_Detect package is missing");
$missing = true;
unset($php_errormsg);
}
@include_once 'Console/Getopt.php';
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("PEAR Console_Getopt package is missing");
$missing = true;
unset($php_errormsg);
}
@include_once $this->cfg->smarty_path .'/libs/Smarty.class.php';
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("Smarty template engine can not be found in ". $this->cfg->smarty_path ."/libs/Smarty.class.php");
$missing = true;
unset($php_errormsg);
}
ini_restore('track_errors');
if(isset($missing))
return false;
return true;
} // checkRequirements()
/**
* return the type of protocol used
*
* this function returns wether HTTP or HTTPS
* is used for the client connection.
*
* @return string
*/
private function get_web_protocol()
{
if(!isset($_SERVER['HTTPS']))
return "http";
else
return "https";
} // get_web_protocol()
/**
* return url to this installation
*
* @return string
*/
private function get_nephthys_url()
{
return $this->get_web_protocol() ."://". $this->get_server_name() . $this->cfg->web_path;
} // get_nephthys_url()
/**
* check file exists and is readable
*
* returns true, if everything is ok, otherwise false
* if $silent is not set, this function will output and
* error message
*/
private function check_readable($file, $silent = null)
{
if(!file_exists($file)) {
if(!isset($silent))
print "File \"". $file ."\" does not exist.\n";
return false;
}
if(!is_readable($file)) {
if(!isset($silent))
print "File \"". $file ."\" is not reachable for user ". $this->getuid() ."\n";
return false;
}
return true;
} // check_readable()
/**
* validate config options
*
* this function checks if all necessary configuration options are
* specified and set.
*/
private function check_config_options()
{
if(!isset($this->cfg->page_title) || $this->cfg->page_title == "")
$this->_error("Please set \$page_title in nephthys_cfg");
if(!isset($this->cfg->base_path) || $this->cfg->base_path == "")
$this->_error("Please set \$base_path in nephthys_cfg");
if(!isset($this->cfg->web_path) || $this->cfg->web_path == "")
$this->_error("Please set \$web_path in nephthys_cfg");
if(!isset($this->cfg->smarty_path) || $this->cfg->smarty_path == "")
$this->_error("Please set \$smarty_path in nephthys_cfg");
if(!isset($this->cfg->theme_name))
$this->_error("Please set \$theme_name in nephthys_cfg");
if(!isset($this->cfg->mysql_host))
$this->_error("Please set \$mysql_host in nephthys_cfg");
if(!isset($this->cfg->mysql_db))
$this->_error("Please set \$mysql_db in nephthys_cfg");
if(!isset($this->cfg->mysql_user))
$this->_error("Please set \$mysql_user in nephthys_cfg");
if(!isset($this->cfg->mysql_pass))
$this->_error("Please set \$mysql_pas in nephthys_cfg");
if(!isset($this->cfg->logging))
$this->_error("Please set \$logging in nephthys_cfg");
if(isset($this->cfg->logging) && $this->cfg->logging == 'logfile') {
if(!isset($this->cfg->log_file))
$this->_error("Please set \$log_file because you set logging = log_file in nephthys_cfg");
if(!is_writable($this->cfg->log_file))
$this->_error("The specified \$log_file ". $log_file ." is not writeable!");
}
/* check for pending slash on web_path */
if(!preg_match("/\/$/", $this->cfg->web_path))
$this->cfg->web_path.= "/";
return $this->runtime_error;
} // check_config_options()
/**
* return the current process-user
*/
private function getuid()
{
if($uid = posix_getuid()) {
if($user = posix_getpwuid($uid)) {
return $user['name'];
}
}
return 'n/a';
} // getuid()
/**
* returns the email address of the provided user id
* @param int $user_idx
* @return string
*/
public function get_user_email($user_idx)
{
$row = $this->db->db_fetchSingleRow("
SELECT
user_email
FROM
nephthys_users
WHERE
user_idx='". $user_idx ."'
");
if(isset($row->user_email)) {
return $row->user_email;
}
return NULL;
} // get_user_email()
/**
* return current users email address
*
* this function returns the email address of the
* currently logged in user.
* @return string
*/
public function get_my_email()
{
/* if no user is logged in yet, return */
if(!isset($_SESSION['login_name']))
return NULL;
$row = $this->db->db_fetchSingleRow("
SELECT user_email
FROM nephthys_users
WHERE user_name LIKE '". $_SESSION['login_name'] ."'
");
if(isset($row->user_email)) {
return $row->user_email;
}
return NULL;
} // get_my_email()
/**
* return all user details for the provided user_name
*/
private function get_user_details_by_name($user_name)
{
if($user = $this->db->db_fetchSingleRow("
SELECT *
FROM nephthys_users
WHERE
user_name LIKE '". $user_name ."'")) {
return $user;
}
return NULL;
} // get_user_detail_by_name()
/**
* return all user details for the provided user_idx
*/
private function get_user_details_by_idx($user_idx)
{
if($user = $this->db->db_fetchSingleRow("
SELECT *
FROM nephthys_users
WHERE
user_idx LIKE '". $user_idx ."'")) {
return $user;
}
return NULL;
} // get_user_details()
/**
* returns user name
*
* @return string
*/
public function get_user_name($user_idx)
{
if($user = $this->get_user_details_by_idx($user_idx)) {
return $user->user_name;
}
return NULL;
} // get_user_name()
/**
* return the specified users full name
*
* @return string
*/
public function get_user_fullname($user_idx)
{
if($user = $this->get_user_details_by_idx($user_idx)) {
return $user->user_full_name;
}
return NULL;
} // get_user_fullname()
/**
* returns user privilege
*/
public function get_user_priv($user_idx)
{
if($user = $this->get_user_details_by_idx($user_idx)) {
return $user->user_priv;
}
return NULL;
} // get_user_priv()
/**
* returns users default expiration time
*/
public function get_user_expire($user_idx)
{
if($user = $this->get_user_details_by_idx($user_idx)) {
return $user->user_default_expire;
}
return NULL;
} // get_user_expire()
/**
* returns true if a user is logged in, otherwise false
*/
public function is_logged_in()
{
if(isset($_SESSION['login_name']) && !empty($_SESSION['login_name']) &&
$this->is_valid_user($_SESSION['login_name'])) {
return true;
}
return false;
} // is_logged_in()
/**
* return true if the user exists
*/
private function is_valid_user($user_name)
{
if($this->db->db_fetchSingleRow("
SELECT user_idx
FROM nephthys_users
WHERE user_name LIKE '". $user_name ."'
")) {
return true;
}
return false;
} // is_valid_user()
/***
* validates all provided email addresses.
* multiple email addresses are seperated by comma
*
* @param string $email
* @return boolean
*/
public function is_valid_email($email)
{
/* only one email address? */
if(strstr($email, ',') === false)
return $this->validate_email($email);
/* multiple email addresses */
$emails = preg_split("/,/", $email);
if(!is_array($emails) || empty($emails))
return false;
foreach($emails as $email_addr) {
$email_addr = trim($email_addr);
/* return as soon as an invalid address has been found */
if(!$this->validate_email($email_addr))
return false;
}
return true;
} // is_valid_email()
/***
* verify email address
*
* found on: http://www.ilovejackdaniels.com/php/email-address-validation/
*/
public function validate_email($email)
{
/* if email has been entered in the format
fullname <email-address>
then we need to extract the address first
*/
if(preg_match('/^(.+)\s\<(.+)\>/', $email, $matches)) {
$email = $matches[2];
}
//if php version < 5.2
if ( version_compare( phpversion(), "5.2","<" ) ) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
return false;
}
}
if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", trim($email_array[1]));
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
return false;
}
}
} else {
//regular expression verifies that each component is a number from 1 to 3 characters in length
if (!preg_match("/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/", $email_array[1])){
return false;
}
}
} else if ( filter_var( $email, FILTER_VALIDATE_EMAIL ) === false ){
return false;
}
return true;
} // validate_email()
/**
* generates a SHA-1 hash from the provided parameters
* and some random stuff
*/
public function get_sha_hash($sender, $receiver = false)
{
if(!$receiver)
$receiver = mktime();
return sha1($sender . $receiver . rand(0, 32768));
} // get_sha_hash()
public function notifybucket()
{
if(isset($_POST['id']) && is_numeric($_POST['id'])) {
$bucket = new NEPHTHYS_BUCKETS($_POST['id']);
return $bucket->notify();
}
return "unkown bucket";
} // notifybucket()
/**
* load Nephthys configuration file
*/
private function load_config()
{
ini_set('track_errors', 1);
@include_once "nephthys_cfg.php";
if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
$this->_error("Can't read nephthys_cfg.php or have no permission to do it. Follow the documentation");
$this->_error("create nephthys_cfg.php from nephthys_cfg.php.dist");
return false;
}
ini_restore('track_errors');
$this->cfg = new NEPHTHYS_CFG;
/* verify config settings */
if($this->check_config_options()) {
return false;
}
/* set application name and version information */
$this->cfg->product = "Nephthys";
$this->cfg->version = "1.6";
$this->cfg->db_version = 7;
return true;
} // load_config()
/**
* check login
*
* this function gets called via RPC to verify users entered
* credential informations and permit or deny finally login.
* @return string
*/
public function login()
{
if(isset($_POST['login_name']) && !empty($_POST['login_name']) &&
isset($_POST['login_pass']) && !empty($_POST['login_pass'])) {
/* get user details */
if($user = $this->get_user_details_by_name($_POST['login_name'])) {
/* reject inactive users */
if($user->user_active != 'Y')
return $this->_("##FAILURE_USER_LOGON##");
/* do not allow auto-created users to login (they have no password set...) */
if($user->user_auto_created != 'Y' &&
$user->user_pass == sha1($_POST['login_pass'])) {
$_SESSION['login_name'] = $_POST['login_name'];
$_SESSION['login_idx'] = $user->user_idx;
/* update the last login time of this user */
$this->update_last_login($user->user_idx);
return "ok";
}
else {
return $this->_("##FAILURE_PASSWORD##");
}
}
else {
return $this->_("##FAILURE_USER_LOGON##");
}
}
else {
return $this->_("##FAILURE_USER_PASS##");
}
} // check_login()
/**
* destroy the current user session to force logout
*/
public function logout()
{
foreach($_SESSION as $k => $v) {
unset($_SESSION[$k]);
}
session_destroy();
return "ok";
} // destroySession()
/**
* returns true if the requests user privilege is matching
* with the actually user privileges
*/
public function check_privileges($priv)
{
if($user = $this->get_user_details_by_idx($_SESSION['login_idx'])) {
if($user->user_priv == $priv)
return true;
}
return false;
} // check_privileges()
/**
* user has permission for long-time buckets
*
* this function returns true, if the user is allowed to
* create long-time buckets while he has only "user" privileges.
*/
public function has_bucket_privileges()
{
if($user = $this->get_user_details_by_idx($_SESSION['login_idx'])) {
if($user->user_priv_expire == 'Y')
return true;
}
return false;
} // has_bucket_privileges()
/**
* returns true, if user is owner of the supplied bucket