-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
114 lines (88 loc) · 2.54 KB
/
index.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
<?php
define( 'DEBUG_MODE', false );
define( 'LOCAL_MODE', false );
define( 'PIX_URL', '127.0.0.1' );
define( 'PIX_PATH', '/rest/' );
define( 'CL_URL', 'www.colourlovers.com' );
global $cl_palette_paths;
$cl_palette_paths = array(
'base' => '/api/palettes',
'top' => '/api/palettes/top',
'new' => '/api/palettes/new',
'random' => '/api/palettes/random'
);
define( 'MAX_PALETTES', 5 );
define( 'MAX_COLOURS', 5 );
define( 'MAX_IMAGES', 5 );
require_once('HttpClient.class.php');
require_once('utils.php');
require_once('view.utils.php');
function get_palettes( $type = 'top' ) {
if( !LOCAL_MODE ) {
$client = new HttpClient( CL_URL );
$type_path = get_palette_type_path( $type );
$client->get( $type_path );
$xml = $client->getContent();
} else {
$xml = file_get_contents('top.xml');
}
$result = parse_xml($xml);
out('get_palettes result', $result);
if( !count( $result ) )
return false;
if( isset( $result['palette']['id'] ) )
return array( $result['palette'] );
else
return $result['palette'];
}
function get_palette_type_path( $type ) {
global $cl_palette_paths;
if( isset( $cl_palette_paths[$type] ) )
return $cl_palette_paths[$type];
else
return $cl_palette_paths['base'] . '?lover=' . urlencode( $type );
}
function get_images( $colours ) {
if( !is_array( $colours ) ) $colours = array( $colours );
$client = new HttpClient( PIX_URL );
if( DEBUG_MODE ) $client->setDebug(true);
$data = array('method' => 'color_search');
$colours_data = build_colors_data_array( $colours );
$data = array_merge($data, $colours_data);
out( 'get_images data:', $data );
$client->get( PIX_PATH, $data);
$result = $client->getContent();
$result_encoded = json_decode( $result );
return isset( $result_encoded->result ) ? $result_encoded->result : array();
}
function get_image_url( $filepath ) {
return 'http://' . PIX_URL . '/collection/?filepath=' . $filepath;
}
function build_colors_data_array( $colours ) {
$colours_data = array();
for( $i = 0; $i < count($colours); $i++ ) {
$colour = $colours[$i];
$colours_data['colors[' . $i . ']'] = $colour;
}
return $colours_data;
}
if( isset($_REQUEST['doing_ajax']) ) {
$action = $_REQUEST['action'];
switch( $action ) {
case 'palettes':
$type = $_REQUEST['type'];
$palettes = get_palettes( $type );
require_once('views/view.palettes.php');
break;
case 'images':
$match_all = $_REQUEST['match_all'];
$colours = $_REQUEST['colours'];
require_once('views/views.images.php');
break;
default:
return;
}
} else {
require_once('view.php');
}
?>