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