/** * Check if DB schema is valid * * @param pointer $db Database class * @return string message * @todo Update list of versions */ function checkSchemaVersion(&$db) { $result = array('status' => tl::ERROR, 'msg' => null, 'kill_session' => true); $last_version = TL_LAST_DB_VERSION; $db_version_table = DB_TABLE_PREFIX . 'db_version'; $sql = "SELECT * FROM {$db_version_table} ORDER BY upgrade_ts DESC"; $res = $db->exec_query($sql, 1); if (!$res) { return $result['msg'] = "Failed to get Schema version from DB"; } $myrow = $db->fetch_array($res); $upgrade_msg = "You need to upgrade your Testlink Database to {$last_version} - <br>" . '<a href="./install/index.php" style="color: white">click here access install and upgrade page </a><br>'; $manualop_msg = "You need to proceed with Manual upgrade of your DB scheme to {$last_version} - Read README file!"; switch (trim($myrow['version'])) { case '1.7.0 Alpha': case '1.7.0 Beta 1': case '1.7.0 Beta 2': case '1.7.0 Beta 3': case '1.7.0 Beta 4': case '1.7.0 Beta 5': case '1.7.0 RC 2': case '1.7.0 RC 3': case 'DB 1.1': case 'DB 1.2': $result['msg'] = $upgrade_msg; break; case 'DB 1.3': case 'DB 1.4': case 'DB 1.5': case 'DB 1.6': case 'DB 1.9.8': $result['msg'] = $manualop_msg; break; case $last_version: $result['status'] = tl::OK; $result['kill_session'] = 'false'; break; default: $result['msg'] = "Unknown Schema version " . trim($myrow['version']) . ", please upgrade your Testlink Database to " . $last_version; break; } return $result; }
function drawXBarGraph($data) { // set data, style (fonts, etc) to be used (This must all be done before ANY GD functions) { // Data { $name = $data["meta"]["name"]; if ($data["meta"]["class"]) { $class = $data["meta"]["class"]; } else { $class = "sets"; } $keylist = $data["key"]; $imgheight = $data["meta"]["height"]; $imgwidth = $data["meta"]["width"]; $graphleft = 5; $graphright = $data["meta"]["width"] - 6; $graphtop = 30; $graphbottom = $data["meta"]["height"] - 50; $graphwidth = $graphright - $graphleft; $leftmargin = 0; $colours = $data["meta"]["colours"]; $topval = $data["meta"]["topval"]; $bottomval = $data["meta"]["bottomval"]; $cutoff = $data["meta"]["cutoff"]; $rows = $data["meta"]["rows"]; $barlist = $data["meta"]["barlist"]; $barwidth = $data["meta"]["barwidth"]; $bargap = $data["meta"]["bargap"]; $values = $data["values"]; $maxheight = 0; foreach ($values as $divkey => $divval) { foreach ($divval as $barkey => $barval) { if ($barval > $maxheight) { $maxheight = $barval; } } } if ($data["pointer"]) { $pointer = $data["pointer"]; } $settotal = count($values) + 1; // The number of lines, + 1 so a row set is not presented at the final coordinate of x; $valrange = $topval - $bottomval; // The range of values, as from least (bottom of graph) to most (top of graph) $ratio = ($graphbottom - $graphtop) / $valrange; $divinc = round(($graphright - $graphleft) / $settotal, 2); // The pixel increment between each division of $baselinevalues; // } // Font size and header placement{ $font = BASE . "/basefunctions/fonts/verdana.ttf"; if ($graphwidth < 200) { $linefontsize = 6; $titlesize = 7; $subtitlesize = 6; $headery = 8; } else { if ($graphwidth < 450) { $linefontsize = 7; $titlesize = 10; $subtitlesize = 7; $headery = 11; } else { $linefontsize = 8; $titlesize = 12; $subtitlesize = 10; $headery = 15; } } // } // } // create image { $im = imagecreate($imgwidth + 1, $imgheight + 1); // } // Colours { $white = imagecolorallocate($im, 255, 255, 255); //$bg = imagecolorallocate($im, hexdec('0x' . $colour{0} . $colour{1}), hexdec('0x' . $colour{2} . $colour{3}), hexdec('0x' . $colour{4} . $colour{5})); $imgbacksource = MAXINETOP; $graphbacksource = MAXINEBACK; $imgback = imagecolorallocate($im, hexdec('0x' . $imgbacksource[1] . $imgbacksource[2]), hexdec('0x' . $imgbacksource[3] . $imgbacksource[4]), hexdec('0x' . $imgbacksource[5] . $imgbacksource[6])); $graphback = imagecolorallocate($im, hexdec('0x' . $graphbacksource[1] . $graphbacksource[2]), hexdec('0x' . $graphbacksource[3] . $graphbacksource[4]), hexdec('0x' . $graphbacksource[5] . $graphbacksource[6])); $black = imagecolorallocate($im, 0x0, 0x0, 0x0); $red = imagecolorallocate($im, 255, 0, 0); $yellow = imagecolorallocate($im, 255, 255, 0); $lime = imagecolorallocate($im, 195, 245, 90); $green = imagecolorallocate($im, 40, 150, 40); $blue = imagecolorallocate($im, 90, 90, 255); $barcolor = imagecolorallocate($im, 0xff, 0x0, 0x0); // Fore colour $colourcount = 1; if ($colours) { foreach ($colours as $colourkey => $colourval) { $colourlist[$colourkey] = imagecolorallocate($im, hexdec('0x' . $colourval[0] . $colourval[1]), hexdec('0x' . $colourval[2] . $colourval[3]), hexdec('0x' . $colourval[4] . $colourval[5])); $colourcount++; } } else { $colourlist = array("0" => $green, "1" => $yellow, "2" => $red, "3" => $blue, "4" => $lime); } // } // draw Image backdrop { //imagerectangle($im, 0,0,$imgwidth+1,$imgheight+1,$black); imageline($im, 15, 0, $imgwidth - 15, 0, $black); // Top imageline($im, $leftmargin, 15, $leftmargin, $imgheight - 10, $black); // Left imageline($im, $imgwidth, 15, $imgwidth, $imgheight - 15, $black); // Right imageline($im, 10, $imgheight, $imgwidth - 15, $imgheight, $black); // Bottom imagearc($im, 14, 14, 30, 30, 181, 269, $black); // Top left imagearc($im, $imgwidth - 14, 14, 30, 30, 271, 359, $black); // Top right imagearc($im, 14, $imgheight - 14, 30, 30, 91, 179, $black); // Bottom left imagearc($im, $imgwidth - 14, $imgheight - 14, 30, 30, 1, 89, $black); // Bottom right // imagearc(resource, x, y, imgheight, imgwidth, degree start, degree end, colour) //imagearc($im, 40, 40, 30, 20, 91, 179, $black); // Bottom left imagefill($im, 20, 20, $imgback); // } // Draw Graph Backdrop{ //imagerectangle($im, 0,0,$graphwidth+1,$graphheight+1,$black); imageline($im, $graphleft + 5, $graphtop, $graphright - 5, $graphtop, $black); // Top imageline($im, $graphleft, $graphtop + 5, $graphleft, $graphbottom - 5, $black); // Left imageline($im, $graphright, $graphtop + 5, $graphright, $graphbottom - 5, $black); // Right imageline($im, 10, $graphbottom, $graphright - 5, $graphbottom, $black); // Bottom imagearc($im, $graphleft + 4, $graphtop + 4, 10, 10, 181, 269, $black); // Top left imagearc($im, $graphright - 4, $graphtop + 4, 10, 10, 271, 359, $black); // Top right imagearc($im, $graphleft + 4, $graphbottom - 4, 10, 10, 91, 179, $black); // Bottom left imagearc($im, $graphright - 4, $graphbottom - 4, 10, 10, 1, 89, $black); // Bottom Right // imagearc(resource, x, y, graphheight, graphwidth, degree start, degree end, colour) imagefill($im, $graphleft + 5, $graphtop + 5, $graphback); // } // Draw dividing lines { $valinc = ($topval - $bottomval) / $rows; // The value increment between each line; $pixinc = ($graphbottom - $graphtop) / $rows; // The pixel increment between each line; //imagettftext($im, $linefontsize, 0, 350, 80, $black, $font, "Rows : ".$rows.", Valinc : ".$valinc.", Pixinc : ".$pixinc); for ($linecount = 0; $linecount < $rows; $linecount++) { $linepos = $graphbottom - $linecount * $pixinc; $linetext = $linecount * $valinc + $bottomval; imagettftext($im, $linefontsize, 0, $graphleft + 2, $linepos - 1, $black, $font, $linetext); if ($linecount != 0) { imageline($im, $graphleft, $linepos, $graphright, $linepos, $black); } } // } // draw the bars { if ($class == "sets") { $setcount = 1; foreach ($values as $setkey => $setval) { // $fallback is the calculated distance back from the X point so that the set is centered around it. $bartotal = count($setval); $fallback = floor($bartotal / 2); $fallback = $fallback * $barwidth; $xpos = $setcount * $divinc - $fallback; $barcount = 1; foreach ($barlist as $listkey => $listval) { if ($setval[$listval]) { $barheight = $graphbottom - ($setval[$listval] - $bottomval) * $ratio; //imagettftext($im, $linefontsize, 0, $xpos, $barheight, $black, $font, $setval[$listval]); imagefilledrectangle($im, $xpos, $barheight, $xpos + $barwidth, $graphbottom - 1, $colourlist[$listkey]); $barcount++; $xpos += $bargap; } } imagettftext($im, $linefontsize, 90, $xpos + 10, $graphbottom - 1, $black, $font, $setkey); $setcount++; } } else { if ($class == "span") { $xpos = 50; $count = 1; foreach ($values as $setkey => $setval) { foreach ($setval as $barkey => $barval) { if ($barval < $topval) { $barheight = $graphbottom - ($barval - $bottomval) * $ratio; } else { $barheight = $graphtop; } imagefilledrectangle($im, $xpos, $barheight, $xpos + $barwidth, $graphbottom - 1, $colourlist[$barkey]); imagerectangle($im, $xpos, $barheight, $xpos + $barwidth, $graphbottom, $black); $xpos += $bargap; if ($pointer == $count) { $myclass = new pointer(); $pointerx = $xpos - floor($barwidth / 2); $myclass->downPointer($im, $pointerx, $barheight, $colourlist[$barkey]); } } $count++; } } else { if ($class == "comparison") { $setcount = 1; foreach ($values as $setkey => $setval) { // $fallback is the calculated distance back from the X point so that the set is centered around it. $bartotal = count($setval); $fallback = floor($bartotal / 2); $fallback = $fallback * $barwidth; $xpos = $setcount * $divinc - $fallback; $barcount = 1; foreach ($barlist as $listkey => $listval) { if ($setval[$listval]) { $barheight = $graphbottom - ($setval[$listval] - $bottomval) * $ratio; imagefilledrectangle($im, $xpos, $barheight, $xpos + $barwidth, $graphbottom - 1, $colourlist[$setcount]); $barcount++; $xpos += $bargap; } } imagettftext($im, $linefontsize, 90, $xpos + 10, $graphbottom - 1, $black, $font, $keylist[$setkey][1]); $setcount++; } } } } // } // Draw the Cutoff line, if required { if ($cutoff) { $cutoffy = $graphbottom - $cutoff * $ratio; imageline($im, $graphleft, $cutoffy, $graphright, $cutoffy, $red); } // } // Titling { $title = $data["meta"]["title"]; $subtitle = $data["meta"]["subtitle"]; $lettercount = strlen($title); $titlex = $imgwidth / 2 - $lettercount * 4; imagettftext($im, $titlesize, 0, $titlex, $headery, $black, $font, $title); $lettercount = strlen($subtitle); $titlex = $imgwidth / 2 - $lettercount * 3; imagettftext($im, $subtitlesize, 0, $titlex, $headery + $titlesize, $black, $font, $subtitle); // } // Save image to directory for program to access { /* header("content-type: image/png"); imagepng($im); imagedestroy($im); */ ImagePng($im, BASE . "/images/graphs/" . $name . ".png"); // } }
/** * does the DB migration * @param pointer $db * @param string $pf db_prefix */ private function migrate($db, $pf) { set_time_limit(0); $db_config = core::config('database.default'); $prefix = $db_config['table_prefix']; //connect DB original/to where we migrate $dbo = Database::instance('default'); //oc_accounts --> oc_users $users_map = array(); $accounts = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'accounts`'); foreach ($accounts as $account) { $user = new Model_User(); $user->where('email', '=', $account['email'])->limit(1)->find(); if (!$user->loaded()) { $user->name = $account['name']; $user->email = $account['email']; $user->password = $account['password']; $user->created = $account['createdDate']; $user->last_modified = $account['lastModifiedDate']; $user->last_login = $account['lastSigninDate']; $user->status = $account['active']; $user->id_role = 1; $user->seoname = $user->gen_seo_title($user->name); $user->save(); } $users_map[$account['email']] = $user->id_user; } //categories --> categories $categories_map = array(0 => 1); $categories = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'categories` ORDER BY `idCategoryParent` ASC'); foreach ($categories as $category) { $cat = new Model_Category(); $cat->name = $category['name']; $cat->order = $category['order']; $cat->created = $category['created']; $cat->seoname = $category['friendlyName']; $cat->price = $category['price']; $cat->description = substr($category['description'], 0, 250); $cat->parent_deep = $category['idCategoryParent'] > 0 ? 1 : 0; //there's only 1 deep $cat->id_category_parent = isset($categories_map[$category['idCategoryParent']]) ? $categories_map[$category['idCategoryParent']] : 1; $cat->save(); //we save old_id stores the new ID, so later we know the category parent, and to changes the ADS category id $categories_map[$category['idCategory']] = $cat->id_category; } //locations --> locations $locations_map = array(0 => 1); $locations = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'locations` ORDER BY `idLocationParent` ASC'); foreach ($locations as $location) { $loc = new Model_Location(); $loc->name = $location['name']; $loc->seoname = $location['friendlyName']; $loc->parent_deep = $location['idLocationParent'] > 0 ? 1 : 0; //there's only 1 deep $loc->id_location_parent = isset($locations_map[$location['idLocationParent']]) ? $locations_map[$location['idLocationParent']] : 1; $loc->save(); //we save old_id stores the new ID, so later we know the location parent, and to changes the ADS location id $locations_map[$location['idLocation']] = $loc->id_location; } //posts --> ads $ads_map = array(); $ads = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'posts`'); foreach ($ads as $a) { if (Valid::email($a['email'])) { //gettin the id_user if (isset($users_map[$a['email']])) { $id_user = $users_map[$a['email']]; } else { $user = Model_User::create_email($a['email'], $a['name']); $id_user = $user->id_user; } $ad = new Model_Ad(); $ad->id_ad = $a['idPost']; //so images still work $ad->id_user = $id_user; $ad->id_category = isset($categories_map[$a['idCategory']]) ? $categories_map[$a['idCategory']] : 1; $ad->id_location = isset($locations_map[$a['idLocation']]) ? $locations_map[$a['idLocation']] : 1; $ad->title = $a['title']; $ad->seotitle = $ad->gen_seo_title($a['title']); $ad->description = !empty($a['description']) ? Text::html2bb($a['description']) : $a['title']; $ad->address = $a['place']; $ad->price = $a['price']; $ad->phone = $a['phone']; $ad->has_images = $a['hasImages']; $ad->ip_address = ip2long($a['ip']); $ad->created = $a['insertDate']; $ad->published = $ad->created; //Status migration...big mess! if ($a['isAvailable'] == 0 and $a['isConfirmed'] == 0) { $ad->status = Model_Ad::STATUS_NOPUBLISHED; } elseif ($a['isAvailable'] == 1 and $a['isConfirmed'] == 0) { $ad->status = Model_Ad::STATUS_NOPUBLISHED; } elseif ($a['isAvailable'] == 1 and $a['isConfirmed'] == 1) { $ad->status = Model_Ad::STATUS_PUBLISHED; } elseif ($a['isAvailable'] == 0 and $a['isConfirmed'] == 1) { $ad->status = Model_Ad::STATUS_UNAVAILABLE; } elseif ($a['isAvailable'] == 2) { $ad->status = Model_Ad::STATUS_SPAM; } else { $ad->status = Model_Ad::STATUS_UNAVAILABLE; } try { $ad->save(); } catch (ORM_Validation_Exception $e) { // d($e->errors('')); } $ads_map[$a['idPost']] = $ad->id_ad; } } //posthits --> visits, mass migration $insert = 'INSERT INTO `' . $prefix . 'visits` ( `id_ad`, `created`, `ip_address`) VALUES'; $step = 5000; $total = $db->query(Database::SELECT, 'SELECT count(*) cont FROM `' . $pf . 'postshits`')->as_array(); $total = $total[0]['cont']; for ($i = 0; $i < $total; $i += $step) { $hits = $db->query(Database::SELECT, 'SELECT * FROM `' . $pf . 'postshits` LIMIT ' . $i . ', ' . $step); $values = ''; foreach ($hits as $hit) { //build insert query $values .= '(' . $hit['idPost'] . ', \'' . $hit['hitTime'] . '\', \'' . ip2long($hit['ip']) . '\'),'; } $dbo->query(Database::INSERT, $insert . substr($values, 0, -1)); } //old way of migrating // $hits = $db->query(Database::SELECT, 'SELECT * FROM `'.$pf.'postshits` '); // foreach ($hits as $hit) // { // //build insert query // $visit = new Model_Visit(); // $visit->id_ad = (isset($ads_map[$hit['idPost']]))?$ads_map[$hit['idPost']]:NULL; // $visit->created = $hit['hitTime']; // $visit->ip_address = ip2long($hit['ip']); // $visit->save(); // } }
/** * Check if DB schema is valid * * @param pointer $db Database class * @return string message * @todo Update list of versions */ function checkSchemaVersion(&$db) { $last_version = 'DB 1.3'; // havlatm: updated for 1.9 $db_version_table = DB_TABLE_PREFIX . 'db_version'; $sql = "SELECT * FROM {$db_version_table} ORDER BY upgrade_ts DESC"; $res = $db->exec_query($sql, 1); if (!$res) { return $msg = "Failed to get Schema version from DB"; } $myrow = $db->fetch_array($res); $msg = ""; switch (trim($myrow['version'])) { case '1.7.0 Alpha': case '1.7.0 Beta 1': case '1.7.0 Beta 2': case '1.7.0 Beta 3': case '1.7.0 Beta 4': case '1.7.0 Beta 5': case '1.7.0 RC 2': case '1.7.0 RC 3': case 'DB 1.1': case 'DB 1.2': $msg = "You need to upgrade your Testlink Database to {$last_version} - <br>" . '<a href="SCHEMA_CHANGES" style="color: white"> click here to see the Schema changes </a><br>' . '<a href="./install/index.php" style="color: white">click here access install and upgrade page </a><br>'; break; case $last_version: break; default: $msg = "Unknown Schema version " . trim($myrow['version']) . ", please upgrade your Testlink Database to " . $last_version; break; } return $msg; }