Untracking backupfiles
[mod_lotto.git] / helper.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
 * @subpackage	mod_lotto
 * @copyright	Copyright (C) 2012 Ben Tasker . All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */


// no direct access
defined('_JEXEC') or die;

class modLottoHelper
{
	
function setparams($params){
$this->params = $params;
}

function getAPIKey(){


return $this->params->get('APIKEY', '9c59922ab287d59d06011a4c91a21fcf');


}

      function checkuserResults($game,$draws,$ball1,$ball2,$ball3,$ball4,$ball5,$ball6,$ball7 = null){
      return $this->place_request('UserCheck',$game,"ball1=$ball1&ball2=$ball2&ball3=$ball3&ball4=$ball4&ball5=$ball5&ball6=$ball6&ball7=$ball7&draws=$draws");

      }




       function generateEuroluckydip($number){
      // Are we using the API or generating on site?
      if ($this->params->get('useLDAPI')){

      return $this->place_request('ELD','3',"no=$number");
	  }else{

	// This is not as random as the method used by the API, but someone could still win!


	// Generate the numbers

	  $x = 1;

	// Enforce the 10 limit to avoid accidental DoS
	if ($number > 10){ $number = 10; }
	 while ($x <= $number){
	      
	      $a = 1;
	      $str = ".";
		      // Generate 6 numbers
			while ($a <= 5){
			    $no = rand(1,49);
			    if (strpos($str,".". $no . ".") == false){
			    $str .= $no;
			    if ($a != 5){ $str .= "."; }
			    $a++;
			    }
			    }

	      // Numbers generated, now add them to an object

	      $str = substr($str,1,strlen($str) - 1);
	      $balls = explode(".",$str);
	      sort($balls);
	      

	      $star1 = rand(1,11);
	      $star2 = rand(1,11);

	      while ($star1 == $star2){
	      $star2 = rand(1,11);
	      }

	      if ($star1 < $star2){

	    $balls[5] = "$star1";
	    $balls[6] = "$star2";
	    }else{
	    $balls[5] = "$star2";
	    $balls[6] = "$star1";
	    }


	      $LDs["dip$x"] = $balls;

	      $x++;
	      }
	return $LDs;

	}
      }





      function generateLottoluckydip($number){
      // Are we using the API or generating on site?
      if ($this->params->get('useLDAPI')){

      return $this->place_request('LD','1',"no=$number");
	  }else{

	// This is not as random as the method use by the API, but someone could still win!


	// Generate the numbers

	  $x = 1;

	// Enforce the 10 limit to avoid accidental DoS
	if ($number > 10){ $number = 10; }
	 while ($x <= $number){
	      
	      $a = 1;
	      $str = ".";
		      // Generate 6 numbers
			while ($a <= 6){
			    $no = rand(1,49);
			    if (strpos($str,".". $no . ".") == false){
			    $str .= $no;
			    if ($a != 6){ $str .= "."; }
			    $a++;
			    }
			    }

	      // Numbers generated, now add them to an object

	      $str = substr($str,1,strlen($str) - 1);
	      $balls = explode(".",$str);
	      sort($balls);

	      $LDs["dip$x"] = $balls;

	      $x++;
	      }
	return $LDs;

	}
      }


      function checklottoresults($month){

      $shortcache = $this->params->get('shortcache') * 60;
      $shortcache = $this->params->get('longcache') * 60;
       // Get the config object from factory
        $conf =& JFactory::getConfig();
        // Get the current cachetime value
        $cachetime = $conf->getValue('config.cachetime');
        
	$datecheck = strtotime($month."-01");
	$now = strtotime(date('Y-m')."-01");
	if ($datecheck < $now){
	// Looking at historic results, we can cache for much longer
        $conf->setValue('config.cachetime', $longcache);

	}else{
	// Set the cache time to 8 hours i.e. 8 * 60 mins
        $conf->setValue('config.cachetime', $shortcache);
	}



        // Get the Cache object
        $cache =& JFactory::getCache('mod_lotto_latest_results'.$month, 'output');
        // Enable caching (if disabled in global configuration)
        $cache->setCaching( 1 );
        // Try to get the results from cache


    if (!($json = $cache->get('mod_lotto_latest_results_json'.$month))) {
            $uri =& JFactory::getURI();
            
           
            if ($json = $this->place_request('retrieve','1',"month=$month")) {
                // Store the data in cache
                if (!$cache->store($json, 'mod_lotto_latest_results_json'.$month)) {
                    // If storing in cache failed then we will return the error
                    $error = 'cache';
                }
            } else {
                $error = 'Could not retrieve';
            }
        }

return $json;








      //return $this->place_request('retrieve','1',"month=$month");

      }


function getlatestLottoResults(){



$shortcache = $this->params->get('shortcache') * 60;
$str = "draws=" . $this->params->get('LottoResultsDrawDays');

    
        // Get the config object from factory
        $conf =& JFactory::getConfig();
        // Get the current cachetime value
        $cachetime = $conf->getValue('config.cachetime');
        // Set the cache time 
        $conf->setValue('config.cachetime', $shortcache);
        // Get the Cache object
        $cache =& JFactory::getCache('mod_lotto_latest_results', 'output');
        // Enable caching (if disabled in global configuration)
        $cache->setCaching( 1 );
        // Try to get the results from cache


    if (!($json = $cache->get('mod_lotto_latest_results_json'))) {
            $uri =& JFactory::getURI();
            
           
            if ($json = $this->place_request('LatestResults','1',$str)) {
                // Store the data in cache
                if (!$cache->store($json, 'mod_lotto_latest_results_json')) {
                    // If storing in cache failed then we will return the error
                    $error = 'cache';
                }
            } else {
                $error = 'Could not retrieve';
            }
        }
$conf->setValue('config.cachetime', $origcachetime);
return $json;

}



function getlatestEuroResults(){



$shortcache = $this->params->get('shortcache') * 60;
$str = "draws=" . $this->params->get('EuroResultsDrawDays');

    
        // Get the config object from factory
        $conf =& JFactory::getConfig();
        // Get the current cachetime value
        $cachetime = $conf->getValue('config.cachetime');
        // Set the cache time 
        $conf->setValue('config.cachetime', $shortcache);
        // Get the Cache object
        $cache =& JFactory::getCache('mod_lotto_latest_results', 'output');
        // Enable caching (if disabled in global configuration)
        $cache->setCaching( 1 );
        // Try to get the results from cache


    if (!($json = $cache->get('mod_lotto_latest_results_euro_json'))) {
            $uri =& JFactory::getURI();
            
           
            if ($json = $this->place_request('LatestResults','3',$str)) {
                // Store the data in cache
                if (!$cache->store($json, 'mod_lotto_latest_results_euro_json')) {
                    // If storing in cache failed then we will return the error
                    $error = 'cache';
                }
            } else {
                $error = 'Could not retrieve';
            }
        }
$conf->setValue('config.cachetime', $origcachetime);
return $json;

}






function getlatestTBResults(){

$str = "draws=" . $this->params->get('TBResultsDrawDays');

    $shortcache = $this->params->get('shortcache') * 60;
        // Get the config object from factory
        $conf =& JFactory::getConfig();
        // Get the current cachetime value
        $origcachetime = $conf->getValue('config.cachetime');
        // Set the cache time to 8 hours i.e. 6 * 60 mins
        $conf->setValue('config.cachetime', $shortcache);
        // Get the Cache object
        $cache =& JFactory::getCache('mod_lotto_latest_results', 'output');
        // Enable caching (if disabled in global configuration)
        $cache->setCaching( 1 );
        // Try to get the results from cache


    if (!($json = $cache->get('mod_lotto_latest_results_tb_json'))) {
            $uri =& JFactory::getURI();
            
           
            if ($json = $this->place_request('LatestResults','2',$str)) {
                // Store the data in cache
                if (!$cache->store($json, 'mod_lotto_latest_results_tb_json')) {
                    // If storing in cache failed then we will return the error
                    $error = 'cache';
                }
            } else {
                $error = 'Could not retrieve';
            }
        }
$conf->setValue('config.cachetime', $origcachetime);

return $json;

}


function place_request($action,$game,$additional = null){

// Cache time is sent so that abuse can be detected and prevented.
// Site admin will be notified in first instance if they've left cache set at 0

 $shortcache = $this->params->get('shortcache');
 $longcache = $this->params->get('shortcache');

unset($this->result);
// Check whether we can use File or if we need to resort to Curl
if (ini_get('allow_url_fopen')){
$results = $this->placefilerequest($action,$game,$additional."&shortcache=$shortcache&longcache=$longcache");
}else{
$results = $this->placecurlrequest($action,$game,$additional."&shortcache=$shortcache&longcache=$longcache");

}


// Process the response
$X=0;
foreach ($results as $result){

$result = json_decode($result);

$key = "row$X";
$this->result->$key = $result;
$X++;
}




return $this->result;
}








function placefilerequest($action,$game,$additional = null){
$key = $this->getAPIKey();
$results = file("http://api.bentasker.co.uk/lottopredict/?game=$game&action=$action&key=$key&$additional");

return $results;
}


function placecurlrequest($action,$game,$additional){
$key = $this->getAPIKey();
 $ch = curl_init("http://api.bentasker.co.uk/lottopredict/?game=$game&action=$action&key=$key&$additional");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    $data = curl_exec($ch);
 
    curl_close($ch);
$data = explode("\n",$data);

return $data;
}


}