tiki-ajax_services.php 7.39 KB
Newer Older
1
<?php
2
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
3 4 5 6 7
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

8 9 10 11 12 13 14 15 16 17 18
// To contain data services for ajax calls
//
// If controller and action are specified in the request, the controller class matching the
// controller key in the $contollerMap registry will be instanciated. The method matching the
// action name will be called. The input to the method is a JitFilter. The output of the method
// will be serialized and sent to the browser.
//
// Otherwise, the procedural script remains

$controllerMap = array(
	'comment' => 'Services_Comment_Controller',
19
	'file' => 'Services_File_Controller',
20
	'auth_source' => 'Services_AuthSource_Controller',
21
	'tracker' => 'Services_Tracker_Controller',
22
	'tracker_sync' => 'Services_Tracker_SyncController',
23
	'favorite' => 'Services_Favorite_Controller',
24
	'translation' => 'Services_Language_TranslationController',
25
	'user' => 'Services_User_Controller',
26
	'category' => 'Services_Category_Controller',
27 28 29 30 31 32 33 34
);

$inputConfiguration = array(array(
	'staticKeyFilters' => array(
		'action' => 'word',
		'controller' => 'word',
	),
));
35

36 37 38 39
if (isset($_REQUEST['controller'], $_REQUEST['action'])) {
	$inputConfiguration[] = array('catchAllUnset' => null);
}

40 41
require_once ('tiki-setup.php');

42 43 44 45
if (isset($_REQUEST['controller'], $_REQUEST['action'])) {
	$controller = $_REQUEST['controller'];
	$action = $_REQUEST['action'];

46 47
	$broker = new Services_Broker($controllerMap);
	$broker->process($controller, $action, $jitRequest);
48 49
	exit;
}
50 51

if ($access->is_serializable_request() && isset($_REQUEST['listonly'])) {
52 53
	$access->check_feature( array( 'feature_ajax', 'feature_jquery_autocomplete' ) );

lphuberdeau's avatar
lphuberdeau committed
54 55 56 57 58 59 60 61 62
	$sep = '|';
	if( isset( $_REQUEST['separator'] ) ) {
		$sep = $_REQUEST['separator'];
	}
	$p = strrpos($_REQUEST['q'], $sep);
	if ($p !== false) {
		$_REQUEST['q'] = substr($_REQUEST['q'], $p + 1);
	}

63 64 65 66
	if ($_REQUEST['listonly'] == 'groups') {
		$listgroups = $userlib->get_groups(0, -1, 'groupName_asc', '', '', 'n');
		
		// TODO proper perms checking - this looks right but returns nothing for reg, and everything for admin
67
		// $listgroups['data'] = Perms::filter( array( 'type' => 'group' ), 'object', $listgroups['data'], array( 'object' => 'groupName' ), 'view_group' );
68 69 70 71 72 73 74 75
		
		$grs = array();
		foreach($listgroups['data'] as $gr) {
			if (isset($_REQUEST['q']) && stripos($gr['groupName'], $_REQUEST['q']) !== false) {
				$grs[] = $gr['groupName'];
			}
		}
		$access->output_serialized($grs);
lphuberdeau's avatar
lphuberdeau committed
76
	} elseif ($_REQUEST['listonly'] == 'users') {
77 78 79 80 81 82 83 84 85 86 87 88
		$listusers = $userlib->get_users_names();
		
		// TODO also - proper perms checking
		// tricker for users? Check the group they're in, then tiki_p_group_view_members
		
		$usrs = array();
		foreach($listusers as $usr) {
			if (isset($_REQUEST['q']) && stripos($usr, $_REQUEST['q']) !== false) {
				$usrs[] = $usr;
			}
		}
		$access->output_serialized($usrs);
89
	} elseif ($_REQUEST['listonly'] == 'usersandcontacts') {
nyloth's avatar
nyloth committed
90
		$contactlib = TikiLib::lib('contact');
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		$listcontact = $contactlib->list_contacts($user);
		$listusers = $userlib->get_users();
		
		$contacts = array();		
		$query = $_REQUEST['q'];
				
		foreach($listcontact as $key=>$contact) {
			if (isset($query) && (stripos($contact['firstName'], $query) !== false or stripos($contact['lastName'], $query) !== false or stripos($contact['email'], $query) !== false)) {
				if($contact['email']<>''){ $contacts[] = $contact['email']; }
			}
		}
		foreach($listusers['data'] as $key=>$contact) {
			if (isset($query) && (stripos($contact['firstName'], $query) !== false or stripos($contact['login'], $query) !== false or stripos($contact['lastName'], $query) !== false or stripos($contact['email'], $query) !== false)) {
				if($prefs['login_is_email'] == 'y'){
					$contacts[] = $contact['login'];
				} else {
					$contacts[] = $contact['email'];
				}
			}
		}
		$contacts = array_unique($contacts);
		sort($contacts);
		$access->output_serialized($contacts);
		
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
	} elseif ($_REQUEST['listonly'] == 'userrealnames') {
		$groups = '';
		$listusers = $userlib->get_users_light(0, -1, 'login_asc', '', $groups);
		$done = array();
		$finalusers = array();
		foreach($listusers as $usrId => $usr) {
			if (isset($_REQUEST['q'])) {
				$longusr = $usr . ' (' . $usrId . ')';
				if (array_key_exists($usr, $done)) {
					// disambiguate duplicates
					if (stripos($longusr, $_REQUEST['q']) !== false) {
						$oldkey = array_search($usr, $finalusers);
						if ($oldkey !== false) {
							$finalusers[$oldkey] = $done[$usr];
						}
					}
					if (stripos($longusr, $_REQUEST['q']) !== false) {
						$finalusers[] = $longusr;
					}
				} else {
					if (stripos($longusr, $_REQUEST['q']) !== false) {
						$finalusers[] = $usr;
					}
				}
				$done[$usr] = $longusr;
			}
		}
		
		// TODO also - proper perms checking
		// tricker for users? Check the group they're in, then tiki_p_group_view_members
				
		$access->output_serialized($finalusers);
lphuberdeau's avatar
lphuberdeau committed
147 148 149 150 151
	} elseif( $_REQUEST['listonly'] == 'tags' ) {
		global $freetaglib; require_once 'lib/freetag/freetaglib.php';

		$tags = $freetaglib->get_tags_containing( $_REQUEST['q'] );
		$access->output_serialized( $tags );
152 153 154 155 156 157 158 159 160 161 162
	} elseif( $_REQUEST['listonly'] == 'icons' ) {

		$dir = 'pics/icons';
		$max = isset($_REQUEST['max']) ? $_REQUEST['max'] : 10;
		$icons = array();
		$style_dir = $tikilib->get_style_path($prefs['style'], $prefs['style_option']);
		if ($style_dir && is_dir($style_dir . $dir)) {
			read_icon_dir($style_dir . $dir, $icons, $max);
		}
		read_icon_dir($dir, $icons, $max);
		$access->output_serialized($icons);
163 164
	} elseif( $_REQUEST['listonly'] == 'shipping' && $prefs['shipping_service'] == 'y' ) {
		global $shippinglib; require_once 'lib/shipping/shippinglib.php';
165

166
		$access->output_serialized( $shippinglib->getRates( $_REQUEST['from'], $_REQUEST['to'], $_REQUEST['packages'] ) );
167
	} elseif(  $_REQUEST['listonly'] == 'trackername' ) {
168
		$trackers = TikiLib::lib('trk')->list_trackers();
169 170 171 172 173
		$ret = array();
		foreach ($trackers['data'] as $tracker) {
			$ret[] = $tracker['name'];
		}
		$access->output_serialized($ret);
174
	}
175 176
}

177
// Handle Zotero Requests
178 179
if ($access->is_serializable_request() && isset($_REQUEST['zotero_tags'])) {
	$access->check_feature( array( 'zotero_enabled' ) );
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
	$zoterolib = TikiLib::lib('zotero');

	$references = $zoterolib->get_references($_REQUEST['zotero_tags']);
	
	if ($references === false) {
		$access->output_serialized(array(
			'type' => 'unauthorized',
			'results' => array(),
		));
	} else {
		$access->output_serialized(array(
			'type' => 'success',
			'results' => $references,
		));
	}
}

if (isset($_REQUEST['oauth_request'])) {
	$oauthlib = TikiLib::lib('oauth');

	$oauthlib->request_token($_REQUEST['oauth_request']);
	die('Provider not supported.');
}

if (isset($_REQUEST['oauth_callback'])) {
	$oauthlib = TikiLib::lib('oauth');

	$oauthlib->request_access($_REQUEST['oauth_callback']);
	$access->redirect('');
}

211 212 213 214
if (isset($_REQUEST['geocode']) && $access->is_serializable_request()) {
	$access->output_serialized(TikiLib::lib('geo')->geocode($_REQUEST['geocode']));
}

215 216 217 218 219 220 221 222 223 224 225
function read_icon_dir($dir, &$icons, $max) {
	$fp = opendir($dir);
	while(false !== ($f = readdir($fp))) {
		preg_match('/^([^\.].*)\..*$/', $f, $m);
		if (count($m) > 0 && count($icons) < $max &&
				stripos($m[1], $_REQUEST['q']) !== false &&
				!in_array($dir . '/' . $f, $icons)) {
			
			$icons[] = $dir . '/' . $f;
		}
	}
226
}