Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like documentItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use documentItem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class documentItem extends Object |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Document number |
||
| 15 | * @var int |
||
| 16 | */ |
||
| 17 | var $document_srl = 0; |
||
| 18 | /** |
||
| 19 | * Language code |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | var $lang_code = null; |
||
| 23 | /** |
||
| 24 | * Status of allow trackback |
||
| 25 | * @var bool |
||
| 26 | */ |
||
| 27 | var $allow_trackback_status = null; |
||
| 28 | /** |
||
| 29 | * column list |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | var $columnList = array(); |
||
| 33 | /** |
||
| 34 | * allow script access list |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | var $allowscriptaccessList = array(); |
||
| 38 | /** |
||
| 39 | * allow script access key |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | var $allowscriptaccessKey = 0; |
||
| 43 | /** |
||
| 44 | * upload file list |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | var $uploadedFiles = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor |
||
| 51 | * @param int $document_srl |
||
| 52 | * @param bool $load_extra_vars |
||
| 53 | * @param array columnList |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | function documentItem($document_srl = 0, $load_extra_vars = true, $columnList = array()) |
||
|
|
|||
| 57 | { |
||
| 58 | $this->document_srl = $document_srl; |
||
| 59 | $this->columnList = $columnList; |
||
| 60 | |||
| 61 | $this->_loadFromDB($load_extra_vars); |
||
| 62 | } |
||
| 63 | |||
| 64 | function setDocument($document_srl, $load_extra_vars = true) |
||
| 65 | { |
||
| 66 | $this->document_srl = $document_srl; |
||
| 67 | $this->_loadFromDB($load_extra_vars); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get data from database, and set the value to documentItem object |
||
| 72 | * @param bool $load_extra_vars |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | function _loadFromDB($load_extra_vars = true) |
||
| 76 | { |
||
| 77 | if(!$this->document_srl) return; |
||
| 78 | |||
| 79 | $document_item = false; |
||
| 80 | $cache_put = false; |
||
| 81 | $columnList = array(); |
||
| 82 | $this->columnList = array(); |
||
| 83 | |||
| 84 | // cache controll |
||
| 85 | $oCacheHandler = CacheHandler::getInstance('object'); |
||
| 86 | if($oCacheHandler->isSupport()) |
||
| 87 | { |
||
| 88 | $cache_key = 'document_item:' . getNumberingPath($this->document_srl) . $this->document_srl; |
||
| 89 | $document_item = $oCacheHandler->get($cache_key); |
||
| 90 | if($document_item !== false) |
||
| 91 | { |
||
| 92 | $columnList = array('readed_count', 'voted_count', 'blamed_count', 'comment_count', 'trackback_count'); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $args = new stdClass(); |
||
| 97 | $args->document_srl = $this->document_srl; |
||
| 98 | $output = executeQuery('document.getDocument', $args, $columnList); |
||
| 99 | |||
| 100 | if($document_item === false) |
||
| 101 | { |
||
| 102 | $document_item = $output->data; |
||
| 103 | |||
| 104 | //insert in cache |
||
| 105 | if($document_item && $oCacheHandler->isSupport()) |
||
| 106 | { |
||
| 107 | $oCacheHandler->put($cache_key, $document_item); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | else |
||
| 111 | { |
||
| 112 | $document_item->readed_count = $output->data->readed_count; |
||
| 113 | $document_item->voted_count = $output->data->voted_count; |
||
| 114 | $document_item->blamed_count = $output->data->blamed_count; |
||
| 115 | $document_item->comment_count = $output->data->comment_count; |
||
| 116 | $document_item->trackback_count = $output->data->trackback_count; |
||
| 117 | } |
||
| 118 | |||
| 119 | $this->setAttribute($document_item, $load_extra_vars); |
||
| 120 | } |
||
| 121 | |||
| 122 | function setAttribute($attribute, $load_extra_vars=true) |
||
| 123 | { |
||
| 124 | if(!$attribute->document_srl) |
||
| 125 | { |
||
| 126 | $this->document_srl = null; |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | $this->document_srl = $attribute->document_srl; |
||
| 130 | $this->lang_code = $attribute->lang_code; |
||
| 131 | $this->adds($attribute); |
||
| 132 | |||
| 133 | // Tags |
||
| 134 | if($this->get('tags')) |
||
| 135 | { |
||
| 136 | $tag_list = explode(',', $this->get('tags')); |
||
| 137 | $tag_list = array_map('trim', $tag_list); |
||
| 138 | $this->add('tag_list', $tag_list); |
||
| 139 | } |
||
| 140 | |||
| 141 | $oDocumentModel = getModel('document'); |
||
| 142 | if($load_extra_vars) |
||
| 143 | { |
||
| 144 | $GLOBALS['XE_DOCUMENT_LIST'][$attribute->document_srl] = $this; |
||
| 145 | $oDocumentModel->setToAllDocumentExtraVars(); |
||
| 146 | } |
||
| 147 | $GLOBALS['XE_DOCUMENT_LIST'][$this->document_srl] = $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | function isExists() |
||
| 151 | { |
||
| 152 | return $this->document_srl ? true : false; |
||
| 153 | } |
||
| 154 | |||
| 155 | function isGranted() |
||
| 156 | { |
||
| 157 | if($_SESSION['own_document'][$this->document_srl]) return true; |
||
| 158 | |||
| 159 | if(!Context::get('is_logged')) return false; |
||
| 160 | |||
| 161 | $logged_info = Context::get('logged_info'); |
||
| 162 | if($logged_info->is_admin == 'Y') return true; |
||
| 163 | |||
| 164 | $oModuleModel = getModel('module'); |
||
| 165 | $grant = $oModuleModel->getGrant($oModuleModel->getModuleInfoByModuleSrl($this->get('module_srl')), $logged_info); |
||
| 166 | if($grant->manager) return true; |
||
| 167 | |||
| 168 | View Code Duplication | if($this->get('member_srl') && ($this->get('member_srl') == $logged_info->member_srl || $this->get('member_srl')*-1 == $logged_info->member_srl)) return true; |
|
| 169 | |||
| 170 | return false; |
||
| 171 | } |
||
| 172 | |||
| 173 | function setGrant() |
||
| 174 | { |
||
| 175 | $_SESSION['own_document'][$this->document_srl] = true; |
||
| 176 | } |
||
| 177 | |||
| 178 | function isAccessible() |
||
| 179 | { |
||
| 180 | return $_SESSION['accessible'][$this->document_srl]==true?true:false; |
||
| 181 | } |
||
| 182 | |||
| 183 | function allowComment() |
||
| 184 | { |
||
| 185 | // init write, document is not exists. so allow comment status is true |
||
| 186 | if(!$this->isExists()) return true; |
||
| 187 | |||
| 188 | return $this->get('comment_status') == 'ALLOW' ? true : false; |
||
| 189 | } |
||
| 190 | |||
| 191 | function allowTrackback() |
||
| 192 | { |
||
| 193 | static $allow_trackback_status = null; |
||
| 194 | if(is_null($allow_trackback_status)) |
||
| 195 | { |
||
| 196 | |||
| 197 | // Check the tarckback module exist |
||
| 198 | if(!getClass('trackback')) |
||
| 199 | { |
||
| 200 | $allow_trackback_status = false; |
||
| 201 | } |
||
| 202 | else |
||
| 203 | { |
||
| 204 | // If the trackback module is configured to be disabled, do not allow. Otherwise, check the setting of each module. |
||
| 205 | $oModuleModel = getModel('module'); |
||
| 206 | $trackback_config = $oModuleModel->getModuleConfig('trackback'); |
||
| 207 | |||
| 208 | if(!$trackback_config) |
||
| 209 | { |
||
| 210 | $trackback_config = new stdClass(); |
||
| 211 | } |
||
| 212 | |||
| 213 | if(!isset($trackback_config->enable_trackback)) $trackback_config->enable_trackback = 'Y'; |
||
| 214 | if($trackback_config->enable_trackback != 'Y') $allow_trackback_status = false; |
||
| 215 | else |
||
| 216 | { |
||
| 217 | $module_srl = $this->get('module_srl'); |
||
| 218 | // Check settings of each module |
||
| 219 | $module_config = $oModuleModel->getModulePartConfig('trackback', $module_srl); |
||
| 220 | if($module_config->enable_trackback == 'N') $allow_trackback_status = false; |
||
| 221 | else if($this->get('allow_trackback')=='Y' || !$this->isExists()) $allow_trackback_status = true; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | return $allow_trackback_status; |
||
| 226 | } |
||
| 227 | |||
| 228 | function isLocked() |
||
| 229 | { |
||
| 230 | if(!$this->isExists()) return false; |
||
| 231 | |||
| 232 | return $this->get('comment_status') == 'ALLOW' ? false : true; |
||
| 233 | } |
||
| 234 | |||
| 235 | function isEditable() |
||
| 236 | { |
||
| 237 | if($this->isGranted() || !$this->get('member_srl')) return true; |
||
| 238 | return false; |
||
| 239 | } |
||
| 240 | |||
| 241 | function isSecret() |
||
| 242 | { |
||
| 243 | $oDocumentModel = getModel('document'); |
||
| 244 | return $this->get('status') == $oDocumentModel->getConfigStatus('secret') ? true : false; |
||
| 245 | } |
||
| 246 | |||
| 247 | function isNotice() |
||
| 248 | { |
||
| 249 | return $this->get('is_notice') == 'Y' ? true : false; |
||
| 250 | } |
||
| 251 | |||
| 252 | function useNotify() |
||
| 253 | { |
||
| 254 | return $this->get('notify_message')=='Y' ? true : false; |
||
| 255 | } |
||
| 256 | |||
| 257 | function doCart() |
||
| 258 | { |
||
| 259 | if(!$this->document_srl) return false; |
||
| 260 | if($this->isCarted()) $this->removeCart(); |
||
| 261 | else $this->addCart(); |
||
| 262 | } |
||
| 263 | |||
| 264 | function addCart() |
||
| 265 | { |
||
| 266 | $_SESSION['document_management'][$this->document_srl] = true; |
||
| 267 | } |
||
| 268 | |||
| 269 | function removeCart() |
||
| 270 | { |
||
| 271 | unset($_SESSION['document_management'][$this->document_srl]); |
||
| 272 | } |
||
| 273 | |||
| 274 | function isCarted() |
||
| 275 | { |
||
| 276 | return $_SESSION['document_management'][$this->document_srl]; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Send notify message to document owner |
||
| 281 | * @param string $type |
||
| 282 | * @param string $content |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | function notify($type, $content) |
||
| 286 | { |
||
| 287 | if(!$this->document_srl) return; |
||
| 288 | // return if it is not useNotify |
||
| 289 | if(!$this->useNotify()) return; |
||
| 290 | // Pass if an author is not a logged-in user |
||
| 291 | if(!$this->get('member_srl')) return; |
||
| 292 | // Return if the currently logged-in user is an author |
||
| 293 | $logged_info = Context::get('logged_info'); |
||
| 294 | if($logged_info->member_srl == $this->get('member_srl')) return; |
||
| 295 | // List variables |
||
| 296 | if($type) $title = "[".$type."] "; |
||
| 297 | $title .= cut_str(strip_tags($content), 10, '...'); |
||
| 298 | $content = sprintf('%s<br /><br />from : <a href="%s" target="_blank">%s</a>',$content, getFullUrl('','document_srl',$this->document_srl), getFullUrl('','document_srl',$this->document_srl)); |
||
| 299 | $receiver_srl = $this->get('member_srl'); |
||
| 300 | $sender_member_srl = $logged_info->member_srl; |
||
| 301 | // Send a message |
||
| 302 | $oCommunicationController = getController('communication'); |
||
| 303 | $oCommunicationController->sendMessage($sender_member_srl, $receiver_srl, $title, $content, false); |
||
| 304 | } |
||
| 305 | |||
| 306 | function getLangCode() |
||
| 307 | { |
||
| 308 | return $this->get('lang_code'); |
||
| 309 | } |
||
| 310 | |||
| 311 | View Code Duplication | function getIpAddress() |
|
| 312 | { |
||
| 313 | if($this->isGranted()) |
||
| 314 | { |
||
| 315 | return $this->get('ipaddress'); |
||
| 316 | } |
||
| 317 | |||
| 318 | return '*' . strstr($this->get('ipaddress'), '.'); |
||
| 319 | } |
||
| 320 | |||
| 321 | function isExistsHomepage() |
||
| 322 | { |
||
| 323 | if(trim($this->get('homepage'))) return true; |
||
| 324 | return false; |
||
| 325 | } |
||
| 326 | |||
| 327 | function getHomepageUrl() |
||
| 328 | { |
||
| 329 | $url = trim($this->get('homepage')); |
||
| 330 | if(!$url) return; |
||
| 331 | |||
| 332 | View Code Duplication | if(strncasecmp('http://', $url, 7) !== 0 && strncasecmp('https://', $url, 8) !== 0) $url = 'http://' . $url; |
|
| 333 | |||
| 334 | return $url; |
||
| 335 | } |
||
| 336 | |||
| 337 | function getMemberSrl() |
||
| 338 | { |
||
| 339 | return $this->get('member_srl'); |
||
| 340 | } |
||
| 341 | |||
| 342 | function getUserID() |
||
| 343 | { |
||
| 344 | return htmlspecialchars($this->get('user_id'), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 345 | } |
||
| 346 | |||
| 347 | function getUserName() |
||
| 348 | { |
||
| 349 | return htmlspecialchars($this->get('user_name'), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 350 | } |
||
| 351 | |||
| 352 | function getNickName() |
||
| 353 | { |
||
| 354 | return htmlspecialchars($this->get('nick_name'), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 355 | } |
||
| 356 | |||
| 357 | function getLastUpdater() |
||
| 358 | { |
||
| 359 | return htmlspecialchars($this->get('last_updater'), ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 360 | } |
||
| 361 | |||
| 362 | function getTitleText($cut_size = 0, $tail='...') |
||
| 363 | { |
||
| 364 | if(!$this->document_srl) return; |
||
| 365 | |||
| 366 | if($cut_size) $title = cut_str($this->get('title'), $cut_size, $tail); |
||
| 367 | else $title = $this->get('title'); |
||
| 368 | |||
| 369 | return $title; |
||
| 370 | } |
||
| 371 | |||
| 372 | function getTitle($cut_size = 0, $tail='...') |
||
| 373 | { |
||
| 374 | if(!$this->document_srl) return; |
||
| 375 | |||
| 376 | $title = $this->getTitleText($cut_size, $tail); |
||
| 377 | |||
| 378 | $attrs = array(); |
||
| 379 | $this->add('title_color', trim($this->get('title_color'))); |
||
| 380 | if($this->get('title_bold')=='Y') $attrs[] = "font-weight:bold;"; |
||
| 381 | if($this->get('title_color') && $this->get('title_color') != 'N') $attrs[] = "color:#".$this->get('title_color'); |
||
| 382 | |||
| 383 | if(count($attrs)) return sprintf("<span style=\"%s\">%s</span>", implode(';',$attrs), htmlspecialchars($title, ENT_COMPAT | ENT_HTML401, 'UTF-8', false)); |
||
| 384 | else return htmlspecialchars($title, ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
||
| 385 | } |
||
| 386 | |||
| 387 | function getContentText($strlen = 0) |
||
| 388 | { |
||
| 389 | if(!$this->document_srl) return; |
||
| 390 | |||
| 391 | if($this->isSecret() && !$this->isGranted() && !$this->isAccessible()) return Context::getLang('msg_is_secret'); |
||
| 392 | |||
| 393 | $result = $this->_checkAccessibleFromStatus(); |
||
| 394 | if($result) $_SESSION['accessible'][$this->document_srl] = true; |
||
| 395 | |||
| 396 | $content = $this->get('content'); |
||
| 397 | $content = preg_replace_callback('/<(object|param|embed)[^>]*/is', array($this, '_checkAllowScriptAccess'), $content); |
||
| 398 | $content = preg_replace_callback('/<object[^>]*>/is', array($this, '_addAllowScriptAccess'), $content); |
||
| 399 | |||
| 400 | if($strlen) return cut_str(strip_tags($content),$strlen,'...'); |
||
| 401 | |||
| 402 | return htmlspecialchars($content); |
||
| 403 | } |
||
| 404 | |||
| 405 | View Code Duplication | function _addAllowScriptAccess($m) |
|
| 406 | { |
||
| 407 | if($this->allowscriptaccessList[$this->allowscriptaccessKey] == 1) |
||
| 408 | { |
||
| 409 | $m[0] = $m[0].'<param name="allowscriptaccess" value="never"></param>'; |
||
| 410 | } |
||
| 411 | $this->allowscriptaccessKey++; |
||
| 412 | return $m[0]; |
||
| 413 | } |
||
| 414 | |||
| 415 | View Code Duplication | function _checkAllowScriptAccess($m) |
|
| 416 | { |
||
| 417 | if($m[1] == 'object') |
||
| 418 | { |
||
| 419 | $this->allowscriptaccessList[] = 1; |
||
| 420 | } |
||
| 421 | |||
| 422 | if($m[1] == 'param') |
||
| 423 | { |
||
| 424 | if(stripos($m[0], 'allowscriptaccess')) |
||
| 425 | { |
||
| 426 | $m[0] = '<param name="allowscriptaccess" value="never"'; |
||
| 427 | if(substr($m[0], -1) == '/') |
||
| 428 | { |
||
| 429 | $m[0] .= '/'; |
||
| 430 | } |
||
| 431 | $this->allowscriptaccessList[count($this->allowscriptaccessList)-1]--; |
||
| 432 | } |
||
| 433 | } |
||
| 434 | else if($m[1] == 'embed') |
||
| 435 | { |
||
| 436 | if(stripos($m[0], 'allowscriptaccess')) |
||
| 437 | { |
||
| 438 | $m[0] = preg_replace('/always|samedomain/i', 'never', $m[0]); |
||
| 439 | } |
||
| 440 | else |
||
| 441 | { |
||
| 442 | $m[0] = preg_replace('/\<embed/i', '<embed allowscriptaccess="never"', $m[0]); |
||
| 443 | } |
||
| 444 | } |
||
| 445 | return $m[0]; |
||
| 446 | } |
||
| 447 | |||
| 448 | function getContent($add_popup_menu = true, $add_content_info = true, $resource_realpath = false, $add_xe_content_class = true, $stripEmbedTagException = false) |
||
| 449 | { |
||
| 450 | if(!$this->document_srl) return; |
||
| 451 | |||
| 452 | if($this->isSecret() && !$this->isGranted() && !$this->isAccessible()) return Context::getLang('msg_is_secret'); |
||
| 453 | |||
| 454 | $result = $this->_checkAccessibleFromStatus(); |
||
| 455 | if($result) $_SESSION['accessible'][$this->document_srl] = true; |
||
| 456 | |||
| 457 | $content = $this->get('content'); |
||
| 458 | if(!$stripEmbedTagException) stripEmbedTagForAdmin($content, $this->get('member_srl')); |
||
| 459 | |||
| 460 | // Define a link if using a rewrite module |
||
| 461 | $oContext = &Context::getInstance(); |
||
| 462 | if($oContext->allow_rewrite) |
||
| 463 | { |
||
| 464 | $content = preg_replace('/<a([ \t]+)href=("|\')\.\/\?/i',"<a href=\\2". Context::getRequestUri() ."?", $content); |
||
| 465 | } |
||
| 466 | // To display a pop-up menu |
||
| 467 | if($add_popup_menu) |
||
| 468 | { |
||
| 469 | $content = sprintf( |
||
| 470 | '%s<div class="document_popup_menu"><a href="#popup_menu_area" class="document_%d" onclick="return false">%s</a></div>', |
||
| 471 | $content, |
||
| 472 | $this->document_srl, Context::getLang('cmd_document_do') |
||
| 473 | ); |
||
| 474 | } |
||
| 475 | // If additional content information is set |
||
| 476 | View Code Duplication | if($add_content_info) |
|
| 477 | { |
||
| 478 | $memberSrl = $this->get('member_srl'); |
||
| 479 | if($memberSrl < 0) |
||
| 480 | { |
||
| 481 | $memberSrl = 0; |
||
| 482 | } |
||
| 483 | $content = sprintf( |
||
| 484 | '<!--BeforeDocument(%d,%d)--><div class="document_%d_%d xe_content">%s</div><!--AfterDocument(%d,%d)-->', |
||
| 485 | $this->document_srl, $memberSrl, |
||
| 486 | $this->document_srl, $memberSrl, |
||
| 487 | $content, |
||
| 488 | $this->document_srl, $memberSrl, |
||
| 489 | $this->document_srl, $memberSrl |
||
| 490 | ); |
||
| 491 | // Add xe_content class although accessing content is not required |
||
| 492 | } |
||
| 493 | else |
||
| 494 | { |
||
| 495 | if($add_xe_content_class) $content = sprintf('<div class="xe_content">%s</div>', $content); |
||
| 496 | } |
||
| 497 | // Change the image path to a valid absolute path if resource_realpath is true |
||
| 498 | if($resource_realpath) |
||
| 499 | { |
||
| 500 | $content = preg_replace_callback('/<img([^>]+)>/i',array($this,'replaceResourceRealPath'), $content); |
||
| 501 | } |
||
| 502 | |||
| 503 | return $content; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Return transformed content by Editor codes |
||
| 508 | * @param bool $add_popup_menu |
||
| 509 | * @param bool $add_content_info |
||
| 510 | * @param bool $resource_realpath |
||
| 511 | * @param bool $add_xe_content_class |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | function getTransContent($add_popup_menu = true, $add_content_info = true, $resource_realpath = false, $add_xe_content_class = true) |
||
| 515 | { |
||
| 516 | $oEditorController = getController('editor'); |
||
| 517 | |||
| 518 | $content = $this->getContent($add_popup_menu, $add_content_info, $resource_realpath, $add_xe_content_class); |
||
| 519 | $content = $oEditorController->transComponent($content); |
||
| 520 | |||
| 521 | return $content; |
||
| 522 | } |
||
| 523 | |||
| 524 | View Code Duplication | function getSummary($str_size = 50, $tail = '...') |
|
| 525 | { |
||
| 526 | $content = $this->getContent(FALSE, FALSE); |
||
| 527 | |||
| 528 | $content = nl2br($content); |
||
| 529 | |||
| 530 | // For a newlink, inert a whitespace |
||
| 531 | $content = preg_replace('!(<br[\s]*/{0,1}>[\s]*)+!is', ' ', $content); |
||
| 532 | |||
| 533 | // Replace tags such as </p> , </div> , </li> and others to a whitespace |
||
| 534 | $content = str_replace(array('</p>', '</div>', '</li>', '-->'), ' ', $content); |
||
| 535 | |||
| 536 | // Remove Tags |
||
| 537 | $content = preg_replace('!<([^>]*?)>!is', '', $content); |
||
| 538 | |||
| 539 | // Replace < , >, " |
||
| 540 | $content = str_replace(array('<', '>', '"', ' '), array('<', '>', '"', ' '), $content); |
||
| 541 | |||
| 542 | // Delete a series of whitespaces |
||
| 543 | $content = preg_replace('/ ( +)/is', ' ', $content); |
||
| 544 | |||
| 545 | // Truncate string |
||
| 546 | $content = trim(cut_str($content, $str_size, $tail)); |
||
| 547 | |||
| 548 | // Replace back < , <, " |
||
| 549 | $content = str_replace(array('<', '>', '"'),array('<', '>', '"'), $content); |
||
| 550 | |||
| 551 | return $content; |
||
| 552 | } |
||
| 553 | |||
| 554 | function getRegdate($format = 'Y.m.d H:i:s') |
||
| 555 | { |
||
| 556 | return zdate($this->get('regdate'), $format); |
||
| 557 | } |
||
| 558 | |||
| 559 | View Code Duplication | function getRegdateTime() |
|
| 560 | { |
||
| 561 | $regdate = $this->get('regdate'); |
||
| 562 | $year = substr($regdate,0,4); |
||
| 563 | $month = substr($regdate,4,2); |
||
| 564 | $day = substr($regdate,6,2); |
||
| 565 | $hour = substr($regdate,8,2); |
||
| 566 | $min = substr($regdate,10,2); |
||
| 567 | $sec = substr($regdate,12,2); |
||
| 568 | return mktime($hour,$min,$sec,$month,$day,$year); |
||
| 569 | } |
||
| 570 | |||
| 571 | function getRegdateGM() |
||
| 572 | { |
||
| 573 | return $this->getRegdate('D, d M Y H:i:s').' '.$GLOBALS['_time_zone']; |
||
| 574 | } |
||
| 575 | |||
| 576 | function getRegdateDT() |
||
| 577 | { |
||
| 578 | return $this->getRegdate('Y-m-d').'T'.$this->getRegdate('H:i:s').substr($GLOBALS['_time_zone'],0,3).':'.substr($GLOBALS['_time_zone'],3,2); |
||
| 579 | } |
||
| 580 | |||
| 581 | function getUpdate($format = 'Y.m.d H:i:s') |
||
| 582 | { |
||
| 583 | return zdate($this->get('last_update'), $format); |
||
| 584 | } |
||
| 585 | |||
| 586 | View Code Duplication | function getUpdateTime() |
|
| 587 | { |
||
| 588 | $year = substr($this->get('last_update'),0,4); |
||
| 589 | $month = substr($this->get('last_update'),4,2); |
||
| 590 | $day = substr($this->get('last_update'),6,2); |
||
| 591 | $hour = substr($this->get('last_update'),8,2); |
||
| 592 | $min = substr($this->get('last_update'),10,2); |
||
| 593 | $sec = substr($this->get('last_update'),12,2); |
||
| 594 | return mktime($hour,$min,$sec,$month,$day,$year); |
||
| 595 | } |
||
| 596 | |||
| 597 | function getUpdateGM() |
||
| 598 | { |
||
| 599 | return gmdate("D, d M Y H:i:s", $this->getUpdateTime()); |
||
| 600 | } |
||
| 601 | |||
| 602 | function getUpdateDT() |
||
| 603 | { |
||
| 604 | return $this->getUpdate('Y-m-d').'T'.$this->getUpdate('H:i:s').substr($GLOBALS['_time_zone'],0,3).':'.substr($GLOBALS['_time_zone'],3,2); |
||
| 605 | } |
||
| 606 | |||
| 607 | function getPermanentUrl() |
||
| 608 | { |
||
| 609 | return getFullUrl('','document_srl',$this->get('document_srl')); |
||
| 610 | } |
||
| 611 | |||
| 612 | function getTrackbackUrl() |
||
| 613 | { |
||
| 614 | if(!$this->document_srl) return; |
||
| 615 | |||
| 616 | // Generate a key to prevent spams |
||
| 617 | $oTrackbackModel = getModel('trackback'); |
||
| 618 | if($oTrackbackModel) return $oTrackbackModel->getTrackbackUrl($this->document_srl, $this->getDocumentMid()); |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Update readed count |
||
| 623 | * @return void |
||
| 624 | */ |
||
| 625 | function updateReadedCount() |
||
| 626 | { |
||
| 627 | $oDocumentController = getController('document'); |
||
| 628 | if($oDocumentController->updateReadedCount($this)) |
||
| 629 | { |
||
| 630 | $readed_count = $this->get('readed_count'); |
||
| 631 | $this->add('readed_count', $readed_count+1); |
||
| 632 | } |
||
| 633 | } |
||
| 634 | |||
| 635 | function isExtraVarsExists() |
||
| 636 | { |
||
| 637 | if(!$this->get('module_srl')) return false; |
||
| 638 | $oDocumentModel = getModel('document'); |
||
| 639 | $extra_keys = $oDocumentModel->getExtraKeys($this->get('module_srl')); |
||
| 640 | return count($extra_keys)?true:false; |
||
| 641 | } |
||
| 642 | |||
| 643 | function getExtraVars() |
||
| 644 | { |
||
| 645 | if(!$this->get('module_srl') || !$this->document_srl) return null; |
||
| 646 | |||
| 647 | $oDocumentModel = getModel('document'); |
||
| 648 | return $oDocumentModel->getExtraVars($this->get('module_srl'), $this->document_srl); |
||
| 649 | } |
||
| 650 | |||
| 651 | View Code Duplication | function getExtraValue($idx) |
|
| 663 | |||
| 664 | View Code Duplication | function getExtraValueHTML($idx) |
|
| 665 | { |
||
| 666 | $extra_vars = $this->getExtraVars(); |
||
| 667 | if(is_array($extra_vars) && array_key_exists($idx,$extra_vars)) |
||
| 668 | { |
||
| 669 | return $extra_vars[$idx]->getValueHTML(); |
||
| 670 | } |
||
| 671 | else |
||
| 672 | { |
||
| 673 | return ''; |
||
| 674 | } |
||
| 675 | } |
||
| 676 | |||
| 677 | View Code Duplication | function getExtraEidValue($eid) |
|
| 699 | |||
| 700 | View Code Duplication | function getExtraEidValueHTML($eid) |
|
| 718 | |||
| 719 | function getExtraVarsValue($key) |
||
| 720 | { |
||
| 721 | $extra_vals = unserialize($this->get('extra_vars')); |
||
| 722 | $val = $extra_vals->$key; |
||
| 723 | return $val; |
||
| 724 | } |
||
| 725 | |||
| 726 | function getCommentCount() |
||
| 727 | { |
||
| 728 | return $this->get('comment_count'); |
||
| 729 | } |
||
| 730 | |||
| 731 | function getComments() |
||
| 732 | { |
||
| 733 | if(!$this->getCommentCount()) return; |
||
| 734 | if(!$this->isGranted() && $this->isSecret()) return; |
||
| 735 | // cpage is a number of comment pages |
||
| 736 | $cpageStr = sprintf('%d_cpage', $this->document_srl); |
||
| 737 | $cpage = Context::get($cpageStr); |
||
| 738 | |||
| 739 | if(!$cpage) |
||
| 740 | { |
||
| 741 | $cpage = Context::get('cpage'); |
||
| 742 | } |
||
| 743 | |||
| 744 | // Get a list of comments |
||
| 745 | $oCommentModel = getModel('comment'); |
||
| 746 | $output = $oCommentModel->getCommentList($this->document_srl, $cpage, $is_admin); |
||
| 747 | if(!$output->toBool() || !count($output->data)) return; |
||
| 748 | // Create commentItem object from a comment list |
||
| 749 | // If admin priviledge is granted on parent posts, you can read its child posts. |
||
| 750 | $accessible = array(); |
||
| 751 | $comment_list = array(); |
||
| 752 | foreach($output->data as $key => $val) |
||
| 753 | { |
||
| 754 | $oCommentItem = new commentItem(); |
||
| 755 | $oCommentItem->setAttribute($val); |
||
| 756 | // If permission is granted to the post, you can access it temporarily |
||
| 757 | if($oCommentItem->isGranted()) $accessible[$val->comment_srl] = true; |
||
| 758 | // If the comment is set to private and it belongs child post, it is allowable to read the comment for who has a admin privilege on its parent post |
||
| 759 | if($val->parent_srl>0 && $val->is_secret == 'Y' && !$oCommentItem->isAccessible() && $accessible[$val->parent_srl]===true) |
||
| 760 | { |
||
| 761 | $oCommentItem->setAccessible(); |
||
| 762 | } |
||
| 763 | $comment_list[$val->comment_srl] = $oCommentItem; |
||
| 764 | } |
||
| 765 | // Variable setting to be displayed on the skin |
||
| 766 | Context::set($cpageStr, $output->page_navigation->cur_page); |
||
| 767 | Context::set('cpage', $output->page_navigation->cur_page); |
||
| 768 | if($output->total_page>1) $this->comment_page_navigation = $output->page_navigation; |
||
| 769 | |||
| 770 | return $comment_list; |
||
| 771 | } |
||
| 772 | |||
| 773 | function getTrackbackCount() |
||
| 774 | { |
||
| 775 | return $this->get('trackback_count'); |
||
| 776 | } |
||
| 777 | |||
| 778 | function getTrackbacks() |
||
| 779 | { |
||
| 780 | if(!$this->document_srl) return; |
||
| 781 | |||
| 782 | if(!$this->allowTrackback() || !$this->get('trackback_count')) return; |
||
| 783 | |||
| 784 | $oTrackbackModel = getModel('trackback'); |
||
| 785 | return $oTrackbackModel->getTrackbackList($this->document_srl, $is_admin); |
||
| 786 | } |
||
| 787 | |||
| 788 | function thumbnailExists($width = 80, $height = 0, $type = '') |
||
| 789 | { |
||
| 790 | if(!$this->document_srl) return false; |
||
| 791 | if(!$this->getThumbnail($width, $height, $type)) return false; |
||
| 792 | return true; |
||
| 793 | } |
||
| 794 | |||
| 795 | function getThumbnail($width = 80, $height = 0, $thumbnail_type = '') |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Functions to display icons for new post, latest update, secret(private) post, image/video/attachment |
||
| 937 | * Determine new post and latest update by $time_interval |
||
| 938 | * @param int $time_interval |
||
| 939 | * @return array |
||
| 940 | */ |
||
| 941 | function getExtraImages($time_interval = 43200) |
||
| 942 | { |
||
| 943 | if(!$this->document_srl) return; |
||
| 944 | // variables for icon list |
||
| 945 | $buffs = array(); |
||
| 946 | |||
| 947 | $check_files = false; |
||
| 948 | |||
| 949 | // Check if secret post is |
||
| 950 | if($this->isSecret()) $buffs[] = "secret"; |
||
| 951 | |||
| 952 | // Set the latest time |
||
| 953 | $time_check = date("YmdHis", $_SERVER['REQUEST_TIME']-$time_interval); |
||
| 954 | |||
| 955 | // Check new post |
||
| 956 | if($this->get('regdate')>$time_check) $buffs[] = "new"; |
||
| 957 | else if($this->get('last_update')>$time_check) $buffs[] = "update"; |
||
| 958 | |||
| 959 | /* |
||
| 960 | $content = $this->get('content'); |
||
| 961 | |||
| 962 | // Check image files |
||
| 963 | preg_match_all('!<img([^>]*?)>!is', $content, $matches); |
||
| 964 | $cnt = count($matches[0]); |
||
| 965 | for($i=0;$i<$cnt;$i++) { |
||
| 966 | if(preg_match('/editor_component=/',$matches[0][$i])&&!preg_match('/image_(gallery|link)/i',$matches[0][$i])) continue; |
||
| 967 | $buffs[] = "image"; |
||
| 968 | $check_files = true; |
||
| 969 | break; |
||
| 970 | } |
||
| 971 | |||
| 972 | // Check video files |
||
| 973 | if(preg_match('!<embed([^>]*?)>!is', $content) || preg_match('/editor_component=("|\')*multimedia_link/i', $content) ) { |
||
| 974 | $buffs[] = "movie"; |
||
| 975 | $check_files = true; |
||
| 976 | } |
||
| 977 | */ |
||
| 978 | |||
| 979 | // Check the attachment |
||
| 980 | if($this->hasUploadedFiles()) $buffs[] = "file"; |
||
| 981 | |||
| 982 | return $buffs; |
||
| 983 | } |
||
| 984 | |||
| 985 | function getStatus() |
||
| 986 | { |
||
| 987 | if(!$this->get('status')) return $this->getDefaultStatus(); |
||
| 988 | return $this->get('status'); |
||
| 989 | } |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Return the value obtained from getExtraImages with image tag |
||
| 993 | * @param int $time_check |
||
| 994 | * @return string |
||
| 995 | */ |
||
| 996 | function printExtraImages($time_check = 43200) |
||
| 1012 | |||
| 1013 | View Code Duplication | function hasUploadedFiles() |
|
| 1020 | |||
| 1021 | function getUploadedFiles($sortIndex = 'file_srl') |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Return Editor html |
||
| 1039 | * @return string |
||
| 1040 | */ |
||
| 1041 | View Code Duplication | function getEditor() |
|
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Check whether to have a permission to write comment |
||
| 1052 | * Authority to write a comment and to write a document is separated |
||
| 1053 | * @return bool |
||
| 1054 | */ |
||
| 1055 | function isEnableComment() |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Return comment editor's html |
||
| 1066 | * @return string |
||
| 1067 | */ |
||
| 1068 | function getCommentEditor() |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Return author's profile image |
||
| 1078 | * @return string |
||
| 1079 | */ |
||
| 1080 | View Code Duplication | function getProfileImage() |
|
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Return author's signiture |
||
| 1092 | * @return string |
||
| 1093 | */ |
||
| 1094 | View Code Duplication | function getSignature() |
|
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Change an image path in the content to absolute path |
||
| 1119 | * @param array $matches |
||
| 1120 | * @return mixed |
||
| 1121 | */ |
||
| 1122 | function replaceResourceRealPath($matches) |
||
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Check accessible by document status |
||
| 1129 | * @param array $matches |
||
| 1130 | * @return mixed |
||
| 1131 | */ |
||
| 1132 | function _checkAccessibleFromStatus() |
||
| 1152 | |||
| 1153 | function getTranslationLangCodes() |
||
| 1172 | |||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Returns the document's mid in order to construct SEO friendly URLs |
||
| 1176 | * @return string |
||
| 1177 | */ |
||
| 1178 | function getDocumentMid() |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Returns the document's type (document/page/wiki/board/etc) |
||
| 1187 | * @return string |
||
| 1188 | */ |
||
| 1189 | function getDocumentType() |
||
| 1195 | |||
| 1196 | /** |
||
| 1197 | * Returns the document's alias |
||
| 1198 | * @return string |
||
| 1199 | */ |
||
| 1200 | function getDocumentAlias() |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Returns the document's actual title (browser_title) |
||
| 1208 | * @return string |
||
| 1209 | */ |
||
| 1210 | function getModuleName() |
||
| 1216 | |||
| 1217 | function getBrowserTitle() |
||
| 1221 | } |
||
| 1222 | /* End of file document.item.php */ |
||
| 1224 |