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