/
generate.php
162 lines (142 loc) · 6.07 KB
/
generate.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
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$versionPath = "./variables/";
$sourceAdsPath = "./source/";
$generatedVersionsPath = "./generated/";
if (!isCLI()){
$url = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$url);
$dir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++) {
$dir .= $parts[$i] . "/";
}
$url = "http://".$dir;
} else {
$url = '';
}
@mkdir($generatedVersionsPath);
@mkdir($sourceAdsPath);
@mkdir($versionPath);
//Read All Versions into arrays
@$versionFiles = array_splice(scandir($versionPath), 2);
@$sourceFiles = array_splice(scandir($sourceAdsPath), 2);
//Run Through All Version Files and Create Array For Referance
foreach ($versionFiles as $currentVersionFile) {
$VersionAdName = str_replace(".json", "", $currentVersionFile);
$currentVersionFileContent = file_get_contents($versionPath . $currentVersionFile);
$currentVersionArray = (array) json_decode($currentVersionFileContent, true);
foreach ($currentVersionArray as $AdVersion) {
$currentLang = $AdVersion['version'];
$version[$VersionAdName][$currentLang] = array();
$version[$VersionAdName][$currentLang] = $AdVersion;
}
}
foreach ($sourceFiles as $sourceAdName) {
foreach ($version[$sourceAdName] as $currentVersionName => $currentVersionVars) {
echo "Ad: ".$sourceAdName . " | Version:" . $currentVersionName . " - Started\n<br>";
//Create Directory for Current Version
@mkdir($generatedVersionsPath . $sourceAdName);
@$sourceAdFiles = array_splice(scandir($sourceAdsPath . "/" . $sourceAdName), 2);
//Remove Gross Files that we dont want
if (($key = array_search("banner.jpg", $sourceAdFiles)) !== false) {
unset($sourceAdFiles[$key]);
}
if (($key = array_search("banner.png", $sourceAdFiles)) !== false) {
unset($sourceAdFiles[$key]);
}
if (($key = array_search(".DS_Store", $sourceAdFiles)) !== false) {
unset($sourceAdFiles[$key]);
}
foreach ($sourceAdFiles as $sourceAdSize) {
//Generate and Create Path to current ad/version version
$currentAdCreationPath = $generatedVersionsPath . $sourceAdName . "/" . $currentVersionName;
@mkdir($currentAdCreationPath);
$currentAdCreationPath = $generatedVersionsPath . $sourceAdName . "/" . $currentVersionName . "/" . $sourceAdSize . "/";
@mkdir($currentAdCreationPath);
//Copy Source ad Files to New Lanugage Directory
recurse_copy($sourceAdsPath . $sourceAdName . "/" . $sourceAdSize . "/", $currentAdCreationPath);
$varFile = '';
foreach ($currentVersionVars as $varName => $varValue) {
if ($varName == "clickTag") {
//Lets do something else for clickTag
$currentIndexFileSrc = file_get_contents($currentAdCreationPath . "index.html");
$currentIndexFileSrc = str_replace("[clickTag]", $varValue, $currentIndexFileSrc);
file_put_contents($currentAdCreationPath . "index.html", $currentIndexFileSrc);
} else {
$varFile .= "var " . $varName . " = '" . str_replace("'", "\'", $varValue) . "';\n";
}
}
//Put vars file in root ad directory
file_put_contents($currentAdCreationPath . "/vars.js", $varFile);
@mkdir($generatedVersionsPath . $sourceAdName . "/zips");
zip_directory($sourceAdName . "-" . $currentVersionName . "-" . $sourceAdSize, $currentAdCreationPath, $generatedVersionsPath . $sourceAdName . "/zips");
if (!isCLI()){
echo $sourceAdSize." Link: <a target='_blank' href='".$url.str_replace("./","",$currentAdCreationPath)."'>".$url.str_replace("./","",$currentAdCreationPath)."</a><br>";
}
}
echo "Finished.<br><br>\n\n";
//Let screen know what was finished.
}
}
echo "Finished.\n";
/* UTILITIE FUNCTIONS !!!!DO NOT CHANGE!!!! */
function recurse_copy($src, $dst)
{
$dir = opendir($src);
@mkdir($dst);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
recurse_copy($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
}
closedir($dir);
}
function csv_to_array($filename = '', $delimiter = ',')
{
if (!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE) {
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
if (!$header) {
$header = $row;
} else {
$data[] = array_combine($header, $row);
}
}
fclose($handle);
}
return $data;
}
function isCLI()
{
return (php_sapi_name() === 'cli');
}
function zip_directory($zipName, $path, $dest)
{
$rootPath = $path;
// Initialize archive object
$zip = new ZipArchive();
$zip->open($dest . "/" . $zipName . ".zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file) {
// Skip directories (they would be added automatically)
if (!$file->isDir()) {
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
}