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: class Solr {
22:
23: 24: 25: 26: 27:
28: private static $_name = 'search_solr';
29:
30: 31: 32: 33:
34: public static function log($whatever, $file = NULL, $line = NULL) {
35: $msg = '';
36: if ($whatever instanceof Exception) {
37: $msg .= '========================' . PHP_EOL;
38: $msg .= '=== LOGGED EXCEPTION ===' . PHP_EOL;
39: $msg .= 'REFERER: ' . $_SERVER['HTTP_REFERER'] . PHP_EOL;
40: $msg .= 'URI: ' . $_SERVER['REQUEST_URI'] . PHP_EOL;
41: $msg .= 'MSG: ' . $whatever->getMessage() . PHP_EOL;
42: $msg .= 'TRACE: ' . $whatever->getTraceAsString() . PHP_EOL;
43: $msg .= '========================';
44: } else {
45: $msg = $whatever;
46: }
47:
48: static $start = 0;
49: if (0 == $start) {
50: $start = microtime(true);
51: }
52: $delta = microtime(true) - $start;
53:
54:
55: $cfg = cRegistry::getConfig();
56: $filename = $cfg['path']['contenido_logs'] . 'errorlog.txt';
57:
58:
59: $prefix = number_format($delta * 1000, 0) . 'ms: ';
60: if (NULL !== $file) {
61: $prefix .= $file;
62: if (NULL !== $line) {
63: $prefix .= ':' . $line;
64: }
65: $prefix .= ' ';
66: }
67:
68:
69: $log = new cLog(cLogWriter::factory('file', array(
70: 'destination' => $filename
71: )));
72: $log->info($prefix . $whatever);
73: }
74:
75: 76:
77: public static function getName() {
78: return self::$_name;
79: }
80:
81: 82: 83: 84: 85:
86: public static function getPath() {
87: $cfg = cRegistry::getConfig();
88:
89: $path = cRegistry::getBackendPath() . $cfg['path']['plugins'];
90: $path .= self::$_name . '/';
91:
92: return $path;
93: }
94:
95: 96: 97: 98: 99:
100: public static function i18n($key) {
101: $trans = i18n($key, self::$_name);
102: return $trans;
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112:
113: public static function getClientOptions($idclient, $idlang) {
114:
115:
116: $queryOption = 'getEffectiveSetting';
117:
118: $options = array();
119:
120:
121: $options['secure'] = (bool) $queryOption('solr', 'secure');
122:
123:
124: $options['hostname'] = $queryOption('solr', 'hostname');
125:
126:
127: $options['port'] = $queryOption('solr', 'port');
128:
129:
130: $options['path'] = $queryOption('solr', 'path');
131:
132:
133: $clientLanguage = new cApiClientLanguage();
134: $clientLanguage->loadByMany(array(
135: 'idclient' => $idclient,
136: 'idlang' => $idlang
137: ));
138: $value = $clientLanguage->isLoaded() ? $clientLanguage->getProperty($type, $name) : false;
139: if (false === $value) {
140: $client = new cApiClient($idclient);
141: $value = $client->isLoaded() ? $client->getProperty($type, $name) : false;
142: }
143: if (false === $value) {
144: $value = getSystemProperty($type, $name);
145: }
146:
147:
148: $options['wt'] = $queryOption('solr', 'wt');
149:
150:
151: $options['login'] = $queryOption('solr', 'login');
152:
153:
154: $options['password'] = $queryOption('solr', 'password');
155:
156:
157: $options['proxy_host'] = $queryOption('solr', 'proxy_host');
158:
159:
160: $options['proxy_port'] = $queryOption('solr', 'proxy_port');
161:
162:
163: $options['proxy_login'] = $queryOption('solr', 'proxy_login');
164:
165:
166: $options['proxy_password'] = $queryOption('solr', 'proxy_password');
167:
168:
169:
170: $options['timeout'] = $queryOption('solr', 'timeout');
171:
172:
173:
174:
175:
176: $options['ssl_cert'] = $queryOption('solr', 'ssl_cert');
177:
178:
179: $options['ssl_key'] = $queryOption('solr', 'ssl_key');
180:
181:
182:
183:
184: $options['ssl_keypassword'] = $queryOption('solr', 'ssl_keypassword');
185:
186:
187: $options['ssl_cainfo'] = $queryOption('solr', 'ssl_cainfo');
188:
189:
190:
191: $options['ssl_capath'] = $queryOption('solr', 'ssl_capath');
192:
193:
194: foreach ($options as $key => $value) {
195: if (0 == strlen(trim($value))) {
196: unset($options[$key]);
197: }
198: }
199:
200: return $options;
201: }
202:
203: 204: 205: 206: 207: 208: 209:
210: public static function validateClientOptions(array $options) {
211: $valid = true;
212: $valid &= array_key_exists('hostname', $options);
213: $valid &= array_key_exists('port', $options);
214: $valid &= array_key_exists('path', $options);
215:
216:
217:
218:
219:
220: if (!$valid) {
221: throw new SolrWarning(Solr::i18n('WARNING_INVALID_CLIENT_OPTIONS'));
222: }
223: }
224:
225: 226: 227: 228:
229: public static function logException(Exception $e) {
230: $cfg = cRegistry::getConfig();
231:
232: $log = new cLog(cLogWriter::factory('file', array(
233: 'destination' => $cfg['path']['contenido_logs'] . 'errorlog.txt'
234: )), cLog::ERR);
235:
236: $log->err($e->getMessage());
237: $log->err($e->getTraceAsString());
238: }
239:
240: 241: 242: 243: 244: 245:
246: public static function displayException(Exception $e, $showTrace = false) {
247: header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
248:
249: if (true) {
250:
251: $class = "ui-state-error";
252: $icon = "ui-icon-alert";
253: } else {
254:
255: $class = "ui-state-highlight";
256: $icon = "ui-icon-info";
257: }
258:
259: echo '<div class="ui-widget">';
260: echo '<div class="' . $class . ' ui-corner-all">';
261: echo '<p>';
262: echo '<span class="ui-icon ' . $icon . '"></span>';
263:
264: echo $e->getMessage();
265: if (true === $showTrace) {
266: echo '<pre style="overflow: auto">';
267: echo htmlentities($e->getTraceAsString(), ENT_COMPAT | ENT_HTML401, 'UTF-8');
268: echo '</pre>';
269: }
270: echo '</p>';
271: echo '</div>';
272: echo '</div>';
273: }
274:
275: 276: 277: 278: 279: 280: 281:
282: public static function notifyException(Exception $e) {
283: $cGuiNotification = new cGuiNotification();
284: $level = cGuiNotification::LEVEL_ERROR;
285: $message = $e->getMessage();
286:
287: return $cGuiNotification->returnNotification($level, $message);
288: }
289: }
290:
291:
292: $cfg['templates']['solr_right_bottom'] = $cfg['plugins'][Solr::getName()] . 'templates/template.right_bottom.tpl';
293:
294:
295: $pluginClassPath = 'contenido/plugins/' . Solr::getName() . '/';
296: cAutoload::addClassmapConfig(array(
297: 'SolrIndexer' => $pluginClassPath . 'classes/class.solr_indexer.php',
298: 'SolrSearcherAbstract' => $pluginClassPath . 'classes/class.solr_searcher_abstract.php',
299: 'SolrSearcherSimple' => $pluginClassPath . 'classes/class.solr_searcher_simple.php',
300: 'SolrSearchModule' => $pluginClassPath . 'classes/class.solr_search_module.php',
301: 'SolrRightBottomPage' => $pluginClassPath . 'classes/class.solr.gui.php',
302: 'SolrException' => $pluginClassPath . 'classes/class.solr_exception.php',
303: 'SolrWarning' => $pluginClassPath . 'classes/class.solr_warning.php'
304: ));
305: unset($pluginClassPath);
306:
307:
308:
309: cRegistry::getCecRegistry()->addChainFunction('Contenido.Action.con_saveart.AfterCall', 'SolrIndexer::handleStoringOfArticle');
310:
311: cRegistry::getCecRegistry()->addChainFunction('Contenido.Content.AfterStore', 'SolrIndexer::handleStoringOfContentEntry');
312: