1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: require_once("./always.php");
13: require_once("DAViCalSession.php");
14: $session->LoginRequired();
15:
16: require_once("DataEntry.php");
17: require_once("interactive-page.php");
18: require_once("classBrowser.php");
19:
20: require_once("caldav-PUT-functions.php");
21: include_once('check_UTF8.php');
22:
23: if ( !$session->AllowedTo("Admin" ) ) {
24: @ob_flush(); exit(0);
25: }
26: if( function_exists("sync_LDAP") && isset($_POST['Sync_LDAP'])){
27: sync_LDAP();
28: }
29:
30: if( function_exists("sync_LDAP_groups") && isset($_POST['Sync_LDAP_groups'])){
31: sync_LDAP_groups();
32: }
33:
34: if(isset($_POST['import_from_directory'])){
35: Tools::importFromDirectory();
36: }
37:
38:
39: class Tools {
40:
41: function render(){
42: global $c;
43: echo $this->renderImportFromDirectory();
44: if ( isset($c->authenticate_hook['call']) && $c->authenticate_hook['call'] == 'LDAP_check' && function_exists("sync_LDAP") ) {
45: echo $this->renderSyncLDAP();
46: }
47: }
48:
49: static function renderSyncLDAP(){
50: $html = '<div id="entryform">';
51: $html .= '<h1>'.translate('Sync LDAP with DAViCal') .'</h1>';
52:
53: $data = (object) array('directory_path' => '/path/to/your/ics/files','calendar_path' => 'home');
54: $ef = new EntryForm( $_SERVER['REQUEST_URI'],$data , true,true );
55: $html .= "<table width=\"100%\" class=\"data\">\n";
56: $html .= $ef->StartForm( array("autocomplete" => "off" ) );
57: $html .= sprintf( "<tr><td style=\"text-align:left\" colspan=\"2\" >%s</td></tr>\n",
58: translate("This operation does the following: <ul><li>check valid users in LDAP directory</li> <li>check users in DAViCal</li></ul> then <ul><li>if a user is present in DAViCal but not in LDAP set him as inactive in DAViCal</li> <li>if a user is present in LDAP but not in DAViCal create the user in DAViCal</li> <li>if a user in present in LDAP and DAViCal then update information in DAViCal</li> </ul>"));
59: $html .= "</table>\n";
60:
61: $html .= $ef->SubmitButton( "Sync_LDAP", translate('Submit'));
62:
63: $html .= '<h1>'.translate('Sync LDAP Groups with DAViCal') .'</h1>';
64: $html .= "<table width=\"100%\" class=\"data\">\n";
65: $html .= $ef->StartForm( array("autocomplete" => "off" ) );
66: $html .= sprintf( "<tr><td style=\"text-align:left\" colspan=\"2\" >%s</td></tr>\n",
67: translate("This operation does the following: <ul><li>check valid groups in LDAP directory</li> <li>check groups in DAViCal</li></ul> then <ul><li>if a group is present in DAViCal but not in LDAP set as inactive in DAViCal</li> <li>if a group is present in LDAP but not in DAViCal create the group in DAViCal</li> <li>if a group in present in LDAP and DAViCal then update information in DAViCal</li> </ul>"));
68: $html .= "</table>\n";
69:
70: $html .= $ef->SubmitButton( "Sync_LDAP_groups", translate('Submit'));
71: $html .= $ef->EndForm();
72:
73: $html .= "</div>";
74: return $html;
75: }
76:
77: static function renderImportFromDirectory(){
78: $html = '<div id="entryform">';
79: $html .= '<h1>'.translate('Import all .ics files of a directory') .'</h1>';
80: $html .= '<p>'.translate('This process will import each file in a directory named "username.ics" and create a user and calendar for each file to import.') .'</p>';
81:
82: $data = (object) array('directory_path' => '/path/to/your/ics/files','calendar_path' => 'calendar');
83: $ef = new EntryForm( $_SERVER['REQUEST_URI'],$data , true,true );
84: $html .= "<table width=\"100%\" class=\"data\">\n";
85: $html .= $ef->StartForm( array("autocomplete" => "off" ) );
86:
87: $html .= $ef->DataEntryLine( translate("path to store your ics"), "%s", "text", "calendar_path",
88: array( "size" => 20,
89: "title" => translate("Set the path to store your ics e.g. 'calendar' will be referenced as /caldav.php/username/calendar/"),
90: "help" => translate("<b>WARNING: all events in this path will be deleted before inserting allof the ics file</b>")
91: )
92: , '' );
93:
94: $html .= $ef->DataEntryLine( translate("Directory on the server"), "%s", "text", "directory_path",
95: array( "size" => 20, "title" => translate("The path on the server where your .ics files are.")));
96:
97: $html .= "</table>\n";
98: $html .= $ef->SubmitButton( "import_from_directory", translate('Submit'));
99: $html .= $ef->EndForm();
100:
101: $html .= "</div>";
102: return $html;
103: }
104:
105: static function importFromDirectory(){
106: global $c;
107: if(empty($_POST["calendar_path"])){
108: dbg_error_log( "importFromDirectory", "calendar path not given");
109: return ;
110: }
111: $path_ics = $_POST["calendar_path"];
112: if ( substr($path_ics,-1,1) != '/' ) $path_ics .= '/';
113: if ( substr($path_ics,0,1) != '/' ) $path_ics = '/'.$path_ics;
114:
115: if(empty($_POST["directory_path"])){
116: dbg_error_log( "importFromDirectory", "directory path not given");
117: return ;
118: }
119: $dir = $_POST["directory_path"];
120: if(!is_readable($dir)){
121: $c->messages[] = sprintf(i18n('directory %s is not readable'),htmlspecialchars($dir));
122: dbg_error_log( "importFromDirectory", "directory is not readable");
123: return ;
124: }
125: if ($handle = opendir($dir)) {
126: $c->readonly_webdav_collections = false;
127: while (false !== ($file = readdir($handle))) {
128: if ($file == "." || $file == ".." || substr($file,-4) != '.ics') continue;
129: if ( !is_readable($dir.'/'.$file) ) {
130: dbg_error_log( "importFromDirectory", "ics file '%s' is not readable",$dir .'/'.$file);
131: continue;
132: }
133: $ics = file_get_contents($dir.'/'.$file);
134: $ics = trim($ics);
135:
136:
137: if ( $ics != '' ) {
138: if ( ! check_string($ics) ) {
139: $c->messages[] = sprintf(translate('The file "%s" is not UTF-8 encoded, please check error for more details'),$dir.'/'.$file);
140: continue;
141: }
142: $username = substr($file,0,-4);
143: $principal = new Principal('username',$username);
144: if ( !$principal->Exists() ) {
145: $c->messages[] = sprintf(translate('The principal "%s" does not exist'),$username);
146: continue;
147: }
148: $path = "/".$username.$path_ics;
149: $user_no = $principal->user_no();
150: if ( controlRequestContainer($username, $user_no, $path, false) === -1)
151: continue;
152: dbg_error_log( "importFromDirectory", "importing to $path");
153: import_collection($ics,$user_no,$path,1);
154: $c->messages[] = sprintf(translate('All events of user "%s" were deleted and replaced by those from file %s'),substr($file,0,-4),$dir.'/'.$file);
155: }
156: }
157: closedir($handle);
158: }
159: }
160: }
161:
162: $Tools = new Tools();
163:
164: include("page-header.php");
165: $Tools->render();
166: include("page-footer.php");
167: