forked from 512banque/Uspin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUltimateSpinning.class.php
More file actions
executable file
·274 lines (238 loc) · 10 KB
/
UltimateSpinning.class.php
File metadata and controls
executable file
·274 lines (238 loc) · 10 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
include_once './tools/Tools.php';
require_once 'vendor/autoload.php';
// permet d'éviter que la fonction ne "plante" si votre texte à spinner est trop long
ini_set('pcre.backtrack_limit', 10000000);
ini_set('pcre.recursion_limit', 10000000);
/*
Script créé par Harisseo (www.harisseo.com) et 512banque (www.deliciouscadaver.com)
Vous avez le droit d'inclure ce script dans un projet commercial. Vous n'avez pas le droit de le vendre en l'état ou de le donner.
Merci de ne simplement pas supprimer ces lignes de commentaire, ce ne serait pas correct de votre part. Gardez un bon esprit, jouez le jeu et amusez-vous avec ce script ;)
Bon spin à tous !
*/
class UltimateSpinning{
public $DELIMITER_L = '{';
public $DELIMITER_R = '}';
public $spinnableText='';
public $spinnedText='';
public $kw=array();
function __construct($spinContent, $kw=array())
{
if($spinContent=='')
{
$this->error('Texte vide');
}elseif(!is_string($spinContent))
{
$this->error('Entrez une chaine de caractère...');
}else
{
$this->spinnableText = ($spinContent);
}
$this->checkMySpin();
if(!is_array($kw))
{
$this->error('Entrez une chaine de caractère...');
}else
{
$this->kw = $kw;
$this->parse_kw();
}
}
function checkMySpin()
{
if(substr_count($this->spinnableText, $this->DELIMITER_L)!=substr_count($this->spinnableText, $this->DELIMITER_R)) $this->error('Délimiteurs mal fermés');
return true;
}
function setSpinnedText($spinnedText)
{
if($spinnedText=='')
{
$this->error('Texte vide');
}elseif(!is_string($spinnedText))
{
$this->error('Entrez une chaine de caractère...');
}else
{
$this->spinnedText = ($spinnedText);
}
}
function error($error, $exit=true)
{
echo('[!-ERROR-!]] '.$error.'<br>');
if($exit) exit();
}
function howManyKeyWords($words, $detail=false)
{
if($words=='')
{
$this->error('Texte vide');
}else
{
$all_words = explode('|', $words);
$counter = 0;
$words_detail = array();
foreach($all_words as $key=>$word)
{
$counter+= substr_count($this->spinnedText, $word);
if($detail)
{
$words_detail[$word] = $counter;
$counter = 0;
}
}
}
if($detail) return $words_detail; else return $counter;
}
function spinIt()
{
$pattern = '#\{([^{}]*)\}#msi';
$test = preg_match_all($pattern, $this->spinnableText, $out);
if (!$test) {
$this->spinnedText = $this->spinnableText;
}else
{
$atrouver = array();
$aremplacer = array();
foreach($out[0] as $id => $match)
{
$choisir = explode("|", $out[1][$id]);
$atrouver = trim($match);
$aremplacer = trim($choisir[rand(0, count($choisir)-1)]);
$this->spinnableText = str_replace_once($atrouver, $aremplacer, $this->spinnableText);
}
return $this->spinIt($this->spinnableText);
}
//traitement du #
$this->spinnedText = $this->parse_sharp($this->spinnedText);
//traitement des blocks
$this->spinnedText = $this->parse_blocks($this->spinnedText);
//traitement du bbcode
$this->spinnedText = $this->bb_parse($this->spinnedText);
return $this->spinnedText;
}
function parse_sharp($string) {
preg_match_all('`#([\d]+)#`siU', $string, $matches);
$alea = $matches[1][array_rand($matches[1])];
$string = preg_replace('`#'.$alea.'#(.+)#/'.$alea.'#`siU', '$1', $string);
$string = preg_replace('`#[^'.$alea.']#(.+)#/[^'.$alea.']#`siU', '', $string);
return $string;
}
function parse_kw(){
if(empty($this->kw)) return;
preg_match_all('#\[\*(\d+)\*\]#', $this->spinnableText, $matches);
foreach($matches[1] as $key=>$value){
$this->spinnableText = str_replace($matches[0][$key], trim($this->kw[$value]), $this->spinnableText);
}
}
function bb_parse($string) {
while (preg_match_all('`\[((?!blocks).+?)=?(.*?)\](.+?)\[/\1\]`si', $string, $matches)) foreach ($matches[0] as $key => $match) {
//echo '<pre>';print_r($matches);echo '</pre>';
list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);
switch ($tag) {
case 'b': $replacement = "<strong>$innertext</strong>"; break;
case 'i': $replacement = "<em>$innertext</em>"; break;
case 'size': $replacement = "<span style=\"font-size: $param;\">$innertext</span>"; break;
case 'comment': $replacement = "<!--".$innertext."-->"; break;
case 'color': $replacement = "<span style=\"color: $param;\">$innertext</span>"; break;
case 'center': $replacement = "<center>".$innertext."</center>"; break;
case 'url': $replacement = '<a href="' . ($param? $param : $innertext) . "\">$innertext</a>"; break;
case 'list':
$list = explode(';', $innertext);
shuffle($list);
if(!isset($param) || $param==''){
$params = array("=>", "*", ">>", "->", "|->", "#", "~", ",");
$randkey = array_rand($params);
$param = $params[$randkey];
}
switch($param){
case 'li':
$stringf = '<ul>';
foreach($list as $key=>$elt){
$stringf .= '<li>'.trim($elt).'</li>';
}
$stringf .= '</ul>';
$replacement = $stringf;
break;
case ',':
case ',+':
$stringf = '';
foreach($list as $key=>$elt){
if($param==',+'&&$z==0) {$param = ','; $stringf .= ucfirst(strtolower($elt)).$param.' '; }
elseif($z==count($list)-1) { $stringf = mb_substr($stringf,0,-2,'utf-8'); $stringf .= ' et '.strtolower($elt); } // avant-dernier
elseif($z==count($list)) { $stringf .= strtolower($elt); } // dernier
else { $stringf .= strtolower($elt).$param.' '; }
$z++;
}
$replacement = $stringf;
break;
default:
$stringf = '';
foreach($list as $key=>$elt){
$stringf .= $param.' '.$elt.' '.'<br>';
}
$replacement = $stringf;
break;
}
break;
case 'flickr':
$f = new \Samwilson\PhpFlickr\PhpFlickr("c7db253c116bbd8633f044e94515e612", "61048141d5a74cbd");
$f->enableCache("fs", "./tools/phpFlickr/cache");
$photos = $f->photos_search(array("text"=>"$innertext", "per_page"=>250));
$randkey = array_rand($photos['photo']);
$replacement = '<img src="'.$f->buildPhotoURL($photos['photo'][$randkey], "medium").'" /><br>';
$replacement .= $photos['photo'][$randkey]['title'];
break;
case 'blocks':
$list = explode('|', $innertext);
shuffle($list);
$stringf = '';
foreach($list as $key=>$elt){
$stringf .= $elt;
}
$replacement = $stringf;
break;
case 'img':
$replacement = "<img src=\"$innertext\" />";
break;
case 'video':
$videourl = parse_url($innertext);
parse_str($videourl['query'], $videoquery);
if (strpos($videourl['host'], 'youtube.com') !== FALSE) $replacement = '<embed src="http://www.youtube.com/v/' . $videoquery['v'] . '" type="application/x-shockwave-flash" width="425" height="344"></embed>';
if (strpos($videourl['host'], 'google.com') !== FALSE) $replacement = '<embed src="http://video.google.com/googleplayer.swf?docid=' . $videoquery['docid'] . '" width="400" height="326" type="application/x-shockwave-flash"></embed>';
break;
default: if($param!=''){$replacement = '<'.$param.'>'.$innertext.'</'.$param.'>';}else{$replacement = $innertext;} break;
}
$string = str_replace($match, $replacement, $string);
}
return $string;
}
function parse_blocks(){ // a traiter après le spin
$this->spinnedText = str_replace('[blocks]','{',$this->spinnedText);
$this->spinnedText = str_replace('[/blocks]','}',$this->spinnedText);
$pattern = '#\{([^{}]*)\}#msi';
$test = preg_match_all($pattern, $this->spinnedText, $out);
if (!$test) return $this->spinnedText;
$atrouver = array();
$aremplacer = array();
foreach($out[0] as $id => $match)
{
$choisir = explode("|", $out[1][$id]);
shuffle($choisir);
$atrouver[] = trim($match);
$aremplacer[] = trim(implode('',$choisir));
}
$this->spinnedText = str_replace($atrouver, $aremplacer, $this->spinnedText);
return $this->parse_blocks($this->spinnedText);
}
}
function str_replace_once($search, $replace, $subject) {
$firstChar = strpos($subject, $search);
if($firstChar !== false) {
$beforeStr = substr($subject,0,$firstChar);
$afterStr = substr($subject, $firstChar + strlen($search));
return $beforeStr.$replace.$afterStr;
} else {
return $subject;
}
}
?>