A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. if(!defined('entry') || !entry) die('Not a valid page');
  3. /* ===========================
  4. gelato CMS - A PHP based tumblelog CMS
  5. development version
  6. http://www.gelatocms.com/
  7. gelato CMS is a free software licensed under the GPL 2.0
  8. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  9. =========================== */
  10. ?>
  11. <?php
  12. function version() {
  13. return "0.95";
  14. }
  15. function codeName() {
  16. return "cioccolato RC1";
  17. }
  18. function beginsWith($str, $sub) {
  19. return (strpos($str, $sub) === 0);
  20. }
  21. function endsWith($str, $sub) {
  22. return (substr($str, strlen($str) - strlen($sub)) == $sub);
  23. }
  24. function getFileName($fileUrl) {
  25. $path = explode('/', $fileUrl);
  26. return $path[count($path)-1];
  27. }
  28. function isMP3($fileUrl) {
  29. if (endsWith($fileUrl, ".mp3")) {
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. }
  35. function getMP3File($remoteFileName) {
  36. if (isMP3($remoteFileName)) {
  37. if (getFile($remoteFileName)) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. } elseif (isGoEar($remoteFileName)) {
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. }
  48. function getGoEarCode($songUrl) {
  49. $pos = strpos($songUrl, "?v=");
  50. $lon = strlen($songUrl);
  51. $str = substr($songUrl, $pos + 3, $lon);
  52. return $str;
  53. }
  54. function isGoEar($songUrl) {
  55. if (beginsWith($songUrl, "http://www.goear.com/listen.php?v="))
  56. return true;
  57. else
  58. return false;
  59. }
  60. function isImageFile($photoUrl) {
  61. if (endsWith($photoUrl, ".jpg")) { return true; }
  62. elseif (endsWith($photoUrl, ".gif")) { return true; }
  63. elseif (endsWith($photoUrl, ".png")) { return true; }
  64. else { return false; }
  65. }
  66. function getPhotoFile($remoteFileName) {
  67. if (isImageFile($remoteFileName)) {
  68. if (getFile($remoteFileName)) {
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. } else {
  74. return false;
  75. }
  76. }
  77. function getFile($remoteFileName) {
  78. $fileName = "../uploads/".sanitizeName(getFileName($remoteFileName));
  79. $str = _file_get_contents($remoteFileName);
  80. if (!$handle = fopen($fileName, 'w')) {
  81. //die("no se abrio de escritura");
  82. return false;
  83. }
  84. if (fwrite($handle, $str) === FALSE) {
  85. //die("no se escribio");
  86. return false;
  87. }
  88. fclose($handle);
  89. return true;
  90. }
  91. function isVimeoVideo($videoUrl) {
  92. if (beginsWith($videoUrl, "http://vimeo.com/clip:") || beginsWith($videoUrl, "http://www.vimeo.com/clip:"))
  93. return true;
  94. else
  95. return false;
  96. }
  97. function getVimeoVideoUrl($videoUrl) {
  98. return array_pop(explode("clip:",$videoUrl));
  99. }
  100. function isYoutubeVideo($videoUrl) {
  101. if (beginsWith($videoUrl, "http://youtube.com/watch?v=") || beginsWith($videoUrl, "http://www.youtube.com/watch?v="))
  102. return true;
  103. else
  104. return false;
  105. }
  106. function getYoutubeVideoUrl($videoUrl) {
  107. $params = explode("?v=", $videoUrl);
  108. $params2 = explode("&",$params[1]);
  109. return $params2[0];
  110. }
  111. function isVideo($url) {
  112. if (isYoutubeVideo($url)) { return true; }
  113. elseif (isVimeoVideo($url)) { return true; }
  114. else { return false; }
  115. }
  116. function sendMail($to, $title, $body, $from) {
  117. $rp = trim($from);
  118. $org = "gelato CMS";
  119. $mailer = "gelato CMS Mailer";
  120. $head = '';
  121. $head .= "Content-Type: text/html \r\n";
  122. $head .= "Date: ". date('r'). " \r\n";
  123. $head .= "Return-Path: $rp \r\n";
  124. $head .= "From: $from \r\n";
  125. $head .= "Sender: $from \r\n";
  126. $head .= "Reply-To: $from \r\n";
  127. $head .= "Organization: $org \r\n";
  128. $head .= "X-Sender: $from \r\n";
  129. $head .= "X-Priority: 3 \r\n";
  130. $head .= "X-Mailer: $mailer \r\n";
  131. $body = str_replace("\r\n", "\n", $body);
  132. $body = str_replace("\n", "\r\n", $body);
  133. return @mail($to, $title, $body, $head);
  134. }
  135. function getThemes() {
  136. $themes_dir = "themes";
  137. $dirs = array();
  138. $path = getcwd();
  139. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$themes_dir : $path."/".$themes_dir;
  140. $dir = str_replace("admin\\", "", $dir);
  141. $dir = str_replace("admin/", "", $dir);
  142. $handle = opendir($dir);
  143. $i=0;
  144. while($filename = readdir($handle)) {
  145. if($filename != "." && $filename != "..") {
  146. $dirs[$i]=trim($filename);
  147. $i++;
  148. }
  149. }
  150. closedir($handle);
  151. return $dirs;
  152. }
  153. function sanitizeName($name) {
  154. $name = preg_replace('/[\'"]/', '', $name);
  155. $name = preg_replace('/[^a-zA-Z0-9]+/', '-', $name);
  156. $name = trim($name, '-');
  157. $name = strtolower($name);
  158. //HACK: We need to rework the regular expression to allow the dot
  159. $ext = substr($name, strlen($name)-3, strlen($name));
  160. $body = substr($name, 0, strlen($name)-4);
  161. $name = $body.".".$ext;
  162. return $name;
  163. }
  164. function _file_get_contents($path) {
  165. // Modified function from:
  166. // http://work.dokoku.net/Anieto2k/_file_get_contents.phps
  167. // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
  168. if (!preg_match('/^http/i', $path)) {
  169. if ($fp = fopen($path, 'r')) {
  170. return fread($fp, 1024);
  171. } else {
  172. return false;
  173. }
  174. } else {
  175. if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
  176. $ch = curl_init();
  177. $timeout = 5;
  178. curl_setopt ($ch, CURLOPT_URL, $path);
  179. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  180. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  181. $file_contents = curl_exec($ch);
  182. curl_close($ch);
  183. if (is_string($file_contents)) {
  184. return $file_contents;
  185. } else {
  186. return false;
  187. }
  188. } else {
  189. $data = parse_url($path);
  190. if (!$data['host'] || $data['scheme'] != "http") {
  191. return false;
  192. }
  193. $f = @fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
  194. if (!$f) {
  195. return false;
  196. }
  197. $q = "GET " . $data['path'] . (isset($data['query'])?'?'.$data['query']:'') . " HTTP/1.1\r\n";
  198. $q .= "Host: " . $data['host'] . "\r\n";
  199. $q .= "Connection: close\r\n";
  200. $q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
  201. $recv = "";
  202. fwrite($f, $q);
  203. while (!feof($f)) {
  204. $recv .= fread($f, 1024);
  205. }
  206. $request = $q;
  207. $response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
  208. $body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
  209. if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
  210. if ($res[1][0] != "2") {
  211. return false;
  212. }
  213. } else {
  214. return false;
  215. }
  216. if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
  217. $tmp_body = $body;
  218. $new = "";
  219. $exit = false;
  220. while (!$exit) {
  221. if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
  222. $len = hexdec($res[1]);
  223. if ($len == "0") {
  224. $exit = true;
  225. break;
  226. }
  227. $new .= substr($tmp_body, strlen($res[0]), $len);
  228. $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
  229. } else {
  230. $exit = true;
  231. }
  232. }
  233. $body = $new;
  234. }
  235. return $body;
  236. }
  237. }
  238. }
  239. function get_curl_version() {
  240. $curl = 0;
  241. if (is_array(curl_version())) {
  242. $curl = curl_version();
  243. $curl = $curl['version'];
  244. } else {
  245. $curl = curl_version();
  246. $curl = explode(' ', $curl);
  247. $curl = explode('/', $curl[0]);
  248. $curl = $curl[1];
  249. }
  250. return $curl;
  251. }
  252. function transform_offset($offset){
  253. $sp = strpos($offset , ".")? explode("." , $offset) : false;
  254. if(is_array($sp)){
  255. $minutes = strval($sp[1]);
  256. $off_h = $sp[0]*3600;
  257. $off_m = (($minutes*60)/100)*60;
  258. $off = $off_h+$off_m;
  259. } else {
  260. $off = ($offset*3600);
  261. }
  262. return $off;
  263. }
  264. function getLangs() {
  265. $langs_dir = "languages";
  266. $dirs = array();
  267. $path = getcwd();
  268. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir : $path."/".$langs_dir;
  269. $dir = str_replace("admin\\", "", $dir);
  270. $dir = str_replace("admin/", "", $dir);
  271. $i=0;
  272. $cls_lang_dir = @ dir($dir);
  273. while (($directory = $cls_lang_dir->read()) !== false) {
  274. if($directory != "." && $directory != "..") {
  275. $dir2 = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir."\\".$directory : $path."/".$langs_dir."/".$directory;
  276. $dir2 = str_replace("admin\\", "", $dir2);
  277. $dir2 = str_replace("admin/", "", $dir2);
  278. if(is_dir($dir2)){
  279. $cls_lang_dir2 = @ dir($dir2);
  280. while (($directory2 = $cls_lang_dir2->read()) !== false) {
  281. if($directory2 != "." && $directory2 != "..") {
  282. if (preg_match('|^\.+$|', $directory2)){
  283. continue;
  284. }
  285. if (preg_match('|\.mo$|', $directory2)){
  286. if(!in_array($directory2,$dirs)){
  287. $dirs[$i]=trim($directory);
  288. $i++;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296. $dirs = array_unique($dirs);
  297. return $dirs;
  298. }
  299. function sql_escape($value) {
  300. if(get_magic_quotes_gpc()) {
  301. $value = stripslashes($value);
  302. }
  303. if( function_exists("mysql_real_escape_string")) {
  304. $value = mysql_real_escape_string($value);
  305. } else {
  306. $value = addslashes($value);
  307. }
  308. return $value;
  309. }
  310. ?>