1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
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: class SolrRightBottomPage extends cGuiPage {
40:
41: 42: 43: 44:
45: private $_clientOptions;
46:
47: 48: 49:
50: public function __construct() {
51:
52: global $action;
53:
54: parent::__construct('right_bottom', Solr::getName());
55:
56: $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
57: $this->addStyle('right_bottom.css');
58: $this->addScript('right_bottom.js');
59:
60: $this->set('s', 'CONTENIDO', cRegistry::getBackendSessionId());
61:
62: $this->set('s', 'I18N_CLIENT_OPTIONS', Solr::i18n('CLIENT_OPTIONS'));
63: $this->set('s', 'I18N_OPTION', Solr::i18n('OPTION'));
64: $this->set('s', 'I18N_VALUE', Solr::i18n('VALUE'));
65: $this->set('s', 'I18N_DESCRIPTION', Solr::i18n('DESCRIPTION'));
66: $this->set('s', 'I18N_DESCR_HOSTNAME', Solr::i18n('DESCR_HOSTNAME'));
67: $this->set('s', 'I18N_DESCR_PORT', Solr::i18n('DESCR_PORT'));
68: $this->set('s', 'I18N_DESCR_PATH', Solr::i18n('DESCR_PATH'));
69: $this->set('s', 'I18N_DESCR_LOGIN', Solr::i18n('DESCR_LOGIN'));
70: $this->set('s', 'I18N_DESCR_PASSWORD', Solr::i18n('DESCR_PASSWORD'));
71: $this->set('s', 'I18N_DESCR_SECURE', Solr::i18n('DESCR_SECURE'));
72: $this->set('s', 'I18N_DESCR_TIMEOUT', Solr::i18n('DESCR_TIMEOUT'));
73: $this->set('s', 'I18N_DESCR_WT', Solr::i18n('DESCR_WT'));
74: $this->set('s', 'I18N_DESCR_PROXY_HOST', Solr::i18n('DESCR_PROXY_HOST'));
75: $this->set('s', 'I18N_DESCR_PROXY_PORT', Solr::i18n('DESCR_PROXY_PORT'));
76: $this->set('s', 'I18N_DESCR_PROXY_LOGIN', Solr::i18n('DESCR_PROXY_LOGIN'));
77: $this->set('s', 'I18N_DESCR_PROXY_PASSWORD', Solr::i18n('DESCR_PROXY_PASSWORD'));
78: $this->set('s', 'I18N_DESCR_SSL_CERT', Solr::i18n('DESCR_SSL_CERT'));
79: $this->set('s', 'I18N_DESCR_SSL_KEY', Solr::i18n('DESCR_SSL_KEY'));
80: $this->set('s', 'I18N_DESCR_SSL_KEYPASSWORD', Solr::i18n('DESCR_SSL_KEYPASSWORD'));
81: $this->set('s', 'I18N_DESCR_SSL_CAINFO', Solr::i18n('DESCR_SSL_CAINFO'));
82: $this->set('s', 'I18N_DESCR_SSL_CAPATH', Solr::i18n('DESCR_SSL_CAPATH'));
83: $this->set('s', 'I18N_DESCR_RELOAD', Solr::i18n('DESCR_RELOAD'));
84: $this->set('s', 'I18N_DESCR_REINDEX', Solr::i18n('DESCR_REINDEX'));
85: $this->set('s', 'I18N_DESCR_DELETE', Solr::i18n('DESCR_DELETE'));
86:
87:
88: $idclient = cRegistry::getClientId();
89: $idlang = cRegistry::getLanguageId();
90: $this->_clientOptions = Solr::getClientOptions($idclient, $idlang);
91: $this->set('s', 'HOSTNAME', $this->_clientOptions['hostname']);
92: $this->set('s', 'PORT', $this->_clientOptions['port']);
93: $this->set('s', 'PATH', $this->_clientOptions['path']);
94: $this->set('s', 'LOGIN', $this->_clientOptions['login']);
95: $this->set('s', 'PASSWORD', $this->_clientOptions['password']);
96: $this->set('s', 'SECURE', 'true' == $this->_clientOptions['secure'] ? 'checked="checked"' : '');
97: $this->set('s', 'TIMEOUT', $this->_clientOptions['timeout']);
98: $this->set('s', 'WT', $this->_clientOptions['wt']);
99: $this->set('s', 'PROXY_HOST', $this->_clientOptions['proxy_host']);
100: $this->set('s', 'PROXY_PORT', $this->_clientOptions['proxy_port']);
101: $this->set('s', 'PROXY_LOGIN', $this->_clientOptions['proxy_login']);
102: $this->set('s', 'PROXY_PASSWORD', $this->_clientOptions['proxy_password']);
103: $this->set('s', 'SSL_CERT', $this->_clientOptions['ssl_cert']);
104: $this->set('s', 'SSL_KEY', $this->_clientOptions['ssl_key']);
105: $this->set('s', 'SSL_KEYPASSWORD', $this->_clientOptions['ssl_keypassword']);
106: $this->set('s', 'SSL_CAINFO', $this->_clientOptions['ssl_cainfo']);
107: $this->set('s', 'SSL_CAPATH', $this->_clientOptions['ssl_capath']);
108:
109:
110: try {
111: $this->_dispatch($action);
112:
113:
114: try {
115: Solr::validateClientOptions($this->_clientOptions);
116: $validClientOptions = true;
117: } catch (SolrWarning $e) {
118: $validClientOptions = false;
119: }
120: $this->set('s', 'DISABLED_RELOAD', $validClientOptions ? '' : 'disabled="disabled"');
121: $this->set('s', 'DISABLED_REINDEX', $validClientOptions ? '' : 'disabled="disabled"');
122: $this->set('s', 'DISABLED_DELETE', $validClientOptions ? '' : 'disabled="disabled"');
123:
124: } catch (InvalidArgumentException $e) {
125: $cGuiNotification = new cGuiNotification();
126: $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
127: $this->set('s', 'notification', $notification);
128: }
129: }
130:
131: 132: 133: 134: 135: 136:
137: protected function _dispatch($action) {
138: global $area;
139:
140:
141: $perm = cRegistry::getPerm();
142: if (!$perm->have_perm_area_action($area, $action)) {
143: throw new IllegalStateException('no permissions');
144: }
145:
146: if (NULL === $action) {
147: $this->set('s', 'notification', '');
148: $this->set('s', 'content', '');
149: return;
150: }
151:
152:
153: try {
154: switch ($action) {
155: case 'store_client_options':
156: $this->set('s', 'notification', $this->_storeClientOptions());
157: break;
158: case 'reload':
159: $this->set('s', 'notification', $this->_reload());
160: break;
161: case 'reindex':
162: $this->set('s', 'notification', $this->_reindex());
163: break;
164: case 'delete':
165: $this->set('s', 'notification', $this->_delete());
166: break;
167: default:
168: throw new InvalidArgumentException('unknown action ' . $action);
169: }
170: } catch (Exception $e) {
171: $notification = Solr::notifyException($e);
172: }
173: }
174:
175: 176: 177: 178:
179: private function _storeClientOptions() {
180: $settings = 'secure,hostname,port,path,wt,login,password,timeout,';
181: $settings .= 'proxy_host,proxy_port,proxy_login,proxy_password,';
182: $settings .= 'ssl_cert,ssl_key,ssl_keypassword,ssl_cainfo,ssl_capath';
183: foreach (explode(',', $settings) as $setting) {
184: $value = $_POST[$setting];
185: if (0 < strlen(trim($value))) {
186: setSystemProperty('solr', $setting, $value);
187: } else {
188:
189:
190: deleteSystemProperty('solr', $setting);
191: }
192: }
193: $cGuiNotification = new cGuiNotification();
194: return $cGuiNotification->returnNotification(cGuiNotification::LEVEL_OK, 'client options were stored');
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204: 205: 206:
207: private function _reload() {
208:
209:
210:
211: $url = 'http://';
212: $url .= $this->_clientOptions['login'] . ':' . $this->_clientOptions['password'] . '@';
213: $url .= $this->_clientOptions['hostname'] . ':' . $this->_clientOptions['port'];
214: $url .= '/solr/admin/cores?' . http_build_query(array(
215: 'action' => 'RELOAD',
216: 'core' => array_pop(explode('/', $this->_clientOptions['path']))
217: ));
218:
219:
220: $ch = curl_init();
221:
222: $data = false;
223: if (false !== $ch) {
224:
225: $opt = array(
226:
227: CURLOPT_URL => $url,
228:
229: CURLOPT_HTTPGET => true,
230:
231:
232: CURLOPT_USERAGENT => 'CONTENIDO Solr Plugin v1.0',
233:
234:
235: CURLOPT_TIMEOUT_MS => 5000,
236:
237:
238: CURLOPT_CONNECTTIMEOUT_MS => 5000,
239:
240: CURLOPT_RETURNTRANSFER => 1,
241:
242: CURLOPT_HEADER => false
243: );
244:
245: curl_setopt_array($ch, $opt);
246:
247:
248: $data = curl_exec($ch);
249:
250:
251: $curlInfo = curl_getinfo($ch);
252:
253:
254: curl_close($ch);
255:
256: if (200 !== (int) $curlInfo['http_code']) {
257: $msg = 'HTTP status code ' . $curlInfo['http_code'];
258:
259:
260: $msg .= "\n(complete cUrl-Info:";
261: foreach ($curlInfo as $key => $value) {
262: $msg .= "\n\t" . $key . ' => ' . $value;
263: }
264: $msg .= "\n)";
265:
266: throw new cException($msg);
267: }
268: }
269:
270: if (false === $data) {
271: throw new cException('server did not answer');
272: }
273:
274: $cGuiNotification = new cGuiNotification();
275: return $cGuiNotification->returnNotification(cGuiNotification::LEVEL_OK, 'core was reloaded');
276: }
277:
278: 279: 280: 281:
282: private function _reindex() {
283: $cfg = cRegistry::getConfig();
284:
285: $idclient = cRegistry::getClientId();
286: $idclient = cSecurity::toInteger($idclient);
287:
288: $idlang = cRegistry::getLanguageId();
289: $idlang = cSecurity::toInteger($idlang);
290:
291:
292: $db = cRegistry::getDb();
293: $db->query("-- SolrRightBottomPage->_reindex()
294: SELECT
295: art.idclient
296: , art_lang.idlang
297: , cat_art.idcat
298: , cat_lang.idcatlang
299: , art_lang.idart
300: , art_lang.idartlang
301: FROM
302: `{$cfg['tab']['art_lang']}` AS art_lang
303: INNER JOIN
304: `{$cfg['tab']['art']}` AS art
305: ON
306: art_lang.idart = art.idart
307: INNER JOIN
308: `{$cfg['tab']['cat_art']}` AS cat_art
309: ON
310: art_lang.idart = cat_art.idart
311: INNER JOIN
312: `{$cfg['tab']['cat_lang']}` AS cat_lang
313: ON
314: cat_art.idcat = cat_lang.idcat
315: AND art_lang.idlang = cat_lang.idlang
316: WHERE
317: art.idclient = $idclient
318: -- AND art_lang.idlang = $idlang
319: ORDER BY
320: art_lang.idartlang
321: ;");
322:
323: $articleIds = array();
324: while ($db->nextRecord()) {
325: array_push($articleIds, array(
326: 'idclient' => $db->f('idclient'),
327: 'idlang' => $db->f('idlang'),
328: 'idcat' => $db->f('idcat'),
329: 'idcatlang' => $db->f('idcatlang'),
330: 'idart' => $db->f('idart'),
331: 'idartlang' => $db->f('idartlang')
332: ));
333: }
334:
335: $indexer = new SolrIndexer($articleIds);
336: $indexer->updateArticles();
337:
338: $cGuiNotification = new cGuiNotification();
339: return $cGuiNotification->returnNotification(cGuiNotification::LEVEL_OK, 'core was reindexed');
340: }
341:
342: 343: 344: 345:
346: private function _delete() {
347:
348: $cfg = cRegistry::getConfig();
349:
350:
351: $db = cRegistry::getDb();
352: $db->query("-- SolrRightBottomPage->_delete()
353: SELECT
354: art.idclient
355: , art_lang.idlang
356: , cat_art.idcat
357: , cat_lang.idcatlang
358: , art_lang.idart
359: , art_lang.idartlang
360: FROM
361: `{$cfg['tab']['art_lang']}` AS art_lang
362: INNER JOIN
363: `{$cfg['tab']['art']}` AS art
364: ON
365: art_lang.idart = art.idart
366: INNER JOIN
367: `{$cfg['tab']['cat_art']}` AS cat_art
368: ON
369: art_lang.idart = cat_art.idart
370: INNER JOIN
371: `{$cfg['tab']['cat_lang']}` AS cat_lang
372: ON
373: cat_art.idcat = cat_lang.idcat
374: AND art_lang.idlang = cat_lang.idlang
375: ORDER BY
376: art_lang.idartlang
377: ;");
378:
379: $articleIds = array();
380: while ($db->nextRecord()) {
381: array_push($articleIds, array(
382: 'idclient' => $db->f('idclient'),
383: 'idlang' => $db->f('idlang'),
384: 'idcat' => $db->f('idcat'),
385: 'idcatlang' => $db->f('idcatlang'),
386: 'idart' => $db->f('idart'),
387: 'idartlang' => $db->f('idartlang')
388: ));
389: }
390:
391: $indexer = new SolrIndexer($articleIds);
392: $indexer->deleteArticles();
393:
394: $cGuiNotification = new cGuiNotification();
395: return $cGuiNotification->returnNotification(cGuiNotification::LEVEL_OK, 'core was deleted');
396: }
397:
398: }
399: