-
Notifications
You must be signed in to change notification settings - Fork 11
/
overrides.php
128 lines (114 loc) · 4.57 KB
/
overrides.php
1
2
3
4
5
6
7
8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../');
exit;
}
function qa_q_request($questionid, $title)
{
// URL Clean
if (qa_opt('useo_url_cleanup')){ //clean url's title
$words = qa_opt('useo_url_words_list');
$raw_title = $title;
// ~~ str_replace method
//$word_list = explode(',', $words);
//$title = str_replace($word_list, '', $raw_title);
// ~~preg_replace method with Q2A functions
require_once QA_INCLUDE_DIR.'qa-util-string.php';
$word_list = qa_block_words_to_preg($words);
$title=trim(qa_block_words_replace($title, $word_list , ''));
if ( (strlen($title)==0) && (qa_opt('useo_url_dont_make_empty')) )
$title = $raw_title;
}
$url = qa_q_request_base($questionid, $title);
// capitalize letters
if (qa_opt('useo_url_q_uppercase')){
$type = qa_opt('useo_url_q_uppercase_type');
if($type==1){ // first word's first letter
$parts = explode('/', $url);
$parts[1] = ucfirst($parts[1]);
$url = implode('/', $parts);
}else if($type==2) // all word's first letter
$url = str_replace(' ', '?', ucwords(str_replace('?', ' ', str_replace(' ', '/', ucwords(str_replace('/', ' ', str_replace(' ', '-', ucwords( str_replace('-', ' ', strtolower($url)) )) )) ))));
else // whole words
$url = strtoupper($url);
}
return $url;
}
function qa_tag_html($tag, $microformats=false, $favorited=false)
{
// URL Customization
$type = qa_opt('useo_url_tag_uppercase_type');
if($type==1){ // first word's first letter
$taglink = ucfirst($tag);
}else if($type==2) // all word's first letter
$taglink = str_replace(' ', '?', ucwords(str_replace('?', ' ', str_replace(' ', '/', ucwords(str_replace('/', ' ', str_replace(' ', '-', ucwords( str_replace('-', ' ', strtolower($tag)) )) )) ))));
else // whole words
$taglink = strtoupper($tag);
// Tag Description
global $useo_tag_desc_list;
require_once QA_INCLUDE_DIR.'qa-util-string.php';
$taglc=qa_strtolower($tag);
$useo_tag_desc_list[$taglc]=true;
return '<a href="'.qa_path_html('tag/'.$taglink).'"'.($microformats ? ' rel="tag"' : '').' class="qa-tag-link'.
($favorited ? ' qa-tag-favorited' : '').'">'.qa_html($tag).'</a>';
}
function qa_sanitize_html($html, $linksnewwindow=false, $storage=false)
{
$safe=qa_sanitize_html_base($html, $linksnewwindow, $storage);
if ($safe == '')
return '';
$rel_types = array(1 => 'Nofollow', 2 => 'External', 3 => 'Nofollow External', 4 => '');
$links_list=json_decode(qa_opt('useo_link_relations'));
$dom = new DOMDocument;
$encod = mb_detect_encoding($safe);
$dom->loadHTML(mb_convert_encoding($safe, 'HTML-ENTITIES', $encod));
$links = $dom->getElementsByTagName('a');
// apply rel change to list of links
if(is_array(@$links) && count($links))
foreach ($links as $link) {
if(count($links_list))
foreach($links_list as $key=>$value)
{
$site_url=parse_url($value->url);
// add rel attribute according to host address
$link_attribute = $link->getAttribute('href');
if( (isset($site_url['host'])) && (!(empty($site_url['host']))) && (!(empty($link_attribute))) )
if (strpos( strtolower($link->getAttribute('href')) , strtolower($site_url['host']) ))
$link->setAttribute('rel', $rel_types[$value->rel]);
}
}
if( qa_opt('useo_links_internal_dofollow') )
{
if(is_array(@$links) && count($links))
foreach ($links as $link) {
$site_url=parse_url(qa_opt('site_url'));
if (strpos( strtolower($link->getAttribute('href')) , strtolower($site_url['host']) ))
$link->setAttribute('rel', '');
}
$safe = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
}
return $safe;
}
function qa_html_convert_urls($html, $newwindow=false)
{
$host=useo_get_host($html);
$rel_types = array(1 => 'nofollow', 2 => 'external', 3 => 'external nofollow', 4 => 'dofollow');
$links_list=json_decode(qa_opt('useo_link_relations'));
$rel='nofollow';
if(is_array(@$links_list) && count($links_list)){
foreach($links_list as $key=>$value)
if (useo_get_host($value->url) == $host){
$rel=$rel_types[$value->rel];
}
}
if( qa_opt('useo_links_internal_dofollow') ){
$host = useo_get_host($html);
$site_url=parse_url(qa_opt('site_url'));
if($host==$site_url['host'])
$rel = 'dofollow';
}
return substr(preg_replace('/([^A-Za-z0-9])((http|https|ftp):\/\/([^\s&<>"\'\.])+\.([^\s&<>"\']|&)+)/i', '\1<a href="\2" rel="'. $rel .'"'.($newwindow ? ' target="_blank"' : '').'>\2</a>', ' '.$html.' '), 1, -1);
}
/*
Omit PHP closing tag to help avoid accidental output
*/