summaryrefslogtreecommitdiff
path: root/www/admin/user/activesync.php
blob: a0b6c0c3237d14914731ca2e32ac7247afffdf76 (plain)
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
<?php
/*
 *  Copyright (c) 2004 Klarälvdalens Datakonsult AB
 *
 *    Written by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
 *    
 *	(c) 2011 Bogomil Shopov <shopov@kolabsys.com>
 *
 *  This  program 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 2, or
 *  (at your option) any later version.
 *
 *  This program 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 can view the  GNU General Public License, online, at the GNU
 *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
 */

require_once('KolabAdmin/include/mysmarty.php');
require_once('KolabAdmin/include/headers.php');
require_once('KolabAdmin/include/locale.php');
require_once('KolabAdmin/include/authenticate.php');
require_once('KolabAdmin/include/form.class.php');
require_once('KolabAdmin/include/passwd.php');

//try to include ALL possible configuration files
@include_once '/kolab/var/kolab/www/z-push/config.php';
@include_once '/etc/z-push/config.php';
@include_once '/usr/share/z-push/config.php';
@include_once '/var/www/z-push/config.php';



//define errors array
$errors = array();

if((@include_once 'Horde/Kolab/Kolab_Zpush/lib/kolabActivesyncData.php') === false ) {
	//z-Push in not installed. Why don't you show some scarry warining?
	$errors[] =_('zPush in not enabled in your system.');
}

/*read from her value of the KOLAB_LAXPIC
 -1 = allow the user to select (or if the constant doesn't exist) 
 0  = same as the annotations (no lax mode just jpeg)
 1  = force the lax mode for all 
*/
if(!defined('KOLAB_LAXPIC')){define('KOLAB_LAXPIC',-1);}



//delete device
if(isset($_POST['deid']))
{
 $save = new ActiveSyncManager();
 $save->deleteDevice($_POST['deid']);
}

if (isset($_POST['actived']))
{

	$save = new ActiveSyncManager();
	$save->storeDevicenFolders($_POST);
}



 class ActiveSyncManager
{

 var $inbox;
 var $connectstring;
 var $orig_devs;
 var $base_folders;

function __construct()
{
	//not sure about that
	list($prefix, $domain) = split("@",$_SESSION['auth_user']);
	$this->connectstring= $_server = "{localhost:143/imap/notls/norsh}"; 
	$tmbox = imap_open($_server , $_SESSION['auth_user'], $_SESSION['auth_pw'], OP_HALFOPEN);
	  if ($tmbox) {
	             $this->inbox=$tmbox;
	            }
	            else {
	               return false;
	        }
}




//get devices from annotation

 function getDevices()
    {
	        
	    $result = imap_getannotation($this->inbox, "INBOX", "/vendor/kolab/activesync", "value.priv");
	   
		$gp=new GlobalParam();
		if (isset($result["value.priv"])) 
	    {
	         $gp->unserialize($result["value.priv"]);
	         
	         
	    }
	    $i=0;
	     foreach( $gp->deviceList() as $device)
	     {
	      //$devs[$device]=array("S"=>$gp->getDeviceMode($device),"Type"=>$gp->getDeviceType($device));
	      
	      
	      
	      $odevs[$device]['MODE']= $gp->getDeviceMode($device);
	      $odevs[$device]['TYPE'] = $gp->getDeviceType($device);
	      $odevs[$device]['ALIAS']= $gp->getDeviceAlias($device);
	      $odevs[$device]['LAXPIC'] = $gp->getDeviceLaxPic($device);
	      
	      //we need original devices in order to save the annotation later
	      $this->orig_devs = $odevs;
	     
	     //variable for pasring via Smarty
	      $devs[$i]['name'] = $device;
	      $devs[$i]['id'] = $i;
	      $devs[$i]['mode']= $gp->getDeviceMode($device);
	      $devs[$i]['type'] = $gp->getDeviceType($device);
	      $devs[$i]['alias']= $gp->getDeviceAlias($device);
	      $devs[$i]['laxpic'] = $gp->getDeviceLaxPic($device);
	      $i++;
	      
	     }
	     
	    return $devs;
    
    }

    
    function deleteDevice($serial)
    {
    	$result = imap_getannotation($this->inbox, "INBOX", "/vendor/kolab/activesync", "value.priv");
	   
		$gp=new GlobalParam();
		if (isset($result["value.priv"])) 
	    {
	         $gp->unserialize($result["value.priv"]);
	                  
	    }
	    foreach( $gp->deviceList() as $device)
	     {
	      $odevs[$device]['MODE']= $gp->getDeviceMode($device);
	      $odevs[$device]['TYPE'] = $gp->getDeviceType($device);
	      $odevs[$device]['ALIAS']= $gp->getDeviceAlias($device);
	      $odevs[$device]['LAXPIC'] = $gp->getDeviceLaxPic($device);
	     }	
	     
	     unset($odevs[$serial]);
	     
    
    //delete device
   $to_store=array('DEVICE'=>$odevs);
   $value_to_store= base64_encode(json_encode($to_store));
 
  //resource stream_id, string mailbox, string entry, string attr, string value
  imap_setannotation($this->inbox, "INBOX", "/vendor/kolab/activesync","value.priv",$value_to_store);
    
    
    }
    
    
    
    
    function getFolderAnn($folder)
    {
    
    $result = imap_getannotation($this->inbox, $folder, "/vendor/kolab/activesync", "value.priv");
    
    
    if (isset($result["value.priv"])) 
	    {
	     $res=  json_decode(base64_decode($result["value.priv"]),true);
	    }
	    
    return $res['FOLDER'];
    
    }
    
    function getFolders()
    {
    
		    $list = imap_getmailboxes($this->inbox, $this->connectstring, "*");
		    
		if (is_array($list)) {
		
			$i=0;
			//print_r($list);
		    foreach ($list as $key => $val) {
		      
		     //we need realname in order to check for annotations
		     
		     $last= strrpos($val->name,'}');
		     
		     //foldername
		     
		     $folder=substr($val->name,($last+1),strlen($val->name));
		     
		     //put in array folders
		     $folders[$i]['realname']=$folder;
		     
		     //fic the encoding
		     $folder = mb_convert_encoding($folder,"UTF8", "UTF7-IMAP");
		     
		     //remove Inbox/ part from the name
		     $folder_chunk=explode("/",$folder);
		     
		     //if if only inbox/ show it
		     if($folder_chunk[1]!=""){
		     //else is inbox/somepart - show only somepart
		     $folder=$folder_chunk[1];
		     
		     }if($folder_chunk[2]!="")
		     {
		      //user folder
		     $folder="(".$folder_chunk[1].") ".$folder_chunk[2];
		     }
		     //for saving annot
		     $part_folder[]=$folders[$i]['realname'];
		     
		     
		     //for smarty
		     $folders[$i]['name']=$folder;
			 $folders[$i]['type']=$this->getFolderType($folders[$i]['realname']);
			 
			 $this->base_folders=$part_folder;
		     $i++;
		        
		      
		    }
		} else {
		    echo "imap_getmailboxes failed: " . imap_last_error() . "\n";
		}
		
		//start sorting
		//define array
		$types = array("MAIL","CONTACT","EVENT","TASKS");
		
		
		
		
		foreach ($types as $key => $row) {
		        for($im=0;$im<count($folders);$im++) {
				 if ($folders[$im]['type']==$row)
		    		 $foldern[$row][$im] = $folders[$im];
		    		
		        }
			}
		//array_multisort($type, SORT_DESC, $folders);
		
		return $foldern;
		//return $folders;
    }
    
function getFolderType($folder)
  {
  
  
        $result = imap_getannotation($this->inbox, $folder, "/vendor/kolab/folder-type", "value.shared");
		
	    if (isset($result["value.shared"])) 
	    {
	    
	     if(preg_match("/(event)/",$result["value.shared"]))
	     {
	     	return "EVENT";
	     
	     }else if(preg_match("/(task)/",$result["value.shared"]))
	     {
	     
	      return "TASKS";
	     }else if(preg_match("/(contact)/",$result["value.shared"]))
	     {
	     
	      return "CONTACT";
	     }
	    else if(preg_match("/(note)/",$result["value.shared"]))
	     {
	     
	      return "NOTES";
	     }
	    }else{
	    
	    return "MAIL";
	    }
  
  }
  
  
  
  function storeDevicenFolders($device){
  

  //get current annotation devices
  $devices= $this->getDevices();
  
  //assign the array to an variable
  $old_devices= $this->orig_devs;
  
  //change the data on this device, based on $POST_data
  $old_devices[$device['serial']]['MODE']=$device['mode'];
  $old_devices[$device['serial']]['ALIAS']=$device['alias'];
  $old_devices[$device['serial']]['LAXPIC']=$device['laxpic'];
  $old_devices[$device['serial']]['TYPE']=$device['type'];
  $to_store=array('DEVICE'=>$old_devices);
  $value_to_store= base64_encode(json_encode($to_store));
 
  //resource stream_id, string mailbox, string entry, string attr, string value
  imap_setannotation($this->inbox, "INBOX", "/vendor/kolab/activesync","value.priv","");
  if($old_devices[$device['serial']]['TYPE']=$device['type'])
  {
  	imap_setannotation($this->inbox, "INBOX", "/vendor/kolab/activesync","value.priv",$value_to_store);
  }
  
  //Folders
  //1. Get All folders
    $this->GetFolders();
  
  //set all folders -> array
    $allfolders= $this->base_folders;
  
    foreach($allfolders as $index=>$folder)
    {
    
    $smfolder=preg_replace(array('/( )/','/\./'),array('@','*'),$folder);
    
    
    
      //is folder is default and ! in POST array = set s=0;
      if($this->is_default($folder) and !isset($device[$smfolder]))
      {
    		$this->SetUnset($folder,$device); 
    		
      
      }else if($this->is_default($folder) and isset($device[$smfolder]))
      {
        $this->SetSet($folder,$device); 
        
      }else if(isset($device[$smfolder]))
      {
     
             // is not defaul - just we have it in post with value == put in annot this value
      $this->SetSet($folder,$device);
      
      
      }else
      {
      //is not default folder - we don't have it == unset this value in annot
       $this->SetUnset($folder,$device); 
      
      }
    
    }
   
  
  }
  
 //if the folder is yes for sync by default -return true; 
  
  
  function SetUnset($folder,$device)
  {
  
 
  $result = imap_getannotation($this->inbox, $folder, "/vendor/kolab/activesync", "value.priv");
    
    
    if (isset($result["value.priv"])) 
	    {
	   $res=  json_decode(base64_decode($result["value.priv"]),true);
	    }
	    
	    $res['FOLDER'][$device['serial']]['S']=0;
	   
	  imap_setannotation($this->inbox, $folder, "/vendor/kolab/activesync","value.priv",base64_encode(json_encode($res)));
	    
	 
 
  
  }
  
  function SetSet($folder,$device)
  {
  $result = imap_getannotation($this->inbox, $folder, "/vendor/kolab/activesync", "value.priv");
    //echo "$folder<br/>";
    
    if (isset($result["value.priv"])) 
	    {
	   $res=  json_decode(base64_decode($result["value.priv"]),true);
	    }
	//    echo $folder."<pre>";
	 //   print_r($res);
	  //  echo "</pre><hr>";
	    $smfolder=preg_replace(array('/( )/','/\./'),array('@','*'),$folder);
	    
	    $res['FOLDER'][$device['serial']]['S']=$device[$smfolder];
	    imap_setannotation($this->inbox, $folder, "/vendor/kolab/activesync","value.priv",base64_encode(json_encode($res)));
	   // echo $folder."<pre>";
	   // print_r($res);
	  //  echo "</pre><hr>";
	    
	    }
  
function is_default($folder)
	{
	
	 if(preg_match("/(^INBOX)/", $folder))
	 {
	 
	  return true;
	 }else
	 {
	 	return false;
	 }
	}
  
  
  function __destruct()
	{
	imap_close($this->inbox);
	$this->inbox = "";
	 
	
	}
}



/**** Authentication etc. ***/
$sidx = 'user';
require_once('KolabAdmin/include/menu.php');

/**** Submenu for current page ***/
$menuitems[$sidx]['selected'] = 'selected';



//ActiveSync part
if(count($errors) <1)
{
$d= new ActiveSyncManager;

$devs= $d->getdevices();
$folds=$d->getfolders();
$ola=true;

if(count($devs)<1)
{
	$errors[] =_("There are currently no devices known for your user.<br/><br/>In order to register a device, please connect it to the server first, using <a href='http://wiki.kolab.org/Z_push#Clients'>the instructions in the Wiki</a>. Afterwards the device should become available for configuration in this dialogue.");
	$ola = false;
}
}



/**** Insert into template and output ***/
$smarty = new MySmarty();
//add the plugin
$smarty->plugins_dir[] = 'KolabAdmin/include/';
$smarty->assign( 'errors', $errors );
$smarty->assign( 'messages', $messages );
$smarty->assign( 'uid', $auth->uid() );
$smarty->assign( 'group', $auth->group() );
$smarty->assign( 'page_title', $menuitems[$sidx]['title'] );
$smarty->assign( 'menuitems', $menuitems );
$smarty->assign( 'submenuitems', 
				 array_key_exists('submenu', 
								  $menuitems[$sidx])?$menuitems[$sidx]['submenu']:array() );
$smarty->assign( 'active', $active );


//print_r($d->getdevices());
if ($ola !=false)
{
 //select list of modes
 
$publicnames = array("MAIL"=>_("E-mail"),"CONTACT"=>_("Address Books"),"EVENT"=>_("Calendars"),"TASKS"=>_("Tasks"),"NOTES"=>_("Notes"));


$smarty->assign('modetypes', array(
                                -1 => _('Automatic'),
                                0 => _('Flat Mode'),
                                1 => _('Folder Mode')
                                ));
                                
$smarty->assign('picsettings', array(
                                0 => _('No'),
                                1 => _('Yes')
                                ));
                                

 $smarty->assign( 'devices',$devs);
 $smarty->assign( 'pnames',$publicnames);
 $smarty->assign( 'folders',$folds);
 if ($_POST['actived']){$ac=$_POST['actived'];}else{$ac=0;}
 $smarty->assign( 'actived',$ac );
 $smarty->assign( 'laxpicdef',KOLAB_LAXPIC);
 $smarty->assign( 'maincontent', 'activesync.tpl' );
}


$smarty->display('page-ajax.tpl');

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
  vim:encoding=utf-8:
 */
?>